Commit | Line | Data |
---|---|---|
f915a115 GH |
1 | /* |
2 | * isa bus support for qdev. | |
3 | * | |
4 | * Copyright (c) 2009 Gerd Hoffmann <kraxel@redhat.com> | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
0430891c | 19 | #include "qemu/osdep.h" |
1081ed2c | 20 | #include "qemu/error-report.h" |
da34e65c | 21 | #include "qapi/error.h" |
83c9f4ca | 22 | #include "hw/hw.h" |
83c9089e | 23 | #include "monitor/monitor.h" |
83c9f4ca | 24 | #include "hw/sysbus.h" |
9c17d615 | 25 | #include "sysemu/sysemu.h" |
0d09e41a | 26 | #include "hw/isa/isa.h" |
9157eee1 | 27 | #include "hw/i386/pc.h" |
f915a115 | 28 | |
f915a115 GH |
29 | static ISABus *isabus; |
30 | ||
2091ba23 | 31 | static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
6a26e119 | 32 | static char *isabus_get_fw_dev_path(DeviceState *dev); |
2091ba23 | 33 | |
0d936928 AL |
34 | static void isa_bus_class_init(ObjectClass *klass, void *data) |
35 | { | |
36 | BusClass *k = BUS_CLASS(klass); | |
37 | ||
38 | k->print_dev = isabus_dev_print; | |
39 | k->get_fw_dev_path = isabus_get_fw_dev_path; | |
40 | } | |
41 | ||
5484f30b HP |
42 | static const TypeInfo isa_dma_info = { |
43 | .name = TYPE_ISADMA, | |
44 | .parent = TYPE_INTERFACE, | |
45 | .class_size = sizeof(IsaDmaClass), | |
46 | }; | |
47 | ||
0d936928 AL |
48 | static const TypeInfo isa_bus_info = { |
49 | .name = TYPE_ISA_BUS, | |
50 | .parent = TYPE_BUS, | |
51 | .instance_size = sizeof(ISABus), | |
52 | .class_init = isa_bus_class_init, | |
f915a115 GH |
53 | }; |
54 | ||
bb2ed009 | 55 | ISABus *isa_bus_new(DeviceState *dev, MemoryRegion* address_space, |
d10e5432 | 56 | MemoryRegion *address_space_io, Error **errp) |
f915a115 GH |
57 | { |
58 | if (isabus) { | |
d10e5432 | 59 | error_setg(errp, "Can't create a second ISA bus"); |
f915a115 GH |
60 | return NULL; |
61 | } | |
337a3e5c | 62 | if (!dev) { |
2091ba23 | 63 | dev = qdev_create(NULL, "isabus-bridge"); |
e23a1b33 | 64 | qdev_init_nofail(dev); |
2091ba23 | 65 | } |
f915a115 | 66 | |
2ae0e48d | 67 | isabus = ISA_BUS(qbus_create(TYPE_ISA_BUS, dev, NULL)); |
bb2ed009 | 68 | isabus->address_space = address_space; |
c2d0d012 | 69 | isabus->address_space_io = address_space_io; |
f915a115 GH |
70 | return isabus; |
71 | } | |
72 | ||
48a18b3c | 73 | void isa_bus_irqs(ISABus *bus, qemu_irq *irqs) |
f915a115 | 74 | { |
d3c68e4f | 75 | bus->irqs = irqs; |
2091ba23 GH |
76 | } |
77 | ||
3a38d437 | 78 | /* |
ee951a37 | 79 | * isa_get_irq() returns the corresponding qemu_irq entry for the i8259. |
3a38d437 JS |
80 | * |
81 | * This function is only for special cases such as the 'ferr', and | |
82 | * temporary use for normal devices until they are converted to qdev. | |
83 | */ | |
48a18b3c | 84 | qemu_irq isa_get_irq(ISADevice *dev, int isairq) |
3a38d437 | 85 | { |
2ae0e48d | 86 | assert(!dev || ISA_BUS(qdev_get_parent_bus(DEVICE(dev))) == isabus); |
3a38d437 | 87 | if (isairq < 0 || isairq > 15) { |
74782223 | 88 | hw_error("isa irq %d invalid", isairq); |
3a38d437 | 89 | } |
3a38d437 JS |
90 | return isabus->irqs[isairq]; |
91 | } | |
92 | ||
2e15e23b | 93 | void isa_init_irq(ISADevice *dev, qemu_irq *p, int isairq) |
f915a115 | 94 | { |
2e15e23b | 95 | assert(dev->nirqs < ARRAY_SIZE(dev->isairq)); |
2e15e23b | 96 | dev->isairq[dev->nirqs] = isairq; |
48a18b3c | 97 | *p = isa_get_irq(dev, isairq); |
f915a115 GH |
98 | dev->nirqs++; |
99 | } | |
100 | ||
25026303 EV |
101 | void isa_connect_gpio_out(ISADevice *isadev, int gpioirq, int isairq) |
102 | { | |
103 | qemu_irq irq; | |
104 | isa_init_irq(isadev, &irq, isairq); | |
105 | qdev_connect_gpio_out(DEVICE(isadev), gpioirq, irq); | |
106 | } | |
107 | ||
5484f30b HP |
108 | void isa_bus_dma(ISABus *bus, IsaDma *dma8, IsaDma *dma16) |
109 | { | |
110 | assert(bus && dma8 && dma16); | |
111 | assert(!bus->dma[0] && !bus->dma[1]); | |
112 | bus->dma[0] = dma8; | |
113 | bus->dma[1] = dma16; | |
114 | } | |
115 | ||
116 | IsaDma *isa_get_dma(ISABus *bus, int nchan) | |
117 | { | |
118 | assert(bus); | |
119 | return bus->dma[nchan > 3 ? 1 : 0]; | |
120 | } | |
121 | ||
0d959524 | 122 | static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport) |
dee41d58 | 123 | { |
0d959524 RH |
124 | if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) { |
125 | dev->ioport_id = ioport; | |
dee41d58 | 126 | } |
dee41d58 GN |
127 | } |
128 | ||
78e20593 RH |
129 | void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start) |
130 | { | |
131 | memory_region_add_subregion(isabus->address_space_io, start, io); | |
0d959524 | 132 | isa_init_ioport(dev, start); |
78e20593 RH |
133 | } |
134 | ||
e305a165 MAL |
135 | void isa_register_portio_list(ISADevice *dev, |
136 | PortioList *piolist, uint16_t start, | |
d7500734 AK |
137 | const MemoryRegionPortio *pio_start, |
138 | void *opaque, const char *name) | |
139 | { | |
e305a165 | 140 | assert(piolist && !piolist->owner); |
d7500734 AK |
141 | |
142 | /* START is how we should treat DEV, regardless of the actual | |
143 | contents of the portio array. This is how the old code | |
144 | actually handled e.g. the FDC device. */ | |
0d959524 | 145 | isa_init_ioport(dev, start); |
d7500734 | 146 | |
e305a165 MAL |
147 | portio_list_init(piolist, OBJECT(dev), pio_start, opaque, name); |
148 | portio_list_add(piolist, isabus->address_space_io, start); | |
d7500734 AK |
149 | } |
150 | ||
c538ca66 AF |
151 | static void isa_device_init(Object *obj) |
152 | { | |
153 | ISADevice *dev = ISA_DEVICE(obj); | |
154 | ||
155 | dev->isairq[0] = -1; | |
156 | dev->isairq[1] = -1; | |
157 | } | |
158 | ||
48a18b3c | 159 | ISADevice *isa_create(ISABus *bus, const char *name) |
f915a115 GH |
160 | { |
161 | DeviceState *dev; | |
f915a115 | 162 | |
2ae0e48d | 163 | dev = qdev_create(BUS(bus), name); |
8f04ee08 | 164 | return ISA_DEVICE(dev); |
f915a115 | 165 | } |
2091ba23 | 166 | |
48a18b3c | 167 | ISADevice *isa_try_create(ISABus *bus, const char *name) |
86f4a9a5 BS |
168 | { |
169 | DeviceState *dev; | |
170 | ||
2ae0e48d | 171 | dev = qdev_try_create(BUS(bus), name); |
8f04ee08 | 172 | return ISA_DEVICE(dev); |
86f4a9a5 BS |
173 | } |
174 | ||
48a18b3c | 175 | ISADevice *isa_create_simple(ISABus *bus, const char *name) |
924f6d72 GH |
176 | { |
177 | ISADevice *dev; | |
178 | ||
48a18b3c | 179 | dev = isa_create(bus, name); |
4a17cc4f | 180 | qdev_init_nofail(DEVICE(dev)); |
924f6d72 GH |
181 | return dev; |
182 | } | |
183 | ||
14e7a645 AJ |
184 | ISADevice *isa_vga_init(ISABus *bus) |
185 | { | |
186 | switch (vga_interface_type) { | |
187 | case VGA_CIRRUS: | |
188 | return isa_create_simple(bus, "isa-cirrus-vga"); | |
189 | case VGA_QXL: | |
1081ed2c | 190 | error_report("%s: qxl: no PCI bus", __func__); |
14e7a645 AJ |
191 | return NULL; |
192 | case VGA_STD: | |
193 | return isa_create_simple(bus, "isa-vga"); | |
194 | case VGA_VMWARE: | |
1081ed2c | 195 | error_report("%s: vmware_vga: no PCI bus", __func__); |
14e7a645 | 196 | return NULL; |
a94f0c5c | 197 | case VGA_VIRTIO: |
1081ed2c | 198 | error_report("%s: virtio-vga: no PCI bus", __func__); |
a94f0c5c | 199 | return NULL; |
14e7a645 AJ |
200 | case VGA_NONE: |
201 | default: | |
202 | return NULL; | |
203 | } | |
204 | } | |
205 | ||
2091ba23 GH |
206 | static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
207 | { | |
8f04ee08 | 208 | ISADevice *d = ISA_DEVICE(dev); |
2091ba23 GH |
209 | |
210 | if (d->isairq[1] != -1) { | |
211 | monitor_printf(mon, "%*sisa irqs %d,%d\n", indent, "", | |
212 | d->isairq[0], d->isairq[1]); | |
213 | } else if (d->isairq[0] != -1) { | |
214 | monitor_printf(mon, "%*sisa irq %d\n", indent, "", | |
215 | d->isairq[0]); | |
216 | } | |
217 | } | |
218 | ||
999e12bb AL |
219 | static void isabus_bridge_class_init(ObjectClass *klass, void *data) |
220 | { | |
39bffca2 | 221 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 222 | |
5658ffa3 | 223 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
39bffca2 | 224 | dc->fw_name = "isa"; |
999e12bb AL |
225 | } |
226 | ||
8c43a6f0 | 227 | static const TypeInfo isabus_bridge_info = { |
39bffca2 AL |
228 | .name = "isabus-bridge", |
229 | .parent = TYPE_SYS_BUS_DEVICE, | |
230 | .instance_size = sizeof(SysBusDevice), | |
231 | .class_init = isabus_bridge_class_init, | |
2091ba23 GH |
232 | }; |
233 | ||
39bffca2 AL |
234 | static void isa_device_class_init(ObjectClass *klass, void *data) |
235 | { | |
236 | DeviceClass *k = DEVICE_CLASS(klass); | |
0d936928 | 237 | k->bus_type = TYPE_ISA_BUS; |
39bffca2 AL |
238 | } |
239 | ||
8c43a6f0 | 240 | static const TypeInfo isa_device_type_info = { |
8f04ee08 AL |
241 | .name = TYPE_ISA_DEVICE, |
242 | .parent = TYPE_DEVICE, | |
243 | .instance_size = sizeof(ISADevice), | |
c538ca66 | 244 | .instance_init = isa_device_init, |
8f04ee08 AL |
245 | .abstract = true, |
246 | .class_size = sizeof(ISADeviceClass), | |
39bffca2 | 247 | .class_init = isa_device_class_init, |
8f04ee08 AL |
248 | }; |
249 | ||
83f7d43a | 250 | static void isabus_register_types(void) |
2091ba23 | 251 | { |
5484f30b | 252 | type_register_static(&isa_dma_info); |
0d936928 | 253 | type_register_static(&isa_bus_info); |
39bffca2 | 254 | type_register_static(&isabus_bridge_info); |
8f04ee08 | 255 | type_register_static(&isa_device_type_info); |
2091ba23 GH |
256 | } |
257 | ||
6a26e119 GN |
258 | static char *isabus_get_fw_dev_path(DeviceState *dev) |
259 | { | |
4a17cc4f | 260 | ISADevice *d = ISA_DEVICE(dev); |
6a26e119 GN |
261 | char path[40]; |
262 | int off; | |
263 | ||
264 | off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); | |
ebf47c24 RH |
265 | if (d->ioport_id) { |
266 | snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id); | |
6a26e119 GN |
267 | } |
268 | ||
a5cf8262 | 269 | return g_strdup(path); |
6a26e119 GN |
270 | } |
271 | ||
c839adec AK |
272 | MemoryRegion *isa_address_space(ISADevice *dev) |
273 | { | |
bb2ed009 HP |
274 | if (dev) { |
275 | return isa_bus_from_device(dev)->address_space; | |
276 | } | |
277 | ||
278 | return isabus->address_space; | |
c839adec AK |
279 | } |
280 | ||
ac100273 JG |
281 | MemoryRegion *isa_address_space_io(ISADevice *dev) |
282 | { | |
283 | if (dev) { | |
284 | return isa_bus_from_device(dev)->address_space_io; | |
285 | } | |
286 | ||
287 | return isabus->address_space_io; | |
288 | } | |
289 | ||
83f7d43a | 290 | type_init(isabus_register_types) |
9157eee1 | 291 | |
0ec7b3e7 | 292 | static void parallel_init(ISABus *bus, int index, Chardev *chr) |
9157eee1 MR |
293 | { |
294 | DeviceState *dev; | |
295 | ISADevice *isadev; | |
296 | ||
297 | isadev = isa_create(bus, "isa-parallel"); | |
298 | dev = DEVICE(isadev); | |
299 | qdev_prop_set_uint32(dev, "index", index); | |
300 | qdev_prop_set_chr(dev, "chardev", chr); | |
301 | qdev_init_nofail(dev); | |
302 | } | |
303 | ||
304 | void parallel_hds_isa_init(ISABus *bus, int n) | |
305 | { | |
306 | int i; | |
307 | ||
308 | assert(n <= MAX_PARALLEL_PORTS); | |
309 | ||
310 | for (i = 0; i < n; i++) { | |
311 | if (parallel_hds[i]) { | |
312 | parallel_init(bus, i, parallel_hds[i]); | |
313 | } | |
314 | } | |
315 | } |