]> git.proxmox.com Git - qemu.git/blame - hw/sysbus.c
virtio-blk: cleanup: init and exit functions.
[qemu.git] / hw / sysbus.c
CommitLineData
aae9460e
PB
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 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
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aae9460e
PB
18 */
19
83c9f4ca 20#include "hw/sysbus.h"
83c9089e 21#include "monitor/monitor.h"
022c62cb 22#include "exec/address-spaces.h"
aae9460e 23
10c4c98a 24static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
c646f74f 25static char *sysbus_get_fw_dev_path(DeviceState *dev);
10c4c98a 26
0d936928
AL
27static void system_bus_class_init(ObjectClass *klass, void *data)
28{
29 BusClass *k = BUS_CLASS(klass);
30
31 k->print_dev = sysbus_dev_print;
32 k->get_fw_dev_path = sysbus_get_fw_dev_path;
33}
34
35static const TypeInfo system_bus_info = {
36 .name = TYPE_SYSTEM_BUS,
37 .parent = TYPE_BUS,
38 .instance_size = sizeof(BusState),
39 .class_init = system_bus_class_init,
10c4c98a
GH
40};
41
aae9460e
PB
42void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
43{
44 assert(n >= 0 && n < dev->num_irq);
660f11be 45 dev->irqs[n] = NULL;
aae9460e
PB
46 if (dev->irqp[n]) {
47 *dev->irqp[n] = irq;
48 }
49}
50
7feb640c
AK
51static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr,
52 bool may_overlap, unsigned priority)
aae9460e
PB
53{
54 assert(n >= 0 && n < dev->num_mmio);
55
56 if (dev->mmio[n].addr == addr) {
57 /* ??? region already mapped here. */
58 return;
59 }
a8170e5e 60 if (dev->mmio[n].addr != (hwaddr)-1) {
aae9460e 61 /* Unregister previous mapping. */
e114fead 62 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
aae9460e
PB
63 }
64 dev->mmio[n].addr = addr;
7feb640c
AK
65 if (may_overlap) {
66 memory_region_add_subregion_overlap(get_system_memory(),
67 addr,
68 dev->mmio[n].memory,
69 priority);
70 }
71 else {
72 memory_region_add_subregion(get_system_memory(),
73 addr,
74 dev->mmio[n].memory);
75 }
aae9460e
PB
76}
77
7feb640c
AK
78void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
79{
80 sysbus_mmio_map_common(dev, n, addr, false, 0);
81}
82
83void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
84 unsigned priority)
85{
86 sysbus_mmio_map_common(dev, n, addr, true, priority);
87}
aae9460e
PB
88
89/* Request an IRQ source. The actual IRQ object may be populated later. */
90void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
91{
92 int n;
93
94 assert(dev->num_irq < QDEV_MAX_IRQ);
95 n = dev->num_irq++;
96 dev->irqp[n] = p;
97}
98
99/* Pass IRQs from a target device. */
100void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
101{
102 int i;
103 assert(dev->num_irq == 0);
104 dev->num_irq = target->num_irq;
105 for (i = 0; i < dev->num_irq; i++) {
106 dev->irqp[i] = target->irqp[i];
107 }
108}
109
750ecd44 110void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
ec3bb837
AK
111{
112 int n;
113
114 assert(dev->num_mmio < QDEV_MAX_MMIO);
115 n = dev->num_mmio++;
116 dev->mmio[n].addr = -1;
ec3bb837
AK
117 dev->mmio[n].memory = memory;
118}
119
46c305ef
PM
120MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
121{
122 return dev->mmio[n].memory;
123}
124
c646f74f
GN
125void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
126{
127 pio_addr_t i;
128
129 for (i = 0; i < size; i++) {
130 assert(dev->num_pio < QDEV_MAX_PIO);
131 dev->pio[dev->num_pio++] = ioport++;
132 }
133}
134
d307af79 135static int sysbus_device_init(DeviceState *dev)
aae9460e 136{
999e12bb
AL
137 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
138 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
aae9460e 139
999e12bb 140 return sbc->init(sd);
aae9460e
PB
141}
142
aae9460e 143DeviceState *sysbus_create_varargs(const char *name,
a8170e5e 144 hwaddr addr, ...)
aae9460e
PB
145{
146 DeviceState *dev;
147 SysBusDevice *s;
148 va_list va;
149 qemu_irq irq;
150 int n;
151
152 dev = qdev_create(NULL, name);
1356b98d 153 s = SYS_BUS_DEVICE(dev);
e23a1b33 154 qdev_init_nofail(dev);
a8170e5e 155 if (addr != (hwaddr)-1) {
aae9460e
PB
156 sysbus_mmio_map(s, 0, addr);
157 }
158 va_start(va, addr);
159 n = 0;
160 while (1) {
4912371f
BS
161 irq = va_arg(va, qemu_irq);
162 if (!irq) {
163 break;
164 }
165 sysbus_connect_irq(s, n, irq);
166 n++;
167 }
d0bc5bc3 168 va_end(va);
4912371f
BS
169 return dev;
170}
171
172DeviceState *sysbus_try_create_varargs(const char *name,
a8170e5e 173 hwaddr addr, ...)
4912371f
BS
174{
175 DeviceState *dev;
176 SysBusDevice *s;
177 va_list va;
178 qemu_irq irq;
179 int n;
180
181 dev = qdev_try_create(NULL, name);
182 if (!dev) {
183 return NULL;
184 }
1356b98d 185 s = SYS_BUS_DEVICE(dev);
4912371f 186 qdev_init_nofail(dev);
a8170e5e 187 if (addr != (hwaddr)-1) {
4912371f
BS
188 sysbus_mmio_map(s, 0, addr);
189 }
190 va_start(va, addr);
191 n = 0;
192 while (1) {
aae9460e
PB
193 irq = va_arg(va, qemu_irq);
194 if (!irq) {
195 break;
196 }
197 sysbus_connect_irq(s, n, irq);
198 n++;
199 }
d0bc5bc3 200 va_end(va);
aae9460e
PB
201 return dev;
202}
cae4956e 203
10c4c98a 204static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e 205{
1356b98d 206 SysBusDevice *s = SYS_BUS_DEVICE(dev);
a8170e5e 207 hwaddr size;
cae4956e
GH
208 int i;
209
0fba9fd6 210 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
cae4956e 211 for (i = 0; i < s->num_mmio; i++) {
e114fead 212 size = memory_region_size(s->mmio[i].memory);
cae4956e 213 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
3f7f1c80 214 indent, "", s->mmio[i].addr, size);
cae4956e
GH
215 }
216}
c646f74f
GN
217
218static char *sysbus_get_fw_dev_path(DeviceState *dev)
219{
1356b98d 220 SysBusDevice *s = SYS_BUS_DEVICE(dev);
c646f74f
GN
221 char path[40];
222 int off;
223
224 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
225
226 if (s->num_mmio) {
227 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
228 s->mmio[0].addr);
229 } else if (s->num_pio) {
230 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
231 }
232
a5cf8262 233 return g_strdup(path);
c646f74f 234}
2b985d9c 235
a8170e5e 236void sysbus_add_memory(SysBusDevice *dev, hwaddr addr,
2b985d9c
AK
237 MemoryRegion *mem)
238{
239 memory_region_add_subregion(get_system_memory(), addr, mem);
240}
241
a8170e5e 242void sysbus_add_memory_overlap(SysBusDevice *dev, hwaddr addr,
d40b2af8
AK
243 MemoryRegion *mem, unsigned priority)
244{
245 memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
246 priority);
247}
248
2b985d9c
AK
249void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
250{
251 memory_region_del_subregion(get_system_memory(), mem);
252}
253
a8170e5e 254void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
2b985d9c
AK
255 MemoryRegion *mem)
256{
257 memory_region_add_subregion(get_system_io(), addr, mem);
258}
259
260void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
261{
262 memory_region_del_subregion(get_system_io(), mem);
263}
62ec4832
AK
264
265MemoryRegion *sysbus_address_space(SysBusDevice *dev)
266{
267 return get_system_memory();
268}
999e12bb 269
39bffca2
AL
270static void sysbus_device_class_init(ObjectClass *klass, void *data)
271{
272 DeviceClass *k = DEVICE_CLASS(klass);
273 k->init = sysbus_device_init;
0d936928 274 k->bus_type = TYPE_SYSTEM_BUS;
39bffca2
AL
275}
276
8c43a6f0 277static const TypeInfo sysbus_device_type_info = {
999e12bb
AL
278 .name = TYPE_SYS_BUS_DEVICE,
279 .parent = TYPE_DEVICE,
280 .instance_size = sizeof(SysBusDevice),
281 .abstract = true,
282 .class_size = sizeof(SysBusDeviceClass),
39bffca2 283 .class_init = sysbus_device_class_init,
999e12bb
AL
284};
285
8185d216
PB
286/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
287static BusState *main_system_bus;
288
289static void main_system_bus_create(void)
290{
291 /* assign main_system_bus before qbus_create_inplace()
292 * in order to make "if (bus != sysbus_get_default())" work */
0d936928
AL
293 main_system_bus = g_malloc0(system_bus_info.instance_size);
294 qbus_create_inplace(main_system_bus, TYPE_SYSTEM_BUS, NULL,
8185d216 295 "main-system-bus");
64b625f4 296 OBJECT(main_system_bus)->free = g_free;
f968fc68
AL
297 object_property_add_child(container_get(qdev_get_machine(),
298 "/unattached"),
299 "sysbus", OBJECT(main_system_bus), NULL);
8185d216
PB
300}
301
302BusState *sysbus_get_default(void)
303{
304 if (!main_system_bus) {
305 main_system_bus_create();
306 }
307 return main_system_bus;
308}
309
83f7d43a 310static void sysbus_register_types(void)
999e12bb 311{
0d936928 312 type_register_static(&system_bus_info);
999e12bb
AL
313 type_register_static(&sysbus_device_type_info);
314}
315
83f7d43a 316type_init(sysbus_register_types)