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