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/>.
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 = {
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);
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. */
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,
55 dev->mmio[n].addr = addr;
56 if (dev->mmio[n].cb) {
57 dev->mmio[n].cb(dev, addr);
59 cpu_register_physical_memory(addr, dev->mmio[n].size,
65 /* Request an IRQ source. The actual IRQ object may be populated later. */
66 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
70 assert(dev->num_irq < QDEV_MAX_IRQ);
75 /* Pass IRQs from a target device. */
76 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
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,
91 assert(dev->num_mmio < QDEV_MAX_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,
103 assert(dev->num_mmio < QDEV_MAX_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)
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;
144 sysbus_register_withprop(info);
147 DeviceState *sysbus_create_varargs(const char *name,
148 target_phys_addr_t addr, ...)
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);
165 irq = va_arg(va, qemu_irq);
169 sysbus_connect_irq(s, n, irq);
175 DeviceState *sysbus_try_create_varargs(const char *name,
176 target_phys_addr_t addr, ...)
184 dev = qdev_try_create(NULL, name);
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);
196 irq = va_arg(va, qemu_irq);
200 sysbus_connect_irq(s, n, irq);
206 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
208 SysBusDevice *s = sysbus_from_qdev(dev);
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);
224 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
227 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
229 } else if (s->num_pio) {
230 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);