blob: c8b4b000cd0e14c18e666008a2c598bee64ee8b2 [file] [log] [blame]
Aurelien Jarno7b9cbad2010-03-14 23:30:19 +01001/*
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 Bonzini83c9f4c2013-02-04 15:40:22 +010023#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010024#include "hw/mips/cpudevs.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/timer.h"
thse16fe402006-12-06 21:38:37 +000026
aurel32ea86e4e2008-04-11 04:55:31 +000027#define TIMER_FREQ 100 * 1000 * 1000
28
thse16fe402006-12-06 21:38:37 +000029/* XXX: do not use a global */
Andreas Färber61c56c82012-03-14 01:38:23 +010030uint32_t cpu_mips_get_random (CPUMIPSState *env)
thse16fe402006-12-06 21:38:37 +000031{
aurel3259d94132009-01-08 18:48:12 +000032 static uint32_t lfsr = 1;
33 static uint32_t prev_idx = 0;
thse16fe402006-12-06 21:38:37 +000034 uint32_t idx;
aurel3259d94132009-01-08 18:48:12 +000035 /* 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;
thse16fe402006-12-06 21:38:37 +000041 return idx;
42}
43
44/* MIPS R4K timer */
Andreas Färber61c56c82012-03-14 01:38:23 +010045static void cpu_mips_timer_update(CPUMIPSState *env)
aurel32ea86e4e2008-04-11 04:55:31 +000046{
47 uint64_t now, next;
48 uint32_t wait;
49
Alex Blighbc72ad62013-08-21 16:03:08 +010050 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
aurel32ea86e4e2008-04-11 04:55:31 +000051 wait = env->CP0_Compare - env->CP0_Count -
Juan Quintela6ee093c2009-09-10 03:04:26 +020052 (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec());
53 next = now + muldiv64(wait, get_ticks_per_sec(), TIMER_FREQ);
Alex Blighbc72ad62013-08-21 16:03:08 +010054 timer_mod(env->timer, next);
thse16fe402006-12-06 21:38:37 +000055}
56
Edgar E. Iglesiasb1dfe642011-01-18 00:07:49 +010057/* Expire the timer. */
Andreas Färber61c56c82012-03-14 01:38:23 +010058static void cpu_mips_timer_expire(CPUMIPSState *env)
Edgar E. Iglesiasb1dfe642011-01-18 00:07:49 +010059{
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ärber61c56c82012-03-14 01:38:23 +010067uint32_t cpu_mips_get_count (CPUMIPSState *env)
Edgar E. Iglesiasb1dfe642011-01-18 00:07:49 +010068{
69 if (env->CP0_Cause & (1 << CP0Ca_DC)) {
70 return env->CP0_Count;
71 } else {
Edgar E. Iglesiase027e1f2011-01-18 00:12:22 +010072 uint64_t now;
73
Alex Blighbc72ad62013-08-21 16:03:08 +010074 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Alex Blighe93379b2013-08-21 16:02:39 +010075 if (timer_pending(env->timer)
76 && timer_expired(env->timer, now)) {
Edgar E. Iglesiase027e1f2011-01-18 00:12:22 +010077 /* The timer has already expired. */
78 cpu_mips_timer_expire(env);
79 }
80
Edgar E. Iglesiasb1dfe642011-01-18 00:07:49 +010081 return env->CP0_Count +
Edgar E. Iglesiase027e1f2011-01-18 00:12:22 +010082 (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec());
Edgar E. Iglesiasb1dfe642011-01-18 00:07:49 +010083 }
84}
85
Andreas Färber61c56c82012-03-14 01:38:23 +010086void cpu_mips_store_count (CPUMIPSState *env, uint32_t count)
thse16fe402006-12-06 21:38:37 +000087{
ths3529b532007-04-05 23:17:40 +000088 if (env->CP0_Cause & (1 << CP0Ca_DC))
aurel32ea86e4e2008-04-11 04:55:31 +000089 env->CP0_Count = count;
90 else {
91 /* Store new count register */
92 env->CP0_Count =
Alex Blighbc72ad62013-08-21 16:03:08 +010093 count - (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
Juan Quintela6ee093c2009-09-10 03:04:26 +020094 TIMER_FREQ, get_ticks_per_sec());
aurel32ea86e4e2008-04-11 04:55:31 +000095 /* Update timer timer */
96 cpu_mips_timer_update(env);
97 }
thse16fe402006-12-06 21:38:37 +000098}
99
Andreas Färber61c56c82012-03-14 01:38:23 +0100100void cpu_mips_store_compare (CPUMIPSState *env, uint32_t value)
thse16fe402006-12-06 21:38:37 +0000101{
ths3529b532007-04-05 23:17:40 +0000102 env->CP0_Compare = value;
aurel32ea86e4e2008-04-11 04:55:31 +0000103 if (!(env->CP0_Cause & (1 << CP0Ca_DC)))
104 cpu_mips_timer_update(env);
105 if (env->insn_flags & ISA_MIPS32R2)
ths39d51eb2007-03-18 12:43:40 +0000106 env->CP0_Cause &= ~(1 << CP0Ca_TI);
ths42532182007-09-25 16:53:15 +0000107 qemu_irq_lower(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);
108}
109
Andreas Färber61c56c82012-03-14 01:38:23 +0100110void cpu_mips_start_count(CPUMIPSState *env)
ths42532182007-09-25 16:53:15 +0000111{
112 cpu_mips_store_count(env, env->CP0_Count);
113}
114
Andreas Färber61c56c82012-03-14 01:38:23 +0100115void cpu_mips_stop_count(CPUMIPSState *env)
ths42532182007-09-25 16:53:15 +0000116{
117 /* Store the current value */
Alex Blighbc72ad62013-08-21 16:03:08 +0100118 env->CP0_Count += (uint32_t)muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
Juan Quintela6ee093c2009-09-10 03:04:26 +0200119 TIMER_FREQ, get_ticks_per_sec());
thse16fe402006-12-06 21:38:37 +0000120}
121
122static void mips_timer_cb (void *opaque)
123{
Andreas Färber61c56c82012-03-14 01:38:23 +0100124 CPUMIPSState *env;
thse16fe402006-12-06 21:38:37 +0000125
126 env = opaque;
127#if 0
aliguori93fcfe32009-01-15 22:34:14 +0000128 qemu_log("%s\n", __func__);
thse16fe402006-12-06 21:38:37 +0000129#endif
ths42532182007-09-25 16:53:15 +0000130
131 if (env->CP0_Cause & (1 << CP0Ca_DC))
132 return;
133
pbrook2e70f6e2008-06-29 01:03:05 +0000134 /* ??? 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. Iglesiasb1dfe642011-01-18 00:07:49 +0100138 cpu_mips_timer_expire(env);
pbrook2e70f6e2008-06-29 01:03:05 +0000139 env->CP0_Count--;
thse16fe402006-12-06 21:38:37 +0000140}
141
Andreas Färber61c56c82012-03-14 01:38:23 +0100142void cpu_mips_clock_init (CPUMIPSState *env)
thse16fe402006-12-06 21:38:37 +0000143{
Alex Blighbc72ad62013-08-21 16:03:08 +0100144 env->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &mips_timer_cb, env);
thse16fe402006-12-06 21:38:37 +0000145 env->CP0_Compare = 0;
aurel32ea86e4e2008-04-11 04:55:31 +0000146 cpu_mips_store_count(env, 1);
thse16fe402006-12-06 21:38:37 +0000147}