isa-bus.c 7.0 KB

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