linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
To: iommu@lists.linux-foundation.org
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>,
	Will Deacon <will.deacon@arm.com>,
	Hanjun Guo <hanjun.guo@linaro.org>,
	Robin Murphy <robin.murphy@arm.com>,
	Tomasz Nowicki <tn@semihalf.com>,
	Marc Zyngier <marc.zyngier@arm.com>,
	Joerg Roedel <joro@8bytes.org>,
	"Rafael J. Wysocki" <rjw@rjwysocki.net>,
	Jon Masters <jcm@redhat.com>, Sinan Kaya <okaya@codeaurora.org>,
	linux-acpi@vger.kernel.org, linux-pci@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org
Subject: [RFC PATCH 10/11] drivers: iommu: arm-smmu: implement ACPI probing
Date: Thu, 14 Apr 2016 18:25:42 +0100	[thread overview]
Message-ID: <1460654743-7896-11-git-send-email-lorenzo.pieralisi@arm.com> (raw)
In-Reply-To: <1460654743-7896-1-git-send-email-lorenzo.pieralisi@arm.com>

In ACPI world ARM SMMU components are described through IORT table
entries, that contain SMMU parameters and provide the kernel with
data to create the corresponding kernel abstractions and allow
SMMU probing and initialization.

Currently, the arm-smmu driver probe routines are DT based, hence,
to enable the driver to probe through ACPI they should be augmented
so that the DT and ACPI probing paths can actually share the common
code that probes and initializes the ARM SMMU allowing them to co-exist
seamlessly.

This patch refactors the ARM SMMU probing path to introduce ACPI
probing effectively enabling the ARM SMMU on ACPI based systems.

To create the required platform devices representing ARM SMMU components
and initialize them with the required data, this patch also implements
ARM SMMU ACPI probing by adding a hook into the ACPI IORT linker section,
that is executed automatically by the kernel on boot and allows
to detect and configure the ARM SMMU devices present in the system.

Based on prior work by Hanjun Guo <hanjun.guo@linaro.org>.

Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Hanjun Guo <hanjun.guo@linaro.org>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Tomasz Nowicki <tn@semihalf.com>
---
 drivers/iommu/arm-smmu.c | 240 +++++++++++++++++++++++++++++++++++++++++++++--
 include/linux/iort.h     |   3 +
 2 files changed, 233 insertions(+), 10 deletions(-)

diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 0f1e784..85ab4b7 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -28,6 +28,7 @@
 
 #define pr_fmt(fmt) "arm-smmu: " fmt
 
+#include <linux/acpi.h>
 #include <linux/delay.h>
 #include <linux/dma-iommu.h>
 #include <linux/dma-mapping.h>
@@ -36,6 +37,7 @@
 #include <linux/io.h>
 #include <linux/iommu.h>
 #include <linux/iopoll.h>
+#include <linux/iort.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
@@ -1729,6 +1731,103 @@ static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
 	return 0;
 }
 
+#ifdef CONFIG_ACPI
+static int __init acpi_smmu_version(u32 model, u32 *version)
+{
+	switch (model) {
+	case ACPI_IORT_SMMU_V1:
+	case ACPI_IORT_SMMU_CORELINK_MMU400:
+		*version = ARM_SMMU_V1;
+		return 0;
+	case ACPI_IORT_SMMU_V2:
+	case ACPI_IORT_SMMU_CORELINK_MMU500:
+		*version = ARM_SMMU_V2;
+		return 0;
+	default:
+		break;
+	}
+
+	return -ENODEV;
+}
+
+static int __init arm_smmu_acpi_probe(struct platform_device *pdev,
+				      struct arm_smmu_device *smmu)
+{
+	struct device *dev = smmu->dev;
+	struct acpi_iort_node *node =
+		*(struct acpi_iort_node **)dev_get_platdata(dev);
+	struct acpi_iort_smmu *iort_smmu;
+	int num_irqs, i, err, trigger, ret, irq_idx;
+	u64 *ctx_irq, *glb_irq;
+
+	/* Retrieve SMMU1/2 specific data */
+	iort_smmu = (struct acpi_iort_smmu *)node->node_data;
+
+	ret = acpi_smmu_version(iort_smmu->model, &smmu->version);
+	if (ret < 0)
+		return ret;
+
+	glb_irq = ACPI_ADD_PTR(u64, node, iort_smmu->global_interrupt_offset);
+	if (!IORT_IRQ_MASK(glb_irq[1]))	/* 0 means not implemented */
+		smmu->num_global_irqs = 1;
+	else
+		smmu->num_global_irqs = 2;
+
+	smmu->num_context_irqs = iort_smmu->context_interrupt_count;
+	num_irqs = smmu->num_context_irqs + smmu->num_global_irqs;
+
+	if (!smmu->num_context_irqs) {
+		dev_err(dev, "found %d interrupts but expected at least %d\n",
+			num_irqs, smmu->num_global_irqs + 1);
+		return -ENODEV;
+	}
+
+	smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
+				  GFP_KERNEL);
+	if (!smmu->irqs) {
+		dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
+		return -ENOMEM;
+	}
+
+	/* Global IRQs */
+	for (i = irq_idx = 0; i < smmu->num_global_irqs; i++, irq_idx++) {
+		int hw_irq = IORT_IRQ_MASK(glb_irq[i]);
+
+		trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[i]);
+		smmu->irqs[irq_idx] = acpi_register_gsi(NULL, hw_irq, trigger,
+							ACPI_ACTIVE_HIGH);
+	}
+
+	/* Context IRQs */
+	ctx_irq = ACPI_ADD_PTR(u64, node, iort_smmu->context_interrupt_offset);
+	for (i = 0; i < smmu->num_context_irqs; i++, irq_idx++) {
+		int hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
+
+		trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
+		smmu->irqs[irq_idx] = acpi_register_gsi(NULL, hw_irq, trigger,
+							ACPI_ACTIVE_HIGH);
+	}
+
+	if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
+		smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
+
+	err = arm_smmu_device_cfg_probe(smmu);
+	if (err)
+		return err;
+
+	iort_iommu_set_node(&arm_smmu_ops, node, pdev->dev.fwnode);
+
+	return 0;
+}
+
+#else
+static inline int arm_smmu_acpi_probe(struct platform_device *pdev,
+				      struct arm_smmu_device *smmu)
+{
+	return -ENODEV;
+}
+#endif
+
 static const struct of_device_id arm_smmu_of_match[] = {
 	{ .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
 	{ .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
@@ -1933,7 +2032,10 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 		return PTR_ERR(smmu->base);
 	smmu->size = resource_size(res);
 
-	err = arm_smmu_dt_probe(pdev, smmu);
+	if (acpi_disabled)
+		err = arm_smmu_dt_probe(pdev, smmu);
+	else
+		err = arm_smmu_acpi_probe(pdev, smmu);
 
 	if (err)
 		return err;
@@ -2007,16 +2109,20 @@ static int __init arm_smmu_init(void)
 
 	if (done)
 		return 0;
-	/*
-	 * Play nice with systems that don't have an ARM SMMU by checking that
-	 * an ARM SMMU exists in the system before proceeding with the driver
-	 * and IOMMU bus operation registration.
-	 */
-	np = of_find_matching_node(NULL, arm_smmu_of_match);
-	if (!np)
-		return 0;
 
-	of_node_put(np);
+	if (acpi_disabled) {
+		/*
+		 * Play nice with systems that don't have an ARM SMMU by
+		 * checking that an ARM SMMU exists in the system before
+		 * proceeding with the driver and IOMMU bus operation
+		 * registration.
+		 */
+		np = of_find_matching_node(NULL, arm_smmu_of_match);
+		if (!np)
+			return 0;
+
+		of_node_put(np);
+	}
 
 	ret = platform_driver_register(&arm_smmu_driver);
 	if (ret)
@@ -2048,6 +2154,120 @@ static void __exit arm_smmu_exit(void)
 subsys_initcall(arm_smmu_init);
 module_exit(arm_smmu_exit);
 
+#ifdef CONFIG_ACPI
+static int __init add_smmu_platform_device(struct acpi_iort_node *node)
+{
+	struct acpi_iort_smmu *smmu;
+	struct platform_device *pdev = NULL;
+	struct resource resources;
+	enum dev_dma_attr attr;
+	int ret;
+
+	/* Retrieve SMMU1/2 specific data */
+	smmu = (struct acpi_iort_smmu *)node->node_data;
+
+	memset(&resources, 0, sizeof(resources));
+	resources.start = smmu->base_address;
+	resources.end = smmu->base_address + smmu->span - 1;
+	resources.flags = IORESOURCE_MEM;
+
+	pdev = platform_device_alloc("arm-smmu", PLATFORM_DEVID_AUTO);
+	if (!pdev)
+		return PTR_ERR(pdev);
+
+	pdev->dev.fwnode = iommu_alloc_fwnode();
+
+	if (!pdev->dev.fwnode) {
+		ret = -ENOMEM;
+		goto dev_put;
+	}
+
+	ret = platform_device_add_resources(pdev, &resources, 1);
+	if (ret)
+		goto free_node;
+
+	ret = platform_device_add_data(pdev, &node, sizeof(node));
+	if (ret)
+		goto free_node;
+
+	pdev->dev.dma_mask = kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
+	if (!pdev->dev.dma_mask) {
+		ret = -ENOMEM;
+		goto free_node;
+	}
+
+	/*
+	 * Set default dma mask value for the table walker,
+	 * to be overridden on probing with correct value.
+	 */
+	*pdev->dev.dma_mask = DMA_BIT_MASK(32);
+	pdev->dev.coherent_dma_mask = *pdev->dev.dma_mask;
+
+	attr = smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK ?
+			     DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
+
+	/* Configure DMA for the page table walker */
+	acpi_dma_configure(&pdev->dev, attr);
+
+	ret = platform_device_add(pdev);
+	if (ret)
+		goto dma_deconfigure;
+
+	return 0;
+
+dma_deconfigure:
+	acpi_dma_deconfigure(&pdev->dev);
+	kfree(pdev->dev.dma_mask);
+free_node:
+	iommu_free_fwnode(pdev->dev.fwnode);
+dev_put:
+	platform_device_put(pdev);
+
+	return ret;
+}
+
+static int __init arm_smmu_acpi_init(struct acpi_table_header *table)
+{
+	struct acpi_iort_node *iort_node, *iort_end;
+	struct acpi_table_iort *iort;
+	int i, ret = arm_smmu_init();
+
+	if (ret)
+		return ret;
+	/*
+	 * iort_table and iort both point to the start of IORT table, but
+	 * have different struct types
+	 */
+	iort = (struct acpi_table_iort *)table;
+
+	/* Get the first IORT node */
+	iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
+				 iort->node_offset);
+	iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
+				table->length);
+
+	for (i = 0; i < iort->node_count; i++) {
+		if (iort_node >= iort_end) {
+			pr_err("iort node pointer overflows, bad table\n");
+			return -EINVAL;
+		}
+
+		if (iort_node->type == ACPI_IORT_NODE_SMMU) {
+			ret = add_smmu_platform_device(iort_node);
+			if (ret)
+				return ret;
+		}
+
+		iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
+					 iort_node->length);
+	}
+
+	return 0;
+}
+
+IORT_ACPI_DECLARE(arm_smmu, ACPI_SIG_IORT, arm_smmu_acpi_init);
+#endif
+
 static int __init arm_smmu_of_init(struct device_node *np)
 {
 	struct arm_smmu_device *smmu;
diff --git a/include/linux/iort.h b/include/linux/iort.h
index 7a7af40..958b236 100644
--- a/include/linux/iort.h
+++ b/include/linux/iort.h
@@ -21,6 +21,9 @@
 
 #include <linux/acpi.h>
 
+#define IORT_IRQ_MASK(irq)		(irq & 0xffffffffULL)
+#define IORT_IRQ_TRIGGER_MASK(irq)	((irq >> 32) & 0xffffffffULL)
+
 struct pci_dev;
 struct fwnode_handle;
 int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node);
-- 
2.6.4

  parent reply	other threads:[~2016-04-14 17:23 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-14 17:25 [RFC PATCH 00/11] ACPI IORT ARM SMMU support Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 01/11] drivers: acpi: iort: fix struct pci_dev compiler warnings Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 02/11] drivers: acpi: iort: add support for IOMMU registration Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 03/11] drivers: iommu: add FWNODE_IOMMU fwnode type Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 04/11] drivers: acpi: iort: introduce linker section for IORT entries probing Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 05/11] drivers: iommu: arm-smmu: split probe functions into DT/generic portions Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 06/11] drivers: iommu: make of_xlate() interface DT agnostic Lorenzo Pieralisi
2016-04-19  8:28   ` Marek Szyprowski
2016-04-19 11:30     ` Lorenzo Pieralisi
2016-04-20  7:14       ` Marek Szyprowski
2016-04-14 17:25 ` [RFC PATCH 07/11] drivers: iommu: arm-smmu: allow ACPI based streamid translation Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 08/11] drivers: acpi: iort: enhance mapping API Lorenzo Pieralisi
2016-04-14 17:25 ` [RFC PATCH 09/11] drivers: acpi: implement acpi_dma_configure Lorenzo Pieralisi
2016-04-15 16:14   ` Bjorn Helgaas
2016-04-15 16:31     ` Robin Murphy
2016-04-15 18:29   ` Timur Tabi
2016-04-18 10:30     ` Lorenzo Pieralisi
2016-04-18 10:43     ` Robin Murphy
2016-05-16 15:15       ` Tomasz Nowicki
2016-05-16 15:26         ` Tomasz Nowicki
2016-04-21 22:45   ` Andy Shevchenko
2016-04-22 10:57     ` Lorenzo Pieralisi
2016-05-17  8:07   ` Tomasz Nowicki
2016-05-17 12:32     ` Tomasz Nowicki
2016-04-14 17:25 ` Lorenzo Pieralisi [this message]
2016-04-14 17:25 ` [RFC PATCH 11/11] drivers: irqchip: make struct irq_fwspec generic Lorenzo Pieralisi

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=1460654743-7896-11-git-send-email-lorenzo.pieralisi@arm.com \
    --to=lorenzo.pieralisi@arm.com \
    --cc=hanjun.guo@linaro.org \
    --cc=iommu@lists.linux-foundation.org \
    --cc=jcm@redhat.com \
    --cc=joro@8bytes.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=marc.zyngier@arm.com \
    --cc=okaya@codeaurora.org \
    --cc=rjw@rjwysocki.net \
    --cc=robin.murphy@arm.com \
    --cc=tn@semihalf.com \
    --cc=will.deacon@arm.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;
as well as URLs for NNTP newsgroup(s).