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
25 * splitted out ioport related stuffs from vl.c.
28 #include "qemu/osdep.h"
29 #include "qemu-common.h"
31 #include "exec/ioport.h"
32 #include "trace-root.h"
33 #include "exec/memory.h"
34 #include "exec/address-spaces.h"
36 typedef struct MemoryRegionPortioList
{
39 MemoryRegionPortio ports
[];
40 } MemoryRegionPortioList
;
42 static uint64_t unassigned_io_read(void *opaque
, hwaddr addr
, unsigned size
)
47 static void unassigned_io_write(void *opaque
, hwaddr addr
, uint64_t val
,
52 const MemoryRegionOps unassigned_io_ops
= {
53 .read
= unassigned_io_read
,
54 .write
= unassigned_io_write
,
55 .endianness
= DEVICE_NATIVE_ENDIAN
,
58 void cpu_outb(uint32_t addr
, uint8_t val
)
60 trace_cpu_out(addr
, 'b', val
);
61 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
65 void cpu_outw(uint32_t addr
, uint16_t val
)
69 trace_cpu_out(addr
, 'w', val
);
71 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
75 void cpu_outl(uint32_t addr
, uint32_t val
)
79 trace_cpu_out(addr
, 'l', val
);
81 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
85 uint8_t cpu_inb(uint32_t addr
)
89 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
91 trace_cpu_in(addr
, 'b', val
);
95 uint16_t cpu_inw(uint32_t addr
)
100 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
, buf
, 2);
102 trace_cpu_in(addr
, 'w', val
);
106 uint32_t cpu_inl(uint32_t addr
)
111 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
, buf
, 4);
113 trace_cpu_in(addr
, 'l', val
);
117 void portio_list_init(PortioList
*piolist
,
119 const MemoryRegionPortio
*callbacks
,
120 void *opaque
, const char *name
)
124 while (callbacks
[n
].size
) {
128 piolist
->ports
= callbacks
;
130 piolist
->regions
= g_new0(MemoryRegion
*, n
);
131 piolist
->address_space
= NULL
;
132 piolist
->opaque
= opaque
;
133 piolist
->owner
= owner
;
134 piolist
->name
= name
;
135 piolist
->flush_coalesced_mmio
= false;
138 void portio_list_set_flush_coalesced(PortioList
*piolist
)
140 piolist
->flush_coalesced_mmio
= true;
143 void portio_list_destroy(PortioList
*piolist
)
145 MemoryRegionPortioList
*mrpio
;
148 for (i
= 0; i
< piolist
->nr
; ++i
) {
149 mrpio
= container_of(piolist
->regions
[i
], MemoryRegionPortioList
, mr
);
150 object_unparent(OBJECT(&mrpio
->mr
));
153 g_free(piolist
->regions
);
156 static const MemoryRegionPortio
*find_portio(MemoryRegionPortioList
*mrpio
,
157 uint64_t offset
, unsigned size
,
160 const MemoryRegionPortio
*mrp
;
162 for (mrp
= mrpio
->ports
; mrp
->size
; ++mrp
) {
163 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
&&
165 (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
172 static uint64_t portio_read(void *opaque
, hwaddr addr
, unsigned size
)
174 MemoryRegionPortioList
*mrpio
= opaque
;
175 const MemoryRegionPortio
*mrp
= find_portio(mrpio
, addr
, size
, false);
178 data
= ((uint64_t)1 << (size
* 8)) - 1;
180 data
= mrp
->read(mrpio
->portio_opaque
, mrp
->base
+ addr
);
181 } else if (size
== 2) {
182 mrp
= find_portio(mrpio
, addr
, 1, false);
184 data
= mrp
->read(mrpio
->portio_opaque
, mrp
->base
+ addr
);
185 if (addr
+ 1 < mrp
->offset
+ mrp
->len
) {
186 data
|= mrp
->read(mrpio
->portio_opaque
, mrp
->base
+ addr
+ 1) << 8;
195 static void portio_write(void *opaque
, hwaddr addr
, uint64_t data
,
198 MemoryRegionPortioList
*mrpio
= opaque
;
199 const MemoryRegionPortio
*mrp
= find_portio(mrpio
, addr
, size
, true);
202 mrp
->write(mrpio
->portio_opaque
, mrp
->base
+ addr
, data
);
203 } else if (size
== 2) {
204 mrp
= find_portio(mrpio
, addr
, 1, true);
206 mrp
->write(mrpio
->portio_opaque
, mrp
->base
+ addr
, data
& 0xff);
207 if (addr
+ 1 < mrp
->offset
+ mrp
->len
) {
208 mrp
->write(mrpio
->portio_opaque
, mrp
->base
+ addr
+ 1, data
>> 8);
214 static const MemoryRegionOps portio_ops
= {
216 .write
= portio_write
,
217 .endianness
= DEVICE_LITTLE_ENDIAN
,
218 .valid
.unaligned
= true,
219 .impl
.unaligned
= true,
222 static void portio_list_add_1(PortioList
*piolist
,
223 const MemoryRegionPortio
*pio_init
,
224 unsigned count
, unsigned start
,
225 unsigned off_low
, unsigned off_high
)
227 MemoryRegionPortioList
*mrpio
;
230 /* Copy the sub-list and null-terminate it. */
231 mrpio
= g_malloc0(sizeof(MemoryRegionPortioList
) +
232 sizeof(MemoryRegionPortio
) * (count
+ 1));
233 mrpio
->portio_opaque
= piolist
->opaque
;
234 memcpy(mrpio
->ports
, pio_init
, sizeof(MemoryRegionPortio
) * count
);
235 memset(mrpio
->ports
+ count
, 0, sizeof(MemoryRegionPortio
));
237 /* Adjust the offsets to all be zero-based for the region. */
238 for (i
= 0; i
< count
; ++i
) {
239 mrpio
->ports
[i
].offset
-= off_low
;
240 mrpio
->ports
[i
].base
= start
+ off_low
;
243 memory_region_init_io(&mrpio
->mr
, piolist
->owner
, &portio_ops
, mrpio
,
244 piolist
->name
, off_high
- off_low
);
245 if (piolist
->flush_coalesced_mmio
) {
246 memory_region_set_flush_coalesced(&mrpio
->mr
);
248 memory_region_add_subregion(piolist
->address_space
,
249 start
+ off_low
, &mrpio
->mr
);
250 piolist
->regions
[piolist
->nr
] = &mrpio
->mr
;
254 void portio_list_add(PortioList
*piolist
,
255 MemoryRegion
*address_space
,
258 const MemoryRegionPortio
*pio
, *pio_start
= piolist
->ports
;
259 unsigned int off_low
, off_high
, off_last
, count
;
261 piolist
->address_space
= address_space
;
263 /* Handle the first entry specially. */
264 off_last
= off_low
= pio_start
->offset
;
265 off_high
= off_low
+ pio_start
->len
+ pio_start
->size
- 1;
268 for (pio
= pio_start
+ 1; pio
->size
!= 0; pio
++, count
++) {
269 /* All entries must be sorted by offset. */
270 assert(pio
->offset
>= off_last
);
271 off_last
= pio
->offset
;
273 /* If we see a hole, break the region. */
274 if (off_last
> off_high
) {
275 portio_list_add_1(piolist
, pio_start
, count
, start
, off_low
,
277 /* ... and start collecting anew. */
280 off_high
= off_low
+ pio
->len
+ pio_start
->size
- 1;
282 } else if (off_last
+ pio
->len
> off_high
) {
283 off_high
= off_last
+ pio
->len
+ pio_start
->size
- 1;
287 /* There will always be an open sub-list. */
288 portio_list_add_1(piolist
, pio_start
, count
, start
, off_low
, off_high
);
291 void portio_list_del(PortioList
*piolist
)
293 MemoryRegionPortioList
*mrpio
;
296 for (i
= 0; i
< piolist
->nr
; ++i
) {
297 mrpio
= container_of(piolist
->regions
[i
], MemoryRegionPortioList
, mr
);
298 memory_region_del_subregion(piolist
->address_space
, &mrpio
->mr
);