]> git.proxmox.com Git - qemu.git/blob - hw/sysbus.c
Merge remote-tracking branch 'kwolf/for-anthony' into staging
[qemu.git] / hw / sysbus.c
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
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "sysbus.h"
21 #include "monitor.h"
22 #include "exec-memory.h"
23
24 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25 static char *sysbus_get_fw_dev_path(DeviceState *dev);
26
27 struct BusInfo system_bus_info = {
28 .name = "System",
29 .size = sizeof(BusState),
30 .print_dev = sysbus_dev_print,
31 .get_fw_dev_path = sysbus_get_fw_dev_path,
32 };
33
34 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
35 {
36 assert(n >= 0 && n < dev->num_irq);
37 dev->irqs[n] = NULL;
38 if (dev->irqp[n]) {
39 *dev->irqp[n] = irq;
40 }
41 }
42
43 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
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 }
51 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
52 /* Unregister previous mapping. */
53 if (dev->mmio[n].memory) {
54 memory_region_del_subregion(get_system_memory(),
55 dev->mmio[n].memory);
56 } else if (dev->mmio[n].unmap) {
57 dev->mmio[n].unmap(dev, dev->mmio[n].addr);
58 }
59 }
60 dev->mmio[n].addr = addr;
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) {
66 dev->mmio[n].cb(dev, addr);
67 }
68 }
69
70
71 /* Request an IRQ source. The actual IRQ object may be populated later. */
72 void 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. */
82 void 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
92 void 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;
100 dev->mmio[n].cb = cb;
101 dev->mmio[n].unmap = unmap;
102 }
103
104 void sysbus_init_mmio(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;
111 dev->mmio[n].memory = memory;
112 }
113
114 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
115 {
116 return dev->mmio[n].memory;
117 }
118
119 void 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
129 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
130 {
131 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
132
133 return info->init(sysbus_from_qdev(dev));
134 }
135
136 void sysbus_register_withprop(SysBusDeviceInfo *info)
137 {
138 info->qdev.init = sysbus_device_init;
139 info->qdev.bus_info = &system_bus_info;
140
141 assert(info->qdev.size >= sizeof(SysBusDevice));
142 qdev_register(&info->qdev);
143 }
144
145 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
146 {
147 SysBusDeviceInfo *info;
148
149 info = g_malloc0(sizeof(*info));
150 info->qdev.name = g_strdup(name);
151 info->qdev.size = size;
152 info->init = init;
153 sysbus_register_withprop(info);
154 }
155
156 DeviceState *sysbus_create_varargs(const char *name,
157 target_phys_addr_t addr, ...)
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);
167 qdev_init_nofail(dev);
168 if (addr != (target_phys_addr_t)-1) {
169 sysbus_mmio_map(s, 0, addr);
170 }
171 va_start(va, addr);
172 n = 0;
173 while (1) {
174 irq = va_arg(va, qemu_irq);
175 if (!irq) {
176 break;
177 }
178 sysbus_connect_irq(s, n, irq);
179 n++;
180 }
181 va_end(va);
182 return dev;
183 }
184
185 DeviceState *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) {
206 irq = va_arg(va, qemu_irq);
207 if (!irq) {
208 break;
209 }
210 sysbus_connect_irq(s, n, irq);
211 n++;
212 }
213 va_end(va);
214 return dev;
215 }
216
217 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
218 {
219 SysBusDevice *s = sysbus_from_qdev(dev);
220 target_phys_addr_t size;
221 int i;
222
223 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
224 for (i = 0; i < s->num_mmio; i++) {
225 size = 0;
226 if (s->mmio[i].memory) {
227 size = memory_region_size(s->mmio[i].memory);
228 }
229 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
230 indent, "", s->mmio[i].addr, size);
231 }
232 }
233
234 static 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 }
251
252 void 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
258 void 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
265 void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
266 {
267 memory_region_del_subregion(get_system_memory(), mem);
268 }
269
270 void 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
276 void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
277 {
278 memory_region_del_subregion(get_system_io(), mem);
279 }