[PATCH v6 1/3] iommu/io-pgtable-arm: Add support for contiguous hint bit

From: Vijayanand Jitta

Date: Tue Dec 01 2026 - 04:38:19 EST


From: Prakash Gupta <prakash.gupta@xxxxxxxxxxxxxxxx>

Add opt-in support for the contiguous hint (CONT) bit in ARM LPAE page
tables. When a naturally aligned, contiguous range is mapped using a
supported effective CONT size, set CONT on every constituent descriptor so
the hardware can combine translations and improve TLB reach.

Advertise effective CONT sizes in pgsize_bitmap only if the caller selects
IO_PGTABLE_QUIRK_ARM_CONT_HINT. The normal IOMMU page-size selection path
can then select those sizes, while io-pgtable-arm expands each request into
its native leaf or block descriptors. This keeps unaudited consumers opted
out.

Dirty tracking updates individual descriptors, but a CONT group requires
identical attributes. Therefore ARM_HD takes precedence and disables the
CONT opt-in.

Suggested-by: Robin Murphy <robin.murphy@xxxxxxx>
Signed-off-by: Vijayanand Jitta <vijayanand.jitta@xxxxxxxxxxxxxxxx>
Signed-off-by: Prakash Gupta <prakash.gupta@xxxxxxxxxxxxxxxx>
---
drivers/iommu/io-pgtable-arm.c | 120 ++++++++++++++++++++++++++++++++++++++---
include/linux/io-pgtable.h | 6 +++
2 files changed, 119 insertions(+), 7 deletions(-)

diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c
index 476c0e25631af..333343084d70b 100644
--- a/drivers/iommu/io-pgtable-arm.c
+++ b/drivers/iommu/io-pgtable-arm.c
@@ -86,6 +86,9 @@
/* Software bit for solving coherency races */
#define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)

+/* PTE Contiguous Bit */
+#define ARM_LPAE_PTE_CONT (((arm_lpae_iopte)1) << 52)
+
/* Stage-1 PTE */
#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
#define ARM_LPAE_PTE_AP_RDONLY_BIT 7
@@ -453,6 +456,49 @@ static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
return old;
}

+static int arm_lpae_num_cont(size_t size)
+{
+ switch (size) {
+ case SZ_4K:
+ case SZ_2M:
+ return 16;
+ case SZ_1G:
+ return sizeof(size_t) >= sizeof(u64) ? 16 : 1;
+ case SZ_64K:
+ case SZ_32M:
+ return 32;
+ case SZ_512M:
+ return sizeof(size_t) >= sizeof(u64) ? 32 : 1;
+ case SZ_16K:
+ return 128;
+ default:
+ return 1;
+ }
+}
+
+static bool arm_lpae_cont_size(size_t block_size, int num_cont,
+ size_t *cont_size)
+{
+ if (num_cont <= 1)
+ return false;
+
+ *cont_size = block_size * num_cont;
+ return true;
+}
+
+static bool arm_lpae_cont_enabled(const struct io_pgtable_cfg *cfg)
+{
+ return cfg->quirks & IO_PGTABLE_QUIRK_ARM_CONT_HINT;
+}
+
+static bool arm_lpae_cont_size_supported(size_t cont_size,
+ const struct io_pgtable_cfg *cfg)
+{
+ unsigned int shift = ilog2(cont_size);
+
+ return shift < BITS_PER_LONG && shift <= cfg->ias && shift <= cfg->oas;
+}
+
static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
phys_addr_t paddr, size_t size, size_t pgcount,
arm_lpae_iopte prot, int lvl, arm_lpae_iopte *ptep,
@@ -462,7 +508,15 @@ static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
size_t tblsz = ARM_LPAE_GRANULE(data);
struct io_pgtable_cfg *cfg = &data->iop.cfg;
- int ret = 0, num_entries, max_entries, map_idx_start;
+ int ret = 0, num_cont, num_entries, max_entries, map_idx_start;
+
+ num_cont = arm_lpae_num_cont(block_size);
+ if (arm_lpae_cont_enabled(cfg) && num_cont > 1 &&
+ size == block_size * num_cont) {
+ size = block_size;
+ pgcount *= num_cont;
+ prot |= ARM_LPAE_PTE_CONT;
+ }

/* Find our entry at the current level */
map_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
@@ -660,12 +714,23 @@ static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
{
arm_lpae_iopte pte;
struct io_pgtable *iop = &data->iop;
+ size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
+ int num_cont;
int i = 0, num_entries, max_entries, unmap_idx_start;
+ bool cont;

/* Something went horribly wrong and we ran out of page table */
if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
return 0;

+ num_cont = arm_lpae_num_cont(block_size);
+ cont = arm_lpae_cont_enabled(&data->iop.cfg) && num_cont > 1 &&
+ size == block_size * num_cont;
+ if (cont) {
+ pgcount *= num_cont;
+ size = block_size;
+ }
+
unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
ptep += unmap_idx_start;
pte = READ_ONCE(*ptep);
@@ -681,12 +746,25 @@ static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,

/* Find and handle non-leaf entries */
for (i = 0; i < num_entries; i++) {
+ int cont_start;
+
pte = READ_ONCE(ptep[i]);
if (!pte) {
WARN_ON(!(data->iop.cfg.quirks & IO_PGTABLE_QUIRK_NO_WARN));
break;
}

+ cont_start = (unmap_idx_start + i) % num_cont;
+ if (!cont && (pte & ARM_LPAE_PTE_CONT) &&
+ (cont_start || i + num_cont > num_entries)) {
+ WARN_ONCE(true,
+ "Unmap of a partial CONT IOPTE group is not allowed");
+ break;
+ }
+
+ if (!cont && (pte & ARM_LPAE_PTE_CONT))
+ i += num_cont - 1;
+
if (!iopte_leaf(pte, lvl, iop->fmt)) {
__arm_lpae_clear_pte(&ptep[i], &iop->cfg, 1);

@@ -724,7 +802,8 @@ static size_t arm_lpae_unmap_pages(struct io_pgtable_ops *ops, unsigned long iov
arm_lpae_iopte *ptep = data->pgd;
long iaext = (s64)iova >> cfg->ias;

- if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount))
+ if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize ||
+ !pgcount || (iova & (pgsize - 1))))
return 0;

if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
@@ -904,7 +983,7 @@ static int arm_lpae_read_and_clear_dirty(struct io_pgtable_ops *ops,
return __arm_lpae_iopte_walk(data, &walk_data, ptep, lvl);
}

-static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
+static void arm_lpae_adjust_pgsizes(struct io_pgtable_cfg *cfg)
{
unsigned long granule, page_sizes;
unsigned int max_addr_bits = 48;
@@ -914,7 +993,8 @@ static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
* translation regime for a particular granule. Aim to match
* the CPU page size if possible, otherwise prefer smaller sizes.
* While we're at it, restrict the block sizes to match the
- * chosen granule.
+ * chosen granule. CONT sizes are added below as effective
+ * mapping sizes, not as additional translation granules.
*/
if (cfg->pgsize_bitmap & PAGE_SIZE)
granule = PAGE_SIZE;
@@ -943,8 +1023,25 @@ static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
}

cfg->pgsize_bitmap &= page_sizes;
+
cfg->ias = min(cfg->ias, max_addr_bits);
cfg->oas = min(cfg->oas, max_addr_bits);
+
+ if (arm_lpae_cont_enabled(cfg)) {
+ unsigned long sizes = cfg->pgsize_bitmap;
+ unsigned int bit;
+
+ for_each_set_bit(bit, &sizes, BITS_PER_LONG) {
+ size_t size = BIT(bit);
+ int num_cont = arm_lpae_num_cont(size);
+ size_t cont_size;
+
+ if (!arm_lpae_cont_size(size, num_cont, &cont_size))
+ continue;
+ if (arm_lpae_cont_size_supported(cont_size, cfg))
+ cfg->pgsize_bitmap |= cont_size;
+ }
+ }
}

static struct arm_lpae_io_pgtable *
@@ -953,7 +1050,7 @@ arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
struct arm_lpae_io_pgtable *data;
int levels, va_bits, pg_shift;

- arm_lpae_restrict_pgsizes(cfg);
+ arm_lpae_adjust_pgsizes(cfg);

if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
return NULL;
@@ -1001,9 +1098,17 @@ arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
IO_PGTABLE_QUIRK_ARM_TTBR1 |
IO_PGTABLE_QUIRK_ARM_OUTER_WBWA |
IO_PGTABLE_QUIRK_ARM_HD |
- IO_PGTABLE_QUIRK_NO_WARN))
+ IO_PGTABLE_QUIRK_NO_WARN |
+ IO_PGTABLE_QUIRK_ARM_CONT_HINT))
return NULL;

+ /*
+ * Dirty tracking updates AP[2] one PTE at a time, which is incompatible
+ * with the uniform attributes required by CONT groups.
+ */
+ if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_HD)
+ cfg->quirks &= ~IO_PGTABLE_QUIRK_ARM_CONT_HINT;
+
data = arm_lpae_alloc_pgtable(cfg);
if (!data)
return NULL;
@@ -1103,7 +1208,8 @@ arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr;

if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_S2FWB |
- IO_PGTABLE_QUIRK_NO_WARN))
+ IO_PGTABLE_QUIRK_NO_WARN |
+ IO_PGTABLE_QUIRK_ARM_CONT_HINT))
return NULL;

data = arm_lpae_alloc_pgtable(cfg);
diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h
index e19872e37e067..016fc1cf1de9f 100644
--- a/include/linux/io-pgtable.h
+++ b/include/linux/io-pgtable.h
@@ -85,7 +85,12 @@ struct io_pgtable_cfg {
* attributes set in the TCR for a non-coherent page-table walker.
*
* IO_PGTABLE_QUIRK_ARM_HD: Enables dirty tracking in stage 1 pagetable.
+ * Contiguous hints are disabled when this quirk is enabled, since dirty
+ * tracking updates individual descriptors while CONT groups require
+ * identical attributes.
* IO_PGTABLE_QUIRK_ARM_S2FWB: Use the FWB format for the MemAttrs bits
+ * IO_PGTABLE_QUIRK_ARM_CONT_HINT: Enable use of the contiguous hint for
+ * page-table consumers that have audited support for it.
*
* IO_PGTABLE_QUIRK_NO_WARN: Do not WARN_ON() on conflicting
* mappings, but silently return -EEXISTS. Normally an attempt
@@ -103,6 +108,7 @@ struct io_pgtable_cfg {
#define IO_PGTABLE_QUIRK_ARM_HD BIT(7)
#define IO_PGTABLE_QUIRK_ARM_S2FWB BIT(8)
#define IO_PGTABLE_QUIRK_NO_WARN BIT(9)
+ #define IO_PGTABLE_QUIRK_ARM_CONT_HINT BIT(10)
unsigned long quirks;
unsigned long pgsize_bitmap;
unsigned int ias;

--
2.34.1