]> git.proxmox.com Git - qemu.git/blob - hw/sysbus.c
usb: Pass the packet to the device's handle_control callback
[qemu.git] / hw / sysbus.c
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
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "sysbus.h"
21 #include "monitor.h"
22
23 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
24 static char *sysbus_get_fw_dev_path(DeviceState *dev);
25
26 struct BusInfo system_bus_info = {
27 .name = "System",
28 .size = sizeof(BusState),
29 .print_dev = sysbus_dev_print,
30 .get_fw_dev_path = sysbus_get_fw_dev_path,
31 };
32
33 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
34 {
35 assert(n >= 0 && n < dev->num_irq);
36 dev->irqs[n] = NULL;
37 if (dev->irqp[n]) {
38 *dev->irqp[n] = irq;
39 }
40 }
41
42 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
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 }
50 if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
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. */
66 void 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. */
76 void 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
86 void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
87 ram_addr_t iofunc)
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
98 void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
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
110 void 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
120 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
121 {
122 SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
123
124 return info->init(sysbus_from_qdev(dev));
125 }
126
127 void sysbus_register_withprop(SysBusDeviceInfo *info)
128 {
129 info->qdev.init = sysbus_device_init;
130 info->qdev.bus_info = &system_bus_info;
131
132 assert(info->qdev.size >= sizeof(SysBusDevice));
133 qdev_register(&info->qdev);
134 }
135
136 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
137 {
138 SysBusDeviceInfo *info;
139
140 info = qemu_mallocz(sizeof(*info));
141 info->qdev.name = qemu_strdup(name);
142 info->qdev.size = size;
143 info->init = init;
144 sysbus_register_withprop(info);
145 }
146
147 DeviceState *sysbus_create_varargs(const char *name,
148 target_phys_addr_t addr, ...)
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);
158 qdev_init_nofail(dev);
159 if (addr != (target_phys_addr_t)-1) {
160 sysbus_mmio_map(s, 0, addr);
161 }
162 va_start(va, addr);
163 n = 0;
164 while (1) {
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
175 DeviceState *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) {
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 }
205
206 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
207 {
208 SysBusDevice *s = sysbus_from_qdev(dev);
209 int i;
210
211 monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
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 }
217
218 static 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 }