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