2
0

sysbus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "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 spawened 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, NULL);
  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. return dev->mmio[n].memory;
  169. }
  170. void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
  171. {
  172. uint32_t i;
  173. for (i = 0; i < size; i++) {
  174. assert(dev->num_pio < QDEV_MAX_PIO);
  175. dev->pio[dev->num_pio++] = ioport++;
  176. }
  177. }
  178. /* The purpose of preserving this empty realize function
  179. * is to prevent the parent_realize field of some subclasses
  180. * from being set to NULL to break the normal init/realize
  181. * of some devices.
  182. */
  183. static void sysbus_realize(DeviceState *dev, Error **errp)
  184. {
  185. }
  186. DeviceState *sysbus_create_varargs(const char *name,
  187. hwaddr addr, ...)
  188. {
  189. DeviceState *dev;
  190. SysBusDevice *s;
  191. va_list va;
  192. qemu_irq irq;
  193. int n;
  194. dev = qdev_create(NULL, name);
  195. s = SYS_BUS_DEVICE(dev);
  196. qdev_init_nofail(dev);
  197. if (addr != (hwaddr)-1) {
  198. sysbus_mmio_map(s, 0, addr);
  199. }
  200. va_start(va, addr);
  201. n = 0;
  202. while (1) {
  203. irq = va_arg(va, qemu_irq);
  204. if (!irq) {
  205. break;
  206. }
  207. sysbus_connect_irq(s, n, irq);
  208. n++;
  209. }
  210. va_end(va);
  211. return dev;
  212. }
  213. DeviceState *sysbus_try_create_varargs(const char *name,
  214. hwaddr addr, ...)
  215. {
  216. DeviceState *dev;
  217. SysBusDevice *s;
  218. va_list va;
  219. qemu_irq irq;
  220. int n;
  221. dev = qdev_try_create(NULL, name);
  222. if (!dev) {
  223. return NULL;
  224. }
  225. s = SYS_BUS_DEVICE(dev);
  226. qdev_init_nofail(dev);
  227. if (addr != (hwaddr)-1) {
  228. sysbus_mmio_map(s, 0, addr);
  229. }
  230. va_start(va, addr);
  231. n = 0;
  232. while (1) {
  233. irq = va_arg(va, qemu_irq);
  234. if (!irq) {
  235. break;
  236. }
  237. sysbus_connect_irq(s, n, irq);
  238. n++;
  239. }
  240. va_end(va);
  241. return dev;
  242. }
  243. static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  244. {
  245. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  246. hwaddr size;
  247. int i;
  248. for (i = 0; i < s->num_mmio; i++) {
  249. size = memory_region_size(s->mmio[i].memory);
  250. monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
  251. indent, "", s->mmio[i].addr, size);
  252. }
  253. }
  254. static char *sysbus_get_fw_dev_path(DeviceState *dev)
  255. {
  256. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  257. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
  258. char *addr, *fw_dev_path;
  259. if (sbc->explicit_ofw_unit_address) {
  260. addr = sbc->explicit_ofw_unit_address(s);
  261. if (addr) {
  262. fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
  263. g_free(addr);
  264. return fw_dev_path;
  265. }
  266. }
  267. if (s->num_mmio) {
  268. return g_strdup_printf("%s@" TARGET_FMT_plx, qdev_fw_name(dev),
  269. s->mmio[0].addr);
  270. }
  271. if (s->num_pio) {
  272. return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
  273. }
  274. return g_strdup(qdev_fw_name(dev));
  275. }
  276. void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
  277. MemoryRegion *mem)
  278. {
  279. memory_region_add_subregion(get_system_io(), addr, mem);
  280. }
  281. MemoryRegion *sysbus_address_space(SysBusDevice *dev)
  282. {
  283. return get_system_memory();
  284. }
  285. static void sysbus_device_class_init(ObjectClass *klass, void *data)
  286. {
  287. DeviceClass *k = DEVICE_CLASS(klass);
  288. k->realize = sysbus_realize;
  289. k->bus_type = TYPE_SYSTEM_BUS;
  290. /*
  291. * device_add plugs devices into a suitable bus. For "real" buses,
  292. * that actually connects the device. For sysbus, the connections
  293. * need to be made separately, and device_add can't do that. The
  294. * device would be left unconnected, and will probably not work
  295. *
  296. * However, a few machines can handle device_add/-device with
  297. * a few specific sysbus devices. In those cases, the device
  298. * subclass needs to override it and set user_creatable=true.
  299. */
  300. k->user_creatable = false;
  301. }
  302. static const TypeInfo sysbus_device_type_info = {
  303. .name = TYPE_SYS_BUS_DEVICE,
  304. .parent = TYPE_DEVICE,
  305. .instance_size = sizeof(SysBusDevice),
  306. .abstract = true,
  307. .class_size = sizeof(SysBusDeviceClass),
  308. .class_init = sysbus_device_class_init,
  309. };
  310. /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
  311. static BusState *main_system_bus;
  312. static void main_system_bus_create(void)
  313. {
  314. /* assign main_system_bus before qbus_create_inplace()
  315. * in order to make "if (bus != sysbus_get_default())" work */
  316. main_system_bus = g_malloc0(system_bus_info.instance_size);
  317. qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
  318. TYPE_SYSTEM_BUS, NULL, "main-system-bus");
  319. OBJECT(main_system_bus)->free = g_free;
  320. }
  321. BusState *sysbus_get_default(void)
  322. {
  323. if (!main_system_bus) {
  324. main_system_bus_create();
  325. }
  326. return main_system_bus;
  327. }
  328. void sysbus_init_child_obj(Object *parent, const char *childname, void *child,
  329. size_t childsize, const char *childtype)
  330. {
  331. object_initialize_child(parent, childname, child, childsize, childtype,
  332. &error_abort, NULL);
  333. qdev_set_parent_bus(DEVICE(child), sysbus_get_default());
  334. }
  335. static void sysbus_register_types(void)
  336. {
  337. type_register_static(&system_bus_info);
  338. type_register_static(&sysbus_device_type_info);
  339. }
  340. type_init(sysbus_register_types)