]> git.proxmox.com Git - qemu.git/blame - hw/ppc.c
Merge remote-tracking branch 'awilliam/tags/qemu-1.4-vfio-20130109.0' into staging
[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}
9fddaa0c 431/*****************************************************************************/
e9df014c 432/* PowerPC time base and decrementer emulation */
9fddaa0c 433
ddd1055b 434uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset)
9fddaa0c
FB
435{
436 /* TB time in tb periods */
6ee093c9 437 return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset;
9fddaa0c
FB
438}
439
e2684c0b 440uint64_t cpu_ppc_load_tbl (CPUPPCState *env)
9fddaa0c 441{
c227f099 442 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c
FB
443 uint64_t tb;
444
90dc8812
SW
445 if (kvm_enabled()) {
446 return env->spr[SPR_TBL];
447 }
448
74475455 449 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
d12d51d5 450 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
9fddaa0c 451
e3ea6529 452 return tb;
9fddaa0c
FB
453}
454
e2684c0b 455static inline uint32_t _cpu_ppc_load_tbu(CPUPPCState *env)
9fddaa0c 456{
c227f099 457 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c
FB
458 uint64_t tb;
459
74475455 460 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
d12d51d5 461 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
76a66253 462
9fddaa0c
FB
463 return tb >> 32;
464}
465
e2684c0b 466uint32_t cpu_ppc_load_tbu (CPUPPCState *env)
8a84de23 467{
90dc8812
SW
468 if (kvm_enabled()) {
469 return env->spr[SPR_TBU];
470 }
471
8a84de23
JM
472 return _cpu_ppc_load_tbu(env);
473}
474
c227f099 475static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk,
636aa200 476 int64_t *tb_offsetp, uint64_t value)
9fddaa0c 477{
6ee093c9 478 *tb_offsetp = value - muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec());
d12d51d5 479 LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n",
aae9366a 480 __func__, value, *tb_offsetp);
9fddaa0c
FB
481}
482
e2684c0b 483void cpu_ppc_store_tbl (CPUPPCState *env, uint32_t value)
a062e36c 484{
c227f099 485 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
486 uint64_t tb;
487
74475455 488 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
a062e36c 489 tb &= 0xFFFFFFFF00000000ULL;
74475455 490 cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
dbdd2506 491 &tb_env->tb_offset, tb | (uint64_t)value);
a062e36c
JM
492}
493
e2684c0b 494static inline void _cpu_ppc_store_tbu(CPUPPCState *env, uint32_t value)
9fddaa0c 495{
c227f099 496 ppc_tb_t *tb_env = env->tb_env;
a062e36c 497 uint64_t tb;
9fddaa0c 498
74475455 499 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset);
a062e36c 500 tb &= 0x00000000FFFFFFFFULL;
74475455 501 cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
dbdd2506 502 &tb_env->tb_offset, ((uint64_t)value << 32) | tb);
9fddaa0c
FB
503}
504
e2684c0b 505void cpu_ppc_store_tbu (CPUPPCState *env, uint32_t value)
8a84de23
JM
506{
507 _cpu_ppc_store_tbu(env, value);
508}
509
e2684c0b 510uint64_t cpu_ppc_load_atbl (CPUPPCState *env)
a062e36c 511{
c227f099 512 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
513 uint64_t tb;
514
74475455 515 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
d12d51d5 516 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
a062e36c 517
b711de95 518 return tb;
a062e36c
JM
519}
520
e2684c0b 521uint32_t cpu_ppc_load_atbu (CPUPPCState *env)
a062e36c 522{
c227f099 523 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
524 uint64_t tb;
525
74475455 526 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
d12d51d5 527 LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb);
a062e36c
JM
528
529 return tb >> 32;
530}
531
e2684c0b 532void cpu_ppc_store_atbl (CPUPPCState *env, uint32_t value)
a062e36c 533{
c227f099 534 ppc_tb_t *tb_env = env->tb_env;
a062e36c
JM
535 uint64_t tb;
536
74475455 537 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
a062e36c 538 tb &= 0xFFFFFFFF00000000ULL;
74475455 539 cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
dbdd2506 540 &tb_env->atb_offset, tb | (uint64_t)value);
a062e36c
JM
541}
542
e2684c0b 543void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t value)
9fddaa0c 544{
c227f099 545 ppc_tb_t *tb_env = env->tb_env;
a062e36c 546 uint64_t tb;
9fddaa0c 547
74475455 548 tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset);
a062e36c 549 tb &= 0x00000000FFFFFFFFULL;
74475455 550 cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock),
dbdd2506
JM
551 &tb_env->atb_offset, ((uint64_t)value << 32) | tb);
552}
553
e2684c0b 554static void cpu_ppc_tb_stop (CPUPPCState *env)
dbdd2506 555{
c227f099 556 ppc_tb_t *tb_env = env->tb_env;
dbdd2506
JM
557 uint64_t tb, atb, vmclk;
558
559 /* If the time base is already frozen, do nothing */
560 if (tb_env->tb_freq != 0) {
74475455 561 vmclk = qemu_get_clock_ns(vm_clock);
dbdd2506
JM
562 /* Get the time base */
563 tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset);
564 /* Get the alternate time base */
565 atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset);
566 /* Store the time base value (ie compute the current offset) */
567 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
568 /* Store the alternate time base value (compute the current offset) */
569 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
570 /* Set the time base frequency to zero */
571 tb_env->tb_freq = 0;
572 /* Now, the time bases are frozen to tb_offset / atb_offset value */
573 }
574}
575
e2684c0b 576static void cpu_ppc_tb_start (CPUPPCState *env)
dbdd2506 577{
c227f099 578 ppc_tb_t *tb_env = env->tb_env;
dbdd2506 579 uint64_t tb, atb, vmclk;
aae9366a 580
dbdd2506
JM
581 /* If the time base is not frozen, do nothing */
582 if (tb_env->tb_freq == 0) {
74475455 583 vmclk = qemu_get_clock_ns(vm_clock);
dbdd2506
JM
584 /* Get the time base from tb_offset */
585 tb = tb_env->tb_offset;
586 /* Get the alternate time base from atb_offset */
587 atb = tb_env->atb_offset;
588 /* Restore the tb frequency from the decrementer frequency */
589 tb_env->tb_freq = tb_env->decr_freq;
590 /* Store the time base value */
591 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb);
592 /* Store the alternate time base value */
593 cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb);
594 }
9fddaa0c
FB
595}
596
e2684c0b 597static inline uint32_t _cpu_ppc_load_decr(CPUPPCState *env, uint64_t next)
9fddaa0c 598{
c227f099 599 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c 600 uint32_t decr;
4e588a4d 601 int64_t diff;
9fddaa0c 602
74475455 603 diff = next - qemu_get_clock_ns(vm_clock);
ddd1055b 604 if (diff >= 0) {
6ee093c9 605 decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec());
ddd1055b
FC
606 } else if (tb_env->flags & PPC_TIMER_BOOKE) {
607 decr = 0;
608 } else {
6ee093c9 609 decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec());
ddd1055b 610 }
d12d51d5 611 LOG_TB("%s: %08" PRIx32 "\n", __func__, decr);
76a66253 612
9fddaa0c
FB
613 return decr;
614}
615
e2684c0b 616uint32_t cpu_ppc_load_decr (CPUPPCState *env)
58a7d328 617{
c227f099 618 ppc_tb_t *tb_env = env->tb_env;
58a7d328 619
90dc8812
SW
620 if (kvm_enabled()) {
621 return env->spr[SPR_DECR];
622 }
623
f55e9d9a 624 return _cpu_ppc_load_decr(env, tb_env->decr_next);
58a7d328
JM
625}
626
e2684c0b 627uint32_t cpu_ppc_load_hdecr (CPUPPCState *env)
58a7d328 628{
c227f099 629 ppc_tb_t *tb_env = env->tb_env;
58a7d328 630
f55e9d9a 631 return _cpu_ppc_load_decr(env, tb_env->hdecr_next);
58a7d328
JM
632}
633
e2684c0b 634uint64_t cpu_ppc_load_purr (CPUPPCState *env)
58a7d328 635{
c227f099 636 ppc_tb_t *tb_env = env->tb_env;
58a7d328
JM
637 uint64_t diff;
638
74475455 639 diff = qemu_get_clock_ns(vm_clock) - tb_env->purr_start;
b33c17e1 640
6ee093c9 641 return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, get_ticks_per_sec());
58a7d328 642}
58a7d328 643
9fddaa0c
FB
644/* When decrementer expires,
645 * all we need to do is generate or queue a CPU exception
646 */
7e0a9247 647static inline void cpu_ppc_decr_excp(PowerPCCPU *cpu)
9fddaa0c
FB
648{
649 /* Raise it */
d12d51d5 650 LOG_TB("raise decrementer exception\n");
7058581a 651 ppc_set_irq(cpu, PPC_INTERRUPT_DECR, 1);
9fddaa0c
FB
652}
653
7e0a9247 654static inline void cpu_ppc_hdecr_excp(PowerPCCPU *cpu)
58a7d328
JM
655{
656 /* Raise it */
d12d51d5 657 LOG_TB("raise decrementer exception\n");
7058581a 658 ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 1);
58a7d328
JM
659}
660
7e0a9247
AF
661static void __cpu_ppc_store_decr(PowerPCCPU *cpu, uint64_t *nextp,
662 struct QEMUTimer *timer,
663 void (*raise_excp)(PowerPCCPU *),
664 uint32_t decr, uint32_t value,
665 int is_excp)
9fddaa0c 666{
7e0a9247 667 CPUPPCState *env = &cpu->env;
c227f099 668 ppc_tb_t *tb_env = env->tb_env;
9fddaa0c
FB
669 uint64_t now, next;
670
d12d51d5 671 LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__,
aae9366a 672 decr, value);
55f7d4b0
DG
673
674 if (kvm_enabled()) {
675 /* KVM handles decrementer exceptions, we don't need our own timer */
676 return;
677 }
678
74475455 679 now = qemu_get_clock_ns(vm_clock);
6ee093c9 680 next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq);
ddd1055b 681 if (is_excp) {
58a7d328 682 next += *nextp - now;
ddd1055b
FC
683 }
684 if (next == now) {
76a66253 685 next++;
ddd1055b 686 }
58a7d328 687 *nextp = next;
9fddaa0c 688 /* Adjust timer */
58a7d328 689 qemu_mod_timer(timer, next);
ddd1055b
FC
690
691 /* If we set a negative value and the decrementer was positive, raise an
692 * exception.
9fddaa0c 693 */
ddd1055b
FC
694 if ((tb_env->flags & PPC_DECR_UNDERFLOW_TRIGGERED)
695 && (value & 0x80000000)
696 && !(decr & 0x80000000)) {
7e0a9247 697 (*raise_excp)(cpu);
ddd1055b 698 }
58a7d328
JM
699}
700
7e0a9247 701static inline void _cpu_ppc_store_decr(PowerPCCPU *cpu, uint32_t decr,
636aa200 702 uint32_t value, int is_excp)
58a7d328 703{
7e0a9247 704 ppc_tb_t *tb_env = cpu->env.tb_env;
58a7d328 705
7e0a9247 706 __cpu_ppc_store_decr(cpu, &tb_env->decr_next, tb_env->decr_timer,
58a7d328 707 &cpu_ppc_decr_excp, decr, value, is_excp);
9fddaa0c
FB
708}
709
e2684c0b 710void cpu_ppc_store_decr (CPUPPCState *env, uint32_t value)
9fddaa0c 711{
7e0a9247
AF
712 PowerPCCPU *cpu = ppc_env_get_cpu(env);
713
714 _cpu_ppc_store_decr(cpu, cpu_ppc_load_decr(env), value, 0);
9fddaa0c
FB
715}
716
50c680f0 717static void cpu_ppc_decr_cb(void *opaque)
9fddaa0c 718{
50c680f0 719 PowerPCCPU *cpu = opaque;
7e0a9247 720
50c680f0 721 _cpu_ppc_store_decr(cpu, 0x00000000, 0xFFFFFFFF, 1);
9fddaa0c
FB
722}
723
7e0a9247 724static inline void _cpu_ppc_store_hdecr(PowerPCCPU *cpu, uint32_t hdecr,
636aa200 725 uint32_t value, int is_excp)
58a7d328 726{
7e0a9247 727 ppc_tb_t *tb_env = cpu->env.tb_env;
58a7d328 728
b172c56a 729 if (tb_env->hdecr_timer != NULL) {
7e0a9247 730 __cpu_ppc_store_decr(cpu, &tb_env->hdecr_next, tb_env->hdecr_timer,
b172c56a
JM
731 &cpu_ppc_hdecr_excp, hdecr, value, is_excp);
732 }
58a7d328
JM
733}
734
e2684c0b 735void cpu_ppc_store_hdecr (CPUPPCState *env, uint32_t value)
58a7d328 736{
7e0a9247
AF
737 PowerPCCPU *cpu = ppc_env_get_cpu(env);
738
739 _cpu_ppc_store_hdecr(cpu, cpu_ppc_load_hdecr(env), value, 0);
58a7d328
JM
740}
741
50c680f0 742static void cpu_ppc_hdecr_cb(void *opaque)
58a7d328 743{
50c680f0 744 PowerPCCPU *cpu = opaque;
7e0a9247 745
50c680f0 746 _cpu_ppc_store_hdecr(cpu, 0x00000000, 0xFFFFFFFF, 1);
58a7d328
JM
747}
748
7e0a9247 749static void cpu_ppc_store_purr(PowerPCCPU *cpu, uint64_t value)
58a7d328 750{
7e0a9247 751 ppc_tb_t *tb_env = cpu->env.tb_env;
58a7d328
JM
752
753 tb_env->purr_load = value;
74475455 754 tb_env->purr_start = qemu_get_clock_ns(vm_clock);
58a7d328 755}
58a7d328 756
8ecc7913
JM
757static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
758{
e2684c0b 759 CPUPPCState *env = opaque;
7e0a9247 760 PowerPCCPU *cpu = ppc_env_get_cpu(env);
c227f099 761 ppc_tb_t *tb_env = env->tb_env;
8ecc7913
JM
762
763 tb_env->tb_freq = freq;
dbdd2506 764 tb_env->decr_freq = freq;
8ecc7913
JM
765 /* There is a bug in Linux 2.4 kernels:
766 * if a decrementer exception is pending when it enables msr_ee at startup,
767 * it's not ready to handle it...
768 */
7e0a9247
AF
769 _cpu_ppc_store_decr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 0);
770 _cpu_ppc_store_hdecr(cpu, 0xFFFFFFFF, 0xFFFFFFFF, 0);
771 cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
8ecc7913
JM
772}
773
9fddaa0c 774/* Set up (once) timebase frequency (in Hz) */
e2684c0b 775clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
9fddaa0c 776{
50c680f0 777 PowerPCCPU *cpu = ppc_env_get_cpu(env);
c227f099 778 ppc_tb_t *tb_env;
9fddaa0c 779
7267c094 780 tb_env = g_malloc0(sizeof(ppc_tb_t));
9fddaa0c 781 env->tb_env = tb_env;
ddd1055b 782 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
8ecc7913 783 /* Create new timer */
50c680f0 784 tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_decr_cb, cpu);
b172c56a
JM
785 if (0) {
786 /* XXX: find a suitable condition to enable the hypervisor decrementer
787 */
50c680f0
AF
788 tb_env->hdecr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_hdecr_cb,
789 cpu);
b172c56a
JM
790 } else {
791 tb_env->hdecr_timer = NULL;
792 }
8ecc7913 793 cpu_ppc_set_tb_clk(env, freq);
9fddaa0c 794
8ecc7913 795 return &cpu_ppc_set_tb_clk;
9fddaa0c
FB
796}
797
76a66253 798/* Specific helpers for POWER & PowerPC 601 RTC */
b1d8e52e 799#if 0
e2684c0b 800static clk_setup_cb cpu_ppc601_rtc_init (CPUPPCState *env)
76a66253
JM
801{
802 return cpu_ppc_tb_init(env, 7812500);
803}
b1d8e52e 804#endif
76a66253 805
e2684c0b 806void cpu_ppc601_store_rtcu (CPUPPCState *env, uint32_t value)
8a84de23
JM
807{
808 _cpu_ppc_store_tbu(env, value);
809}
76a66253 810
e2684c0b 811uint32_t cpu_ppc601_load_rtcu (CPUPPCState *env)
8a84de23
JM
812{
813 return _cpu_ppc_load_tbu(env);
814}
76a66253 815
e2684c0b 816void cpu_ppc601_store_rtcl (CPUPPCState *env, uint32_t value)
76a66253
JM
817{
818 cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
819}
820
e2684c0b 821uint32_t cpu_ppc601_load_rtcl (CPUPPCState *env)
76a66253
JM
822{
823 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
824}
825
636aaad7 826/*****************************************************************************/
ddd1055b 827/* PowerPC 40x timers */
636aaad7
JM
828
829/* PIT, FIT & WDT */
ddd1055b
FC
830typedef struct ppc40x_timer_t ppc40x_timer_t;
831struct ppc40x_timer_t {
636aaad7
JM
832 uint64_t pit_reload; /* PIT auto-reload value */
833 uint64_t fit_next; /* Tick for next FIT interrupt */
834 struct QEMUTimer *fit_timer;
835 uint64_t wdt_next; /* Tick for next WDT interrupt */
836 struct QEMUTimer *wdt_timer;
d63cb48d
EI
837
838 /* 405 have the PIT, 440 have a DECR. */
839 unsigned int decr_excp;
636aaad7 840};
3b46e624 841
636aaad7
JM
842/* Fixed interval timer */
843static void cpu_4xx_fit_cb (void *opaque)
844{
7058581a 845 PowerPCCPU *cpu;
e2684c0b 846 CPUPPCState *env;
c227f099 847 ppc_tb_t *tb_env;
ddd1055b 848 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
849 uint64_t now, next;
850
851 env = opaque;
7058581a 852 cpu = ppc_env_get_cpu(env);
636aaad7 853 tb_env = env->tb_env;
ddd1055b 854 ppc40x_timer = tb_env->opaque;
74475455 855 now = qemu_get_clock_ns(vm_clock);
636aaad7
JM
856 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
857 case 0:
858 next = 1 << 9;
859 break;
860 case 1:
861 next = 1 << 13;
862 break;
863 case 2:
864 next = 1 << 17;
865 break;
866 case 3:
867 next = 1 << 21;
868 break;
869 default:
870 /* Cannot occur, but makes gcc happy */
871 return;
872 }
6ee093c9 873 next = now + muldiv64(next, get_ticks_per_sec(), tb_env->tb_freq);
636aaad7
JM
874 if (next == now)
875 next++;
ddd1055b 876 qemu_mod_timer(ppc40x_timer->fit_timer, next);
636aaad7 877 env->spr[SPR_40x_TSR] |= 1 << 26;
7058581a
AF
878 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) {
879 ppc_set_irq(cpu, PPC_INTERRUPT_FIT, 1);
880 }
90e189ec
BS
881 LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
882 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
883 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
636aaad7
JM
884}
885
886/* Programmable interval timer */
e2684c0b 887static void start_stop_pit (CPUPPCState *env, ppc_tb_t *tb_env, int is_excp)
76a66253 888{
ddd1055b 889 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
890 uint64_t now, next;
891
ddd1055b
FC
892 ppc40x_timer = tb_env->opaque;
893 if (ppc40x_timer->pit_reload <= 1 ||
4b6d0a4c
JM
894 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
895 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
896 /* Stop PIT */
d12d51d5 897 LOG_TB("%s: stop PIT\n", __func__);
4b6d0a4c
JM
898 qemu_del_timer(tb_env->decr_timer);
899 } else {
d12d51d5 900 LOG_TB("%s: start PIT %016" PRIx64 "\n",
ddd1055b 901 __func__, ppc40x_timer->pit_reload);
74475455 902 now = qemu_get_clock_ns(vm_clock);
ddd1055b 903 next = now + muldiv64(ppc40x_timer->pit_reload,
6ee093c9 904 get_ticks_per_sec(), tb_env->decr_freq);
4b6d0a4c
JM
905 if (is_excp)
906 next += tb_env->decr_next - now;
636aaad7
JM
907 if (next == now)
908 next++;
909 qemu_mod_timer(tb_env->decr_timer, next);
910 tb_env->decr_next = next;
911 }
4b6d0a4c
JM
912}
913
914static void cpu_4xx_pit_cb (void *opaque)
915{
7058581a 916 PowerPCCPU *cpu;
e2684c0b 917 CPUPPCState *env;
c227f099 918 ppc_tb_t *tb_env;
ddd1055b 919 ppc40x_timer_t *ppc40x_timer;
4b6d0a4c
JM
920
921 env = opaque;
7058581a 922 cpu = ppc_env_get_cpu(env);
4b6d0a4c 923 tb_env = env->tb_env;
ddd1055b 924 ppc40x_timer = tb_env->opaque;
636aaad7 925 env->spr[SPR_40x_TSR] |= 1 << 27;
7058581a
AF
926 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) {
927 ppc_set_irq(cpu, ppc40x_timer->decr_excp, 1);
928 }
4b6d0a4c 929 start_stop_pit(env, tb_env, 1);
90e189ec
BS
930 LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " "
931 "%016" PRIx64 "\n", __func__,
932 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
933 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
934 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
ddd1055b 935 ppc40x_timer->pit_reload);
636aaad7
JM
936}
937
938/* Watchdog timer */
939static void cpu_4xx_wdt_cb (void *opaque)
940{
7058581a 941 PowerPCCPU *cpu;
e2684c0b 942 CPUPPCState *env;
c227f099 943 ppc_tb_t *tb_env;
ddd1055b 944 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
945 uint64_t now, next;
946
947 env = opaque;
7058581a 948 cpu = ppc_env_get_cpu(env);
636aaad7 949 tb_env = env->tb_env;
ddd1055b 950 ppc40x_timer = tb_env->opaque;
74475455 951 now = qemu_get_clock_ns(vm_clock);
636aaad7
JM
952 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
953 case 0:
954 next = 1 << 17;
955 break;
956 case 1:
957 next = 1 << 21;
958 break;
959 case 2:
960 next = 1 << 25;
961 break;
962 case 3:
963 next = 1 << 29;
964 break;
965 default:
966 /* Cannot occur, but makes gcc happy */
967 return;
968 }
6ee093c9 969 next = now + muldiv64(next, get_ticks_per_sec(), tb_env->decr_freq);
636aaad7
JM
970 if (next == now)
971 next++;
90e189ec
BS
972 LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__,
973 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
636aaad7
JM
974 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
975 case 0x0:
976 case 0x1:
ddd1055b
FC
977 qemu_mod_timer(ppc40x_timer->wdt_timer, next);
978 ppc40x_timer->wdt_next = next;
636aaad7
JM
979 env->spr[SPR_40x_TSR] |= 1 << 31;
980 break;
981 case 0x2:
ddd1055b
FC
982 qemu_mod_timer(ppc40x_timer->wdt_timer, next);
983 ppc40x_timer->wdt_next = next;
636aaad7 984 env->spr[SPR_40x_TSR] |= 1 << 30;
7058581a
AF
985 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) {
986 ppc_set_irq(cpu, PPC_INTERRUPT_WDT, 1);
987 }
636aaad7
JM
988 break;
989 case 0x3:
990 env->spr[SPR_40x_TSR] &= ~0x30000000;
991 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
992 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
993 case 0x0:
994 /* No reset */
995 break;
996 case 0x1: /* Core reset */
8ecc7913
JM
997 ppc40x_core_reset(env);
998 break;
636aaad7 999 case 0x2: /* Chip reset */
8ecc7913
JM
1000 ppc40x_chip_reset(env);
1001 break;
636aaad7 1002 case 0x3: /* System reset */
8ecc7913
JM
1003 ppc40x_system_reset(env);
1004 break;
636aaad7
JM
1005 }
1006 }
76a66253
JM
1007}
1008
e2684c0b 1009void store_40x_pit (CPUPPCState *env, target_ulong val)
76a66253 1010{
c227f099 1011 ppc_tb_t *tb_env;
ddd1055b 1012 ppc40x_timer_t *ppc40x_timer;
636aaad7
JM
1013
1014 tb_env = env->tb_env;
ddd1055b 1015 ppc40x_timer = tb_env->opaque;
90e189ec 1016 LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val);
ddd1055b 1017 ppc40x_timer->pit_reload = val;
4b6d0a4c 1018 start_stop_pit(env, tb_env, 0);
76a66253
JM
1019}
1020
e2684c0b 1021target_ulong load_40x_pit (CPUPPCState *env)
76a66253 1022{
636aaad7 1023 return cpu_ppc_load_decr(env);
76a66253
JM
1024}
1025
ddd1055b 1026static void ppc_40x_set_tb_clk (void *opaque, uint32_t freq)
4b6d0a4c 1027{
e2684c0b 1028 CPUPPCState *env = opaque;
c227f099 1029 ppc_tb_t *tb_env = env->tb_env;
4b6d0a4c 1030
d12d51d5 1031 LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__,
aae9366a 1032 freq);
4b6d0a4c 1033 tb_env->tb_freq = freq;
dbdd2506 1034 tb_env->decr_freq = freq;
4b6d0a4c
JM
1035 /* XXX: we should also update all timers */
1036}
1037
e2684c0b 1038clk_setup_cb ppc_40x_timers_init (CPUPPCState *env, uint32_t freq,
d63cb48d 1039 unsigned int decr_excp)
636aaad7 1040{
c227f099 1041 ppc_tb_t *tb_env;
ddd1055b 1042 ppc40x_timer_t *ppc40x_timer;
636aaad7 1043
7267c094 1044 tb_env = g_malloc0(sizeof(ppc_tb_t));
8ecc7913 1045 env->tb_env = tb_env;
ddd1055b
FC
1046 tb_env->flags = PPC_DECR_UNDERFLOW_TRIGGERED;
1047 ppc40x_timer = g_malloc0(sizeof(ppc40x_timer_t));
8ecc7913 1048 tb_env->tb_freq = freq;
dbdd2506 1049 tb_env->decr_freq = freq;
ddd1055b 1050 tb_env->opaque = ppc40x_timer;
d12d51d5 1051 LOG_TB("%s freq %" PRIu32 "\n", __func__, freq);
ddd1055b 1052 if (ppc40x_timer != NULL) {
636aaad7 1053 /* We use decr timer for PIT */
74475455 1054 tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_4xx_pit_cb, env);
ddd1055b 1055 ppc40x_timer->fit_timer =
74475455 1056 qemu_new_timer_ns(vm_clock, &cpu_4xx_fit_cb, env);
ddd1055b 1057 ppc40x_timer->wdt_timer =
74475455 1058 qemu_new_timer_ns(vm_clock, &cpu_4xx_wdt_cb, env);
ddd1055b 1059 ppc40x_timer->decr_excp = decr_excp;
636aaad7 1060 }
8ecc7913 1061
ddd1055b 1062 return &ppc_40x_set_tb_clk;
76a66253
JM
1063}
1064
2e719ba3
JM
1065/*****************************************************************************/
1066/* Embedded PowerPC Device Control Registers */
c227f099
AL
1067typedef struct ppc_dcrn_t ppc_dcrn_t;
1068struct ppc_dcrn_t {
2e719ba3
JM
1069 dcr_read_cb dcr_read;
1070 dcr_write_cb dcr_write;
1071 void *opaque;
1072};
1073
a750fc0b
JM
1074/* XXX: on 460, DCR addresses are 32 bits wide,
1075 * using DCRIPR to get the 22 upper bits of the DCR address
1076 */
2e719ba3 1077#define DCRN_NB 1024
c227f099
AL
1078struct ppc_dcr_t {
1079 ppc_dcrn_t dcrn[DCRN_NB];
2e719ba3
JM
1080 int (*read_error)(int dcrn);
1081 int (*write_error)(int dcrn);
1082};
1083
73b01960 1084int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
2e719ba3 1085{
c227f099 1086 ppc_dcrn_t *dcr;
2e719ba3
JM
1087
1088 if (dcrn < 0 || dcrn >= DCRN_NB)
1089 goto error;
1090 dcr = &dcr_env->dcrn[dcrn];
1091 if (dcr->dcr_read == NULL)
1092 goto error;
1093 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
1094
1095 return 0;
1096
1097 error:
1098 if (dcr_env->read_error != NULL)
1099 return (*dcr_env->read_error)(dcrn);
1100
1101 return -1;
1102}
1103
73b01960 1104int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
2e719ba3 1105{
c227f099 1106 ppc_dcrn_t *dcr;
2e719ba3
JM
1107
1108 if (dcrn < 0 || dcrn >= DCRN_NB)
1109 goto error;
1110 dcr = &dcr_env->dcrn[dcrn];
1111 if (dcr->dcr_write == NULL)
1112 goto error;
1113 (*dcr->dcr_write)(dcr->opaque, dcrn, val);
1114
1115 return 0;
1116
1117 error:
1118 if (dcr_env->write_error != NULL)
1119 return (*dcr_env->write_error)(dcrn);
1120
1121 return -1;
1122}
1123
e2684c0b 1124int ppc_dcr_register (CPUPPCState *env, int dcrn, void *opaque,
2e719ba3
JM
1125 dcr_read_cb dcr_read, dcr_write_cb dcr_write)
1126{
c227f099
AL
1127 ppc_dcr_t *dcr_env;
1128 ppc_dcrn_t *dcr;
2e719ba3
JM
1129
1130 dcr_env = env->dcr_env;
1131 if (dcr_env == NULL)
1132 return -1;
1133 if (dcrn < 0 || dcrn >= DCRN_NB)
1134 return -1;
1135 dcr = &dcr_env->dcrn[dcrn];
1136 if (dcr->opaque != NULL ||
1137 dcr->dcr_read != NULL ||
1138 dcr->dcr_write != NULL)
1139 return -1;
1140 dcr->opaque = opaque;
1141 dcr->dcr_read = dcr_read;
1142 dcr->dcr_write = dcr_write;
1143
1144 return 0;
1145}
1146
e2684c0b 1147int ppc_dcr_init (CPUPPCState *env, int (*read_error)(int dcrn),
2e719ba3
JM
1148 int (*write_error)(int dcrn))
1149{
c227f099 1150 ppc_dcr_t *dcr_env;
2e719ba3 1151
7267c094 1152 dcr_env = g_malloc0(sizeof(ppc_dcr_t));
2e719ba3
JM
1153 dcr_env->read_error = read_error;
1154 dcr_env->write_error = write_error;
1155 env->dcr_env = dcr_env;
1156
1157 return 0;
1158}
1159
64201201
FB
1160/*****************************************************************************/
1161/* Debug port */
fd0bbb12 1162void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
64201201
FB
1163{
1164 addr &= 0xF;
1165 switch (addr) {
1166 case 0:
1167 printf("%c", val);
1168 break;
1169 case 1:
1170 printf("\n");
1171 fflush(stdout);
1172 break;
1173 case 2:
aae9366a 1174 printf("Set loglevel to %04" PRIx32 "\n", val);
fd0bbb12 1175 cpu_set_log(val | 0x100);
64201201
FB
1176 break;
1177 }
1178}
1179
1180/*****************************************************************************/
1181/* NVRAM helpers */
c227f099 1182static inline uint32_t nvram_read (nvram_t *nvram, uint32_t addr)
64201201 1183{
3a93113a 1184 return (*nvram->read_fn)(nvram->opaque, addr);
64201201
FB
1185}
1186
c227f099 1187static inline void nvram_write (nvram_t *nvram, uint32_t addr, uint32_t val)
64201201 1188{
3cbee15b 1189 (*nvram->write_fn)(nvram->opaque, addr, val);
64201201
FB
1190}
1191
43448292 1192static void NVRAM_set_byte(nvram_t *nvram, uint32_t addr, uint8_t value)
64201201 1193{
3cbee15b 1194 nvram_write(nvram, addr, value);
64201201
FB
1195}
1196
43448292 1197static uint8_t NVRAM_get_byte(nvram_t *nvram, uint32_t addr)
3cbee15b
JM
1198{
1199 return nvram_read(nvram, addr);
1200}
1201
43448292 1202static void NVRAM_set_word(nvram_t *nvram, uint32_t addr, uint16_t value)
3cbee15b
JM
1203{
1204 nvram_write(nvram, addr, value >> 8);
1205 nvram_write(nvram, addr + 1, value & 0xFF);
1206}
1207
43448292 1208static uint16_t NVRAM_get_word(nvram_t *nvram, uint32_t addr)
64201201
FB
1209{
1210 uint16_t tmp;
1211
3cbee15b
JM
1212 tmp = nvram_read(nvram, addr) << 8;
1213 tmp |= nvram_read(nvram, addr + 1);
1214
64201201
FB
1215 return tmp;
1216}
1217
43448292 1218static void NVRAM_set_lword(nvram_t *nvram, uint32_t addr, uint32_t value)
64201201 1219{
3cbee15b
JM
1220 nvram_write(nvram, addr, value >> 24);
1221 nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
1222 nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
1223 nvram_write(nvram, addr + 3, value & 0xFF);
64201201
FB
1224}
1225
c227f099 1226uint32_t NVRAM_get_lword (nvram_t *nvram, uint32_t addr)
64201201
FB
1227{
1228 uint32_t tmp;
1229
3cbee15b
JM
1230 tmp = nvram_read(nvram, addr) << 24;
1231 tmp |= nvram_read(nvram, addr + 1) << 16;
1232 tmp |= nvram_read(nvram, addr + 2) << 8;
1233 tmp |= nvram_read(nvram, addr + 3);
76a66253 1234
64201201
FB
1235 return tmp;
1236}
1237
43448292
BS
1238static void NVRAM_set_string(nvram_t *nvram, uint32_t addr, const char *str,
1239 uint32_t max)
64201201
FB
1240{
1241 int i;
1242
1243 for (i = 0; i < max && str[i] != '\0'; i++) {
3cbee15b 1244 nvram_write(nvram, addr + i, str[i]);
64201201 1245 }
3cbee15b
JM
1246 nvram_write(nvram, addr + i, str[i]);
1247 nvram_write(nvram, addr + max - 1, '\0');
64201201
FB
1248}
1249
c227f099 1250int NVRAM_get_string (nvram_t *nvram, uint8_t *dst, uint16_t addr, int max)
64201201
FB
1251{
1252 int i;
1253
1254 memset(dst, 0, max);
1255 for (i = 0; i < max; i++) {
1256 dst[i] = NVRAM_get_byte(nvram, addr + i);
1257 if (dst[i] == '\0')
1258 break;
1259 }
1260
1261 return i;
1262}
1263
1264static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
1265{
1266 uint16_t tmp;
1267 uint16_t pd, pd1, pd2;
1268
1269 tmp = prev >> 8;
1270 pd = prev ^ value;
1271 pd1 = pd & 0x000F;
1272 pd2 = ((pd >> 4) & 0x000F) ^ pd1;
1273 tmp ^= (pd1 << 3) | (pd1 << 8);
1274 tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
1275
1276 return tmp;
1277}
1278
c227f099 1279static uint16_t NVRAM_compute_crc (nvram_t *nvram, uint32_t start, uint32_t count)
64201201
FB
1280{
1281 uint32_t i;
1282 uint16_t crc = 0xFFFF;
1283 int odd;
1284
1285 odd = count & 1;
1286 count &= ~1;
1287 for (i = 0; i != count; i++) {
76a66253 1288 crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
64201201
FB
1289 }
1290 if (odd) {
76a66253 1291 crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
64201201
FB
1292 }
1293
1294 return crc;
1295}
1296
fd0bbb12
FB
1297#define CMDLINE_ADDR 0x017ff000
1298
c227f099 1299int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size,
b55266b5 1300 const char *arch,
64201201
FB
1301 uint32_t RAM_size, int boot_device,
1302 uint32_t kernel_image, uint32_t kernel_size,
fd0bbb12 1303 const char *cmdline,
64201201 1304 uint32_t initrd_image, uint32_t initrd_size,
fd0bbb12
FB
1305 uint32_t NVRAM_image,
1306 int width, int height, int depth)
64201201
FB
1307{
1308 uint16_t crc;
1309
1310 /* Set parameters for Open Hack'Ware BIOS */
1311 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
1312 NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
1313 NVRAM_set_word(nvram, 0x14, NVRAM_size);
1314 NVRAM_set_string(nvram, 0x20, arch, 16);
1315 NVRAM_set_lword(nvram, 0x30, RAM_size);
1316 NVRAM_set_byte(nvram, 0x34, boot_device);
1317 NVRAM_set_lword(nvram, 0x38, kernel_image);
1318 NVRAM_set_lword(nvram, 0x3C, kernel_size);
fd0bbb12
FB
1319 if (cmdline) {
1320 /* XXX: put the cmdline in NVRAM too ? */
3c178e72 1321 pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR, cmdline);
fd0bbb12
FB
1322 NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
1323 NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
1324 } else {
1325 NVRAM_set_lword(nvram, 0x40, 0);
1326 NVRAM_set_lword(nvram, 0x44, 0);
1327 }
64201201
FB
1328 NVRAM_set_lword(nvram, 0x48, initrd_image);
1329 NVRAM_set_lword(nvram, 0x4C, initrd_size);
1330 NVRAM_set_lword(nvram, 0x50, NVRAM_image);
fd0bbb12
FB
1331
1332 NVRAM_set_word(nvram, 0x54, width);
1333 NVRAM_set_word(nvram, 0x56, height);
1334 NVRAM_set_word(nvram, 0x58, depth);
1335 crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
3cbee15b 1336 NVRAM_set_word(nvram, 0xFC, crc);
64201201
FB
1337
1338 return 0;
a541f297 1339}