]> git.proxmox.com Git - qemu.git/blame - hw/sysbus.c
Merge branch 'tci' of git://qemu.weilnetz.de/qemu
[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);
d7612013
AK
56 } else if (dev->mmio[n].unmap) {
57 dev->mmio[n].unmap(dev, dev->mmio[n].addr);
ec3bb837
AK
58 } else {
59 cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
60 IO_MEM_UNASSIGNED);
61 }
aae9460e
PB
62 }
63 dev->mmio[n].addr = addr;
ec3bb837
AK
64 if (dev->mmio[n].memory) {
65 memory_region_add_subregion(get_system_memory(),
66 addr,
67 dev->mmio[n].memory);
68 } else if (dev->mmio[n].cb) {
aae9460e
PB
69 dev->mmio[n].cb(dev, addr);
70 } else {
71 cpu_register_physical_memory(addr, dev->mmio[n].size,
72 dev->mmio[n].iofunc);
73 }
74}
75
76
77/* Request an IRQ source. The actual IRQ object may be populated later. */
78void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
79{
80 int n;
81
82 assert(dev->num_irq < QDEV_MAX_IRQ);
83 n = dev->num_irq++;
84 dev->irqp[n] = p;
85}
86
87/* Pass IRQs from a target device. */
88void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
89{
90 int i;
91 assert(dev->num_irq == 0);
92 dev->num_irq = target->num_irq;
93 for (i = 0; i < dev->num_irq; i++) {
94 dev->irqp[i] = target->irqp[i];
95 }
96}
97
3f7132d1
BS
98void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
99 ram_addr_t iofunc)
aae9460e
PB
100{
101 int n;
102
103 assert(dev->num_mmio < QDEV_MAX_MMIO);
104 n = dev->num_mmio++;
105 dev->mmio[n].addr = -1;
106 dev->mmio[n].size = size;
107 dev->mmio[n].iofunc = iofunc;
108}
109
d7612013
AK
110void sysbus_init_mmio_cb2(SysBusDevice *dev,
111 mmio_mapfunc cb, mmio_mapfunc unmap)
112{
113 int n;
114
115 assert(dev->num_mmio < QDEV_MAX_MMIO);
116 n = dev->num_mmio++;
117 dev->mmio[n].addr = -1;
118 dev->mmio[n].size = 0;
119 dev->mmio[n].cb = cb;
120 dev->mmio[n].unmap = unmap;
121}
122
ec3bb837
AK
123void sysbus_init_mmio_region(SysBusDevice *dev, MemoryRegion *memory)
124{
125 int n;
126
127 assert(dev->num_mmio < QDEV_MAX_MMIO);
128 n = dev->num_mmio++;
129 dev->mmio[n].addr = -1;
130 dev->mmio[n].size = memory_region_size(memory);
131 dev->mmio[n].memory = memory;
132}
133
46c305ef
PM
134MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
135{
136 return dev->mmio[n].memory;
137}
138
c646f74f
GN
139void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
140{
141 pio_addr_t i;
142
143 for (i = 0; i < size; i++) {
144 assert(dev->num_pio < QDEV_MAX_PIO);
145 dev->pio[dev->num_pio++] = ioport++;
146 }
147}
148
81a322d4 149static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
aae9460e 150{
02e2da45 151 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
aae9460e 152
81a322d4 153 return info->init(sysbus_from_qdev(dev));
aae9460e
PB
154}
155
074f2fff 156void sysbus_register_withprop(SysBusDeviceInfo *info)
aae9460e 157{
02e2da45 158 info->qdev.init = sysbus_device_init;
10c4c98a 159 info->qdev.bus_info = &system_bus_info;
02e2da45 160
074f2fff
GH
161 assert(info->qdev.size >= sizeof(SysBusDevice));
162 qdev_register(&info->qdev);
aae9460e
PB
163}
164
1431b6a1
PB
165void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
166{
167 SysBusDeviceInfo *info;
168
7267c094
AL
169 info = g_malloc0(sizeof(*info));
170 info->qdev.name = g_strdup(name);
074f2fff 171 info->qdev.size = size;
1431b6a1 172 info->init = init;
074f2fff 173 sysbus_register_withprop(info);
1431b6a1
PB
174}
175
aae9460e 176DeviceState *sysbus_create_varargs(const char *name,
c227f099 177 target_phys_addr_t addr, ...)
aae9460e
PB
178{
179 DeviceState *dev;
180 SysBusDevice *s;
181 va_list va;
182 qemu_irq irq;
183 int n;
184
185 dev = qdev_create(NULL, name);
186 s = sysbus_from_qdev(dev);
e23a1b33 187 qdev_init_nofail(dev);
c227f099 188 if (addr != (target_phys_addr_t)-1) {
aae9460e
PB
189 sysbus_mmio_map(s, 0, addr);
190 }
191 va_start(va, addr);
192 n = 0;
193 while (1) {
4912371f
BS
194 irq = va_arg(va, qemu_irq);
195 if (!irq) {
196 break;
197 }
198 sysbus_connect_irq(s, n, irq);
199 n++;
200 }
d0bc5bc3 201 va_end(va);
4912371f
BS
202 return dev;
203}
204
205DeviceState *sysbus_try_create_varargs(const char *name,
206 target_phys_addr_t addr, ...)
207{
208 DeviceState *dev;
209 SysBusDevice *s;
210 va_list va;
211 qemu_irq irq;
212 int n;
213
214 dev = qdev_try_create(NULL, name);
215 if (!dev) {
216 return NULL;
217 }
218 s = sysbus_from_qdev(dev);
219 qdev_init_nofail(dev);
220 if (addr != (target_phys_addr_t)-1) {
221 sysbus_mmio_map(s, 0, addr);
222 }
223 va_start(va, addr);
224 n = 0;
225 while (1) {
aae9460e
PB
226 irq = va_arg(va, qemu_irq);
227 if (!irq) {
228 break;
229 }
230 sysbus_connect_irq(s, n, irq);
231 n++;
232 }
d0bc5bc3 233 va_end(va);
aae9460e
PB
234 return dev;
235}
cae4956e 236
10c4c98a 237static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e
GH
238{
239 SysBusDevice *s = sysbus_from_qdev(dev);
240 int i;
241
0fba9fd6 242 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
cae4956e
GH
243 for (i = 0; i < s->num_mmio; i++) {
244 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
245 indent, "", s->mmio[i].addr, s->mmio[i].size);
246 }
247}
c646f74f
GN
248
249static char *sysbus_get_fw_dev_path(DeviceState *dev)
250{
251 SysBusDevice *s = sysbus_from_qdev(dev);
252 char path[40];
253 int off;
254
255 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
256
257 if (s->num_mmio) {
258 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
259 s->mmio[0].addr);
260 } else if (s->num_pio) {
261 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
262 }
263
264 return strdup(path);
265}
2b985d9c
AK
266
267void sysbus_add_memory(SysBusDevice *dev, target_phys_addr_t addr,
268 MemoryRegion *mem)
269{
270 memory_region_add_subregion(get_system_memory(), addr, mem);
271}
272
d40b2af8
AK
273void sysbus_add_memory_overlap(SysBusDevice *dev, target_phys_addr_t addr,
274 MemoryRegion *mem, unsigned priority)
275{
276 memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
277 priority);
278}
279
2b985d9c
AK
280void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
281{
282 memory_region_del_subregion(get_system_memory(), mem);
283}
284
285void sysbus_add_io(SysBusDevice *dev, target_phys_addr_t addr,
286 MemoryRegion *mem)
287{
288 memory_region_add_subregion(get_system_io(), addr, mem);
289}
290
291void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
292{
293 memory_region_del_subregion(get_system_io(), mem);
294}