17634fe3cSAlexander Graf /*
27634fe3cSAlexander Graf * Platform Bus device to support dynamic Sysbus devices
37634fe3cSAlexander Graf *
47634fe3cSAlexander Graf * Copyright (C) 2014 Freescale Semiconductor, Inc. All rights reserved.
57634fe3cSAlexander Graf *
67634fe3cSAlexander Graf * Author: Alexander Graf, <agraf@suse.de>
77634fe3cSAlexander Graf *
87634fe3cSAlexander Graf * This library is free software; you can redistribute it and/or
97634fe3cSAlexander Graf * modify it under the terms of the GNU Lesser General Public
107634fe3cSAlexander Graf * License as published by the Free Software Foundation; either
1161f3c91aSChetan Pant * version 2.1 of the License, or (at your option) any later version.
127634fe3cSAlexander Graf *
137634fe3cSAlexander Graf * This library is distributed in the hope that it will be useful,
147634fe3cSAlexander Graf * but WITHOUT ANY WARRANTY; without even the implied warranty of
157634fe3cSAlexander Graf * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
167634fe3cSAlexander Graf * Lesser General Public License for more details.
177634fe3cSAlexander Graf *
187634fe3cSAlexander Graf * You should have received a copy of the GNU Lesser General Public
197634fe3cSAlexander Graf * License along with this library; if not, see <http://www.gnu.org/licenses/>.
207634fe3cSAlexander Graf */
217634fe3cSAlexander Graf
2218c86e2bSPeter Maydell #include "qemu/osdep.h"
237634fe3cSAlexander Graf #include "hw/platform-bus.h"
24a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
25552d7f49SMarkus Armbruster #include "qapi/error.h"
26c72fbf98SMarkus Armbruster #include "qemu/error-report.h"
270b8fa32fSMarkus Armbruster #include "qemu/module.h"
287634fe3cSAlexander Graf
297634fe3cSAlexander Graf
307634fe3cSAlexander Graf /*
317634fe3cSAlexander Graf * Returns the PlatformBus IRQ number for a SysBusDevice irq number or -1 if
327634fe3cSAlexander Graf * the IRQ is not mapped on this Platform bus.
337634fe3cSAlexander Graf */
platform_bus_get_irqn(PlatformBusDevice * pbus,SysBusDevice * sbdev,int n)347634fe3cSAlexander Graf int platform_bus_get_irqn(PlatformBusDevice *pbus, SysBusDevice *sbdev,
357634fe3cSAlexander Graf int n)
367634fe3cSAlexander Graf {
377634fe3cSAlexander Graf qemu_irq sbirq = sysbus_get_connected_irq(sbdev, n);
387634fe3cSAlexander Graf int i;
397634fe3cSAlexander Graf
407634fe3cSAlexander Graf for (i = 0; i < pbus->num_irqs; i++) {
417634fe3cSAlexander Graf if (pbus->irqs[i] == sbirq) {
427634fe3cSAlexander Graf return i;
437634fe3cSAlexander Graf }
447634fe3cSAlexander Graf }
457634fe3cSAlexander Graf
467634fe3cSAlexander Graf /* IRQ not mapped on platform bus */
477634fe3cSAlexander Graf return -1;
487634fe3cSAlexander Graf }
497634fe3cSAlexander Graf
507634fe3cSAlexander Graf /*
517634fe3cSAlexander Graf * Returns the PlatformBus MMIO region offset for Region n of a SysBusDevice or
527634fe3cSAlexander Graf * -1 if the region is not mapped on this Platform bus.
537634fe3cSAlexander Graf */
platform_bus_get_mmio_addr(PlatformBusDevice * pbus,SysBusDevice * sbdev,int n)547634fe3cSAlexander Graf hwaddr platform_bus_get_mmio_addr(PlatformBusDevice *pbus, SysBusDevice *sbdev,
557634fe3cSAlexander Graf int n)
567634fe3cSAlexander Graf {
577634fe3cSAlexander Graf MemoryRegion *pbus_mr = &pbus->mmio;
587634fe3cSAlexander Graf MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
597634fe3cSAlexander Graf Object *pbus_mr_obj = OBJECT(pbus_mr);
607634fe3cSAlexander Graf Object *parent_mr;
617634fe3cSAlexander Graf
627634fe3cSAlexander Graf if (!memory_region_is_mapped(sbdev_mr)) {
637634fe3cSAlexander Graf /* Region is not mapped? */
647634fe3cSAlexander Graf return -1;
657634fe3cSAlexander Graf }
667634fe3cSAlexander Graf
67552d7f49SMarkus Armbruster parent_mr = object_property_get_link(OBJECT(sbdev_mr), "container",
68552d7f49SMarkus Armbruster &error_abort);
697634fe3cSAlexander Graf if (parent_mr != pbus_mr_obj) {
707634fe3cSAlexander Graf /* MMIO region is not mapped on platform bus */
717634fe3cSAlexander Graf return -1;
727634fe3cSAlexander Graf }
737634fe3cSAlexander Graf
746d13643aSMarc-André Lureau return object_property_get_uint(OBJECT(sbdev_mr), "addr", NULL);
757634fe3cSAlexander Graf }
767634fe3cSAlexander Graf
platform_bus_count_irqs(SysBusDevice * sbdev,void * opaque)774f01a637SDavid Gibson static void platform_bus_count_irqs(SysBusDevice *sbdev, void *opaque)
787634fe3cSAlexander Graf {
797634fe3cSAlexander Graf PlatformBusDevice *pbus = opaque;
807634fe3cSAlexander Graf qemu_irq sbirq;
817634fe3cSAlexander Graf int n, i;
827634fe3cSAlexander Graf
837634fe3cSAlexander Graf for (n = 0; ; n++) {
847634fe3cSAlexander Graf if (!sysbus_has_irq(sbdev, n)) {
857634fe3cSAlexander Graf break;
867634fe3cSAlexander Graf }
877634fe3cSAlexander Graf
887634fe3cSAlexander Graf sbirq = sysbus_get_connected_irq(sbdev, n);
897634fe3cSAlexander Graf for (i = 0; i < pbus->num_irqs; i++) {
907634fe3cSAlexander Graf if (pbus->irqs[i] == sbirq) {
917634fe3cSAlexander Graf bitmap_set(pbus->used_irqs, i, 1);
927634fe3cSAlexander Graf break;
937634fe3cSAlexander Graf }
947634fe3cSAlexander Graf }
957634fe3cSAlexander Graf }
967634fe3cSAlexander Graf }
977634fe3cSAlexander Graf
987634fe3cSAlexander Graf /*
997634fe3cSAlexander Graf * Loop through all sysbus devices and look for unassigned IRQ lines as well as
1007634fe3cSAlexander Graf * unassociated MMIO regions. Connect them to the platform bus if available.
1017634fe3cSAlexander Graf */
plaform_bus_refresh_irqs(PlatformBusDevice * pbus)1027634fe3cSAlexander Graf static void plaform_bus_refresh_irqs(PlatformBusDevice *pbus)
1037634fe3cSAlexander Graf {
1047634fe3cSAlexander Graf bitmap_zero(pbus->used_irqs, pbus->num_irqs);
1057634fe3cSAlexander Graf foreach_dynamic_sysbus_device(platform_bus_count_irqs, pbus);
1067634fe3cSAlexander Graf }
1077634fe3cSAlexander Graf
platform_bus_map_irq(PlatformBusDevice * pbus,SysBusDevice * sbdev,int n)108c72fbf98SMarkus Armbruster static void platform_bus_map_irq(PlatformBusDevice *pbus, SysBusDevice *sbdev,
1097634fe3cSAlexander Graf int n)
1107634fe3cSAlexander Graf {
1117634fe3cSAlexander Graf int max_irqs = pbus->num_irqs;
1127634fe3cSAlexander Graf int irqn;
1137634fe3cSAlexander Graf
1147634fe3cSAlexander Graf if (sysbus_is_irq_connected(sbdev, n)) {
1157634fe3cSAlexander Graf /* IRQ is already mapped, nothing to do */
116c72fbf98SMarkus Armbruster return;
1177634fe3cSAlexander Graf }
1187634fe3cSAlexander Graf
1197634fe3cSAlexander Graf irqn = find_first_zero_bit(pbus->used_irqs, max_irqs);
1207634fe3cSAlexander Graf if (irqn >= max_irqs) {
121c72fbf98SMarkus Armbruster error_report("Platform Bus: Can not fit IRQ line");
122c72fbf98SMarkus Armbruster exit(1);
1237634fe3cSAlexander Graf }
1247634fe3cSAlexander Graf
1257634fe3cSAlexander Graf set_bit(irqn, pbus->used_irqs);
1267634fe3cSAlexander Graf sysbus_connect_irq(sbdev, n, pbus->irqs[irqn]);
1277634fe3cSAlexander Graf }
1287634fe3cSAlexander Graf
platform_bus_map_mmio(PlatformBusDevice * pbus,SysBusDevice * sbdev,int n)129c72fbf98SMarkus Armbruster static void platform_bus_map_mmio(PlatformBusDevice *pbus, SysBusDevice *sbdev,
1307634fe3cSAlexander Graf int n)
1317634fe3cSAlexander Graf {
1327634fe3cSAlexander Graf MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
1337634fe3cSAlexander Graf uint64_t size = memory_region_size(sbdev_mr);
1347634fe3cSAlexander Graf uint64_t alignment = (1ULL << (63 - clz64(size + size - 1)));
1357634fe3cSAlexander Graf uint64_t off;
1367634fe3cSAlexander Graf bool found_region = false;
1377634fe3cSAlexander Graf
1387634fe3cSAlexander Graf if (memory_region_is_mapped(sbdev_mr)) {
1397634fe3cSAlexander Graf /* Region is already mapped, nothing to do */
140c72fbf98SMarkus Armbruster return;
1417634fe3cSAlexander Graf }
1427634fe3cSAlexander Graf
1437634fe3cSAlexander Graf /*
1447634fe3cSAlexander Graf * Look for empty space in the MMIO space that is naturally aligned with
1457634fe3cSAlexander Graf * the target device's memory region
1467634fe3cSAlexander Graf */
1477634fe3cSAlexander Graf for (off = 0; off < pbus->mmio_size; off += alignment) {
148*99ec7b44SGao Shiyuan MemoryRegion *mr = memory_region_find(&pbus->mmio, off, size).mr;
149*99ec7b44SGao Shiyuan if (!mr) {
1507634fe3cSAlexander Graf found_region = true;
1517634fe3cSAlexander Graf break;
152*99ec7b44SGao Shiyuan } else {
153*99ec7b44SGao Shiyuan memory_region_unref(mr);
1547634fe3cSAlexander Graf }
1557634fe3cSAlexander Graf }
1567634fe3cSAlexander Graf
1577634fe3cSAlexander Graf if (!found_region) {
158c72fbf98SMarkus Armbruster error_report("Platform Bus: Can not fit MMIO region of size %"PRIx64,
159c72fbf98SMarkus Armbruster size);
160c72fbf98SMarkus Armbruster exit(1);
1617634fe3cSAlexander Graf }
1627634fe3cSAlexander Graf
1637634fe3cSAlexander Graf /* Map the device's region into our Platform Bus MMIO space */
1647634fe3cSAlexander Graf memory_region_add_subregion(&pbus->mmio, off, sbdev_mr);
1657634fe3cSAlexander Graf }
1667634fe3cSAlexander Graf
1677634fe3cSAlexander Graf /*
168a3fc8396SIgor Mammedov * Look for unassigned IRQ lines as well as unassociated MMIO regions.
169a3fc8396SIgor Mammedov * Connect them to the platform bus if available.
1707634fe3cSAlexander Graf */
platform_bus_link_device(PlatformBusDevice * pbus,SysBusDevice * sbdev)171a3fc8396SIgor Mammedov void platform_bus_link_device(PlatformBusDevice *pbus, SysBusDevice *sbdev)
1727634fe3cSAlexander Graf {
1737634fe3cSAlexander Graf int i;
1747634fe3cSAlexander Graf
1757634fe3cSAlexander Graf for (i = 0; sysbus_has_irq(sbdev, i); i++) {
1767634fe3cSAlexander Graf platform_bus_map_irq(pbus, sbdev, i);
1777634fe3cSAlexander Graf }
1787634fe3cSAlexander Graf
1797634fe3cSAlexander Graf for (i = 0; sysbus_has_mmio(sbdev, i); i++) {
1807634fe3cSAlexander Graf platform_bus_map_mmio(pbus, sbdev, i);
1817634fe3cSAlexander Graf }
1827634fe3cSAlexander Graf }
1837634fe3cSAlexander Graf
platform_bus_realize(DeviceState * dev,Error ** errp)1847634fe3cSAlexander Graf static void platform_bus_realize(DeviceState *dev, Error **errp)
1857634fe3cSAlexander Graf {
1867634fe3cSAlexander Graf PlatformBusDevice *pbus;
1877634fe3cSAlexander Graf SysBusDevice *d;
1887634fe3cSAlexander Graf int i;
1897634fe3cSAlexander Graf
1907634fe3cSAlexander Graf d = SYS_BUS_DEVICE(dev);
1917634fe3cSAlexander Graf pbus = PLATFORM_BUS_DEVICE(dev);
1927634fe3cSAlexander Graf
193de95af99SPhilippe Mathieu-Daudé memory_region_init(&pbus->mmio, OBJECT(dev), "platform bus",
194de95af99SPhilippe Mathieu-Daudé pbus->mmio_size);
1957634fe3cSAlexander Graf sysbus_init_mmio(d, &pbus->mmio);
1967634fe3cSAlexander Graf
1977634fe3cSAlexander Graf pbus->used_irqs = bitmap_new(pbus->num_irqs);
1987634fe3cSAlexander Graf pbus->irqs = g_new0(qemu_irq, pbus->num_irqs);
1997634fe3cSAlexander Graf for (i = 0; i < pbus->num_irqs; i++) {
2007634fe3cSAlexander Graf sysbus_init_irq(d, &pbus->irqs[i]);
2017634fe3cSAlexander Graf }
2027634fe3cSAlexander Graf
203a3fc8396SIgor Mammedov /* some devices might be initialized before so update used IRQs map */
204a3fc8396SIgor Mammedov plaform_bus_refresh_irqs(pbus);
2057634fe3cSAlexander Graf }
2067634fe3cSAlexander Graf
2077634fe3cSAlexander Graf static Property platform_bus_properties[] = {
2087634fe3cSAlexander Graf DEFINE_PROP_UINT32("num_irqs", PlatformBusDevice, num_irqs, 0),
2097634fe3cSAlexander Graf DEFINE_PROP_UINT32("mmio_size", PlatformBusDevice, mmio_size, 0),
2107634fe3cSAlexander Graf DEFINE_PROP_END_OF_LIST()
2117634fe3cSAlexander Graf };
2127634fe3cSAlexander Graf
platform_bus_class_init(ObjectClass * klass,void * data)2137634fe3cSAlexander Graf static void platform_bus_class_init(ObjectClass *klass, void *data)
2147634fe3cSAlexander Graf {
2157634fe3cSAlexander Graf DeviceClass *dc = DEVICE_CLASS(klass);
2167634fe3cSAlexander Graf
2177634fe3cSAlexander Graf dc->realize = platform_bus_realize;
2184f67d30bSMarc-André Lureau device_class_set_props(dc, platform_bus_properties);
2197634fe3cSAlexander Graf }
2207634fe3cSAlexander Graf
2217634fe3cSAlexander Graf static const TypeInfo platform_bus_info = {
2227634fe3cSAlexander Graf .name = TYPE_PLATFORM_BUS_DEVICE,
2237634fe3cSAlexander Graf .parent = TYPE_SYS_BUS_DEVICE,
2247634fe3cSAlexander Graf .instance_size = sizeof(PlatformBusDevice),
2257634fe3cSAlexander Graf .class_init = platform_bus_class_init,
2267634fe3cSAlexander Graf };
2277634fe3cSAlexander Graf
platform_bus_register_types(void)2287634fe3cSAlexander Graf static void platform_bus_register_types(void)
2297634fe3cSAlexander Graf {
2307634fe3cSAlexander Graf type_register_static(&platform_bus_info);
2317634fe3cSAlexander Graf }
2327634fe3cSAlexander Graf
2337634fe3cSAlexander Graf type_init(platform_bus_register_types)
234