diff mbox series

[4/7] coresight: tpiu: Move ACPI support from AMBA driver to platform driver

Message ID 20231027072943.3418997-5-anshuman.khandual@arm.com (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series Subject: coresight: Move remaining AMBA ACPI devices into platform driver | expand

Commit Message

Anshuman Khandual Oct. 27, 2023, 7:29 a.m. UTC
Add support for the tpiu device in the platform driver, which can then be
used on ACPI based platforms. This change would now allow runtime power
management for ACPI based systems. The driver would try to enable the APB
clock if available.

Cc: Lorenzo Pieralisi <lpieralisi@kernel.org>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: James Clark <james.clark@arm.com>
Cc: linux-acpi@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Cc: coresight@lists.linaro.org
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 drivers/acpi/arm64/amba.c                    |  1 -
 drivers/hwtracing/coresight/coresight-tpiu.c | 76 ++++++++++++++++++--
 2 files changed, 69 insertions(+), 8 deletions(-)

Comments

James Clark Nov. 15, 2023, 1:53 p.m. UTC | #1
On 27/10/2023 08:29, Anshuman Khandual wrote:
> Add support for the tpiu device in the platform driver, which can then be
> used on ACPI based platforms. This change would now allow runtime power
> management for ACPI based systems. The driver would try to enable the APB
> clock if available.
> 
[...]
> +#ifdef CONFIG_ACPI
> +static const struct acpi_device_id tpiu_acpi_ids[] = {
> +	{"ARMHC979", 0}, /* ARM CoreSight TPIU */
> +	{}
> +};
> +MODULE_DEVICE_TABLE(acpi, tpiu_acpi_ids);
> +#endif
> +
> +static struct platform_driver tpiu_platform_driver = {
> +	.probe	= tpiu_platform_probe,
> +	.remove	= tpiu_platform_remove,
> +	.driver = {
> +		.name			= "coresight-tpiu-platform",
> +		.acpi_match_table	= ACPI_PTR(tpiu_acpi_ids),
> +		.suppress_bind_attrs	= true,
> +		.pm			= &tpiu_dev_pm_ops,
> +	},
> +};
> +module_platform_driver(tpiu_platform_driver);
> +

Is there a special build config where this works? I get an error here
because module_platform_driver() redefines things that are in
module_amba_driver() which is defined above:

  module_amba_driver(tpiu_driver);

This isn't a W=1 build or anything, just a normal one. And it applies to
most of the patches in this set.
Anshuman Khandual Nov. 22, 2023, 7:02 a.m. UTC | #2
On 11/15/23 19:23, James Clark wrote:
> 
> 
> On 27/10/2023 08:29, Anshuman Khandual wrote:
>> Add support for the tpiu device in the platform driver, which can then be
>> used on ACPI based platforms. This change would now allow runtime power
>> management for ACPI based systems. The driver would try to enable the APB
>> clock if available.
>>
> [...]
>> +#ifdef CONFIG_ACPI
>> +static const struct acpi_device_id tpiu_acpi_ids[] = {
>> +	{"ARMHC979", 0}, /* ARM CoreSight TPIU */
>> +	{}
>> +};
>> +MODULE_DEVICE_TABLE(acpi, tpiu_acpi_ids);
>> +#endif
>> +
>> +static struct platform_driver tpiu_platform_driver = {
>> +	.probe	= tpiu_platform_probe,
>> +	.remove	= tpiu_platform_remove,
>> +	.driver = {
>> +		.name			= "coresight-tpiu-platform",
>> +		.acpi_match_table	= ACPI_PTR(tpiu_acpi_ids),
>> +		.suppress_bind_attrs	= true,
>> +		.pm			= &tpiu_dev_pm_ops,
>> +	},
>> +};
>> +module_platform_driver(tpiu_platform_driver);
>> +
> 
> Is there a special build config where this works? I get an error here

I have been testing this with a config known to work on RB5 board.

> because module_platform_driver() redefines things that are in
> module_amba_driver() which is defined above:
> 
>   module_amba_driver(tpiu_driver);
> 
> This isn't a W=1 build or anything, just a normal one. And it applies to
> most of the patches in this set.

You are right, I am able to recreate this problem with defconfig on
6.7-rc2 as well. The problem here seems to be caused by having both
module_amba_driver() and module_platform_driver() in the same file.

#define module_amba_driver(__amba_drv) \
        module_driver(__amba_drv, amba_driver_register, amba_driver_unregister)

#define module_platform_driver(__platform_driver) \
        module_driver(__platform_driver, platform_driver_register, \
                        platform_driver_unregister)

Although, AFAICT, have not seen these before - even on the defconfig.
Just to work around this problem, there can be a common module_init()
/module_exit() to register/unregister both AMBA and platform drivers,
similar to etm4x_init()/etm4x_exit() setup in coresight-etm4x-core.c.
Sudeep Holla Nov. 22, 2023, 11:21 a.m. UTC | #3
On Wed, Nov 22, 2023 at 12:32:33PM +0530, Anshuman Khandual wrote:
> On 11/15/23 19:23, James Clark wrote:
> > On 27/10/2023 08:29, Anshuman Khandual wrote:
> >> Add support for the tpiu device in the platform driver, which can then be
> >> used on ACPI based platforms. This change would now allow runtime power
> >> management for ACPI based systems. The driver would try to enable the APB
> >> clock if available.
> >>
> > [...]
> >> +#ifdef CONFIG_ACPI
> >> +static const struct acpi_device_id tpiu_acpi_ids[] = {
> >> +	{"ARMHC979", 0}, /* ARM CoreSight TPIU */
> >> +	{}
> >> +};
> >> +MODULE_DEVICE_TABLE(acpi, tpiu_acpi_ids);
> >> +#endif
> >> +
> >> +static struct platform_driver tpiu_platform_driver = {
> >> +	.probe	= tpiu_platform_probe,
> >> +	.remove	= tpiu_platform_remove,
> >> +	.driver = {
> >> +		.name			= "coresight-tpiu-platform",
> >> +		.acpi_match_table	= ACPI_PTR(tpiu_acpi_ids),
> >> +		.suppress_bind_attrs	= true,
> >> +		.pm			= &tpiu_dev_pm_ops,
> >> +	},
> >> +};
> >> +module_platform_driver(tpiu_platform_driver);
> >> +
> > 
> > Is there a special build config where this works? I get an error here
> 
> I have been testing this with a config known to work on RB5 board.
> 
> > because module_platform_driver() redefines things that are in
> > module_amba_driver() which is defined above:
> > 
> >   module_amba_driver(tpiu_driver);
> > 
> > This isn't a W=1 build or anything, just a normal one. And it applies to
> > most of the patches in this set.
> 
> You are right, I am able to recreate this problem with defconfig on
> 6.7-rc2 as well. The problem here seems to be caused by having both
> module_amba_driver() and module_platform_driver() in the same file.
> 
> #define module_amba_driver(__amba_drv) \
>         module_driver(__amba_drv, amba_driver_register, amba_driver_unregister)
> 
> #define module_platform_driver(__platform_driver) \
>         module_driver(__platform_driver, platform_driver_register, \
>                         platform_driver_unregister)
> 
> Although, AFAICT, have not seen these before - even on the defconfig.
> Just to work around this problem, there can be a common module_init()
> /module_exit() to register/unregister both AMBA and platform drivers,
> similar to etm4x_init()/etm4x_exit() setup in coresight-etm4x-core.c.

Could this be the reason why I am seeing the below error why booting with
ACPI ? I wanted to check the tables before I comment but this discussion
made me think it could be the reason, hence posting this before I got time
to analyse it.

  |  coresight-tmc-platform ARMHC97C:00: can't request region for resource [mem 0x20010000-0x20010fff]
  |  coresight-tmc-platform: probe of ARMHC97C:00 failed with error -16
  |  coresight-tmc-platform ARMHC501:00: can't request region for resource [mem 0x20070000-0x20070fff]
  |  coresight-tmc-platform: probe of ARMHC501:00 failed with error -16

--
Regards,
Sudeep
Sudeep Holla Nov. 22, 2023, 12:10 p.m. UTC | #4
On Wed, Nov 22, 2023 at 11:21:58AM +0000, Sudeep Holla wrote:
> On Wed, Nov 22, 2023 at 12:32:33PM +0530, Anshuman Khandual wrote:
[..]

> > Although, AFAICT, have not seen these before - even on the defconfig.
> > Just to work around this problem, there can be a common module_init()
> > /module_exit() to register/unregister both AMBA and platform drivers,
> > similar to etm4x_init()/etm4x_exit() setup in coresight-etm4x-core.c.
> 
> Could this be the reason why I am seeing the below error why booting with
> ACPI ? I wanted to check the tables before I comment but this discussion
> made me think it could be the reason, hence posting this before I got time
> to analyse it.
> 
>   |  coresight-tmc-platform ARMHC97C:00: can't request region for resource [mem 0x20010000-0x20010fff]
>   |  coresight-tmc-platform: probe of ARMHC97C:00 failed with error -16
>   |  coresight-tmc-platform ARMHC501:00: can't request region for resource [mem 0x20070000-0x20070fff]
>   |  coresight-tmc-platform: probe of ARMHC501:00 failed with error -16
> 

Scratch that, it didn't help. This error I am seeing is not related to
the issue reported here. I tried a hack below, it didn't help.
 
Regards,
Sudeep

-->8
diff --git i/drivers/hwtracing/coresight/coresight-tpiu.c w/drivers/hwtracing/coresight/coresight-tpiu.c
index ea8827d289ca..f6ba350b3777 100644
--- i/drivers/hwtracing/coresight/coresight-tpiu.c
+++ w/drivers/hwtracing/coresight/coresight-tpiu.c
@@ -254,8 +254,6 @@ static struct amba_driver tpiu_driver = {
        .id_table       = tpiu_ids,
 };

-module_amba_driver(tpiu_driver);
-
 static int tpiu_platform_probe(struct platform_device *pdev)
 {
        struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -297,7 +295,21 @@ static struct platform_driver tpiu_platform_driver = {
                .pm                     = &tpiu_dev_pm_ops,
        },
 };
-module_platform_driver(tpiu_platform_driver);
+
+static int __init tpiu_init(void)
+{
+       amba_driver_register(&tpiu_driver);
+       platform_driver_register(&tpiu_platform_driver);
+       return 0;
+}
+
+static void __exit tpiu_exit(void)
+{
+       amba_driver_unregister(&tpiu_driver);
+       platform_driver_unregister(&tpiu_platform_driver);
+}
+module_init(tpiu_init);
+module_exit(tpiu_exit);

 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
diff mbox series

Patch

diff --git a/drivers/acpi/arm64/amba.c b/drivers/acpi/arm64/amba.c
index 56a7e020555b..8e1783166c33 100644
--- a/drivers/acpi/arm64/amba.c
+++ b/drivers/acpi/arm64/amba.c
@@ -25,7 +25,6 @@  static const struct acpi_device_id amba_id_list[] = {
 	{"ARMHC501", 0}, /* ARM CoreSight ETR */
 	{"ARMHC502", 0}, /* ARM CoreSight STM */
 	{"ARMHC503", 0}, /* ARM CoreSight Debug */
-	{"ARMHC979", 0}, /* ARM CoreSight TPIU */
 	{"ARMHC97C", 0}, /* ARM CoreSight SoC-400 TMC, SoC-600 ETF/ETB */
 	{"", 0},
 };
diff --git a/drivers/hwtracing/coresight/coresight-tpiu.c b/drivers/hwtracing/coresight/coresight-tpiu.c
index 59eac93fd6bb..ea8827d289ca 100644
--- a/drivers/hwtracing/coresight/coresight-tpiu.c
+++ b/drivers/hwtracing/coresight/coresight-tpiu.c
@@ -5,6 +5,8 @@ 
  * Description: CoreSight Trace Port Interface Unit driver
  */
 
+#include <linux/platform_device.h>
+#include <linux/acpi.h>
 #include <linux/atomic.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
@@ -57,6 +59,7 @@  DEFINE_CORESIGHT_DEVLIST(tpiu_devs, "tpiu");
 struct tpiu_drvdata {
 	void __iomem		*base;
 	struct clk		*atclk;
+	struct clk		*pclk;
 	struct coresight_device	*csdev;
 };
 
@@ -114,14 +117,12 @@  static const struct coresight_ops tpiu_cs_ops = {
 	.sink_ops	= &tpiu_sink_ops,
 };
 
-static int tpiu_probe(struct amba_device *adev, const struct amba_id *id)
+static int __tpiu_probe(struct device *dev, struct resource *res)
 {
 	int ret;
 	void __iomem *base;
-	struct device *dev = &adev->dev;
 	struct coresight_platform_data *pdata = NULL;
 	struct tpiu_drvdata *drvdata;
-	struct resource *res = &adev->res;
 	struct coresight_desc desc = { 0 };
 
 	desc.name = coresight_alloc_device_name(&tpiu_devs, dev);
@@ -132,12 +133,16 @@  static int tpiu_probe(struct amba_device *adev, const struct amba_id *id)
 	if (!drvdata)
 		return -ENOMEM;
 
-	drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
+	drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
 	if (!IS_ERR(drvdata->atclk)) {
 		ret = clk_prepare_enable(drvdata->atclk);
 		if (ret)
 			return ret;
 	}
+
+	drvdata->pclk = coresight_get_enable_apb_pclk(dev);
+	if (IS_ERR(drvdata->pclk))
+		return -ENODEV;
 	dev_set_drvdata(dev, drvdata);
 
 	/* Validity for the resource is already checked by the AMBA core */
@@ -164,20 +169,30 @@  static int tpiu_probe(struct amba_device *adev, const struct amba_id *id)
 	drvdata->csdev = coresight_register(&desc);
 
 	if (!IS_ERR(drvdata->csdev)) {
-		pm_runtime_put(&adev->dev);
+		pm_runtime_put(dev);
 		return 0;
 	}
 
 	return PTR_ERR(drvdata->csdev);
 }
 
-static void tpiu_remove(struct amba_device *adev)
+static int tpiu_probe(struct amba_device *adev, const struct amba_id *id)
 {
-	struct tpiu_drvdata *drvdata = dev_get_drvdata(&adev->dev);
+	return __tpiu_probe(&adev->dev, &adev->res);
+}
+
+static void __tpiu_remove(struct device *dev)
+{
+	struct tpiu_drvdata *drvdata = dev_get_drvdata(dev);
 
 	coresight_unregister(drvdata->csdev);
 }
 
+static void tpiu_remove(struct amba_device *adev)
+{
+	__tpiu_remove(&adev->dev);
+}
+
 #ifdef CONFIG_PM
 static int tpiu_runtime_suspend(struct device *dev)
 {
@@ -186,6 +201,8 @@  static int tpiu_runtime_suspend(struct device *dev)
 	if (drvdata && !IS_ERR(drvdata->atclk))
 		clk_disable_unprepare(drvdata->atclk);
 
+	if (drvdata && !IS_ERR_OR_NULL(drvdata->pclk))
+		clk_disable_unprepare(drvdata->pclk);
 	return 0;
 }
 
@@ -196,6 +213,8 @@  static int tpiu_runtime_resume(struct device *dev)
 	if (drvdata && !IS_ERR(drvdata->atclk))
 		clk_prepare_enable(drvdata->atclk);
 
+	if (drvdata && !IS_ERR_OR_NULL(drvdata->pclk))
+		clk_prepare_enable(drvdata->pclk);
 	return 0;
 }
 #endif
@@ -237,6 +256,49 @@  static struct amba_driver tpiu_driver = {
 
 module_amba_driver(tpiu_driver);
 
+static int tpiu_platform_probe(struct platform_device *pdev)
+{
+	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	int ret;
+
+	pm_runtime_get_noresume(&pdev->dev);
+	pm_runtime_set_active(&pdev->dev);
+	pm_runtime_enable(&pdev->dev);
+
+	ret = __tpiu_probe(&pdev->dev, res);
+	if (ret) {
+		pm_runtime_put_noidle(&pdev->dev);
+		pm_runtime_disable(&pdev->dev);
+	}
+	return ret;
+}
+
+static int tpiu_platform_remove(struct platform_device *pdev)
+{
+	__tpiu_remove(&pdev->dev);
+	return 0;
+}
+
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id tpiu_acpi_ids[] = {
+	{"ARMHC979", 0}, /* ARM CoreSight TPIU */
+	{}
+};
+MODULE_DEVICE_TABLE(acpi, tpiu_acpi_ids);
+#endif
+
+static struct platform_driver tpiu_platform_driver = {
+	.probe	= tpiu_platform_probe,
+	.remove	= tpiu_platform_remove,
+	.driver = {
+		.name			= "coresight-tpiu-platform",
+		.acpi_match_table	= ACPI_PTR(tpiu_acpi_ids),
+		.suppress_bind_attrs	= true,
+		.pm			= &tpiu_dev_pm_ops,
+	},
+};
+module_platform_driver(tpiu_platform_driver);
+
 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
 MODULE_DESCRIPTION("Arm CoreSight TPIU (Trace Port Interface Unit) driver");