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/qdev-properties.h"
16 #include "hw/intc/ioapic_internal.h"
17 #include "hw/intc/kvm_irqcount.h"
18 #include "sysemu/kvm.h"
19 #include "kvm/kvm_i386.h"
21 /* PC Utility function */
22 void kvm_pc_setup_irq_routing(bool pci_enabled
)
24 KVMState
*s
= kvm_state
;
27 assert(kvm_has_gsi_routing());
28 for (i
= 0; i
< 8; ++i
) {
32 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_MASTER
, i
);
34 for (i
= 8; i
< 16; ++i
) {
35 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_PIC_SLAVE
, i
- 8);
38 for (i
= 0; i
< KVM_IOAPIC_NUM_PINS
; ++i
) {
40 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, 2);
42 kvm_irqchip_add_irq_route(s
, i
, KVM_IRQCHIP_IOAPIC
, i
);
46 kvm_irqchip_commit_routes(s
);
49 typedef struct KVMIOAPICState KVMIOAPICState
;
51 struct KVMIOAPICState
{
52 IOAPICCommonState ioapic
;
53 uint32_t kvm_gsi_base
;
56 static void kvm_ioapic_get(IOAPICCommonState
*s
)
58 struct kvm_irqchip chip
;
59 struct kvm_ioapic_state
*kioapic
;
62 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
63 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_IRQCHIP
, &chip
);
65 fprintf(stderr
, "KVM_GET_IRQCHIP failed: %s\n", strerror(-ret
));
69 kioapic
= &chip
.chip
.ioapic
;
72 s
->ioregsel
= kioapic
->ioregsel
;
73 s
->irr
= kioapic
->irr
;
74 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
75 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
79 static void kvm_ioapic_put(IOAPICCommonState
*s
)
81 struct kvm_irqchip chip
;
82 struct kvm_ioapic_state
*kioapic
;
85 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
86 kioapic
= &chip
.chip
.ioapic
;
89 kioapic
->ioregsel
= s
->ioregsel
;
90 kioapic
->base_address
= s
->busdev
.mmio
[0].addr
;
91 kioapic
->irr
= s
->irr
;
92 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
93 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
96 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_IRQCHIP
, &chip
);
98 fprintf(stderr
, "KVM_SET_IRQCHIP failed: %s\n", strerror(-ret
));
103 static void kvm_ioapic_reset(DeviceState
*dev
)
105 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
107 ioapic_reset_common(dev
);
111 static void kvm_ioapic_set_irq(void *opaque
, int irq
, int level
)
113 KVMIOAPICState
*s
= opaque
;
114 IOAPICCommonState
*common
= IOAPIC_COMMON(s
);
117 ioapic_stat_update_irq(common
, irq
, level
);
118 delivered
= kvm_set_irq(kvm_state
, s
->kvm_gsi_base
+ irq
, level
);
119 kvm_report_irq_delivered(delivered
);
122 static void kvm_ioapic_realize(DeviceState
*dev
, Error
**errp
)
124 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
126 memory_region_init_io(&s
->io_memory
, OBJECT(dev
), NULL
, NULL
, "kvm-ioapic", 0x1000);
128 * KVM ioapic only supports 0x11 now. This will only be used when
129 * we want to dump ioapic version.
133 qdev_init_gpio_in(dev
, kvm_ioapic_set_irq
, IOAPIC_NUM_PINS
);
136 static Property kvm_ioapic_properties
[] = {
137 DEFINE_PROP_UINT32("gsi_base", KVMIOAPICState
, kvm_gsi_base
, 0),
138 DEFINE_PROP_END_OF_LIST()
141 static void kvm_ioapic_class_init(ObjectClass
*klass
, void *data
)
143 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
144 DeviceClass
*dc
= DEVICE_CLASS(klass
);
146 k
->realize
= kvm_ioapic_realize
;
147 k
->pre_save
= kvm_ioapic_get
;
148 k
->post_load
= kvm_ioapic_put
;
149 device_class_set_legacy_reset(dc
, kvm_ioapic_reset
);
150 device_class_set_props(dc
, kvm_ioapic_properties
);
153 static const TypeInfo kvm_ioapic_info
= {
154 .name
= TYPE_KVM_IOAPIC
,
155 .parent
= TYPE_IOAPIC_COMMON
,
156 .instance_size
= sizeof(KVMIOAPICState
),
157 .class_init
= kvm_ioapic_class_init
,
160 static void kvm_ioapic_register_types(void)
162 type_register_static(&kvm_ioapic_info
);
165 type_init(kvm_ioapic_register_types
)