]>
Commit | Line | Data |
---|---|---|
a541f297 | 1 | /* |
e9df014c | 2 | * QEMU generic PowerPC hardware System Emulator |
5fafdf24 | 3 | * |
76a66253 | 4 | * Copyright (c) 2003-2007 Jocelyn Mayer |
5fafdf24 | 5 | * |
a541f297 FB |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
87ecb68b PB |
24 | #include "hw.h" |
25 | #include "ppc.h" | |
26 | #include "qemu-timer.h" | |
27 | #include "sysemu.h" | |
28 | #include "nvram.h" | |
3b3fb322 | 29 | #include "qemu-log.h" |
ca20cf32 | 30 | #include "loader.h" |
fc87e185 AG |
31 | #include "kvm.h" |
32 | #include "kvm_ppc.h" | |
a541f297 | 33 | |
e9df014c | 34 | //#define PPC_DEBUG_IRQ |
4b6d0a4c | 35 | //#define PPC_DEBUG_TB |
e9df014c | 36 | |
d12d51d5 | 37 | #ifdef PPC_DEBUG_IRQ |
93fcfe39 | 38 | # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__) |
d12d51d5 AL |
39 | #else |
40 | # define LOG_IRQ(...) do { } while (0) | |
41 | #endif | |
42 | ||
43 | ||
44 | #ifdef PPC_DEBUG_TB | |
93fcfe39 | 45 | # define LOG_TB(...) qemu_log(__VA_ARGS__) |
d12d51d5 AL |
46 | #else |
47 | # define LOG_TB(...) do { } while (0) | |
48 | #endif | |
49 | ||
e2684c0b AF |
50 | static void cpu_ppc_tb_stop (CPUPPCState *env); |
51 | static void cpu_ppc_tb_start (CPUPPCState *env); | |
dbdd2506 | 52 | |
e2684c0b | 53 | void ppc_set_irq(CPUPPCState *env, int n_IRQ, int level) |
47103572 | 54 | { |
fc87e185 AG |
55 | unsigned int old_pending = env->pending_interrupts; |
56 | ||
47103572 JM |
57 | if (level) { |
58 | env->pending_interrupts |= 1 << n_IRQ; | |
59 | cpu_interrupt(env, CPU_INTERRUPT_HARD); | |
60 | } else { | |
61 | env->pending_interrupts &= ~(1 << n_IRQ); | |
62 | if (env->pending_interrupts == 0) | |
63 | cpu_reset_interrupt(env, CPU_INTERRUPT_HARD); | |
64 | } | |
fc87e185 AG |
65 | |
66 | if (old_pending != env->pending_interrupts) { | |
67 | #ifdef CONFIG_KVM | |
68 | kvmppc_set_interrupt(env, n_IRQ, level); | |
69 | #endif | |
70 | } | |
71 | ||
d12d51d5 | 72 | LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32 |
aae9366a | 73 | "req %08x\n", __func__, env, n_IRQ, level, |
a496775f | 74 | env->pending_interrupts, env->interrupt_request); |
47103572 JM |
75 | } |
76 | ||
e9df014c JM |
77 | /* PowerPC 6xx / 7xx internal IRQ controller */ |
78 | static void ppc6xx_set_irq (void *opaque, int pin, int level) | |
d537cf6c | 79 | { |
e2684c0b | 80 | CPUPPCState *env = opaque; |
e9df014c | 81 | int cur_level; |
d537cf6c | 82 | |
d12d51d5 | 83 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, |
a496775f | 84 | env, pin, level); |
e9df014c JM |
85 | cur_level = (env->irq_input_state >> pin) & 1; |
86 | /* Don't generate spurious events */ | |
24be5ae3 | 87 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { |
e9df014c | 88 | switch (pin) { |
dbdd2506 JM |
89 | case PPC6xx_INPUT_TBEN: |
90 | /* Level sensitive - active high */ | |
d12d51d5 | 91 | LOG_IRQ("%s: %s the time base\n", |
dbdd2506 | 92 | __func__, level ? "start" : "stop"); |
dbdd2506 JM |
93 | if (level) { |
94 | cpu_ppc_tb_start(env); | |
95 | } else { | |
96 | cpu_ppc_tb_stop(env); | |
97 | } | |
24be5ae3 JM |
98 | case PPC6xx_INPUT_INT: |
99 | /* Level sensitive - active high */ | |
d12d51d5 | 100 | LOG_IRQ("%s: set the external IRQ state to %d\n", |
a496775f | 101 | __func__, level); |
e9df014c JM |
102 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); |
103 | break; | |
24be5ae3 | 104 | case PPC6xx_INPUT_SMI: |
e9df014c | 105 | /* Level sensitive - active high */ |
d12d51d5 | 106 | LOG_IRQ("%s: set the SMI IRQ state to %d\n", |
a496775f | 107 | __func__, level); |
e9df014c JM |
108 | ppc_set_irq(env, PPC_INTERRUPT_SMI, level); |
109 | break; | |
24be5ae3 | 110 | case PPC6xx_INPUT_MCP: |
e9df014c JM |
111 | /* Negative edge sensitive */ |
112 | /* XXX: TODO: actual reaction may depends on HID0 status | |
113 | * 603/604/740/750: check HID0[EMCP] | |
114 | */ | |
115 | if (cur_level == 1 && level == 0) { | |
d12d51d5 | 116 | LOG_IRQ("%s: raise machine check state\n", |
a496775f | 117 | __func__); |
e9df014c JM |
118 | ppc_set_irq(env, PPC_INTERRUPT_MCK, 1); |
119 | } | |
120 | break; | |
24be5ae3 | 121 | case PPC6xx_INPUT_CKSTP_IN: |
e9df014c JM |
122 | /* Level sensitive - active low */ |
123 | /* XXX: TODO: relay the signal to CKSTP_OUT pin */ | |
e63ecc6f | 124 | /* XXX: Note that the only way to restart the CPU is to reset it */ |
e9df014c | 125 | if (level) { |
d12d51d5 | 126 | LOG_IRQ("%s: stop the CPU\n", __func__); |
e9df014c | 127 | env->halted = 1; |
e9df014c JM |
128 | } |
129 | break; | |
24be5ae3 | 130 | case PPC6xx_INPUT_HRESET: |
e9df014c JM |
131 | /* Level sensitive - active low */ |
132 | if (level) { | |
d12d51d5 | 133 | LOG_IRQ("%s: reset the CPU\n", __func__); |
fc0b2c0f | 134 | cpu_interrupt(env, CPU_INTERRUPT_RESET); |
e9df014c JM |
135 | } |
136 | break; | |
24be5ae3 | 137 | case PPC6xx_INPUT_SRESET: |
d12d51d5 | 138 | LOG_IRQ("%s: set the RESET IRQ state to %d\n", |
a496775f | 139 | __func__, level); |
e9df014c JM |
140 | ppc_set_irq(env, PPC_INTERRUPT_RESET, level); |
141 | break; | |
142 | default: | |
143 | /* Unknown pin - do nothing */ | |
d12d51d5 | 144 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); |
e9df014c JM |
145 | return; |
146 | } | |
147 | if (level) | |
148 | env->irq_input_state |= 1 << pin; | |
149 | else | |
150 | env->irq_input_state &= ~(1 << pin); | |
d537cf6c PB |
151 | } |
152 | } | |
153 | ||
e2684c0b | 154 | void ppc6xx_irq_init (CPUPPCState *env) |
47103572 | 155 | { |
7b62a955 JM |
156 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, env, |
157 | PPC6xx_INPUT_NB); | |
47103572 JM |
158 | } |
159 | ||
00af685f | 160 | #if defined(TARGET_PPC64) |
d0dfae6e JM |
161 | /* PowerPC 970 internal IRQ controller */ |
162 | static void ppc970_set_irq (void *opaque, int pin, int level) | |
163 | { | |
e2684c0b | 164 | CPUPPCState *env = opaque; |
d0dfae6e JM |
165 | int cur_level; |
166 | ||
d12d51d5 | 167 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, |
d0dfae6e | 168 | env, pin, level); |
d0dfae6e JM |
169 | cur_level = (env->irq_input_state >> pin) & 1; |
170 | /* Don't generate spurious events */ | |
171 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { | |
172 | switch (pin) { | |
173 | case PPC970_INPUT_INT: | |
174 | /* Level sensitive - active high */ | |
d12d51d5 | 175 | LOG_IRQ("%s: set the external IRQ state to %d\n", |
d0dfae6e | 176 | __func__, level); |
d0dfae6e JM |
177 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); |
178 | break; | |
179 | case PPC970_INPUT_THINT: | |
180 | /* Level sensitive - active high */ | |
d12d51d5 | 181 | LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__, |
d0dfae6e | 182 | level); |
d0dfae6e JM |
183 | ppc_set_irq(env, PPC_INTERRUPT_THERM, level); |
184 | break; | |
185 | case PPC970_INPUT_MCP: | |
186 | /* Negative edge sensitive */ | |
187 | /* XXX: TODO: actual reaction may depends on HID0 status | |
188 | * 603/604/740/750: check HID0[EMCP] | |
189 | */ | |
190 | if (cur_level == 1 && level == 0) { | |
d12d51d5 | 191 | LOG_IRQ("%s: raise machine check state\n", |
d0dfae6e | 192 | __func__); |
d0dfae6e JM |
193 | ppc_set_irq(env, PPC_INTERRUPT_MCK, 1); |
194 | } | |
195 | break; | |
196 | case PPC970_INPUT_CKSTP: | |
197 | /* Level sensitive - active low */ | |
198 | /* XXX: TODO: relay the signal to CKSTP_OUT pin */ | |
199 | if (level) { | |
d12d51d5 | 200 | LOG_IRQ("%s: stop the CPU\n", __func__); |
d0dfae6e JM |
201 | env->halted = 1; |
202 | } else { | |
d12d51d5 | 203 | LOG_IRQ("%s: restart the CPU\n", __func__); |
d0dfae6e | 204 | env->halted = 0; |
94ad5b00 | 205 | qemu_cpu_kick(env); |
d0dfae6e JM |
206 | } |
207 | break; | |
208 | case PPC970_INPUT_HRESET: | |
209 | /* Level sensitive - active low */ | |
210 | if (level) { | |
fc0b2c0f | 211 | cpu_interrupt(env, CPU_INTERRUPT_RESET); |
d0dfae6e JM |
212 | } |
213 | break; | |
214 | case PPC970_INPUT_SRESET: | |
d12d51d5 | 215 | LOG_IRQ("%s: set the RESET IRQ state to %d\n", |
d0dfae6e | 216 | __func__, level); |
d0dfae6e JM |
217 | ppc_set_irq(env, PPC_INTERRUPT_RESET, level); |
218 | break; | |
219 | case PPC970_INPUT_TBEN: | |
d12d51d5 | 220 | LOG_IRQ("%s: set the TBEN state to %d\n", __func__, |
d0dfae6e | 221 | level); |
d0dfae6e JM |
222 | /* XXX: TODO */ |
223 | break; | |
224 | default: | |
225 | /* Unknown pin - do nothing */ | |
d12d51d5 | 226 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); |
d0dfae6e JM |
227 | return; |
228 | } | |
229 | if (level) | |
230 | env->irq_input_state |= 1 << pin; | |
231 | else | |
232 | env->irq_input_state &= ~(1 << pin); | |
233 | } | |
234 | } | |
235 | ||
e2684c0b | 236 | void ppc970_irq_init (CPUPPCState *env) |
d0dfae6e | 237 | { |
7b62a955 JM |
238 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, env, |
239 | PPC970_INPUT_NB); | |
d0dfae6e | 240 | } |
9d52e907 DG |
241 | |
242 | /* POWER7 internal IRQ controller */ | |
243 | static void power7_set_irq (void *opaque, int pin, int level) | |
244 | { | |
e2684c0b | 245 | CPUPPCState *env = opaque; |
9d52e907 DG |
246 | |
247 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, | |
248 | env, pin, level); | |
9d52e907 DG |
249 | |
250 | switch (pin) { | |
251 | case POWER7_INPUT_INT: | |
252 | /* Level sensitive - active high */ | |
253 | LOG_IRQ("%s: set the external IRQ state to %d\n", | |
254 | __func__, level); | |
255 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); | |
256 | break; | |
257 | default: | |
258 | /* Unknown pin - do nothing */ | |
259 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); | |
260 | return; | |
261 | } | |
262 | if (level) { | |
263 | env->irq_input_state |= 1 << pin; | |
264 | } else { | |
265 | env->irq_input_state &= ~(1 << pin); | |
266 | } | |
267 | } | |
268 | ||
e2684c0b | 269 | void ppcPOWER7_irq_init (CPUPPCState *env) |
9d52e907 DG |
270 | { |
271 | env->irq_inputs = (void **)qemu_allocate_irqs(&power7_set_irq, env, | |
272 | POWER7_INPUT_NB); | |
273 | } | |
00af685f | 274 | #endif /* defined(TARGET_PPC64) */ |
d0dfae6e | 275 | |
4e290a0b JM |
276 | /* PowerPC 40x internal IRQ controller */ |
277 | static void ppc40x_set_irq (void *opaque, int pin, int level) | |
24be5ae3 | 278 | { |
e2684c0b | 279 | CPUPPCState *env = opaque; |
24be5ae3 JM |
280 | int cur_level; |
281 | ||
d12d51d5 | 282 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, |
8ecc7913 | 283 | env, pin, level); |
24be5ae3 JM |
284 | cur_level = (env->irq_input_state >> pin) & 1; |
285 | /* Don't generate spurious events */ | |
286 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { | |
287 | switch (pin) { | |
4e290a0b | 288 | case PPC40x_INPUT_RESET_SYS: |
8ecc7913 | 289 | if (level) { |
d12d51d5 | 290 | LOG_IRQ("%s: reset the PowerPC system\n", |
8ecc7913 | 291 | __func__); |
8ecc7913 JM |
292 | ppc40x_system_reset(env); |
293 | } | |
294 | break; | |
4e290a0b | 295 | case PPC40x_INPUT_RESET_CHIP: |
8ecc7913 | 296 | if (level) { |
d12d51d5 | 297 | LOG_IRQ("%s: reset the PowerPC chip\n", __func__); |
8ecc7913 JM |
298 | ppc40x_chip_reset(env); |
299 | } | |
300 | break; | |
4e290a0b | 301 | case PPC40x_INPUT_RESET_CORE: |
24be5ae3 JM |
302 | /* XXX: TODO: update DBSR[MRR] */ |
303 | if (level) { | |
d12d51d5 | 304 | LOG_IRQ("%s: reset the PowerPC core\n", __func__); |
8ecc7913 | 305 | ppc40x_core_reset(env); |
24be5ae3 JM |
306 | } |
307 | break; | |
4e290a0b | 308 | case PPC40x_INPUT_CINT: |
24be5ae3 | 309 | /* Level sensitive - active high */ |
d12d51d5 | 310 | LOG_IRQ("%s: set the critical IRQ state to %d\n", |
8ecc7913 | 311 | __func__, level); |
4e290a0b | 312 | ppc_set_irq(env, PPC_INTERRUPT_CEXT, level); |
24be5ae3 | 313 | break; |
4e290a0b | 314 | case PPC40x_INPUT_INT: |
24be5ae3 | 315 | /* Level sensitive - active high */ |
d12d51d5 | 316 | LOG_IRQ("%s: set the external IRQ state to %d\n", |
a496775f | 317 | __func__, level); |
24be5ae3 JM |
318 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); |
319 | break; | |
4e290a0b | 320 | case PPC40x_INPUT_HALT: |
24be5ae3 JM |
321 | /* Level sensitive - active low */ |
322 | if (level) { | |
d12d51d5 | 323 | LOG_IRQ("%s: stop the CPU\n", __func__); |
24be5ae3 JM |
324 | env->halted = 1; |
325 | } else { | |
d12d51d5 | 326 | LOG_IRQ("%s: restart the CPU\n", __func__); |
24be5ae3 | 327 | env->halted = 0; |
94ad5b00 | 328 | qemu_cpu_kick(env); |
24be5ae3 JM |
329 | } |
330 | break; | |
4e290a0b | 331 | case PPC40x_INPUT_DEBUG: |
24be5ae3 | 332 | /* Level sensitive - active high */ |
d12d51d5 | 333 | LOG_IRQ("%s: set the debug pin state to %d\n", |
a496775f | 334 | __func__, level); |
a750fc0b | 335 | ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level); |
24be5ae3 JM |
336 | break; |
337 | default: | |
338 | /* Unknown pin - do nothing */ | |
d12d51d5 | 339 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); |
24be5ae3 JM |
340 | return; |
341 | } | |
342 | if (level) | |
343 | env->irq_input_state |= 1 << pin; | |
344 | else | |
345 | env->irq_input_state &= ~(1 << pin); | |
346 | } | |
347 | } | |
348 | ||
e2684c0b | 349 | void ppc40x_irq_init (CPUPPCState *env) |
24be5ae3 | 350 | { |
4e290a0b JM |
351 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq, |
352 | env, PPC40x_INPUT_NB); | |
24be5ae3 JM |
353 | } |
354 | ||
9fdc60bf AJ |
355 | /* PowerPC E500 internal IRQ controller */ |
356 | static void ppce500_set_irq (void *opaque, int pin, int level) | |
357 | { | |
e2684c0b | 358 | CPUPPCState *env = opaque; |
9fdc60bf AJ |
359 | int cur_level; |
360 | ||
361 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, | |
362 | env, pin, level); | |
363 | cur_level = (env->irq_input_state >> pin) & 1; | |
364 | /* Don't generate spurious events */ | |
365 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { | |
366 | switch (pin) { | |
367 | case PPCE500_INPUT_MCK: | |
368 | if (level) { | |
369 | LOG_IRQ("%s: reset the PowerPC system\n", | |
370 | __func__); | |
371 | qemu_system_reset_request(); | |
372 | } | |
373 | break; | |
374 | case PPCE500_INPUT_RESET_CORE: | |
375 | if (level) { | |
376 | LOG_IRQ("%s: reset the PowerPC core\n", __func__); | |
377 | ppc_set_irq(env, PPC_INTERRUPT_MCK, level); | |
378 | } | |
379 | break; | |
380 | case PPCE500_INPUT_CINT: | |
381 | /* Level sensitive - active high */ | |
382 | LOG_IRQ("%s: set the critical IRQ state to %d\n", | |
383 | __func__, level); | |
384 | ppc_set_irq(env, PPC_INTERRUPT_CEXT, level); | |
385 | break; | |
386 | case PPCE500_INPUT_INT: | |
387 | /* Level sensitive - active high */ | |
388 | LOG_IRQ("%s: set the core IRQ state to %d\n", | |
389 | __func__, level); | |
390 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); | |
391 | break; | |
392 | case PPCE500_INPUT_DEBUG: | |
393 | /* Level sensitive - active high */ | |
394 | LOG_IRQ("%s: set the debug pin state to %d\n", | |
395 | __func__, level); | |
396 | ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level); | |
397 | break; | |
398 | default: | |
399 | /* Unknown pin - do nothing */ | |
400 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); | |
401 | return; | |
402 | } | |
403 | if (level) | |
404 | env->irq_input_state |= 1 << pin; | |
405 | else | |
406 | env->irq_input_state &= ~(1 << pin); | |
407 | } | |
408 | } | |
409 | ||
e2684c0b | 410 | void ppce500_irq_init (CPUPPCState *env) |
9fdc60bf AJ |
411 | { |
412 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq, | |
413 | env, PPCE500_INPUT_NB); | |
414 | } | |
9fddaa0c | 415 | /*****************************************************************************/ |
e9df014c | 416 | /* PowerPC time base and decrementer emulation */ |
9fddaa0c | 417 | |
ddd1055b | 418 | uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset) |
9fddaa0c FB |
419 | { |
420 | /* TB time in tb periods */ | |
6ee093c9 | 421 | return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset; |
9fddaa0c FB |
422 | } |
423 | ||
e2684c0b | 424 | uint64_t cpu_ppc_load_tbl (CPUPPCState *env) |
9fddaa0c | 425 | { |
c227f099 | 426 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c FB |
427 | uint64_t tb; |
428 | ||
90dc8812 SW |
429 | if (kvm_enabled()) { |
430 | return env->spr[SPR_TBL]; | |
431 | } | |
432 | ||
74475455 | 433 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
d12d51d5 | 434 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
9fddaa0c | 435 | |
e3ea6529 | 436 | return tb; |
9fddaa0c FB |
437 | } |
438 | ||
e2684c0b | 439 | static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState *env) |
9fddaa0c | 440 | { |
c227f099 | 441 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c FB |
442 | uint64_t tb; |
443 | ||
74475455 | 444 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
d12d51d5 | 445 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
76a66253 | 446 | |
9fddaa0c FB |
447 | return tb >> 32; |
448 | } | |
449 | ||
e2684c0b | 450 | uint32_t cpu_ppc_load_tbu (CPUPPCState *env) |
8a84de23 | 451 | { |
90dc8812 SW |
452 | if (kvm_enabled()) { |
453 | return env->spr[SPR_TBU]; | |
454 | } | |
455 | ||
8a84de23 JM |
456 | return _cpu_ppc_load_tbu(env); |
457 | } | |
458 | ||
c227f099 | 459 | static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk, |
636aa200 | 460 | int64_t *tb_offsetp, uint64_t value) |
9fddaa0c | 461 | { |
6ee093c9 | 462 | *tb_offsetp = value - muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()); |
d12d51d5 | 463 | LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n", |
aae9366a | 464 | __func__, value, *tb_offsetp); |
9fddaa0c FB |
465 | } |
466 | ||
e2684c0b | 467 | void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value) |
a062e36c | 468 | { |
c227f099 | 469 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
470 | uint64_t tb; |
471 | ||
74475455 | 472 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
a062e36c | 473 | tb &= 0xFFFFFFFF00000000ULL; |
74475455 | 474 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 | 475 | &tb_env->tb_offset, tb | (uint64_t)value); |
a062e36c JM |
476 | } |
477 | ||
e2684c0b | 478 | static inline void _cpu_ppc_store_tbu(CPUPPCState *env, uint32_t value) |
9fddaa0c | 479 | { |
c227f099 | 480 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c | 481 | uint64_t tb; |
9fddaa0c | 482 | |
74475455 | 483 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
a062e36c | 484 | tb &= 0x00000000FFFFFFFFULL; |
74475455 | 485 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 | 486 | &tb_env->tb_offset, ((uint64_t)value << 32) | tb); |
9fddaa0c FB |
487 | } |
488 | ||
e2684c0b | 489 | void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value) |
8a84de23 JM |
490 | { |
491 | _cpu_ppc_store_tbu(env, value); | |
492 | } | |
493 | ||
e2684c0b | 494 | uint64_t cpu_ppc_load_atbl (CPUPPCState *env) |
a062e36c | 495 | { |
c227f099 | 496 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
497 | uint64_t tb; |
498 | ||
74475455 | 499 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
d12d51d5 | 500 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
a062e36c | 501 | |
b711de95 | 502 | return tb; |
a062e36c JM |
503 | } |
504 | ||
e2684c0b | 505 | uint32_t cpu_ppc_load_atbu (CPUPPCState *env) |
a062e36c | 506 | { |
c227f099 | 507 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
508 | uint64_t tb; |
509 | ||
74475455 | 510 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
d12d51d5 | 511 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
a062e36c JM |
512 | |
513 | return tb >> 32; | |
514 | } | |
515 | ||
e2684c0b | 516 | void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value) |
a062e36c | 517 | { |
c227f099 | 518 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
519 | uint64_t tb; |
520 | ||
74475455 | 521 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
a062e36c | 522 | tb &= 0xFFFFFFFF00000000ULL; |
74475455 | 523 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 | 524 | &tb_env->atb_offset, tb | (uint64_t)value); |
a062e36c JM |
525 | } |
526 | ||
e2684c0b | 527 | void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value) |
9fddaa0c | 528 | { |
c227f099 | 529 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c | 530 | uint64_t tb; |
9fddaa0c | 531 | |
74475455 | 532 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
a062e36c | 533 | tb &= 0x00000000FFFFFFFFULL; |
74475455 | 534 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 JM |
535 | &tb_env->atb_offset, ((uint64_t)value << 32) | tb); |
536 | } | |
537 | ||
e2684c0b | 538 | static void cpu_ppc_tb_stop (CPUPPCState *env) |
dbdd2506 | 539 | { |
c227f099 | 540 | ppc_tb_t *tb_env = env->tb_env; |
dbdd2506 JM |
541 | uint64_t tb, atb, vmclk; |
542 | ||
543 | /* If the time base is already frozen, do nothing */ | |
544 | if (tb_env->tb_freq != 0) { | |
74475455 | 545 | vmclk = qemu_get_clock_ns(vm_clock); |
dbdd2506 JM |
546 | /* Get the time base */ |
547 | tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset); | |
548 | /* Get the alternate time base */ | |
549 | atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset); | |
550 | /* Store the time base value (ie compute the current offset) */ | |
551 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb); | |
552 | /* Store the alternate time base value (compute the current offset) */ | |
553 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb); | |
554 | /* Set the time base frequency to zero */ | |
555 | tb_env->tb_freq = 0; | |
556 | /* Now, the time bases are frozen to tb_offset / atb_offset value */ | |
557 | } | |
558 | } | |
559 | ||
e2684c0b | 560 | static void cpu_ppc_tb_start (CPUPPCState *env) |
dbdd2506 | 561 | { |
c227f099 | 562 | ppc_tb_t *tb_env = env->tb_env; |
dbdd2506 | 563 | uint64_t tb, atb, vmclk; |
aae9366a | 564 | |
dbdd2506 JM |
565 | /* If the time base is not frozen, do nothing */ |
566 | if (tb_env->tb_freq == 0) { | |
74475455 | 567 | vmclk = qemu_get_clock_ns(vm_clock); |
dbdd2506 JM |
568 | /* Get the time base from tb_offset */ |
569 | tb = tb_env->tb_offset; | |
570 | /* Get the alternate time base from atb_offset */ | |
571 | atb = tb_env->atb_offset; | |
572 | /* Restore the tb frequency from the decrementer frequency */ | |
573 | tb_env->tb_freq = tb_env->decr_freq; | |
574 | /* Store the time base value */ | |
575 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb); | |
576 | /* Store the alternate time base value */ | |
577 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb); | |
578 | } | |
9fddaa0c FB |
579 | } |
580 | ||
e2684c0b | 581 | static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next) |
9fddaa0c | 582 | { |
c227f099 | 583 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c | 584 | uint32_t decr; |
4e588a4d | 585 | int64_t diff; |
9fddaa0c | 586 | |
74475455 | 587 | diff = next - qemu_get_clock_ns(vm_clock); |
ddd1055b | 588 | if (diff >= 0) { |
6ee093c9 | 589 | decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec()); |
ddd1055b FC |
590 | } else if (tb_env->flags & PPC_TIMER_BOOKE) { |
591 | decr = 0; | |
592 | } else { | |
6ee093c9 | 593 | decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec()); |
ddd1055b | 594 | } |
d12d51d5 | 595 | LOG_TB("%s: %08" PRIx32 "\n", __func__, decr); |
76a66253 | 596 | |
9fddaa0c FB |
597 | return decr; |
598 | } | |
599 | ||
e2684c0b | 600 | uint32_t cpu_ppc_load_decr (CPUPPCState *env) |
58a7d328 | 601 | { |
c227f099 | 602 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 | 603 | |
90dc8812 SW |
604 | if (kvm_enabled()) { |
605 | return env->spr[SPR_DECR]; | |
606 | } | |
607 | ||
f55e9d9a | 608 | return _cpu_ppc_load_decr(env, tb_env->decr_next); |
58a7d328 JM |
609 | } |
610 | ||
e2684c0b | 611 | uint32_t cpu_ppc_load_hdecr (CPUPPCState *env) |
58a7d328 | 612 | { |
c227f099 | 613 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 | 614 | |
f55e9d9a | 615 | return _cpu_ppc_load_decr(env, tb_env->hdecr_next); |
58a7d328 JM |
616 | } |
617 | ||
e2684c0b | 618 | uint64_t cpu_ppc_load_purr (CPUPPCState *env) |
58a7d328 | 619 | { |
c227f099 | 620 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 JM |
621 | uint64_t diff; |
622 | ||
74475455 | 623 | diff = qemu_get_clock_ns(vm_clock) - tb_env->purr_start; |
b33c17e1 | 624 | |
6ee093c9 | 625 | return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, get_ticks_per_sec()); |
58a7d328 | 626 | } |
58a7d328 | 627 | |
9fddaa0c FB |
628 | /* When decrementer expires, |
629 | * all we need to do is generate or queue a CPU exception | |
630 | */ | |
e2684c0b | 631 | static inline void cpu_ppc_decr_excp(CPUPPCState *env) |
9fddaa0c FB |
632 | { |
633 | /* Raise it */ | |
d12d51d5 | 634 | LOG_TB("raise decrementer exception\n"); |
47103572 | 635 | ppc_set_irq(env, PPC_INTERRUPT_DECR, 1); |
9fddaa0c FB |
636 | } |
637 | ||
e2684c0b | 638 | static inline void cpu_ppc_hdecr_excp(CPUPPCState *env) |
58a7d328 JM |
639 | { |
640 | /* Raise it */ | |
d12d51d5 | 641 | LOG_TB("raise decrementer exception\n"); |
58a7d328 JM |
642 | ppc_set_irq(env, PPC_INTERRUPT_HDECR, 1); |
643 | } | |
644 | ||
e2684c0b | 645 | static void __cpu_ppc_store_decr (CPUPPCState *env, uint64_t *nextp, |
b33c17e1 | 646 | struct QEMUTimer *timer, |
e2684c0b | 647 | void (*raise_excp)(CPUPPCState *), |
b33c17e1 JM |
648 | uint32_t decr, uint32_t value, |
649 | int is_excp) | |
9fddaa0c | 650 | { |
c227f099 | 651 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c FB |
652 | uint64_t now, next; |
653 | ||
d12d51d5 | 654 | LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__, |
aae9366a | 655 | decr, value); |
55f7d4b0 DG |
656 | |
657 | if (kvm_enabled()) { | |
658 | /* KVM handles decrementer exceptions, we don't need our own timer */ | |
659 | return; | |
660 | } | |
661 | ||
74475455 | 662 | now = qemu_get_clock_ns(vm_clock); |
6ee093c9 | 663 | next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq); |
ddd1055b | 664 | if (is_excp) { |
58a7d328 | 665 | next += *nextp - now; |
ddd1055b FC |
666 | } |
667 | if (next == now) { | |
76a66253 | 668 | next++; |
ddd1055b | 669 | } |
58a7d328 | 670 | *nextp = next; |
9fddaa0c | 671 | /* Adjust timer */ |
58a7d328 | 672 | qemu_mod_timer(timer, next); |
ddd1055b FC |
673 | |
674 | /* If we set a negative value and the decrementer was positive, raise an | |
675 | * exception. | |
9fddaa0c | 676 | */ |
ddd1055b FC |
677 | if ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) |
678 | && (value & 0x80000000) | |
679 | && !(decr & 0x80000000)) { | |
58a7d328 | 680 | (*raise_excp)(env); |
ddd1055b | 681 | } |
58a7d328 JM |
682 | } |
683 | ||
e2684c0b | 684 | static inline void _cpu_ppc_store_decr(CPUPPCState *env, uint32_t decr, |
636aa200 | 685 | uint32_t value, int is_excp) |
58a7d328 | 686 | { |
c227f099 | 687 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 JM |
688 | |
689 | __cpu_ppc_store_decr(env, &tb_env->decr_next, tb_env->decr_timer, | |
690 | &cpu_ppc_decr_excp, decr, value, is_excp); | |
9fddaa0c FB |
691 | } |
692 | ||
e2684c0b | 693 | void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value) |
9fddaa0c FB |
694 | { |
695 | _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0); | |
696 | } | |
697 | ||
698 | static void cpu_ppc_decr_cb (void *opaque) | |
699 | { | |
700 | _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1); | |
701 | } | |
702 | ||
e2684c0b | 703 | static inline void _cpu_ppc_store_hdecr(CPUPPCState *env, uint32_t hdecr, |
636aa200 | 704 | uint32_t value, int is_excp) |
58a7d328 | 705 | { |
c227f099 | 706 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 | 707 | |
b172c56a JM |
708 | if (tb_env->hdecr_timer != NULL) { |
709 | __cpu_ppc_store_decr(env, &tb_env->hdecr_next, tb_env->hdecr_timer, | |
710 | &cpu_ppc_hdecr_excp, hdecr, value, is_excp); | |
711 | } | |
58a7d328 JM |
712 | } |
713 | ||
e2684c0b | 714 | void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value) |
58a7d328 JM |
715 | { |
716 | _cpu_ppc_store_hdecr(env, cpu_ppc_load_hdecr(env), value, 0); | |
717 | } | |
718 | ||
719 | static void cpu_ppc_hdecr_cb (void *opaque) | |
720 | { | |
721 | _cpu_ppc_store_hdecr(opaque, 0x00000000, 0xFFFFFFFF, 1); | |
722 | } | |
723 | ||
e2684c0b | 724 | void cpu_ppc_store_purr (CPUPPCState *env, uint64_t value) |
58a7d328 | 725 | { |
c227f099 | 726 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 JM |
727 | |
728 | tb_env->purr_load = value; | |
74475455 | 729 | tb_env->purr_start = qemu_get_clock_ns(vm_clock); |
58a7d328 | 730 | } |
58a7d328 | 731 | |
8ecc7913 JM |
732 | static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq) |
733 | { | |
e2684c0b | 734 | CPUPPCState *env = opaque; |
c227f099 | 735 | ppc_tb_t *tb_env = env->tb_env; |
8ecc7913 JM |
736 | |
737 | tb_env->tb_freq = freq; | |
dbdd2506 | 738 | tb_env->decr_freq = freq; |
8ecc7913 JM |
739 | /* There is a bug in Linux 2.4 kernels: |
740 | * if a decrementer exception is pending when it enables msr_ee at startup, | |
741 | * it's not ready to handle it... | |
742 | */ | |
743 | _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0); | |
58a7d328 JM |
744 | _cpu_ppc_store_hdecr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0); |
745 | cpu_ppc_store_purr(env, 0x0000000000000000ULL); | |
8ecc7913 JM |
746 | } |
747 | ||
9fddaa0c | 748 | /* Set up (once) timebase frequency (in Hz) */ |
e2684c0b | 749 | clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq) |
9fddaa0c | 750 | { |
c227f099 | 751 | ppc_tb_t *tb_env; |
9fddaa0c | 752 | |
7267c094 | 753 | tb_env = g_malloc0(sizeof(ppc_tb_t)); |
9fddaa0c | 754 | env->tb_env = tb_env; |
ddd1055b | 755 | tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED; |
8ecc7913 | 756 | /* Create new timer */ |
74475455 | 757 | tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_decr_cb, env); |
b172c56a JM |
758 | if (0) { |
759 | /* XXX: find a suitable condition to enable the hypervisor decrementer | |
760 | */ | |
74475455 | 761 | tb_env->hdecr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_hdecr_cb, env); |
b172c56a JM |
762 | } else { |
763 | tb_env->hdecr_timer = NULL; | |
764 | } | |
8ecc7913 | 765 | cpu_ppc_set_tb_clk(env, freq); |
9fddaa0c | 766 | |
8ecc7913 | 767 | return &cpu_ppc_set_tb_clk; |
9fddaa0c FB |
768 | } |
769 | ||
76a66253 | 770 | /* Specific helpers for POWER & PowerPC 601 RTC */ |
b1d8e52e | 771 | #if 0 |
e2684c0b | 772 | static clk_setup_cb cpu_ppc601_rtc_init (CPUPPCState *env) |
76a66253 JM |
773 | { |
774 | return cpu_ppc_tb_init(env, 7812500); | |
775 | } | |
b1d8e52e | 776 | #endif |
76a66253 | 777 | |
e2684c0b | 778 | void cpu_ppc601_store_rtcu (CPUPPCState *env, uint32_t value) |
8a84de23 JM |
779 | { |
780 | _cpu_ppc_store_tbu(env, value); | |
781 | } | |
76a66253 | 782 | |
e2684c0b | 783 | uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env) |
8a84de23 JM |
784 | { |
785 | return _cpu_ppc_load_tbu(env); | |
786 | } | |
76a66253 | 787 | |
e2684c0b | 788 | void cpu_ppc601_store_rtcl (CPUPPCState *env, uint32_t value) |
76a66253 JM |
789 | { |
790 | cpu_ppc_store_tbl(env, value & 0x3FFFFF80); | |
791 | } | |
792 | ||
e2684c0b | 793 | uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env) |
76a66253 JM |
794 | { |
795 | return cpu_ppc_load_tbl(env) & 0x3FFFFF80; | |
796 | } | |
797 | ||
636aaad7 | 798 | /*****************************************************************************/ |
ddd1055b | 799 | /* PowerPC 40x timers */ |
636aaad7 JM |
800 | |
801 | /* PIT, FIT & WDT */ | |
ddd1055b FC |
802 | typedef struct ppc40x_timer_t ppc40x_timer_t; |
803 | struct ppc40x_timer_t { | |
636aaad7 JM |
804 | uint64_t pit_reload; /* PIT auto-reload value */ |
805 | uint64_t fit_next; /* Tick for next FIT interrupt */ | |
806 | struct QEMUTimer *fit_timer; | |
807 | uint64_t wdt_next; /* Tick for next WDT interrupt */ | |
808 | struct QEMUTimer *wdt_timer; | |
d63cb48d EI |
809 | |
810 | /* 405 have the PIT, 440 have a DECR. */ | |
811 | unsigned int decr_excp; | |
636aaad7 | 812 | }; |
3b46e624 | 813 | |
636aaad7 JM |
814 | /* Fixed interval timer */ |
815 | static void cpu_4xx_fit_cb (void *opaque) | |
816 | { | |
e2684c0b | 817 | CPUPPCState *env; |
c227f099 | 818 | ppc_tb_t *tb_env; |
ddd1055b | 819 | ppc40x_timer_t *ppc40x_timer; |
636aaad7 JM |
820 | uint64_t now, next; |
821 | ||
822 | env = opaque; | |
823 | tb_env = env->tb_env; | |
ddd1055b | 824 | ppc40x_timer = tb_env->opaque; |
74475455 | 825 | now = qemu_get_clock_ns(vm_clock); |
636aaad7 JM |
826 | switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) { |
827 | case 0: | |
828 | next = 1 << 9; | |
829 | break; | |
830 | case 1: | |
831 | next = 1 << 13; | |
832 | break; | |
833 | case 2: | |
834 | next = 1 << 17; | |
835 | break; | |
836 | case 3: | |
837 | next = 1 << 21; | |
838 | break; | |
839 | default: | |
840 | /* Cannot occur, but makes gcc happy */ | |
841 | return; | |
842 | } | |
6ee093c9 | 843 | next = now + muldiv64(next, get_ticks_per_sec(), tb_env->tb_freq); |
636aaad7 JM |
844 | if (next == now) |
845 | next++; | |
ddd1055b | 846 | qemu_mod_timer(ppc40x_timer->fit_timer, next); |
636aaad7 JM |
847 | env->spr[SPR_40x_TSR] |= 1 << 26; |
848 | if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) | |
849 | ppc_set_irq(env, PPC_INTERRUPT_FIT, 1); | |
90e189ec BS |
850 | LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__, |
851 | (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1), | |
852 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); | |
636aaad7 JM |
853 | } |
854 | ||
855 | /* Programmable interval timer */ | |
e2684c0b | 856 | static void start_stop_pit (CPUPPCState *env, ppc_tb_t *tb_env, int is_excp) |
76a66253 | 857 | { |
ddd1055b | 858 | ppc40x_timer_t *ppc40x_timer; |
636aaad7 JM |
859 | uint64_t now, next; |
860 | ||
ddd1055b FC |
861 | ppc40x_timer = tb_env->opaque; |
862 | if (ppc40x_timer->pit_reload <= 1 || | |
4b6d0a4c JM |
863 | !((env->spr[SPR_40x_TCR] >> 26) & 0x1) || |
864 | (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) { | |
865 | /* Stop PIT */ | |
d12d51d5 | 866 | LOG_TB("%s: stop PIT\n", __func__); |
4b6d0a4c JM |
867 | qemu_del_timer(tb_env->decr_timer); |
868 | } else { | |
d12d51d5 | 869 | LOG_TB("%s: start PIT %016" PRIx64 "\n", |
ddd1055b | 870 | __func__, ppc40x_timer->pit_reload); |
74475455 | 871 | now = qemu_get_clock_ns(vm_clock); |
ddd1055b | 872 | next = now + muldiv64(ppc40x_timer->pit_reload, |
6ee093c9 | 873 | get_ticks_per_sec(), tb_env->decr_freq); |
4b6d0a4c JM |
874 | if (is_excp) |
875 | next += tb_env->decr_next - now; | |
636aaad7 JM |
876 | if (next == now) |
877 | next++; | |
878 | qemu_mod_timer(tb_env->decr_timer, next); | |
879 | tb_env->decr_next = next; | |
880 | } | |
4b6d0a4c JM |
881 | } |
882 | ||
883 | static void cpu_4xx_pit_cb (void *opaque) | |
884 | { | |
e2684c0b | 885 | CPUPPCState *env; |
c227f099 | 886 | ppc_tb_t *tb_env; |
ddd1055b | 887 | ppc40x_timer_t *ppc40x_timer; |
4b6d0a4c JM |
888 | |
889 | env = opaque; | |
890 | tb_env = env->tb_env; | |
ddd1055b | 891 | ppc40x_timer = tb_env->opaque; |
636aaad7 JM |
892 | env->spr[SPR_40x_TSR] |= 1 << 27; |
893 | if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) | |
ddd1055b | 894 | ppc_set_irq(env, ppc40x_timer->decr_excp, 1); |
4b6d0a4c | 895 | start_stop_pit(env, tb_env, 1); |
90e189ec BS |
896 | LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " " |
897 | "%016" PRIx64 "\n", __func__, | |
898 | (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1), | |
899 | (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1), | |
900 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR], | |
ddd1055b | 901 | ppc40x_timer->pit_reload); |
636aaad7 JM |
902 | } |
903 | ||
904 | /* Watchdog timer */ | |
905 | static void cpu_4xx_wdt_cb (void *opaque) | |
906 | { | |
e2684c0b | 907 | CPUPPCState *env; |
c227f099 | 908 | ppc_tb_t *tb_env; |
ddd1055b | 909 | ppc40x_timer_t *ppc40x_timer; |
636aaad7 JM |
910 | uint64_t now, next; |
911 | ||
912 | env = opaque; | |
913 | tb_env = env->tb_env; | |
ddd1055b | 914 | ppc40x_timer = tb_env->opaque; |
74475455 | 915 | now = qemu_get_clock_ns(vm_clock); |
636aaad7 JM |
916 | switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) { |
917 | case 0: | |
918 | next = 1 << 17; | |
919 | break; | |
920 | case 1: | |
921 | next = 1 << 21; | |
922 | break; | |
923 | case 2: | |
924 | next = 1 << 25; | |
925 | break; | |
926 | case 3: | |
927 | next = 1 << 29; | |
928 | break; | |
929 | default: | |
930 | /* Cannot occur, but makes gcc happy */ | |
931 | return; | |
932 | } | |
6ee093c9 | 933 | next = now + muldiv64(next, get_ticks_per_sec(), tb_env->decr_freq); |
636aaad7 JM |
934 | if (next == now) |
935 | next++; | |
90e189ec BS |
936 | LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__, |
937 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); | |
636aaad7 JM |
938 | switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) { |
939 | case 0x0: | |
940 | case 0x1: | |
ddd1055b FC |
941 | qemu_mod_timer(ppc40x_timer->wdt_timer, next); |
942 | ppc40x_timer->wdt_next = next; | |
636aaad7 JM |
943 | env->spr[SPR_40x_TSR] |= 1 << 31; |
944 | break; | |
945 | case 0x2: | |
ddd1055b FC |
946 | qemu_mod_timer(ppc40x_timer->wdt_timer, next); |
947 | ppc40x_timer->wdt_next = next; | |
636aaad7 JM |
948 | env->spr[SPR_40x_TSR] |= 1 << 30; |
949 | if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) | |
950 | ppc_set_irq(env, PPC_INTERRUPT_WDT, 1); | |
951 | break; | |
952 | case 0x3: | |
953 | env->spr[SPR_40x_TSR] &= ~0x30000000; | |
954 | env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000; | |
955 | switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) { | |
956 | case 0x0: | |
957 | /* No reset */ | |
958 | break; | |
959 | case 0x1: /* Core reset */ | |
8ecc7913 JM |
960 | ppc40x_core_reset(env); |
961 | break; | |
636aaad7 | 962 | case 0x2: /* Chip reset */ |
8ecc7913 JM |
963 | ppc40x_chip_reset(env); |
964 | break; | |
636aaad7 | 965 | case 0x3: /* System reset */ |
8ecc7913 JM |
966 | ppc40x_system_reset(env); |
967 | break; | |
636aaad7 JM |
968 | } |
969 | } | |
76a66253 JM |
970 | } |
971 | ||
e2684c0b | 972 | void store_40x_pit (CPUPPCState *env, target_ulong val) |
76a66253 | 973 | { |
c227f099 | 974 | ppc_tb_t *tb_env; |
ddd1055b | 975 | ppc40x_timer_t *ppc40x_timer; |
636aaad7 JM |
976 | |
977 | tb_env = env->tb_env; | |
ddd1055b | 978 | ppc40x_timer = tb_env->opaque; |
90e189ec | 979 | LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val); |
ddd1055b | 980 | ppc40x_timer->pit_reload = val; |
4b6d0a4c | 981 | start_stop_pit(env, tb_env, 0); |
76a66253 JM |
982 | } |
983 | ||
e2684c0b | 984 | target_ulong load_40x_pit (CPUPPCState *env) |
76a66253 | 985 | { |
636aaad7 | 986 | return cpu_ppc_load_decr(env); |
76a66253 JM |
987 | } |
988 | ||
ddd1055b | 989 | static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq) |
4b6d0a4c | 990 | { |
e2684c0b | 991 | CPUPPCState *env = opaque; |
c227f099 | 992 | ppc_tb_t *tb_env = env->tb_env; |
4b6d0a4c | 993 | |
d12d51d5 | 994 | LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__, |
aae9366a | 995 | freq); |
4b6d0a4c | 996 | tb_env->tb_freq = freq; |
dbdd2506 | 997 | tb_env->decr_freq = freq; |
4b6d0a4c JM |
998 | /* XXX: we should also update all timers */ |
999 | } | |
1000 | ||
e2684c0b | 1001 | clk_setup_cb ppc_40x_timers_init (CPUPPCState *env, uint32_t freq, |
d63cb48d | 1002 | unsigned int decr_excp) |
636aaad7 | 1003 | { |
c227f099 | 1004 | ppc_tb_t *tb_env; |
ddd1055b | 1005 | ppc40x_timer_t *ppc40x_timer; |
636aaad7 | 1006 | |
7267c094 | 1007 | tb_env = g_malloc0(sizeof(ppc_tb_t)); |
8ecc7913 | 1008 | env->tb_env = tb_env; |
ddd1055b FC |
1009 | tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED; |
1010 | ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t)); | |
8ecc7913 | 1011 | tb_env->tb_freq = freq; |
dbdd2506 | 1012 | tb_env->decr_freq = freq; |
ddd1055b | 1013 | tb_env->opaque = ppc40x_timer; |
d12d51d5 | 1014 | LOG_TB("%s freq %" PRIu32 "\n", __func__, freq); |
ddd1055b | 1015 | if (ppc40x_timer != NULL) { |
636aaad7 | 1016 | /* We use decr timer for PIT */ |
74475455 | 1017 | tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_4xx_pit_cb, env); |
ddd1055b | 1018 | ppc40x_timer->fit_timer = |
74475455 | 1019 | qemu_new_timer_ns(vm_clock, &cpu_4xx_fit_cb, env); |
ddd1055b | 1020 | ppc40x_timer->wdt_timer = |
74475455 | 1021 | qemu_new_timer_ns(vm_clock, &cpu_4xx_wdt_cb, env); |
ddd1055b | 1022 | ppc40x_timer->decr_excp = decr_excp; |
636aaad7 | 1023 | } |
8ecc7913 | 1024 | |
ddd1055b | 1025 | return &ppc_40x_set_tb_clk; |
76a66253 JM |
1026 | } |
1027 | ||
2e719ba3 JM |
1028 | /*****************************************************************************/ |
1029 | /* Embedded PowerPC Device Control Registers */ | |
c227f099 AL |
1030 | typedef struct ppc_dcrn_t ppc_dcrn_t; |
1031 | struct ppc_dcrn_t { | |
2e719ba3 JM |
1032 | dcr_read_cb dcr_read; |
1033 | dcr_write_cb dcr_write; | |
1034 | void *opaque; | |
1035 | }; | |
1036 | ||
a750fc0b JM |
1037 | /* XXX: on 460, DCR addresses are 32 bits wide, |
1038 | * using DCRIPR to get the 22 upper bits of the DCR address | |
1039 | */ | |
2e719ba3 | 1040 | #define DCRN_NB 1024 |
c227f099 AL |
1041 | struct ppc_dcr_t { |
1042 | ppc_dcrn_t dcrn[DCRN_NB]; | |
2e719ba3 JM |
1043 | int (*read_error)(int dcrn); |
1044 | int (*write_error)(int dcrn); | |
1045 | }; | |
1046 | ||
73b01960 | 1047 | int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp) |
2e719ba3 | 1048 | { |
c227f099 | 1049 | ppc_dcrn_t *dcr; |
2e719ba3 JM |
1050 | |
1051 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
1052 | goto error; | |
1053 | dcr = &dcr_env->dcrn[dcrn]; | |
1054 | if (dcr->dcr_read == NULL) | |
1055 | goto error; | |
1056 | *valp = (*dcr->dcr_read)(dcr->opaque, dcrn); | |
1057 | ||
1058 | return 0; | |
1059 | ||
1060 | error: | |
1061 | if (dcr_env->read_error != NULL) | |
1062 | return (*dcr_env->read_error)(dcrn); | |
1063 | ||
1064 | return -1; | |
1065 | } | |
1066 | ||
73b01960 | 1067 | int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val) |
2e719ba3 | 1068 | { |
c227f099 | 1069 | ppc_dcrn_t *dcr; |
2e719ba3 JM |
1070 | |
1071 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
1072 | goto error; | |
1073 | dcr = &dcr_env->dcrn[dcrn]; | |
1074 | if (dcr->dcr_write == NULL) | |
1075 | goto error; | |
1076 | (*dcr->dcr_write)(dcr->opaque, dcrn, val); | |
1077 | ||
1078 | return 0; | |
1079 | ||
1080 | error: | |
1081 | if (dcr_env->write_error != NULL) | |
1082 | return (*dcr_env->write_error)(dcrn); | |
1083 | ||
1084 | return -1; | |
1085 | } | |
1086 | ||
e2684c0b | 1087 | int ppc_dcr_register (CPUPPCState *env, int dcrn, void *opaque, |
2e719ba3 JM |
1088 | dcr_read_cb dcr_read, dcr_write_cb dcr_write) |
1089 | { | |
c227f099 AL |
1090 | ppc_dcr_t *dcr_env; |
1091 | ppc_dcrn_t *dcr; | |
2e719ba3 JM |
1092 | |
1093 | dcr_env = env->dcr_env; | |
1094 | if (dcr_env == NULL) | |
1095 | return -1; | |
1096 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
1097 | return -1; | |
1098 | dcr = &dcr_env->dcrn[dcrn]; | |
1099 | if (dcr->opaque != NULL || | |
1100 | dcr->dcr_read != NULL || | |
1101 | dcr->dcr_write != NULL) | |
1102 | return -1; | |
1103 | dcr->opaque = opaque; | |
1104 | dcr->dcr_read = dcr_read; | |
1105 | dcr->dcr_write = dcr_write; | |
1106 | ||
1107 | return 0; | |
1108 | } | |
1109 | ||
e2684c0b | 1110 | int ppc_dcr_init (CPUPPCState *env, int (*read_error)(int dcrn), |
2e719ba3 JM |
1111 | int (*write_error)(int dcrn)) |
1112 | { | |
c227f099 | 1113 | ppc_dcr_t *dcr_env; |
2e719ba3 | 1114 | |
7267c094 | 1115 | dcr_env = g_malloc0(sizeof(ppc_dcr_t)); |
2e719ba3 JM |
1116 | dcr_env->read_error = read_error; |
1117 | dcr_env->write_error = write_error; | |
1118 | env->dcr_env = dcr_env; | |
1119 | ||
1120 | return 0; | |
1121 | } | |
1122 | ||
64201201 FB |
1123 | /*****************************************************************************/ |
1124 | /* Debug port */ | |
fd0bbb12 | 1125 | void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val) |
64201201 FB |
1126 | { |
1127 | addr &= 0xF; | |
1128 | switch (addr) { | |
1129 | case 0: | |
1130 | printf("%c", val); | |
1131 | break; | |
1132 | case 1: | |
1133 | printf("\n"); | |
1134 | fflush(stdout); | |
1135 | break; | |
1136 | case 2: | |
aae9366a | 1137 | printf("Set loglevel to %04" PRIx32 "\n", val); |
fd0bbb12 | 1138 | cpu_set_log(val | 0x100); |
64201201 FB |
1139 | break; |
1140 | } | |
1141 | } | |
1142 | ||
1143 | /*****************************************************************************/ | |
1144 | /* NVRAM helpers */ | |
c227f099 | 1145 | static inline uint32_t nvram_read (nvram_t *nvram, uint32_t addr) |
64201201 | 1146 | { |
3a93113a | 1147 | return (*nvram->read_fn)(nvram->opaque, addr); |
64201201 FB |
1148 | } |
1149 | ||
c227f099 | 1150 | static inline void nvram_write (nvram_t *nvram, uint32_t addr, uint32_t val) |
64201201 | 1151 | { |
3cbee15b | 1152 | (*nvram->write_fn)(nvram->opaque, addr, val); |
64201201 FB |
1153 | } |
1154 | ||
c227f099 | 1155 | void NVRAM_set_byte (nvram_t *nvram, uint32_t addr, uint8_t value) |
64201201 | 1156 | { |
3cbee15b | 1157 | nvram_write(nvram, addr, value); |
64201201 FB |
1158 | } |
1159 | ||
c227f099 | 1160 | uint8_t NVRAM_get_byte (nvram_t *nvram, uint32_t addr) |
3cbee15b JM |
1161 | { |
1162 | return nvram_read(nvram, addr); | |
1163 | } | |
1164 | ||
c227f099 | 1165 | void NVRAM_set_word (nvram_t *nvram, uint32_t addr, uint16_t value) |
3cbee15b JM |
1166 | { |
1167 | nvram_write(nvram, addr, value >> 8); | |
1168 | nvram_write(nvram, addr + 1, value & 0xFF); | |
1169 | } | |
1170 | ||
c227f099 | 1171 | uint16_t NVRAM_get_word (nvram_t *nvram, uint32_t addr) |
64201201 FB |
1172 | { |
1173 | uint16_t tmp; | |
1174 | ||
3cbee15b JM |
1175 | tmp = nvram_read(nvram, addr) << 8; |
1176 | tmp |= nvram_read(nvram, addr + 1); | |
1177 | ||
64201201 FB |
1178 | return tmp; |
1179 | } | |
1180 | ||
c227f099 | 1181 | void NVRAM_set_lword (nvram_t *nvram, uint32_t addr, uint32_t value) |
64201201 | 1182 | { |
3cbee15b JM |
1183 | nvram_write(nvram, addr, value >> 24); |
1184 | nvram_write(nvram, addr + 1, (value >> 16) & 0xFF); | |
1185 | nvram_write(nvram, addr + 2, (value >> 8) & 0xFF); | |
1186 | nvram_write(nvram, addr + 3, value & 0xFF); | |
64201201 FB |
1187 | } |
1188 | ||
c227f099 | 1189 | uint32_t NVRAM_get_lword (nvram_t *nvram, uint32_t addr) |
64201201 FB |
1190 | { |
1191 | uint32_t tmp; | |
1192 | ||
3cbee15b JM |
1193 | tmp = nvram_read(nvram, addr) << 24; |
1194 | tmp |= nvram_read(nvram, addr + 1) << 16; | |
1195 | tmp |= nvram_read(nvram, addr + 2) << 8; | |
1196 | tmp |= nvram_read(nvram, addr + 3); | |
76a66253 | 1197 | |
64201201 FB |
1198 | return tmp; |
1199 | } | |
1200 | ||
c227f099 | 1201 | void NVRAM_set_string (nvram_t *nvram, uint32_t addr, |
b55266b5 | 1202 | const char *str, uint32_t max) |
64201201 FB |
1203 | { |
1204 | int i; | |
1205 | ||
1206 | for (i = 0; i < max && str[i] != '\0'; i++) { | |
3cbee15b | 1207 | nvram_write(nvram, addr + i, str[i]); |
64201201 | 1208 | } |
3cbee15b JM |
1209 | nvram_write(nvram, addr + i, str[i]); |
1210 | nvram_write(nvram, addr + max - 1, '\0'); | |
64201201 FB |
1211 | } |
1212 | ||
c227f099 | 1213 | int NVRAM_get_string (nvram_t *nvram, uint8_t *dst, uint16_t addr, int max) |
64201201 FB |
1214 | { |
1215 | int i; | |
1216 | ||
1217 | memset(dst, 0, max); | |
1218 | for (i = 0; i < max; i++) { | |
1219 | dst[i] = NVRAM_get_byte(nvram, addr + i); | |
1220 | if (dst[i] == '\0') | |
1221 | break; | |
1222 | } | |
1223 | ||
1224 | return i; | |
1225 | } | |
1226 | ||
1227 | static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value) | |
1228 | { | |
1229 | uint16_t tmp; | |
1230 | uint16_t pd, pd1, pd2; | |
1231 | ||
1232 | tmp = prev >> 8; | |
1233 | pd = prev ^ value; | |
1234 | pd1 = pd & 0x000F; | |
1235 | pd2 = ((pd >> 4) & 0x000F) ^ pd1; | |
1236 | tmp ^= (pd1 << 3) | (pd1 << 8); | |
1237 | tmp ^= pd2 | (pd2 << 7) | (pd2 << 12); | |
1238 | ||
1239 | return tmp; | |
1240 | } | |
1241 | ||
c227f099 | 1242 | static uint16_t NVRAM_compute_crc (nvram_t *nvram, uint32_t start, uint32_t count) |
64201201 FB |
1243 | { |
1244 | uint32_t i; | |
1245 | uint16_t crc = 0xFFFF; | |
1246 | int odd; | |
1247 | ||
1248 | odd = count & 1; | |
1249 | count &= ~1; | |
1250 | for (i = 0; i != count; i++) { | |
76a66253 | 1251 | crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i)); |
64201201 FB |
1252 | } |
1253 | if (odd) { | |
76a66253 | 1254 | crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8); |
64201201 FB |
1255 | } |
1256 | ||
1257 | return crc; | |
1258 | } | |
1259 | ||
fd0bbb12 FB |
1260 | #define CMDLINE_ADDR 0x017ff000 |
1261 | ||
c227f099 | 1262 | int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, |
b55266b5 | 1263 | const char *arch, |
64201201 FB |
1264 | uint32_t RAM_size, int boot_device, |
1265 | uint32_t kernel_image, uint32_t kernel_size, | |
fd0bbb12 | 1266 | const char *cmdline, |
64201201 | 1267 | uint32_t initrd_image, uint32_t initrd_size, |
fd0bbb12 FB |
1268 | uint32_t NVRAM_image, |
1269 | int width, int height, int depth) | |
64201201 FB |
1270 | { |
1271 | uint16_t crc; | |
1272 | ||
1273 | /* Set parameters for Open Hack'Ware BIOS */ | |
1274 | NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16); | |
1275 | NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */ | |
1276 | NVRAM_set_word(nvram, 0x14, NVRAM_size); | |
1277 | NVRAM_set_string(nvram, 0x20, arch, 16); | |
1278 | NVRAM_set_lword(nvram, 0x30, RAM_size); | |
1279 | NVRAM_set_byte(nvram, 0x34, boot_device); | |
1280 | NVRAM_set_lword(nvram, 0x38, kernel_image); | |
1281 | NVRAM_set_lword(nvram, 0x3C, kernel_size); | |
fd0bbb12 FB |
1282 | if (cmdline) { |
1283 | /* XXX: put the cmdline in NVRAM too ? */ | |
3c178e72 | 1284 | pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR, cmdline); |
fd0bbb12 FB |
1285 | NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR); |
1286 | NVRAM_set_lword(nvram, 0x44, strlen(cmdline)); | |
1287 | } else { | |
1288 | NVRAM_set_lword(nvram, 0x40, 0); | |
1289 | NVRAM_set_lword(nvram, 0x44, 0); | |
1290 | } | |
64201201 FB |
1291 | NVRAM_set_lword(nvram, 0x48, initrd_image); |
1292 | NVRAM_set_lword(nvram, 0x4C, initrd_size); | |
1293 | NVRAM_set_lword(nvram, 0x50, NVRAM_image); | |
fd0bbb12 FB |
1294 | |
1295 | NVRAM_set_word(nvram, 0x54, width); | |
1296 | NVRAM_set_word(nvram, 0x56, height); | |
1297 | NVRAM_set_word(nvram, 0x58, depth); | |
1298 | crc = NVRAM_compute_crc(nvram, 0x00, 0xF8); | |
3cbee15b | 1299 | NVRAM_set_word(nvram, 0xFC, crc); |
64201201 FB |
1300 | |
1301 | return 0; | |
a541f297 | 1302 | } |