From patchwork Thu Apr 28 08:15:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8966181 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B9E0A9F1C1 for ; Thu, 28 Apr 2016 08:18:09 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 943432027D for ; Thu, 28 Apr 2016 08:18:08 +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 7DBB320295 for ; Thu, 28 Apr 2016 08:18:04 +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 1avh7q-000136-Qc; Thu, 28 Apr 2016 08:16:42 +0000 Received: from mail-wm0-x22d.google.com ([2a00:1450:400c:c09::22d]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1avh7D-0000Jf-Gz for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2016 08:16:04 +0000 Received: by mail-wm0-x22d.google.com with SMTP id a17so51626471wme.0 for ; Thu, 28 Apr 2016 01:15:42 -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=MHlrjeDP0oychYf5bgOqA1EZo4Zn1tGn6w4HaM28Ep8=; b=UaI2zjRJeuq11w6sdEjqL2V1z7fN8rWjOTyNPxTB7OEebLsXwCCACj+U2QPTTW23YE aXXjmN3HwXUqVW7Bg0dozY+FQIManLSTHVJiCI0ze+XD3FuyU4RWA+EAIWkz45RkKrFF uyHn7RwqxscTsbvTMEMzf0RHuhdgbsyja9gk4= 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=MHlrjeDP0oychYf5bgOqA1EZo4Zn1tGn6w4HaM28Ep8=; b=lEh66R1kfk7rqrlDkv0pcUjM9sbkeXw/AR31MFJqJtc08UbRuvMvZDz0HzZzJOxmBs w4RCZV4l180SBkImDGCO2yEDpav4ZSUTu/S8gshg/GCQvHW5ejQEmiP09GYrbB9vhmL7 U1zwXK6Opsag7XOWeyzdyLrpl3ndpYgVlf7JX4qBbr6eI3LWvgO7rtoo+H34Eje2dIJU H9d6ExBNAuiRGz/8x6oJAFkMKCLU2q1A1CCyiaICwjOjo2vUM1WojyhrlmaLh0Ns/xdx ygblfxytjTYMBM7SmQumekmwQHBRsd5z41KLatCQoTVq1ZX9EPvKgt5k7mxAfY6k5G0h k9Jg== X-Gm-Message-State: AOPr4FUEIUEK0dvIx3G6nvzdYbpnzfupk+lPpcKq+F7R4UJ8nCe5tZeDYXqryxG2KceqCDLI X-Received: by 10.28.45.216 with SMTP id t207mr14350182wmt.40.1461831341793; Thu, 28 Apr 2016 01:15:41 -0700 (PDT) Received: from new-host-46.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id h8sm12749860wmd.2.2016.04.28.01.15.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 28 Apr 2016 01:15:39 -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 v8 4/8] iommu/msi-iommu: initialization Date: Thu, 28 Apr 2016 08:15:19 +0000 Message-Id: <1461831323-5480-5-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1461831323-5480-1-git-send-email-eric.auger@linaro.org> References: <1461831323-5480-1-git-send-email-eric.auger@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160428_011603_856930_82E20373 X-CRM114-Status: GOOD ( 20.31 ) 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 iommu_get/put_msi_cookie allocates/frees the resource used to store and ref count the MSI doorbell mappings. iommu_msi_set_aperture initializes the iova domain used for MSI IOVA allocation. The implementation relies on dma-iommu API and iova API. New msi functions are fully implemented if CONFIG_IOMMU_MSI is set. Signed-off-by: Eric Auger --- v8: - new design where msi-iommu relies on dma-iommu - remove the iommu_domain * from the doorbell_mapping struct - added is_aperture_set 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 | 7 ++++ drivers/iommu/Makefile | 1 + drivers/iommu/msi-iommu.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/msi-iommu.h | 65 ++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 drivers/iommu/msi-iommu.c create mode 100644 include/linux/msi-iommu.h diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index dd1dc39..0078b72 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -74,6 +74,11 @@ config IOMMU_DMA select IOMMU_IOVA select NEED_SG_DMA_LENGTH +# IOMMU MSI mapping +config IOMMU_MSI + bool + select IOMMU_DMA + config FSL_PAMU bool "Freescale IOMMU support" depends on PPC32 @@ -307,6 +312,7 @@ config SPAPR_TCE_IOMMU config ARM_SMMU bool "ARM Ltd. System MMU (SMMU) Support" depends on (ARM64 || ARM) && MMU + select IOMMU_MSI select IOMMU_API select IOMMU_IO_PGTABLE_LPAE select ARM_DMA_USE_IOMMU if ARM @@ -320,6 +326,7 @@ config ARM_SMMU config ARM_SMMU_V3 bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" depends on ARM64 && PCI + select IOMMU_MSI 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..a381e66 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_MSI) += msi-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/msi-iommu.c b/drivers/iommu/msi-iommu.c new file mode 100644 index 0000000..d38725e --- /dev/null +++ b/drivers/iommu/msi-iommu.c @@ -0,0 +1,95 @@ +/* + * 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 +#include +#include +#include + +struct doorbell_mapping { + struct kref kref; + struct list_head next; + phys_addr_t addr; + dma_addr_t iova; + size_t size; +}; + +struct doorbell_mapping_info { + struct list_head list; /* list of doorbell mapping entries */ + bool is_aperture_set; /* Is the MSI IOVA aperture set? */ + spinlock_t lock; +}; + +int iommu_get_msi_cookie(struct iommu_domain *domain) +{ + struct doorbell_mapping_info *dmi; + int ret; + + if (domain->msi_cookie || domain->iova_cookie) + return -EINVAL; + + ret = iommu_get_dma_cookie(domain); + if (ret) + return ret; + + dmi = kzalloc(sizeof(*dmi), GFP_KERNEL); + + INIT_LIST_HEAD(&dmi->list); + spin_lock_init(&dmi->lock); + iova_cache_get(); + + domain->msi_cookie = dmi; + + return dmi ? 0 : -ENOMEM; +} +EXPORT_SYMBOL(iommu_get_msi_cookie); + +void iommu_put_msi_cookie(struct iommu_domain *domain) +{ + struct doorbell_mapping_info *dmi = domain->msi_cookie; + + if (!dmi) + return; + + domain->msi_cookie = NULL; + + WARN_ON(!list_empty(&dmi->list)); + + kfree(dmi); + iommu_put_dma_cookie(domain); + iova_cache_put(); +} +EXPORT_SYMBOL(iommu_put_msi_cookie); + +int iommu_msi_set_aperture(struct iommu_domain *domain, + dma_addr_t start, dma_addr_t end) +{ + struct doorbell_mapping_info *dmi = domain->msi_cookie; + int ret; + + if (!dmi) + return -ENODEV; + + ret = iommu_dma_init_domain(domain, start, end - start + 1); + if (!ret) + dmi->is_aperture_set = true; + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_msi_set_aperture); + diff --git a/include/linux/msi-iommu.h b/include/linux/msi-iommu.h new file mode 100644 index 0000000..392aa6f --- /dev/null +++ b/include/linux/msi-iommu.h @@ -0,0 +1,65 @@ +/* + * 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 __MSI_IOMMU_H +#define __MSI_IOMMU_H + +#include +#include + +struct iommu_domain; + +#ifdef CONFIG_IOMMU_MSI + +/** + * iommu_get_msi_cookie - Acquire MSI mapping resources for a domain + * @domain: IOMMU domain to prepare for MSI mapping + * + * IOMMU drivers which require MSI mapping should normally call this + * from their domain_alloc callback when domain->type == + * IOMMU_DOMAIN_UNMANAGED. + */ +int iommu_get_msi_cookie(struct iommu_domain *domain); + +/** + * iommu_put_msi_cookie - Release a domain's MSI mapping resources + * @domain: IOMMU domain previously prepared by iommu_get_msi_cookie() + * + * IOMMU drivers requesting MSI mapping should normally call this from + * their domain_free callback. + */ +void iommu_put_msi_cookie(struct iommu_domain *domain); + +/** + * iommu_msi_set_aperture: allocate the msi iova domain + * according to the specified start/end IOVAs + * + * @domain: iommu domain handle + * @start: MSI iova start address + * @end: MSI iova end address + */ +int iommu_msi_set_aperture(struct iommu_domain *domain, + dma_addr_t start, dma_addr_t end); + +#else + +static inline int +iommu_msi_set_aperture(struct iommu_domain *domain, + dma_addr_t start, dma_addr_t end) +{ + return -ENOENT; +} + +#endif /* CONFIG_IOMMU_MSI */ +#endif /* __MSI_IOMMU_H */