xref: /qemu/hw/core/sysbus.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
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_overlap(SysBusDevice * dev,int n,hwaddr addr,int priority)154 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
155                              int priority)
156 {
157     sysbus_mmio_map_common(dev, n, addr, true, priority);
158 }
159 
160 /* Request an IRQ source.  The actual IRQ object may be populated later.  */
sysbus_init_irq(SysBusDevice * dev,qemu_irq * p)161 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
162 {
163     qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1);
164 }
165 
166 /* Pass IRQs from a target device.  */
sysbus_pass_irq(SysBusDevice * dev,SysBusDevice * target)167 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
168 {
169     qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ);
170 }
171 
sysbus_init_mmio(SysBusDevice * dev,MemoryRegion * memory)172 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
173 {
174     int n;
175 
176     assert(dev->num_mmio < QDEV_MAX_MMIO);
177     n = dev->num_mmio++;
178     dev->mmio[n].addr = -1;
179     dev->mmio[n].memory = memory;
180 }
181 
sysbus_mmio_get_region(SysBusDevice * dev,int n)182 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
183 {
184     assert(n >= 0 && n < QDEV_MAX_MMIO);
185     return dev->mmio[n].memory;
186 }
187 
sysbus_init_ioports(SysBusDevice * dev,uint32_t ioport,uint32_t size)188 void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
189 {
190     uint32_t i;
191 
192     for (i = 0; i < size; i++) {
193         assert(dev->num_pio < QDEV_MAX_PIO);
194         dev->pio[dev->num_pio++] = ioport++;
195     }
196 }
197 
198 /* The purpose of preserving this empty realize function
199  * is to prevent the parent_realize field of some subclasses
200  * from being set to NULL to break the normal init/realize
201  * of some devices.
202  */
sysbus_device_realize(DeviceState * dev,Error ** errp)203 static void sysbus_device_realize(DeviceState *dev, Error **errp)
204 {
205 }
206 
sysbus_create_varargs(const char * name,hwaddr addr,...)207 DeviceState *sysbus_create_varargs(const char *name,
208                                    hwaddr addr, ...)
209 {
210     DeviceState *dev;
211     SysBusDevice *s;
212     va_list va;
213     qemu_irq irq;
214     int n;
215 
216     dev = qdev_new(name);
217     s = SYS_BUS_DEVICE(dev);
218     sysbus_realize_and_unref(s, &error_fatal);
219     if (addr != (hwaddr)-1) {
220         sysbus_mmio_map(s, 0, addr);
221     }
222     va_start(va, addr);
223     n = 0;
224     while (1) {
225         irq = va_arg(va, qemu_irq);
226         if (!irq) {
227             break;
228         }
229         sysbus_connect_irq(s, n, irq);
230         n++;
231     }
232     va_end(va);
233     return dev;
234 }
235 
sysbus_realize(SysBusDevice * dev,Error ** errp)236 bool sysbus_realize(SysBusDevice *dev, Error **errp)
237 {
238     return qdev_realize(DEVICE(dev), sysbus_get_default(), errp);
239 }
240 
sysbus_realize_and_unref(SysBusDevice * dev,Error ** errp)241 bool sysbus_realize_and_unref(SysBusDevice *dev, Error **errp)
242 {
243     return qdev_realize_and_unref(DEVICE(dev), sysbus_get_default(), errp);
244 }
245 
sysbus_dev_print(Monitor * mon,DeviceState * dev,int indent)246 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
247 {
248     SysBusDevice *s = SYS_BUS_DEVICE(dev);
249     hwaddr size;
250     int i;
251 
252     for (i = 0; i < s->num_mmio; i++) {
253         size = memory_region_size(s->mmio[i].memory);
254         monitor_printf(mon, "%*smmio " HWADDR_FMT_plx "/" HWADDR_FMT_plx "\n",
255                        indent, "", s->mmio[i].addr, size);
256     }
257 }
258 
sysbus_get_fw_dev_path(DeviceState * dev)259 static char *sysbus_get_fw_dev_path(DeviceState *dev)
260 {
261     SysBusDevice *s = SYS_BUS_DEVICE(dev);
262     SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s);
263     char *addr, *fw_dev_path;
264 
265     if (sbc->explicit_ofw_unit_address) {
266         addr = sbc->explicit_ofw_unit_address(s);
267         if (addr) {
268             fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr);
269             g_free(addr);
270             return fw_dev_path;
271         }
272     }
273     if (s->num_mmio) {
274         return g_strdup_printf("%s@" HWADDR_FMT_plx, qdev_fw_name(dev),
275                                s->mmio[0].addr);
276     }
277     if (s->num_pio) {
278         return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]);
279     }
280     return g_strdup(qdev_fw_name(dev));
281 }
282 
sysbus_device_class_init(ObjectClass * klass,const void * data)283 static void sysbus_device_class_init(ObjectClass *klass, const void *data)
284 {
285     DeviceClass *k = DEVICE_CLASS(klass);
286     k->realize = sysbus_device_realize;
287     k->bus_type = TYPE_SYSTEM_BUS;
288     /*
289      * device_add plugs devices into a suitable bus.  For "real" buses,
290      * that actually connects the device.  For sysbus, the connections
291      * need to be made separately, and device_add can't do that.  The
292      * device would be left unconnected, and will probably not work
293      *
294      * However, a few machines can handle device_add/-device with
295      * a few specific sysbus devices. In those cases, the device
296      * subclass needs to override it and set user_creatable=true.
297      */
298     k->user_creatable = false;
299 }
300 
301 static BusState *main_system_bus;
302 
main_system_bus_create(void)303 static void main_system_bus_create(void)
304 {
305     /*
306      * assign main_system_bus before qbus_init()
307      * in order to make "if (bus != sysbus_get_default())" work
308      */
309     main_system_bus = g_new0(BusState, 1);
310     qbus_init(main_system_bus, sizeof(BusState),
311               TYPE_SYSTEM_BUS, NULL, "main-system-bus");
312     OBJECT(main_system_bus)->free = g_free;
313 }
314 
sysbus_get_default(void)315 BusState *sysbus_get_default(void)
316 {
317     if (!main_system_bus) {
318         main_system_bus_create();
319     }
320     return main_system_bus;
321 }
322 
dynamic_sysbus_device_class_init(ObjectClass * klass,const void * data)323 static void dynamic_sysbus_device_class_init(ObjectClass *klass,
324                                              const void *data)
325 {
326     DeviceClass *k = DEVICE_CLASS(klass);
327 
328     k->user_creatable = true;
329     k->hotpluggable = false;
330 }
331 
332 static const TypeInfo sysbus_types[] = {
333     {
334         .name           = TYPE_SYSTEM_BUS,
335         .parent         = TYPE_BUS,
336         .instance_size  = sizeof(BusState),
337         .class_init     = system_bus_class_init,
338     },
339     {
340         .name           = TYPE_SYS_BUS_DEVICE,
341         .parent         = TYPE_DEVICE,
342         .instance_size  = sizeof(SysBusDevice),
343         .abstract       = true,
344         .class_size     = sizeof(SysBusDeviceClass),
345         .class_init     = sysbus_device_class_init,
346     },
347     {
348         .name           = TYPE_DYNAMIC_SYS_BUS_DEVICE,
349         .parent         = TYPE_SYS_BUS_DEVICE,
350         .class_init     = dynamic_sysbus_device_class_init,
351         .abstract       = true,
352     }
353 };
354 
355 DEFINE_TYPES(sysbus_types)
356