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 "exec/ioport.h"
30 #include "exec/memory.h"
31 #include "exec/address-spaces.h"
33 typedef struct MemoryRegionPortioList
{
36 MemoryRegionPortio ports
[];
37 } MemoryRegionPortioList
;
39 static uint64_t unassigned_io_read(void *opaque
, hwaddr addr
, unsigned size
)
44 static void unassigned_io_write(void *opaque
, hwaddr addr
, uint64_t val
,
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
,
62 void cpu_outw(pio_addr_t addr
, uint16_t val
)
66 trace_cpu_out(addr
, 'w', val
);
68 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
72 void cpu_outl(pio_addr_t addr
, uint32_t val
)
76 trace_cpu_out(addr
, 'l', val
);
78 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
82 uint8_t cpu_inb(pio_addr_t addr
)
86 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
88 trace_cpu_in(addr
, 'b', val
);
92 uint16_t cpu_inw(pio_addr_t addr
)
97 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
, buf
, 2);
99 trace_cpu_in(addr
, 'w', val
);
103 uint32_t cpu_inl(pio_addr_t addr
)
108 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
, buf
, 4);
110 trace_cpu_in(addr
, 'l', val
);
114 void portio_list_init(PortioList
*piolist
,
116 const MemoryRegionPortio
*callbacks
,
117 void *opaque
, const char *name
)
121 while (callbacks
[n
].size
) {
125 piolist
->ports
= callbacks
;
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
;
145 for (i
= 0; i
< piolist
->nr
; ++i
) {
146 mrpio
= container_of(piolist
->regions
[i
], MemoryRegionPortioList
, mr
);
147 object_unparent(OBJECT(&mrpio
->mr
));
150 g_free(piolist
->regions
);
153 static const MemoryRegionPortio
*find_portio(MemoryRegionPortioList
*mrpio
,
154 uint64_t offset
, unsigned size
,
157 const MemoryRegionPortio
*mrp
;
159 for (mrp
= mrpio
->ports
; mrp
->size
; ++mrp
) {
160 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
&&
162 (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
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);
175 data
= ((uint64_t)1 << (size
* 8)) - 1;
177 data
= mrp
->read(mrpio
->portio_opaque
, mrp
->base
+ addr
);
178 } else if (size
== 2) {
179 mrp
= find_portio(mrpio
, addr
, 1, false);
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;
192 static void portio_write(void *opaque
, hwaddr addr
, uint64_t data
,
195 MemoryRegionPortioList
*mrpio
= opaque
;
196 const MemoryRegionPortio
*mrp
= find_portio(mrpio
, addr
, size
, true);
199 mrp
->write(mrpio
->portio_opaque
, mrp
->base
+ addr
, data
);
200 } else if (size
== 2) {
201 mrp
= find_portio(mrpio
, addr
, 1, true);
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
= {
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
;
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
;
251 void portio_list_add(PortioList
*piolist
,
252 MemoryRegion
*address_space
,
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;
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
,
274 /* ... and start collecting anew. */
277 off_high
= off_low
+ pio
->len
+ pio_start
->size
- 1;
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
;
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
);