diff options
author | Gabriel L. Somlo <somlo@cmu.edu> | 2015-11-05 09:32:51 -0500 |
---|---|---|
committer | Gerd Hoffmann <kraxel@redhat.com> | 2015-12-15 11:45:59 +0100 |
commit | 38bf20931afe761fccda6e1eb91d64c7498ed9c9 (patch) | |
tree | 5ec403d6462051ec06961da5aafeda072e8d3d36 /hw/nvram | |
parent | 66f8fd9dda312191b78d2a2ba2848bcee76127a2 (diff) |
fw_cfg: add generic non-DMA read method
Introduce fw_cfg_data_read(), a generic read method which works
on all access widths (1 through 8 bytes, inclusive), and can be
used during both IOPort and MMIO read accesses.
To maintain legibility, only fw_cfg_data_mem_read() (the MMIO
data read method) is replaced by this patch. The new method
essentially unwinds the fw_cfg_data_mem_read() + fw_cfg_read()
combo, but without unnecessarily repeating all the validity
checks performed by the latter on each byte being read.
This patch also modifies the trace_fw_cfg_read prototype to
accept a 64-bit value argument, allowing it to work properly
with the new read method, but also remain backward compatible
with existing call sites.
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Marc MarĂ <markmb@redhat.com>
Signed-off-by: Gabriel Somlo <somlo@cmu.edu>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Message-id: 1446733972-1602-6-git-send-email-somlo@cmu.edu
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Diffstat (limited to 'hw/nvram')
-rw-r--r-- | hw/nvram/fw_cfg.c | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c index 046fa749a5..8f566f61c1 100644 --- a/hw/nvram/fw_cfg.c +++ b/hw/nvram/fw_cfg.c @@ -274,6 +274,36 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key) return ret; } +static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size) +{ + FWCfgState *s = opaque; + int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); + FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL : + &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; + uint64_t value = 0; + + assert(size > 0 && size <= sizeof(value)); + if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) { + /* The least significant 'size' bytes of the return value are + * expected to contain a string preserving portion of the item + * data, padded with zeros on the right in case we run out early. + * In technical terms, we're composing the host-endian representation + * of the big endian interpretation of the fw_cfg string. + */ + do { + value = (value << 8) | e->data[s->cur_offset++]; + } while (--size && s->cur_offset < e->len); + /* If size is still not zero, we *did* run out early, so continue + * left-shifting, to add the appropriate number of padding zeros + * on the right. + */ + value <<= 8 * size; + } + + trace_fw_cfg_read(s, value); + return value; +} + static uint8_t fw_cfg_read(FWCfgState *s) { int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); @@ -291,19 +321,6 @@ static uint8_t fw_cfg_read(FWCfgState *s) return ret; } -static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr, - unsigned size) -{ - FWCfgState *s = opaque; - uint64_t value = 0; - unsigned i; - - for (i = 0; i < size; ++i) { - value = (value << 8) | fw_cfg_read(s); - } - return value; -} - static void fw_cfg_data_mem_write(void *opaque, hwaddr addr, uint64_t value, unsigned size) { @@ -485,7 +502,7 @@ static const MemoryRegionOps fw_cfg_ctl_mem_ops = { }; static const MemoryRegionOps fw_cfg_data_mem_ops = { - .read = fw_cfg_data_mem_read, + .read = fw_cfg_data_read, .write = fw_cfg_data_mem_write, .endianness = DEVICE_BIG_ENDIAN, .valid = { |