qapi/error: Check format string argument in error_*prepend()
[qemu/armbru.git] / hw / intc / apic.c
blob38aabd60cda46cfe6be930dec710d0180271a7e8
1 /*
2 * APIC support
4 * Copyright (c) 2004-2005 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>
19 #include "qemu/osdep.h"
20 #include "cpu.h"
21 #include "qemu/thread.h"
22 #include "hw/i386/apic_internal.h"
23 #include "hw/i386/apic.h"
24 #include "hw/i386/ioapic.h"
25 #include "hw/intc/i8259.h"
26 #include "hw/pci/msi.h"
27 #include "qemu/host-utils.h"
28 #include "trace.h"
29 #include "hw/i386/apic-msidef.h"
30 #include "qapi/error.h"
32 #define MAX_APICS 255
33 #define MAX_APIC_WORDS 8
35 #define SYNC_FROM_VAPIC 0x1
36 #define SYNC_TO_VAPIC 0x2
37 #define SYNC_ISR_IRR_TO_VAPIC 0x4
39 static APICCommonState *local_apics[MAX_APICS + 1];
41 #define TYPE_APIC "apic"
42 #define APIC(obj) \
43 OBJECT_CHECK(APICCommonState, (obj), TYPE_APIC)
45 static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode);
46 static void apic_update_irq(APICCommonState *s);
47 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
48 uint8_t dest, uint8_t dest_mode);
50 /* Find first bit starting from msb */
51 static int apic_fls_bit(uint32_t value)
53 return 31 - clz32(value);
56 /* Find first bit starting from lsb */
57 static int apic_ffs_bit(uint32_t value)
59 return ctz32(value);
62 static inline void apic_reset_bit(uint32_t *tab, int index)
64 int i, mask;
65 i = index >> 5;
66 mask = 1 << (index & 0x1f);
67 tab[i] &= ~mask;
70 /* return -1 if no bit is set */
71 static int get_highest_priority_int(uint32_t *tab)
73 int i;
74 for (i = 7; i >= 0; i--) {
75 if (tab[i] != 0) {
76 return i * 32 + apic_fls_bit(tab[i]);
79 return -1;
82 static void apic_sync_vapic(APICCommonState *s, int sync_type)
84 VAPICState vapic_state;
85 size_t length;
86 off_t start;
87 int vector;
89 if (!s->vapic_paddr) {
90 return;
92 if (sync_type & SYNC_FROM_VAPIC) {
93 cpu_physical_memory_read(s->vapic_paddr, &vapic_state,
94 sizeof(vapic_state));
95 s->tpr = vapic_state.tpr;
97 if (sync_type & (SYNC_TO_VAPIC | SYNC_ISR_IRR_TO_VAPIC)) {
98 start = offsetof(VAPICState, isr);
99 length = offsetof(VAPICState, enabled) - offsetof(VAPICState, isr);
101 if (sync_type & SYNC_TO_VAPIC) {
102 assert(qemu_cpu_is_self(CPU(s->cpu)));
104 vapic_state.tpr = s->tpr;
105 vapic_state.enabled = 1;
106 start = 0;
107 length = sizeof(VAPICState);
110 vector = get_highest_priority_int(s->isr);
111 if (vector < 0) {
112 vector = 0;
114 vapic_state.isr = vector & 0xf0;
116 vapic_state.zero = 0;
118 vector = get_highest_priority_int(s->irr);
119 if (vector < 0) {
120 vector = 0;
122 vapic_state.irr = vector & 0xff;
124 address_space_write_rom(&address_space_memory,
125 s->vapic_paddr + start,
126 MEMTXATTRS_UNSPECIFIED,
127 ((void *)&vapic_state) + start, length);
131 static void apic_vapic_base_update(APICCommonState *s)
133 apic_sync_vapic(s, SYNC_TO_VAPIC);
136 static void apic_local_deliver(APICCommonState *s, int vector)
138 uint32_t lvt = s->lvt[vector];
139 int trigger_mode;
141 trace_apic_local_deliver(vector, (lvt >> 8) & 7);
143 if (lvt & APIC_LVT_MASKED)
144 return;
146 switch ((lvt >> 8) & 7) {
147 case APIC_DM_SMI:
148 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SMI);
149 break;
151 case APIC_DM_NMI:
152 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_NMI);
153 break;
155 case APIC_DM_EXTINT:
156 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_HARD);
157 break;
159 case APIC_DM_FIXED:
160 trigger_mode = APIC_TRIGGER_EDGE;
161 if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
162 (lvt & APIC_LVT_LEVEL_TRIGGER))
163 trigger_mode = APIC_TRIGGER_LEVEL;
164 apic_set_irq(s, lvt & 0xff, trigger_mode);
168 void apic_deliver_pic_intr(DeviceState *dev, int level)
170 APICCommonState *s = APIC(dev);
172 if (level) {
173 apic_local_deliver(s, APIC_LVT_LINT0);
174 } else {
175 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
177 switch ((lvt >> 8) & 7) {
178 case APIC_DM_FIXED:
179 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
180 break;
181 apic_reset_bit(s->irr, lvt & 0xff);
182 /* fall through */
183 case APIC_DM_EXTINT:
184 apic_update_irq(s);
185 break;
190 static void apic_external_nmi(APICCommonState *s)
192 apic_local_deliver(s, APIC_LVT_LINT1);
195 #define foreach_apic(apic, deliver_bitmask, code) \
197 int __i, __j;\
198 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
199 uint32_t __mask = deliver_bitmask[__i];\
200 if (__mask) {\
201 for(__j = 0; __j < 32; __j++) {\
202 if (__mask & (1U << __j)) {\
203 apic = local_apics[__i * 32 + __j];\
204 if (apic) {\
205 code;\
213 static void apic_bus_deliver(const uint32_t *deliver_bitmask,
214 uint8_t delivery_mode, uint8_t vector_num,
215 uint8_t trigger_mode)
217 APICCommonState *apic_iter;
219 switch (delivery_mode) {
220 case APIC_DM_LOWPRI:
221 /* XXX: search for focus processor, arbitration */
223 int i, d;
224 d = -1;
225 for(i = 0; i < MAX_APIC_WORDS; i++) {
226 if (deliver_bitmask[i]) {
227 d = i * 32 + apic_ffs_bit(deliver_bitmask[i]);
228 break;
231 if (d >= 0) {
232 apic_iter = local_apics[d];
233 if (apic_iter) {
234 apic_set_irq(apic_iter, vector_num, trigger_mode);
238 return;
240 case APIC_DM_FIXED:
241 break;
243 case APIC_DM_SMI:
244 foreach_apic(apic_iter, deliver_bitmask,
245 cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_SMI)
247 return;
249 case APIC_DM_NMI:
250 foreach_apic(apic_iter, deliver_bitmask,
251 cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_NMI)
253 return;
255 case APIC_DM_INIT:
256 /* normal INIT IPI sent to processors */
257 foreach_apic(apic_iter, deliver_bitmask,
258 cpu_interrupt(CPU(apic_iter->cpu),
259 CPU_INTERRUPT_INIT)
261 return;
263 case APIC_DM_EXTINT:
264 /* handled in I/O APIC code */
265 break;
267 default:
268 return;
271 foreach_apic(apic_iter, deliver_bitmask,
272 apic_set_irq(apic_iter, vector_num, trigger_mode) );
275 void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode,
276 uint8_t vector_num, uint8_t trigger_mode)
278 uint32_t deliver_bitmask[MAX_APIC_WORDS];
280 trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
281 trigger_mode);
283 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
284 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
287 static void apic_set_base(APICCommonState *s, uint64_t val)
289 s->apicbase = (val & 0xfffff000) |
290 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
291 /* if disabled, cannot be enabled again */
292 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
293 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
294 cpu_clear_apic_feature(&s->cpu->env);
295 s->spurious_vec &= ~APIC_SV_ENABLE;
299 static void apic_set_tpr(APICCommonState *s, uint8_t val)
301 /* Updates from cr8 are ignored while the VAPIC is active */
302 if (!s->vapic_paddr) {
303 s->tpr = val << 4;
304 apic_update_irq(s);
308 int apic_get_highest_priority_irr(DeviceState *dev)
310 APICCommonState *s;
312 if (!dev) {
313 /* no interrupts */
314 return -1;
316 s = APIC_COMMON(dev);
317 return get_highest_priority_int(s->irr);
320 static uint8_t apic_get_tpr(APICCommonState *s)
322 apic_sync_vapic(s, SYNC_FROM_VAPIC);
323 return s->tpr >> 4;
326 int apic_get_ppr(APICCommonState *s)
328 int tpr, isrv, ppr;
330 tpr = (s->tpr >> 4);
331 isrv = get_highest_priority_int(s->isr);
332 if (isrv < 0)
333 isrv = 0;
334 isrv >>= 4;
335 if (tpr >= isrv)
336 ppr = s->tpr;
337 else
338 ppr = isrv << 4;
339 return ppr;
342 static int apic_get_arb_pri(APICCommonState *s)
344 /* XXX: arbitration */
345 return 0;
350 * <0 - low prio interrupt,
351 * 0 - no interrupt,
352 * >0 - interrupt number
354 static int apic_irq_pending(APICCommonState *s)
356 int irrv, ppr;
358 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
359 return 0;
362 irrv = get_highest_priority_int(s->irr);
363 if (irrv < 0) {
364 return 0;
366 ppr = apic_get_ppr(s);
367 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
368 return -1;
371 return irrv;
374 /* signal the CPU if an irq is pending */
375 static void apic_update_irq(APICCommonState *s)
377 CPUState *cpu;
378 DeviceState *dev = (DeviceState *)s;
380 cpu = CPU(s->cpu);
381 if (!qemu_cpu_is_self(cpu)) {
382 cpu_interrupt(cpu, CPU_INTERRUPT_POLL);
383 } else if (apic_irq_pending(s) > 0) {
384 cpu_interrupt(cpu, CPU_INTERRUPT_HARD);
385 } else if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
386 cpu_reset_interrupt(cpu, CPU_INTERRUPT_HARD);
390 void apic_poll_irq(DeviceState *dev)
392 APICCommonState *s = APIC(dev);
394 apic_sync_vapic(s, SYNC_FROM_VAPIC);
395 apic_update_irq(s);
398 static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
400 apic_report_irq_delivered(!apic_get_bit(s->irr, vector_num));
402 apic_set_bit(s->irr, vector_num);
403 if (trigger_mode)
404 apic_set_bit(s->tmr, vector_num);
405 else
406 apic_reset_bit(s->tmr, vector_num);
407 if (s->vapic_paddr) {
408 apic_sync_vapic(s, SYNC_ISR_IRR_TO_VAPIC);
410 * The vcpu thread needs to see the new IRR before we pull its current
411 * TPR value. That way, if we miss a lowering of the TRP, the guest
412 * has the chance to notice the new IRR and poll for IRQs on its own.
414 smp_wmb();
415 apic_sync_vapic(s, SYNC_FROM_VAPIC);
417 apic_update_irq(s);
420 static void apic_eoi(APICCommonState *s)
422 int isrv;
423 isrv = get_highest_priority_int(s->isr);
424 if (isrv < 0)
425 return;
426 apic_reset_bit(s->isr, isrv);
427 if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && apic_get_bit(s->tmr, isrv)) {
428 ioapic_eoi_broadcast(isrv);
430 apic_sync_vapic(s, SYNC_FROM_VAPIC | SYNC_TO_VAPIC);
431 apic_update_irq(s);
434 static int apic_find_dest(uint8_t dest)
436 APICCommonState *apic = local_apics[dest];
437 int i;
439 if (apic && apic->id == dest)
440 return dest; /* shortcut in case apic->id == local_apics[dest]->id */
442 for (i = 0; i < MAX_APICS; i++) {
443 apic = local_apics[i];
444 if (apic && apic->id == dest)
445 return i;
446 if (!apic)
447 break;
450 return -1;
453 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
454 uint8_t dest, uint8_t dest_mode)
456 APICCommonState *apic_iter;
457 int i;
459 if (dest_mode == 0) {
460 if (dest == 0xff) {
461 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
462 } else {
463 int idx = apic_find_dest(dest);
464 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
465 if (idx >= 0)
466 apic_set_bit(deliver_bitmask, idx);
468 } else {
469 /* XXX: cluster mode */
470 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
471 for(i = 0; i < MAX_APICS; i++) {
472 apic_iter = local_apics[i];
473 if (apic_iter) {
474 if (apic_iter->dest_mode == 0xf) {
475 if (dest & apic_iter->log_dest)
476 apic_set_bit(deliver_bitmask, i);
477 } else if (apic_iter->dest_mode == 0x0) {
478 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
479 (dest & apic_iter->log_dest & 0x0f)) {
480 apic_set_bit(deliver_bitmask, i);
483 } else {
484 break;
490 static void apic_startup(APICCommonState *s, int vector_num)
492 s->sipi_vector = vector_num;
493 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
496 void apic_sipi(DeviceState *dev)
498 APICCommonState *s = APIC(dev);
500 cpu_reset_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
502 if (!s->wait_for_sipi)
503 return;
504 cpu_x86_load_seg_cache_sipi(s->cpu, s->sipi_vector);
505 s->wait_for_sipi = 0;
508 static void apic_deliver(DeviceState *dev, uint8_t dest, uint8_t dest_mode,
509 uint8_t delivery_mode, uint8_t vector_num,
510 uint8_t trigger_mode)
512 APICCommonState *s = APIC(dev);
513 uint32_t deliver_bitmask[MAX_APIC_WORDS];
514 int dest_shorthand = (s->icr[0] >> 18) & 3;
515 APICCommonState *apic_iter;
517 switch (dest_shorthand) {
518 case 0:
519 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
520 break;
521 case 1:
522 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
523 apic_set_bit(deliver_bitmask, s->id);
524 break;
525 case 2:
526 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
527 break;
528 case 3:
529 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
530 apic_reset_bit(deliver_bitmask, s->id);
531 break;
534 switch (delivery_mode) {
535 case APIC_DM_INIT:
537 int trig_mode = (s->icr[0] >> 15) & 1;
538 int level = (s->icr[0] >> 14) & 1;
539 if (level == 0 && trig_mode == 1) {
540 foreach_apic(apic_iter, deliver_bitmask,
541 apic_iter->arb_id = apic_iter->id );
542 return;
545 break;
547 case APIC_DM_SIPI:
548 foreach_apic(apic_iter, deliver_bitmask,
549 apic_startup(apic_iter, vector_num) );
550 return;
553 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
556 static bool apic_check_pic(APICCommonState *s)
558 DeviceState *dev = (DeviceState *)s;
560 if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
561 return false;
563 apic_deliver_pic_intr(dev, 1);
564 return true;
567 int apic_get_interrupt(DeviceState *dev)
569 APICCommonState *s = APIC(dev);
570 int intno;
572 /* if the APIC is installed or enabled, we let the 8259 handle the
573 IRQs */
574 if (!s)
575 return -1;
576 if (!(s->spurious_vec & APIC_SV_ENABLE))
577 return -1;
579 apic_sync_vapic(s, SYNC_FROM_VAPIC);
580 intno = apic_irq_pending(s);
582 /* if there is an interrupt from the 8259, let the caller handle
583 * that first since ExtINT interrupts ignore the priority.
585 if (intno == 0 || apic_check_pic(s)) {
586 apic_sync_vapic(s, SYNC_TO_VAPIC);
587 return -1;
588 } else if (intno < 0) {
589 apic_sync_vapic(s, SYNC_TO_VAPIC);
590 return s->spurious_vec & 0xff;
592 apic_reset_bit(s->irr, intno);
593 apic_set_bit(s->isr, intno);
594 apic_sync_vapic(s, SYNC_TO_VAPIC);
596 apic_update_irq(s);
598 return intno;
601 int apic_accept_pic_intr(DeviceState *dev)
603 APICCommonState *s = APIC(dev);
604 uint32_t lvt0;
606 if (!s)
607 return -1;
609 lvt0 = s->lvt[APIC_LVT_LINT0];
611 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
612 (lvt0 & APIC_LVT_MASKED) == 0)
613 return isa_pic != NULL;
615 return 0;
618 static void apic_timer_update(APICCommonState *s, int64_t current_time)
620 if (apic_next_timer(s, current_time)) {
621 timer_mod(s->timer, s->next_time);
622 } else {
623 timer_del(s->timer);
627 static void apic_timer(void *opaque)
629 APICCommonState *s = opaque;
631 apic_local_deliver(s, APIC_LVT_TIMER);
632 apic_timer_update(s, s->next_time);
635 static uint64_t apic_mem_read(void *opaque, hwaddr addr, unsigned size)
637 DeviceState *dev;
638 APICCommonState *s;
639 uint32_t val;
640 int index;
642 if (size < 4) {
643 return 0;
646 dev = cpu_get_current_apic();
647 if (!dev) {
648 return 0;
650 s = APIC(dev);
652 index = (addr >> 4) & 0xff;
653 switch(index) {
654 case 0x02: /* id */
655 val = s->id << 24;
656 break;
657 case 0x03: /* version */
658 val = s->version | ((APIC_LVT_NB - 1) << 16);
659 break;
660 case 0x08:
661 apic_sync_vapic(s, SYNC_FROM_VAPIC);
662 if (apic_report_tpr_access) {
663 cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_READ);
665 val = s->tpr;
666 break;
667 case 0x09:
668 val = apic_get_arb_pri(s);
669 break;
670 case 0x0a:
671 /* ppr */
672 val = apic_get_ppr(s);
673 break;
674 case 0x0b:
675 val = 0;
676 break;
677 case 0x0d:
678 val = s->log_dest << 24;
679 break;
680 case 0x0e:
681 val = (s->dest_mode << 28) | 0xfffffff;
682 break;
683 case 0x0f:
684 val = s->spurious_vec;
685 break;
686 case 0x10 ... 0x17:
687 val = s->isr[index & 7];
688 break;
689 case 0x18 ... 0x1f:
690 val = s->tmr[index & 7];
691 break;
692 case 0x20 ... 0x27:
693 val = s->irr[index & 7];
694 break;
695 case 0x28:
696 val = s->esr;
697 break;
698 case 0x30:
699 case 0x31:
700 val = s->icr[index & 1];
701 break;
702 case 0x32 ... 0x37:
703 val = s->lvt[index - 0x32];
704 break;
705 case 0x38:
706 val = s->initial_count;
707 break;
708 case 0x39:
709 val = apic_get_current_count(s);
710 break;
711 case 0x3e:
712 val = s->divide_conf;
713 break;
714 default:
715 s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
716 val = 0;
717 break;
719 trace_apic_mem_readl(addr, val);
720 return val;
723 static void apic_send_msi(MSIMessage *msi)
725 uint64_t addr = msi->address;
726 uint32_t data = msi->data;
727 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
728 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
729 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
730 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
731 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
732 /* XXX: Ignore redirection hint. */
733 apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
736 static void apic_mem_write(void *opaque, hwaddr addr, uint64_t val,
737 unsigned size)
739 DeviceState *dev;
740 APICCommonState *s;
741 int index = (addr >> 4) & 0xff;
743 if (size < 4) {
744 return;
747 if (addr > 0xfff || !index) {
748 /* MSI and MMIO APIC are at the same memory location,
749 * but actually not on the global bus: MSI is on PCI bus
750 * APIC is connected directly to the CPU.
751 * Mapping them on the global bus happens to work because
752 * MSI registers are reserved in APIC MMIO and vice versa. */
753 MSIMessage msi = { .address = addr, .data = val };
754 apic_send_msi(&msi);
755 return;
758 dev = cpu_get_current_apic();
759 if (!dev) {
760 return;
762 s = APIC(dev);
764 trace_apic_mem_writel(addr, val);
766 switch(index) {
767 case 0x02:
768 s->id = (val >> 24);
769 break;
770 case 0x03:
771 break;
772 case 0x08:
773 if (apic_report_tpr_access) {
774 cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_WRITE);
776 s->tpr = val;
777 apic_sync_vapic(s, SYNC_TO_VAPIC);
778 apic_update_irq(s);
779 break;
780 case 0x09:
781 case 0x0a:
782 break;
783 case 0x0b: /* EOI */
784 apic_eoi(s);
785 break;
786 case 0x0d:
787 s->log_dest = val >> 24;
788 break;
789 case 0x0e:
790 s->dest_mode = val >> 28;
791 break;
792 case 0x0f:
793 s->spurious_vec = val & 0x1ff;
794 apic_update_irq(s);
795 break;
796 case 0x10 ... 0x17:
797 case 0x18 ... 0x1f:
798 case 0x20 ... 0x27:
799 case 0x28:
800 break;
801 case 0x30:
802 s->icr[0] = val;
803 apic_deliver(dev, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
804 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
805 (s->icr[0] >> 15) & 1);
806 break;
807 case 0x31:
808 s->icr[1] = val;
809 break;
810 case 0x32 ... 0x37:
812 int n = index - 0x32;
813 s->lvt[n] = val;
814 if (n == APIC_LVT_TIMER) {
815 apic_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
816 } else if (n == APIC_LVT_LINT0 && apic_check_pic(s)) {
817 apic_update_irq(s);
820 break;
821 case 0x38:
822 s->initial_count = val;
823 s->initial_count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
824 apic_timer_update(s, s->initial_count_load_time);
825 break;
826 case 0x39:
827 break;
828 case 0x3e:
830 int v;
831 s->divide_conf = val & 0xb;
832 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
833 s->count_shift = (v + 1) & 7;
835 break;
836 default:
837 s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
838 break;
842 static void apic_pre_save(APICCommonState *s)
844 apic_sync_vapic(s, SYNC_FROM_VAPIC);
847 static void apic_post_load(APICCommonState *s)
849 if (s->timer_expiry != -1) {
850 timer_mod(s->timer, s->timer_expiry);
851 } else {
852 timer_del(s->timer);
856 static const MemoryRegionOps apic_io_ops = {
857 .read = apic_mem_read,
858 .write = apic_mem_write,
859 .impl.min_access_size = 1,
860 .impl.max_access_size = 4,
861 .valid.min_access_size = 1,
862 .valid.max_access_size = 4,
863 .endianness = DEVICE_NATIVE_ENDIAN,
866 static void apic_realize(DeviceState *dev, Error **errp)
868 APICCommonState *s = APIC(dev);
870 if (s->id >= MAX_APICS) {
871 error_setg(errp, "%s initialization failed. APIC ID %d is invalid",
872 object_get_typename(OBJECT(dev)), s->id);
873 return;
876 memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
877 APIC_SPACE_SIZE);
879 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s);
880 local_apics[s->id] = s;
882 msi_nonbroken = true;
885 static void apic_unrealize(DeviceState *dev)
887 APICCommonState *s = APIC(dev);
889 timer_del(s->timer);
890 timer_free(s->timer);
891 local_apics[s->id] = NULL;
894 static void apic_class_init(ObjectClass *klass, void *data)
896 APICCommonClass *k = APIC_COMMON_CLASS(klass);
898 k->realize = apic_realize;
899 k->unrealize = apic_unrealize;
900 k->set_base = apic_set_base;
901 k->set_tpr = apic_set_tpr;
902 k->get_tpr = apic_get_tpr;
903 k->vapic_base_update = apic_vapic_base_update;
904 k->external_nmi = apic_external_nmi;
905 k->pre_save = apic_pre_save;
906 k->post_load = apic_post_load;
907 k->send_msi = apic_send_msi;
910 static const TypeInfo apic_info = {
911 .name = TYPE_APIC,
912 .instance_size = sizeof(APICCommonState),
913 .parent = TYPE_APIC_COMMON,
914 .class_init = apic_class_init,
917 static void apic_register_types(void)
919 type_register_static(&apic_info);
922 type_init(apic_register_types)