Skip to contentSkip to conversation

fix the pci device malfunction if a wrong bus address is assigned by firmware

PATCH Applied Kevin Hao 3 participants 8 messages May 25, 2013 – May 26, 2013 PCI

lore
Share & tools
lore

Kevin Hao

author
#0 lore raw

v2 cover fix the pci device malfunction if a wrong bus address is assigned by firmware

v2:
* Use the method suggested by Bjorn to check if we have a wrong bus address set in the PCI BAR register.
* Split the "move the pcibios_bus_to_resource() call out of the if/else wrap" change into a new patch.
* A minor new patch is introduced to add 0x prefix for the PCI BAR register position.

v1:
On a fsl p2020rdb-pc board, the onboard pci device is assigned the bus address 0xa0000000 by firmware. But the kernel allocate the cpu address 0xa0000000 ~ 0xbfffffff to this PCIe controller and use 0xe0000000 ~ 0xdfffffff as its bus address. In this case the kernel would think that the bus address in this pci device is correct and leave it unchanged. Then causes this device not work.

Even though this looks like a mismatch of the address space between firmware and kernel. I think we should detect this kind of error and fix it automatically if we don't want just to probe only (PCI_PROBE_ONLY is not set).

---
Kevin Hao (3):
PCI: add 0x prefix when printing the BAR registers position in

    __pci_read_base
  PCI: converge the unnecessary sprinkling of pcibios_bus_to_resource in
        __pci_read_base

PCI: unset the resource if a wrong bus address is assigned by firmware

 drivers/pci/probe.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)
Signature · 1 line
1.8.1.4
author
#1 lore raw

PATCH v2 1/3 PCI: add 0x prefix when printing the BAR registers position in __pci_read_base

We print the BAR registers position in hexadecimal format, so it is more readable if 0x prefix is added. Also fix the following checkpatch warning:
WARNING: Prefer dev_dbg(... to dev_printk(KERN_DEBUG, ...

---
v2: A new patch introduced in v2.

 drivers/pci/probe.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +2 -2
index 70f10fa..2505d5e 100644
@@ -278,9 +278,9 @@ out:
 		pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
 
 	if (bar_too_big)
-		dev_err(&dev->dev, "reg %x: can't handle 64-bit BAR\n", pos);
+		dev_err(&dev->dev, "reg 0x%x: can't handle 64-bit BAR\n", pos);
 	if (res->flags && !bar_disabled)
-		dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos, res);
+		dev_dbg(&dev->dev, "reg 0x%x: %pR\n", pos, res);
 
 	return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
 }
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [PATCH 1/3] PCI: add 0x prefix when printing the BAR registers position in __pci_read_base

Quoted 17 lines · Kevin Hao

We print the BAR registers position in hexadecimal format, so it is more readable if 0x prefix is added. Also fix the following checkpatch warning:
WARNING: Prefer dev_dbg(... to dev_printk(KERN_DEBUG, ...

---
v2: A new patch introduced in v2.

  drivers/pci/probe.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +0 -0
index 70f10fa..2505d5e 100644

drivers/pci/probe.c out: · line 283

Quoted 8 lines
drivers/pci/probe.c +2 -2
@@ -278,9 +278,9 @@ out:
  		pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
 
  	if (bar_too_big)
-		dev_err(&dev->dev, "reg %x: can't handle 64-bit BAR\n", pos);
+		dev_err(&dev->dev, "reg 0x%x: can't handle 64-bit BAR\n", pos);
  	if (res->flags && !bar_disabled)
-		dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos, res);
+		dev_dbg(&dev->dev, "reg 0x%x: %pR\n", pos, res);

Hi Kevin,

    dev_printk(KERN_DEBUG) -> dev_dbg() is not an equivalent change, it 

depends
on CONFIG_DEBUG and CONFIG_DYNAMIC_DEBUG.
Regards!
Gerry

Quoted 3 lines
drivers/pci/probe.c +0 -0
  	return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  }
maintainer
#3 lore raw

Re: [PATCH 1/3] PCI: add 0x prefix when printing the BAR registers position in __pci_read_base

Quoted 31 lines · Liu Jiang
Quoted 18 lines · Kevin Hao

We print the BAR registers position in hexadecimal format, so it is more readable if 0x prefix is added. Also fix the following checkpatch warning:
WARNING: Prefer dev_dbg(... to dev_printk(KERN_DEBUG, ...

---
v2: A new patch introduced in v2.

  drivers/pci/probe.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +0 -0
index 70f10fa..2505d5e 100644

drivers/pci/probe.c out: · line 285

Quoted 10 lines
drivers/pci/probe.c +2 -2
@@ -278,9 +278,9 @@ out:
                pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
        if (bar_too_big)
-               dev_err(&dev->dev, "reg %x: can't handle 64-bit BAR\n",
 pos);
+               dev_err(&dev->dev, "reg 0x%x: can't handle 64-bit BAR\n",
 pos);
        if (res->flags && !bar_disabled)
-               dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos,
 res);
+               dev_dbg(&dev->dev, "reg 0x%x: %pR\n", pos, res);

Hi Kevin,

Quoted 3 lines
    dev_printk(KERN_DEBUG) -> dev_dbg() is not an equivalent change, it

depends
on CONFIG_DEBUG and CONFIG_DYNAMIC_DEBUG.

I did keep the "dev_printk" here for that reason. I don't want that information to be missing from dmesg.

Quoted 6 lines
Quoted 4 lines
drivers/pci/probe.c +0 -0
        return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  }
author
#4 lore raw

Re: [PATCH 1/3] PCI: add 0x prefix when printing the BAR registers position in __pci_read_base

Quoted 36 lines · Bjorn Helgaas
Quoted 31 lines · Liu Jiang
Quoted 18 lines · Kevin Hao

We print the BAR registers position in hexadecimal format, so it is more readable if 0x prefix is added. Also fix the following checkpatch warning:
WARNING: Prefer dev_dbg(... to dev_printk(KERN_DEBUG, ...

---
v2: A new patch introduced in v2.

  drivers/pci/probe.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/probe.c +0 -0
index 70f10fa..2505d5e 100644

drivers/pci/probe.c out: · line 285

Quoted 10 lines
drivers/pci/probe.c +2 -2
@@ -278,9 +278,9 @@ out:
                pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
        if (bar_too_big)
-               dev_err(&dev->dev, "reg %x: can't handle 64-bit BAR\n",
 pos);
+               dev_err(&dev->dev, "reg 0x%x: can't handle 64-bit BAR\n",
 pos);
        if (res->flags && !bar_disabled)
-               dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos,
 res);
+               dev_dbg(&dev->dev, "reg 0x%x: %pR\n", pos, res);

Hi Kevin,

Quoted 3 lines
    dev_printk(KERN_DEBUG) -> dev_dbg() is not an equivalent change, it

depends
on CONFIG_DEBUG and CONFIG_DYNAMIC_DEBUG.

Quoted 2 lines

I did keep the "dev_printk" here for that reason. I don't want that information to be missing from dmesg.

Obviously I neglected this difference. :-(
Thanks for the reminder and fix Jiang and Bjorn.

Thanks,
Kevin

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
drivers/pci/probe.c +0 -0
        return (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
  }
author
#5 lore raw

PATCH v2 2/3 PCI: converge the unnecessary sprinkling of pcibios_bus_to_resource in __pci_read_base

In reply to Kevin Hao

Since we will invoke pcibios_bus_to_resource unconditionally if we don't goto fail, move it out of if/else wrap. No function change.

---
v2: Split these changed into a new patch compare with the v1 patch.

 drivers/pci/probe.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/pci/probe.c +2 -3
index 2505d5e..c57eb27 100644
@@ -250,12 +250,10 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
 			pci_write_config_dword(dev, pos + 4, 0);
 			region.start = 0;
 			region.end = sz64;
-			pcibios_bus_to_resource(dev, res, &region);
 			bar_disabled = true;
 		} else {
 			region.start = l64;
 			region.end = l64 + sz64;
-			pcibios_bus_to_resource(dev, res, &region);
 		}
 	} else {
 		sz = pci_size(l, sz, mask);
@@ -265,9 +263,10 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
 
 		region.start = l;
 		region.end = l + sz;
-		pcibios_bus_to_resource(dev, res, &region);
 	}
 
+	pcibios_bus_to_resource(dev, res, &region);
+
 	goto out;
 
 
Signature · 1 line
1.8.1.4
author
#6 lore raw

PATCH v2 3/3 PCI: unset the resource if a wrong bus address is assigned by firmware

In reply to Kevin Hao

In some situations, the bus and memory address used by kernel are not equal and the firmware may assign a bus address which happen to be a legitimate memory address to a PCI device, then the kernel would not find a matching bus region in host bridge and assume an offset of zero and translate it to a memory address the same as the bus address. This will leave the bus address in the PCI BAR register unchanged and this address is definitely not a legal bus address, then cause the device malfunction. We try to detect this by doing a invert translation. We can make sure that we run into this case if the value we get from the invert translation is not equal to the original bus address. In this case we would unset this resource and wish the kernel would trigger a reassign later.

---
v2:
Instead of tweak the pcibios_bus_to_resource and assume we know about all the host bridge window, we do an invert translation to check if we have a wrong bus address set in the PCI BAR register.

 drivers/pci/probe.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/pci/probe.c +24 -1
index c57eb27..33c2e72 100644
@@ -170,7 +170,7 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
 {
 	u32 l, sz, mask;
 	u16 orig_cmd;
-	struct pci_bus_region region;
+	struct pci_bus_region region, inverted_region;
 	bool bar_too_big = false, bar_disabled = false;
 
 	mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
@@ -267,6 +267,29 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
 
 	pcibios_bus_to_resource(dev, res, &region);
 
+	/*
+	 * In some situations, the bus and memory address used by kernel are
+	 * not equal and the firmware may assign a bus address which happen
+	 * to be a legitimate memory address to a PCI device, then the kernel
+	 * would not find a matching bus region in host bridge and assume an
+	 * offset of zero and translate it to a memory address the same as the
+	 * bus address. This will leave the bus address in the PCI BAR register
+	 * unchanged and this address is definitely not a legal bus address,
+	 * then cause the device malfunction. We try to detect this by doing
+	 * a invert translation. We can make sure that we run into this case
+	 * if the value we get from the invert translation is not equal to the
+	 * original bus address.
+	 */
+	pcibios_resource_to_bus(dev, &inverted_region, res);
+	if (inverted_region.start != region.start ||
+		 inverted_region.end != region.end) {
+		dev_info(&dev->dev, "reg 0x%x: bus address [%pa - %pa] allocated by firmware is not in any of the address regions of PCI host bridge, we would force to reassign it later\n",
+			pos, &region.start, &region.end);
+		res->flags |= IORESOURCE_UNSET;
+		res->end -= res->start;
+		res->start = 0;
+	}
+
 	goto out;
 
 
Signature · 1 line
1.8.1.4
maintainer Applied
#7 lore raw
In reply to Kevin Hao
Quoted 22 lines · Kevin Hao

v2:
* Use the method suggested by Bjorn to check if we have a wrong bus address set in the PCI BAR register.
* Split the "move the pcibios_bus_to_resource() call out of the if/else wrap" change into a new patch.
* A minor new patch is introduced to add 0x prefix for the PCI BAR register position.

v1:
On a fsl p2020rdb-pc board, the onboard pci device is assigned the bus address 0xa0000000 by firmware. But the kernel allocate the cpu address 0xa0000000 ~ 0xbfffffff to this PCIe controller and use 0xe0000000 ~ 0xdfffffff as its bus address. In this case the kernel would think that the bus address in this pci device is correct and leave it unchanged. Then causes this device not work.

Even though this looks like a mismatch of the address space between firmware and kernel. I think we should detect this kind of error and fix it automatically if we don't want just to probe only (PCI_PROBE_ONLY is not set).

---
Kevin Hao (3):
PCI: add 0x prefix when printing the BAR registers position in

Quoted 7 lines
    __pci_read_base
  PCI: converge the unnecessary sprinkling of pcibios_bus_to_resource in
        __pci_read_base

PCI: unset the resource if a wrong bus address is assigned by firmware

 drivers/pci/probe.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

Applied to my "next" branch for v3.11, thanks.

Bjorn