/src/qemu/hw/intc/i8259_common.c
Line | Count | Source |
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 | | */ |
25 | | |
26 | | #include "qemu/osdep.h" |
27 | | #include "hw/intc/i8259.h" |
28 | | #include "hw/isa/i8259_internal.h" |
29 | | #include "hw/core/qdev-properties.h" |
30 | | #include "migration/vmstate.h" |
31 | | #include "qapi/error.h" |
32 | | |
33 | | |
34 | | void i8259_common_reset(I8259CommonState *cs) |
35 | 145k | { |
36 | 145k | cs->last_irr = 0; |
37 | 145k | cs->irr &= cs->elcr; |
38 | 145k | cs->imr = 0; |
39 | 145k | cs->isr = 0; |
40 | 145k | cs->priority_add = 0; |
41 | 145k | cs->irq_base = 0; |
42 | 145k | cs->read_reg_select = 0; |
43 | 145k | cs->poll = 0; |
44 | 145k | cs->special_mask = 0; |
45 | 145k | cs->init_state = 0; |
46 | 145k | cs->auto_eoi = 0; |
47 | 145k | cs->rotate_on_auto_eoi = 0; |
48 | 145k | cs->special_fully_nested_mode = 0; |
49 | 145k | cs->init4 = 0; |
50 | 145k | cs->single_mode = 0; |
51 | | /* Note: ELCR and LTIM are not reset */ |
52 | 145k | } |
53 | | |
54 | | static int i8259_common_dispatch_pre_save(void *opaque) |
55 | 0 | { |
56 | 0 | I8259CommonState *cs = opaque; |
57 | 0 | I8259CommonClass *info = I8259_COMMON_GET_CLASS(cs); |
58 | |
|
59 | 0 | if (info->pre_save) { |
60 | 0 | info->pre_save(cs); |
61 | 0 | } |
62 | |
|
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | | static int i8259_common_dispatch_post_load(void *opaque, int version_id) |
67 | 0 | { |
68 | 0 | I8259CommonState *cs = opaque; |
69 | 0 | I8259CommonClass *info = I8259_COMMON_GET_CLASS(cs); |
70 | |
|
71 | 0 | if (info->post_load) { |
72 | 0 | info->post_load(cs); |
73 | 0 | } |
74 | 0 | return 0; |
75 | 0 | } |
76 | | |
77 | | static void i8259_common_realize(DeviceState *dev, Error **errp) |
78 | 126 | { |
79 | 126 | I8259CommonState *cs = I8259_COMMON(dev); |
80 | 126 | ISADevice *isa = ISA_DEVICE(dev); |
81 | | |
82 | 126 | isa_register_ioport(isa, &cs->base_io, cs->iobase); |
83 | 126 | if (cs->elcr_addr != -1) { |
84 | 126 | isa_register_ioport(isa, &cs->elcr_io, cs->elcr_addr); |
85 | 126 | } |
86 | | |
87 | 126 | qdev_set_legacy_instance_id(dev, cs->iobase, 1); |
88 | 126 | } |
89 | | |
90 | | void i8259_stat_update_irq(uint64_t *irq_counts, int *irq_levels, |
91 | | int irq, int level) |
92 | 29.0M | { |
93 | 29.0M | if (level != irq_levels[irq]) { |
94 | 9.71M | irq_levels[irq] = level; |
95 | 9.71M | if (level == 1) { |
96 | 4.85M | irq_counts[irq]++; |
97 | 4.85M | } |
98 | 9.71M | } |
99 | 29.0M | } |
100 | | |
101 | | void i8259_pic_print_info(InterruptStatsProvider *obj, GString *buf) |
102 | 0 | { |
103 | | /* No information for i8259-based PICs */ |
104 | 0 | return; |
105 | 0 | } |
106 | | |
107 | | static bool i8259_common_get_statistics(InterruptStatsProvider *obj, |
108 | | uint64_t **irq_counts, |
109 | | unsigned int *nb_irqs) |
110 | 0 | { |
111 | | /* No statistics for individual i8259s */ |
112 | 0 | *nb_irqs = 0; |
113 | |
|
114 | 0 | return true; |
115 | 0 | } |
116 | | |
117 | | static void i8259_common_print_info(InterruptStatsProvider *obj, GString *buf) |
118 | 0 | { |
119 | 0 | I8259CommonState *cs = I8259_COMMON(obj); |
120 | |
|
121 | 0 | i8259_common_dispatch_pre_save(cs); |
122 | 0 | g_string_append_printf(buf, |
123 | 0 | "pic%d: irr=0x%02x imr=0x%02x isr=0x%02x hprio=%d " |
124 | 0 | "irq_base=0x%02x rr_sel=%d elcr=0x%02x fnm=%d\n", |
125 | 0 | cs->master ? 0 : 1, cs->irr, cs->imr, cs->isr, |
126 | 0 | cs->priority_add, |
127 | 0 | cs->irq_base, cs->read_reg_select, cs->elcr, |
128 | 0 | cs->special_fully_nested_mode); |
129 | 0 | } |
130 | | |
131 | | static bool ltim_state_needed(void *opaque) |
132 | 0 | { |
133 | 0 | I8259CommonState *cs = I8259_COMMON(opaque); |
134 | |
|
135 | 0 | return !!cs->ltim; |
136 | 0 | } |
137 | | |
138 | | static const VMStateDescription vmstate_pic_ltim = { |
139 | | .name = "i8259/ltim", |
140 | | .version_id = 1, |
141 | | .minimum_version_id = 1, |
142 | | .needed = ltim_state_needed, |
143 | | .fields = (const VMStateField[]) { |
144 | | VMSTATE_UINT8(ltim, I8259CommonState), |
145 | | VMSTATE_END_OF_LIST() |
146 | | } |
147 | | }; |
148 | | |
149 | | static const VMStateDescription vmstate_i8259_common = { |
150 | | .name = "i8259", |
151 | | .version_id = 1, |
152 | | .minimum_version_id = 1, |
153 | | .pre_save = i8259_common_dispatch_pre_save, |
154 | | .post_load = i8259_common_dispatch_post_load, |
155 | | .fields = (const VMStateField[]) { |
156 | | VMSTATE_UINT8(last_irr, I8259CommonState), |
157 | | VMSTATE_UINT8(irr, I8259CommonState), |
158 | | VMSTATE_UINT8(imr, I8259CommonState), |
159 | | VMSTATE_UINT8(isr, I8259CommonState), |
160 | | VMSTATE_UINT8(priority_add, I8259CommonState), |
161 | | VMSTATE_UINT8(irq_base, I8259CommonState), |
162 | | VMSTATE_UINT8(read_reg_select, I8259CommonState), |
163 | | VMSTATE_UINT8(poll, I8259CommonState), |
164 | | VMSTATE_UINT8(special_mask, I8259CommonState), |
165 | | VMSTATE_UINT8(init_state, I8259CommonState), |
166 | | VMSTATE_UINT8(auto_eoi, I8259CommonState), |
167 | | VMSTATE_UINT8(rotate_on_auto_eoi, I8259CommonState), |
168 | | VMSTATE_UINT8(special_fully_nested_mode, I8259CommonState), |
169 | | VMSTATE_UINT8(init4, I8259CommonState), |
170 | | VMSTATE_UINT8(single_mode, I8259CommonState), |
171 | | VMSTATE_UINT8(elcr, I8259CommonState), |
172 | | VMSTATE_END_OF_LIST() |
173 | | }, |
174 | | .subsections = (const VMStateDescription * const []) { |
175 | | &vmstate_pic_ltim, |
176 | | NULL |
177 | | } |
178 | | }; |
179 | | |
180 | | static const Property i8259_common_properties[] = { |
181 | | DEFINE_PROP_UINT32("iobase", I8259CommonState, iobase, -1), |
182 | | DEFINE_PROP_UINT32("elcr_addr", I8259CommonState, elcr_addr, -1), |
183 | | DEFINE_PROP_UINT8("elcr_mask", I8259CommonState, elcr_mask, -1), |
184 | | DEFINE_PROP_BIT("master", I8259CommonState, master, 0, false), |
185 | | }; |
186 | | |
187 | | static void i8259_common_class_init(ObjectClass *klass, const void *data) |
188 | 63 | { |
189 | 63 | DeviceClass *dc = DEVICE_CLASS(klass); |
190 | 63 | InterruptStatsProviderClass *ic = INTERRUPT_STATS_PROVIDER_CLASS(klass); |
191 | | |
192 | 63 | dc->vmsd = &vmstate_i8259_common; |
193 | 63 | device_class_set_props(dc, i8259_common_properties); |
194 | 63 | dc->realize = i8259_common_realize; |
195 | | /* |
196 | | * Reason: unlike ordinary ISA devices, the PICs need additional |
197 | | * wiring: its IRQ input lines are set up by board code, and the |
198 | | * wiring of the slave to the master is hard-coded in device model |
199 | | * code. |
200 | | */ |
201 | | dc->user_creatable = false; |
202 | 63 | ic->get_statistics = i8259_common_get_statistics; |
203 | 63 | ic->print_info = i8259_common_print_info; |
204 | 63 | } |
205 | | |
206 | | static const TypeInfo i8259_common_type_infos[] = { |
207 | | { |
208 | | .name = TYPE_I8259_COMMON, |
209 | | .parent = TYPE_ISA_DEVICE, |
210 | | .instance_size = sizeof(I8259CommonState), |
211 | | .class_size = sizeof(I8259CommonClass), |
212 | | .class_init = i8259_common_class_init, |
213 | | .abstract = true, |
214 | | .interfaces = (const InterfaceInfo[]) { |
215 | | { TYPE_INTERRUPT_STATS_PROVIDER }, |
216 | | { } |
217 | | }, |
218 | | }, |
219 | | }; |
220 | | |
221 | | DEFINE_TYPES(i8259_common_type_infos) |