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 
66eae41feSSam Ravnborg #include <drm/drm_prime.h>
76eae41feSSam Ravnborg #include <linux/dma-mapping.h>
8a8c21a54SThe etnaviv authors #include <linux/shmem_fs.h>
96eae41feSSam Ravnborg #include <linux/spinlock.h>
106eae41feSSam Ravnborg #include <linux/vmalloc.h>
11a8c21a54SThe etnaviv authors 
12a8c21a54SThe etnaviv authors #include "etnaviv_drv.h"
13a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
14a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
15a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
16a8c21a54SThe etnaviv authors 
17d6a8743dSLucas Stach static struct lock_class_key etnaviv_shm_lock_class;
18d6a8743dSLucas Stach static struct lock_class_key etnaviv_userptr_lock_class;
19d6a8743dSLucas Stach 
20a8c21a54SThe etnaviv authors static void etnaviv_gem_scatter_map(struct etnaviv_gem_object *etnaviv_obj)
21a8c21a54SThe etnaviv authors {
22a8c21a54SThe etnaviv authors 	struct drm_device *dev = etnaviv_obj->base.dev;
23a8c21a54SThe etnaviv authors 	struct sg_table *sgt = etnaviv_obj->sgt;
24a8c21a54SThe etnaviv authors 
25a8c21a54SThe etnaviv authors 	/*
26a8c21a54SThe etnaviv authors 	 * For non-cached buffers, ensure the new pages are clean
27a8c21a54SThe etnaviv authors 	 * because display controller, GPU, etc. are not coherent.
28a8c21a54SThe etnaviv authors 	 */
29a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK)
30182354a5SMarek Szyprowski 		dma_map_sgtable(dev->dev, sgt, DMA_BIDIRECTIONAL, 0);
31a8c21a54SThe etnaviv authors }
32a8c21a54SThe etnaviv authors 
33a8c21a54SThe etnaviv authors static void etnaviv_gem_scatterlist_unmap(struct etnaviv_gem_object *etnaviv_obj)
34a8c21a54SThe etnaviv authors {
35a8c21a54SThe etnaviv authors 	struct drm_device *dev = etnaviv_obj->base.dev;
36a8c21a54SThe etnaviv authors 	struct sg_table *sgt = etnaviv_obj->sgt;
37a8c21a54SThe etnaviv authors 
38a8c21a54SThe etnaviv authors 	/*
39a8c21a54SThe etnaviv authors 	 * For non-cached buffers, ensure the new pages are clean
40a8c21a54SThe etnaviv authors 	 * because display controller, GPU, etc. are not coherent:
41a8c21a54SThe etnaviv authors 	 *
42a8c21a54SThe etnaviv authors 	 * WARNING: The DMA API does not support concurrent CPU
43a8c21a54SThe etnaviv authors 	 * and device access to the memory area.  With BIDIRECTIONAL,
44a8c21a54SThe etnaviv authors 	 * we will clean the cache lines which overlap the region,
45a8c21a54SThe etnaviv authors 	 * and invalidate all cache lines (partially) contained in
46a8c21a54SThe etnaviv authors 	 * the region.
47a8c21a54SThe etnaviv authors 	 *
48a8c21a54SThe etnaviv authors 	 * If you have dirty data in the overlapping cache lines,
49a8c21a54SThe etnaviv authors 	 * that will corrupt the GPU-written data.  If you have
50a8c21a54SThe etnaviv authors 	 * written into the remainder of the region, this can
51a8c21a54SThe etnaviv authors 	 * discard those writes.
52a8c21a54SThe etnaviv authors 	 */
53a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK)
54182354a5SMarek Szyprowski 		dma_unmap_sgtable(dev->dev, sgt, DMA_BIDIRECTIONAL, 0);
55a8c21a54SThe etnaviv authors }
56a8c21a54SThe etnaviv authors 
57a8c21a54SThe etnaviv authors /* called with etnaviv_obj->lock held */
58a8c21a54SThe etnaviv authors static int etnaviv_gem_shmem_get_pages(struct etnaviv_gem_object *etnaviv_obj)
59a8c21a54SThe etnaviv authors {
60a8c21a54SThe etnaviv authors 	struct drm_device *dev = etnaviv_obj->base.dev;
61a8c21a54SThe etnaviv authors 	struct page **p = drm_gem_get_pages(&etnaviv_obj->base);
62a8c21a54SThe etnaviv authors 
63a8c21a54SThe etnaviv authors 	if (IS_ERR(p)) {
64f91ac470SLucas Stach 		dev_dbg(dev->dev, "could not get pages: %ld\n", PTR_ERR(p));
65a8c21a54SThe etnaviv authors 		return PTR_ERR(p);
66a8c21a54SThe etnaviv authors 	}
67a8c21a54SThe etnaviv authors 
68a8c21a54SThe etnaviv authors 	etnaviv_obj->pages = p;
69a8c21a54SThe etnaviv authors 
70a8c21a54SThe etnaviv authors 	return 0;
71a8c21a54SThe etnaviv authors }
72a8c21a54SThe etnaviv authors 
73a8c21a54SThe etnaviv authors static void put_pages(struct etnaviv_gem_object *etnaviv_obj)
74a8c21a54SThe etnaviv authors {
75a8c21a54SThe etnaviv authors 	if (etnaviv_obj->sgt) {
76a8c21a54SThe etnaviv authors 		etnaviv_gem_scatterlist_unmap(etnaviv_obj);
77a8c21a54SThe etnaviv authors 		sg_free_table(etnaviv_obj->sgt);
78a8c21a54SThe etnaviv authors 		kfree(etnaviv_obj->sgt);
79a8c21a54SThe etnaviv authors 		etnaviv_obj->sgt = NULL;
80a8c21a54SThe etnaviv authors 	}
81a8c21a54SThe etnaviv authors 	if (etnaviv_obj->pages) {
82a8c21a54SThe etnaviv authors 		drm_gem_put_pages(&etnaviv_obj->base, etnaviv_obj->pages,
83a8c21a54SThe etnaviv authors 				  true, false);
84a8c21a54SThe etnaviv authors 
85a8c21a54SThe etnaviv authors 		etnaviv_obj->pages = NULL;
86a8c21a54SThe etnaviv authors 	}
87a8c21a54SThe etnaviv authors }
88a8c21a54SThe etnaviv authors 
89a8c21a54SThe etnaviv authors struct page **etnaviv_gem_get_pages(struct etnaviv_gem_object *etnaviv_obj)
90a8c21a54SThe etnaviv authors {
91a8c21a54SThe etnaviv authors 	int ret;
92a8c21a54SThe etnaviv authors 
93a8c21a54SThe etnaviv authors 	lockdep_assert_held(&etnaviv_obj->lock);
94a8c21a54SThe etnaviv authors 
95a8c21a54SThe etnaviv authors 	if (!etnaviv_obj->pages) {
96a8c21a54SThe etnaviv authors 		ret = etnaviv_obj->ops->get_pages(etnaviv_obj);
97a8c21a54SThe etnaviv authors 		if (ret < 0)
98a8c21a54SThe etnaviv authors 			return ERR_PTR(ret);
99a8c21a54SThe etnaviv authors 	}
100a8c21a54SThe etnaviv authors 
101a8c21a54SThe etnaviv authors 	if (!etnaviv_obj->sgt) {
102a8c21a54SThe etnaviv authors 		struct drm_device *dev = etnaviv_obj->base.dev;
103a8c21a54SThe etnaviv authors 		int npages = etnaviv_obj->base.size >> PAGE_SHIFT;
104a8c21a54SThe etnaviv authors 		struct sg_table *sgt;
105a8c21a54SThe etnaviv authors 
106707d561fSGerd Hoffmann 		sgt = drm_prime_pages_to_sg(etnaviv_obj->base.dev,
107707d561fSGerd Hoffmann 					    etnaviv_obj->pages, npages);
108a8c21a54SThe etnaviv authors 		if (IS_ERR(sgt)) {
109a8c21a54SThe etnaviv authors 			dev_err(dev->dev, "failed to allocate sgt: %ld\n",
110a8c21a54SThe etnaviv authors 				PTR_ERR(sgt));
111a8c21a54SThe etnaviv authors 			return ERR_CAST(sgt);
112a8c21a54SThe etnaviv authors 		}
113a8c21a54SThe etnaviv authors 
114a8c21a54SThe etnaviv authors 		etnaviv_obj->sgt = sgt;
115a8c21a54SThe etnaviv authors 
116a8c21a54SThe etnaviv authors 		etnaviv_gem_scatter_map(etnaviv_obj);
117a8c21a54SThe etnaviv authors 	}
118a8c21a54SThe etnaviv authors 
119a8c21a54SThe etnaviv authors 	return etnaviv_obj->pages;
120a8c21a54SThe etnaviv authors }
121a8c21a54SThe etnaviv authors 
122a8c21a54SThe etnaviv authors void etnaviv_gem_put_pages(struct etnaviv_gem_object *etnaviv_obj)
123a8c21a54SThe etnaviv authors {
124a8c21a54SThe etnaviv authors 	lockdep_assert_held(&etnaviv_obj->lock);
125a8c21a54SThe etnaviv authors 	/* when we start tracking the pin count, then do something here */
126a8c21a54SThe etnaviv authors }
127a8c21a54SThe etnaviv authors 
1280e7f26e6SLucas Stach static int etnaviv_gem_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
129a8c21a54SThe etnaviv authors 		struct vm_area_struct *vma)
130a8c21a54SThe etnaviv authors {
131a8c21a54SThe etnaviv authors 	pgprot_t vm_page_prot;
132a8c21a54SThe etnaviv authors 
133*81fd23e2SThomas Zimmermann 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
134a8c21a54SThe etnaviv authors 
135a8c21a54SThe etnaviv authors 	vm_page_prot = vm_get_page_prot(vma->vm_flags);
136a8c21a54SThe etnaviv authors 
137a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_WC) {
138a8c21a54SThe etnaviv authors 		vma->vm_page_prot = pgprot_writecombine(vm_page_prot);
139a8c21a54SThe etnaviv authors 	} else if (etnaviv_obj->flags & ETNA_BO_UNCACHED) {
140a8c21a54SThe etnaviv authors 		vma->vm_page_prot = pgprot_noncached(vm_page_prot);
141a8c21a54SThe etnaviv authors 	} else {
142a8c21a54SThe etnaviv authors 		/*
143a8c21a54SThe etnaviv authors 		 * Shunt off cached objs to shmem file so they have their own
144a8c21a54SThe etnaviv authors 		 * address_space (so unmap_mapping_range does what we want,
145a8c21a54SThe etnaviv authors 		 * in particular in the case of mmap'd dmabufs)
146a8c21a54SThe etnaviv authors 		 */
147a8c21a54SThe etnaviv authors 		vma->vm_pgoff = 0;
148295992fbSChristian König 		vma_set_file(vma, etnaviv_obj->base.filp);
149a8c21a54SThe etnaviv authors 
150a8c21a54SThe etnaviv authors 		vma->vm_page_prot = vm_page_prot;
151a8c21a54SThe etnaviv authors 	}
152a8c21a54SThe etnaviv authors 
153a8c21a54SThe etnaviv authors 	return 0;
154a8c21a54SThe etnaviv authors }
155a8c21a54SThe etnaviv authors 
156*81fd23e2SThomas Zimmermann static int etnaviv_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
157a8c21a54SThe etnaviv authors {
158*81fd23e2SThomas Zimmermann 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
159a8c21a54SThe etnaviv authors 
160*81fd23e2SThomas Zimmermann 	return etnaviv_obj->ops->mmap(etnaviv_obj, vma);
161a8c21a54SThe etnaviv authors }
162a8c21a54SThe etnaviv authors 
163a7730627SThomas Zimmermann static vm_fault_t etnaviv_gem_fault(struct vm_fault *vmf)
164a8c21a54SThe etnaviv authors {
16511bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
166a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj = vma->vm_private_data;
167a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
168a8c21a54SThe etnaviv authors 	struct page **pages, *page;
169a8c21a54SThe etnaviv authors 	pgoff_t pgoff;
170cfad05a2SSouptick Joarder 	int err;
171a8c21a54SThe etnaviv authors 
172a8c21a54SThe etnaviv authors 	/*
173a8c21a54SThe etnaviv authors 	 * Make sure we don't parallel update on a fault, nor move or remove
174cfad05a2SSouptick Joarder 	 * something from beneath our feet.  Note that vmf_insert_page() is
175a8c21a54SThe etnaviv authors 	 * specifically coded to take care of this, so we don't have to.
176a8c21a54SThe etnaviv authors 	 */
177cfad05a2SSouptick Joarder 	err = mutex_lock_interruptible(&etnaviv_obj->lock);
178cfad05a2SSouptick Joarder 	if (err)
179cfad05a2SSouptick Joarder 		return VM_FAULT_NOPAGE;
180a8c21a54SThe etnaviv authors 	/* make sure we have pages attached now */
181a8c21a54SThe etnaviv authors 	pages = etnaviv_gem_get_pages(etnaviv_obj);
182a8c21a54SThe etnaviv authors 	mutex_unlock(&etnaviv_obj->lock);
183a8c21a54SThe etnaviv authors 
184a8c21a54SThe etnaviv authors 	if (IS_ERR(pages)) {
185cfad05a2SSouptick Joarder 		err = PTR_ERR(pages);
186cfad05a2SSouptick Joarder 		return vmf_error(err);
187a8c21a54SThe etnaviv authors 	}
188a8c21a54SThe etnaviv authors 
189a8c21a54SThe etnaviv authors 	/* We don't use vmf->pgoff since that has the fake offset: */
1901a29d85eSJan Kara 	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
191a8c21a54SThe etnaviv authors 
192a8c21a54SThe etnaviv authors 	page = pages[pgoff];
193a8c21a54SThe etnaviv authors 
1941a29d85eSJan Kara 	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
195a8c21a54SThe etnaviv authors 	     page_to_pfn(page), page_to_pfn(page) << PAGE_SHIFT);
196a8c21a54SThe etnaviv authors 
197cfad05a2SSouptick Joarder 	return vmf_insert_page(vma, vmf->address, page);
198a8c21a54SThe etnaviv authors }
199a8c21a54SThe etnaviv authors 
200a8c21a54SThe etnaviv authors int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset)
201a8c21a54SThe etnaviv authors {
202a8c21a54SThe etnaviv authors 	int ret;
203a8c21a54SThe etnaviv authors 
204a8c21a54SThe etnaviv authors 	/* Make it mmapable */
205a8c21a54SThe etnaviv authors 	ret = drm_gem_create_mmap_offset(obj);
206a8c21a54SThe etnaviv authors 	if (ret)
207a8c21a54SThe etnaviv authors 		dev_err(obj->dev->dev, "could not allocate mmap offset\n");
208a8c21a54SThe etnaviv authors 	else
209a8c21a54SThe etnaviv authors 		*offset = drm_vma_node_offset_addr(&obj->vma_node);
210a8c21a54SThe etnaviv authors 
211a8c21a54SThe etnaviv authors 	return ret;
212a8c21a54SThe etnaviv authors }
213a8c21a54SThe etnaviv authors 
214a8c21a54SThe etnaviv authors static struct etnaviv_vram_mapping *
215a8c21a54SThe etnaviv authors etnaviv_gem_get_vram_mapping(struct etnaviv_gem_object *obj,
21627b67278SLucas Stach 			     struct etnaviv_iommu_context *context)
217a8c21a54SThe etnaviv authors {
218a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping;
219a8c21a54SThe etnaviv authors 
220a8c21a54SThe etnaviv authors 	list_for_each_entry(mapping, &obj->vram_list, obj_node) {
22127b67278SLucas Stach 		if (mapping->context == context)
222a8c21a54SThe etnaviv authors 			return mapping;
223a8c21a54SThe etnaviv authors 	}
224a8c21a54SThe etnaviv authors 
225a8c21a54SThe etnaviv authors 	return NULL;
226a8c21a54SThe etnaviv authors }
227a8c21a54SThe etnaviv authors 
228b6325f40SRussell King void etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping)
229b6325f40SRussell King {
230b6325f40SRussell King 	struct etnaviv_gem_object *etnaviv_obj = mapping->object;
231b6325f40SRussell King 
232b6325f40SRussell King 	mutex_lock(&etnaviv_obj->lock);
233b6325f40SRussell King 	WARN_ON(mapping->use == 0);
234b6325f40SRussell King 	mapping->use -= 1;
235b6325f40SRussell King 	mutex_unlock(&etnaviv_obj->lock);
236b6325f40SRussell King 
2376780bf32SEmil Velikov 	drm_gem_object_put(&etnaviv_obj->base);
238b6325f40SRussell King }
239b6325f40SRussell King 
240b6325f40SRussell King struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
241088880ddSLucas Stach 	struct drm_gem_object *obj, struct etnaviv_iommu_context *mmu_context,
242088880ddSLucas Stach 	u64 va)
243a8c21a54SThe etnaviv authors {
244a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
245a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping;
246a8c21a54SThe etnaviv authors 	struct page **pages;
247a8c21a54SThe etnaviv authors 	int ret = 0;
248a8c21a54SThe etnaviv authors 
249a8c21a54SThe etnaviv authors 	mutex_lock(&etnaviv_obj->lock);
250e6364d70SLucas Stach 	mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, mmu_context);
251a8c21a54SThe etnaviv authors 	if (mapping) {
252a8c21a54SThe etnaviv authors 		/*
253a8c21a54SThe etnaviv authors 		 * Holding the object lock prevents the use count changing
254a8c21a54SThe etnaviv authors 		 * beneath us.  If the use count is zero, the MMU might be
255a8c21a54SThe etnaviv authors 		 * reaping this object, so take the lock and re-check that
256a8c21a54SThe etnaviv authors 		 * the MMU owns this mapping to close this race.
257a8c21a54SThe etnaviv authors 		 */
258a8c21a54SThe etnaviv authors 		if (mapping->use == 0) {
259e6364d70SLucas Stach 			mutex_lock(&mmu_context->lock);
260e6364d70SLucas Stach 			if (mapping->context == mmu_context)
261a8c21a54SThe etnaviv authors 				mapping->use += 1;
262a8c21a54SThe etnaviv authors 			else
263a8c21a54SThe etnaviv authors 				mapping = NULL;
264e6364d70SLucas Stach 			mutex_unlock(&mmu_context->lock);
265a8c21a54SThe etnaviv authors 			if (mapping)
266a8c21a54SThe etnaviv authors 				goto out;
267a8c21a54SThe etnaviv authors 		} else {
268a8c21a54SThe etnaviv authors 			mapping->use += 1;
269a8c21a54SThe etnaviv authors 			goto out;
270a8c21a54SThe etnaviv authors 		}
271a8c21a54SThe etnaviv authors 	}
272a8c21a54SThe etnaviv authors 
273a8c21a54SThe etnaviv authors 	pages = etnaviv_gem_get_pages(etnaviv_obj);
274a8c21a54SThe etnaviv authors 	if (IS_ERR(pages)) {
275a8c21a54SThe etnaviv authors 		ret = PTR_ERR(pages);
276a8c21a54SThe etnaviv authors 		goto out;
277a8c21a54SThe etnaviv authors 	}
278a8c21a54SThe etnaviv authors 
279a8c21a54SThe etnaviv authors 	/*
280a8c21a54SThe etnaviv authors 	 * See if we have a reaped vram mapping we can re-use before
281a8c21a54SThe etnaviv authors 	 * allocating a fresh mapping.
282a8c21a54SThe etnaviv authors 	 */
283a8c21a54SThe etnaviv authors 	mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, NULL);
284a8c21a54SThe etnaviv authors 	if (!mapping) {
285a8c21a54SThe etnaviv authors 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
286ed94add0SDan Carpenter 		if (!mapping) {
287ed94add0SDan Carpenter 			ret = -ENOMEM;
288ed94add0SDan Carpenter 			goto out;
289ed94add0SDan Carpenter 		}
290a8c21a54SThe etnaviv authors 
291a8c21a54SThe etnaviv authors 		INIT_LIST_HEAD(&mapping->scan_node);
292a8c21a54SThe etnaviv authors 		mapping->object = etnaviv_obj;
293a8c21a54SThe etnaviv authors 	} else {
294a8c21a54SThe etnaviv authors 		list_del(&mapping->obj_node);
295a8c21a54SThe etnaviv authors 	}
296a8c21a54SThe etnaviv authors 
297e6364d70SLucas Stach 	etnaviv_iommu_context_get(mmu_context);
298e6364d70SLucas Stach 	mapping->context = mmu_context;
299a8c21a54SThe etnaviv authors 	mapping->use = 1;
300a8c21a54SThe etnaviv authors 
30117e4660aSLucas Stach 	ret = etnaviv_iommu_map_gem(mmu_context, etnaviv_obj,
30217eae23bSLucas Stach 				    mmu_context->global->memory_base,
303088880ddSLucas Stach 				    mapping, va);
304e6364d70SLucas Stach 	if (ret < 0) {
305e6364d70SLucas Stach 		etnaviv_iommu_context_put(mmu_context);
306a8c21a54SThe etnaviv authors 		kfree(mapping);
307e6364d70SLucas Stach 	} else {
308a8c21a54SThe etnaviv authors 		list_add_tail(&mapping->obj_node, &etnaviv_obj->vram_list);
309e6364d70SLucas Stach 	}
310a8c21a54SThe etnaviv authors 
311a8c21a54SThe etnaviv authors out:
312a8c21a54SThe etnaviv authors 	mutex_unlock(&etnaviv_obj->lock);
313a8c21a54SThe etnaviv authors 
314b6325f40SRussell King 	if (ret)
315b6325f40SRussell King 		return ERR_PTR(ret);
316b6325f40SRussell King 
317a8c21a54SThe etnaviv authors 	/* Take a reference on the object */
31823d1dd03SCihangir Akturk 	drm_gem_object_get(obj);
319b6325f40SRussell King 	return mapping;
320a8c21a54SThe etnaviv authors }
321a8c21a54SThe etnaviv authors 
322ce3088fdSLucas Stach void *etnaviv_gem_vmap(struct drm_gem_object *obj)
323a8c21a54SThe etnaviv authors {
324a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
325a8c21a54SThe etnaviv authors 
326a0a5ab3eSLucas Stach 	if (etnaviv_obj->vaddr)
327a0a5ab3eSLucas Stach 		return etnaviv_obj->vaddr;
328a0a5ab3eSLucas Stach 
329a8c21a54SThe etnaviv authors 	mutex_lock(&etnaviv_obj->lock);
330a0a5ab3eSLucas Stach 	/*
331a0a5ab3eSLucas Stach 	 * Need to check again, as we might have raced with another thread
332a0a5ab3eSLucas Stach 	 * while waiting for the mutex.
333a0a5ab3eSLucas Stach 	 */
334a0a5ab3eSLucas Stach 	if (!etnaviv_obj->vaddr)
335a0a5ab3eSLucas Stach 		etnaviv_obj->vaddr = etnaviv_obj->ops->vmap(etnaviv_obj);
336a8c21a54SThe etnaviv authors 	mutex_unlock(&etnaviv_obj->lock);
337a8c21a54SThe etnaviv authors 
338a8c21a54SThe etnaviv authors 	return etnaviv_obj->vaddr;
339a8c21a54SThe etnaviv authors }
340a8c21a54SThe etnaviv authors 
341a0a5ab3eSLucas Stach static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj)
342a0a5ab3eSLucas Stach {
343a0a5ab3eSLucas Stach 	struct page **pages;
344a0a5ab3eSLucas Stach 
345a0a5ab3eSLucas Stach 	lockdep_assert_held(&obj->lock);
346a0a5ab3eSLucas Stach 
347a0a5ab3eSLucas Stach 	pages = etnaviv_gem_get_pages(obj);
348a0a5ab3eSLucas Stach 	if (IS_ERR(pages))
349a0a5ab3eSLucas Stach 		return NULL;
350a0a5ab3eSLucas Stach 
351a0a5ab3eSLucas Stach 	return vmap(pages, obj->base.size >> PAGE_SHIFT,
352a0a5ab3eSLucas Stach 			VM_MAP, pgprot_writecombine(PAGE_KERNEL));
353a0a5ab3eSLucas Stach }
354a0a5ab3eSLucas Stach 
355a8c21a54SThe etnaviv authors static inline enum dma_data_direction etnaviv_op_to_dma_dir(u32 op)
356a8c21a54SThe etnaviv authors {
357a8c21a54SThe etnaviv authors 	if (op & ETNA_PREP_READ)
358a8c21a54SThe etnaviv authors 		return DMA_FROM_DEVICE;
359a8c21a54SThe etnaviv authors 	else if (op & ETNA_PREP_WRITE)
360a8c21a54SThe etnaviv authors 		return DMA_TO_DEVICE;
361a8c21a54SThe etnaviv authors 	else
362a8c21a54SThe etnaviv authors 		return DMA_BIDIRECTIONAL;
363a8c21a54SThe etnaviv authors }
364a8c21a54SThe etnaviv authors 
365a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
36638c4a4cfSArnd Bergmann 		struct drm_etnaviv_timespec *timeout)
367a8c21a54SThe etnaviv authors {
368a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
369a8c21a54SThe etnaviv authors 	struct drm_device *dev = obj->dev;
370a8c21a54SThe etnaviv authors 	bool write = !!(op & ETNA_PREP_WRITE);
37146a269daSLucas Stach 	int ret;
372a8c21a54SThe etnaviv authors 
3738cc47b3eSLucas Stach 	if (!etnaviv_obj->sgt) {
3748cc47b3eSLucas Stach 		void *ret;
3758cc47b3eSLucas Stach 
3768cc47b3eSLucas Stach 		mutex_lock(&etnaviv_obj->lock);
3778cc47b3eSLucas Stach 		ret = etnaviv_gem_get_pages(etnaviv_obj);
3788cc47b3eSLucas Stach 		mutex_unlock(&etnaviv_obj->lock);
3798cc47b3eSLucas Stach 		if (IS_ERR(ret))
3808cc47b3eSLucas Stach 			return PTR_ERR(ret);
3818cc47b3eSLucas Stach 	}
3828cc47b3eSLucas Stach 
38346a269daSLucas Stach 	if (op & ETNA_PREP_NOSYNC) {
384d3fae3b3SChristian König 		if (!dma_resv_test_signaled(obj->resv, write))
38546a269daSLucas Stach 			return -EBUSY;
38646a269daSLucas Stach 	} else {
38746a269daSLucas Stach 		unsigned long remain = etnaviv_timeout_to_jiffies(timeout);
38846a269daSLucas Stach 
389d3fae3b3SChristian König 		ret = dma_resv_wait_timeout(obj->resv, write, true, remain);
39046a269daSLucas Stach 		if (ret <= 0)
39146a269daSLucas Stach 			return ret == 0 ? -ETIMEDOUT : ret;
39246a269daSLucas Stach 	}
393a8c21a54SThe etnaviv authors 
394a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHED) {
395182354a5SMarek Szyprowski 		dma_sync_sgtable_for_cpu(dev->dev, etnaviv_obj->sgt,
396a8c21a54SThe etnaviv authors 					 etnaviv_op_to_dma_dir(op));
397a8c21a54SThe etnaviv authors 		etnaviv_obj->last_cpu_prep_op = op;
398a8c21a54SThe etnaviv authors 	}
399a8c21a54SThe etnaviv authors 
400a8c21a54SThe etnaviv authors 	return 0;
401a8c21a54SThe etnaviv authors }
402a8c21a54SThe etnaviv authors 
403a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_fini(struct drm_gem_object *obj)
404a8c21a54SThe etnaviv authors {
405a8c21a54SThe etnaviv authors 	struct drm_device *dev = obj->dev;
406a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
407a8c21a54SThe etnaviv authors 
408a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHED) {
409a8c21a54SThe etnaviv authors 		/* fini without a prep is almost certainly a userspace error */
410a8c21a54SThe etnaviv authors 		WARN_ON(etnaviv_obj->last_cpu_prep_op == 0);
411182354a5SMarek Szyprowski 		dma_sync_sgtable_for_device(dev->dev, etnaviv_obj->sgt,
412a8c21a54SThe etnaviv authors 			etnaviv_op_to_dma_dir(etnaviv_obj->last_cpu_prep_op));
413a8c21a54SThe etnaviv authors 		etnaviv_obj->last_cpu_prep_op = 0;
414a8c21a54SThe etnaviv authors 	}
415a8c21a54SThe etnaviv authors 
416a8c21a54SThe etnaviv authors 	return 0;
417a8c21a54SThe etnaviv authors }
418a8c21a54SThe etnaviv authors 
419a8c21a54SThe etnaviv authors int etnaviv_gem_wait_bo(struct etnaviv_gpu *gpu, struct drm_gem_object *obj,
42038c4a4cfSArnd Bergmann 	struct drm_etnaviv_timespec *timeout)
421a8c21a54SThe etnaviv authors {
422a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
423a8c21a54SThe etnaviv authors 
424a8c21a54SThe etnaviv authors 	return etnaviv_gpu_wait_obj_inactive(gpu, etnaviv_obj, timeout);
425a8c21a54SThe etnaviv authors }
426a8c21a54SThe etnaviv authors 
427a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
428f54d1867SChris Wilson static void etnaviv_gem_describe_fence(struct dma_fence *fence,
429a8c21a54SThe etnaviv authors 	const char *type, struct seq_file *m)
430a8c21a54SThe etnaviv authors {
431f54d1867SChris Wilson 	if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
4323415701aSChristian König 		seq_printf(m, "\t%9s: %s %s seq %llu\n",
433a8c21a54SThe etnaviv authors 			   type,
434a8c21a54SThe etnaviv authors 			   fence->ops->get_driver_name(fence),
435a8c21a54SThe etnaviv authors 			   fence->ops->get_timeline_name(fence),
436a8c21a54SThe etnaviv authors 			   fence->seqno);
437a8c21a54SThe etnaviv authors }
438a8c21a54SThe etnaviv authors 
439a8c21a54SThe etnaviv authors static void etnaviv_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
440a8c21a54SThe etnaviv authors {
441a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
44252791eeeSChristian König 	struct dma_resv *robj = obj->resv;
44352791eeeSChristian König 	struct dma_resv_list *fobj;
444f54d1867SChris Wilson 	struct dma_fence *fence;
445a8c21a54SThe etnaviv authors 	unsigned long off = drm_vma_node_start(&obj->vma_node);
446a8c21a54SThe etnaviv authors 
447a8c21a54SThe etnaviv authors 	seq_printf(m, "%08x: %c %2d (%2d) %08lx %p %zd\n",
448a8c21a54SThe etnaviv authors 			etnaviv_obj->flags, is_active(etnaviv_obj) ? 'A' : 'I',
4492c935bc5SPeter Zijlstra 			obj->name, kref_read(&obj->refcount),
450a8c21a54SThe etnaviv authors 			off, etnaviv_obj->vaddr, obj->size);
451a8c21a54SThe etnaviv authors 
452a8c21a54SThe etnaviv authors 	rcu_read_lock();
453fb5ce730SChristian König 	fobj = dma_resv_shared_list(robj);
454a8c21a54SThe etnaviv authors 	if (fobj) {
455a8c21a54SThe etnaviv authors 		unsigned int i, shared_count = fobj->shared_count;
456a8c21a54SThe etnaviv authors 
457a8c21a54SThe etnaviv authors 		for (i = 0; i < shared_count; i++) {
458a8c21a54SThe etnaviv authors 			fence = rcu_dereference(fobj->shared[i]);
459a8c21a54SThe etnaviv authors 			etnaviv_gem_describe_fence(fence, "Shared", m);
460a8c21a54SThe etnaviv authors 		}
461a8c21a54SThe etnaviv authors 	}
462a8c21a54SThe etnaviv authors 
4636edbd6abSChristian König 	fence = dma_resv_excl_fence(robj);
464a8c21a54SThe etnaviv authors 	if (fence)
465a8c21a54SThe etnaviv authors 		etnaviv_gem_describe_fence(fence, "Exclusive", m);
466a8c21a54SThe etnaviv authors 	rcu_read_unlock();
467a8c21a54SThe etnaviv authors }
468a8c21a54SThe etnaviv authors 
469a8c21a54SThe etnaviv authors void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
470a8c21a54SThe etnaviv authors 	struct seq_file *m)
471a8c21a54SThe etnaviv authors {
472a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj;
473a8c21a54SThe etnaviv authors 	int count = 0;
474a8c21a54SThe etnaviv authors 	size_t size = 0;
475a8c21a54SThe etnaviv authors 
476a8c21a54SThe etnaviv authors 	mutex_lock(&priv->gem_lock);
477a8c21a54SThe etnaviv authors 	list_for_each_entry(etnaviv_obj, &priv->gem_list, gem_node) {
478a8c21a54SThe etnaviv authors 		struct drm_gem_object *obj = &etnaviv_obj->base;
479a8c21a54SThe etnaviv authors 
480a8c21a54SThe etnaviv authors 		seq_puts(m, "   ");
481a8c21a54SThe etnaviv authors 		etnaviv_gem_describe(obj, m);
482a8c21a54SThe etnaviv authors 		count++;
483a8c21a54SThe etnaviv authors 		size += obj->size;
484a8c21a54SThe etnaviv authors 	}
485a8c21a54SThe etnaviv authors 	mutex_unlock(&priv->gem_lock);
486a8c21a54SThe etnaviv authors 
487a8c21a54SThe etnaviv authors 	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
488a8c21a54SThe etnaviv authors }
489a8c21a54SThe etnaviv authors #endif
490a8c21a54SThe etnaviv authors 
491a8c21a54SThe etnaviv authors static void etnaviv_gem_shmem_release(struct etnaviv_gem_object *etnaviv_obj)
492a8c21a54SThe etnaviv authors {
493a8c21a54SThe etnaviv authors 	vunmap(etnaviv_obj->vaddr);
494a8c21a54SThe etnaviv authors 	put_pages(etnaviv_obj);
495a8c21a54SThe etnaviv authors }
496a8c21a54SThe etnaviv authors 
497a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_shmem_ops = {
498a8c21a54SThe etnaviv authors 	.get_pages = etnaviv_gem_shmem_get_pages,
499a8c21a54SThe etnaviv authors 	.release = etnaviv_gem_shmem_release,
500a0a5ab3eSLucas Stach 	.vmap = etnaviv_gem_vmap_impl,
501a10e2bdeSLucas Stach 	.mmap = etnaviv_gem_mmap_obj,
502a8c21a54SThe etnaviv authors };
503a8c21a54SThe etnaviv authors 
504a8c21a54SThe etnaviv authors void etnaviv_gem_free_object(struct drm_gem_object *obj)
505a8c21a54SThe etnaviv authors {
506a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
50751841752SLucas Stach 	struct etnaviv_drm_private *priv = obj->dev->dev_private;
508a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping, *tmp;
509a8c21a54SThe etnaviv authors 
510a8c21a54SThe etnaviv authors 	/* object should not be active */
511a8c21a54SThe etnaviv authors 	WARN_ON(is_active(etnaviv_obj));
512a8c21a54SThe etnaviv authors 
51351841752SLucas Stach 	mutex_lock(&priv->gem_lock);
514a8c21a54SThe etnaviv authors 	list_del(&etnaviv_obj->gem_node);
51551841752SLucas Stach 	mutex_unlock(&priv->gem_lock);
516a8c21a54SThe etnaviv authors 
517a8c21a54SThe etnaviv authors 	list_for_each_entry_safe(mapping, tmp, &etnaviv_obj->vram_list,
518a8c21a54SThe etnaviv authors 				 obj_node) {
51927b67278SLucas Stach 		struct etnaviv_iommu_context *context = mapping->context;
520a8c21a54SThe etnaviv authors 
521a8c21a54SThe etnaviv authors 		WARN_ON(mapping->use);
522a8c21a54SThe etnaviv authors 
523e6364d70SLucas Stach 		if (context) {
52427b67278SLucas Stach 			etnaviv_iommu_unmap_gem(context, mapping);
525e6364d70SLucas Stach 			etnaviv_iommu_context_put(context);
526e6364d70SLucas Stach 		}
527a8c21a54SThe etnaviv authors 
528a8c21a54SThe etnaviv authors 		list_del(&mapping->obj_node);
529a8c21a54SThe etnaviv authors 		kfree(mapping);
530a8c21a54SThe etnaviv authors 	}
531a8c21a54SThe etnaviv authors 
532a8c21a54SThe etnaviv authors 	drm_gem_free_mmap_offset(obj);
533a8c21a54SThe etnaviv authors 	etnaviv_obj->ops->release(etnaviv_obj);
534a8c21a54SThe etnaviv authors 	drm_gem_object_release(obj);
535a8c21a54SThe etnaviv authors 
536a8c21a54SThe etnaviv authors 	kfree(etnaviv_obj);
537a8c21a54SThe etnaviv authors }
538a8c21a54SThe etnaviv authors 
53954f09288SLucas Stach void etnaviv_gem_obj_add(struct drm_device *dev, struct drm_gem_object *obj)
540a8c21a54SThe etnaviv authors {
541a8c21a54SThe etnaviv authors 	struct etnaviv_drm_private *priv = dev->dev_private;
542a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
543a8c21a54SThe etnaviv authors 
544a8c21a54SThe etnaviv authors 	mutex_lock(&priv->gem_lock);
545a8c21a54SThe etnaviv authors 	list_add_tail(&etnaviv_obj->gem_node, &priv->gem_list);
546a8c21a54SThe etnaviv authors 	mutex_unlock(&priv->gem_lock);
547a8c21a54SThe etnaviv authors }
548a8c21a54SThe etnaviv authors 
549a7730627SThomas Zimmermann static const struct vm_operations_struct vm_ops = {
550a7730627SThomas Zimmermann 	.fault = etnaviv_gem_fault,
551a7730627SThomas Zimmermann 	.open = drm_gem_vm_open,
552a7730627SThomas Zimmermann 	.close = drm_gem_vm_close,
553a7730627SThomas Zimmermann };
554a7730627SThomas Zimmermann 
555a7730627SThomas Zimmermann static const struct drm_gem_object_funcs etnaviv_gem_object_funcs = {
556a7730627SThomas Zimmermann 	.free = etnaviv_gem_free_object,
557a7730627SThomas Zimmermann 	.pin = etnaviv_gem_prime_pin,
558a7730627SThomas Zimmermann 	.unpin = etnaviv_gem_prime_unpin,
559a7730627SThomas Zimmermann 	.get_sg_table = etnaviv_gem_prime_get_sg_table,
560a7730627SThomas Zimmermann 	.vmap = etnaviv_gem_prime_vmap,
561*81fd23e2SThomas Zimmermann 	.mmap = etnaviv_gem_mmap,
562a7730627SThomas Zimmermann 	.vm_ops = &vm_ops,
563a7730627SThomas Zimmermann };
564a7730627SThomas Zimmermann 
565a8c21a54SThe etnaviv authors static int etnaviv_gem_new_impl(struct drm_device *dev, u32 size, u32 flags,
566c6be8086SDaniel Vetter 	const struct etnaviv_gem_ops *ops, struct drm_gem_object **obj)
567a8c21a54SThe etnaviv authors {
568a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj;
569a8c21a54SThe etnaviv authors 	unsigned sz = sizeof(*etnaviv_obj);
570a8c21a54SThe etnaviv authors 	bool valid = true;
571a8c21a54SThe etnaviv authors 
572a8c21a54SThe etnaviv authors 	/* validate flags */
573a8c21a54SThe etnaviv authors 	switch (flags & ETNA_BO_CACHE_MASK) {
574a8c21a54SThe etnaviv authors 	case ETNA_BO_UNCACHED:
575a8c21a54SThe etnaviv authors 	case ETNA_BO_CACHED:
576a8c21a54SThe etnaviv authors 	case ETNA_BO_WC:
577a8c21a54SThe etnaviv authors 		break;
578a8c21a54SThe etnaviv authors 	default:
579a8c21a54SThe etnaviv authors 		valid = false;
580a8c21a54SThe etnaviv authors 	}
581a8c21a54SThe etnaviv authors 
582a8c21a54SThe etnaviv authors 	if (!valid) {
583a8c21a54SThe etnaviv authors 		dev_err(dev->dev, "invalid cache flag: %x\n",
584a8c21a54SThe etnaviv authors 			(flags & ETNA_BO_CACHE_MASK));
585a8c21a54SThe etnaviv authors 		return -EINVAL;
586a8c21a54SThe etnaviv authors 	}
587a8c21a54SThe etnaviv authors 
588a8c21a54SThe etnaviv authors 	etnaviv_obj = kzalloc(sz, GFP_KERNEL);
589a8c21a54SThe etnaviv authors 	if (!etnaviv_obj)
590a8c21a54SThe etnaviv authors 		return -ENOMEM;
591a8c21a54SThe etnaviv authors 
592a8c21a54SThe etnaviv authors 	etnaviv_obj->flags = flags;
593a8c21a54SThe etnaviv authors 	etnaviv_obj->ops = ops;
594a8c21a54SThe etnaviv authors 
595a8c21a54SThe etnaviv authors 	mutex_init(&etnaviv_obj->lock);
596a8c21a54SThe etnaviv authors 	INIT_LIST_HEAD(&etnaviv_obj->vram_list);
597a8c21a54SThe etnaviv authors 
598a8c21a54SThe etnaviv authors 	*obj = &etnaviv_obj->base;
599a7730627SThomas Zimmermann 	(*obj)->funcs = &etnaviv_gem_object_funcs;
600a8c21a54SThe etnaviv authors 
601a8c21a54SThe etnaviv authors 	return 0;
602a8c21a54SThe etnaviv authors }
603a8c21a54SThe etnaviv authors 
604cdd32563SLucas Stach /* convenience method to construct a GEM buffer object, and userspace handle */
605cdd32563SLucas Stach int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
606cdd32563SLucas Stach 	u32 size, u32 flags, u32 *handle)
607a8c21a54SThe etnaviv authors {
608b72af445SLucas Stach 	struct etnaviv_drm_private *priv = dev->dev_private;
609a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj = NULL;
610a8c21a54SThe etnaviv authors 	int ret;
611a8c21a54SThe etnaviv authors 
612a8c21a54SThe etnaviv authors 	size = PAGE_ALIGN(size);
613a8c21a54SThe etnaviv authors 
614c6be8086SDaniel Vetter 	ret = etnaviv_gem_new_impl(dev, size, flags,
615a8c21a54SThe etnaviv authors 				   &etnaviv_gem_shmem_ops, &obj);
616a8c21a54SThe etnaviv authors 	if (ret)
617a8c21a54SThe etnaviv authors 		goto fail;
618a8c21a54SThe etnaviv authors 
619d6a8743dSLucas Stach 	lockdep_set_class(&to_etnaviv_bo(obj)->lock, &etnaviv_shm_lock_class);
620d6a8743dSLucas Stach 
621a8c21a54SThe etnaviv authors 	ret = drm_gem_object_init(dev, obj, size);
622a8c21a54SThe etnaviv authors 	if (ret)
623a8c21a54SThe etnaviv authors 		goto fail;
624a8c21a54SThe etnaviv authors 
625fd2450a7SLucas Stach 	/*
626fd2450a7SLucas Stach 	 * Our buffers are kept pinned, so allocating them from the MOVABLE
627fd2450a7SLucas Stach 	 * zone is a really bad idea, and conflicts with CMA. See comments
628fd2450a7SLucas Stach 	 * above new_inode() why this is required _and_ expected if you're
629fd2450a7SLucas Stach 	 * going to pin these pages.
630fd2450a7SLucas Stach 	 */
631b72af445SLucas Stach 	mapping_set_gfp_mask(obj->filp->f_mapping, priv->shm_gfp_mask);
632fd2450a7SLucas Stach 
63354f09288SLucas Stach 	etnaviv_gem_obj_add(dev, obj);
634a8c21a54SThe etnaviv authors 
635a8c21a54SThe etnaviv authors 	ret = drm_gem_handle_create(file, obj, handle);
636a8c21a54SThe etnaviv authors 
637a8c21a54SThe etnaviv authors 	/* drop reference from allocate - handle holds it now */
638cdd32563SLucas Stach fail:
6396780bf32SEmil Velikov 	drm_gem_object_put(obj);
640a8c21a54SThe etnaviv authors 
641a8c21a54SThe etnaviv authors 	return ret;
642a8c21a54SThe etnaviv authors }
643a8c21a54SThe etnaviv authors 
644a8c21a54SThe etnaviv authors int etnaviv_gem_new_private(struct drm_device *dev, size_t size, u32 flags,
645c6be8086SDaniel Vetter 	const struct etnaviv_gem_ops *ops, struct etnaviv_gem_object **res)
646a8c21a54SThe etnaviv authors {
647a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj;
648a8c21a54SThe etnaviv authors 	int ret;
649a8c21a54SThe etnaviv authors 
650c6be8086SDaniel Vetter 	ret = etnaviv_gem_new_impl(dev, size, flags, ops, &obj);
651a8c21a54SThe etnaviv authors 	if (ret)
652a8c21a54SThe etnaviv authors 		return ret;
653a8c21a54SThe etnaviv authors 
654a8c21a54SThe etnaviv authors 	drm_gem_private_object_init(dev, obj, size);
655a8c21a54SThe etnaviv authors 
656a8c21a54SThe etnaviv authors 	*res = to_etnaviv_bo(obj);
657a8c21a54SThe etnaviv authors 
658a8c21a54SThe etnaviv authors 	return 0;
659a8c21a54SThe etnaviv authors }
660a8c21a54SThe etnaviv authors 
661a8c21a54SThe etnaviv authors static int etnaviv_gem_userptr_get_pages(struct etnaviv_gem_object *etnaviv_obj)
662a8c21a54SThe etnaviv authors {
663a8c21a54SThe etnaviv authors 	struct page **pvec = NULL;
664b2295c24SLucas Stach 	struct etnaviv_gem_userptr *userptr = &etnaviv_obj->userptr;
665b2295c24SLucas Stach 	int ret, pinned = 0, npages = etnaviv_obj->base.size >> PAGE_SHIFT;
666a8c21a54SThe etnaviv authors 
667da1c55f1SMichel Lespinasse 	might_lock_read(&current->mm->mmap_lock);
668783c06cbSLucas Stach 
669b2295c24SLucas Stach 	if (userptr->mm != current->mm)
670b2295c24SLucas Stach 		return -EPERM;
671b2295c24SLucas Stach 
672b2295c24SLucas Stach 	pvec = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
673b2295c24SLucas Stach 	if (!pvec)
674b2295c24SLucas Stach 		return -ENOMEM;
675b2295c24SLucas Stach 
676b2295c24SLucas Stach 	do {
677b2295c24SLucas Stach 		unsigned num_pages = npages - pinned;
678b2295c24SLucas Stach 		uint64_t ptr = userptr->ptr + pinned * PAGE_SIZE;
679b2295c24SLucas Stach 		struct page **pages = pvec + pinned;
680b2295c24SLucas Stach 
68186824e60SJohn Hubbard 		ret = pin_user_pages_fast(ptr, num_pages,
68250891beaSDaniel Vetter 					  FOLL_WRITE | FOLL_FORCE | FOLL_LONGTERM,
68350891beaSDaniel Vetter 					  pages);
684b2295c24SLucas Stach 		if (ret < 0) {
68586824e60SJohn Hubbard 			unpin_user_pages(pvec, pinned);
686b2295c24SLucas Stach 			kvfree(pvec);
687a8c21a54SThe etnaviv authors 			return ret;
688a8c21a54SThe etnaviv authors 		}
689a8c21a54SThe etnaviv authors 
690b2295c24SLucas Stach 		pinned += ret;
691a8c21a54SThe etnaviv authors 
692b2295c24SLucas Stach 	} while (pinned < npages);
693a8c21a54SThe etnaviv authors 
694a8c21a54SThe etnaviv authors 	etnaviv_obj->pages = pvec;
695b2295c24SLucas Stach 
696a8c21a54SThe etnaviv authors 	return 0;
697a8c21a54SThe etnaviv authors }
698a8c21a54SThe etnaviv authors 
699a8c21a54SThe etnaviv authors static void etnaviv_gem_userptr_release(struct etnaviv_gem_object *etnaviv_obj)
700a8c21a54SThe etnaviv authors {
701a8c21a54SThe etnaviv authors 	if (etnaviv_obj->sgt) {
702a8c21a54SThe etnaviv authors 		etnaviv_gem_scatterlist_unmap(etnaviv_obj);
703a8c21a54SThe etnaviv authors 		sg_free_table(etnaviv_obj->sgt);
704a8c21a54SThe etnaviv authors 		kfree(etnaviv_obj->sgt);
705a8c21a54SThe etnaviv authors 	}
706a8c21a54SThe etnaviv authors 	if (etnaviv_obj->pages) {
707a8c21a54SThe etnaviv authors 		int npages = etnaviv_obj->base.size >> PAGE_SHIFT;
708a8c21a54SThe etnaviv authors 
70986824e60SJohn Hubbard 		unpin_user_pages(etnaviv_obj->pages, npages);
7102098105eSMichal Hocko 		kvfree(etnaviv_obj->pages);
711a8c21a54SThe etnaviv authors 	}
712a8c21a54SThe etnaviv authors }
713a8c21a54SThe etnaviv authors 
714a10e2bdeSLucas Stach static int etnaviv_gem_userptr_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
715a10e2bdeSLucas Stach 		struct vm_area_struct *vma)
716a10e2bdeSLucas Stach {
717a10e2bdeSLucas Stach 	return -EINVAL;
718a10e2bdeSLucas Stach }
719a10e2bdeSLucas Stach 
720a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_userptr_ops = {
721a8c21a54SThe etnaviv authors 	.get_pages = etnaviv_gem_userptr_get_pages,
722a8c21a54SThe etnaviv authors 	.release = etnaviv_gem_userptr_release,
723a0a5ab3eSLucas Stach 	.vmap = etnaviv_gem_vmap_impl,
724a10e2bdeSLucas Stach 	.mmap = etnaviv_gem_userptr_mmap_obj,
725a8c21a54SThe etnaviv authors };
726a8c21a54SThe etnaviv authors 
727a8c21a54SThe etnaviv authors int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
728a8c21a54SThe etnaviv authors 	uintptr_t ptr, u32 size, u32 flags, u32 *handle)
729a8c21a54SThe etnaviv authors {
730a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj;
731a8c21a54SThe etnaviv authors 	int ret;
732a8c21a54SThe etnaviv authors 
733c6be8086SDaniel Vetter 	ret = etnaviv_gem_new_private(dev, size, ETNA_BO_CACHED,
734a8c21a54SThe etnaviv authors 				      &etnaviv_gem_userptr_ops, &etnaviv_obj);
735a8c21a54SThe etnaviv authors 	if (ret)
736a8c21a54SThe etnaviv authors 		return ret;
737a8c21a54SThe etnaviv authors 
738d6a8743dSLucas Stach 	lockdep_set_class(&etnaviv_obj->lock, &etnaviv_userptr_lock_class);
739d6a8743dSLucas Stach 
740a8c21a54SThe etnaviv authors 	etnaviv_obj->userptr.ptr = ptr;
741b2295c24SLucas Stach 	etnaviv_obj->userptr.mm = current->mm;
742a8c21a54SThe etnaviv authors 	etnaviv_obj->userptr.ro = !(flags & ETNA_USERPTR_WRITE);
743a8c21a54SThe etnaviv authors 
74454f09288SLucas Stach 	etnaviv_gem_obj_add(dev, &etnaviv_obj->base);
745a8c21a54SThe etnaviv authors 
746a8c21a54SThe etnaviv authors 	ret = drm_gem_handle_create(file, &etnaviv_obj->base, handle);
74754f09288SLucas Stach 
748a8c21a54SThe etnaviv authors 	/* drop reference from allocate - handle holds it now */
7496780bf32SEmil Velikov 	drm_gem_object_put(&etnaviv_obj->base);
750a8c21a54SThe etnaviv authors 	return ret;
751a8c21a54SThe etnaviv authors }
752