]>
Commit | Line | Data |
---|---|---|
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" | |
21 | #include "sysemu.h" | |
cae4956e | 22 | #include "monitor.h" |
aae9460e | 23 | |
10c4c98a GH |
24 | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
25 | ||
26 | struct BusInfo system_bus_info = { | |
27 | .name = "System", | |
28 | .size = sizeof(BusState), | |
29 | .print_dev = sysbus_dev_print, | |
30 | }; | |
31 | ||
aae9460e PB |
32 | void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) |
33 | { | |
34 | assert(n >= 0 && n < dev->num_irq); | |
660f11be | 35 | dev->irqs[n] = NULL; |
aae9460e PB |
36 | if (dev->irqp[n]) { |
37 | *dev->irqp[n] = irq; | |
38 | } | |
39 | } | |
40 | ||
c227f099 | 41 | void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr) |
aae9460e PB |
42 | { |
43 | assert(n >= 0 && n < dev->num_mmio); | |
44 | ||
45 | if (dev->mmio[n].addr == addr) { | |
46 | /* ??? region already mapped here. */ | |
47 | return; | |
48 | } | |
c227f099 | 49 | if (dev->mmio[n].addr != (target_phys_addr_t)-1) { |
aae9460e PB |
50 | /* Unregister previous mapping. */ |
51 | cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size, | |
52 | IO_MEM_UNASSIGNED); | |
53 | } | |
54 | dev->mmio[n].addr = addr; | |
55 | if (dev->mmio[n].cb) { | |
56 | dev->mmio[n].cb(dev, addr); | |
57 | } else { | |
58 | cpu_register_physical_memory(addr, dev->mmio[n].size, | |
59 | dev->mmio[n].iofunc); | |
60 | } | |
61 | } | |
62 | ||
63 | ||
64 | /* Request an IRQ source. The actual IRQ object may be populated later. */ | |
65 | void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p) | |
66 | { | |
67 | int n; | |
68 | ||
69 | assert(dev->num_irq < QDEV_MAX_IRQ); | |
70 | n = dev->num_irq++; | |
71 | dev->irqp[n] = p; | |
72 | } | |
73 | ||
74 | /* Pass IRQs from a target device. */ | |
75 | void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target) | |
76 | { | |
77 | int i; | |
78 | assert(dev->num_irq == 0); | |
79 | dev->num_irq = target->num_irq; | |
80 | for (i = 0; i < dev->num_irq; i++) { | |
81 | dev->irqp[i] = target->irqp[i]; | |
82 | } | |
83 | } | |
84 | ||
c227f099 | 85 | void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, int iofunc) |
aae9460e PB |
86 | { |
87 | int n; | |
88 | ||
89 | assert(dev->num_mmio < QDEV_MAX_MMIO); | |
90 | n = dev->num_mmio++; | |
91 | dev->mmio[n].addr = -1; | |
92 | dev->mmio[n].size = size; | |
93 | dev->mmio[n].iofunc = iofunc; | |
94 | } | |
95 | ||
c227f099 | 96 | void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size, |
aae9460e PB |
97 | mmio_mapfunc cb) |
98 | { | |
99 | int n; | |
100 | ||
101 | assert(dev->num_mmio < QDEV_MAX_MMIO); | |
102 | n = dev->num_mmio++; | |
103 | dev->mmio[n].addr = -1; | |
104 | dev->mmio[n].size = size; | |
105 | dev->mmio[n].cb = cb; | |
106 | } | |
107 | ||
81a322d4 | 108 | static 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 | 115 | void 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 |
124 | void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init) |
125 | { | |
126 | SysBusDeviceInfo *info; | |
127 | ||
128 | info = qemu_mallocz(sizeof(*info)); | |
074f2fff GH |
129 | info->qdev.name = qemu_strdup(name); |
130 | info->qdev.size = size; | |
1431b6a1 | 131 | info->init = init; |
074f2fff | 132 | sysbus_register_withprop(info); |
1431b6a1 PB |
133 | } |
134 | ||
aae9460e | 135 | DeviceState *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) { | |
153 | irq = va_arg(va, qemu_irq); | |
154 | if (!irq) { | |
155 | break; | |
156 | } | |
157 | sysbus_connect_irq(s, n, irq); | |
158 | n++; | |
159 | } | |
160 | return dev; | |
161 | } | |
cae4956e | 162 | |
10c4c98a | 163 | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
cae4956e GH |
164 | { |
165 | SysBusDevice *s = sysbus_from_qdev(dev); | |
166 | int i; | |
167 | ||
168 | for (i = 0; i < s->num_mmio; i++) { | |
169 | monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n", | |
170 | indent, "", s->mmio[i].addr, s->mmio[i].size); | |
171 | } | |
172 | } |