isa-bus.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. struct ISABus {
  25. BusState qbus;
  26. MemoryRegion *address_space_io;
  27. qemu_irq *irqs;
  28. };
  29. static ISABus *isabus;
  30. target_phys_addr_t isa_mem_base = 0;
  31. static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  32. static char *isabus_get_fw_dev_path(DeviceState *dev);
  33. static struct BusInfo isa_bus_info = {
  34. .name = "ISA",
  35. .size = sizeof(ISABus),
  36. .print_dev = isabus_dev_print,
  37. .get_fw_dev_path = isabus_get_fw_dev_path,
  38. };
  39. ISABus *isa_bus_new(DeviceState *dev, MemoryRegion *address_space_io)
  40. {
  41. if (isabus) {
  42. fprintf(stderr, "Can't create a second ISA bus\n");
  43. return NULL;
  44. }
  45. if (NULL == dev) {
  46. dev = qdev_create(NULL, "isabus-bridge");
  47. qdev_init_nofail(dev);
  48. }
  49. isabus = FROM_QBUS(ISABus, qbus_create(&isa_bus_info, dev, NULL));
  50. isabus->address_space_io = address_space_io;
  51. return isabus;
  52. }
  53. void isa_bus_irqs(qemu_irq *irqs)
  54. {
  55. isabus->irqs = irqs;
  56. }
  57. /*
  58. * isa_get_irq() returns the corresponding qemu_irq entry for the i8259.
  59. *
  60. * This function is only for special cases such as the 'ferr', and
  61. * temporary use for normal devices until they are converted to qdev.
  62. */
  63. qemu_irq isa_get_irq(int isairq)
  64. {
  65. if (isairq < 0 || isairq > 15) {
  66. hw_error("isa irq %d invalid", isairq);
  67. }
  68. return isabus->irqs[isairq];
  69. }
  70. void isa_init_irq(ISADevice *dev, qemu_irq *p, int isairq)
  71. {
  72. assert(dev->nirqs < ARRAY_SIZE(dev->isairq));
  73. dev->isairq[dev->nirqs] = isairq;
  74. *p = isa_get_irq(isairq);
  75. dev->nirqs++;
  76. }
  77. static inline void isa_init_ioport(ISADevice *dev, uint16_t ioport)
  78. {
  79. if (dev && (dev->ioport_id == 0 || ioport < dev->ioport_id)) {
  80. dev->ioport_id = ioport;
  81. }
  82. }
  83. void isa_register_ioport(ISADevice *dev, MemoryRegion *io, uint16_t start)
  84. {
  85. memory_region_add_subregion(isabus->address_space_io, start, io);
  86. isa_init_ioport(dev, start);
  87. }
  88. void isa_register_portio_list(ISADevice *dev, uint16_t start,
  89. const MemoryRegionPortio *pio_start,
  90. void *opaque, const char *name)
  91. {
  92. PortioList *piolist = g_new(PortioList, 1);
  93. /* START is how we should treat DEV, regardless of the actual
  94. contents of the portio array. This is how the old code
  95. actually handled e.g. the FDC device. */
  96. isa_init_ioport(dev, start);
  97. portio_list_init(piolist, pio_start, opaque, name);
  98. portio_list_add(piolist, isabus->address_space_io, start);
  99. }
  100. static int isa_qdev_init(DeviceState *qdev, DeviceInfo *base)
  101. {
  102. ISADevice *dev = DO_UPCAST(ISADevice, qdev, qdev);
  103. ISADeviceInfo *info = DO_UPCAST(ISADeviceInfo, qdev, base);
  104. dev->isairq[0] = -1;
  105. dev->isairq[1] = -1;
  106. return info->init(dev);
  107. }
  108. void isa_qdev_register(ISADeviceInfo *info)
  109. {
  110. info->qdev.init = isa_qdev_init;
  111. info->qdev.bus_info = &isa_bus_info;
  112. qdev_register(&info->qdev);
  113. }
  114. ISADevice *isa_create(const char *name)
  115. {
  116. DeviceState *dev;
  117. if (!isabus) {
  118. hw_error("Tried to create isa device %s with no isa bus present.",
  119. name);
  120. }
  121. dev = qdev_create(&isabus->qbus, name);
  122. return DO_UPCAST(ISADevice, qdev, dev);
  123. }
  124. ISADevice *isa_try_create(const char *name)
  125. {
  126. DeviceState *dev;
  127. if (!isabus) {
  128. hw_error("Tried to create isa device %s with no isa bus present.",
  129. name);
  130. }
  131. dev = qdev_try_create(&isabus->qbus, name);
  132. return DO_UPCAST(ISADevice, qdev, dev);
  133. }
  134. ISADevice *isa_create_simple(const char *name)
  135. {
  136. ISADevice *dev;
  137. dev = isa_create(name);
  138. qdev_init_nofail(&dev->qdev);
  139. return dev;
  140. }
  141. static void isabus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  142. {
  143. ISADevice *d = DO_UPCAST(ISADevice, qdev, dev);
  144. if (d->isairq[1] != -1) {
  145. monitor_printf(mon, "%*sisa irqs %d,%d\n", indent, "",
  146. d->isairq[0], d->isairq[1]);
  147. } else if (d->isairq[0] != -1) {
  148. monitor_printf(mon, "%*sisa irq %d\n", indent, "",
  149. d->isairq[0]);
  150. }
  151. }
  152. static int isabus_bridge_init(SysBusDevice *dev)
  153. {
  154. /* nothing */
  155. return 0;
  156. }
  157. static SysBusDeviceInfo isabus_bridge_info = {
  158. .init = isabus_bridge_init,
  159. .qdev.name = "isabus-bridge",
  160. .qdev.fw_name = "isa",
  161. .qdev.size = sizeof(SysBusDevice),
  162. .qdev.no_user = 1,
  163. };
  164. static void isabus_register_devices(void)
  165. {
  166. sysbus_register_withprop(&isabus_bridge_info);
  167. }
  168. static char *isabus_get_fw_dev_path(DeviceState *dev)
  169. {
  170. ISADevice *d = (ISADevice*)dev;
  171. char path[40];
  172. int off;
  173. off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
  174. if (d->ioport_id) {
  175. snprintf(path + off, sizeof(path) - off, "@%04x", d->ioport_id);
  176. }
  177. return strdup(path);
  178. }
  179. MemoryRegion *isa_address_space(ISADevice *dev)
  180. {
  181. return get_system_memory();
  182. }
  183. device_init(isabus_register_devices)