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