Patchwork [18/23] pci: add helper functions for pci config write function.

login
register
mail settings
Submitter Isaku Yamahata
Date Oct. 5, 2009, 10:06 a.m.
Message ID <1254737223-16129-19-git-send-email-yamahata@valinux.co.jp>
Download mbox | patch
Permalink /patch/34983/
State Superseded
Headers show

Comments

Isaku Yamahata - Oct. 5, 2009, 10:06 a.m.
add helper functions for pci config write function to check
if its configuration space is changed.
Those function will be used later and generic enough for specific
pci device to use.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
---
 hw/pci.h |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)

Patch

diff --git a/hw/pci.h b/hw/pci.h
index 1f402d2..0ea08b7 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -393,6 +393,65 @@  static inline uint32_t pcie_config_size(PCIDevice *d)
     return pci_is_pcie(d)? PCIE_CONFIG_SPACE_SIZE: PCI_CONFIG_SPACE_SIZE;
 }
 
+struct pci_config_update {
+    PCIDevice *d;
+    uint32_t addr;
+    uint32_t val;
+    int len;
+
+    uint8_t orig[4];
+};
+
+static inline void pci_write_config_init(struct pci_config_update *update,
+                                         PCIDevice *d,
+                                         uint32_t addr, uint32_t val, int len)
+{
+    assert(len == 1 || len == 2 || len == 4);
+
+    update->d = d;
+    update->addr = addr;
+    update->val = val;
+    update->len = len;
+    memcpy(update->orig, &d->config[addr], len);
+}
+
+static inline void pci_write_config_update(struct pci_config_update *update)
+{
+    PCIDevice *d = update->d;
+    uint32_t addr = update->addr;
+    uint32_t val = update->val;
+    uint32_t config_size = pcie_config_size(d);
+    int i;
+
+    for(i = 0; i < update->len && addr < config_size; val >>= 8, ++i, ++addr) {
+        uint8_t wmask = d->wmask[addr];
+        d->config[addr] = (d->config[addr] & ~wmask) | (val & wmask);
+    }
+}
+
+static inline int pci_config_changed(const struct pci_config_update *update,
+                                     uint32_t base, uint32_t end)
+{
+    uint32_t low = MAX(update->addr, base);
+    uint32_t high = MIN(update->addr + update->len, end);
+
+    /* Check if [addr, addr + len) intersects [base, end).
+       The intersection is [low, high), so see if it's empty or not */
+    if (low >= high)
+        return 0;
+
+    return memcmp(update->orig + (low - update->addr),
+                  update->d->config + low,
+                  high - low);
+}
+
+/* for convinience not to type symbol constant twice */
+static inline int pci_config_changed_with_size(
+    const struct pci_config_update *update, uint32_t base, uint32_t size)
+{
+    return pci_config_changed(update, base, base + size);
+}
+
 /* lsi53c895a.c */
 #define LSI_MAX_DEVS 7