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