xref: /openbmc/linux/drivers/gpu/drm/etnaviv/etnaviv_mmu.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1f6ffbd4fSLucas Stach // SPDX-License-Identifier: GPL-2.0
2a8c21a54SThe etnaviv authors /*
3f6ffbd4fSLucas Stach  * Copyright (C) 2015-2018 Etnaviv Project
4a8c21a54SThe etnaviv authors  */
5a8c21a54SThe etnaviv authors 
627b67278SLucas Stach #include <linux/dma-mapping.h>
76eae41feSSam Ravnborg #include <linux/scatterlist.h>
86eae41feSSam Ravnborg 
9dd34bb96SLucas Stach #include "common.xml.h"
10ea1f5729SLucas Stach #include "etnaviv_cmdbuf.h"
11a8c21a54SThe etnaviv authors #include "etnaviv_drv.h"
12a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
13a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
14a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
15a8c21a54SThe etnaviv authors 
etnaviv_context_unmap(struct etnaviv_iommu_context * context,unsigned long iova,size_t size)1627b67278SLucas Stach static void etnaviv_context_unmap(struct etnaviv_iommu_context *context,
1750073cf9SLucas Stach 				 unsigned long iova, size_t size)
1850073cf9SLucas Stach {
1950073cf9SLucas Stach 	size_t unmapped_page, unmapped = 0;
2050073cf9SLucas Stach 	size_t pgsize = SZ_4K;
2150073cf9SLucas Stach 
2250073cf9SLucas Stach 	if (!IS_ALIGNED(iova | size, pgsize)) {
23ba5a4219SLucas Stach 		pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%zx\n",
2450073cf9SLucas Stach 		       iova, size, pgsize);
2550073cf9SLucas Stach 		return;
2650073cf9SLucas Stach 	}
2750073cf9SLucas Stach 
2850073cf9SLucas Stach 	while (unmapped < size) {
2927b67278SLucas Stach 		unmapped_page = context->global->ops->unmap(context, iova,
3027b67278SLucas Stach 							    pgsize);
3150073cf9SLucas Stach 		if (!unmapped_page)
3250073cf9SLucas Stach 			break;
3350073cf9SLucas Stach 
3450073cf9SLucas Stach 		iova += unmapped_page;
3550073cf9SLucas Stach 		unmapped += unmapped_page;
3650073cf9SLucas Stach 	}
3750073cf9SLucas Stach }
3850073cf9SLucas Stach 
etnaviv_context_map(struct etnaviv_iommu_context * context,unsigned long iova,phys_addr_t paddr,size_t size,int prot)3927b67278SLucas Stach static int etnaviv_context_map(struct etnaviv_iommu_context *context,
40b6709083SLucas Stach 			      unsigned long iova, phys_addr_t paddr,
41b6709083SLucas Stach 			      size_t size, int prot)
4250073cf9SLucas Stach {
4350073cf9SLucas Stach 	unsigned long orig_iova = iova;
4450073cf9SLucas Stach 	size_t pgsize = SZ_4K;
4550073cf9SLucas Stach 	size_t orig_size = size;
4650073cf9SLucas Stach 	int ret = 0;
4750073cf9SLucas Stach 
4850073cf9SLucas Stach 	if (!IS_ALIGNED(iova | paddr | size, pgsize)) {
49ba5a4219SLucas Stach 		pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%zx\n",
5050073cf9SLucas Stach 		       iova, &paddr, size, pgsize);
5150073cf9SLucas Stach 		return -EINVAL;
5250073cf9SLucas Stach 	}
5350073cf9SLucas Stach 
5450073cf9SLucas Stach 	while (size) {
5527b67278SLucas Stach 		ret = context->global->ops->map(context, iova, paddr, pgsize,
5627b67278SLucas Stach 						prot);
5750073cf9SLucas Stach 		if (ret)
5850073cf9SLucas Stach 			break;
5950073cf9SLucas Stach 
6050073cf9SLucas Stach 		iova += pgsize;
6150073cf9SLucas Stach 		paddr += pgsize;
6250073cf9SLucas Stach 		size -= pgsize;
6350073cf9SLucas Stach 	}
6450073cf9SLucas Stach 
6550073cf9SLucas Stach 	/* unroll mapping in case something went wrong */
6650073cf9SLucas Stach 	if (ret)
6727b67278SLucas Stach 		etnaviv_context_unmap(context, orig_iova, orig_size - size);
6850073cf9SLucas Stach 
6950073cf9SLucas Stach 	return ret;
7050073cf9SLucas Stach }
7150073cf9SLucas Stach 
etnaviv_iommu_map(struct etnaviv_iommu_context * context,u32 iova,struct sg_table * sgt,unsigned len,int prot)7227b67278SLucas Stach static int etnaviv_iommu_map(struct etnaviv_iommu_context *context, u32 iova,
73a8c21a54SThe etnaviv authors 			     struct sg_table *sgt, unsigned len, int prot)
7427b67278SLucas Stach {	struct scatterlist *sg;
75a8c21a54SThe etnaviv authors 	unsigned int da = iova;
76182354a5SMarek Szyprowski 	unsigned int i;
77a8c21a54SThe etnaviv authors 	int ret;
78a8c21a54SThe etnaviv authors 
7927b67278SLucas Stach 	if (!context || !sgt)
80a8c21a54SThe etnaviv authors 		return -EINVAL;
81a8c21a54SThe etnaviv authors 
82182354a5SMarek Szyprowski 	for_each_sgtable_dma_sg(sgt, sg, i) {
83d37c120bSLucas Stach 		phys_addr_t pa = sg_dma_address(sg) - sg->offset;
84a8c21a54SThe etnaviv authors 		size_t bytes = sg_dma_len(sg) + sg->offset;
85a8c21a54SThe etnaviv authors 
86d37c120bSLucas Stach 		VERB("map[%d]: %08x %pap(%zx)", i, iova, &pa, bytes);
87a8c21a54SThe etnaviv authors 
8827b67278SLucas Stach 		ret = etnaviv_context_map(context, da, pa, bytes, prot);
89a8c21a54SThe etnaviv authors 		if (ret)
90a8c21a54SThe etnaviv authors 			goto fail;
91a8c21a54SThe etnaviv authors 
92a8c21a54SThe etnaviv authors 		da += bytes;
93a8c21a54SThe etnaviv authors 	}
94a8c21a54SThe etnaviv authors 
959247fccaSLucas Stach 	context->flush_seq++;
969247fccaSLucas Stach 
97a8c21a54SThe etnaviv authors 	return 0;
98a8c21a54SThe etnaviv authors 
99a8c21a54SThe etnaviv authors fail:
100182354a5SMarek Szyprowski 	etnaviv_context_unmap(context, iova, da - iova);
101a8c21a54SThe etnaviv authors 	return ret;
102a8c21a54SThe etnaviv authors }
103a8c21a54SThe etnaviv authors 
etnaviv_iommu_unmap(struct etnaviv_iommu_context * context,u32 iova,struct sg_table * sgt,unsigned len)10427b67278SLucas Stach static void etnaviv_iommu_unmap(struct etnaviv_iommu_context *context, u32 iova,
105a8c21a54SThe etnaviv authors 				struct sg_table *sgt, unsigned len)
106a8c21a54SThe etnaviv authors {
107a8c21a54SThe etnaviv authors 	struct scatterlist *sg;
108a8c21a54SThe etnaviv authors 	unsigned int da = iova;
109a8c21a54SThe etnaviv authors 	int i;
110a8c21a54SThe etnaviv authors 
111182354a5SMarek Szyprowski 	for_each_sgtable_dma_sg(sgt, sg, i) {
112a8c21a54SThe etnaviv authors 		size_t bytes = sg_dma_len(sg) + sg->offset;
113a8c21a54SThe etnaviv authors 
11427b67278SLucas Stach 		etnaviv_context_unmap(context, da, bytes);
115a8c21a54SThe etnaviv authors 
116a8c21a54SThe etnaviv authors 		VERB("unmap[%d]: %08x(%zx)", i, iova, bytes);
117a8c21a54SThe etnaviv authors 
118a8c21a54SThe etnaviv authors 		BUG_ON(!PAGE_ALIGNED(bytes));
119a8c21a54SThe etnaviv authors 
120a8c21a54SThe etnaviv authors 		da += bytes;
121a8c21a54SThe etnaviv authors 	}
1229247fccaSLucas Stach 
1239247fccaSLucas Stach 	context->flush_seq++;
124a8c21a54SThe etnaviv authors }
125a8c21a54SThe etnaviv authors 
etnaviv_iommu_remove_mapping(struct etnaviv_iommu_context * context,struct etnaviv_vram_mapping * mapping)12627b67278SLucas Stach static void etnaviv_iommu_remove_mapping(struct etnaviv_iommu_context *context,
127a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping)
128a8c21a54SThe etnaviv authors {
129a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = mapping->object;
130a8c21a54SThe etnaviv authors 
1314612bad5SGuido Günther 	lockdep_assert_held(&context->lock);
1324612bad5SGuido Günther 
13327b67278SLucas Stach 	etnaviv_iommu_unmap(context, mapping->vram_node.start,
134a8c21a54SThe etnaviv authors 			    etnaviv_obj->sgt, etnaviv_obj->base.size);
135a8c21a54SThe etnaviv authors 	drm_mm_remove_node(&mapping->vram_node);
136a8c21a54SThe etnaviv authors }
137a8c21a54SThe etnaviv authors 
etnaviv_iommu_reap_mapping(struct etnaviv_vram_mapping * mapping)1385a40837dSLucas Stach void etnaviv_iommu_reap_mapping(struct etnaviv_vram_mapping *mapping)
1395a40837dSLucas Stach {
1405a40837dSLucas Stach 	struct etnaviv_iommu_context *context = mapping->context;
1415a40837dSLucas Stach 
1425a40837dSLucas Stach 	lockdep_assert_held(&context->lock);
1435a40837dSLucas Stach 	WARN_ON(mapping->use);
1445a40837dSLucas Stach 
1455a40837dSLucas Stach 	etnaviv_iommu_remove_mapping(context, mapping);
1465a40837dSLucas Stach 	etnaviv_iommu_context_put(mapping->context);
1475a40837dSLucas Stach 	mapping->context = NULL;
1485a40837dSLucas Stach 	list_del_init(&mapping->mmu_node);
1495a40837dSLucas Stach }
1505a40837dSLucas Stach 
etnaviv_iommu_find_iova(struct etnaviv_iommu_context * context,struct drm_mm_node * node,size_t size)15127b67278SLucas Stach static int etnaviv_iommu_find_iova(struct etnaviv_iommu_context *context,
15290969c9aSLucas Stach 				   struct drm_mm_node *node, size_t size)
153a8c21a54SThe etnaviv authors {
154a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *free = NULL;
1554e64e553SChris Wilson 	enum drm_mm_insert_mode mode = DRM_MM_INSERT_LOW;
156a8c21a54SThe etnaviv authors 	int ret;
157a8c21a54SThe etnaviv authors 
15827b67278SLucas Stach 	lockdep_assert_held(&context->lock);
159a8c21a54SThe etnaviv authors 
160a8c21a54SThe etnaviv authors 	while (1) {
161a8c21a54SThe etnaviv authors 		struct etnaviv_vram_mapping *m, *n;
1629a71e277SChris Wilson 		struct drm_mm_scan scan;
163a8c21a54SThe etnaviv authors 		struct list_head list;
164a8c21a54SThe etnaviv authors 		bool found;
165a8c21a54SThe etnaviv authors 
16627b67278SLucas Stach 		ret = drm_mm_insert_node_in_range(&context->mm, node,
167ccae4592SLucas Stach 						  size, 0, 0, 0, U64_MAX, mode);
168a8c21a54SThe etnaviv authors 		if (ret != -ENOSPC)
169a8c21a54SThe etnaviv authors 			break;
170a8c21a54SThe etnaviv authors 
171a8c21a54SThe etnaviv authors 		/* Try to retire some entries */
17227b67278SLucas Stach 		drm_mm_scan_init(&scan, &context->mm, size, 0, 0, mode);
173a8c21a54SThe etnaviv authors 
174a8c21a54SThe etnaviv authors 		found = 0;
175a8c21a54SThe etnaviv authors 		INIT_LIST_HEAD(&list);
17627b67278SLucas Stach 		list_for_each_entry(free, &context->mappings, mmu_node) {
177a8c21a54SThe etnaviv authors 			/* If this vram node has not been used, skip this. */
178a8c21a54SThe etnaviv authors 			if (!free->vram_node.mm)
179a8c21a54SThe etnaviv authors 				continue;
180a8c21a54SThe etnaviv authors 
181a8c21a54SThe etnaviv authors 			/*
182a8c21a54SThe etnaviv authors 			 * If the iova is pinned, then it's in-use,
183a8c21a54SThe etnaviv authors 			 * so we must keep its mapping.
184a8c21a54SThe etnaviv authors 			 */
185a8c21a54SThe etnaviv authors 			if (free->use)
186a8c21a54SThe etnaviv authors 				continue;
187a8c21a54SThe etnaviv authors 
188a8c21a54SThe etnaviv authors 			list_add(&free->scan_node, &list);
1899a71e277SChris Wilson 			if (drm_mm_scan_add_block(&scan, &free->vram_node)) {
190a8c21a54SThe etnaviv authors 				found = true;
191a8c21a54SThe etnaviv authors 				break;
192a8c21a54SThe etnaviv authors 			}
193a8c21a54SThe etnaviv authors 		}
194a8c21a54SThe etnaviv authors 
195a8c21a54SThe etnaviv authors 		if (!found) {
196a8c21a54SThe etnaviv authors 			/* Nothing found, clean up and fail */
197a8c21a54SThe etnaviv authors 			list_for_each_entry_safe(m, n, &list, scan_node)
1989a71e277SChris Wilson 				BUG_ON(drm_mm_scan_remove_block(&scan, &m->vram_node));
199a8c21a54SThe etnaviv authors 			break;
200a8c21a54SThe etnaviv authors 		}
201a8c21a54SThe etnaviv authors 
202a8c21a54SThe etnaviv authors 		/*
203a8c21a54SThe etnaviv authors 		 * drm_mm does not allow any other operations while
204a8c21a54SThe etnaviv authors 		 * scanning, so we have to remove all blocks first.
205a8c21a54SThe etnaviv authors 		 * If drm_mm_scan_remove_block() returns false, we
206a8c21a54SThe etnaviv authors 		 * can leave the block pinned.
207a8c21a54SThe etnaviv authors 		 */
208a8c21a54SThe etnaviv authors 		list_for_each_entry_safe(m, n, &list, scan_node)
2099a71e277SChris Wilson 			if (!drm_mm_scan_remove_block(&scan, &m->vram_node))
210a8c21a54SThe etnaviv authors 				list_del_init(&m->scan_node);
211a8c21a54SThe etnaviv authors 
212a8c21a54SThe etnaviv authors 		/*
213a8c21a54SThe etnaviv authors 		 * Unmap the blocks which need to be reaped from the MMU.
214b6325f40SRussell King 		 * Clear the mmu pointer to prevent the mapping_get finding
215a8c21a54SThe etnaviv authors 		 * this mapping.
216a8c21a54SThe etnaviv authors 		 */
217a8c21a54SThe etnaviv authors 		list_for_each_entry_safe(m, n, &list, scan_node) {
2185a40837dSLucas Stach 			etnaviv_iommu_reap_mapping(m);
219a8c21a54SThe etnaviv authors 			list_del_init(&m->scan_node);
220a8c21a54SThe etnaviv authors 		}
221a8c21a54SThe etnaviv authors 
2224e64e553SChris Wilson 		mode = DRM_MM_INSERT_EVICT;
2234e64e553SChris Wilson 
224a8c21a54SThe etnaviv authors 		/*
225a8c21a54SThe etnaviv authors 		 * We removed enough mappings so that the new allocation will
226d4645073SLucas Stach 		 * succeed, retry the allocation one more time.
227a8c21a54SThe etnaviv authors 		 */
228a8c21a54SThe etnaviv authors 	}
229a8c21a54SThe etnaviv authors 
23090969c9aSLucas Stach 	return ret;
23190969c9aSLucas Stach }
23290969c9aSLucas Stach 
etnaviv_iommu_insert_exact(struct etnaviv_iommu_context * context,struct drm_mm_node * node,size_t size,u64 va)23317eae23bSLucas Stach static int etnaviv_iommu_insert_exact(struct etnaviv_iommu_context *context,
23417eae23bSLucas Stach 		   struct drm_mm_node *node, size_t size, u64 va)
23517eae23bSLucas Stach {
2362829a9fcSLucas Stach 	struct etnaviv_vram_mapping *m, *n;
2372829a9fcSLucas Stach 	struct drm_mm_node *scan_node;
2382829a9fcSLucas Stach 	LIST_HEAD(scan_list);
2392829a9fcSLucas Stach 	int ret;
2402829a9fcSLucas Stach 
2414612bad5SGuido Günther 	lockdep_assert_held(&context->lock);
2424612bad5SGuido Günther 
2432829a9fcSLucas Stach 	ret = drm_mm_insert_node_in_range(&context->mm, node, size, 0, 0, va,
2442829a9fcSLucas Stach 					  va + size, DRM_MM_INSERT_LOWEST);
2452829a9fcSLucas Stach 	if (ret != -ENOSPC)
2462829a9fcSLucas Stach 		return ret;
2472829a9fcSLucas Stach 
2482829a9fcSLucas Stach 	/*
2492829a9fcSLucas Stach 	 * When we can't insert the node, due to a existing mapping blocking
2502829a9fcSLucas Stach 	 * the address space, there are two possible reasons:
2512829a9fcSLucas Stach 	 * 1. Userspace genuinely messed up and tried to reuse address space
2522829a9fcSLucas Stach 	 * before the last job using this VMA has finished executing.
2532829a9fcSLucas Stach 	 * 2. The existing buffer mappings are idle, but the buffers are not
2542829a9fcSLucas Stach 	 * destroyed yet (likely due to being referenced by another context) in
2552829a9fcSLucas Stach 	 * which case the mappings will not be cleaned up and we must reap them
2562829a9fcSLucas Stach 	 * here to make space for the new mapping.
2572829a9fcSLucas Stach 	 */
2582829a9fcSLucas Stach 
2592829a9fcSLucas Stach 	drm_mm_for_each_node_in_range(scan_node, &context->mm, va, va + size) {
2602829a9fcSLucas Stach 		m = container_of(scan_node, struct etnaviv_vram_mapping,
2612829a9fcSLucas Stach 				 vram_node);
2622829a9fcSLucas Stach 
2632829a9fcSLucas Stach 		if (m->use)
2642829a9fcSLucas Stach 			return -ENOSPC;
2652829a9fcSLucas Stach 
2662829a9fcSLucas Stach 		list_add(&m->scan_node, &scan_list);
2672829a9fcSLucas Stach 	}
2682829a9fcSLucas Stach 
2692829a9fcSLucas Stach 	list_for_each_entry_safe(m, n, &scan_list, scan_node) {
2705a40837dSLucas Stach 		etnaviv_iommu_reap_mapping(m);
2712829a9fcSLucas Stach 		list_del_init(&m->scan_node);
2722829a9fcSLucas Stach 	}
2732829a9fcSLucas Stach 
27417eae23bSLucas Stach 	return drm_mm_insert_node_in_range(&context->mm, node, size, 0, 0, va,
27517eae23bSLucas Stach 					   va + size, DRM_MM_INSERT_LOWEST);
27617eae23bSLucas Stach }
27717eae23bSLucas Stach 
etnaviv_iommu_map_gem(struct etnaviv_iommu_context * context,struct etnaviv_gem_object * etnaviv_obj,u32 memory_base,struct etnaviv_vram_mapping * mapping,u64 va)27827b67278SLucas Stach int etnaviv_iommu_map_gem(struct etnaviv_iommu_context *context,
27990969c9aSLucas Stach 	struct etnaviv_gem_object *etnaviv_obj, u32 memory_base,
28017eae23bSLucas Stach 	struct etnaviv_vram_mapping *mapping, u64 va)
28190969c9aSLucas Stach {
28290969c9aSLucas Stach 	struct sg_table *sgt = etnaviv_obj->sgt;
28390969c9aSLucas Stach 	struct drm_mm_node *node;
28490969c9aSLucas Stach 	int ret;
28590969c9aSLucas Stach 
28690969c9aSLucas Stach 	lockdep_assert_held(&etnaviv_obj->lock);
28790969c9aSLucas Stach 
28827b67278SLucas Stach 	mutex_lock(&context->lock);
28990969c9aSLucas Stach 
29090969c9aSLucas Stach 	/* v1 MMU can optimize single entry (contiguous) scatterlists */
29127b67278SLucas Stach 	if (context->global->version == ETNAVIV_IOMMU_V1 &&
29290969c9aSLucas Stach 	    sgt->nents == 1 && !(etnaviv_obj->flags & ETNA_BO_FORCE_MMU)) {
29390969c9aSLucas Stach 		u32 iova;
29490969c9aSLucas Stach 
29590969c9aSLucas Stach 		iova = sg_dma_address(sgt->sgl) - memory_base;
29690969c9aSLucas Stach 		if (iova < 0x80000000 - sg_dma_len(sgt->sgl)) {
29790969c9aSLucas Stach 			mapping->iova = iova;
29811ad6a1fSLucas Stach 			mapping->context = etnaviv_iommu_context_get(context);
29927b67278SLucas Stach 			list_add_tail(&mapping->mmu_node, &context->mappings);
300ff981595SMarkus Elfring 			ret = 0;
301ff981595SMarkus Elfring 			goto unlock;
30290969c9aSLucas Stach 		}
30390969c9aSLucas Stach 	}
30490969c9aSLucas Stach 
30590969c9aSLucas Stach 	node = &mapping->vram_node;
30690969c9aSLucas Stach 
30717eae23bSLucas Stach 	if (va)
30817eae23bSLucas Stach 		ret = etnaviv_iommu_insert_exact(context, node,
30917eae23bSLucas Stach 						 etnaviv_obj->base.size, va);
31017eae23bSLucas Stach 	else
31117eae23bSLucas Stach 		ret = etnaviv_iommu_find_iova(context, node,
31217eae23bSLucas Stach 					      etnaviv_obj->base.size);
313ff981595SMarkus Elfring 	if (ret < 0)
314ff981595SMarkus Elfring 		goto unlock;
315a8c21a54SThe etnaviv authors 
316a8c21a54SThe etnaviv authors 	mapping->iova = node->start;
31727b67278SLucas Stach 	ret = etnaviv_iommu_map(context, node->start, sgt, etnaviv_obj->base.size,
318b6709083SLucas Stach 				ETNAVIV_PROT_READ | ETNAVIV_PROT_WRITE);
319a8c21a54SThe etnaviv authors 
320a8c21a54SThe etnaviv authors 	if (ret < 0) {
321a8c21a54SThe etnaviv authors 		drm_mm_remove_node(node);
322ff981595SMarkus Elfring 		goto unlock;
323a8c21a54SThe etnaviv authors 	}
324a8c21a54SThe etnaviv authors 
32511ad6a1fSLucas Stach 	mapping->context = etnaviv_iommu_context_get(context);
32627b67278SLucas Stach 	list_add_tail(&mapping->mmu_node, &context->mappings);
327ff981595SMarkus Elfring unlock:
32827b67278SLucas Stach 	mutex_unlock(&context->lock);
329a8c21a54SThe etnaviv authors 
330a8c21a54SThe etnaviv authors 	return ret;
331a8c21a54SThe etnaviv authors }
332a8c21a54SThe etnaviv authors 
etnaviv_iommu_unmap_gem(struct etnaviv_iommu_context * context,struct etnaviv_vram_mapping * mapping)33327b67278SLucas Stach void etnaviv_iommu_unmap_gem(struct etnaviv_iommu_context *context,
334a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping)
335a8c21a54SThe etnaviv authors {
336a8c21a54SThe etnaviv authors 	WARN_ON(mapping->use);
337a8c21a54SThe etnaviv authors 
33827b67278SLucas Stach 	mutex_lock(&context->lock);
339a8c21a54SThe etnaviv authors 
340e168c255SLucas Stach 	/* Bail if the mapping has been reaped by another thread */
341e168c255SLucas Stach 	if (!mapping->context) {
342e168c255SLucas Stach 		mutex_unlock(&context->lock);
343e168c255SLucas Stach 		return;
344e168c255SLucas Stach 	}
345e168c255SLucas Stach 
346a8c21a54SThe etnaviv authors 	/* If the vram node is on the mm, unmap and remove the node */
34727b67278SLucas Stach 	if (mapping->vram_node.mm == &context->mm)
34827b67278SLucas Stach 		etnaviv_iommu_remove_mapping(context, mapping);
349a8c21a54SThe etnaviv authors 
350a8c21a54SThe etnaviv authors 	list_del(&mapping->mmu_node);
35127b67278SLucas Stach 	mutex_unlock(&context->lock);
35211ad6a1fSLucas Stach 	etnaviv_iommu_context_put(context);
353a8c21a54SThe etnaviv authors }
354a8c21a54SThe etnaviv authors 
etnaviv_iommu_context_free(struct kref * kref)35527b67278SLucas Stach static void etnaviv_iommu_context_free(struct kref *kref)
356a8c21a54SThe etnaviv authors {
35727b67278SLucas Stach 	struct etnaviv_iommu_context *context =
35827b67278SLucas Stach 		container_of(kref, struct etnaviv_iommu_context, refcount);
359a8c21a54SThe etnaviv authors 
36017e4660aSLucas Stach 	etnaviv_cmdbuf_suballoc_unmap(context, &context->cmdbuf_mapping);
36117e4660aSLucas Stach 
36227b67278SLucas Stach 	context->global->ops->free(context);
36327b67278SLucas Stach }
etnaviv_iommu_context_put(struct etnaviv_iommu_context * context)36427b67278SLucas Stach void etnaviv_iommu_context_put(struct etnaviv_iommu_context *context)
365a8c21a54SThe etnaviv authors {
36627b67278SLucas Stach 	kref_put(&context->refcount, etnaviv_iommu_context_free);
367dd34bb96SLucas Stach }
368dd34bb96SLucas Stach 
36927b67278SLucas Stach struct etnaviv_iommu_context *
etnaviv_iommu_context_init(struct etnaviv_iommu_global * global,struct etnaviv_cmdbuf_suballoc * suballoc)37017e4660aSLucas Stach etnaviv_iommu_context_init(struct etnaviv_iommu_global *global,
37117e4660aSLucas Stach 			   struct etnaviv_cmdbuf_suballoc *suballoc)
372e095c8feSLucas Stach {
37317e4660aSLucas Stach 	struct etnaviv_iommu_context *ctx;
37417e4660aSLucas Stach 	int ret;
37517e4660aSLucas Stach 
37627b67278SLucas Stach 	if (global->version == ETNAVIV_IOMMU_V1)
37717e4660aSLucas Stach 		ctx = etnaviv_iommuv1_context_alloc(global);
378e095c8feSLucas Stach 	else
37917e4660aSLucas Stach 		ctx = etnaviv_iommuv2_context_alloc(global);
38017e4660aSLucas Stach 
38117e4660aSLucas Stach 	if (!ctx)
38217e4660aSLucas Stach 		return NULL;
38317e4660aSLucas Stach 
38417e4660aSLucas Stach 	ret = etnaviv_cmdbuf_suballoc_map(suballoc, ctx, &ctx->cmdbuf_mapping,
38517e4660aSLucas Stach 					  global->memory_base);
38618fa692dSLucas Stach 	if (ret)
38718fa692dSLucas Stach 		goto out_free;
38818fa692dSLucas Stach 
38918fa692dSLucas Stach 	if (global->version == ETNAVIV_IOMMU_V1 &&
39018fa692dSLucas Stach 	    ctx->cmdbuf_mapping.iova > 0x80000000) {
39118fa692dSLucas Stach 		dev_err(global->dev,
39218fa692dSLucas Stach 		        "command buffer outside valid memory window\n");
39318fa692dSLucas Stach 		goto out_unmap;
39417e4660aSLucas Stach 	}
39517e4660aSLucas Stach 
39617e4660aSLucas Stach 	return ctx;
39718fa692dSLucas Stach 
39818fa692dSLucas Stach out_unmap:
39918fa692dSLucas Stach 	etnaviv_cmdbuf_suballoc_unmap(ctx, &ctx->cmdbuf_mapping);
40018fa692dSLucas Stach out_free:
40118fa692dSLucas Stach 	global->ops->free(ctx);
40218fa692dSLucas Stach 	return NULL;
403e095c8feSLucas Stach }
404e095c8feSLucas Stach 
etnaviv_iommu_restore(struct etnaviv_gpu * gpu,struct etnaviv_iommu_context * context)40527b67278SLucas Stach void etnaviv_iommu_restore(struct etnaviv_gpu *gpu,
40627b67278SLucas Stach 			   struct etnaviv_iommu_context *context)
40727b67278SLucas Stach {
40827b67278SLucas Stach 	context->global->ops->restore(gpu, context);
40927b67278SLucas Stach }
41027b67278SLucas Stach 
etnaviv_iommu_get_suballoc_va(struct etnaviv_iommu_context * context,struct etnaviv_vram_mapping * mapping,u32 memory_base,dma_addr_t paddr,size_t size)41127b67278SLucas Stach int etnaviv_iommu_get_suballoc_va(struct etnaviv_iommu_context *context,
412db82a043SLucas Stach 				  struct etnaviv_vram_mapping *mapping,
413db82a043SLucas Stach 				  u32 memory_base, dma_addr_t paddr,
414db82a043SLucas Stach 				  size_t size)
415e07c0db5SLucas Stach {
41627b67278SLucas Stach 	mutex_lock(&context->lock);
417e68f270fSLucas Stach 
41817e4660aSLucas Stach 	if (mapping->use > 0) {
41917e4660aSLucas Stach 		mapping->use++;
42017e4660aSLucas Stach 		mutex_unlock(&context->lock);
42117e4660aSLucas Stach 		return 0;
42217e4660aSLucas Stach 	}
42317e4660aSLucas Stach 
424db82a043SLucas Stach 	/*
425db82a043SLucas Stach 	 * For MMUv1 we don't add the suballoc region to the pagetables, as
426db82a043SLucas Stach 	 * those GPUs can only work with cmdbufs accessed through the linear
427db82a043SLucas Stach 	 * window. Instead we manufacture a mapping to make it look uniform
428db82a043SLucas Stach 	 * to the upper layers.
429db82a043SLucas Stach 	 */
43027b67278SLucas Stach 	if (context->global->version == ETNAVIV_IOMMU_V1) {
431db82a043SLucas Stach 		mapping->iova = paddr - memory_base;
432e68f270fSLucas Stach 	} else {
433db82a043SLucas Stach 		struct drm_mm_node *node = &mapping->vram_node;
434e68f270fSLucas Stach 		int ret;
435e68f270fSLucas Stach 
43627b67278SLucas Stach 		ret = etnaviv_iommu_find_iova(context, node, size);
437e68f270fSLucas Stach 		if (ret < 0) {
43827b67278SLucas Stach 			mutex_unlock(&context->lock);
439e66774ddSLucas Stach 			return ret;
440e68f270fSLucas Stach 		}
441db82a043SLucas Stach 
442db82a043SLucas Stach 		mapping->iova = node->start;
44327b67278SLucas Stach 		ret = etnaviv_context_map(context, node->start, paddr, size,
444db82a043SLucas Stach 					  ETNAVIV_PROT_READ);
445e68f270fSLucas Stach 		if (ret < 0) {
446db82a043SLucas Stach 			drm_mm_remove_node(node);
44727b67278SLucas Stach 			mutex_unlock(&context->lock);
448e66774ddSLucas Stach 			return ret;
449e68f270fSLucas Stach 		}
450db82a043SLucas Stach 
45127b67278SLucas Stach 		context->flush_seq++;
452db82a043SLucas Stach 	}
453db82a043SLucas Stach 
45427b67278SLucas Stach 	list_add_tail(&mapping->mmu_node, &context->mappings);
455db82a043SLucas Stach 	mapping->use = 1;
456db82a043SLucas Stach 
45727b67278SLucas Stach 	mutex_unlock(&context->lock);
458e68f270fSLucas Stach 
459e66774ddSLucas Stach 	return 0;
460e68f270fSLucas Stach }
461e07c0db5SLucas Stach 
etnaviv_iommu_put_suballoc_va(struct etnaviv_iommu_context * context,struct etnaviv_vram_mapping * mapping)46227b67278SLucas Stach void etnaviv_iommu_put_suballoc_va(struct etnaviv_iommu_context *context,
463db82a043SLucas Stach 		  struct etnaviv_vram_mapping *mapping)
464e68f270fSLucas Stach {
465db82a043SLucas Stach 	struct drm_mm_node *node = &mapping->vram_node;
466e68f270fSLucas Stach 
46727b67278SLucas Stach 	mutex_lock(&context->lock);
46817e4660aSLucas Stach 	mapping->use--;
46917e4660aSLucas Stach 
47017e4660aSLucas Stach 	if (mapping->use > 0 || context->global->version == ETNAVIV_IOMMU_V1) {
47117e4660aSLucas Stach 		mutex_unlock(&context->lock);
47217e4660aSLucas Stach 		return;
47317e4660aSLucas Stach 	}
47417e4660aSLucas Stach 
47527b67278SLucas Stach 	etnaviv_context_unmap(context, node->start, node->size);
476db82a043SLucas Stach 	drm_mm_remove_node(node);
47727b67278SLucas Stach 	mutex_unlock(&context->lock);
478e68f270fSLucas Stach }
479db82a043SLucas Stach 
etnaviv_iommu_dump_size(struct etnaviv_iommu_context * context)48027b67278SLucas Stach size_t etnaviv_iommu_dump_size(struct etnaviv_iommu_context *context)
481a8c21a54SThe etnaviv authors {
48227b67278SLucas Stach 	return context->global->ops->dump_size(context);
483a8c21a54SThe etnaviv authors }
484a8c21a54SThe etnaviv authors 
etnaviv_iommu_dump(struct etnaviv_iommu_context * context,void * buf)48527b67278SLucas Stach void etnaviv_iommu_dump(struct etnaviv_iommu_context *context, void *buf)
486a8c21a54SThe etnaviv authors {
48727b67278SLucas Stach 	context->global->ops->dump(context, buf);
48827b67278SLucas Stach }
48927b67278SLucas Stach 
etnaviv_iommu_global_init(struct etnaviv_gpu * gpu)49027b67278SLucas Stach int etnaviv_iommu_global_init(struct etnaviv_gpu *gpu)
49127b67278SLucas Stach {
49227b67278SLucas Stach 	enum etnaviv_iommu_version version = ETNAVIV_IOMMU_V1;
49327b67278SLucas Stach 	struct etnaviv_drm_private *priv = gpu->drm->dev_private;
49427b67278SLucas Stach 	struct etnaviv_iommu_global *global;
49527b67278SLucas Stach 	struct device *dev = gpu->drm->dev;
49627b67278SLucas Stach 
49727b67278SLucas Stach 	if (gpu->identity.minor_features1 & chipMinorFeatures1_MMU_VERSION)
49827b67278SLucas Stach 		version = ETNAVIV_IOMMU_V2;
49927b67278SLucas Stach 
50027b67278SLucas Stach 	if (priv->mmu_global) {
50127b67278SLucas Stach 		if (priv->mmu_global->version != version) {
50227b67278SLucas Stach 			dev_err(gpu->dev,
50327b67278SLucas Stach 				"MMU version doesn't match global version\n");
50427b67278SLucas Stach 			return -ENXIO;
50527b67278SLucas Stach 		}
50627b67278SLucas Stach 
50727b67278SLucas Stach 		priv->mmu_global->use++;
50827b67278SLucas Stach 		return 0;
50927b67278SLucas Stach 	}
51027b67278SLucas Stach 
51127b67278SLucas Stach 	global = kzalloc(sizeof(*global), GFP_KERNEL);
51227b67278SLucas Stach 	if (!global)
51327b67278SLucas Stach 		return -ENOMEM;
51427b67278SLucas Stach 
51527b67278SLucas Stach 	global->bad_page_cpu = dma_alloc_wc(dev, SZ_4K, &global->bad_page_dma,
51627b67278SLucas Stach 					    GFP_KERNEL);
51727b67278SLucas Stach 	if (!global->bad_page_cpu)
51827b67278SLucas Stach 		goto free_global;
51927b67278SLucas Stach 
52027b67278SLucas Stach 	memset32(global->bad_page_cpu, 0xdead55aa, SZ_4K / sizeof(u32));
52127b67278SLucas Stach 
52227b67278SLucas Stach 	if (version == ETNAVIV_IOMMU_V2) {
52327b67278SLucas Stach 		global->v2.pta_cpu = dma_alloc_wc(dev, ETNAVIV_PTA_SIZE,
52427b67278SLucas Stach 					       &global->v2.pta_dma, GFP_KERNEL);
52527b67278SLucas Stach 		if (!global->v2.pta_cpu)
52627b67278SLucas Stach 			goto free_bad_page;
52727b67278SLucas Stach 	}
52827b67278SLucas Stach 
52927b67278SLucas Stach 	global->dev = dev;
53027b67278SLucas Stach 	global->version = version;
53127b67278SLucas Stach 	global->use = 1;
53227b67278SLucas Stach 	mutex_init(&global->lock);
53327b67278SLucas Stach 
53427b67278SLucas Stach 	if (version == ETNAVIV_IOMMU_V1)
53527b67278SLucas Stach 		global->ops = &etnaviv_iommuv1_ops;
53627b67278SLucas Stach 	else
53727b67278SLucas Stach 		global->ops = &etnaviv_iommuv2_ops;
53827b67278SLucas Stach 
53927b67278SLucas Stach 	priv->mmu_global = global;
54027b67278SLucas Stach 
54127b67278SLucas Stach 	return 0;
54227b67278SLucas Stach 
54327b67278SLucas Stach free_bad_page:
54427b67278SLucas Stach 	dma_free_wc(dev, SZ_4K, global->bad_page_cpu, global->bad_page_dma);
54527b67278SLucas Stach free_global:
54627b67278SLucas Stach 	kfree(global);
54727b67278SLucas Stach 
54827b67278SLucas Stach 	return -ENOMEM;
54927b67278SLucas Stach }
55027b67278SLucas Stach 
etnaviv_iommu_global_fini(struct etnaviv_gpu * gpu)55127b67278SLucas Stach void etnaviv_iommu_global_fini(struct etnaviv_gpu *gpu)
55227b67278SLucas Stach {
55327b67278SLucas Stach 	struct etnaviv_drm_private *priv = gpu->drm->dev_private;
55427b67278SLucas Stach 	struct etnaviv_iommu_global *global = priv->mmu_global;
55527b67278SLucas Stach 
556*e116be25SLucas Stach 	if (!global)
557*e116be25SLucas Stach 		return;
558*e116be25SLucas Stach 
55927b67278SLucas Stach 	if (--global->use > 0)
56027b67278SLucas Stach 		return;
56127b67278SLucas Stach 
56227b67278SLucas Stach 	if (global->v2.pta_cpu)
56327b67278SLucas Stach 		dma_free_wc(global->dev, ETNAVIV_PTA_SIZE,
56427b67278SLucas Stach 			    global->v2.pta_cpu, global->v2.pta_dma);
56527b67278SLucas Stach 
56627b67278SLucas Stach 	if (global->bad_page_cpu)
56727b67278SLucas Stach 		dma_free_wc(global->dev, SZ_4K,
56827b67278SLucas Stach 			    global->bad_page_cpu, global->bad_page_dma);
56927b67278SLucas Stach 
57027b67278SLucas Stach 	mutex_destroy(&global->lock);
57127b67278SLucas Stach 	kfree(global);
57227b67278SLucas Stach 
57327b67278SLucas Stach 	priv->mmu_global = NULL;
574a8c21a54SThe etnaviv authors }
575