Skip to contentSkip to conversation

Align PCI capabilities in pci_find_space

PATCH Superseded [email protected] 3 participants 10 messages Sep 26, 2012

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Align PCI capabilities in pci_find_space

From: Matt Renzelmann <[email protected]>

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

Thanks to Alex Williamson for continuing feedback.

---

In this patch, I've revised the pci_find_space function as suggested (more-or-less). I searched for calls to pci_add_capability, and at this time, most rely only on capabilities that fit in the PCI config space. More importantly, almost all specify the capability offset instead of relying on pci_find_space, so this change does not impact any calls that specify an offset manually. However, it's important to double-check that there are no calls from PCI-E virtual devices to pci_add_capability that both:

(a) relied on pci_find_space to find them space

(b) needed the PCI-E extended config space searched in addition to the PCI space

as these would break with this patch. Here is the list of files that refer to pcie_cap_init:

./hw/pcie.c
./hw/pcie.h
./hw/ioh3420.c
./hw/usb/hcd-xhci.c
./hw/xio3130_upstream.c
./hw/xio3130_downstream.c

The goal of this search was simply to find PCI-E devices--there may be a better way. The next list contain calls to pci_add_capability:

./hw/pci_bridge.c
./hw/shpc.c
./hw/pcie.c
./hw/kvm/pci-assign.c
./hw/msi.c
./hw/pci.c
./hw/ide/ich.c
./hw/pci.h
./hw/eepro100.c
./hw/msix.c
./hw/slotid_cap.c

 hw/pci.c |   28 +++++++++++++++++++++-------
 1 files changed, 21 insertions(+), 7 deletions(-)
hw/pci.c +21 -7
index f855cf3..2217dda 100644
@@ -1626,16 +1626,30 @@ 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, uint8_t size, bool include_pcie)
 {
-    int config_size = pci_config_size(pdev);
+    int config_size;
     int offset = PCI_CONFIG_HEADER_SIZE;
     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[PCI_CONFIG_HEADER_SIZE];
+
+    if (include_pcie) {
+        assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
+        config_size = PCIE_CONFIG_SPACE_SIZE;
+    } else {
+        config_size = PCI_CONFIG_SPACE_SIZE;
+    }
+
+    /* This approach ensures the capability is dword-aligned, as
+       required by the PCI specification */
+    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; i += 4, dword_used++) {
+        if (*dword_used) {
+            offset = i + 4;
+        } else if (i - offset + 4 >= size) {
             return offset;
+        }
+    }
+
     return 0;
 }
 
@@ -1826,7 +1840,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_space(pdev, size, false);
         if (!offset) {
             return -ENOSPC;
         }
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 76 lines · On Wed, 2012-09-26 at 10:30 -0500, [email protected] wrote:

From: Matt Renzelmann <[email protected]>

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

Thanks to Alex Williamson for continuing feedback.

---

In this patch, I've revised the pci_find_space function as suggested (more-or-less). I searched for calls to pci_add_capability, and at this time, most rely only on capabilities that fit in the PCI config space. More importantly, almost all specify the capability offset instead of relying on pci_find_space, so this change does not impact any calls that specify an offset manually. However, it's important to double-check that there are no calls from PCI-E virtual devices to pci_add_capability that both:

(a) relied on pci_find_space to find them space

(b) needed the PCI-E extended config space searched in addition to the PCI space

as these would break with this patch. Here is the list of files that refer to pcie_cap_init:

./hw/pcie.c
./hw/pcie.h
./hw/ioh3420.c
./hw/usb/hcd-xhci.c
./hw/xio3130_upstream.c
./hw/xio3130_downstream.c

The goal of this search was simply to find PCI-E devices--there may be a better way. The next list contain calls to pci_add_capability:

./hw/pci_bridge.c
./hw/shpc.c
./hw/pcie.c
./hw/kvm/pci-assign.c
./hw/msi.c
./hw/pci.c
./hw/ide/ich.c
./hw/pci.h
./hw/eepro100.c
./hw/msix.c
./hw/slotid_cap.c

 hw/pci.c |   28 +++++++++++++++++++++-------
 1 files changed, 21 insertions(+), 7 deletions(-)
hw/pci.c +4 -6
index f855cf3..2217dda 100644
@@ -1626,16 +1626,30 @@ 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, uint8_t size, bool include_pcie)
 {
-    int config_size = pci_config_size(pdev);
+    int config_size;
     int offset = PCI_CONFIG_HEADER_SIZE;
     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[PCI_CONFIG_HEADER_SIZE];
+

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

Quoted 10 lines
hw/pci.c +10 -0
+    if (include_pcie) {
+        assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
+        config_size = PCIE_CONFIG_SPACE_SIZE;
+    } else {
+        config_size = PCI_CONFIG_SPACE_SIZE;
+    }
+
+    /* This approach ensures the capability is dword-aligned, as
+       required by the PCI specification */
+    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; i += 4, dword_used++) {

I don't believe there's ever a case where a driver would want space and not care if it's in standard or extended config space. They'll want one or the other. So we'd be searching two distinct ranges. Thanks,

Alex

Quoted 19 lines
hw/pci.c +7 -1
+        if (*dword_used) {
+            offset = i + 4;
+        } else if (i - offset + 4 >= size) {
             return offset;
+        }
+    }
+
     return 0;
 }
 
@@ -1826,7 +1840,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_space(pdev, size, false);
         if (!offset) {
             return -ENOSPC;
         }
#2 lore raw
Quoted 38 lines

hw/pci.c PCIDevice *pci_create_simple(PCIBus *bus, int devfn,

Quoted 9 lines
 hw/pci.c |   28 +++++++++++++++++++++-------
 1 files changed, 21 insertions(+), 7 deletions(-)
hw/pci.c +0 -0
index f855cf3..2217dda 100644
@@ -1626,16 +1626,30 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn,

const char *name)

Quoted 16 lines
hw/pci.c +4 -6
     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, uint8_t size, bool include_pcie)
 {
-    int config_size = pci_config_size(pdev);
+    int config_size;
     int offset = PCI_CONFIG_HEADER_SIZE;
     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[PCI_CONFIG_HEADER_SIZE];
+

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

Quoted 10 lines
hw/pci.c +10 -0
+    if (include_pcie) {
+        assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
+        config_size = PCIE_CONFIG_SPACE_SIZE;
+    } else {
+        config_size = PCI_CONFIG_SPACE_SIZE;
+    }
+
+    /* This approach ensures the capability is dword-aligned, as
+       required by the PCI specification */
+    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; i += 4, dword_used++)

{

Quoted 5 lines

I don't believe there's ever a case where a driver would want space and not care if it's in standard or extended config space. They'll want one or the other. So we'd be searching two distinct ranges. Thanks,

Alex

Ah, that makes sense, so would something like this work? I can re-send as a patch once we've got it.

static int pci_find_space(PCIDevice *pdev, uint8_t size, bool pcie_space)

{
    int config_base;
    int config_size;
    int offset = PCI_CONFIG_HEADER_SIZE;
    int i;
    uint32_t *dword_used = &pdev->used[PCI_CONFIG_HEADER_SIZE];

    if (pcie_space) {
        assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
        config_base = PCI_CONFIG_SPACE_SIZE;
        config_size = PCIE_CONFIG_SPACE_SIZE;
    } else {        
        config_base = PCI_CONFIG_HEADER_SIZE;
        config_size = PCI_CONFIG_SPACE_SIZE;
    }

    /* This approach ensures the capability is dword-aligned, as                                                        
       required by the PCI specification */
    for (i = config_base; i < config_size; i += 4, dword_used++) {                       
        if (*dword_used)
            offset = i + 4;
        else if (i - offset + 4 >= size)
            return offset;
    }        

    return 0;
}

Thanks for all your help with this,
Matt

Quoted 23 lines

hw/pci.c int pci_add_capability(PCIDevice *pdev, uint8_t

Quoted 12 lines
hw/pci.c +6 -0
+        if (*dword_used) {
+            offset = i + 4;
+        } else if (i - offset + 4 >= size) {
             return offset;
+        }
+    }
+
     return 0;
 }
 
@@ -1826,7 +1840,7 @@ int pci_add_capability(PCIDevice *pdev, uint8_t

cap_id,

Quoted 8 lines
hw/pci.c +1 -1
     int i, overlapping_cap;
 
     if (!offset) {
-        offset = pci_find_space(pdev, size);
+        offset = pci_find_space(pdev, size, false);
         if (!offset) {
             return -ENOSPC;
         }
#3 lore raw
Quoted 46 lines · Matt Renzelmann
Quoted 38 lines

hw/pci.c PCIDevice *pci_create_simple(PCIBus *bus, int devfn,

Quoted 9 lines
 hw/pci.c |   28 +++++++++++++++++++++-------
 1 files changed, 21 insertions(+), 7 deletions(-)
hw/pci.c +0 -0
index f855cf3..2217dda 100644
@@ -1626,16 +1626,30 @@ PCIDevice *pci_create_simple(PCIBus *bus, int devfn,

const char *name)

Quoted 16 lines
hw/pci.c +4 -6
     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, uint8_t size, bool include_pcie)
 {
-    int config_size = pci_config_size(pdev);
+    int config_size;
     int offset = PCI_CONFIG_HEADER_SIZE;
     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[PCI_CONFIG_HEADER_SIZE];
+

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

Quoted 10 lines
hw/pci.c +10 -0
+    if (include_pcie) {
+        assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
+        config_size = PCIE_CONFIG_SPACE_SIZE;
+    } else {
+        config_size = PCI_CONFIG_SPACE_SIZE;
+    }
+
+    /* This approach ensures the capability is dword-aligned, as
+       required by the PCI specification */
+    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; i += 4, dword_used++)

{

Quoted 5 lines

I don't believe there's ever a case where a driver would want space and not care if it's in standard or extended config space. They'll want one or the other. So we'd be searching two distinct ranges. Thanks,

Alex

Ah, that makes sense, so would something like this work? I can re-send as a patch once we've got it.

Quoted 7 lines

static int pci_find_space(PCIDevice *pdev, uint8_t size, bool pcie_space)

{
    int config_base;
    int config_size;
    int offset = PCI_CONFIG_HEADER_SIZE;
    int i;
    uint32_t *dword_used = &pdev->used[PCI_CONFIG_HEADER_SIZE];

This needs to change too.

Is there a different alignment requirement for pcie? Seems like you might be better off creating some helper like:

pci_find_space(PCIDevice *pdev, unsigned start, unsigned size, unsigned align)

Then a pci_find_legacy_space and pci_find_express_space could both call into it. Thanks,

Alex

Quoted 50 lines
    if (pcie_space) {
        assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
        config_base = PCI_CONFIG_SPACE_SIZE;
        config_size = PCIE_CONFIG_SPACE_SIZE;
    } else {        
        config_base = PCI_CONFIG_HEADER_SIZE;
        config_size = PCI_CONFIG_SPACE_SIZE;
    }

    /* This approach ensures the capability is dword-aligned, as                                                        
       required by the PCI specification */
    for (i = config_base; i < config_size; i += 4, dword_used++) {                       
        if (*dword_used)
            offset = i + 4;
        else if (i - offset + 4 >= size)
            return offset;
    }        

    return 0;
}

Thanks for all your help with this,
Matt

Quoted 23 lines

hw/pci.c int pci_add_capability(PCIDevice *pdev, uint8_t

Quoted 12 lines
hw/pci.c +6 -0
+        if (*dword_used) {
+            offset = i + 4;
+        } else if (i - offset + 4 >= size) {
             return offset;
+        }
+    }
+
     return 0;
 }
 
@@ -1826,7 +1840,7 @@ int pci_add_capability(PCIDevice *pdev, uint8_t

cap_id,

Quoted 8 lines
hw/pci.c +1 -1
     int i, overlapping_cap;
 
     if (!offset) {
-        offset = pci_find_space(pdev, size);
+        offset = pci_find_space(pdev, size, false);
         if (!offset) {
             return -ENOSPC;
         }
#4 lore raw
Quoted 8 lines
Quoted 7 lines

static int pci_find_space(PCIDevice *pdev, uint8_t size, bool pcie_space)

{
    int config_base;
    int config_size;
    int offset = PCI_CONFIG_HEADER_SIZE;
    int i;
    uint32_t *dword_used = &pdev->used[PCI_CONFIG_HEADER_SIZE];
Quoted 4 lines

This needs to change too.

Is there a different alignment requirement for pcie? Seems like you might be better off creating some helper like:

I found a copy of the PCI-E 1.0 specification -- I don't have any later copy available -- and it contains this text:

============
7.9. PCI Express Extended Capabilities

PCI Express Extended Capability registers are located in device configuration space at offsets 256 or greater ...

Each capability structure must be DWORD aligned.

...

7.9.3. PCI Express Enhanced Capability Header

Next Capability Offset – This field contains the offset to the next PCI Express capability structure or 000h if no other items exist in the linked list of capabilities.

...

The bottom two bits of this offset are reserved and must be implemented as 00b although software must mask them to allow for future uses of these bits.
============

so, I think it's reasonable to align everything to 4-bytes all the time. Here's another draft:

static int pci_find_space(PCIDevice *pdev, uint32_t start, uint32_t size)

{
    int offset = start;
    int i;
    uint32_t *dword_used = &pdev->used[start];

    /* This approach ensures the capability is dword-aligned, as                                                        
       required by the PCI and PCI-E specifications */
    for (i = start; i < size; 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);
}
static int pci_find_express_space(PCIDevice *pdev, uint16_t size) {
    assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
    return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE, PCIE_CONFIG_SPACE_SIZE);
}
#5 lore raw
Quoted 43 lines · Matt Renzelmann
Quoted 8 lines
Quoted 7 lines

static int pci_find_space(PCIDevice *pdev, uint8_t size, bool pcie_space)

{
    int config_base;
    int config_size;
    int offset = PCI_CONFIG_HEADER_SIZE;
    int i;
    uint32_t *dword_used = &pdev->used[PCI_CONFIG_HEADER_SIZE];
Quoted 4 lines

This needs to change too.

Is there a different alignment requirement for pcie? Seems like you might be better off creating some helper like:

I found a copy of the PCI-E 1.0 specification -- I don't have any later copy available -- and it contains this text:

============
7.9. PCI Express Extended Capabilities

PCI Express Extended Capability registers are located in device configuration space at offsets 256 or greater ...

Each capability structure must be DWORD aligned.

...

7.9.3. PCI Express Enhanced Capability Header

Next Capability Offset – This field contains the offset to the next PCI Express capability structure or 000h if no other items exist in the linked list of capabilities.

...

The bottom two bits of this offset are reserved and must be implemented as 00b although software must mask them to allow for future uses of these bits.
============

so, I think it's reasonable to align everything to 4-bytes all the time. Here's another draft:

static int pci_find_space(PCIDevice *pdev, uint32_t start, uint32_t size)

Quoted 13 lines
{
    int offset = start;
    int i;
    uint32_t *dword_used = &pdev->used[start];

    /* This approach ensures the capability is dword-aligned, as                                                        
       required by the PCI and PCI-E specifications */
    for (i = start; i < size; i += 4, dword_used++) {
        if (*dword_used)
            offset = i + 4;
        else if (i - offset + 4 >= size)
            return offset;
    }

Mismatched uses of "size" here. We need both the end of the range to search and the size of the sub-range we're looking for. Maybe start, end, and size. Thanks,

Alex

Quoted 14 lines
    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);
}
static int pci_find_express_space(PCIDevice *pdev, uint16_t size) {
    assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
    return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE, PCIE_CONFIG_SPACE_SIZE);
}
#6 lore raw
Quoted 47 lines · Alex Williamson
Quoted 46 lines · Matt Renzelmann
Quoted 11 lines
Quoted 7 lines

static int pci_find_space(PCIDevice *pdev, uint8_t size, bool pcie_space)

{
     int config_base;
     int config_size;
     int offset = PCI_CONFIG_HEADER_SIZE;
     int i;
     uint32_t *dword_used = &pdev->used[PCI_CONFIG_HEADER_SIZE];

This needs to change too.

Is there a different alignment requirement for pcie? Seems like you might be better off creating some helper like:

I found a copy of the PCI-E 1.0 specification -- I don't have any later copy available -- and it contains this text:

============
7.9. PCI Express Extended Capabilities

PCI Express Extended Capability registers are located in device configuration space at offsets 256 or greater ...

Each capability structure must be DWORD aligned.

...

7.9.3. PCI Express Enhanced Capability Header

Next Capability Offset – This field contains the offset to the next PCI Express capability structure or 000h if no other items exist in the linked list of capabilities.

...

The bottom two bits of this offset are reserved and must be implemented as 00b although software must mask them to allow for future uses of these bits.
============

so, I think it's reasonable to align everything to 4-bytes all the time. Here's another draft:

static int pci_find_space(PCIDevice *pdev, uint32_t start, uint32_t size)

{
     int offset = start;
     int i;
     uint32_t *dword_used = &pdev->used[start];
Quoted 4 lines
Quoted 4 lines
     /* This approach ensures the capability is dword-aligned, as
        required by the PCI and PCI-E specifications */
     for (i = start; i < size; i += 4, dword_used++) {
         if (*dword_used)

I would think that dword_used needs to be based on i not start.

Quoted 4 lines
Quoted 4 lines
             offset = i + 4;
         else if (i - offset + 4 >= size)
             return offset;
     }
Quoted 22 lines

Mismatched uses of "size" here. We need both the end of the range to search and the size of the sub-range we're looking for. Maybe start, end, and size. Thanks,

Alex

Quoted 13 lines
     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);
}
static int pci_find_express_space(PCIDevice *pdev, uint16_t size) {
     assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
     return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE, PCIE_CONFIG_SPACE_SIZE);
}

-Don Slutz

#7 lore raw
Quoted 48 lines · Don Slutz
Quoted 47 lines · Alex Williamson
Quoted 46 lines · Matt Renzelmann
Quoted 11 lines
Quoted 7 lines

static int pci_find_space(PCIDevice *pdev, uint8_t size, bool pcie_space)

{
     int config_base;
     int config_size;
     int offset = PCI_CONFIG_HEADER_SIZE;
     int i;
     uint32_t *dword_used = &pdev->used[PCI_CONFIG_HEADER_SIZE];

This needs to change too.

Is there a different alignment requirement for pcie? Seems like you might be better off creating some helper like:

I found a copy of the PCI-E 1.0 specification -- I don't have any later copy available -- and it contains this text:

============
7.9. PCI Express Extended Capabilities

PCI Express Extended Capability registers are located in device configuration space at offsets 256 or greater ...

Each capability structure must be DWORD aligned.

...

7.9.3. PCI Express Enhanced Capability Header

Next Capability Offset – This field contains the offset to the next PCI Express capability structure or 000h if no other items exist in the linked list of capabilities.

...

The bottom two bits of this offset are reserved and must be implemented as 00b although software must mask them to allow for future uses of these bits.
============

so, I think it's reasonable to align everything to 4-bytes all the time. Here's another draft:

static int pci_find_space(PCIDevice *pdev, uint32_t start, uint32_t size)

{
     int offset = start;
     int i;
     uint32_t *dword_used = &pdev->used[start];
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
     /* This approach ensures the capability is dword-aligned, as
        required by the PCI and PCI-E specifications */
     for (i = start; i < size; i += 4, dword_used++) {

Opps, I missed the dword_used++

Forget this comment...

Quoted 30 lines
Quoted 1 line
Quoted 1 line

if (*dword_used)

I would think that dword_used needs to be based on i not start.

Quoted 4 lines
Quoted 4 lines
             offset = i + 4;
         else if (i - offset + 4 >= size)
             return offset;
     }
Quoted 24 lines

Mismatched uses of "size" here. We need both the end of the range to search and the size of the sub-range we're looking for. Maybe start, end, and size. Thanks,

Alex

Quoted 15 lines
     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);
}

static int pci_find_express_space(PCIDevice *pdev, uint16_t size) {
assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE); return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE,
PCIE_CONFIG_SPACE_SIZE);
}

Quoted 2 lines

-Don Slutz

-Don Slutz

#8 lore raw
In reply to Alex Williamson
Quoted 5 lines

Mismatched uses of "size" here. We need both the end of the range to search and the size of the sub-range we're looking for. Maybe start, end, and size. Thanks,

Ah of course, how's this:

static int pci_find_space(PCIDevice *pdev, uint32_t start,

                          uint32_t end, uint32_t size)
{
    int offset = start;
    int i;
    uint32_t *dword_used = &pdev->used[start];

    /* 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) {
    assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);
    return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE,
                          PCIE_CONFIG_SPACE_SIZE, size);
}
#9 lore raw
Quoted 18 lines · Matt Renzelmann
Quoted 5 lines

Mismatched uses of "size" here. We need both the end of the range to search and the size of the sub-range we're looking for. Maybe start, end, and size. Thanks,

Ah of course, how's this:

static int pci_find_space(PCIDevice *pdev, uint32_t start,

                          uint32_t end, uint32_t size)
{
    int offset = start;
    int i;
    uint32_t *dword_used = &pdev->used[start];

    /* 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++) { 
Quoted 8 lines
        if (*dword_used)
            offset = i + 4;
        else if (i - offset + 4 >= size)
            return offset;
    }

    return 0;
}

Looks reasonable to me

Quoted 7 lines

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) {
    assert (pci_config_size(pdev) >= PCIE_CONFIG_SPACE_SIZE);

This could be done generically in pci_find_space

assert(pci_config_size(pdev) >= end)

You could also add an alignment check

assert(!(start & 0x3))

Thanks,

Alex

Quoted 4 lines

return pci_find_space(pdev, PCI_CONFIG_SPACE_SIZE,
PCIE_CONFIG_SPACE_SIZE, size);
}