platform-bus.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "qemu/error-report.h"
  25. #include "qemu/module.h"
  26. /*
  27. * Returns the PlatformBus IRQ number for a SysBusDevice irq number or -1 if
  28. * the IRQ is not mapped on this Platform bus.
  29. */
  30. int platform_bus_get_irqn(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  31. int n)
  32. {
  33. qemu_irq sbirq = sysbus_get_connected_irq(sbdev, n);
  34. int i;
  35. for (i = 0; i < pbus->num_irqs; i++) {
  36. if (pbus->irqs[i] == sbirq) {
  37. return i;
  38. }
  39. }
  40. /* IRQ not mapped on platform bus */
  41. return -1;
  42. }
  43. /*
  44. * Returns the PlatformBus MMIO region offset for Region n of a SysBusDevice or
  45. * -1 if the region is not mapped on this Platform bus.
  46. */
  47. hwaddr platform_bus_get_mmio_addr(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  48. int n)
  49. {
  50. MemoryRegion *pbus_mr = &pbus->mmio;
  51. MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
  52. Object *pbus_mr_obj = OBJECT(pbus_mr);
  53. Object *parent_mr;
  54. if (!memory_region_is_mapped(sbdev_mr)) {
  55. /* Region is not mapped? */
  56. return -1;
  57. }
  58. parent_mr = object_property_get_link(OBJECT(sbdev_mr), "container", NULL);
  59. assert(parent_mr);
  60. if (parent_mr != pbus_mr_obj) {
  61. /* MMIO region is not mapped on platform bus */
  62. return -1;
  63. }
  64. return object_property_get_uint(OBJECT(sbdev_mr), "addr", NULL);
  65. }
  66. static void platform_bus_count_irqs(SysBusDevice *sbdev, void *opaque)
  67. {
  68. PlatformBusDevice *pbus = opaque;
  69. qemu_irq sbirq;
  70. int n, i;
  71. for (n = 0; ; n++) {
  72. if (!sysbus_has_irq(sbdev, n)) {
  73. break;
  74. }
  75. sbirq = sysbus_get_connected_irq(sbdev, n);
  76. for (i = 0; i < pbus->num_irqs; i++) {
  77. if (pbus->irqs[i] == sbirq) {
  78. bitmap_set(pbus->used_irqs, i, 1);
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. /*
  85. * Loop through all sysbus devices and look for unassigned IRQ lines as well as
  86. * unassociated MMIO regions. Connect them to the platform bus if available.
  87. */
  88. static void plaform_bus_refresh_irqs(PlatformBusDevice *pbus)
  89. {
  90. bitmap_zero(pbus->used_irqs, pbus->num_irqs);
  91. foreach_dynamic_sysbus_device(platform_bus_count_irqs, pbus);
  92. }
  93. static void platform_bus_map_irq(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  94. int n)
  95. {
  96. int max_irqs = pbus->num_irqs;
  97. int irqn;
  98. if (sysbus_is_irq_connected(sbdev, n)) {
  99. /* IRQ is already mapped, nothing to do */
  100. return;
  101. }
  102. irqn = find_first_zero_bit(pbus->used_irqs, max_irqs);
  103. if (irqn >= max_irqs) {
  104. error_report("Platform Bus: Can not fit IRQ line");
  105. exit(1);
  106. }
  107. set_bit(irqn, pbus->used_irqs);
  108. sysbus_connect_irq(sbdev, n, pbus->irqs[irqn]);
  109. }
  110. static void platform_bus_map_mmio(PlatformBusDevice *pbus, SysBusDevice *sbdev,
  111. int n)
  112. {
  113. MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
  114. uint64_t size = memory_region_size(sbdev_mr);
  115. uint64_t alignment = (1ULL << (63 - clz64(size + size - 1)));
  116. uint64_t off;
  117. bool found_region = false;
  118. if (memory_region_is_mapped(sbdev_mr)) {
  119. /* Region is already mapped, nothing to do */
  120. return;
  121. }
  122. /*
  123. * Look for empty space in the MMIO space that is naturally aligned with
  124. * the target device's memory region
  125. */
  126. for (off = 0; off < pbus->mmio_size; off += alignment) {
  127. if (!memory_region_find(&pbus->mmio, off, size).mr) {
  128. found_region = true;
  129. break;
  130. }
  131. }
  132. if (!found_region) {
  133. error_report("Platform Bus: Can not fit MMIO region of size %"PRIx64,
  134. size);
  135. exit(1);
  136. }
  137. /* Map the device's region into our Platform Bus MMIO space */
  138. memory_region_add_subregion(&pbus->mmio, off, sbdev_mr);
  139. }
  140. /*
  141. * Look for unassigned IRQ lines as well as unassociated MMIO regions.
  142. * Connect them to the platform bus if available.
  143. */
  144. void platform_bus_link_device(PlatformBusDevice *pbus, SysBusDevice *sbdev)
  145. {
  146. int i;
  147. for (i = 0; sysbus_has_irq(sbdev, i); i++) {
  148. platform_bus_map_irq(pbus, sbdev, i);
  149. }
  150. for (i = 0; sysbus_has_mmio(sbdev, i); i++) {
  151. platform_bus_map_mmio(pbus, sbdev, i);
  152. }
  153. }
  154. static void platform_bus_realize(DeviceState *dev, Error **errp)
  155. {
  156. PlatformBusDevice *pbus;
  157. SysBusDevice *d;
  158. int i;
  159. d = SYS_BUS_DEVICE(dev);
  160. pbus = PLATFORM_BUS_DEVICE(dev);
  161. memory_region_init(&pbus->mmio, NULL, "platform bus", pbus->mmio_size);
  162. sysbus_init_mmio(d, &pbus->mmio);
  163. pbus->used_irqs = bitmap_new(pbus->num_irqs);
  164. pbus->irqs = g_new0(qemu_irq, pbus->num_irqs);
  165. for (i = 0; i < pbus->num_irqs; i++) {
  166. sysbus_init_irq(d, &pbus->irqs[i]);
  167. }
  168. /* some devices might be initialized before so update used IRQs map */
  169. plaform_bus_refresh_irqs(pbus);
  170. }
  171. static Property platform_bus_properties[] = {
  172. DEFINE_PROP_UINT32("num_irqs", PlatformBusDevice, num_irqs, 0),
  173. DEFINE_PROP_UINT32("mmio_size", PlatformBusDevice, mmio_size, 0),
  174. DEFINE_PROP_END_OF_LIST()
  175. };
  176. static void platform_bus_class_init(ObjectClass *klass, void *data)
  177. {
  178. DeviceClass *dc = DEVICE_CLASS(klass);
  179. dc->realize = platform_bus_realize;
  180. dc->props = platform_bus_properties;
  181. }
  182. static const TypeInfo platform_bus_info = {
  183. .name = TYPE_PLATFORM_BUS_DEVICE,
  184. .parent = TYPE_SYS_BUS_DEVICE,
  185. .instance_size = sizeof(PlatformBusDevice),
  186. .class_init = platform_bus_class_init,
  187. };
  188. static void platform_bus_register_types(void)
  189. {
  190. type_register_static(&platform_bus_info);
  191. }
  192. type_init(platform_bus_register_types)