* [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support
@ 2026-08-26 14:52 Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 1/8] iommu/dma: Prepare MSI physical address lists Andrew Jones
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:52 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
The RISC-V IOMMU MSI remapping series [1] needs to prepare mappings for
every possible IMSIC target before MSI composition may occur in atomic
context. The previous approach mapped each physical address separately
and kept a driver-owned PA-to-IOVA lookup table.
Jason Gunthorpe suggested passing the complete list of physical
addresses through the existing MSI preparation flow. This series
implements that idea as iommu_dma_prepare_msi_list(), which maps the
ordered list into one contiguous IOVA range and records its base and
granule shift in the MSI descriptor. The interrupt-remapping driver can
then derive the IOVA for each list entry without maintaining a separate
IOVA array.
Both DMA-IOMMU and iommufd backends are extended to support these list
mappings. The existing per-page mapping objects are retained, while
the first object identifies the complete range so identical lists can
reuse an existing mapping. iommufd allocates and installs a complete
range atomically and grows its software-MSI bitmaps beyond their
previous 64-entry limit.
This is being posted separately from the RISC-V MSI remapping work for
early RFC review of the common DMA-IOMMU and iommufd interfaces. The
RISC-V adaptation to iommu_dma_prepare_msi_list() is still in progress
and will be posted later.
LLM-based coding assistants were used during development for code
exploration, patch review, test execution, and drafting and editing
commit messages and this cover letter. I reviewed and finalized all
resulting code and text. Per-patch Assisted-by tags are omitted in
light of the ongoing discussion about simplifying coding-assistant
attribution[2].
Thanks,
drew
[1] https://lore.kernel.org/all/20260820214150.545737-1-andrew.jones@oss.qualcomm.com/
[2] https://lore.kernel.org/all/20260701-work-coding-assistants-v1-1-a20a94d1d606@kernel.org/
Andrew Jones (8):
iommu/dma: Prepare MSI physical address lists
iommufd: Convert struct iommufd_sw_msi_maps to a growable bitmap
iommufd: Split software MSI map lookup and allocation
iommufd: Bound software MSI mappings to the reserved range
iommufd: Prepare software MSI maps for address lists
iommufd: Install software MSI map ranges atomically
iommufd: Prepare software MSI installation for address lists
iommu/dma: Introduce iommu_dma_prepare_msi_list()
drivers/iommu/dma-iommu.c | 116 ++++++---
drivers/iommu/dma-iommu.h | 13 +-
drivers/iommu/iommu-priv.h | 7 +-
drivers/iommu/iommu.c | 109 ++++++--
drivers/iommu/iommufd/device.c | 20 +-
drivers/iommu/iommufd/driver.c | 315 ++++++++++++++++++++----
drivers/iommu/iommufd/hw_pagetable.c | 1 +
drivers/iommu/iommufd/io_pagetable.c | 9 +-
drivers/iommu/iommufd/iommufd_private.h | 51 +++-
include/linux/iommu.h | 8 +
10 files changed, 528 insertions(+), 121 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 1/8] iommu/dma: Prepare MSI physical address lists
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
@ 2026-08-26 14:52 ` Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 2/8] iommufd: Convert struct iommufd_sw_msi_maps to a growable bitmap Andrew Jones
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:52 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
Software MSI mappings may cover an ordered list of physical addresses
which must be mapped into one contiguous IOVA range. Extend the internal
DMA-IOMMU mapping helpers to accept an address array, address count, and
required mapping granule.
Teach iommu_dma_get_msi_page() to map the list into one contiguous IOVA
allocation. Keep the existing per-page cache entries and mark the first
entry with the size of the allocation so an identical list can reuse the
mapping without changing the existing cache representation.
This prepares for the forthcoming iommu_dma_prepare_msi_list() API.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/dma-iommu.c | 115 ++++++++++++++++++++++++++++----------
1 file changed, 87 insertions(+), 28 deletions(-)
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 9a07eb39336e..c256531ff19f 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -42,6 +42,7 @@ struct iommu_dma_msi_page {
struct list_head list;
dma_addr_t iova;
phys_addr_t phys;
+ size_t range_size; /* IOVA range size, or 0 if not the first map */
};
enum iommu_dma_queue_type {
@@ -481,7 +482,7 @@ static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie,
num_pages = iova_align(iovad, end - start) >> iova_shift(iovad);
for (i = 0; i < num_pages; i++) {
- msi_page = kmalloc_obj(*msi_page);
+ msi_page = kzalloc_obj(*msi_page);
if (!msi_page)
return -ENOMEM;
@@ -2196,15 +2197,41 @@ static struct list_head *cookie_msi_pages(const struct iommu_domain *domain)
}
}
+static bool iommu_dma_msi_range_matches(struct list_head *msi_page_list,
+ const struct iommu_dma_msi_page *base_page,
+ const phys_addr_t *phys_addrs,
+ unsigned int nr_addrs, size_t granule)
+{
+ const struct iommu_dma_msi_page *msi_page;
+ unsigned int nr_found = 0;
+ dma_addr_t offset;
+
+ list_for_each_entry(msi_page, msi_page_list, list) {
+ if (msi_page->iova < base_page->iova)
+ continue;
+ offset = msi_page->iova - base_page->iova;
+ if (offset >= base_page->range_size)
+ continue;
+ if (!IS_ALIGNED(offset, granule) || msi_page->phys != phys_addrs[offset / granule])
+ return false;
+ nr_found++;
+ }
+
+ return nr_found == nr_addrs;
+}
+
static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
- phys_addr_t msi_addr, struct iommu_domain *domain)
+ const phys_addr_t *phys_addrs, unsigned int nr_addrs, size_t granule,
+ 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;
+ struct iommu_dma_msi_page *msi_page, *first_page = NULL;
int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
- size_t size = cookie_msi_granule(domain);
static DEFINE_MUTEX(msi_prepare_lock);
+ LIST_HEAD(new_msi_pages);
+ dma_addr_t base_iova;
+ unsigned int i;
+ size_t size;
/*
* Normally a device's default domain is only ever attached to that
@@ -2218,32 +2245,60 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
*/
guard(mutex)(&msi_prepare_lock);
- msi_addr &= ~(phys_addr_t)(size - 1);
- list_for_each_entry(msi_page, msi_page_list, list)
- if (msi_page->phys == msi_addr)
+ if (!nr_addrs || nr_addrs > SIZE_MAX / granule)
+ return NULL;
+ size = nr_addrs * granule;
+
+ list_for_each_entry(msi_page, msi_page_list, list) {
+ if (msi_page->phys != phys_addrs[0])
+ continue;
+ if (nr_addrs == 1)
+ return msi_page;
+ if (msi_page->range_size == size &&
+ iommu_dma_msi_range_matches(msi_page_list, msi_page, phys_addrs,
+ nr_addrs, granule))
return msi_page;
+ }
- msi_page = kzalloc_obj(*msi_page);
- if (!msi_page)
- return NULL;
+ for (i = 0; i < nr_addrs; i++) {
+ msi_page = kzalloc_obj(*msi_page);
+ if (!msi_page)
+ goto out_free_pages;
+ list_add_tail(&msi_page->list, &new_msi_pages);
+ }
- iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
- if (!iova)
- goto out_free_page;
+ base_iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
+ if (!base_iova)
+ goto out_free_pages;
- if (iommu_map(domain, iova, msi_addr, size, prot, GFP_KERNEL))
- goto out_free_iova;
+ i = 0;
+ list_for_each_entry(msi_page, &new_msi_pages, list) {
+ msi_page->phys = phys_addrs[i];
+ msi_page->iova = base_iova + i * granule;
+ if (!i)
+ first_page = msi_page;
+ if (iommu_map(domain, msi_page->iova, msi_page->phys, granule, prot, GFP_KERNEL))
+ goto out_unmap;
+ i++;
+ }
- 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;
+ first_page->range_size = size;
+ list_splice(&new_msi_pages, msi_page_list);
+ return first_page;
-out_free_iova:
- iommu_dma_free_iova(domain, iova, size, NULL);
-out_free_page:
- kfree(msi_page);
+out_unmap:
+ if (i) {
+ size_t unmap = iommu_unmap(domain, base_iova, i * granule);
+
+ WARN_ON_ONCE(unmap != i * granule);
+ }
+ iommu_dma_free_iova(domain, base_iova, size, NULL);
+out_free_pages:
+ while (!list_empty(&new_msi_pages)) {
+ msi_page = list_first_entry(&new_msi_pages, typeof(*msi_page), list);
+ list_del(&msi_page->list);
+ kfree(msi_page);
+ }
return NULL;
}
@@ -2252,19 +2307,23 @@ int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
{
struct device *dev = msi_desc_to_dev(desc);
const struct iommu_dma_msi_page *msi_page;
+ phys_addr_t phys_addr;
+ size_t granule;
if (!has_msi_cookie(domain)) {
msi_desc_set_iommu_msi_iova(desc, 0, 0);
return 0;
}
+ granule = cookie_msi_granule(domain);
+ phys_addr = ALIGN_DOWN(msi_addr, granule);
+
iommu_group_mutex_assert(dev);
- msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
+ msi_page = iommu_dma_get_msi_page(dev, &phys_addr, 1, granule, domain);
if (!msi_page)
return -ENOMEM;
- msi_desc_set_iommu_msi_iova(desc, msi_page->iova,
- ilog2(cookie_msi_granule(domain)));
+ msi_desc_set_iommu_msi_iova(desc, msi_page->iova, ilog2(granule));
return 0;
}
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 2/8] iommufd: Convert struct iommufd_sw_msi_maps to a growable bitmap
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 1/8] iommu/dma: Prepare MSI physical address lists Andrew Jones
@ 2026-08-26 14:52 ` Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 3/8] iommufd: Split software MSI map lookup and allocation Andrew Jones
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:52 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
struct iommufd_sw_msi_maps uses a fixed 64-bit bitmap, limiting each
group and hardware page table to 64 software MSI mappings. RISC-V
interrupt remapping needs a mapping for every possible CPU, so this
limit is insufficient.
Make the bitmap grow on demand and treat IDs beyond its current size
as absent. Cap it at 16K entries to bound allocation size while leaving
ample room for expected software MSI users.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/iommufd/device.c | 3 +-
drivers/iommu/iommufd/driver.c | 37 ++++++++++++++--------
drivers/iommu/iommufd/hw_pagetable.c | 1 +
drivers/iommu/iommufd/iommufd_private.h | 41 +++++++++++++++++++++++--
4 files changed, 66 insertions(+), 16 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index d488c23fd353..868f1e591208 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -34,6 +34,7 @@ static void iommufd_group_release(struct kref *kref)
NULL, GFP_KERNEL);
iommu_group_put(igroup->group);
mutex_destroy(&igroup->lock);
+ kfree(igroup->required_sw_msi.bitmap);
kfree(igroup);
}
@@ -384,7 +385,7 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
int rc;
if (cur->sw_msi_start != igroup->sw_msi_start ||
- !test_bit(cur->id, igroup->required_sw_msi.bitmap))
+ !iommufd_sw_msi_maps_test_bit(&igroup->required_sw_msi, cur->id))
continue;
rc = iommufd_sw_msi_install(ictx, hwpt_paging, cur);
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 3b8067976eac..d0b05b1fda42 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -196,13 +196,15 @@ iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
if (cur->sw_msi_start != sw_msi_start)
continue;
+ if (cur->pgoff == UINT_MAX)
+ return ERR_PTR(-EOVERFLOW);
max_pgoff = max(max_pgoff, cur->pgoff + 1);
if (cur->msi_addr == msi_addr)
return cur;
}
- if (ictx->sw_msi_id >=
- BITS_PER_BYTE * sizeof_field(struct iommufd_sw_msi_maps, bitmap))
+ if (ictx->sw_msi_id > IOMMUFD_SW_MSI_MAX_ID ||
+ max_pgoff > (ULONG_MAX - sw_msi_start) / PAGE_SIZE)
return ERR_PTR(-EOVERFLOW);
cur = kzalloc_obj(*cur);
@@ -222,21 +224,25 @@ int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
struct iommufd_sw_msi_map *msi_map)
{
unsigned long iova;
+ int rc;
lockdep_assert_held(&ictx->sw_msi_lock);
+ if (iommufd_sw_msi_maps_test_bit(&hwpt_paging->present_sw_msi, msi_map->id))
+ return 0;
+
iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
- if (!test_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap)) {
- int rc;
-
- rc = iommu_map(hwpt_paging->common.domain, iova,
- msi_map->msi_addr, PAGE_SIZE,
- IOMMU_WRITE | IOMMU_READ | IOMMU_MMIO,
- GFP_KERNEL_ACCOUNT);
- if (rc)
- return rc;
- __set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
- }
+ rc = iommufd_sw_msi_maps_ensure(&hwpt_paging->present_sw_msi, msi_map->id);
+ if (rc)
+ return rc;
+
+ rc = iommu_map(hwpt_paging->common.domain, iova,
+ msi_map->msi_addr, PAGE_SIZE,
+ IOMMU_WRITE | IOMMU_READ | IOMMU_MMIO,
+ GFP_KERNEL_ACCOUNT);
+ if (rc)
+ return rc;
+ __set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
return 0;
}
EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi_install, "IOMMUFD_INTERNAL");
@@ -290,6 +296,11 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
if (IS_ERR(msi_map))
return PTR_ERR(msi_map);
+ rc = iommufd_sw_msi_maps_ensure(&handle->idev->igroup->required_sw_msi,
+ msi_map->id);
+ if (rc)
+ return rc;
+
rc = iommufd_sw_msi_install(ictx, hwpt_paging, msi_map);
if (rc)
return rc;
diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
index 623cc608ca0c..54873de43eb0 100644
--- a/drivers/iommu/iommufd/hw_pagetable.c
+++ b/drivers/iommu/iommufd/hw_pagetable.c
@@ -32,6 +32,7 @@ void iommufd_hwpt_paging_destroy(struct iommufd_object *obj)
}
__iommufd_hwpt_destroy(&hwpt_paging->common);
+ kfree(hwpt_paging->present_sw_msi.bitmap);
refcount_dec(&hwpt_paging->ioas->obj.users);
}
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 43fbc5bed8de..9ca5f9f92cdf 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -9,6 +9,7 @@
#include <linux/iova_bitmap.h>
#include <linux/maple_tree.h>
#include <linux/rwsem.h>
+#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/xarray.h>
#include <uapi/linux/iommufd.h>
@@ -29,11 +30,47 @@ struct iommufd_sw_msi_map {
unsigned int id;
};
-/* Bitmap of struct iommufd_sw_msi_map::id */
+/* Bitmap of struct iommufd_sw_msi_map::id; starts empty, grows on demand. */
struct iommufd_sw_msi_maps {
- DECLARE_BITMAP(bitmap, 64);
+ unsigned long *bitmap;
+ unsigned int nbits;
};
+/* Large enough for foreseeable SW MSI users while bounding bitmap growth. */
+#define IOMMUFD_SW_MSI_MAX_ID (16U * 1024 - 1)
+
+/* Grow bitmap to accommodate id. Must be called under ictx->sw_msi_lock. */
+static inline int iommufd_sw_msi_maps_ensure(struct iommufd_sw_msi_maps *maps,
+ unsigned int id)
+{
+ unsigned long *new_bitmap;
+ unsigned int new_nbits;
+
+ if (id < maps->nbits)
+ return 0;
+ if (id > IOMMUFD_SW_MSI_MAX_ID)
+ return -EOVERFLOW;
+
+ new_nbits = max(ALIGN(id + 1, BITS_PER_LONG), 64U);
+ new_bitmap = krealloc(maps->bitmap,
+ BITS_TO_LONGS(new_nbits) * sizeof(unsigned long),
+ GFP_KERNEL_ACCOUNT);
+ if (!new_bitmap)
+ return -ENOMEM;
+ bitmap_clear(new_bitmap, maps->nbits, new_nbits - maps->nbits);
+ maps->bitmap = new_bitmap;
+ maps->nbits = new_nbits;
+ return 0;
+}
+
+static inline bool iommufd_sw_msi_maps_test_bit(const struct iommufd_sw_msi_maps *maps,
+ unsigned int id)
+{
+ if (id >= maps->nbits)
+ return false;
+ return test_bit(id, maps->bitmap);
+}
+
#ifdef CONFIG_IRQ_MSI_IOMMU
int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
struct iommufd_hwpt_paging *hwpt_paging,
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 3/8] iommufd: Split software MSI map lookup and allocation
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 1/8] iommu/dma: Prepare MSI physical address lists Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 2/8] iommufd: Convert struct iommufd_sw_msi_maps to a growable bitmap Andrew Jones
@ 2026-08-26 14:52 ` Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 4/8] iommufd: Bound software MSI mappings to the reserved range Andrew Jones
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:52 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
Separate lookup of an existing software MSI mapping from allocation of a
new mapping. This prepares the code for allocating multiple mappings for
an MSI physical address list.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/iommufd/driver.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index d0b05b1fda42..3856989f5c4b 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -189,6 +189,23 @@ iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
phys_addr_t sw_msi_start)
{
struct iommufd_sw_msi_map *cur;
+
+ lockdep_assert_held(&ictx->sw_msi_lock);
+
+ list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
+ if (cur->sw_msi_start != sw_msi_start)
+ continue;
+ if (cur->msi_addr == msi_addr)
+ return cur;
+ }
+ return NULL;
+}
+
+static struct iommufd_sw_msi_map *
+iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
+ phys_addr_t sw_msi_start)
+{
+ struct iommufd_sw_msi_map *cur;
unsigned int max_pgoff = 0;
lockdep_assert_held(&ictx->sw_msi_lock);
@@ -199,8 +216,6 @@ iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
if (cur->pgoff == UINT_MAX)
return ERR_PTR(-EOVERFLOW);
max_pgoff = max(max_pgoff, cur->pgoff + 1);
- if (cur->msi_addr == msi_addr)
- return cur;
}
if (ictx->sw_msi_id > IOMMUFD_SW_MSI_MAX_ID ||
@@ -293,6 +308,10 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
msi_map = iommufd_sw_msi_get_map(handle->idev->ictx,
msi_addr & PAGE_MASK,
handle->idev->igroup->sw_msi_start);
+ if (!msi_map)
+ msi_map = iommufd_sw_msi_alloc_map(handle->idev->ictx,
+ msi_addr & PAGE_MASK,
+ handle->idev->igroup->sw_msi_start);
if (IS_ERR(msi_map))
return PTR_ERR(msi_map);
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 4/8] iommufd: Bound software MSI mappings to the reserved range
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
` (2 preceding siblings ...)
2026-08-26 14:52 ` [RFC PATCH v1 3/8] iommufd: Split software MSI map lookup and allocation Andrew Jones
@ 2026-08-26 14:52 ` Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 5/8] iommufd: Prepare software MSI maps for address lists Andrew Jones
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:52 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
Track the start and length of the software MSI reserved region as one
range and pass it to the mapping lookup and allocation helpers.
Reject cached mappings outside the region and new mappings when no
complete page remains. This enforces the reserved region bounds and
prepares the helpers to allocate contiguous MSI address lists.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/iommufd/device.c | 17 +++++++++++------
drivers/iommu/iommufd/driver.c | 21 ++++++++++++---------
drivers/iommu/iommufd/io_pagetable.c | 9 +++++----
drivers/iommu/iommufd/iommufd_private.h | 9 +++++++--
4 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 868f1e591208..7b83810a1c90 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -97,7 +97,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx,
kref_init(&new_igroup->ref);
mutex_init(&new_igroup->lock);
xa_init(&new_igroup->pasid_attach);
- new_igroup->sw_msi_start = PHYS_ADDR_MAX;
+ new_igroup->sw_msi_range.start = PHYS_ADDR_MAX;
/* group reference moves into new_igroup */
new_igroup->group = group;
@@ -374,7 +374,7 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
struct iommufd_ctx *ictx = igroup->ictx;
struct iommufd_sw_msi_map *cur;
- if (igroup->sw_msi_start == PHYS_ADDR_MAX)
+ if (igroup->sw_msi_range.start == PHYS_ADDR_MAX)
return 0;
/*
@@ -384,7 +384,7 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
int rc;
- if (cur->sw_msi_start != igroup->sw_msi_start ||
+ if (cur->sw_msi_start != igroup->sw_msi_range.start ||
!iommufd_sw_msi_maps_test_bit(&igroup->required_sw_msi, cur->id))
continue;
@@ -414,18 +414,23 @@ static int
iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
struct iommufd_hwpt_paging *hwpt_paging)
{
+ struct iommufd_sw_msi_range sw_msi_range = {
+ .start = PHYS_ADDR_MAX,
+ };
struct iommufd_group *igroup = idev->igroup;
+ bool first_attach;
int rc;
lockdep_assert_held(&igroup->lock);
+ first_attach = iommufd_group_first_attach(igroup, IOMMU_NO_PASID);
rc = iopt_table_enforce_dev_resv_regions(&hwpt_paging->ioas->iopt,
- idev->dev,
- &igroup->sw_msi_start);
+ idev->dev, &sw_msi_range);
if (rc)
return rc;
- if (iommufd_group_first_attach(igroup, IOMMU_NO_PASID)) {
+ if (first_attach) {
+ igroup->sw_msi_range = sw_msi_range;
rc = iommufd_group_setup_msi(igroup, hwpt_paging);
if (rc) {
iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt,
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 3856989f5c4b..8bb47a81fed1 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -186,14 +186,15 @@ EXPORT_SYMBOL_NS_GPL(iommufd_viommu_report_event, "IOMMUFD");
*/
static struct iommufd_sw_msi_map *
iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
- phys_addr_t sw_msi_start)
+ const struct iommufd_sw_msi_range *sw_msi_range)
{
struct iommufd_sw_msi_map *cur;
lockdep_assert_held(&ictx->sw_msi_lock);
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
- if (cur->sw_msi_start != sw_msi_start)
+ if (cur->sw_msi_start != sw_msi_range->start ||
+ cur->pgoff >= sw_msi_range->length / PAGE_SIZE)
continue;
if (cur->msi_addr == msi_addr)
return cur;
@@ -203,7 +204,7 @@ iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
static struct iommufd_sw_msi_map *
iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
- phys_addr_t sw_msi_start)
+ const struct iommufd_sw_msi_range *sw_msi_range)
{
struct iommufd_sw_msi_map *cur;
unsigned int max_pgoff = 0;
@@ -211,7 +212,7 @@ iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
lockdep_assert_held(&ictx->sw_msi_lock);
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
- if (cur->sw_msi_start != sw_msi_start)
+ if (cur->sw_msi_start != sw_msi_range->start)
continue;
if (cur->pgoff == UINT_MAX)
return ERR_PTR(-EOVERFLOW);
@@ -219,14 +220,16 @@ iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
}
if (ictx->sw_msi_id > IOMMUFD_SW_MSI_MAX_ID ||
- max_pgoff > (ULONG_MAX - sw_msi_start) / PAGE_SIZE)
+ max_pgoff > (ULONG_MAX - sw_msi_range->start) / PAGE_SIZE)
return ERR_PTR(-EOVERFLOW);
+ if (max_pgoff >= sw_msi_range->length / PAGE_SIZE)
+ return ERR_PTR(-ENOSPC);
cur = kzalloc_obj(*cur);
if (!cur)
return ERR_PTR(-ENOMEM);
- cur->sw_msi_start = sw_msi_start;
+ cur->sw_msi_start = sw_msi_range->start;
cur->msi_addr = msi_addr;
cur->pgoff = max_pgoff;
cur->id = ictx->sw_msi_id++;
@@ -295,7 +298,7 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
handle = to_iommufd_handle(raw_handle);
/* No IOMMU_RESV_SW_MSI means no change to the msi_msg */
- if (handle->idev->igroup->sw_msi_start == PHYS_ADDR_MAX)
+ if (handle->idev->igroup->sw_msi_range.start == PHYS_ADDR_MAX)
return 0;
ictx = handle->idev->ictx;
@@ -307,11 +310,11 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
*/
msi_map = iommufd_sw_msi_get_map(handle->idev->ictx,
msi_addr & PAGE_MASK,
- handle->idev->igroup->sw_msi_start);
+ &handle->idev->igroup->sw_msi_range);
if (!msi_map)
msi_map = iommufd_sw_msi_alloc_map(handle->idev->ictx,
msi_addr & PAGE_MASK,
- handle->idev->igroup->sw_msi_start);
+ &handle->idev->igroup->sw_msi_range);
if (IS_ERR(msi_map))
return PTR_ERR(msi_map);
diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
index 24d4917105d9..8bf2554af334 100644
--- a/drivers/iommu/iommufd/io_pagetable.c
+++ b/drivers/iommu/iommufd/io_pagetable.c
@@ -1501,7 +1501,7 @@ void iopt_remove_access(struct io_pagetable *iopt,
/* Narrow the valid_iova_itree to include reserved ranges from a device. */
int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
struct device *dev,
- phys_addr_t *sw_msi_start)
+ struct iommufd_sw_msi_range *sw_msi_range)
{
struct iommu_resv_region *resv;
LIST_HEAD(resv_regions);
@@ -1520,10 +1520,11 @@ int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE)
continue;
- if (sw_msi_start && resv->type == IOMMU_RESV_MSI)
+ if (sw_msi_range && resv->type == IOMMU_RESV_MSI)
num_hw_msi++;
- if (sw_msi_start && resv->type == IOMMU_RESV_SW_MSI) {
- *sw_msi_start = resv->start;
+ if (sw_msi_range && resv->type == IOMMU_RESV_SW_MSI) {
+ sw_msi_range->start = resv->start;
+ sw_msi_range->length = resv->length;
num_sw_msi++;
}
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 9ca5f9f92cdf..4e2d32809695 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -22,6 +22,11 @@ struct iommu_option;
struct iommufd_device;
struct dma_buf_attachment;
+struct iommufd_sw_msi_range {
+ phys_addr_t start;
+ size_t length;
+};
+
struct iommufd_sw_msi_map {
struct list_head sw_msi_item;
phys_addr_t sw_msi_start;
@@ -171,7 +176,7 @@ void iopt_table_remove_domain(struct io_pagetable *iopt,
struct iommu_domain *domain);
int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
struct device *dev,
- phys_addr_t *sw_msi_start);
+ struct iommufd_sw_msi_range *sw_msi_range);
int iopt_set_allow_iova(struct io_pagetable *iopt,
struct rb_root_cached *allowed_iova);
int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start,
@@ -510,7 +515,7 @@ struct iommufd_group {
struct iommu_group *group;
struct xarray pasid_attach;
struct iommufd_sw_msi_maps required_sw_msi;
- phys_addr_t sw_msi_start;
+ struct iommufd_sw_msi_range sw_msi_range;
};
/*
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 5/8] iommufd: Prepare software MSI maps for address lists
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
` (3 preceding siblings ...)
2026-08-26 14:52 ` [RFC PATCH v1 4/8] iommufd: Bound software MSI mappings to the reserved range Andrew Jones
@ 2026-08-26 14:52 ` Andrew Jones
2026-08-26 14:53 ` [RFC PATCH v1 6/8] iommufd: Install software MSI map ranges atomically Andrew Jones
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:52 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
Teach iommufd_sw_msi_get_map() to match an ordered physical address list
against an existing contiguous IOVA range. Mark the first map with the
range size so an identical list can reuse the allocation.
Teach iommufd_sw_msi_alloc_map() to reserve identifiers and offsets for
the complete list and build its maps on a temporary list. Keep the
existing scalar installation behavior and only publish newly allocated
maps after installation succeeds.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/iommufd/driver.c | 161 ++++++++++++++++++------
drivers/iommu/iommufd/iommufd_private.h | 1 +
2 files changed, 123 insertions(+), 39 deletions(-)
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 8bb47a81fed1..6a0527f4b058 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -179,35 +179,91 @@ EXPORT_SYMBOL_NS_GPL(iommufd_viommu_report_event, "IOMMUFD");
#ifdef CONFIG_IRQ_MSI_IOMMU
/*
- * Get a iommufd_sw_msi_map for the msi physical address requested by the irq
+ * Get an iommufd_sw_msi_map for the msi physical addresses requested by the irq
* layer. The mapping to IOVA is global to the iommufd file descriptor, every
* domain that is attached to a device using the same MSI parameters will use
- * the same IOVA.
+ * the same contiguous IOVA range.
*/
static struct iommufd_sw_msi_map *
-iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
- const struct iommufd_sw_msi_range *sw_msi_range)
+iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, const phys_addr_t *phys_addrs,
+ unsigned int nr_addrs, const struct iommufd_sw_msi_range *sw_msi_range)
{
- struct iommufd_sw_msi_map *cur;
+ struct iommufd_sw_msi_map *cur, *msi_map;
+ unsigned int nr_found;
lockdep_assert_held(&ictx->sw_msi_lock);
- list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
- if (cur->sw_msi_start != sw_msi_range->start ||
- cur->pgoff >= sw_msi_range->length / PAGE_SIZE)
+ list_for_each_entry(msi_map, &ictx->sw_msi_list, sw_msi_item) {
+ if (msi_map->sw_msi_start != sw_msi_range->start ||
+ msi_map->msi_addr != phys_addrs[0])
+ continue;
+ if (msi_map->range_size != nr_addrs * PAGE_SIZE)
continue;
- if (cur->msi_addr == msi_addr)
- return cur;
+ if (msi_map->pgoff > sw_msi_range->length / PAGE_SIZE ||
+ nr_addrs > sw_msi_range->length / PAGE_SIZE - msi_map->pgoff)
+ continue;
+
+ nr_found = 0;
+ list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
+ unsigned int index;
+
+ if (cur->sw_msi_start != sw_msi_range->start ||
+ cur->pgoff < msi_map->pgoff)
+ continue;
+ index = cur->pgoff - msi_map->pgoff;
+ if (index >= nr_addrs)
+ continue;
+ if (cur->msi_addr != phys_addrs[index])
+ break;
+ nr_found++;
+ }
+ if (nr_found == nr_addrs)
+ return msi_map;
}
return NULL;
}
+static int iommufd_sw_msi_check_alloc(struct iommufd_ctx *ictx,
+ const struct iommufd_sw_msi_range *sw_msi_range,
+ unsigned int first_pgoff, unsigned int nr_addrs,
+ size_t *range_size)
+{
+ unsigned long max_iova_pgoff;
+ unsigned int last_pgoff;
+ unsigned int last_id;
+ size_t range_pages;
+
+ if (!nr_addrs)
+ return -EINVAL;
+ if (sw_msi_range->start > ULONG_MAX)
+ return -EOVERFLOW;
+
+ range_pages = sw_msi_range->length / PAGE_SIZE;
+ max_iova_pgoff = (ULONG_MAX - sw_msi_range->start) / PAGE_SIZE;
+
+ if (check_add_overflow(ictx->sw_msi_id, nr_addrs - 1, &last_id) ||
+ last_id > IOMMUFD_SW_MSI_MAX_ID ||
+ check_add_overflow(first_pgoff, nr_addrs - 1, &last_pgoff) ||
+ last_pgoff > max_iova_pgoff ||
+ check_mul_overflow((size_t)nr_addrs, PAGE_SIZE, range_size))
+ return -EOVERFLOW;
+
+ if (last_pgoff >= range_pages)
+ return -ENOSPC;
+
+ return 0;
+}
+
static struct iommufd_sw_msi_map *
-iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
- const struct iommufd_sw_msi_range *sw_msi_range)
+iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, const phys_addr_t *phys_addrs,
+ unsigned int nr_addrs, const struct iommufd_sw_msi_range *sw_msi_range,
+ struct list_head *new_msi_maps)
{
- struct iommufd_sw_msi_map *cur;
- unsigned int max_pgoff = 0;
+ struct iommufd_sw_msi_map *cur, *first_map = NULL;
+ unsigned int next_pgoff = 0;
+ unsigned int i;
+ size_t size;
+ int rc;
lockdep_assert_held(&ictx->sw_msi_lock);
@@ -216,25 +272,38 @@ iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
continue;
if (cur->pgoff == UINT_MAX)
return ERR_PTR(-EOVERFLOW);
- max_pgoff = max(max_pgoff, cur->pgoff + 1);
+ next_pgoff = max(next_pgoff, cur->pgoff + 1);
}
- if (ictx->sw_msi_id > IOMMUFD_SW_MSI_MAX_ID ||
- max_pgoff > (ULONG_MAX - sw_msi_range->start) / PAGE_SIZE)
- return ERR_PTR(-EOVERFLOW);
- if (max_pgoff >= sw_msi_range->length / PAGE_SIZE)
- return ERR_PTR(-ENOSPC);
-
- cur = kzalloc_obj(*cur);
- if (!cur)
- return ERR_PTR(-ENOMEM);
-
- cur->sw_msi_start = sw_msi_range->start;
- cur->msi_addr = msi_addr;
- cur->pgoff = max_pgoff;
- cur->id = ictx->sw_msi_id++;
- list_add_tail(&cur->sw_msi_item, &ictx->sw_msi_list);
- return cur;
+ rc = iommufd_sw_msi_check_alloc(ictx, sw_msi_range, next_pgoff, nr_addrs, &size);
+ if (rc)
+ return ERR_PTR(rc);
+
+ for (i = 0; i < nr_addrs; i++) {
+ cur = kzalloc_obj(*cur);
+ if (!cur)
+ goto err_free;
+
+ cur->sw_msi_start = sw_msi_range->start;
+ cur->msi_addr = phys_addrs[i];
+ cur->pgoff = next_pgoff + i;
+ cur->id = ictx->sw_msi_id + i;
+ if (!i) {
+ cur->range_size = size;
+ first_map = cur;
+ }
+ list_add_tail(&cur->sw_msi_item, new_msi_maps);
+ }
+
+ return first_map;
+
+err_free:
+ while (!list_empty(new_msi_maps)) {
+ cur = list_first_entry(new_msi_maps, typeof(*cur), sw_msi_item);
+ list_del(&cur->sw_msi_item);
+ kfree(cur);
+ }
+ return ERR_PTR(-ENOMEM);
}
int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
@@ -280,7 +349,9 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
struct iommufd_attach_handle *handle;
struct iommufd_sw_msi_map *msi_map;
struct iommufd_ctx *ictx;
+ LIST_HEAD(new_msi_maps);
unsigned long iova;
+ phys_addr_t phys_addr;
int rc;
/*
@@ -308,29 +379,41 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
* assume the caller has checked that it is contained with a MMIO region
* that is secure to map at PAGE_SIZE.
*/
- msi_map = iommufd_sw_msi_get_map(handle->idev->ictx,
- msi_addr & PAGE_MASK,
- &handle->idev->igroup->sw_msi_range);
+ phys_addr = msi_addr & PAGE_MASK;
+ msi_map = iommufd_sw_msi_get_map(ictx, &phys_addr, 1, &handle->idev->igroup->sw_msi_range);
if (!msi_map)
- msi_map = iommufd_sw_msi_alloc_map(handle->idev->ictx,
- msi_addr & PAGE_MASK,
- &handle->idev->igroup->sw_msi_range);
+ msi_map = iommufd_sw_msi_alloc_map(ictx, &phys_addr, 1,
+ &handle->idev->igroup->sw_msi_range,
+ &new_msi_maps);
if (IS_ERR(msi_map))
return PTR_ERR(msi_map);
rc = iommufd_sw_msi_maps_ensure(&handle->idev->igroup->required_sw_msi,
msi_map->id);
if (rc)
- return rc;
+ goto err_free;
rc = iommufd_sw_msi_install(ictx, hwpt_paging, msi_map);
if (rc)
- return rc;
+ goto err_free;
__set_bit(msi_map->id, handle->idev->igroup->required_sw_msi.bitmap);
+ if (!list_empty(&new_msi_maps)) {
+ list_splice_tail_init(&new_msi_maps, &ictx->sw_msi_list);
+ ictx->sw_msi_id++;
+ }
+
iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
msi_desc_set_iommu_msi_iova(desc, iova, PAGE_SHIFT);
return 0;
+
+err_free:
+ while (!list_empty(&new_msi_maps)) {
+ msi_map = list_first_entry(&new_msi_maps, typeof(*msi_map), sw_msi_item);
+ list_del(&msi_map->sw_msi_item);
+ kfree(msi_map);
+ }
+ return rc;
}
EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi, "IOMMUFD");
#endif
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 4e2d32809695..797965557281 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -33,6 +33,7 @@ struct iommufd_sw_msi_map {
phys_addr_t msi_addr;
unsigned int pgoff;
unsigned int id;
+ size_t range_size; /* IOVA range size, or 0 if not the first map */
};
/* Bitmap of struct iommufd_sw_msi_map::id; starts empty, grows on demand. */
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 6/8] iommufd: Install software MSI map ranges atomically
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
` (4 preceding siblings ...)
2026-08-26 14:52 ` [RFC PATCH v1 5/8] iommufd: Prepare software MSI maps for address lists Andrew Jones
@ 2026-08-26 14:53 ` Andrew Jones
2026-08-26 14:53 ` [RFC PATCH v1 7/8] iommufd: Prepare software MSI installation for address lists Andrew Jones
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:53 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
Add a range installation helper which installs every map in a contiguous
software MSI range and rolls back only mappings added by the failed
operation. Use the helper when replaying range mappings into a new
paging domain.
This prepares iommufd to publish and install physical address lists
without exposing partially installed ranges.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/iommufd/driver.c | 76 ++++++++++++++++++++++++++++++----
1 file changed, 68 insertions(+), 8 deletions(-)
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 6a0527f4b058..a43a25078f87 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -178,6 +178,17 @@ int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
EXPORT_SYMBOL_NS_GPL(iommufd_viommu_report_event, "IOMMUFD");
#ifdef CONFIG_IRQ_MSI_IOMMU
+static bool iommufd_sw_msi_range_index(const struct iommufd_sw_msi_map *map,
+ const struct iommufd_sw_msi_map *base_map,
+ unsigned int nr_addrs, unsigned int *index)
+{
+ if (map->sw_msi_start != base_map->sw_msi_start || map->pgoff < base_map->pgoff)
+ return false;
+
+ *index = map->pgoff - base_map->pgoff;
+ return *index < nr_addrs;
+}
+
/*
* Get an iommufd_sw_msi_map for the msi physical addresses requested by the irq
* layer. The mapping to IOVA is global to the iommufd file descriptor, every
@@ -207,11 +218,7 @@ iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, const phys_addr_t *phys_addrs,
list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
unsigned int index;
- if (cur->sw_msi_start != sw_msi_range->start ||
- cur->pgoff < msi_map->pgoff)
- continue;
- index = cur->pgoff - msi_map->pgoff;
- if (index >= nr_addrs)
+ if (!iommufd_sw_msi_range_index(cur, msi_map, nr_addrs, &index))
continue;
if (cur->msi_addr != phys_addrs[index])
break;
@@ -306,9 +313,9 @@ iommufd_sw_msi_alloc_map(struct iommufd_ctx *ictx, const phys_addr_t *phys_addrs
return ERR_PTR(-ENOMEM);
}
-int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
- struct iommufd_hwpt_paging *hwpt_paging,
- struct iommufd_sw_msi_map *msi_map)
+static int iommufd_sw_msi_install_one(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct iommufd_sw_msi_map *msi_map)
{
unsigned long iova;
int rc;
@@ -332,6 +339,59 @@ int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
__set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
return 0;
}
+
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct iommufd_sw_msi_map *base_map)
+{
+ struct iommufd_sw_msi_map *msi_map;
+ struct list_head *msi_maps = &ictx->sw_msi_list;
+ unsigned long *newly_mapped;
+ unsigned int nr_addrs = base_map->range_size ? base_map->range_size / PAGE_SIZE : 1;
+ unsigned int nr_found = 0;
+ unsigned int index;
+ int rc = 0;
+
+ newly_mapped = bitmap_zalloc(nr_addrs, GFP_KERNEL_ACCOUNT);
+ if (!newly_mapped)
+ return -ENOMEM;
+
+ list_for_each_entry(msi_map, msi_maps, sw_msi_item) {
+ if (!iommufd_sw_msi_range_index(msi_map, base_map, nr_addrs, &index))
+ continue;
+ if (iommufd_sw_msi_maps_test_bit(&hwpt_paging->present_sw_msi, msi_map->id)) {
+ nr_found++;
+ continue;
+ }
+ rc = iommufd_sw_msi_install_one(ictx, hwpt_paging, msi_map);
+ if (rc)
+ goto err_unmap;
+ __set_bit(index, newly_mapped);
+ nr_found++;
+ }
+ if (nr_found != nr_addrs) {
+ rc = -EINVAL;
+ goto err_unmap;
+ }
+
+ bitmap_free(newly_mapped);
+ return 0;
+
+err_unmap:
+ list_for_each_entry(msi_map, msi_maps, sw_msi_item) {
+ unsigned long iova;
+
+ if (!iommufd_sw_msi_range_index(msi_map, base_map, nr_addrs, &index) ||
+ !test_bit(index, newly_mapped))
+ continue;
+
+ iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
+ WARN_ON_ONCE(iommu_unmap(hwpt_paging->common.domain, iova, PAGE_SIZE) != PAGE_SIZE);
+ __clear_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
+ }
+ bitmap_free(newly_mapped);
+ return rc;
+}
EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi_install, "IOMMUFD_INTERNAL");
/*
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 7/8] iommufd: Prepare software MSI installation for address lists
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
` (5 preceding siblings ...)
2026-08-26 14:53 ` [RFC PATCH v1 6/8] iommufd: Install software MSI map ranges atomically Andrew Jones
@ 2026-08-26 14:53 ` Andrew Jones
2026-08-26 14:53 ` [RFC PATCH v1 8/8] iommu/dma: Introduce iommu_dma_prepare_msi_list() Andrew Jones
2026-08-27 8:13 ` [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:53 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
Teach the software MSI path to install every mapping in an address list
before publishing newly allocated maps. Mark the complete list as
required by the group and roll back mappings installed by a failed
operation.
Keep iommufd_sw_msi() as a one-address wrapper so existing callers
retain their current behavior.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/iommufd/driver.c | 101 +++++++++++++++++++++++----------
1 file changed, 72 insertions(+), 29 deletions(-)
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index a43a25078f87..45278c11c688 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -340,14 +340,14 @@ static int iommufd_sw_msi_install_one(struct iommufd_ctx *ictx,
return 0;
}
-int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
- struct iommufd_hwpt_paging *hwpt_paging,
- struct iommufd_sw_msi_map *base_map)
+static int __iommufd_sw_msi_install_range(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct list_head *msi_maps,
+ const struct iommufd_sw_msi_map *base_map,
+ unsigned int nr_addrs)
{
struct iommufd_sw_msi_map *msi_map;
- struct list_head *msi_maps = &ictx->sw_msi_list;
unsigned long *newly_mapped;
- unsigned int nr_addrs = base_map->range_size ? base_map->range_size / PAGE_SIZE : 1;
unsigned int nr_found = 0;
unsigned int index;
int rc = 0;
@@ -392,16 +392,36 @@ int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
bitmap_free(newly_mapped);
return rc;
}
+
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct iommufd_sw_msi_map *base_map)
+{
+ unsigned int nr_addrs = base_map->range_size ? base_map->range_size / PAGE_SIZE : 1;
+
+ return __iommufd_sw_msi_install_range(ictx, hwpt_paging, &ictx->sw_msi_list, base_map,
+ nr_addrs);
+}
EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi_install, "IOMMUFD_INTERNAL");
-/*
- * Called by the irq code if the platform translates the MSI address through the
- * IOMMU. msi_addr is the physical address of the MSI page. iommufd will
- * allocate a fd global iova for the physical page that is the same on all
- * domains and devices.
- */
-int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr)
+static void iommufd_sw_msi_set_required(struct iommufd_group *igroup,
+ struct list_head *msi_maps,
+ const struct iommufd_sw_msi_map *base_map,
+ unsigned int nr_addrs)
+{
+ struct iommufd_sw_msi_map *msi_map;
+ unsigned int index;
+
+ list_for_each_entry(msi_map, msi_maps, sw_msi_item) {
+ if (!iommufd_sw_msi_range_index(msi_map, base_map, nr_addrs, &index))
+ continue;
+ __set_bit(msi_map->id, igroup->required_sw_msi.bitmap);
+ }
+}
+
+static int iommufd_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 iommufd_hwpt_paging *hwpt_paging;
@@ -410,10 +430,19 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
struct iommufd_sw_msi_map *msi_map;
struct iommufd_ctx *ictx;
LIST_HEAD(new_msi_maps);
+ struct list_head *msi_maps;
unsigned long iova;
- phys_addr_t phys_addr;
+ unsigned int i;
int rc;
+ if (granule != PAGE_SIZE)
+ return -EOPNOTSUPP;
+ if (!nr_addrs || nr_addrs > SIZE_MAX / PAGE_SIZE)
+ return -EINVAL;
+ for (i = 0; i < nr_addrs; i++)
+ if (!IS_ALIGNED(phys_addrs[i], PAGE_SIZE))
+ return -EINVAL;
+
/*
* It is safe to call iommu_attach_handle_get() here because the iommu
* core code invokes this under the group mutex which also prevents any
@@ -434,33 +463,33 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
ictx = handle->idev->ictx;
guard(mutex)(&ictx->sw_msi_lock);
- /*
- * The input msi_addr is the exact byte offset of the MSI doorbell, we
- * assume the caller has checked that it is contained with a MMIO region
- * that is secure to map at PAGE_SIZE.
- */
- phys_addr = msi_addr & PAGE_MASK;
- msi_map = iommufd_sw_msi_get_map(ictx, &phys_addr, 1, &handle->idev->igroup->sw_msi_range);
- if (!msi_map)
- msi_map = iommufd_sw_msi_alloc_map(ictx, &phys_addr, 1,
+ msi_map = iommufd_sw_msi_get_map(ictx, phys_addrs, nr_addrs,
+ &handle->idev->igroup->sw_msi_range);
+ if (msi_map) {
+ msi_maps = &ictx->sw_msi_list;
+ } else {
+ msi_map = iommufd_sw_msi_alloc_map(ictx, phys_addrs, nr_addrs,
&handle->idev->igroup->sw_msi_range,
&new_msi_maps);
- if (IS_ERR(msi_map))
- return PTR_ERR(msi_map);
+ if (IS_ERR(msi_map))
+ return PTR_ERR(msi_map);
+ msi_maps = &new_msi_maps;
+ }
rc = iommufd_sw_msi_maps_ensure(&handle->idev->igroup->required_sw_msi,
- msi_map->id);
+ msi_map->id + nr_addrs - 1);
if (rc)
goto err_free;
- rc = iommufd_sw_msi_install(ictx, hwpt_paging, msi_map);
+ rc = __iommufd_sw_msi_install_range(ictx, hwpt_paging, msi_maps, msi_map, nr_addrs);
if (rc)
goto err_free;
- __set_bit(msi_map->id, handle->idev->igroup->required_sw_msi.bitmap);
+
+ iommufd_sw_msi_set_required(handle->idev->igroup, msi_maps, msi_map, nr_addrs);
if (!list_empty(&new_msi_maps)) {
list_splice_tail_init(&new_msi_maps, &ictx->sw_msi_list);
- ictx->sw_msi_id++;
+ ictx->sw_msi_id += nr_addrs;
}
iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
@@ -475,6 +504,20 @@ int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
}
return rc;
}
+
+/*
+ * Called by the irq code if the platform translates the MSI address through the
+ * IOMMU. msi_addr is the physical address of the MSI page. iommufd will
+ * allocate a fd global iova for the physical page that is the same on all
+ * domains and devices.
+ */
+int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+ phys_addr_t msi_addr)
+{
+ phys_addr_t phys_addr = msi_addr & PAGE_MASK;
+
+ return iommufd_sw_msi_list(domain, desc, &phys_addr, 1, PAGE_SIZE);
+}
EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi, "IOMMUFD");
#endif
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [RFC PATCH v1 8/8] iommu/dma: Introduce iommu_dma_prepare_msi_list()
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
` (6 preceding siblings ...)
2026-08-26 14:53 ` [RFC PATCH v1 7/8] iommufd: Prepare software MSI installation for address lists Andrew Jones
@ 2026-08-26 14:53 ` Andrew Jones
2026-08-27 8:13 ` [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-26 14:53 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
Add iommu_dma_prepare_msi_list() to map an ordered physical address list
through the current domain backend. Validate that the requested granule
is a power of two no smaller than PAGE_SIZE and that every address is
aligned to it.
Refactor iommu_dma_prepare_msi() to obtain the backend granule and align
its single address before using the same list dispatcher. Convert the
DMA-IOMMU and iommufd backend entry points to consume address lists
directly.
Signed-off-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
---
drivers/iommu/dma-iommu.c | 13 ++--
drivers/iommu/dma-iommu.h | 13 +++-
drivers/iommu/iommu-priv.h | 7 ++-
drivers/iommu/iommu.c | 109 +++++++++++++++++++++++++++------
drivers/iommu/iommufd/driver.c | 24 +++-----
include/linux/iommu.h | 8 +++
6 files changed, 126 insertions(+), 48 deletions(-)
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index c256531ff19f..0699785adbfb 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -2173,7 +2173,7 @@ static bool has_msi_cookie(const struct iommu_domain *domain)
domain->cookie_type == IOMMU_COOKIE_DMA_MSI);
}
-static size_t cookie_msi_granule(const struct iommu_domain *domain)
+size_t iommu_dma_msi_granule(const struct iommu_domain *domain)
{
switch (domain->cookie_type) {
case IOMMU_COOKIE_DMA_IOVA:
@@ -2303,23 +2303,20 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
}
int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr)
+ const phys_addr_t *phys_addrs, unsigned int nr_addrs, size_t granule)
{
struct device *dev = msi_desc_to_dev(desc);
const struct iommu_dma_msi_page *msi_page;
- phys_addr_t phys_addr;
- size_t granule;
if (!has_msi_cookie(domain)) {
msi_desc_set_iommu_msi_iova(desc, 0, 0);
return 0;
}
-
- granule = cookie_msi_granule(domain);
- phys_addr = ALIGN_DOWN(msi_addr, granule);
+ if (granule != iommu_dma_msi_granule(domain))
+ return -EOPNOTSUPP;
iommu_group_mutex_assert(dev);
- msi_page = iommu_dma_get_msi_page(dev, &phys_addr, 1, granule, domain);
+ msi_page = iommu_dma_get_msi_page(dev, phys_addrs, nr_addrs, granule, domain);
if (!msi_page)
return -ENOMEM;
diff --git a/drivers/iommu/dma-iommu.h b/drivers/iommu/dma-iommu.h
index 040d00252563..bf9cd4102d35 100644
--- a/drivers/iommu/dma-iommu.h
+++ b/drivers/iommu/dma-iommu.h
@@ -19,8 +19,9 @@ int iommu_dma_init_fq(struct iommu_domain *domain);
void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list);
+size_t iommu_dma_msi_granule(const struct iommu_domain *domain);
int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr);
+ const phys_addr_t *phys_addrs, unsigned int nr_addrs, size_t granule);
extern bool iommu_dma_forcedac;
@@ -53,8 +54,14 @@ static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_he
{
}
-static inline int iommu_dma_sw_msi(struct iommu_domain *domain,
- struct msi_desc *desc, phys_addr_t msi_addr)
+static inline size_t iommu_dma_msi_granule(const struct iommu_domain *domain)
+{
+ return 0;
+}
+
+static inline int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+ const phys_addr_t *phys_addrs, unsigned int nr_addrs,
+ size_t granule)
{
return -ENODEV;
}
diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h
index aaffad5854fc..1122c99566d5 100644
--- a/drivers/iommu/iommu-priv.h
+++ b/drivers/iommu/iommu-priv.h
@@ -54,10 +54,11 @@ int iommu_replace_group_handle(struct iommu_group *group,
#if IS_ENABLED(CONFIG_IOMMUFD_DRIVER_CORE) && IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr);
+ const phys_addr_t *phys_addrs, unsigned int nr_addrs, size_t granule);
#else /* !CONFIG_IOMMUFD_DRIVER_CORE || !CONFIG_IRQ_MSI_IOMMU */
-static inline int iommufd_sw_msi(struct iommu_domain *domain,
- struct msi_desc *desc, phys_addr_t msi_addr)
+static inline int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+ const phys_addr_t *phys_addrs, unsigned int nr_addrs,
+ size_t granule)
{
return -EOPNOTSUPP;
}
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index a3dea7353500..692e793c624e 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -4223,42 +4223,115 @@ void pci_dev_reset_iommu_done(struct pci_dev *pdev)
EXPORT_SYMBOL_GPL(pci_dev_reset_iommu_done);
#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
+static int __iommu_dma_prepare_msi_list(struct iommu_group *group, struct msi_desc *desc,
+ const phys_addr_t *phys_addrs, unsigned int nr_addrs,
+ size_t granule)
+{
+ if (!group->domain || group->domain->type == IOMMU_DOMAIN_IDENTITY)
+ return 0;
+
+ switch (group->domain->cookie_type) {
+ case IOMMU_COOKIE_DMA_MSI:
+ case IOMMU_COOKIE_DMA_IOVA:
+ return iommu_dma_sw_msi(group->domain, desc, phys_addrs, nr_addrs, granule);
+ case IOMMU_COOKIE_IOMMUFD:
+ return iommufd_sw_msi(group->domain, desc, phys_addrs, nr_addrs, granule);
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static int iommu_dma_validate_msi_list(const phys_addr_t *phys_addrs, unsigned int nr_addrs,
+ size_t granule)
+{
+ unsigned int i;
+
+ if (!nr_addrs || granule < PAGE_SIZE || !is_power_of_2(granule) ||
+ nr_addrs > SIZE_MAX / granule)
+ return -EINVAL;
+
+ for (i = 0; i < nr_addrs; i++)
+ if (!IS_ALIGNED(phys_addrs[i], granule))
+ return -EINVAL;
+
+ return 0;
+}
+
+/**
+ * iommu_dma_prepare_msi_list() - Map MSI pages in the IOMMU domain
+ * @desc: MSI descriptor to update with the base IOVA
+ * @phys_addrs: Ordered MSI target physical addresses
+ * @nr_addrs: Number of addresses in @phys_addrs
+ * @granule: Mapping granule for every address
+ *
+ * @nr_addrs must be nonzero and @granule must be a power of two no smaller than
+ * PAGE_SIZE. Every address must be aligned to @granule. The addresses are mapped
+ * in order to one contiguous IOVA range and may repeat. The list is consumed
+ * synchronously and is not retained.
+ *
+ * When a software MSI mapping is required, the backend records the base IOVA
+ * and granule shift in @desc. Otherwise, @desc is left unchanged.
+ *
+ * Return: 0 on success, -EINVAL if the parameters are invalid, -EOPNOTSUPP if
+ * the domain backend cannot provide the mapping, or another negative error
+ * code from the backend.
+ */
+int iommu_dma_prepare_msi_list(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_group *group = dev->iommu_group;
+ int ret;
+
+ ret = iommu_dma_validate_msi_list(phys_addrs, nr_addrs, granule);
+ if (ret || !group)
+ return ret;
+
+ mutex_lock(&group->mutex);
+ ret = __iommu_dma_prepare_msi_list(group, desc, phys_addrs, nr_addrs, granule);
+ mutex_unlock(&group->mutex);
+ return ret;
+}
+
/**
* iommu_dma_prepare_msi() - Map the MSI page in the IOMMU domain
* @desc: MSI descriptor, will store the MSI page
* @msi_addr: MSI target address to be mapped
*
- * The implementation of sw_msi() should take msi_addr and map it to
- * an IOVA in the domain and call msi_desc_set_iommu_msi_iova() with the
- * mapping information.
- *
* Return: 0 on success or negative error code if the mapping failed.
*/
int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
{
struct device *dev = msi_desc_to_dev(desc);
struct iommu_group *group = dev->iommu_group;
+ phys_addr_t phys_addr;
+ size_t granule;
int ret = 0;
if (!group)
return 0;
mutex_lock(&group->mutex);
- /* An IDENTITY domain must pass through */
- if (group->domain && group->domain->type != IOMMU_DOMAIN_IDENTITY) {
- switch (group->domain->cookie_type) {
- case IOMMU_COOKIE_DMA_MSI:
- case IOMMU_COOKIE_DMA_IOVA:
- ret = iommu_dma_sw_msi(group->domain, desc, msi_addr);
- break;
- case IOMMU_COOKIE_IOMMUFD:
- ret = iommufd_sw_msi(group->domain, desc, msi_addr);
- break;
- default:
- ret = -EOPNOTSUPP;
- break;
- }
+ if (!group->domain || group->domain->type == IOMMU_DOMAIN_IDENTITY)
+ goto out_unlock;
+
+ switch (group->domain->cookie_type) {
+ case IOMMU_COOKIE_DMA_MSI:
+ case IOMMU_COOKIE_DMA_IOVA:
+ granule = iommu_dma_msi_granule(group->domain);
+ break;
+ case IOMMU_COOKIE_IOMMUFD:
+ granule = PAGE_SIZE;
+ break;
+ default:
+ ret = -EOPNOTSUPP;
+ goto out_unlock;
}
+
+ phys_addr = ALIGN_DOWN(msi_addr, granule);
+ ret = __iommu_dma_prepare_msi_list(group, desc, &phys_addr, 1, granule);
+
+out_unlock:
mutex_unlock(&group->mutex);
return ret;
}
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 45278c11c688..72e9ce63a5ee 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -419,9 +419,14 @@ static void iommufd_sw_msi_set_required(struct iommufd_group *igroup,
}
}
-static int iommufd_sw_msi_list(struct iommu_domain *domain, struct msi_desc *desc,
- const phys_addr_t *phys_addrs, unsigned int nr_addrs,
- size_t granule)
+/*
+ * Called by the irq code if the platform translates the MSI addresses through the
+ * IOMMU. phys_addrs are the physical addresses of the MSI pages. iommufd will
+ * allocate contiguous fd global iovas for the physical pages that are the same on
+ * all domains and devices.
+ */
+int iommufd_sw_msi(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 iommufd_hwpt_paging *hwpt_paging;
@@ -505,19 +510,6 @@ static int iommufd_sw_msi_list(struct iommu_domain *domain, struct msi_desc *des
return rc;
}
-/*
- * Called by the irq code if the platform translates the MSI address through the
- * IOMMU. msi_addr is the physical address of the MSI page. iommufd will
- * allocate a fd global iova for the physical page that is the same on all
- * domains and devices.
- */
-int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr)
-{
- phys_addr_t phys_addr = msi_addr & PAGE_MASK;
-
- return iommufd_sw_msi_list(domain, desc, &phys_addr, 1, PAGE_SIZE);
-}
EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi, "IOMMUFD");
#endif
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index ac43b8b93f14..bf08be172e40 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -1561,8 +1561,16 @@ static inline void pci_dev_reset_iommu_done(struct pci_dev *pdev)
#ifdef CONFIG_IRQ_MSI_IOMMU
#ifdef CONFIG_IOMMU_API
+int iommu_dma_prepare_msi_list(struct msi_desc *desc, const phys_addr_t *phys_addrs,
+ unsigned int nr_addrs, size_t granule);
int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr);
#else
+static inline int iommu_dma_prepare_msi_list(struct msi_desc *desc, const phys_addr_t *phys_addrs,
+ unsigned int nr_addrs, size_t granule)
+{
+ return 0;
+}
+
static inline int iommu_dma_prepare_msi(struct msi_desc *desc,
phys_addr_t msi_addr)
{
--
2.43.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
` (7 preceding siblings ...)
2026-08-26 14:53 ` [RFC PATCH v1 8/8] iommu/dma: Introduce iommu_dma_prepare_msi_list() Andrew Jones
@ 2026-08-27 8:13 ` Andrew Jones
8 siblings, 0 replies; 10+ messages in thread
From: Andrew Jones @ 2026-08-27 8:13 UTC (permalink / raw)
To: iommu, linux-kernel; +Cc: jgg, kevin.tian, robin.murphy, joro, will
On Wed, Aug 26, 2026 at 04:52:54PM +0200, Andrew Jones wrote:
> The RISC-V IOMMU MSI remapping series [1] needs to prepare mappings for
> every possible IMSIC target before MSI composition may occur in atomic
> context. The previous approach mapped each physical address separately
> and kept a driver-owned PA-to-IOVA lookup table.
>
> Jason Gunthorpe suggested passing the complete list of physical
> addresses through the existing MSI preparation flow. This series
> implements that idea as iommu_dma_prepare_msi_list(), which maps the
> ordered list into one contiguous IOVA range and records its base and
> granule shift in the MSI descriptor. The interrupt-remapping driver can
> then derive the IOVA for each list entry without maintaining a separate
> IOVA array.
>
> Both DMA-IOMMU and iommufd backends are extended to support these list
> mappings. The existing per-page mapping objects are retained, while
> the first object identifies the complete range so identical lists can
> reuse an existing mapping. iommufd allocates and installs a complete
> range atomically and grows its software-MSI bitmaps beyond their
> previous 64-entry limit.
>
> This is being posted separately from the RISC-V MSI remapping work for
> early RFC review of the common DMA-IOMMU and iommufd interfaces. The
> RISC-V adaptation to iommu_dma_prepare_msi_list() is still in progress
> and will be posted later.
>
Applying this to riscv makes the MSI remapping surprisingly (to me) simple
and unintrusive (IMHO) to the IMSIC driver. Only the changes below were
needed.
I'll post this series along with the imsic and kconfig changes as v5 of
the riscv host msi remapping series without the RFC tag.
Thanks,
drew
diff --git a/drivers/irqchip/Kconfig b/drivers/irqchip/Kconfig
index 42f2278a702d..c7cda52b21eb 100644
--- a/drivers/irqchip/Kconfig
+++ b/drivers/irqchip/Kconfig
@@ -656,6 +656,7 @@ config RISCV_IMSIC
select IRQ_DOMAIN_HIERARCHY
select GENERIC_IRQ_MATRIX_ALLOCATOR
select GENERIC_MSI_IRQ
+ select IRQ_MSI_IOMMU
select IRQ_MSI_LIB
config RISCV_RPMI_SYSMSI
diff --git a/drivers/irqchip/irq-riscv-imsic-platform.c b/drivers/irqchip/irq-riscv-imsic-platform.c
index 643c8e459611..72f595c0d320 100644
--- a/drivers/irqchip/irq-riscv-imsic-platform.c
+++ b/drivers/irqchip/irq-riscv-imsic-platform.c
@@ -10,6 +10,7 @@
#include <linux/cpu.h>
#include <linux/interrupt.h>
#include <linux/io.h>
+#include <linux/iommu.h>
#include <linux/irq.h>
#include <linux/irqchip.h>
#include <linux/irqdomain.h>
@@ -69,8 +70,10 @@ static void imsic_irq_ack(struct irq_data *d)
irq_move_irq(d);
}
-static void imsic_irq_compose_vector_msg(struct imsic_vector *vec, struct msi_msg *msg)
+static void imsic_irq_compose_vector_msg(struct irq_data *d, struct imsic_vector *vec,
+ struct msi_msg *msg)
{
+ struct msi_desc *desc = irq_data_get_msi_desc(d);
phys_addr_t msi_addr;
if (WARN_ON(!vec))
@@ -79,6 +82,14 @@ static void imsic_irq_compose_vector_msg(struct imsic_vector *vec, struct msi_ms
if (WARN_ON(!imsic_cpu_page_phys(vec->cpu, 0, &msi_addr)))
return;
+ if (desc->iommu_msi_shift) {
+ const struct imsic_local_config *local;
+
+ local = per_cpu_ptr(imsic->global.local, vec->cpu);
+ msi_addr = (desc->iommu_msi_iova << desc->iommu_msi_shift) +
+ local->smode_msi_pa_index * IMSIC_MMIO_PAGE_SZ;
+ }
+
msg->address_hi = upper_32_bits(msi_addr);
msg->address_lo = lower_32_bits(msi_addr);
msg->data = vec->local_id;
@@ -86,7 +97,7 @@ static void imsic_irq_compose_vector_msg(struct imsic_vector *vec, struct msi_ms
static void imsic_irq_compose_msg(struct irq_data *d, struct msi_msg *msg)
{
- imsic_irq_compose_vector_msg(irq_data_get_irq_chip_data(d), msg);
+ imsic_irq_compose_vector_msg(d, irq_data_get_irq_chip_data(d), msg);
}
#ifdef CONFIG_SMP
@@ -94,7 +105,7 @@ static void imsic_msi_update_msg(struct irq_data *d, struct imsic_vector *vec)
{
struct msi_msg msg = { };
- imsic_irq_compose_vector_msg(vec, &msg);
+ imsic_irq_compose_vector_msg(d, vec, &msg);
irq_data_get_irq_chip(d)->irq_write_msi_msg(d, &msg);
}
@@ -225,7 +236,9 @@ static struct irq_chip imsic_irq_base_chip = {
static int imsic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
unsigned int nr_irqs, void *args)
{
+ msi_alloc_info_t *info = args;
struct imsic_vector *vec;
+ int ret;
/* Multi-MSI is not supported yet. */
if (nr_irqs > 1)
@@ -235,6 +248,13 @@ static int imsic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
if (!vec)
return -ENOSPC;
+ ret = iommu_dma_prepare_msi_list(info->desc, imsic->smode_msi_pa,
+ imsic->nr_smode_msi_pa, IMSIC_MMIO_PAGE_SZ);
+ if (ret) {
+ imsic_vector_free(vec);
+ return ret;
+ }
+
irq_domain_set_info(domain, virq, virq, &imsic_irq_base_chip, vec,
handle_edge_irq, NULL, NULL);
irq_set_noprobe(virq);
diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
index b8d1bbbf42f7..082522081a2d 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.c
+++ b/drivers/irqchip/irq-riscv-imsic-state.c
@@ -687,6 +687,32 @@ static int __init imsic_get_mmio_resource(struct fwnode_handle *fwnode,
return of_address_to_resource(to_of_node(fwnode), index, res);
}
+static int __init imsic_init_smode_msi_pa(void)
+{
+ struct imsic_global_config *global = &imsic->global;
+ phys_addr_t *smode_msi_pa;
+ unsigned int cpu, index = 0;
+
+ smode_msi_pa = kcalloc(num_possible_cpus(), sizeof(*smode_msi_pa), GFP_KERNEL);
+ if (!smode_msi_pa)
+ return -ENOMEM;
+
+ for_each_possible_cpu(cpu) {
+ struct imsic_local_config *local = per_cpu_ptr(global->local, cpu);
+
+ if (!local->msi_pa)
+ continue;
+
+ local->smode_msi_pa_index = index;
+ smode_msi_pa[index] = local->msi_pa;
+ index++;
+ }
+
+ imsic->smode_msi_pa = smode_msi_pa;
+ imsic->nr_smode_msi_pa = index;
+ return 0;
+}
+
static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
struct imsic_global_config *global,
u32 *nr_parent_irqs,
@@ -937,6 +963,12 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
goto out_local_cleanup;
}
+ rc = imsic_init_smode_msi_pa();
+ if (rc) {
+ pr_err("%pfwP: failed to initialize S-mode MSI addresses\n", fwnode);
+ goto out_local_cleanup;
+ }
+
/* Initialize matrix allocator */
rc = imsic_matrix_init();
if (rc) {
@@ -962,6 +994,7 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
out_free_local:
free_percpu(imsic->global.local);
out_free_priv:
+ kfree(imsic->smode_msi_pa);
kfree(imsic);
imsic = NULL;
return rc;
diff --git a/drivers/irqchip/irq-riscv-imsic-state.h b/drivers/irqchip/irq-riscv-imsic-state.h
index c42ee180b305..7bc519340b12 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.h
+++ b/drivers/irqchip/irq-riscv-imsic-state.h
@@ -49,6 +49,8 @@ struct imsic_priv {
/* Global configuration common for all HARTs */
struct imsic_global_config global;
+ phys_addr_t *smode_msi_pa;
+ unsigned int nr_smode_msi_pa;
/* Per-CPU state */
struct imsic_local_priv __percpu *lpriv;
diff --git a/include/linux/irqchip/riscv-imsic.h b/include/linux/irqchip/riscv-imsic.h
index ce8fe1ead7a0..523022e0a144 100644
--- a/include/linux/irqchip/riscv-imsic.h
+++ b/include/linux/irqchip/riscv-imsic.h
@@ -40,6 +40,7 @@
struct imsic_local_config {
phys_addr_t msi_pa;
void __iomem *msi_va;
+ unsigned int smode_msi_pa_index;
/* Number of guest interrupt files per-HART */
u32 nr_guest_files;
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-08-27 8:13 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-26 14:52 [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 1/8] iommu/dma: Prepare MSI physical address lists Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 2/8] iommufd: Convert struct iommufd_sw_msi_maps to a growable bitmap Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 3/8] iommufd: Split software MSI map lookup and allocation Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 4/8] iommufd: Bound software MSI mappings to the reserved range Andrew Jones
2026-08-26 14:52 ` [RFC PATCH v1 5/8] iommufd: Prepare software MSI maps for address lists Andrew Jones
2026-08-26 14:53 ` [RFC PATCH v1 6/8] iommufd: Install software MSI map ranges atomically Andrew Jones
2026-08-26 14:53 ` [RFC PATCH v1 7/8] iommufd: Prepare software MSI installation for address lists Andrew Jones
2026-08-26 14:53 ` [RFC PATCH v1 8/8] iommu/dma: Introduce iommu_dma_prepare_msi_list() Andrew Jones
2026-08-27 8:13 ` [RFC PATCH v1 0/8] iommu: Add contiguous MSI mapping support Andrew Jones
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®