]>
git.proxmox.com Git - mirror_qemu.git/blob - hw/sparc64/sparc64.c
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"
27 #include "qapi/error.h"
29 #include "hw/boards.h"
30 #include "hw/sparc/sparc64.h"
31 #include "qemu/timer.h"
32 #include "sysemu/reset.h"
36 #define TICK_MAX 0x7fffffffffffffffULL
38 static void cpu_kick_irq(SPARCCPU
*cpu
)
40 CPUState
*cs
= CPU(cpu
);
41 CPUSPARCState
*env
= &cpu
->env
;
48 void sparc64_cpu_set_ivec_irq(void *opaque
, int irq
, int level
)
50 SPARCCPU
*cpu
= opaque
;
51 CPUSPARCState
*env
= &cpu
->env
;
55 if (!(env
->ivec_status
& 0x20)) {
56 trace_sparc64_cpu_ivec_raise_irq(irq
);
59 env
->interrupt_index
= TT_IVEC
;
60 env
->ivec_status
|= 0x20;
61 env
->ivec_data
[0] = (0x1f << 6) | irq
;
62 env
->ivec_data
[1] = 0;
63 env
->ivec_data
[2] = 0;
64 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
67 if (env
->ivec_status
& 0x20) {
68 trace_sparc64_cpu_ivec_lower_irq(irq
);
70 env
->ivec_status
&= ~0x20;
71 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
76 typedef struct ResetData
{
81 static CPUTimer
*cpu_timer_create(const char *name
, SPARCCPU
*cpu
,
82 QEMUBHFunc
*cb
, uint32_t frequency
,
83 uint64_t disabled_mask
, uint64_t npt_mask
)
85 CPUTimer
*timer
= g_new0(CPUTimer
, 1);
88 timer
->frequency
= frequency
;
89 timer
->disabled_mask
= disabled_mask
;
90 timer
->npt_mask
= npt_mask
;
94 timer
->clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
96 timer
->qtimer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, cb
, cpu
);
101 static void cpu_timer_reset(CPUTimer
*timer
)
104 timer
->clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
106 timer_del(timer
->qtimer
);
109 static void main_cpu_reset(void *opaque
)
111 ResetData
*s
= (ResetData
*)opaque
;
112 CPUSPARCState
*env
= &s
->cpu
->env
;
113 static unsigned int nr_resets
;
115 cpu_reset(CPU(s
->cpu
));
117 cpu_timer_reset(env
->tick
);
118 cpu_timer_reset(env
->stick
);
119 cpu_timer_reset(env
->hstick
);
121 env
->gregs
[1] = 0; /* Memory start */
122 env
->gregs
[2] = current_machine
->ram_size
; /* Memory size */
123 env
->gregs
[3] = 0; /* Machine description XXX */
124 if (nr_resets
++ == 0) {
126 env
->pc
= s
->prom_addr
+ 0x20ULL
;
128 env
->pc
= s
->prom_addr
+ 0x40ULL
;
130 env
->npc
= env
->pc
+ 4;
133 static void tick_irq(void *opaque
)
135 SPARCCPU
*cpu
= opaque
;
136 CPUSPARCState
*env
= &cpu
->env
;
138 CPUTimer
*timer
= env
->tick
;
140 if (timer
->disabled
) {
141 trace_sparc64_cpu_tick_irq_disabled();
144 trace_sparc64_cpu_tick_irq_fire();
147 env
->softint
|= SOFTINT_TIMER
;
151 static void stick_irq(void *opaque
)
153 SPARCCPU
*cpu
= opaque
;
154 CPUSPARCState
*env
= &cpu
->env
;
156 CPUTimer
*timer
= env
->stick
;
158 if (timer
->disabled
) {
159 trace_sparc64_cpu_stick_irq_disabled();
162 trace_sparc64_cpu_stick_irq_fire();
165 env
->softint
|= SOFTINT_STIMER
;
169 static void hstick_irq(void *opaque
)
171 SPARCCPU
*cpu
= opaque
;
172 CPUSPARCState
*env
= &cpu
->env
;
174 CPUTimer
*timer
= env
->hstick
;
176 if (timer
->disabled
) {
177 trace_sparc64_cpu_hstick_irq_disabled();
180 trace_sparc64_cpu_hstick_irq_fire();
183 env
->softint
|= SOFTINT_STIMER
;
187 static int64_t cpu_to_timer_ticks(int64_t cpu_ticks
, uint32_t frequency
)
189 return muldiv64(cpu_ticks
, NANOSECONDS_PER_SECOND
, frequency
);
192 static uint64_t timer_to_cpu_ticks(int64_t timer_ticks
, uint32_t frequency
)
194 return muldiv64(timer_ticks
, frequency
, NANOSECONDS_PER_SECOND
);
197 void cpu_tick_set_count(CPUTimer
*timer
, uint64_t count
)
199 uint64_t real_count
= count
& ~timer
->npt_mask
;
200 uint64_t npt_bit
= count
& timer
->npt_mask
;
202 int64_t vm_clock_offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
203 cpu_to_timer_ticks(real_count
, timer
->frequency
);
205 trace_sparc64_cpu_tick_set_count(timer
->name
, real_count
,
206 timer
->npt
? "disabled" : "enabled",
209 timer
->npt
= npt_bit
? 1 : 0;
210 timer
->clock_offset
= vm_clock_offset
;
213 uint64_t cpu_tick_get_count(CPUTimer
*timer
)
215 uint64_t real_count
= timer_to_cpu_ticks(
216 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - timer
->clock_offset
,
219 trace_sparc64_cpu_tick_get_count(timer
->name
, real_count
,
220 timer
->npt
? "disabled" : "enabled",
224 real_count
|= timer
->npt_mask
;
230 void cpu_tick_set_limit(CPUTimer
*timer
, uint64_t limit
)
232 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
234 uint64_t real_limit
= limit
& ~timer
->disabled_mask
;
235 timer
->disabled
= (limit
& timer
->disabled_mask
) ? 1 : 0;
237 int64_t expires
= cpu_to_timer_ticks(real_limit
, timer
->frequency
) +
244 trace_sparc64_cpu_tick_set_limit(timer
->name
, real_limit
,
245 timer
->disabled
? "disabled" : "enabled",
248 now
- timer
->clock_offset
,
252 expires
- now
, timer
->frequency
256 trace_sparc64_cpu_tick_set_limit_zero(timer
->name
);
257 timer_del(timer
->qtimer
);
258 } else if (timer
->disabled
) {
259 timer_del(timer
->qtimer
);
261 timer_mod(timer
->qtimer
, expires
);
265 SPARCCPU
*sparc64_cpu_devinit(const char *cpu_type
, uint64_t prom_addr
)
269 ResetData
*reset_info
;
271 uint32_t tick_frequency
= 100 * 1000000;
272 uint32_t stick_frequency
= 100 * 1000000;
273 uint32_t hstick_frequency
= 100 * 1000000;
275 cpu
= SPARC_CPU(object_new(cpu_type
));
276 qdev_init_gpio_in_named(DEVICE(cpu
), sparc64_cpu_set_ivec_irq
,
277 "ivec-irq", IVEC_MAX
);
278 qdev_realize(DEVICE(cpu
), NULL
, &error_fatal
);
281 env
->tick
= cpu_timer_create("tick", cpu
, tick_irq
,
282 tick_frequency
, TICK_INT_DIS
,
285 env
->stick
= cpu_timer_create("stick", cpu
, stick_irq
,
286 stick_frequency
, TICK_INT_DIS
,
289 env
->hstick
= cpu_timer_create("hstick", cpu
, hstick_irq
,
290 hstick_frequency
, TICK_INT_DIS
,
293 reset_info
= g_new0(ResetData
, 1);
294 reset_info
->cpu
= cpu
;
295 reset_info
->prom_addr
= prom_addr
;
296 qemu_register_reset(main_cpu_reset
, reset_info
);