2011-10-16 23:25:49 +02:00
|
|
|
/*
|
|
|
|
* KVM in-kernel IOPIC support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Siemens AG
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Jan Kiszka <jan.kiszka@siemens.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL version 2.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:03 +01:00
|
|
|
#include "qemu/osdep.h"
|
2015-09-22 15:18:20 +02:00
|
|
|
#include "monitor/monitor.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/i386/pc.h"
|
|
|
|
#include "hw/i386/ioapic_internal.h"
|
|
|
|
#include "hw/i386/apic_internal.h"
|
2012-12-17 18:20:04 +01:00
|
|
|
#include "sysemu/kvm.h"
|
2011-10-16 23:25:49 +02:00
|
|
|
|
2012-11-14 21:54:02 +01:00
|
|
|
/* PC Utility function */
|
|
|
|
void kvm_pc_setup_irq_routing(bool pci_enabled)
|
|
|
|
{
|
|
|
|
KVMState *s = kvm_state;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
|
|
|
|
for (i = 0; i < 8; ++i) {
|
|
|
|
if (i == 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
|
|
|
|
}
|
|
|
|
for (i = 8; i < 16; ++i) {
|
|
|
|
kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
|
|
|
|
}
|
|
|
|
if (pci_enabled) {
|
|
|
|
for (i = 0; i < 24; ++i) {
|
|
|
|
if (i == 0) {
|
|
|
|
kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
|
|
|
|
} else if (i != 2) {
|
|
|
|
kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 01:11:55 +02:00
|
|
|
kvm_irqchip_commit_routes(s);
|
2012-11-14 21:54:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_pc_gsi_handler(void *opaque, int n, int level)
|
|
|
|
{
|
|
|
|
GSIState *s = opaque;
|
|
|
|
|
|
|
|
if (n < ISA_NUM_IRQS) {
|
|
|
|
/* Kernel will forward to both PIC and IOAPIC */
|
|
|
|
qemu_set_irq(s->i8259_irq[n], level);
|
|
|
|
} else {
|
|
|
|
qemu_set_irq(s->ioapic_irq[n], level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-16 23:25:49 +02:00
|
|
|
typedef struct KVMIOAPICState KVMIOAPICState;
|
|
|
|
|
|
|
|
struct KVMIOAPICState {
|
|
|
|
IOAPICCommonState ioapic;
|
|
|
|
uint32_t kvm_gsi_base;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_ioapic_get(IOAPICCommonState *s)
|
|
|
|
{
|
|
|
|
struct kvm_irqchip chip;
|
|
|
|
struct kvm_ioapic_state *kioapic;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
chip.chip_id = KVM_IRQCHIP_IOAPIC;
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, &chip);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
kioapic = &chip.chip.ioapic;
|
|
|
|
|
|
|
|
s->id = kioapic->id;
|
|
|
|
s->ioregsel = kioapic->ioregsel;
|
|
|
|
s->irr = kioapic->irr;
|
|
|
|
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
|
|
|
|
s->ioredtbl[i] = kioapic->redirtbl[i].bits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_ioapic_put(IOAPICCommonState *s)
|
|
|
|
{
|
|
|
|
struct kvm_irqchip chip;
|
|
|
|
struct kvm_ioapic_state *kioapic;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
chip.chip_id = KVM_IRQCHIP_IOAPIC;
|
|
|
|
kioapic = &chip.chip.ioapic;
|
|
|
|
|
|
|
|
kioapic->id = s->id;
|
|
|
|
kioapic->ioregsel = s->ioregsel;
|
|
|
|
kioapic->base_address = s->busdev.mmio[0].addr;
|
|
|
|
kioapic->irr = s->irr;
|
|
|
|
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
|
|
|
|
kioapic->redirtbl[i].bits = s->ioredtbl[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, &chip);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 15:18:20 +02:00
|
|
|
void kvm_ioapic_dump_state(Monitor *mon, const QDict *qdict)
|
|
|
|
{
|
|
|
|
IOAPICCommonState s;
|
|
|
|
|
|
|
|
kvm_ioapic_get(&s);
|
|
|
|
|
|
|
|
ioapic_print_redtbl(mon, &s);
|
|
|
|
}
|
|
|
|
|
2011-10-16 23:25:49 +02:00
|
|
|
static void kvm_ioapic_reset(DeviceState *dev)
|
|
|
|
{
|
2013-07-27 16:30:47 +02:00
|
|
|
IOAPICCommonState *s = IOAPIC_COMMON(dev);
|
2011-10-16 23:25:49 +02:00
|
|
|
|
|
|
|
ioapic_reset_common(dev);
|
|
|
|
kvm_ioapic_put(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_ioapic_set_irq(void *opaque, int irq, int level)
|
|
|
|
{
|
|
|
|
KVMIOAPICState *s = opaque;
|
|
|
|
int delivered;
|
|
|
|
|
2012-07-26 16:35:12 +02:00
|
|
|
delivered = kvm_set_irq(kvm_state, s->kvm_gsi_base + irq, level);
|
2011-10-16 23:25:49 +02:00
|
|
|
apic_report_irq_delivered(delivered);
|
|
|
|
}
|
|
|
|
|
2013-11-05 11:16:05 +01:00
|
|
|
static void kvm_ioapic_realize(DeviceState *dev, Error **errp)
|
2011-10-16 23:25:49 +02:00
|
|
|
{
|
2013-11-05 11:16:05 +01:00
|
|
|
IOAPICCommonState *s = IOAPIC_COMMON(dev);
|
2013-11-05 11:16:04 +01:00
|
|
|
|
2013-06-06 11:41:28 +02:00
|
|
|
memory_region_init_reservation(&s->io_memory, NULL, "kvm-ioapic", 0x1000);
|
2011-10-16 23:25:49 +02:00
|
|
|
|
2013-11-05 11:16:04 +01:00
|
|
|
qdev_init_gpio_in(dev, kvm_ioapic_set_irq, IOAPIC_NUM_PINS);
|
2011-10-16 23:25:49 +02:00
|
|
|
}
|
|
|
|
|
2011-12-08 04:34:16 +01:00
|
|
|
static Property kvm_ioapic_properties[] = {
|
|
|
|
DEFINE_PROP_UINT32("gsi_base", KVMIOAPICState, kvm_gsi_base, 0),
|
|
|
|
DEFINE_PROP_END_OF_LIST()
|
|
|
|
};
|
|
|
|
|
2012-01-24 20:12:29 +01:00
|
|
|
static void kvm_ioapic_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 20:12:29 +01:00
|
|
|
|
2013-11-05 11:16:05 +01:00
|
|
|
k->realize = kvm_ioapic_realize;
|
2012-01-24 20:12:29 +01:00
|
|
|
k->pre_save = kvm_ioapic_get;
|
|
|
|
k->post_load = kvm_ioapic_put;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->reset = kvm_ioapic_reset;
|
|
|
|
dc->props = kvm_ioapic_properties;
|
2012-01-24 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo kvm_ioapic_info = {
|
2012-01-24 20:12:29 +01:00
|
|
|
.name = "kvm-ioapic",
|
2011-12-08 04:34:16 +01:00
|
|
|
.parent = TYPE_IOAPIC_COMMON,
|
|
|
|
.instance_size = sizeof(KVMIOAPICState),
|
2012-01-24 20:12:29 +01:00
|
|
|
.class_init = kvm_ioapic_class_init,
|
2011-10-16 23:25:49 +02:00
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void kvm_ioapic_register_types(void)
|
2011-10-16 23:25:49 +02:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&kvm_ioapic_info);
|
2011-10-16 23:25:49 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(kvm_ioapic_register_types)
|