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