2 * KVM in-kernel IOPIC support
4 * Copyright (c) 2011 Siemens AG
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL version 2.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "monitor/monitor.h"
15 #include "hw/i386/pc.h"
17 #include "hw/qdev-properties.h"
18 #include "hw/i386/ioapic_internal.h"
19 #include "hw/i386/apic_internal.h"
20 #include "sysemu/kvm.h"
22 /* PC Utility function */
23 void kvm_pc_setup_irq_routing(bool pci_enabled
)
25 KVMState
*s
= kvm_state
;
28 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
29 for (i
= 0; i
< 8; ++i
) {
33 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
35 for (i
= 8; i
< 16; ++i
) {
36 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
39 for (i
= 0; i
< 24; ++i
) {
41 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
43 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
47 kvm_irqchip_commit_routes(s
);
51 void kvm_pc_gsi_handler(void *opaque
, int n
, int level
)
55 if (n
< ISA_NUM_IRQS
) {
56 /* Kernel will forward to both PIC and IOAPIC */
57 qemu_set_irq(s
->i8259_irq
[n
], level
);
59 qemu_set_irq(s
->ioapic_irq
[n
], level
);
63 typedef struct KVMIOAPICState KVMIOAPICState
;
65 struct KVMIOAPICState
{
66 IOAPICCommonState ioapic
;
67 uint32_t kvm_gsi_base
;
70 static void kvm_ioapic_get(IOAPICCommonState
*s
)
72 struct kvm_irqchip chip
;
73 struct kvm_ioapic_state
*kioapic
;
76 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
77 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_IRQCHIP
, &chip
);
79 fprintf(stderr
, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret
));
83 kioapic
= &chip
.chip
.ioapic
;
86 s
->ioregsel
= kioapic
->ioregsel
;
87 s
->irr
= kioapic
->irr
;
88 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
89 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
93 static void kvm_ioapic_put(IOAPICCommonState
*s
)
95 struct kvm_irqchip chip
;
96 struct kvm_ioapic_state
*kioapic
;
99 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
100 kioapic
= &chip
.chip
.ioapic
;
103 kioapic
->ioregsel
= s
->ioregsel
;
104 kioapic
->base_address
= s
->busdev
.mmio
[0].addr
;
105 kioapic
->irr
= s
->irr
;
106 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
107 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
110 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_IRQCHIP
, &chip
);
112 fprintf(stderr
, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret
));
117 static void kvm_ioapic_reset(DeviceState
*dev
)
119 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
121 ioapic_reset_common(dev
);
125 static void kvm_ioapic_set_irq(void *opaque
, int irq
, int level
)
127 KVMIOAPICState
*s
= opaque
;
128 IOAPICCommonState
*common
= IOAPIC_COMMON(s
);
131 ioapic_stat_update_irq(common
, irq
, level
);
132 delivered
= kvm_set_irq(kvm_state
, s
->kvm_gsi_base
+ irq
, level
);
133 apic_report_irq_delivered(delivered
);
136 static void kvm_ioapic_realize(DeviceState
*dev
, Error
**errp
)
138 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
140 memory_region_init_io(&s
->io_memory
, OBJECT(dev
), NULL
, NULL
, "kvm-ioapic", 0x1000);
142 * KVM ioapic only supports 0x11 now. This will only be used when
143 * we want to dump ioapic version.
147 qdev_init_gpio_in(dev
, kvm_ioapic_set_irq
, IOAPIC_NUM_PINS
);
150 static Property kvm_ioapic_properties
[] = {
151 DEFINE_PROP_UINT32("gsi_base", KVMIOAPICState
, kvm_gsi_base
, 0),
152 DEFINE_PROP_END_OF_LIST()
155 static void kvm_ioapic_class_init(ObjectClass
*klass
, void *data
)
157 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
158 DeviceClass
*dc
= DEVICE_CLASS(klass
);
160 k
->realize
= kvm_ioapic_realize
;
161 k
->pre_save
= kvm_ioapic_get
;
162 k
->post_load
= kvm_ioapic_put
;
163 dc
->reset
= kvm_ioapic_reset
;
164 dc
->props
= kvm_ioapic_properties
;
167 static const TypeInfo kvm_ioapic_info
= {
168 .name
= TYPE_KVM_IOAPIC
,
169 .parent
= TYPE_IOAPIC_COMMON
,
170 .instance_size
= sizeof(KVMIOAPICState
),
171 .class_init
= kvm_ioapic_class_init
,
174 static void kvm_ioapic_register_types(void)
176 type_register_static(&kvm_ioapic_info
);
179 type_init(kvm_ioapic_register_types
)