2 * KVM in-kernel PIC (i8259) 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.
12 #include "hw/isa/i8259_internal.h"
13 #include "hw/i386/apic_internal.h"
14 #include "sysemu/kvm.h"
16 #define TYPE_KVM_I8259 "kvm-i8259"
17 #define KVM_PIC_CLASS(class) \
18 OBJECT_CLASS_CHECK(KVMPICClass, (class), TYPE_KVM_I8259)
19 #define KVM_PIC_GET_CLASS(obj) \
20 OBJECT_GET_CLASS(KVMPICClass, (obj), TYPE_KVM_I8259)
24 * @parent_realize: The parent's realizefn.
26 typedef struct KVMPICClass
{
27 PICCommonClass parent_class
;
29 DeviceRealize parent_realize
;
32 static void kvm_pic_get(PICCommonState
*s
)
34 struct kvm_irqchip chip
;
35 struct kvm_pic_state
*kpic
;
38 chip
.chip_id
= s
->master
? KVM_IRQCHIP_PIC_MASTER
: KVM_IRQCHIP_PIC_SLAVE
;
39 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_IRQCHIP
, &chip
);
41 fprintf(stderr
, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret
));
45 kpic
= &chip
.chip
.pic
;
47 s
->last_irr
= kpic
->last_irr
;
51 s
->priority_add
= kpic
->priority_add
;
52 s
->irq_base
= kpic
->irq_base
;
53 s
->read_reg_select
= kpic
->read_reg_select
;
55 s
->special_mask
= kpic
->special_mask
;
56 s
->init_state
= kpic
->init_state
;
57 s
->auto_eoi
= kpic
->auto_eoi
;
58 s
->rotate_on_auto_eoi
= kpic
->rotate_on_auto_eoi
;
59 s
->special_fully_nested_mode
= kpic
->special_fully_nested_mode
;
60 s
->init4
= kpic
->init4
;
62 s
->elcr_mask
= kpic
->elcr_mask
;
65 static void kvm_pic_put(PICCommonState
*s
)
67 struct kvm_irqchip chip
;
68 struct kvm_pic_state
*kpic
;
71 chip
.chip_id
= s
->master
? KVM_IRQCHIP_PIC_MASTER
: KVM_IRQCHIP_PIC_SLAVE
;
73 kpic
= &chip
.chip
.pic
;
75 kpic
->last_irr
= s
->last_irr
;
79 kpic
->priority_add
= s
->priority_add
;
80 kpic
->irq_base
= s
->irq_base
;
81 kpic
->read_reg_select
= s
->read_reg_select
;
83 kpic
->special_mask
= s
->special_mask
;
84 kpic
->init_state
= s
->init_state
;
85 kpic
->auto_eoi
= s
->auto_eoi
;
86 kpic
->rotate_on_auto_eoi
= s
->rotate_on_auto_eoi
;
87 kpic
->special_fully_nested_mode
= s
->special_fully_nested_mode
;
88 kpic
->init4
= s
->init4
;
90 kpic
->elcr_mask
= s
->elcr_mask
;
92 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_IRQCHIP
, &chip
);
94 fprintf(stderr
, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret
));
99 static void kvm_pic_reset(DeviceState
*dev
)
101 PICCommonState
*s
= PIC_COMMON(dev
);
109 static void kvm_pic_set_irq(void *opaque
, int irq
, int level
)
113 delivered
= kvm_set_irq(kvm_state
, irq
, level
);
114 apic_report_irq_delivered(delivered
);
117 static void kvm_pic_realize(DeviceState
*dev
, Error
**errp
)
119 PICCommonState
*s
= PIC_COMMON(dev
);
120 KVMPICClass
*kpc
= KVM_PIC_GET_CLASS(dev
);
122 memory_region_init_reservation(&s
->base_io
, NULL
, "kvm-pic", 2);
123 memory_region_init_reservation(&s
->elcr_io
, NULL
, "kvm-elcr", 1);
125 kpc
->parent_realize(dev
, errp
);
128 qemu_irq
*kvm_i8259_init(ISABus
*bus
)
130 i8259_init_chip(TYPE_KVM_I8259
, bus
, true);
131 i8259_init_chip(TYPE_KVM_I8259
, bus
, false);
133 return qemu_allocate_irqs(kvm_pic_set_irq
, NULL
, ISA_NUM_IRQS
);
136 static void kvm_i8259_class_init(ObjectClass
*klass
, void *data
)
138 KVMPICClass
*kpc
= KVM_PIC_CLASS(klass
);
139 PICCommonClass
*k
= PIC_COMMON_CLASS(klass
);
140 DeviceClass
*dc
= DEVICE_CLASS(klass
);
142 dc
->reset
= kvm_pic_reset
;
143 kpc
->parent_realize
= dc
->realize
;
144 dc
->realize
= kvm_pic_realize
;
145 k
->pre_save
= kvm_pic_get
;
146 k
->post_load
= kvm_pic_put
;
149 static const TypeInfo kvm_i8259_info
= {
150 .name
= TYPE_KVM_I8259
,
151 .parent
= TYPE_PIC_COMMON
,
152 .instance_size
= sizeof(PICCommonState
),
153 .class_init
= kvm_i8259_class_init
,
154 .class_size
= sizeof(KVMPICClass
),
157 static void kvm_pic_register_types(void)
159 type_register_static(&kvm_i8259_info
);
162 type_init(kvm_pic_register_types
)