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