master
c 371 lines 11.1 KB
Raw
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 /*
25 * split out ioport related stuffs from vl.c.
26 */
27
28 #include "qemu/osdep.h"
29 #include "system/ioport.h"
30 #include "system/memory.h"
31 #include "system/address-spaces.h"
32 #include "hw/core/qdev.h"
33 #include "trace.h"
34
35 struct MemoryRegionPortioList {
36 Object obj;
37
38 MemoryRegion mr;
39 void *portio_opaque;
40 MemoryRegionPortio *ports;
41 };
42
43 #define TYPE_MEMORY_REGION_PORTIO_LIST "memory-region-portio-list"
44 OBJECT_DECLARE_SIMPLE_TYPE(MemoryRegionPortioList, MEMORY_REGION_PORTIO_LIST)
45
46 static uint64_t unassigned_io_read(void *opaque, hwaddr addr, unsigned size)
47 {
48 return -1ULL;
49 }
50
51 static void unassigned_io_write(void *opaque, hwaddr addr, uint64_t val,
52 unsigned size)
53 {
54 }
55
56 const MemoryRegionOps unassigned_io_ops = {
57 .read = unassigned_io_read,
58 .write = unassigned_io_write,
59 .endianness = DEVICE_LITTLE_ENDIAN,
60 };
61
62 void cpu_outb(uint32_t addr, uint8_t val)
63 {
64 trace_cpu_out(addr, 'b', val);
65 address_space_stb(&address_space_io, addr, val,
66 MEMTXATTRS_UNSPECIFIED, NULL);
67 }
68
69 void cpu_outw(uint32_t addr, uint16_t val)
70 {
71 trace_cpu_out(addr, 'w', val);
72 address_space_stw_le(&address_space_io, addr, val,
73 MEMTXATTRS_UNSPECIFIED, NULL);
74 }
75
76 void cpu_outl(uint32_t addr, uint32_t val)
77 {
78 trace_cpu_out(addr, 'l', val);
79 address_space_stl_le(&address_space_io, addr, val,
80 MEMTXATTRS_UNSPECIFIED, NULL);
81 }
82
83 uint8_t cpu_inb(uint32_t addr)
84 {
85 uint8_t val;
86
87 val = address_space_ldub(&address_space_io, addr,
88 MEMTXATTRS_UNSPECIFIED, NULL);
89 trace_cpu_in(addr, 'b', val);
90 return val;
91 }
92
93 uint16_t cpu_inw(uint32_t addr)
94 {
95 uint16_t val;
96
97 val = address_space_lduw_le(&address_space_io, addr,
98 MEMTXATTRS_UNSPECIFIED, NULL);
99 trace_cpu_in(addr, 'w', val);
100 return val;
101 }
102
103 uint32_t cpu_inl(uint32_t addr)
104 {
105 uint32_t val;
106
107 val = address_space_ldl_le(&address_space_io, addr,
108 MEMTXATTRS_UNSPECIFIED, NULL);
109 trace_cpu_in(addr, 'l', val);
110 return val;
111 }
112
113 void portio_list_init(PortioList *piolist,
114 Object *owner,
115 const MemoryRegionPortio *callbacks,
116 void *opaque, const char *name)
117 {
118 unsigned n = 0;
119
120 while (callbacks[n].size) {
121 ++n;
122 }
123
124 piolist->ports = callbacks;
125 piolist->nr = 0;
126 piolist->regions = g_new0(MemoryRegion *, n);
127 piolist->address_space = NULL;
128 piolist->addr = 0;
129 piolist->opaque = opaque;
130 piolist->owner = owner;
131 piolist->name = name;
132 piolist->flush_coalesced_mmio = false;
133 }
134
135 void portio_list_set_flush_coalesced(PortioList *piolist)
136 {
137 piolist->flush_coalesced_mmio = true;
138 }
139
140 void portio_list_destroy(PortioList *piolist)
141 {
142 MemoryRegionPortioList *mrpio;
143 unsigned i;
144
145 for (i = 0; i < piolist->nr; ++i) {
146 mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
147 object_unparent(OBJECT(&mrpio->mr));
148 object_unref(mrpio);
149 }
150 g_free(piolist->regions);
151 }
152
153 static const MemoryRegionPortio *find_portio(MemoryRegionPortioList *mrpio,
154 uint64_t offset, unsigned size,
155 bool write)
156 {
157 const MemoryRegionPortio *mrp;
158
159 for (mrp = mrpio->ports; mrp->size; ++mrp) {
160 if (offset >= mrp->offset && offset < mrp->offset + mrp->len &&
161 size == mrp->size &&
162 (write ? (bool)mrp->write : (bool)mrp->read)) {
163 return mrp;
164 }
165 }
166 return NULL;
167 }
168
169 static uint64_t portio_read(void *opaque, hwaddr addr, unsigned size)
170 {
171 MemoryRegionPortioList *mrpio = opaque;
172 const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, false);
173 uint64_t data;
174
175 data = ((uint64_t)1 << (size * 8)) - 1;
176 if (mrp) {
177 data = mrp->read(mrpio->portio_opaque, mrpio->mr.addr + addr);
178 } else if (size == 2) {
179 mrp = find_portio(mrpio, addr, 1, false);
180 if (mrp) {
181 data = mrp->read(mrpio->portio_opaque, mrpio->mr.addr + addr);
182 if (addr + 1 < mrp->offset + mrp->len) {
183 data |= mrp->read(mrpio->portio_opaque, mrpio->mr.addr + addr + 1) << 8;
184 } else {
185 data |= 0xff00;
186 }
187 }
188 }
189 return data;
190 }
191
192 static void portio_write(void *opaque, hwaddr addr, uint64_t data,
193 unsigned size)
194 {
195 MemoryRegionPortioList *mrpio = opaque;
196 const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, true);
197
198 if (mrp) {
199 mrp->write(mrpio->portio_opaque, mrpio->mr.addr + addr, data);
200 } else if (size == 2) {
201 mrp = find_portio(mrpio, addr, 1, true);
202 if (mrp) {
203 mrp->write(mrpio->portio_opaque, mrpio->mr.addr + addr, data & 0xff);
204 if (addr + 1 < mrp->offset + mrp->len) {
205 mrp->write(mrpio->portio_opaque, mrpio->mr.addr + addr + 1, data >> 8);
206 }
207 }
208 }
209 }
210
211 static const MemoryRegionOps portio_ops = {
212 .read = portio_read,
213 .write = portio_write,
214 .endianness = DEVICE_LITTLE_ENDIAN,
215 .valid.unaligned = true,
216 .impl.unaligned = true,
217 };
218
219 static void portio_list_add_1(PortioList *piolist,
220 const MemoryRegionPortio *pio_init,
221 unsigned count, unsigned start,
222 unsigned off_low, unsigned off_high)
223 {
224 MemoryRegionPortioList *mrpio;
225 Object *owner;
226 char *name;
227 unsigned i;
228
229 /* Copy the sub-list and null-terminate it. */
230 mrpio = MEMORY_REGION_PORTIO_LIST(
231 object_new(TYPE_MEMORY_REGION_PORTIO_LIST));
232 mrpio->portio_opaque = piolist->opaque;
233 mrpio->ports = g_new0(MemoryRegionPortio, count + 1);
234 memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count);
235
236 /* Adjust the offsets to all be zero-based for the region. */
237 for (i = 0; i < count; ++i) {
238 mrpio->ports[i].offset -= off_low;
239 }
240
241 /*
242 * The MemoryRegion owner is the MemoryRegionPortioList since that manages
243 * the lifecycle via the refcount
244 */
245 memory_region_init_io(&mrpio->mr, OBJECT(mrpio), &portio_ops, mrpio,
246 piolist->name, off_high - off_low);
247
248 /* Reparent the MemoryRegion to the piolist owner */
249 object_ref(&mrpio->mr);
250 object_unparent(OBJECT(&mrpio->mr));
251 if (!piolist->owner) {
252 owner = machine_get_container("unattached");
253 } else {
254 owner = piolist->owner;
255 }
256 name = g_strdup_printf("%s[*]", piolist->name);
257 object_property_add_child(owner, name, OBJECT(&mrpio->mr));
258 g_free(name);
259
260 if (piolist->flush_coalesced_mmio) {
261 memory_region_set_flush_coalesced(&mrpio->mr);
262 }
263 memory_region_add_subregion(piolist->address_space,
264 start + off_low, &mrpio->mr);
265 piolist->regions[piolist->nr] = &mrpio->mr;
266 ++piolist->nr;
267 }
268
269 void portio_list_add(PortioList *piolist,
270 MemoryRegion *address_space,
271 uint32_t start)
272 {
273 const MemoryRegionPortio *pio, *pio_start = piolist->ports;
274 unsigned int off_low, off_high, off_last, count;
275
276 piolist->address_space = address_space;
277 piolist->addr = start;
278
279 /* Handle the first entry specially. */
280 off_last = off_low = pio_start->offset;
281 off_high = off_low + pio_start->len + pio_start->size - 1;
282 count = 1;
283
284 for (pio = pio_start + 1; pio->size != 0; pio++, count++) {
285 /* All entries must be sorted by offset. */
286 assert(pio->offset >= off_last);
287 off_last = pio->offset;
288
289 /* If we see a hole, break the region. */
290 if (off_last > off_high) {
291 portio_list_add_1(piolist, pio_start, count, start, off_low,
292 off_high);
293 /* ... and start collecting anew. */
294 pio_start = pio;
295 off_low = off_last;
296 off_high = off_low + pio->len + pio_start->size - 1;
297 count = 0;
298 } else if (off_last + pio->len > off_high) {
299 off_high = off_last + pio->len + pio_start->size - 1;
300 }
301 }
302
303 /* There will always be an open sub-list. */
304 portio_list_add_1(piolist, pio_start, count, start, off_low, off_high);
305 }
306
307 void portio_list_del(PortioList *piolist)
308 {
309 MemoryRegionPortioList *mrpio;
310 unsigned i;
311
312 for (i = 0; i < piolist->nr; ++i) {
313 mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
314 memory_region_del_subregion(piolist->address_space, &mrpio->mr);
315 }
316 }
317
318 void portio_list_set_enabled(PortioList *piolist, bool enabled)
319 {
320 unsigned i;
321
322 for (i = 0; i < piolist->nr; ++i) {
323 memory_region_set_enabled(piolist->regions[i], enabled);
324 }
325 }
326
327 void portio_list_set_address(PortioList *piolist, uint32_t addr)
328 {
329 MemoryRegionPortioList *mrpio;
330 unsigned i, j;
331
332 for (i = 0; i < piolist->nr; ++i) {
333 mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
334 memory_region_set_address(&mrpio->mr,
335 mrpio->mr.addr - piolist->addr + addr);
336 for (j = 0; mrpio->ports[j].size; ++j) {
337 mrpio->ports[j].offset += addr - piolist->addr;
338 }
339 }
340
341 piolist->addr = addr;
342 }
343
344 static void memory_region_portio_list_finalize(Object *obj)
345 {
346 MemoryRegionPortioList *mrpio = MEMORY_REGION_PORTIO_LIST(obj);
347
348 /*
349 * This check makes sure any random object_new() (without doing the
350 * rest inits in portio_list_add_1()) will not crash when finalizing.
351 * One example is QMP command qom-list-properties.
352 */
353 if (mrpio->ports) {
354 object_unref(&mrpio->mr);
355 g_free(mrpio->ports);
356 }
357 }
358
359 static const TypeInfo memory_region_portio_list_info = {
360 .parent = TYPE_OBJECT,
361 .name = TYPE_MEMORY_REGION_PORTIO_LIST,
362 .instance_size = sizeof(MemoryRegionPortioList),
363 .instance_finalize = memory_region_portio_list_finalize,
364 };
365
366 static void ioport_register_types(void)
367 {
368 type_register_static(&memory_region_portio_list_info);
369 }
370
371 type_init(ioport_register_types)