Merge remote-tracking branch 'spice/spice.v38' into staging
[qemu/aliguori.git] / hw / sysbus.c
blob2e22be7b25ba227f7c2bce122f950360c8179f2c
1 /*
2 * System (CPU) Bus device support code
4 * Copyright (c) 2009 CodeSourcery
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.
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.
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/>.
20 #include "sysbus.h"
21 #include "monitor.h"
23 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
24 static char *sysbus_get_fw_dev_path(DeviceState *dev);
26 struct BusInfo system_bus_info = {
27 .name = "System",
28 .size = sizeof(BusState),
29 .print_dev = sysbus_dev_print,
30 .get_fw_dev_path = sysbus_get_fw_dev_path,
33 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
35 assert(n >= 0 && n < dev->num_irq);
36 dev->irqs[n] = NULL;
37 if (dev->irqp[n]) {
38 *dev->irqp[n] = irq;
42 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
44 assert(n >= 0 && n < dev->num_mmio);
46 if (dev->mmio[n].addr == addr) {
47 /* ??? region already mapped here. */
48 return;
50 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
51 /* Unregister previous mapping. */
52 cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
53 IO_MEM_UNASSIGNED);
55 dev->mmio[n].addr = addr;
56 if (dev->mmio[n].cb) {
57 dev->mmio[n].cb(dev, addr);
58 } else {
59 cpu_register_physical_memory(addr, dev->mmio[n].size,
60 dev->mmio[n].iofunc);
65 /* Request an IRQ source. The actual IRQ object may be populated later. */
66 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
68 int n;
70 assert(dev->num_irq < QDEV_MAX_IRQ);
71 n = dev->num_irq++;
72 dev->irqp[n] = p;
75 /* Pass IRQs from a target device. */
76 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
78 int i;
79 assert(dev->num_irq == 0);
80 dev->num_irq = target->num_irq;
81 for (i = 0; i < dev->num_irq; i++) {
82 dev->irqp[i] = target->irqp[i];
86 void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
87 ram_addr_t iofunc)
89 int n;
91 assert(dev->num_mmio < QDEV_MAX_MMIO);
92 n = dev->num_mmio++;
93 dev->mmio[n].addr = -1;
94 dev->mmio[n].size = size;
95 dev->mmio[n].iofunc = iofunc;
98 void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
99 mmio_mapfunc cb)
101 int n;
103 assert(dev->num_mmio < QDEV_MAX_MMIO);
104 n = dev->num_mmio++;
105 dev->mmio[n].addr = -1;
106 dev->mmio[n].size = size;
107 dev->mmio[n].cb = cb;
110 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
112 pio_addr_t i;
114 for (i = 0; i < size; i++) {
115 assert(dev->num_pio < QDEV_MAX_PIO);
116 dev->pio[dev->num_pio++] = ioport++;
120 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
122 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
124 return info->init(sysbus_from_qdev(dev));
127 void sysbus_register_withprop(SysBusDeviceInfo *info)
129 info->qdev.init = sysbus_device_init;
130 info->qdev.bus_info = &system_bus_info;
132 assert(info->qdev.size >= sizeof(SysBusDevice));
133 qdev_register(&info->qdev);
136 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
138 SysBusDeviceInfo *info;
140 info = qemu_mallocz(sizeof(*info));
141 info->qdev.name = qemu_strdup(name);
142 info->qdev.size = size;
143 info->init = init;
144 sysbus_register_withprop(info);
147 DeviceState *sysbus_create_varargs(const char *name,
148 target_phys_addr_t addr, ...)
150 DeviceState *dev;
151 SysBusDevice *s;
152 va_list va;
153 qemu_irq irq;
154 int n;
156 dev = qdev_create(NULL, name);
157 s = sysbus_from_qdev(dev);
158 qdev_init_nofail(dev);
159 if (addr != (target_phys_addr_t)-1) {
160 sysbus_mmio_map(s, 0, addr);
162 va_start(va, addr);
163 n = 0;
164 while (1) {
165 irq = va_arg(va, qemu_irq);
166 if (!irq) {
167 break;
169 sysbus_connect_irq(s, n, irq);
170 n++;
172 return dev;
175 DeviceState *sysbus_try_create_varargs(const char *name,
176 target_phys_addr_t addr, ...)
178 DeviceState *dev;
179 SysBusDevice *s;
180 va_list va;
181 qemu_irq irq;
182 int n;
184 dev = qdev_try_create(NULL, name);
185 if (!dev) {
186 return NULL;
188 s = sysbus_from_qdev(dev);
189 qdev_init_nofail(dev);
190 if (addr != (target_phys_addr_t)-1) {
191 sysbus_mmio_map(s, 0, addr);
193 va_start(va, addr);
194 n = 0;
195 while (1) {
196 irq = va_arg(va, qemu_irq);
197 if (!irq) {
198 break;
200 sysbus_connect_irq(s, n, irq);
201 n++;
203 return dev;
206 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
208 SysBusDevice *s = sysbus_from_qdev(dev);
209 int i;
211 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
212 for (i = 0; i < s->num_mmio; i++) {
213 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
214 indent, "", s->mmio[i].addr, s->mmio[i].size);
218 static char *sysbus_get_fw_dev_path(DeviceState *dev)
220 SysBusDevice *s = sysbus_from_qdev(dev);
221 char path[40];
222 int off;
224 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
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]);
233 return strdup(path);