]> Git Repo - qemu.git/blame - hw/ppc/ppc.c
hw: Remove unused 'hw/devices.h' include
[qemu.git] / hw / ppc / ppc.c
CommitLineData
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 */
0d75590d 24#include "qemu/osdep.h"
4771d756
PB
25#include "qemu-common.h"
26#include "cpu.h"
83c9f4ca 27#include "hw/hw.h"
0d09e41a 28#include "hw/ppc/ppc.h"
2b927571 29#include "hw/ppc/ppc_e500.h"
1de7afc9 30#include "qemu/timer.h"
9c17d615 31#include "sysemu/sysemu.h"
0ce470cd 32#include "sysemu/cpus.h"
1de7afc9 33#include "qemu/log.h"
98a8b524 34#include "qemu/error-report.h"
9c17d615 35#include "sysemu/kvm.h"
fc87e185 36#include "kvm_ppc.h"
98a8b524 37#include "trace.h"
a541f297 38
e9df014c 39//#define PPC_DEBUG_IRQ
4b6d0a4c 40//#define PPC_DEBUG_TB
e9df014c 41
d12d51d5 42#ifdef PPC_DEBUG_IRQ
93fcfe39 43# define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__)
d12d51d5
AL
44#else
45# define LOG_IRQ(...) do { } while (0)
46#endif
47
48
49#ifdef PPC_DEBUG_TB
93fcfe39 50# define LOG_TB(...) qemu_log(__VA_ARGS__)
d12d51d5
AL
51#else
52# define LOG_TB(...) do { } while (0)
53#endif
54
e2684c0b
AF
55static void cpu_ppc_tb_stop (CPUPPCState *env);
56static void cpu_ppc_tb_start (CPUPPCState *env);
dbdd2506 57
7058581a 58void ppc_set_irq(PowerPCCPU *cpu, int n_IRQ, int level)
47103572 59{
d8ed887b 60 CPUState *cs = CPU(cpu);
7058581a 61 CPUPPCState *env = &cpu->env;
8d04fb55
JK
62 unsigned int old_pending;
63 bool locked = false;
64
65 /* We may already have the BQL if coming from the reset path */
66 if (!qemu_mutex_iothread_locked()) {
67 locked = true;
68 qemu_mutex_lock_iothread();
69 }
70
71 old_pending = env->pending_interrupts;
fc87e185 72
47103572
JM
73 if (level) {
74 env->pending_interrupts |= 1 << n_IRQ;
c3affe56 75 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
47103572
JM
76 } else {
77 env->pending_interrupts &= ~(1 << n_IRQ);
d8ed887b
AF
78 if (env->pending_interrupts == 0) {
79 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
80 }
47103572 81 }
fc87e185
AG
82
83 if (old_pending != env->pending_interrupts) {
84#ifdef CONFIG_KVM
7058581a 85 kvmppc_set_interrupt(cpu, n_IRQ, level);
fc87e185
AG
86#endif
87 }
88
8d04fb55 89
d12d51d5 90 LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32
aae9366a 91 "req %08x\n", __func__, env, n_IRQ, level,
259186a7 92 env->pending_interrupts, CPU(cpu)->interrupt_request);
8d04fb55
JK
93
94 if (locked) {
95 qemu_mutex_unlock_iothread();
96 }
47103572
JM
97}
98
e9df014c 99/* PowerPC 6xx / 7xx internal IRQ controller */
a0961245 100static void ppc6xx_set_irq(void *opaque, int pin, int level)
d537cf6c 101{
a0961245
AF
102 PowerPCCPU *cpu = opaque;
103 CPUPPCState *env = &cpu->env;
e9df014c 104 int cur_level;
d537cf6c 105
d12d51d5 106 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
a496775f 107 env, pin, level);
e9df014c
JM
108 cur_level = (env->irq_input_state >> pin) & 1;
109 /* Don't generate spurious events */
24be5ae3 110 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
259186a7
AF
111 CPUState *cs = CPU(cpu);
112
e9df014c 113 switch (pin) {
dbdd2506
JM
114 case PPC6xx_INPUT_TBEN:
115 /* Level sensitive - active high */
d12d51d5 116 LOG_IRQ("%s: %s the time base\n",
dbdd2506 117 __func__, level ? "start" : "stop");
dbdd2506
JM
118 if (level) {
119 cpu_ppc_tb_start(env);
120 } else {
121 cpu_ppc_tb_stop(env);
122 }
24be5ae3
JM
123 case PPC6xx_INPUT_INT:
124 /* Level sensitive - active high */
d12d51d5 125 LOG_IRQ("%s: set the external IRQ state to %d\n",
a496775f 126 __func__, level);
7058581a 127 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
e9df014c 128 break;
24be5ae3 129 case PPC6xx_INPUT_SMI:
e9df014c 130 /* Level sensitive - active high */
d12d51d5 131 LOG_IRQ("%s: set the SMI IRQ state to %d\n",
a496775f 132 __func__, level);
7058581a 133 ppc_set_irq(cpu, PPC_INTERRUPT_SMI, level);
e9df014c 134 break;
24be5ae3 135 case PPC6xx_INPUT_MCP:
e9df014c
JM
136 /* Negative edge sensitive */
137 /* XXX: TODO: actual reaction may depends on HID0 status
138 * 603/604/740/750: check HID0[EMCP]
139 */
140 if (cur_level == 1 && level == 0) {
d12d51d5 141 LOG_IRQ("%s: raise machine check state\n",
a496775f 142 __func__);
7058581a 143 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
e9df014c
JM
144 }
145 break;
24be5ae3 146 case PPC6xx_INPUT_CKSTP_IN:
e9df014c
JM
147 /* Level sensitive - active low */
148 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
e63ecc6f 149 /* XXX: Note that the only way to restart the CPU is to reset it */
e9df014c 150 if (level) {
d12d51d5 151 LOG_IRQ("%s: stop the CPU\n", __func__);
259186a7 152 cs->halted = 1;
e9df014c
JM
153 }
154 break;
24be5ae3 155 case PPC6xx_INPUT_HRESET:
e9df014c
JM
156 /* Level sensitive - active low */
157 if (level) {
d12d51d5 158 LOG_IRQ("%s: reset the CPU\n", __func__);
c3affe56 159 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
e9df014c
JM
160 }
161 break;
24be5ae3 162 case PPC6xx_INPUT_SRESET:
d12d51d5 163 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
a496775f 164 __func__, level);
7058581a 165 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
e9df014c
JM
166 break;
167 default:
168 /* Unknown pin - do nothing */
d12d51d5 169 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
e9df014c
JM
170 return;
171 }
172 if (level)
173 env->irq_input_state |= 1 << pin;
174 else
175 env->irq_input_state &= ~(1 << pin);
d537cf6c
PB
176 }
177}
178
aa5a9e24 179void ppc6xx_irq_init(PowerPCCPU *cpu)
47103572 180{
aa5a9e24 181 CPUPPCState *env = &cpu->env;
a0961245
AF
182
183 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, cpu,
7b62a955 184 PPC6xx_INPUT_NB);
47103572
JM
185}
186
00af685f 187#if defined(TARGET_PPC64)
d0dfae6e 188/* PowerPC 970 internal IRQ controller */
a0961245 189static void ppc970_set_irq(void *opaque, int pin, int level)
d0dfae6e 190{
a0961245
AF
191 PowerPCCPU *cpu = opaque;
192 CPUPPCState *env = &cpu->env;
d0dfae6e
JM
193 int cur_level;
194
d12d51d5 195 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
d0dfae6e 196 env, pin, level);
d0dfae6e
JM
197 cur_level = (env->irq_input_state >> pin) & 1;
198 /* Don't generate spurious events */
199 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
259186a7
AF
200 CPUState *cs = CPU(cpu);
201
d0dfae6e
JM
202 switch (pin) {
203 case PPC970_INPUT_INT:
204 /* Level sensitive - active high */
d12d51d5 205 LOG_IRQ("%s: set the external IRQ state to %d\n",
d0dfae6e 206 __func__, level);
7058581a 207 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
d0dfae6e
JM
208 break;
209 case PPC970_INPUT_THINT:
210 /* Level sensitive - active high */
d12d51d5 211 LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__,
d0dfae6e 212 level);
7058581a 213 ppc_set_irq(cpu, PPC_INTERRUPT_THERM, level);
d0dfae6e
JM
214 break;
215 case PPC970_INPUT_MCP:
216 /* Negative edge sensitive */
217 /* XXX: TODO: actual reaction may depends on HID0 status
218 * 603/604/740/750: check HID0[EMCP]
219 */
220 if (cur_level == 1 && level == 0) {
d12d51d5 221 LOG_IRQ("%s: raise machine check state\n",
d0dfae6e 222 __func__);
7058581a 223 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, 1);
d0dfae6e
JM
224 }
225 break;
226 case PPC970_INPUT_CKSTP:
227 /* Level sensitive - active low */
228 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
229 if (level) {
d12d51d5 230 LOG_IRQ("%s: stop the CPU\n", __func__);
259186a7 231 cs->halted = 1;
d0dfae6e 232 } else {
d12d51d5 233 LOG_IRQ("%s: restart the CPU\n", __func__);
259186a7
AF
234 cs->halted = 0;
235 qemu_cpu_kick(cs);
d0dfae6e
JM
236 }
237 break;
238 case PPC970_INPUT_HRESET:
239 /* Level sensitive - active low */
240 if (level) {
c3affe56 241 cpu_interrupt(cs, CPU_INTERRUPT_RESET);
d0dfae6e
JM
242 }
243 break;
244 case PPC970_INPUT_SRESET:
d12d51d5 245 LOG_IRQ("%s: set the RESET IRQ state to %d\n",
d0dfae6e 246 __func__, level);
7058581a 247 ppc_set_irq(cpu, PPC_INTERRUPT_RESET, level);
d0dfae6e
JM
248 break;
249 case PPC970_INPUT_TBEN:
d12d51d5 250 LOG_IRQ("%s: set the TBEN state to %d\n", __func__,
d0dfae6e 251 level);
d0dfae6e
JM
252 /* XXX: TODO */
253 break;
254 default:
255 /* Unknown pin - do nothing */
d12d51d5 256 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
d0dfae6e
JM
257 return;
258 }
259 if (level)
260 env->irq_input_state |= 1 << pin;
261 else
262 env->irq_input_state &= ~(1 << pin);
263 }
264}
265
aa5a9e24 266void ppc970_irq_init(PowerPCCPU *cpu)
d0dfae6e 267{
aa5a9e24 268 CPUPPCState *env = &cpu->env;
a0961245
AF
269
270 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, cpu,
7b62a955 271 PPC970_INPUT_NB);
d0dfae6e 272}
9d52e907
DG
273
274/* POWER7 internal IRQ controller */
a0961245 275static void power7_set_irq(void *opaque, int pin, int level)
9d52e907 276{
a0961245
AF
277 PowerPCCPU *cpu = opaque;
278 CPUPPCState *env = &cpu->env;
9d52e907
DG
279
280 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
281 env, pin, level);
9d52e907
DG
282
283 switch (pin) {
284 case POWER7_INPUT_INT:
285 /* Level sensitive - active high */
286 LOG_IRQ("%s: set the external IRQ state to %d\n",
287 __func__, level);
7058581a 288 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
9d52e907
DG
289 break;
290 default:
291 /* Unknown pin - do nothing */
292 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
293 return;
294 }
295 if (level) {
296 env->irq_input_state |= 1 << pin;
297 } else {
298 env->irq_input_state &= ~(1 << pin);
299 }
300}
301
aa5a9e24 302void ppcPOWER7_irq_init(PowerPCCPU *cpu)
9d52e907 303{
aa5a9e24 304 CPUPPCState *env = &cpu->env;
a0961245
AF
305
306 env->irq_inputs = (void **)qemu_allocate_irqs(&power7_set_irq, cpu,
9d52e907
DG
307 POWER7_INPUT_NB);
308}
67afe775
BH
309
310/* POWER9 internal IRQ controller */
311static void power9_set_irq(void *opaque, int pin, int level)
312{
313 PowerPCCPU *cpu = opaque;
314 CPUPPCState *env = &cpu->env;
315
316 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
317 env, pin, level);
318
319 switch (pin) {
320 case POWER9_INPUT_INT:
321 /* Level sensitive - active high */
322 LOG_IRQ("%s: set the external IRQ state to %d\n",
323 __func__, level);
324 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
325 break;
326 case POWER9_INPUT_HINT:
327 /* Level sensitive - active high */
328 LOG_IRQ("%s: set the external IRQ state to %d\n",
329 __func__, level);
330 ppc_set_irq(cpu, PPC_INTERRUPT_HVIRT, level);
331 break;
332 default:
333 /* Unknown pin - do nothing */
334 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
335 return;
336 }
337 if (level) {
338 env->irq_input_state |= 1 << pin;
339 } else {
340 env->irq_input_state &= ~(1 << pin);
341 }
342}
343
344void ppcPOWER9_irq_init(PowerPCCPU *cpu)
345{
346 CPUPPCState *env = &cpu->env;
347
348 env->irq_inputs = (void **)qemu_allocate_irqs(&power9_set_irq, cpu,
349 POWER9_INPUT_NB);
350}
00af685f 351#endif /* defined(TARGET_PPC64) */
d0dfae6e 352
52144b69
TH
353void ppc40x_core_reset(PowerPCCPU *cpu)
354{
355 CPUPPCState *env = &cpu->env;
356 target_ulong dbsr;
357
358 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC core\n");
359 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_RESET);
360 dbsr = env->spr[SPR_40x_DBSR];
361 dbsr &= ~0x00000300;
362 dbsr |= 0x00000100;
363 env->spr[SPR_40x_DBSR] = dbsr;
364}
365
366void ppc40x_chip_reset(PowerPCCPU *cpu)
367{
368 CPUPPCState *env = &cpu->env;
369 target_ulong dbsr;
370
371 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC chip\n");
372 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_RESET);
373 /* XXX: TODO reset all internal peripherals */
374 dbsr = env->spr[SPR_40x_DBSR];
375 dbsr &= ~0x00000300;
376 dbsr |= 0x00000200;
377 env->spr[SPR_40x_DBSR] = dbsr;
378}
379
380void ppc40x_system_reset(PowerPCCPU *cpu)
381{
382 qemu_log_mask(CPU_LOG_RESET, "Reset PowerPC system\n");
383 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
384}
385
386void store_40x_dbcr0(CPUPPCState *env, uint32_t val)
387{
388 PowerPCCPU *cpu = ppc_env_get_cpu(env);
389
390 switch ((val >> 28) & 0x3) {
391 case 0x0:
392 /* No action */
393 break;
394 case 0x1:
395 /* Core reset */
396 ppc40x_core_reset(cpu);
397 break;
398 case 0x2:
399 /* Chip reset */
400 ppc40x_chip_reset(cpu);
401 break;
402 case 0x3:
403 /* System reset */
404 ppc40x_system_reset(cpu);
405 break;
406 }
407}
408
4e290a0b 409/* PowerPC 40x internal IRQ controller */
a0961245 410static void ppc40x_set_irq(void *opaque, int pin, int level)
24be5ae3 411{
a0961245
AF
412 PowerPCCPU *cpu = opaque;
413 CPUPPCState *env = &cpu->env;
24be5ae3
JM
414 int cur_level;
415
d12d51d5 416 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
8ecc7913 417 env, pin, level);
24be5ae3
JM
418 cur_level = (env->irq_input_state >> pin) & 1;
419 /* Don't generate spurious events */
420 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
259186a7
AF
421 CPUState *cs = CPU(cpu);
422
24be5ae3 423 switch (pin) {
4e290a0b 424 case PPC40x_INPUT_RESET_SYS:
8ecc7913 425 if (level) {
d12d51d5 426 LOG_IRQ("%s: reset the PowerPC system\n",
8ecc7913 427 __func__);
f3273ba6 428 ppc40x_system_reset(cpu);
8ecc7913
JM
429 }
430 break;
4e290a0b 431 case PPC40x_INPUT_RESET_CHIP:
8ecc7913 432 if (level) {
d12d51d5 433 LOG_IRQ("%s: reset the PowerPC chip\n", __func__);
f3273ba6 434 ppc40x_chip_reset(cpu);
8ecc7913
JM
435 }
436 break;
4e290a0b 437 case PPC40x_INPUT_RESET_CORE:
24be5ae3
JM
438 /* XXX: TODO: update DBSR[MRR] */
439 if (level) {
d12d51d5 440 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
f3273ba6 441 ppc40x_core_reset(cpu);
24be5ae3
JM
442 }
443 break;
4e290a0b 444 case PPC40x_INPUT_CINT:
24be5ae3 445 /* Level sensitive - active high */
d12d51d5 446 LOG_IRQ("%s: set the critical IRQ state to %d\n",
8ecc7913 447 __func__, level);
7058581a 448 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
24be5ae3 449 break;
4e290a0b 450 case PPC40x_INPUT_INT:
24be5ae3 451 /* Level sensitive - active high */
d12d51d5 452 LOG_IRQ("%s: set the external IRQ state to %d\n",
a496775f 453 __func__, level);
7058581a 454 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
24be5ae3 455 break;
4e290a0b 456 case PPC40x_INPUT_HALT:
24be5ae3
JM
457 /* Level sensitive - active low */
458 if (level) {
d12d51d5 459 LOG_IRQ("%s: stop the CPU\n", __func__);
259186a7 460 cs->halted = 1;
24be5ae3 461 } else {
d12d51d5 462 LOG_IRQ("%s: restart the CPU\n", __func__);
259186a7
AF
463 cs->halted = 0;
464 qemu_cpu_kick(cs);
24be5ae3
JM
465 }
466 break;
4e290a0b 467 case PPC40x_INPUT_DEBUG:
24be5ae3 468 /* Level sensitive - active high */
d12d51d5 469 LOG_IRQ("%s: set the debug pin state to %d\n",
a496775f 470 __func__, level);
7058581a 471 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
24be5ae3
JM
472 break;
473 default:
474 /* Unknown pin - do nothing */
d12d51d5 475 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
24be5ae3
JM
476 return;
477 }
478 if (level)
479 env->irq_input_state |= 1 << pin;
480 else
481 env->irq_input_state &= ~(1 << pin);
482 }
483}
484
aa5a9e24 485void ppc40x_irq_init(PowerPCCPU *cpu)
24be5ae3 486{
aa5a9e24 487 CPUPPCState *env = &cpu->env;
a0961245 488
4e290a0b 489 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
a0961245 490 cpu, PPC40x_INPUT_NB);
24be5ae3
JM
491}
492
9fdc60bf 493/* PowerPC E500 internal IRQ controller */
a0961245 494static void ppce500_set_irq(void *opaque, int pin, int level)
9fdc60bf 495{
a0961245
AF
496 PowerPCCPU *cpu = opaque;
497 CPUPPCState *env = &cpu->env;
9fdc60bf
AJ
498 int cur_level;
499
500 LOG_IRQ("%s: env %p pin %d level %d\n", __func__,
501 env, pin, level);
502 cur_level = (env->irq_input_state >> pin) & 1;
503 /* Don't generate spurious events */
504 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
505 switch (pin) {
506 case PPCE500_INPUT_MCK:
507 if (level) {
508 LOG_IRQ("%s: reset the PowerPC system\n",
509 __func__);
cf83f140 510 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
9fdc60bf
AJ
511 }
512 break;
513 case PPCE500_INPUT_RESET_CORE:
514 if (level) {
515 LOG_IRQ("%s: reset the PowerPC core\n", __func__);
7058581a 516 ppc_set_irq(cpu, PPC_INTERRUPT_MCK, level);
9fdc60bf
AJ
517 }
518 break;
519 case PPCE500_INPUT_CINT:
520 /* Level sensitive - active high */
521 LOG_IRQ("%s: set the critical IRQ state to %d\n",
522 __func__, level);
7058581a 523 ppc_set_irq(cpu, PPC_INTERRUPT_CEXT, level);
9fdc60bf
AJ
524 break;
525 case PPCE500_INPUT_INT:
526 /* Level sensitive - active high */
527 LOG_IRQ("%s: set the core IRQ state to %d\n",
528 __func__, level);
7058581a 529 ppc_set_irq(cpu, PPC_INTERRUPT_EXT, level);
9fdc60bf
AJ
530 break;
531 case PPCE500_INPUT_DEBUG:
532 /* Level sensitive - active high */
533 LOG_IRQ("%s: set the debug pin state to %d\n",
534 __func__, level);
7058581a 535 ppc_set_irq(cpu, PPC_INTERRUPT_DEBUG, level);
9fdc60bf
AJ
536 break;
537 default:
538 /* Unknown pin - do nothing */
539 LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin);
540 return;
541 }
542 if (level)
543 env->irq_input_state |= 1 << pin;
544 else
545 env->irq_input_state &= ~(1 << pin);
546 }
547}
548
aa5a9e24 549void ppce500_irq_init(PowerPCCPU *cpu)
9fdc60bf 550{
aa5a9e24 551 CPUPPCState *env = &cpu->env;
a0961245 552
9fdc60bf 553 env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq,
a0961245 554 cpu, PPCE500_INPUT_NB);
9fdc60bf 555}
e49798b1
AG
556
557/* Enable or Disable the E500 EPR capability */
558void ppce500_set_mpic_proxy(bool enabled)
559{
182735ef 560 CPUState *cs;
e49798b1 561
bdc44640 562 CPU_FOREACH(cs) {
182735ef 563 PowerPCCPU *cpu = POWERPC_CPU(cs);
5b95b8b9 564
182735ef 565 cpu->env.mpic_proxy = enabled;
5b95b8b9 566 if (kvm_enabled()) {
182735ef 567 kvmppc_set_mpic_proxy(cpu, enabled);
5b95b8b9 568 }
e49798b1
AG
569 }
570}
571
9fddaa0c 572/*****************************************************************************/
e9df014c 573/* PowerPC time base and decrementer emulation */
9fddaa0c 574
ddd1055b 575uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset)
9fddaa0c
FB
576{
577 /* TB time in tb periods */
73bcb24d 578 return muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND) + tb_offset;
9fddaa0c
FB
579}
580
e2684c0b 581uint64_t cpu_ppc_load_tbl (CPUPPCState *env)
9fddaa0c 582{
c227f099 583 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c
FB
584 uint64_t tb;
585
90dc8812
SW
586 if (kvm_enabled()) {
587 return env->spr[SPR_TBL];
588 }
589
bc72ad67 590 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
d12d51d5 591 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
9fddaa0c 592
e3ea6529 593 return tb;
9fddaa0c
FB
594}
595
e2684c0b 596static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState *env)
9fddaa0c 597{
c227f099 598 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c
FB
599 uint64_t tb;
600
bc72ad67 601 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
d12d51d5 602 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
76a66253 603
9fddaa0c
FB
604 return tb >> 32;
605}
606
e2684c0b 607uint32_t cpu_ppc_load_tbu (CPUPPCState *env)
8a84de23 608{
90dc8812
SW
609 if (kvm_enabled()) {
610 return env->spr[SPR_TBU];
611 }
612
8a84de23
JM
613 return _cpu_ppc_load_tbu(env);
614}
615
c227f099 616static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk,
636aa200 617 int64_t *tb_offsetp, uint64_t value)
9fddaa0c 618{
73bcb24d
RS
619 *tb_offsetp = value -
620 muldiv64(vmclk, tb_env->tb_freq, NANOSECONDS_PER_SECOND);
621
d12d51d5 622 LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n",
aae9366a 623 __func__, value, *tb_offsetp);
9fddaa0c
FB
624}
625
e2684c0b 626void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value)
a062e36c 627{
c227f099 628 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
629 uint64_t tb;
630
bc72ad67 631 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
a062e36c 632 tb &= 0xFFFFFFFF00000000ULL;
bc72ad67 633 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
dbdd2506 634 &tb_env->tb_offset, tb | (uint64_t)value);
a062e36c
JM
635}
636
e2684c0b 637static inline void _cpu_ppc_store_tbu(CPUPPCState *env, uint32_t value)
9fddaa0c 638{
c227f099 639 ppc_tb_t *tb_env = env->tb_env;
a062e36c 640 uint64_t tb;
9fddaa0c 641
bc72ad67 642 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->tb_offset);
a062e36c 643 tb &= 0x00000000FFFFFFFFULL;
bc72ad67 644 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
dbdd2506 645 &tb_env->tb_offset, ((uint64_t)value << 32) | tb);
9fddaa0c
FB
646}
647
e2684c0b 648void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value)
8a84de23
JM
649{
650 _cpu_ppc_store_tbu(env, value);
651}
652
e2684c0b 653uint64_t cpu_ppc_load_atbl (CPUPPCState *env)
a062e36c 654{
c227f099 655 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
656 uint64_t tb;
657
bc72ad67 658 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
d12d51d5 659 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
a062e36c 660
b711de95 661 return tb;
a062e36c
JM
662}
663
e2684c0b 664uint32_t cpu_ppc_load_atbu (CPUPPCState *env)
a062e36c 665{
c227f099 666 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
667 uint64_t tb;
668
bc72ad67 669 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
d12d51d5 670 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
a062e36c
JM
671
672 return tb >> 32;
673}
674
e2684c0b 675void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value)
a062e36c 676{
c227f099 677 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
678 uint64_t tb;
679
bc72ad67 680 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
a062e36c 681 tb &= 0xFFFFFFFF00000000ULL;
bc72ad67 682 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
dbdd2506 683 &tb_env->atb_offset, tb | (uint64_t)value);
a062e36c
JM
684}
685
e2684c0b 686void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value)
9fddaa0c 687{
c227f099 688 ppc_tb_t *tb_env = env->tb_env;
a062e36c 689 uint64_t tb;
9fddaa0c 690
bc72ad67 691 tb = cpu_ppc_get_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tb_env->atb_offset);
a062e36c 692 tb &= 0x00000000FFFFFFFFULL;
bc72ad67 693 cpu_ppc_store_tb(tb_env, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
dbdd2506
JM
694 &tb_env->atb_offset, ((uint64_t)value << 32) | tb);
695}
696
e2684c0b 697static void cpu_ppc_tb_stop (CPUPPCState *env)
dbdd2506 698{
c227f099 699 ppc_tb_t *tb_env = env->tb_env;
dbdd2506
JM
700 uint64_t tb, atb, vmclk;
701
702 /* If the time base is already frozen, do nothing */
703 if (tb_env->tb_freq != 0) {
bc72ad67 704 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
dbdd2506
JM
705 /* Get the time base */
706 tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset);
707 /* Get the alternate time base */
708 atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset);
709 /* Store the time base value (ie compute the current offset) */
710 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
711 /* Store the alternate time base value (compute the current offset) */
712 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
713 /* Set the time base frequency to zero */
714 tb_env->tb_freq = 0;
715 /* Now, the time bases are frozen to tb_offset / atb_offset value */
716 }
717}
718
e2684c0b 719static void cpu_ppc_tb_start (CPUPPCState *env)
dbdd2506 720{
c227f099 721 ppc_tb_t *tb_env = env->tb_env;
dbdd2506 722 uint64_t tb, atb, vmclk;
aae9366a 723
dbdd2506
JM
724 /* If the time base is not frozen, do nothing */
725 if (tb_env->tb_freq == 0) {
bc72ad67 726 vmclk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
dbdd2506
JM
727 /* Get the time base from tb_offset */
728 tb = tb_env->tb_offset;
729 /* Get the alternate time base from atb_offset */
730 atb = tb_env->atb_offset;
731 /* Restore the tb frequency from the decrementer frequency */
732 tb_env->tb_freq = tb_env->decr_freq;
733 /* Store the time base value */
734 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
735 /* Store the alternate time base value */
736 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
737 }
9fddaa0c
FB
738}
739
e81a982a
AG
740bool ppc_decr_clear_on_delivery(CPUPPCState *env)
741{
742 ppc_tb_t *tb_env = env->tb_env;
743 int flags = PPC_DECR_UNDERFLOW_TRIGGERED | PPC_DECR_UNDERFLOW_LEVEL;
744 return ((tb_env->flags & flags) == PPC_DECR_UNDERFLOW_TRIGGERED);
745}
746
e2684c0b 747static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
9fddaa0c 748{
c227f099 749 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c 750 uint32_t decr;
4e588a4d 751 int64_t diff;
9fddaa0c 752
bc72ad67 753 diff = next - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
ddd1055b 754 if (diff >= 0) {
73bcb24d 755 decr = muldiv64(diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND);
ddd1055b
FC
756 } else if (tb_env->flags & PPC_TIMER_BOOKE) {
757 decr = 0;
758 } else {
73bcb24d 759 decr = -muldiv64(-diff, tb_env->decr_freq, NANOSECONDS_PER_SECOND);
ddd1055b 760 }
d12d51d5 761 LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
76a66253 762
9fddaa0c
FB
763 return decr;
764}
765
e2684c0b 766uint32_t cpu_ppc_load_decr (CPUPPCState *env)
58a7d328 767{
c227f099 768 ppc_tb_t *tb_env = env->tb_env;
58a7d328 769
90dc8812
SW
770 if (kvm_enabled()) {
771 return env->spr[SPR_DECR];
772 }
773
f55e9d9a 774 return _cpu_ppc_load_decr(env, tb_env->decr_next);
58a7d328
JM
775}
776
e2684c0b 777uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
58a7d328 778{
c227f099 779 ppc_tb_t *tb_env = env->tb_env;
58a7d328 780
f55e9d9a 781 return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
58a7d328
JM
782}
783
e2684c0b 784uint64_t cpu_ppc_load_purr (CPUPPCState *env)
58a7d328 785{
c227f099 786 ppc_tb_t *tb_env = env->tb_env;
58a7d328
JM
787 uint64_t diff;
788
bc72ad67 789 diff = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - tb_env->purr_start;
b33c17e1 790
73bcb24d
RS
791 return tb_env->purr_load +
792 muldiv64(diff, tb_env->tb_freq, NANOSECONDS_PER_SECOND);
58a7d328 793}
58a7d328 794
9fddaa0c
FB
795/* When decrementer expires,
796 * all we need to do is generate or queue a CPU exception
797 */
7e0a9247 798static inline void cpu_ppc_decr_excp(PowerPCCPU *cpu)
9fddaa0c
FB
799{
800 /* Raise it */
d12d51d5 801 LOG_TB("raise decrementer exception\n");
7058581a 802 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 1);
9fddaa0c
FB
803}
804
e81a982a
AG
805static inline void cpu_ppc_decr_lower(PowerPCCPU *cpu)
806{
807 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 0);
808}
809
7e0a9247 810static inline void cpu_ppc_hdecr_excp(PowerPCCPU *cpu)
58a7d328 811{
4b236b62
BH
812 CPUPPCState *env = &cpu->env;
813
58a7d328 814 /* Raise it */
4b236b62
BH
815 LOG_TB("raise hv decrementer exception\n");
816
817 /* The architecture specifies that we don't deliver HDEC
818 * interrupts in a PM state. Not only they don't cause a
819 * wakeup but they also get effectively discarded.
820 */
1e7fd61d 821 if (!env->resume_as_sreset) {
4b236b62
BH
822 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 1);
823 }
58a7d328
JM
824}
825
e81a982a
AG
826static inline void cpu_ppc_hdecr_lower(PowerPCCPU *cpu)
827{
828 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
829}
830
7e0a9247 831static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
1246b259 832 QEMUTimer *timer,
e81a982a
AG
833 void (*raise_excp)(void *),
834 void (*lower_excp)(PowerPCCPU *),
835 uint32_t decr, uint32_t value)
9fddaa0c 836{
7e0a9247 837 CPUPPCState *env = &cpu->env;
c227f099 838 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c
FB
839 uint64_t now, next;
840
d12d51d5 841 LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
aae9366a 842 decr, value);
55f7d4b0
DG
843
844 if (kvm_enabled()) {
845 /* KVM handles decrementer exceptions, we don't need our own timer */
846 return;
847 }
848
e81a982a
AG
849 /*
850 * Going from 2 -> 1, 1 -> 0 or 0 -> -1 is the event to generate a DEC
851 * interrupt.
852 *
853 * If we get a really small DEC value, we can assume that by the time we
854 * handled it we should inject an interrupt already.
855 *
856 * On MSB level based DEC implementations the MSB always means the interrupt
857 * is pending, so raise it on those.
858 *
859 * On MSB edge based DEC implementations the MSB going from 0 -> 1 triggers
860 * an edge interrupt, so raise it here too.
861 */
862 if ((value < 3) ||
863 ((tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL) && (value & 0x80000000)) ||
864 ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED) && (value & 0x80000000)
865 && !(decr & 0x80000000))) {
866 (*raise_excp)(cpu);
867 return;
ddd1055b 868 }
e81a982a
AG
869
870 /* On MSB level based systems a 0 for the MSB stops interrupt delivery */
871 if (!(value & 0x80000000) && (tb_env->flags & PPC_DECR_UNDERFLOW_LEVEL)) {
872 (*lower_excp)(cpu);
ddd1055b 873 }
e81a982a
AG
874
875 /* Calculate the next timer event */
876 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
73bcb24d 877 next = now + muldiv64(value, NANOSECONDS_PER_SECOND, tb_env->decr_freq);
58a7d328 878 *nextp = next;
e81a982a 879
9fddaa0c 880 /* Adjust timer */
bc72ad67 881 timer_mod(timer, next);
58a7d328
JM
882}
883
7e0a9247 884static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t decr,
e81a982a 885 uint32_t value)
58a7d328 886{
7e0a9247 887 ppc_tb_t *tb_env = cpu->env.tb_env;
58a7d328 888
7e0a9247 889 __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer,
e81a982a
AG
890 tb_env->decr_timer->cb, &cpu_ppc_decr_lower, decr,
891 value);
9fddaa0c
FB
892}
893
e2684c0b 894void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
9fddaa0c 895{
7e0a9247
AF
896 PowerPCCPU *cpu = ppc_env_get_cpu(env);
897
e81a982a 898 _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value);
9fddaa0c
FB
899}
900
50c680f0 901static void cpu_ppc_decr_cb(void *opaque)
9fddaa0c 902{
50c680f0 903 PowerPCCPU *cpu = opaque;
7e0a9247 904
e81a982a 905 cpu_ppc_decr_excp(cpu);
9fddaa0c
FB
906}
907
7e0a9247 908static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t hdecr,
e81a982a 909 uint32_t value)
58a7d328 910{
7e0a9247 911 ppc_tb_t *tb_env = cpu->env.tb_env;
58a7d328 912
b172c56a 913 if (tb_env->hdecr_timer != NULL) {
7e0a9247 914 __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer,
e81a982a
AG
915 tb_env->hdecr_timer->cb, &cpu_ppc_hdecr_lower,
916 hdecr, value);
b172c56a 917 }
58a7d328
JM
918}
919
e2684c0b 920void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
58a7d328 921{
7e0a9247
AF
922 PowerPCCPU *cpu = ppc_env_get_cpu(env);
923
e81a982a 924 _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value);
58a7d328
JM
925}
926
50c680f0 927static void cpu_ppc_hdecr_cb(void *opaque)
58a7d328 928{
50c680f0 929 PowerPCCPU *cpu = opaque;
7e0a9247 930
e81a982a 931 cpu_ppc_hdecr_excp(cpu);
58a7d328
JM
932}
933
7e0a9247 934static void cpu_ppc_store_purr(PowerPCCPU *cpu, uint64_t value)
58a7d328 935{
7e0a9247 936 ppc_tb_t *tb_env = cpu->env.tb_env;
58a7d328
JM
937
938 tb_env->purr_load = value;
bc72ad67 939 tb_env->purr_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
58a7d328 940}
58a7d328 941
8ecc7913
JM
942static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
943{
e2684c0b 944 CPUPPCState *env = opaque;
7e0a9247 945 PowerPCCPU *cpu = ppc_env_get_cpu(env);
c227f099 946 ppc_tb_t *tb_env = env->tb_env;
8ecc7913
JM
947
948 tb_env->tb_freq = freq;
dbdd2506 949 tb_env->decr_freq = freq;
8ecc7913
JM
950 /* There is a bug in Linux 2.4 kernels:
951 * if a decrementer exception is pending when it enables msr_ee at startup,
952 * it's not ready to handle it...
953 */
e81a982a
AG
954 _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
955 _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF);
7e0a9247 956 cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
8ecc7913
JM
957}
958
42043e4f 959static void timebase_save(PPCTimebase *tb)
98a8b524 960{
4a7428c5 961 uint64_t ticks = cpu_get_host_ticks();
98a8b524
AK
962 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
963
964 if (!first_ppc_cpu->env.tb_env) {
965 error_report("No timebase object");
966 return;
967 }
968
42043e4f 969 /* not used anymore, we keep it for compatibility */
77bad151 970 tb->time_of_the_day_ns = qemu_clock_get_ns(QEMU_CLOCK_HOST);
98a8b524 971 /*
42043e4f 972 * tb_offset is only expected to be changed by QEMU so
98a8b524
AK
973 * there is no need to update it from KVM here
974 */
975 tb->guest_timebase = ticks + first_ppc_cpu->env.tb_env->tb_offset;
976}
977
42043e4f 978static void timebase_load(PPCTimebase *tb)
98a8b524 979{
98a8b524
AK
980 CPUState *cpu;
981 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
42043e4f 982 int64_t tb_off_adj, tb_off;
98a8b524
AK
983 unsigned long freq;
984
985 if (!first_ppc_cpu->env.tb_env) {
986 error_report("No timebase object");
42043e4f 987 return;
98a8b524
AK
988 }
989
990 freq = first_ppc_cpu->env.tb_env->tb_freq;
98a8b524 991
42043e4f 992 tb_off_adj = tb->guest_timebase - cpu_get_host_ticks();
98a8b524
AK
993
994 tb_off = first_ppc_cpu->env.tb_env->tb_offset;
995 trace_ppc_tb_adjust(tb_off, tb_off_adj, tb_off_adj - tb_off,
996 (tb_off_adj - tb_off) / freq);
997
998 /* Set new offset to all CPUs */
999 CPU_FOREACH(cpu) {
1000 PowerPCCPU *pcpu = POWERPC_CPU(cpu);
1001 pcpu->env.tb_env->tb_offset = tb_off_adj;
42043e4f
LV
1002#if defined(CONFIG_KVM)
1003 kvm_set_one_reg(cpu, KVM_REG_PPC_TB_OFFSET,
1004 &pcpu->env.tb_env->tb_offset);
1005#endif
1006 }
1007}
1008
1009void cpu_ppc_clock_vm_state_change(void *opaque, int running,
1010 RunState state)
1011{
1012 PPCTimebase *tb = opaque;
1013
1014 if (running) {
1015 timebase_load(tb);
1016 } else {
1017 timebase_save(tb);
98a8b524 1018 }
42043e4f
LV
1019}
1020
1021/*
1022 * When migrating, read the clock just before migration,
1023 * so that the guest clock counts during the events
1024 * between:
1025 *
1026 * * vm_stop()
1027 * *
1028 * * pre_save()
1029 *
1030 * This reduces clock difference on migration from 5s
1031 * to 0.1s (when max_downtime == 5s), because sending the
1032 * final pages of memory (which happens between vm_stop()
1033 * and pre_save()) takes max_downtime.
1034 */
44b1ff31 1035static int timebase_pre_save(void *opaque)
42043e4f
LV
1036{
1037 PPCTimebase *tb = opaque;
98a8b524 1038
42043e4f 1039 timebase_save(tb);
44b1ff31
DDAG
1040
1041 return 0;
98a8b524
AK
1042}
1043
1044const VMStateDescription vmstate_ppc_timebase = {
1045 .name = "timebase",
1046 .version_id = 1,
1047 .minimum_version_id = 1,
1048 .minimum_version_id_old = 1,
1049 .pre_save = timebase_pre_save,
98a8b524
AK
1050 .fields = (VMStateField []) {
1051 VMSTATE_UINT64(guest_timebase, PPCTimebase),
1052 VMSTATE_INT64(time_of_the_day_ns, PPCTimebase),
1053 VMSTATE_END_OF_LIST()
1054 },
1055};
1056
9fddaa0c 1057/* Set up (once) timebase frequency (in Hz) */
e2684c0b 1058clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
9fddaa0c 1059{
50c680f0 1060 PowerPCCPU *cpu = ppc_env_get_cpu(env);
c227f099 1061 ppc_tb_t *tb_env;
9fddaa0c 1062
7267c094 1063 tb_env = g_malloc0(sizeof(ppc_tb_t));
9fddaa0c 1064 env->tb_env = tb_env;
ddd1055b 1065 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
e81a982a
AG
1066 if (env->insns_flags & PPC_SEGMENT_64B) {
1067 /* All Book3S 64bit CPUs implement level based DEC logic */
1068 tb_env->flags |= PPC_DECR_UNDERFLOW_LEVEL;
1069 }
8ecc7913 1070 /* Create new timer */
bc72ad67 1071 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_decr_cb, cpu);
4b236b62 1072 if (env->has_hv_mode) {
bc72ad67 1073 tb_env->hdecr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_ppc_hdecr_cb,
50c680f0 1074 cpu);
b172c56a
JM
1075 } else {
1076 tb_env->hdecr_timer = NULL;
1077 }
8ecc7913 1078 cpu_ppc_set_tb_clk(env, freq);
9fddaa0c 1079
8ecc7913 1080 return &cpu_ppc_set_tb_clk;
9fddaa0c
FB
1081}
1082
76a66253 1083/* Specific helpers for POWER & PowerPC 601 RTC */
e2684c0b 1084void cpu_ppc601_store_rtcu (CPUPPCState *env, uint32_t value)
8a84de23
JM
1085{
1086 _cpu_ppc_store_tbu(env, value);
1087}
76a66253 1088
e2684c0b 1089uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env)
8a84de23
JM
1090{
1091 return _cpu_ppc_load_tbu(env);
1092}
76a66253 1093
e2684c0b 1094void cpu_ppc601_store_rtcl (CPUPPCState *env, uint32_t value)
76a66253
JM
1095{
1096 cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
1097}
1098
e2684c0b 1099uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env)
76a66253
JM
1100{
1101 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
1102}
1103
636aaad7 1104/*****************************************************************************/
ddd1055b 1105/* PowerPC 40x timers */
636aaad7
JM
1106
1107/* PIT, FIT & WDT */
ddd1055b
FC
1108typedef struct ppc40x_timer_t ppc40x_timer_t;
1109struct ppc40x_timer_t {
636aaad7
JM
1110 uint64_t pit_reload; /* PIT auto-reload value */
1111 uint64_t fit_next; /* Tick for next FIT interrupt */
1246b259 1112 QEMUTimer *fit_timer;
636aaad7 1113 uint64_t wdt_next; /* Tick for next WDT interrupt */
1246b259 1114 QEMUTimer *wdt_timer;
d63cb48d
EI
1115
1116 /* 405 have the PIT, 440 have a DECR. */
1117 unsigned int decr_excp;
636aaad7 1118};
3b46e624 1119
636aaad7
JM
1120/* Fixed interval timer */
1121static void cpu_4xx_fit_cb (void *opaque)
1122{
7058581a 1123 PowerPCCPU *cpu;
e2684c0b 1124 CPUPPCState *env;
c227f099 1125 ppc_tb_t *tb_env;
ddd1055b 1126 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
1127 uint64_t now, next;
1128
1129 env = opaque;
7058581a 1130 cpu = ppc_env_get_cpu(env);
636aaad7 1131 tb_env = env->tb_env;
ddd1055b 1132 ppc40x_timer = tb_env->opaque;
bc72ad67 1133 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
636aaad7
JM
1134 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
1135 case 0:
1136 next = 1 << 9;
1137 break;
1138 case 1:
1139 next = 1 << 13;
1140 break;
1141 case 2:
1142 next = 1 << 17;
1143 break;
1144 case 3:
1145 next = 1 << 21;
1146 break;
1147 default:
1148 /* Cannot occur, but makes gcc happy */
1149 return;
1150 }
73bcb24d 1151 next = now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->tb_freq);
636aaad7
JM
1152 if (next == now)
1153 next++;
bc72ad67 1154 timer_mod(ppc40x_timer->fit_timer, next);
636aaad7 1155 env->spr[SPR_40x_TSR] |= 1 << 26;
7058581a
AF
1156 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) {
1157 ppc_set_irq(cpu, PPC_INTERRUPT_FIT, 1);
1158 }
90e189ec
BS
1159 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
1160 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
1161 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
636aaad7
JM
1162}
1163
1164/* Programmable interval timer */
e2684c0b 1165static void start_stop_pit (CPUPPCState *env, ppc_tb_t *tb_env, int is_excp)
76a66253 1166{
ddd1055b 1167 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
1168 uint64_t now, next;
1169
ddd1055b
FC
1170 ppc40x_timer = tb_env->opaque;
1171 if (ppc40x_timer->pit_reload <= 1 ||
4b6d0a4c
JM
1172 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
1173 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
1174 /* Stop PIT */
d12d51d5 1175 LOG_TB("%s: stop PIT\n", __func__);
bc72ad67 1176 timer_del(tb_env->decr_timer);
4b6d0a4c 1177 } else {
d12d51d5 1178 LOG_TB("%s: start PIT %016" PRIx64 "\n",
ddd1055b 1179 __func__, ppc40x_timer->pit_reload);
bc72ad67 1180 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
ddd1055b 1181 next = now + muldiv64(ppc40x_timer->pit_reload,
73bcb24d 1182 NANOSECONDS_PER_SECOND, tb_env->decr_freq);
4b6d0a4c
JM
1183 if (is_excp)
1184 next += tb_env->decr_next - now;
636aaad7
JM
1185 if (next == now)
1186 next++;
bc72ad67 1187 timer_mod(tb_env->decr_timer, next);
636aaad7
JM
1188 tb_env->decr_next = next;
1189 }
4b6d0a4c
JM
1190}
1191
1192static void cpu_4xx_pit_cb (void *opaque)
1193{
7058581a 1194 PowerPCCPU *cpu;
e2684c0b 1195 CPUPPCState *env;
c227f099 1196 ppc_tb_t *tb_env;
ddd1055b 1197 ppc40x_timer_t *ppc40x_timer;
4b6d0a4c
JM
1198
1199 env = opaque;
7058581a 1200 cpu = ppc_env_get_cpu(env);
4b6d0a4c 1201 tb_env = env->tb_env;
ddd1055b 1202 ppc40x_timer = tb_env->opaque;
636aaad7 1203 env->spr[SPR_40x_TSR] |= 1 << 27;
7058581a
AF
1204 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) {
1205 ppc_set_irq(cpu, ppc40x_timer->decr_excp, 1);
1206 }
4b6d0a4c 1207 start_stop_pit(env, tb_env, 1);
90e189ec
BS
1208 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " "
1209 "%016" PRIx64 "\n", __func__,
1210 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
1211 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
1212 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
ddd1055b 1213 ppc40x_timer->pit_reload);
636aaad7
JM
1214}
1215
1216/* Watchdog timer */
1217static void cpu_4xx_wdt_cb (void *opaque)
1218{
7058581a 1219 PowerPCCPU *cpu;
e2684c0b 1220 CPUPPCState *env;
c227f099 1221 ppc_tb_t *tb_env;
ddd1055b 1222 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
1223 uint64_t now, next;
1224
1225 env = opaque;
7058581a 1226 cpu = ppc_env_get_cpu(env);
636aaad7 1227 tb_env = env->tb_env;
ddd1055b 1228 ppc40x_timer = tb_env->opaque;
bc72ad67 1229 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
636aaad7
JM
1230 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
1231 case 0:
1232 next = 1 << 17;
1233 break;
1234 case 1:
1235 next = 1 << 21;
1236 break;
1237 case 2:
1238 next = 1 << 25;
1239 break;
1240 case 3:
1241 next = 1 << 29;
1242 break;
1243 default:
1244 /* Cannot occur, but makes gcc happy */
1245 return;
1246 }
73bcb24d 1247 next = now + muldiv64(next, NANOSECONDS_PER_SECOND, tb_env->decr_freq);
636aaad7
JM
1248 if (next == now)
1249 next++;
90e189ec
BS
1250 LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
1251 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
636aaad7
JM
1252 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
1253 case 0x0:
1254 case 0x1:
bc72ad67 1255 timer_mod(ppc40x_timer->wdt_timer, next);
ddd1055b 1256 ppc40x_timer->wdt_next = next;
a1f7f97b 1257 env->spr[SPR_40x_TSR] |= 1U << 31;
636aaad7
JM
1258 break;
1259 case 0x2:
bc72ad67 1260 timer_mod(ppc40x_timer->wdt_timer, next);
ddd1055b 1261 ppc40x_timer->wdt_next = next;
636aaad7 1262 env->spr[SPR_40x_TSR] |= 1 << 30;
7058581a
AF
1263 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) {
1264 ppc_set_irq(cpu, PPC_INTERRUPT_WDT, 1);
1265 }
636aaad7
JM
1266 break;
1267 case 0x3:
1268 env->spr[SPR_40x_TSR] &= ~0x30000000;
1269 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
1270 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
1271 case 0x0:
1272 /* No reset */
1273 break;
1274 case 0x1: /* Core reset */
f3273ba6 1275 ppc40x_core_reset(cpu);
8ecc7913 1276 break;
636aaad7 1277 case 0x2: /* Chip reset */
f3273ba6 1278 ppc40x_chip_reset(cpu);
8ecc7913 1279 break;
636aaad7 1280 case 0x3: /* System reset */
f3273ba6 1281 ppc40x_system_reset(cpu);
8ecc7913 1282 break;
636aaad7
JM
1283 }
1284 }
76a66253
JM
1285}
1286
e2684c0b 1287void store_40x_pit (CPUPPCState *env, target_ulong val)
76a66253 1288{
c227f099 1289 ppc_tb_t *tb_env;
ddd1055b 1290 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
1291
1292 tb_env = env->tb_env;
ddd1055b 1293 ppc40x_timer = tb_env->opaque;
90e189ec 1294 LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val);
ddd1055b 1295 ppc40x_timer->pit_reload = val;
4b6d0a4c 1296 start_stop_pit(env, tb_env, 0);
76a66253
JM
1297}
1298
e2684c0b 1299target_ulong load_40x_pit (CPUPPCState *env)
76a66253 1300{
636aaad7 1301 return cpu_ppc_load_decr(env);
76a66253
JM
1302}
1303
ddd1055b 1304static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq)
4b6d0a4c 1305{
e2684c0b 1306 CPUPPCState *env = opaque;
c227f099 1307 ppc_tb_t *tb_env = env->tb_env;
4b6d0a4c 1308
d12d51d5 1309 LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__,
aae9366a 1310 freq);
4b6d0a4c 1311 tb_env->tb_freq = freq;
dbdd2506 1312 tb_env->decr_freq = freq;
4b6d0a4c
JM
1313 /* XXX: we should also update all timers */
1314}
1315
e2684c0b 1316clk_setup_cb ppc_40x_timers_init (CPUPPCState *env, uint32_t freq,
d63cb48d 1317 unsigned int decr_excp)
636aaad7 1318{
c227f099 1319 ppc_tb_t *tb_env;
ddd1055b 1320 ppc40x_timer_t *ppc40x_timer;
636aaad7 1321
7267c094 1322 tb_env = g_malloc0(sizeof(ppc_tb_t));
8ecc7913 1323 env->tb_env = tb_env;
ddd1055b
FC
1324 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
1325 ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t));
8ecc7913 1326 tb_env->tb_freq = freq;
dbdd2506 1327 tb_env->decr_freq = freq;
ddd1055b 1328 tb_env->opaque = ppc40x_timer;
d12d51d5 1329 LOG_TB("%s freq %" PRIu32 "\n", __func__, freq);
ddd1055b 1330 if (ppc40x_timer != NULL) {
636aaad7 1331 /* We use decr timer for PIT */
bc72ad67 1332 tb_env->decr_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_pit_cb, env);
ddd1055b 1333 ppc40x_timer->fit_timer =
bc72ad67 1334 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_fit_cb, env);
ddd1055b 1335 ppc40x_timer->wdt_timer =
bc72ad67 1336 timer_new_ns(QEMU_CLOCK_VIRTUAL, &cpu_4xx_wdt_cb, env);
ddd1055b 1337 ppc40x_timer->decr_excp = decr_excp;
636aaad7 1338 }
8ecc7913 1339
ddd1055b 1340 return &ppc_40x_set_tb_clk;
76a66253
JM
1341}
1342
2e719ba3
JM
1343/*****************************************************************************/
1344/* Embedded PowerPC Device Control Registers */
c227f099
AL
1345typedef struct ppc_dcrn_t ppc_dcrn_t;
1346struct ppc_dcrn_t {
2e719ba3
JM
1347 dcr_read_cb dcr_read;
1348 dcr_write_cb dcr_write;
1349 void *opaque;
1350};
1351
a750fc0b
JM
1352/* XXX: on 460, DCR addresses are 32 bits wide,
1353 * using DCRIPR to get the 22 upper bits of the DCR address
1354 */
2e719ba3 1355#define DCRN_NB 1024
c227f099
AL
1356struct ppc_dcr_t {
1357 ppc_dcrn_t dcrn[DCRN_NB];
2e719ba3
JM
1358 int (*read_error)(int dcrn);
1359 int (*write_error)(int dcrn);
1360};
1361
73b01960 1362int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
2e719ba3 1363{
c227f099 1364 ppc_dcrn_t *dcr;
2e719ba3
JM
1365
1366 if (dcrn < 0 || dcrn >= DCRN_NB)
1367 goto error;
1368 dcr = &dcr_env->dcrn[dcrn];
1369 if (dcr->dcr_read == NULL)
1370 goto error;
1371 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
1372
1373 return 0;
1374
1375 error:
1376 if (dcr_env->read_error != NULL)
1377 return (*dcr_env->read_error)(dcrn);
1378
1379 return -1;
1380}
1381
73b01960 1382int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
2e719ba3 1383{
c227f099 1384 ppc_dcrn_t *dcr;
2e719ba3
JM
1385
1386 if (dcrn < 0 || dcrn >= DCRN_NB)
1387 goto error;
1388 dcr = &dcr_env->dcrn[dcrn];
1389 if (dcr->dcr_write == NULL)
1390 goto error;
1391 (*dcr->dcr_write)(dcr->opaque, dcrn, val);
1392
1393 return 0;
1394
1395 error:
1396 if (dcr_env->write_error != NULL)
1397 return (*dcr_env->write_error)(dcrn);
1398
1399 return -1;
1400}
1401
e2684c0b 1402int ppc_dcr_register (CPUPPCState *env, int dcrn, void *opaque,
2e719ba3
JM
1403 dcr_read_cb dcr_read, dcr_write_cb dcr_write)
1404{
c227f099
AL
1405 ppc_dcr_t *dcr_env;
1406 ppc_dcrn_t *dcr;
2e719ba3
JM
1407
1408 dcr_env = env->dcr_env;
1409 if (dcr_env == NULL)
1410 return -1;
1411 if (dcrn < 0 || dcrn >= DCRN_NB)
1412 return -1;
1413 dcr = &dcr_env->dcrn[dcrn];
1414 if (dcr->opaque != NULL ||
1415 dcr->dcr_read != NULL ||
1416 dcr->dcr_write != NULL)
1417 return -1;
1418 dcr->opaque = opaque;
1419 dcr->dcr_read = dcr_read;
1420 dcr->dcr_write = dcr_write;
1421
1422 return 0;
1423}
1424
e2684c0b 1425int ppc_dcr_init (CPUPPCState *env, int (*read_error)(int dcrn),
2e719ba3
JM
1426 int (*write_error)(int dcrn))
1427{
c227f099 1428 ppc_dcr_t *dcr_env;
2e719ba3 1429
7267c094 1430 dcr_env = g_malloc0(sizeof(ppc_dcr_t));
2e719ba3
JM
1431 dcr_env->read_error = read_error;
1432 dcr_env->write_error = write_error;
1433 env->dcr_env = dcr_env;
1434
1435 return 0;
1436}
1437
64201201
FB
1438/*****************************************************************************/
1439/* Debug port */
fd0bbb12 1440void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
64201201
FB
1441{
1442 addr &= 0xF;
1443 switch (addr) {
1444 case 0:
1445 printf("%c", val);
1446 break;
1447 case 1:
1448 printf("\n");
1449 fflush(stdout);
1450 break;
1451 case 2:
aae9366a 1452 printf("Set loglevel to %04" PRIx32 "\n", val);
24537a01 1453 qemu_set_log(val | 0x100);
64201201
FB
1454 break;
1455 }
1456}
This page took 1.254343 seconds and 4 git commands to generate.