sysbus.c 9.8 KB

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