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/x86.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 assert(kvm_has_gsi_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
);
50 typedef struct KVMIOAPICState KVMIOAPICState
;
52 struct KVMIOAPICState
{
53 IOAPICCommonState ioapic
;
54 uint32_t kvm_gsi_base
;
57 static void kvm_ioapic_get(IOAPICCommonState
*s
)
59 struct kvm_irqchip chip
;
60 struct kvm_ioapic_state
*kioapic
;
63 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
64 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_IRQCHIP
, &chip
);
66 fprintf(stderr
, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret
));
70 kioapic
= &chip
.chip
.ioapic
;
73 s
->ioregsel
= kioapic
->ioregsel
;
74 s
->irr
= kioapic
->irr
;
75 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
76 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
80 static void kvm_ioapic_put(IOAPICCommonState
*s
)
82 struct kvm_irqchip chip
;
83 struct kvm_ioapic_state
*kioapic
;
86 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
87 kioapic
= &chip
.chip
.ioapic
;
90 kioapic
->ioregsel
= s
->ioregsel
;
91 kioapic
->base_address
= s
->busdev
.mmio
[0].addr
;
92 kioapic
->irr
= s
->irr
;
93 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
94 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
97 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_IRQCHIP
, &chip
);
99 fprintf(stderr
, "KVM_SET_IRQCHIP failed: %s\n", strerror(ret
));
104 static void kvm_ioapic_reset(DeviceState
*dev
)
106 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
108 ioapic_reset_common(dev
);
112 static void kvm_ioapic_set_irq(void *opaque
, int irq
, int level
)
114 KVMIOAPICState
*s
= opaque
;
115 IOAPICCommonState
*common
= IOAPIC_COMMON(s
);
118 ioapic_stat_update_irq(common
, irq
, level
);
119 delivered
= kvm_set_irq(kvm_state
, s
->kvm_gsi_base
+ irq
, level
);
120 apic_report_irq_delivered(delivered
);
123 static void kvm_ioapic_realize(DeviceState
*dev
, Error
**errp
)
125 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
127 memory_region_init_io(&s
->io_memory
, OBJECT(dev
), NULL
, NULL
, "kvm-ioapic", 0x1000);
129 * KVM ioapic only supports 0x11 now. This will only be used when
130 * we want to dump ioapic version.
134 qdev_init_gpio_in(dev
, kvm_ioapic_set_irq
, IOAPIC_NUM_PINS
);
137 static Property kvm_ioapic_properties
[] = {
138 DEFINE_PROP_UINT32("gsi_base", KVMIOAPICState
, kvm_gsi_base
, 0),
139 DEFINE_PROP_END_OF_LIST()
142 static void kvm_ioapic_class_init(ObjectClass
*klass
, void *data
)
144 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
145 DeviceClass
*dc
= DEVICE_CLASS(klass
);
147 k
->realize
= kvm_ioapic_realize
;
148 k
->pre_save
= kvm_ioapic_get
;
149 k
->post_load
= kvm_ioapic_put
;
150 dc
->reset
= kvm_ioapic_reset
;
151 device_class_set_props(dc
, kvm_ioapic_properties
);
154 static const TypeInfo kvm_ioapic_info
= {
155 .name
= TYPE_KVM_IOAPIC
,
156 .parent
= TYPE_IOAPIC_COMMON
,
157 .instance_size
= sizeof(KVMIOAPICState
),
158 .class_init
= kvm_ioapic_class_init
,
161 static void kvm_ioapic_register_types(void)
163 type_register_static(&kvm_ioapic_info
);
166 type_init(kvm_ioapic_register_types
)