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 * split out ioport related stuffs from vl.c.
28 #include "qemu/osdep.h"
30 #include "exec/ioport.h"
31 #include "exec/memory.h"
32 #include "exec/address-spaces.h"
35 struct MemoryRegionPortioList
{
40 MemoryRegionPortio
*ports
;
43 #define TYPE_MEMORY_REGION_PORTIO_LIST "memory-region-portio-list"
44 OBJECT_DECLARE_SIMPLE_TYPE(MemoryRegionPortioList
, MEMORY_REGION_PORTIO_LIST
)
46 static uint64_t unassigned_io_read(void *opaque
, hwaddr addr
, unsigned size
)
51 static void unassigned_io_write(void *opaque
, hwaddr addr
, uint64_t val
,
56 const MemoryRegionOps unassigned_io_ops
= {
57 .read
= unassigned_io_read
,
58 .write
= unassigned_io_write
,
59 .endianness
= DEVICE_NATIVE_ENDIAN
,
62 void cpu_outb(uint32_t addr
, uint8_t val
)
64 trace_cpu_out(addr
, 'b', val
);
65 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
69 void cpu_outw(uint32_t addr
, uint16_t val
)
73 trace_cpu_out(addr
, 'w', val
);
75 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
79 void cpu_outl(uint32_t addr
, uint32_t val
)
83 trace_cpu_out(addr
, 'l', val
);
85 address_space_write(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
89 uint8_t cpu_inb(uint32_t addr
)
93 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
,
95 trace_cpu_in(addr
, 'b', val
);
99 uint16_t cpu_inw(uint32_t addr
)
104 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
, buf
, 2);
106 trace_cpu_in(addr
, 'w', val
);
110 uint32_t cpu_inl(uint32_t addr
)
115 address_space_read(&address_space_io
, addr
, MEMTXATTRS_UNSPECIFIED
, buf
, 4);
117 trace_cpu_in(addr
, 'l', val
);
121 void portio_list_init(PortioList
*piolist
,
123 const MemoryRegionPortio
*callbacks
,
124 void *opaque
, const char *name
)
128 while (callbacks
[n
].size
) {
132 piolist
->ports
= callbacks
;
134 piolist
->regions
= g_new0(MemoryRegion
*, n
);
135 piolist
->address_space
= NULL
;
137 piolist
->opaque
= opaque
;
138 piolist
->owner
= owner
;
139 piolist
->name
= name
;
140 piolist
->flush_coalesced_mmio
= false;
143 void portio_list_set_flush_coalesced(PortioList
*piolist
)
145 piolist
->flush_coalesced_mmio
= true;
148 void portio_list_destroy(PortioList
*piolist
)
150 MemoryRegionPortioList
*mrpio
;
153 for (i
= 0; i
< piolist
->nr
; ++i
) {
154 mrpio
= container_of(piolist
->regions
[i
], MemoryRegionPortioList
, mr
);
155 object_unparent(OBJECT(&mrpio
->mr
));
158 g_free(piolist
->regions
);
161 static const MemoryRegionPortio
*find_portio(MemoryRegionPortioList
*mrpio
,
162 uint64_t offset
, unsigned size
,
165 const MemoryRegionPortio
*mrp
;
167 for (mrp
= mrpio
->ports
; mrp
->size
; ++mrp
) {
168 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
&&
170 (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
177 static uint64_t portio_read(void *opaque
, hwaddr addr
, unsigned size
)
179 MemoryRegionPortioList
*mrpio
= opaque
;
180 const MemoryRegionPortio
*mrp
= find_portio(mrpio
, addr
, size
, false);
183 data
= ((uint64_t)1 << (size
* 8)) - 1;
185 data
= mrp
->read(mrpio
->portio_opaque
, mrpio
->mr
.addr
+ addr
);
186 } else if (size
== 2) {
187 mrp
= find_portio(mrpio
, addr
, 1, false);
189 data
= mrp
->read(mrpio
->portio_opaque
, mrpio
->mr
.addr
+ addr
);
190 if (addr
+ 1 < mrp
->offset
+ mrp
->len
) {
191 data
|= mrp
->read(mrpio
->portio_opaque
, mrpio
->mr
.addr
+ addr
+ 1) << 8;
200 static void portio_write(void *opaque
, hwaddr addr
, uint64_t data
,
203 MemoryRegionPortioList
*mrpio
= opaque
;
204 const MemoryRegionPortio
*mrp
= find_portio(mrpio
, addr
, size
, true);
207 mrp
->write(mrpio
->portio_opaque
, mrpio
->mr
.addr
+ addr
, data
);
208 } else if (size
== 2) {
209 mrp
= find_portio(mrpio
, addr
, 1, true);
211 mrp
->write(mrpio
->portio_opaque
, mrpio
->mr
.addr
+ addr
, data
& 0xff);
212 if (addr
+ 1 < mrp
->offset
+ mrp
->len
) {
213 mrp
->write(mrpio
->portio_opaque
, mrpio
->mr
.addr
+ addr
+ 1, data
>> 8);
219 static const MemoryRegionOps portio_ops
= {
221 .write
= portio_write
,
222 .endianness
= DEVICE_LITTLE_ENDIAN
,
223 .valid
.unaligned
= true,
224 .impl
.unaligned
= true,
227 static void portio_list_add_1(PortioList
*piolist
,
228 const MemoryRegionPortio
*pio_init
,
229 unsigned count
, unsigned start
,
230 unsigned off_low
, unsigned off_high
)
232 MemoryRegionPortioList
*mrpio
;
237 /* Copy the sub-list and null-terminate it. */
238 mrpio
= MEMORY_REGION_PORTIO_LIST(
239 object_new(TYPE_MEMORY_REGION_PORTIO_LIST
));
240 mrpio
->portio_opaque
= piolist
->opaque
;
241 mrpio
->ports
= g_malloc0(sizeof(MemoryRegionPortio
) * (count
+ 1));
242 memcpy(mrpio
->ports
, pio_init
, sizeof(MemoryRegionPortio
) * count
);
243 memset(mrpio
->ports
+ count
, 0, sizeof(MemoryRegionPortio
));
245 /* Adjust the offsets to all be zero-based for the region. */
246 for (i
= 0; i
< count
; ++i
) {
247 mrpio
->ports
[i
].offset
-= off_low
;
251 * The MemoryRegion owner is the MemoryRegionPortioList since that manages
252 * the lifecycle via the refcount
254 memory_region_init_io(&mrpio
->mr
, OBJECT(mrpio
), &portio_ops
, mrpio
,
255 piolist
->name
, off_high
- off_low
);
257 /* Reparent the MemoryRegion to the piolist owner */
258 object_ref(&mrpio
->mr
);
259 object_unparent(OBJECT(&mrpio
->mr
));
260 if (!piolist
->owner
) {
261 owner
= container_get(qdev_get_machine(), "/unattached");
263 owner
= piolist
->owner
;
265 name
= g_strdup_printf("%s[*]", piolist
->name
);
266 object_property_add_child(owner
, name
, OBJECT(&mrpio
->mr
));
269 if (piolist
->flush_coalesced_mmio
) {
270 memory_region_set_flush_coalesced(&mrpio
->mr
);
272 memory_region_add_subregion(piolist
->address_space
,
273 start
+ off_low
, &mrpio
->mr
);
274 piolist
->regions
[piolist
->nr
] = &mrpio
->mr
;
278 void portio_list_add(PortioList
*piolist
,
279 MemoryRegion
*address_space
,
282 const MemoryRegionPortio
*pio
, *pio_start
= piolist
->ports
;
283 unsigned int off_low
, off_high
, off_last
, count
;
285 piolist
->address_space
= address_space
;
286 piolist
->addr
= start
;
288 /* Handle the first entry specially. */
289 off_last
= off_low
= pio_start
->offset
;
290 off_high
= off_low
+ pio_start
->len
+ pio_start
->size
- 1;
293 for (pio
= pio_start
+ 1; pio
->size
!= 0; pio
++, count
++) {
294 /* All entries must be sorted by offset. */
295 assert(pio
->offset
>= off_last
);
296 off_last
= pio
->offset
;
298 /* If we see a hole, break the region. */
299 if (off_last
> off_high
) {
300 portio_list_add_1(piolist
, pio_start
, count
, start
, off_low
,
302 /* ... and start collecting anew. */
305 off_high
= off_low
+ pio
->len
+ pio_start
->size
- 1;
307 } else if (off_last
+ pio
->len
> off_high
) {
308 off_high
= off_last
+ pio
->len
+ pio_start
->size
- 1;
312 /* There will always be an open sub-list. */
313 portio_list_add_1(piolist
, pio_start
, count
, start
, off_low
, off_high
);
316 void portio_list_del(PortioList
*piolist
)
318 MemoryRegionPortioList
*mrpio
;
321 for (i
= 0; i
< piolist
->nr
; ++i
) {
322 mrpio
= container_of(piolist
->regions
[i
], MemoryRegionPortioList
, mr
);
323 memory_region_del_subregion(piolist
->address_space
, &mrpio
->mr
);
327 void portio_list_set_enabled(PortioList
*piolist
, bool enabled
)
331 for (i
= 0; i
< piolist
->nr
; ++i
) {
332 memory_region_set_enabled(piolist
->regions
[i
], enabled
);
336 void portio_list_set_address(PortioList
*piolist
, uint32_t addr
)
338 MemoryRegionPortioList
*mrpio
;
341 for (i
= 0; i
< piolist
->nr
; ++i
) {
342 mrpio
= container_of(piolist
->regions
[i
], MemoryRegionPortioList
, mr
);
343 memory_region_set_address(&mrpio
->mr
,
344 mrpio
->mr
.addr
- piolist
->addr
+ addr
);
345 for (j
= 0; mrpio
->ports
[j
].size
; ++j
) {
346 mrpio
->ports
[j
].offset
+= addr
- piolist
->addr
;
350 piolist
->addr
= addr
;
353 static void memory_region_portio_list_finalize(Object
*obj
)
355 MemoryRegionPortioList
*mrpio
= MEMORY_REGION_PORTIO_LIST(obj
);
357 object_unref(&mrpio
->mr
);
358 g_free(mrpio
->ports
);
361 static const TypeInfo memory_region_portio_list_info
= {
362 .parent
= TYPE_OBJECT
,
363 .name
= TYPE_MEMORY_REGION_PORTIO_LIST
,
364 .instance_size
= sizeof(MemoryRegionPortioList
),
365 .instance_finalize
= memory_region_portio_list_finalize
,
368 static void ioport_register_types(void)
370 type_register_static(&memory_region_portio_list_info
);
373 type_init(ioport_register_types
)