From patchwork Fri Sep 12 16:34:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 37357 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8FDE8206A5 for ; Fri, 12 Sep 2014 16:37:47 +0000 (UTC) Received: by mail-wg0-f69.google.com with SMTP id y10sf621594wgg.8 for ; Fri, 12 Sep 2014 09:37:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:subject:date:message-id :in-reply-to:references:cc:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version:sender :errors-to:x-original-sender:x-original-authentication-results :mailing-list:content-type:content-transfer-encoding; bh=wLdxUqIw3jrfPZObVHouiFztTmlcfEc0rmwG4vu8+BU=; b=R2Hx3as6IuI+6nR5/r8Mg7AC4f4bVnLe2OH+zJ4xrfRUM+Nzoa0YcumryrR30o2Oyz BwPUn9KwLuufVJ5UFazNjJbIETWU/BfJyxUR/KYf0rWqBOmWlSmC1skuMjT3Kc3iYp/V WcuJLrW6Cc/4NpyYM/hwCs6LUhRsj+OW0cVtPpCBzbjh3e9r3VayoAET3dDs0GumxwfY 2VgqtDFMo3bgjFBwvRhtesZoDjNTw7seyhNfMJYMSZy1QTK25gmEP71HMK7qtwa/bcdj yL3Dp8/jz/z/37HdPf515Md0SqtiGWO0N8IGAU0z+ZS3kKuM0dHES7sHKIw631cD1SEy t1SQ== X-Gm-Message-State: ALoCoQkhZQqv22OGWrK8XZWOVcwQzmJyOllNXdrvOXZWgWsYU2oTXunMkNqsVNjYiFUGh7qoRd5D X-Received: by 10.112.137.230 with SMTP id ql6mr2596260lbb.13.1410539863616; Fri, 12 Sep 2014 09:37:43 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.27.2 with SMTP id p2ls199059lag.10.gmail; Fri, 12 Sep 2014 09:37:43 -0700 (PDT) X-Received: by 10.152.203.204 with SMTP id ks12mr10388348lac.65.1410539863389; Fri, 12 Sep 2014 09:37:43 -0700 (PDT) Received: from mail-la0-f50.google.com (mail-la0-f50.google.com [209.85.215.50]) by mx.google.com with ESMTPS id y10si7365874laj.52.2014.09.12.09.37.43 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 12 Sep 2014 09:37:43 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.50 as permitted sender) client-ip=209.85.215.50; Received: by mail-la0-f50.google.com with SMTP id ty20so1313910lab.23 for ; Fri, 12 Sep 2014 09:37:43 -0700 (PDT) X-Received: by 10.152.5.168 with SMTP id t8mr9982208lat.67.1410539863311; Fri, 12 Sep 2014 09:37:43 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.141.42 with SMTP id rl10csp769241lbb; Fri, 12 Sep 2014 09:37:42 -0700 (PDT) X-Received: by 10.66.174.107 with SMTP id br11mr13820251pac.105.1410539860986; Fri, 12 Sep 2014 09:37:40 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id se10si8963164pbb.72.2014.09.12.09.37.40 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Sep 2014 09:37:40 -0700 (PDT) Received-SPF: none (google.com: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org does not designate permitted sender hosts) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XSTpl-00032K-U5; Fri, 12 Sep 2014 16:36:29 +0000 Received: from cam-admin0.cambridge.arm.com ([217.140.96.50]) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XSTop-0001wV-Fp; Fri, 12 Sep 2014 16:35:32 +0000 Received: from edgewater-inn.cambridge.arm.com (edgewater-inn.cambridge.arm.com [10.1.203.204]) by cam-admin0.cambridge.arm.com (8.12.6/8.12.6) with ESMTP id s8CGYuwo029969; Fri, 12 Sep 2014 17:34:56 +0100 (BST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 76D621AE0B6F; Fri, 12 Sep 2014 17:34:57 +0100 (BST) From: Will Deacon To: linux-arm-kernel@lists.infradead.org, iommu@lists.linux-foundation.org Subject: [RFC PATCH v3 4/7] iommu: provide helper function to configure an IOMMU for an of master Date: Fri, 12 Sep 2014 17:34:52 +0100 Message-Id: <1410539695-29128-5-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1410539695-29128-1-git-send-email-will.deacon@arm.com> References: <1410539695-29128-1-git-send-email-will.deacon@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140912_093531_937298_D6107610 X-CRM114-Status: GOOD ( 17.43 ) X-Spam-Score: -7.2 (-------) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-7.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high trust [217.140.96.50 listed in list.dnswl.org] -2.2 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -0.0 SPF_PASS SPF: sender matches SPF record Cc: jroedel@suse.de, arnd@arndb.de, Will Deacon , thierry.reding@gmail.com, laurent.pinchart@ideasonboard.com, Varun.Sethi@freescale.com, m.szyprowski@samsung.com, dwmw2@infradead.org, hdoyu@nvidia.com X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: will.deacon@arm.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.50 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 The generic IOMMU device-tree bindings can be used to add arbitrary OF masters to an IOMMU with a compliant binding. This patch introduces of_iommu_configure, which does exactly that. A list of iommu_dma_mapping structures are created for each device, which represent the set of IOMMU instances through which the device can master. The list is protected by a kref count and freed when no users remain. It is expected that DMA-mapping code will take a reference if it wishes to make use of the IOMMU information. Signed-off-by: Will Deacon --- drivers/iommu/Kconfig | 2 +- drivers/iommu/of_iommu.c | 70 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/dma-mapping.h | 8 ++++++ include/linux/of_iommu.h | 10 +++++++ 4 files changed, 89 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index dd5112265cc9..6d13f962f156 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -15,7 +15,7 @@ if IOMMU_SUPPORT config OF_IOMMU def_bool y - depends on OF + depends on OF && IOMMU_API config FSL_PAMU bool "Freescale IOMMU support" diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c index 13d800c4ce25..8656b63f27ee 100644 --- a/drivers/iommu/of_iommu.c +++ b/drivers/iommu/of_iommu.c @@ -18,9 +18,11 @@ */ #include +#include #include #include #include +#include /** * of_get_dma_window - Parse *dma-window property and returns 0 if found. @@ -90,6 +92,74 @@ int of_get_dma_window(struct device_node *dn, const char *prefix, int index, } EXPORT_SYMBOL_GPL(of_get_dma_window); +struct iommu_dma_mapping *of_iommu_configure(struct device *dev) +{ + struct of_phandle_args iommu_spec; + struct iommu_dma_mapping *mapping; + struct device_node *np; + struct iommu_data *iommu = NULL; + int idx = 0; + + /* + * We don't currently walk up the tree looking for a parent IOMMU. + * See the `Notes:' section of + * Documentation/devicetree/bindings/iommu/iommu.txt + */ + while (!of_parse_phandle_with_args(dev->of_node, "iommus", + "#iommu-cells", idx, + &iommu_spec)) { + struct iommu_data *data; + + np = iommu_spec.np; + data = of_iommu_get_data(np); + + if (!data || !data->ops || !data->ops->of_xlate) + goto err_put_node; + + if (!iommu) { + iommu = data; + } else if (iommu != data) { + /* We don't currently support multi-IOMMU masters */ + pr_warn("Rejecting device %s with multiple IOMMU instances\n", + dev_name(dev)); + goto err_put_node; + } + + if (!data->ops->of_xlate(dev, &iommu_spec)) + goto err_put_node; + + of_node_put(np); + idx++; + } + + if (!iommu) + return NULL; + + mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); + if (!mapping) + return NULL; + + kref_init(&mapping->kref); + INIT_LIST_HEAD(&mapping->node); + mapping->iommu = iommu; + return mapping; + +err_put_node: + of_node_put(np); + return NULL; +} + +void of_iommu_deconfigure(struct kref *kref) +{ + struct iommu_dma_mapping *mapping, *curr, *next; + + mapping = container_of(kref, struct iommu_dma_mapping, kref); + list_for_each_entry_safe(curr, next, &mapping->node, node) { + list_del(&curr->node); + kfree(curr); + } +} + void __init of_iommu_init(void) { struct device_node *np; diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 1e944e77d38d..e60e52d82db9 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -62,6 +62,14 @@ struct dma_map_ops { int is_phys; }; +struct iommu_data; + +struct iommu_dma_mapping { + struct iommu_data *iommu; + struct list_head node; + struct kref kref; +}; + #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) #define DMA_MASK_NONE 0x0ULL diff --git a/include/linux/of_iommu.h b/include/linux/of_iommu.h index 0a685e0ab33e..af6179557005 100644 --- a/include/linux/of_iommu.h +++ b/include/linux/of_iommu.h @@ -1,9 +1,12 @@ #ifndef __OF_IOMMU_H #define __OF_IOMMU_H +#include #include #include +struct iommu_dma_mapping; + #ifdef CONFIG_OF_IOMMU extern int of_get_dma_window(struct device_node *dn, const char *prefix, @@ -11,6 +14,8 @@ extern int of_get_dma_window(struct device_node *dn, const char *prefix, size_t *size); extern void of_iommu_init(void); +extern struct iommu_dma_mapping *of_iommu_configure(struct device *dev); +extern void of_iommu_deconfigure(struct kref *kref); #else @@ -22,6 +27,11 @@ static inline int of_get_dma_window(struct device_node *dn, const char *prefix, } static inline void of_iommu_init(void) { } +static inline struct iommu_dma_mapping *of_iommu_configure(struct device *dev) +{ + return NULL; +} +static inline void of_iommu_deconfigure(struct kref *kref) { } #endif /* CONFIG_OF_IOMMU */