sysbus.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * System (CPU) Bus device support code
  3. *
  4. * Copyright (c) 2009 CodeSourcery
  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 "sysbus.h"
  20. #include "monitor.h"
  21. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  22. static char *sysbus_get_fw_dev_path(DeviceState *dev);
  23. struct BusInfo system_bus_info = {
  24. .name = "System",
  25. .size = sizeof(BusState),
  26. .print_dev = sysbus_dev_print,
  27. .get_fw_dev_path = sysbus_get_fw_dev_path,
  28. };
  29. void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
  30. {
  31. assert(n >= 0 && n < dev->num_irq);
  32. dev->irqs[n] = NULL;
  33. if (dev->irqp[n]) {
  34. *dev->irqp[n] = irq;
  35. }
  36. }
  37. void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
  38. {
  39. assert(n >= 0 && n < dev->num_mmio);
  40. if (dev->mmio[n].addr == addr) {
  41. /* ??? region already mapped here. */
  42. return;
  43. }
  44. if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
  45. /* Unregister previous mapping. */
  46. cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
  47. IO_MEM_UNASSIGNED);
  48. }
  49. dev->mmio[n].addr = addr;
  50. if (dev->mmio[n].cb) {
  51. dev->mmio[n].cb(dev, addr);
  52. } else {
  53. cpu_register_physical_memory(addr, dev->mmio[n].size,
  54. dev->mmio[n].iofunc);
  55. }
  56. }
  57. /* Request an IRQ source. The actual IRQ object may be populated later. */
  58. void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
  59. {
  60. int n;
  61. assert(dev->num_irq < QDEV_MAX_IRQ);
  62. n = dev->num_irq++;
  63. dev->irqp[n] = p;
  64. }
  65. /* Pass IRQs from a target device. */
  66. void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
  67. {
  68. int i;
  69. assert(dev->num_irq == 0);
  70. dev->num_irq = target->num_irq;
  71. for (i = 0; i < dev->num_irq; i++) {
  72. dev->irqp[i] = target->irqp[i];
  73. }
  74. }
  75. void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
  76. ram_addr_t iofunc)
  77. {
  78. int n;
  79. assert(dev->num_mmio < QDEV_MAX_MMIO);
  80. n = dev->num_mmio++;
  81. dev->mmio[n].addr = -1;
  82. dev->mmio[n].size = size;
  83. dev->mmio[n].iofunc = iofunc;
  84. }
  85. void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
  86. mmio_mapfunc cb)
  87. {
  88. int n;
  89. assert(dev->num_mmio < QDEV_MAX_MMIO);
  90. n = dev->num_mmio++;
  91. dev->mmio[n].addr = -1;
  92. dev->mmio[n].size = size;
  93. dev->mmio[n].cb = cb;
  94. }
  95. void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
  96. {
  97. pio_addr_t i;
  98. for (i = 0; i < size; i++) {
  99. assert(dev->num_pio < QDEV_MAX_PIO);
  100. dev->pio[dev->num_pio++] = ioport++;
  101. }
  102. }
  103. static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
  104. {
  105. SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
  106. return info->init(sysbus_from_qdev(dev));
  107. }
  108. void sysbus_register_withprop(SysBusDeviceInfo *info)
  109. {
  110. info->qdev.init = sysbus_device_init;
  111. info->qdev.bus_info = &system_bus_info;
  112. assert(info->qdev.size >= sizeof(SysBusDevice));
  113. qdev_register(&info->qdev);
  114. }
  115. void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
  116. {
  117. SysBusDeviceInfo *info;
  118. info = qemu_mallocz(sizeof(*info));
  119. info->qdev.name = qemu_strdup(name);
  120. info->qdev.size = size;
  121. info->init = init;
  122. sysbus_register_withprop(info);
  123. }
  124. DeviceState *sysbus_create_varargs(const char *name,
  125. target_phys_addr_t addr, ...)
  126. {
  127. DeviceState *dev;
  128. SysBusDevice *s;
  129. va_list va;
  130. qemu_irq irq;
  131. int n;
  132. dev = qdev_create(NULL, name);
  133. s = sysbus_from_qdev(dev);
  134. qdev_init_nofail(dev);
  135. if (addr != (target_phys_addr_t)-1) {
  136. sysbus_mmio_map(s, 0, addr);
  137. }
  138. va_start(va, addr);
  139. n = 0;
  140. while (1) {
  141. irq = va_arg(va, qemu_irq);
  142. if (!irq) {
  143. break;
  144. }
  145. sysbus_connect_irq(s, n, irq);
  146. n++;
  147. }
  148. return dev;
  149. }
  150. DeviceState *sysbus_try_create_varargs(const char *name,
  151. target_phys_addr_t addr, ...)
  152. {
  153. DeviceState *dev;
  154. SysBusDevice *s;
  155. va_list va;
  156. qemu_irq irq;
  157. int n;
  158. dev = qdev_try_create(NULL, name);
  159. if (!dev) {
  160. return NULL;
  161. }
  162. s = sysbus_from_qdev(dev);
  163. qdev_init_nofail(dev);
  164. if (addr != (target_phys_addr_t)-1) {
  165. sysbus_mmio_map(s, 0, addr);
  166. }
  167. va_start(va, addr);
  168. n = 0;
  169. while (1) {
  170. irq = va_arg(va, qemu_irq);
  171. if (!irq) {
  172. break;
  173. }
  174. sysbus_connect_irq(s, n, irq);
  175. n++;
  176. }
  177. return dev;
  178. }
  179. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  180. {
  181. SysBusDevice *s = sysbus_from_qdev(dev);
  182. int i;
  183. monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
  184. for (i = 0; i < s->num_mmio; i++) {
  185. monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
  186. indent, "", s->mmio[i].addr, s->mmio[i].size);
  187. }
  188. }
  189. static char *sysbus_get_fw_dev_path(DeviceState *dev)
  190. {
  191. SysBusDevice *s = sysbus_from_qdev(dev);
  192. char path[40];
  193. int off;
  194. off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
  195. if (s->num_mmio) {
  196. snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
  197. s->mmio[0].addr);
  198. } else if (s->num_pio) {
  199. snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
  200. }
  201. return strdup(path);
  202. }