]> git.proxmox.com Git - qemu.git/blame - hw/sysbus.c
g_thread_init users: don't call it if glib >= 2.31
[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
20#include "sysbus.h"
cae4956e 21#include "monitor.h"
ec3bb837 22#include "exec-memory.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
GH
26
27struct BusInfo system_bus_info = {
28 .name = "System",
29 .size = sizeof(BusState),
30 .print_dev = sysbus_dev_print,
c646f74f 31 .get_fw_dev_path = sysbus_get_fw_dev_path,
10c4c98a
GH
32};
33
aae9460e
PB
34void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
35{
36 assert(n >= 0 && n < dev->num_irq);
660f11be 37 dev->irqs[n] = NULL;
aae9460e
PB
38 if (dev->irqp[n]) {
39 *dev->irqp[n] = irq;
40 }
41}
42
c227f099 43void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
aae9460e
PB
44{
45 assert(n >= 0 && n < dev->num_mmio);
46
47 if (dev->mmio[n].addr == addr) {
48 /* ??? region already mapped here. */
49 return;
50 }
c227f099 51 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
aae9460e 52 /* Unregister previous mapping. */
ec3bb837
AK
53 if (dev->mmio[n].memory) {
54 memory_region_del_subregion(get_system_memory(),
55 dev->mmio[n].memory);
ec3bb837 56 }
aae9460e
PB
57 }
58 dev->mmio[n].addr = addr;
ec3bb837
AK
59 if (dev->mmio[n].memory) {
60 memory_region_add_subregion(get_system_memory(),
61 addr,
62 dev->mmio[n].memory);
aae9460e
PB
63 }
64}
65
66
67/* Request an IRQ source. The actual IRQ object may be populated later. */
68void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
69{
70 int n;
71
72 assert(dev->num_irq < QDEV_MAX_IRQ);
73 n = dev->num_irq++;
74 dev->irqp[n] = p;
75}
76
77/* Pass IRQs from a target device. */
78void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
79{
80 int i;
81 assert(dev->num_irq == 0);
82 dev->num_irq = target->num_irq;
83 for (i = 0; i < dev->num_irq; i++) {
84 dev->irqp[i] = target->irqp[i];
85 }
86}
87
750ecd44 88void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
ec3bb837
AK
89{
90 int n;
91
92 assert(dev->num_mmio < QDEV_MAX_MMIO);
93 n = dev->num_mmio++;
94 dev->mmio[n].addr = -1;
ec3bb837
AK
95 dev->mmio[n].memory = memory;
96}
97
46c305ef
PM
98MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
99{
100 return dev->mmio[n].memory;
101}
102
c646f74f
GN
103void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
104{
105 pio_addr_t i;
106
107 for (i = 0; i < size; i++) {
108 assert(dev->num_pio < QDEV_MAX_PIO);
109 dev->pio[dev->num_pio++] = ioport++;
110 }
111}
112
81a322d4 113static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
aae9460e 114{
02e2da45 115 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
aae9460e 116
81a322d4 117 return info->init(sysbus_from_qdev(dev));
aae9460e
PB
118}
119
074f2fff 120void sysbus_register_withprop(SysBusDeviceInfo *info)
aae9460e 121{
02e2da45 122 info->qdev.init = sysbus_device_init;
10c4c98a 123 info->qdev.bus_info = &system_bus_info;
02e2da45 124
074f2fff
GH
125 assert(info->qdev.size >= sizeof(SysBusDevice));
126 qdev_register(&info->qdev);
aae9460e
PB
127}
128
1431b6a1
PB
129void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
130{
131 SysBusDeviceInfo *info;
132
7267c094
AL
133 info = g_malloc0(sizeof(*info));
134 info->qdev.name = g_strdup(name);
074f2fff 135 info->qdev.size = size;
1431b6a1 136 info->init = init;
074f2fff 137 sysbus_register_withprop(info);
1431b6a1
PB
138}
139
aae9460e 140DeviceState *sysbus_create_varargs(const char *name,
c227f099 141 target_phys_addr_t addr, ...)
aae9460e
PB
142{
143 DeviceState *dev;
144 SysBusDevice *s;
145 va_list va;
146 qemu_irq irq;
147 int n;
148
149 dev = qdev_create(NULL, name);
150 s = sysbus_from_qdev(dev);
e23a1b33 151 qdev_init_nofail(dev);
c227f099 152 if (addr != (target_phys_addr_t)-1) {
aae9460e
PB
153 sysbus_mmio_map(s, 0, addr);
154 }
155 va_start(va, addr);
156 n = 0;
157 while (1) {
4912371f
BS
158 irq = va_arg(va, qemu_irq);
159 if (!irq) {
160 break;
161 }
162 sysbus_connect_irq(s, n, irq);
163 n++;
164 }
d0bc5bc3 165 va_end(va);
4912371f
BS
166 return dev;
167}
168
169DeviceState *sysbus_try_create_varargs(const char *name,
170 target_phys_addr_t addr, ...)
171{
172 DeviceState *dev;
173 SysBusDevice *s;
174 va_list va;
175 qemu_irq irq;
176 int n;
177
178 dev = qdev_try_create(NULL, name);
179 if (!dev) {
180 return NULL;
181 }
182 s = sysbus_from_qdev(dev);
183 qdev_init_nofail(dev);
184 if (addr != (target_phys_addr_t)-1) {
185 sysbus_mmio_map(s, 0, addr);
186 }
187 va_start(va, addr);
188 n = 0;
189 while (1) {
aae9460e
PB
190 irq = va_arg(va, qemu_irq);
191 if (!irq) {
192 break;
193 }
194 sysbus_connect_irq(s, n, irq);
195 n++;
196 }
d0bc5bc3 197 va_end(va);
aae9460e
PB
198 return dev;
199}
cae4956e 200
10c4c98a 201static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e
GH
202{
203 SysBusDevice *s = sysbus_from_qdev(dev);
3f7f1c80 204 target_phys_addr_t size;
cae4956e
GH
205 int i;
206
0fba9fd6 207 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
cae4956e 208 for (i = 0; i < s->num_mmio; i++) {
3f7f1c80
AK
209 size = 0;
210 if (s->mmio[i].memory) {
211 size = memory_region_size(s->mmio[i].memory);
212 }
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{
220 SysBusDevice *s = sysbus_from_qdev(dev);
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
233 return strdup(path);
234}
2b985d9c
AK
235
236void sysbus_add_memory(SysBusDevice *dev, target_phys_addr_t addr,
237 MemoryRegion *mem)
238{
239 memory_region_add_subregion(get_system_memory(), addr, mem);
240}
241
d40b2af8
AK
242void sysbus_add_memory_overlap(SysBusDevice *dev, target_phys_addr_t addr,
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
254void sysbus_add_io(SysBusDevice *dev, target_phys_addr_t addr,
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}