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" |
Sanjay Lal | 353a243 | 2014-06-17 23:10:27 +0100 | [diff] [blame] | 26 | #include "sysemu/kvm.h" |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 27 | |
Laurent Vivier | 683dca6 | 2015-08-25 16:16:21 +0200 | [diff] [blame] | 28 | #define TIMER_PERIOD 10 /* 10 ns period for 100 Mhz frequency */ |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 29 | |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 30 | /* XXX: do not use a global */ |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 31 | uint32_t cpu_mips_get_random (CPUMIPSState *env) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 32 | { |
Serge Vakulenko | ceb0ee1 | 2015-07-05 23:14:50 -0700 | [diff] [blame] | 33 | static uint32_t seed = 1; |
aurel32 | 59d9413 | 2009-01-08 18:48:12 +0000 | [diff] [blame] | 34 | static uint32_t prev_idx = 0; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 35 | uint32_t idx; |
Leon Alrae | 3adafef | 2015-09-10 10:15:28 +0100 | [diff] [blame] | 36 | uint32_t nb_rand_tlb = env->tlb->nb_tlb - env->CP0_Wired; |
| 37 | |
| 38 | if (nb_rand_tlb == 1) { |
| 39 | return env->tlb->nb_tlb - 1; |
| 40 | } |
| 41 | |
aurel32 | 59d9413 | 2009-01-08 18:48:12 +0000 | [diff] [blame] | 42 | /* Don't return same value twice, so get another value */ |
| 43 | do { |
Serge Vakulenko | ceb0ee1 | 2015-07-05 23:14:50 -0700 | [diff] [blame] | 44 | /* Use a simple algorithm of Linear Congruential Generator |
| 45 | * from ISO/IEC 9899 standard. */ |
| 46 | seed = 1103515245 * seed + 12345; |
Leon Alrae | 3adafef | 2015-09-10 10:15:28 +0100 | [diff] [blame] | 47 | idx = (seed >> 16) % nb_rand_tlb + env->CP0_Wired; |
aurel32 | 59d9413 | 2009-01-08 18:48:12 +0000 | [diff] [blame] | 48 | } while (idx == prev_idx); |
| 49 | prev_idx = idx; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 50 | return idx; |
| 51 | } |
| 52 | |
| 53 | /* MIPS R4K timer */ |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 54 | static void cpu_mips_timer_update(CPUMIPSState *env) |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 55 | { |
| 56 | uint64_t now, next; |
| 57 | uint32_t wait; |
| 58 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 59 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
Laurent Vivier | 683dca6 | 2015-08-25 16:16:21 +0200 | [diff] [blame] | 60 | wait = env->CP0_Compare - env->CP0_Count - (uint32_t)(now / TIMER_PERIOD); |
| 61 | next = now + (uint64_t)wait * TIMER_PERIOD; |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 62 | timer_mod(env->timer, next); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 65 | /* Expire the timer. */ |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 66 | static void cpu_mips_timer_expire(CPUMIPSState *env) |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 67 | { |
| 68 | cpu_mips_timer_update(env); |
| 69 | if (env->insn_flags & ISA_MIPS32R2) { |
| 70 | env->CP0_Cause |= 1 << CP0Ca_TI; |
| 71 | } |
| 72 | qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]); |
| 73 | } |
| 74 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 75 | uint32_t cpu_mips_get_count (CPUMIPSState *env) |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 76 | { |
| 77 | if (env->CP0_Cause & (1 << CP0Ca_DC)) { |
| 78 | return env->CP0_Count; |
| 79 | } else { |
Edgar E. Iglesias | e027e1f | 2011-01-18 00:12:22 +0100 | [diff] [blame] | 80 | uint64_t now; |
| 81 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 82 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 83 | if (timer_pending(env->timer) |
| 84 | && timer_expired(env->timer, now)) { |
Edgar E. Iglesias | e027e1f | 2011-01-18 00:12:22 +0100 | [diff] [blame] | 85 | /* The timer has already expired. */ |
| 86 | cpu_mips_timer_expire(env); |
| 87 | } |
| 88 | |
Laurent Vivier | 683dca6 | 2015-08-25 16:16:21 +0200 | [diff] [blame] | 89 | return env->CP0_Count + (uint32_t)(now / TIMER_PERIOD); |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 93 | void cpu_mips_store_count (CPUMIPSState *env, uint32_t count) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 94 | { |
James Hogan | 4b69c7e | 2014-06-17 23:10:26 +0100 | [diff] [blame] | 95 | /* |
| 96 | * This gets called from cpu_state_reset(), potentially before timer init. |
| 97 | * So env->timer may be NULL, which is also the case with KVM enabled so |
| 98 | * treat timer as disabled in that case. |
| 99 | */ |
| 100 | if (env->CP0_Cause & (1 << CP0Ca_DC) || !env->timer) |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 101 | env->CP0_Count = count; |
| 102 | else { |
| 103 | /* Store new count register */ |
Laurent Vivier | 683dca6 | 2015-08-25 16:16:21 +0200 | [diff] [blame] | 104 | env->CP0_Count = count - |
| 105 | (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / TIMER_PERIOD); |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 106 | /* Update timer timer */ |
| 107 | cpu_mips_timer_update(env); |
| 108 | } |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 111 | void cpu_mips_store_compare (CPUMIPSState *env, uint32_t value) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 112 | { |
ths | 3529b53 | 2007-04-05 23:17:40 +0000 | [diff] [blame] | 113 | env->CP0_Compare = value; |
aurel32 | ea86e4e | 2008-04-11 04:55:31 +0000 | [diff] [blame] | 114 | if (!(env->CP0_Cause & (1 << CP0Ca_DC))) |
| 115 | cpu_mips_timer_update(env); |
| 116 | if (env->insn_flags & ISA_MIPS32R2) |
ths | 39d51eb | 2007-03-18 12:43:40 +0000 | [diff] [blame] | 117 | env->CP0_Cause &= ~(1 << CP0Ca_TI); |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 118 | qemu_irq_lower(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]); |
| 119 | } |
| 120 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 121 | void cpu_mips_start_count(CPUMIPSState *env) |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 122 | { |
| 123 | cpu_mips_store_count(env, env->CP0_Count); |
| 124 | } |
| 125 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 126 | void cpu_mips_stop_count(CPUMIPSState *env) |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 127 | { |
| 128 | /* Store the current value */ |
Laurent Vivier | 683dca6 | 2015-08-25 16:16:21 +0200 | [diff] [blame] | 129 | env->CP0_Count += (uint32_t)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / |
| 130 | TIMER_PERIOD); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void mips_timer_cb (void *opaque) |
| 134 | { |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 135 | CPUMIPSState *env; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 136 | |
| 137 | env = opaque; |
| 138 | #if 0 |
aliguori | 93fcfe3 | 2009-01-15 22:34:14 +0000 | [diff] [blame] | 139 | qemu_log("%s\n", __func__); |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 140 | #endif |
ths | 4253218 | 2007-09-25 16:53:15 +0000 | [diff] [blame] | 141 | |
| 142 | if (env->CP0_Cause & (1 << CP0Ca_DC)) |
| 143 | return; |
| 144 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 145 | /* ??? This callback should occur when the counter is exactly equal to |
| 146 | the comparator value. Offset the count by one to avoid immediately |
| 147 | retriggering the callback before any virtual time has passed. */ |
| 148 | env->CP0_Count++; |
Edgar E. Iglesias | b1dfe64 | 2011-01-18 00:07:49 +0100 | [diff] [blame] | 149 | cpu_mips_timer_expire(env); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 150 | env->CP0_Count--; |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Andreas Färber | 61c56c8 | 2012-03-14 01:38:23 +0100 | [diff] [blame] | 153 | void cpu_mips_clock_init (CPUMIPSState *env) |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 154 | { |
Sanjay Lal | 353a243 | 2014-06-17 23:10:27 +0100 | [diff] [blame] | 155 | /* |
| 156 | * If we're in KVM mode, don't create the periodic timer, that is handled in |
| 157 | * kernel. |
| 158 | */ |
| 159 | if (!kvm_enabled()) { |
| 160 | env->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &mips_timer_cb, env); |
| 161 | } |
ths | e16fe40 | 2006-12-06 21:38:37 +0000 | [diff] [blame] | 162 | } |