powerpc/kernel/iommu: Align size for IOMMU_PAGE_SIZE() to save TCEs
authorLeonardo Bras <leobras.c@gmail.com>
Thu, 18 Mar 2021 17:44:14 +0000 (14:44 -0300)
committerMichael Ellerman <mpe@ellerman.id.au>
Fri, 23 Apr 2021 02:54:50 +0000 (12:54 +1000)
commit3c0468d4451eb6b4f6604370639f163f9637a479
treef2d55ccac9c9adf36ccf27f3b9c8b307ffa34670
parentee6b25fa7c037e42cc5f3b5c024b2a779edab6dd
powerpc/kernel/iommu: Align size for IOMMU_PAGE_SIZE() to save TCEs

Currently both iommu_alloc_coherent() and iommu_free_coherent() align the
desired allocation size to PAGE_SIZE, and gets system pages and IOMMU
mappings (TCEs) for that value.

When IOMMU_PAGE_SIZE < PAGE_SIZE, this behavior may cause unnecessary
TCEs to be created for mapping the whole system page.

Example:
- PAGE_SIZE = 64k, IOMMU_PAGE_SIZE() = 4k
- iommu_alloc_coherent() is called for 128 bytes
- 1 system page (64k) is allocated
- 16 IOMMU pages (16 x 4k) are allocated (16 TCEs used)

It would be enough to use a single TCE for this, so 15 TCEs are
wasted in the process.

Update iommu_*_coherent() to make sure the size alignment happens only
for IOMMU_PAGE_SIZE() before calling iommu_alloc() and iommu_free().

Also, on iommu_range_alloc(), replace ALIGN(n, 1 << tbl->it_page_shift)
with IOMMU_PAGE_ALIGN(n, tbl), which is easier to read and does the
same.

Signed-off-by: Leonardo Bras <leobras.c@gmail.com>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20210318174414.684630-1-leobras.c@gmail.com
arch/powerpc/kernel/iommu.c