pseries: Clean up error handling in spapr_vga_init()
[qemu/ar7.git] / ioport.c
blob193ef7685cf5233d41bb54d0915b31cfede5596f
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 * splitted out ioport related stuffs from vl.c.
28 #include "exec/ioport.h"
29 #include "trace.h"
30 #include "exec/memory.h"
31 #include "exec/address-spaces.h"
33 typedef struct MemoryRegionPortioList {
34 MemoryRegion mr;
35 void *portio_opaque;
36 MemoryRegionPortio ports[];
37 } MemoryRegionPortioList;
39 static uint64_t unassigned_io_read(void *opaque, hwaddr addr, unsigned size)
41 return -1ULL;
44 static void unassigned_io_write(void *opaque, hwaddr addr, uint64_t val,
45 unsigned size)
49 const MemoryRegionOps unassigned_io_ops = {
50 .read = unassigned_io_read,
51 .write = unassigned_io_write,
52 .endianness = DEVICE_NATIVE_ENDIAN,
55 void cpu_outb(pio_addr_t addr, uint8_t val)
57 trace_cpu_out(addr, 'b', val);
58 address_space_write(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
59 &val, 1);
62 void cpu_outw(pio_addr_t addr, uint16_t val)
64 uint8_t buf[2];
66 trace_cpu_out(addr, 'w', val);
67 stw_p(buf, val);
68 address_space_write(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
69 buf, 2);
72 void cpu_outl(pio_addr_t addr, uint32_t val)
74 uint8_t buf[4];
76 trace_cpu_out(addr, 'l', val);
77 stl_p(buf, val);
78 address_space_write(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
79 buf, 4);
82 uint8_t cpu_inb(pio_addr_t addr)
84 uint8_t val;
86 address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED,
87 &val, 1);
88 trace_cpu_in(addr, 'b', val);
89 return val;
92 uint16_t cpu_inw(pio_addr_t addr)
94 uint8_t buf[2];
95 uint16_t val;
97 address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, buf, 2);
98 val = lduw_p(buf);
99 trace_cpu_in(addr, 'w', val);
100 return val;
103 uint32_t cpu_inl(pio_addr_t addr)
105 uint8_t buf[4];
106 uint32_t val;
108 address_space_read(&address_space_io, addr, MEMTXATTRS_UNSPECIFIED, buf, 4);
109 val = ldl_p(buf);
110 trace_cpu_in(addr, 'l', val);
111 return val;
114 void portio_list_init(PortioList *piolist,
115 Object *owner,
116 const MemoryRegionPortio *callbacks,
117 void *opaque, const char *name)
119 unsigned n = 0;
121 while (callbacks[n].size) {
122 ++n;
125 piolist->ports = callbacks;
126 piolist->nr = 0;
127 piolist->regions = g_new0(MemoryRegion *, n);
128 piolist->address_space = NULL;
129 piolist->opaque = opaque;
130 piolist->owner = owner;
131 piolist->name = name;
132 piolist->flush_coalesced_mmio = false;
135 void portio_list_set_flush_coalesced(PortioList *piolist)
137 piolist->flush_coalesced_mmio = true;
140 void portio_list_destroy(PortioList *piolist)
142 MemoryRegionPortioList *mrpio;
143 unsigned i;
145 for (i = 0; i < piolist->nr; ++i) {
146 mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
147 object_unparent(OBJECT(&mrpio->mr));
148 g_free(mrpio);
150 g_free(piolist->regions);
153 static const MemoryRegionPortio *find_portio(MemoryRegionPortioList *mrpio,
154 uint64_t offset, unsigned size,
155 bool write)
157 const MemoryRegionPortio *mrp;
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;
166 return NULL;
169 static uint64_t portio_read(void *opaque, hwaddr addr, unsigned size)
171 MemoryRegionPortioList *mrpio = opaque;
172 const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, false);
173 uint64_t data;
175 data = ((uint64_t)1 << (size * 8)) - 1;
176 if (mrp) {
177 data = mrp->read(mrpio->portio_opaque, mrp->base + addr);
178 } else if (size == 2) {
179 mrp = find_portio(mrpio, addr, 1, false);
180 if (mrp) {
181 data = mrp->read(mrpio->portio_opaque, mrp->base + addr);
182 if (addr + 1 < mrp->offset + mrp->len) {
183 data |= mrp->read(mrpio->portio_opaque, mrp->base + addr + 1) << 8;
184 } else {
185 data |= 0xff00;
189 return data;
192 static void portio_write(void *opaque, hwaddr addr, uint64_t data,
193 unsigned size)
195 MemoryRegionPortioList *mrpio = opaque;
196 const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, true);
198 if (mrp) {
199 mrp->write(mrpio->portio_opaque, mrp->base + addr, data);
200 } else if (size == 2) {
201 mrp = find_portio(mrpio, addr, 1, true);
202 if (mrp) {
203 mrp->write(mrpio->portio_opaque, mrp->base + addr, data & 0xff);
204 if (addr + 1 < mrp->offset + mrp->len) {
205 mrp->write(mrpio->portio_opaque, mrp->base + addr + 1, data >> 8);
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,
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)
224 MemoryRegionPortioList *mrpio;
225 unsigned i;
227 /* Copy the sub-list and null-terminate it. */
228 mrpio = g_malloc0(sizeof(MemoryRegionPortioList) +
229 sizeof(MemoryRegionPortio) * (count + 1));
230 mrpio->portio_opaque = piolist->opaque;
231 memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count);
232 memset(mrpio->ports + count, 0, sizeof(MemoryRegionPortio));
234 /* Adjust the offsets to all be zero-based for the region. */
235 for (i = 0; i < count; ++i) {
236 mrpio->ports[i].offset -= off_low;
237 mrpio->ports[i].base = start + off_low;
240 memory_region_init_io(&mrpio->mr, piolist->owner, &portio_ops, mrpio,
241 piolist->name, off_high - off_low);
242 if (piolist->flush_coalesced_mmio) {
243 memory_region_set_flush_coalesced(&mrpio->mr);
245 memory_region_add_subregion(piolist->address_space,
246 start + off_low, &mrpio->mr);
247 piolist->regions[piolist->nr] = &mrpio->mr;
248 ++piolist->nr;
251 void portio_list_add(PortioList *piolist,
252 MemoryRegion *address_space,
253 uint32_t start)
255 const MemoryRegionPortio *pio, *pio_start = piolist->ports;
256 unsigned int off_low, off_high, off_last, count;
258 piolist->address_space = address_space;
260 /* Handle the first entry specially. */
261 off_last = off_low = pio_start->offset;
262 off_high = off_low + pio_start->len + pio_start->size - 1;
263 count = 1;
265 for (pio = pio_start + 1; pio->size != 0; pio++, count++) {
266 /* All entries must be sorted by offset. */
267 assert(pio->offset >= off_last);
268 off_last = pio->offset;
270 /* If we see a hole, break the region. */
271 if (off_last > off_high) {
272 portio_list_add_1(piolist, pio_start, count, start, off_low,
273 off_high);
274 /* ... and start collecting anew. */
275 pio_start = pio;
276 off_low = off_last;
277 off_high = off_low + pio->len + pio_start->size - 1;
278 count = 0;
279 } else if (off_last + pio->len > off_high) {
280 off_high = off_last + pio->len + pio_start->size - 1;
284 /* There will always be an open sub-list. */
285 portio_list_add_1(piolist, pio_start, count, start, off_low, off_high);
288 void portio_list_del(PortioList *piolist)
290 MemoryRegionPortioList *mrpio;
291 unsigned i;
293 for (i = 0; i < piolist->nr; ++i) {
294 mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
295 memory_region_del_subregion(piolist->address_space, &mrpio->mr);