sysbus.c 8.2 KB

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