blob: f1e0f140078fb7673c748b51a16cde3f50088fa7 [file] [log] [blame]
Gerd Hoffmannf915a112009-07-31 12:30:14 +02001/*
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
Chetan Pant61f3c912020-10-23 12:44:24 +00009 * version 2.1 of the License, or (at your option) any later version.
Gerd Hoffmannf915a112009-07-31 12:30:14 +020010 *
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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020019
Peter Maydell04308912016-01-26 18:17:30 +000020#include "qemu/osdep.h"
Alistair Francis1081ed22017-11-08 14:57:30 -080021#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020022#include "qemu/module.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010023#include "qapi/error.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010024#include "hw/sysbus.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/sysemu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010026#include "hw/isa/isa.h"
Gerd Hoffmannf915a112009-07-31 12:30:14 +020027
Gerd Hoffmannf915a112009-07-31 12:30:14 +020028static ISABus *isabus;
29
Gleb Natapov6a26e112010-12-08 13:34:57 +020030static char *isabus_get_fw_dev_path(DeviceState *dev);
Gerd Hoffmann2091ba22009-08-14 11:36:14 +020031
Anthony Liguori0d936922012-05-02 09:00:20 +020032static void isa_bus_class_init(ObjectClass *klass, void *data)
33{
34 BusClass *k = BUS_CLASS(klass);
35
Anthony Liguori0d936922012-05-02 09:00:20 +020036 k->get_fw_dev_path = isabus_get_fw_dev_path;
37}
38
Hervé Poussineau5484f302016-02-03 11:28:57 -050039static const TypeInfo isa_dma_info = {
40 .name = TYPE_ISADMA,
41 .parent = TYPE_INTERFACE,
42 .class_size = sizeof(IsaDmaClass),
43};
44
Anthony Liguori0d936922012-05-02 09:00:20 +020045static 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,
Gerd Hoffmannf915a112009-07-31 12:30:14 +020050};
51
Hervé Poussineaubb2ed002015-02-01 09:12:50 +010052ISABus *isa_bus_new(DeviceState *dev, MemoryRegion* address_space,
Markus Armbrusterd10e5432015-12-17 17:35:18 +010053 MemoryRegion *address_space_io, Error **errp)
Gerd Hoffmannf915a112009-07-31 12:30:14 +020054{
Philippe Mathieu-Daudé675d7172023-10-18 14:41:33 +020055 DeviceState *bridge = NULL;
56
Gerd Hoffmannf915a112009-07-31 12:30:14 +020057 if (isabus) {
Markus Armbrusterd10e5432015-12-17 17:35:18 +010058 error_setg(errp, "Can't create a second ISA bus");
Gerd Hoffmannf915a112009-07-31 12:30:14 +020059 return NULL;
60 }
Gonglei337a3e5c2014-08-11 21:00:54 +080061 if (!dev) {
Philippe Mathieu-Daudé675d7172023-10-18 14:41:33 +020062 bridge = qdev_new("isabus-bridge");
63 dev = bridge;
Gerd Hoffmann2091ba22009-08-14 11:36:14 +020064 }
Gerd Hoffmannf915a112009-07-31 12:30:14 +020065
Peter Maydell9388d172021-09-23 13:11:52 +010066 isabus = ISA_BUS(qbus_new(TYPE_ISA_BUS, dev, NULL));
Hervé Poussineaubb2ed002015-02-01 09:12:50 +010067 isabus->address_space = address_space;
Richard Hendersonc2d0d012011-08-10 15:28:11 -070068 isabus->address_space_io = address_space_io;
Philippe Mathieu-Daudé675d7172023-10-18 14:41:33 +020069
70 if (bridge) {
71 sysbus_realize_and_unref(SYS_BUS_DEVICE(bridge), &error_fatal);
72 }
73
Gerd Hoffmannf915a112009-07-31 12:30:14 +020074 return isabus;
75}
76
Philippe Mathieu-Daudé70678872023-02-09 13:32:18 +010077void isa_bus_register_input_irqs(ISABus *bus, qemu_irq *irqs_in)
Gerd Hoffmannf915a112009-07-31 12:30:14 +020078{
Philippe Mathieu-Daudé70678872023-02-09 13:32:18 +010079 bus->irqs_in = irqs_in;
Gerd Hoffmann2091ba22009-08-14 11:36:14 +020080}
81
Philippe Mathieu-Daudéd2fbec52023-02-09 11:55:03 +010082qemu_irq isa_bus_get_irq(ISABus *bus, unsigned irqnum)
83{
84 assert(irqnum < ISA_NUM_IRQS);
85 assert(bus->irqs_in);
86 return bus->irqs_in[irqnum];
87}
88
Jes Sorensen3a38d432009-08-14 11:36:15 +020089/*
Philippe Mathieu-Daudé70678872023-02-09 13:32:18 +010090 * isa_get_irq() returns the corresponding input qemu_irq entry for the i8259.
Jes Sorensen3a38d432009-08-14 11:36:15 +020091 *
92 * This function is only for special cases such as the 'ferr', and
93 * temporary use for normal devices until they are converted to qdev.
94 */
Paolo Bonzini3c29e182019-12-13 13:00:43 +010095qemu_irq isa_get_irq(ISADevice *dev, unsigned isairq)
Jes Sorensen3a38d432009-08-14 11:36:15 +020096{
Andreas Färber2ae0e482013-06-07 14:11:07 +020097 assert(!dev || ISA_BUS(qdev_get_parent_bus(DEVICE(dev))) == isabus);
Philippe Mathieu-Daudéd2fbec52023-02-09 11:55:03 +010098 return isa_bus_get_irq(isabus, isairq);
Jes Sorensen3a38d432009-08-14 11:36:15 +020099}
100
Paolo Bonzini3c29e182019-12-13 13:00:43 +0100101void isa_connect_gpio_out(ISADevice *isadev, int gpioirq, unsigned isairq)
Efimov Vasily25026302016-06-22 15:24:56 +0300102{
Philippe Mathieu-Daudéd2fbec52023-02-09 11:55:03 +0100103 qemu_irq input_irq = isa_get_irq(isadev, isairq);
104 qdev_connect_gpio_out(DEVICE(isadev), gpioirq, input_irq);
Efimov Vasily25026302016-06-22 15:24:56 +0300105}
106
Hervé Poussineau5484f302016-02-03 11:28:57 -0500107void isa_bus_dma(ISABus *bus, IsaDma *dma8, IsaDma *dma16)
108{
109 assert(bus && dma8 && dma16);
110 assert(!bus->dma[0] && !bus->dma[1]);
111 bus->dma[0] = dma8;
112 bus->dma[1] = dma16;
113}
114
Philippe Mathieu-Daudédc8d6cf2023-02-15 15:38:19 +0100115IsaDma *isa_bus_get_dma(ISABus *bus, int nchan)
Hervé Poussineau5484f302016-02-03 11:28:57 -0500116{
117 assert(bus);
118 return bus->dma[nchan > 3 ? 1 : 0];
119}
120
Richard Henderson0d959522011-08-16 09:02:36 -0700121static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport)
Gleb Natapovdee41d52010-12-08 13:34:56 +0200122{
Richard Henderson0d959522011-08-16 09:02:36 -0700123 if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) {
124 dev->ioport_id = ioport;
Gleb Natapovdee41d52010-12-08 13:34:56 +0200125 }
Gleb Natapovdee41d52010-12-08 13:34:56 +0200126}
127
Richard Henderson78e20592011-08-10 15:28:12 -0700128void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start)
129{
Philippe Mathieu-Daudée5bf2772023-01-20 12:45:54 +0100130 memory_region_add_subregion(isa_address_space_io(dev), start, io);
Richard Henderson0d959522011-08-16 09:02:36 -0700131 isa_init_ioport(dev, start);
Richard Henderson78e20592011-08-10 15:28:12 -0700132}
133
Thomas Huth9405d872021-04-16 14:52:56 +0200134int isa_register_portio_list(ISADevice *dev,
135 PortioList *piolist, uint16_t start,
136 const MemoryRegionPortio *pio_start,
137 void *opaque, const char *name)
Avi Kivityd7500732011-09-26 14:52:44 +0300138{
Marc-André Lureaue305a162016-07-13 02:11:59 +0200139 assert(piolist && !piolist->owner);
Avi Kivityd7500732011-09-26 14:52:44 +0300140
Thomas Huth9405d872021-04-16 14:52:56 +0200141 if (!isabus) {
142 return -ENODEV;
143 }
144
Avi Kivityd7500732011-09-26 14:52:44 +0300145 /* START is how we should treat DEV, regardless of the actual
146 contents of the portio array. This is how the old code
147 actually handled e.g. the FDC device. */
Richard Henderson0d959522011-08-16 09:02:36 -0700148 isa_init_ioport(dev, start);
Avi Kivityd7500732011-09-26 14:52:44 +0300149
Marc-André Lureaue305a162016-07-13 02:11:59 +0200150 portio_list_init(piolist, OBJECT(dev), pio_start, opaque, name);
Philippe Mathieu-Daudée5bf2772023-01-20 12:45:54 +0100151 portio_list_add(piolist, isa_address_space_io(dev), start);
Thomas Huth9405d872021-04-16 14:52:56 +0200152
153 return 0;
Avi Kivityd7500732011-09-26 14:52:44 +0300154}
155
Markus Armbruster0fe9d902020-06-10 07:32:07 +0200156ISADevice *isa_new(const char *name)
157{
158 return ISA_DEVICE(qdev_new(name));
159}
160
161ISADevice *isa_try_new(const char *name)
162{
163 return ISA_DEVICE(qdev_try_new(name));
164}
165
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100166ISADevice *isa_create_simple(ISABus *bus, const char *name)
Gerd Hoffmann924f6d72009-09-10 11:43:29 +0200167{
168 ISADevice *dev;
169
Markus Armbruster96927c72020-06-10 07:32:08 +0200170 dev = isa_new(name);
171 isa_realize_and_unref(dev, bus, &error_fatal);
Gerd Hoffmann924f6d72009-09-10 11:43:29 +0200172 return dev;
173}
174
Markus Armbruster0fe9d902020-06-10 07:32:07 +0200175bool isa_realize_and_unref(ISADevice *dev, ISABus *bus, Error **errp)
176{
177 return qdev_realize_and_unref(&dev->parent_obj, &bus->parent_obj, errp);
178}
179
Philippe Mathieu-Daudé23c69bb2023-02-07 22:47:20 +0100180ISABus *isa_bus_from_device(ISADevice *dev)
181{
182 return ISA_BUS(qdev_get_parent_bus(DEVICE(dev)));
183}
184
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200185ISADevice *isa_vga_init(ISABus *bus)
186{
Gautam Agrawalf9bcb2d2022-05-01 17:55:05 +0530187 vga_interface_created = true;
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200188 switch (vga_interface_type) {
189 case VGA_CIRRUS:
190 return isa_create_simple(bus, "isa-cirrus-vga");
191 case VGA_QXL:
Alistair Francis1081ed22017-11-08 14:57:30 -0800192 error_report("%s: qxl: no PCI bus", __func__);
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200193 return NULL;
194 case VGA_STD:
195 return isa_create_simple(bus, "isa-vga");
196 case VGA_VMWARE:
Alistair Francis1081ed22017-11-08 14:57:30 -0800197 error_report("%s: vmware_vga: no PCI bus", __func__);
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200198 return NULL;
Gerd Hoffmanna94f0c52014-09-10 14:28:48 +0200199 case VGA_VIRTIO:
Alistair Francis1081ed22017-11-08 14:57:30 -0800200 error_report("%s: virtio-vga: no PCI bus", __func__);
Gerd Hoffmanna94f0c52014-09-10 14:28:48 +0200201 return NULL;
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200202 case VGA_NONE:
203 default:
204 return NULL;
205 }
206}
207
Anthony Liguori999e12b2012-01-24 13:12:29 -0600208static void isabus_bridge_class_init(ObjectClass *klass, void *data)
209{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600210 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600211
Thomas Huth5658ffa2017-01-20 16:53:59 +0100212 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600213 dc->fw_name = "isa";
Anthony Liguori999e12b2012-01-24 13:12:29 -0600214}
215
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100216static const TypeInfo isabus_bridge_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600217 .name = "isabus-bridge",
218 .parent = TYPE_SYS_BUS_DEVICE,
219 .instance_size = sizeof(SysBusDevice),
220 .class_init = isabus_bridge_class_init,
Gerd Hoffmann2091ba22009-08-14 11:36:14 +0200221};
222
Anthony Liguori39bffca2011-12-07 21:34:16 -0600223static void isa_device_class_init(ObjectClass *klass, void *data)
224{
225 DeviceClass *k = DEVICE_CLASS(klass);
Anthony Liguori0d936922012-05-02 09:00:20 +0200226 k->bus_type = TYPE_ISA_BUS;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600227}
228
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100229static const TypeInfo isa_device_type_info = {
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600230 .name = TYPE_ISA_DEVICE,
231 .parent = TYPE_DEVICE,
232 .instance_size = sizeof(ISADevice),
233 .abstract = true,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600234 .class_init = isa_device_class_init,
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600235};
236
Andreas Färber83f7d432012-02-09 15:20:55 +0100237static void isabus_register_types(void)
Gerd Hoffmann2091ba22009-08-14 11:36:14 +0200238{
Hervé Poussineau5484f302016-02-03 11:28:57 -0500239 type_register_static(&isa_dma_info);
Anthony Liguori0d936922012-05-02 09:00:20 +0200240 type_register_static(&isa_bus_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600241 type_register_static(&isabus_bridge_info);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600242 type_register_static(&isa_device_type_info);
Gerd Hoffmann2091ba22009-08-14 11:36:14 +0200243}
244
Gleb Natapov6a26e112010-12-08 13:34:57 +0200245static char *isabus_get_fw_dev_path(DeviceState *dev)
246{
Andreas Färber4a17cc42013-06-07 13:49:13 +0200247 ISADevice *d = ISA_DEVICE(dev);
Gleb Natapov6a26e112010-12-08 13:34:57 +0200248 char path[40];
249 int off;
250
251 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
Richard Hendersonebf47c22011-08-15 11:59:09 -0700252 if (d->ioport_id) {
253 snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id);
Gleb Natapov6a26e112010-12-08 13:34:57 +0200254 }
255
Jim Meyeringa5cf8262012-10-04 13:09:44 +0200256 return g_strdup(path);
Gleb Natapov6a26e112010-12-08 13:34:57 +0200257}
258
Avi Kivityc839ade2011-08-15 17:17:35 +0300259MemoryRegion *isa_address_space(ISADevice *dev)
260{
Hervé Poussineaubb2ed002015-02-01 09:12:50 +0100261 if (dev) {
262 return isa_bus_from_device(dev)->address_space;
263 }
264
265 return isabus->address_space;
Avi Kivityc839ade2011-08-15 17:17:35 +0300266}
267
Julien Grallac100272012-09-19 12:50:02 +0100268MemoryRegion *isa_address_space_io(ISADevice *dev)
269{
270 if (dev) {
271 return isa_bus_from_device(dev)->address_space_io;
272 }
273
274 return isabus->address_space_io;
275}
276
Andreas Färber83f7d432012-02-09 15:20:55 +0100277type_init(isabus_register_types)