sysbus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 = container_get(qdev_get_machine(), "/peripheral");
  59. find_sysbus_device(container, &find);
  60. container = container_get(qdev_get_machine(), "/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_unmap(SysBusDevice *dev, int n)
  132. {
  133. assert(n >= 0 && n < dev->num_mmio);
  134. if (dev->mmio[n].addr != (hwaddr)-1) {
  135. memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
  136. dev->mmio[n].addr = (hwaddr)-1;
  137. }
  138. }
  139. void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
  140. {
  141. sysbus_mmio_map_common(dev, n, addr, false, 0);
  142. }
  143. void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
  144. int priority)
  145. {
  146. sysbus_mmio_map_common(dev, n, addr, true, priority);
  147. }
  148. /* Request an IRQ source. The actual IRQ object may be populated later. */
  149. void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
  150. {
  151. qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
  152. }
  153. /* Pass IRQs from a target device. */
  154. void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
  155. {
  156. qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
  157. }
  158. void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
  159. {
  160. int n;
  161. assert(dev->num_mmio < QDEV_MAX_MMIO);
  162. n = dev->num_mmio++;
  163. dev->mmio[n].addr = -1;
  164. dev->mmio[n].memory = memory;
  165. }
  166. MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
  167. {
  168. assert(n >= 0 && n < QDEV_MAX_MMIO);
  169. return dev->mmio[n].memory;
  170. }
  171. void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
  172. {
  173. uint32_t i;
  174. for (i = 0; i < size; i++) {
  175. assert(dev->num_pio < QDEV_MAX_PIO);
  176. dev->pio[dev->num_pio++] = ioport++;
  177. }
  178. }
  179. /* The purpose of preserving this empty realize function
  180. * is to prevent the parent_realize field of some subclasses
  181. * from being set to NULL to break the normal init/realize
  182. * of some devices.
  183. */
  184. static void sysbus_device_realize(DeviceState *dev, Error **errp)
  185. {
  186. }
  187. DeviceState *sysbus_create_varargs(const char *name,
  188. hwaddr addr, ...)
  189. {
  190. DeviceState *dev;
  191. SysBusDevice *s;
  192. va_list va;
  193. qemu_irq irq;
  194. int n;
  195. dev = qdev_new(name);
  196. s = SYS_BUS_DEVICE(dev);
  197. sysbus_realize_and_unref(s, &error_fatal);
  198. if (addr != (hwaddr)-1) {
  199. sysbus_mmio_map(s, 0, addr);
  200. }
  201. va_start(va, addr);
  202. n = 0;
  203. while (1) {
  204. irq = va_arg(va, qemu_irq);
  205. if (!irq) {
  206. break;
  207. }
  208. sysbus_connect_irq(s, n, irq);
  209. n++;
  210. }
  211. va_end(va);
  212. return dev;
  213. }
  214. bool sysbus_realize(SysBusDevice *dev, Error **errp)
  215. {
  216. return qdev_realize(DEVICE(dev), sysbus_get_default(), errp);
  217. }
  218. bool sysbus_realize_and_unref(SysBusDevice *dev, Error **errp)
  219. {
  220. return qdev_realize_and_unref(DEVICE(dev), sysbus_get_default(), errp);
  221. }
  222. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  223. {
  224. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  225. hwaddr size;
  226. int i;
  227. for (i = 0; i < s->num_mmio; i++) {
  228. size = memory_region_size(s->mmio[i].memory);
  229. monitor_printf(mon, "%*smmio " HWADDR_FMT_plx "/" HWADDR_FMT_plx "\n",
  230. indent, "", s->mmio[i].addr, size);
  231. }
  232. }
  233. static char *sysbus_get_fw_dev_path(DeviceState *dev)
  234. {
  235. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  236. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
  237. char *addr, *fw_dev_path;
  238. if (sbc->explicit_ofw_unit_address) {
  239. addr = sbc->explicit_ofw_unit_address(s);
  240. if (addr) {
  241. fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
  242. g_free(addr);
  243. return fw_dev_path;
  244. }
  245. }
  246. if (s->num_mmio) {
  247. return g_strdup_printf("%s@" HWADDR_FMT_plx, qdev_fw_name(dev),
  248. s->mmio[0].addr);
  249. }
  250. if (s->num_pio) {
  251. return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
  252. }
  253. return g_strdup(qdev_fw_name(dev));
  254. }
  255. void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
  256. MemoryRegion *mem)
  257. {
  258. memory_region_add_subregion(get_system_io(), addr, mem);
  259. }
  260. MemoryRegion *sysbus_address_space(SysBusDevice *dev)
  261. {
  262. return get_system_memory();
  263. }
  264. static void sysbus_device_class_init(ObjectClass *klass, void *data)
  265. {
  266. DeviceClass *k = DEVICE_CLASS(klass);
  267. k->realize = sysbus_device_realize;
  268. k->bus_type = TYPE_SYSTEM_BUS;
  269. /*
  270. * device_add plugs devices into a suitable bus. For "real" buses,
  271. * that actually connects the device. For sysbus, the connections
  272. * need to be made separately, and device_add can't do that. The
  273. * device would be left unconnected, and will probably not work
  274. *
  275. * However, a few machines can handle device_add/-device with
  276. * a few specific sysbus devices. In those cases, the device
  277. * subclass needs to override it and set user_creatable=true.
  278. */
  279. k->user_creatable = false;
  280. }
  281. static const TypeInfo sysbus_device_type_info = {
  282. .name = TYPE_SYS_BUS_DEVICE,
  283. .parent = TYPE_DEVICE,
  284. .instance_size = sizeof(SysBusDevice),
  285. .abstract = true,
  286. .class_size = sizeof(SysBusDeviceClass),
  287. .class_init = sysbus_device_class_init,
  288. };
  289. static BusState *main_system_bus;
  290. static void main_system_bus_create(void)
  291. {
  292. /*
  293. * assign main_system_bus before qbus_init()
  294. * in order to make "if (bus != sysbus_get_default())" work
  295. */
  296. main_system_bus = g_malloc0(system_bus_info.instance_size);
  297. qbus_init(main_system_bus, system_bus_info.instance_size,
  298. TYPE_SYSTEM_BUS, NULL, "main-system-bus");
  299. OBJECT(main_system_bus)->free = g_free;
  300. }
  301. BusState *sysbus_get_default(void)
  302. {
  303. if (!main_system_bus) {
  304. main_system_bus_create();
  305. }
  306. return main_system_bus;
  307. }
  308. static void sysbus_register_types(void)
  309. {
  310. type_register_static(&system_bus_info);
  311. type_register_static(&sysbus_device_type_info);
  312. }
  313. type_init(sysbus_register_types)