root / hw / sysbus.c @ 3f7132d1
History | View | Annotate | Download (4.5 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 | 10c4c98a | Gerd Hoffmann | |
26 | 10c4c98a | Gerd Hoffmann | struct BusInfo system_bus_info = {
|
27 | 10c4c98a | Gerd Hoffmann | .name = "System",
|
28 | 10c4c98a | Gerd Hoffmann | .size = sizeof(BusState),
|
29 | 10c4c98a | Gerd Hoffmann | .print_dev = sysbus_dev_print, |
30 | 10c4c98a | Gerd Hoffmann | }; |
31 | 10c4c98a | Gerd Hoffmann | |
32 | aae9460e | Paul Brook | void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) |
33 | aae9460e | Paul Brook | { |
34 | aae9460e | Paul Brook | assert(n >= 0 && n < dev->num_irq);
|
35 | 660f11be | Blue Swirl | dev->irqs[n] = NULL;
|
36 | aae9460e | Paul Brook | if (dev->irqp[n]) {
|
37 | aae9460e | Paul Brook | *dev->irqp[n] = irq; |
38 | aae9460e | Paul Brook | } |
39 | aae9460e | Paul Brook | } |
40 | aae9460e | Paul Brook | |
41 | c227f099 | Anthony Liguori | void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr) |
42 | aae9460e | Paul Brook | { |
43 | aae9460e | Paul Brook | assert(n >= 0 && n < dev->num_mmio);
|
44 | aae9460e | Paul Brook | |
45 | aae9460e | Paul Brook | if (dev->mmio[n].addr == addr) {
|
46 | aae9460e | Paul Brook | /* ??? region already mapped here. */
|
47 | aae9460e | Paul Brook | return;
|
48 | aae9460e | Paul Brook | } |
49 | c227f099 | Anthony Liguori | if (dev->mmio[n].addr != (target_phys_addr_t)-1) { |
50 | aae9460e | Paul Brook | /* Unregister previous mapping. */
|
51 | aae9460e | Paul Brook | cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size, |
52 | aae9460e | Paul Brook | IO_MEM_UNASSIGNED); |
53 | aae9460e | Paul Brook | } |
54 | aae9460e | Paul Brook | dev->mmio[n].addr = addr; |
55 | aae9460e | Paul Brook | if (dev->mmio[n].cb) {
|
56 | aae9460e | Paul Brook | dev->mmio[n].cb(dev, addr); |
57 | aae9460e | Paul Brook | } else {
|
58 | aae9460e | Paul Brook | cpu_register_physical_memory(addr, dev->mmio[n].size, |
59 | aae9460e | Paul Brook | dev->mmio[n].iofunc); |
60 | aae9460e | Paul Brook | } |
61 | aae9460e | Paul Brook | } |
62 | aae9460e | Paul Brook | |
63 | aae9460e | Paul Brook | |
64 | aae9460e | Paul Brook | /* Request an IRQ source. The actual IRQ object may be populated later. */
|
65 | aae9460e | Paul Brook | void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
|
66 | aae9460e | Paul Brook | { |
67 | aae9460e | Paul Brook | int n;
|
68 | aae9460e | Paul Brook | |
69 | aae9460e | Paul Brook | assert(dev->num_irq < QDEV_MAX_IRQ); |
70 | aae9460e | Paul Brook | n = dev->num_irq++; |
71 | aae9460e | Paul Brook | dev->irqp[n] = p; |
72 | aae9460e | Paul Brook | } |
73 | aae9460e | Paul Brook | |
74 | aae9460e | Paul Brook | /* Pass IRQs from a target device. */
|
75 | aae9460e | Paul Brook | void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
|
76 | aae9460e | Paul Brook | { |
77 | aae9460e | Paul Brook | int i;
|
78 | aae9460e | Paul Brook | assert(dev->num_irq == 0);
|
79 | aae9460e | Paul Brook | dev->num_irq = target->num_irq; |
80 | aae9460e | Paul Brook | for (i = 0; i < dev->num_irq; i++) { |
81 | aae9460e | Paul Brook | dev->irqp[i] = target->irqp[i]; |
82 | aae9460e | Paul Brook | } |
83 | aae9460e | Paul Brook | } |
84 | aae9460e | Paul Brook | |
85 | 3f7132d1 | Blue Swirl | void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size,
|
86 | 3f7132d1 | Blue Swirl | ram_addr_t iofunc) |
87 | aae9460e | Paul Brook | { |
88 | aae9460e | Paul Brook | int n;
|
89 | aae9460e | Paul Brook | |
90 | aae9460e | Paul Brook | assert(dev->num_mmio < QDEV_MAX_MMIO); |
91 | aae9460e | Paul Brook | n = dev->num_mmio++; |
92 | aae9460e | Paul Brook | dev->mmio[n].addr = -1;
|
93 | aae9460e | Paul Brook | dev->mmio[n].size = size; |
94 | aae9460e | Paul Brook | dev->mmio[n].iofunc = iofunc; |
95 | aae9460e | Paul Brook | } |
96 | aae9460e | Paul Brook | |
97 | c227f099 | Anthony Liguori | void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
|
98 | aae9460e | Paul Brook | mmio_mapfunc cb) |
99 | aae9460e | Paul Brook | { |
100 | aae9460e | Paul Brook | int n;
|
101 | aae9460e | Paul Brook | |
102 | aae9460e | Paul Brook | assert(dev->num_mmio < QDEV_MAX_MMIO); |
103 | aae9460e | Paul Brook | n = dev->num_mmio++; |
104 | aae9460e | Paul Brook | dev->mmio[n].addr = -1;
|
105 | aae9460e | Paul Brook | dev->mmio[n].size = size; |
106 | aae9460e | Paul Brook | dev->mmio[n].cb = cb; |
107 | aae9460e | Paul Brook | } |
108 | aae9460e | Paul Brook | |
109 | 81a322d4 | Gerd Hoffmann | static int sysbus_device_init(DeviceState *dev, DeviceInfo *base) |
110 | aae9460e | Paul Brook | { |
111 | 02e2da45 | Paul Brook | SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev); |
112 | aae9460e | Paul Brook | |
113 | 81a322d4 | Gerd Hoffmann | return info->init(sysbus_from_qdev(dev));
|
114 | aae9460e | Paul Brook | } |
115 | aae9460e | Paul Brook | |
116 | 074f2fff | Gerd Hoffmann | void sysbus_register_withprop(SysBusDeviceInfo *info)
|
117 | aae9460e | Paul Brook | { |
118 | 02e2da45 | Paul Brook | info->qdev.init = sysbus_device_init; |
119 | 10c4c98a | Gerd Hoffmann | info->qdev.bus_info = &system_bus_info; |
120 | 02e2da45 | Paul Brook | |
121 | 074f2fff | Gerd Hoffmann | assert(info->qdev.size >= sizeof(SysBusDevice));
|
122 | 074f2fff | Gerd Hoffmann | qdev_register(&info->qdev); |
123 | aae9460e | Paul Brook | } |
124 | aae9460e | Paul Brook | |
125 | 1431b6a1 | Paul Brook | void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init) |
126 | 1431b6a1 | Paul Brook | { |
127 | 1431b6a1 | Paul Brook | SysBusDeviceInfo *info; |
128 | 1431b6a1 | Paul Brook | |
129 | 1431b6a1 | Paul Brook | info = qemu_mallocz(sizeof(*info));
|
130 | 074f2fff | Gerd Hoffmann | info->qdev.name = qemu_strdup(name); |
131 | 074f2fff | Gerd Hoffmann | info->qdev.size = size; |
132 | 1431b6a1 | Paul Brook | info->init = init; |
133 | 074f2fff | Gerd Hoffmann | sysbus_register_withprop(info); |
134 | 1431b6a1 | Paul Brook | } |
135 | 1431b6a1 | Paul Brook | |
136 | aae9460e | Paul Brook | DeviceState *sysbus_create_varargs(const char *name, |
137 | c227f099 | Anthony Liguori | target_phys_addr_t addr, ...) |
138 | aae9460e | Paul Brook | { |
139 | aae9460e | Paul Brook | DeviceState *dev; |
140 | aae9460e | Paul Brook | SysBusDevice *s; |
141 | aae9460e | Paul Brook | va_list va; |
142 | aae9460e | Paul Brook | qemu_irq irq; |
143 | aae9460e | Paul Brook | int n;
|
144 | aae9460e | Paul Brook | |
145 | aae9460e | Paul Brook | dev = qdev_create(NULL, name);
|
146 | aae9460e | Paul Brook | s = sysbus_from_qdev(dev); |
147 | e23a1b33 | Markus Armbruster | qdev_init_nofail(dev); |
148 | c227f099 | Anthony Liguori | if (addr != (target_phys_addr_t)-1) { |
149 | aae9460e | Paul Brook | sysbus_mmio_map(s, 0, addr);
|
150 | aae9460e | Paul Brook | } |
151 | aae9460e | Paul Brook | va_start(va, addr); |
152 | aae9460e | Paul Brook | n = 0;
|
153 | aae9460e | Paul Brook | while (1) { |
154 | aae9460e | Paul Brook | irq = va_arg(va, qemu_irq); |
155 | aae9460e | Paul Brook | if (!irq) {
|
156 | aae9460e | Paul Brook | break;
|
157 | aae9460e | Paul Brook | } |
158 | aae9460e | Paul Brook | sysbus_connect_irq(s, n, irq); |
159 | aae9460e | Paul Brook | n++; |
160 | aae9460e | Paul Brook | } |
161 | aae9460e | Paul Brook | return dev;
|
162 | aae9460e | Paul Brook | } |
163 | cae4956e | Gerd Hoffmann | |
164 | 10c4c98a | Gerd Hoffmann | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
165 | cae4956e | Gerd Hoffmann | { |
166 | cae4956e | Gerd Hoffmann | SysBusDevice *s = sysbus_from_qdev(dev); |
167 | cae4956e | Gerd Hoffmann | int i;
|
168 | cae4956e | Gerd Hoffmann | |
169 | cae4956e | Gerd Hoffmann | for (i = 0; i < s->num_mmio; i++) { |
170 | cae4956e | Gerd Hoffmann | monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n", |
171 | cae4956e | Gerd Hoffmann | indent, "", s->mmio[i].addr, s->mmio[i].size);
|
172 | cae4956e | Gerd Hoffmann | } |
173 | cae4956e | Gerd Hoffmann | } |