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/boards.h"
29 #include "hw/sparc/sparc64.h"
30 #include "qemu/timer.h"
31 #include "sysemu/reset.h"
35 #define TICK_MAX 0x7fffffffffffffffULL
37 static void cpu_kick_irq(SPARCCPU
*cpu
)
39 CPUState
*cs
= CPU(cpu
);
40 CPUSPARCState
*env
= &cpu
->env
;
47 void sparc64_cpu_set_ivec_irq(void *opaque
, int irq
, int level
)
49 SPARCCPU
*cpu
= opaque
;
50 CPUSPARCState
*env
= &cpu
->env
;
54 if (!(env
->ivec_status
& 0x20)) {
55 trace_sparc64_cpu_ivec_raise_irq(irq
);
58 env
->interrupt_index
= TT_IVEC
;
59 env
->ivec_status
|= 0x20;
60 env
->ivec_data
[0] = (0x1f << 6) | irq
;
61 env
->ivec_data
[1] = 0;
62 env
->ivec_data
[2] = 0;
63 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
66 if (env
->ivec_status
& 0x20) {
67 trace_sparc64_cpu_ivec_lower_irq(irq
);
69 env
->ivec_status
&= ~0x20;
70 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
75 typedef struct ResetData
{
80 static CPUTimer
*cpu_timer_create(const char *name
, SPARCCPU
*cpu
,
81 QEMUBHFunc
*cb
, uint32_t frequency
,
82 uint64_t disabled_mask
, uint64_t npt_mask
)
84 CPUTimer
*timer
= g_new0(CPUTimer
, 1);
87 timer
->frequency
= frequency
;
88 timer
->disabled_mask
= disabled_mask
;
89 timer
->npt_mask
= npt_mask
;
93 timer
->clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
95 timer
->qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, cb
, cpu
);
100 static void cpu_timer_reset(CPUTimer
*timer
)
103 timer
->clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
105 timer_del(timer
->qtimer
);
108 static void main_cpu_reset(void *opaque
)
110 ResetData
*s
= (ResetData
*)opaque
;
111 CPUSPARCState
*env
= &s
->cpu
->env
;
112 static unsigned int nr_resets
;
114 cpu_reset(CPU(s
->cpu
));
116 cpu_timer_reset(env
->tick
);
117 cpu_timer_reset(env
->stick
);
118 cpu_timer_reset(env
->hstick
);
120 env
->gregs
[1] = 0; /* Memory start */
121 env
->gregs
[2] = current_machine
->ram_size
; /* Memory size */
122 env
->gregs
[3] = 0; /* Machine description XXX */
123 if (nr_resets
++ == 0) {
125 env
->pc
= s
->prom_addr
+ 0x20ULL
;
127 env
->pc
= s
->prom_addr
+ 0x40ULL
;
129 env
->npc
= env
->pc
+ 4;
132 static void tick_irq(void *opaque
)
134 SPARCCPU
*cpu
= opaque
;
135 CPUSPARCState
*env
= &cpu
->env
;
137 CPUTimer
*timer
= env
->tick
;
139 if (timer
->disabled
) {
140 trace_sparc64_cpu_tick_irq_disabled();
143 trace_sparc64_cpu_tick_irq_fire();
146 env
->softint
|= SOFTINT_TIMER
;
150 static void stick_irq(void *opaque
)
152 SPARCCPU
*cpu
= opaque
;
153 CPUSPARCState
*env
= &cpu
->env
;
155 CPUTimer
*timer
= env
->stick
;
157 if (timer
->disabled
) {
158 trace_sparc64_cpu_stick_irq_disabled();
161 trace_sparc64_cpu_stick_irq_fire();
164 env
->softint
|= SOFTINT_STIMER
;
168 static void hstick_irq(void *opaque
)
170 SPARCCPU
*cpu
= opaque
;
171 CPUSPARCState
*env
= &cpu
->env
;
173 CPUTimer
*timer
= env
->hstick
;
175 if (timer
->disabled
) {
176 trace_sparc64_cpu_hstick_irq_disabled();
179 trace_sparc64_cpu_hstick_irq_fire();
182 env
->softint
|= SOFTINT_STIMER
;
186 static int64_t cpu_to_timer_ticks(int64_t cpu_ticks
, uint32_t frequency
)
188 return muldiv64(cpu_ticks
, NANOSECONDS_PER_SECOND
, frequency
);
191 static uint64_t timer_to_cpu_ticks(int64_t timer_ticks
, uint32_t frequency
)
193 return muldiv64(timer_ticks
, frequency
, NANOSECONDS_PER_SECOND
);
196 void cpu_tick_set_count(CPUTimer
*timer
, uint64_t count
)
198 uint64_t real_count
= count
& ~timer
->npt_mask
;
199 uint64_t npt_bit
= count
& timer
->npt_mask
;
201 int64_t vm_clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
202 cpu_to_timer_ticks(real_count
, timer
->frequency
);
204 trace_sparc64_cpu_tick_set_count(timer
->name
, real_count
,
205 timer
->npt
? "disabled" : "enabled",
208 timer
->npt
= npt_bit
? 1 : 0;
209 timer
->clock_offset
= vm_clock_offset
;
212 uint64_t cpu_tick_get_count(CPUTimer
*timer
)
214 uint64_t real_count
= timer_to_cpu_ticks(
215 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - timer
->clock_offset
,
218 trace_sparc64_cpu_tick_get_count(timer
->name
, real_count
,
219 timer
->npt
? "disabled" : "enabled",
223 real_count
|= timer
->npt_mask
;
229 void cpu_tick_set_limit(CPUTimer
*timer
, uint64_t limit
)
231 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
233 uint64_t real_limit
= limit
& ~timer
->disabled_mask
;
234 timer
->disabled
= (limit
& timer
->disabled_mask
) ? 1 : 0;
236 int64_t expires
= cpu_to_timer_ticks(real_limit
, timer
->frequency
) +
243 trace_sparc64_cpu_tick_set_limit(timer
->name
, real_limit
,
244 timer
->disabled
? "disabled" : "enabled",
247 now
- timer
->clock_offset
,
251 expires
- now
, timer
->frequency
255 trace_sparc64_cpu_tick_set_limit_zero(timer
->name
);
256 timer_del(timer
->qtimer
);
257 } else if (timer
->disabled
) {
258 timer_del(timer
->qtimer
);
260 timer_mod(timer
->qtimer
, expires
);
264 SPARCCPU
*sparc64_cpu_devinit(const char *cpu_type
, uint64_t prom_addr
)
268 ResetData
*reset_info
;
270 uint32_t tick_frequency
= 100 * 1000000;
271 uint32_t stick_frequency
= 100 * 1000000;
272 uint32_t hstick_frequency
= 100 * 1000000;
274 cpu
= SPARC_CPU(cpu_create(cpu_type
));
275 qdev_init_gpio_in_named(DEVICE(cpu
), sparc64_cpu_set_ivec_irq
,
276 "ivec-irq", IVEC_MAX
);
279 env
->tick
= cpu_timer_create("tick", cpu
, tick_irq
,
280 tick_frequency
, TICK_INT_DIS
,
283 env
->stick
= cpu_timer_create("stick", cpu
, stick_irq
,
284 stick_frequency
, TICK_INT_DIS
,
287 env
->hstick
= cpu_timer_create("hstick", cpu
, hstick_irq
,
288 hstick_frequency
, TICK_INT_DIS
,
291 reset_info
= g_new0(ResetData
, 1);
292 reset_info
->cpu
= cpu
;
293 reset_info
->prom_addr
= prom_addr
;
294 qemu_register_reset(main_cpu_reset
, reset_info
);