xref: /openbmc/qemu/hw/i386/kvm/i8259.c (revision 4a17cc4f)
1 /*
2  * KVM in-kernel PIC (i8259) support
3  *
4  * Copyright (c) 2011 Siemens AG
5  *
6  * Authors:
7  *  Jan Kiszka          <jan.kiszka@siemens.com>
8  *
9  * This work is licensed under the terms of the GNU GPL version 2.
10  * See the COPYING file in the top-level directory.
11  */
12 #include "hw/isa/i8259_internal.h"
13 #include "hw/i386/apic_internal.h"
14 #include "sysemu/kvm.h"
15 
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)
21 
22 /**
23  * KVMPICClass:
24  * @parent_realize: The parent's realizefn.
25  */
26 typedef struct KVMPICClass {
27     PICCommonClass parent_class;
28 
29     DeviceRealize parent_realize;
30 } KVMPICClass;
31 
32 static void kvm_pic_get(PICCommonState *s)
33 {
34     struct kvm_irqchip chip;
35     struct kvm_pic_state *kpic;
36     int ret;
37 
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);
40     if (ret < 0) {
41         fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret));
42         abort();
43     }
44 
45     kpic = &chip.chip.pic;
46 
47     s->last_irr = kpic->last_irr;
48     s->irr = kpic->irr;
49     s->imr = kpic->imr;
50     s->isr = kpic->isr;
51     s->priority_add = kpic->priority_add;
52     s->irq_base = kpic->irq_base;
53     s->read_reg_select = kpic->read_reg_select;
54     s->poll = kpic->poll;
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;
61     s->elcr = kpic->elcr;
62     s->elcr_mask = kpic->elcr_mask;
63 }
64 
65 static void kvm_pic_put(PICCommonState *s)
66 {
67     struct kvm_irqchip chip;
68     struct kvm_pic_state *kpic;
69     int ret;
70 
71     chip.chip_id = s->master ? KVM_IRQCHIP_PIC_MASTER : KVM_IRQCHIP_PIC_SLAVE;
72 
73     kpic = &chip.chip.pic;
74 
75     kpic->last_irr = s->last_irr;
76     kpic->irr = s->irr;
77     kpic->imr = s->imr;
78     kpic->isr = s->isr;
79     kpic->priority_add = s->priority_add;
80     kpic->irq_base = s->irq_base;
81     kpic->read_reg_select = s->read_reg_select;
82     kpic->poll = s->poll;
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;
89     kpic->elcr = s->elcr;
90     kpic->elcr_mask = s->elcr_mask;
91 
92     ret = kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, &chip);
93     if (ret < 0) {
94         fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret));
95         abort();
96     }
97 }
98 
99 static void kvm_pic_reset(DeviceState *dev)
100 {
101     PICCommonState *s = PIC_COMMON(dev);
102 
103     s->elcr = 0;
104     pic_reset_common(s);
105 
106     kvm_pic_put(s);
107 }
108 
109 static void kvm_pic_set_irq(void *opaque, int irq, int level)
110 {
111     int delivered;
112 
113     delivered = kvm_set_irq(kvm_state, irq, level);
114     apic_report_irq_delivered(delivered);
115 }
116 
117 static void kvm_pic_realize(DeviceState *dev, Error **errp)
118 {
119     PICCommonState *s = PIC_COMMON(dev);
120     KVMPICClass *kpc = KVM_PIC_GET_CLASS(dev);
121 
122     memory_region_init_reservation(&s->base_io, "kvm-pic", 2);
123     memory_region_init_reservation(&s->elcr_io, "kvm-elcr", 1);
124 
125     kpc->parent_realize(dev, errp);
126 }
127 
128 qemu_irq *kvm_i8259_init(ISABus *bus)
129 {
130     i8259_init_chip(TYPE_KVM_I8259, bus, true);
131     i8259_init_chip(TYPE_KVM_I8259, bus, false);
132 
133     return qemu_allocate_irqs(kvm_pic_set_irq, NULL, ISA_NUM_IRQS);
134 }
135 
136 static void kvm_i8259_class_init(ObjectClass *klass, void *data)
137 {
138     KVMPICClass *kpc = KVM_PIC_CLASS(klass);
139     PICCommonClass *k = PIC_COMMON_CLASS(klass);
140     DeviceClass *dc = DEVICE_CLASS(klass);
141 
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;
147 }
148 
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),
155 };
156 
157 static void kvm_pic_register_types(void)
158 {
159     type_register_static(&kvm_i8259_info);
160 }
161 
162 type_init(kvm_pic_register_types)
163