xref: /openbmc/qemu/hw/core/platform-bus.c (revision 77a8257e)
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 
22 #include "hw/platform-bus.h"
23 #include "monitor/monitor.h"
24 #include "exec/address-spaces.h"
25 #include "sysemu/sysemu.h"
26 
27 
28 /*
29  * Returns the PlatformBus IRQ number for a SysBusDevice irq number or -1 if
30  * the IRQ is not mapped on this Platform bus.
31  */
32 int platform_bus_get_irqn(PlatformBusDevice *pbus, SysBusDevice *sbdev,
33                           int n)
34 {
35     qemu_irq sbirq = sysbus_get_connected_irq(sbdev, n);
36     int i;
37 
38     for (i = 0; i < pbus->num_irqs; i++) {
39         if (pbus->irqs[i] == sbirq) {
40             return i;
41         }
42     }
43 
44     /* IRQ not mapped on platform bus */
45     return -1;
46 }
47 
48 /*
49  * Returns the PlatformBus MMIO region offset for Region n of a SysBusDevice or
50  * -1 if the region is not mapped on this Platform bus.
51  */
52 hwaddr platform_bus_get_mmio_addr(PlatformBusDevice *pbus, SysBusDevice *sbdev,
53                                   int n)
54 {
55     MemoryRegion *pbus_mr = &pbus->mmio;
56     MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
57     Object *pbus_mr_obj = OBJECT(pbus_mr);
58     Object *parent_mr;
59 
60     if (!memory_region_is_mapped(sbdev_mr)) {
61         /* Region is not mapped? */
62         return -1;
63     }
64 
65     parent_mr = object_property_get_link(OBJECT(sbdev_mr), "container", NULL);
66 
67     assert(parent_mr);
68     if (parent_mr != pbus_mr_obj) {
69         /* MMIO region is not mapped on platform bus */
70         return -1;
71     }
72 
73     return object_property_get_int(OBJECT(sbdev_mr), "addr", NULL);
74 }
75 
76 static int platform_bus_count_irqs(SysBusDevice *sbdev, void *opaque)
77 {
78     PlatformBusDevice *pbus = opaque;
79     qemu_irq sbirq;
80     int n, i;
81 
82     for (n = 0; ; n++) {
83         if (!sysbus_has_irq(sbdev, n)) {
84             break;
85         }
86 
87         sbirq = sysbus_get_connected_irq(sbdev, n);
88         for (i = 0; i < pbus->num_irqs; i++) {
89             if (pbus->irqs[i] == sbirq) {
90                 bitmap_set(pbus->used_irqs, i, 1);
91                 break;
92             }
93         }
94     }
95 
96     return 0;
97 }
98 
99 /*
100  * Loop through all sysbus devices and look for unassigned IRQ lines as well as
101  * unassociated MMIO regions. Connect them to the platform bus if available.
102  */
103 static void plaform_bus_refresh_irqs(PlatformBusDevice *pbus)
104 {
105     bitmap_zero(pbus->used_irqs, pbus->num_irqs);
106     foreach_dynamic_sysbus_device(platform_bus_count_irqs, pbus);
107     pbus->done_gathering = true;
108 }
109 
110 static int platform_bus_map_irq(PlatformBusDevice *pbus, SysBusDevice *sbdev,
111                                 int n)
112 {
113     int max_irqs = pbus->num_irqs;
114     int irqn;
115 
116     if (sysbus_is_irq_connected(sbdev, n)) {
117         /* IRQ is already mapped, nothing to do */
118         return 0;
119     }
120 
121     irqn = find_first_zero_bit(pbus->used_irqs, max_irqs);
122     if (irqn >= max_irqs) {
123         hw_error("Platform Bus: Can not fit IRQ line");
124         return -1;
125     }
126 
127     set_bit(irqn, pbus->used_irqs);
128     sysbus_connect_irq(sbdev, n, pbus->irqs[irqn]);
129 
130     return 0;
131 }
132 
133 static int platform_bus_map_mmio(PlatformBusDevice *pbus, SysBusDevice *sbdev,
134                                  int n)
135 {
136     MemoryRegion *sbdev_mr = sysbus_mmio_get_region(sbdev, n);
137     uint64_t size = memory_region_size(sbdev_mr);
138     uint64_t alignment = (1ULL << (63 - clz64(size + size - 1)));
139     uint64_t off;
140     bool found_region = false;
141 
142     if (memory_region_is_mapped(sbdev_mr)) {
143         /* Region is already mapped, nothing to do */
144         return 0;
145     }
146 
147     /*
148      * Look for empty space in the MMIO space that is naturally aligned with
149      * the target device's memory region
150      */
151     for (off = 0; off < pbus->mmio_size; off += alignment) {
152         if (!memory_region_find(&pbus->mmio, off, size).mr) {
153             found_region = true;
154             break;
155         }
156     }
157 
158     if (!found_region) {
159         hw_error("Platform Bus: Can not fit MMIO region of size %"PRIx64, size);
160     }
161 
162     /* Map the device's region into our Platform Bus MMIO space */
163     memory_region_add_subregion(&pbus->mmio, off, sbdev_mr);
164 
165     return 0;
166 }
167 
168 /*
169  * For each sysbus device, look for unassigned IRQ lines as well as
170  * unassociated MMIO regions. Connect them to the platform bus if available.
171  */
172 static int link_sysbus_device(SysBusDevice *sbdev, void *opaque)
173 {
174     PlatformBusDevice *pbus = opaque;
175     int i;
176 
177     for (i = 0; sysbus_has_irq(sbdev, i); i++) {
178         platform_bus_map_irq(pbus, sbdev, i);
179     }
180 
181     for (i = 0; sysbus_has_mmio(sbdev, i); i++) {
182         platform_bus_map_mmio(pbus, sbdev, i);
183     }
184 
185     return 0;
186 }
187 
188 static void platform_bus_init_notify(Notifier *notifier, void *data)
189 {
190     PlatformBusDevice *pb = container_of(notifier, PlatformBusDevice, notifier);
191 
192     /*
193      * Generate a bitmap of used IRQ lines, as the user might have specified
194      * them on the command line.
195      */
196     plaform_bus_refresh_irqs(pb);
197 
198     foreach_dynamic_sysbus_device(link_sysbus_device, pb);
199 }
200 
201 static void platform_bus_realize(DeviceState *dev, Error **errp)
202 {
203     PlatformBusDevice *pbus;
204     SysBusDevice *d;
205     int i;
206 
207     d = SYS_BUS_DEVICE(dev);
208     pbus = PLATFORM_BUS_DEVICE(dev);
209 
210     memory_region_init(&pbus->mmio, NULL, "platform bus", pbus->mmio_size);
211     sysbus_init_mmio(d, &pbus->mmio);
212 
213     pbus->used_irqs = bitmap_new(pbus->num_irqs);
214     pbus->irqs = g_new0(qemu_irq, pbus->num_irqs);
215     for (i = 0; i < pbus->num_irqs; i++) {
216         sysbus_init_irq(d, &pbus->irqs[i]);
217     }
218 
219     /*
220      * Register notifier that allows us to gather dangling devices once the
221      * machine is completely assembled
222      */
223     pbus->notifier.notify = platform_bus_init_notify;
224     qemu_add_machine_init_done_notifier(&pbus->notifier);
225 }
226 
227 static Property platform_bus_properties[] = {
228     DEFINE_PROP_UINT32("num_irqs", PlatformBusDevice, num_irqs, 0),
229     DEFINE_PROP_UINT32("mmio_size", PlatformBusDevice, mmio_size, 0),
230     DEFINE_PROP_END_OF_LIST()
231 };
232 
233 static void platform_bus_class_init(ObjectClass *klass, void *data)
234 {
235     DeviceClass *dc = DEVICE_CLASS(klass);
236 
237     dc->realize = platform_bus_realize;
238     dc->props = platform_bus_properties;
239 }
240 
241 static const TypeInfo platform_bus_info = {
242     .name          = TYPE_PLATFORM_BUS_DEVICE,
243     .parent        = TYPE_SYS_BUS_DEVICE,
244     .instance_size = sizeof(PlatformBusDevice),
245     .class_init    = platform_bus_class_init,
246 };
247 
248 static void platform_bus_register_types(void)
249 {
250     type_register_static(&platform_bus_info);
251 }
252 
253 type_init(platform_bus_register_types)
254