xref: /openbmc/qemu/hw/isa/isa-bus.c (revision dc5bd18f)
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 #include "qemu/osdep.h"
20 #include "qemu/error-report.h"
21 #include "qapi/error.h"
22 #include "hw/hw.h"
23 #include "monitor/monitor.h"
24 #include "hw/sysbus.h"
25 #include "sysemu/sysemu.h"
26 #include "hw/isa/isa.h"
27 #include "hw/i386/pc.h"
28 
29 static ISABus *isabus;
30 
31 static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent);
32 static char *isabus_get_fw_dev_path(DeviceState *dev);
33 
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 
42 static const TypeInfo isa_dma_info = {
43     .name = TYPE_ISADMA,
44     .parent = TYPE_INTERFACE,
45     .class_size = sizeof(IsaDmaClass),
46 };
47 
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,
53 };
54 
55 ISABus *isa_bus_new(DeviceState *dev, MemoryRegion* address_space,
56                     MemoryRegion *address_space_io, Error **errp)
57 {
58     if (isabus) {
59         error_setg(errp, "Can't create a second ISA bus");
60         return NULL;
61     }
62     if (!dev) {
63         dev = qdev_create(NULL, "isabus-bridge");
64         qdev_init_nofail(dev);
65     }
66 
67     isabus = ISA_BUS(qbus_create(TYPE_ISA_BUS, dev, NULL));
68     isabus->address_space = address_space;
69     isabus->address_space_io = address_space_io;
70     return isabus;
71 }
72 
73 void isa_bus_irqs(ISABus *bus, qemu_irq *irqs)
74 {
75     bus->irqs = irqs;
76 }
77 
78 /*
79  * isa_get_irq() returns the corresponding qemu_irq entry for the i8259.
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  */
84 qemu_irq isa_get_irq(ISADevice *dev, int isairq)
85 {
86     assert(!dev || ISA_BUS(qdev_get_parent_bus(DEVICE(dev))) == isabus);
87     if (isairq < 0 || isairq > 15) {
88         hw_error("isa irq %d invalid", isairq);
89     }
90     return isabus->irqs[isairq];
91 }
92 
93 void isa_init_irq(ISADevice *dev, qemu_irq *p, int isairq)
94 {
95     assert(dev->nirqs < ARRAY_SIZE(dev->isairq));
96     dev->isairq[dev->nirqs] = isairq;
97     *p = isa_get_irq(dev, isairq);
98     dev->nirqs++;
99 }
100 
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 
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 
122 static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport)
123 {
124     if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) {
125         dev->ioport_id = ioport;
126     }
127 }
128 
129 void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start)
130 {
131     memory_region_add_subregion(isabus->address_space_io, start, io);
132     isa_init_ioport(dev, start);
133 }
134 
135 void isa_register_portio_list(ISADevice *dev,
136                               PortioList *piolist, uint16_t start,
137                               const MemoryRegionPortio *pio_start,
138                               void *opaque, const char *name)
139 {
140     assert(piolist && !piolist->owner);
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.  */
145     isa_init_ioport(dev, start);
146 
147     portio_list_init(piolist, OBJECT(dev), pio_start, opaque, name);
148     portio_list_add(piolist, isabus->address_space_io, start);
149 }
150 
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 
159 ISADevice *isa_create(ISABus *bus, const char *name)
160 {
161     DeviceState *dev;
162 
163     dev = qdev_create(BUS(bus), name);
164     return ISA_DEVICE(dev);
165 }
166 
167 ISADevice *isa_try_create(ISABus *bus, const char *name)
168 {
169     DeviceState *dev;
170 
171     dev = qdev_try_create(BUS(bus), name);
172     return ISA_DEVICE(dev);
173 }
174 
175 ISADevice *isa_create_simple(ISABus *bus, const char *name)
176 {
177     ISADevice *dev;
178 
179     dev = isa_create(bus, name);
180     qdev_init_nofail(DEVICE(dev));
181     return dev;
182 }
183 
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:
190         error_report("%s: qxl: no PCI bus", __func__);
191         return NULL;
192     case VGA_STD:
193         return isa_create_simple(bus, "isa-vga");
194     case VGA_VMWARE:
195         error_report("%s: vmware_vga: no PCI bus", __func__);
196         return NULL;
197     case VGA_VIRTIO:
198         error_report("%s: virtio-vga: no PCI bus", __func__);
199         return NULL;
200     case VGA_NONE:
201     default:
202         return NULL;
203     }
204 }
205 
206 static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent)
207 {
208     ISADevice *d = ISA_DEVICE(dev);
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 
219 static void isabus_bridge_class_init(ObjectClass *klass, void *data)
220 {
221     DeviceClass *dc = DEVICE_CLASS(klass);
222 
223     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
224     dc->fw_name = "isa";
225 }
226 
227 static const TypeInfo isabus_bridge_info = {
228     .name          = "isabus-bridge",
229     .parent        = TYPE_SYS_BUS_DEVICE,
230     .instance_size = sizeof(SysBusDevice),
231     .class_init    = isabus_bridge_class_init,
232 };
233 
234 static void isa_device_class_init(ObjectClass *klass, void *data)
235 {
236     DeviceClass *k = DEVICE_CLASS(klass);
237     k->bus_type = TYPE_ISA_BUS;
238 }
239 
240 static const TypeInfo isa_device_type_info = {
241     .name = TYPE_ISA_DEVICE,
242     .parent = TYPE_DEVICE,
243     .instance_size = sizeof(ISADevice),
244     .instance_init = isa_device_init,
245     .abstract = true,
246     .class_size = sizeof(ISADeviceClass),
247     .class_init = isa_device_class_init,
248 };
249 
250 static void isabus_register_types(void)
251 {
252     type_register_static(&isa_dma_info);
253     type_register_static(&isa_bus_info);
254     type_register_static(&isabus_bridge_info);
255     type_register_static(&isa_device_type_info);
256 }
257 
258 static char *isabus_get_fw_dev_path(DeviceState *dev)
259 {
260     ISADevice *d = ISA_DEVICE(dev);
261     char path[40];
262     int off;
263 
264     off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
265     if (d->ioport_id) {
266         snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id);
267     }
268 
269     return g_strdup(path);
270 }
271 
272 MemoryRegion *isa_address_space(ISADevice *dev)
273 {
274     if (dev) {
275         return isa_bus_from_device(dev)->address_space;
276     }
277 
278     return isabus->address_space;
279 }
280 
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 
290 type_init(isabus_register_types)
291 
292 static void parallel_init(ISABus *bus, int index, Chardev *chr)
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 }
316