From patchwork Tue Aug 2 17:15:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 9259291 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 B5AB260865 for ; Tue, 2 Aug 2016 17:20:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A0D2F27317 for ; Tue, 2 Aug 2016 17:20:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 94D8A28407; Tue, 2 Aug 2016 17:20:50 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id 2FFA127317 for ; Tue, 2 Aug 2016 17:20:49 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bUdLB-0001Pi-7T; Tue, 02 Aug 2016 17:18:53 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bUdJ1-00080a-1t for linux-arm-kernel@lists.infradead.org; Tue, 02 Aug 2016 17:16:40 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9F5F3550B1; Tue, 2 Aug 2016 17:16:22 +0000 (UTC) Received: from localhost.redhat.com (vpn1-7-179.ams2.redhat.com [10.36.7.179]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u72HFgTq009851; Tue, 2 Aug 2016 13:16:17 -0400 From: Eric Auger To: eric.auger@redhat.com, eric.auger.pro@gmail.com, christoffer.dall@linaro.org, marc.zyngier@arm.com, robin.murphy@arm.com, alex.williamson@redhat.com, will.deacon@arm.com, joro@8bytes.org, tglx@linutronix.de, jason@lakedaemon.net, linux-arm-kernel@lists.infradead.org Subject: [PATCH v12 7/8] iommu/msi-iommu: iommu_msi_msg_pa_to_va Date: Tue, 2 Aug 2016 17:15:36 +0000 Message-Id: <1470158137-6931-8-git-send-email-eric.auger@redhat.com> In-Reply-To: <1470158137-6931-1-git-send-email-eric.auger@redhat.com> References: <1470158137-6931-1-git-send-email-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 02 Aug 2016 17:16:22 +0000 (UTC) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160802_101639_208260_0AC8C077 X-CRM114-Status: GOOD ( 17.78 ) 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: drjones@redhat.com, kvm@vger.kernel.org, Jean-Philippe.Brucker@arm.com, Manish.Jaggi@caviumnetworks.com, p.fedin@samsung.com, linux-kernel@vger.kernel.org, Bharat.Bhushan@freescale.com, iommu@lists.linux-foundation.org, pranav.sawargaonkar@gmail.com, dennis.chen@arm.com, robert.richter@caviumnetworks.com, yehuday@marvell.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-Virus-Scanned: ClamAV using ClamSMTP Introduce iommu_msi_msg_pa_to_va 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 -> v8: - renamed from iommu_msi_mapping_translate_msg to iommu_msi_msg_pa_to_va and now takes a struct device * as parameter v7: creation --- drivers/iommu/msi-iommu.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/msi-iommu.h | 25 +++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/drivers/iommu/msi-iommu.c b/drivers/iommu/msi-iommu.c index 58912ae..4ca6f584 100644 --- a/drivers/iommu/msi-iommu.c +++ b/drivers/iommu/msi-iommu.c @@ -20,6 +20,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 doorbell_mapping { struct kref kref; @@ -269,3 +277,50 @@ struct iommu_domain *iommu_msi_domain(struct device *dev) } EXPORT_SYMBOL_GPL(iommu_msi_domain); +static dma_addr_t iommu_msi_find_iova(struct iommu_domain *domain, + phys_addr_t addr, size_t size) +{ + struct doorbell_mapping_info *dmi = domain->msi_cookie; + struct iova_domain *iovad = domain->iova_cookie; + struct doorbell_mapping *mapping; + dma_addr_t iova = DMA_ERROR_CODE; + phys_addr_t aligned_base, offset; + size_t binding_size; + + if (!iovad || !dmi) + return iova; + + offset = iova_offset(iovad, addr); + aligned_base = addr - offset; + binding_size = iova_align(iovad, size + offset); + + spin_lock(&dmi->lock); + + mapping = search_msi_doorbell_mapping(dmi, addr, size); + if (mapping) + iova = mapping->iova + offset + aligned_base - mapping->addr; + + spin_unlock(&dmi->lock); + return iova; +} + +int iommu_msi_msg_pa_to_va(struct device *dev, struct msi_msg *msg) +{ + struct iommu_domain *d = iommu_msi_domain(dev); + dma_addr_t iova; + + if (!d) + return 0; + + iova = iommu_msi_find_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_msg_pa_to_va); + diff --git a/include/linux/msi-iommu.h b/include/linux/msi-iommu.h index 44a696d..b80487e 100644 --- a/include/linux/msi-iommu.h +++ b/include/linux/msi-iommu.h @@ -19,6 +19,7 @@ #include struct iommu_domain; +struct msi_msg; #ifdef CONFIG_IOMMU_MSI @@ -90,6 +91,24 @@ void iommu_msi_put_doorbell_iova(struct iommu_domain *domain, phys_addr_t addr); */ struct iommu_domain *iommu_msi_domain(struct device *dev); +/** + * iommu_msi_msg_pa_to_va - in case a device's 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's + * physical address with the doorbell's pre-allocated IOVA, if any. + * @dev: device emitting the MSI + * @msg: original MSI message containing the PA to be overwritten with the IOVA + * + * The doorbell physical address must be bound previously to an IOVA using + * iommu_msi_get_doorbell_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_msg_pa_to_va(struct device *dev, struct msi_msg *msg); + + #else static inline int @@ -114,5 +133,11 @@ static inline struct iommu_domain *iommu_msi_domain(struct device *dev) return NULL; } +static inline int iommu_msi_msg_pa_to_va(struct device *dev, + struct msi_msg *msg) +{ + return 0; +} + #endif /* CONFIG_IOMMU_MSI */ #endif /* __MSI_IOMMU_H */