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