From patchwork Thu Apr 28 08:15:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 66865 Delivered-To: patches@linaro.org Received: by 10.140.93.198 with SMTP id d64csp75581qge; Thu, 28 Apr 2016 01:15:52 -0700 (PDT) X-Received: by 10.28.103.2 with SMTP id b2mr29921332wmc.66.1461831349022; Thu, 28 Apr 2016 01:15:49 -0700 (PDT) Return-Path: Received: from mail-wm0-x22e.google.com (mail-wm0-x22e.google.com. [2a00:1450:400c:c09::22e]) by mx.google.com with ESMTPS id wn5si9301640wjb.196.2016.04.28.01.15.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Apr 2016 01:15:49 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22e as permitted sender) client-ip=2a00:1450:400c:c09::22e; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22e as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-wm0-x22e.google.com with SMTP id a17so51631433wme.0 for ; Thu, 28 Apr 2016 01:15:48 -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=p190GkMp1iE8P6aeHv4+d+bqW4taWWJgyIg4uq9ZTs8=; b=f1RcZVSk9H0cvBBZYrS1rv0ayrtYhHMZuk69HcvS2S/5TYEJEkciOGjwhc+kgdVJbU 1UreuL0309ERLcILGjvuwTId5wr0U0pji5rEfejZ+sVeNI+RDfJT5fhHjulW1Nk5IU16 10sl1ilZmxRZK4x3Od2fj5UQre8MPCcrjrOrM= 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=p190GkMp1iE8P6aeHv4+d+bqW4taWWJgyIg4uq9ZTs8=; b=cNPTQ79EzDU2i+nV32/85HaGjoyoIluF1aFW8K1Xno9mwoW/DWQ434AAEM0emrNaOw xbZVlYV8rwQRNYVldvW5ZmepMblHUBJPnAMllo53HBE7ZHPjHhirYO64BPLyNW+lQjNS dDE90obxgYNEOyl9VSH14cJJD3FgIySZjpVQdQf9N0AjZcWDZJiPM/W7v/LvCsJ3oOLK Ykm49R577hsEWCg0NWfisNNFdyddOrllcCm5pDoLneJqof5XzW3v2mKiLIzgr8tHDQjs OJ7F3lTGpNpSVQtENm7N0EgIpVQfcSRvaQX/68e81z2BwUGtGRzm5D+EKlPRwxxKdHQj sqYQ== X-Gm-Message-State: AOPr4FXymOUVp3wuyvItIi6ZTcQb+Hran1t79NIHYnhZ3X6450VaIwciIxgLjyP2fxBL5U7lq9A= X-Received: by 10.194.174.231 with SMTP id bv7mr13719152wjc.17.1461831348764; Thu, 28 Apr 2016 01:15:48 -0700 (PDT) Return-Path: 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.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 28 Apr 2016 01:15:47 -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 Cc: patches@linaro.org, linux-kernel@vger.kernel.org, Bharat.Bhushan@freescale.com, pranav.sawargaonkar@gmail.com, p.fedin@samsung.com, iommu@lists.linux-foundation.org, Jean-Philippe.Brucker@arm.com, julien.grall@arm.com Subject: [PATCH v8 7/8] iommu/msi-iommu: iommu_msi_msg_pa_to_va Date: Thu, 28 Apr 2016 08:15:22 +0000 Message-Id: <1461831323-5480-8-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> 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(+) -- 1.9.1 diff --git a/drivers/iommu/msi-iommu.c b/drivers/iommu/msi-iommu.c index 023ff17..fb3007f 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; @@ -260,3 +268,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 */