]> git.proxmox.com Git - qemu.git/blame - hw/sysbus.c
versatile_pci: Declare as little endian
[qemu.git] / hw / sysbus.c
CommitLineData
aae9460e
PB
1/*
2 * System (CPU) Bus device support code
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aae9460e
PB
18 */
19
20#include "sysbus.h"
21#include "sysemu.h"
cae4956e 22#include "monitor.h"
aae9460e 23
10c4c98a
GH
24static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25
26struct BusInfo system_bus_info = {
27 .name = "System",
28 .size = sizeof(BusState),
29 .print_dev = sysbus_dev_print,
30};
31
aae9460e
PB
32void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
33{
34 assert(n >= 0 && n < dev->num_irq);
660f11be 35 dev->irqs[n] = NULL;
aae9460e
PB
36 if (dev->irqp[n]) {
37 *dev->irqp[n] = irq;
38 }
39}
40
c227f099 41void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
aae9460e
PB
42{
43 assert(n >= 0 && n < dev->num_mmio);
44
45 if (dev->mmio[n].addr == addr) {
46 /* ??? region already mapped here. */
47 return;
48 }
c227f099 49 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
aae9460e
PB
50 /* Unregister previous mapping. */
51 cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
52 IO_MEM_UNASSIGNED);
53 }
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);
60 }
61}
62
63
64/* Request an IRQ source. The actual IRQ object may be populated later. */
65void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
66{
67 int n;
68
69 assert(dev->num_irq < QDEV_MAX_IRQ);
70 n = dev->num_irq++;
71 dev->irqp[n] = p;
72}
73
74/* Pass IRQs from a target device. */
75void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
76{
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];
82 }
83}
84
3f7132d1
BS
85void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
86 ram_addr_t iofunc)
aae9460e
PB
87{
88 int n;
89
90 assert(dev->num_mmio < QDEV_MAX_MMIO);
91 n = dev->num_mmio++;
92 dev->mmio[n].addr = -1;
93 dev->mmio[n].size = size;
94 dev->mmio[n].iofunc = iofunc;
95}
96
c227f099 97void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
aae9460e
PB
98 mmio_mapfunc cb)
99{
100 int n;
101
102 assert(dev->num_mmio < QDEV_MAX_MMIO);
103 n = dev->num_mmio++;
104 dev->mmio[n].addr = -1;
105 dev->mmio[n].size = size;
106 dev->mmio[n].cb = cb;
107}
108
81a322d4 109static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
aae9460e 110{
02e2da45 111 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
aae9460e 112
81a322d4 113 return info->init(sysbus_from_qdev(dev));
aae9460e
PB
114}
115
074f2fff 116void sysbus_register_withprop(SysBusDeviceInfo *info)
aae9460e 117{
02e2da45 118 info->qdev.init = sysbus_device_init;
10c4c98a 119 info->qdev.bus_info = &system_bus_info;
02e2da45 120
074f2fff
GH
121 assert(info->qdev.size >= sizeof(SysBusDevice));
122 qdev_register(&info->qdev);
aae9460e
PB
123}
124
1431b6a1
PB
125void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
126{
127 SysBusDeviceInfo *info;
128
129 info = qemu_mallocz(sizeof(*info));
074f2fff
GH
130 info->qdev.name = qemu_strdup(name);
131 info->qdev.size = size;
1431b6a1 132 info->init = init;
074f2fff 133 sysbus_register_withprop(info);
1431b6a1
PB
134}
135
aae9460e 136DeviceState *sysbus_create_varargs(const char *name,
c227f099 137 target_phys_addr_t addr, ...)
aae9460e
PB
138{
139 DeviceState *dev;
140 SysBusDevice *s;
141 va_list va;
142 qemu_irq irq;
143 int n;
144
145 dev = qdev_create(NULL, name);
146 s = sysbus_from_qdev(dev);
e23a1b33 147 qdev_init_nofail(dev);
c227f099 148 if (addr != (target_phys_addr_t)-1) {
aae9460e
PB
149 sysbus_mmio_map(s, 0, addr);
150 }
151 va_start(va, addr);
152 n = 0;
153 while (1) {
154 irq = va_arg(va, qemu_irq);
155 if (!irq) {
156 break;
157 }
158 sysbus_connect_irq(s, n, irq);
159 n++;
160 }
161 return dev;
162}
cae4956e 163
10c4c98a 164static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e
GH
165{
166 SysBusDevice *s = sysbus_from_qdev(dev);
167 int i;
168
169 for (i = 0; i < s->num_mmio; i++) {
170 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
171 indent, "", s->mmio[i].addr, s->mmio[i].size);
172 }
173}