platform-bus.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Platform Bus device to support dynamic Sysbus devices
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author: Alexander Graf, <agraf@suse.de>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "hw/platform-bus.h"
  23. #include "hw/qdev-properties.h"
  24. #include "qapi/error.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/module.h"
  27. /*
  28. * Returns the PlatformBus IRQ number for a SysBusDevice irq number or -1 if
  29. * the IRQ is not mapped on this Platform bus.
  30. */
  31. int platform_bus_get_irqn(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  32. int n)
  33. {
  34. qemu_irq sbirq = sysbus_get_connected_irq(sbdev, n);
  35. int i;
  36. for (i = 0; i < pbus->num_irqs; i++) {
  37. if (pbus->irqs[i] == sbirq) {
  38. return i;
  39. }
  40. }
  41. /* IRQ not mapped on platform bus */
  42. return -1;
  43. }
  44. /*
  45. * Returns the PlatformBus MMIO region offset for Region n of a SysBusDevice or
  46. * -1 if the region is not mapped on this Platform bus.
  47. */
  48. hwaddr platform_bus_get_mmio_addr(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  49. int n)
  50. {
  51. MemoryRegion *pbus_mr = &pbus->mmio;
  52. MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
  53. Object *pbus_mr_obj = OBJECT(pbus_mr);
  54. Object *parent_mr;
  55. if (!memory_region_is_mapped(sbdev_mr)) {
  56. /* Region is not mapped? */
  57. return -1;
  58. }
  59. parent_mr = object_property_get_link(OBJECT(sbdev_mr), "container",
  60. &error_abort);
  61. if (parent_mr != pbus_mr_obj) {
  62. /* MMIO region is not mapped on platform bus */
  63. return -1;
  64. }
  65. return object_property_get_uint(OBJECT(sbdev_mr), "addr", NULL);
  66. }
  67. static void platform_bus_count_irqs(SysBusDevice *sbdev, void *opaque)
  68. {
  69. PlatformBusDevice *pbus = opaque;
  70. qemu_irq sbirq;
  71. int n, i;
  72. for (n = 0; ; n++) {
  73. if (!sysbus_has_irq(sbdev, n)) {
  74. break;
  75. }
  76. sbirq = sysbus_get_connected_irq(sbdev, n);
  77. for (i = 0; i < pbus->num_irqs; i++) {
  78. if (pbus->irqs[i] == sbirq) {
  79. bitmap_set(pbus->used_irqs, i, 1);
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. /*
  86. * Loop through all sysbus devices and look for unassigned IRQ lines as well as
  87. * unassociated MMIO regions. Connect them to the platform bus if available.
  88. */
  89. static void plaform_bus_refresh_irqs(PlatformBusDevice *pbus)
  90. {
  91. bitmap_zero(pbus->used_irqs, pbus->num_irqs);
  92. foreach_dynamic_sysbus_device(platform_bus_count_irqs, pbus);
  93. }
  94. static void platform_bus_map_irq(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  95. int n)
  96. {
  97. int max_irqs = pbus->num_irqs;
  98. int irqn;
  99. if (sysbus_is_irq_connected(sbdev, n)) {
  100. /* IRQ is already mapped, nothing to do */
  101. return;
  102. }
  103. irqn = find_first_zero_bit(pbus->used_irqs, max_irqs);
  104. if (irqn >= max_irqs) {
  105. error_report("Platform Bus: Can not fit IRQ line");
  106. exit(1);
  107. }
  108. set_bit(irqn, pbus->used_irqs);
  109. sysbus_connect_irq(sbdev, n, pbus->irqs[irqn]);
  110. }
  111. static void platform_bus_map_mmio(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  112. int n)
  113. {
  114. MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
  115. uint64_t size = memory_region_size(sbdev_mr);
  116. uint64_t alignment = (1ULL << (63 - clz64(size + size - 1)));
  117. uint64_t off;
  118. bool found_region = false;
  119. if (memory_region_is_mapped(sbdev_mr)) {
  120. /* Region is already mapped, nothing to do */
  121. return;
  122. }
  123. /*
  124. * Look for empty space in the MMIO space that is naturally aligned with
  125. * the target device's memory region
  126. */
  127. for (off = 0; off < pbus->mmio_size; off += alignment) {
  128. if (!memory_region_find(&pbus->mmio, off, size).mr) {
  129. found_region = true;
  130. break;
  131. }
  132. }
  133. if (!found_region) {
  134. error_report("Platform Bus: Can not fit MMIO region of size %"PRIx64,
  135. size);
  136. exit(1);
  137. }
  138. /* Map the device's region into our Platform Bus MMIO space */
  139. memory_region_add_subregion(&pbus->mmio, off, sbdev_mr);
  140. }
  141. /*
  142. * Look for unassigned IRQ lines as well as unassociated MMIO regions.
  143. * Connect them to the platform bus if available.
  144. */
  145. void platform_bus_link_device(PlatformBusDevice *pbus, SysBusDevice *sbdev)
  146. {
  147. int i;
  148. for (i = 0; sysbus_has_irq(sbdev, i); i++) {
  149. platform_bus_map_irq(pbus, sbdev, i);
  150. }
  151. for (i = 0; sysbus_has_mmio(sbdev, i); i++) {
  152. platform_bus_map_mmio(pbus, sbdev, i);
  153. }
  154. }
  155. static void platform_bus_realize(DeviceState *dev, Error **errp)
  156. {
  157. PlatformBusDevice *pbus;
  158. SysBusDevice *d;
  159. int i;
  160. d = SYS_BUS_DEVICE(dev);
  161. pbus = PLATFORM_BUS_DEVICE(dev);
  162. memory_region_init(&pbus->mmio, OBJECT(dev), "platform bus",
  163. pbus->mmio_size);
  164. sysbus_init_mmio(d, &pbus->mmio);
  165. pbus->used_irqs = bitmap_new(pbus->num_irqs);
  166. pbus->irqs = g_new0(qemu_irq, pbus->num_irqs);
  167. for (i = 0; i < pbus->num_irqs; i++) {
  168. sysbus_init_irq(d, &pbus->irqs[i]);
  169. }
  170. /* some devices might be initialized before so update used IRQs map */
  171. plaform_bus_refresh_irqs(pbus);
  172. }
  173. static Property platform_bus_properties[] = {
  174. DEFINE_PROP_UINT32("num_irqs", PlatformBusDevice, num_irqs, 0),
  175. DEFINE_PROP_UINT32("mmio_size", PlatformBusDevice, mmio_size, 0),
  176. DEFINE_PROP_END_OF_LIST()
  177. };
  178. static void platform_bus_class_init(ObjectClass *klass, void *data)
  179. {
  180. DeviceClass *dc = DEVICE_CLASS(klass);
  181. dc->realize = platform_bus_realize;
  182. device_class_set_props(dc, platform_bus_properties);
  183. }
  184. static const TypeInfo platform_bus_info = {
  185. .name = TYPE_PLATFORM_BUS_DEVICE,
  186. .parent = TYPE_SYS_BUS_DEVICE,
  187. .instance_size = sizeof(PlatformBusDevice),
  188. .class_init = platform_bus_class_init,
  189. };
  190. static void platform_bus_register_types(void)
  191. {
  192. type_register_static(&platform_bus_info);
  193. }
  194. type_init(platform_bus_register_types)