2 * QEMU Sun4u/Sun4v System Emulator common routines
4 * Copyright (c) 2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
28 #include "hw/char/serial.h"
29 #include "hw/sparc/sparc64.h"
30 #include "qemu/timer.h"
31 #include "sysemu/reset.h"
35 #define TICK_MAX 0x7fffffffffffffffULL
37 void cpu_check_irqs(CPUSPARCState
*env
)
40 uint32_t pil
= env
->pil_in
|
41 (env
->softint
& ~(SOFTINT_TIMER
| SOFTINT_STIMER
));
43 /* We should be holding the BQL before we mess with IRQs */
44 g_assert(qemu_mutex_iothread_locked());
46 /* TT_IVEC has a higher priority (16) than TT_EXTINT (31..17) */
47 if (env
->ivec_status
& 0x20) {
51 /* check if TM or SM in SOFTINT are set
52 setting these also causes interrupt 14 */
53 if (env
->softint
& (SOFTINT_TIMER
| SOFTINT_STIMER
)) {
57 /* The bit corresponding to psrpil is (1<< psrpil), the next bit
59 if (pil
< (2 << env
->psrpil
)) {
60 if (cs
->interrupt_request
& CPU_INTERRUPT_HARD
) {
61 trace_sparc64_cpu_check_irqs_reset_irq(env
->interrupt_index
);
62 env
->interrupt_index
= 0;
63 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
68 if (cpu_interrupts_enabled(env
)) {
72 for (i
= 15; i
> env
->psrpil
; i
--) {
74 int old_interrupt
= env
->interrupt_index
;
75 int new_interrupt
= TT_EXTINT
| i
;
77 if (unlikely(env
->tl
> 0 && cpu_tsptr(env
)->tt
> new_interrupt
78 && ((cpu_tsptr(env
)->tt
& 0x1f0) == TT_EXTINT
))) {
79 trace_sparc64_cpu_check_irqs_noset_irq(env
->tl
,
82 } else if (old_interrupt
!= new_interrupt
) {
83 env
->interrupt_index
= new_interrupt
;
84 trace_sparc64_cpu_check_irqs_set_irq(i
, old_interrupt
,
86 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
91 } else if (cs
->interrupt_request
& CPU_INTERRUPT_HARD
) {
92 trace_sparc64_cpu_check_irqs_disabled(pil
, env
->pil_in
, env
->softint
,
93 env
->interrupt_index
);
94 env
->interrupt_index
= 0;
95 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
99 static void cpu_kick_irq(SPARCCPU
*cpu
)
101 CPUState
*cs
= CPU(cpu
);
102 CPUSPARCState
*env
= &cpu
->env
;
109 void sparc64_cpu_set_ivec_irq(void *opaque
, int irq
, int level
)
111 SPARCCPU
*cpu
= opaque
;
112 CPUSPARCState
*env
= &cpu
->env
;
116 if (!(env
->ivec_status
& 0x20)) {
117 trace_sparc64_cpu_ivec_raise_irq(irq
);
120 env
->interrupt_index
= TT_IVEC
;
121 env
->ivec_status
|= 0x20;
122 env
->ivec_data
[0] = (0x1f << 6) | irq
;
123 env
->ivec_data
[1] = 0;
124 env
->ivec_data
[2] = 0;
125 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
128 if (env
->ivec_status
& 0x20) {
129 trace_sparc64_cpu_ivec_lower_irq(irq
);
131 env
->ivec_status
&= ~0x20;
132 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
137 typedef struct ResetData
{
142 static CPUTimer
*cpu_timer_create(const char *name
, SPARCCPU
*cpu
,
143 QEMUBHFunc
*cb
, uint32_t frequency
,
144 uint64_t disabled_mask
, uint64_t npt_mask
)
146 CPUTimer
*timer
= g_malloc0(sizeof(CPUTimer
));
149 timer
->frequency
= frequency
;
150 timer
->disabled_mask
= disabled_mask
;
151 timer
->npt_mask
= npt_mask
;
155 timer
->clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
157 timer
->qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, cb
, cpu
);
162 static void cpu_timer_reset(CPUTimer
*timer
)
165 timer
->clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
167 timer_del(timer
->qtimer
);
170 static void main_cpu_reset(void *opaque
)
172 ResetData
*s
= (ResetData
*)opaque
;
173 CPUSPARCState
*env
= &s
->cpu
->env
;
174 static unsigned int nr_resets
;
176 cpu_reset(CPU(s
->cpu
));
178 cpu_timer_reset(env
->tick
);
179 cpu_timer_reset(env
->stick
);
180 cpu_timer_reset(env
->hstick
);
182 env
->gregs
[1] = 0; /* Memory start */
183 env
->gregs
[2] = ram_size
; /* Memory size */
184 env
->gregs
[3] = 0; /* Machine description XXX */
185 if (nr_resets
++ == 0) {
187 env
->pc
= s
->prom_addr
+ 0x20ULL
;
189 env
->pc
= s
->prom_addr
+ 0x40ULL
;
191 env
->npc
= env
->pc
+ 4;
194 static void tick_irq(void *opaque
)
196 SPARCCPU
*cpu
= opaque
;
197 CPUSPARCState
*env
= &cpu
->env
;
199 CPUTimer
*timer
= env
->tick
;
201 if (timer
->disabled
) {
202 trace_sparc64_cpu_tick_irq_disabled();
205 trace_sparc64_cpu_tick_irq_fire();
208 env
->softint
|= SOFTINT_TIMER
;
212 static void stick_irq(void *opaque
)
214 SPARCCPU
*cpu
= opaque
;
215 CPUSPARCState
*env
= &cpu
->env
;
217 CPUTimer
*timer
= env
->stick
;
219 if (timer
->disabled
) {
220 trace_sparc64_cpu_stick_irq_disabled();
223 trace_sparc64_cpu_stick_irq_fire();
226 env
->softint
|= SOFTINT_STIMER
;
230 static void hstick_irq(void *opaque
)
232 SPARCCPU
*cpu
= opaque
;
233 CPUSPARCState
*env
= &cpu
->env
;
235 CPUTimer
*timer
= env
->hstick
;
237 if (timer
->disabled
) {
238 trace_sparc64_cpu_hstick_irq_disabled();
241 trace_sparc64_cpu_hstick_irq_fire();
244 env
->softint
|= SOFTINT_STIMER
;
248 static int64_t cpu_to_timer_ticks(int64_t cpu_ticks
, uint32_t frequency
)
250 return muldiv64(cpu_ticks
, NANOSECONDS_PER_SECOND
, frequency
);
253 static uint64_t timer_to_cpu_ticks(int64_t timer_ticks
, uint32_t frequency
)
255 return muldiv64(timer_ticks
, frequency
, NANOSECONDS_PER_SECOND
);
258 void cpu_tick_set_count(CPUTimer
*timer
, uint64_t count
)
260 uint64_t real_count
= count
& ~timer
->npt_mask
;
261 uint64_t npt_bit
= count
& timer
->npt_mask
;
263 int64_t vm_clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
264 cpu_to_timer_ticks(real_count
, timer
->frequency
);
266 trace_sparc64_cpu_tick_set_count(timer
->name
, real_count
,
267 timer
->npt
? "disabled" : "enabled",
270 timer
->npt
= npt_bit
? 1 : 0;
271 timer
->clock_offset
= vm_clock_offset
;
274 uint64_t cpu_tick_get_count(CPUTimer
*timer
)
276 uint64_t real_count
= timer_to_cpu_ticks(
277 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - timer
->clock_offset
,
280 trace_sparc64_cpu_tick_get_count(timer
->name
, real_count
,
281 timer
->npt
? "disabled" : "enabled",
285 real_count
|= timer
->npt_mask
;
291 void cpu_tick_set_limit(CPUTimer
*timer
, uint64_t limit
)
293 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
295 uint64_t real_limit
= limit
& ~timer
->disabled_mask
;
296 timer
->disabled
= (limit
& timer
->disabled_mask
) ? 1 : 0;
298 int64_t expires
= cpu_to_timer_ticks(real_limit
, timer
->frequency
) +
305 trace_sparc64_cpu_tick_set_limit(timer
->name
, real_limit
,
306 timer
->disabled
? "disabled" : "enabled",
309 now
- timer
->clock_offset
,
313 expires
- now
, timer
->frequency
317 trace_sparc64_cpu_tick_set_limit_zero(timer
->name
);
318 timer_del(timer
->qtimer
);
319 } else if (timer
->disabled
) {
320 timer_del(timer
->qtimer
);
322 timer_mod(timer
->qtimer
, expires
);
326 SPARCCPU
*sparc64_cpu_devinit(const char *cpu_type
, uint64_t prom_addr
)
330 ResetData
*reset_info
;
332 uint32_t tick_frequency
= 100 * 1000000;
333 uint32_t stick_frequency
= 100 * 1000000;
334 uint32_t hstick_frequency
= 100 * 1000000;
336 cpu
= SPARC_CPU(cpu_create(cpu_type
));
337 qdev_init_gpio_in_named(DEVICE(cpu
), sparc64_cpu_set_ivec_irq
,
338 "ivec-irq", IVEC_MAX
);
341 env
->tick
= cpu_timer_create("tick", cpu
, tick_irq
,
342 tick_frequency
, TICK_INT_DIS
,
345 env
->stick
= cpu_timer_create("stick", cpu
, stick_irq
,
346 stick_frequency
, TICK_INT_DIS
,
349 env
->hstick
= cpu_timer_create("hstick", cpu
, hstick_irq
,
350 hstick_frequency
, TICK_INT_DIS
,
353 reset_info
= g_malloc0(sizeof(ResetData
));
354 reset_info
->cpu
= cpu
;
355 reset_info
->prom_addr
= prom_addr
;
356 qemu_register_reset(main_cpu_reset
, reset_info
);