root / hw / sysbus.c @ 0fba9fd6
History | View | Annotate | Download (5.4 kB)
1 | aae9460e | Paul Brook | /*
|
---|---|---|---|
2 | aae9460e | Paul Brook | * System (CPU) Bus device support code
|
3 | aae9460e | Paul Brook | *
|
4 | aae9460e | Paul Brook | * Copyright (c) 2009 CodeSourcery
|
5 | aae9460e | Paul Brook | *
|
6 | aae9460e | Paul Brook | * This library is free software; you can redistribute it and/or
|
7 | aae9460e | Paul Brook | * modify it under the terms of the GNU Lesser General Public
|
8 | aae9460e | Paul Brook | * License as published by the Free Software Foundation; either
|
9 | aae9460e | Paul Brook | * version 2 of the License, or (at your option) any later version.
|
10 | aae9460e | Paul Brook | *
|
11 | aae9460e | Paul Brook | * This library is distributed in the hope that it will be useful,
|
12 | aae9460e | Paul Brook | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | aae9460e | Paul Brook | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | aae9460e | Paul Brook | * Lesser General Public License for more details.
|
15 | aae9460e | Paul Brook | *
|
16 | aae9460e | Paul Brook | * You should have received a copy of the GNU Lesser General Public
|
17 | 8167ee88 | Blue Swirl | * License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
18 | aae9460e | Paul Brook | */
|
19 | aae9460e | Paul Brook | |
20 | aae9460e | Paul Brook | #include "sysbus.h" |
21 | aae9460e | Paul Brook | #include "sysemu.h" |
22 | cae4956e | Gerd Hoffmann | #include "monitor.h" |
23 | aae9460e | Paul Brook | |
24 | 10c4c98a | Gerd Hoffmann | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
25 | c646f74f | Gleb Natapov | static char *sysbus_get_fw_dev_path(DeviceState *dev); |
26 | 10c4c98a | Gerd Hoffmann | |
27 | 10c4c98a | Gerd Hoffmann | struct BusInfo system_bus_info = {
|
28 | 10c4c98a | Gerd Hoffmann | .name = "System",
|
29 | 10c4c98a | Gerd Hoffmann | .size = sizeof(BusState),
|
30 | 10c4c98a | Gerd Hoffmann | .print_dev = sysbus_dev_print, |
31 | c646f74f | Gleb Natapov | .get_fw_dev_path = sysbus_get_fw_dev_path, |
32 | 10c4c98a | Gerd Hoffmann | }; |
33 | 10c4c98a | Gerd Hoffmann | |
34 | aae9460e | Paul Brook | void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) |
35 | aae9460e | Paul Brook | { |
36 | aae9460e | Paul Brook | assert(n >= 0 && n < dev->num_irq);
|
37 | 660f11be | Blue Swirl | dev->irqs[n] = NULL;
|
38 | aae9460e | Paul Brook | if (dev->irqp[n]) {
|
39 | aae9460e | Paul Brook | *dev->irqp[n] = irq; |
40 | aae9460e | Paul Brook | } |
41 | aae9460e | Paul Brook | } |
42 | aae9460e | Paul Brook | |
43 | c227f099 | Anthony Liguori | void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr) |
44 | aae9460e | Paul Brook | { |
45 | aae9460e | Paul Brook | assert(n >= 0 && n < dev->num_mmio);
|
46 | aae9460e | Paul Brook | |
47 | aae9460e | Paul Brook | if (dev->mmio[n].addr == addr) {
|
48 | aae9460e | Paul Brook | /* ??? region already mapped here. */
|
49 | aae9460e | Paul Brook | return;
|
50 | aae9460e | Paul Brook | } |
51 | c227f099 | Anthony Liguori | if (dev->mmio[n].addr != (target_phys_addr_t)-1) { |
52 | aae9460e | Paul Brook | /* Unregister previous mapping. */
|
53 | aae9460e | Paul Brook | cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size, |
54 | aae9460e | Paul Brook | IO_MEM_UNASSIGNED); |
55 | aae9460e | Paul Brook | } |
56 | aae9460e | Paul Brook | dev->mmio[n].addr = addr; |
57 | aae9460e | Paul Brook | if (dev->mmio[n].cb) {
|
58 | aae9460e | Paul Brook | dev->mmio[n].cb(dev, addr); |
59 | aae9460e | Paul Brook | } else {
|
60 | aae9460e | Paul Brook | cpu_register_physical_memory(addr, dev->mmio[n].size, |
61 | aae9460e | Paul Brook | dev->mmio[n].iofunc); |
62 | aae9460e | Paul Brook | } |
63 | aae9460e | Paul Brook | } |
64 | aae9460e | Paul Brook | |
65 | aae9460e | Paul Brook | |
66 | aae9460e | Paul Brook | /* Request an IRQ source. The actual IRQ object may be populated later. */
|
67 | aae9460e | Paul Brook | void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
|
68 | aae9460e | Paul Brook | { |
69 | aae9460e | Paul Brook | int n;
|
70 | aae9460e | Paul Brook | |
71 | aae9460e | Paul Brook | assert(dev->num_irq < QDEV_MAX_IRQ); |
72 | aae9460e | Paul Brook | n = dev->num_irq++; |
73 | aae9460e | Paul Brook | dev->irqp[n] = p; |
74 | aae9460e | Paul Brook | } |
75 | aae9460e | Paul Brook | |
76 | aae9460e | Paul Brook | /* Pass IRQs from a target device. */
|
77 | aae9460e | Paul Brook | void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
|
78 | aae9460e | Paul Brook | { |
79 | aae9460e | Paul Brook | int i;
|
80 | aae9460e | Paul Brook | assert(dev->num_irq == 0);
|
81 | aae9460e | Paul Brook | dev->num_irq = target->num_irq; |
82 | aae9460e | Paul Brook | for (i = 0; i < dev->num_irq; i++) { |
83 | aae9460e | Paul Brook | dev->irqp[i] = target->irqp[i]; |
84 | aae9460e | Paul Brook | } |
85 | aae9460e | Paul Brook | } |
86 | aae9460e | Paul Brook | |
87 | 3f7132d1 | Blue Swirl | void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
|
88 | 3f7132d1 | Blue Swirl | ram_addr_t iofunc) |
89 | aae9460e | Paul Brook | { |
90 | aae9460e | Paul Brook | int n;
|
91 | aae9460e | Paul Brook | |
92 | aae9460e | Paul Brook | assert(dev->num_mmio < QDEV_MAX_MMIO); |
93 | aae9460e | Paul Brook | n = dev->num_mmio++; |
94 | aae9460e | Paul Brook | dev->mmio[n].addr = -1;
|
95 | aae9460e | Paul Brook | dev->mmio[n].size = size; |
96 | aae9460e | Paul Brook | dev->mmio[n].iofunc = iofunc; |
97 | aae9460e | Paul Brook | } |
98 | aae9460e | Paul Brook | |
99 | c227f099 | Anthony Liguori | void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
|
100 | aae9460e | Paul Brook | mmio_mapfunc cb) |
101 | aae9460e | Paul Brook | { |
102 | aae9460e | Paul Brook | int n;
|
103 | aae9460e | Paul Brook | |
104 | aae9460e | Paul Brook | assert(dev->num_mmio < QDEV_MAX_MMIO); |
105 | aae9460e | Paul Brook | n = dev->num_mmio++; |
106 | aae9460e | Paul Brook | dev->mmio[n].addr = -1;
|
107 | aae9460e | Paul Brook | dev->mmio[n].size = size; |
108 | aae9460e | Paul Brook | dev->mmio[n].cb = cb; |
109 | aae9460e | Paul Brook | } |
110 | aae9460e | Paul Brook | |
111 | c646f74f | Gleb Natapov | void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
|
112 | c646f74f | Gleb Natapov | { |
113 | c646f74f | Gleb Natapov | pio_addr_t i; |
114 | c646f74f | Gleb Natapov | |
115 | c646f74f | Gleb Natapov | for (i = 0; i < size; i++) { |
116 | c646f74f | Gleb Natapov | assert(dev->num_pio < QDEV_MAX_PIO); |
117 | c646f74f | Gleb Natapov | dev->pio[dev->num_pio++] = ioport++; |
118 | c646f74f | Gleb Natapov | } |
119 | c646f74f | Gleb Natapov | } |
120 | c646f74f | Gleb Natapov | |
121 | 81a322d4 | Gerd Hoffmann | static int sysbus_device_init(DeviceState *dev, DeviceInfo *base) |
122 | aae9460e | Paul Brook | { |
123 | 02e2da45 | Paul Brook | SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev); |
124 | aae9460e | Paul Brook | |
125 | 81a322d4 | Gerd Hoffmann | return info->init(sysbus_from_qdev(dev));
|
126 | aae9460e | Paul Brook | } |
127 | aae9460e | Paul Brook | |
128 | 074f2fff | Gerd Hoffmann | void sysbus_register_withprop(SysBusDeviceInfo *info)
|
129 | aae9460e | Paul Brook | { |
130 | 02e2da45 | Paul Brook | info->qdev.init = sysbus_device_init; |
131 | 10c4c98a | Gerd Hoffmann | info->qdev.bus_info = &system_bus_info; |
132 | 02e2da45 | Paul Brook | |
133 | 074f2fff | Gerd Hoffmann | assert(info->qdev.size >= sizeof(SysBusDevice));
|
134 | 074f2fff | Gerd Hoffmann | qdev_register(&info->qdev); |
135 | aae9460e | Paul Brook | } |
136 | aae9460e | Paul Brook | |
137 | 1431b6a1 | Paul Brook | void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init) |
138 | 1431b6a1 | Paul Brook | { |
139 | 1431b6a1 | Paul Brook | SysBusDeviceInfo *info; |
140 | 1431b6a1 | Paul Brook | |
141 | 1431b6a1 | Paul Brook | info = qemu_mallocz(sizeof(*info));
|
142 | 074f2fff | Gerd Hoffmann | info->qdev.name = qemu_strdup(name); |
143 | 074f2fff | Gerd Hoffmann | info->qdev.size = size; |
144 | 1431b6a1 | Paul Brook | info->init = init; |
145 | 074f2fff | Gerd Hoffmann | sysbus_register_withprop(info); |
146 | 1431b6a1 | Paul Brook | } |
147 | 1431b6a1 | Paul Brook | |
148 | aae9460e | Paul Brook | DeviceState *sysbus_create_varargs(const char *name, |
149 | c227f099 | Anthony Liguori | target_phys_addr_t addr, ...) |
150 | aae9460e | Paul Brook | { |
151 | aae9460e | Paul Brook | DeviceState *dev; |
152 | aae9460e | Paul Brook | SysBusDevice *s; |
153 | aae9460e | Paul Brook | va_list va; |
154 | aae9460e | Paul Brook | qemu_irq irq; |
155 | aae9460e | Paul Brook | int n;
|
156 | aae9460e | Paul Brook | |
157 | aae9460e | Paul Brook | dev = qdev_create(NULL, name);
|
158 | aae9460e | Paul Brook | s = sysbus_from_qdev(dev); |
159 | e23a1b33 | Markus Armbruster | qdev_init_nofail(dev); |
160 | c227f099 | Anthony Liguori | if (addr != (target_phys_addr_t)-1) { |
161 | aae9460e | Paul Brook | sysbus_mmio_map(s, 0, addr);
|
162 | aae9460e | Paul Brook | } |
163 | aae9460e | Paul Brook | va_start(va, addr); |
164 | aae9460e | Paul Brook | n = 0;
|
165 | aae9460e | Paul Brook | while (1) { |
166 | aae9460e | Paul Brook | irq = va_arg(va, qemu_irq); |
167 | aae9460e | Paul Brook | if (!irq) {
|
168 | aae9460e | Paul Brook | break;
|
169 | aae9460e | Paul Brook | } |
170 | aae9460e | Paul Brook | sysbus_connect_irq(s, n, irq); |
171 | aae9460e | Paul Brook | n++; |
172 | aae9460e | Paul Brook | } |
173 | aae9460e | Paul Brook | return dev;
|
174 | aae9460e | Paul Brook | } |
175 | cae4956e | Gerd Hoffmann | |
176 | 10c4c98a | Gerd Hoffmann | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
177 | cae4956e | Gerd Hoffmann | { |
178 | cae4956e | Gerd Hoffmann | SysBusDevice *s = sysbus_from_qdev(dev); |
179 | cae4956e | Gerd Hoffmann | int i;
|
180 | cae4956e | Gerd Hoffmann | |
181 | 0fba9fd6 | Dmitry Eremin-Solenikov | monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq); |
182 | cae4956e | Gerd Hoffmann | for (i = 0; i < s->num_mmio; i++) { |
183 | cae4956e | Gerd Hoffmann | monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n", |
184 | cae4956e | Gerd Hoffmann | indent, "", s->mmio[i].addr, s->mmio[i].size);
|
185 | cae4956e | Gerd Hoffmann | } |
186 | cae4956e | Gerd Hoffmann | } |
187 | c646f74f | Gleb Natapov | |
188 | c646f74f | Gleb Natapov | static char *sysbus_get_fw_dev_path(DeviceState *dev) |
189 | c646f74f | Gleb Natapov | { |
190 | c646f74f | Gleb Natapov | SysBusDevice *s = sysbus_from_qdev(dev); |
191 | c646f74f | Gleb Natapov | char path[40]; |
192 | c646f74f | Gleb Natapov | int off;
|
193 | c646f74f | Gleb Natapov | |
194 | c646f74f | Gleb Natapov | off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); |
195 | c646f74f | Gleb Natapov | |
196 | c646f74f | Gleb Natapov | if (s->num_mmio) {
|
197 | c646f74f | Gleb Natapov | snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx, |
198 | c646f74f | Gleb Natapov | s->mmio[0].addr);
|
199 | c646f74f | Gleb Natapov | } else if (s->num_pio) { |
200 | c646f74f | Gleb Natapov | snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]); |
201 | c646f74f | Gleb Natapov | } |
202 | c646f74f | Gleb Natapov | |
203 | c646f74f | Gleb Natapov | return strdup(path);
|
204 | c646f74f | Gleb Natapov | } |