From patchwork Fri May 18 21:34:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Crouse X-Patchwork-Id: 10412319 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 63345602CB for ; Fri, 18 May 2018 21:38:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9DD39289CB for ; Fri, 18 May 2018 21:38:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 91BD0289CD; Fri, 18 May 2018 21:38:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7529D289CB for ; Fri, 18 May 2018 21:38:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ABsf9QFTSlx/APLXZHZL8fudIp3P9QHEbLndHDpjd7I=; b=KRSN0ylEQzGBpG6ia1XGQuFrKZ HkPRZQdh/JT0Ys0NnGLRyntgNt1wTOT5D25euptmrzWy0O8FLp+VdJyK/EVR+egjI1k6CFlnYBnZh 35Jed05lPxvsafTht5r09Ky1S9toNhMToPB94HH4Dszkw/qn9l+lLwAy9WeoS9psjznBWa5jvm8mP Nx97XVHbMv8EyNEs3tFRgz2TeHEj5Ajk1aru7iOfPVKQ4I/iF0y6iXWE9EqSVcBYULsPgcZ7LxvbF Xf8aVQX6chCjCGXO/ot6QebArDamUdL6ufzMVCX89Vn5ZL9ZI3Kpfaqjo9iG28HhGCojIVLx2xqik ypO8eeZw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fJn50-0002lm-F1; Fri, 18 May 2018 21:38:26 +0000 Received: from smtp.codeaurora.org ([198.145.29.96]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fJn2B-00006U-5W for linux-arm-kernel@lists.infradead.org; Fri, 18 May 2018 21:35:57 +0000 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 1433C60C67; Fri, 18 May 2018 21:35:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1526679313; bh=OuUDbHISWk9j87cZzeRb6FaryWP6r6HkVtX5+KlFDLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ifA1UED7sOUB1eXNYGXjdGg5Czh9xYtPi2AFuFjeJmpiBuLu+w+mCqYWm8nJbN12o x09/1v+P7yQ8trN4ahx9ew+MoYihHfEuLLEnJP5q2hFWw+Qkjc0ZqWBmlvyi+Fe6Uw pMAiO6zSU6n/eYnBG5eaSHEOqJ7cD0iA4nt7qTPw= Received: from jcrouse-lnx.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: jcrouse@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id F0B0E6085F; Fri, 18 May 2018 21:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1526679308; bh=OuUDbHISWk9j87cZzeRb6FaryWP6r6HkVtX5+KlFDLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TEypYlo4YoDwyQNDI4A2x73IiOJBBQNkvs99GZ81aSJac9hFxHA1UQDMoYH7bJ/NU C3BFFSMg60Z+b/H9gUhpaCxxlUEm2mibZen2OKB5PS03TWnhSsCaCdngiP1GYgfFe9 QtDl1+hD4Gpthwd+i5A94pwP3My0oT69M7t2Q2mA= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org F0B0E6085F Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=jcrouse@codeaurora.org From: Jordan Crouse To: freedreno@lists.freedesktop.org Subject: [PATCH 04/16] iommu: sva: Add support for private PASIDs Date: Fri, 18 May 2018 15:34:48 -0600 Message-Id: <20180518213500.31595-5-jcrouse@codeaurora.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180518213500.31595-1-jcrouse@codeaurora.org> References: <20180518213500.31595-1-jcrouse@codeaurora.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180518_143531_447015_E920158B X-CRM114-Status: GOOD ( 20.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jean-philippe.brucker@arm.com, linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, tfiga@chromium.org, iommu@lists.linux-foundation.org, vivek.gautam@codeaurora.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Some older SMMU implementations that do not have a fully featured hardware PASID features have alternate workarounds for using multiple pagetables. For example, MSM GPUs have logic to automatically switch the user pagetable from hardware by writing the context bank directly. Support private PASIDs by creating a new io-pgtable instance map it to a PASID and provide the APIs for drivers to populate it manually. Signed-off-by: Jordan Crouse --- drivers/iommu/iommu-sva.c | 139 ++++++++++++++++++++++++++++++++++++-- drivers/iommu/iommu.c | 66 +++++++++++++----- include/linux/iommu.h | 74 ++++++++++++++++++-- 3 files changed, 250 insertions(+), 29 deletions(-) diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index e98b994c15f1..26f0da9692d4 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -156,6 +156,7 @@ io_mm_alloc(struct iommu_domain *domain, struct device *dev, mmgrab(mm); io_mm->flags = flags; + io_mm->type = IO_TYPE_SHARED; io_mm->mm = mm; io_mm->notifier.ops = &iommu_mmu_notifier; io_mm->release = domain->ops->mm_free; @@ -544,13 +545,10 @@ int iommu_sva_device_init(struct device *dev, unsigned long features, unsigned int max_pasid, iommu_mm_exit_handler_t mm_exit) { - int ret; + int ret = 0; struct iommu_sva_param *param; struct iommu_domain *domain = iommu_get_domain_for_dev(dev); - if (!domain || !domain->ops->sva_device_init) - return -ENODEV; - if (features & ~IOMMU_SVA_FEAT_IOPF) return -EINVAL; @@ -576,9 +574,12 @@ int iommu_sva_device_init(struct device *dev, unsigned long features, * IOMMU driver updates the limits depending on the IOMMU and device * capabilities. */ - ret = domain->ops->sva_device_init(dev, param); - if (ret) - goto err_free_param; + + if (domain && domain->ops->sva_device_init) { + ret = domain->ops->sva_device_init(dev, param); + if (ret) + goto err_free_param; + } mutex_lock(&dev->iommu_param->lock); if (dev->iommu_param->sva_param) @@ -790,3 +791,127 @@ struct mm_struct *iommu_sva_find(int pasid) return mm; } EXPORT_SYMBOL_GPL(iommu_sva_find); + +int iommu_sva_alloc_pasid(struct iommu_domain *domain, struct device *dev) +{ + int ret, pasid; + struct io_mm *io_mm; + struct iommu_sva_param *param = dev->iommu_param->sva_param; + + if (!domain->ops->mm_attach || !domain->ops->mm_detach) + return -ENODEV; + + if (domain->ops->mm_alloc) + io_mm = domain->ops->mm_alloc(domain, NULL, 0); + else + io_mm = kzalloc(sizeof(*io_mm), GFP_KERNEL); + + if (IS_ERR(io_mm)) + return PTR_ERR(io_mm); + if (!io_mm) + return -ENOMEM; + + io_mm->domain = domain; + io_mm->type = IO_TYPE_PRIVATE; + + idr_preload(GFP_KERNEL); + spin_lock(&iommu_sva_lock); + pasid = idr_alloc_cyclic(&iommu_pasid_idr, io_mm, param->min_pasid, + param->max_pasid + 1, GFP_ATOMIC); + io_mm->pasid = pasid; + spin_unlock(&iommu_sva_lock); + idr_preload_end(); + + if (pasid < 0) { + kfree(io_mm); + return pasid; + } + + ret = domain->ops->mm_attach(domain, dev, io_mm, false); + if (!ret) + return pasid; + + spin_lock(&iommu_sva_lock); + idr_remove(&iommu_pasid_idr, io_mm->pasid); + spin_unlock(&iommu_sva_lock); + + if (domain->ops->mm_free) + domain->ops->mm_free(io_mm); + else + kfree(io_mm); + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_sva_alloc_pasid); + +static struct io_mm *get_io_mm(int pasid) +{ + struct io_mm *io_mm; + + spin_lock(&iommu_sva_lock); + io_mm = idr_find(&iommu_pasid_idr, pasid); + spin_unlock(&iommu_sva_lock); + + return io_mm; +} + +int iommu_sva_map(int pasid, unsigned long iova, + phys_addr_t paddr, size_t size, int prot) +{ + struct io_mm *io_mm = get_io_mm(pasid); + + if (!io_mm || io_mm->type != IO_TYPE_PRIVATE) + return -ENODEV; + + return __iommu_map(io_mm->domain, &pasid, iova, paddr, size, prot); +} +EXPORT_SYMBOL_GPL(iommu_sva_map); + +size_t iommu_sva_map_sg(int pasid, unsigned long iova, struct scatterlist *sg, + unsigned int nents, int prot) +{ + struct io_mm *io_mm = get_io_mm(pasid); + struct iommu_domain *domain; + + if (!io_mm || io_mm->type != IO_TYPE_PRIVATE) + return -ENODEV; + + domain = io_mm->domain; + + return domain->ops->map_sg(domain, &pasid, iova, sg, nents, prot); +} +EXPORT_SYMBOL_GPL(iommu_sva_map_sg); + +size_t iommu_sva_unmap(int pasid, unsigned long iova, size_t size) +{ + struct io_mm *io_mm = get_io_mm(pasid); + + if (!io_mm || io_mm->type != IO_TYPE_PRIVATE) + return -ENODEV; + + return __iommu_unmap(io_mm->domain, &pasid, iova, size, false); +} +EXPORT_SYMBOL_GPL(iommu_sva_unmap); + +void iommu_sva_free_pasid(int pasid, struct device *dev) +{ + struct io_mm *io_mm = get_io_mm(pasid); + struct iommu_domain *domain; + + if (!io_mm || io_mm->type != IO_TYPE_PRIVATE) + return; + + domain = io_mm->domain; + + domain->ops->mm_detach(domain, dev, io_mm, false); + + spin_lock(&iommu_sva_lock); + idr_remove(&iommu_pasid_idr, io_mm->pasid); + spin_unlock(&iommu_sva_lock); + + if (domain->ops->mm_free) + domain->ops->mm_free(io_mm); + else + kfree(io_mm); +} +EXPORT_SYMBOL_GPL(iommu_sva_free_pasid); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 13f705df0725..0ba3d27f2300 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1792,7 +1792,7 @@ static size_t iommu_pgsize(struct iommu_domain *domain, return pgsize; } -int iommu_map(struct iommu_domain *domain, unsigned long iova, +int __iommu_map(struct iommu_domain *domain, int *pasid, unsigned long iova, phys_addr_t paddr, size_t size, int prot) { unsigned long orig_iova = iova; @@ -1801,10 +1801,17 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova, phys_addr_t orig_paddr = paddr; int ret = 0; - if (unlikely(domain->ops->map == NULL || - domain->pgsize_bitmap == 0UL)) + if (unlikely(domain->pgsize_bitmap == 0UL)) return -ENODEV; + if (pasid) { + if (unlikely(domain->ops->sva_map == NULL)) + return -ENODEV; + } else { + if (unlikely(domain->ops->map == NULL)) + return -ENODEV; + } + if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) return -EINVAL; @@ -1830,7 +1837,13 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova, pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", iova, &paddr, pgsize); - ret = domain->ops->map(domain, iova, paddr, pgsize, prot); + if (pasid) + ret = domain->ops->sva_map(domain, *pasid, iova, paddr, + pgsize, prot); + else + ret = domain->ops->map(domain, iova, paddr, pgsize, + prot); + if (ret) break; @@ -1841,16 +1854,23 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova, /* unroll mapping in case something went wrong */ if (ret) - iommu_unmap(domain, orig_iova, orig_size - size); + __iommu_unmap(domain, pasid, orig_iova, orig_size - size, + pasid ? false : true); else trace_map(orig_iova, orig_paddr, orig_size); return ret; } + +int iommu_map(struct iommu_domain *domain, unsigned long iova, + phys_addr_t paddr, size_t size, int prot) +{ + return __iommu_map(domain, NULL, iova, paddr, size, prot); +} EXPORT_SYMBOL_GPL(iommu_map); -static size_t __iommu_unmap(struct iommu_domain *domain, - unsigned long iova, size_t size, +size_t __iommu_unmap(struct iommu_domain *domain, + int *pasid, unsigned long iova, size_t size, bool sync) { const struct iommu_ops *ops = domain->ops; @@ -1858,9 +1878,16 @@ static size_t __iommu_unmap(struct iommu_domain *domain, unsigned long orig_iova = iova; unsigned int min_pagesz; - if (unlikely(ops->unmap == NULL || - domain->pgsize_bitmap == 0UL)) - return 0; + if (unlikely(domain->pgsize_bitmap == 0UL)) + return -0; + + if (pasid) { + if (unlikely(domain->ops->sva_unmap == NULL)) + return 0; + } else { + if (unlikely(domain->ops->unmap == NULL)) + return 0; + } if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) return 0; @@ -1888,7 +1915,12 @@ static size_t __iommu_unmap(struct iommu_domain *domain, while (unmapped < size) { size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); - unmapped_page = ops->unmap(domain, iova, pgsize); + if (pasid) + unmapped_page = ops->sva_unmap(domain, *pasid, iova, + pgsize); + else + unmapped_page = ops->unmap(domain, iova, pgsize); + if (!unmapped_page) break; @@ -1912,19 +1944,20 @@ static size_t __iommu_unmap(struct iommu_domain *domain, size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size) { - return __iommu_unmap(domain, iova, size, true); + return __iommu_unmap(domain, NULL, iova, size, true); } EXPORT_SYMBOL_GPL(iommu_unmap); size_t iommu_unmap_fast(struct iommu_domain *domain, unsigned long iova, size_t size) { - return __iommu_unmap(domain, iova, size, false); + return __iommu_unmap(domain, NULL, iova, size, false); } EXPORT_SYMBOL_GPL(iommu_unmap_fast); -size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, - struct scatterlist *sg, unsigned int nents, int prot) +size_t default_iommu_map_sg(struct iommu_domain *domain, int *pasid, + unsigned long iova, struct scatterlist *sg, + unsigned int nents, int prot) { struct scatterlist *s; size_t mapped = 0; @@ -1948,7 +1981,8 @@ size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, if (!IS_ALIGNED(s->offset, min_pagesz)) goto out_err; - ret = iommu_map(domain, iova + mapped, phys, s->length, prot); + ret = __iommu_map(domain, pasid, iova + mapped, phys, s->length, + prot); if (ret) goto out_err; diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 366254e4b07f..3d72d636c13d 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -108,7 +108,13 @@ struct iommu_domain { struct list_head mm_list; }; +enum iommu_io_type { + IO_TYPE_SHARED, + IO_TYPE_PRIVATE, +}; + struct io_mm { + enum iommu_io_type type; int pasid; /* IOMMU_SVA_FEAT_* */ unsigned long flags; @@ -123,6 +129,9 @@ struct io_mm { void (*release)(struct io_mm *io_mm); /* For postponed release */ struct rcu_head rcu; + + /* This is used by private entries */ + struct iommu_domain *domain; }; enum iommu_cap { @@ -315,8 +324,9 @@ struct iommu_ops { phys_addr_t paddr, size_t size, int prot); size_t (*unmap)(struct iommu_domain *domain, unsigned long iova, size_t size); - size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova, - struct scatterlist *sg, unsigned int nents, int prot); + size_t (*map_sg)(struct iommu_domain *domain, int *pasid, + unsigned long iova, struct scatterlist *sg, + unsigned int nents, int prot); void (*flush_iotlb_all)(struct iommu_domain *domain); void (*iotlb_range_add)(struct iommu_domain *domain, unsigned long iova, size_t size); @@ -358,6 +368,12 @@ struct iommu_ops { struct device *dev, struct tlb_invalidate_info *inv_info); int (*page_response)(struct device *dev, struct page_response_msg *msg); + int (*sva_map)(struct iommu_domain *domain, int pasid, + unsigned long iova, phys_addr_t paddr, size_t size, + int prot); + size_t (*sva_unmap)(struct iommu_domain *domain, int pasid, + unsigned long iova, size_t size); + unsigned long pgsize_bitmap; }; @@ -548,9 +564,9 @@ extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size); extern size_t iommu_unmap_fast(struct iommu_domain *domain, unsigned long iova, size_t size); -extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, - struct scatterlist *sg,unsigned int nents, - int prot); +extern size_t default_iommu_map_sg(struct iommu_domain *domain, int *pasid, + unsigned long iova, struct scatterlist *sg, + unsigned int nents, int prot); extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova); extern void iommu_set_fault_handler(struct iommu_domain *domain, iommu_fault_handler_t handler, void *token); @@ -636,7 +652,7 @@ static inline size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, struct scatterlist *sg, unsigned int nents, int prot) { - return domain->ops->map_sg(domain, iova, sg, nents, prot); + return domain->ops->map_sg(domain, NULL, iova, sg, nents, prot); } /* PCI device grouping function */ @@ -676,6 +692,14 @@ extern int iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, int *pasid, unsigned long flags, void *drvdata); extern int iommu_sva_unbind_device(struct device *dev, int pasid); +/* Common map and unmap functions */ +extern int __iommu_map(struct iommu_domain *domain, int *pasid, + unsigned long iova, phys_addr_t paddr, size_t size, int prot); + +extern size_t __iommu_unmap(struct iommu_domain *domain, + int *pasid, unsigned long iova, size_t size, + bool sync); + #else /* CONFIG_IOMMU_API */ struct iommu_ops {}; @@ -1027,6 +1051,16 @@ extern int __iommu_sva_unbind_device(struct device *dev, int pasid); extern void __iommu_sva_unbind_dev_all(struct device *dev); extern struct mm_struct *iommu_sva_find(int pasid); + +extern int iommu_sva_alloc_pasid(struct iommu_domain *domain, + struct device *dev); +extern int iommu_sva_map(int pasid, unsigned long iova, phys_addr_t physaddr, + size_t size, int prot); +extern size_t iommu_sva_map_sg(int pasid, unsigned long iova, + struct scatterlist *sg, unsigned int nents, int prot); +extern size_t iommu_sva_unmap(int pasid, unsigned long iova, size_t size); +extern void iommu_sva_free_pasid(int pasid, struct device *dev); + #else /* CONFIG_IOMMU_SVA */ static inline int iommu_sva_device_init(struct device *dev, unsigned long features, @@ -1061,6 +1095,34 @@ static inline struct mm_struct *iommu_sva_find(int pasid) { return NULL; } + +static inline int iommu_sva_alloc_pasid(struct iommu_domain *domain, + struct device *dev) +{ + return -EOPNOTSUPP; +} + +static inline int iommu_sva_map(int pasid, unsigned long iova, + phys_addr_t physaddr, size_t size, int prot) +{ + return -ENODEV; +} + + +static inline size_t iommu_sva_map_sg(int pasid, unsigned long iova, + struct scatterlist *sg, unsigned int nents, int prot) +{ + return 0; +} + +static inline size_t iommu_sva_unmap(int pasid, unsigned long iova, size_t size) +{ + return size; +} + +static inline void iommu_sva_free_pasid(int pasid, struct device *dev) { } + + #endif /* CONFIG_IOMMU_SVA */ #ifdef CONFIG_IOMMU_PAGE_FAULT