isa-bus.c 6.5 KB

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