We're going to re-use these in the next commit. No functional changes, just some ugly callbacks.
Tested-by: Jianfeng Liu <[email protected]> # x1e78100 (Acer SFA14-11), v7.3-rc5 + this series, CONFIG_DMABUF_DEBUG=y Signed-off-by: Rob Clark <[email protected]> --- drivers/gpu/drm/msm/msm_iommu.c | 155 +++++++++++++++++++++----------- 1 file changed, 101 insertions(+), 54 deletions(-) diff --git a/drivers/gpu/drm/msm/msm_iommu.c b/drivers/gpu/drm/msm/msm_iommu.c index da6782fca6bd..78c82f4a01b9 100644 --- a/drivers/gpu/drm/msm/msm_iommu.c +++ b/drivers/gpu/drm/msm/msm_iommu.c @@ -52,9 +52,8 @@ static struct msm_iommu_pagetable *to_pagetable(struct msm_mmu *mmu) } /* based on iommu_pgsize() in iommu.c: */ -static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable, - unsigned long iova, phys_addr_t paddr, - size_t size, size_t *count) +static size_t calc_pgsize(unsigned long pgsize_bitmap, unsigned long iova, + phys_addr_t paddr, size_t size, size_t *count) { unsigned int pgsize_idx, pgsize_idx_next; unsigned long pgsizes; @@ -62,7 +61,7 @@ static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable, unsigned long addr_merge = paddr | iova; /* Page sizes supported by the hardware and small enough for @size */ - pgsizes = pagetable->pgsize_bitmap & GENMASK(__fls(size), 0); + pgsizes = pgsize_bitmap & GENMASK(__fls(size), 0); /* Constrain the page sizes further based on the maximum alignment */ if (likely(addr_merge)) @@ -78,7 +77,7 @@ static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable, return pgsize; /* Find the next biggest support page size, if it exists */ - pgsizes = pagetable->pgsize_bitmap & ~GENMASK(pgsize_idx, 0); + pgsizes = pgsize_bitmap & ~GENMASK(pgsize_idx, 0); if (!pgsizes) goto out_set_count; @@ -107,24 +106,27 @@ static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable, return pgsize; } -static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova, - size_t size) +typedef size_t (*unmap_fn)(void *arg, u64 iova, size_t pgsize, size_t count); +typedef int (*map_fn)(void *arg, phys_addr_t paddr, u64 iova, size_t pgsize, + size_t pgcount, int prot, size_t *mapped); + +static inline int +__do_unmap(unsigned long pgsize_bitmap, u64 iova, size_t size, + void *arg, unmap_fn unmap) { - struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); - struct io_pgtable_ops *ops = pagetable->pgtbl_ops; int ret = 0; while (size) { size_t pgsize, count; ssize_t unmapped; - pgsize = calc_pgsize(pagetable, iova, iova, size, &count); + pgsize = calc_pgsize(pgsize_bitmap, iova, iova, size, &count); - unmapped = ops->unmap_pages(ops, iova, pgsize, count, NULL); + unmapped = unmap(arg, iova, pgsize, count); if (unmapped <= 0) { ret = -EINVAL; /* - * Continue attempting to unamp the remained of the + * Continue attempting to unmap the remained of the * range, so we don't end up with some dangling * mapped pages */ @@ -135,6 +137,83 @@ static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova, size -= unmapped; } + return ret; +} + +static inline int +__do_map(unsigned long pgsize_bitmap, u64 iova, struct sg_table *sgt, size_t off, + size_t len, int prot, void *arg, map_fn map, unmap_fn unmap) +{ + struct scatterlist *sg; + u64 addr = iova; + unsigned int i; + + for_each_sgtable_sg (sgt, sg, i) { + size_t size = sg->length; + phys_addr_t phys = sg_phys(sg); + + if (!len) + break; + + if (size <= off) { + off -= size; + continue; + } + + phys += off; + size -= off; + size = min_t(size_t, size, len); + off = 0; + + while (size) { + size_t pgsize, count, mapped = 0; + int ret; + + pgsize = calc_pgsize(pgsize_bitmap, addr, phys, size, &count); + + ret = map(arg, phys, addr, pgsize, count, prot, &mapped); + + /* map_pages could fail after mapping some of the pages, + * so update the counters before error handling. + */ + phys += mapped; + addr += mapped; + size -= mapped; + len -= mapped; + + if (ret) { + __do_unmap(pgsize_bitmap, iova, addr - iova, arg, unmap); + return -EINVAL; + } + } + } + + return 0; +} + +static size_t +__unmap_pgtable(void *arg, u64 iova, size_t pgsize, size_t pgcount) +{ + struct io_pgtable_ops *ops = arg; + return ops->unmap_pages(ops, iova, pgsize, pgcount, NULL); +} + +static int +__map_pgtable(void *arg, phys_addr_t paddr, u64 iova, size_t pgsize, + size_t pgcount, int prot, size_t *mapped) +{ + struct io_pgtable_ops *ops = arg; + return ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, GFP_KERNEL, mapped); +} + +static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova, size_t size) +{ + struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); + struct io_pgtable_ops *ops = pagetable->pgtbl_ops; + int ret = 0; + + ret = __do_unmap(pagetable->pgsize_bitmap, iova, size, ops, __unmap_pgtable); + iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain); return ret; @@ -176,55 +255,23 @@ static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova, { struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); struct io_pgtable_ops *ops = pagetable->pgtbl_ops; - struct scatterlist *sg; - u64 addr = iova; - unsigned int i; + int ret; if (!sgt) return msm_iommu_pagetable_map_prr(mmu, iova, len, prot); - for_each_sgtable_sg(sgt, sg, i) { - size_t size = sg->length; - phys_addr_t phys = sg_phys(sg); - - if (!len) - break; - - if (size <= off) { - off -= size; - continue; - } - - phys += off; - size -= off; - size = min_t(size_t, size, len); - off = 0; - - while (size) { - size_t pgsize, count, mapped = 0; - int ret; - - pgsize = calc_pgsize(pagetable, addr, phys, size, &count); - - ret = ops->map_pages(ops, addr, phys, pgsize, count, - prot, GFP_KERNEL, &mapped); - - /* map_pages could fail after mapping some of the pages, - * so update the counters before error handling. - */ - phys += mapped; - addr += mapped; - size -= mapped; - len -= mapped; + ret = __do_map(pagetable->pgsize_bitmap, iova, sgt, off, len, prot, + ops, __map_pgtable, __unmap_pgtable); - if (ret) { - msm_iommu_pagetable_unmap(mmu, iova, addr - iova); - return -EINVAL; - } - } + if (ret) { + /* + * If the map failed mid-way, it will have triggered an + * unmap, so we need to flush the TLB: + */ + iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain); } - return 0; + return ret; } static void msm_iommu_pagetable_destroy(struct msm_mmu *mmu) -- 2.56.0
