/* * QEMU System Emulator * * Copyright (c) 2003-2008 Fabrice Bellard * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /* * splitted out ioport related stuffs from vl.c. */ #include "exec/ioport.h" #include "trace.h" #include "exec/memory.h" #include "exec/address-spaces.h" /***********************************************************/ /* IO Port */ //#define DEBUG_UNUSED_IOPORT //#define DEBUG_IOPORT #ifdef DEBUG_UNUSED_IOPORT # define LOG_UNUSED_IOPORT(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) #else # define LOG_UNUSED_IOPORT(fmt, ...) do{ } while (0) #endif #ifdef DEBUG_IOPORT # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__) #else # define LOG_IOPORT(...) do { } while (0) #endif typedef struct MemoryRegionPortioList { MemoryRegion mr; void *portio_opaque; MemoryRegionPortio ports[]; } MemoryRegionPortioList; /* XXX: use a two level table to limit memory usage */ static void *ioport_opaque[MAX_IOPORTS]; static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; static IOPortDestructor *ioport_destructor_table[MAX_IOPORTS]; static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl; static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel; static uint32_t ioport_read(int index, uint32_t address) { static IOPortReadFunc * const default_func[3] = { default_ioport_readb, default_ioport_readw, default_ioport_readl }; IOPortReadFunc *func = ioport_read_table[index][address]; if (!func) func = default_func[index]; return func(ioport_opaque[address], address); } static void ioport_write(int index, uint32_t address, uint32_t data) { static IOPortWriteFunc * const default_func[3] = { default_ioport_writeb, default_ioport_writew, default_ioport_writel }; IOPortWriteFunc *func = ioport_write_table[index][address]; if (!func) func = default_func[index]; func(ioport_opaque[address], address, data); } static uint32_t default_ioport_readb(void *opaque, uint32_t address) { LOG_UNUSED_IOPORT("unused inb: port=0x%04"PRIx32"\n", address); return 0xff; } static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) { LOG_UNUSED_IOPORT("unused outb: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", address, data); } /* default is to make two byte accesses */ static uint32_t default_ioport_readw(void *opaque, uint32_t address) { uint32_t data; data = ioport_read(0, address); address = (address + 1) & IOPORTS_MASK; data |= ioport_read(0, address) << 8; return data; } static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) { ioport_write(0, address, data & 0xff); address = (address + 1) & IOPORTS_MASK; ioport_write(0, address, (data >> 8) & 0xff); } static uint32_t default_ioport_readl(void *opaque, uint32_t address) { LOG_UNUSED_IOPORT("unused inl: port=0x%04"PRIx32"\n", address); return 0xffffffff; } static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) { LOG_UNUSED_IOPORT("unused outl: port=0x%04"PRIx32" data=0x%02"PRIx32"\n", address, data); } static int ioport_bsize(int size, int *bsize) { if (size == 1) { *bsize = 0; } else if (size == 2) { *bsize = 1; } else if (size == 4) { *bsize = 2; } else { return -1; } return 0; } /* size is the word size in byte */ static int register_ioport_read(pio_addr_t start, int length, int size, IOPortReadFunc *func, void *opaque) { int i, bsize; if (ioport_bsize(size, &bsize)) { hw_error("register_ioport_read: invalid size"); return -1; } for(i = start; i < start + length; ++i) { ioport_read_table[bsize][i] = func; if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) hw_error("register_ioport_read: invalid opaque for address 0x%x", i); ioport_opaque[i] = opaque; } return 0; } /* size is the word size in byte */ static int register_ioport_write(pio_addr_t start, int length, int size, IOPortWriteFunc *func, void *opaque) { int i, bsize; if (ioport_bsize(size, &bsize)) { hw_error("register_ioport_write: invalid size"); return -1; } for(i = start; i < start + length; ++i) { ioport_write_table[bsize][i] = func; if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) hw_error("register_ioport_write: invalid opaque for address 0x%x", i); ioport_opaque[i] = opaque; } return 0; } static uint32_t ioport_readb_thunk(void *opaque, uint32_t addr) { IORange *ioport = opaque; uint64_t data; ioport->ops->read(ioport, addr - ioport->base, 1, &data); return data; } static uint32_t ioport_readw_thunk(void *opaque, uint32_t addr) { IORange *ioport = opaque; uint64_t data; ioport->ops->read(ioport, addr - ioport->base, 2, &data); return data; } static uint32_t ioport_readl_thunk(void *opaque, uint32_t addr) { IORange *ioport = opaque; uint64_t data; ioport->ops->read(ioport, addr - ioport->base, 4, &data); return data; } static void ioport_writeb_thunk(void *opaque, uint32_t addr, uint32_t data) { IORange *ioport = opaque; ioport->ops->write(ioport, addr - ioport->base, 1, data); } static void ioport_writew_thunk(void *opaque, uint32_t addr, uint32_t data) { IORange *ioport = opaque; ioport->ops->write(ioport, addr - ioport->base, 2, data); } static void ioport_writel_thunk(void *opaque, uint32_t addr, uint32_t data) { IORange *ioport = opaque; ioport->ops->write(ioport, addr - ioport->base, 4, data); } static void iorange_destructor_thunk(void *opaque) { IORange *iorange = opaque; if (iorange->ops->destructor) { iorange->ops->destructor(iorange); } } void ioport_register(IORange *ioport) { register_ioport_read(ioport->base, ioport->len, 1, ioport_readb_thunk, ioport); register_ioport_read(ioport->base, ioport->len, 2, ioport_readw_thunk, ioport); register_ioport_read(ioport->base, ioport->len, 4, ioport_readl_thunk, ioport); register_ioport_write(ioport->base, ioport->len, 1, ioport_writeb_thunk, ioport); register_ioport_write(ioport->base, ioport->len, 2, ioport_writew_thunk, ioport); register_ioport_write(ioport->base, ioport->len, 4, ioport_writel_thunk, ioport); ioport_destructor_table[ioport->base] = iorange_destructor_thunk; } void isa_unassign_ioport(pio_addr_t start, int length) { int i; if (ioport_destructor_table[start]) { ioport_destructor_table[start](ioport_opaque[start]); ioport_destructor_table[start] = NULL; } for(i = start; i < start + length; i++) { ioport_read_table[0][i] = NULL; ioport_read_table[1][i] = NULL; ioport_read_table[2][i] = NULL; ioport_write_table[0][i] = NULL; ioport_write_table[1][i] = NULL; ioport_write_table[2][i] = NULL; ioport_opaque[i] = NULL; } } /***********************************************************/ void cpu_outb(pio_addr_t addr, uint8_t val) { LOG_IOPORT("outb: %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); trace_cpu_out(addr, val); address_space_write(&address_space_io, addr, &val, 1); } 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); trace_cpu_out(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) { uint8_t buf[4]; LOG_IOPORT("outl: %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); trace_cpu_out(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 val; address_space_read(&address_space_io, addr, &val, 1); trace_cpu_in(addr, val); LOG_IOPORT("inb : %04"FMT_pioaddr" %02"PRIx8"\n", addr, val); return val; } uint16_t cpu_inw(pio_addr_t addr) { uint8_t buf[2]; uint16_t val; address_space_read(&address_space_io, addr, buf, 2); val = lduw_p(buf); trace_cpu_in(addr, val); LOG_IOPORT("inw : %04"FMT_pioaddr" %04"PRIx16"\n", addr, val); return val; } uint32_t cpu_inl(pio_addr_t addr) { uint8_t buf[4]; uint32_t val; address_space_read(&address_space_io, addr, buf, 4); val = ldl_p(buf); trace_cpu_in(addr, val); LOG_IOPORT("inl : %04"FMT_pioaddr" %08"PRIx32"\n", addr, val); return val; } void portio_list_init(PortioList *piolist, const MemoryRegionPortio *callbacks, void *opaque, const char *name) { unsigned n = 0; while (callbacks[n].size) { ++n; } piolist->ports = callbacks; piolist->nr = 0; piolist->regions = g_new0(MemoryRegion *, n); piolist->address_space = NULL; piolist->opaque = opaque; piolist->name = name; } void portio_list_destroy(PortioList *piolist) { g_free(piolist->regions); } 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, const MemoryRegionPortio *pio_init, unsigned count, unsigned start, unsigned off_low, unsigned off_high) { MemoryRegionPortioList *mrpio; unsigned i; /* Copy the sub-list and null-terminate it. */ mrpio = g_malloc0(sizeof(MemoryRegionPortioList) + sizeof(MemoryRegionPortio) * (count + 1)); 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. */ for (i = 0; i < count; ++i) { mrpio->ports[i].offset -= off_low; mrpio->ports[i].base = start + off_low; } /* * Use an alias so that the callback is called with an absolute address, * rather than an offset relative to to start + off_low. */ memory_region_init_io(&mrpio->mr, &portio_ops, mrpio, piolist->name, off_high - off_low); memory_region_add_subregion(piolist->address_space, start + off_low, &mrpio->mr); piolist->regions[piolist->nr] = &mrpio->mr; ++piolist->nr; } void portio_list_add(PortioList *piolist, MemoryRegion *address_space, uint32_t start) { const MemoryRegionPortio *pio, *pio_start = piolist->ports; unsigned int off_low, off_high, off_last, count; piolist->address_space = address_space; /* Handle the first entry specially. */ off_last = off_low = pio_start->offset; off_high = off_low + pio_start->len; count = 1; for (pio = pio_start + 1; pio->size != 0; pio++, count++) { /* All entries must be sorted by offset. */ assert(pio->offset >= off_last); off_last = pio->offset; /* If we see a hole, break the region. */ if (off_last > off_high) { portio_list_add_1(piolist, pio_start, count, start, off_low, off_high); /* ... and start collecting anew. */ pio_start = pio; off_low = off_last; off_high = off_low + pio->len; count = 0; } else if (off_last + pio->len > off_high) { off_high = off_last + pio->len; } } /* There will always be an open sub-list. */ portio_list_add_1(piolist, pio_start, count, start, off_low, off_high); } void portio_list_del(PortioList *piolist) { MemoryRegionPortioList *mrpio; unsigned i; for (i = 0; i < piolist->nr; ++i) { mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr); memory_region_del_subregion(piolist->address_space, &mrpio->mr); memory_region_destroy(&mrpio->mr); g_free(mrpio); piolist->regions[i] = NULL; } }