linux-user: stack_base is now mandatory on all targets
[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 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
54 }
55 dev->mmio[n].addr = addr;
56 memory_region_add_subregion(get_system_memory(),
57 addr,
58 dev->mmio[n].memory);
59 }
60
61
62 /* Request an IRQ source. The actual IRQ object may be populated later. */
63 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
64 {
65 int n;
66
67 assert(dev->num_irq < QDEV_MAX_IRQ);
68 n = dev->num_irq++;
69 dev->irqp[n] = p;
70 }
71
72 /* Pass IRQs from a target device. */
73 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
74 {
75 int i;
76 assert(dev->num_irq == 0);
77 dev->num_irq = target->num_irq;
78 for (i = 0; i < dev->num_irq; i++) {
79 dev->irqp[i] = target->irqp[i];
80 }
81 }
82
83 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
84 {
85 int n;
86
87 assert(dev->num_mmio < QDEV_MAX_MMIO);
88 n = dev->num_mmio++;
89 dev->mmio[n].addr = -1;
90 dev->mmio[n].memory = memory;
91 }
92
93 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
94 {
95 return dev->mmio[n].memory;
96 }
97
98 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
99 {
100 pio_addr_t i;
101
102 for (i = 0; i < size; i++) {
103 assert(dev->num_pio < QDEV_MAX_PIO);
104 dev->pio[dev->num_pio++] = ioport++;
105 }
106 }
107
108 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
109 {
110 SysBusDevice *sd = SYS_BUS_DEVICE(dev);
111 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
112
113 return sbc->init(sd);
114 }
115
116 void sysbus_qdev_register_subclass(DeviceInfo *info, const char *parent)
117 {
118 info->init = sysbus_device_init;
119 info->bus_info = &system_bus_info;
120
121 assert(info->size >= sizeof(SysBusDevice));
122 qdev_register_subclass(info, parent);
123 }
124
125 void sysbus_qdev_register(DeviceInfo *info)
126 {
127 sysbus_qdev_register_subclass(info, TYPE_SYS_BUS_DEVICE);
128 }
129
130 DeviceState *sysbus_create_varargs(const char *name,
131 target_phys_addr_t addr, ...)
132 {
133 DeviceState *dev;
134 SysBusDevice *s;
135 va_list va;
136 qemu_irq irq;
137 int n;
138
139 dev = qdev_create(NULL, name);
140 s = sysbus_from_qdev(dev);
141 qdev_init_nofail(dev);
142 if (addr != (target_phys_addr_t)-1) {
143 sysbus_mmio_map(s, 0, addr);
144 }
145 va_start(va, addr);
146 n = 0;
147 while (1) {
148 irq = va_arg(va, qemu_irq);
149 if (!irq) {
150 break;
151 }
152 sysbus_connect_irq(s, n, irq);
153 n++;
154 }
155 va_end(va);
156 return dev;
157 }
158
159 DeviceState *sysbus_try_create_varargs(const char *name,
160 target_phys_addr_t addr, ...)
161 {
162 DeviceState *dev;
163 SysBusDevice *s;
164 va_list va;
165 qemu_irq irq;
166 int n;
167
168 dev = qdev_try_create(NULL, name);
169 if (!dev) {
170 return NULL;
171 }
172 s = sysbus_from_qdev(dev);
173 qdev_init_nofail(dev);
174 if (addr != (target_phys_addr_t)-1) {
175 sysbus_mmio_map(s, 0, addr);
176 }
177 va_start(va, addr);
178 n = 0;
179 while (1) {
180 irq = va_arg(va, qemu_irq);
181 if (!irq) {
182 break;
183 }
184 sysbus_connect_irq(s, n, irq);
185 n++;
186 }
187 va_end(va);
188 return dev;
189 }
190
191 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
192 {
193 SysBusDevice *s = sysbus_from_qdev(dev);
194 target_phys_addr_t size;
195 int i;
196
197 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
198 for (i = 0; i < s->num_mmio; i++) {
199 size = memory_region_size(s->mmio[i].memory);
200 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
201 indent, "", s->mmio[i].addr, size);
202 }
203 }
204
205 static char *sysbus_get_fw_dev_path(DeviceState *dev)
206 {
207 SysBusDevice *s = sysbus_from_qdev(dev);
208 char path[40];
209 int off;
210
211 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
212
213 if (s->num_mmio) {
214 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
215 s->mmio[0].addr);
216 } else if (s->num_pio) {
217 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
218 }
219
220 return strdup(path);
221 }
222
223 void sysbus_add_memory(SysBusDevice *dev, target_phys_addr_t addr,
224 MemoryRegion *mem)
225 {
226 memory_region_add_subregion(get_system_memory(), addr, mem);
227 }
228
229 void sysbus_add_memory_overlap(SysBusDevice *dev, target_phys_addr_t addr,
230 MemoryRegion *mem, unsigned priority)
231 {
232 memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
233 priority);
234 }
235
236 void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
237 {
238 memory_region_del_subregion(get_system_memory(), mem);
239 }
240
241 void sysbus_add_io(SysBusDevice *dev, target_phys_addr_t addr,
242 MemoryRegion *mem)
243 {
244 memory_region_add_subregion(get_system_io(), addr, mem);
245 }
246
247 void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
248 {
249 memory_region_del_subregion(get_system_io(), mem);
250 }
251
252 MemoryRegion *sysbus_address_space(SysBusDevice *dev)
253 {
254 return get_system_memory();
255 }
256
257 static TypeInfo sysbus_device_type_info = {
258 .name = TYPE_SYS_BUS_DEVICE,
259 .parent = TYPE_DEVICE,
260 .instance_size = sizeof(SysBusDevice),
261 .abstract = true,
262 .class_size = sizeof(SysBusDeviceClass),
263 };
264
265 static void sysbus_register(void)
266 {
267 type_register_static(&sysbus_device_type_info);
268 }
269
270 device_init(sysbus_register);