Skip to contentSkip to conversation

ACPI / LPSS: set first resuming device as a 'proxy'

PATCH In review Andy Shevchenko 2 participants 5 messages Jan 15, 2015 – Jan 22, 2015 ACPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ACPI / LPSS: set first resuming device as a 'proxy'

We can't rely on the first enumerated device since it might be not probed yet when DMA driver wants to resume the device. This patch changes the place where we assign the 'proxy' device. From now on the first resumed device will be recognized as a 'proxy' because it is probed.

Fixes 6c17ee44d524 (ACPI / LPSS: introduce a 'proxy' device to power on LPSS for DMA)
---
 drivers/acpi/acpi_lpss.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/acpi/acpi_lpss.c +11 -6
index 4f3febf..d851290 100644
@@ -374,8 +374,6 @@ static int acpi_lpss_create_device(struct acpi_device *adev,
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
 	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
 		return 1;
 	}
 
@@ -615,10 +613,17 @@ static int acpi_lpss_runtime_resume(struct device *dev)
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
-		if (ret)
-			return ret;
+	if (!proxy_device && pdata->dev_desc->flags & LPSS_DEV_PROXY)
+		proxy_device = dev;
+
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		if (proxy_device) {
+			ret = pm_runtime_get_sync(proxy_device);
+			if (ret)
+				return ret;
+		} else {
+			return -EPROBE_DEFER;
+		}
 	}
 
 	ret = acpi_dev_runtime_resume(dev);
Signature · 1 line
2.1.4
#1 lore raw
Quoted 4 lines · Andy Shevchenko

We can't rely on the first enumerated device since it might be not probed yet when DMA driver wants to resume the device. This patch changes the place where we assign the 'proxy' device. From now on the first resumed device will be recognized as a 'proxy' because it is probed.

I was about to apply this, but I realized that we couldn't do it this way. ->

Quoted 21 lines
Fixes 6c17ee44d524 (ACPI / LPSS: introduce a 'proxy' device to power on LPSS for DMA)
---
 drivers/acpi/acpi_lpss.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/acpi/acpi_lpss.c +0 -2
index 4f3febf..d851290 100644
@@ -374,8 +374,6 @@ static int acpi_lpss_create_device(struct acpi_device *adev,
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
 	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
 		return 1;
 	}
 

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 617

Quoted 9 lines
drivers/acpi/acpi_lpss.c +2 -4
@@ -615,10 +613,17 @@ static int acpi_lpss_runtime_resume(struct device *dev)
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
-		if (ret)
-			return ret;
+	if (!proxy_device && pdata->dev_desc->flags & LPSS_DEV_PROXY)
+		proxy_device = dev;

-> OK, so what if the driver of proxy_device is removed later?

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 625

Quoted 8 lines
drivers/acpi/acpi_lpss.c +8 -0
+
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		if (proxy_device) {
+			ret = pm_runtime_get_sync(proxy_device);
+			if (ret)
+				return ret;
+		} else {
+			return -EPROBE_DEFER;

The error code returned here will disable runtime PM for dev forever, even if proxy_device becomes available at one point. You could return -EBUSY instead, but why don't we add BUS_NOTIFY_BOUND_DRIVER and BUS_NOTIFY_UNBIND_DRIVER handling to acpi_lpss_platform_notify() and set/unset proxy_device from there?

Quoted 5 lines
drivers/acpi/acpi_lpss.c +1 -0
+		}
 	}
 
 	ret = acpi_dev_runtime_resume(dev);
Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
#2 lore raw
Quoted 41 lines · Rafael J. Wysocki
Quoted 4 lines · Andy Shevchenko

We can't rely on the first enumerated device since it might be not probed yet when DMA driver wants to resume the device. This patch changes the place where we assign the 'proxy' device. From now on the first resumed device will be recognized as a 'proxy' because it is probed.

I was about to apply this, but I realized that we couldn't do it this way. ->

Quoted 21 lines
Fixes 6c17ee44d524 (ACPI / LPSS: introduce a 'proxy' device to power on LPSS for DMA)
---
 drivers/acpi/acpi_lpss.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/acpi/acpi_lpss.c +0 -2
index 4f3febf..d851290 100644
@@ -374,8 +374,6 @@ static int acpi_lpss_create_device(struct acpi_device *adev,
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
 	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
 		return 1;
 	}
 

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 617

Quoted 9 lines
drivers/acpi/acpi_lpss.c +2 -4
@@ -615,10 +613,17 @@ static int acpi_lpss_runtime_resume(struct device *dev)
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
-		if (ret)
-			return ret;
+	if (!proxy_device && pdata->dev_desc->flags & LPSS_DEV_PROXY)
+		proxy_device = dev;

-> OK, so what if the driver of proxy_device is removed later?

Quoted 14 lines

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 625

Quoted 8 lines
drivers/acpi/acpi_lpss.c +8 -0
+
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		if (proxy_device) {
+			ret = pm_runtime_get_sync(proxy_device);
+			if (ret)
+				return ret;
+		} else {
+			return -EPROBE_DEFER;

The error code returned here will disable runtime PM for dev forever, even if proxy_device becomes available at one point. You could return -EBUSY instead, but why don't we add BUS_NOTIFY_BOUND_DRIVER and BUS_NOTIFY_UNBIND_DRIVER handling to acpi_lpss_platform_notify() and set/unset proxy_device from there?

Well, that's more complicated, because we need to be careful when we switch proxies while the old one has been reference counted.

Maybe something like this (untested)?

---
 drivers/acpi/acpi_lpss.c |  103 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 90 insertions(+), 13 deletions(-)
drivers/acpi/acpi_lpss.c +90 -13
Index: linux-pm/drivers/acpi/acpi_lpss.c
===================================================================
@@ -72,12 +72,20 @@ struct lpss_device_desc {
 	void (*setup)(struct lpss_private_data *pdata);
 };
 
-static struct device *proxy_device;
-
 static struct lpss_device_desc lpss_dma_desc = {
 	.flags = LPSS_CLK | LPSS_PROXY_REQ,
 };
 
+struct lpss_proxy {
+	struct device *dev;
+	struct list_head node;
+};
+
+static struct lpss_proxy *current_proxy;
+static unsigned int current_proxy_count;
+static LIST_HEAD(proxy_list);
+static DEFINE_MUTEX(proxy_list_lock);
+
 struct lpss_private_data {
 	void __iomem *mmio_base;
 	resource_size_t mmio_size;
@@ -85,6 +93,7 @@ struct lpss_private_data {
 	struct clk *clk;
 	const struct lpss_device_desc *dev_desc;
 	u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
+	struct lpss_proxy proxy_entry;
 };
 
 /* UART Component Parameter Register */
@@ -373,11 +382,8 @@ static int acpi_lpss_create_device(struc
 
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
-	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
+	if (!IS_ERR_OR_NULL(pdev))
 		return 1;
-	}
 
 	ret = PTR_ERR(pdev);
 	adev->driver_data = NULL;
@@ -591,23 +597,41 @@ static int acpi_lpss_resume_early(struct
 static int acpi_lpss_runtime_suspend(struct device *dev)
 {
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
+	bool use_proxy;
 	int ret;
 
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		use_proxy = true;
+		mutex_lock(&proxy_list_lock);
+		if (!current_proxy) {
+			ret = -EBUSY;
+			goto out;
+		}
+	} else {
+		use_proxy = false;
+	}
+
 	ret = pm_generic_runtime_suspend(dev);
 	if (ret)
-		return ret;
+		goto out;
 
 	if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
 		acpi_lpss_save_ctx(dev, pdata);
 
 	ret = acpi_dev_runtime_suspend(dev);
 	if (ret)
-		return ret;
+		goto out;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device)
-		return pm_runtime_put_sync_suspend(proxy_device);
+	if (use_proxy) {
+		current_proxy_count--;
+		ret = pm_runtime_put_sync_suspend(current_proxy->dev);
+	}
 
-	return 0;
+ out:
+	if (use_proxy)
+		mutex_unlock(&proxy_list_lock);
+
+	return ret;
 }
 
 static int acpi_lpss_runtime_resume(struct device *dev)
@@ -615,8 +639,16 @@ static int acpi_lpss_runtime_resume(stru
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		mutex_lock(&proxy_list_lock);
+		if (current_proxy) {
+			ret = pm_runtime_get_sync(current_proxy->dev);
+			if (!ret)
+				current_proxy_count++;
+		} else {
+			ret = -EBUSY;
+		}
+		mutex_unlock(&proxy_list_lock);
 		if (ret)
 			return ret;
 	}
@@ -652,6 +684,28 @@ static struct dev_pm_domain acpi_lpss_pm
 	},
 };
 
+static void acpi_lpss_switch_proxy(void)
+{
+	struct lpss_private_data *lpd;
+	unsigned int count;
+	struct device *dev;
+
+	if (list_empty(&proxy_list)) {
+		current_proxy = NULL;
+		WARN_ON(current_proxy_count);
+		return;
+	}
+	count = current_proxy_count;
+	dev = current_proxy->dev;
+	lpd = list_first_entry(&proxy_list, struct lpss_private_data,
+			       proxy_entry.node);
+	current_proxy = &lpd->proxy_entry;
+	while (count--) {
+		pm_runtime_put_noidle(dev);
+		pm_runtime_get_sync(current_proxy->dev);
+	}
+}
+
 static int acpi_lpss_platform_notify(struct notifier_block *nb,
 				     unsigned long action, void *data)
 {
@@ -689,6 +743,29 @@ static int acpi_lpss_platform_notify(str
 			sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
 		pdev->dev.pm_domain = NULL;
 		break;
+	case BUS_NOTIFY_BOUND_DRIVER:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_list_lock);
+
+			INIT_LIST_HEAD(&pdata->proxy_entry.node);
+			pdata->proxy_entry.dev = &pdev->dev;
+			list_add_tail(&pdata->proxy_entry.node, &proxy_list);
+			if (!current_proxy)
+				current_proxy = &pdata->proxy_entry;
+
+			mutex_unlock(&proxy_list_lock);
+		}
+		break;
+	case BUS_NOTIFY_UNBIND_DRIVER:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_list_lock);
+
+			list_del(&pdata->proxy_entry.node);
+			acpi_lpss_switch_proxy();
+
+			mutex_unlock(&proxy_list_lock);
+		}
+		break;
 	default:
 		break;
 	}
#3 lore raw
Quoted 57 lines · Rafael J. Wysocki
Quoted 41 lines · Rafael J. Wysocki
Quoted 4 lines · Andy Shevchenko

We can't rely on the first enumerated device since it might be not probed yet when DMA driver wants to resume the device. This patch changes the place where we assign the 'proxy' device. From now on the first resumed device will be recognized as a 'proxy' because it is probed.

I was about to apply this, but I realized that we couldn't do it this way. ->

Quoted 21 lines
Fixes 6c17ee44d524 (ACPI / LPSS: introduce a 'proxy' device to power on LPSS for DMA)
---
 drivers/acpi/acpi_lpss.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/acpi/acpi_lpss.c +0 -2
index 4f3febf..d851290 100644
@@ -374,8 +374,6 @@ static int acpi_lpss_create_device(struct acpi_device *adev,
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
 	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
 		return 1;
 	}
 

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 617

Quoted 9 lines
drivers/acpi/acpi_lpss.c +2 -4
@@ -615,10 +613,17 @@ static int acpi_lpss_runtime_resume(struct device *dev)
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
-		if (ret)
-			return ret;
+	if (!proxy_device && pdata->dev_desc->flags & LPSS_DEV_PROXY)
+		proxy_device = dev;

-> OK, so what if the driver of proxy_device is removed later?

Quoted 14 lines

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 625

Quoted 8 lines
drivers/acpi/acpi_lpss.c +8 -0
+
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		if (proxy_device) {
+			ret = pm_runtime_get_sync(proxy_device);
+			if (ret)
+				return ret;
+		} else {
+			return -EPROBE_DEFER;

The error code returned here will disable runtime PM for dev forever, even if proxy_device becomes available at one point. You could return -EBUSY instead, but why don't we add BUS_NOTIFY_BOUND_DRIVER and BUS_NOTIFY_UNBIND_DRIVER handling to acpi_lpss_platform_notify() and set/unset proxy_device from there?

Quoted 4 lines

Well, that's more complicated, because we need to be careful when we switch proxies while the old one has been reference counted.

Maybe something like this (untested)?

That sill had a few problems.

The one below should be better (still untested, though). It assumes that the devices needing a proxy will never start as runtime-suspended and will always be resumed before removal.

---
 drivers/acpi/acpi_lpss.c |  126 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 113 insertions(+), 13 deletions(-)
drivers/acpi/acpi_lpss.c +113 -13
Index: linux-pm/drivers/acpi/acpi_lpss.c
===================================================================
@@ -72,12 +72,21 @@ struct lpss_device_desc {
 	void (*setup)(struct lpss_private_data *pdata);
 };
 
-static struct device *proxy_device;
-
 static struct lpss_device_desc lpss_dma_desc = {
 	.flags = LPSS_CLK | LPSS_PROXY_REQ,
 };
 
+struct lpss_proxy {
+	struct device *dev;
+	struct list_head node;
+};
+
+static struct lpss_proxy *current_proxy;
+static unsigned int proxy_count;
+static unsigned int proxy_suspend_count;
+static LIST_HEAD(proxy_list);
+static DEFINE_MUTEX(proxy_lock);
+
 struct lpss_private_data {
 	void __iomem *mmio_base;
 	resource_size_t mmio_size;
@@ -85,6 +94,7 @@ struct lpss_private_data {
 	struct clk *clk;
 	const struct lpss_device_desc *dev_desc;
 	u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
+	struct lpss_proxy proxy_entry;
 };
 
 /* UART Component Parameter Register */
@@ -373,11 +383,8 @@ static int acpi_lpss_create_device(struc
 
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
-	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
+	if (!IS_ERR_OR_NULL(pdev))
 		return 1;
-	}
 
 	ret = PTR_ERR(pdev);
 	adev->driver_data = NULL;
@@ -591,23 +598,41 @@ static int acpi_lpss_resume_early(struct
 static int acpi_lpss_runtime_suspend(struct device *dev)
 {
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
+	bool use_proxy;
 	int ret;
 
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		use_proxy = true;
+		mutex_lock(&proxy_lock);
+		if (!current_proxy) {
+			ret = -EBUSY;
+			goto out;
+		}
+	} else {
+		use_proxy = false;
+	}
+
 	ret = pm_generic_runtime_suspend(dev);
 	if (ret)
-		return ret;
+		goto out;
 
 	if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
 		acpi_lpss_save_ctx(dev, pdata);
 
 	ret = acpi_dev_runtime_suspend(dev);
 	if (ret)
-		return ret;
+		goto out;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device)
-		return pm_runtime_put_sync_suspend(proxy_device);
+	if (use_proxy) {
+		proxy_suspend_count++;
+		ret = pm_runtime_put_sync_suspend(current_proxy->dev);
+	}
 
-	return 0;
+ out:
+	if (use_proxy)
+		mutex_unlock(&proxy_lock);
+
+	return ret;
 }
 
 static int acpi_lpss_runtime_resume(struct device *dev)
@@ -615,8 +640,16 @@ static int acpi_lpss_runtime_resume(stru
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		mutex_lock(&proxy_lock);
+		if (current_proxy) {
+			ret = pm_runtime_get_sync(current_proxy->dev);
+			if (!ret)
+				proxy_suspend_count--;
+		} else {
+			ret = -EBUSY;
+		}
+		mutex_unlock(&proxy_lock);
 		if (ret)
 			return ret;
 	}
@@ -652,6 +685,29 @@ static struct dev_pm_domain acpi_lpss_pm
 	},
 };
 
+static void acpi_lpss_switch_proxy(void)
+{
+	struct device *dev = current_proxy ? current_proxy->dev : NULL;
+	unsigned int count = proxy_count - proxy_suspend_count;
+
+	if (list_empty(&proxy_list)) {
+		current_proxy = NULL;
+	} else {
+		struct lpss_private_data *lpd;
+
+		lpd = list_first_entry(&proxy_list, struct lpss_private_data,
+				       proxy_entry.node);
+		current_proxy = &lpd->proxy_entry;
+	}
+	while (count--) {
+		if (dev)
+			pm_runtime_put_noidle(dev);
+
+		if (current_proxy)
+			pm_runtime_get_sync(current_proxy->dev);
+	}
+}
+
 static int acpi_lpss_platform_notify(struct notifier_block *nb,
 				     unsigned long action, void *data)
 {
@@ -680,15 +736,59 @@ static int acpi_lpss_platform_notify(str
 	switch (action) {
 	case BUS_NOTIFY_ADD_DEVICE:
 		pdev->dev.pm_domain = &acpi_lpss_pm_domain;
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			/* This assimes the device to be powered up. */
+			proxy_count++;
+			if (current_proxy)
+				pm_runtime_get_sync(current_proxy->dev);
+
+			mutex_unlock(&proxy_lock);
+		}
 		if (pdata->dev_desc->flags & LPSS_LTR)
 			return sysfs_create_group(&pdev->dev.kobj,
 						  &lpss_attr_group);
 		break;
 	case BUS_NOTIFY_DEL_DEVICE:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			/* This assumes that the device is not suspended. */
+			proxy_count--;
+			if (current_proxy)
+				pm_runtime_put(current_proxy->dev);
+
+			mutex_unlock(&proxy_lock);
+		}
 		if (pdata->dev_desc->flags & LPSS_LTR)
 			sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
 		pdev->dev.pm_domain = NULL;
 		break;
+	case BUS_NOTIFY_BOUND_DRIVER:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			INIT_LIST_HEAD(&pdata->proxy_entry.node);
+			pdata->proxy_entry.dev = &pdev->dev;
+			list_add_tail(&pdata->proxy_entry.node, &proxy_list);
+			if (!current_proxy)
+				acpi_lpss_switch_proxy();
+
+			mutex_unlock(&proxy_lock);
+		}
+		break;
+	case BUS_NOTIFY_UNBIND_DRIVER:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			list_del(&pdata->proxy_entry.node);
+			if (current_proxy == &pdata->proxy_entry)
+				acpi_lpss_switch_proxy();
+
+			mutex_unlock(&proxy_lock);
+		}
+		break;
 	default:
 		break;
 	}
#4 lore raw

+ Jerome Blin

Jerome, this is new version of patch from Rafael that I mentioned in LCK-1599. Please take it instead.

Quoted 279 lines · Rafael J. Wysocki
Quoted 57 lines · Rafael J. Wysocki
Quoted 41 lines · Rafael J. Wysocki
Quoted 4 lines · Andy Shevchenko

We can't rely on the first enumerated device since it might be not probed yet when DMA driver wants to resume the device. This patch changes the place where we assign the 'proxy' device. From now on the first resumed device will be recognized as a 'proxy' because it is probed.

I was about to apply this, but I realized that we couldn't do it this way. ->

Quoted 21 lines
Fixes 6c17ee44d524 (ACPI / LPSS: introduce a 'proxy' device to power on LPSS for DMA)
---
 drivers/acpi/acpi_lpss.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/acpi/acpi_lpss.c +0 -2
index 4f3febf..d851290 100644
@@ -374,8 +374,6 @@ static int acpi_lpss_create_device(struct acpi_device *adev,
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
 	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
 		return 1;
 	}
 

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 617

Quoted 9 lines
drivers/acpi/acpi_lpss.c +2 -4
@@ -615,10 +613,17 @@ static int acpi_lpss_runtime_resume(struct device *dev)
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
-		if (ret)
-			return ret;
+	if (!proxy_device && pdata->dev_desc->flags & LPSS_DEV_PROXY)
+		proxy_device = dev;

-> OK, so what if the driver of proxy_device is removed later?

Quoted 14 lines

drivers/acpi/acpi_lpss.c static int acpi_lpss_runtime_resume(struct device *dev) · line 625

Quoted 8 lines
drivers/acpi/acpi_lpss.c +8 -0
+
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		if (proxy_device) {
+			ret = pm_runtime_get_sync(proxy_device);
+			if (ret)
+				return ret;
+		} else {
+			return -EPROBE_DEFER;

The error code returned here will disable runtime PM for dev forever, even if proxy_device becomes available at one point. You could return -EBUSY instead, but why don't we add BUS_NOTIFY_BOUND_DRIVER and BUS_NOTIFY_UNBIND_DRIVER handling to acpi_lpss_platform_notify() and set/unset proxy_device from there?

Quoted 4 lines

Well, that's more complicated, because we need to be careful when we switch proxies while the old one has been reference counted.

Maybe something like this (untested)?

That sill had a few problems.

The one below should be better (still untested, though). It assumes that the devices needing a proxy will never start as runtime-suspended and will always be resumed before removal.

---
 drivers/acpi/acpi_lpss.c |  126 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 113 insertions(+), 13 deletions(-)

Index: linux-pm/drivers/acpi/acpi_lpss.c
===================================================================

drivers/acpi/acpi_lpss.c +113 -13
@@ -72,12 +72,21 @@ struct lpss_device_desc {
 	void (*setup)(struct lpss_private_data *pdata);
 };
 
-static struct device *proxy_device;
-
 static struct lpss_device_desc lpss_dma_desc = {
 	.flags = LPSS_CLK | LPSS_PROXY_REQ,
 };
 
+struct lpss_proxy {
+	struct device *dev;
+	struct list_head node;
+};
+
+static struct lpss_proxy *current_proxy;
+static unsigned int proxy_count;
+static unsigned int proxy_suspend_count;
+static LIST_HEAD(proxy_list);
+static DEFINE_MUTEX(proxy_lock);
+
 struct lpss_private_data {
 	void __iomem *mmio_base;
 	resource_size_t mmio_size;
@@ -85,6 +94,7 @@ struct lpss_private_data {
 	struct clk *clk;
 	const struct lpss_device_desc *dev_desc;
 	u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
+	struct lpss_proxy proxy_entry;
 };
 
 /* UART Component Parameter Register */
@@ -373,11 +383,8 @@ static int acpi_lpss_create_device(struc
 
 	adev->driver_data = pdata;
 	pdev = acpi_create_platform_device(adev);
-	if (!IS_ERR_OR_NULL(pdev)) {
-		if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
-			proxy_device = &pdev->dev;
+	if (!IS_ERR_OR_NULL(pdev))
 		return 1;
-	}
 
 	ret = PTR_ERR(pdev);
 	adev->driver_data = NULL;
@@ -591,23 +598,41 @@ static int acpi_lpss_resume_early(struct
 static int acpi_lpss_runtime_suspend(struct device *dev)
 {
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
+	bool use_proxy;
 	int ret;
 
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		use_proxy = true;
+		mutex_lock(&proxy_lock);
+		if (!current_proxy) {
+			ret = -EBUSY;
+			goto out;
+		}
+	} else {
+		use_proxy = false;
+	}
+
 	ret = pm_generic_runtime_suspend(dev);
 	if (ret)
-		return ret;
+		goto out;
 
 	if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
 		acpi_lpss_save_ctx(dev, pdata);
 
 	ret = acpi_dev_runtime_suspend(dev);
 	if (ret)
-		return ret;
+		goto out;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device)
-		return pm_runtime_put_sync_suspend(proxy_device);
+	if (use_proxy) {
+		proxy_suspend_count++;
+		ret = pm_runtime_put_sync_suspend(current_proxy->dev);
+	}
 
-	return 0;
+ out:
+	if (use_proxy)
+		mutex_unlock(&proxy_lock);
+
+	return ret;
 }
 
 static int acpi_lpss_runtime_resume(struct device *dev)
@@ -615,8 +640,16 @@ static int acpi_lpss_runtime_resume(stru
 	struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
 	int ret;
 
-	if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
-		ret = pm_runtime_get_sync(proxy_device);
+	if (pdata->dev_desc->flags & LPSS_PROXY_REQ) {
+		mutex_lock(&proxy_lock);
+		if (current_proxy) {
+			ret = pm_runtime_get_sync(current_proxy->dev);
+			if (!ret)
+				proxy_suspend_count--;
+		} else {
+			ret = -EBUSY;
+		}
+		mutex_unlock(&proxy_lock);
 		if (ret)
 			return ret;
 	}
@@ -652,6 +685,29 @@ static struct dev_pm_domain acpi_lpss_pm
 	},
 };
 
+static void acpi_lpss_switch_proxy(void)
+{
+	struct device *dev = current_proxy ? current_proxy->dev : NULL;
+	unsigned int count = proxy_count - proxy_suspend_count;
+
+	if (list_empty(&proxy_list)) {
+		current_proxy = NULL;
+	} else {
+		struct lpss_private_data *lpd;
+
+		lpd = list_first_entry(&proxy_list, struct lpss_private_data,
+				       proxy_entry.node);
+		current_proxy = &lpd->proxy_entry;
+	}
+	while (count--) {
+		if (dev)
+			pm_runtime_put_noidle(dev);
+
+		if (current_proxy)
+			pm_runtime_get_sync(current_proxy->dev);
+	}
+}
+
 static int acpi_lpss_platform_notify(struct notifier_block *nb,
 				     unsigned long action, void *data)
 {
@@ -680,15 +736,59 @@ static int acpi_lpss_platform_notify(str
 	switch (action) {
 	case BUS_NOTIFY_ADD_DEVICE:
 		pdev->dev.pm_domain = &acpi_lpss_pm_domain;
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			/* This assimes the device to be powered up. */
+			proxy_count++;
+			if (current_proxy)
+				pm_runtime_get_sync(current_proxy->dev);
+
+			mutex_unlock(&proxy_lock);
+		}
 		if (pdata->dev_desc->flags & LPSS_LTR)
 			return sysfs_create_group(&pdev->dev.kobj,
 						  &lpss_attr_group);
 		break;
 	case BUS_NOTIFY_DEL_DEVICE:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			/* This assumes that the device is not suspended. */
+			proxy_count--;
+			if (current_proxy)
+				pm_runtime_put(current_proxy->dev);
+
+			mutex_unlock(&proxy_lock);
+		}
 		if (pdata->dev_desc->flags & LPSS_LTR)
 			sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
 		pdev->dev.pm_domain = NULL;
 		break;
+	case BUS_NOTIFY_BOUND_DRIVER:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			INIT_LIST_HEAD(&pdata->proxy_entry.node);
+			pdata->proxy_entry.dev = &pdev->dev;
+			list_add_tail(&pdata->proxy_entry.node, &proxy_list);
+			if (!current_proxy)
+				acpi_lpss_switch_proxy();
+
+			mutex_unlock(&proxy_lock);
+		}
+		break;
+	case BUS_NOTIFY_UNBIND_DRIVER:
+		if (pdata->dev_desc->flags & LPSS_DEV_PROXY) {
+			mutex_lock(&proxy_lock);
+
+			list_del(&pdata->proxy_entry.node);
+			if (current_proxy == &pdata->proxy_entry)
+				acpi_lpss_switch_proxy();
+
+			mutex_unlock(&proxy_lock);
+		}
+		break;
 	default:
 		break;
 	}
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy