blob: a289eccfb194c658fea2affe1a6d9b0fba9b6c0c [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{
55 if (isabus) {
Markus Armbrusterd10e5432015-12-17 17:35:18 +010056 error_setg(errp, "Can't create a second ISA bus");
Gerd Hoffmannf915a112009-07-31 12:30:14 +020057 return NULL;
58 }
Gonglei337a3e5c2014-08-11 21:00:54 +080059 if (!dev) {
Markus Armbruster3e80f692020-06-10 07:31:58 +020060 dev = qdev_new("isabus-bridge");
Markus Armbruster3c6ef472020-06-10 07:32:34 +020061 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
Gerd Hoffmann2091ba22009-08-14 11:36:14 +020062 }
Gerd Hoffmannf915a112009-07-31 12:30:14 +020063
Peter Maydell9388d172021-09-23 13:11:52 +010064 isabus = ISA_BUS(qbus_new(TYPE_ISA_BUS, dev, NULL));
Hervé Poussineaubb2ed002015-02-01 09:12:50 +010065 isabus->address_space = address_space;
Richard Hendersonc2d0d012011-08-10 15:28:11 -070066 isabus->address_space_io = address_space_io;
Gerd Hoffmannf915a112009-07-31 12:30:14 +020067 return isabus;
68}
69
Philippe Mathieu-Daudé70678872023-02-09 13:32:18 +010070void isa_bus_register_input_irqs(ISABus *bus, qemu_irq *irqs_in)
Gerd Hoffmannf915a112009-07-31 12:30:14 +020071{
Philippe Mathieu-Daudé70678872023-02-09 13:32:18 +010072 bus->irqs_in = irqs_in;
Gerd Hoffmann2091ba22009-08-14 11:36:14 +020073}
74
Philippe Mathieu-Daudéd2fbec52023-02-09 11:55:03 +010075qemu_irq isa_bus_get_irq(ISABus *bus, unsigned irqnum)
76{
77 assert(irqnum < ISA_NUM_IRQS);
78 assert(bus->irqs_in);
79 return bus->irqs_in[irqnum];
80}
81
Jes Sorensen3a38d432009-08-14 11:36:15 +020082/*
Philippe Mathieu-Daudé70678872023-02-09 13:32:18 +010083 * isa_get_irq() returns the corresponding input qemu_irq entry for the i8259.
Jes Sorensen3a38d432009-08-14 11:36:15 +020084 *
85 * This function is only for special cases such as the 'ferr', and
86 * temporary use for normal devices until they are converted to qdev.
87 */
Paolo Bonzini3c29e182019-12-13 13:00:43 +010088qemu_irq isa_get_irq(ISADevice *dev, unsigned isairq)
Jes Sorensen3a38d432009-08-14 11:36:15 +020089{
Andreas Färber2ae0e482013-06-07 14:11:07 +020090 assert(!dev || ISA_BUS(qdev_get_parent_bus(DEVICE(dev))) == isabus);
Philippe Mathieu-Daudéd2fbec52023-02-09 11:55:03 +010091 return isa_bus_get_irq(isabus, isairq);
Jes Sorensen3a38d432009-08-14 11:36:15 +020092}
93
Paolo Bonzini3c29e182019-12-13 13:00:43 +010094void isa_connect_gpio_out(ISADevice *isadev, int gpioirq, unsigned isairq)
Efimov Vasily25026302016-06-22 15:24:56 +030095{
Philippe Mathieu-Daudéd2fbec52023-02-09 11:55:03 +010096 qemu_irq input_irq = isa_get_irq(isadev, isairq);
97 qdev_connect_gpio_out(DEVICE(isadev), gpioirq, input_irq);
Efimov Vasily25026302016-06-22 15:24:56 +030098}
99
Hervé Poussineau5484f302016-02-03 11:28:57 -0500100void isa_bus_dma(ISABus *bus, IsaDma *dma8, IsaDma *dma16)
101{
102 assert(bus && dma8 && dma16);
103 assert(!bus->dma[0] && !bus->dma[1]);
104 bus->dma[0] = dma8;
105 bus->dma[1] = dma16;
106}
107
Philippe Mathieu-Daudédc8d6cf2023-02-15 15:38:19 +0100108IsaDma *isa_bus_get_dma(ISABus *bus, int nchan)
Hervé Poussineau5484f302016-02-03 11:28:57 -0500109{
110 assert(bus);
111 return bus->dma[nchan > 3 ? 1 : 0];
112}
113
Richard Henderson0d959522011-08-16 09:02:36 -0700114static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport)
Gleb Natapovdee41d52010-12-08 13:34:56 +0200115{
Richard Henderson0d959522011-08-16 09:02:36 -0700116 if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) {
117 dev->ioport_id = ioport;
Gleb Natapovdee41d52010-12-08 13:34:56 +0200118 }
Gleb Natapovdee41d52010-12-08 13:34:56 +0200119}
120
Richard Henderson78e20592011-08-10 15:28:12 -0700121void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start)
122{
Philippe Mathieu-Daudée5bf2772023-01-20 12:45:54 +0100123 memory_region_add_subregion(isa_address_space_io(dev), start, io);
Richard Henderson0d959522011-08-16 09:02:36 -0700124 isa_init_ioport(dev, start);
Richard Henderson78e20592011-08-10 15:28:12 -0700125}
126
Thomas Huth9405d872021-04-16 14:52:56 +0200127int isa_register_portio_list(ISADevice *dev,
128 PortioList *piolist, uint16_t start,
129 const MemoryRegionPortio *pio_start,
130 void *opaque, const char *name)
Avi Kivityd7500732011-09-26 14:52:44 +0300131{
Marc-André Lureaue305a162016-07-13 02:11:59 +0200132 assert(piolist && !piolist->owner);
Avi Kivityd7500732011-09-26 14:52:44 +0300133
Thomas Huth9405d872021-04-16 14:52:56 +0200134 if (!isabus) {
135 return -ENODEV;
136 }
137
Avi Kivityd7500732011-09-26 14:52:44 +0300138 /* START is how we should treat DEV, regardless of the actual
139 contents of the portio array. This is how the old code
140 actually handled e.g. the FDC device. */
Richard Henderson0d959522011-08-16 09:02:36 -0700141 isa_init_ioport(dev, start);
Avi Kivityd7500732011-09-26 14:52:44 +0300142
Marc-André Lureaue305a162016-07-13 02:11:59 +0200143 portio_list_init(piolist, OBJECT(dev), pio_start, opaque, name);
Philippe Mathieu-Daudée5bf2772023-01-20 12:45:54 +0100144 portio_list_add(piolist, isa_address_space_io(dev), start);
Thomas Huth9405d872021-04-16 14:52:56 +0200145
146 return 0;
Avi Kivityd7500732011-09-26 14:52:44 +0300147}
148
Markus Armbruster0fe9d902020-06-10 07:32:07 +0200149ISADevice *isa_new(const char *name)
150{
151 return ISA_DEVICE(qdev_new(name));
152}
153
154ISADevice *isa_try_new(const char *name)
155{
156 return ISA_DEVICE(qdev_try_new(name));
157}
158
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100159ISADevice *isa_create_simple(ISABus *bus, const char *name)
Gerd Hoffmann924f6d72009-09-10 11:43:29 +0200160{
161 ISADevice *dev;
162
Markus Armbruster96927c72020-06-10 07:32:08 +0200163 dev = isa_new(name);
164 isa_realize_and_unref(dev, bus, &error_fatal);
Gerd Hoffmann924f6d72009-09-10 11:43:29 +0200165 return dev;
166}
167
Markus Armbruster0fe9d902020-06-10 07:32:07 +0200168bool isa_realize_and_unref(ISADevice *dev, ISABus *bus, Error **errp)
169{
170 return qdev_realize_and_unref(&dev->parent_obj, &bus->parent_obj, errp);
171}
172
Philippe Mathieu-Daudé23c69bb2023-02-07 22:47:20 +0100173ISABus *isa_bus_from_device(ISADevice *dev)
174{
175 return ISA_BUS(qdev_get_parent_bus(DEVICE(dev)));
176}
177
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200178ISADevice *isa_vga_init(ISABus *bus)
179{
Gautam Agrawalf9bcb2d2022-05-01 17:55:05 +0530180 vga_interface_created = true;
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200181 switch (vga_interface_type) {
182 case VGA_CIRRUS:
183 return isa_create_simple(bus, "isa-cirrus-vga");
184 case VGA_QXL:
Alistair Francis1081ed22017-11-08 14:57:30 -0800185 error_report("%s: qxl: no PCI bus", __func__);
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200186 return NULL;
187 case VGA_STD:
188 return isa_create_simple(bus, "isa-vga");
189 case VGA_VMWARE:
Alistair Francis1081ed22017-11-08 14:57:30 -0800190 error_report("%s: vmware_vga: no PCI bus", __func__);
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200191 return NULL;
Gerd Hoffmanna94f0c52014-09-10 14:28:48 +0200192 case VGA_VIRTIO:
Alistair Francis1081ed22017-11-08 14:57:30 -0800193 error_report("%s: virtio-vga: no PCI bus", __func__);
Gerd Hoffmanna94f0c52014-09-10 14:28:48 +0200194 return NULL;
Aurelien Jarno14e7a642012-09-08 16:58:57 +0200195 case VGA_NONE:
196 default:
197 return NULL;
198 }
199}
200
Anthony Liguori999e12b2012-01-24 13:12:29 -0600201static void isabus_bridge_class_init(ObjectClass *klass, void *data)
202{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600203 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600204
Thomas Huth5658ffa2017-01-20 16:53:59 +0100205 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600206 dc->fw_name = "isa";
Anthony Liguori999e12b2012-01-24 13:12:29 -0600207}
208
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100209static const TypeInfo isabus_bridge_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600210 .name = "isabus-bridge",
211 .parent = TYPE_SYS_BUS_DEVICE,
212 .instance_size = sizeof(SysBusDevice),
213 .class_init = isabus_bridge_class_init,
Gerd Hoffmann2091ba22009-08-14 11:36:14 +0200214};
215
Anthony Liguori39bffca2011-12-07 21:34:16 -0600216static void isa_device_class_init(ObjectClass *klass, void *data)
217{
218 DeviceClass *k = DEVICE_CLASS(klass);
Anthony Liguori0d936922012-05-02 09:00:20 +0200219 k->bus_type = TYPE_ISA_BUS;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600220}
221
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100222static const TypeInfo isa_device_type_info = {
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600223 .name = TYPE_ISA_DEVICE,
224 .parent = TYPE_DEVICE,
225 .instance_size = sizeof(ISADevice),
226 .abstract = true,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600227 .class_init = isa_device_class_init,
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600228};
229
Andreas Färber83f7d432012-02-09 15:20:55 +0100230static void isabus_register_types(void)
Gerd Hoffmann2091ba22009-08-14 11:36:14 +0200231{
Hervé Poussineau5484f302016-02-03 11:28:57 -0500232 type_register_static(&isa_dma_info);
Anthony Liguori0d936922012-05-02 09:00:20 +0200233 type_register_static(&isa_bus_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600234 type_register_static(&isabus_bridge_info);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600235 type_register_static(&isa_device_type_info);
Gerd Hoffmann2091ba22009-08-14 11:36:14 +0200236}
237
Gleb Natapov6a26e112010-12-08 13:34:57 +0200238static char *isabus_get_fw_dev_path(DeviceState *dev)
239{
Andreas Färber4a17cc42013-06-07 13:49:13 +0200240 ISADevice *d = ISA_DEVICE(dev);
Gleb Natapov6a26e112010-12-08 13:34:57 +0200241 char path[40];
242 int off;
243
244 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
Richard Hendersonebf47c22011-08-15 11:59:09 -0700245 if (d->ioport_id) {
246 snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id);
Gleb Natapov6a26e112010-12-08 13:34:57 +0200247 }
248
Jim Meyeringa5cf8262012-10-04 13:09:44 +0200249 return g_strdup(path);
Gleb Natapov6a26e112010-12-08 13:34:57 +0200250}
251
Avi Kivityc839ade2011-08-15 17:17:35 +0300252MemoryRegion *isa_address_space(ISADevice *dev)
253{
Hervé Poussineaubb2ed002015-02-01 09:12:50 +0100254 if (dev) {
255 return isa_bus_from_device(dev)->address_space;
256 }
257
258 return isabus->address_space;
Avi Kivityc839ade2011-08-15 17:17:35 +0300259}
260
Julien Grallac100272012-09-19 12:50:02 +0100261MemoryRegion *isa_address_space_io(ISADevice *dev)
262{
263 if (dev) {
264 return isa_bus_from_device(dev)->address_space_io;
265 }
266
267 return isabus->address_space_io;
268}
269
Andreas Färber83f7d432012-02-09 15:20:55 +0100270type_init(isabus_register_types)