]> git.proxmox.com Git - qemu.git/blame - hw/sysbus.c
Merge remote-tracking branch 'qemu-kvm/memory/page_desc' into staging
[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"
cae4956e 21#include "monitor.h"
ec3bb837 22#include "exec-memory.h"
aae9460e 23
10c4c98a 24static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
c646f74f 25static char *sysbus_get_fw_dev_path(DeviceState *dev);
10c4c98a
GH
26
27struct BusInfo system_bus_info = {
28 .name = "System",
29 .size = sizeof(BusState),
30 .print_dev = sysbus_dev_print,
c646f74f 31 .get_fw_dev_path = sysbus_get_fw_dev_path,
10c4c98a
GH
32};
33
aae9460e
PB
34void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
35{
36 assert(n >= 0 && n < dev->num_irq);
660f11be 37 dev->irqs[n] = NULL;
aae9460e
PB
38 if (dev->irqp[n]) {
39 *dev->irqp[n] = irq;
40 }
41}
42
c227f099 43void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
aae9460e
PB
44{
45 assert(n >= 0 && n < dev->num_mmio);
46
47 if (dev->mmio[n].addr == addr) {
48 /* ??? region already mapped here. */
49 return;
50 }
c227f099 51 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
aae9460e 52 /* Unregister previous mapping. */
e114fead 53 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
aae9460e
PB
54 }
55 dev->mmio[n].addr = addr;
e114fead
PM
56 memory_region_add_subregion(get_system_memory(),
57 addr,
58 dev->mmio[n].memory);
aae9460e
PB
59}
60
61
62/* Request an IRQ source. The actual IRQ object may be populated later. */
63void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
64{
65 int n;
66
67 assert(dev->num_irq < QDEV_MAX_IRQ);
68 n = dev->num_irq++;
69 dev->irqp[n] = p;
70}
71
72/* Pass IRQs from a target device. */
73void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
74{
75 int i;
76 assert(dev->num_irq == 0);
77 dev->num_irq = target->num_irq;
78 for (i = 0; i < dev->num_irq; i++) {
79 dev->irqp[i] = target->irqp[i];
80 }
81}
82
750ecd44 83void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
ec3bb837
AK
84{
85 int n;
86
87 assert(dev->num_mmio < QDEV_MAX_MMIO);
88 n = dev->num_mmio++;
89 dev->mmio[n].addr = -1;
ec3bb837
AK
90 dev->mmio[n].memory = memory;
91}
92
46c305ef
PM
93MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
94{
95 return dev->mmio[n].memory;
96}
97
c646f74f
GN
98void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
99{
100 pio_addr_t i;
101
102 for (i = 0; i < size; i++) {
103 assert(dev->num_pio < QDEV_MAX_PIO);
104 dev->pio[dev->num_pio++] = ioport++;
105 }
106}
107
81a322d4 108static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
aae9460e 109{
02e2da45 110 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
aae9460e 111
81a322d4 112 return info->init(sysbus_from_qdev(dev));
aae9460e
PB
113}
114
074f2fff 115void sysbus_register_withprop(SysBusDeviceInfo *info)
aae9460e 116{
02e2da45 117 info->qdev.init = sysbus_device_init;
10c4c98a 118 info->qdev.bus_info = &system_bus_info;
02e2da45 119
074f2fff
GH
120 assert(info->qdev.size >= sizeof(SysBusDevice));
121 qdev_register(&info->qdev);
aae9460e
PB
122}
123
1431b6a1
PB
124void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
125{
126 SysBusDeviceInfo *info;
127
7267c094
AL
128 info = g_malloc0(sizeof(*info));
129 info->qdev.name = g_strdup(name);
074f2fff 130 info->qdev.size = size;
1431b6a1 131 info->init = init;
074f2fff 132 sysbus_register_withprop(info);
1431b6a1
PB
133}
134
aae9460e 135DeviceState *sysbus_create_varargs(const char *name,
c227f099 136 target_phys_addr_t addr, ...)
aae9460e
PB
137{
138 DeviceState *dev;
139 SysBusDevice *s;
140 va_list va;
141 qemu_irq irq;
142 int n;
143
144 dev = qdev_create(NULL, name);
145 s = sysbus_from_qdev(dev);
e23a1b33 146 qdev_init_nofail(dev);
c227f099 147 if (addr != (target_phys_addr_t)-1) {
aae9460e
PB
148 sysbus_mmio_map(s, 0, addr);
149 }
150 va_start(va, addr);
151 n = 0;
152 while (1) {
4912371f
BS
153 irq = va_arg(va, qemu_irq);
154 if (!irq) {
155 break;
156 }
157 sysbus_connect_irq(s, n, irq);
158 n++;
159 }
d0bc5bc3 160 va_end(va);
4912371f
BS
161 return dev;
162}
163
164DeviceState *sysbus_try_create_varargs(const char *name,
165 target_phys_addr_t addr, ...)
166{
167 DeviceState *dev;
168 SysBusDevice *s;
169 va_list va;
170 qemu_irq irq;
171 int n;
172
173 dev = qdev_try_create(NULL, name);
174 if (!dev) {
175 return NULL;
176 }
177 s = sysbus_from_qdev(dev);
178 qdev_init_nofail(dev);
179 if (addr != (target_phys_addr_t)-1) {
180 sysbus_mmio_map(s, 0, addr);
181 }
182 va_start(va, addr);
183 n = 0;
184 while (1) {
aae9460e
PB
185 irq = va_arg(va, qemu_irq);
186 if (!irq) {
187 break;
188 }
189 sysbus_connect_irq(s, n, irq);
190 n++;
191 }
d0bc5bc3 192 va_end(va);
aae9460e
PB
193 return dev;
194}
cae4956e 195
10c4c98a 196static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
cae4956e
GH
197{
198 SysBusDevice *s = sysbus_from_qdev(dev);
3f7f1c80 199 target_phys_addr_t size;
cae4956e
GH
200 int i;
201
0fba9fd6 202 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
cae4956e 203 for (i = 0; i < s->num_mmio; i++) {
e114fead 204 size = memory_region_size(s->mmio[i].memory);
cae4956e 205 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
3f7f1c80 206 indent, "", s->mmio[i].addr, size);
cae4956e
GH
207 }
208}
c646f74f
GN
209
210static char *sysbus_get_fw_dev_path(DeviceState *dev)
211{
212 SysBusDevice *s = sysbus_from_qdev(dev);
213 char path[40];
214 int off;
215
216 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
217
218 if (s->num_mmio) {
219 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
220 s->mmio[0].addr);
221 } else if (s->num_pio) {
222 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
223 }
224
225 return strdup(path);
226}
2b985d9c
AK
227
228void sysbus_add_memory(SysBusDevice *dev, target_phys_addr_t addr,
229 MemoryRegion *mem)
230{
231 memory_region_add_subregion(get_system_memory(), addr, mem);
232}
233
d40b2af8
AK
234void sysbus_add_memory_overlap(SysBusDevice *dev, target_phys_addr_t addr,
235 MemoryRegion *mem, unsigned priority)
236{
237 memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
238 priority);
239}
240
2b985d9c
AK
241void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
242{
243 memory_region_del_subregion(get_system_memory(), mem);
244}
245
246void sysbus_add_io(SysBusDevice *dev, target_phys_addr_t addr,
247 MemoryRegion *mem)
248{
249 memory_region_add_subregion(get_system_io(), addr, mem);
250}
251
252void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
253{
254 memory_region_del_subregion(get_system_io(), mem);
255}
62ec4832
AK
256
257MemoryRegion *sysbus_address_space(SysBusDevice *dev)
258{
259 return get_system_memory();
260}