isa-bus.c 6.7 KB

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