From patchwork Wed May 4 11:40:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 9012601 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 AF9419F1D3 for ; Wed, 4 May 2016 11:43:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B7804203A4 for ; Wed, 4 May 2016 11:43: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 914FF2039C for ; Wed, 4 May 2016 11:43: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 1axvBe-0002XD-RX; Wed, 04 May 2016 11:41:50 +0000 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1axvAh-0001WK-Br for linux-arm-kernel@lists.infradead.org; Wed, 04 May 2016 11:40:53 +0000 Received: by mail-wm0-x229.google.com with SMTP id g17so88779277wme.1 for ; Wed, 04 May 2016 04:40:31 -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=MvX6KE5f0ZOHo2wri5i2/BAFXbAyosVJ0cRv//oW+Ts=; b=BoK4Y3YfHDTYFZxDlKpdJfZGY5uhha4ytHV7gNpgt55jIjdU8YZ+1vFEai2+4dZ53n 7OEgbUwJbCdtthzaOYmeDL+oVTeGI5Pc4pnZx1asd7RnjC6L3t4dBGh+M28xKEkJi9RN vhwvOm0D8n+6pnL8LVedc6VBs1v1SLw1JKM5I= 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=MvX6KE5f0ZOHo2wri5i2/BAFXbAyosVJ0cRv//oW+Ts=; b=my254UjWcbWmEbjptgquwzRQ0YZHmH9ybjqHJkE+T/c6w3xFsb66SFb68htxyifoH+ YSjy5w+NNtxvXrT6VSOLKOng8UA5F27uSnupnK2QheugsierQhbJLYYFsIcbz5lw4B7W yjgU6uBYuBGhCie0E9V6n9QdEv2d1g74swnrA5jGM8Nrgm8UovSFRKwoGhuhuJjONzkN +Kr1GT9QtG+5tM8yw7nOZC2aTSB4bh/NO0iF8nyYUOeWrC8NhXhT1QChj7ONde6XXv7F a/fG9NBmHo6alce3R0fZ//MGwEWMpHKFDb56O2HbszjabEIiLS68XP0GueXWYRFhVI1S 12UQ== X-Gm-Message-State: AOPr4FWLNtFlezTsGxTTQY76oTuN+NvlgD3TZ8/zt1i+BgpC95zGfco37fCOgKIkKcBpIyfu X-Received: by 10.28.101.215 with SMTP id z206mr29093482wmb.37.1462362030022; Wed, 04 May 2016 04:40:30 -0700 (PDT) Received: from new-host-61.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id u6sm3766963wjh.2.2016.05.04.04.40.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 04 May 2016 04:40:28 -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 v9 7/8] iommu/msi-iommu: iommu_msi_msg_pa_to_va Date: Wed, 4 May 2016 11:40:06 +0000 Message-Id: <1462362007-2753-8-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1462362007-2753-1-git-send-email-eric.auger@linaro.org> References: <1462362007-2753-1-git-send-email-eric.auger@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160504_044051_780832_744215FE X-CRM114-Status: GOOD ( 18.25 ) X-Spam-Score: -2.0 (--) 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, 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-Spam-Status: No, score=-6.2 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_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 | 26 ++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/drivers/iommu/msi-iommu.c b/drivers/iommu/msi-iommu.c index 6d52985..d71e164 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; @@ -262,3 +270,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 114bd69..c1c9bd5 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,25 @@ 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. + * + * The doorbell physical address must be bound previously to an IOVA using + * iommu_msi_get_doorbell_iova + * + * @dev: device emitting the MSI + * @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_msg_pa_to_va(struct device *dev, struct msi_msg *msg); + + #else static inline int @@ -114,5 +134,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 */