Skip to contentSkip to conversation

pci: Skip configuring PCI Rom Address for unsupported header types

PATCH In review Pali Rohár 3 participants 15 messages Oct 7, 2021 – Oct 15, 2021 PCI

lore
Share & tools
lore
#1 lore raw

PATCH 2/5 pci: Skip configuring invalid P2P bridge devices

In reply to Pali Rohár

Function dm_pci_hose_probe_bus() expects that bus is valid PCI device with Bridge header type (0x01). So add check before touching PCI config space to prevent misconfiguring some non-standard device.

---
 drivers/pci/pci-uclass.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/pci/pci-uclass.c +9 -0
index 044babee164f..5da3515f5f25 100644
@@ -627,6 +627,7 @@ int pci_generic_mmap_read_config(
 
 int dm_pci_hose_probe_bus(struct udevice *bus)
 {
+	u8 header_type;
 	int sub_bus;
 	int ret;
 	int ea_pos;
@@ -634,6 +635,14 @@ int dm_pci_hose_probe_bus(struct udevice *bus)
 
 	debug("%s\n", __func__);
 
+	dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);
+	header_type &= 0x7f;
+	if (header_type != PCI_HEADER_TYPE_BRIDGE) {
+		debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
+		      __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
+		return log_msg_ret("probe", -EINVAL);
+	}
+
 	ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
 	if (ea_pos) {
 		dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
Signature · 1 line
2.20.1
#0 lore raw

PATCH 1/5 pci: Skip configuring PCI Rom Address for unsupported header types

PCI Rom Address is currently supported only for Normal (0x00) and Bridge (0x01) header types. Fix code accordingly.

---
 drivers/pci/pci_auto.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pci/pci_auto.c +2 -1
index 08082460eb86..288f7996c7c0 100644
@@ -131,7 +131,8 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
 	/* Configure the expansion ROM address */
 	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
 	header_type &= 0x7f;
-	if (header_type != PCI_HEADER_TYPE_CARDBUS) {
+	if (header_type == PCI_HEADER_TYPE_NORMAL ||
+	    header_type == PCI_HEADER_TYPE_BRIDGE) {
 		rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
 			PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
 		dm_pci_write_config32(dev, rom_addr, 0xfffffffe);
Signature · 1 line
2.20.1
#7 lore raw

PATCH 4/5 pci: Fix showing bars

Header type is 7-bit number so properly clear upper 8th bit which indicates multifunction device.

And do not try to show bars for unsupported header types.

---
 cmd/pci.c | 5 +++++
 1 file changed, 5 insertions(+)
cmd/pci.c +5 -0
index cfabdc0f3012..4a82854db7fa 100644
@@ -71,10 +71,15 @@ static int pci_bar_show(struct udevice *dev)
 	int prefetchable;
 
 	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
+	header_type &= 0x7f;
 
 	if (header_type == PCI_HEADER_TYPE_CARDBUS) {
 		printf("CardBus doesn't support BARs\n");
 		return -ENOSYS;
+	} else if (header_type != PCI_HEADER_TYPE_NORMAL &&
+		   header_type != PCI_HEADER_TYPE_BRIDGE) {
+		printf("unknown header type\n");
+		return -ENOSYS;
 	}
 
 	bar_cnt = (header_type == PCI_HEADER_TYPE_NORMAL) ? 6 : 2;
Signature · 1 line
2.20.1
#4 lore raw

PATCH 3/5 pci: Fix configuring BARs

In reply to Pali Rohár

Number of BARs is defined by header type, not by class code.

---
 drivers/pci/pci_auto.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
drivers/pci/pci_auto.c +25 -6
index 288f7996c7c0..5af4ee6e56df 100644
@@ -19,7 +19,7 @@
 #define CONFIG_SYS_PCI_CACHE_LINE_SIZE	8
 #endif
 
-static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
+static void dm_pciauto_setup_device(struct udevice *dev,
 				    struct pci_region *mem,
 				    struct pci_region *prefetch,
 				    struct pci_region *io)
@@ -28,6 +28,7 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
 	pci_size_t bar_size;
 	u16 cmdstat = 0;
 	int bar, bar_nr = 0;
+	int bars_num;
 	u8 header_type;
 	int rom_addr;
 	pci_addr_t bar_value;
@@ -39,6 +40,26 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
 	cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
 			PCI_COMMAND_MASTER;
 
+	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
+	header_type &= 0x7f;
+
+	switch (header_type) {
+	case PCI_HEADER_TYPE_NORMAL:
+		bars_num = 6;
+		break;
+	case PCI_HEADER_TYPE_BRIDGE:
+		bars_num = 2;
+		break;
+	case PCI_HEADER_TYPE_CARDBUS:
+		/* CardBus header does not have any BAR */
+		bars_num = 0;
+		break;
+	default:
+		/* Skip configuring BARs for unknown header types */
+		bars_num = 0;
+		break;
+	}
+
 	for (bar = PCI_BASE_ADDRESS_0;
 	     bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
 		int ret = 0;
@@ -129,8 +150,6 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
 	}
 
 	/* Configure the expansion ROM address */
-	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
-	header_type &= 0x7f;
 	if (header_type == PCI_HEADER_TYPE_NORMAL ||
 	    header_type == PCI_HEADER_TYPE_BRIDGE) {
 		rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
@@ -343,7 +362,7 @@ int dm_pciauto_config_device(struct udevice *dev)
 		debug("PCI Autoconfig: Found P2P bridge, device %d\n",
 		      PCI_DEV(dm_pci_get_bdf(dev)));
 
-		dm_pciauto_setup_device(dev, 2, pci_mem, pci_prefetch, pci_io);
+		dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
 
 		ret = dm_pci_hose_probe_bus(dev);
 		if (ret < 0)
@@ -356,7 +375,7 @@ int dm_pciauto_config_device(struct udevice *dev)
 		 * just do a minimal setup of the bridge,
 		 * let the OS take care of the rest
 		 */
-		dm_pciauto_setup_device(dev, 0, pci_mem, pci_prefetch, pci_io);
+		dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
 
 		debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
 		      PCI_DEV(dm_pci_get_bdf(dev)));
@@ -388,7 +407,7 @@ int dm_pciauto_config_device(struct udevice *dev)
 		/* fall through */
 
 	default:
-		dm_pciauto_setup_device(dev, 6, pci_mem, pci_prefetch, pci_io);
+		dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
 		break;
 	}
 
Signature · 1 line
2.20.1
#10 lore raw

PATCH 5/5 pci: Fix showing registers

In reply to Pali Rohár

Header type is 7-bit number so use all 7 bits when detecting header type and not only 2 bits.

---
 cmd/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/pci.c +1 -1
index 4a82854db7fa..3b1863f139c9 100644
@@ -239,7 +239,7 @@ static void pci_header_show(struct udevice *dev)
 	       pci_class_str(class));
 	pci_show_regs(dev, regs_rest);
 
-	switch (header_type & 0x03) {
+	switch (header_type & 0x7f) {
 	case PCI_HEADER_TYPE_NORMAL:	/* "normal" PCI device */
 		pci_show_regs(dev, regs_normal);
 		break;
Signature · 1 line
2.20.1
Reviewed-by on v1 1/5
#13 lore raw
In reply to Pali Rohár
Quoted 4 lines · Pali Rohár

PCI Rom Address is currently supported only for Normal (0x00) and Bridge (0x01) header types. Fix code accordingly.

Thanks,
Stefan

Quoted 10 lines
---
  drivers/pci/pci_auto.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pci/pci_auto.c +0 -0
index 08082460eb86..288f7996c7c0 100644

drivers/pci/pci_auto.c static void dm_pciauto_setup_device(struct udevice *dev, int bars_num, · line 138

Quoted 9 lines
drivers/pci/pci_auto.c +2 -1
@@ -131,7 +131,8 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
  	/* Configure the expansion ROM address */
  	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
  	header_type &= 0x7f;
-	if (header_type != PCI_HEADER_TYPE_CARDBUS) {
+	if (header_type == PCI_HEADER_TYPE_NORMAL ||
+	    header_type == PCI_HEADER_TYPE_BRIDGE) {
  		rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
  			PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
  		dm_pci_write_config32(dev, rom_addr, 0xfffffffe);

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 2/5
#2 lore raw

Re: [PATCH 2/5] pci: Skip configuring invalid P2P bridge devices

In reply to Pali Rohár
Quoted 5 lines · Pali Rohár

Function dm_pci_hose_probe_bus() expects that bus is valid PCI device with Bridge header type (0x01). So add check before touching PCI config space to prevent misconfiguring some non-standard device.

Thanks,
Stefan

Quoted 22 lines
---
  drivers/pci/pci-uclass.c | 9 +++++++++
  1 file changed, 9 insertions(+)
drivers/pci/pci-uclass.c +2 -0
index 044babee164f..5da3515f5f25 100644
@@ -627,6 +627,7 @@ int pci_generic_mmap_read_config(
 
  int dm_pci_hose_probe_bus(struct udevice *bus)
  {
+	u8 header_type;
  	int sub_bus;
  	int ret;
  	int ea_pos;
@@ -634,6 +635,14 @@ int dm_pci_hose_probe_bus(struct udevice *bus)
 
  	debug("%s\n", __func__);
 
+	dm_pci_read_config8(bus, PCI_HEADER_TYPE, &header_type);

drivers/pci/pci-uclass.c int dm_pci_hose_probe_bus(struct udevice *bus) · line 648

Quoted 10 lines
drivers/pci/pci-uclass.c +7 -0
+	header_type &= 0x7f;
+	if (header_type != PCI_HEADER_TYPE_BRIDGE) {
+		debug("%s: Skipping PCI device %d with Non-Bridge Header Type 0x%x\n",
+		      __func__, PCI_DEV(dm_pci_get_bdf(bus)), header_type);
+		return log_msg_ret("probe", -EINVAL);
+	}
+
  	ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
  	if (ea_pos) {
  		dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 3/5
#5 lore raw

Re: [PATCH 3/5] pci: Fix configuring BARs

In reply to Pali Rohár
Quoted 3 lines · Pali Rohár

Number of BARs is defined by header type, not by class code.

Thanks,
Stefan

Quoted 81 lines
---
  drivers/pci/pci_auto.c | 31 +++++++++++++++++++++++++------
  1 file changed, 25 insertions(+), 6 deletions(-)
drivers/pci/pci_auto.c +24 -5
index 288f7996c7c0..5af4ee6e56df 100644
@@ -19,7 +19,7 @@
  #define CONFIG_SYS_PCI_CACHE_LINE_SIZE	8
  #endif
 
-static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
+static void dm_pciauto_setup_device(struct udevice *dev,
  				    struct pci_region *mem,
  				    struct pci_region *prefetch,
  				    struct pci_region *io)
@@ -28,6 +28,7 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
  	pci_size_t bar_size;
  	u16 cmdstat = 0;
  	int bar, bar_nr = 0;
+	int bars_num;
  	u8 header_type;
  	int rom_addr;
  	pci_addr_t bar_value;
@@ -39,6 +40,26 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
  	cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
  			PCI_COMMAND_MASTER;
 
+	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
+	header_type &= 0x7f;
+
+	switch (header_type) {
+	case PCI_HEADER_TYPE_NORMAL:
+		bars_num = 6;
+		break;
+	case PCI_HEADER_TYPE_BRIDGE:
+		bars_num = 2;
+		break;
+	case PCI_HEADER_TYPE_CARDBUS:
+		/* CardBus header does not have any BAR */
+		bars_num = 0;
+		break;
+	default:
+		/* Skip configuring BARs for unknown header types */
+		bars_num = 0;
+		break;
+	}
+
  	for (bar = PCI_BASE_ADDRESS_0;
  	     bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
  		int ret = 0;
@@ -129,8 +150,6 @@ static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
  	}
 
  	/* Configure the expansion ROM address */
-	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
-	header_type &= 0x7f;
  	if (header_type == PCI_HEADER_TYPE_NORMAL ||
  	    header_type == PCI_HEADER_TYPE_BRIDGE) {
  		rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
@@ -343,7 +362,7 @@ int dm_pciauto_config_device(struct udevice *dev)
  		debug("PCI Autoconfig: Found P2P bridge, device %d\n",
  		      PCI_DEV(dm_pci_get_bdf(dev)));
 
-		dm_pciauto_setup_device(dev, 2, pci_mem, pci_prefetch, pci_io);
+		dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
 
  		ret = dm_pci_hose_probe_bus(dev);
  		if (ret < 0)
@@ -356,7 +375,7 @@ int dm_pciauto_config_device(struct udevice *dev)
  		 * just do a minimal setup of the bridge,
  		 * let the OS take care of the rest
  		 */
-		dm_pciauto_setup_device(dev, 0, pci_mem, pci_prefetch, pci_io);
+		dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
 
  		debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
  		      PCI_DEV(dm_pci_get_bdf(dev)));

drivers/pci/pci_auto.c int dm_pciauto_config_device(struct udevice *dev) · line 413

Quoted 8 lines
drivers/pci/pci_auto.c +1 -1
@@ -388,7 +407,7 @@ int dm_pciauto_config_device(struct udevice *dev)
  		/* fall through */
 
  	default:
-		dm_pciauto_setup_device(dev, 6, pci_mem, pci_prefetch, pci_io);
+		dm_pciauto_setup_device(dev, pci_mem, pci_prefetch, pci_io);
  		break;
  	}
 

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] pci: Fix showing bars

In reply to Pali Rohár
Quoted 6 lines · Pali Rohár

Header type is 7-bit number so properly clear upper 8th bit which indicates multifunction device.

And do not try to show bars for unsupported header types.

Thanks,
Stefan

Quoted 15 lines
---
  cmd/pci.c | 5 +++++
  1 file changed, 5 insertions(+)
cmd/pci.c +1 -0
index cfabdc0f3012..4a82854db7fa 100644
@@ -71,10 +71,15 @@ static int pci_bar_show(struct udevice *dev)
  	int prefetchable;
 
  	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
+	header_type &= 0x7f;
 

cmd/pci.c static int pci_bar_show(struct udevice *dev) · line 85

Quoted 10 lines
cmd/pci.c +4 -0
  	if (header_type == PCI_HEADER_TYPE_CARDBUS) {
  		printf("CardBus doesn't support BARs\n");
  		return -ENOSYS;
+	} else if (header_type != PCI_HEADER_TYPE_NORMAL &&
+		   header_type != PCI_HEADER_TYPE_BRIDGE) {
+		printf("unknown header type\n");
+		return -ENOSYS;
  	}
 
  	bar_cnt = (header_type == PCI_HEADER_TYPE_NORMAL) ? 6 : 2;

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Reviewed-by on v1 5/5
#11 lore raw

Re: [PATCH 5/5] pci: Fix showing registers

In reply to Pali Rohár
Quoted 4 lines · Pali Rohár

Header type is 7-bit number so use all 7 bits when detecting header type and not only 2 bits.

Thanks,
Stefan

Quoted 10 lines
---
  cmd/pci.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
cmd/pci.c +0 -0
index 4a82854db7fa..3b1863f139c9 100644

cmd/pci.c static void pci_header_show(struct udevice *dev) · line 245

Quoted 8 lines
cmd/pci.c +1 -1
@@ -239,7 +239,7 @@ static void pci_header_show(struct udevice *dev)
  	       pci_class_str(class));
  	pci_show_regs(dev, regs_rest);
 
-	switch (header_type & 0x03) {
+	switch (header_type & 0x7f) {
  	case PCI_HEADER_TYPE_NORMAL:	/* "normal" PCI device */
  		pci_show_regs(dev, regs_normal);
  		break;

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#3 lore raw

Re: [PATCH 2/5] pci: Skip configuring invalid P2P bridge devices

In reply to Pali Rohár
Quoted 6 lines · Pali Rohár

Function dm_pci_hose_probe_bus() expects that bus is valid PCI device with Bridge header type (0x01). So add check before touching PCI config space to prevent misconfiguring some non-standard device.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom