Merge branch 'upstream-merge'
[qemu-kvm/markmc.git] / hw / sysbus.c
blob1f7f13841677c92d3c7409d6155d1e0292500a81
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 "sysemu.h"
22 #include "monitor.h"
24 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
26 struct BusInfo system_bus_info = {
27 .name = "System",
28 .size = sizeof(BusState),
29 .print_dev = sysbus_dev_print,
32 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
34 assert(n >= 0 && n < dev->num_irq);
35 dev->irqs[n] = NULL;
36 if (dev->irqp[n]) {
37 *dev->irqp[n] = irq;
41 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
43 assert(n >= 0 && n < dev->num_mmio);
45 if (dev->mmio[n].addr == addr) {
46 /* ??? region already mapped here. */
47 return;
49 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
50 /* Unregister previous mapping. */
51 cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
52 IO_MEM_UNASSIGNED);
54 dev->mmio[n].addr = addr;
55 if (dev->mmio[n].cb) {
56 dev->mmio[n].cb(dev, addr);
57 } else {
58 cpu_register_physical_memory(addr, dev->mmio[n].size,
59 dev->mmio[n].iofunc);
64 /* Request an IRQ source. The actual IRQ object may be populated later. */
65 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
67 int n;
69 assert(dev->num_irq < QDEV_MAX_IRQ);
70 n = dev->num_irq++;
71 dev->irqp[n] = p;
74 /* Pass IRQs from a target device. */
75 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
77 int i;
78 assert(dev->num_irq == 0);
79 dev->num_irq = target->num_irq;
80 for (i = 0; i < dev->num_irq; i++) {
81 dev->irqp[i] = target->irqp[i];
85 void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, int iofunc)
87 int n;
89 assert(dev->num_mmio < QDEV_MAX_MMIO);
90 n = dev->num_mmio++;
91 dev->mmio[n].addr = -1;
92 dev->mmio[n].size = size;
93 dev->mmio[n].iofunc = iofunc;
96 void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
97 mmio_mapfunc cb)
99 int n;
101 assert(dev->num_mmio < QDEV_MAX_MMIO);
102 n = dev->num_mmio++;
103 dev->mmio[n].addr = -1;
104 dev->mmio[n].size = size;
105 dev->mmio[n].cb = cb;
108 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
110 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
112 return info->init(sysbus_from_qdev(dev));
115 void sysbus_register_withprop(SysBusDeviceInfo *info)
117 info->qdev.init = sysbus_device_init;
118 info->qdev.bus_info = &system_bus_info;
120 assert(info->qdev.size >= sizeof(SysBusDevice));
121 qdev_register(&info->qdev);
124 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
126 SysBusDeviceInfo *info;
128 info = qemu_mallocz(sizeof(*info));
129 info->qdev.name = qemu_strdup(name);
130 info->qdev.size = size;
131 info->init = init;
132 sysbus_register_withprop(info);
135 DeviceState *sysbus_create_varargs(const char *name,
136 target_phys_addr_t addr, ...)
138 DeviceState *dev;
139 SysBusDevice *s;
140 va_list va;
141 qemu_irq irq;
142 int n;
144 dev = qdev_create(NULL, name);
145 s = sysbus_from_qdev(dev);
146 qdev_init_nofail(dev);
147 if (addr != (target_phys_addr_t)-1) {
148 sysbus_mmio_map(s, 0, addr);
150 va_start(va, addr);
151 n = 0;
152 while (1) {
153 irq = va_arg(va, qemu_irq);
154 if (!irq) {
155 break;
157 sysbus_connect_irq(s, n, irq);
158 n++;
160 return dev;
163 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
165 SysBusDevice *s = sysbus_from_qdev(dev);
166 int i;
168 for (i = 0; i < s->num_mmio; i++) {
169 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
170 indent, "", s->mmio[i].addr, s->mmio[i].size);