Aurelien Jarno | 7b9cbad | 2010-03-14 23:30:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU MIPS timer support |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | * THE SOFTWARE. |
| 21 | */ |
| 22 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 23 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 24 | #include "hw/mips/cpudevs.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 25 | #include "qemu/timer.h" |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 26 | |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 27 | #define TIMER_FREQ 100 * 1000 * 1000 |
| 28 | |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 29 | /* XXX: do not use a global */ |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 30 | uint32_t cpu_mips_get_random (CPUMIPSState *env) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 31 | { |
aurel32 | 59d9413 | 2009-01-08 18:48:12 +0000 | [diff] [blame] | 32 | static uint32_t lfsr = 1; |
| 33 | static uint32_t prev_idx = 0; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 34 | uint32_t idx; |
aurel32 | 59d9413 | 2009-01-08 18:48:12 +0000 | [diff] [blame] | 35 | /* Don't return same value twice, so get another value */ |
| 36 | do { |
| 37 | lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xd0000001u); |
| 38 | idx = lfsr % (env->tlb->nb_tlb - env->CP0_Wired) + env->CP0_Wired; |
| 39 | } while (idx == prev_idx); |
| 40 | prev_idx = idx; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 41 | return idx; |
| 42 | } |
| 43 | |
| 44 | /* MIPS R4K timer */ |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 45 | static void cpu_mips_timer_update(CPUMIPSState *env) |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 46 | { |
| 47 | uint64_t now, next; |
| 48 | uint32_t wait; |
| 49 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 50 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 51 | wait = env->CP0_Compare - env->CP0_Count - |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 52 | (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec()); |
| 53 | next = now + muldiv64(wait, get_ticks_per_sec(), TIMER_FREQ); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 54 | timer_mod(env->timer, next); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 57 | /* Expire the timer. */ |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 58 | static void cpu_mips_timer_expire(CPUMIPSState *env) |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 59 | { |
| 60 | cpu_mips_timer_update(env); |
| 61 | if (env->insn_flags & ISA_MIPS32R2) { |
| 62 | env->CP0_Cause |= 1 << CP0Ca_TI; |
| 63 | } |
| 64 | qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]); |
| 65 | } |
| 66 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 67 | uint32_t cpu_mips_get_count (CPUMIPSState *env) |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 68 | { |
| 69 | if (env->CP0_Cause & (1 << CP0Ca_DC)) { |
| 70 | return env->CP0_Count; |
| 71 | } else { |
Edgar E. Iglesias | e027e1f | 2011-01-18 00:12:22 +0100 | [diff] [blame] | 72 | uint64_t now; |
| 73 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 74 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 75 | if (timer_pending(env->timer) |
| 76 | && timer_expired(env->timer, now)) { |
Edgar E. Iglesias | e027e1f | 2011-01-18 00:12:22 +0100 | [diff] [blame] | 77 | /* The timer has already expired. */ |
| 78 | cpu_mips_timer_expire(env); |
| 79 | } |
| 80 | |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 81 | return env->CP0_Count + |
Edgar E. Iglesias | e027e1f | 2011-01-18 00:12:22 +0100 | [diff] [blame] | 82 | (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec()); |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 86 | void cpu_mips_store_count (CPUMIPSState *env, uint32_t count) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 87 | { |
ths | 3529b53 | 2007-04-05 23:17:40 +0000 | [diff] [blame] | 88 | if (env->CP0_Cause & (1 << CP0Ca_DC)) |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 89 | env->CP0_Count = count; |
| 90 | else { |
| 91 | /* Store new count register */ |
| 92 | env->CP0_Count = |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 93 | count - (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 94 | TIMER_FREQ, get_ticks_per_sec()); |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 95 | /* Update timer timer */ |
| 96 | cpu_mips_timer_update(env); |
| 97 | } |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 100 | void cpu_mips_store_compare (CPUMIPSState *env, uint32_t value) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 101 | { |
ths | 3529b53 | 2007-04-05 23:17:40 +0000 | [diff] [blame] | 102 | env->CP0_Compare = value; |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 103 | if (!(env->CP0_Cause & (1 << CP0Ca_DC))) |
| 104 | cpu_mips_timer_update(env); |
| 105 | if (env->insn_flags & ISA_MIPS32R2) |
ths | 39d51eb | 2007-03-18 12:43:40 +0000 | [diff] [blame] | 106 | env->CP0_Cause &= ~(1 << CP0Ca_TI); |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 107 | qemu_irq_lower(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]); |
| 108 | } |
| 109 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 110 | void cpu_mips_start_count(CPUMIPSState *env) |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 111 | { |
| 112 | cpu_mips_store_count(env, env->CP0_Count); |
| 113 | } |
| 114 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 115 | void cpu_mips_stop_count(CPUMIPSState *env) |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 116 | { |
| 117 | /* Store the current value */ |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 118 | env->CP0_Count += (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 119 | TIMER_FREQ, get_ticks_per_sec()); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void mips_timer_cb (void *opaque) |
| 123 | { |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 124 | CPUMIPSState *env; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 125 | |
| 126 | env = opaque; |
| 127 | #if 0 |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 128 | qemu_log("%s\n", __func__); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 129 | #endif |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 130 | |
| 131 | if (env->CP0_Cause & (1 << CP0Ca_DC)) |
| 132 | return; |
| 133 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 134 | /* ??? This callback should occur when the counter is exactly equal to |
| 135 | the comparator value. Offset the count by one to avoid immediately |
| 136 | retriggering the callback before any virtual time has passed. */ |
| 137 | env->CP0_Count++; |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 138 | cpu_mips_timer_expire(env); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 139 | env->CP0_Count--; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 142 | void cpu_mips_clock_init (CPUMIPSState *env) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 143 | { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 144 | env->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &mips_timer_cb, env); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 145 | env->CP0_Compare = 0; |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 146 | cpu_mips_store_count(env, 1); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 147 | } |