Remove code duplication in pci_host_template.h by using cpp trick. Based on Alexander Graf patch.
Cc: Alexander Graf <ag...@suse.de> Signed-off-by: Isaku Yamahata <yamah...@valinux.co.jp> --- hw/pci_host.c | 4 +- hw/pci_host_template.h | 82 +++++++++---------------------------------- hw/pci_host_template_all.h | 23 ++++++++++++ 3 files changed, 42 insertions(+), 67 deletions(-) create mode 100644 hw/pci_host_template_all.h diff --git a/hw/pci_host.c b/hw/pci_host.c index 6289ead..307f7d4 100644 --- a/hw/pci_host.c +++ b/hw/pci_host.c @@ -190,7 +190,7 @@ void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s) #define PCI_ADDR_T target_phys_addr_t #define PCI_HOST_SUFFIX _mmio -#include "pci_host_template.h" +#include "pci_host_template_all.h" static CPUWriteMemoryFunc * const pci_host_data_write_mmio[] = { pci_host_data_writeb_mmio, @@ -217,7 +217,7 @@ int pci_host_data_register_mmio(PCIHostState *s) #define PCI_ADDR_T uint32_t #define PCI_HOST_SUFFIX _ioport -#include "pci_host_template.h" +#include "pci_host_template_all.h" void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s) { diff --git a/hw/pci_host_template.h b/hw/pci_host_template.h index 11e6c88..2c508bf 100644 --- a/hw/pci_host_template.h +++ b/hw/pci_host_template.h @@ -25,85 +25,37 @@ /* Worker routines for a PCI host controller that uses an {address,data} register pair to access PCI configuration space. */ -static void glue(pci_host_data_writeb, PCI_HOST_SUFFIX)( +static void glue(glue(pci_host_data_write, PCI_HOST_BWL), PCI_HOST_SUFFIX)( void* opaque, PCI_ADDR_T addr, uint32_t val) { PCIHostState *s = opaque; - PCI_DPRINTF("writeb addr " TARGET_FMT_plx " val %x\n", - (target_phys_addr_t)addr, val); - if (s->config_reg & (1u << 31)) - pci_data_write(s->bus, s->config_reg | (addr & 3), val, 1); -} - -static void glue(pci_host_data_writew, PCI_HOST_SUFFIX)( - void* opaque, PCI_ADDR_T addr, uint32_t val) -{ - PCIHostState *s = opaque; -#ifdef TARGET_WORDS_BIGENDIAN - val = bswap16(val); -#endif - PCI_DPRINTF("writew addr " TARGET_FMT_plx " val %x\n", - (target_phys_addr_t)addr, val); - if (s->config_reg & (1u << 31)) - pci_data_write(s->bus, s->config_reg | (addr & 3), val, 2); -} - -static void glue(pci_host_data_writel, PCI_HOST_SUFFIX)( - void* opaque, PCI_ADDR_T addr, uint32_t val) -{ - PCIHostState *s = opaque; -#ifdef TARGET_WORDS_BIGENDIAN - val = bswap32(val); +#if defined(TARGET_WORDS_BIGENDIAN) && (PCI_HOST_BITS > 8) + val = glue(bswap, PCI_HOST_BITS)(val); #endif - PCI_DPRINTF("writel addr " TARGET_FMT_plx " val %x\n", - (target_phys_addr_t)addr, val); + PCI_DPRINTF("%s: addr " TARGET_FMT_plx " val %x\n", + __func__, (target_phys_addr_t)addr, val); if (s->config_reg & (1u << 31)) - pci_data_write(s->bus, s->config_reg, val, 4); + pci_data_write(s->bus, s->config_reg | (addr & 3), val, + sizeof(glue(glue(uint, PCI_HOST_BITS), _t))); } -static uint32_t glue(pci_host_data_readb, PCI_HOST_SUFFIX)( +static uint32_t glue(glue(pci_host_data_read, PCI_HOST_BWL), PCI_HOST_SUFFIX)( void* opaque, PCI_ADDR_T addr) { PCIHostState *s = opaque; uint32_t val; - if (!(s->config_reg & (1 << 31))) - return 0xff; - val = pci_data_read(s->bus, s->config_reg | (addr & 3), 1); - PCI_DPRINTF("readb addr " TARGET_FMT_plx " val %x\n", - (target_phys_addr_t)addr, val); - return val; -} + if (!(s->config_reg & (1 << 31))) { + return ( 1ULL << PCI_HOST_BITS ) - 1; + } -static uint32_t glue(pci_host_data_readw, PCI_HOST_SUFFIX)( - void* opaque, PCI_ADDR_T addr) -{ - PCIHostState *s = opaque; - uint32_t val; - if (!(s->config_reg & (1 << 31))) - return 0xffff; - val = pci_data_read(s->bus, s->config_reg | (addr & 3), 2); - PCI_DPRINTF("readw addr " TARGET_FMT_plx " val %x\n", - (target_phys_addr_t)addr, val); -#ifdef TARGET_WORDS_BIGENDIAN - val = bswap16(val); -#endif - return val; -} - -static uint32_t glue(pci_host_data_readl, PCI_HOST_SUFFIX)( - void* opaque, PCI_ADDR_T addr) -{ - PCIHostState *s = opaque; - uint32_t val; - if (!(s->config_reg & (1 << 31))) - return 0xffffffff; - val = pci_data_read(s->bus, s->config_reg | (addr & 3), 4); - PCI_DPRINTF("readl addr " TARGET_FMT_plx " val %x\n", - (target_phys_addr_t)addr, val); -#ifdef TARGET_WORDS_BIGENDIAN - val = bswap32(val); + val = pci_data_read(s->bus, s->config_reg | (addr & 3), + sizeof(glue(glue(uint, PCI_HOST_BITS), _t))); + PCI_DPRINTF("%s addr " TARGET_FMT_plx " val %x\n", + __func__, (target_phys_addr_t)addr, val); +#if defined(TARGET_WORDS_BIGENDIAN) && (PCI_HOST_BITS > 8) + val = glue(bswap, PCI_HOST_BITS)(val); #endif return val; } diff --git a/hw/pci_host_template_all.h b/hw/pci_host_template_all.h new file mode 100644 index 0000000..74b3e84 --- /dev/null +++ b/hw/pci_host_template_all.h @@ -0,0 +1,23 @@ +#define PCI_HOST_BWL b +#define PCI_HOST_BITS 8 + +#include "pci_host_template.h" + +#undef PCI_HOST_BWL +#undef PCI_HOST_BITS + +#define PCI_HOST_BWL w +#define PCI_HOST_BITS 16 + +#include "pci_host_template.h" + +#undef PCI_HOST_BWL +#undef PCI_HOST_BITS + +#define PCI_HOST_BWL l +#define PCI_HOST_BITS 32 + +#include "pci_host_template.h" + +#undef PCI_HOST_BWL +#undef PCI_HOST_BITS -- 1.6.5.4