Skip to contentSkip to conversation

dm: pci: Various fixes and test cases update

PATCH Superseded Bin Meng 2 participants 43 messages Jul 29, 2018 – Aug 2, 2018

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

v2 cover dm: pci: Various fixes and test cases update

This fixes several bugs in dm pci driver, as well as updating test cases running on Sandbox to test dm pci.

It turns out adding test cases for v1 (3 commits) reveals more design limitations/bugs of sandbox pci driver. Now the v2 series contains 20+ patches!

This series is available for testing at u-boot-x86/pci branch.

Changes in v2:
- Mask header_type with 0x7f

Bin Meng (21):
pci: Remove 440ep specific macros
dm: Correct typos in uclass_first/next_device_check()
dm: core: Add ofnode function to read PCI vendor and device id
dm: pci: Extract vendor/device id in child_post_bind()
dm: pci: Fix scanning multi-function device
test: dm: pci: Remove unnecessary steps in dm_test_pci_swapcase()
test: dm: pci: Test more than one device on the same bus
pci: sandbox: swap_case: Preserve space indicator bit in BAR registers
test: dm: pci: Test more than one PCI host controller
test: dm: pci: Add tests for configuration space access
pci: sandbox: emul: Fix the call to pci_bus_find_devfn()
dm: pci: Assign correct driver data when binding a driver
pci: sandbox: Support dynamically binding device driver
pci: sandbox: swap_case: Declare dynamic driver matching
sandbox: Update test.dts for dynamic PCI device driver matching
test: dm: pci: Test driver binding with driver data provided
pci: sandbox: emul: Rename priv structure
test: dm: pci: Add tests for mixed static and dynamic devices on the
same bus
pci: Add all known capability and extended capability ids
dm: pci: Add APIs to find capability and extended capability
test: dm: pci: Add cases for finding PCI capability APIs

 arch/sandbox/dts/test.dts       |  41 +++++++++-
 arch/sandbox/include/asm/test.h |  12 +++
 doc/driver-model/pci-info.txt   |  23 ++++++
 drivers/core/ofnode.c           |  36 +++++++++
 drivers/misc/swap_case.c        |  36 ++++++++-
 drivers/pci/pci-emul-uclass.c   |  24 +++---
 drivers/pci/pci-uclass.c        |  83 ++++++++++++++++++--
 drivers/pci/pci_sandbox.c       |  78 ++++++++++++++++--
 include/dm/ofnode.h             |  13 +++
 include/dm/uclass.h             |   4 +-
 include/pci.h                   |  84 ++++++++++++++++----
 test/dm/pci.c                   | 170 ++++++++++++++++++++++++++++++++++++++--
 12 files changed, 551 insertions(+), 53 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 1/21 pci: Remove 440ep specific macros

These macros should not be put in the generic pci.h header file. Since they are not referenced anywhere, remove them completely.

---

Changes in v2: None

 include/pci.h | 15 ---------------
 1 file changed, 15 deletions(-)
include/pci.h +0 -15
index 8e27cbf..427094c 100644
@@ -271,21 +271,6 @@
 #define  PCI_BRIDGE_CTL_BUS_RESET 0x40	/* Secondary bus reset */
 #define  PCI_BRIDGE_CTL_FAST_BACK 0x80	/* Fast Back2Back enabled on secondary interface */
 
-/* From 440ep */
-#define PCI_ERREN       0x48     /* Error Enable */
-#define PCI_ERRSTS      0x49     /* Error Status */
-#define PCI_BRDGOPT1    0x4A     /* PCI Bridge Options 1 */
-#define PCI_PLBSESR0    0x4C     /* PCI PLB Slave Error Syndrome 0 */
-#define PCI_PLBSESR1    0x50     /* PCI PLB Slave Error Syndrome 1 */
-#define PCI_PLBSEAR     0x54     /* PCI PLB Slave Error Address */
-#define PCI_CAPID       0x58     /* Capability Identifier */
-#define PCI_NEXTITEMPTR 0x59     /* Next Item Pointer */
-#define PCI_PMC         0x5A     /* Power Management Capabilities */
-#define PCI_PMCSR       0x5C     /* Power Management Control Status */
-#define PCI_PMCSRBSE    0x5E     /* PMCSR PCI to PCI Bridge Support Extensions */
-#define PCI_BRDGOPT2    0x60     /* PCI Bridge Options 2 */
-#define PCI_PMSCRR      0x64     /* Power Management State Change Request Re. */
-
 /* Header type 2 (CardBus bridges) */
 #define PCI_CB_CAPABILITY_LIST	0x14
 /* 0x15 reserved */
Signature · 1 line
2.7.4
author
#3 lore raw

PATCH v2 2/21 dm: Correct typos in uclass_first/next_device_check()

In reply to Bin Meng

Correct typos in the comment block of uclass_first/next_device_check().

---

Changes in v2: None

 include/dm/uclass.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/dm/uclass.h +2 -2
index 9fbaa7d..0e882ce 100644
@@ -302,7 +302,7 @@ int uclass_first_device_err(enum uclass_id id, struct udevice **devp);
 int uclass_next_device(struct udevice **devp);
 
 /**
- * uclass_first_device() - Get the first device in a uclass
+ * uclass_first_device_check() - Get the first device in a uclass
  *
  * The device returned is probed if necessary, and ready for use
  *
@@ -318,7 +318,7 @@ int uclass_next_device(struct udevice **devp);
 int uclass_first_device_check(enum uclass_id id, struct udevice **devp);
 
 /**
- * uclass_next_device() - Get the next device in a uclass
+ * uclass_next_device_check() - Get the next device in a uclass
  *
  * The device returned is probed if necessary, and ready for use
  *
Signature · 1 line
2.7.4
author
#5 lore raw

PATCH v2 3/21 dm: core: Add ofnode function to read PCI vendor and device id

In reply to Bin Meng

We don't have the live-tree version of fdtdec_get_pci_vendev(). This adds the API.

---

Changes in v2: None

 drivers/core/ofnode.c | 36 ++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 13 +++++++++++++
 2 files changed, 49 insertions(+)
drivers/core/ofnode.c +36 -0
index 2937539..0cfb0fb 100644
@@ -623,6 +623,42 @@ fail:
 	return ret;
 }
 
+int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
+{
+	const char *list, *end;
+	int len;
+
+	list = ofnode_get_property(node, "compatible", &len);
+	if (!list)
+		return -ENOENT;
+
+	end = list + len;
+	while (list < end) {
+		len = strlen(list);
+		if (len >= strlen("pciVVVV,DDDD")) {
+			char *s = strstr(list, "pci");
+
+			/*
+			 * check if the string is something like pciVVVV,DDDD.RR
+			 * or just pciVVVV,DDDD
+			 */
+			if (s && s[7] == ',' &&
+			    (s[12] == '.' || s[12] == 0)) {
+				s += 3;
+				*vendor = simple_strtol(s, NULL, 16);
+
+				s += 5;
+				*device = simple_strtol(s, NULL, 16);
+
+				return 0;
+			}
+		}
+		list += (len + 1);
+	}
+
+	return -ENOENT;
+}
+
 int ofnode_read_addr_cells(ofnode node)
 {
 	if (ofnode_is_np(node))
include/dm/ofnode.h +13 -0
index cd08a7e..ab36b74 100644
@@ -586,6 +586,19 @@ int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
 			 const char *propname, struct fdt_pci_addr *addr);
 
 /**
+ * ofnode_read_pci_vendev() - look up PCI vendor and device id
+ *
+ * Look at the compatible property of a device node that represents a PCI
+ * device and extract pci vendor id and device id from it.
+ *
+ * @param node		node to examine
+ * @param vendor	vendor id of the pci device
+ * @param device	device id of the pci device
+ * @return 0 if ok, negative on error
+ */
+int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
+
+/**
  * ofnode_read_addr_cells() - Get the number of address cells for a node
  *
  * This walks back up the tree to find the closest #address-cells property
Signature · 1 line
2.7.4
author
#7 lore raw

PATCH v2 4/21 dm: pci: Extract vendor/device id in child_post_bind()

In reply to Bin Meng

Currently only devfn is extracted in child_post_bind(). Now that we have the live-tree version API to look up PCI vendor and device id from the compatible string, let's extract and save them too.

---

Changes in v2: None

 drivers/pci/pci-uclass.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/pci/pci-uclass.c +5 -6
index 46e9c71..b2909a3 100644
@@ -987,19 +987,18 @@ static int pci_uclass_child_post_bind(struct udevice *dev)
 	if (!dev_of_valid(dev))
 		return 0;
 
-	/*
-	 * We could read vendor, device, class if available. But for now we
-	 * just check the address.
-	 */
 	pplat = dev_get_parent_platdata(dev);
+
+	/* Extract vendor id and device id if available */
+	ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
+
+	/* Extract the devfn from fdt_pci_addr */
 	ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG, "reg",
 				   &addr);
-
 	if (ret) {
 		if (ret != -ENOENT)
 			return -EINVAL;
 	} else {
-		/* extract the devfn from fdt_pci_addr */
 		pplat->devfn = addr.phys_hi & 0xff00;
 	}
 
Signature · 1 line
2.7.4
author
#9 lore raw

PATCH v2 5/21 dm: pci: Fix scanning multi-function device

In reply to Bin Meng

The flag to control whether to scan multi-function device during enumeration should be cleared at the beginning of each iteration if the device's function number equals to zero.

---

Changes in v2: None

 drivers/pci/pci-uclass.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/pci-uclass.c +2 -0
index b2909a3..28ea8c5 100644
@@ -745,6 +745,8 @@ int pci_bind_bus_devices(struct udevice *bus)
 		struct udevice *dev;
 		ulong class;
 
+		if (!PCI_FUNC(bdf))
+			found_multi = false;
 		if (PCI_FUNC(bdf) && !found_multi)
 			continue;
 		/* Check only the first access, we don't expect problems */
Signature · 1 line
2.7.4
author
#11 lore raw

PATCH v2 6/21 test: dm: pci: Remove unnecessary steps in dm_test_pci_swapcase()

In reply to Bin Meng

The check on uclass_get_device() and device_active() is unnecessary as the follow-up test operations will implicitly probe the driver.

---

Changes in v2: None

 test/dm/pci.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
test/dm/pci.c +1 -3
index 47b5d22..be1208c 100644
@@ -34,14 +34,12 @@ DM_TEST(dm_test_pci_busnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
 /* Test that we can use the swapcase device correctly */
 static int dm_test_pci_swapcase(struct unit_test_state *uts)
 {
-	struct udevice *emul, *swap;
+	struct udevice *swap;
 	ulong io_addr, mem_addr;
 	char *ptr;
 
 	/* Check that asking for the device automatically fires up PCI */
-	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 0, &emul));
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
-	ut_assert(device_active(swap));
 
 	/* First test I/O */
 	io_addr = dm_pci_read_bar32(swap, 0);
Signature · 1 line
2.7.4
author
#13 lore raw

PATCH v2 7/21 test: dm: pci: Test more than one device on the same bus

In reply to Bin Meng

It's quite common to have more than one device on the same PCI bus. This updates the test case to test such scenario.

---

Changes in v2: None

 arch/sandbox/dts/test.dts |  7 +++++++
 test/dm/pci.c             | 37 +++++++++++++++++++++++++++++++++----
 2 files changed, 40 insertions(+), 4 deletions(-)
arch/sandbox/dts/test.dts +7 -0
index 137679a..237266d 100644
@@ -302,6 +302,13 @@
 		#size-cells = <2>;
 		ranges = <0x02000000 0 0x10000000 0x10000000 0 0x2000
 				0x01000000 0 0x20000000 0x20000000 0 0x2000>;
+		pci at 0,0 {
+			compatible = "pci-generic";
+			reg = <0x0000 0 0 0 0>;
+			emul at 0,0 {
+				compatible = "sandbox,swap-case";
+			};
+		};
 		pci at 1f,0 {
 			compatible = "pci-generic";
 			reg = <0xf800 0 0 0 0>;
test/dm/pci.c +33 -4
index be1208c..f2bd52a 100644
@@ -20,16 +20,24 @@ static int dm_test_pci_base(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
 
-/* Test that sandbox PCI bus numbering works correctly */
-static int dm_test_pci_busnum(struct unit_test_state *uts)
+/* Test that sandbox PCI bus numbering and device works correctly */
+static int dm_test_pci_busdev(struct unit_test_state *uts)
 {
 	struct udevice *bus;
+	struct udevice *emul, *swap;
 
 	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
 
+	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 0, &emul));
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
+	ut_assert(device_active(swap));
+	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 1, &emul));
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
+	ut_assert(device_active(swap));
+
 	return 0;
 }
-DM_TEST(dm_test_pci_busnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+DM_TEST(dm_test_pci_busdev, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
 
 /* Test that we can use the swapcase device correctly */
 static int dm_test_pci_swapcase(struct unit_test_state *uts)
@@ -38,7 +46,28 @@ static int dm_test_pci_swapcase(struct unit_test_state *uts)
 	ulong io_addr, mem_addr;
 	char *ptr;
 
-	/* Check that asking for the device automatically fires up PCI */
+	/* Check that asking for the device 0 automatically fires up PCI */
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
+
+	/* First test I/O */
+	io_addr = dm_pci_read_bar32(swap, 0);
+	outb(2, io_addr);
+	ut_asserteq(2, inb(io_addr));
+
+	/*
+	 * Now test memory mapping - note we must unmap and remap to cause
+	 * the swapcase emulation to see our data and response.
+	 */
+	mem_addr = dm_pci_read_bar32(swap, 1);
+	ptr = map_sysmem(mem_addr, 20);
+	strcpy(ptr, "This is a TesT");
+	unmap_sysmem(ptr);
+
+	ptr = map_sysmem(mem_addr, 20);
+	ut_asserteq_str("tHIS IS A tESt", ptr);
+	unmap_sysmem(ptr);
+
+	/* Check that asking for the device 1 automatically fires up PCI */
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
 
 	/* First test I/O */
Signature · 1 line
2.7.4
author
#15 lore raw

PATCH v2 8/21 pci: sandbox: swap_case: Preserve space indicator bit in BAR registers

In reply to Bin Meng

With the newly added testing of more than one device, we get:

=> ut dm pci_swapcase
Test: dm_test_pci_swapcase: pci.c
test/dm/pci.c:88, dm_test_pci_swapcase(): "tHIS IS A tESt" = ptr:
Expected "tHIS IS A tESt", got "this is a test"
Test: dm_test_pci_swapcase: pci.c (flat tree) test/dm/pci.c:88, dm_test_pci_swapcase(): "tHIS IS A tESt" = ptr:
Expected "tHIS IS A tESt", got "this is a test"
Failures: 2

The failure only happens on the 2nd swap_case device on the PCI bus. The case passes on the 1st device.

It turns out the swap_case driver does not emulate bit#0 in BAR registers as a read-only bit. This corrects the implementation.

---

Changes in v2: None

 drivers/misc/swap_case.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/misc/swap_case.c +5 -3
index b777404..80ccb9f 100644
@@ -142,6 +142,8 @@ static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
 
 		debug("w bar %d=%lx\n", barnum, value);
 		*bar = value;
+		/* space indicator (bit#0) is read-only */
+		*bar |= barinfo[barnum].type;
 		break;
 	}
 	}
@@ -157,11 +159,11 @@ static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
 
 	for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
 		unsigned int size = barinfo[barnum].size;
+		u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
 
-		if (addr >= plat->bar[barnum] &&
-		    addr < plat->bar[barnum] + size) {
+		if (addr >= base && addr < base + size) {
 			*barnump = barnum;
-			*offsetp = addr - plat->bar[barnum];
+			*offsetp = addr - base;
 			return 0;
 		}
 	}
Signature · 1 line
2.7.4
author
#17 lore raw

PATCH v2 9/21 test: dm: pci: Test more than one PCI host controller

In reply to Bin Meng

So far there is only one PCI host controller in the sandbox test configuration. This is normally the case for x86, but it can be common on other architectures like ARM/PPC to have more than one PCI host controller in the system.

This updates the case to cover such scenario.

---

Changes in v2: None

 arch/sandbox/dts/test.dts | 28 ++++++++++++++++++++++++++--
 test/dm/pci.c             | 11 +++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)
arch/sandbox/dts/test.dts +26 -2
index 237266d..0bce6d0 100644
@@ -14,7 +14,8 @@
 		i2c0 = "/i2c at 0";
 		mmc0 = "/mmc0";
 		mmc1 = "/mmc1";
-		pci0 = &pci;
+		pci0 = &pci0;
+		pci1 = &pci1;
 		remoteproc1 = &rproc_1;
 		remoteproc2 = &rproc_2;
 		rtc0 = &rtc_0;
@@ -295,7 +296,7 @@
 		compatible = "sandbox,mmc";
 	};
 
-	pci: pci-controller {
+	pci0: pci-controller0 {
 		compatible = "sandbox,pci";
 		device_type = "pci";
 		#address-cells = <3>;
@@ -318,6 +319,29 @@
 		};
 	};
 
+	pci1: pci-controller1 {
+		compatible = "sandbox,pci";
+		device_type = "pci";
+		#address-cells = <3>;
+		#size-cells = <2>;
+		ranges = <0x02000000 0 0x30000000 0x30000000 0 0x2000
+				0x01000000 0 0x40000000 0x40000000 0 0x2000>;
+		pci at 8,0 {
+			compatible = "pci-generic";
+			reg = <0x4000 0 0 0 0>;
+			emul at 8,0 {
+				compatible = "sandbox,swap-case";
+			};
+		};
+		pci at c,0 {
+			compatible = "pci-generic";
+			reg = <0x6000 0 0 0 0>;
+			emul at c,0 {
+				compatible = "sandbox,swap-case";
+			};
+		};
+	};
+
 	probing {
 		compatible = "simple-bus";
 		test1 {
test/dm/pci.c +11 -0
index f2bd52a..727ec34 100644
@@ -26,6 +26,7 @@ static int dm_test_pci_busdev(struct unit_test_state *uts)
 	struct udevice *bus;
 	struct udevice *emul, *swap;
 
+	/* Test bus#0 and its devices */
 	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
 
 	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 0, &emul));
@@ -35,6 +36,16 @@ static int dm_test_pci_busdev(struct unit_test_state *uts)
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
 	ut_assert(device_active(swap));
 
+	/* Test bus#1 and its devices */
+	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
+
+	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 2, &emul));
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
+	ut_assert(device_active(swap));
+	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 3, &emul));
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
+	ut_assert(device_active(swap));
+
 	return 0;
 }
 DM_TEST(dm_test_pci_busdev, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Signature · 1 line
2.7.4
author
#19 lore raw

PATCH v2 10/21 test: dm: pci: Add tests for configuration space access

In reply to Bin Meng

So far we missed the testing for PCI configuration space access.

---

Changes in v2: None

 test/dm/pci.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
test/dm/pci.c +15 -9
index 727ec34..089b72e 100644
@@ -6,6 +6,7 @@
 #include <common.h>
 #include <dm.h>
 #include <asm/io.h>
+#include <asm/test.h>
 #include <dm/test.h>
 #include <test/ut.h>
 
@@ -24,27 +25,32 @@ DM_TEST(dm_test_pci_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
 static int dm_test_pci_busdev(struct unit_test_state *uts)
 {
 	struct udevice *bus;
-	struct udevice *emul, *swap;
+	struct udevice *swap;
+	u16 vendor, device;
 
 	/* Test bus#0 and its devices */
 	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
 
-	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 0, &emul));
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x00, 0), &swap));
-	ut_assert(device_active(swap));
-	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 1, &emul));
+	vendor = 0;
+	ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
+	ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
-	ut_assert(device_active(swap));
+	device = 0;
+	ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
+	ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
 
 	/* Test bus#1 and its devices */
 	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
 
-	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 2, &emul));
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
-	ut_assert(device_active(swap));
-	ut_assertok(uclass_get_device(UCLASS_PCI_EMUL, 3, &emul));
+	vendor = 0;
+	ut_assertok(dm_pci_read_config16(swap, PCI_VENDOR_ID, &vendor));
+	ut_asserteq(SANDBOX_PCI_VENDOR_ID, vendor);
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
-	ut_assert(device_active(swap));
+	device = 0;
+	ut_assertok(dm_pci_read_config16(swap, PCI_DEVICE_ID, &device));
+	ut_asserteq(SANDBOX_PCI_DEVICE_ID, device);
 
 	return 0;
 }
Signature · 1 line
2.7.4
author
#22 lore raw

PATCH v2 11/21 pci: sandbox: emul: Fix the call to pci_bus_find_devfn()

In reply to Bin Meng

With the newly added test cases for PCI configuration access, we get:

=> ut dm pci_busdev
Test: dm_test_pci_busdev: pci.c
test/dm/pci.c:49, dm_test_pci_busdev(): SANDBOX_PCI_VENDOR_ID == vendor:
Expected 4660, got 65535
Test: dm_test_pci_busdev: pci.c (flat tree) test/dm/pci.c:49, dm_test_pci_busdev(): SANDBOX_PCI_VENDOR_ID == vendor:
Expected 4660, got 65535
Failures: 2

The bug only shows up when bus number is not equal to zero. This is caused by the plain find_devfn parameter is passed to function call pci_bus_find_devfn(), inside which find_devfn is compared to devfn in the device's pplat structure. However pplat->devfn does not carry the bus number. Fix this by passing find_devfn with bus number masked.

---

Changes in v2: None

 drivers/pci/pci-emul-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/pci-emul-uclass.c +1 -1
index 79e2c14..8570a5d 100644
@@ -21,7 +21,7 @@ int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
 	struct udevice *dev;
 	int ret;
 
-	ret = pci_bus_find_devfn(bus, find_devfn, &dev);
+	ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
 	if (ret) {
 		debug("%s: Could not find emulator for dev %x\n", __func__,
 		      find_devfn);
Signature · 1 line
2.7.4
author
#24 lore raw

PATCH v2 12/21 dm: pci: Assign correct driver data when binding a driver

In reply to Bin Meng

The correct driver data comes from the matching 'id' instead of 'find_id' in pci_find_and_bind_driver().

---

Changes in v2: None

 drivers/pci/pci-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/pci-uclass.c +1 -1
index 28ea8c5..5eb6841 100644
@@ -690,7 +690,7 @@ static int pci_find_and_bind_driver(struct udevice *parent,
 			if (ret)
 				goto error;
 			debug("%s: Match found: %s\n", __func__, drv->name);
-			dev->driver_data = find_id->driver_data;
+			dev->driver_data = id->driver_data;
 			*devp = dev;
 			return 0;
 		}
Signature · 1 line
2.7.4
author
#25 lore raw

PATCH v2 13/21 pci: sandbox: Support dynamically binding device driver

In reply to Bin Meng

At present all emulated sandbox pci devices must be present in the device tree in order to be used. The real world pci uclass driver supports pci device driver matching, and we should add such support on sandbox too.

---

Changes in v2: None

 doc/driver-model/pci-info.txt | 23 +++++++++++++
 drivers/pci/pci-emul-uclass.c | 14 +++++---
 drivers/pci/pci_sandbox.c     | 78 +++++++++++++++++++++++++++++++++++++++----
 include/pci.h                 |  3 +-
 4 files changed, 107 insertions(+), 11 deletions(-)
doc/driver-model/pci-info.txt +23 -0
index 52b4389..e67264b 100644
@@ -133,3 +133,26 @@ When this bus is scanned we will end up with something like this:
 
 When accesses go to the pci at 1f,0 device they are forwarded to its child, the
 emulator.
+
+The sandbox PCI drivers also support dynamic driver binding, allowing device
+driver to declare the driver binding information via U_BOOT_PCI_DEVICE(),
+eliminating the need to provide any device tree node under the host controller
+node. It is required a "sandbox,dev-info" property must be provided in the
+host controller node for this functionality to work.
+
+	pci1: pci-controller1 {
+		compatible = "sandbox,pci";
+		...
+		sandbox,dev-info = <0x08 0x00 0x1234 0x5678
+				    0x0c 0x00 0x1234 0x5678>;
+	};
+
+When this bus is scanned we will end up with something like this:
+
+ pci        [ + ]   pci_sandbo  |-- pci-controller1
+ pci_emul   [   ]   sandbox_sw  |   |-- sandbox_swap_case_emul
+ pci_emul   [   ]   sandbox_sw  |   `-- sandbox_swap_case_emul
+
+Note the difference from the statically declared device nodes is that the
+device is directly attached to the host controller, instead of via a container
+device like pci at 1f,0.
drivers/pci/pci-emul-uclass.c +10 -4
index 8570a5d..e9d2f49 100644
@@ -16,21 +16,27 @@ struct sandbox_pci_priv {
 };
 
 int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
-			 struct udevice **emulp)
+			 struct udevice **containerp, struct udevice **emulp)
 {
 	struct udevice *dev;
 	int ret;
 
+	*containerp = NULL;
 	ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
 	if (ret) {
 		debug("%s: Could not find emulator for dev %x\n", __func__,
 		      find_devfn);
 		return ret;
 	}
+	*containerp = dev;
 
-	ret = device_find_first_child(dev, emulp);
-	if (ret)
-		return ret;
+	if (device_get_uclass_id(dev) == UCLASS_PCI_GENERIC) {
+		ret = device_find_first_child(dev, emulp);
+		if (ret)
+			return ret;
+	} else {
+		*emulp = dev;
+	}
 
 	return *emulp ? 0 : -ENODEV;
 }
drivers/pci/pci_sandbox.c +72 -6

113 lines not on this page · raw message

include/pci.h +2 -1
index 427094c..c0ae5d1 100644
@@ -1441,11 +1441,12 @@ struct dm_pci_emul_ops {
  *
  * @bus:	PCI bus to search
  * @find_devfn:	PCI device and function address (PCI_DEVFN())
+ * @containerp:	Returns container device if found
  * @emulp:	Returns emulated device if found
  * @return 0 if found, -ENODEV if not found
  */
 int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
-			 struct udevice **emulp);
+			 struct udevice **containerp, struct udevice **emulp);
 
 #endif /* CONFIG_DM_PCI */
 
Signature · 1 line
2.7.4
author
#27 lore raw

PATCH v2 14/21 pci: sandbox: swap_case: Declare dynamic driver matching

In reply to Bin Meng

This adds a U_BOOT_PCI_DEVICE() declaration to the swap_case driver.

---

Changes in v2: None

 arch/sandbox/include/asm/test.h | 4 ++++
 drivers/misc/swap_case.c        | 7 +++++++
 2 files changed, 11 insertions(+)
arch/sandbox/include/asm/test.h +4 -0
index 08863bf..57aeca8 100644
@@ -16,6 +16,10 @@
 #define SANDBOX_PCI_CLASS_CODE		PCI_CLASS_CODE_COMM
 #define SANDBOX_PCI_CLASS_SUB_CODE	PCI_CLASS_SUB_CODE_COMM_SERIAL
 
+/* Useful for PCI_VDEVICE() macro */
+#define PCI_VENDOR_ID_SANDBOX		SANDBOX_PCI_VENDOR_ID
+#define SWAP_CASE_DRV_DATA		0x55aa
+
 #define SANDBOX_CLK_RATE		32768
 
 /* System controller driver data */
drivers/misc/swap_case.c +7 -0
index 80ccb9f..790bb0c 100644
@@ -285,3 +285,10 @@ U_BOOT_DRIVER(sandbox_swap_case_emul) = {
 	.priv_auto_alloc_size = sizeof(struct swap_case_priv),
 	.platdata_auto_alloc_size = sizeof(struct swap_case_platdata),
 };
+
+static struct pci_device_id sandbox_swap_case_supported[] = {
+	{ PCI_VDEVICE(SANDBOX, SANDBOX_PCI_DEVICE_ID), SWAP_CASE_DRV_DATA },
+	{},
+};
+
+U_BOOT_PCI_DEVICE(sandbox_swap_case_emul, sandbox_swap_case_supported);
Signature · 1 line
2.7.4
author
#29 lore raw

PATCH v2 15/21 sandbox: Update test.dts for dynamic PCI device driver matching

In reply to Bin Meng

---

Changes in v2: None

 arch/sandbox/dts/test.dts | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
arch/sandbox/dts/test.dts +2 -14
index 0bce6d0..44d24f9 100644
@@ -326,20 +326,8 @@
 		#size-cells = <2>;
 		ranges = <0x02000000 0 0x30000000 0x30000000 0 0x2000
 				0x01000000 0 0x40000000 0x40000000 0 0x2000>;
-		pci at 8,0 {
-			compatible = "pci-generic";
-			reg = <0x4000 0 0 0 0>;
-			emul at 8,0 {
-				compatible = "sandbox,swap-case";
-			};
-		};
-		pci at c,0 {
-			compatible = "pci-generic";
-			reg = <0x6000 0 0 0 0>;
-			emul at c,0 {
-				compatible = "sandbox,swap-case";
-			};
-		};
+		sandbox,dev-info = <0x08 0x00 0x1234 0x5678
+				    0x0c 0x00 0x1234 0x5678>;
 	};
 
 	probing {
Signature · 1 line
2.7.4
author
#32 lore raw

PATCH v2 16/21 test: dm: pci: Test driver binding with driver data provided

In reply to Bin Meng

With struct pci_device_id, it's possible to pass a driver data for bound driver to use. This adds a test case for this functionality.

---

Changes in v2: None

 test/dm/pci.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
test/dm/pci.c +17 -0
index 089b72e..53d2ca1 100644
@@ -108,3 +108,20 @@ static int dm_test_pci_swapcase(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_pci_swapcase, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test that we can dynamically bind the device driver correctly */
+static int dm_test_pci_drvdata(struct unit_test_state *uts)
+{
+	struct udevice *bus, *swap;
+
+	/* Check that asking for the device automatically fires up PCI */
+	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
+
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
+	ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x0c, 0), &swap));
+	ut_asserteq(SWAP_CASE_DRV_DATA, swap->driver_data);
+
+	return 0;
+}
+DM_TEST(dm_test_pci_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Signature · 1 line
2.7.4
author
#34 lore raw

PATCH v2 17/21 pci: sandbox: emul: Rename priv structure

In reply to Bin Meng

We have "struct sandbox_pci_priv" in pci_sandbox driver. To avoid confusion, rename the emul's priv to "struct sandbox_pci_emul_priv".

---

Changes in v2: None

 drivers/pci/pci-emul-uclass.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pci/pci-emul-uclass.c +4 -4
index e9d2f49..3822758 100644
@@ -11,7 +11,7 @@
 #include <pci.h>
 #include <dm/lists.h>
 
-struct sandbox_pci_priv {
+struct sandbox_pci_emul_priv {
 	int dev_count;
 };
 
@@ -43,7 +43,7 @@ int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
 
 static int sandbox_pci_emul_post_probe(struct udevice *dev)
 {
-	struct sandbox_pci_priv *priv = dev->uclass->priv;
+	struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
 
 	priv->dev_count++;
 	sandbox_set_enable_pci_map(true);
@@ -53,7 +53,7 @@ static int sandbox_pci_emul_post_probe(struct udevice *dev)
 
 static int sandbox_pci_emul_pre_remove(struct udevice *dev)
 {
-	struct sandbox_pci_priv *priv = dev->uclass->priv;
+	struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
 
 	priv->dev_count--;
 	sandbox_set_enable_pci_map(priv->dev_count > 0);
@@ -66,5 +66,5 @@ UCLASS_DRIVER(pci_emul) = {
 	.name		= "pci_emul",
 	.post_probe	= sandbox_pci_emul_post_probe,
 	.pre_remove	= sandbox_pci_emul_pre_remove,
-	.priv_auto_alloc_size	= sizeof(struct sandbox_pci_priv),
+	.priv_auto_alloc_size	= sizeof(struct sandbox_pci_emul_priv),
 };
Signature · 1 line
2.7.4
author
#36 lore raw

PATCH v2 18/21 test: dm: pci: Add tests for mixed static and dynamic devices on the same bus

In reply to Bin Meng

In the Sandbox test configuration, PCI bus#0 only has static devices while bus#1 only has dynamic devices. Create a bus#2 that has both types of devices and test such.

---

Changes in v2: None

 arch/sandbox/dts/test.dts | 18 ++++++++++++++
 test/dm/pci.c             | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
arch/sandbox/dts/test.dts +18 -0
index 44d24f9..7035646 100644
@@ -16,6 +16,7 @@
 		mmc1 = "/mmc1";
 		pci0 = &pci0;
 		pci1 = &pci1;
+		pci2 = &pci2;
 		remoteproc1 = &rproc_1;
 		remoteproc2 = &rproc_2;
 		rtc0 = &rtc_0;
@@ -330,6 +331,23 @@
 				    0x0c 0x00 0x1234 0x5678>;
 	};
 
+	pci2: pci-controller2 {
+		compatible = "sandbox,pci";
+		device_type = "pci";
+		#address-cells = <3>;
+		#size-cells = <2>;
+		ranges = <0x02000000 0 0x50000000 0x50000000 0 0x2000
+				0x01000000 0 0x60000000 0x60000000 0 0x2000>;
+		sandbox,dev-info = <0x08 0x00 0x1234 0x5678>;
+		pci at 1f,0 {
+			compatible = "pci-generic";
+			reg = <0xf800 0 0 0 0>;
+			emul at 1f,0 {
+				compatible = "sandbox,swap-case";
+			};
+		};
+	};
+
 	probing {
 		compatible = "simple-bus";
 		test1 {
test/dm/pci.c +63 -0

66 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#38 lore raw

PATCH v2 19/21 pci: Add all known capability and extended capability ids

In reply to Bin Meng

Currently we don't have a complete list of capability and extended capability ids. This adds them.

---

Changes in v2: None

 include/pci.h | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
include/pci.h +19 -1
index c0ae5d1..83a40a5 100644
@@ -318,7 +318,21 @@
 #define  PCI_CAP_ID_SLOTID	0x04	/* Slot Identification */
 #define  PCI_CAP_ID_MSI		0x05	/* Message Signalled Interrupts */
 #define  PCI_CAP_ID_CHSWP	0x06	/* CompactPCI HotSwap */
-#define  PCI_CAP_ID_EXP 	0x10	/* PCI Express */
+#define  PCI_CAP_ID_PCIX	0x07	/* PCI-X */
+#define  PCI_CAP_ID_HT		0x08	/* HyperTransport */
+#define  PCI_CAP_ID_VNDR	0x09	/* Vendor-Specific */
+#define  PCI_CAP_ID_DBG		0x0A	/* Debug port */
+#define  PCI_CAP_ID_CCRC	0x0B	/* CompactPCI Central Resource Control */
+#define  PCI_CAP_ID_SHPC	0x0C	/* PCI Standard Hot-Plug Controller */
+#define  PCI_CAP_ID_SSVID	0x0D	/* Bridge subsystem vendor/device ID */
+#define  PCI_CAP_ID_AGP3	0x0E	/* AGP Target PCI-PCI bridge */
+#define  PCI_CAP_ID_SECDEV	0x0F	/* Secure Device */
+#define  PCI_CAP_ID_EXP		0x10	/* PCI Express */
+#define  PCI_CAP_ID_MSIX	0x11	/* MSI-X */
+#define  PCI_CAP_ID_SATA	0x12	/* SATA Data/Index Conf. */
+#define  PCI_CAP_ID_AF		0x13	/* PCI Advanced Features */
+#define  PCI_CAP_ID_EA		0x14	/* PCI Enhanced Allocation */
+#define  PCI_CAP_ID_MAX		PCI_CAP_ID_EA
 #define PCI_CAP_LIST_NEXT	1	/* Next capability in the list */
 #define PCI_CAP_FLAGS		2	/* Capability defined flags (16 bits) */
 #define PCI_CAP_SIZEOF		4
@@ -434,6 +448,10 @@
 #define PCI_EXT_CAP_ID_SECPCI	0x19	/* Secondary PCIe Capability */
 #define PCI_EXT_CAP_ID_PMUX	0x1A	/* Protocol Multiplexing */
 #define PCI_EXT_CAP_ID_PASID	0x1B	/* Process Address Space ID */
+#define PCI_EXT_CAP_ID_DPC	0x1D	/* Downstream Port Containment */
+#define PCI_EXT_CAP_ID_L1SS	0x1E	/* L1 PM Substates */
+#define PCI_EXT_CAP_ID_PTM	0x1F	/* Precision Time Measurement */
+#define PCI_EXT_CAP_ID_MAX	PCI_EXT_CAP_ID_PTM
 
 /* Include the ID list */
 
Signature · 1 line
2.7.4
author
#39 lore raw

PATCH v2 20/21 dm: pci: Add APIs to find capability and extended capability

In reply to Bin Meng

This introduces two new APIs dm_pci_find_capability() and dm_pci_find_ext_capability() to get PCI capability address and PCI express extended capability address for a given PCI device.

---

Changes in v2:
- Mask header_type with 0x7f

 drivers/pci/pci-uclass.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/pci.h            | 46 ++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+)
drivers/pci/pci-uclass.c +68 -0
index 5eb6841..e9671d9 100644
@@ -1320,6 +1320,74 @@ void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
 	return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
 }
 
+int dm_pci_find_capability(struct udevice *dev, int cap)
+{
+	u16 status;
+	u8 header_type;
+	int ttl = PCI_FIND_CAP_TTL;
+	u8 id;
+	u16 ent;
+	u8 pos;
+
+	dm_pci_read_config16(dev, PCI_STATUS, &status);
+	if (!(status & PCI_STATUS_CAP_LIST))
+		return 0;
+
+	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
+	if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
+		pos = PCI_CB_CAPABILITY_LIST;
+	else
+		pos = PCI_CAPABILITY_LIST;
+
+	dm_pci_read_config8(dev, pos, &pos);
+	while (ttl--) {
+		if (pos < PCI_STD_HEADER_SIZEOF)
+			break;
+		pos &= ~3;
+		dm_pci_read_config16(dev, pos, &ent);
+
+		id = ent & 0xff;
+		if (id == 0xff)
+			break;
+		if (id == cap)
+			return pos;
+		pos = (ent >> 8);
+	}
+
+	return 0;
+}
+
+int dm_pci_find_ext_capability(struct udevice *dev, int cap)
+{
+	u32 header;
+	int ttl;
+	int pos = PCI_CFG_SPACE_SIZE;
+
+	/* minimum 8 bytes per capability */
+	ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
+
+	dm_pci_read_config32(dev, pos, &header);
+	/*
+	 * If we have no capabilities, this is indicated by cap ID,
+	 * cap version and next pointer all being 0.
+	 */
+	if (header == 0)
+		return 0;
+
+	while (ttl--) {
+		if (PCI_EXT_CAP_ID(header) == cap)
+			return pos;
+
+		pos = PCI_EXT_CAP_NEXT(header);
+		if (pos < PCI_CFG_SPACE_SIZE)
+			break;
+
+		dm_pci_read_config32(dev, pos, &header);
+	}
+
+	return 0;
+}
+
 UCLASS_DRIVER(pci) = {
 	.id		= UCLASS_PCI,
 	.name		= "pci",
include/pci.h +46 -0

58 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#41 lore raw

PATCH v2 21/21 test: dm: pci: Add cases for finding PCI capability APIs

In reply to Bin Meng

Add several PCI capability and extended capability ID registers in the swap_case driver, so that we can add test case for dm_pci_find_capability() and dm_pci_find_ext_capability().

---

Changes in v2: None

 arch/sandbox/include/asm/test.h |  8 ++++++++
 drivers/misc/swap_case.c        | 21 +++++++++++++++++++++
 test/dm/pci.c                   | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
arch/sandbox/include/asm/test.h +8 -0
index 57aeca8..c8ae52b 100644
@@ -16,6 +16,14 @@
 #define SANDBOX_PCI_CLASS_CODE		PCI_CLASS_CODE_COMM
 #define SANDBOX_PCI_CLASS_SUB_CODE	PCI_CLASS_SUB_CODE_COMM_SERIAL
 
+#define PCI_CAP_ID_PM_OFFSET		0x50
+#define PCI_CAP_ID_EXP_OFFSET		0x60
+#define PCI_CAP_ID_MSIX_OFFSET		0x70
+
+#define PCI_EXT_CAP_ID_ERR_OFFSET	0x100
+#define PCI_EXT_CAP_ID_VC_OFFSET	0x200
+#define PCI_EXT_CAP_ID_DSN_OFFSET	0x300
+
 /* Useful for PCI_VDEVICE() macro */
 #define PCI_VENDOR_ID_SANDBOX		SANDBOX_PCI_VENDOR_ID
 #define SWAP_CASE_DRV_DATA		0x55aa
drivers/misc/swap_case.c +21 -0
index 790bb0c..bffb809 100644
@@ -118,6 +118,27 @@ static int sandbox_swap_case_read_config(struct udevice *emul, uint offset,
 		*valuep = result;
 		break;
 	}
+	case PCI_CAPABILITY_LIST:
+		*valuep = PCI_CAP_ID_PM_OFFSET;
+		break;
+	case PCI_CAP_ID_PM_OFFSET:
+		*valuep = (PCI_CAP_ID_EXP_OFFSET << 8) | PCI_CAP_ID_PM;
+		break;
+	case PCI_CAP_ID_EXP_OFFSET:
+		*valuep = (PCI_CAP_ID_MSIX_OFFSET << 8) | PCI_CAP_ID_EXP;
+		break;
+	case PCI_CAP_ID_MSIX_OFFSET:
+		*valuep = PCI_CAP_ID_MSIX;
+		break;
+	case PCI_EXT_CAP_ID_ERR_OFFSET:
+		*valuep = (PCI_EXT_CAP_ID_VC_OFFSET << 20) | PCI_EXT_CAP_ID_ERR;
+		break;
+	case PCI_EXT_CAP_ID_VC_OFFSET:
+		*valuep = (PCI_EXT_CAP_ID_DSN_OFFSET << 20) | PCI_EXT_CAP_ID_VC;
+		break;
+	case PCI_EXT_CAP_ID_DSN_OFFSET:
+		*valuep = PCI_EXT_CAP_ID_DSN;
+		break;
 	}
 
 	return 0;
test/dm/pci.c +32 -0
index e03f6ba..8699700 100644
@@ -188,3 +188,35 @@ static int dm_test_pci_mixed(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_pci_mixed, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test looking up PCI capability and extended capability */
+static int dm_test_pci_cap(struct unit_test_state *uts)
+{
+	struct udevice *bus, *swap;
+	int cap;
+
+	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 0, &bus));
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &swap));
+
+	/* look up PCI_CAP_ID_EXP */
+	cap = dm_pci_find_capability(swap, PCI_CAP_ID_EXP);
+	ut_asserteq(PCI_CAP_ID_EXP_OFFSET, cap);
+
+	/* look up PCI_CAP_ID_PCIX */
+	cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX);
+	ut_asserteq(0, cap);
+
+	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
+
+	/* look up PCI_EXT_CAP_ID_DSN */
+	cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_DSN);
+	ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
+
+	/* look up PCI_EXT_CAP_ID_SRIOV */
+	cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV);
+	ut_asserteq(0, cap);
+
+	return 0;
+}
+DM_TEST(dm_test_pci_cap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Signature · 1 line
2.7.4
#8 lore raw

PATCH v2 4/21 dm: pci: Extract vendor/device id in child_post_bind()

In reply to Bin Meng
Quoted 11 lines · Bin Meng

Currently only devfn is extracted in child_post_bind(). Now that we have the live-tree version API to look up PCI vendor and device id from the compatible string, let's extract and save them too.

---

Changes in v2: None

 drivers/pci/pci-uclass.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
#10 lore raw

PATCH v2 5/21 dm: pci: Fix scanning multi-function device

In reply to Bin Meng
Quoted 11 lines · Bin Meng

The flag to control whether to scan multi-function device during enumeration should be cleared at the beginning of each iteration if the device's function number equals to zero.

---

Changes in v2: None

 drivers/pci/pci-uclass.c | 2 ++
 1 file changed, 2 insertions(+)

Oops!

#14 lore raw

PATCH v2 7/21 test: dm: pci: Test more than one device on the same bus

In reply to Bin Meng
Quoted 11 lines · Bin Meng

It's quite common to have more than one device on the same PCI bus. This updates the test case to test such scenario.

---

Changes in v2: None

 arch/sandbox/dts/test.dts |  7 +++++++
 test/dm/pci.c             | 37 +++++++++++++++++++++++++++++++++----
 2 files changed, 40 insertions(+), 4 deletions(-)
#16 lore raw

PATCH v2 8/21 pci: sandbox: swap_case: Preserve space indicator bit in BAR registers

In reply to Bin Meng
Quoted 17 lines · Bin Meng

With the newly added testing of more than one device, we get:

=> ut dm pci_swapcase
Test: dm_test_pci_swapcase: pci.c
test/dm/pci.c:88, dm_test_pci_swapcase(): "tHIS IS A tESt" = ptr:
Expected "tHIS IS A tESt", got "this is a test"
Test: dm_test_pci_swapcase: pci.c (flat tree) test/dm/pci.c:88, dm_test_pci_swapcase(): "tHIS IS A tESt" = ptr:
Expected "tHIS IS A tESt", got "this is a test"
Failures: 2

The failure only happens on the 2nd swap_case device on the PCI bus. The case passes on the 1st device.

It turns out the swap_case driver does not emulate bit#0 in BAR registers as a read-only bit. This corrects the implementation.

Quoted 7 lines

---

Changes in v2: None

 drivers/misc/swap_case.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
#18 lore raw

PATCH v2 9/21 test: dm: pci: Test more than one PCI host controller

In reply to Bin Meng
Quoted 7 lines · Bin Meng

So far there is only one PCI host controller in the sandbox test configuration. This is normally the case for x86, but it can be common on other architectures like ARM/PPC to have more than one PCI host controller in the system.

This updates the case to cover such scenario.

Quoted 8 lines

---

Changes in v2: None

 arch/sandbox/dts/test.dts | 28 ++++++++++++++++++++++++++--
 test/dm/pci.c             | 11 +++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)
#20 lore raw

PATCH v2 10/21 test: dm: pci: Add tests for configuration space access

In reply to Bin Meng

Hi Bin,

Quoted 1 line · Bin Meng

So far we missed the testing for PCI configuration space access.

You are also removing some asserts, but I suppose they are rendundant.

Quoted 9 lines

---

Changes in v2: None

 test/dm/pci.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

Regards,
Simon

#23 lore raw

PATCH v2 11/21 pci: sandbox: emul: Fix the call to pci_bus_find_devfn()

In reply to Bin Meng
Quoted 17 lines · Bin Meng

With the newly added test cases for PCI configuration access, we get:

=> ut dm pci_busdev
Test: dm_test_pci_busdev: pci.c
test/dm/pci.c:49, dm_test_pci_busdev(): SANDBOX_PCI_VENDOR_ID == vendor:
Expected 4660, got 65535
Test: dm_test_pci_busdev: pci.c (flat tree) test/dm/pci.c:49, dm_test_pci_busdev(): SANDBOX_PCI_VENDOR_ID == vendor:
Expected 4660, got 65535
Failures: 2

The bug only shows up when bus number is not equal to zero. This is caused by the plain find_devfn parameter is passed to function call pci_bus_find_devfn(), inside which find_devfn is compared to devfn in the device's pplat structure. However pplat->devfn does not carry the bus number. Fix this by passing find_devfn with bus number masked.

Quoted 7 lines

---

Changes in v2: None

 drivers/pci/pci-emul-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Nice to find this.

#26 lore raw

PATCH v2 13/21 pci: sandbox: Support dynamically binding device driver

In reply to Bin Meng
Quoted 8 lines · Bin Meng

At present all emulated sandbox pci devices must be present in the device tree in order to be used. The real world pci uclass driver supports pci device driver matching, and we should add such support on sandbox too.

---

Quoted 8 lines

Changes in v2: None

 doc/driver-model/pci-info.txt | 23 +++++++++++++
 drivers/pci/pci-emul-uclass.c | 14 +++++---
 drivers/pci/pci_sandbox.c     | 78 +++++++++++++++++++++++++++++++++++++++----
 include/pci.h                 |  3 +-
 4 files changed, 107 insertions(+), 11 deletions(-)
#30 lore raw

PATCH v2 15/21 sandbox: Update test.dts for dynamic PCI device driver matching

In reply to Bin Meng

Hi Bin,

Quoted 7 lines · Bin Meng

---

Changes in v2: None

 arch/sandbox/dts/test.dts | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)

But doesn't this mean we lose testing of the device-tree config? We need to keep that.

- Simon

#37 lore raw

PATCH v2 18/21 test: dm: pci: Add tests for mixed static and dynamic devices on the same bus

In reply to Bin Meng
Quoted 4 lines · Bin Meng

In the Sandbox test configuration, PCI bus#0 only has static devices while bus#1 only has dynamic devices. Create a bus#2 that has both types of devices and test such.

Quoted 8 lines

---

Changes in v2: None

 arch/sandbox/dts/test.dts | 18 ++++++++++++++
 test/dm/pci.c             | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
#40 lore raw

PATCH v2 20/21 dm: pci: Add APIs to find capability and extended capability

In reply to Bin Meng
Quoted 6 lines · Bin Meng

This introduces two new APIs dm_pci_find_capability() and dm_pci_find_ext_capability() to get PCI capability address and PCI express extended capability address for a given PCI device.

Quoted 8 lines

---

Changes in v2:
- Mask header_type with 0x7f

 drivers/pci/pci-uclass.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/pci.h            | 46 ++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+)
#42 lore raw

PATCH v2 21/21 test: dm: pci: Add cases for finding PCI capability APIs

In reply to Bin Meng
Quoted 6 lines · Bin Meng

Add several PCI capability and extended capability ID registers in the swap_case driver, so that we can add test case for dm_pci_find_capability() and dm_pci_find_ext_capability().

Quoted 9 lines

---

Changes in v2: None

 arch/sandbox/include/asm/test.h |  8 ++++++++
 drivers/misc/swap_case.c        | 21 +++++++++++++++++++++
 test/dm/pci.c                   | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
author
#21 lore raw

PATCH v2 10/21 test: dm: pci: Add tests for configuration space access

In reply to Simon Glass

Hi Simon,

Quoted 7 lines · Simon Glass

Hi Bin,

Quoted 1 line · Bin Meng

So far we missed the testing for PCI configuration space access.

You are also removing some asserts, but I suppose they are rendundant.

Correct.

Quoted 4 lines
Quoted 4 lines

---

Quoted 8 lines
Quoted 5 lines

Changes in v2: None

 test/dm/pci.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

Regards,
Bin

author
#31 lore raw

PATCH v2 15/21 sandbox: Update test.dts for dynamic PCI device driver matching

In reply to Simon Glass

Hi Simon,

Quoted 6 lines · Simon Glass

Hi Bin,

Quoted 3 lines · Bin Meng

---

Quoted 8 lines
Quoted 4 lines

Changes in v2: None

 arch/sandbox/dts/test.dts | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)

But doesn't this mean we lose testing of the device-tree config? We need to keep that.

The device-tree config testing is on bus 0. So there are 3 buses in the test configuration.

bus 0: 2 device-tree devices
bus 1: 2 dynamic devices
bus 2: 1 device-tree and 1 dynamic (mixed config)

Regards,
Bin