sysbus.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/monitor.h"
  21. #include "exec/address-spaces.h"
  22. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  23. static char *sysbus_get_fw_dev_path(DeviceState *dev);
  24. static void system_bus_class_init(ObjectClass *klass, void *data)
  25. {
  26. BusClass *k = BUS_CLASS(klass);
  27. k->print_dev = sysbus_dev_print;
  28. k->get_fw_dev_path = sysbus_get_fw_dev_path;
  29. }
  30. static const TypeInfo system_bus_info = {
  31. .name = TYPE_SYSTEM_BUS,
  32. .parent = TYPE_BUS,
  33. .instance_size = sizeof(BusState),
  34. .class_init = system_bus_class_init,
  35. };
  36. void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
  37. {
  38. assert(n >= 0 && n < dev->num_irq);
  39. dev->irqs[n] = NULL;
  40. if (dev->irqp[n]) {
  41. *dev->irqp[n] = irq;
  42. }
  43. }
  44. void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
  45. {
  46. assert(n >= 0 && n < dev->num_mmio);
  47. if (dev->mmio[n].addr == addr) {
  48. /* ??? region already mapped here. */
  49. return;
  50. }
  51. if (dev->mmio[n].addr != (hwaddr)-1) {
  52. /* Unregister previous mapping. */
  53. memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
  54. }
  55. dev->mmio[n].addr = addr;
  56. memory_region_add_subregion(get_system_memory(),
  57. addr,
  58. dev->mmio[n].memory);
  59. }
  60. /* Request an IRQ source. The actual IRQ object may be populated later. */
  61. void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
  62. {
  63. int n;
  64. assert(dev->num_irq < QDEV_MAX_IRQ);
  65. n = dev->num_irq++;
  66. dev->irqp[n] = p;
  67. }
  68. /* Pass IRQs from a target device. */
  69. void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
  70. {
  71. int i;
  72. assert(dev->num_irq == 0);
  73. dev->num_irq = target->num_irq;
  74. for (i = 0; i < dev->num_irq; i++) {
  75. dev->irqp[i] = target->irqp[i];
  76. }
  77. }
  78. void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
  79. {
  80. int n;
  81. assert(dev->num_mmio < QDEV_MAX_MMIO);
  82. n = dev->num_mmio++;
  83. dev->mmio[n].addr = -1;
  84. dev->mmio[n].memory = memory;
  85. }
  86. MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
  87. {
  88. return dev->mmio[n].memory;
  89. }
  90. void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
  91. {
  92. pio_addr_t i;
  93. for (i = 0; i < size; i++) {
  94. assert(dev->num_pio < QDEV_MAX_PIO);
  95. dev->pio[dev->num_pio++] = ioport++;
  96. }
  97. }
  98. static int sysbus_device_init(DeviceState *dev)
  99. {
  100. SysBusDevice *sd = SYS_BUS_DEVICE(dev);
  101. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
  102. return sbc->init(sd);
  103. }
  104. DeviceState *sysbus_create_varargs(const char *name,
  105. hwaddr addr, ...)
  106. {
  107. DeviceState *dev;
  108. SysBusDevice *s;
  109. va_list va;
  110. qemu_irq irq;
  111. int n;
  112. dev = qdev_create(NULL, name);
  113. s = SYS_BUS_DEVICE(dev);
  114. qdev_init_nofail(dev);
  115. if (addr != (hwaddr)-1) {
  116. sysbus_mmio_map(s, 0, addr);
  117. }
  118. va_start(va, addr);
  119. n = 0;
  120. while (1) {
  121. irq = va_arg(va, qemu_irq);
  122. if (!irq) {
  123. break;
  124. }
  125. sysbus_connect_irq(s, n, irq);
  126. n++;
  127. }
  128. va_end(va);
  129. return dev;
  130. }
  131. DeviceState *sysbus_try_create_varargs(const char *name,
  132. hwaddr addr, ...)
  133. {
  134. DeviceState *dev;
  135. SysBusDevice *s;
  136. va_list va;
  137. qemu_irq irq;
  138. int n;
  139. dev = qdev_try_create(NULL, name);
  140. if (!dev) {
  141. return NULL;
  142. }
  143. s = SYS_BUS_DEVICE(dev);
  144. qdev_init_nofail(dev);
  145. if (addr != (hwaddr)-1) {
  146. sysbus_mmio_map(s, 0, addr);
  147. }
  148. va_start(va, addr);
  149. n = 0;
  150. while (1) {
  151. irq = va_arg(va, qemu_irq);
  152. if (!irq) {
  153. break;
  154. }
  155. sysbus_connect_irq(s, n, irq);
  156. n++;
  157. }
  158. va_end(va);
  159. return dev;
  160. }
  161. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  162. {
  163. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  164. hwaddr size;
  165. int i;
  166. monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
  167. for (i = 0; i < s->num_mmio; i++) {
  168. size = memory_region_size(s->mmio[i].memory);
  169. monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
  170. indent, "", s->mmio[i].addr, size);
  171. }
  172. }
  173. static char *sysbus_get_fw_dev_path(DeviceState *dev)
  174. {
  175. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  176. char path[40];
  177. int off;
  178. off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
  179. if (s->num_mmio) {
  180. snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
  181. s->mmio[0].addr);
  182. } else if (s->num_pio) {
  183. snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
  184. }
  185. return g_strdup(path);
  186. }
  187. void sysbus_add_memory(SysBusDevice *dev, hwaddr addr,
  188. MemoryRegion *mem)
  189. {
  190. memory_region_add_subregion(get_system_memory(), addr, mem);
  191. }
  192. void sysbus_add_memory_overlap(SysBusDevice *dev, hwaddr addr,
  193. MemoryRegion *mem, unsigned priority)
  194. {
  195. memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
  196. priority);
  197. }
  198. void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
  199. {
  200. memory_region_del_subregion(get_system_memory(), mem);
  201. }
  202. void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
  203. MemoryRegion *mem)
  204. {
  205. memory_region_add_subregion(get_system_io(), addr, mem);
  206. }
  207. void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
  208. {
  209. memory_region_del_subregion(get_system_io(), mem);
  210. }
  211. MemoryRegion *sysbus_address_space(SysBusDevice *dev)
  212. {
  213. return get_system_memory();
  214. }
  215. static void sysbus_device_class_init(ObjectClass *klass, void *data)
  216. {
  217. DeviceClass *k = DEVICE_CLASS(klass);
  218. k->init = sysbus_device_init;
  219. k->bus_type = TYPE_SYSTEM_BUS;
  220. }
  221. static const TypeInfo sysbus_device_type_info = {
  222. .name = TYPE_SYS_BUS_DEVICE,
  223. .parent = TYPE_DEVICE,
  224. .instance_size = sizeof(SysBusDevice),
  225. .abstract = true,
  226. .class_size = sizeof(SysBusDeviceClass),
  227. .class_init = sysbus_device_class_init,
  228. };
  229. /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
  230. static BusState *main_system_bus;
  231. static void main_system_bus_create(void)
  232. {
  233. /* assign main_system_bus before qbus_create_inplace()
  234. * in order to make "if (bus != sysbus_get_default())" work */
  235. main_system_bus = g_malloc0(system_bus_info.instance_size);
  236. qbus_create_inplace(main_system_bus, TYPE_SYSTEM_BUS, NULL,
  237. "main-system-bus");
  238. OBJECT(main_system_bus)->free = g_free;
  239. object_property_add_child(container_get(qdev_get_machine(),
  240. "/unattached"),
  241. "sysbus", OBJECT(main_system_bus), NULL);
  242. }
  243. BusState *sysbus_get_default(void)
  244. {
  245. if (!main_system_bus) {
  246. main_system_bus_create();
  247. }
  248. return main_system_bus;
  249. }
  250. static void sysbus_register_types(void)
  251. {
  252. type_register_static(&system_bus_info);
  253. type_register_static(&sysbus_device_type_info);
  254. }
  255. type_init(sysbus_register_types)