ioport: Switch dispatching to memory core layer

The current ioport dispatcher is a complex beast, mostly due to the
need to deal with old portio interface users. But we can overcome it
without converting all portio users by embedding the required base
address of a MemoryRegionPortio access into that data structure. That
removes the need to have the additional MemoryRegionIORange structure
in the loop on every access.

To handle old portio memory ops, we simply install dispatching handlers
for portio memory regions when registering them with the memory core.
This removes the need for the old_portio field.

We can drop the additional aliasing of ioport regions and also the
special address space listener. cpu_in and cpu_out now simply call
address_space_read/write. And we can concentrate portio handling in a
single source file.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Jan Kiszka 2013-06-24 10:45:09 +02:00 committed by Paolo Bonzini
parent 962b03fcf5
commit b40acf99be
6 changed files with 102 additions and 159 deletions

27
exec.c
View File

@ -1752,26 +1752,6 @@ static void core_log_global_stop(MemoryListener *listener)
cpu_physical_memory_set_dirty_tracking(0); cpu_physical_memory_set_dirty_tracking(0);
} }
static void io_region_add(MemoryListener *listener,
MemoryRegionSection *section)
{
MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);
mrio->mr = section->mr;
mrio->offset = section->offset_within_region;
iorange_init(&mrio->iorange, &memory_region_iorange_ops,
section->offset_within_address_space,
int128_get64(section->size));
ioport_register(&mrio->iorange);
}
static void io_region_del(MemoryListener *listener,
MemoryRegionSection *section)
{
isa_unassign_ioport(section->offset_within_address_space,
int128_get64(section->size));
}
static MemoryListener core_memory_listener = { static MemoryListener core_memory_listener = {
.begin = core_begin, .begin = core_begin,
.log_global_start = core_log_global_start, .log_global_start = core_log_global_start,
@ -1779,12 +1759,6 @@ static MemoryListener core_memory_listener = {
.priority = 1, .priority = 1,
}; };
static MemoryListener io_memory_listener = {
.region_add = io_region_add,
.region_del = io_region_del,
.priority = 0,
};
static MemoryListener tcg_memory_listener = { static MemoryListener tcg_memory_listener = {
.commit = tcg_commit, .commit = tcg_commit,
}; };
@ -1826,7 +1800,6 @@ static void memory_map_init(void)
address_space_init(&address_space_io, system_io, "I/O"); address_space_init(&address_space_io, system_io, "I/O");
memory_listener_register(&core_memory_listener, &address_space_memory); memory_listener_register(&core_memory_listener, &address_space_memory);
memory_listener_register(&io_memory_listener, &address_space_io);
memory_listener_register(&tcg_memory_listener, &address_space_memory); memory_listener_register(&tcg_memory_listener, &address_space_memory);
} }

View File

@ -56,7 +56,6 @@ typedef struct PortioList {
struct MemoryRegion *address_space; struct MemoryRegion *address_space;
unsigned nr; unsigned nr;
struct MemoryRegion **regions; struct MemoryRegion **regions;
struct MemoryRegion **aliases;
void *opaque; void *opaque;
const char *name; const char *name;
} PortioList; } PortioList;

View File

@ -119,8 +119,6 @@ static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end, void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
int dirty_flags); int dirty_flags);
extern const IORangeOps memory_region_iorange_ops;
#endif #endif
#endif #endif

View File

@ -126,10 +126,6 @@ struct MemoryRegionOps {
bool unaligned; bool unaligned;
} impl; } impl;
/* If .read and .write are not present, old_portio may be used for
* backwards compatibility with old portio registration
*/
const MemoryRegionPortio *old_portio;
/* If .read and .write are not present, old_mmio may be used for /* If .read and .write are not present, old_mmio may be used for
* backwards compatibility with old mmio registration * backwards compatibility with old mmio registration
*/ */
@ -185,6 +181,7 @@ struct MemoryRegionPortio {
unsigned size; unsigned size;
IOPortReadFunc *read; IOPortReadFunc *read;
IOPortWriteFunc *write; IOPortWriteFunc *write;
uint32_t base; /* private field */
}; };
#define PORTIO_END_OF_LIST() { } #define PORTIO_END_OF_LIST() { }

138
ioport.c
View File

@ -28,6 +28,7 @@
#include "exec/ioport.h" #include "exec/ioport.h"
#include "trace.h" #include "trace.h"
#include "exec/memory.h" #include "exec/memory.h"
#include "exec/address-spaces.h"
/***********************************************************/ /***********************************************************/
/* IO Port */ /* IO Port */
@ -47,6 +48,12 @@
# define LOG_IOPORT(...) do { } while (0) # define LOG_IOPORT(...) do { } while (0)
#endif #endif
typedef struct MemoryRegionPortioList {
MemoryRegion mr;
void *portio_opaque;
MemoryRegionPortio ports[];
} MemoryRegionPortioList;
/* XXX: use a two level table to limit memory usage */ /* XXX: use a two level table to limit memory usage */
static void *ioport_opaque[MAX_IOPORTS]; static void *ioport_opaque[MAX_IOPORTS];
@ -279,27 +286,34 @@ void cpu_outb(pio_addr_t addr, uint8_t val)
{ {
LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
trace_cpu_out(addr, val); trace_cpu_out(addr, val);
ioport_write(0, addr, val); address_space_write(&address_space_io, addr, &val, 1);
} }
void cpu_outw(pio_addr_t addr, uint16_t val) void cpu_outw(pio_addr_t addr, uint16_t val)
{ {
uint8_t buf[2];
LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); LOG_IOPORT("outw: %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
trace_cpu_out(addr, val); trace_cpu_out(addr, val);
ioport_write(1, addr, val); stw_p(buf, val);
address_space_write(&address_space_io, addr, buf, 2);
} }
void cpu_outl(pio_addr_t addr, uint32_t val) void cpu_outl(pio_addr_t addr, uint32_t val)
{ {
uint8_t buf[4];
LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
trace_cpu_out(addr, val); trace_cpu_out(addr, val);
ioport_write(2, addr, val); stl_p(buf, val);
address_space_write(&address_space_io, addr, buf, 4);
} }
uint8_t cpu_inb(pio_addr_t addr) uint8_t cpu_inb(pio_addr_t addr)
{ {
uint8_t val; uint8_t val;
val = ioport_read(0, addr);
address_space_read(&address_space_io, addr, &val, 1);
trace_cpu_in(addr, val); trace_cpu_in(addr, val);
LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val);
return val; return val;
@ -307,8 +321,11 @@ uint8_t cpu_inb(pio_addr_t addr)
uint16_t cpu_inw(pio_addr_t addr) uint16_t cpu_inw(pio_addr_t addr)
{ {
uint8_t buf[2];
uint16_t val; uint16_t val;
val = ioport_read(1, addr);
address_space_read(&address_space_io, addr, buf, 2);
val = lduw_p(buf);
trace_cpu_in(addr, val); trace_cpu_in(addr, val);
LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val);
return val; return val;
@ -316,8 +333,11 @@ uint16_t cpu_inw(pio_addr_t addr)
uint32_t cpu_inl(pio_addr_t addr) uint32_t cpu_inl(pio_addr_t addr)
{ {
uint8_t buf[4];
uint32_t val; uint32_t val;
val = ioport_read(2, addr);
address_space_read(&address_space_io, addr, buf, 4);
val = ldl_p(buf);
trace_cpu_in(addr, val); trace_cpu_in(addr, val);
LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val);
return val; return val;
@ -336,7 +356,6 @@ void portio_list_init(PortioList *piolist,
piolist->ports = callbacks; piolist->ports = callbacks;
piolist->nr = 0; piolist->nr = 0;
piolist->regions = g_new0(MemoryRegion *, n); piolist->regions = g_new0(MemoryRegion *, n);
piolist->aliases = g_new0(MemoryRegion *, n);
piolist->address_space = NULL; piolist->address_space = NULL;
piolist->opaque = opaque; piolist->opaque = opaque;
piolist->name = name; piolist->name = name;
@ -345,46 +364,96 @@ void portio_list_init(PortioList *piolist,
void portio_list_destroy(PortioList *piolist) void portio_list_destroy(PortioList *piolist)
{ {
g_free(piolist->regions); g_free(piolist->regions);
g_free(piolist->aliases);
} }
static const MemoryRegionPortio *find_portio(MemoryRegionPortioList *mrpio,
uint64_t offset, unsigned size,
bool write)
{
const MemoryRegionPortio *mrp;
for (mrp = mrpio->ports; mrp->size; ++mrp) {
if (offset >= mrp->offset && offset < mrp->offset + mrp->len &&
size == mrp->size &&
(write ? (bool)mrp->write : (bool)mrp->read)) {
return mrp;
}
}
return NULL;
}
static uint64_t portio_read(void *opaque, hwaddr addr, unsigned size)
{
MemoryRegionPortioList *mrpio = opaque;
const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, false);
uint64_t data;
data = ((uint64_t)1 << (size * 8)) - 1;
if (mrp) {
data = mrp->read(mrpio->portio_opaque, mrp->base + addr);
} else if (size == 2) {
mrp = find_portio(mrpio, addr, 1, false);
assert(mrp);
data = mrp->read(mrpio->portio_opaque, mrp->base + addr) |
(mrp->read(mrpio->portio_opaque, mrp->base + addr + 1) << 8);
}
return data;
}
static void portio_write(void *opaque, hwaddr addr, uint64_t data,
unsigned size)
{
MemoryRegionPortioList *mrpio = opaque;
const MemoryRegionPortio *mrp = find_portio(mrpio, addr, size, true);
if (mrp) {
mrp->write(mrpio->portio_opaque, mrp->base + addr, data);
} else if (size == 2) {
mrp = find_portio(mrpio, addr, 1, true);
assert(mrp);
mrp->write(mrpio->portio_opaque, mrp->base + addr, data & 0xff);
mrp->write(mrpio->portio_opaque, mrp->base + addr + 1, data >> 8);
}
}
static const MemoryRegionOps portio_ops = {
.read = portio_read,
.write = portio_write,
.endianness = DEVICE_LITTLE_ENDIAN,
.valid.unaligned = true,
.impl.unaligned = true,
};
static void portio_list_add_1(PortioList *piolist, static void portio_list_add_1(PortioList *piolist,
const MemoryRegionPortio *pio_init, const MemoryRegionPortio *pio_init,
unsigned count, unsigned start, unsigned count, unsigned start,
unsigned off_low, unsigned off_high) unsigned off_low, unsigned off_high)
{ {
MemoryRegionPortio *pio; MemoryRegionPortioList *mrpio;
MemoryRegionOps *ops;
MemoryRegion *region, *alias;
unsigned i; unsigned i;
/* Copy the sub-list and null-terminate it. */ /* Copy the sub-list and null-terminate it. */
pio = g_new(MemoryRegionPortio, count + 1); mrpio = g_malloc0(sizeof(MemoryRegionPortioList) +
memcpy(pio, pio_init, sizeof(MemoryRegionPortio) * count); sizeof(MemoryRegionPortio) * (count + 1));
memset(pio + count, 0, sizeof(MemoryRegionPortio)); mrpio->portio_opaque = piolist->opaque;
memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count);
memset(mrpio->ports + count, 0, sizeof(MemoryRegionPortio));
/* Adjust the offsets to all be zero-based for the region. */ /* Adjust the offsets to all be zero-based for the region. */
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
pio[i].offset -= off_low; mrpio->ports[i].offset -= off_low;
mrpio->ports[i].base = start + off_low;
} }
ops = g_new0(MemoryRegionOps, 1);
ops->old_portio = pio;
region = g_new(MemoryRegion, 1);
alias = g_new(MemoryRegion, 1);
/* /*
* Use an alias so that the callback is called with an absolute address, * Use an alias so that the callback is called with an absolute address,
* rather than an offset relative to to start + off_low. * rather than an offset relative to to start + off_low.
*/ */
memory_region_init_io(region, ops, piolist->opaque, piolist->name, memory_region_init_io(&mrpio->mr, &portio_ops, mrpio, piolist->name,
INT64_MAX); off_high - off_low);
memory_region_init_alias(alias, piolist->name,
region, start + off_low, off_high - off_low);
memory_region_add_subregion(piolist->address_space, memory_region_add_subregion(piolist->address_space,
start + off_low, alias); start + off_low, &mrpio->mr);
piolist->regions[piolist->nr] = region; piolist->regions[piolist->nr] = &mrpio->mr;
piolist->aliases[piolist->nr] = alias;
++piolist->nr; ++piolist->nr;
} }
@ -427,19 +496,14 @@ void portio_list_add(PortioList *piolist,
void portio_list_del(PortioList *piolist) void portio_list_del(PortioList *piolist)
{ {
MemoryRegion *mr, *alias; MemoryRegionPortioList *mrpio;
unsigned i; unsigned i;
for (i = 0; i < piolist->nr; ++i) { for (i = 0; i < piolist->nr; ++i) {
mr = piolist->regions[i]; mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
alias = piolist->aliases[i]; memory_region_del_subregion(piolist->address_space, &mrpio->mr);
memory_region_del_subregion(piolist->address_space, alias); memory_region_destroy(&mrpio->mr);
memory_region_destroy(alias); g_free(mrpio);
memory_region_destroy(mr);
g_free((MemoryRegionOps *)mr->ops);
g_free(mr);
g_free(alias);
piolist->regions[i] = NULL; piolist->regions[i] = NULL;
piolist->aliases[i] = NULL;
} }
} }

View File

@ -401,94 +401,6 @@ static void access_with_adjusted_size(hwaddr addr,
} }
} }
static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
unsigned width, bool write)
{
const MemoryRegionPortio *mrp;
for (mrp = mr->ops->old_portio; mrp->size; ++mrp) {
if (offset >= mrp->offset && offset < mrp->offset + mrp->len
&& width == mrp->size
&& (write ? (bool)mrp->write : (bool)mrp->read)) {
return mrp;
}
}
return NULL;
}
static void memory_region_iorange_read(IORange *iorange,
uint64_t offset,
unsigned width,
uint64_t *data)
{
MemoryRegionIORange *mrio
= container_of(iorange, MemoryRegionIORange, iorange);
MemoryRegion *mr = mrio->mr;
offset += mrio->offset;
if (mr->ops->old_portio) {
const MemoryRegionPortio *mrp = find_portio(mr, offset - mrio->offset,
width, false);
*data = ((uint64_t)1 << (width * 8)) - 1;
if (mrp) {
*data = mrp->read(mr->opaque, offset);
} else if (width == 2) {
mrp = find_portio(mr, offset - mrio->offset, 1, false);
assert(mrp);
*data = mrp->read(mr->opaque, offset) |
(mrp->read(mr->opaque, offset + 1) << 8);
}
return;
}
*data = 0;
access_with_adjusted_size(offset, data, width,
mr->ops->impl.min_access_size,
mr->ops->impl.max_access_size,
memory_region_read_accessor, mr);
}
static void memory_region_iorange_write(IORange *iorange,
uint64_t offset,
unsigned width,
uint64_t data)
{
MemoryRegionIORange *mrio
= container_of(iorange, MemoryRegionIORange, iorange);
MemoryRegion *mr = mrio->mr;
offset += mrio->offset;
if (mr->ops->old_portio) {
const MemoryRegionPortio *mrp = find_portio(mr, offset - mrio->offset,
width, true);
if (mrp) {
mrp->write(mr->opaque, offset, data);
} else if (width == 2) {
mrp = find_portio(mr, offset - mrio->offset, 1, true);
assert(mrp);
mrp->write(mr->opaque, offset, data & 0xff);
mrp->write(mr->opaque, offset + 1, data >> 8);
}
return;
}
access_with_adjusted_size(offset, &data, width,
mr->ops->impl.min_access_size,
mr->ops->impl.max_access_size,
memory_region_write_accessor, mr);
}
static void memory_region_iorange_destructor(IORange *iorange)
{
g_free(container_of(iorange, MemoryRegionIORange, iorange));
}
const IORangeOps memory_region_iorange_ops = {
.read = memory_region_iorange_read,
.write = memory_region_iorange_write,
.destructor = memory_region_iorange_destructor,
};
static AddressSpace *memory_region_to_address_space(MemoryRegion *mr) static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
{ {
AddressSpace *as; AddressSpace *as;