From patchwork Tue Apr 19 16:56:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8881821 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id F196BBF29F for ; Tue, 19 Apr 2016 16:59:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CE08520172 for ; Tue, 19 Apr 2016 16:59:10 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B4CCE201B4 for ; Tue, 19 Apr 2016 16:59:09 +0000 (UTC) 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 1asYyD-0001X7-FZ; Tue, 19 Apr 2016 16:57:49 +0000 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1asYxb-000138-8i for linux-arm-kernel@lists.infradead.org; Tue, 19 Apr 2016 16:57:13 +0000 Received: by mail-wm0-x22f.google.com with SMTP id n3so40531112wmn.0 for ; Tue, 19 Apr 2016 09:56:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=v15JX+8ZhfGWHU2Z1vyPHl73c+SzpHNW/k1gz5vQ7hc=; b=a+wrpsZriRudxQSryx/SXqGo64D7ChZfC4+D9w5GylEXMbUWReXfRreUCzt801Oq4X Iwxd6rO6fLp5kzlxhaMAOvnAjoG6eAi5rmQcyz7zwjYA3tNXbra+zP4xswNG7llzY3VW MCOgszXTecOiEPdPJNgLMk/wvZgIP+KQ1elqY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=v15JX+8ZhfGWHU2Z1vyPHl73c+SzpHNW/k1gz5vQ7hc=; b=cMy8dmRsolsY9CMyqhEeRsT3yWV+Yozy0h4H73+nhY0aYQysXRyO7INgcEVVLXWwwe WaKxgtFV7U5I5+mm37/qfgneuMui6BIpn8V64TzNWjxwosh4V0YgY8qZdJnSvt5pBZqN LJHzKqPwcfbT7ztk7QhT745Rf/w18eWRyUB97A5bzdQOVPUDYsjT7tR0g/1g8AvaDqwd gyKgW5aAL0Wk6yeVaSQ45juim63ICiOTNwJ59vWBpV9prwJxS0Em3RIMkCi78/bQHMUu 8b6zeUzf6B+ve+qOpuho1k5MKfPZnO38eE0xJe/gZ32noh5/cXJpXGFU0ryI2II8T5Eb mT2g== X-Gm-Message-State: AOPr4FXeVY9C7xN0q69VZwfBHmFogxFXErBU3QTtRDhUYvkRJ364r1EbrTM0zdCwk+8rb0CT X-Received: by 10.28.236.156 with SMTP id h28mr3806705wmi.25.1461085010952; Tue, 19 Apr 2016 09:56:50 -0700 (PDT) Received: from new-host-34.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id x2sm1169329wjr.33.2016.04.19.09.56.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 19 Apr 2016 09:56:49 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, robin.murphy@arm.com, alex.williamson@redhat.com, will.deacon@arm.com, joro@8bytes.org, tglx@linutronix.de, jason@lakedaemon.net, marc.zyngier@arm.com, christoffer.dall@linaro.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v7 04/10] iommu/dma-reserved-iommu: alloc/free_reserved_iova_domain Date: Tue, 19 Apr 2016 16:56:28 +0000 Message-Id: <1461084994-2355-5-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1461084994-2355-1-git-send-email-eric.auger@linaro.org> References: <1461084994-2355-1-git-send-email-eric.auger@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160419_095711_675587_5F75E773 X-CRM114-Status: GOOD ( 21.89 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: julien.grall@arm.com, patches@linaro.org, Jean-Philippe.Brucker@arm.com, p.fedin@samsung.com, linux-kernel@vger.kernel.org, Bharat.Bhushan@freescale.com, iommu@lists.linux-foundation.org, pranav.sawargaonkar@gmail.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Introduce alloc/free_reserved_iova_domain in the IOMMU API. alloc_reserved_iova_domain initializes an iova domain at a given iova base address and with a given size. This iova domain will be used to allocate iova within that window. Those IOVAs will be reserved for special purpose, typically MSI frame binding. Allocation function within the reserved iova domain will be introduced in subsequent patches. Those functions are fully implemented if CONFIG_IOMMU_DMA_RESERVED is set. Signed-off-by: Eric Auger --- v7: - fix locking - add iova_cache_get/put - static inline functions when CONFIG_IOMMU_DMA_RESERVED is not set - introduce struct reserved_iova_domain to encapsulate prot info & add prot parameter in alloc_reserved_iova_domain v5 -> v6: - use spin lock instead of mutex v3 -> v4: - formerly in "iommu/arm-smmu: implement alloc/free_reserved_iova_domain" & "iommu: add alloc/free_reserved_iova_domain" v2 -> v3: - remove iommu_alloc_reserved_iova_domain & iommu_free_reserved_iova_domain static implementation in case CONFIG_IOMMU_API is not set v1 -> v2: - moved from vfio API to IOMMU API --- drivers/iommu/Kconfig | 8 +++ drivers/iommu/Makefile | 1 + drivers/iommu/dma-reserved-iommu.c | 99 ++++++++++++++++++++++++++++++++++++++ include/linux/dma-reserved-iommu.h | 59 +++++++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 drivers/iommu/dma-reserved-iommu.c create mode 100644 include/linux/dma-reserved-iommu.h diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index dd1dc39..a5a1530 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -74,6 +74,12 @@ config IOMMU_DMA select IOMMU_IOVA select NEED_SG_DMA_LENGTH +# IOMMU reserved IOVA mapping (MSI doorbell) +config IOMMU_DMA_RESERVED + bool + select IOMMU_API + select IOMMU_IOVA + config FSL_PAMU bool "Freescale IOMMU support" depends on PPC32 @@ -307,6 +313,7 @@ config SPAPR_TCE_IOMMU config ARM_SMMU bool "ARM Ltd. System MMU (SMMU) Support" depends on (ARM64 || ARM) && MMU + select IOMMU_DMA_RESERVED select IOMMU_API select IOMMU_IO_PGTABLE_LPAE select ARM_DMA_USE_IOMMU if ARM @@ -320,6 +327,7 @@ config ARM_SMMU config ARM_SMMU_V3 bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" depends on ARM64 && PCI + select IOMMU_DMA_RESERVED select IOMMU_API select IOMMU_IO_PGTABLE_LPAE select GENERIC_MSI_IRQ_DOMAIN diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile index c6edb31..6c9ae99 100644 --- a/drivers/iommu/Makefile +++ b/drivers/iommu/Makefile @@ -2,6 +2,7 @@ obj-$(CONFIG_IOMMU_API) += iommu.o obj-$(CONFIG_IOMMU_API) += iommu-traces.o obj-$(CONFIG_IOMMU_API) += iommu-sysfs.o obj-$(CONFIG_IOMMU_DMA) += dma-iommu.o +obj-$(CONFIG_IOMMU_DMA_RESERVED) += dma-reserved-iommu.o obj-$(CONFIG_IOMMU_IO_PGTABLE) += io-pgtable.o obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o diff --git a/drivers/iommu/dma-reserved-iommu.c b/drivers/iommu/dma-reserved-iommu.c new file mode 100644 index 0000000..2562af0 --- /dev/null +++ b/drivers/iommu/dma-reserved-iommu.c @@ -0,0 +1,99 @@ +/* + * Reserved IOVA Management + * + * Copyright (c) 2015 Linaro Ltd. + * www.linaro.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include + +struct reserved_iova_domain { + struct iova_domain *iovad; + int prot; /* iommu protection attributes to be obeyed */ +}; + +int iommu_alloc_reserved_iova_domain(struct iommu_domain *domain, + dma_addr_t iova, size_t size, int prot, + unsigned long order) +{ + unsigned long granule, mask, flags; + struct reserved_iova_domain *rid; + int ret = 0; + + granule = 1UL << order; + mask = granule - 1; + if (iova & mask || (!size) || (size & mask)) + return -EINVAL; + + rid = kzalloc(sizeof(struct reserved_iova_domain), GFP_KERNEL); + if (!rid) + return -ENOMEM; + + rid->iovad = kzalloc(sizeof(struct iova_domain), GFP_KERNEL); + if (!rid->iovad) { + kfree(rid); + return -ENOMEM; + } + + iova_cache_get(); + + init_iova_domain(rid->iovad, granule, + iova >> order, (iova + size - 1) >> order); + + spin_lock_irqsave(&domain->reserved_lock, flags); + + if (domain->reserved_iova_cookie) { + ret = -EEXIST; + goto unlock; + } + + domain->reserved_iova_cookie = rid; + +unlock: + spin_unlock_irqrestore(&domain->reserved_lock, flags); + if (ret) { + put_iova_domain(rid->iovad); + kfree(rid->iovad); + kfree(rid); + iova_cache_put(); + } + return ret; +} +EXPORT_SYMBOL_GPL(iommu_alloc_reserved_iova_domain); + +void iommu_free_reserved_iova_domain(struct iommu_domain *domain) +{ + struct reserved_iova_domain *rid; + unsigned long flags; + int ret = 0; + + spin_lock_irqsave(&domain->reserved_lock, flags); + + rid = (struct reserved_iova_domain *)domain->reserved_iova_cookie; + if (!rid) { + ret = -EINVAL; + goto unlock; + } + + domain->reserved_iova_cookie = NULL; +unlock: + spin_unlock_irqrestore(&domain->reserved_lock, flags); + if (!ret) { + put_iova_domain(rid->iovad); + kfree(rid->iovad); + kfree(rid); + iova_cache_put(); + } +} +EXPORT_SYMBOL_GPL(iommu_free_reserved_iova_domain); diff --git a/include/linux/dma-reserved-iommu.h b/include/linux/dma-reserved-iommu.h new file mode 100644 index 0000000..01ec385 --- /dev/null +++ b/include/linux/dma-reserved-iommu.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015 Linaro Ltd. + * www.linaro.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ +#ifndef __DMA_RESERVED_IOMMU_H +#define __DMA_RESERVED_IOMMU_H + +#include +#include + +struct iommu_domain; + +#ifdef CONFIG_IOMMU_DMA_RESERVED + +/** + * iommu_alloc_reserved_iova_domain: allocate the reserved iova domain + * + * @domain: iommu domain handle + * @iova: base iova address + * @size: iova window size + * @prot: protection attribute flags + * @order: page order + */ +int iommu_alloc_reserved_iova_domain(struct iommu_domain *domain, + dma_addr_t iova, size_t size, int prot, + unsigned long order); + +/** + * iommu_free_reserved_iova_domain: free the reserved iova domain + * + * @domain: iommu domain handle + */ +void iommu_free_reserved_iova_domain(struct iommu_domain *domain); + +#else + +static inline int +iommu_alloc_reserved_iova_domain(struct iommu_domain *domain, + dma_addr_t iova, size_t size, int prot, + unsigned long order) +{ + return -ENOENT; +} + +static inline void +iommu_free_reserved_iova_domain(struct iommu_domain *domain) {} + +#endif /* CONFIG_IOMMU_DMA_RESERVED */ +#endif /* __DMA_RESERVED_IOMMU_H */