Record device property types
[qemu/kevin.git] / hw / sysbus.c
bloba1843088e3bec92298e96cde2b364d4d26075167
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
21 #include "sysbus.h"
22 #include "sysemu.h"
24 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
26 assert(n >= 0 && n < dev->num_irq);
27 dev->irqs[n] = 0;
28 if (dev->irqp[n]) {
29 *dev->irqp[n] = irq;
33 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
35 assert(n >= 0 && n < dev->num_mmio);
37 if (dev->mmio[n].addr == addr) {
38 /* ??? region already mapped here. */
39 return;
41 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
42 /* Unregister previous mapping. */
43 cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
44 IO_MEM_UNASSIGNED);
46 dev->mmio[n].addr = addr;
47 if (dev->mmio[n].cb) {
48 dev->mmio[n].cb(dev, addr);
49 } else {
50 cpu_register_physical_memory(addr, dev->mmio[n].size,
51 dev->mmio[n].iofunc);
56 /* Request an IRQ source. The actual IRQ object may be populated later. */
57 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
59 int n;
61 assert(dev->num_irq < QDEV_MAX_IRQ);
62 n = dev->num_irq++;
63 dev->irqp[n] = p;
66 /* Pass IRQs from a target device. */
67 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
69 int i;
70 assert(dev->num_irq == 0);
71 dev->num_irq = target->num_irq;
72 for (i = 0; i < dev->num_irq; i++) {
73 dev->irqp[i] = target->irqp[i];
77 void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, int iofunc)
79 int n;
81 assert(dev->num_mmio < QDEV_MAX_MMIO);
82 n = dev->num_mmio++;
83 dev->mmio[n].addr = -1;
84 dev->mmio[n].size = size;
85 dev->mmio[n].iofunc = iofunc;
88 void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
89 mmio_mapfunc cb)
91 int n;
93 assert(dev->num_mmio < QDEV_MAX_MMIO);
94 n = dev->num_mmio++;
95 dev->mmio[n].addr = -1;
96 dev->mmio[n].size = size;
97 dev->mmio[n].cb = cb;
100 static void sysbus_device_init(DeviceState *dev, DeviceInfo *base)
102 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
104 info->init(sysbus_from_qdev(dev));
107 void sysbus_register_withprop(const char *name, size_t size,
108 SysBusDeviceInfo *info)
110 info->qdev.init = sysbus_device_init;
111 info->qdev.bus_type = BUS_TYPE_SYSTEM;
113 assert(size >= sizeof(SysBusDevice));
114 qdev_register(name, size, &info->qdev);
117 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
119 SysBusDeviceInfo *info;
121 info = qemu_mallocz(sizeof(*info));
122 info->init = init;
123 sysbus_register_withprop(name, size, info);
126 DeviceState *sysbus_create_varargs(const char *name,
127 target_phys_addr_t addr, ...)
129 DeviceState *dev;
130 SysBusDevice *s;
131 va_list va;
132 qemu_irq irq;
133 int n;
135 dev = qdev_create(NULL, name);
136 s = sysbus_from_qdev(dev);
137 qdev_init(dev);
138 if (addr != (target_phys_addr_t)-1) {
139 sysbus_mmio_map(s, 0, addr);
141 va_start(va, addr);
142 n = 0;
143 while (1) {
144 irq = va_arg(va, qemu_irq);
145 if (!irq) {
146 break;
148 sysbus_connect_irq(s, n, irq);
149 n++;
151 return dev;