xref: /openbmc/qemu/hw/isa/isa-bus.c (revision dc8d6cf2033c813ade9863a926f2d71a22edd249)
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.1 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/sysbus.h"
25 #include "sysemu/sysemu.h"
26 #include "hw/isa/isa.h"
27 
28 static ISABus *isabus;
29 
30 static char *isabus_get_fw_dev_path(DeviceState *dev);
31 
32 static void isa_bus_class_init(ObjectClass *klass, void *data)
33 {
34     BusClass *k = BUS_CLASS(klass);
35 
36     k->get_fw_dev_path = isabus_get_fw_dev_path;
37 }
38 
39 static const TypeInfo isa_dma_info = {
40     .name = TYPE_ISADMA,
41     .parent = TYPE_INTERFACE,
42     .class_size = sizeof(IsaDmaClass),
43 };
44 
45 static const TypeInfo isa_bus_info = {
46     .name = TYPE_ISA_BUS,
47     .parent = TYPE_BUS,
48     .instance_size = sizeof(ISABus),
49     .class_init = isa_bus_class_init,
50 };
51 
52 ISABus *isa_bus_new(DeviceState *dev, MemoryRegion* address_space,
53                     MemoryRegion *address_space_io, Error **errp)
54 {
55     if (isabus) {
56         error_setg(errp, "Can't create a second ISA bus");
57         return NULL;
58     }
59     if (!dev) {
60         dev = qdev_new("isabus-bridge");
61         sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
62     }
63 
64     isabus = ISA_BUS(qbus_new(TYPE_ISA_BUS, dev, NULL));
65     isabus->address_space = address_space;
66     isabus->address_space_io = address_space_io;
67     return isabus;
68 }
69 
70 void isa_bus_register_input_irqs(ISABus *bus, qemu_irq *irqs_in)
71 {
72     bus->irqs_in = irqs_in;
73 }
74 
75 /*
76  * isa_get_irq() returns the corresponding input qemu_irq entry for the i8259.
77  *
78  * This function is only for special cases such as the 'ferr', and
79  * temporary use for normal devices until they are converted to qdev.
80  */
81 qemu_irq isa_get_irq(ISADevice *dev, unsigned isairq)
82 {
83     assert(!dev || ISA_BUS(qdev_get_parent_bus(DEVICE(dev))) == isabus);
84     assert(isairq < ISA_NUM_IRQS);
85     return isabus->irqs_in[isairq];
86 }
87 
88 void isa_connect_gpio_out(ISADevice *isadev, int gpioirq, unsigned isairq)
89 {
90     qemu_irq irq = isa_get_irq(isadev, isairq);
91     qdev_connect_gpio_out(DEVICE(isadev), gpioirq, irq);
92 }
93 
94 void isa_bus_dma(ISABus *bus, IsaDma *dma8, IsaDma *dma16)
95 {
96     assert(bus && dma8 && dma16);
97     assert(!bus->dma[0] && !bus->dma[1]);
98     bus->dma[0] = dma8;
99     bus->dma[1] = dma16;
100 }
101 
102 IsaDma *isa_bus_get_dma(ISABus *bus, int nchan)
103 {
104     assert(bus);
105     return bus->dma[nchan > 3 ? 1 : 0];
106 }
107 
108 static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport)
109 {
110     if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) {
111         dev->ioport_id = ioport;
112     }
113 }
114 
115 void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start)
116 {
117     memory_region_add_subregion(isa_address_space_io(dev), start, io);
118     isa_init_ioport(dev, start);
119 }
120 
121 int isa_register_portio_list(ISADevice *dev,
122                              PortioList *piolist, uint16_t start,
123                              const MemoryRegionPortio *pio_start,
124                              void *opaque, const char *name)
125 {
126     assert(piolist && !piolist->owner);
127 
128     if (!isabus) {
129         return -ENODEV;
130     }
131 
132     /* START is how we should treat DEV, regardless of the actual
133        contents of the portio array.  This is how the old code
134        actually handled e.g. the FDC device.  */
135     isa_init_ioport(dev, start);
136 
137     portio_list_init(piolist, OBJECT(dev), pio_start, opaque, name);
138     portio_list_add(piolist, isa_address_space_io(dev), start);
139 
140     return 0;
141 }
142 
143 ISADevice *isa_new(const char *name)
144 {
145     return ISA_DEVICE(qdev_new(name));
146 }
147 
148 ISADevice *isa_try_new(const char *name)
149 {
150     return ISA_DEVICE(qdev_try_new(name));
151 }
152 
153 ISADevice *isa_create_simple(ISABus *bus, const char *name)
154 {
155     ISADevice *dev;
156 
157     dev = isa_new(name);
158     isa_realize_and_unref(dev, bus, &error_fatal);
159     return dev;
160 }
161 
162 bool isa_realize_and_unref(ISADevice *dev, ISABus *bus, Error **errp)
163 {
164     return qdev_realize_and_unref(&dev->parent_obj, &bus->parent_obj, errp);
165 }
166 
167 ISABus *isa_bus_from_device(ISADevice *dev)
168 {
169     return ISA_BUS(qdev_get_parent_bus(DEVICE(dev)));
170 }
171 
172 ISADevice *isa_vga_init(ISABus *bus)
173 {
174     vga_interface_created = true;
175     switch (vga_interface_type) {
176     case VGA_CIRRUS:
177         return isa_create_simple(bus, "isa-cirrus-vga");
178     case VGA_QXL:
179         error_report("%s: qxl: no PCI bus", __func__);
180         return NULL;
181     case VGA_STD:
182         return isa_create_simple(bus, "isa-vga");
183     case VGA_VMWARE:
184         error_report("%s: vmware_vga: no PCI bus", __func__);
185         return NULL;
186     case VGA_VIRTIO:
187         error_report("%s: virtio-vga: no PCI bus", __func__);
188         return NULL;
189     case VGA_NONE:
190     default:
191         return NULL;
192     }
193 }
194 
195 static void isabus_bridge_class_init(ObjectClass *klass, void *data)
196 {
197     DeviceClass *dc = DEVICE_CLASS(klass);
198 
199     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
200     dc->fw_name = "isa";
201 }
202 
203 static const TypeInfo isabus_bridge_info = {
204     .name          = "isabus-bridge",
205     .parent        = TYPE_SYS_BUS_DEVICE,
206     .instance_size = sizeof(SysBusDevice),
207     .class_init    = isabus_bridge_class_init,
208 };
209 
210 static void isa_device_class_init(ObjectClass *klass, void *data)
211 {
212     DeviceClass *k = DEVICE_CLASS(klass);
213     k->bus_type = TYPE_ISA_BUS;
214 }
215 
216 static const TypeInfo isa_device_type_info = {
217     .name = TYPE_ISA_DEVICE,
218     .parent = TYPE_DEVICE,
219     .instance_size = sizeof(ISADevice),
220     .abstract = true,
221     .class_init = isa_device_class_init,
222 };
223 
224 static void isabus_register_types(void)
225 {
226     type_register_static(&isa_dma_info);
227     type_register_static(&isa_bus_info);
228     type_register_static(&isabus_bridge_info);
229     type_register_static(&isa_device_type_info);
230 }
231 
232 static char *isabus_get_fw_dev_path(DeviceState *dev)
233 {
234     ISADevice *d = ISA_DEVICE(dev);
235     char path[40];
236     int off;
237 
238     off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
239     if (d->ioport_id) {
240         snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id);
241     }
242 
243     return g_strdup(path);
244 }
245 
246 MemoryRegion *isa_address_space(ISADevice *dev)
247 {
248     if (dev) {
249         return isa_bus_from_device(dev)->address_space;
250     }
251 
252     return isabus->address_space;
253 }
254 
255 MemoryRegion *isa_address_space_io(ISADevice *dev)
256 {
257     if (dev) {
258         return isa_bus_from_device(dev)->address_space_io;
259     }
260 
261     return isabus->address_space_io;
262 }
263 
264 type_init(isabus_register_types)
265