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