xref: /qemu/hw/isa/isa-bus.c (revision 96927c744f9cd5205f95ef897c35695038dc2896)
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  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/error-report.h"
22 #include "qemu/module.h"
23 #include "qapi/error.h"
24 #include "hw/hw.h"
25 #include "monitor/monitor.h"
26 #include "hw/sysbus.h"
27 #include "sysemu/sysemu.h"
28 #include "hw/isa/isa.h"
29 
30 static ISABus *isabus;
31 
32 static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent);
33 static char *isabus_get_fw_dev_path(DeviceState *dev);
34 
35 static void isa_bus_class_init(ObjectClass *klass, void *data)
36 {
37     BusClass *k = BUS_CLASS(klass);
38 
39     k->print_dev = isabus_dev_print;
40     k->get_fw_dev_path = isabus_get_fw_dev_path;
41 }
42 
43 static const TypeInfo isa_dma_info = {
44     .name = TYPE_ISADMA,
45     .parent = TYPE_INTERFACE,
46     .class_size = sizeof(IsaDmaClass),
47 };
48 
49 static const TypeInfo isa_bus_info = {
50     .name = TYPE_ISA_BUS,
51     .parent = TYPE_BUS,
52     .instance_size = sizeof(ISABus),
53     .class_init = isa_bus_class_init,
54 };
55 
56 ISABus *isa_bus_new(DeviceState *dev, MemoryRegion* address_space,
57                     MemoryRegion *address_space_io, Error **errp)
58 {
59     if (isabus) {
60         error_setg(errp, "Can't create a second ISA bus");
61         return NULL;
62     }
63     if (!dev) {
64         dev = qdev_new("isabus-bridge");
65         qdev_realize_and_unref(dev, NULL, &error_fatal);
66     }
67 
68     isabus = ISA_BUS(qbus_create(TYPE_ISA_BUS, dev, NULL));
69     isabus->address_space = address_space;
70     isabus->address_space_io = address_space_io;
71     return isabus;
72 }
73 
74 void isa_bus_irqs(ISABus *bus, qemu_irq *irqs)
75 {
76     bus->irqs = irqs;
77 }
78 
79 /*
80  * isa_get_irq() returns the corresponding qemu_irq entry for the i8259.
81  *
82  * This function is only for special cases such as the 'ferr', and
83  * temporary use for normal devices until they are converted to qdev.
84  */
85 qemu_irq isa_get_irq(ISADevice *dev, unsigned isairq)
86 {
87     assert(!dev || ISA_BUS(qdev_get_parent_bus(DEVICE(dev))) == isabus);
88     if (isairq >= ISA_NUM_IRQS) {
89         hw_error("isa irq %d invalid", isairq);
90     }
91     return isabus->irqs[isairq];
92 }
93 
94 void isa_init_irq(ISADevice *dev, qemu_irq *p, unsigned isairq)
95 {
96     assert(dev->nirqs < ARRAY_SIZE(dev->isairq));
97     if (isairq >= ISA_NUM_IRQS) {
98         hw_error("isa irq %d invalid", isairq);
99     }
100     dev->isairq[dev->nirqs] = isairq;
101     *p = isa_get_irq(dev, isairq);
102     dev->nirqs++;
103 }
104 
105 void isa_connect_gpio_out(ISADevice *isadev, int gpioirq, unsigned isairq)
106 {
107     qemu_irq irq;
108     isa_init_irq(isadev, &irq, isairq);
109     qdev_connect_gpio_out(DEVICE(isadev), gpioirq, irq);
110 }
111 
112 void isa_bus_dma(ISABus *bus, IsaDma *dma8, IsaDma *dma16)
113 {
114     assert(bus && dma8 && dma16);
115     assert(!bus->dma[0] && !bus->dma[1]);
116     bus->dma[0] = dma8;
117     bus->dma[1] = dma16;
118 }
119 
120 IsaDma *isa_get_dma(ISABus *bus, int nchan)
121 {
122     assert(bus);
123     return bus->dma[nchan > 3 ? 1 : 0];
124 }
125 
126 static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport)
127 {
128     if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) {
129         dev->ioport_id = ioport;
130     }
131 }
132 
133 void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start)
134 {
135     memory_region_add_subregion(isabus->address_space_io, start, io);
136     isa_init_ioport(dev, start);
137 }
138 
139 void isa_register_portio_list(ISADevice *dev,
140                               PortioList *piolist, uint16_t start,
141                               const MemoryRegionPortio *pio_start,
142                               void *opaque, const char *name)
143 {
144     assert(piolist && !piolist->owner);
145 
146     /* START is how we should treat DEV, regardless of the actual
147        contents of the portio array.  This is how the old code
148        actually handled e.g. the FDC device.  */
149     isa_init_ioport(dev, start);
150 
151     portio_list_init(piolist, OBJECT(dev), pio_start, opaque, name);
152     portio_list_add(piolist, isabus->address_space_io, start);
153 }
154 
155 static void isa_device_init(Object *obj)
156 {
157     ISADevice *dev = ISA_DEVICE(obj);
158 
159     dev->isairq[0] = -1;
160     dev->isairq[1] = -1;
161 }
162 
163 ISADevice *isa_create(ISABus *bus, const char *name)
164 {
165     DeviceState *dev;
166 
167     dev = qdev_create(BUS(bus), name);
168     return ISA_DEVICE(dev);
169 }
170 
171 ISADevice *isa_try_create(ISABus *bus, const char *name)
172 {
173     DeviceState *dev;
174 
175     dev = qdev_try_create(BUS(bus), name);
176     return ISA_DEVICE(dev);
177 }
178 
179 ISADevice *isa_new(const char *name)
180 {
181     return ISA_DEVICE(qdev_new(name));
182 }
183 
184 ISADevice *isa_try_new(const char *name)
185 {
186     return ISA_DEVICE(qdev_try_new(name));
187 }
188 
189 ISADevice *isa_create_simple(ISABus *bus, const char *name)
190 {
191     ISADevice *dev;
192 
193     dev = isa_new(name);
194     isa_realize_and_unref(dev, bus, &error_fatal);
195     return dev;
196 }
197 
198 bool isa_realize_and_unref(ISADevice *dev, ISABus *bus, Error **errp)
199 {
200     return qdev_realize_and_unref(&dev->parent_obj, &bus->parent_obj, errp);
201 }
202 
203 ISADevice *isa_vga_init(ISABus *bus)
204 {
205     switch (vga_interface_type) {
206     case VGA_CIRRUS:
207         return isa_create_simple(bus, "isa-cirrus-vga");
208     case VGA_QXL:
209         error_report("%s: qxl: no PCI bus", __func__);
210         return NULL;
211     case VGA_STD:
212         return isa_create_simple(bus, "isa-vga");
213     case VGA_VMWARE:
214         error_report("%s: vmware_vga: no PCI bus", __func__);
215         return NULL;
216     case VGA_VIRTIO:
217         error_report("%s: virtio-vga: no PCI bus", __func__);
218         return NULL;
219     case VGA_NONE:
220     default:
221         return NULL;
222     }
223 }
224 
225 void isa_build_aml(ISABus *bus, Aml *scope)
226 {
227     BusChild *kid;
228     ISADevice *dev;
229     ISADeviceClass *dc;
230 
231     QTAILQ_FOREACH(kid, &bus->parent_obj.children, sibling) {
232         dev = ISA_DEVICE(kid->child);
233         dc = ISA_DEVICE_GET_CLASS(dev);
234         if (dc->build_aml) {
235             dc->build_aml(dev, scope);
236         }
237     }
238 }
239 
240 static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent)
241 {
242     ISADevice *d = ISA_DEVICE(dev);
243 
244     if (d->isairq[1] != -1) {
245         monitor_printf(mon, "%*sisa irqs %d,%d\n", indent, "",
246                        d->isairq[0], d->isairq[1]);
247     } else if (d->isairq[0] != -1) {
248         monitor_printf(mon, "%*sisa irq %d\n", indent, "",
249                        d->isairq[0]);
250     }
251 }
252 
253 static void isabus_bridge_class_init(ObjectClass *klass, void *data)
254 {
255     DeviceClass *dc = DEVICE_CLASS(klass);
256 
257     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
258     dc->fw_name = "isa";
259 }
260 
261 static const TypeInfo isabus_bridge_info = {
262     .name          = "isabus-bridge",
263     .parent        = TYPE_SYS_BUS_DEVICE,
264     .instance_size = sizeof(SysBusDevice),
265     .class_init    = isabus_bridge_class_init,
266 };
267 
268 static void isa_device_class_init(ObjectClass *klass, void *data)
269 {
270     DeviceClass *k = DEVICE_CLASS(klass);
271     k->bus_type = TYPE_ISA_BUS;
272 }
273 
274 static const TypeInfo isa_device_type_info = {
275     .name = TYPE_ISA_DEVICE,
276     .parent = TYPE_DEVICE,
277     .instance_size = sizeof(ISADevice),
278     .instance_init = isa_device_init,
279     .abstract = true,
280     .class_size = sizeof(ISADeviceClass),
281     .class_init = isa_device_class_init,
282 };
283 
284 static void isabus_register_types(void)
285 {
286     type_register_static(&isa_dma_info);
287     type_register_static(&isa_bus_info);
288     type_register_static(&isabus_bridge_info);
289     type_register_static(&isa_device_type_info);
290 }
291 
292 static char *isabus_get_fw_dev_path(DeviceState *dev)
293 {
294     ISADevice *d = ISA_DEVICE(dev);
295     char path[40];
296     int off;
297 
298     off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
299     if (d->ioport_id) {
300         snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id);
301     }
302 
303     return g_strdup(path);
304 }
305 
306 MemoryRegion *isa_address_space(ISADevice *dev)
307 {
308     if (dev) {
309         return isa_bus_from_device(dev)->address_space;
310     }
311 
312     return isabus->address_space;
313 }
314 
315 MemoryRegion *isa_address_space_io(ISADevice *dev)
316 {
317     if (dev) {
318         return isa_bus_from_device(dev)->address_space_io;
319     }
320 
321     return isabus->address_space_io;
322 }
323 
324 type_init(isabus_register_types)
325