From patchwork Tue Apr 19 16:56:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8881901 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 58D9B9F1C1 for ; Tue, 19 Apr 2016 16:59:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4AC1820172 for ; Tue, 19 Apr 2016 16:59:49 +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 432152017D for ; Tue, 19 Apr 2016 16:59:48 +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 1asYyw-0002f4-66; Tue, 19 Apr 2016 16:58:34 +0000 Received: from mail-wm0-x235.google.com ([2a00:1450:400c:c09::235]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1asYxm-00013q-D3 for linux-arm-kernel@lists.infradead.org; Tue, 19 Apr 2016 16:57:23 +0000 Received: by mail-wm0-x235.google.com with SMTP id u206so40301448wme.1 for ; Tue, 19 Apr 2016 09:57:02 -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=y5WbS7NTuBBb5nZhvAOaY45tIMNVPln+47xDmMfX5LI=; b=hDqQfSWA88BzBUIZuiAjMZXGOu2B5NtusgHZYH23AKt6JPlDSqEEz/HeQFra83yzdv yRM/Jn9+ZAgHWHno6VkrMXstCVxCNinKMFcvgkTwh/MI+9Xbm8WIhHjJP0rvJFEFOgiH s2MOQ38SJG6NxDN4g4m15ftzePWImxbZOU42w= 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=y5WbS7NTuBBb5nZhvAOaY45tIMNVPln+47xDmMfX5LI=; b=fn+ALKZRtwZp++9Gg76rjARiZcMKeGnTKe2pvh/fmAlGelr/bFND+cSxQKTAWYqOlE jvdprWYfaTfKzC8u8be5fgE3o8aJCWy+8MOMSexlDQSb15u83eQ3HbgkWU8WbPE9fZPP sgR77201zVyObnucrbSp4fjaR0AXNLoMiH6NKVOkQiHHvaZKGb4xkrp7frQ/+Q5y0uki bPZg28WH0OeaLUeeZ4gtmHSvdSe2y49YbE9xlHrK5hprwyqggWawaAikZtlXiRgzYM5E DWL813VWxkNyUh4vaGHpBhizHi2RpV4p9RkGTYqpKhz4/vGZ0GBFaIag7jyQ+F+jYhzA VOpg== X-Gm-Message-State: AOPr4FV+EhoX1ctwrLidrBPiK5xPYCX5GgXP0LmIp8NVoRSIPOXqriov9BQmIALs4SWI43H4 X-Received: by 10.28.172.133 with SMTP id v127mr27028675wme.1.1461085020980; Tue, 19 Apr 2016 09:57:00 -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.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 19 Apr 2016 09:56:59 -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 09/10] iommu/dma-reserved-iommu: iommu_msi_mapping_translate_msg Date: Tue, 19 Apr 2016 16:56:33 +0000 Message-Id: <1461084994-2355-10-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_095722_618030_69BED8CE X-CRM114-Status: GOOD ( 17.54 ) 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 iommu_msi_mapping_translate_msg whose role consists in detecting whether the device's MSIs must to be mapped into an IOMMU. It case it must, the function overrides the MSI msg originally composed and replaces the doorbell's PA by a pre-allocated and pre-mapped reserved IOVA. In case the corresponding PA region is not found, the function returns an error. This function is likely to be called in some code that cannot sleep. This is the reason why the allocation/mapping is done separately. Signed-off-by: Eric Auger --- v7: creation --- drivers/iommu/dma-reserved-iommu.c | 69 ++++++++++++++++++++++++++++++++++++++ include/linux/dma-reserved-iommu.h | 27 +++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/drivers/iommu/dma-reserved-iommu.c b/drivers/iommu/dma-reserved-iommu.c index 907a17f..603ee45 100644 --- a/drivers/iommu/dma-reserved-iommu.c +++ b/drivers/iommu/dma-reserved-iommu.c @@ -18,6 +18,14 @@ #include #include #include +#include + +#ifdef CONFIG_PHYS_ADDR_T_64BIT +#define msg_to_phys_addr(msg) \ + (((phys_addr_t)((msg)->address_hi) << 32) | (msg)->address_lo) +#else +#define msg_to_phys_addr(msg) ((msg)->address_lo) +#endif struct reserved_iova_domain { struct iova_domain *iovad; @@ -351,3 +359,64 @@ struct iommu_domain *iommu_msi_mapping_desc_to_domain(struct msi_desc *desc) return d; } EXPORT_SYMBOL_GPL(iommu_msi_mapping_desc_to_domain); + +static dma_addr_t iommu_find_reserved_iova(struct iommu_domain *domain, + phys_addr_t addr, size_t size) +{ + unsigned long base_pfn, end_pfn, nb_iommu_pages, order, flags; + size_t iommu_page_size, binding_size; + struct iommu_reserved_binding *b; + phys_addr_t aligned_base, offset; + dma_addr_t iova = DMA_ERROR_CODE; + struct iova_domain *iovad; + + spin_lock_irqsave(&domain->reserved_lock, flags); + + iovad = (struct iova_domain *)domain->reserved_iova_cookie; + + if (!iovad) + goto unlock; + + order = iova_shift(iovad); + base_pfn = addr >> order; + end_pfn = (addr + size - 1) >> order; + aligned_base = base_pfn << order; + offset = addr - aligned_base; + nb_iommu_pages = end_pfn - base_pfn + 1; + iommu_page_size = 1 << order; + binding_size = nb_iommu_pages * iommu_page_size; + + b = find_reserved_binding(domain, aligned_base, binding_size); + if (b && (b->addr <= aligned_base) && + (aligned_base + binding_size <= b->addr + b->size)) + iova = b->iova + offset + aligned_base - b->addr; +unlock: + spin_unlock_irqrestore(&domain->reserved_lock, flags); + return iova; +} + +int iommu_msi_mapping_translate_msg(struct irq_data *data, struct msi_msg *msg) +{ + struct iommu_domain *d; + struct msi_desc *desc; + dma_addr_t iova; + + desc = irq_data_get_msi_desc(data); + if (!desc) + return -EINVAL; + + d = iommu_msi_mapping_desc_to_domain(desc); + if (!d) + return 0; + + iova = iommu_find_reserved_iova(d, msg_to_phys_addr(msg), + sizeof(phys_addr_t)); + + if (iova == DMA_ERROR_CODE) + return -EINVAL; + + msg->address_lo = lower_32_bits(iova); + msg->address_hi = upper_32_bits(iova); + return 0; +} +EXPORT_SYMBOL_GPL(iommu_msi_mapping_translate_msg); diff --git a/include/linux/dma-reserved-iommu.h b/include/linux/dma-reserved-iommu.h index 8373929..04e1912f 100644 --- a/include/linux/dma-reserved-iommu.h +++ b/include/linux/dma-reserved-iommu.h @@ -20,6 +20,8 @@ struct iommu_domain; struct msi_desc; +struct irq_data; +struct msi_msg; #ifdef CONFIG_IOMMU_DMA_RESERVED @@ -82,6 +84,25 @@ void iommu_put_reserved_iova(struct iommu_domain *domain, phys_addr_t addr); */ struct iommu_domain *iommu_msi_mapping_desc_to_domain(struct msi_desc *desc); +/** + * iommu_msi_mapping_translate_msg: in case the MSI transaction is translated + * by an IOMMU, the msg address must be an IOVA instead of a physical address. + * This function overwrites the original MSI message containing the doorbell + * physical address, result of the primary composition, with the doorbell IOVA. + * + * The doorbell physical address must be bound previously to an IOVA using + * iommu_get_reserved_iova + * + * @data: irq data handle + * @msg: original msi message containing the PA to be overwritten with + * the IOVA + * + * return 0 if the MSI does not need to be mapped or when the PA/IOVA + * were successfully swapped; return -EINVAL if the addresses need + * to be swapped but not IOMMU binding is found + */ +int iommu_msi_mapping_translate_msg(struct irq_data *data, struct msi_msg *msg); + #else static inline int @@ -111,5 +132,11 @@ iommu_msi_mapping_desc_to_domain(struct msi_desc *desc) return NULL; } +static inline int iommu_msi_mapping_translate_msg(struct irq_data *data, + struct msi_msg *msg) +{ + return 0; +} + #endif /* CONFIG_IOMMU_DMA_RESERVED */ #endif /* __DMA_RESERVED_IOMMU_H */