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/>.
24 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25 static char *sysbus_get_fw_dev_path(DeviceState *dev);
27 struct BusInfo system_bus_info = {
29 .size = sizeof(BusState),
30 .print_dev = sysbus_dev_print,
31 .get_fw_dev_path = sysbus_get_fw_dev_path,
34 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
36 assert(n >= 0 && n < dev->num_irq);
43 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
45 assert(n >= 0 && n < dev->num_mmio);
47 if (dev->mmio[n].addr == addr) {
48 /* ??? region already mapped here. */
51 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
52 /* Unregister previous mapping. */
53 cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
56 dev->mmio[n].addr = addr;
57 if (dev->mmio[n].cb) {
58 dev->mmio[n].cb(dev, addr);
60 cpu_register_physical_memory(addr, dev->mmio[n].size,
66 /* Request an IRQ source. The actual IRQ object may be populated later. */
67 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
71 assert(dev->num_irq < QDEV_MAX_IRQ);
76 /* Pass IRQs from a target device. */
77 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
80 assert(dev->num_irq == 0);
81 dev->num_irq = target->num_irq;
82 for (i = 0; i < dev->num_irq; i++) {
83 dev->irqp[i] = target->irqp[i];
87 void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
92 assert(dev->num_mmio < QDEV_MAX_MMIO);
94 dev->mmio[n].addr = -1;
95 dev->mmio[n].size = size;
96 dev->mmio[n].iofunc = iofunc;
99 void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
104 assert(dev->num_mmio < QDEV_MAX_MMIO);
106 dev->mmio[n].addr = -1;
107 dev->mmio[n].size = size;
108 dev->mmio[n].cb = cb;
111 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
115 for (i = 0; i < size; i++) {
116 assert(dev->num_pio < QDEV_MAX_PIO);
117 dev->pio[dev->num_pio++] = ioport++;
121 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
123 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
125 return info->init(sysbus_from_qdev(dev));
128 void sysbus_register_withprop(SysBusDeviceInfo *info)
130 info->qdev.init = sysbus_device_init;
131 info->qdev.bus_info = &system_bus_info;
133 assert(info->qdev.size >= sizeof(SysBusDevice));
134 qdev_register(&info->qdev);
137 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
139 SysBusDeviceInfo *info;
141 info = qemu_mallocz(sizeof(*info));
142 info->qdev.name = qemu_strdup(name);
143 info->qdev.size = size;
145 sysbus_register_withprop(info);
148 DeviceState *sysbus_create_varargs(const char *name,
149 target_phys_addr_t addr, ...)
157 dev = qdev_create(NULL, name);
158 s = sysbus_from_qdev(dev);
159 qdev_init_nofail(dev);
160 if (addr != (target_phys_addr_t)-1) {
161 sysbus_mmio_map(s, 0, addr);
166 irq = va_arg(va, qemu_irq);
170 sysbus_connect_irq(s, n, irq);
176 DeviceState *sysbus_try_create_varargs(const char *name,
177 target_phys_addr_t addr, ...)
185 dev = qdev_try_create(NULL, name);
189 s = sysbus_from_qdev(dev);
190 qdev_init_nofail(dev);
191 if (addr != (target_phys_addr_t)-1) {
192 sysbus_mmio_map(s, 0, addr);
197 irq = va_arg(va, qemu_irq);
201 sysbus_connect_irq(s, n, irq);
207 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
209 SysBusDevice *s = sysbus_from_qdev(dev);
212 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
213 for (i = 0; i < s->num_mmio; i++) {
214 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
215 indent, "", s->mmio[i].addr, s->mmio[i].size);
219 static char *sysbus_get_fw_dev_path(DeviceState *dev)
221 SysBusDevice *s = sysbus_from_qdev(dev);
225 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
228 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
230 } else if (s->num_pio) {
231 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);