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 const struct iommu_flush_ops *tlb;
25 struct device *iommu_dev;
26 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
30 static struct msm_iommu_pagetable *to_pagetable(struct msm_mmu *mmu)
32 return container_of(mmu, struct msm_iommu_pagetable, base);
35 /* based on iommu_pgsize() in iommu.c: */
36 static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable,
37 unsigned long iova, phys_addr_t paddr,
38 size_t size, size_t *count)
40 unsigned int pgsize_idx, pgsize_idx_next;
41 unsigned long pgsizes;
42 size_t offset, pgsize, pgsize_next;
43 unsigned long addr_merge = paddr | iova;
45 /* Page sizes supported by the hardware and small enough for @size */
46 pgsizes = pagetable->pgsize_bitmap & GENMASK(__fls(size), 0);
48 /* Constrain the page sizes further based on the maximum alignment */
49 if (likely(addr_merge))
50 pgsizes &= GENMASK(__ffs(addr_merge), 0);
52 /* Make sure we have at least one suitable page size */
55 /* Pick the biggest page size remaining */
56 pgsize_idx = __fls(pgsizes);
57 pgsize = BIT(pgsize_idx);
61 /* Find the next biggest support page size, if it exists */
62 pgsizes = pagetable->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
66 pgsize_idx_next = __ffs(pgsizes);
67 pgsize_next = BIT(pgsize_idx_next);
70 * There's no point trying a bigger page size unless the virtual
71 * and physical addresses are similarly offset within the larger page.
73 if ((iova ^ paddr) & (pgsize_next - 1))
76 /* Calculate the offset to the next page size alignment boundary */
77 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
80 * If size is big enough to accommodate the larger page, reduce
81 * the number of smaller pages.
83 if (offset + pgsize_next <= size)
87 *count = size >> pgsize_idx;
91 static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova,
94 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
95 struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
98 size_t unmapped, pgsize, count;
100 pgsize = calc_pgsize(pagetable, iova, iova, size, &count);
102 unmapped = ops->unmap_pages(ops, iova, pgsize, count, NULL);
110 iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain);
112 return (size == 0) ? 0 : -EINVAL;
115 static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova,
116 struct sg_table *sgt, size_t len, int prot)
118 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
119 struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
120 struct scatterlist *sg;
124 for_each_sgtable_sg(sgt, sg, i) {
125 size_t size = sg->length;
126 phys_addr_t phys = sg_phys(sg);
129 size_t pgsize, count, mapped = 0;
132 pgsize = calc_pgsize(pagetable, addr, phys, size, &count);
134 ret = ops->map_pages(ops, addr, phys, pgsize, count,
135 prot, GFP_KERNEL, &mapped);
137 /* map_pages could fail after mapping some of the pages,
138 * so update the counters before error handling.
145 msm_iommu_pagetable_unmap(mmu, iova, addr - iova);
154 static void msm_iommu_pagetable_destroy(struct msm_mmu *mmu)
156 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
157 struct msm_iommu *iommu = to_msm_iommu(pagetable->parent);
158 struct adreno_smmu_priv *adreno_smmu =
159 dev_get_drvdata(pagetable->parent->dev);
162 * If this is the last attached pagetable for the parent,
163 * disable TTBR0 in the arm-smmu driver
165 if (atomic_dec_return(&iommu->pagetables) == 0)
166 adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, NULL);
168 free_io_pgtable_ops(pagetable->pgtbl_ops);
172 int msm_iommu_pagetable_params(struct msm_mmu *mmu,
173 phys_addr_t *ttbr, int *asid)
175 struct msm_iommu_pagetable *pagetable;
177 if (mmu->type != MSM_MMU_IOMMU_PAGETABLE)
180 pagetable = to_pagetable(mmu);
183 *ttbr = pagetable->ttbr;
186 *asid = pagetable->asid;
191 struct iommu_domain_geometry *msm_iommu_get_geometry(struct msm_mmu *mmu)
193 struct msm_iommu *iommu = to_msm_iommu(mmu);
195 return &iommu->domain->geometry;
198 static const struct msm_mmu_funcs pagetable_funcs = {
199 .map = msm_iommu_pagetable_map,
200 .unmap = msm_iommu_pagetable_unmap,
201 .destroy = msm_iommu_pagetable_destroy,
204 static void msm_iommu_tlb_flush_all(void *cookie)
206 struct msm_iommu_pagetable *pagetable = cookie;
207 struct adreno_smmu_priv *adreno_smmu;
209 if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
212 adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
214 pagetable->tlb->tlb_flush_all((void *)adreno_smmu->cookie);
216 pm_runtime_put_autosuspend(pagetable->iommu_dev);
219 static void msm_iommu_tlb_flush_walk(unsigned long iova, size_t size,
220 size_t granule, void *cookie)
222 struct msm_iommu_pagetable *pagetable = cookie;
223 struct adreno_smmu_priv *adreno_smmu;
225 if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
228 adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
230 pagetable->tlb->tlb_flush_walk(iova, size, granule, (void *)adreno_smmu->cookie);
232 pm_runtime_put_autosuspend(pagetable->iommu_dev);
235 static void msm_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
236 unsigned long iova, size_t granule, void *cookie)
240 static const struct iommu_flush_ops tlb_ops = {
241 .tlb_flush_all = msm_iommu_tlb_flush_all,
242 .tlb_flush_walk = msm_iommu_tlb_flush_walk,
243 .tlb_add_page = msm_iommu_tlb_add_page,
246 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
247 unsigned long iova, int flags, void *arg);
249 struct msm_mmu *msm_iommu_pagetable_create(struct msm_mmu *parent)
251 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(parent->dev);
252 struct msm_iommu *iommu = to_msm_iommu(parent);
253 struct msm_iommu_pagetable *pagetable;
254 const struct io_pgtable_cfg *ttbr1_cfg = NULL;
255 struct io_pgtable_cfg ttbr0_cfg;
258 /* Get the pagetable configuration from the domain */
259 if (adreno_smmu->cookie)
260 ttbr1_cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
263 * If you hit this WARN_ONCE() you are probably missing an entry in
264 * qcom_smmu_impl_of_match[] in arm-smmu-qcom.c
266 if (WARN_ONCE(!ttbr1_cfg, "No per-process page tables"))
267 return ERR_PTR(-ENODEV);
269 pagetable = kzalloc(sizeof(*pagetable), GFP_KERNEL);
271 return ERR_PTR(-ENOMEM);
273 msm_mmu_init(&pagetable->base, parent->dev, &pagetable_funcs,
274 MSM_MMU_IOMMU_PAGETABLE);
276 /* Clone the TTBR1 cfg as starting point for TTBR0 cfg: */
277 ttbr0_cfg = *ttbr1_cfg;
279 /* The incoming cfg will have the TTBR1 quirk enabled */
280 ttbr0_cfg.quirks &= ~IO_PGTABLE_QUIRK_ARM_TTBR1;
281 ttbr0_cfg.tlb = &tlb_ops;
283 pagetable->pgtbl_ops = alloc_io_pgtable_ops(ARM_64_LPAE_S1,
284 &ttbr0_cfg, pagetable);
286 if (!pagetable->pgtbl_ops) {
288 return ERR_PTR(-ENOMEM);
292 * If this is the first pagetable that we've allocated, send it back to
293 * the arm-smmu driver as a trigger to set up TTBR0
295 if (atomic_inc_return(&iommu->pagetables) == 1) {
296 ret = adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, &ttbr0_cfg);
298 free_io_pgtable_ops(pagetable->pgtbl_ops);
304 /* Needed later for TLB flush */
305 pagetable->parent = parent;
306 pagetable->tlb = ttbr1_cfg->tlb;
307 pagetable->iommu_dev = ttbr1_cfg->iommu_dev;
308 pagetable->pgsize_bitmap = ttbr0_cfg.pgsize_bitmap;
309 pagetable->ttbr = ttbr0_cfg.arm_lpae_s1_cfg.ttbr;
312 * TODO we would like each set of page tables to have a unique ASID
313 * to optimize TLB invalidation. But iommu_flush_iotlb_all() will
314 * end up flushing the ASID used for TTBR1 pagetables, which is not
315 * what we want. So for now just use the same ASID as TTBR1.
319 return &pagetable->base;
322 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
323 unsigned long iova, int flags, void *arg)
325 struct msm_iommu *iommu = arg;
326 struct msm_mmu *mmu = &iommu->base;
327 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(iommu->base.dev);
328 struct adreno_smmu_fault_info info, *ptr = NULL;
330 if (adreno_smmu->get_fault_info) {
331 adreno_smmu->get_fault_info(adreno_smmu->cookie, &info);
335 if (iommu->base.handler)
336 return iommu->base.handler(iommu->base.arg, iova, flags, ptr);
338 pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags);
340 if (mmu->funcs->resume_translation)
341 mmu->funcs->resume_translation(mmu);
346 static void msm_iommu_resume_translation(struct msm_mmu *mmu)
348 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(mmu->dev);
350 if (adreno_smmu->resume_translation)
351 adreno_smmu->resume_translation(adreno_smmu->cookie, true);
354 static void msm_iommu_detach(struct msm_mmu *mmu)
356 struct msm_iommu *iommu = to_msm_iommu(mmu);
358 iommu_detach_device(iommu->domain, mmu->dev);
361 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
362 struct sg_table *sgt, size_t len, int prot)
364 struct msm_iommu *iommu = to_msm_iommu(mmu);
367 /* The arm-smmu driver expects the addresses to be sign extended */
368 if (iova & BIT_ULL(48))
369 iova |= GENMASK_ULL(63, 49);
371 ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot);
374 return (ret == len) ? 0 : -EINVAL;
377 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len)
379 struct msm_iommu *iommu = to_msm_iommu(mmu);
381 if (iova & BIT_ULL(48))
382 iova |= GENMASK_ULL(63, 49);
384 iommu_unmap(iommu->domain, iova, len);
389 static void msm_iommu_destroy(struct msm_mmu *mmu)
391 struct msm_iommu *iommu = to_msm_iommu(mmu);
392 iommu_domain_free(iommu->domain);
396 static const struct msm_mmu_funcs funcs = {
397 .detach = msm_iommu_detach,
398 .map = msm_iommu_map,
399 .unmap = msm_iommu_unmap,
400 .destroy = msm_iommu_destroy,
401 .resume_translation = msm_iommu_resume_translation,
404 struct msm_mmu *msm_iommu_new(struct device *dev, unsigned long quirks)
406 struct iommu_domain *domain;
407 struct msm_iommu *iommu;
410 if (!device_iommu_mapped(dev))
413 domain = iommu_paging_domain_alloc(dev);
415 return ERR_CAST(domain);
417 iommu_set_pgtable_quirks(domain, quirks);
419 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
421 iommu_domain_free(domain);
422 return ERR_PTR(-ENOMEM);
425 iommu->domain = domain;
426 msm_mmu_init(&iommu->base, dev, &funcs, MSM_MMU_IOMMU);
428 atomic_set(&iommu->pagetables, 0);
430 ret = iommu_attach_device(iommu->domain, dev);
432 iommu_domain_free(domain);
440 struct msm_mmu *msm_iommu_gpu_new(struct device *dev, struct msm_gpu *gpu, unsigned long quirks)
442 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
443 struct msm_iommu *iommu;
446 mmu = msm_iommu_new(dev, quirks);
447 if (IS_ERR_OR_NULL(mmu))
450 iommu = to_msm_iommu(mmu);
451 iommu_set_fault_handler(iommu->domain, msm_fault_handler, iommu);
453 /* Enable stall on iommu fault: */
454 if (adreno_smmu->set_stall)
455 adreno_smmu->set_stall(adreno_smmu->cookie, true);