xref: /linux/drivers/gpu/drm/msm/msm_iommu.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <linux/adreno-smmu-priv.h>
8 #include <linux/io-pgtable.h>
9 #include <linux/kmemleak.h>
10 
11 #if defined(CONFIG_ARM_DMA_USE_IOMMU)
12 #include <asm/dma-iommu.h>
13 #else
14 #define arm_iommu_detach_device(...)	({ })
15 #define arm_iommu_release_mapping(...)	({ })
16 #define to_dma_iommu_mapping(dev)	NULL
17 #endif
18 
19 #include "msm_drv.h"
20 #include "msm_gpu_trace.h"
21 #include "msm_mmu.h"
22 
23 struct msm_iommu {
24 	struct msm_mmu base;
25 	struct iommu_domain *domain;
26 
27 	struct mutex init_lock;  /* protects pagetables counter and prr_page */
28 	int pagetables;
29 	struct page *prr_page;
30 
31 	struct kmem_cache *pt_cache;
32 };
33 
34 #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
35 
36 struct msm_iommu_pagetable {
37 	struct msm_mmu base;
38 	struct msm_mmu *parent;
39 	struct io_pgtable_ops *pgtbl_ops;
40 	const struct iommu_flush_ops *tlb;
41 	struct device *iommu_dev;
42 	unsigned long pgsize_bitmap;	/* Bitmap of page sizes in use */
43 	phys_addr_t ttbr;
44 	u32 asid;
45 
46 	/** @root_page_table: Stores the root page table pointer. */
47 	void *root_page_table;
48 };
49 static struct msm_iommu_pagetable *to_pagetable(struct msm_mmu *mmu)
50 {
51 	return container_of(mmu, struct msm_iommu_pagetable, base);
52 }
53 
54 /* based on iommu_pgsize() in iommu.c: */
55 static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable,
56 			   unsigned long iova, phys_addr_t paddr,
57 			   size_t size, size_t *count)
58 {
59 	unsigned int pgsize_idx, pgsize_idx_next;
60 	unsigned long pgsizes;
61 	size_t offset, pgsize, pgsize_next;
62 	unsigned long addr_merge = paddr | iova;
63 
64 	/* Page sizes supported by the hardware and small enough for @size */
65 	pgsizes = pagetable->pgsize_bitmap & GENMASK(__fls(size), 0);
66 
67 	/* Constrain the page sizes further based on the maximum alignment */
68 	if (likely(addr_merge))
69 		pgsizes &= GENMASK(__ffs(addr_merge), 0);
70 
71 	/* Make sure we have at least one suitable page size */
72 	BUG_ON(!pgsizes);
73 
74 	/* Pick the biggest page size remaining */
75 	pgsize_idx = __fls(pgsizes);
76 	pgsize = BIT(pgsize_idx);
77 	if (!count)
78 		return pgsize;
79 
80 	/* Find the next biggest support page size, if it exists */
81 	pgsizes = pagetable->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
82 	if (!pgsizes)
83 		goto out_set_count;
84 
85 	pgsize_idx_next = __ffs(pgsizes);
86 	pgsize_next = BIT(pgsize_idx_next);
87 
88 	/*
89 	 * There's no point trying a bigger page size unless the virtual
90 	 * and physical addresses are similarly offset within the larger page.
91 	 */
92 	if ((iova ^ paddr) & (pgsize_next - 1))
93 		goto out_set_count;
94 
95 	/* Calculate the offset to the next page size alignment boundary */
96 	offset = pgsize_next - (addr_merge & (pgsize_next - 1));
97 
98 	/*
99 	 * If size is big enough to accommodate the larger page, reduce
100 	 * the number of smaller pages.
101 	 */
102 	if (offset + pgsize_next <= size)
103 		size = offset;
104 
105 out_set_count:
106 	*count = size >> pgsize_idx;
107 	return pgsize;
108 }
109 
110 static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova,
111 		size_t size)
112 {
113 	struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
114 	struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
115 	int ret = 0;
116 
117 	while (size) {
118 		size_t pgsize, count;
119 		ssize_t unmapped;
120 
121 		pgsize = calc_pgsize(pagetable, iova, iova, size, &count);
122 
123 		unmapped = ops->unmap_pages(ops, iova, pgsize, count, NULL);
124 		if (unmapped <= 0) {
125 			ret = -EINVAL;
126 			/*
127 			 * Continue attempting to unamp the remained of the
128 			 * range, so we don't end up with some dangling
129 			 * mapped pages
130 			 */
131 			unmapped = PAGE_SIZE;
132 		}
133 
134 		iova += unmapped;
135 		size -= unmapped;
136 	}
137 
138 	iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain);
139 
140 	return ret;
141 }
142 
143 static int msm_iommu_pagetable_map_prr(struct msm_mmu *mmu, u64 iova, size_t len, int prot)
144 {
145 	struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
146 	struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
147 	struct msm_iommu *iommu = to_msm_iommu(pagetable->parent);
148 	phys_addr_t phys = page_to_phys(iommu->prr_page);
149 	u64 addr = iova;
150 
151 	while (len) {
152 		size_t mapped = 0;
153 		size_t size = PAGE_SIZE;
154 		int ret;
155 
156 		ret = ops->map_pages(ops, addr, phys, size, 1, prot, GFP_KERNEL, &mapped);
157 
158 		/* map_pages could fail after mapping some of the pages,
159 		 * so update the counters before error handling.
160 		 */
161 		addr += mapped;
162 		len  -= mapped;
163 
164 		if (ret) {
165 			msm_iommu_pagetable_unmap(mmu, iova, addr - iova);
166 			return -EINVAL;
167 		}
168 	}
169 
170 	return 0;
171 }
172 
173 static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova,
174 				   struct sg_table *sgt, size_t off, size_t len,
175 				   int prot)
176 {
177 	struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
178 	struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
179 	struct scatterlist *sg;
180 	u64 addr = iova;
181 	unsigned int i;
182 
183 	if (!sgt)
184 		return msm_iommu_pagetable_map_prr(mmu, iova, len, prot);
185 
186 	for_each_sgtable_sg(sgt, sg, i) {
187 		size_t size = sg->length;
188 		phys_addr_t phys = sg_phys(sg);
189 
190 		if (!len)
191 			break;
192 
193 		if (size <= off) {
194 			off -= size;
195 			continue;
196 		}
197 
198 		phys += off;
199 		size -= off;
200 		size = min_t(size_t, size, len);
201 		off = 0;
202 
203 		while (size) {
204 			size_t pgsize, count, mapped = 0;
205 			int ret;
206 
207 			pgsize = calc_pgsize(pagetable, addr, phys, size, &count);
208 
209 			ret = ops->map_pages(ops, addr, phys, pgsize, count,
210 					     prot, GFP_KERNEL, &mapped);
211 
212 			/* map_pages could fail after mapping some of the pages,
213 			 * so update the counters before error handling.
214 			 */
215 			phys += mapped;
216 			addr += mapped;
217 			size -= mapped;
218 			len  -= mapped;
219 
220 			if (ret) {
221 				msm_iommu_pagetable_unmap(mmu, iova, addr - iova);
222 				return -EINVAL;
223 			}
224 		}
225 	}
226 
227 	return 0;
228 }
229 
230 static void msm_iommu_pagetable_destroy(struct msm_mmu *mmu)
231 {
232 	struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
233 	struct msm_iommu *iommu = to_msm_iommu(pagetable->parent);
234 	struct adreno_smmu_priv *adreno_smmu =
235 		dev_get_drvdata(pagetable->parent->dev);
236 
237 	/*
238 	 * If this is the last attached pagetable for the parent,
239 	 * disable TTBR0 in the arm-smmu driver
240 	 */
241 	mutex_lock(&iommu->init_lock);
242 	if (--iommu->pagetables == 0) {
243 		adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, NULL);
244 
245 		if (adreno_smmu->set_prr_bit) {
246 			adreno_smmu->set_prr_bit(adreno_smmu->cookie, false);
247 			__free_page(iommu->prr_page);
248 			iommu->prr_page = NULL;
249 		}
250 	}
251 	mutex_unlock(&iommu->init_lock);
252 
253 	free_io_pgtable_ops(pagetable->pgtbl_ops);
254 	kfree(pagetable);
255 }
256 
257 int msm_iommu_pagetable_params(struct msm_mmu *mmu,
258 		phys_addr_t *ttbr, int *asid)
259 {
260 	struct msm_iommu_pagetable *pagetable;
261 
262 	if (mmu->type != MSM_MMU_IOMMU_PAGETABLE)
263 		return -EINVAL;
264 
265 	pagetable = to_pagetable(mmu);
266 
267 	if (ttbr)
268 		*ttbr = pagetable->ttbr;
269 
270 	if (asid)
271 		*asid = pagetable->asid;
272 
273 	return 0;
274 }
275 
276 struct iommu_domain_geometry *msm_iommu_get_geometry(struct msm_mmu *mmu)
277 {
278 	struct msm_iommu *iommu = to_msm_iommu(mmu);
279 
280 	return &iommu->domain->geometry;
281 }
282 
283 int
284 msm_iommu_pagetable_walk(struct msm_mmu *mmu, unsigned long iova, uint64_t ptes[4])
285 {
286 	struct msm_iommu_pagetable *pagetable;
287 	struct arm_lpae_io_pgtable_walk_data wd = {};
288 
289 	if (mmu->type != MSM_MMU_IOMMU_PAGETABLE)
290 		return -EINVAL;
291 
292 	pagetable = to_pagetable(mmu);
293 
294 	if (!pagetable->pgtbl_ops->pgtable_walk)
295 		return -EINVAL;
296 
297 	pagetable->pgtbl_ops->pgtable_walk(pagetable->pgtbl_ops, iova, &wd);
298 
299 	for (int i = 0; i < ARRAY_SIZE(wd.ptes); i++)
300 		ptes[i] = wd.ptes[i];
301 
302 	return 0;
303 }
304 
305 static void
306 msm_iommu_pagetable_prealloc_count(struct msm_mmu *mmu, struct msm_mmu_prealloc *p,
307 				   uint64_t iova, size_t len)
308 {
309 	u64 pt_count;
310 
311 	/*
312 	 * L1, L2 and L3 page tables.
313 	 *
314 	 * We could optimize L3 allocation by iterating over the sgt and merging
315 	 * 2M contiguous blocks, but it's simpler to over-provision and return
316 	 * the pages if they're not used.
317 	 *
318 	 * The first level descriptor (v8 / v7-lpae page table format) encodes
319 	 * 30 bits of address.  The second level encodes 29.  For the 3rd it is
320 	 * 39.
321 	 *
322 	 * https://developer.arm.com/documentation/ddi0406/c/System-Level-Architecture/Virtual-Memory-System-Architecture--VMSA-/Long-descriptor-translation-table-format/Long-descriptor-translation-table-format-descriptors?lang=en#BEIHEFFB
323 	 */
324 	pt_count = ((ALIGN(iova + len, 1ull << 39) - ALIGN_DOWN(iova, 1ull << 39)) >> 39) +
325 		   ((ALIGN(iova + len, 1ull << 30) - ALIGN_DOWN(iova, 1ull << 30)) >> 30) +
326 		   ((ALIGN(iova + len, 1ull << 21) - ALIGN_DOWN(iova, 1ull << 21)) >> 21);
327 
328 	p->count += pt_count;
329 }
330 
331 static struct kmem_cache *
332 get_pt_cache(struct msm_mmu *mmu)
333 {
334 	struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
335 	return to_msm_iommu(pagetable->parent)->pt_cache;
336 }
337 
338 static int
339 msm_iommu_pagetable_prealloc_allocate(struct msm_mmu *mmu, struct msm_mmu_prealloc *p)
340 {
341 	struct kmem_cache *pt_cache = get_pt_cache(mmu);
342 
343 	if (!p->count) {
344 		p->pages = NULL;
345 		return 0;
346 	}
347 
348 	p->pages = kvmalloc_objs(*p->pages, p->count);
349 	if (!p->pages)
350 		return -ENOMEM;
351 
352 	if (!kmem_cache_alloc_bulk(pt_cache, GFP_KERNEL, p->count, p->pages)) {
353 		kvfree(p->pages);
354 		p->pages = NULL;
355 		p->count = 0;
356 		return -ENOMEM;
357 	}
358 
359 	return 0;
360 }
361 
362 static void
363 msm_iommu_pagetable_prealloc_cleanup(struct msm_mmu *mmu, struct msm_mmu_prealloc *p)
364 {
365 	struct kmem_cache *pt_cache = get_pt_cache(mmu);
366 	uint32_t remaining_pt_count = p->count - p->ptr;
367 
368 	if (!p->pages)
369 		return;
370 
371 	if (p->count > 0)
372 		trace_msm_mmu_prealloc_cleanup(p->count, remaining_pt_count);
373 
374 	kmem_cache_free_bulk(pt_cache, remaining_pt_count, &p->pages[p->ptr]);
375 	kvfree(p->pages);
376 }
377 
378 /**
379  * msm_iommu_pagetable_alloc_pt() - Custom page table allocator
380  * @cookie: Cookie passed at page table allocation time.
381  * @size: Size of the page table. This size should be fixed,
382  * and determined at creation time based on the granule size.
383  * @gfp: GFP flags.
384  *
385  * We want a custom allocator so we can use a cache for page table
386  * allocations and amortize the cost of the over-reservation that's
387  * done to allow asynchronous VM operations.
388  *
389  * Return: non-NULL on success, NULL if the allocation failed for any
390  * reason.
391  */
392 static void *
393 msm_iommu_pagetable_alloc_pt(void *cookie, size_t size, gfp_t gfp)
394 {
395 	struct msm_iommu_pagetable *pagetable = cookie;
396 	struct msm_mmu_prealloc *p = pagetable->base.prealloc;
397 	void *page;
398 
399 	/* Allocation of the root page table happening during init. */
400 	if (unlikely(!pagetable->root_page_table)) {
401 		struct page *p;
402 
403 		p = alloc_pages_node(dev_to_node(pagetable->iommu_dev),
404 				     gfp | __GFP_ZERO, get_order(size));
405 		page = p ? page_address(p) : NULL;
406 		pagetable->root_page_table = page;
407 		return page;
408 	}
409 
410 	if (WARN_ON(!p) || WARN_ON(p->ptr >= p->count))
411 		return NULL;
412 
413 	page = p->pages[p->ptr++];
414 	memset(page, 0, size);
415 
416 	/*
417 	 * Page table entries don't use virtual addresses, which trips out
418 	 * kmemleak. kmemleak_alloc_phys() might work, but physical addresses
419 	 * are mixed with other fields, and I fear kmemleak won't detect that
420 	 * either.
421 	 *
422 	 * Let's just ignore memory passed to the page-table driver for now.
423 	 */
424 	kmemleak_ignore(page);
425 
426 	return page;
427 }
428 
429 
430 /**
431  * msm_iommu_pagetable_free_pt() - Custom page table free function
432  * @cookie: Cookie passed at page table allocation time.
433  * @data: Page table to free.
434  * @size: Size of the page table. This size should be fixed,
435  * and determined at creation time based on the granule size.
436  */
437 static void
438 msm_iommu_pagetable_free_pt(void *cookie, void *data, size_t size)
439 {
440 	struct msm_iommu_pagetable *pagetable = cookie;
441 
442 	if (unlikely(pagetable->root_page_table == data)) {
443 		free_pages((unsigned long)data, get_order(size));
444 		pagetable->root_page_table = NULL;
445 		return;
446 	}
447 
448 	kmem_cache_free(get_pt_cache(&pagetable->base), data);
449 }
450 
451 static const struct msm_mmu_funcs pagetable_funcs = {
452 		.prealloc_count = msm_iommu_pagetable_prealloc_count,
453 		.prealloc_allocate = msm_iommu_pagetable_prealloc_allocate,
454 		.prealloc_cleanup = msm_iommu_pagetable_prealloc_cleanup,
455 		.map = msm_iommu_pagetable_map,
456 		.unmap = msm_iommu_pagetable_unmap,
457 		.destroy = msm_iommu_pagetable_destroy,
458 };
459 
460 static void msm_iommu_tlb_flush_all(void *cookie)
461 {
462 	struct msm_iommu_pagetable *pagetable = cookie;
463 	struct adreno_smmu_priv *adreno_smmu;
464 
465 	if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
466 		return;
467 
468 	adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
469 
470 	pagetable->tlb->tlb_flush_all((void *)adreno_smmu->cookie);
471 
472 	pm_runtime_put_autosuspend(pagetable->iommu_dev);
473 }
474 
475 static void msm_iommu_tlb_flush_walk(unsigned long iova, size_t size,
476 		size_t granule, void *cookie)
477 {
478 	struct msm_iommu_pagetable *pagetable = cookie;
479 	struct adreno_smmu_priv *adreno_smmu;
480 
481 	if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
482 		return;
483 
484 	adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
485 
486 	pagetable->tlb->tlb_flush_walk(iova, size, granule, (void *)adreno_smmu->cookie);
487 
488 	pm_runtime_put_autosuspend(pagetable->iommu_dev);
489 }
490 
491 static void msm_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
492 		unsigned long iova, size_t granule, void *cookie)
493 {
494 }
495 
496 static const struct iommu_flush_ops tlb_ops = {
497 	.tlb_flush_all = msm_iommu_tlb_flush_all,
498 	.tlb_flush_walk = msm_iommu_tlb_flush_walk,
499 	.tlb_add_page = msm_iommu_tlb_add_page,
500 };
501 
502 static int msm_gpu_fault_handler(struct iommu_domain *domain, struct device *dev,
503 		unsigned long iova, int flags, void *arg);
504 
505 static size_t get_tblsz(const struct io_pgtable_cfg *cfg)
506 {
507 	int pg_shift, bits_per_level;
508 
509 	pg_shift = __ffs(cfg->pgsize_bitmap);
510 	/* arm_lpae_iopte is u64: */
511 	bits_per_level = pg_shift - ilog2(sizeof(u64));
512 
513 	return sizeof(u64) << bits_per_level;
514 }
515 
516 struct msm_mmu *msm_iommu_pagetable_create(struct msm_mmu *parent, bool kernel_managed)
517 {
518 	struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(parent->dev);
519 	struct msm_iommu *iommu = to_msm_iommu(parent);
520 	struct msm_iommu_pagetable *pagetable;
521 	const struct io_pgtable_cfg *ttbr1_cfg = NULL;
522 	struct io_pgtable_cfg ttbr0_cfg;
523 	int ret;
524 
525 	/* Get the pagetable configuration from the domain */
526 	if (adreno_smmu->cookie)
527 		ttbr1_cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
528 
529 	/*
530 	 * If you hit this WARN_ONCE() you are probably missing an entry in
531 	 * qcom_smmu_impl_of_match[] in arm-smmu-qcom.c
532 	 */
533 	if (WARN_ONCE(!ttbr1_cfg, "No per-process page tables"))
534 		return ERR_PTR(-ENODEV);
535 
536 	pagetable = kzalloc_obj(*pagetable);
537 	if (!pagetable)
538 		return ERR_PTR(-ENOMEM);
539 
540 	msm_mmu_init(&pagetable->base, parent->dev, &pagetable_funcs,
541 		MSM_MMU_IOMMU_PAGETABLE);
542 
543 	/* Clone the TTBR1 cfg as starting point for TTBR0 cfg: */
544 	ttbr0_cfg = *ttbr1_cfg;
545 
546 	/* The incoming cfg will have the TTBR1 quirk enabled */
547 	ttbr0_cfg.quirks &= ~IO_PGTABLE_QUIRK_ARM_TTBR1;
548 	ttbr0_cfg.tlb = &tlb_ops;
549 
550 	if (!kernel_managed) {
551 		ttbr0_cfg.quirks |= IO_PGTABLE_QUIRK_NO_WARN;
552 
553 		/*
554 		 * With userspace managed VM (aka VM_BIND), we need to pre-
555 		 * allocate pages ahead of time for map/unmap operations,
556 		 * handing them to io-pgtable via custom alloc/free ops as
557 		 * needed:
558 		 */
559 		ttbr0_cfg.alloc = msm_iommu_pagetable_alloc_pt;
560 		ttbr0_cfg.free  = msm_iommu_pagetable_free_pt;
561 
562 		/*
563 		 * Restrict to single page granules.  Otherwise we may run
564 		 * into a situation where userspace wants to unmap/remap
565 		 * only a part of a larger block mapping, which is not
566 		 * possible without unmapping the entire block.  Which in
567 		 * turn could cause faults if the GPU is accessing other
568 		 * parts of the block mapping.
569 		 *
570 		 * Note that prior to commit 33729a5fc0ca ("iommu/io-pgtable-arm:
571 		 * Remove split on unmap behavior)" this was handled in
572 		 * io-pgtable-arm.  But this apparently does not work
573 		 * correctly on SMMUv3.
574 		 */
575 		WARN_ON(!(ttbr0_cfg.pgsize_bitmap & PAGE_SIZE));
576 		ttbr0_cfg.pgsize_bitmap = PAGE_SIZE;
577 	}
578 
579 	pagetable->iommu_dev = ttbr1_cfg->iommu_dev;
580 	pagetable->pgtbl_ops = alloc_io_pgtable_ops(ARM_64_LPAE_S1,
581 		&ttbr0_cfg, pagetable);
582 
583 	if (!pagetable->pgtbl_ops) {
584 		kfree(pagetable);
585 		return ERR_PTR(-ENOMEM);
586 	}
587 
588 	/*
589 	 * If this is the first pagetable that we've allocated, send it back to
590 	 * the arm-smmu driver as a trigger to set up TTBR0
591 	 */
592 	mutex_lock(&iommu->init_lock);
593 	if (iommu->pagetables++ == 0) {
594 		ret = adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, &ttbr0_cfg);
595 		if (ret) {
596 			iommu->pagetables--;
597 			mutex_unlock(&iommu->init_lock);
598 			free_io_pgtable_ops(pagetable->pgtbl_ops);
599 			kfree(pagetable);
600 			return ERR_PTR(ret);
601 		}
602 
603 		BUG_ON(iommu->prr_page);
604 		if (adreno_smmu->set_prr_bit) {
605 			/*
606 			 * We need a zero'd page for two reasons:
607 			 *
608 			 * 1) Reserve a known physical address to use when
609 			 *    mapping NULL / sparsely resident regions
610 			 * 2) Read back zero
611 			 *
612 			 * It appears the hw drops writes to the PRR region
613 			 * on the floor, but reads actually return whatever
614 			 * is in the PRR page.
615 			 */
616 			iommu->prr_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
617 			adreno_smmu->set_prr_addr(adreno_smmu->cookie,
618 						  page_to_phys(iommu->prr_page));
619 			adreno_smmu->set_prr_bit(adreno_smmu->cookie, true);
620 		}
621 	}
622 	mutex_unlock(&iommu->init_lock);
623 
624 	/* Needed later for TLB flush */
625 	pagetable->parent = parent;
626 	pagetable->tlb = ttbr1_cfg->tlb;
627 	pagetable->pgsize_bitmap = ttbr0_cfg.pgsize_bitmap;
628 	pagetable->ttbr = ttbr0_cfg.arm_lpae_s1_cfg.ttbr;
629 
630 	/*
631 	 * TODO we would like each set of page tables to have a unique ASID
632 	 * to optimize TLB invalidation.  But iommu_flush_iotlb_all() will
633 	 * end up flushing the ASID used for TTBR1 pagetables, which is not
634 	 * what we want.  So for now just use the same ASID as TTBR1.
635 	 */
636 	pagetable->asid = 0;
637 
638 	return &pagetable->base;
639 }
640 
641 static int msm_gpu_fault_handler(struct iommu_domain *domain, struct device *dev,
642 		unsigned long iova, int flags, void *arg)
643 {
644 	struct msm_iommu *iommu = arg;
645 	struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(iommu->base.dev);
646 	struct adreno_smmu_fault_info info, *ptr = NULL;
647 
648 	if (adreno_smmu->get_fault_info) {
649 		adreno_smmu->get_fault_info(adreno_smmu->cookie, &info);
650 		ptr = &info;
651 	}
652 
653 	if (iommu->base.handler)
654 		return iommu->base.handler(iommu->base.arg, iova, flags, ptr);
655 
656 	pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags);
657 
658 	return 0;
659 }
660 
661 static int msm_disp_fault_handler(struct iommu_domain *domain, struct device *dev,
662 				  unsigned long iova, int flags, void *arg)
663 {
664 	struct msm_iommu *iommu = arg;
665 
666 	if (iommu->base.handler)
667 		return iommu->base.handler(iommu->base.arg, iova, flags, NULL);
668 
669 	return -ENOSYS;
670 }
671 
672 static void msm_iommu_set_stall(struct msm_mmu *mmu, bool enable)
673 {
674 	struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(mmu->dev);
675 
676 	if (adreno_smmu->set_stall)
677 		adreno_smmu->set_stall(adreno_smmu->cookie, enable);
678 }
679 
680 static void msm_iommu_detach(struct msm_mmu *mmu)
681 {
682 	struct msm_iommu *iommu = to_msm_iommu(mmu);
683 
684 	iommu_detach_device(iommu->domain, mmu->dev);
685 }
686 
687 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
688 			 struct sg_table *sgt, size_t off, size_t len,
689 			 int prot)
690 {
691 	struct msm_iommu *iommu = to_msm_iommu(mmu);
692 	ssize_t ret;
693 
694 	WARN_ON(off != 0);
695 
696 	/* The arm-smmu driver expects the addresses to be sign extended */
697 	if (iova & BIT_ULL(48))
698 		iova |= GENMASK_ULL(63, 49);
699 
700 	ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot);
701 	if (ret < 0)
702 		return ret;
703 
704 	return (ret == len) ? 0 : -EINVAL;
705 }
706 
707 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len)
708 {
709 	struct msm_iommu *iommu = to_msm_iommu(mmu);
710 
711 	if (iova & BIT_ULL(48))
712 		iova |= GENMASK_ULL(63, 49);
713 
714 	iommu_unmap(iommu->domain, iova, len);
715 
716 	return 0;
717 }
718 
719 static void msm_iommu_destroy(struct msm_mmu *mmu)
720 {
721 	struct msm_iommu *iommu = to_msm_iommu(mmu);
722 	iommu_domain_free(iommu->domain);
723 	kmem_cache_destroy(iommu->pt_cache);
724 	kfree(iommu);
725 }
726 
727 static const struct msm_mmu_funcs funcs = {
728 		.detach = msm_iommu_detach,
729 		.map = msm_iommu_map,
730 		.unmap = msm_iommu_unmap,
731 		.destroy = msm_iommu_destroy,
732 		.set_stall = msm_iommu_set_stall,
733 };
734 
735 struct msm_mmu *msm_iommu_new(struct device *dev, unsigned long quirks)
736 {
737 	struct iommu_domain *domain;
738 	struct msm_iommu *iommu;
739 	int ret;
740 
741 	if (!device_iommu_mapped(dev))
742 		return ERR_PTR(-ENODEV);
743 
744 	domain = iommu_paging_domain_alloc(dev);
745 	if (IS_ERR(domain))
746 		return ERR_CAST(domain);
747 
748 	iommu_set_pgtable_quirks(domain, quirks);
749 
750 	iommu = kzalloc_obj(*iommu);
751 	if (!iommu) {
752 		iommu_domain_free(domain);
753 		return ERR_PTR(-ENOMEM);
754 	}
755 
756 	iommu->domain = domain;
757 	msm_mmu_init(&iommu->base, dev, &funcs, MSM_MMU_IOMMU);
758 
759 	mutex_init(&iommu->init_lock);
760 
761 	/*
762 	 * ARM32 attaches a DMA mapping domain to every IOMMU-backed device,
763 	 * which would make attaching our own domain fail with -EBUSY.
764 	 */
765 	if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
766 		struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
767 
768 		if (mapping) {
769 			arm_iommu_detach_device(dev);
770 			arm_iommu_release_mapping(mapping);
771 		}
772 	}
773 
774 	ret = iommu_attach_device(iommu->domain, dev);
775 	if (ret) {
776 		iommu_domain_free(domain);
777 		kfree(iommu);
778 		return ERR_PTR(ret);
779 	}
780 
781 	return &iommu->base;
782 }
783 
784 struct msm_mmu *msm_iommu_disp_new(struct device *dev, unsigned long quirks)
785 {
786 	struct msm_iommu *iommu;
787 	struct msm_mmu *mmu;
788 
789 	mmu = msm_iommu_new(dev, quirks);
790 	if (IS_ERR(mmu))
791 		return mmu;
792 
793 	iommu = to_msm_iommu(mmu);
794 	iommu_set_fault_handler(iommu->domain, msm_disp_fault_handler, iommu);
795 
796 	return mmu;
797 }
798 
799 struct msm_mmu *msm_iommu_gpu_new(struct device *dev, struct msm_gpu *gpu, unsigned long quirks)
800 {
801 	struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
802 	struct msm_iommu *iommu;
803 	struct msm_mmu *mmu;
804 
805 	mmu = msm_iommu_new(dev, quirks);
806 	if (IS_ERR(mmu))
807 		return mmu;
808 
809 	iommu = to_msm_iommu(mmu);
810 	if (adreno_smmu->cookie) {
811 		const struct io_pgtable_cfg *cfg =
812 			adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
813 		size_t tblsz = get_tblsz(cfg);
814 
815 		iommu->pt_cache =
816 			kmem_cache_create("msm-mmu-pt", tblsz, tblsz, 0, NULL);
817 	}
818 	iommu_set_fault_handler(iommu->domain, msm_gpu_fault_handler, iommu);
819 
820 	/* Enable stall on iommu fault: */
821 	if (adreno_smmu->set_stall)
822 		adreno_smmu->set_stall(adreno_smmu->cookie, true);
823 
824 	return mmu;
825 }
826