isa-bus.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "hw.h"
  20. #include "monitor/monitor.h"
  21. #include "sysbus.h"
  22. #include "sysemu/sysemu.h"
  23. #include "isa.h"
  24. #include "exec/address-spaces.h"
  25. static ISABus *isabus;
  26. hwaddr isa_mem_base = 0;
  27. static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  28. static char *isabus_get_fw_dev_path(DeviceState *dev);
  29. static void isa_bus_class_init(ObjectClass *klass, void *data)
  30. {
  31. BusClass *k = BUS_CLASS(klass);
  32. k->print_dev = isabus_dev_print;
  33. k->get_fw_dev_path = isabus_get_fw_dev_path;
  34. }
  35. static const TypeInfo isa_bus_info = {
  36. .name = TYPE_ISA_BUS,
  37. .parent = TYPE_BUS,
  38. .instance_size = sizeof(ISABus),
  39. .class_init = isa_bus_class_init,
  40. };
  41. ISABus *isa_bus_new(DeviceState *dev, MemoryRegion *address_space_io)
  42. {
  43. if (isabus) {
  44. fprintf(stderr, "Can't create a second ISA bus\n");
  45. return NULL;
  46. }
  47. if (NULL == dev) {
  48. dev = qdev_create(NULL, "isabus-bridge");
  49. qdev_init_nofail(dev);
  50. }
  51. isabus = FROM_QBUS(ISABus, qbus_create(TYPE_ISA_BUS, dev, NULL));
  52. isabus->address_space_io = address_space_io;
  53. return isabus;
  54. }
  55. void isa_bus_irqs(ISABus *bus, qemu_irq *irqs)
  56. {
  57. if (!bus) {
  58. hw_error("Can't set isa irqs with no isa bus present.");
  59. }
  60. bus->irqs = irqs;
  61. }
  62. /*
  63. * isa_get_irq() returns the corresponding qemu_irq entry for the i8259.
  64. *
  65. * This function is only for special cases such as the 'ferr', and
  66. * temporary use for normal devices until they are converted to qdev.
  67. */
  68. qemu_irq isa_get_irq(ISADevice *dev, int isairq)
  69. {
  70. assert(!dev || DO_UPCAST(ISABus, qbus, dev->qdev.parent_bus) == isabus);
  71. if (isairq < 0 || isairq > 15) {
  72. hw_error("isa irq %d invalid", isairq);
  73. }
  74. return isabus->irqs[isairq];
  75. }
  76. void isa_init_irq(ISADevice *dev, qemu_irq *p, int isairq)
  77. {
  78. assert(dev->nirqs < ARRAY_SIZE(dev->isairq));
  79. dev->isairq[dev->nirqs] = isairq;
  80. *p = isa_get_irq(dev, isairq);
  81. dev->nirqs++;
  82. }
  83. static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport)
  84. {
  85. if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) {
  86. dev->ioport_id = ioport;
  87. }
  88. }
  89. void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start)
  90. {
  91. memory_region_add_subregion(isabus->address_space_io, start, io);
  92. isa_init_ioport(dev, start);
  93. }
  94. void isa_register_portio_list(ISADevice *dev, uint16_t start,
  95. const MemoryRegionPortio *pio_start,
  96. void *opaque, const char *name)
  97. {
  98. PortioList *piolist = g_new(PortioList, 1);
  99. /* START is how we should treat DEV, regardless of the actual
  100. contents of the portio array. This is how the old code
  101. actually handled e.g. the FDC device. */
  102. isa_init_ioport(dev, start);
  103. portio_list_init(piolist, pio_start, opaque, name);
  104. portio_list_add(piolist, isabus->address_space_io, start);
  105. }
  106. static int isa_qdev_init(DeviceState *qdev)
  107. {
  108. ISADevice *dev = ISA_DEVICE(qdev);
  109. ISADeviceClass *klass = ISA_DEVICE_GET_CLASS(dev);
  110. dev->isairq[0] = -1;
  111. dev->isairq[1] = -1;
  112. if (klass->init) {
  113. return klass->init(dev);
  114. }
  115. return 0;
  116. }
  117. ISADevice *isa_create(ISABus *bus, const char *name)
  118. {
  119. DeviceState *dev;
  120. if (!bus) {
  121. hw_error("Tried to create isa device %s with no isa bus present.",
  122. name);
  123. }
  124. dev = qdev_create(&bus->qbus, name);
  125. return ISA_DEVICE(dev);
  126. }
  127. ISADevice *isa_try_create(ISABus *bus, const char *name)
  128. {
  129. DeviceState *dev;
  130. if (!bus) {
  131. hw_error("Tried to create isa device %s with no isa bus present.",
  132. name);
  133. }
  134. dev = qdev_try_create(&bus->qbus, name);
  135. return ISA_DEVICE(dev);
  136. }
  137. ISADevice *isa_create_simple(ISABus *bus, const char *name)
  138. {
  139. ISADevice *dev;
  140. dev = isa_create(bus, name);
  141. qdev_init_nofail(&dev->qdev);
  142. return dev;
  143. }
  144. ISADevice *isa_vga_init(ISABus *bus)
  145. {
  146. switch (vga_interface_type) {
  147. case VGA_CIRRUS:
  148. return isa_create_simple(bus, "isa-cirrus-vga");
  149. case VGA_QXL:
  150. fprintf(stderr, "%s: qxl: no PCI bus\n", __func__);
  151. return NULL;
  152. case VGA_STD:
  153. return isa_create_simple(bus, "isa-vga");
  154. case VGA_VMWARE:
  155. fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __func__);
  156. return NULL;
  157. case VGA_NONE:
  158. default:
  159. return NULL;
  160. }
  161. }
  162. static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  163. {
  164. ISADevice *d = ISA_DEVICE(dev);
  165. if (d->isairq[1] != -1) {
  166. monitor_printf(mon, "%*sisa irqs %d,%d\n", indent, "",
  167. d->isairq[0], d->isairq[1]);
  168. } else if (d->isairq[0] != -1) {
  169. monitor_printf(mon, "%*sisa irq %d\n", indent, "",
  170. d->isairq[0]);
  171. }
  172. }
  173. static int isabus_bridge_init(SysBusDevice *dev)
  174. {
  175. /* nothing */
  176. return 0;
  177. }
  178. static void isabus_bridge_class_init(ObjectClass *klass, void *data)
  179. {
  180. DeviceClass *dc = DEVICE_CLASS(klass);
  181. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  182. k->init = isabus_bridge_init;
  183. dc->fw_name = "isa";
  184. dc->no_user = 1;
  185. }
  186. static const TypeInfo isabus_bridge_info = {
  187. .name = "isabus-bridge",
  188. .parent = TYPE_SYS_BUS_DEVICE,
  189. .instance_size = sizeof(SysBusDevice),
  190. .class_init = isabus_bridge_class_init,
  191. };
  192. static void isa_device_class_init(ObjectClass *klass, void *data)
  193. {
  194. DeviceClass *k = DEVICE_CLASS(klass);
  195. k->init = isa_qdev_init;
  196. k->bus_type = TYPE_ISA_BUS;
  197. }
  198. static const TypeInfo isa_device_type_info = {
  199. .name = TYPE_ISA_DEVICE,
  200. .parent = TYPE_DEVICE,
  201. .instance_size = sizeof(ISADevice),
  202. .abstract = true,
  203. .class_size = sizeof(ISADeviceClass),
  204. .class_init = isa_device_class_init,
  205. };
  206. static void isabus_register_types(void)
  207. {
  208. type_register_static(&isa_bus_info);
  209. type_register_static(&isabus_bridge_info);
  210. type_register_static(&isa_device_type_info);
  211. }
  212. static char *isabus_get_fw_dev_path(DeviceState *dev)
  213. {
  214. ISADevice *d = (ISADevice*)dev;
  215. char path[40];
  216. int off;
  217. off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
  218. if (d->ioport_id) {
  219. snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id);
  220. }
  221. return g_strdup(path);
  222. }
  223. MemoryRegion *isa_address_space(ISADevice *dev)
  224. {
  225. return get_system_memory();
  226. }
  227. MemoryRegion *isa_address_space_io(ISADevice *dev)
  228. {
  229. if (dev) {
  230. return isa_bus_from_device(dev)->address_space_io;
  231. }
  232. return isabus->address_space_io;
  233. }
  234. type_init(isabus_register_types)