target/mips: Use generic cpu_list()
[qemu/ar7.git] / hw / intc / apic.c
blobac3d47d2318f3db374e214d2a3085f3e7d89650c
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.1 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 "qemu/thread.h"
21 #include "qemu/error-report.h"
22 #include "hw/i386/apic_internal.h"
23 #include "hw/i386/apic.h"
24 #include "hw/intc/ioapic.h"
25 #include "hw/intc/i8259.h"
26 #include "hw/intc/kvm_irqcount.h"
27 #include "hw/pci/msi.h"
28 #include "qemu/host-utils.h"
29 #include "sysemu/kvm.h"
30 #include "trace.h"
31 #include "hw/i386/apic-msidef.h"
32 #include "qapi/error.h"
33 #include "qom/object.h"
35 #define MAX_APICS 255
36 #define MAX_APIC_WORDS 8
38 #define SYNC_FROM_VAPIC 0x1
39 #define SYNC_TO_VAPIC 0x2
40 #define SYNC_ISR_IRR_TO_VAPIC 0x4
42 static APICCommonState *local_apics[MAX_APICS + 1];
44 #define TYPE_APIC "apic"
45 /*This is reusing the APICCommonState typedef from APIC_COMMON */
46 DECLARE_INSTANCE_CHECKER(APICCommonState, APIC,
47 TYPE_APIC)
49 static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode);
50 static void apic_update_irq(APICCommonState *s);
51 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
52 uint8_t dest, uint8_t dest_mode);
54 /* Find first bit starting from msb */
55 static int apic_fls_bit(uint32_t value)
57 return 31 - clz32(value);
60 /* Find first bit starting from lsb */
61 static int apic_ffs_bit(uint32_t value)
63 return ctz32(value);
66 static inline void apic_reset_bit(uint32_t *tab, int index)
68 int i, mask;
69 i = index >> 5;
70 mask = 1 << (index & 0x1f);
71 tab[i] &= ~mask;
74 /* return -1 if no bit is set */
75 static int get_highest_priority_int(uint32_t *tab)
77 int i;
78 for (i = 7; i >= 0; i--) {
79 if (tab[i] != 0) {
80 return i * 32 + apic_fls_bit(tab[i]);
83 return -1;
86 static void apic_sync_vapic(APICCommonState *s, int sync_type)
88 VAPICState vapic_state;
89 size_t length;
90 off_t start;
91 int vector;
93 if (!s->vapic_paddr) {
94 return;
96 if (sync_type & SYNC_FROM_VAPIC) {
97 cpu_physical_memory_read(s->vapic_paddr, &vapic_state,
98 sizeof(vapic_state));
99 s->tpr = vapic_state.tpr;
101 if (sync_type & (SYNC_TO_VAPIC | SYNC_ISR_IRR_TO_VAPIC)) {
102 start = offsetof(VAPICState, isr);
103 length = offsetof(VAPICState, enabled) - offsetof(VAPICState, isr);
105 if (sync_type & SYNC_TO_VAPIC) {
106 assert(qemu_cpu_is_self(CPU(s->cpu)));
108 vapic_state.tpr = s->tpr;
109 vapic_state.enabled = 1;
110 start = 0;
111 length = sizeof(VAPICState);
114 vector = get_highest_priority_int(s->isr);
115 if (vector < 0) {
116 vector = 0;
118 vapic_state.isr = vector & 0xf0;
120 vapic_state.zero = 0;
122 vector = get_highest_priority_int(s->irr);
123 if (vector < 0) {
124 vector = 0;
126 vapic_state.irr = vector & 0xff;
128 address_space_write_rom(&address_space_memory,
129 s->vapic_paddr + start,
130 MEMTXATTRS_UNSPECIFIED,
131 ((void *)&vapic_state) + start, length);
135 static void apic_vapic_base_update(APICCommonState *s)
137 apic_sync_vapic(s, SYNC_TO_VAPIC);
140 static void apic_local_deliver(APICCommonState *s, int vector)
142 uint32_t lvt = s->lvt[vector];
143 int trigger_mode;
145 trace_apic_local_deliver(vector, (lvt >> 8) & 7);
147 if (lvt & APIC_LVT_MASKED)
148 return;
150 switch ((lvt >> 8) & 7) {
151 case APIC_DM_SMI:
152 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SMI);
153 break;
155 case APIC_DM_NMI:
156 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_NMI);
157 break;
159 case APIC_DM_EXTINT:
160 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_HARD);
161 break;
163 case APIC_DM_FIXED:
164 trigger_mode = APIC_TRIGGER_EDGE;
165 if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
166 (lvt & APIC_LVT_LEVEL_TRIGGER))
167 trigger_mode = APIC_TRIGGER_LEVEL;
168 apic_set_irq(s, lvt & 0xff, trigger_mode);
172 void apic_deliver_pic_intr(DeviceState *dev, int level)
174 APICCommonState *s = APIC(dev);
176 if (level) {
177 apic_local_deliver(s, APIC_LVT_LINT0);
178 } else {
179 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
181 switch ((lvt >> 8) & 7) {
182 case APIC_DM_FIXED:
183 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
184 break;
185 apic_reset_bit(s->irr, lvt & 0xff);
186 /* fall through */
187 case APIC_DM_EXTINT:
188 apic_update_irq(s);
189 break;
194 static void apic_external_nmi(APICCommonState *s)
196 apic_local_deliver(s, APIC_LVT_LINT1);
199 #define foreach_apic(apic, deliver_bitmask, code) \
201 int __i, __j;\
202 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
203 uint32_t __mask = deliver_bitmask[__i];\
204 if (__mask) {\
205 for(__j = 0; __j < 32; __j++) {\
206 if (__mask & (1U << __j)) {\
207 apic = local_apics[__i * 32 + __j];\
208 if (apic) {\
209 code;\
217 static void apic_bus_deliver(const uint32_t *deliver_bitmask,
218 uint8_t delivery_mode, uint8_t vector_num,
219 uint8_t trigger_mode)
221 APICCommonState *apic_iter;
223 switch (delivery_mode) {
224 case APIC_DM_LOWPRI:
225 /* XXX: search for focus processor, arbitration */
227 int i, d;
228 d = -1;
229 for(i = 0; i < MAX_APIC_WORDS; i++) {
230 if (deliver_bitmask[i]) {
231 d = i * 32 + apic_ffs_bit(deliver_bitmask[i]);
232 break;
235 if (d >= 0) {
236 apic_iter = local_apics[d];
237 if (apic_iter) {
238 apic_set_irq(apic_iter, vector_num, trigger_mode);
242 return;
244 case APIC_DM_FIXED:
245 break;
247 case APIC_DM_SMI:
248 foreach_apic(apic_iter, deliver_bitmask,
249 cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_SMI)
251 return;
253 case APIC_DM_NMI:
254 foreach_apic(apic_iter, deliver_bitmask,
255 cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_NMI)
257 return;
259 case APIC_DM_INIT:
260 /* normal INIT IPI sent to processors */
261 foreach_apic(apic_iter, deliver_bitmask,
262 cpu_interrupt(CPU(apic_iter->cpu),
263 CPU_INTERRUPT_INIT)
265 return;
267 case APIC_DM_EXTINT:
268 /* handled in I/O APIC code */
269 break;
271 default:
272 return;
275 foreach_apic(apic_iter, deliver_bitmask,
276 apic_set_irq(apic_iter, vector_num, trigger_mode) );
279 void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode,
280 uint8_t vector_num, uint8_t trigger_mode)
282 uint32_t deliver_bitmask[MAX_APIC_WORDS];
284 trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
285 trigger_mode);
287 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
288 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
291 static void apic_set_base(APICCommonState *s, uint64_t val)
293 s->apicbase = (val & 0xfffff000) |
294 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
295 /* if disabled, cannot be enabled again */
296 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
297 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
298 cpu_clear_apic_feature(&s->cpu->env);
299 s->spurious_vec &= ~APIC_SV_ENABLE;
303 static void apic_set_tpr(APICCommonState *s, uint8_t val)
305 /* Updates from cr8 are ignored while the VAPIC is active */
306 if (!s->vapic_paddr) {
307 s->tpr = val << 4;
308 apic_update_irq(s);
312 int apic_get_highest_priority_irr(DeviceState *dev)
314 APICCommonState *s;
316 if (!dev) {
317 /* no interrupts */
318 return -1;
320 s = APIC_COMMON(dev);
321 return get_highest_priority_int(s->irr);
324 static uint8_t apic_get_tpr(APICCommonState *s)
326 apic_sync_vapic(s, SYNC_FROM_VAPIC);
327 return s->tpr >> 4;
330 int apic_get_ppr(APICCommonState *s)
332 int tpr, isrv, ppr;
334 tpr = (s->tpr >> 4);
335 isrv = get_highest_priority_int(s->isr);
336 if (isrv < 0)
337 isrv = 0;
338 isrv >>= 4;
339 if (tpr >= isrv)
340 ppr = s->tpr;
341 else
342 ppr = isrv << 4;
343 return ppr;
346 static int apic_get_arb_pri(APICCommonState *s)
348 /* XXX: arbitration */
349 return 0;
354 * <0 - low prio interrupt,
355 * 0 - no interrupt,
356 * >0 - interrupt number
358 static int apic_irq_pending(APICCommonState *s)
360 int irrv, ppr;
362 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
363 return 0;
366 irrv = get_highest_priority_int(s->irr);
367 if (irrv < 0) {
368 return 0;
370 ppr = apic_get_ppr(s);
371 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
372 return -1;
375 return irrv;
378 /* signal the CPU if an irq is pending */
379 static void apic_update_irq(APICCommonState *s)
381 CPUState *cpu;
382 DeviceState *dev = (DeviceState *)s;
384 cpu = CPU(s->cpu);
385 if (!qemu_cpu_is_self(cpu)) {
386 cpu_interrupt(cpu, CPU_INTERRUPT_POLL);
387 } else if (apic_irq_pending(s) > 0) {
388 cpu_interrupt(cpu, CPU_INTERRUPT_HARD);
389 } else if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
390 cpu_reset_interrupt(cpu, CPU_INTERRUPT_HARD);
394 void apic_poll_irq(DeviceState *dev)
396 APICCommonState *s = APIC(dev);
398 apic_sync_vapic(s, SYNC_FROM_VAPIC);
399 apic_update_irq(s);
402 static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
404 kvm_report_irq_delivered(!apic_get_bit(s->irr, vector_num));
406 apic_set_bit(s->irr, vector_num);
407 if (trigger_mode)
408 apic_set_bit(s->tmr, vector_num);
409 else
410 apic_reset_bit(s->tmr, vector_num);
411 if (s->vapic_paddr) {
412 apic_sync_vapic(s, SYNC_ISR_IRR_TO_VAPIC);
414 * The vcpu thread needs to see the new IRR before we pull its current
415 * TPR value. That way, if we miss a lowering of the TRP, the guest
416 * has the chance to notice the new IRR and poll for IRQs on its own.
418 smp_wmb();
419 apic_sync_vapic(s, SYNC_FROM_VAPIC);
421 apic_update_irq(s);
424 static void apic_eoi(APICCommonState *s)
426 int isrv;
427 isrv = get_highest_priority_int(s->isr);
428 if (isrv < 0)
429 return;
430 apic_reset_bit(s->isr, isrv);
431 if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && apic_get_bit(s->tmr, isrv)) {
432 ioapic_eoi_broadcast(isrv);
434 apic_sync_vapic(s, SYNC_FROM_VAPIC | SYNC_TO_VAPIC);
435 apic_update_irq(s);
438 static int apic_find_dest(uint8_t dest)
440 APICCommonState *apic = local_apics[dest];
441 int i;
443 if (apic && apic->id == dest)
444 return dest; /* shortcut in case apic->id == local_apics[dest]->id */
446 for (i = 0; i < MAX_APICS; i++) {
447 apic = local_apics[i];
448 if (apic && apic->id == dest)
449 return i;
450 if (!apic)
451 break;
454 return -1;
457 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
458 uint8_t dest, uint8_t dest_mode)
460 APICCommonState *apic_iter;
461 int i;
463 if (dest_mode == 0) {
464 if (dest == 0xff) {
465 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
466 } else {
467 int idx = apic_find_dest(dest);
468 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
469 if (idx >= 0)
470 apic_set_bit(deliver_bitmask, idx);
472 } else {
473 /* XXX: cluster mode */
474 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
475 for(i = 0; i < MAX_APICS; i++) {
476 apic_iter = local_apics[i];
477 if (apic_iter) {
478 if (apic_iter->dest_mode == 0xf) {
479 if (dest & apic_iter->log_dest)
480 apic_set_bit(deliver_bitmask, i);
481 } else if (apic_iter->dest_mode == 0x0) {
482 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
483 (dest & apic_iter->log_dest & 0x0f)) {
484 apic_set_bit(deliver_bitmask, i);
487 } else {
488 break;
494 static void apic_startup(APICCommonState *s, int vector_num)
496 s->sipi_vector = vector_num;
497 cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
500 void apic_sipi(DeviceState *dev)
502 APICCommonState *s = APIC(dev);
504 cpu_reset_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
506 if (!s->wait_for_sipi)
507 return;
508 cpu_x86_load_seg_cache_sipi(s->cpu, s->sipi_vector);
509 s->wait_for_sipi = 0;
512 static void apic_deliver(DeviceState *dev, uint8_t dest, uint8_t dest_mode,
513 uint8_t delivery_mode, uint8_t vector_num,
514 uint8_t trigger_mode)
516 APICCommonState *s = APIC(dev);
517 uint32_t deliver_bitmask[MAX_APIC_WORDS];
518 int dest_shorthand = (s->icr[0] >> 18) & 3;
519 APICCommonState *apic_iter;
521 switch (dest_shorthand) {
522 case 0:
523 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
524 break;
525 case 1:
526 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
527 apic_set_bit(deliver_bitmask, s->id);
528 break;
529 case 2:
530 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
531 break;
532 case 3:
533 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
534 apic_reset_bit(deliver_bitmask, s->id);
535 break;
538 switch (delivery_mode) {
539 case APIC_DM_INIT:
541 int trig_mode = (s->icr[0] >> 15) & 1;
542 int level = (s->icr[0] >> 14) & 1;
543 if (level == 0 && trig_mode == 1) {
544 foreach_apic(apic_iter, deliver_bitmask,
545 apic_iter->arb_id = apic_iter->id );
546 return;
549 break;
551 case APIC_DM_SIPI:
552 foreach_apic(apic_iter, deliver_bitmask,
553 apic_startup(apic_iter, vector_num) );
554 return;
557 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
560 static bool apic_check_pic(APICCommonState *s)
562 DeviceState *dev = (DeviceState *)s;
564 if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
565 return false;
567 apic_deliver_pic_intr(dev, 1);
568 return true;
571 int apic_get_interrupt(DeviceState *dev)
573 APICCommonState *s = APIC(dev);
574 int intno;
576 /* if the APIC is installed or enabled, we let the 8259 handle the
577 IRQs */
578 if (!s)
579 return -1;
580 if (!(s->spurious_vec & APIC_SV_ENABLE))
581 return -1;
583 apic_sync_vapic(s, SYNC_FROM_VAPIC);
584 intno = apic_irq_pending(s);
586 /* if there is an interrupt from the 8259, let the caller handle
587 * that first since ExtINT interrupts ignore the priority.
589 if (intno == 0 || apic_check_pic(s)) {
590 apic_sync_vapic(s, SYNC_TO_VAPIC);
591 return -1;
592 } else if (intno < 0) {
593 apic_sync_vapic(s, SYNC_TO_VAPIC);
594 return s->spurious_vec & 0xff;
596 apic_reset_bit(s->irr, intno);
597 apic_set_bit(s->isr, intno);
598 apic_sync_vapic(s, SYNC_TO_VAPIC);
600 apic_update_irq(s);
602 return intno;
605 int apic_accept_pic_intr(DeviceState *dev)
607 APICCommonState *s = APIC(dev);
608 uint32_t lvt0;
610 if (!s)
611 return -1;
613 lvt0 = s->lvt[APIC_LVT_LINT0];
615 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
616 (lvt0 & APIC_LVT_MASKED) == 0)
617 return isa_pic != NULL;
619 return 0;
622 static void apic_timer_update(APICCommonState *s, int64_t current_time)
624 if (apic_next_timer(s, current_time)) {
625 timer_mod(s->timer, s->next_time);
626 } else {
627 timer_del(s->timer);
631 static void apic_timer(void *opaque)
633 APICCommonState *s = opaque;
635 apic_local_deliver(s, APIC_LVT_TIMER);
636 apic_timer_update(s, s->next_time);
639 static uint64_t apic_mem_read(void *opaque, hwaddr addr, unsigned size)
641 DeviceState *dev;
642 APICCommonState *s;
643 uint32_t val;
644 int index;
646 if (size < 4) {
647 return 0;
650 dev = cpu_get_current_apic();
651 if (!dev) {
652 return 0;
654 s = APIC(dev);
656 index = (addr >> 4) & 0xff;
657 switch(index) {
658 case 0x02: /* id */
659 val = s->id << 24;
660 break;
661 case 0x03: /* version */
662 val = s->version | ((APIC_LVT_NB - 1) << 16);
663 break;
664 case 0x08:
665 apic_sync_vapic(s, SYNC_FROM_VAPIC);
666 if (apic_report_tpr_access) {
667 cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_READ);
669 val = s->tpr;
670 break;
671 case 0x09:
672 val = apic_get_arb_pri(s);
673 break;
674 case 0x0a:
675 /* ppr */
676 val = apic_get_ppr(s);
677 break;
678 case 0x0b:
679 val = 0;
680 break;
681 case 0x0d:
682 val = s->log_dest << 24;
683 break;
684 case 0x0e:
685 val = (s->dest_mode << 28) | 0xfffffff;
686 break;
687 case 0x0f:
688 val = s->spurious_vec;
689 break;
690 case 0x10 ... 0x17:
691 val = s->isr[index & 7];
692 break;
693 case 0x18 ... 0x1f:
694 val = s->tmr[index & 7];
695 break;
696 case 0x20 ... 0x27:
697 val = s->irr[index & 7];
698 break;
699 case 0x28:
700 val = s->esr;
701 break;
702 case 0x30:
703 case 0x31:
704 val = s->icr[index & 1];
705 break;
706 case 0x32 ... 0x37:
707 val = s->lvt[index - 0x32];
708 break;
709 case 0x38:
710 val = s->initial_count;
711 break;
712 case 0x39:
713 val = apic_get_current_count(s);
714 break;
715 case 0x3e:
716 val = s->divide_conf;
717 break;
718 default:
719 s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
720 val = 0;
721 break;
723 trace_apic_mem_readl(addr, val);
724 return val;
727 static void apic_send_msi(MSIMessage *msi)
729 uint64_t addr = msi->address;
730 uint32_t data = msi->data;
731 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
732 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
733 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
734 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
735 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
736 /* XXX: Ignore redirection hint. */
737 apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
740 static void apic_mem_write(void *opaque, hwaddr addr, uint64_t val,
741 unsigned size)
743 DeviceState *dev;
744 APICCommonState *s;
745 int index = (addr >> 4) & 0xff;
747 if (size < 4) {
748 return;
751 if (addr > 0xfff || !index) {
752 /* MSI and MMIO APIC are at the same memory location,
753 * but actually not on the global bus: MSI is on PCI bus
754 * APIC is connected directly to the CPU.
755 * Mapping them on the global bus happens to work because
756 * MSI registers are reserved in APIC MMIO and vice versa. */
757 MSIMessage msi = { .address = addr, .data = val };
758 apic_send_msi(&msi);
759 return;
762 dev = cpu_get_current_apic();
763 if (!dev) {
764 return;
766 s = APIC(dev);
768 trace_apic_mem_writel(addr, val);
770 switch(index) {
771 case 0x02:
772 s->id = (val >> 24);
773 break;
774 case 0x03:
775 break;
776 case 0x08:
777 if (apic_report_tpr_access) {
778 cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_WRITE);
780 s->tpr = val;
781 apic_sync_vapic(s, SYNC_TO_VAPIC);
782 apic_update_irq(s);
783 break;
784 case 0x09:
785 case 0x0a:
786 break;
787 case 0x0b: /* EOI */
788 apic_eoi(s);
789 break;
790 case 0x0d:
791 s->log_dest = val >> 24;
792 break;
793 case 0x0e:
794 s->dest_mode = val >> 28;
795 break;
796 case 0x0f:
797 s->spurious_vec = val & 0x1ff;
798 apic_update_irq(s);
799 break;
800 case 0x10 ... 0x17:
801 case 0x18 ... 0x1f:
802 case 0x20 ... 0x27:
803 case 0x28:
804 break;
805 case 0x30:
806 s->icr[0] = val;
807 apic_deliver(dev, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
808 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
809 (s->icr[0] >> 15) & 1);
810 break;
811 case 0x31:
812 s->icr[1] = val;
813 break;
814 case 0x32 ... 0x37:
816 int n = index - 0x32;
817 s->lvt[n] = val;
818 if (n == APIC_LVT_TIMER) {
819 apic_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
820 } else if (n == APIC_LVT_LINT0 && apic_check_pic(s)) {
821 apic_update_irq(s);
824 break;
825 case 0x38:
826 s->initial_count = val;
827 s->initial_count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
828 apic_timer_update(s, s->initial_count_load_time);
829 break;
830 case 0x39:
831 break;
832 case 0x3e:
834 int v;
835 s->divide_conf = val & 0xb;
836 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
837 s->count_shift = (v + 1) & 7;
839 break;
840 default:
841 s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
842 break;
846 static void apic_pre_save(APICCommonState *s)
848 apic_sync_vapic(s, SYNC_FROM_VAPIC);
851 static void apic_post_load(APICCommonState *s)
853 if (s->timer_expiry != -1) {
854 timer_mod(s->timer, s->timer_expiry);
855 } else {
856 timer_del(s->timer);
860 static const MemoryRegionOps apic_io_ops = {
861 .read = apic_mem_read,
862 .write = apic_mem_write,
863 .impl.min_access_size = 1,
864 .impl.max_access_size = 4,
865 .valid.min_access_size = 1,
866 .valid.max_access_size = 4,
867 .endianness = DEVICE_NATIVE_ENDIAN,
870 static void apic_realize(DeviceState *dev, Error **errp)
872 APICCommonState *s = APIC(dev);
874 if (s->id >= MAX_APICS) {
875 error_setg(errp, "%s initialization failed. APIC ID %d is invalid",
876 object_get_typename(OBJECT(dev)), s->id);
877 return;
880 if (kvm_enabled()) {
881 warn_report("Userspace local APIC is deprecated for KVM.");
882 warn_report("Do not use kernel-irqchip except for the -M isapc machine type.");
885 memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
886 APIC_SPACE_SIZE);
889 * apic-msi's apic_mem_write can call into ioapic_eoi_broadcast, which can
890 * write back to apic-msi. As such mark the apic-msi region re-entrancy
891 * safe.
893 s->io_memory.disable_reentrancy_guard = true;
895 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s);
896 local_apics[s->id] = s;
898 msi_nonbroken = true;
901 static void apic_unrealize(DeviceState *dev)
903 APICCommonState *s = APIC(dev);
905 timer_free(s->timer);
906 local_apics[s->id] = NULL;
909 static void apic_class_init(ObjectClass *klass, void *data)
911 APICCommonClass *k = APIC_COMMON_CLASS(klass);
913 k->realize = apic_realize;
914 k->unrealize = apic_unrealize;
915 k->set_base = apic_set_base;
916 k->set_tpr = apic_set_tpr;
917 k->get_tpr = apic_get_tpr;
918 k->vapic_base_update = apic_vapic_base_update;
919 k->external_nmi = apic_external_nmi;
920 k->pre_save = apic_pre_save;
921 k->post_load = apic_post_load;
922 k->send_msi = apic_send_msi;
925 static const TypeInfo apic_info = {
926 .name = TYPE_APIC,
927 .instance_size = sizeof(APICCommonState),
928 .parent = TYPE_APIC_COMMON,
929 .class_init = apic_class_init,
932 static void apic_register_types(void)
934 type_register_static(&apic_info);
937 type_init(apic_register_types)