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/>.
22 #include "exec-memory.h"
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 if (dev->mmio[n].memory) {
54 memory_region_del_subregion(get_system_memory(),
58 dev->mmio[n].addr = addr;
59 if (dev->mmio[n].memory) {
60 memory_region_add_subregion(get_system_memory(),
67 /* Request an IRQ source. The actual IRQ object may be populated later. */
68 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
72 assert(dev->num_irq < QDEV_MAX_IRQ);
77 /* Pass IRQs from a target device. */
78 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
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];
88 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
92 assert(dev->num_mmio < QDEV_MAX_MMIO);
94 dev->mmio[n].addr = -1;
95 dev->mmio[n].memory = memory;
98 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
100 return dev->mmio[n].memory;
103 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
107 for (i = 0; i < size; i++) {
108 assert(dev->num_pio < QDEV_MAX_PIO);
109 dev->pio[dev->num_pio++] = ioport++;
113 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
115 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
117 return info->init(sysbus_from_qdev(dev));
120 void sysbus_register_withprop(SysBusDeviceInfo *info)
122 info->qdev.init = sysbus_device_init;
123 info->qdev.bus_info = &system_bus_info;
125 assert(info->qdev.size >= sizeof(SysBusDevice));
126 qdev_register(&info->qdev);
129 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
131 SysBusDeviceInfo *info;
133 info = g_malloc0(sizeof(*info));
134 info->qdev.name = g_strdup(name);
135 info->qdev.size = size;
137 sysbus_register_withprop(info);
140 DeviceState *sysbus_create_varargs(const char *name,
141 target_phys_addr_t addr, ...)
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);
158 irq = va_arg(va, qemu_irq);
162 sysbus_connect_irq(s, n, irq);
169 DeviceState *sysbus_try_create_varargs(const char *name,
170 target_phys_addr_t addr, ...)
178 dev = qdev_try_create(NULL, name);
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);
190 irq = va_arg(va, qemu_irq);
194 sysbus_connect_irq(s, n, irq);
201 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
203 SysBusDevice *s = sysbus_from_qdev(dev);
204 target_phys_addr_t size;
207 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
208 for (i = 0; i < s->num_mmio; i++) {
210 if (s->mmio[i].memory) {
211 size = memory_region_size(s->mmio[i].memory);
213 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
214 indent, "", s->mmio[i].addr, 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]);
236 void sysbus_add_memory(SysBusDevice *dev, target_phys_addr_t addr,
239 memory_region_add_subregion(get_system_memory(), addr, mem);
242 void sysbus_add_memory_overlap(SysBusDevice *dev, target_phys_addr_t addr,
243 MemoryRegion *mem, unsigned priority)
245 memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
249 void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
251 memory_region_del_subregion(get_system_memory(), mem);
254 void sysbus_add_io(SysBusDevice *dev, target_phys_addr_t addr,
257 memory_region_add_subregion(get_system_io(), addr, mem);
260 void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
262 memory_region_del_subregion(get_system_io(), mem);