xref: /openbmc/qemu/hw/core/sysbus.c (revision e452053097371880910c744a5d42ae2df058a4a7)
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.1 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 
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "hw/sysbus.h"
23 #include "monitor/monitor.h"
24 #include "system/address-spaces.h"
25 
26 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
27 static char *sysbus_get_fw_dev_path(DeviceState *dev);
28 
29 typedef struct SysBusFind {
30     void *opaque;
31     FindSysbusDeviceFunc *func;
32 } SysBusFind;
33 
34 /* Run func() for every sysbus device, traverse the tree for everything else */
find_sysbus_device(Object * obj,void * opaque)35 static int find_sysbus_device(Object *obj, void *opaque)
36 {
37     SysBusFind *find = opaque;
38     Object *dev;
39     SysBusDevice *sbdev;
40 
41     dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE);
42     sbdev = (SysBusDevice *)dev;
43 
44     if (!sbdev) {
45         /* Container, traverse it for children */
46         return object_child_foreach(obj, find_sysbus_device, opaque);
47     }
48 
49     find->func(sbdev, find->opaque);
50 
51     return 0;
52 }
53 
54 /*
55  * Loop through all dynamically created sysbus devices and call
56  * func() for each instance.
57  */
foreach_dynamic_sysbus_device(FindSysbusDeviceFunc * func,void * opaque)58 void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque)
59 {
60     Object *container;
61     SysBusFind find = {
62         .func = func,
63         .opaque = opaque,
64     };
65 
66     /* Loop through all sysbus devices that were spawned outside the machine */
67     container = machine_get_container("peripheral");
68     find_sysbus_device(container, &find);
69     container = machine_get_container("peripheral-anon");
70     find_sysbus_device(container, &find);
71 }
72 
73 
system_bus_class_init(ObjectClass * klass,const void * data)74 static void system_bus_class_init(ObjectClass *klass, const void *data)
75 {
76     BusClass *k = BUS_CLASS(klass);
77 
78     k->print_dev = sysbus_dev_print;
79     k->get_fw_dev_path = sysbus_get_fw_dev_path;
80 }
81 
82 /* Check whether an IRQ source exists */
sysbus_has_irq(SysBusDevice * dev,int n)83 bool sysbus_has_irq(SysBusDevice *dev, int n)
84 {
85     char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n);
86     ObjectProperty *r;
87 
88     r = object_property_find(OBJECT(dev), prop);
89     g_free(prop);
90 
91     return (r != NULL);
92 }
93 
sysbus_is_irq_connected(SysBusDevice * dev,int n)94 bool sysbus_is_irq_connected(SysBusDevice *dev, int n)
95 {
96     return !!sysbus_get_connected_irq(dev, n);
97 }
98 
sysbus_get_connected_irq(SysBusDevice * dev,int n)99 qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n)
100 {
101     DeviceState *d = DEVICE(dev);
102     return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n);
103 }
104 
sysbus_connect_irq(SysBusDevice * dev,int n,qemu_irq irq)105 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
106 {
107     SysBusDeviceClass *sbd = SYS_BUS_DEVICE_GET_CLASS(dev);
108 
109     qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq);
110 
111     if (sbd->connect_irq_notifier) {
112         sbd->connect_irq_notifier(dev, irq);
113     }
114 }
115 
116 /* Check whether an MMIO region exists */
sysbus_has_mmio(SysBusDevice * dev,unsigned int n)117 bool sysbus_has_mmio(SysBusDevice *dev, unsigned int n)
118 {
119     return (n < dev->num_mmio);
120 }
121 
sysbus_mmio_map_common(SysBusDevice * dev,int n,hwaddr addr,bool may_overlap,int priority)122 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
123                                    bool may_overlap, int priority)
124 {
125     assert(n >= 0 && n < dev->num_mmio);
126 
127     if (dev->mmio[n].addr == addr) {
128         /* ??? region already mapped here.  */
129         return;
130     }
131     if (dev->mmio[n].addr != (hwaddr)-1) {
132         /* Unregister previous mapping.  */
133         memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
134     }
135     dev->mmio[n].addr = addr;
136     if (may_overlap) {
137         memory_region_add_subregion_overlap(get_system_memory(),
138                                             addr,
139                                             dev->mmio[n].memory,
140                                             priority);
141     }
142     else {
143         memory_region_add_subregion(get_system_memory(),
144                                     addr,
145                                     dev->mmio[n].memory);
146     }
147 }
148 
sysbus_mmio_map(SysBusDevice * dev,int n,hwaddr addr)149 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
150 {
151     sysbus_mmio_map_common(dev, n, addr, false, 0);
152 }
153 
sysbus_mmio_map_name(SysBusDevice * dev,const char * name,hwaddr addr)154 int sysbus_mmio_map_name(SysBusDevice *dev, const char *name, hwaddr addr)
155 {
156     for (int i = 0; i < dev->num_mmio; i++) {
157         if (!strcmp(dev->mmio[i].memory->name, name)) {
158             sysbus_mmio_map(dev, i, addr);
159             return i;
160         }
161     }
162     return -1;
163 }
164 
sysbus_mmio_map_overlap(SysBusDevice * dev,int n,hwaddr addr,int priority)165 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
166                              int priority)
167 {
168     sysbus_mmio_map_common(dev, n, addr, true, priority);
169 }
170 
171 /* Request an IRQ source.  The actual IRQ object may be populated later.  */
sysbus_init_irq(SysBusDevice * dev,qemu_irq * p)172 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
173 {
174     qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
175 }
176 
177 /* Pass IRQs from a target device.  */
sysbus_pass_irq(SysBusDevice * dev,SysBusDevice * target)178 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
179 {
180     qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
181 }
182 
sysbus_init_mmio(SysBusDevice * dev,MemoryRegion * memory)183 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
184 {
185     int n;
186 
187     assert(dev->num_mmio < QDEV_MAX_MMIO);
188     n = dev->num_mmio++;
189     dev->mmio[n].addr = -1;
190     dev->mmio[n].memory = memory;
191 }
192 
sysbus_mmio_get_region(SysBusDevice * dev,int n)193 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
194 {
195     assert(n >= 0 && n < QDEV_MAX_MMIO);
196     return dev->mmio[n].memory;
197 }
198 
sysbus_init_ioports(SysBusDevice * dev,uint32_t ioport,uint32_t size)199 void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
200 {
201     uint32_t i;
202 
203     for (i = 0; i < size; i++) {
204         assert(dev->num_pio < QDEV_MAX_PIO);
205         dev->pio[dev->num_pio++] = ioport++;
206     }
207 }
208 
209 /* The purpose of preserving this empty realize function
210  * is to prevent the parent_realize field of some subclasses
211  * from being set to NULL to break the normal init/realize
212  * of some devices.
213  */
sysbus_device_realize(DeviceState * dev,Error ** errp)214 static void sysbus_device_realize(DeviceState *dev, Error **errp)
215 {
216 }
217 
sysbus_create_varargs(const char * name,hwaddr addr,...)218 DeviceState *sysbus_create_varargs(const char *name,
219                                    hwaddr addr, ...)
220 {
221     DeviceState *dev;
222     SysBusDevice *s;
223     va_list va;
224     qemu_irq irq;
225     int n;
226 
227     dev = qdev_new(name);
228     s = SYS_BUS_DEVICE(dev);
229     sysbus_realize_and_unref(s, &error_fatal);
230     if (addr != (hwaddr)-1) {
231         sysbus_mmio_map(s, 0, addr);
232     }
233     va_start(va, addr);
234     n = 0;
235     while (1) {
236         irq = va_arg(va, qemu_irq);
237         if (!irq) {
238             break;
239         }
240         sysbus_connect_irq(s, n, irq);
241         n++;
242     }
243     va_end(va);
244     return dev;
245 }
246 
sysbus_realize(SysBusDevice * dev,Error ** errp)247 bool sysbus_realize(SysBusDevice *dev, Error **errp)
248 {
249     return qdev_realize(DEVICE(dev), sysbus_get_default(), errp);
250 }
251 
sysbus_realize_and_unref(SysBusDevice * dev,Error ** errp)252 bool sysbus_realize_and_unref(SysBusDevice *dev, Error **errp)
253 {
254     return qdev_realize_and_unref(DEVICE(dev), sysbus_get_default(), errp);
255 }
256 
sysbus_dev_print(Monitor * mon,DeviceState * dev,int indent)257 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
258 {
259     SysBusDevice *s = SYS_BUS_DEVICE(dev);
260     hwaddr size;
261     int i;
262 
263     for (i = 0; i < s->num_mmio; i++) {
264         size = memory_region_size(s->mmio[i].memory);
265         monitor_printf(mon, "%*smmio " HWADDR_FMT_plx "/" HWADDR_FMT_plx "\n",
266                        indent, "", s->mmio[i].addr, size);
267     }
268 }
269 
sysbus_get_fw_dev_path(DeviceState * dev)270 static char *sysbus_get_fw_dev_path(DeviceState *dev)
271 {
272     SysBusDevice *s = SYS_BUS_DEVICE(dev);
273     SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
274     char *addr, *fw_dev_path;
275 
276     if (sbc->explicit_ofw_unit_address) {
277         addr = sbc->explicit_ofw_unit_address(s);
278         if (addr) {
279             fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
280             g_free(addr);
281             return fw_dev_path;
282         }
283     }
284     if (s->num_mmio) {
285         return g_strdup_printf("%s@" HWADDR_FMT_plx, qdev_fw_name(dev),
286                                s->mmio[0].addr);
287     }
288     if (s->num_pio) {
289         return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
290     }
291     return g_strdup(qdev_fw_name(dev));
292 }
293 
sysbus_device_class_init(ObjectClass * klass,const void * data)294 static void sysbus_device_class_init(ObjectClass *klass, const void *data)
295 {
296     DeviceClass *k = DEVICE_CLASS(klass);
297     k->realize = sysbus_device_realize;
298     k->bus_type = TYPE_SYSTEM_BUS;
299     /*
300      * device_add plugs devices into a suitable bus.  For "real" buses,
301      * that actually connects the device.  For sysbus, the connections
302      * need to be made separately, and device_add can't do that.  The
303      * device would be left unconnected, and will probably not work
304      *
305      * However, a few machines can handle device_add/-device with
306      * a few specific sysbus devices. In those cases, the device
307      * subclass needs to override it and set user_creatable=true.
308      */
309     k->user_creatable = false;
310 }
311 
312 static BusState *main_system_bus;
313 
main_system_bus_create(void)314 static void main_system_bus_create(void)
315 {
316     /*
317      * assign main_system_bus before qbus_init()
318      * in order to make "if (bus != sysbus_get_default())" work
319      */
320     main_system_bus = g_new0(BusState, 1);
321     qbus_init(main_system_bus, sizeof(BusState),
322               TYPE_SYSTEM_BUS, NULL, "main-system-bus");
323     OBJECT(main_system_bus)->free = g_free;
324 }
325 
sysbus_get_default(void)326 BusState *sysbus_get_default(void)
327 {
328     if (!main_system_bus) {
329         main_system_bus_create();
330     }
331     return main_system_bus;
332 }
333 
dynamic_sysbus_device_class_init(ObjectClass * klass,const void * data)334 static void dynamic_sysbus_device_class_init(ObjectClass *klass,
335                                              const void *data)
336 {
337     DeviceClass *k = DEVICE_CLASS(klass);
338 
339     k->user_creatable = true;
340     k->hotpluggable = false;
341 }
342 
343 static const TypeInfo sysbus_types[] = {
344     {
345         .name           = TYPE_SYSTEM_BUS,
346         .parent         = TYPE_BUS,
347         .instance_size  = sizeof(BusState),
348         .class_init     = system_bus_class_init,
349     },
350     {
351         .name           = TYPE_SYS_BUS_DEVICE,
352         .parent         = TYPE_DEVICE,
353         .instance_size  = sizeof(SysBusDevice),
354         .abstract       = true,
355         .class_size     = sizeof(SysBusDeviceClass),
356         .class_init     = sysbus_device_class_init,
357     },
358     {
359         .name           = TYPE_DYNAMIC_SYS_BUS_DEVICE,
360         .parent         = TYPE_SYS_BUS_DEVICE,
361         .class_init     = dynamic_sysbus_device_class_init,
362         .abstract       = true,
363     }
364 };
365 
366 DEFINE_TYPES(sysbus_types)
367