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_irqs(ISABus *bus, qemu_irq *irqs) 71 { 72 bus->irqs = irqs; 73 } 74 75 /* 76 * isa_get_irq() returns the corresponding 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[isairq]; 86 } 87 88 void isa_init_irq(ISADevice *dev, qemu_irq *p, unsigned isairq) 89 { 90 assert(dev->nirqs < ARRAY_SIZE(dev->isairq)); 91 assert(isairq < ISA_NUM_IRQS); 92 dev->isairq[dev->nirqs] = isairq; 93 *p = isa_get_irq(dev, isairq); 94 dev->nirqs++; 95 } 96 97 void isa_connect_gpio_out(ISADevice *isadev, int gpioirq, unsigned isairq) 98 { 99 qemu_irq irq; 100 isa_init_irq(isadev, &irq, isairq); 101 qdev_connect_gpio_out(DEVICE(isadev), gpioirq, irq); 102 } 103 104 void isa_bus_dma(ISABus *bus, IsaDma *dma8, IsaDma *dma16) 105 { 106 assert(bus && dma8 && dma16); 107 assert(!bus->dma[0] && !bus->dma[1]); 108 bus->dma[0] = dma8; 109 bus->dma[1] = dma16; 110 } 111 112 IsaDma *isa_get_dma(ISABus *bus, int nchan) 113 { 114 assert(bus); 115 return bus->dma[nchan > 3 ? 1 : 0]; 116 } 117 118 static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport) 119 { 120 if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) { 121 dev->ioport_id = ioport; 122 } 123 } 124 125 void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start) 126 { 127 memory_region_add_subregion(isabus->address_space_io, start, io); 128 isa_init_ioport(dev, start); 129 } 130 131 int isa_register_portio_list(ISADevice *dev, 132 PortioList *piolist, uint16_t start, 133 const MemoryRegionPortio *pio_start, 134 void *opaque, const char *name) 135 { 136 assert(piolist && !piolist->owner); 137 138 if (!isabus) { 139 return -ENODEV; 140 } 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 return 0; 151 } 152 153 static void isa_device_init(Object *obj) 154 { 155 ISADevice *dev = ISA_DEVICE(obj); 156 157 dev->isairq[0] = -1; 158 dev->isairq[1] = -1; 159 } 160 161 ISADevice *isa_new(const char *name) 162 { 163 return ISA_DEVICE(qdev_new(name)); 164 } 165 166 ISADevice *isa_try_new(const char *name) 167 { 168 return ISA_DEVICE(qdev_try_new(name)); 169 } 170 171 ISADevice *isa_create_simple(ISABus *bus, const char *name) 172 { 173 ISADevice *dev; 174 175 dev = isa_new(name); 176 isa_realize_and_unref(dev, bus, &error_fatal); 177 return dev; 178 } 179 180 bool isa_realize_and_unref(ISADevice *dev, ISABus *bus, Error **errp) 181 { 182 return qdev_realize_and_unref(&dev->parent_obj, &bus->parent_obj, errp); 183 } 184 185 ISADevice *isa_vga_init(ISABus *bus) 186 { 187 switch (vga_interface_type) { 188 case VGA_CIRRUS: 189 return isa_create_simple(bus, "isa-cirrus-vga"); 190 case VGA_QXL: 191 error_report("%s: qxl: no PCI bus", __func__); 192 return NULL; 193 case VGA_STD: 194 return isa_create_simple(bus, "isa-vga"); 195 case VGA_VMWARE: 196 error_report("%s: vmware_vga: no PCI bus", __func__); 197 return NULL; 198 case VGA_VIRTIO: 199 error_report("%s: virtio-vga: no PCI bus", __func__); 200 return NULL; 201 case VGA_NONE: 202 default: 203 return NULL; 204 } 205 } 206 207 void isa_build_aml(ISABus *bus, Aml *scope) 208 { 209 BusChild *kid; 210 ISADevice *dev; 211 ISADeviceClass *dc; 212 213 QTAILQ_FOREACH(kid, &bus->parent_obj.children, sibling) { 214 dev = ISA_DEVICE(kid->child); 215 dc = ISA_DEVICE_GET_CLASS(dev); 216 if (dc->build_aml) { 217 dc->build_aml(dev, scope); 218 } 219 } 220 } 221 222 static void isabus_bridge_class_init(ObjectClass *klass, void *data) 223 { 224 DeviceClass *dc = DEVICE_CLASS(klass); 225 226 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 227 dc->fw_name = "isa"; 228 } 229 230 static const TypeInfo isabus_bridge_info = { 231 .name = "isabus-bridge", 232 .parent = TYPE_SYS_BUS_DEVICE, 233 .instance_size = sizeof(SysBusDevice), 234 .class_init = isabus_bridge_class_init, 235 }; 236 237 static void isa_device_class_init(ObjectClass *klass, void *data) 238 { 239 DeviceClass *k = DEVICE_CLASS(klass); 240 k->bus_type = TYPE_ISA_BUS; 241 } 242 243 static const TypeInfo isa_device_type_info = { 244 .name = TYPE_ISA_DEVICE, 245 .parent = TYPE_DEVICE, 246 .instance_size = sizeof(ISADevice), 247 .instance_init = isa_device_init, 248 .abstract = true, 249 .class_size = sizeof(ISADeviceClass), 250 .class_init = isa_device_class_init, 251 }; 252 253 static void isabus_register_types(void) 254 { 255 type_register_static(&isa_dma_info); 256 type_register_static(&isa_bus_info); 257 type_register_static(&isabus_bridge_info); 258 type_register_static(&isa_device_type_info); 259 } 260 261 static char *isabus_get_fw_dev_path(DeviceState *dev) 262 { 263 ISADevice *d = ISA_DEVICE(dev); 264 char path[40]; 265 int off; 266 267 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); 268 if (d->ioport_id) { 269 snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id); 270 } 271 272 return g_strdup(path); 273 } 274 275 MemoryRegion *isa_address_space(ISADevice *dev) 276 { 277 if (dev) { 278 return isa_bus_from_device(dev)->address_space; 279 } 280 281 return isabus->address_space; 282 } 283 284 MemoryRegion *isa_address_space_io(ISADevice *dev) 285 { 286 if (dev) { 287 return isa_bus_from_device(dev)->address_space_io; 288 } 289 290 return isabus->address_space_io; 291 } 292 293 type_init(isabus_register_types) 294