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: Auger Eric X-Patchwork-Id: 66120 Delivered-To: patches@linaro.org Received: by 10.140.93.198 with SMTP id d64csp1968834qge; Tue, 19 Apr 2016 09:57:08 -0700 (PDT) X-Received: by 10.28.144.20 with SMTP id s20mr4959411wmd.12.1461085021327; Tue, 19 Apr 2016 09:57:01 -0700 (PDT) Return-Path: Received: from mail-wm0-x229.google.com (mail-wm0-x229.google.com. [2a00:1450:400c:c09::229]) by mx.google.com with ESMTPS id u3si5183300wmg.22.2016.04.19.09.57.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Apr 2016 09:57:01 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::229 as permitted sender) client-ip=2a00:1450:400c:c09::229; 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::229 as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-wm0-x229.google.com with SMTP id e201so21407040wme.0 for ; Tue, 19 Apr 2016 09:57:01 -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=eVXMFwUODPt8GNP/WnaZQtnSpcDq445Fn8NfhP8e5249d1HHswUU5VTtc+cEqYXMRL 0W4gtBRkRYWasN9FDbFBQ8hMCuPZ6tk+2rRd4UOyjJZ9LJU/g7gpV4ZybFCoCwmBD6As Nd75YuiY1GAVRJso4XZwNQ9DDU5zSmH5GYBEAO/yFQFEEACgf85biAHKgQQ3JhuwTh64 8dsf59jQAqTAzX5Gt6Nx8+DIFB5dzUjRQefFB19bWqtHQSIDn0FrVhu/siCqKhiTG1L9 09f2W4u/5xojyQ8On3QCPk+llUOO8MQHdE4xC2quWIrhEPCWzbVZdZSBxfOo1KWQ+8OE PFQw== X-Gm-Message-State: AOPr4FVDCYpxCPtuQjheh/mje3wPs33A5LeO3aWVa0KbsSq9+HnJ8s/Owwi0+/nr/b3dqidaWPk= X-Received: by 10.28.172.133 with SMTP id v127mr27028675wme.1.1461085020980; Tue, 19 Apr 2016 09:57:00 -0700 (PDT) Return-Path: 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 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 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> 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(+) -- 1.9.1 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 */