Add iova_to_phys_length to struct io_pgtable_ops alongside iova_to_phys.
The new callback additionally returns the block/page size of the PTE
entry through the mapped_length parameter.

Implement in all three io-pgtable backends:
- ARM LPAE: returns ARM_LPAE_BLOCK_SIZE at the resolved level
- ARM v7s: returns block size derived from level mask
- DART: returns pgsize_bitmap (single fixed page size)

The old iova_to_phys is kept as a thin wrapper for backward compat.

Signed-off-by: Guanghui Feng <[email protected]>
Acked-by: Shiqiang Zhang <[email protected]>
Acked-by: Simon Guo <[email protected]>
---
 drivers/iommu/io-pgtable-arm-v7s.c | 30 ++++++++++++++++++++++++------
 drivers/iommu/io-pgtable-arm.c     | 19 ++++++++++++++++++-
 drivers/iommu/io-pgtable-dart.c    | 26 +++++++++++++++++++++-----
 include/linux/io-pgtable.h         |  7 +++++++
 4 files changed, 70 insertions(+), 12 deletions(-)

diff --git a/drivers/iommu/io-pgtable-arm-v7s.c 
b/drivers/iommu/io-pgtable-arm-v7s.c
index 40e33257d3c2..54e8cc673eb6 100644
--- a/drivers/iommu/io-pgtable-arm-v7s.c
+++ b/drivers/iommu/io-pgtable-arm-v7s.c
@@ -641,13 +641,25 @@ static size_t arm_v7s_unmap_pages(struct io_pgtable_ops 
*ops, unsigned long iova
        return unmapped;
 }
 
+static phys_addr_t arm_v7s_iova_to_phys_length(struct io_pgtable_ops *ops,
+                                               unsigned long iova,
+                                               size_t *mapped_length);
+
 static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops,
                                        unsigned long iova)
+{
+       return arm_v7s_iova_to_phys_length(ops, iova, NULL);
+}
+
+static phys_addr_t arm_v7s_iova_to_phys_length(struct io_pgtable_ops *ops,
+                                               unsigned long iova,
+                                               size_t *mapped_length)
 {
        struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
        arm_v7s_iopte *ptep = data->pgd, pte;
        int lvl = 0;
        u32 mask;
+       size_t blk_size;
 
        do {
                ptep += ARM_V7S_LVL_IDX(iova, ++lvl, &data->iop.cfg);
@@ -661,6 +673,11 @@ static phys_addr_t arm_v7s_iova_to_phys(struct 
io_pgtable_ops *ops,
        mask = ARM_V7S_LVL_MASK(lvl);
        if (arm_v7s_pte_is_cont(pte, lvl))
                mask *= ARM_V7S_CONT_PAGES;
+
+       blk_size = ~mask + 1U;
+       if (mapped_length)
+               *mapped_length = blk_size;
+
        return iopte_to_paddr(pte, lvl, &data->iop.cfg) | (iova & ~mask);
 }
 
@@ -714,6 +731,7 @@ static struct io_pgtable *arm_v7s_alloc_pgtable(struct 
io_pgtable_cfg *cfg,
                .map_pages      = arm_v7s_map_pages,
                .unmap_pages    = arm_v7s_unmap_pages,
                .iova_to_phys   = arm_v7s_iova_to_phys,
+               .iova_to_phys_length    = arm_v7s_iova_to_phys_length,
        };
 
        /* We have to do this early for __arm_v7s_alloc_table to work... */
@@ -837,13 +855,13 @@ static int __init arm_v7s_do_selftests(void)
         * Initial sanity checks.
         * Empty page tables shouldn't provide any translations.
         */
-       if (ops->iova_to_phys(ops, 42))
+       if (ops->iova_to_phys_length(ops, 42, NULL))
                return __FAIL(ops);
 
-       if (ops->iova_to_phys(ops, SZ_1G + 42))
+       if (ops->iova_to_phys_length(ops, SZ_1G + 42, NULL))
                return __FAIL(ops);
 
-       if (ops->iova_to_phys(ops, SZ_2G + 42))
+       if (ops->iova_to_phys_length(ops, SZ_2G + 42, NULL))
                return __FAIL(ops);
 
        /*
@@ -864,7 +882,7 @@ static int __init arm_v7s_do_selftests(void)
                                    &mapped))
                        return __FAIL(ops);
 
-               if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
+               if (ops->iova_to_phys_length(ops, iova + 42, NULL) != (iova + 
42))
                        return __FAIL(ops);
 
                iova += SZ_16M;
@@ -878,7 +896,7 @@ static int __init arm_v7s_do_selftests(void)
                if (ops->unmap_pages(ops, iova, size, 1, NULL) != size)
                        return __FAIL(ops);
 
-               if (ops->iova_to_phys(ops, iova + 42))
+               if (ops->iova_to_phys_length(ops, iova + 42, NULL))
                        return __FAIL(ops);
 
                /* Remap full block */
@@ -886,7 +904,7 @@ static int __init arm_v7s_do_selftests(void)
                                   GFP_KERNEL, &mapped))
                        return __FAIL(ops);
 
-               if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
+               if (ops->iova_to_phys_length(ops, iova + 42, NULL) != (iova + 
42))
                        return __FAIL(ops);
 
                iova += SZ_16M;
diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c
index 0208e5897c29..e2ea84669fbf 100644
--- a/drivers/iommu/io-pgtable-arm.c
+++ b/drivers/iommu/io-pgtable-arm.c
@@ -731,8 +731,19 @@ static int visit_iova_to_phys(struct io_pgtable_walk_data 
*walk_data, int lvl,
        return 0;
 }
 
+static phys_addr_t arm_lpae_iova_to_phys_length(struct io_pgtable_ops *ops,
+                                                unsigned long iova,
+                                                size_t *mapped_length);
+
 static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
                                         unsigned long iova)
+{
+       return arm_lpae_iova_to_phys_length(ops, iova, NULL);
+}
+
+static phys_addr_t arm_lpae_iova_to_phys_length(struct io_pgtable_ops *ops,
+                                                unsigned long iova,
+                                                size_t *mapped_length)
 {
        struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
        struct iova_to_phys_data d;
@@ -742,13 +753,18 @@ static phys_addr_t arm_lpae_iova_to_phys(struct 
io_pgtable_ops *ops,
                .addr = iova,
                .end = iova + 1,
        };
+       size_t block_size;
        int ret;
 
        ret = __arm_lpae_iopte_walk(data, &walk_data, data->pgd, 
data->start_level);
        if (ret)
                return 0;
 
-       iova &= (ARM_LPAE_BLOCK_SIZE(d.lvl, data) - 1);
+       block_size = ARM_LPAE_BLOCK_SIZE(d.lvl, data);
+       if (mapped_length)
+               *mapped_length = block_size;
+
+       iova &= (block_size - 1);
        return iopte_to_paddr(d.pte, data) | iova;
 }
 
@@ -948,6 +964,7 @@ arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
                .map_pages      = arm_lpae_map_pages,
                .unmap_pages    = arm_lpae_unmap_pages,
                .iova_to_phys   = arm_lpae_iova_to_phys,
+               .iova_to_phys_length    = arm_lpae_iova_to_phys_length,
                .read_and_clear_dirty = arm_lpae_read_and_clear_dirty,
                .pgtable_walk   = arm_lpae_pgtable_walk,
        };
diff --git a/drivers/iommu/io-pgtable-dart.c b/drivers/iommu/io-pgtable-dart.c
index cbc5d6aa2daa..fa250f5d16ed 100644
--- a/drivers/iommu/io-pgtable-dart.c
+++ b/drivers/iommu/io-pgtable-dart.c
@@ -333,11 +333,23 @@ static size_t dart_unmap_pages(struct io_pgtable_ops 
*ops, unsigned long iova,
        return i * pgsize;
 }
 
+static phys_addr_t dart_iova_to_phys_length(struct io_pgtable_ops *ops,
+                                           unsigned long iova,
+                                           size_t *mapped_length);
+
 static phys_addr_t dart_iova_to_phys(struct io_pgtable_ops *ops,
-                                        unsigned long iova)
+                                    unsigned long iova)
+{
+       return dart_iova_to_phys_length(ops, iova, NULL);
+}
+
+static phys_addr_t dart_iova_to_phys_length(struct io_pgtable_ops *ops,
+                                           unsigned long iova,
+                                           size_t *mapped_length)
 {
        struct dart_io_pgtable *data = io_pgtable_ops_to_data(ops);
        dart_iopte pte, *ptep;
+       size_t pgsize;
 
        ptep = dart_get_last(data, iova);
 
@@ -350,7 +362,10 @@ static phys_addr_t dart_iova_to_phys(struct io_pgtable_ops 
*ops,
        pte = READ_ONCE(*ptep);
        /* Found translation */
        if (pte) {
-               iova &= (data->iop.cfg.pgsize_bitmap - 1);
+               pgsize = data->iop.cfg.pgsize_bitmap;
+               if (mapped_length)
+                       *mapped_length = pgsize;
+               iova &= (pgsize - 1);
                return iopte_to_paddr(pte, data) | iova;
        }
 
@@ -397,9 +412,10 @@ dart_alloc_pgtable(struct io_pgtable_cfg *cfg)
        data->bits_per_level = bits_per_level;
 
        data->iop.ops = (struct io_pgtable_ops) {
-               .map_pages      = dart_map_pages,
-               .unmap_pages    = dart_unmap_pages,
-               .iova_to_phys   = dart_iova_to_phys,
+               .map_pages              = dart_map_pages,
+               .unmap_pages            = dart_unmap_pages,
+               .iova_to_phys           = dart_iova_to_phys,
+               .iova_to_phys_length    = dart_iova_to_phys_length,
        };
 
        return data;
diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h
index e19872e37e06..42bcdd309b88 100644
--- a/include/linux/io-pgtable.h
+++ b/include/linux/io-pgtable.h
@@ -203,6 +203,10 @@ struct arm_lpae_io_pgtable_walk_data {
  * @map_pages:    Map a physically contiguous range of pages of the same size.
  * @unmap_pages:  Unmap a range of virtually contiguous pages of the same size.
  * @iova_to_phys: Translate iova to physical address.
+ * @iova_to_phys_length: Translate iova to physical address and return the
+ *                       remaining mapped length from iova to the end of the
+ *                       mapping entry via @mapped_length. If @mapped_length is
+ *                       NULL, only the physical address is returned.
  * @pgtable_walk: (optional) Perform a page table walk for a given iova.
  * @read_and_clear_dirty: Record dirty info per IOVA. If an IOVA is dirty,
  *                       clear its dirty state from the PTE unless the
@@ -220,6 +224,9 @@ struct io_pgtable_ops {
                              struct iommu_iotlb_gather *gather);
        phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
                                    unsigned long iova);
+       phys_addr_t (*iova_to_phys_length)(struct io_pgtable_ops *ops,
+                                          unsigned long iova,
+                                          size_t *mapped_length);
        int (*pgtable_walk)(struct io_pgtable_ops *ops, unsigned long iova, 
void *wd);
        int (*read_and_clear_dirty)(struct io_pgtable_ops *ops,
                                    unsigned long iova, size_t size,
-- 
2.43.7

Reply via email to