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