1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
7 #include <linux/adreno-smmu-priv.h>
8 #include <linux/io-pgtable.h>
14 struct iommu_domain *domain;
18 #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
20 struct msm_iommu_pagetable {
22 struct msm_mmu *parent;
23 struct io_pgtable_ops *pgtbl_ops;
24 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
28 static struct msm_iommu_pagetable *to_pagetable(struct msm_mmu *mmu)
30 return container_of(mmu, struct msm_iommu_pagetable, base);
33 /* based on iommu_pgsize() in iommu.c: */
34 static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable,
35 unsigned long iova, phys_addr_t paddr,
36 size_t size, size_t *count)
38 unsigned int pgsize_idx, pgsize_idx_next;
39 unsigned long pgsizes;
40 size_t offset, pgsize, pgsize_next;
41 unsigned long addr_merge = paddr | iova;
43 /* Page sizes supported by the hardware and small enough for @size */
44 pgsizes = pagetable->pgsize_bitmap & GENMASK(__fls(size), 0);
46 /* Constrain the page sizes further based on the maximum alignment */
47 if (likely(addr_merge))
48 pgsizes &= GENMASK(__ffs(addr_merge), 0);
50 /* Make sure we have at least one suitable page size */
53 /* Pick the biggest page size remaining */
54 pgsize_idx = __fls(pgsizes);
55 pgsize = BIT(pgsize_idx);
59 /* Find the next biggest support page size, if it exists */
60 pgsizes = pagetable->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
64 pgsize_idx_next = __ffs(pgsizes);
65 pgsize_next = BIT(pgsize_idx_next);
68 * There's no point trying a bigger page size unless the virtual
69 * and physical addresses are similarly offset within the larger page.
71 if ((iova ^ paddr) & (pgsize_next - 1))
74 /* Calculate the offset to the next page size alignment boundary */
75 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
78 * If size is big enough to accommodate the larger page, reduce
79 * the number of smaller pages.
81 if (offset + pgsize_next <= size)
85 *count = size >> pgsize_idx;
89 static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova,
92 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
93 struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
96 size_t unmapped, pgsize, count;
98 pgsize = calc_pgsize(pagetable, iova, iova, size, &count);
100 unmapped = ops->unmap_pages(ops, iova, pgsize, count, NULL);
108 iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain);
110 return (size == 0) ? 0 : -EINVAL;
113 static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova,
114 struct sg_table *sgt, size_t len, int prot)
116 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
117 struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
118 struct scatterlist *sg;
122 for_each_sgtable_sg(sgt, sg, i) {
123 size_t size = sg->length;
124 phys_addr_t phys = sg_phys(sg);
127 size_t pgsize, count, mapped = 0;
130 pgsize = calc_pgsize(pagetable, addr, phys, size, &count);
132 ret = ops->map_pages(ops, addr, phys, pgsize, count,
133 prot, GFP_KERNEL, &mapped);
135 /* map_pages could fail after mapping some of the pages,
136 * so update the counters before error handling.
143 msm_iommu_pagetable_unmap(mmu, iova, addr - iova);
152 static void msm_iommu_pagetable_destroy(struct msm_mmu *mmu)
154 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
155 struct msm_iommu *iommu = to_msm_iommu(pagetable->parent);
156 struct adreno_smmu_priv *adreno_smmu =
157 dev_get_drvdata(pagetable->parent->dev);
160 * If this is the last attached pagetable for the parent,
161 * disable TTBR0 in the arm-smmu driver
163 if (atomic_dec_return(&iommu->pagetables) == 0)
164 adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, NULL);
166 free_io_pgtable_ops(pagetable->pgtbl_ops);
170 int msm_iommu_pagetable_params(struct msm_mmu *mmu,
171 phys_addr_t *ttbr, int *asid)
173 struct msm_iommu_pagetable *pagetable;
175 if (mmu->type != MSM_MMU_IOMMU_PAGETABLE)
178 pagetable = to_pagetable(mmu);
181 *ttbr = pagetable->ttbr;
184 *asid = pagetable->asid;
189 struct iommu_domain_geometry *msm_iommu_get_geometry(struct msm_mmu *mmu)
191 struct msm_iommu *iommu = to_msm_iommu(mmu);
193 return &iommu->domain->geometry;
196 static const struct msm_mmu_funcs pagetable_funcs = {
197 .map = msm_iommu_pagetable_map,
198 .unmap = msm_iommu_pagetable_unmap,
199 .destroy = msm_iommu_pagetable_destroy,
202 static void msm_iommu_tlb_flush_all(void *cookie)
206 static void msm_iommu_tlb_flush_walk(unsigned long iova, size_t size,
207 size_t granule, void *cookie)
211 static void msm_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
212 unsigned long iova, size_t granule, void *cookie)
216 static const struct iommu_flush_ops null_tlb_ops = {
217 .tlb_flush_all = msm_iommu_tlb_flush_all,
218 .tlb_flush_walk = msm_iommu_tlb_flush_walk,
219 .tlb_add_page = msm_iommu_tlb_add_page,
222 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
223 unsigned long iova, int flags, void *arg);
225 struct msm_mmu *msm_iommu_pagetable_create(struct msm_mmu *parent)
227 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(parent->dev);
228 struct msm_iommu *iommu = to_msm_iommu(parent);
229 struct msm_iommu_pagetable *pagetable;
230 const struct io_pgtable_cfg *ttbr1_cfg = NULL;
231 struct io_pgtable_cfg ttbr0_cfg;
234 /* Get the pagetable configuration from the domain */
235 if (adreno_smmu->cookie)
236 ttbr1_cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
239 * If you hit this WARN_ONCE() you are probably missing an entry in
240 * qcom_smmu_impl_of_match[] in arm-smmu-qcom.c
242 if (WARN_ONCE(!ttbr1_cfg, "No per-process page tables"))
243 return ERR_PTR(-ENODEV);
245 pagetable = kzalloc(sizeof(*pagetable), GFP_KERNEL);
247 return ERR_PTR(-ENOMEM);
249 msm_mmu_init(&pagetable->base, parent->dev, &pagetable_funcs,
250 MSM_MMU_IOMMU_PAGETABLE);
252 /* Clone the TTBR1 cfg as starting point for TTBR0 cfg: */
253 ttbr0_cfg = *ttbr1_cfg;
255 /* The incoming cfg will have the TTBR1 quirk enabled */
256 ttbr0_cfg.quirks &= ~IO_PGTABLE_QUIRK_ARM_TTBR1;
257 ttbr0_cfg.tlb = &null_tlb_ops;
259 pagetable->pgtbl_ops = alloc_io_pgtable_ops(ARM_64_LPAE_S1,
260 &ttbr0_cfg, iommu->domain);
262 if (!pagetable->pgtbl_ops) {
264 return ERR_PTR(-ENOMEM);
268 * If this is the first pagetable that we've allocated, send it back to
269 * the arm-smmu driver as a trigger to set up TTBR0
271 if (atomic_inc_return(&iommu->pagetables) == 1) {
272 ret = adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, &ttbr0_cfg);
274 free_io_pgtable_ops(pagetable->pgtbl_ops);
280 /* Needed later for TLB flush */
281 pagetable->parent = parent;
282 pagetable->pgsize_bitmap = ttbr0_cfg.pgsize_bitmap;
283 pagetable->ttbr = ttbr0_cfg.arm_lpae_s1_cfg.ttbr;
286 * TODO we would like each set of page tables to have a unique ASID
287 * to optimize TLB invalidation. But iommu_flush_iotlb_all() will
288 * end up flushing the ASID used for TTBR1 pagetables, which is not
289 * what we want. So for now just use the same ASID as TTBR1.
293 return &pagetable->base;
296 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
297 unsigned long iova, int flags, void *arg)
299 struct msm_iommu *iommu = arg;
300 struct msm_mmu *mmu = &iommu->base;
301 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(iommu->base.dev);
302 struct adreno_smmu_fault_info info, *ptr = NULL;
304 if (adreno_smmu->get_fault_info) {
305 adreno_smmu->get_fault_info(adreno_smmu->cookie, &info);
309 if (iommu->base.handler)
310 return iommu->base.handler(iommu->base.arg, iova, flags, ptr);
312 pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags);
314 if (mmu->funcs->resume_translation)
315 mmu->funcs->resume_translation(mmu);
320 static void msm_iommu_resume_translation(struct msm_mmu *mmu)
322 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(mmu->dev);
324 if (adreno_smmu->resume_translation)
325 adreno_smmu->resume_translation(adreno_smmu->cookie, true);
328 static void msm_iommu_detach(struct msm_mmu *mmu)
330 struct msm_iommu *iommu = to_msm_iommu(mmu);
332 iommu_detach_device(iommu->domain, mmu->dev);
335 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
336 struct sg_table *sgt, size_t len, int prot)
338 struct msm_iommu *iommu = to_msm_iommu(mmu);
341 /* The arm-smmu driver expects the addresses to be sign extended */
342 if (iova & BIT_ULL(48))
343 iova |= GENMASK_ULL(63, 49);
345 ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot);
348 return (ret == len) ? 0 : -EINVAL;
351 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len)
353 struct msm_iommu *iommu = to_msm_iommu(mmu);
355 if (iova & BIT_ULL(48))
356 iova |= GENMASK_ULL(63, 49);
358 iommu_unmap(iommu->domain, iova, len);
363 static void msm_iommu_destroy(struct msm_mmu *mmu)
365 struct msm_iommu *iommu = to_msm_iommu(mmu);
366 iommu_domain_free(iommu->domain);
370 static const struct msm_mmu_funcs funcs = {
371 .detach = msm_iommu_detach,
372 .map = msm_iommu_map,
373 .unmap = msm_iommu_unmap,
374 .destroy = msm_iommu_destroy,
375 .resume_translation = msm_iommu_resume_translation,
378 struct msm_mmu *msm_iommu_new(struct device *dev, unsigned long quirks)
380 struct iommu_domain *domain;
381 struct msm_iommu *iommu;
384 domain = iommu_domain_alloc(dev->bus);
388 iommu_set_pgtable_quirks(domain, quirks);
390 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
392 iommu_domain_free(domain);
393 return ERR_PTR(-ENOMEM);
396 iommu->domain = domain;
397 msm_mmu_init(&iommu->base, dev, &funcs, MSM_MMU_IOMMU);
399 atomic_set(&iommu->pagetables, 0);
401 ret = iommu_attach_device(iommu->domain, dev);
403 iommu_domain_free(domain);
411 struct msm_mmu *msm_iommu_gpu_new(struct device *dev, struct msm_gpu *gpu, unsigned long quirks)
413 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
414 struct msm_iommu *iommu;
417 mmu = msm_iommu_new(dev, quirks);
418 if (IS_ERR_OR_NULL(mmu))
421 iommu = to_msm_iommu(mmu);
422 iommu_set_fault_handler(iommu->domain, msm_fault_handler, iommu);
424 /* Enable stall on iommu fault: */
425 if (adreno_smmu->set_stall)
426 adreno_smmu->set_stall(adreno_smmu->cookie, true);