diff mbox series

[v8,33/35] pci: Avoid a crash in device_is_on_pci_bus()

Message ID 20200426152618.125646-25-sjg@chromium.org
State Superseded
Headers show
Series dm: Add programmatic generation of ACPI tables (part B) | expand

Commit Message

Simon Glass April 26, 2020, 3:26 p.m. UTC
This function cannot currently be called on the root node. Add a check
for this as well as a test.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v8: None
Changes in v3: None
Changes in v2: None

 include/dm/device.h |  2 +-
 test/dm/pci.c       | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/include/dm/device.h b/include/dm/device.h
index 975eec5d0e..6a41ee5500 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -742,7 +742,7 @@  int dev_enable_by_path(const char *path);
  */
 static inline bool device_is_on_pci_bus(const struct udevice *dev)
 {
-	return device_get_uclass_id(dev->parent) == UCLASS_PCI;
+	return dev->parent && device_get_uclass_id(dev->parent) == UCLASS_PCI;
 }
 
 /**
diff --git a/test/dm/pci.c b/test/dm/pci.c
index fb93e4c78a..39e82b3699 100644
--- a/test/dm/pci.c
+++ b/test/dm/pci.c
@@ -339,3 +339,17 @@  static int dm_test_pci_addr_live(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_pci_addr_live, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT |
 		DM_TESTF_LIVE_TREE);
+
+/* Test device_is_on_pci_bus() */
+static int dm_test_pci_on_bus(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+
+	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), &dev));
+	ut_asserteq(true, device_is_on_pci_bus(dev));
+	ut_asserteq(false, device_is_on_pci_bus(dev_get_parent(dev)));
+	ut_asserteq(true, device_is_on_pci_bus(dev));
+
+	return 0;
+}
+DM_TEST(dm_test_pci_on_bus, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);