mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zijun Hu <zijun_hu@icloud.com>
To: Russell King <linux@armlinux.org.uk>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Saravana Kannan <saravanak@google.com>,
	 Isaac Manjarres <isaacmanjarres@google.com>,
	 Lu Baolu <baolu.lu@linux.intel.com>,
	Zijun Hu <zijun_hu@icloud.com>,
	 linux-kernel@vger.kernel.org, Zijun Hu <quic_zijuhu@quicinc.com>
Subject: [PATCH RFC 3/3] amba: bus: Move reading periphid operation from amba_match() to amba_probe()
Date: Mon, 09 Sep 2024 07:37:34 +0800	[thread overview]
Message-ID: <20240909-fix_amba-v1-3-4658eed26906@quicinc.com> (raw)
In-Reply-To: <20240909-fix_amba-v1-0-4658eed26906@quicinc.com>

From: Zijun Hu <quic_zijuhu@quicinc.com>

amba_match(), as bus_type @amba_bustype's match(), reads periphid from
hardware and may return -EPROBE_DEFER consequently, and it is the only
one that breaks below ideal rule in current kernel tree:

bus_type's match() should only return bool type compatible integer 0 or
1 ideally since its main operations are lookup and comparison normally.

fixed by moving reading periphid operation to amba_probe().

Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
---
 drivers/amba/bus.c       | 55 +++++++++++++++++++++++++++++-------------------
 include/linux/amba/bus.h |  1 -
 2 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
index 033d626aff46..8fe2e054b5ce 100644
--- a/drivers/amba/bus.c
+++ b/drivers/amba/bus.c
@@ -188,7 +188,7 @@ static int amba_read_periphid(struct amba_device *dev)
 	}
 
 	if (cid == AMBA_CID || cid == CORESIGHT_CID) {
-		dev->periphid = pid;
+		WRITE_ONCE(dev->periphid, pid);
 		dev->cid = cid;
 	}
 
@@ -246,24 +246,14 @@ static int amba_match(struct device *dev, const struct device_driver *drv)
 	struct amba_device *pcdev = to_amba_device(dev);
 	const struct amba_driver *pcdrv = to_amba_driver(drv);
 
-	mutex_lock(&pcdev->periphid_lock);
-	if (!pcdev->periphid) {
-		int ret = amba_read_periphid(pcdev);
-
-		/*
-		 * Returning any error other than -EPROBE_DEFER from bus match
-		 * can cause driver registration failure. So, if there's a
-		 * permanent failure in reading pid and cid, simply map it to
-		 * -EPROBE_DEFER.
-		 */
-		if (ret) {
-			mutex_unlock(&pcdev->periphid_lock);
-			return -EPROBE_DEFER;
-		}
-		dev_set_uevent_suppress(dev, false);
-		kobject_uevent(&dev->kobj, KOBJ_ADD);
-	}
-	mutex_unlock(&pcdev->periphid_lock);
+	/*
+	 * For an AMBA device without valid periphid, only read periphid
+	 * in amba_probe() for it when try to bind @amba_proxy_drv.
+	 * For @pcdev->periphid, Reading here has a little race with
+	 * writing in amba_probe().
+	 */
+	if (!READ_ONCE(pcdev->periphid))
+		return pcdrv == &amba_proxy_drv ? 1 : 0;
 
 	/* When driver_override is set, only bind to the matching driver */
 	if (pcdev->driver_override)
@@ -315,10 +305,24 @@ static int amba_probe(struct device *dev)
 {
 	struct amba_device *pcdev = to_amba_device(dev);
 	struct amba_driver *pcdrv = to_amba_driver(dev->driver);
-	const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
+	const struct amba_id *id;
 	int ret;
 
 	do {
+		if (!pcdev->periphid) {
+			ret = amba_read_periphid(pcdev);
+			if (ret) {
+				dev_err_probe(dev, ret, "failed to read periphid\n");
+			} else {
+				dev_set_uevent_suppress(dev, false);
+				kobject_uevent(&dev->kobj, KOBJ_ADD);
+			}
+
+			ret = -EPROBE_DEFER;
+			break;
+		}
+		id = amba_lookup(pcdrv->id_table, pcdev);
+
 		ret = of_amba_device_decode_irq(pcdev);
 		if (ret)
 			break;
@@ -389,10 +393,15 @@ static void amba_shutdown(struct device *dev)
 
 static int amba_dma_configure(struct device *dev)
 {
+	struct amba_device *pcdev = to_amba_device(dev);
 	struct amba_driver *drv = to_amba_driver(dev->driver);
 	enum dev_dma_attr attr;
 	int ret = 0;
 
+	/* To successfully go to amba_probe() to read periphid */
+	if (!pcdev->periphid)
+		return 0;
+
 	if (dev->of_node) {
 		ret = of_dma_configure(dev, dev->of_node, true);
 	} else if (has_acpi_companion(dev)) {
@@ -411,8 +420,12 @@ static int amba_dma_configure(struct device *dev)
 
 static void amba_dma_cleanup(struct device *dev)
 {
+	struct amba_device *pcdev = to_amba_device(dev);
 	struct amba_driver *drv = to_amba_driver(dev->driver);
 
+	if (!pcdev->periphid)
+		return;
+
 	if (!drv->driver_managed_dma)
 		iommu_device_unuse_default_domain(dev);
 }
@@ -535,7 +548,6 @@ static void amba_device_release(struct device *dev)
 	fwnode_handle_put(dev_fwnode(&d->dev));
 	if (d->res.parent)
 		release_resource(&d->res);
-	mutex_destroy(&d->periphid_lock);
 	kfree(d);
 }
 
@@ -593,7 +605,6 @@ static void amba_device_initialize(struct amba_device *dev, const char *name)
 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
 	dev->dev.dma_parms = &dev->dma_parms;
 	dev->res.name = dev_name(&dev->dev);
-	mutex_init(&dev->periphid_lock);
 }
 
 /**
diff --git a/include/linux/amba/bus.h b/include/linux/amba/bus.h
index 958a55bcc708..4bb3467d9c3d 100644
--- a/include/linux/amba/bus.h
+++ b/include/linux/amba/bus.h
@@ -67,7 +67,6 @@ struct amba_device {
 	struct clk		*pclk;
 	struct device_dma_parameters dma_parms;
 	unsigned int		periphid;
-	struct mutex		periphid_lock;
 	unsigned int		cid;
 	struct amba_cs_uci_id	uci;
 	unsigned int		irq[AMBA_NR_IRQS];

-- 
2.34.1


  parent reply	other threads:[~2024-09-08 23:38 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-08 23:37 [PATCH RFC 0/3] " Zijun Hu
2024-09-08 23:37 ` [PATCH RFC 1/3] amba: bus: Warn on adding an AMBA device without valid periphid Zijun Hu
2024-09-08 23:37 ` [PATCH RFC 2/3] amba: bus: Move empty @amba_proxy_drv's definition to the front Zijun Hu
2024-09-08 23:37 ` Zijun Hu [this message]
2024-09-08 23:51   ` [PATCH RFC 3/3] amba: bus: Move reading periphid operation from amba_match() to amba_probe() Zijun Hu
2024-09-09  7:24 ` [PATCH RFC 0/3] " Saravana Kannan
2024-09-10 12:17   ` Zijun Hu
2024-09-10 16:27     ` Saravana Kannan
2024-09-11 12:50       ` Zijun Hu
2024-10-03  1:49         ` Saravana Kannan
2024-10-10 13:10           ` Zijun Hu
2024-09-17  9:52 ` Russell King (Oracle)
2024-09-17 11:02   ` Zijun Hu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240909-fix_amba-v1-3-4658eed26906@quicinc.com \
    --to=zijun_hu@icloud.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=isaacmanjarres@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=quic_zijuhu@quicinc.com \
    --cc=saravanak@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®