isa-bus.c 8.0 KB

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