[U-Boot] [PATCH 069/126] sandbox: pci: Remember the device being emulated

Simon Glass sjg at chromium.org
Wed Sep 25 14:56:53 UTC 2019


Add a field to the PCI emulator per-device data which records which device
is being emulated. This is useful when the emulator needs to check the
device for something.

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

 drivers/pci/pci-emul-uclass.c | 32 +++++++++++++++++++++++++-------
 include/pci.h                 | 20 ++++++++++++++++++++
 2 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/drivers/pci/pci-emul-uclass.c b/drivers/pci/pci-emul-uclass.c
index 6e6172836a4..589fb539031 100644
--- a/drivers/pci/pci-emul-uclass.c
+++ b/drivers/pci/pci-emul-uclass.c
@@ -18,6 +18,7 @@ struct sandbox_pci_emul_priv {
 int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
 			 struct udevice **containerp, struct udevice **emulp)
 {
+	struct pci_emul_uc_priv *upriv;
 	struct udevice *dev;
 	int ret;
 
@@ -30,17 +31,33 @@ int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
 	}
 	*containerp = dev;
 
-	/*
-	 * TODO(sjg at chromium.org): This code needs a comment as I'm not sure
-	 * why UCLASS_PCI_GENERIC devices end up being their own emulators. I
-	 * left this code as is.
-	 */
 	ret = uclass_get_device_by_phandle(UCLASS_PCI_EMUL, dev, "sandbox,emul",
 					   emulp);
-	if (ret && device_get_uclass_id(dev) != UCLASS_PCI_GENERIC)
+	if (!ret) {
+		upriv = dev_get_uclass_priv(*emulp);
+
+		upriv->client = dev;
+	} else if (device_get_uclass_id(dev) != UCLASS_PCI_GENERIC) {
+		/*
+		 * TODO(sjg at chromium.org): This code needs a comment as I'm not
+		 * sure why non-UCLASS_PCI_GENERIC devices end up being their
+		 * own emulators. I left this code as is.
+		 */
 		*emulp = dev;
+	}
+
+	return 0;
+}
 
-	return *emulp ? 0 : -ENODEV;
+int sandbox_pci_get_client(struct udevice *emul, struct udevice **devp)
+{
+	struct pci_emul_uc_priv *upriv = dev_get_uclass_priv(emul);
+
+	if (!upriv->client)
+		return -ENOENT;
+	*devp = upriv->client;
+
+	return 0;
 }
 
 uint sandbox_pci_read_bar(u32 barval, int type, uint size)
@@ -89,6 +106,7 @@ UCLASS_DRIVER(pci_emul) = {
 	.post_probe	= sandbox_pci_emul_post_probe,
 	.pre_remove	= sandbox_pci_emul_pre_remove,
 	.priv_auto_alloc_size	= sizeof(struct sandbox_pci_emul_priv),
+	.per_device_auto_alloc_size	= sizeof(struct pci_emul_uc_priv),
 };
 
 /*
diff --git a/include/pci.h b/include/pci.h
index 8aa6636cfbf..ff59ac0e695 100644
--- a/include/pci.h
+++ b/include/pci.h
@@ -1490,6 +1490,17 @@ int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
  */
 int dm_pci_find_class(uint find_class, int index, struct udevice **devp);
 
+/**
+ * struct pci_emul_uc_priv - holds info about an emulator device
+ *
+ * There is always at most one emulator per client
+ *
+ * @client: Client device if any, else NULL
+ */
+struct pci_emul_uc_priv {
+	struct udevice *client;
+};
+
 /**
  * struct dm_pci_emul_ops - PCI device emulator operations
  */
@@ -1592,6 +1603,15 @@ struct dm_pci_emul_ops {
 int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
 			 struct udevice **containerp, struct udevice **emulp);
 
+/**
+ * sandbox_pci_get_client() - Find the client for an emulation device
+ *
+ * @emul:	Emulation device to check
+ * @devp:	Returns the client device emulated by this device
+ * @return 0 if OK, -ENOENT if the device has no client yet
+ */
+int sandbox_pci_get_client(struct udevice *emul, struct udevice **devp);
+
 /**
  * pci_get_devfn() - Extract the devfn from fdt_pci_addr of the device
  *
-- 
2.23.0.444.g18eeb5a265-goog



More information about the U-Boot mailing list