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