1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
3 
4 #include <drm/panfrost_drm.h>
5 
6 #include <linux/atomic.h>
7 #include <linux/bitfield.h>
8 #include <linux/delay.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/iopoll.h>
13 #include <linux/io-pgtable.h>
14 #include <linux/iommu.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/shmem_fs.h>
18 #include <linux/sizes.h>
19 
20 #include "panfrost_device.h"
21 #include "panfrost_mmu.h"
22 #include "panfrost_gem.h"
23 #include "panfrost_features.h"
24 #include "panfrost_regs.h"
25 
26 #define mmu_write(dev, reg, data) writel(data, dev->iomem + reg)
27 #define mmu_read(dev, reg) readl(dev->iomem + reg)
28 
29 static int wait_ready(struct panfrost_device *pfdev, u32 as_nr)
30 {
31 	int ret;
32 	u32 val;
33 
34 	/* Wait for the MMU status to indicate there is no active command, in
35 	 * case one is pending. */
36 	ret = readl_relaxed_poll_timeout_atomic(pfdev->iomem + AS_STATUS(as_nr),
37 		val, !(val & AS_STATUS_AS_ACTIVE), 10, 100000);
38 
39 	if (ret) {
40 		/* The GPU hung, let's trigger a reset */
41 		panfrost_device_schedule_reset(pfdev);
42 		dev_err(pfdev->dev, "AS_ACTIVE bit stuck\n");
43 	}
44 
45 	return ret;
46 }
47 
48 static int write_cmd(struct panfrost_device *pfdev, u32 as_nr, u32 cmd)
49 {
50 	int status;
51 
52 	/* write AS_COMMAND when MMU is ready to accept another command */
53 	status = wait_ready(pfdev, as_nr);
54 	if (!status)
55 		mmu_write(pfdev, AS_COMMAND(as_nr), cmd);
56 
57 	return status;
58 }
59 
60 static void lock_region(struct panfrost_device *pfdev, u32 as_nr,
61 			u64 region_start, u64 size)
62 {
63 	u8 region_width;
64 	u64 region;
65 	u64 region_end = region_start + size;
66 
67 	if (!size)
68 		return;
69 
70 	/*
71 	 * The locked region is a naturally aligned power of 2 block encoded as
72 	 * log2 minus(1).
73 	 * Calculate the desired start/end and look for the highest bit which
74 	 * differs. The smallest naturally aligned block must include this bit
75 	 * change, the desired region starts with this bit (and subsequent bits)
76 	 * zeroed and ends with the bit (and subsequent bits) set to one.
77 	 */
78 	region_width = max(fls64(region_start ^ (region_end - 1)),
79 			   const_ilog2(AS_LOCK_REGION_MIN_SIZE)) - 1;
80 
81 	/*
82 	 * Mask off the low bits of region_start (which would be ignored by
83 	 * the hardware anyway)
84 	 */
85 	region_start &= GENMASK_ULL(63, region_width);
86 
87 	region = region_width | region_start;
88 
89 	/* Lock the region that needs to be updated */
90 	mmu_write(pfdev, AS_LOCKADDR_LO(as_nr), lower_32_bits(region));
91 	mmu_write(pfdev, AS_LOCKADDR_HI(as_nr), upper_32_bits(region));
92 	write_cmd(pfdev, as_nr, AS_COMMAND_LOCK);
93 }
94 
95 
96 static int mmu_hw_do_operation_locked(struct panfrost_device *pfdev, int as_nr,
97 				      u64 iova, u64 size, u32 op)
98 {
99 	if (as_nr < 0)
100 		return 0;
101 
102 	if (op != AS_COMMAND_UNLOCK)
103 		lock_region(pfdev, as_nr, iova, size);
104 
105 	/* Run the MMU operation */
106 	write_cmd(pfdev, as_nr, op);
107 
108 	/* Wait for the flush to complete */
109 	return wait_ready(pfdev, as_nr);
110 }
111 
112 static int mmu_hw_do_operation(struct panfrost_device *pfdev,
113 			       struct panfrost_mmu *mmu,
114 			       u64 iova, u64 size, u32 op)
115 {
116 	int ret;
117 
118 	spin_lock(&pfdev->as_lock);
119 	ret = mmu_hw_do_operation_locked(pfdev, mmu->as, iova, size, op);
120 	spin_unlock(&pfdev->as_lock);
121 	return ret;
122 }
123 
124 static void panfrost_mmu_enable(struct panfrost_device *pfdev, struct panfrost_mmu *mmu)
125 {
126 	int as_nr = mmu->as;
127 	struct io_pgtable_cfg *cfg = &mmu->pgtbl_cfg;
128 	u64 transtab = cfg->arm_mali_lpae_cfg.transtab;
129 	u64 memattr = cfg->arm_mali_lpae_cfg.memattr;
130 
131 	mmu_hw_do_operation_locked(pfdev, as_nr, 0, ~0ULL, AS_COMMAND_FLUSH_MEM);
132 
133 	mmu_write(pfdev, AS_TRANSTAB_LO(as_nr), lower_32_bits(transtab));
134 	mmu_write(pfdev, AS_TRANSTAB_HI(as_nr), upper_32_bits(transtab));
135 
136 	/* Need to revisit mem attrs.
137 	 * NC is the default, Mali driver is inner WT.
138 	 */
139 	mmu_write(pfdev, AS_MEMATTR_LO(as_nr), lower_32_bits(memattr));
140 	mmu_write(pfdev, AS_MEMATTR_HI(as_nr), upper_32_bits(memattr));
141 
142 	write_cmd(pfdev, as_nr, AS_COMMAND_UPDATE);
143 }
144 
145 static void panfrost_mmu_disable(struct panfrost_device *pfdev, u32 as_nr)
146 {
147 	mmu_hw_do_operation_locked(pfdev, as_nr, 0, ~0ULL, AS_COMMAND_FLUSH_MEM);
148 
149 	mmu_write(pfdev, AS_TRANSTAB_LO(as_nr), 0);
150 	mmu_write(pfdev, AS_TRANSTAB_HI(as_nr), 0);
151 
152 	mmu_write(pfdev, AS_MEMATTR_LO(as_nr), 0);
153 	mmu_write(pfdev, AS_MEMATTR_HI(as_nr), 0);
154 
155 	write_cmd(pfdev, as_nr, AS_COMMAND_UPDATE);
156 }
157 
158 u32 panfrost_mmu_as_get(struct panfrost_device *pfdev, struct panfrost_mmu *mmu)
159 {
160 	int as;
161 
162 	spin_lock(&pfdev->as_lock);
163 
164 	as = mmu->as;
165 	if (as >= 0) {
166 		int en = atomic_inc_return(&mmu->as_count);
167 		u32 mask = BIT(as) | BIT(16 + as);
168 
169 		/*
170 		 * AS can be retained by active jobs or a perfcnt context,
171 		 * hence the '+ 1' here.
172 		 */
173 		WARN_ON(en >= (NUM_JOB_SLOTS + 1));
174 
175 		list_move(&mmu->list, &pfdev->as_lru_list);
176 
177 		if (pfdev->as_faulty_mask & mask) {
178 			/* Unhandled pagefault on this AS, the MMU was
179 			 * disabled. We need to re-enable the MMU after
180 			 * clearing+unmasking the AS interrupts.
181 			 */
182 			mmu_write(pfdev, MMU_INT_CLEAR, mask);
183 			mmu_write(pfdev, MMU_INT_MASK, ~pfdev->as_faulty_mask);
184 			pfdev->as_faulty_mask &= ~mask;
185 			panfrost_mmu_enable(pfdev, mmu);
186 		}
187 
188 		goto out;
189 	}
190 
191 	/* Check for a free AS */
192 	as = ffz(pfdev->as_alloc_mask);
193 	if (!(BIT(as) & pfdev->features.as_present)) {
194 		struct panfrost_mmu *lru_mmu;
195 
196 		list_for_each_entry_reverse(lru_mmu, &pfdev->as_lru_list, list) {
197 			if (!atomic_read(&lru_mmu->as_count))
198 				break;
199 		}
200 		WARN_ON(&lru_mmu->list == &pfdev->as_lru_list);
201 
202 		list_del_init(&lru_mmu->list);
203 		as = lru_mmu->as;
204 
205 		WARN_ON(as < 0);
206 		lru_mmu->as = -1;
207 	}
208 
209 	/* Assign the free or reclaimed AS to the FD */
210 	mmu->as = as;
211 	set_bit(as, &pfdev->as_alloc_mask);
212 	atomic_set(&mmu->as_count, 1);
213 	list_add(&mmu->list, &pfdev->as_lru_list);
214 
215 	dev_dbg(pfdev->dev, "Assigned AS%d to mmu %p, alloc_mask=%lx", as, mmu, pfdev->as_alloc_mask);
216 
217 	panfrost_mmu_enable(pfdev, mmu);
218 
219 out:
220 	spin_unlock(&pfdev->as_lock);
221 	return as;
222 }
223 
224 void panfrost_mmu_as_put(struct panfrost_device *pfdev, struct panfrost_mmu *mmu)
225 {
226 	atomic_dec(&mmu->as_count);
227 	WARN_ON(atomic_read(&mmu->as_count) < 0);
228 }
229 
230 void panfrost_mmu_reset(struct panfrost_device *pfdev)
231 {
232 	struct panfrost_mmu *mmu, *mmu_tmp;
233 
234 	spin_lock(&pfdev->as_lock);
235 
236 	pfdev->as_alloc_mask = 0;
237 	pfdev->as_faulty_mask = 0;
238 
239 	list_for_each_entry_safe(mmu, mmu_tmp, &pfdev->as_lru_list, list) {
240 		mmu->as = -1;
241 		atomic_set(&mmu->as_count, 0);
242 		list_del_init(&mmu->list);
243 	}
244 
245 	spin_unlock(&pfdev->as_lock);
246 
247 	mmu_write(pfdev, MMU_INT_CLEAR, ~0);
248 	mmu_write(pfdev, MMU_INT_MASK, ~0);
249 }
250 
251 static size_t get_pgsize(u64 addr, size_t size, size_t *count)
252 {
253 	size_t blk_offset = -addr % SZ_2M;
254 
255 	if (blk_offset || size < SZ_2M) {
256 		*count = min_not_zero(blk_offset, size) / SZ_4K;
257 		return SZ_4K;
258 	}
259 	*count = size / SZ_2M;
260 	return SZ_2M;
261 }
262 
263 static void panfrost_mmu_flush_range(struct panfrost_device *pfdev,
264 				     struct panfrost_mmu *mmu,
265 				     u64 iova, u64 size)
266 {
267 	if (mmu->as < 0)
268 		return;
269 
270 	pm_runtime_get_noresume(pfdev->dev);
271 
272 	/* Flush the PTs only if we're already awake */
273 	if (pm_runtime_active(pfdev->dev))
274 		mmu_hw_do_operation(pfdev, mmu, iova, size, AS_COMMAND_FLUSH_PT);
275 
276 	pm_runtime_put_sync_autosuspend(pfdev->dev);
277 }
278 
279 static int mmu_map_sg(struct panfrost_device *pfdev, struct panfrost_mmu *mmu,
280 		      u64 iova, int prot, struct sg_table *sgt)
281 {
282 	unsigned int count;
283 	struct scatterlist *sgl;
284 	struct io_pgtable_ops *ops = mmu->pgtbl_ops;
285 	u64 start_iova = iova;
286 
287 	for_each_sgtable_dma_sg(sgt, sgl, count) {
288 		unsigned long paddr = sg_dma_address(sgl);
289 		size_t len = sg_dma_len(sgl);
290 
291 		dev_dbg(pfdev->dev, "map: as=%d, iova=%llx, paddr=%lx, len=%zx", mmu->as, iova, paddr, len);
292 
293 		while (len) {
294 			size_t pgcount, mapped = 0;
295 			size_t pgsize = get_pgsize(iova | paddr, len, &pgcount);
296 
297 			ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot,
298 				       GFP_KERNEL, &mapped);
299 			/* Don't get stuck if things have gone wrong */
300 			mapped = max(mapped, pgsize);
301 			iova += mapped;
302 			paddr += mapped;
303 			len -= mapped;
304 		}
305 	}
306 
307 	panfrost_mmu_flush_range(pfdev, mmu, start_iova, iova - start_iova);
308 
309 	return 0;
310 }
311 
312 int panfrost_mmu_map(struct panfrost_gem_mapping *mapping)
313 {
314 	struct panfrost_gem_object *bo = mapping->obj;
315 	struct drm_gem_shmem_object *shmem = &bo->base;
316 	struct drm_gem_object *obj = &shmem->base;
317 	struct panfrost_device *pfdev = to_panfrost_device(obj->dev);
318 	struct sg_table *sgt;
319 	int prot = IOMMU_READ | IOMMU_WRITE;
320 
321 	if (WARN_ON(mapping->active))
322 		return 0;
323 
324 	if (bo->noexec)
325 		prot |= IOMMU_NOEXEC;
326 
327 	sgt = drm_gem_shmem_get_pages_sgt(shmem);
328 	if (WARN_ON(IS_ERR(sgt)))
329 		return PTR_ERR(sgt);
330 
331 	mmu_map_sg(pfdev, mapping->mmu, mapping->mmnode.start << PAGE_SHIFT,
332 		   prot, sgt);
333 	mapping->active = true;
334 
335 	return 0;
336 }
337 
338 void panfrost_mmu_unmap(struct panfrost_gem_mapping *mapping)
339 {
340 	struct panfrost_gem_object *bo = mapping->obj;
341 	struct drm_gem_object *obj = &bo->base.base;
342 	struct panfrost_device *pfdev = to_panfrost_device(obj->dev);
343 	struct io_pgtable_ops *ops = mapping->mmu->pgtbl_ops;
344 	u64 iova = mapping->mmnode.start << PAGE_SHIFT;
345 	size_t len = mapping->mmnode.size << PAGE_SHIFT;
346 	size_t unmapped_len = 0;
347 
348 	if (WARN_ON(!mapping->active))
349 		return;
350 
351 	dev_dbg(pfdev->dev, "unmap: as=%d, iova=%llx, len=%zx",
352 		mapping->mmu->as, iova, len);
353 
354 	while (unmapped_len < len) {
355 		size_t unmapped_page, pgcount;
356 		size_t pgsize = get_pgsize(iova, len - unmapped_len, &pgcount);
357 
358 		if (bo->is_heap)
359 			pgcount = 1;
360 		if (!bo->is_heap || ops->iova_to_phys(ops, iova)) {
361 			unmapped_page = ops->unmap_pages(ops, iova, pgsize, pgcount, NULL);
362 			WARN_ON(unmapped_page != pgsize * pgcount);
363 		}
364 		iova += pgsize * pgcount;
365 		unmapped_len += pgsize * pgcount;
366 	}
367 
368 	panfrost_mmu_flush_range(pfdev, mapping->mmu,
369 				 mapping->mmnode.start << PAGE_SHIFT, len);
370 	mapping->active = false;
371 }
372 
373 static void mmu_tlb_inv_context_s1(void *cookie)
374 {}
375 
376 static void mmu_tlb_sync_context(void *cookie)
377 {
378 	//struct panfrost_mmu *mmu = cookie;
379 	// TODO: Wait 1000 GPU cycles for HW_ISSUE_6367/T60X
380 }
381 
382 static void mmu_tlb_flush_walk(unsigned long iova, size_t size, size_t granule,
383 			       void *cookie)
384 {
385 	mmu_tlb_sync_context(cookie);
386 }
387 
388 static const struct iommu_flush_ops mmu_tlb_ops = {
389 	.tlb_flush_all	= mmu_tlb_inv_context_s1,
390 	.tlb_flush_walk = mmu_tlb_flush_walk,
391 };
392 
393 static struct panfrost_gem_mapping *
394 addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)
395 {
396 	struct panfrost_gem_mapping *mapping = NULL;
397 	struct drm_mm_node *node;
398 	u64 offset = addr >> PAGE_SHIFT;
399 	struct panfrost_mmu *mmu;
400 
401 	spin_lock(&pfdev->as_lock);
402 	list_for_each_entry(mmu, &pfdev->as_lru_list, list) {
403 		if (as == mmu->as)
404 			goto found_mmu;
405 	}
406 	goto out;
407 
408 found_mmu:
409 
410 	spin_lock(&mmu->mm_lock);
411 
412 	drm_mm_for_each_node(node, &mmu->mm) {
413 		if (offset >= node->start &&
414 		    offset < (node->start + node->size)) {
415 			mapping = drm_mm_node_to_panfrost_mapping(node);
416 
417 			kref_get(&mapping->refcount);
418 			break;
419 		}
420 	}
421 
422 	spin_unlock(&mmu->mm_lock);
423 out:
424 	spin_unlock(&pfdev->as_lock);
425 	return mapping;
426 }
427 
428 #define NUM_FAULT_PAGES (SZ_2M / PAGE_SIZE)
429 
430 static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
431 				       u64 addr)
432 {
433 	int ret, i;
434 	struct panfrost_gem_mapping *bomapping;
435 	struct panfrost_gem_object *bo;
436 	struct address_space *mapping;
437 	pgoff_t page_offset;
438 	struct sg_table *sgt;
439 	struct page **pages;
440 
441 	bomapping = addr_to_mapping(pfdev, as, addr);
442 	if (!bomapping)
443 		return -ENOENT;
444 
445 	bo = bomapping->obj;
446 	if (!bo->is_heap) {
447 		dev_WARN(pfdev->dev, "matching BO is not heap type (GPU VA = %llx)",
448 			 bomapping->mmnode.start << PAGE_SHIFT);
449 		ret = -EINVAL;
450 		goto err_bo;
451 	}
452 	WARN_ON(bomapping->mmu->as != as);
453 
454 	/* Assume 2MB alignment and size multiple */
455 	addr &= ~((u64)SZ_2M - 1);
456 	page_offset = addr >> PAGE_SHIFT;
457 	page_offset -= bomapping->mmnode.start;
458 
459 	mutex_lock(&bo->base.pages_lock);
460 
461 	if (!bo->base.pages) {
462 		bo->sgts = kvmalloc_array(bo->base.base.size / SZ_2M,
463 				     sizeof(struct sg_table), GFP_KERNEL | __GFP_ZERO);
464 		if (!bo->sgts) {
465 			mutex_unlock(&bo->base.pages_lock);
466 			ret = -ENOMEM;
467 			goto err_bo;
468 		}
469 
470 		pages = kvmalloc_array(bo->base.base.size >> PAGE_SHIFT,
471 				       sizeof(struct page *), GFP_KERNEL | __GFP_ZERO);
472 		if (!pages) {
473 			kvfree(bo->sgts);
474 			bo->sgts = NULL;
475 			mutex_unlock(&bo->base.pages_lock);
476 			ret = -ENOMEM;
477 			goto err_bo;
478 		}
479 		bo->base.pages = pages;
480 		bo->base.pages_use_count = 1;
481 	} else {
482 		pages = bo->base.pages;
483 		if (pages[page_offset]) {
484 			/* Pages are already mapped, bail out. */
485 			mutex_unlock(&bo->base.pages_lock);
486 			goto out;
487 		}
488 	}
489 
490 	mapping = bo->base.base.filp->f_mapping;
491 	mapping_set_unevictable(mapping);
492 
493 	for (i = page_offset; i < page_offset + NUM_FAULT_PAGES; i++) {
494 		pages[i] = shmem_read_mapping_page(mapping, i);
495 		if (IS_ERR(pages[i])) {
496 			mutex_unlock(&bo->base.pages_lock);
497 			ret = PTR_ERR(pages[i]);
498 			goto err_pages;
499 		}
500 	}
501 
502 	mutex_unlock(&bo->base.pages_lock);
503 
504 	sgt = &bo->sgts[page_offset / (SZ_2M / PAGE_SIZE)];
505 	ret = sg_alloc_table_from_pages(sgt, pages + page_offset,
506 					NUM_FAULT_PAGES, 0, SZ_2M, GFP_KERNEL);
507 	if (ret)
508 		goto err_pages;
509 
510 	ret = dma_map_sgtable(pfdev->dev, sgt, DMA_BIDIRECTIONAL, 0);
511 	if (ret)
512 		goto err_map;
513 
514 	mmu_map_sg(pfdev, bomapping->mmu, addr,
515 		   IOMMU_WRITE | IOMMU_READ | IOMMU_NOEXEC, sgt);
516 
517 	bomapping->active = true;
518 
519 	dev_dbg(pfdev->dev, "mapped page fault @ AS%d %llx", as, addr);
520 
521 out:
522 	panfrost_gem_mapping_put(bomapping);
523 
524 	return 0;
525 
526 err_map:
527 	sg_free_table(sgt);
528 err_pages:
529 	drm_gem_shmem_put_pages(&bo->base);
530 err_bo:
531 	panfrost_gem_mapping_put(bomapping);
532 	return ret;
533 }
534 
535 static void panfrost_mmu_release_ctx(struct kref *kref)
536 {
537 	struct panfrost_mmu *mmu = container_of(kref, struct panfrost_mmu,
538 						refcount);
539 	struct panfrost_device *pfdev = mmu->pfdev;
540 
541 	spin_lock(&pfdev->as_lock);
542 	if (mmu->as >= 0) {
543 		pm_runtime_get_noresume(pfdev->dev);
544 		if (pm_runtime_active(pfdev->dev))
545 			panfrost_mmu_disable(pfdev, mmu->as);
546 		pm_runtime_put_autosuspend(pfdev->dev);
547 
548 		clear_bit(mmu->as, &pfdev->as_alloc_mask);
549 		clear_bit(mmu->as, &pfdev->as_in_use_mask);
550 		list_del(&mmu->list);
551 	}
552 	spin_unlock(&pfdev->as_lock);
553 
554 	free_io_pgtable_ops(mmu->pgtbl_ops);
555 	drm_mm_takedown(&mmu->mm);
556 	kfree(mmu);
557 }
558 
559 void panfrost_mmu_ctx_put(struct panfrost_mmu *mmu)
560 {
561 	kref_put(&mmu->refcount, panfrost_mmu_release_ctx);
562 }
563 
564 struct panfrost_mmu *panfrost_mmu_ctx_get(struct panfrost_mmu *mmu)
565 {
566 	kref_get(&mmu->refcount);
567 
568 	return mmu;
569 }
570 
571 #define PFN_4G		(SZ_4G >> PAGE_SHIFT)
572 #define PFN_4G_MASK	(PFN_4G - 1)
573 #define PFN_16M		(SZ_16M >> PAGE_SHIFT)
574 
575 static void panfrost_drm_mm_color_adjust(const struct drm_mm_node *node,
576 					 unsigned long color,
577 					 u64 *start, u64 *end)
578 {
579 	/* Executable buffers can't start or end on a 4GB boundary */
580 	if (!(color & PANFROST_BO_NOEXEC)) {
581 		u64 next_seg;
582 
583 		if ((*start & PFN_4G_MASK) == 0)
584 			(*start)++;
585 
586 		if ((*end & PFN_4G_MASK) == 0)
587 			(*end)--;
588 
589 		next_seg = ALIGN(*start, PFN_4G);
590 		if (next_seg - *start <= PFN_16M)
591 			*start = next_seg + 1;
592 
593 		*end = min(*end, ALIGN(*start, PFN_4G) - 1);
594 	}
595 }
596 
597 struct panfrost_mmu *panfrost_mmu_ctx_create(struct panfrost_device *pfdev)
598 {
599 	struct panfrost_mmu *mmu;
600 
601 	mmu = kzalloc(sizeof(*mmu), GFP_KERNEL);
602 	if (!mmu)
603 		return ERR_PTR(-ENOMEM);
604 
605 	mmu->pfdev = pfdev;
606 	spin_lock_init(&mmu->mm_lock);
607 
608 	/* 4G enough for now. can be 48-bit */
609 	drm_mm_init(&mmu->mm, SZ_32M >> PAGE_SHIFT, (SZ_4G - SZ_32M) >> PAGE_SHIFT);
610 	mmu->mm.color_adjust = panfrost_drm_mm_color_adjust;
611 
612 	INIT_LIST_HEAD(&mmu->list);
613 	mmu->as = -1;
614 
615 	mmu->pgtbl_cfg = (struct io_pgtable_cfg) {
616 		.pgsize_bitmap	= SZ_4K | SZ_2M,
617 		.ias		= FIELD_GET(0xff, pfdev->features.mmu_features),
618 		.oas		= FIELD_GET(0xff00, pfdev->features.mmu_features),
619 		.coherent_walk	= pfdev->coherent,
620 		.tlb		= &mmu_tlb_ops,
621 		.iommu_dev	= pfdev->dev,
622 	};
623 
624 	mmu->pgtbl_ops = alloc_io_pgtable_ops(ARM_MALI_LPAE, &mmu->pgtbl_cfg,
625 					      mmu);
626 	if (!mmu->pgtbl_ops) {
627 		kfree(mmu);
628 		return ERR_PTR(-EINVAL);
629 	}
630 
631 	kref_init(&mmu->refcount);
632 
633 	return mmu;
634 }
635 
636 static const char *access_type_name(struct panfrost_device *pfdev,
637 		u32 fault_status)
638 {
639 	switch (fault_status & AS_FAULTSTATUS_ACCESS_TYPE_MASK) {
640 	case AS_FAULTSTATUS_ACCESS_TYPE_ATOMIC:
641 		if (panfrost_has_hw_feature(pfdev, HW_FEATURE_AARCH64_MMU))
642 			return "ATOMIC";
643 		else
644 			return "UNKNOWN";
645 	case AS_FAULTSTATUS_ACCESS_TYPE_READ:
646 		return "READ";
647 	case AS_FAULTSTATUS_ACCESS_TYPE_WRITE:
648 		return "WRITE";
649 	case AS_FAULTSTATUS_ACCESS_TYPE_EX:
650 		return "EXECUTE";
651 	default:
652 		WARN_ON(1);
653 		return NULL;
654 	}
655 }
656 
657 static irqreturn_t panfrost_mmu_irq_handler(int irq, void *data)
658 {
659 	struct panfrost_device *pfdev = data;
660 
661 	if (!mmu_read(pfdev, MMU_INT_STAT))
662 		return IRQ_NONE;
663 
664 	mmu_write(pfdev, MMU_INT_MASK, 0);
665 	return IRQ_WAKE_THREAD;
666 }
667 
668 static irqreturn_t panfrost_mmu_irq_handler_thread(int irq, void *data)
669 {
670 	struct panfrost_device *pfdev = data;
671 	u32 status = mmu_read(pfdev, MMU_INT_RAWSTAT);
672 	int ret;
673 
674 	while (status) {
675 		u32 as = ffs(status | (status >> 16)) - 1;
676 		u32 mask = BIT(as) | BIT(as + 16);
677 		u64 addr;
678 		u32 fault_status;
679 		u32 exception_type;
680 		u32 access_type;
681 		u32 source_id;
682 
683 		fault_status = mmu_read(pfdev, AS_FAULTSTATUS(as));
684 		addr = mmu_read(pfdev, AS_FAULTADDRESS_LO(as));
685 		addr |= (u64)mmu_read(pfdev, AS_FAULTADDRESS_HI(as)) << 32;
686 
687 		/* decode the fault status */
688 		exception_type = fault_status & 0xFF;
689 		access_type = (fault_status >> 8) & 0x3;
690 		source_id = (fault_status >> 16);
691 
692 		mmu_write(pfdev, MMU_INT_CLEAR, mask);
693 
694 		/* Page fault only */
695 		ret = -1;
696 		if ((status & mask) == BIT(as) && (exception_type & 0xF8) == 0xC0)
697 			ret = panfrost_mmu_map_fault_addr(pfdev, as, addr);
698 
699 		if (ret) {
700 			/* terminal fault, print info about the fault */
701 			dev_err(pfdev->dev,
702 				"Unhandled Page fault in AS%d at VA 0x%016llX\n"
703 				"Reason: %s\n"
704 				"raw fault status: 0x%X\n"
705 				"decoded fault status: %s\n"
706 				"exception type 0x%X: %s\n"
707 				"access type 0x%X: %s\n"
708 				"source id 0x%X\n",
709 				as, addr,
710 				"TODO",
711 				fault_status,
712 				(fault_status & (1 << 10) ? "DECODER FAULT" : "SLAVE FAULT"),
713 				exception_type, panfrost_exception_name(exception_type),
714 				access_type, access_type_name(pfdev, fault_status),
715 				source_id);
716 
717 			spin_lock(&pfdev->as_lock);
718 			/* Ignore MMU interrupts on this AS until it's been
719 			 * re-enabled.
720 			 */
721 			pfdev->as_faulty_mask |= mask;
722 
723 			/* Disable the MMU to kill jobs on this AS. */
724 			panfrost_mmu_disable(pfdev, as);
725 			spin_unlock(&pfdev->as_lock);
726 		}
727 
728 		status &= ~mask;
729 
730 		/* If we received new MMU interrupts, process them before returning. */
731 		if (!status)
732 			status = mmu_read(pfdev, MMU_INT_RAWSTAT) & ~pfdev->as_faulty_mask;
733 	}
734 
735 	spin_lock(&pfdev->as_lock);
736 	mmu_write(pfdev, MMU_INT_MASK, ~pfdev->as_faulty_mask);
737 	spin_unlock(&pfdev->as_lock);
738 
739 	return IRQ_HANDLED;
740 };
741 
742 int panfrost_mmu_init(struct panfrost_device *pfdev)
743 {
744 	int err, irq;
745 
746 	irq = platform_get_irq_byname(to_platform_device(pfdev->dev), "mmu");
747 	if (irq <= 0)
748 		return -ENODEV;
749 
750 	err = devm_request_threaded_irq(pfdev->dev, irq,
751 					panfrost_mmu_irq_handler,
752 					panfrost_mmu_irq_handler_thread,
753 					IRQF_SHARED, KBUILD_MODNAME "-mmu",
754 					pfdev);
755 
756 	if (err) {
757 		dev_err(pfdev->dev, "failed to request mmu irq");
758 		return err;
759 	}
760 
761 	return 0;
762 }
763 
764 void panfrost_mmu_fini(struct panfrost_device *pfdev)
765 {
766 	mmu_write(pfdev, MMU_INT_MASK, 0);
767 }
768