Add PowerPC power-management state check callback.
[qemu] / hw / apic.c
1 /*
2  *  APIC support
3  *
4  *  Copyright (c) 2004-2005 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "vl.h"
21
22 //#define DEBUG_APIC
23 //#define DEBUG_IOAPIC
24
25 /* APIC Local Vector Table */
26 #define APIC_LVT_TIMER   0
27 #define APIC_LVT_THERMAL 1
28 #define APIC_LVT_PERFORM 2
29 #define APIC_LVT_LINT0   3
30 #define APIC_LVT_LINT1   4
31 #define APIC_LVT_ERROR   5
32 #define APIC_LVT_NB      6
33
34 /* APIC delivery modes */
35 #define APIC_DM_FIXED   0
36 #define APIC_DM_LOWPRI  1
37 #define APIC_DM_SMI     2
38 #define APIC_DM_NMI     4
39 #define APIC_DM_INIT    5
40 #define APIC_DM_SIPI    6
41 #define APIC_DM_EXTINT  7
42
43 /* APIC destination mode */
44 #define APIC_DESTMODE_FLAT      0xf
45 #define APIC_DESTMODE_CLUSTER   1
46
47 #define APIC_TRIGGER_EDGE  0
48 #define APIC_TRIGGER_LEVEL 1
49
50 #define APIC_LVT_TIMER_PERIODIC         (1<<17)
51 #define APIC_LVT_MASKED                 (1<<16)
52 #define APIC_LVT_LEVEL_TRIGGER          (1<<15)
53 #define APIC_LVT_REMOTE_IRR             (1<<14)
54 #define APIC_INPUT_POLARITY             (1<<13)
55 #define APIC_SEND_PENDING               (1<<12)
56
57 #define IOAPIC_NUM_PINS                 0x18
58
59 #define ESR_ILLEGAL_ADDRESS (1 << 7)
60
61 #define APIC_SV_ENABLE (1 << 8)
62
63 #define MAX_APICS 255
64 #define MAX_APIC_WORDS 8
65
66 typedef struct APICState {
67     CPUState *cpu_env;
68     uint32_t apicbase;
69     uint8_t id;
70     uint8_t arb_id;
71     uint8_t tpr;
72     uint32_t spurious_vec;
73     uint8_t log_dest;
74     uint8_t dest_mode;
75     uint32_t isr[8];  /* in service register */
76     uint32_t tmr[8];  /* trigger mode register */
77     uint32_t irr[8]; /* interrupt request register */
78     uint32_t lvt[APIC_LVT_NB];
79     uint32_t esr; /* error register */
80     uint32_t icr[2];
81
82     uint32_t divide_conf;
83     int count_shift;
84     uint32_t initial_count;
85     int64_t initial_count_load_time, next_time;
86     QEMUTimer *timer;
87 } APICState;
88
89 struct IOAPICState {
90     uint8_t id;
91     uint8_t ioregsel;
92
93     uint32_t irr;
94     uint64_t ioredtbl[IOAPIC_NUM_PINS];
95 };
96
97 static int apic_io_memory;
98 static APICState *local_apics[MAX_APICS + 1];
99 static int last_apic_id = 0;
100
101 static void apic_init_ipi(APICState *s);
102 static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
103 static void apic_update_irq(APICState *s);
104
105 /* Find first bit starting from msb. Return 0 if value = 0 */
106 static int fls_bit(uint32_t value)
107 {
108     unsigned int ret = 0;
109
110 #if defined(HOST_I386)
111     __asm__ __volatile__ ("bsr %1, %0\n" : "+r" (ret) : "rm" (value));
112     return ret;
113 #else
114     if (value > 0xffff)
115         value >>= 16, ret = 16;
116     if (value > 0xff)
117         value >>= 8, ret += 8;
118     if (value > 0xf)
119         value >>= 4, ret += 4;
120     if (value > 0x3)
121         value >>= 2, ret += 2;
122     return ret + (value >> 1);
123 #endif
124 }
125
126 /* Find first bit starting from lsb. Return 0 if value = 0 */
127 static int ffs_bit(uint32_t value)
128 {
129     unsigned int ret = 0;
130
131 #if defined(HOST_I386)
132     __asm__ __volatile__ ("bsf %1, %0\n" : "+r" (ret) : "rm" (value));
133     return ret;
134 #else
135     if (!value)
136         return 0;
137     if (!(value & 0xffff))
138         value >>= 16, ret = 16;
139     if (!(value & 0xff))
140         value >>= 8, ret += 8;
141     if (!(value & 0xf))
142         value >>= 4, ret += 4;
143     if (!(value & 0x3))
144         value >>= 2, ret += 2;
145     if (!(value & 0x1))
146         ret++;
147     return ret;
148 #endif
149 }
150
151 static inline void set_bit(uint32_t *tab, int index)
152 {
153     int i, mask;
154     i = index >> 5;
155     mask = 1 << (index & 0x1f);
156     tab[i] |= mask;
157 }
158
159 static inline void reset_bit(uint32_t *tab, int index)
160 {
161     int i, mask;
162     i = index >> 5;
163     mask = 1 << (index & 0x1f);
164     tab[i] &= ~mask;
165 }
166
167 #define foreach_apic(apic, deliver_bitmask, code) \
168 {\
169     int __i, __j, __mask;\
170     for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
171         __mask = deliver_bitmask[__i];\
172         if (__mask) {\
173             for(__j = 0; __j < 32; __j++) {\
174                 if (__mask & (1 << __j)) {\
175                     apic = local_apics[__i * 32 + __j];\
176                     if (apic) {\
177                         code;\
178                     }\
179                 }\
180             }\
181         }\
182     }\
183 }
184
185 static void apic_bus_deliver(const uint32_t *deliver_bitmask,
186                              uint8_t delivery_mode,
187                              uint8_t vector_num, uint8_t polarity,
188                              uint8_t trigger_mode)
189 {
190     APICState *apic_iter;
191
192     switch (delivery_mode) {
193         case APIC_DM_LOWPRI:
194             /* XXX: search for focus processor, arbitration */
195             {
196                 int i, d;
197                 d = -1;
198                 for(i = 0; i < MAX_APIC_WORDS; i++) {
199                     if (deliver_bitmask[i]) {
200                         d = i * 32 + ffs_bit(deliver_bitmask[i]);
201                         break;
202                     }
203                 }
204                 if (d >= 0) {
205                     apic_iter = local_apics[d];
206                     if (apic_iter) {
207                         apic_set_irq(apic_iter, vector_num, trigger_mode);
208                     }
209                 }
210             }
211             return;
212
213         case APIC_DM_FIXED:
214             break;
215
216         case APIC_DM_SMI:
217         case APIC_DM_NMI:
218             break;
219
220         case APIC_DM_INIT:
221             /* normal INIT IPI sent to processors */
222             foreach_apic(apic_iter, deliver_bitmask,
223                          apic_init_ipi(apic_iter) );
224             return;
225
226         case APIC_DM_EXTINT:
227             /* handled in I/O APIC code */
228             break;
229
230         default:
231             return;
232     }
233
234     foreach_apic(apic_iter, deliver_bitmask,
235                  apic_set_irq(apic_iter, vector_num, trigger_mode) );
236 }
237
238 void cpu_set_apic_base(CPUState *env, uint64_t val)
239 {
240     APICState *s = env->apic_state;
241 #ifdef DEBUG_APIC
242     printf("cpu_set_apic_base: %016" PRIx64 "\n", val);
243 #endif
244     s->apicbase = (val & 0xfffff000) |
245         (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
246     /* if disabled, cannot be enabled again */
247     if (!(val & MSR_IA32_APICBASE_ENABLE)) {
248         s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
249         env->cpuid_features &= ~CPUID_APIC;
250         s->spurious_vec &= ~APIC_SV_ENABLE;
251     }
252 }
253
254 uint64_t cpu_get_apic_base(CPUState *env)
255 {
256     APICState *s = env->apic_state;
257 #ifdef DEBUG_APIC
258     printf("cpu_get_apic_base: %016" PRIx64 "\n", (uint64_t)s->apicbase);
259 #endif
260     return s->apicbase;
261 }
262
263 void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
264 {
265     APICState *s = env->apic_state;
266     s->tpr = (val & 0x0f) << 4;
267     apic_update_irq(s);
268 }
269
270 uint8_t cpu_get_apic_tpr(CPUX86State *env)
271 {
272     APICState *s = env->apic_state;
273     return s->tpr >> 4;
274 }
275
276 /* return -1 if no bit is set */
277 static int get_highest_priority_int(uint32_t *tab)
278 {
279     int i;
280     for(i = 7; i >= 0; i--) {
281         if (tab[i] != 0) {
282             return i * 32 + fls_bit(tab[i]);
283         }
284     }
285     return -1;
286 }
287
288 static int apic_get_ppr(APICState *s)
289 {
290     int tpr, isrv, ppr;
291
292     tpr = (s->tpr >> 4);
293     isrv = get_highest_priority_int(s->isr);
294     if (isrv < 0)
295         isrv = 0;
296     isrv >>= 4;
297     if (tpr >= isrv)
298         ppr = s->tpr;
299     else
300         ppr = isrv << 4;
301     return ppr;
302 }
303
304 static int apic_get_arb_pri(APICState *s)
305 {
306     /* XXX: arbitration */
307     return 0;
308 }
309
310 /* signal the CPU if an irq is pending */
311 static void apic_update_irq(APICState *s)
312 {
313     int irrv, ppr;
314     if (!(s->spurious_vec & APIC_SV_ENABLE))
315         return;
316     irrv = get_highest_priority_int(s->irr);
317     if (irrv < 0)
318         return;
319     ppr = apic_get_ppr(s);
320     if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
321         return;
322     cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
323 }
324
325 static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
326 {
327     set_bit(s->irr, vector_num);
328     if (trigger_mode)
329         set_bit(s->tmr, vector_num);
330     else
331         reset_bit(s->tmr, vector_num);
332     apic_update_irq(s);
333 }
334
335 static void apic_eoi(APICState *s)
336 {
337     int isrv;
338     isrv = get_highest_priority_int(s->isr);
339     if (isrv < 0)
340         return;
341     reset_bit(s->isr, isrv);
342     /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
343             set the remote IRR bit for level triggered interrupts. */
344     apic_update_irq(s);
345 }
346
347 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
348                                       uint8_t dest, uint8_t dest_mode)
349 {
350     APICState *apic_iter;
351     int i;
352
353     if (dest_mode == 0) {
354         if (dest == 0xff) {
355             memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
356         } else {
357             memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
358             set_bit(deliver_bitmask, dest);
359         }
360     } else {
361         /* XXX: cluster mode */
362         memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
363         for(i = 0; i < MAX_APICS; i++) {
364             apic_iter = local_apics[i];
365             if (apic_iter) {
366                 if (apic_iter->dest_mode == 0xf) {
367                     if (dest & apic_iter->log_dest)
368                         set_bit(deliver_bitmask, i);
369                 } else if (apic_iter->dest_mode == 0x0) {
370                     if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
371                         (dest & apic_iter->log_dest & 0x0f)) {
372                         set_bit(deliver_bitmask, i);
373                     }
374                 }
375             }
376         }
377     }
378 }
379
380
381 static void apic_init_ipi(APICState *s)
382 {
383     int i;
384
385     s->tpr = 0;
386     s->spurious_vec = 0xff;
387     s->log_dest = 0;
388     s->dest_mode = 0xf;
389     memset(s->isr, 0, sizeof(s->isr));
390     memset(s->tmr, 0, sizeof(s->tmr));
391     memset(s->irr, 0, sizeof(s->irr));
392     for(i = 0; i < APIC_LVT_NB; i++)
393         s->lvt[i] = 1 << 16; /* mask LVT */
394     s->esr = 0;
395     memset(s->icr, 0, sizeof(s->icr));
396     s->divide_conf = 0;
397     s->count_shift = 0;
398     s->initial_count = 0;
399     s->initial_count_load_time = 0;
400     s->next_time = 0;
401 }
402
403 /* send a SIPI message to the CPU to start it */
404 static void apic_startup(APICState *s, int vector_num)
405 {
406     CPUState *env = s->cpu_env;
407     if (!(env->hflags & HF_HALTED_MASK))
408         return;
409     env->eip = 0;
410     cpu_x86_load_seg_cache(env, R_CS, vector_num << 8, vector_num << 12,
411                            0xffff, 0);
412     env->hflags &= ~HF_HALTED_MASK;
413 }
414
415 static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
416                          uint8_t delivery_mode, uint8_t vector_num,
417                          uint8_t polarity, uint8_t trigger_mode)
418 {
419     uint32_t deliver_bitmask[MAX_APIC_WORDS];
420     int dest_shorthand = (s->icr[0] >> 18) & 3;
421     APICState *apic_iter;
422
423     switch (dest_shorthand) {
424     case 0:
425         apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
426         break;
427     case 1:
428         memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
429         set_bit(deliver_bitmask, s->id);
430         break;
431     case 2:
432         memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
433         break;
434     case 3:
435         memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
436         reset_bit(deliver_bitmask, s->id);
437         break;
438     }
439
440     switch (delivery_mode) {
441         case APIC_DM_INIT:
442             {
443                 int trig_mode = (s->icr[0] >> 15) & 1;
444                 int level = (s->icr[0] >> 14) & 1;
445                 if (level == 0 && trig_mode == 1) {
446                     foreach_apic(apic_iter, deliver_bitmask,
447                                  apic_iter->arb_id = apic_iter->id );
448                     return;
449                 }
450             }
451             break;
452
453         case APIC_DM_SIPI:
454             foreach_apic(apic_iter, deliver_bitmask,
455                          apic_startup(apic_iter, vector_num) );
456             return;
457     }
458
459     apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
460                      trigger_mode);
461 }
462
463 int apic_get_interrupt(CPUState *env)
464 {
465     APICState *s = env->apic_state;
466     int intno;
467
468     /* if the APIC is installed or enabled, we let the 8259 handle the
469        IRQs */
470     if (!s)
471         return -1;
472     if (!(s->spurious_vec & APIC_SV_ENABLE))
473         return -1;
474
475     /* XXX: spurious IRQ handling */
476     intno = get_highest_priority_int(s->irr);
477     if (intno < 0)
478         return -1;
479     if (s->tpr && intno <= s->tpr)
480         return s->spurious_vec & 0xff;
481     reset_bit(s->irr, intno);
482     set_bit(s->isr, intno);
483     apic_update_irq(s);
484     return intno;
485 }
486
487 int apic_accept_pic_intr(CPUState *env)
488 {
489     APICState *s = env->apic_state;
490     uint32_t lvt0;
491
492     if (!s)
493         return -1;
494
495     lvt0 = s->lvt[APIC_LVT_LINT0];
496
497     if (s->id == 0 &&
498         ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
499          ((lvt0 & APIC_LVT_MASKED) == 0 &&
500           ((lvt0 >> 8) & 0x7) == APIC_DM_EXTINT)))
501         return 1;
502
503     return 0;
504 }
505
506 static uint32_t apic_get_current_count(APICState *s)
507 {
508     int64_t d;
509     uint32_t val;
510     d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
511         s->count_shift;
512     if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
513         /* periodic */
514         val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
515     } else {
516         if (d >= s->initial_count)
517             val = 0;
518         else
519             val = s->initial_count - d;
520     }
521     return val;
522 }
523
524 static void apic_timer_update(APICState *s, int64_t current_time)
525 {
526     int64_t next_time, d;
527
528     if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
529         d = (current_time - s->initial_count_load_time) >>
530             s->count_shift;
531         if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
532             d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
533         } else {
534             if (d >= s->initial_count)
535                 goto no_timer;
536             d = (uint64_t)s->initial_count + 1;
537         }
538         next_time = s->initial_count_load_time + (d << s->count_shift);
539         qemu_mod_timer(s->timer, next_time);
540         s->next_time = next_time;
541     } else {
542     no_timer:
543         qemu_del_timer(s->timer);
544     }
545 }
546
547 static void apic_timer(void *opaque)
548 {
549     APICState *s = opaque;
550
551     if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
552         apic_set_irq(s, s->lvt[APIC_LVT_TIMER] & 0xff, APIC_TRIGGER_EDGE);
553     }
554     apic_timer_update(s, s->next_time);
555 }
556
557 static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
558 {
559     return 0;
560 }
561
562 static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
563 {
564     return 0;
565 }
566
567 static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
568 {
569 }
570
571 static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
572 {
573 }
574
575 static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
576 {
577     CPUState *env;
578     APICState *s;
579     uint32_t val;
580     int index;
581
582     env = cpu_single_env;
583     if (!env)
584         return 0;
585     s = env->apic_state;
586
587     index = (addr >> 4) & 0xff;
588     switch(index) {
589     case 0x02: /* id */
590         val = s->id << 24;
591         break;
592     case 0x03: /* version */
593         val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
594         break;
595     case 0x08:
596         val = s->tpr;
597         break;
598     case 0x09:
599         val = apic_get_arb_pri(s);
600         break;
601     case 0x0a:
602         /* ppr */
603         val = apic_get_ppr(s);
604         break;
605     case 0x0d:
606         val = s->log_dest << 24;
607         break;
608     case 0x0e:
609         val = s->dest_mode << 28;
610         break;
611     case 0x0f:
612         val = s->spurious_vec;
613         break;
614     case 0x10 ... 0x17:
615         val = s->isr[index & 7];
616         break;
617     case 0x18 ... 0x1f:
618         val = s->tmr[index & 7];
619         break;
620     case 0x20 ... 0x27:
621         val = s->irr[index & 7];
622         break;
623     case 0x28:
624         val = s->esr;
625         break;
626     case 0x30:
627     case 0x31:
628         val = s->icr[index & 1];
629         break;
630     case 0x32 ... 0x37:
631         val = s->lvt[index - 0x32];
632         break;
633     case 0x38:
634         val = s->initial_count;
635         break;
636     case 0x39:
637         val = apic_get_current_count(s);
638         break;
639     case 0x3e:
640         val = s->divide_conf;
641         break;
642     default:
643         s->esr |= ESR_ILLEGAL_ADDRESS;
644         val = 0;
645         break;
646     }
647 #ifdef DEBUG_APIC
648     printf("APIC read: %08x = %08x\n", (uint32_t)addr, val);
649 #endif
650     return val;
651 }
652
653 static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
654 {
655     CPUState *env;
656     APICState *s;
657     int index;
658
659     env = cpu_single_env;
660     if (!env)
661         return;
662     s = env->apic_state;
663
664 #ifdef DEBUG_APIC
665     printf("APIC write: %08x = %08x\n", (uint32_t)addr, val);
666 #endif
667
668     index = (addr >> 4) & 0xff;
669     switch(index) {
670     case 0x02:
671         s->id = (val >> 24);
672         break;
673     case 0x03:
674         break;
675     case 0x08:
676         s->tpr = val;
677         apic_update_irq(s);
678         break;
679     case 0x09:
680     case 0x0a:
681         break;
682     case 0x0b: /* EOI */
683         apic_eoi(s);
684         break;
685     case 0x0d:
686         s->log_dest = val >> 24;
687         break;
688     case 0x0e:
689         s->dest_mode = val >> 28;
690         break;
691     case 0x0f:
692         s->spurious_vec = val & 0x1ff;
693         apic_update_irq(s);
694         break;
695     case 0x10 ... 0x17:
696     case 0x18 ... 0x1f:
697     case 0x20 ... 0x27:
698     case 0x28:
699         break;
700     case 0x30:
701         s->icr[0] = val;
702         apic_deliver(s, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
703                      (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
704                      (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
705         break;
706     case 0x31:
707         s->icr[1] = val;
708         break;
709     case 0x32 ... 0x37:
710         {
711             int n = index - 0x32;
712             s->lvt[n] = val;
713             if (n == APIC_LVT_TIMER)
714                 apic_timer_update(s, qemu_get_clock(vm_clock));
715         }
716         break;
717     case 0x38:
718         s->initial_count = val;
719         s->initial_count_load_time = qemu_get_clock(vm_clock);
720         apic_timer_update(s, s->initial_count_load_time);
721         break;
722     case 0x39:
723         break;
724     case 0x3e:
725         {
726             int v;
727             s->divide_conf = val & 0xb;
728             v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
729             s->count_shift = (v + 1) & 7;
730         }
731         break;
732     default:
733         s->esr |= ESR_ILLEGAL_ADDRESS;
734         break;
735     }
736 }
737
738 static void apic_save(QEMUFile *f, void *opaque)
739 {
740     APICState *s = opaque;
741     int i;
742
743     qemu_put_be32s(f, &s->apicbase);
744     qemu_put_8s(f, &s->id);
745     qemu_put_8s(f, &s->arb_id);
746     qemu_put_8s(f, &s->tpr);
747     qemu_put_be32s(f, &s->spurious_vec);
748     qemu_put_8s(f, &s->log_dest);
749     qemu_put_8s(f, &s->dest_mode);
750     for (i = 0; i < 8; i++) {
751         qemu_put_be32s(f, &s->isr[i]);
752         qemu_put_be32s(f, &s->tmr[i]);
753         qemu_put_be32s(f, &s->irr[i]);
754     }
755     for (i = 0; i < APIC_LVT_NB; i++) {
756         qemu_put_be32s(f, &s->lvt[i]);
757     }
758     qemu_put_be32s(f, &s->esr);
759     qemu_put_be32s(f, &s->icr[0]);
760     qemu_put_be32s(f, &s->icr[1]);
761     qemu_put_be32s(f, &s->divide_conf);
762     qemu_put_be32s(f, &s->count_shift);
763     qemu_put_be32s(f, &s->initial_count);
764     qemu_put_be64s(f, &s->initial_count_load_time);
765     qemu_put_be64s(f, &s->next_time);
766
767     qemu_put_timer(f, s->timer);
768 }
769
770 static int apic_load(QEMUFile *f, void *opaque, int version_id)
771 {
772     APICState *s = opaque;
773     int i;
774
775     if (version_id > 2)
776         return -EINVAL;
777
778     /* XXX: what if the base changes? (registered memory regions) */
779     qemu_get_be32s(f, &s->apicbase);
780     qemu_get_8s(f, &s->id);
781     qemu_get_8s(f, &s->arb_id);
782     qemu_get_8s(f, &s->tpr);
783     qemu_get_be32s(f, &s->spurious_vec);
784     qemu_get_8s(f, &s->log_dest);
785     qemu_get_8s(f, &s->dest_mode);
786     for (i = 0; i < 8; i++) {
787         qemu_get_be32s(f, &s->isr[i]);
788         qemu_get_be32s(f, &s->tmr[i]);
789         qemu_get_be32s(f, &s->irr[i]);
790     }
791     for (i = 0; i < APIC_LVT_NB; i++) {
792         qemu_get_be32s(f, &s->lvt[i]);
793     }
794     qemu_get_be32s(f, &s->esr);
795     qemu_get_be32s(f, &s->icr[0]);
796     qemu_get_be32s(f, &s->icr[1]);
797     qemu_get_be32s(f, &s->divide_conf);
798     qemu_get_be32s(f, &s->count_shift);
799     qemu_get_be32s(f, &s->initial_count);
800     qemu_get_be64s(f, &s->initial_count_load_time);
801     qemu_get_be64s(f, &s->next_time);
802
803     if (version_id >= 2)
804         qemu_get_timer(f, s->timer);
805     return 0;
806 }
807
808 static void apic_reset(void *opaque)
809 {
810     APICState *s = opaque;
811     apic_init_ipi(s);
812
813     /*
814      * LINT0 delivery mode is set to ExtInt at initialization time
815      * typically by BIOS, so PIC interrupt can be delivered to the
816      * processor when local APIC is enabled.
817      */
818     s->lvt[APIC_LVT_LINT0] = 0x700;
819 }
820
821 static CPUReadMemoryFunc *apic_mem_read[3] = {
822     apic_mem_readb,
823     apic_mem_readw,
824     apic_mem_readl,
825 };
826
827 static CPUWriteMemoryFunc *apic_mem_write[3] = {
828     apic_mem_writeb,
829     apic_mem_writew,
830     apic_mem_writel,
831 };
832
833 int apic_init(CPUState *env)
834 {
835     APICState *s;
836
837     if (last_apic_id >= MAX_APICS)
838         return -1;
839     s = qemu_mallocz(sizeof(APICState));
840     if (!s)
841         return -1;
842     env->apic_state = s;
843     apic_init_ipi(s);
844     s->id = last_apic_id++;
845     env->cpuid_apic_id = s->id;
846     s->cpu_env = env;
847     s->apicbase = 0xfee00000 |
848         (s->id ? 0 : MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
849
850     /*
851      * LINT0 delivery mode is set to ExtInt at initialization time
852      * typically by BIOS, so PIC interrupt can be delivered to the
853      * processor when local APIC is enabled.
854      */
855     s->lvt[APIC_LVT_LINT0] = 0x700;
856
857     /* XXX: mapping more APICs at the same memory location */
858     if (apic_io_memory == 0) {
859         /* NOTE: the APIC is directly connected to the CPU - it is not
860            on the global memory bus. */
861         apic_io_memory = cpu_register_io_memory(0, apic_mem_read,
862                                                 apic_mem_write, NULL);
863         cpu_register_physical_memory(s->apicbase & ~0xfff, 0x1000,
864                                      apic_io_memory);
865     }
866     s->timer = qemu_new_timer(vm_clock, apic_timer, s);
867
868     register_savevm("apic", s->id, 2, apic_save, apic_load, s);
869     qemu_register_reset(apic_reset, s);
870
871     local_apics[s->id] = s;
872     return 0;
873 }
874
875 static void ioapic_service(IOAPICState *s)
876 {
877     uint8_t i;
878     uint8_t trig_mode;
879     uint8_t vector;
880     uint8_t delivery_mode;
881     uint32_t mask;
882     uint64_t entry;
883     uint8_t dest;
884     uint8_t dest_mode;
885     uint8_t polarity;
886     uint32_t deliver_bitmask[MAX_APIC_WORDS];
887
888     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
889         mask = 1 << i;
890         if (s->irr & mask) {
891             entry = s->ioredtbl[i];
892             if (!(entry & APIC_LVT_MASKED)) {
893                 trig_mode = ((entry >> 15) & 1);
894                 dest = entry >> 56;
895                 dest_mode = (entry >> 11) & 1;
896                 delivery_mode = (entry >> 8) & 7;
897                 polarity = (entry >> 13) & 1;
898                 if (trig_mode == APIC_TRIGGER_EDGE)
899                     s->irr &= ~mask;
900                 if (delivery_mode == APIC_DM_EXTINT)
901                     vector = pic_read_irq(isa_pic);
902                 else
903                     vector = entry & 0xff;
904
905                 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
906                 apic_bus_deliver(deliver_bitmask, delivery_mode,
907                                  vector, polarity, trig_mode);
908             }
909         }
910     }
911 }
912
913 void ioapic_set_irq(void *opaque, int vector, int level)
914 {
915     IOAPICState *s = opaque;
916
917     if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
918         uint32_t mask = 1 << vector;
919         uint64_t entry = s->ioredtbl[vector];
920
921         if ((entry >> 15) & 1) {
922             /* level triggered */
923             if (level) {
924                 s->irr |= mask;
925                 ioapic_service(s);
926             } else {
927                 s->irr &= ~mask;
928             }
929         } else {
930             /* edge triggered */
931             if (level) {
932                 s->irr |= mask;
933                 ioapic_service(s);
934             }
935         }
936     }
937 }
938
939 static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr)
940 {
941     IOAPICState *s = opaque;
942     int index;
943     uint32_t val = 0;
944
945     addr &= 0xff;
946     if (addr == 0x00) {
947         val = s->ioregsel;
948     } else if (addr == 0x10) {
949         switch (s->ioregsel) {
950             case 0x00:
951                 val = s->id << 24;
952                 break;
953             case 0x01:
954                 val = 0x11 | ((IOAPIC_NUM_PINS - 1) << 16); /* version 0x11 */
955                 break;
956             case 0x02:
957                 val = 0;
958                 break;
959             default:
960                 index = (s->ioregsel - 0x10) >> 1;
961                 if (index >= 0 && index < IOAPIC_NUM_PINS) {
962                     if (s->ioregsel & 1)
963                         val = s->ioredtbl[index] >> 32;
964                     else
965                         val = s->ioredtbl[index] & 0xffffffff;
966                 }
967         }
968 #ifdef DEBUG_IOAPIC
969         printf("I/O APIC read: %08x = %08x\n", s->ioregsel, val);
970 #endif
971     }
972     return val;
973 }
974
975 static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
976 {
977     IOAPICState *s = opaque;
978     int index;
979
980     addr &= 0xff;
981     if (addr == 0x00)  {
982         s->ioregsel = val;
983         return;
984     } else if (addr == 0x10) {
985 #ifdef DEBUG_IOAPIC
986         printf("I/O APIC write: %08x = %08x\n", s->ioregsel, val);
987 #endif
988         switch (s->ioregsel) {
989             case 0x00:
990                 s->id = (val >> 24) & 0xff;
991                 return;
992             case 0x01:
993             case 0x02:
994                 return;
995             default:
996                 index = (s->ioregsel - 0x10) >> 1;
997                 if (index >= 0 && index < IOAPIC_NUM_PINS) {
998                     if (s->ioregsel & 1) {
999                         s->ioredtbl[index] &= 0xffffffff;
1000                         s->ioredtbl[index] |= (uint64_t)val << 32;
1001                     } else {
1002                         s->ioredtbl[index] &= ~0xffffffffULL;
1003                         s->ioredtbl[index] |= val;
1004                     }
1005                     ioapic_service(s);
1006                 }
1007         }
1008     }
1009 }
1010
1011 static void ioapic_save(QEMUFile *f, void *opaque)
1012 {
1013     IOAPICState *s = opaque;
1014     int i;
1015
1016     qemu_put_8s(f, &s->id);
1017     qemu_put_8s(f, &s->ioregsel);
1018     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1019         qemu_put_be64s(f, &s->ioredtbl[i]);
1020     }
1021 }
1022
1023 static int ioapic_load(QEMUFile *f, void *opaque, int version_id)
1024 {
1025     IOAPICState *s = opaque;
1026     int i;
1027
1028     if (version_id != 1)
1029         return -EINVAL;
1030
1031     qemu_get_8s(f, &s->id);
1032     qemu_get_8s(f, &s->ioregsel);
1033     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1034         qemu_get_be64s(f, &s->ioredtbl[i]);
1035     }
1036     return 0;
1037 }
1038
1039 static void ioapic_reset(void *opaque)
1040 {
1041     IOAPICState *s = opaque;
1042     int i;
1043
1044     memset(s, 0, sizeof(*s));
1045     for(i = 0; i < IOAPIC_NUM_PINS; i++)
1046         s->ioredtbl[i] = 1 << 16; /* mask LVT */
1047 }
1048
1049 static CPUReadMemoryFunc *ioapic_mem_read[3] = {
1050     ioapic_mem_readl,
1051     ioapic_mem_readl,
1052     ioapic_mem_readl,
1053 };
1054
1055 static CPUWriteMemoryFunc *ioapic_mem_write[3] = {
1056     ioapic_mem_writel,
1057     ioapic_mem_writel,
1058     ioapic_mem_writel,
1059 };
1060
1061 IOAPICState *ioapic_init(void)
1062 {
1063     IOAPICState *s;
1064     int io_memory;
1065
1066     s = qemu_mallocz(sizeof(IOAPICState));
1067     if (!s)
1068         return NULL;
1069     ioapic_reset(s);
1070     s->id = last_apic_id++;
1071
1072     io_memory = cpu_register_io_memory(0, ioapic_mem_read,
1073                                        ioapic_mem_write, s);
1074     cpu_register_physical_memory(0xfec00000, 0x1000, io_memory);
1075
1076     register_savevm("ioapic", 0, 1, ioapic_save, ioapic_load, s);
1077     qemu_register_reset(ioapic_reset, s);
1078
1079     return s;
1080 }