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