]> git.proxmox.com Git - mirror_qemu.git/blame - hw/intc/i8259_common.c
Merge tag 'for_upstream' of https://git.kernel.org/pub/scm/virt/kvm/mst/qemu into...
[mirror_qemu.git] / hw / intc / i8259_common.c
CommitLineData
512709f5
JK
1/*
2 * QEMU 8259 - common bits of emulated and KVM kernel model
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2011 Jan Kiszka, Siemens AG
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
d6454270 25
90191d07 26#include "qemu/osdep.h"
852c27e2 27#include "hw/intc/i8259.h"
0d09e41a 28#include "hw/isa/i8259_internal.h"
a27bd6c7 29#include "hw/qdev-properties.h"
d6454270 30#include "migration/vmstate.h"
1b23190a 31#include "monitor/monitor.h"
96927c74 32#include "qapi/error.h"
1b23190a
PX
33
34static int irq_level[16];
35static uint64_t irq_count[16];
512709f5
JK
36
37void pic_reset_common(PICCommonState *s)
38{
39 s->last_irr = 0;
aa24822b 40 s->irr &= s->elcr;
512709f5
JK
41 s->imr = 0;
42 s->isr = 0;
43 s->priority_add = 0;
44 s->irq_base = 0;
45 s->read_reg_select = 0;
46 s->poll = 0;
47 s->special_mask = 0;
48 s->init_state = 0;
49 s->auto_eoi = 0;
50 s->rotate_on_auto_eoi = 0;
51 s->special_fully_nested_mode = 0;
52 s->init4 = 0;
53 s->single_mode = 0;
ecb0e98b 54 /* Note: ELCR and LTIM are not reset */
512709f5
JK
55}
56
44b1ff31 57static int pic_dispatch_pre_save(void *opaque)
512709f5
JK
58{
59 PICCommonState *s = opaque;
8f04ee08 60 PICCommonClass *info = PIC_COMMON_GET_CLASS(s);
512709f5
JK
61
62 if (info->pre_save) {
63 info->pre_save(s);
64 }
44b1ff31
DDAG
65
66 return 0;
512709f5
JK
67}
68
69static int pic_dispatch_post_load(void *opaque, int version_id)
70{
71 PICCommonState *s = opaque;
8f04ee08 72 PICCommonClass *info = PIC_COMMON_GET_CLASS(s);
512709f5
JK
73
74 if (info->post_load) {
75 info->post_load(s);
76 }
77 return 0;
78}
79
db895a1e 80static void pic_common_realize(DeviceState *dev, Error **errp)
512709f5 81{
29bb5317 82 PICCommonState *s = PIC_COMMON(dev);
25a85359 83 ISADevice *isa = ISA_DEVICE(dev);
512709f5 84
25a85359 85 isa_register_ioport(isa, &s->base_io, s->iobase);
512709f5 86 if (s->elcr_addr != -1) {
25a85359 87 isa_register_ioport(isa, &s->elcr_io, s->elcr_addr);
512709f5
JK
88 }
89
db895a1e 90 qdev_set_legacy_instance_id(dev, s->iobase, 1);
512709f5
JK
91}
92
93ISADevice *i8259_init_chip(const char *name, ISABus *bus, bool master)
94{
4a17cc4f
AF
95 DeviceState *dev;
96 ISADevice *isadev;
512709f5 97
96927c74 98 isadev = isa_new(name);
4a17cc4f
AF
99 dev = DEVICE(isadev);
100 qdev_prop_set_uint32(dev, "iobase", master ? 0x20 : 0xa0);
101 qdev_prop_set_uint32(dev, "elcr_addr", master ? 0x4d0 : 0x4d1);
102 qdev_prop_set_uint8(dev, "elcr_mask", master ? 0xf8 : 0xde);
103 qdev_prop_set_bit(dev, "master", master);
96927c74 104 isa_realize_and_unref(isadev, bus, &error_fatal);
512709f5 105
4a17cc4f 106 return isadev;
512709f5
JK
107}
108
1b23190a
PX
109void pic_stat_update_irq(int irq, int level)
110{
111 if (level != irq_level[irq]) {
112 irq_level[irq] = level;
113 if (level == 1) {
114 irq_count[irq]++;
115 }
116 }
117}
118
1206a1ec
BB
119static bool pic_get_statistics(InterruptStatsProvider *obj,
120 uint64_t **irq_counts, unsigned int *nb_irqs)
1b23190a
PX
121{
122 PICCommonState *s = PIC_COMMON(obj);
123
124 if (s->master) {
125 *irq_counts = irq_count;
126 *nb_irqs = ARRAY_SIZE(irq_count);
127 } else {
128 *irq_counts = NULL;
129 *nb_irqs = 0;
130 }
131
132 return true;
133}
134
1206a1ec 135static void pic_print_info(InterruptStatsProvider *obj, Monitor *mon)
1b23190a
PX
136{
137 PICCommonState *s = PIC_COMMON(obj);
138
e267d164 139 pic_dispatch_pre_save(s);
1b23190a
PX
140 monitor_printf(mon, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
141 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
142 s->master ? 0 : 1, s->irr, s->imr, s->isr, s->priority_add,
143 s->irq_base, s->read_reg_select, s->elcr,
144 s->special_fully_nested_mode);
145}
146
ecb0e98b
DW
147static bool ltim_state_needed(void *opaque)
148{
149 PICCommonState *s = PIC_COMMON(opaque);
150
151 return !!s->ltim;
152}
153
154static const VMStateDescription vmstate_pic_ltim = {
155 .name = "i8259/ltim",
156 .version_id = 1,
157 .minimum_version_id = 1,
158 .needed = ltim_state_needed,
159 .fields = (VMStateField[]) {
160 VMSTATE_UINT8(ltim, PICCommonState),
161 VMSTATE_END_OF_LIST()
162 }
163};
164
512709f5
JK
165static const VMStateDescription vmstate_pic_common = {
166 .name = "i8259",
167 .version_id = 1,
168 .minimum_version_id = 1,
512709f5
JK
169 .pre_save = pic_dispatch_pre_save,
170 .post_load = pic_dispatch_post_load,
171 .fields = (VMStateField[]) {
172 VMSTATE_UINT8(last_irr, PICCommonState),
173 VMSTATE_UINT8(irr, PICCommonState),
174 VMSTATE_UINT8(imr, PICCommonState),
175 VMSTATE_UINT8(isr, PICCommonState),
176 VMSTATE_UINT8(priority_add, PICCommonState),
177 VMSTATE_UINT8(irq_base, PICCommonState),
178 VMSTATE_UINT8(read_reg_select, PICCommonState),
179 VMSTATE_UINT8(poll, PICCommonState),
180 VMSTATE_UINT8(special_mask, PICCommonState),
181 VMSTATE_UINT8(init_state, PICCommonState),
182 VMSTATE_UINT8(auto_eoi, PICCommonState),
183 VMSTATE_UINT8(rotate_on_auto_eoi, PICCommonState),
184 VMSTATE_UINT8(special_fully_nested_mode, PICCommonState),
185 VMSTATE_UINT8(init4, PICCommonState),
186 VMSTATE_UINT8(single_mode, PICCommonState),
187 VMSTATE_UINT8(elcr, PICCommonState),
188 VMSTATE_END_OF_LIST()
ecb0e98b
DW
189 },
190 .subsections = (const VMStateDescription*[]) {
191 &vmstate_pic_ltim,
192 NULL
512709f5
JK
193 }
194};
195
196static Property pic_properties_common[] = {
c7bcc85d
PB
197 DEFINE_PROP_UINT32("iobase", PICCommonState, iobase, -1),
198 DEFINE_PROP_UINT32("elcr_addr", PICCommonState, elcr_addr, -1),
199 DEFINE_PROP_UINT8("elcr_mask", PICCommonState, elcr_mask, -1),
512709f5
JK
200 DEFINE_PROP_BIT("master", PICCommonState, master, 0, false),
201 DEFINE_PROP_END_OF_LIST(),
202};
203
8f04ee08
AL
204static void pic_common_class_init(ObjectClass *klass, void *data)
205{
39bffca2 206 DeviceClass *dc = DEVICE_CLASS(klass);
b8c77234 207 InterruptStatsProviderClass *ic = INTERRUPT_STATS_PROVIDER_CLASS(klass);
8f04ee08 208
39bffca2 209 dc->vmsd = &vmstate_pic_common;
4f67d30b 210 device_class_set_props(dc, pic_properties_common);
db895a1e 211 dc->realize = pic_common_realize;
f3b17640
MA
212 /*
213 * Reason: unlike ordinary ISA devices, the PICs need additional
214 * wiring: its IRQ input lines are set up by board code, and the
215 * wiring of the slave to the master is hard-coded in device model
216 * code.
217 */
e90f2a8c 218 dc->user_creatable = false;
b8c77234
PX
219 ic->get_statistics = pic_get_statistics;
220 ic->print_info = pic_print_info;
8f04ee08
AL
221}
222
8c43a6f0 223static const TypeInfo pic_common_type = {
8f04ee08
AL
224 .name = TYPE_PIC_COMMON,
225 .parent = TYPE_ISA_DEVICE,
226 .instance_size = sizeof(PICCommonState),
227 .class_size = sizeof(PICCommonClass),
228 .class_init = pic_common_class_init,
229 .abstract = true,
b8c77234
PX
230 .interfaces = (InterfaceInfo[]) {
231 { TYPE_INTERRUPT_STATS_PROVIDER },
232 { }
233 },
8f04ee08
AL
234};
235
29bb5317 236static void pic_common_register_types(void)
8f04ee08
AL
237{
238 type_register_static(&pic_common_type);
239}
240
29bb5317 241type_init(pic_common_register_types)