isa-bus.c 7.1 KB

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