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