sysbus.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.1 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 "qemu/osdep.h"
  20. #include "qapi/error.h"
  21. #include "qemu/module.h"
  22. #include "hw/sysbus.h"
  23. #include "monitor/monitor.h"
  24. #include "exec/address-spaces.h"
  25. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  26. static char *sysbus_get_fw_dev_path(DeviceState *dev);
  27. typedef struct SysBusFind {
  28. void *opaque;
  29. FindSysbusDeviceFunc *func;
  30. } SysBusFind;
  31. /* Run func() for every sysbus device, traverse the tree for everything else */
  32. static int find_sysbus_device(Object *obj, void *opaque)
  33. {
  34. SysBusFind *find = opaque;
  35. Object *dev;
  36. SysBusDevice *sbdev;
  37. dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
  38. sbdev = (SysBusDevice *)dev;
  39. if (!sbdev) {
  40. /* Container, traverse it for children */
  41. return object_child_foreach(obj, find_sysbus_device, opaque);
  42. }
  43. find->func(sbdev, find->opaque);
  44. return 0;
  45. }
  46. /*
  47. * Loop through all dynamically created sysbus devices and call
  48. * func() for each instance.
  49. */
  50. void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
  51. {
  52. Object *container;
  53. SysBusFind find = {
  54. .func = func,
  55. .opaque = opaque,
  56. };
  57. /* Loop through all sysbus devices that were spawned outside the machine */
  58. container = machine_get_container("peripheral");
  59. find_sysbus_device(container, &find);
  60. container = machine_get_container("peripheral-anon");
  61. find_sysbus_device(container, &find);
  62. }
  63. static void system_bus_class_init(ObjectClass *klass, void *data)
  64. {
  65. BusClass *k = BUS_CLASS(klass);
  66. k->print_dev = sysbus_dev_print;
  67. k->get_fw_dev_path = sysbus_get_fw_dev_path;
  68. }
  69. static const TypeInfo system_bus_info = {
  70. .name = TYPE_SYSTEM_BUS,
  71. .parent = TYPE_BUS,
  72. .instance_size = sizeof(BusState),
  73. .class_init = system_bus_class_init,
  74. };
  75. /* Check whether an IRQ source exists */
  76. bool sysbus_has_irq(SysBusDevice *dev, int n)
  77. {
  78. char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
  79. ObjectProperty *r;
  80. r = object_property_find(OBJECT(dev), prop);
  81. g_free(prop);
  82. return (r != NULL);
  83. }
  84. bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
  85. {
  86. return !!sysbus_get_connected_irq(dev, n);
  87. }
  88. qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
  89. {
  90. DeviceState *d = DEVICE(dev);
  91. return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
  92. }
  93. void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
  94. {
  95. SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);
  96. qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
  97. if (sbd->connect_irq_notifier) {
  98. sbd->connect_irq_notifier(dev, irq);
  99. }
  100. }
  101. /* Check whether an MMIO region exists */
  102. bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
  103. {
  104. return (n < dev->num_mmio);
  105. }
  106. static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
  107. bool may_overlap, int priority)
  108. {
  109. assert(n >= 0 && n < dev->num_mmio);
  110. if (dev->mmio[n].addr == addr) {
  111. /* ??? region already mapped here. */
  112. return;
  113. }
  114. if (dev->mmio[n].addr != (hwaddr)-1) {
  115. /* Unregister previous mapping. */
  116. memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
  117. }
  118. dev->mmio[n].addr = addr;
  119. if (may_overlap) {
  120. memory_region_add_subregion_overlap(get_system_memory(),
  121. addr,
  122. dev->mmio[n].memory,
  123. priority);
  124. }
  125. else {
  126. memory_region_add_subregion(get_system_memory(),
  127. addr,
  128. dev->mmio[n].memory);
  129. }
  130. }
  131. void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
  132. {
  133. sysbus_mmio_map_common(dev, n, addr, false, 0);
  134. }
  135. void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
  136. int priority)
  137. {
  138. sysbus_mmio_map_common(dev, n, addr, true, priority);
  139. }
  140. /* Request an IRQ source. The actual IRQ object may be populated later. */
  141. void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
  142. {
  143. qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
  144. }
  145. /* Pass IRQs from a target device. */
  146. void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
  147. {
  148. qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
  149. }
  150. void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
  151. {
  152. int n;
  153. assert(dev->num_mmio < QDEV_MAX_MMIO);
  154. n = dev->num_mmio++;
  155. dev->mmio[n].addr = -1;
  156. dev->mmio[n].memory = memory;
  157. }
  158. MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
  159. {
  160. assert(n >= 0 && n < QDEV_MAX_MMIO);
  161. return dev->mmio[n].memory;
  162. }
  163. void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
  164. {
  165. uint32_t i;
  166. for (i = 0; i < size; i++) {
  167. assert(dev->num_pio < QDEV_MAX_PIO);
  168. dev->pio[dev->num_pio++] = ioport++;
  169. }
  170. }
  171. /* The purpose of preserving this empty realize function
  172. * is to prevent the parent_realize field of some subclasses
  173. * from being set to NULL to break the normal init/realize
  174. * of some devices.
  175. */
  176. static void sysbus_device_realize(DeviceState *dev, Error **errp)
  177. {
  178. }
  179. DeviceState *sysbus_create_varargs(const char *name,
  180. hwaddr addr, ...)
  181. {
  182. DeviceState *dev;
  183. SysBusDevice *s;
  184. va_list va;
  185. qemu_irq irq;
  186. int n;
  187. dev = qdev_new(name);
  188. s = SYS_BUS_DEVICE(dev);
  189. sysbus_realize_and_unref(s, &error_fatal);
  190. if (addr != (hwaddr)-1) {
  191. sysbus_mmio_map(s, 0, addr);
  192. }
  193. va_start(va, addr);
  194. n = 0;
  195. while (1) {
  196. irq = va_arg(va, qemu_irq);
  197. if (!irq) {
  198. break;
  199. }
  200. sysbus_connect_irq(s, n, irq);
  201. n++;
  202. }
  203. va_end(va);
  204. return dev;
  205. }
  206. bool sysbus_realize(SysBusDevice *dev, Error **errp)
  207. {
  208. return qdev_realize(DEVICE(dev), sysbus_get_default(), errp);
  209. }
  210. bool sysbus_realize_and_unref(SysBusDevice *dev, Error **errp)
  211. {
  212. return qdev_realize_and_unref(DEVICE(dev), sysbus_get_default(), errp);
  213. }
  214. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  215. {
  216. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  217. hwaddr size;
  218. int i;
  219. for (i = 0; i < s->num_mmio; i++) {
  220. size = memory_region_size(s->mmio[i].memory);
  221. monitor_printf(mon, "%*smmio " HWADDR_FMT_plx "/" HWADDR_FMT_plx "\n",
  222. indent, "", s->mmio[i].addr, size);
  223. }
  224. }
  225. static char *sysbus_get_fw_dev_path(DeviceState *dev)
  226. {
  227. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  228. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
  229. char *addr, *fw_dev_path;
  230. if (sbc->explicit_ofw_unit_address) {
  231. addr = sbc->explicit_ofw_unit_address(s);
  232. if (addr) {
  233. fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
  234. g_free(addr);
  235. return fw_dev_path;
  236. }
  237. }
  238. if (s->num_mmio) {
  239. return g_strdup_printf("%s@" HWADDR_FMT_plx, qdev_fw_name(dev),
  240. s->mmio[0].addr);
  241. }
  242. if (s->num_pio) {
  243. return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
  244. }
  245. return g_strdup(qdev_fw_name(dev));
  246. }
  247. static void sysbus_device_class_init(ObjectClass *klass, void *data)
  248. {
  249. DeviceClass *k = DEVICE_CLASS(klass);
  250. k->realize = sysbus_device_realize;
  251. k->bus_type = TYPE_SYSTEM_BUS;
  252. /*
  253. * device_add plugs devices into a suitable bus. For "real" buses,
  254. * that actually connects the device. For sysbus, the connections
  255. * need to be made separately, and device_add can't do that. The
  256. * device would be left unconnected, and will probably not work
  257. *
  258. * However, a few machines can handle device_add/-device with
  259. * a few specific sysbus devices. In those cases, the device
  260. * subclass needs to override it and set user_creatable=true.
  261. */
  262. k->user_creatable = false;
  263. }
  264. static const TypeInfo sysbus_device_type_info = {
  265. .name = TYPE_SYS_BUS_DEVICE,
  266. .parent = TYPE_DEVICE,
  267. .instance_size = sizeof(SysBusDevice),
  268. .abstract = true,
  269. .class_size = sizeof(SysBusDeviceClass),
  270. .class_init = sysbus_device_class_init,
  271. };
  272. static BusState *main_system_bus;
  273. static void main_system_bus_create(void)
  274. {
  275. /*
  276. * assign main_system_bus before qbus_init()
  277. * in order to make "if (bus != sysbus_get_default())" work
  278. */
  279. main_system_bus = g_malloc0(system_bus_info.instance_size);
  280. qbus_init(main_system_bus, system_bus_info.instance_size,
  281. TYPE_SYSTEM_BUS, NULL, "main-system-bus");
  282. OBJECT(main_system_bus)->free = g_free;
  283. }
  284. BusState *sysbus_get_default(void)
  285. {
  286. if (!main_system_bus) {
  287. main_system_bus_create();
  288. }
  289. return main_system_bus;
  290. }
  291. static void sysbus_register_types(void)
  292. {
  293. type_register_static(&system_bus_info);
  294. type_register_static(&sysbus_device_type_info);
  295. }
  296. type_init(sysbus_register_types)