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