Skip to contentSkip to conversation

Align PCI capabilities in pci_find_space

PATCH Superseded Matt Renzelmann 3 participants 3 messages Sep 26, 2012 – Sep 29, 2012

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 Align PCI capabilities in pci_find_space

The current implementation of pci_find_space does not correctly align PCI capabilities in the PCI configuration space. It also does not support PCI-Express devices. This patch fixes these issues.

Thanks to Alex Williamson for feedback.

---

This version adds the assertions to pci_find_space and removes the assertion from pci_find_express_space.

 hw/pci.c |   35 +++++++++++++++++++++++++++--------
 1 files changed, 27 insertions(+), 8 deletions(-)
hw/pci.c +27 -8
index f855cf3..7050596 100644
@@ -1626,19 +1626,38 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
     return pci_create_simple_multifunction(bus, devfn, false, name);
 }
 
-static int pci_find_space(PCIDevice *pdev, uint8_t size)
+static int pci_find_space(PCIDevice *pdev, uint32_t start,
+                          uint32_t end, uint32_t size)
 {
-    int config_size = pci_config_size(pdev);
-    int offset = PCI_CONFIG_HEADER_SIZE;
+    int offset = start;
     int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
-        if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+    uint32_t *dword_used = &pdev->used[start];
+
+    assert(pci_config_size(pdev) >= end);
+    assert(!(start & 0x3));
+
+    /* This approach ensures the capability is dword-aligned, as
+       required by the PCI and PCI-E specifications */
+    for (i = start; i < end; i += 4, dword_used++) {
+        if (*dword_used)
+            offset = i + 4;
+        else if (i - offset + 4 >= size)
             return offset;
+    }
+
     return 0;
 }
 
+static int pci_find_legacy_space(PCIDevice *pdev, uint8_t size) {
+    return pci_find_space(pdev, PCI_CONFIG_HEADER_SIZE,
+                          PCI_CONFIG_SPACE_SIZE, size);
+}
+
+static int pci_find_express_space(PCIDevice *pdev, uint16_t size) {
+    return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE,
+                          PCIE_CONFIG_SPACE_SIZE, size);
+}
+
 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
                                         uint8_t *prev_p)
 {
@@ -1826,7 +1845,7 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
     int i, overlapping_cap;
 
     if (!offset) {
-        offset = pci_find_space(pdev, size);
+        offset = pci_find_legacy_space(pdev, size);
         if (!offset) {
             return -ENOSPC;
         }
Signature · 1 line
1.7.5.4
Acked-by on v4 1/1
#1 lore raw
Quoted 6 lines · Matt Renzelmann

The current implementation of pci_find_space does not correctly align PCI capabilities in the PCI configuration space. It also does not support PCI-Express devices. This patch fixes these issues.

Thanks to Alex Williamson for feedback.

Quoted 8 lines

---

This version adds the assertions to pci_find_space and removes the assertion from pci_find_express_space.

 hw/pci.c |   35 +++++++++++++++++++++++++++--------
 1 files changed, 27 insertions(+), 8 deletions(-)
Quoted 59 lines
hw/pci.c +27 -8
index f855cf3..7050596 100644
@@ -1626,19 +1626,38 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
     return pci_create_simple_multifunction(bus, devfn, false, name);
 }
 
-static int pci_find_space(PCIDevice *pdev, uint8_t size)
+static int pci_find_space(PCIDevice *pdev, uint32_t start,
+                          uint32_t end, uint32_t size)
 {
-    int config_size = pci_config_size(pdev);
-    int offset = PCI_CONFIG_HEADER_SIZE;
+    int offset = start;
     int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
-        if (pdev->used[i])
-            offset = i + 1;
-        else if (i - offset + 1 == size)
+    uint32_t *dword_used = &pdev->used[start];
+
+    assert(pci_config_size(pdev) >= end);
+    assert(!(start & 0x3));
+
+    /* This approach ensures the capability is dword-aligned, as
+       required by the PCI and PCI-E specifications */
+    for (i = start; i < end; i += 4, dword_used++) {
+        if (*dword_used)
+            offset = i + 4;
+        else if (i - offset + 4 >= size)
             return offset;
+    }
+
     return 0;
 }
 
+static int pci_find_legacy_space(PCIDevice *pdev, uint8_t size) {
+    return pci_find_space(pdev, PCI_CONFIG_HEADER_SIZE,
+                          PCI_CONFIG_SPACE_SIZE, size);
+}
+
+static int pci_find_express_space(PCIDevice *pdev, uint16_t size) {
+    return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE,
+                          PCIE_CONFIG_SPACE_SIZE, size);
+}
+
 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
                                         uint8_t *prev_p)
 {
@@ -1826,7 +1845,7 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
     int i, overlapping_cap;
 
     if (!offset) {
-        offset = pci_find_space(pdev, size);
+        offset = pci_find_legacy_space(pdev, size);
         if (!offset) {
             return -ENOSPC;
         }
#2 lore raw
In reply to Matt Renzelmann
Quoted 34 lines · Matt Renzelmann

The current implementation of pci_find_space does not correctly align PCI capabilities in the PCI configuration space. It also does not support PCI-Express devices. This patch fixes these issues.

Thanks to Alex Williamson for feedback.

---

This version adds the assertions to pci_find_space and removes the assertion from pci_find_express_space.

 hw/pci.c |   35 +++++++++++++++++++++++++++--------
 1 files changed, 27 insertions(+), 8 deletions(-)
hw/pci.c +3 -6
index f855cf3..7050596 100644
@@ -1626,19 +1626,38 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
     return pci_create_simple_multifunction(bus, devfn, false, name);
 }
 
-static int pci_find_space(PCIDevice *pdev, uint8_t size)
+static int pci_find_space(PCIDevice *pdev, uint32_t start,
+                          uint32_t end, uint32_t size)
 {
-    int config_size = pci_config_size(pdev);
-    int offset = PCI_CONFIG_HEADER_SIZE;
+    int offset = start;
     int i;
-    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
-        if (pdev->used[i])
-            offset = i + 1;

hw/pci.c PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) · line 1642

Quoted 10 lines
hw/pci.c +9 -1
-        else if (i - offset + 1 == size)
+    uint32_t *dword_used = &pdev->used[start];
+
+    assert(pci_config_size(pdev) >= end);
+    assert(!(start & 0x3));
+
+    /* This approach ensures the capability is dword-aligned, as
+       required by the PCI and PCI-E specifications */
+    for (i = start; i < end; i += 4, dword_used++) {
+        if (*dword_used)

Please add braces.

hw/pci.c PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) · line 1644

Quoted 2 lines
hw/pci.c +2 -0
+            offset = i + 4;
+        else if (i - offset + 4 >= size)

Also here to the 'if' part.

Quoted 32 lines
hw/pci.c +13 -2
             return offset;
+    }
+
     return 0;
 }
 
+static int pci_find_legacy_space(PCIDevice *pdev, uint8_t size) {
+    return pci_find_space(pdev, PCI_CONFIG_HEADER_SIZE,
+                          PCI_CONFIG_SPACE_SIZE, size);
+}
+
+static int pci_find_express_space(PCIDevice *pdev, uint16_t size) {
+    return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE,
+                          PCIE_CONFIG_SPACE_SIZE, size);
+}
+
 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
                                         uint8_t *prev_p)
 {
@@ -1826,7 +1845,7 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
     int i, overlapping_cap;
 
     if (!offset) {
-        offset = pci_find_space(pdev, size);
+        offset = pci_find_legacy_space(pdev, size);
         if (!offset) {
             return -ENOSPC;
         }
--

1.7.5.4