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