dma-mapping-fast: Fix PMD offset calculation for non-2M aligned start aperture

Modify PMD offset calculation for domains with start apertures not 2M
aligned. Currently, when this happens, the PMD offset is calculated to
the next PMD, and hence each IOVA is mapped as IOVA + offset, causing
the IOVA to be tagged to the wrong PA.

Issue occurrence - With the following sample aperture settings by a
fastmap client:
qcom,iommu-dma-addr-pool = <0x87f10000 0x07f00000>;
qcom,iommu-geometry = <0x87f10000 0x07f00000>;
The effective IOVA range is bounded to iova_base: 0x87f10000, iova_end:
0x8FE10000, which makes the IOVA base not aligned to a 2MB boundary.
While calculating PTE, this adds an extra “default offset” to the PMD
base (since all PMD pages’ base addresses are 2MB aligned), which
further gets added on top of the actual offset obtained by (iova - base).
This causes the final PMD offset to have an additional delta, causing
the IOVA to be tagged to the wrong PA. ALIGN_DOWN(base, SZ_2M) helps to
remove the extra “default offset,” helping to tag the IOVA to the right
PA.

┌────────┐        PMD 1 base
│        ├──────►┌┬──────┬┐◄──
│        │       ││      ││ ALIGN_DOWN(base, SZ_2M)
│PGD page├─┐     ││ old  ││
│        │ │     ││offset││
│        │ │────►│└─     ││◄────
└────────┘ │wrong│  new  ││ base
           │iova │offset─┘│◄────
           │     └────────┘  right
           │                 iova
           │      PMD 2 base
           └────►┌────────┐◄────
                 │        │ ALIGN_UP(base, SZ_2M)
                 │        │
                 │        │
                 │        │
                 │        │
                 └┬───────┘
                  │PMD n base
                 ┌┴───────┐
                 │        │
                 │        │
                 │        │
                 │        │
                 │        │
                 └────────┘

Change-Id: Ie320816ee91710fe06cf2337816d0fb8638ccbcb
Fixes: 2e87440c3e6f ("iommu/io-pgtable-fast: optimize statically allocated pages")
Signed-off-by: Bibek Kumar Patro <quic_bibekkum@quicinc.com>
Signed-off-by: Srinivasarao Pathipati <quic_c_spathi@quicinc.com>
This commit is contained in:
Bibek Kumar Patro 2025-01-10 10:53:44 +05:30 • committed by Srinivasarao Pathipati
commit 695a2b9f1d

View file

@ -133,7 +133,7 @@
typeof(base) __base = (base); \
typeof(pmds) __pmds = (pmds); \
(__iova < __base) ? ERR_PTR(-EINVAL) : \
__pmds + ((__iova - __base) >> AV8L_FAST_PAGE_SHIFT); \
__pmds + ((__iova - ALIGN_DOWN(__base, SZ_2M)) >> AV8L_FAST_PAGE_SHIFT); \
})
static inline dma_addr_t av8l_dma_addr(void *addr)