From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.123 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796391122; b=0TmJe/SNWurB2bpu9iCFKPVet6qzdDrekNO7xCfiCOXSkrNiK7wOMsnizMq5rwQqaoDO 98hcw1c9rBwYLLVQ7g/TsXZzvRGAJc4YS5sX3/KavBkEq+hiM1D5bfKJQ+Q2KFssLt+Za BPZ/r2IZNAz+P8yhc2ornGa3n+vDGmbqJYNbZgL1FV2KkYeiUhgDMHArG9oDFuGeoNbiV 7BvAZbnDxkzZ4R98SxFnjsygRLQAMha1aovmBQr8zZEDjCgY07t+zbAC/TrymQm06KyGY yKyr+fGeJMKotnlXpPlm2jqeHiuy4FzQ0anIeNf9AlMepRfElai25oDleZDtw2jGR8g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796391122; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=LwTqQGwIz3Inoy62E+6k7BZYM6ioJosbCr4V5NXhDmg=; b=AY/Gaqo+uzdmye3UBrO/J97e6QUV0lmD+Jub32Qk7EgZncFcCcuIHy61kvRJLLi0IdCn Gcfa1dBDIloc21yVA+92v2gbd0Tp+MUMc1E4tGFH7aHNJ2SgkKhTTAp+7f2dBCxqgBw2f u6YCyascitkcmn+XyDC5ryYg9+niCH15Hjmnxmac/3ORCPczLjayyRhiVdcWt/YqjH1tq YvhRnp7Z0YN9Ckhtty3q6chLBafH4+52zuefgEA5TpCg+uwpmVP7f6CsONRKE2ebq6ate NqX4Wm6OkSaTV97V6hhCSJAL4gyH8/gybaRNGY40kkoWweNlQ7FJ7I04YiQZrHywbyg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=pgHRV3a9; arc=none smtp.remote-ip=95.215.58.123 Received: from mta1.migadu.com (out-123.mta1.migadu.com [95.215.58.123]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8D1F606A332 for ; Fri, 4 Dec 2026 13:32:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E8D1F606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=pgHRV3a9 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=rvKO/eIvLZ9y2YIagUUb8qhaqyRUC6Oxh0Vd8lPxIG8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796391119; v=1; x=1796995919; b=pgHRV3a9W8fIVzlhlTMSCDqQfnRaCnv4g0GgnXVumz/3QpEY3C4gJ4JuxdPZOu1N4nhRhnFR uyGxTPsBUWsJMDOcK53H2JJbkhBpPj2ab7A4QKce+UlmT22rwbT18P9NEJpMV5S8GoygCDEGoCz lsoZ5xeFy1Hvlyl3Cxyji/Es= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 6736a709113a9df8; Fri, 04 Dec 2026 13:31:59 +0000 X-Mizu-Trace-ID: 6736a709113a9df8 X-Migadu-Flow: FLOW_OUT From: Andrew Jones To: linux-riscv@lists.infradead.org, iommu@lists.linux.dev Cc: linux-kernel@vger.kernel.org, tomasz.jeznach@linux.dev, jgg@ziepe.ca, jgg@nvidia.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, pjw@kernel.org, palmer@dabbelt.com, anup@brainfault.org, tglx@kernel.org, kevin.tian@intel.com, fangyu.yu@linux.alibaba.com, nutty.liu@hotmail.com Subject: [PATCH v8 1/6] iommu/dma: Prepare MSI physical address lists Date: Fri, 4 Dec 2026 14:31:01 +0100 Message-ID: <20261204133106.664239-2-andrew.jones@linux.dev> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204133106.664239-1-andrew.jones@linux.dev> References: <20261204133106.664239-1-andrew.jones@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit IMSIC affinity changes need every possible target mapped. Introduce iommu_dma_sw_msi_list() to cache one contiguous IOVA range base for a list of targets. Signed-off-by: Andrew Jones Signed-off-by: Andrew Jones Reviewed-by: Jason Gunthorpe --- drivers/iommu/dma-iommu.c | 77 +++++++++++++++++++++++++++++++++++---- drivers/iommu/dma-iommu.h | 10 +++++ 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 7c5abb77f4d6..a828c9eec7f9 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -59,6 +59,8 @@ struct iommu_dma_options { struct iommu_dma_cookie { struct iova_domain iovad; struct list_head msi_page_list; + /* Base IOVA of the complete MSI target list */ + dma_addr_t msi_list_iova; /* Flush queue */ union { struct iova_fq *single_fq; @@ -2250,13 +2252,42 @@ static struct list_head *cookie_msi_pages(const struct iommu_domain *domain) } } +static dma_addr_t iommu_dma_alloc_msi_iova(struct device *dev, struct iommu_domain *domain, + const phys_addr_t *phys_addrs, + unsigned int nr_addrs, size_t granule) +{ + int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; + size_t size = nr_addrs * granule; + dma_addr_t iova; + unsigned int i; + + iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev); + if (!iova) + return 0; + + for (i = 0; i < nr_addrs; i++) { + if (iommu_map(domain, iova + i * granule, phys_addrs[i], granule, + prot, GFP_KERNEL)) + goto out_unmap; + } + return iova; + +out_unmap: + if (i) { + size_t mapped = i * granule; + + WARN_ON_ONCE(iommu_unmap(domain, iova, mapped) != mapped); + } + iommu_dma_free_iova(domain, iova, size, NULL); + return 0; +} + static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, phys_addr_t msi_addr, struct iommu_domain *domain) { struct list_head *msi_page_list = cookie_msi_pages(domain); struct iommu_dma_msi_page *msi_page; dma_addr_t iova; - int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; size_t size = cookie_msi_granule(domain); static DEFINE_MUTEX(msi_prepare_lock); @@ -2281,21 +2312,16 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, if (!msi_page) return NULL; - iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev); + iova = iommu_dma_alloc_msi_iova(dev, domain, &msi_addr, 1, size); if (!iova) goto out_free_page; - if (iommu_map(domain, iova, msi_addr, size, prot, GFP_KERNEL)) - goto out_free_iova; - INIT_LIST_HEAD(&msi_page->list); msi_page->phys = msi_addr; msi_page->iova = iova; list_add(&msi_page->list, msi_page_list); return msi_page; -out_free_iova: - iommu_dma_free_iova(domain, iova, size, NULL); out_free_page: kfree(msi_page); return NULL; @@ -2317,6 +2343,43 @@ int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc, return 0; } +/* + * All calls for a DMA domain must supply the same complete target list. + * The group mutex serializes creation and reuse of the cached mapping. + */ +int iommu_dma_sw_msi_list(struct iommu_domain *domain, struct msi_desc *desc, + const phys_addr_t *phys_addrs, unsigned int nr_addrs, + size_t granule) +{ + struct device *dev = msi_desc_to_dev(desc); + struct iommu_dma_cookie *cookie; + unsigned int i; + + iommu_group_mutex_assert(dev); + if (domain->cookie_type != IOMMU_COOKIE_DMA_IOVA) + return -EOPNOTSUPP; + + cookie = domain->iova_cookie; + if (granule < PAGE_SIZE || granule != cookie->iovad.granule) + return -EOPNOTSUPP; + if (!nr_addrs || nr_addrs > SIZE_MAX / granule) + return -EINVAL; + + if (!cookie->msi_list_iova) { + for (i = 0; i < nr_addrs; i++) + if (!IS_ALIGNED(phys_addrs[i], granule)) + return -EINVAL; + + cookie->msi_list_iova = iommu_dma_alloc_msi_iova(dev, domain, phys_addrs, + nr_addrs, granule); + if (!cookie->msi_list_iova) + return -ENOMEM; + } + + msi_desc_set_iommu_msi_iova(desc, cookie->msi_list_iova, ilog2(granule)); + return 0; +} + static int iommu_dma_init(void) { if (is_kdump_kernel()) diff --git a/drivers/iommu/dma-iommu.h b/drivers/iommu/dma-iommu.h index 040d00252563..2a36ed736910 100644 --- a/drivers/iommu/dma-iommu.h +++ b/drivers/iommu/dma-iommu.h @@ -21,6 +21,9 @@ void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list); int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc, phys_addr_t msi_addr); +int iommu_dma_sw_msi_list(struct iommu_domain *domain, struct msi_desc *desc, + const phys_addr_t *phys_addrs, unsigned int nr_addrs, + size_t granule); extern bool iommu_dma_forcedac; @@ -59,5 +62,12 @@ static inline int iommu_dma_sw_msi(struct iommu_domain *domain, return -ENODEV; } +static inline int iommu_dma_sw_msi_list(struct iommu_domain *domain, struct msi_desc *desc, + const phys_addr_t *phys_addrs, unsigned int nr_addrs, + size_t granule) +{ + return -ENODEV; +} + #endif /* CONFIG_IOMMU_DMA */ #endif /* __DMA_IOMMU_H */ -- 2.56.1