1a8c21a54SThe etnaviv authors /*
2a8c21a54SThe etnaviv authors  * Copyright (C) 2015 Etnaviv Project
3a8c21a54SThe etnaviv authors  *
4a8c21a54SThe etnaviv authors  * This program is free software; you can redistribute it and/or modify it
5a8c21a54SThe etnaviv authors  * under the terms of the GNU General Public License version 2 as published by
6a8c21a54SThe etnaviv authors  * the Free Software Foundation.
7a8c21a54SThe etnaviv authors  *
8a8c21a54SThe etnaviv authors  * This program is distributed in the hope that it will be useful, but WITHOUT
9a8c21a54SThe etnaviv authors  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10a8c21a54SThe etnaviv authors  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11a8c21a54SThe etnaviv authors  * more details.
12a8c21a54SThe etnaviv authors  *
13a8c21a54SThe etnaviv authors  * You should have received a copy of the GNU General Public License along with
14a8c21a54SThe etnaviv authors  * this program.  If not, see <http://www.gnu.org/licenses/>.
15a8c21a54SThe etnaviv authors  */
16a8c21a54SThe etnaviv authors 
17a8c21a54SThe etnaviv authors #include <linux/spinlock.h>
18a8c21a54SThe etnaviv authors #include <linux/shmem_fs.h>
196e84f315SIngo Molnar #include <linux/sched/mm.h>
200881e7bdSIngo Molnar #include <linux/sched/task.h>
21a8c21a54SThe etnaviv authors 
22a8c21a54SThe etnaviv authors #include "etnaviv_drv.h"
23a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
24a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
25a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
26a8c21a54SThe etnaviv authors 
27a8c21a54SThe etnaviv authors static void etnaviv_gem_scatter_map(struct etnaviv_gem_object *etnaviv_obj)
28a8c21a54SThe etnaviv authors {
29a8c21a54SThe etnaviv authors 	struct drm_device *dev = etnaviv_obj->base.dev;
30a8c21a54SThe etnaviv authors 	struct sg_table *sgt = etnaviv_obj->sgt;
31a8c21a54SThe etnaviv authors 
32a8c21a54SThe etnaviv authors 	/*
33a8c21a54SThe etnaviv authors 	 * For non-cached buffers, ensure the new pages are clean
34a8c21a54SThe etnaviv authors 	 * because display controller, GPU, etc. are not coherent.
35a8c21a54SThe etnaviv authors 	 */
36a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK)
37a8c21a54SThe etnaviv authors 		dma_map_sg(dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL);
38a8c21a54SThe etnaviv authors }
39a8c21a54SThe etnaviv authors 
40a8c21a54SThe etnaviv authors static void etnaviv_gem_scatterlist_unmap(struct etnaviv_gem_object *etnaviv_obj)
41a8c21a54SThe etnaviv authors {
42a8c21a54SThe etnaviv authors 	struct drm_device *dev = etnaviv_obj->base.dev;
43a8c21a54SThe etnaviv authors 	struct sg_table *sgt = etnaviv_obj->sgt;
44a8c21a54SThe etnaviv authors 
45a8c21a54SThe etnaviv authors 	/*
46a8c21a54SThe etnaviv authors 	 * For non-cached buffers, ensure the new pages are clean
47a8c21a54SThe etnaviv authors 	 * because display controller, GPU, etc. are not coherent:
48a8c21a54SThe etnaviv authors 	 *
49a8c21a54SThe etnaviv authors 	 * WARNING: The DMA API does not support concurrent CPU
50a8c21a54SThe etnaviv authors 	 * and device access to the memory area.  With BIDIRECTIONAL,
51a8c21a54SThe etnaviv authors 	 * we will clean the cache lines which overlap the region,
52a8c21a54SThe etnaviv authors 	 * and invalidate all cache lines (partially) contained in
53a8c21a54SThe etnaviv authors 	 * the region.
54a8c21a54SThe etnaviv authors 	 *
55a8c21a54SThe etnaviv authors 	 * If you have dirty data in the overlapping cache lines,
56a8c21a54SThe etnaviv authors 	 * that will corrupt the GPU-written data.  If you have
57a8c21a54SThe etnaviv authors 	 * written into the remainder of the region, this can
58a8c21a54SThe etnaviv authors 	 * discard those writes.
59a8c21a54SThe etnaviv authors 	 */
60a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK)
61a8c21a54SThe etnaviv authors 		dma_unmap_sg(dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL);
62a8c21a54SThe etnaviv authors }
63a8c21a54SThe etnaviv authors 
64a8c21a54SThe etnaviv authors /* called with etnaviv_obj->lock held */
65a8c21a54SThe etnaviv authors static int etnaviv_gem_shmem_get_pages(struct etnaviv_gem_object *etnaviv_obj)
66a8c21a54SThe etnaviv authors {
67a8c21a54SThe etnaviv authors 	struct drm_device *dev = etnaviv_obj->base.dev;
68a8c21a54SThe etnaviv authors 	struct page **p = drm_gem_get_pages(&etnaviv_obj->base);
69a8c21a54SThe etnaviv authors 
70a8c21a54SThe etnaviv authors 	if (IS_ERR(p)) {
71f91ac470SLucas Stach 		dev_dbg(dev->dev, "could not get pages: %ld\n", PTR_ERR(p));
72a8c21a54SThe etnaviv authors 		return PTR_ERR(p);
73a8c21a54SThe etnaviv authors 	}
74a8c21a54SThe etnaviv authors 
75a8c21a54SThe etnaviv authors 	etnaviv_obj->pages = p;
76a8c21a54SThe etnaviv authors 
77a8c21a54SThe etnaviv authors 	return 0;
78a8c21a54SThe etnaviv authors }
79a8c21a54SThe etnaviv authors 
80a8c21a54SThe etnaviv authors static void put_pages(struct etnaviv_gem_object *etnaviv_obj)
81a8c21a54SThe etnaviv authors {
82a8c21a54SThe etnaviv authors 	if (etnaviv_obj->sgt) {
83a8c21a54SThe etnaviv authors 		etnaviv_gem_scatterlist_unmap(etnaviv_obj);
84a8c21a54SThe etnaviv authors 		sg_free_table(etnaviv_obj->sgt);
85a8c21a54SThe etnaviv authors 		kfree(etnaviv_obj->sgt);
86a8c21a54SThe etnaviv authors 		etnaviv_obj->sgt = NULL;
87a8c21a54SThe etnaviv authors 	}
88a8c21a54SThe etnaviv authors 	if (etnaviv_obj->pages) {
89a8c21a54SThe etnaviv authors 		drm_gem_put_pages(&etnaviv_obj->base, etnaviv_obj->pages,
90a8c21a54SThe etnaviv authors 				  true, false);
91a8c21a54SThe etnaviv authors 
92a8c21a54SThe etnaviv authors 		etnaviv_obj->pages = NULL;
93a8c21a54SThe etnaviv authors 	}
94a8c21a54SThe etnaviv authors }
95a8c21a54SThe etnaviv authors 
96a8c21a54SThe etnaviv authors struct page **etnaviv_gem_get_pages(struct etnaviv_gem_object *etnaviv_obj)
97a8c21a54SThe etnaviv authors {
98a8c21a54SThe etnaviv authors 	int ret;
99a8c21a54SThe etnaviv authors 
100a8c21a54SThe etnaviv authors 	lockdep_assert_held(&etnaviv_obj->lock);
101a8c21a54SThe etnaviv authors 
102a8c21a54SThe etnaviv authors 	if (!etnaviv_obj->pages) {
103a8c21a54SThe etnaviv authors 		ret = etnaviv_obj->ops->get_pages(etnaviv_obj);
104a8c21a54SThe etnaviv authors 		if (ret < 0)
105a8c21a54SThe etnaviv authors 			return ERR_PTR(ret);
106a8c21a54SThe etnaviv authors 	}
107a8c21a54SThe etnaviv authors 
108a8c21a54SThe etnaviv authors 	if (!etnaviv_obj->sgt) {
109a8c21a54SThe etnaviv authors 		struct drm_device *dev = etnaviv_obj->base.dev;
110a8c21a54SThe etnaviv authors 		int npages = etnaviv_obj->base.size >> PAGE_SHIFT;
111a8c21a54SThe etnaviv authors 		struct sg_table *sgt;
112a8c21a54SThe etnaviv authors 
113a8c21a54SThe etnaviv authors 		sgt = drm_prime_pages_to_sg(etnaviv_obj->pages, npages);
114a8c21a54SThe etnaviv authors 		if (IS_ERR(sgt)) {
115a8c21a54SThe etnaviv authors 			dev_err(dev->dev, "failed to allocate sgt: %ld\n",
116a8c21a54SThe etnaviv authors 				PTR_ERR(sgt));
117a8c21a54SThe etnaviv authors 			return ERR_CAST(sgt);
118a8c21a54SThe etnaviv authors 		}
119a8c21a54SThe etnaviv authors 
120a8c21a54SThe etnaviv authors 		etnaviv_obj->sgt = sgt;
121a8c21a54SThe etnaviv authors 
122a8c21a54SThe etnaviv authors 		etnaviv_gem_scatter_map(etnaviv_obj);
123a8c21a54SThe etnaviv authors 	}
124a8c21a54SThe etnaviv authors 
125a8c21a54SThe etnaviv authors 	return etnaviv_obj->pages;
126a8c21a54SThe etnaviv authors }
127a8c21a54SThe etnaviv authors 
128a8c21a54SThe etnaviv authors void etnaviv_gem_put_pages(struct etnaviv_gem_object *etnaviv_obj)
129a8c21a54SThe etnaviv authors {
130a8c21a54SThe etnaviv authors 	lockdep_assert_held(&etnaviv_obj->lock);
131a8c21a54SThe etnaviv authors 	/* when we start tracking the pin count, then do something here */
132a8c21a54SThe etnaviv authors }
133a8c21a54SThe etnaviv authors 
1340e7f26e6SLucas Stach static int etnaviv_gem_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
135a8c21a54SThe etnaviv authors 		struct vm_area_struct *vma)
136a8c21a54SThe etnaviv authors {
137a8c21a54SThe etnaviv authors 	pgprot_t vm_page_prot;
138a8c21a54SThe etnaviv authors 
139a8c21a54SThe etnaviv authors 	vma->vm_flags &= ~VM_PFNMAP;
140a8c21a54SThe etnaviv authors 	vma->vm_flags |= VM_MIXEDMAP;
141a8c21a54SThe etnaviv authors 
142a8c21a54SThe etnaviv authors 	vm_page_prot = vm_get_page_prot(vma->vm_flags);
143a8c21a54SThe etnaviv authors 
144a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_WC) {
145a8c21a54SThe etnaviv authors 		vma->vm_page_prot = pgprot_writecombine(vm_page_prot);
146a8c21a54SThe etnaviv authors 	} else if (etnaviv_obj->flags & ETNA_BO_UNCACHED) {
147a8c21a54SThe etnaviv authors 		vma->vm_page_prot = pgprot_noncached(vm_page_prot);
148a8c21a54SThe etnaviv authors 	} else {
149a8c21a54SThe etnaviv authors 		/*
150a8c21a54SThe etnaviv authors 		 * Shunt off cached objs to shmem file so they have their own
151a8c21a54SThe etnaviv authors 		 * address_space (so unmap_mapping_range does what we want,
152a8c21a54SThe etnaviv authors 		 * in particular in the case of mmap'd dmabufs)
153a8c21a54SThe etnaviv authors 		 */
154a8c21a54SThe etnaviv authors 		fput(vma->vm_file);
1550e7f26e6SLucas Stach 		get_file(etnaviv_obj->base.filp);
156a8c21a54SThe etnaviv authors 		vma->vm_pgoff = 0;
1570e7f26e6SLucas Stach 		vma->vm_file  = etnaviv_obj->base.filp;
158a8c21a54SThe etnaviv authors 
159a8c21a54SThe etnaviv authors 		vma->vm_page_prot = vm_page_prot;
160a8c21a54SThe etnaviv authors 	}
161a8c21a54SThe etnaviv authors 
162a8c21a54SThe etnaviv authors 	return 0;
163a8c21a54SThe etnaviv authors }
164a8c21a54SThe etnaviv authors 
165a8c21a54SThe etnaviv authors int etnaviv_gem_mmap(struct file *filp, struct vm_area_struct *vma)
166a8c21a54SThe etnaviv authors {
167a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *obj;
168a8c21a54SThe etnaviv authors 	int ret;
169a8c21a54SThe etnaviv authors 
170a8c21a54SThe etnaviv authors 	ret = drm_gem_mmap(filp, vma);
171a8c21a54SThe etnaviv authors 	if (ret) {
172a8c21a54SThe etnaviv authors 		DBG("mmap failed: %d", ret);
173a8c21a54SThe etnaviv authors 		return ret;
174a8c21a54SThe etnaviv authors 	}
175a8c21a54SThe etnaviv authors 
176a8c21a54SThe etnaviv authors 	obj = to_etnaviv_bo(vma->vm_private_data);
177a10e2bdeSLucas Stach 	return obj->ops->mmap(obj, vma);
178a8c21a54SThe etnaviv authors }
179a8c21a54SThe etnaviv authors 
18011bac800SDave Jiang int etnaviv_gem_fault(struct vm_fault *vmf)
181a8c21a54SThe etnaviv authors {
18211bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
183a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj = vma->vm_private_data;
184a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
185a8c21a54SThe etnaviv authors 	struct page **pages, *page;
186a8c21a54SThe etnaviv authors 	pgoff_t pgoff;
187a8c21a54SThe etnaviv authors 	int ret;
188a8c21a54SThe etnaviv authors 
189a8c21a54SThe etnaviv authors 	/*
190a8c21a54SThe etnaviv authors 	 * Make sure we don't parallel update on a fault, nor move or remove
191a8c21a54SThe etnaviv authors 	 * something from beneath our feet.  Note that vm_insert_page() is
192a8c21a54SThe etnaviv authors 	 * specifically coded to take care of this, so we don't have to.
193a8c21a54SThe etnaviv authors 	 */
194a8c21a54SThe etnaviv authors 	ret = mutex_lock_interruptible(&etnaviv_obj->lock);
195a8c21a54SThe etnaviv authors 	if (ret)
196a8c21a54SThe etnaviv authors 		goto out;
197a8c21a54SThe etnaviv authors 
198a8c21a54SThe etnaviv authors 	/* make sure we have pages attached now */
199a8c21a54SThe etnaviv authors 	pages = etnaviv_gem_get_pages(etnaviv_obj);
200a8c21a54SThe etnaviv authors 	mutex_unlock(&etnaviv_obj->lock);
201a8c21a54SThe etnaviv authors 
202a8c21a54SThe etnaviv authors 	if (IS_ERR(pages)) {
203a8c21a54SThe etnaviv authors 		ret = PTR_ERR(pages);
204a8c21a54SThe etnaviv authors 		goto out;
205a8c21a54SThe etnaviv authors 	}
206a8c21a54SThe etnaviv authors 
207a8c21a54SThe etnaviv authors 	/* We don't use vmf->pgoff since that has the fake offset: */
2081a29d85eSJan Kara 	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
209a8c21a54SThe etnaviv authors 
210a8c21a54SThe etnaviv authors 	page = pages[pgoff];
211a8c21a54SThe etnaviv authors 
2121a29d85eSJan Kara 	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
213a8c21a54SThe etnaviv authors 	     page_to_pfn(page), page_to_pfn(page) << PAGE_SHIFT);
214a8c21a54SThe etnaviv authors 
2151a29d85eSJan Kara 	ret = vm_insert_page(vma, vmf->address, page);
216a8c21a54SThe etnaviv authors 
217a8c21a54SThe etnaviv authors out:
218a8c21a54SThe etnaviv authors 	switch (ret) {
219a8c21a54SThe etnaviv authors 	case -EAGAIN:
220a8c21a54SThe etnaviv authors 	case 0:
221a8c21a54SThe etnaviv authors 	case -ERESTARTSYS:
222a8c21a54SThe etnaviv authors 	case -EINTR:
223a8c21a54SThe etnaviv authors 	case -EBUSY:
224a8c21a54SThe etnaviv authors 		/*
225a8c21a54SThe etnaviv authors 		 * EBUSY is ok: this just means that another thread
226a8c21a54SThe etnaviv authors 		 * already did the job.
227a8c21a54SThe etnaviv authors 		 */
228a8c21a54SThe etnaviv authors 		return VM_FAULT_NOPAGE;
229a8c21a54SThe etnaviv authors 	case -ENOMEM:
230a8c21a54SThe etnaviv authors 		return VM_FAULT_OOM;
231a8c21a54SThe etnaviv authors 	default:
232a8c21a54SThe etnaviv authors 		return VM_FAULT_SIGBUS;
233a8c21a54SThe etnaviv authors 	}
234a8c21a54SThe etnaviv authors }
235a8c21a54SThe etnaviv authors 
236a8c21a54SThe etnaviv authors int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset)
237a8c21a54SThe etnaviv authors {
238a8c21a54SThe etnaviv authors 	int ret;
239a8c21a54SThe etnaviv authors 
240a8c21a54SThe etnaviv authors 	/* Make it mmapable */
241a8c21a54SThe etnaviv authors 	ret = drm_gem_create_mmap_offset(obj);
242a8c21a54SThe etnaviv authors 	if (ret)
243a8c21a54SThe etnaviv authors 		dev_err(obj->dev->dev, "could not allocate mmap offset\n");
244a8c21a54SThe etnaviv authors 	else
245a8c21a54SThe etnaviv authors 		*offset = drm_vma_node_offset_addr(&obj->vma_node);
246a8c21a54SThe etnaviv authors 
247a8c21a54SThe etnaviv authors 	return ret;
248a8c21a54SThe etnaviv authors }
249a8c21a54SThe etnaviv authors 
250a8c21a54SThe etnaviv authors static struct etnaviv_vram_mapping *
251a8c21a54SThe etnaviv authors etnaviv_gem_get_vram_mapping(struct etnaviv_gem_object *obj,
252a8c21a54SThe etnaviv authors 			     struct etnaviv_iommu *mmu)
253a8c21a54SThe etnaviv authors {
254a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping;
255a8c21a54SThe etnaviv authors 
256a8c21a54SThe etnaviv authors 	list_for_each_entry(mapping, &obj->vram_list, obj_node) {
257a8c21a54SThe etnaviv authors 		if (mapping->mmu == mmu)
258a8c21a54SThe etnaviv authors 			return mapping;
259a8c21a54SThe etnaviv authors 	}
260a8c21a54SThe etnaviv authors 
261a8c21a54SThe etnaviv authors 	return NULL;
262a8c21a54SThe etnaviv authors }
263a8c21a54SThe etnaviv authors 
264b6325f40SRussell King void etnaviv_gem_mapping_reference(struct etnaviv_vram_mapping *mapping)
265b6325f40SRussell King {
266b6325f40SRussell King 	struct etnaviv_gem_object *etnaviv_obj = mapping->object;
267b6325f40SRussell King 
26823d1dd03SCihangir Akturk 	drm_gem_object_get(&etnaviv_obj->base);
269b6325f40SRussell King 
270b6325f40SRussell King 	mutex_lock(&etnaviv_obj->lock);
271b6325f40SRussell King 	WARN_ON(mapping->use == 0);
272b6325f40SRussell King 	mapping->use += 1;
273b6325f40SRussell King 	mutex_unlock(&etnaviv_obj->lock);
274b6325f40SRussell King }
275b6325f40SRussell King 
276b6325f40SRussell King void etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping)
277b6325f40SRussell King {
278b6325f40SRussell King 	struct etnaviv_gem_object *etnaviv_obj = mapping->object;
279b6325f40SRussell King 
280b6325f40SRussell King 	mutex_lock(&etnaviv_obj->lock);
281b6325f40SRussell King 	WARN_ON(mapping->use == 0);
282b6325f40SRussell King 	mapping->use -= 1;
283b6325f40SRussell King 	mutex_unlock(&etnaviv_obj->lock);
284b6325f40SRussell King 
28523d1dd03SCihangir Akturk 	drm_gem_object_put_unlocked(&etnaviv_obj->base);
286b6325f40SRussell King }
287b6325f40SRussell King 
288b6325f40SRussell King struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
289b6325f40SRussell King 	struct drm_gem_object *obj, struct etnaviv_gpu *gpu)
290a8c21a54SThe etnaviv authors {
291a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
292a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping;
293a8c21a54SThe etnaviv authors 	struct page **pages;
294a8c21a54SThe etnaviv authors 	int ret = 0;
295a8c21a54SThe etnaviv authors 
296a8c21a54SThe etnaviv authors 	mutex_lock(&etnaviv_obj->lock);
297a8c21a54SThe etnaviv authors 	mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, gpu->mmu);
298a8c21a54SThe etnaviv authors 	if (mapping) {
299a8c21a54SThe etnaviv authors 		/*
300a8c21a54SThe etnaviv authors 		 * Holding the object lock prevents the use count changing
301a8c21a54SThe etnaviv authors 		 * beneath us.  If the use count is zero, the MMU might be
302a8c21a54SThe etnaviv authors 		 * reaping this object, so take the lock and re-check that
303a8c21a54SThe etnaviv authors 		 * the MMU owns this mapping to close this race.
304a8c21a54SThe etnaviv authors 		 */
305a8c21a54SThe etnaviv authors 		if (mapping->use == 0) {
306a8c21a54SThe etnaviv authors 			mutex_lock(&gpu->mmu->lock);
307a8c21a54SThe etnaviv authors 			if (mapping->mmu == gpu->mmu)
308a8c21a54SThe etnaviv authors 				mapping->use += 1;
309a8c21a54SThe etnaviv authors 			else
310a8c21a54SThe etnaviv authors 				mapping = NULL;
311a8c21a54SThe etnaviv authors 			mutex_unlock(&gpu->mmu->lock);
312a8c21a54SThe etnaviv authors 			if (mapping)
313a8c21a54SThe etnaviv authors 				goto out;
314a8c21a54SThe etnaviv authors 		} else {
315a8c21a54SThe etnaviv authors 			mapping->use += 1;
316a8c21a54SThe etnaviv authors 			goto out;
317a8c21a54SThe etnaviv authors 		}
318a8c21a54SThe etnaviv authors 	}
319a8c21a54SThe etnaviv authors 
320a8c21a54SThe etnaviv authors 	pages = etnaviv_gem_get_pages(etnaviv_obj);
321a8c21a54SThe etnaviv authors 	if (IS_ERR(pages)) {
322a8c21a54SThe etnaviv authors 		ret = PTR_ERR(pages);
323a8c21a54SThe etnaviv authors 		goto out;
324a8c21a54SThe etnaviv authors 	}
325a8c21a54SThe etnaviv authors 
326a8c21a54SThe etnaviv authors 	/*
327a8c21a54SThe etnaviv authors 	 * See if we have a reaped vram mapping we can re-use before
328a8c21a54SThe etnaviv authors 	 * allocating a fresh mapping.
329a8c21a54SThe etnaviv authors 	 */
330a8c21a54SThe etnaviv authors 	mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, NULL);
331a8c21a54SThe etnaviv authors 	if (!mapping) {
332a8c21a54SThe etnaviv authors 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
333ed94add0SDan Carpenter 		if (!mapping) {
334ed94add0SDan Carpenter 			ret = -ENOMEM;
335ed94add0SDan Carpenter 			goto out;
336ed94add0SDan Carpenter 		}
337a8c21a54SThe etnaviv authors 
338a8c21a54SThe etnaviv authors 		INIT_LIST_HEAD(&mapping->scan_node);
339a8c21a54SThe etnaviv authors 		mapping->object = etnaviv_obj;
340a8c21a54SThe etnaviv authors 	} else {
341a8c21a54SThe etnaviv authors 		list_del(&mapping->obj_node);
342a8c21a54SThe etnaviv authors 	}
343a8c21a54SThe etnaviv authors 
344a8c21a54SThe etnaviv authors 	mapping->mmu = gpu->mmu;
345a8c21a54SThe etnaviv authors 	mapping->use = 1;
346a8c21a54SThe etnaviv authors 
347a8c21a54SThe etnaviv authors 	ret = etnaviv_iommu_map_gem(gpu->mmu, etnaviv_obj, gpu->memory_base,
348a8c21a54SThe etnaviv authors 				    mapping);
349a8c21a54SThe etnaviv authors 	if (ret < 0)
350a8c21a54SThe etnaviv authors 		kfree(mapping);
351a8c21a54SThe etnaviv authors 	else
352a8c21a54SThe etnaviv authors 		list_add_tail(&mapping->obj_node, &etnaviv_obj->vram_list);
353a8c21a54SThe etnaviv authors 
354a8c21a54SThe etnaviv authors out:
355a8c21a54SThe etnaviv authors 	mutex_unlock(&etnaviv_obj->lock);
356a8c21a54SThe etnaviv authors 
357b6325f40SRussell King 	if (ret)
358b6325f40SRussell King 		return ERR_PTR(ret);
359b6325f40SRussell King 
360a8c21a54SThe etnaviv authors 	/* Take a reference on the object */
36123d1dd03SCihangir Akturk 	drm_gem_object_get(obj);
362b6325f40SRussell King 	return mapping;
363a8c21a54SThe etnaviv authors }
364a8c21a54SThe etnaviv authors 
365ce3088fdSLucas Stach void *etnaviv_gem_vmap(struct drm_gem_object *obj)
366a8c21a54SThe etnaviv authors {
367a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
368a8c21a54SThe etnaviv authors 
369a0a5ab3eSLucas Stach 	if (etnaviv_obj->vaddr)
370a0a5ab3eSLucas Stach 		return etnaviv_obj->vaddr;
371a0a5ab3eSLucas Stach 
372a8c21a54SThe etnaviv authors 	mutex_lock(&etnaviv_obj->lock);
373a0a5ab3eSLucas Stach 	/*
374a0a5ab3eSLucas Stach 	 * Need to check again, as we might have raced with another thread
375a0a5ab3eSLucas Stach 	 * while waiting for the mutex.
376a0a5ab3eSLucas Stach 	 */
377a0a5ab3eSLucas Stach 	if (!etnaviv_obj->vaddr)
378a0a5ab3eSLucas Stach 		etnaviv_obj->vaddr = etnaviv_obj->ops->vmap(etnaviv_obj);
379a8c21a54SThe etnaviv authors 	mutex_unlock(&etnaviv_obj->lock);
380a8c21a54SThe etnaviv authors 
381a8c21a54SThe etnaviv authors 	return etnaviv_obj->vaddr;
382a8c21a54SThe etnaviv authors }
383a8c21a54SThe etnaviv authors 
384a0a5ab3eSLucas Stach static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj)
385a0a5ab3eSLucas Stach {
386a0a5ab3eSLucas Stach 	struct page **pages;
387a0a5ab3eSLucas Stach 
388a0a5ab3eSLucas Stach 	lockdep_assert_held(&obj->lock);
389a0a5ab3eSLucas Stach 
390a0a5ab3eSLucas Stach 	pages = etnaviv_gem_get_pages(obj);
391a0a5ab3eSLucas Stach 	if (IS_ERR(pages))
392a0a5ab3eSLucas Stach 		return NULL;
393a0a5ab3eSLucas Stach 
394a0a5ab3eSLucas Stach 	return vmap(pages, obj->base.size >> PAGE_SHIFT,
395a0a5ab3eSLucas Stach 			VM_MAP, pgprot_writecombine(PAGE_KERNEL));
396a0a5ab3eSLucas Stach }
397a0a5ab3eSLucas Stach 
398a8c21a54SThe etnaviv authors static inline enum dma_data_direction etnaviv_op_to_dma_dir(u32 op)
399a8c21a54SThe etnaviv authors {
400a8c21a54SThe etnaviv authors 	if (op & ETNA_PREP_READ)
401a8c21a54SThe etnaviv authors 		return DMA_FROM_DEVICE;
402a8c21a54SThe etnaviv authors 	else if (op & ETNA_PREP_WRITE)
403a8c21a54SThe etnaviv authors 		return DMA_TO_DEVICE;
404a8c21a54SThe etnaviv authors 	else
405a8c21a54SThe etnaviv authors 		return DMA_BIDIRECTIONAL;
406a8c21a54SThe etnaviv authors }
407a8c21a54SThe etnaviv authors 
408a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
409a8c21a54SThe etnaviv authors 		struct timespec *timeout)
410a8c21a54SThe etnaviv authors {
411a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
412a8c21a54SThe etnaviv authors 	struct drm_device *dev = obj->dev;
413a8c21a54SThe etnaviv authors 	bool write = !!(op & ETNA_PREP_WRITE);
41446a269daSLucas Stach 	int ret;
415a8c21a54SThe etnaviv authors 
4168cc47b3eSLucas Stach 	if (!etnaviv_obj->sgt) {
4178cc47b3eSLucas Stach 		void *ret;
4188cc47b3eSLucas Stach 
4198cc47b3eSLucas Stach 		mutex_lock(&etnaviv_obj->lock);
4208cc47b3eSLucas Stach 		ret = etnaviv_gem_get_pages(etnaviv_obj);
4218cc47b3eSLucas Stach 		mutex_unlock(&etnaviv_obj->lock);
4228cc47b3eSLucas Stach 		if (IS_ERR(ret))
4238cc47b3eSLucas Stach 			return PTR_ERR(ret);
4248cc47b3eSLucas Stach 	}
4258cc47b3eSLucas Stach 
42646a269daSLucas Stach 	if (op & ETNA_PREP_NOSYNC) {
42746a269daSLucas Stach 		if (!reservation_object_test_signaled_rcu(etnaviv_obj->resv,
42846a269daSLucas Stach 							  write))
42946a269daSLucas Stach 			return -EBUSY;
43046a269daSLucas Stach 	} else {
43146a269daSLucas Stach 		unsigned long remain = etnaviv_timeout_to_jiffies(timeout);
43246a269daSLucas Stach 
43346a269daSLucas Stach 		ret = reservation_object_wait_timeout_rcu(etnaviv_obj->resv,
434a8c21a54SThe etnaviv authors 							  write, true, remain);
43546a269daSLucas Stach 		if (ret <= 0)
43646a269daSLucas Stach 			return ret == 0 ? -ETIMEDOUT : ret;
43746a269daSLucas Stach 	}
438a8c21a54SThe etnaviv authors 
439a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHED) {
440a8c21a54SThe etnaviv authors 		dma_sync_sg_for_cpu(dev->dev, etnaviv_obj->sgt->sgl,
441a8c21a54SThe etnaviv authors 				    etnaviv_obj->sgt->nents,
442a8c21a54SThe etnaviv authors 				    etnaviv_op_to_dma_dir(op));
443a8c21a54SThe etnaviv authors 		etnaviv_obj->last_cpu_prep_op = op;
444a8c21a54SThe etnaviv authors 	}
445a8c21a54SThe etnaviv authors 
446a8c21a54SThe etnaviv authors 	return 0;
447a8c21a54SThe etnaviv authors }
448a8c21a54SThe etnaviv authors 
449a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_fini(struct drm_gem_object *obj)
450a8c21a54SThe etnaviv authors {
451a8c21a54SThe etnaviv authors 	struct drm_device *dev = obj->dev;
452a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
453a8c21a54SThe etnaviv authors 
454a8c21a54SThe etnaviv authors 	if (etnaviv_obj->flags & ETNA_BO_CACHED) {
455a8c21a54SThe etnaviv authors 		/* fini without a prep is almost certainly a userspace error */
456a8c21a54SThe etnaviv authors 		WARN_ON(etnaviv_obj->last_cpu_prep_op == 0);
457a8c21a54SThe etnaviv authors 		dma_sync_sg_for_device(dev->dev, etnaviv_obj->sgt->sgl,
458a8c21a54SThe etnaviv authors 			etnaviv_obj->sgt->nents,
459a8c21a54SThe etnaviv authors 			etnaviv_op_to_dma_dir(etnaviv_obj->last_cpu_prep_op));
460a8c21a54SThe etnaviv authors 		etnaviv_obj->last_cpu_prep_op = 0;
461a8c21a54SThe etnaviv authors 	}
462a8c21a54SThe etnaviv authors 
463a8c21a54SThe etnaviv authors 	return 0;
464a8c21a54SThe etnaviv authors }
465a8c21a54SThe etnaviv authors 
466a8c21a54SThe etnaviv authors int etnaviv_gem_wait_bo(struct etnaviv_gpu *gpu, struct drm_gem_object *obj,
467a8c21a54SThe etnaviv authors 	struct timespec *timeout)
468a8c21a54SThe etnaviv authors {
469a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
470a8c21a54SThe etnaviv authors 
471a8c21a54SThe etnaviv authors 	return etnaviv_gpu_wait_obj_inactive(gpu, etnaviv_obj, timeout);
472a8c21a54SThe etnaviv authors }
473a8c21a54SThe etnaviv authors 
474a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
475f54d1867SChris Wilson static void etnaviv_gem_describe_fence(struct dma_fence *fence,
476a8c21a54SThe etnaviv authors 	const char *type, struct seq_file *m)
477a8c21a54SThe etnaviv authors {
478f54d1867SChris Wilson 	if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
479a8c21a54SThe etnaviv authors 		seq_printf(m, "\t%9s: %s %s seq %u\n",
480a8c21a54SThe etnaviv authors 			   type,
481a8c21a54SThe etnaviv authors 			   fence->ops->get_driver_name(fence),
482a8c21a54SThe etnaviv authors 			   fence->ops->get_timeline_name(fence),
483a8c21a54SThe etnaviv authors 			   fence->seqno);
484a8c21a54SThe etnaviv authors }
485a8c21a54SThe etnaviv authors 
486a8c21a54SThe etnaviv authors static void etnaviv_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
487a8c21a54SThe etnaviv authors {
488a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
489a8c21a54SThe etnaviv authors 	struct reservation_object *robj = etnaviv_obj->resv;
490a8c21a54SThe etnaviv authors 	struct reservation_object_list *fobj;
491f54d1867SChris Wilson 	struct dma_fence *fence;
492a8c21a54SThe etnaviv authors 	unsigned long off = drm_vma_node_start(&obj->vma_node);
493a8c21a54SThe etnaviv authors 
494a8c21a54SThe etnaviv authors 	seq_printf(m, "%08x: %c %2d (%2d) %08lx %p %zd\n",
495a8c21a54SThe etnaviv authors 			etnaviv_obj->flags, is_active(etnaviv_obj) ? 'A' : 'I',
4962c935bc5SPeter Zijlstra 			obj->name, kref_read(&obj->refcount),
497a8c21a54SThe etnaviv authors 			off, etnaviv_obj->vaddr, obj->size);
498a8c21a54SThe etnaviv authors 
499a8c21a54SThe etnaviv authors 	rcu_read_lock();
500a8c21a54SThe etnaviv authors 	fobj = rcu_dereference(robj->fence);
501a8c21a54SThe etnaviv authors 	if (fobj) {
502a8c21a54SThe etnaviv authors 		unsigned int i, shared_count = fobj->shared_count;
503a8c21a54SThe etnaviv authors 
504a8c21a54SThe etnaviv authors 		for (i = 0; i < shared_count; i++) {
505a8c21a54SThe etnaviv authors 			fence = rcu_dereference(fobj->shared[i]);
506a8c21a54SThe etnaviv authors 			etnaviv_gem_describe_fence(fence, "Shared", m);
507a8c21a54SThe etnaviv authors 		}
508a8c21a54SThe etnaviv authors 	}
509a8c21a54SThe etnaviv authors 
510a8c21a54SThe etnaviv authors 	fence = rcu_dereference(robj->fence_excl);
511a8c21a54SThe etnaviv authors 	if (fence)
512a8c21a54SThe etnaviv authors 		etnaviv_gem_describe_fence(fence, "Exclusive", m);
513a8c21a54SThe etnaviv authors 	rcu_read_unlock();
514a8c21a54SThe etnaviv authors }
515a8c21a54SThe etnaviv authors 
516a8c21a54SThe etnaviv authors void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
517a8c21a54SThe etnaviv authors 	struct seq_file *m)
518a8c21a54SThe etnaviv authors {
519a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj;
520a8c21a54SThe etnaviv authors 	int count = 0;
521a8c21a54SThe etnaviv authors 	size_t size = 0;
522a8c21a54SThe etnaviv authors 
523a8c21a54SThe etnaviv authors 	mutex_lock(&priv->gem_lock);
524a8c21a54SThe etnaviv authors 	list_for_each_entry(etnaviv_obj, &priv->gem_list, gem_node) {
525a8c21a54SThe etnaviv authors 		struct drm_gem_object *obj = &etnaviv_obj->base;
526a8c21a54SThe etnaviv authors 
527a8c21a54SThe etnaviv authors 		seq_puts(m, "   ");
528a8c21a54SThe etnaviv authors 		etnaviv_gem_describe(obj, m);
529a8c21a54SThe etnaviv authors 		count++;
530a8c21a54SThe etnaviv authors 		size += obj->size;
531a8c21a54SThe etnaviv authors 	}
532a8c21a54SThe etnaviv authors 	mutex_unlock(&priv->gem_lock);
533a8c21a54SThe etnaviv authors 
534a8c21a54SThe etnaviv authors 	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
535a8c21a54SThe etnaviv authors }
536a8c21a54SThe etnaviv authors #endif
537a8c21a54SThe etnaviv authors 
538a8c21a54SThe etnaviv authors static void etnaviv_gem_shmem_release(struct etnaviv_gem_object *etnaviv_obj)
539a8c21a54SThe etnaviv authors {
540a8c21a54SThe etnaviv authors 	vunmap(etnaviv_obj->vaddr);
541a8c21a54SThe etnaviv authors 	put_pages(etnaviv_obj);
542a8c21a54SThe etnaviv authors }
543a8c21a54SThe etnaviv authors 
544a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_shmem_ops = {
545a8c21a54SThe etnaviv authors 	.get_pages = etnaviv_gem_shmem_get_pages,
546a8c21a54SThe etnaviv authors 	.release = etnaviv_gem_shmem_release,
547a0a5ab3eSLucas Stach 	.vmap = etnaviv_gem_vmap_impl,
548a10e2bdeSLucas Stach 	.mmap = etnaviv_gem_mmap_obj,
549a8c21a54SThe etnaviv authors };
550a8c21a54SThe etnaviv authors 
551a8c21a54SThe etnaviv authors void etnaviv_gem_free_object(struct drm_gem_object *obj)
552a8c21a54SThe etnaviv authors {
553a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
55451841752SLucas Stach 	struct etnaviv_drm_private *priv = obj->dev->dev_private;
555a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping, *tmp;
556a8c21a54SThe etnaviv authors 
557a8c21a54SThe etnaviv authors 	/* object should not be active */
558a8c21a54SThe etnaviv authors 	WARN_ON(is_active(etnaviv_obj));
559a8c21a54SThe etnaviv authors 
56051841752SLucas Stach 	mutex_lock(&priv->gem_lock);
561a8c21a54SThe etnaviv authors 	list_del(&etnaviv_obj->gem_node);
56251841752SLucas Stach 	mutex_unlock(&priv->gem_lock);
563a8c21a54SThe etnaviv authors 
564a8c21a54SThe etnaviv authors 	list_for_each_entry_safe(mapping, tmp, &etnaviv_obj->vram_list,
565a8c21a54SThe etnaviv authors 				 obj_node) {
566a8c21a54SThe etnaviv authors 		struct etnaviv_iommu *mmu = mapping->mmu;
567a8c21a54SThe etnaviv authors 
568a8c21a54SThe etnaviv authors 		WARN_ON(mapping->use);
569a8c21a54SThe etnaviv authors 
570a8c21a54SThe etnaviv authors 		if (mmu)
571a8c21a54SThe etnaviv authors 			etnaviv_iommu_unmap_gem(mmu, mapping);
572a8c21a54SThe etnaviv authors 
573a8c21a54SThe etnaviv authors 		list_del(&mapping->obj_node);
574a8c21a54SThe etnaviv authors 		kfree(mapping);
575a8c21a54SThe etnaviv authors 	}
576a8c21a54SThe etnaviv authors 
577a8c21a54SThe etnaviv authors 	drm_gem_free_mmap_offset(obj);
578a8c21a54SThe etnaviv authors 	etnaviv_obj->ops->release(etnaviv_obj);
579a8c21a54SThe etnaviv authors 	if (etnaviv_obj->resv == &etnaviv_obj->_resv)
580a8c21a54SThe etnaviv authors 		reservation_object_fini(&etnaviv_obj->_resv);
581a8c21a54SThe etnaviv authors 	drm_gem_object_release(obj);
582a8c21a54SThe etnaviv authors 
583a8c21a54SThe etnaviv authors 	kfree(etnaviv_obj);
584a8c21a54SThe etnaviv authors }
585a8c21a54SThe etnaviv authors 
586a8c21a54SThe etnaviv authors int etnaviv_gem_obj_add(struct drm_device *dev, struct drm_gem_object *obj)
587a8c21a54SThe etnaviv authors {
588a8c21a54SThe etnaviv authors 	struct etnaviv_drm_private *priv = dev->dev_private;
589a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
590a8c21a54SThe etnaviv authors 
591a8c21a54SThe etnaviv authors 	mutex_lock(&priv->gem_lock);
592a8c21a54SThe etnaviv authors 	list_add_tail(&etnaviv_obj->gem_node, &priv->gem_list);
593a8c21a54SThe etnaviv authors 	mutex_unlock(&priv->gem_lock);
594a8c21a54SThe etnaviv authors 
595a8c21a54SThe etnaviv authors 	return 0;
596a8c21a54SThe etnaviv authors }
597a8c21a54SThe etnaviv authors 
598a8c21a54SThe etnaviv authors static int etnaviv_gem_new_impl(struct drm_device *dev, u32 size, u32 flags,
599a8c21a54SThe etnaviv authors 	struct reservation_object *robj, const struct etnaviv_gem_ops *ops,
600a8c21a54SThe etnaviv authors 	struct drm_gem_object **obj)
601a8c21a54SThe etnaviv authors {
602a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj;
603a8c21a54SThe etnaviv authors 	unsigned sz = sizeof(*etnaviv_obj);
604a8c21a54SThe etnaviv authors 	bool valid = true;
605a8c21a54SThe etnaviv authors 
606a8c21a54SThe etnaviv authors 	/* validate flags */
607a8c21a54SThe etnaviv authors 	switch (flags & ETNA_BO_CACHE_MASK) {
608a8c21a54SThe etnaviv authors 	case ETNA_BO_UNCACHED:
609a8c21a54SThe etnaviv authors 	case ETNA_BO_CACHED:
610a8c21a54SThe etnaviv authors 	case ETNA_BO_WC:
611a8c21a54SThe etnaviv authors 		break;
612a8c21a54SThe etnaviv authors 	default:
613a8c21a54SThe etnaviv authors 		valid = false;
614a8c21a54SThe etnaviv authors 	}
615a8c21a54SThe etnaviv authors 
616a8c21a54SThe etnaviv authors 	if (!valid) {
617a8c21a54SThe etnaviv authors 		dev_err(dev->dev, "invalid cache flag: %x\n",
618a8c21a54SThe etnaviv authors 			(flags & ETNA_BO_CACHE_MASK));
619a8c21a54SThe etnaviv authors 		return -EINVAL;
620a8c21a54SThe etnaviv authors 	}
621a8c21a54SThe etnaviv authors 
622a8c21a54SThe etnaviv authors 	etnaviv_obj = kzalloc(sz, GFP_KERNEL);
623a8c21a54SThe etnaviv authors 	if (!etnaviv_obj)
624a8c21a54SThe etnaviv authors 		return -ENOMEM;
625a8c21a54SThe etnaviv authors 
626a8c21a54SThe etnaviv authors 	etnaviv_obj->flags = flags;
627a8c21a54SThe etnaviv authors 	etnaviv_obj->ops = ops;
628a8c21a54SThe etnaviv authors 	if (robj) {
629a8c21a54SThe etnaviv authors 		etnaviv_obj->resv = robj;
630a8c21a54SThe etnaviv authors 	} else {
631a8c21a54SThe etnaviv authors 		etnaviv_obj->resv = &etnaviv_obj->_resv;
632a8c21a54SThe etnaviv authors 		reservation_object_init(&etnaviv_obj->_resv);
633a8c21a54SThe etnaviv authors 	}
634a8c21a54SThe etnaviv authors 
635a8c21a54SThe etnaviv authors 	mutex_init(&etnaviv_obj->lock);
636a8c21a54SThe etnaviv authors 	INIT_LIST_HEAD(&etnaviv_obj->vram_list);
637a8c21a54SThe etnaviv authors 
638a8c21a54SThe etnaviv authors 	*obj = &etnaviv_obj->base;
639a8c21a54SThe etnaviv authors 
640a8c21a54SThe etnaviv authors 	return 0;
641a8c21a54SThe etnaviv authors }
642a8c21a54SThe etnaviv authors 
643a8c21a54SThe etnaviv authors static struct drm_gem_object *__etnaviv_gem_new(struct drm_device *dev,
644a8c21a54SThe etnaviv authors 		u32 size, u32 flags)
645a8c21a54SThe etnaviv authors {
646a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj = NULL;
647a8c21a54SThe etnaviv authors 	int ret;
648a8c21a54SThe etnaviv authors 
649a8c21a54SThe etnaviv authors 	size = PAGE_ALIGN(size);
650a8c21a54SThe etnaviv authors 
651a8c21a54SThe etnaviv authors 	ret = etnaviv_gem_new_impl(dev, size, flags, NULL,
652a8c21a54SThe etnaviv authors 				   &etnaviv_gem_shmem_ops, &obj);
653a8c21a54SThe etnaviv authors 	if (ret)
654a8c21a54SThe etnaviv authors 		goto fail;
655a8c21a54SThe etnaviv authors 
656a8c21a54SThe etnaviv authors 	ret = drm_gem_object_init(dev, obj, size);
657a8c21a54SThe etnaviv authors 	if (ret == 0) {
658a8c21a54SThe etnaviv authors 		struct address_space *mapping;
659a8c21a54SThe etnaviv authors 
660a8c21a54SThe etnaviv authors 		/*
661a8c21a54SThe etnaviv authors 		 * Our buffers are kept pinned, so allocating them
662a8c21a54SThe etnaviv authors 		 * from the MOVABLE zone is a really bad idea, and
663a8c21a54SThe etnaviv authors 		 * conflicts with CMA.  See coments above new_inode()
664a8c21a54SThe etnaviv authors 		 * why this is required _and_ expected if you're
665a8c21a54SThe etnaviv authors 		 * going to pin these pages.
666a8c21a54SThe etnaviv authors 		 */
66793c76a3dSAl Viro 		mapping = obj->filp->f_mapping;
6686cbf0400SLucas Stach 		mapping_set_gfp_mask(mapping, GFP_HIGHUSER |
66965375b87SLucas Stach 				     __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
670a8c21a54SThe etnaviv authors 	}
671a8c21a54SThe etnaviv authors 
672a8c21a54SThe etnaviv authors 	if (ret)
673a8c21a54SThe etnaviv authors 		goto fail;
674a8c21a54SThe etnaviv authors 
675a8c21a54SThe etnaviv authors 	return obj;
676a8c21a54SThe etnaviv authors 
677a8c21a54SThe etnaviv authors fail:
67823d1dd03SCihangir Akturk 	drm_gem_object_put_unlocked(obj);
679a8c21a54SThe etnaviv authors 	return ERR_PTR(ret);
680a8c21a54SThe etnaviv authors }
681a8c21a54SThe etnaviv authors 
682a8c21a54SThe etnaviv authors /* convenience method to construct a GEM buffer object, and userspace handle */
683a8c21a54SThe etnaviv authors int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
684a8c21a54SThe etnaviv authors 		u32 size, u32 flags, u32 *handle)
685a8c21a54SThe etnaviv authors {
686a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj;
687a8c21a54SThe etnaviv authors 	int ret;
688a8c21a54SThe etnaviv authors 
689a8c21a54SThe etnaviv authors 	obj = __etnaviv_gem_new(dev, size, flags);
690a8c21a54SThe etnaviv authors 	if (IS_ERR(obj))
691a8c21a54SThe etnaviv authors 		return PTR_ERR(obj);
692a8c21a54SThe etnaviv authors 
693a8c21a54SThe etnaviv authors 	ret = etnaviv_gem_obj_add(dev, obj);
694a8c21a54SThe etnaviv authors 	if (ret < 0) {
69523d1dd03SCihangir Akturk 		drm_gem_object_put_unlocked(obj);
696a8c21a54SThe etnaviv authors 		return ret;
697a8c21a54SThe etnaviv authors 	}
698a8c21a54SThe etnaviv authors 
699a8c21a54SThe etnaviv authors 	ret = drm_gem_handle_create(file, obj, handle);
700a8c21a54SThe etnaviv authors 
701a8c21a54SThe etnaviv authors 	/* drop reference from allocate - handle holds it now */
70223d1dd03SCihangir Akturk 	drm_gem_object_put_unlocked(obj);
703a8c21a54SThe etnaviv authors 
704a8c21a54SThe etnaviv authors 	return ret;
705a8c21a54SThe etnaviv authors }
706a8c21a54SThe etnaviv authors 
707a8c21a54SThe etnaviv authors struct drm_gem_object *etnaviv_gem_new(struct drm_device *dev,
708a8c21a54SThe etnaviv authors 		u32 size, u32 flags)
709a8c21a54SThe etnaviv authors {
710a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj;
711a8c21a54SThe etnaviv authors 	int ret;
712a8c21a54SThe etnaviv authors 
713a8c21a54SThe etnaviv authors 	obj = __etnaviv_gem_new(dev, size, flags);
714a8c21a54SThe etnaviv authors 	if (IS_ERR(obj))
715a8c21a54SThe etnaviv authors 		return obj;
716a8c21a54SThe etnaviv authors 
717a8c21a54SThe etnaviv authors 	ret = etnaviv_gem_obj_add(dev, obj);
718a8c21a54SThe etnaviv authors 	if (ret < 0) {
71923d1dd03SCihangir Akturk 		drm_gem_object_put_unlocked(obj);
720a8c21a54SThe etnaviv authors 		return ERR_PTR(ret);
721a8c21a54SThe etnaviv authors 	}
722a8c21a54SThe etnaviv authors 
723a8c21a54SThe etnaviv authors 	return obj;
724a8c21a54SThe etnaviv authors }
725a8c21a54SThe etnaviv authors 
726a8c21a54SThe etnaviv authors int etnaviv_gem_new_private(struct drm_device *dev, size_t size, u32 flags,
727a8c21a54SThe etnaviv authors 	struct reservation_object *robj, const struct etnaviv_gem_ops *ops,
728a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object **res)
729a8c21a54SThe etnaviv authors {
730a8c21a54SThe etnaviv authors 	struct drm_gem_object *obj;
731a8c21a54SThe etnaviv authors 	int ret;
732a8c21a54SThe etnaviv authors 
733a8c21a54SThe etnaviv authors 	ret = etnaviv_gem_new_impl(dev, size, flags, robj, ops, &obj);
734a8c21a54SThe etnaviv authors 	if (ret)
735a8c21a54SThe etnaviv authors 		return ret;
736a8c21a54SThe etnaviv authors 
737a8c21a54SThe etnaviv authors 	drm_gem_private_object_init(dev, obj, size);
738a8c21a54SThe etnaviv authors 
739a8c21a54SThe etnaviv authors 	*res = to_etnaviv_bo(obj);
740a8c21a54SThe etnaviv authors 
741a8c21a54SThe etnaviv authors 	return 0;
742a8c21a54SThe etnaviv authors }
743a8c21a54SThe etnaviv authors 
744a8c21a54SThe etnaviv authors struct get_pages_work {
745a8c21a54SThe etnaviv authors 	struct work_struct work;
746a8c21a54SThe etnaviv authors 	struct mm_struct *mm;
747a8c21a54SThe etnaviv authors 	struct task_struct *task;
748a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj;
749a8c21a54SThe etnaviv authors };
750a8c21a54SThe etnaviv authors 
751a8c21a54SThe etnaviv authors static struct page **etnaviv_gem_userptr_do_get_pages(
752a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj, struct mm_struct *mm, struct task_struct *task)
753a8c21a54SThe etnaviv authors {
754a8c21a54SThe etnaviv authors 	int ret = 0, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT;
755a8c21a54SThe etnaviv authors 	struct page **pvec;
756a8c21a54SThe etnaviv authors 	uintptr_t ptr;
7579beae1eaSLorenzo Stoakes 	unsigned int flags = 0;
758a8c21a54SThe etnaviv authors 
7592098105eSMichal Hocko 	pvec = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
760a8c21a54SThe etnaviv authors 	if (!pvec)
761a8c21a54SThe etnaviv authors 		return ERR_PTR(-ENOMEM);
762a8c21a54SThe etnaviv authors 
7639beae1eaSLorenzo Stoakes 	if (!etnaviv_obj->userptr.ro)
7649beae1eaSLorenzo Stoakes 		flags |= FOLL_WRITE;
7659beae1eaSLorenzo Stoakes 
766a8c21a54SThe etnaviv authors 	pinned = 0;
767a8c21a54SThe etnaviv authors 	ptr = etnaviv_obj->userptr.ptr;
768a8c21a54SThe etnaviv authors 
769a8c21a54SThe etnaviv authors 	down_read(&mm->mmap_sem);
770a8c21a54SThe etnaviv authors 	while (pinned < npages) {
7711e987790SDave Hansen 		ret = get_user_pages_remote(task, mm, ptr, npages - pinned,
7725b56d49fSLorenzo Stoakes 					    flags, pvec + pinned, NULL, NULL);
773a8c21a54SThe etnaviv authors 		if (ret < 0)
774a8c21a54SThe etnaviv authors 			break;
775a8c21a54SThe etnaviv authors 
776a8c21a54SThe etnaviv authors 		ptr += ret * PAGE_SIZE;
777a8c21a54SThe etnaviv authors 		pinned += ret;
778a8c21a54SThe etnaviv authors 	}
779a8c21a54SThe etnaviv authors 	up_read(&mm->mmap_sem);
780a8c21a54SThe etnaviv authors 
781a8c21a54SThe etnaviv authors 	if (ret < 0) {
782c6f92f9fSMel Gorman 		release_pages(pvec, pinned);
7832098105eSMichal Hocko 		kvfree(pvec);
784a8c21a54SThe etnaviv authors 		return ERR_PTR(ret);
785a8c21a54SThe etnaviv authors 	}
786a8c21a54SThe etnaviv authors 
787a8c21a54SThe etnaviv authors 	return pvec;
788a8c21a54SThe etnaviv authors }
789a8c21a54SThe etnaviv authors 
790a8c21a54SThe etnaviv authors static void __etnaviv_gem_userptr_get_pages(struct work_struct *_work)
791a8c21a54SThe etnaviv authors {
792a8c21a54SThe etnaviv authors 	struct get_pages_work *work = container_of(_work, typeof(*work), work);
793a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj = work->etnaviv_obj;
794a8c21a54SThe etnaviv authors 	struct page **pvec;
795a8c21a54SThe etnaviv authors 
796a8c21a54SThe etnaviv authors 	pvec = etnaviv_gem_userptr_do_get_pages(etnaviv_obj, work->mm, work->task);
797a8c21a54SThe etnaviv authors 
798a8c21a54SThe etnaviv authors 	mutex_lock(&etnaviv_obj->lock);
799a8c21a54SThe etnaviv authors 	if (IS_ERR(pvec)) {
800a8c21a54SThe etnaviv authors 		etnaviv_obj->userptr.work = ERR_CAST(pvec);
801a8c21a54SThe etnaviv authors 	} else {
802a8c21a54SThe etnaviv authors 		etnaviv_obj->userptr.work = NULL;
803a8c21a54SThe etnaviv authors 		etnaviv_obj->pages = pvec;
804a8c21a54SThe etnaviv authors 	}
805a8c21a54SThe etnaviv authors 
806a8c21a54SThe etnaviv authors 	mutex_unlock(&etnaviv_obj->lock);
80723d1dd03SCihangir Akturk 	drm_gem_object_put_unlocked(&etnaviv_obj->base);
808a8c21a54SThe etnaviv authors 
809a8c21a54SThe etnaviv authors 	mmput(work->mm);
810a8c21a54SThe etnaviv authors 	put_task_struct(work->task);
811a8c21a54SThe etnaviv authors 	kfree(work);
812a8c21a54SThe etnaviv authors }
813a8c21a54SThe etnaviv authors 
814a8c21a54SThe etnaviv authors static int etnaviv_gem_userptr_get_pages(struct etnaviv_gem_object *etnaviv_obj)
815a8c21a54SThe etnaviv authors {
816a8c21a54SThe etnaviv authors 	struct page **pvec = NULL;
817a8c21a54SThe etnaviv authors 	struct get_pages_work *work;
818a8c21a54SThe etnaviv authors 	struct mm_struct *mm;
819a8c21a54SThe etnaviv authors 	int ret, pinned, npages = etnaviv_obj->base.size >> PAGE_SHIFT;
820a8c21a54SThe etnaviv authors 
821a8c21a54SThe etnaviv authors 	if (etnaviv_obj->userptr.work) {
822a8c21a54SThe etnaviv authors 		if (IS_ERR(etnaviv_obj->userptr.work)) {
823a8c21a54SThe etnaviv authors 			ret = PTR_ERR(etnaviv_obj->userptr.work);
824a8c21a54SThe etnaviv authors 			etnaviv_obj->userptr.work = NULL;
825a8c21a54SThe etnaviv authors 		} else {
826a8c21a54SThe etnaviv authors 			ret = -EAGAIN;
827a8c21a54SThe etnaviv authors 		}
828a8c21a54SThe etnaviv authors 		return ret;
829a8c21a54SThe etnaviv authors 	}
830a8c21a54SThe etnaviv authors 
831a8c21a54SThe etnaviv authors 	mm = get_task_mm(etnaviv_obj->userptr.task);
832a8c21a54SThe etnaviv authors 	pinned = 0;
833a8c21a54SThe etnaviv authors 	if (mm == current->mm) {
8342098105eSMichal Hocko 		pvec = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
835a8c21a54SThe etnaviv authors 		if (!pvec) {
836a8c21a54SThe etnaviv authors 			mmput(mm);
837a8c21a54SThe etnaviv authors 			return -ENOMEM;
838a8c21a54SThe etnaviv authors 		}
839a8c21a54SThe etnaviv authors 
840a8c21a54SThe etnaviv authors 		pinned = __get_user_pages_fast(etnaviv_obj->userptr.ptr, npages,
841a8c21a54SThe etnaviv authors 					       !etnaviv_obj->userptr.ro, pvec);
842a8c21a54SThe etnaviv authors 		if (pinned < 0) {
8432098105eSMichal Hocko 			kvfree(pvec);
844a8c21a54SThe etnaviv authors 			mmput(mm);
845a8c21a54SThe etnaviv authors 			return pinned;
846a8c21a54SThe etnaviv authors 		}
847a8c21a54SThe etnaviv authors 
848a8c21a54SThe etnaviv authors 		if (pinned == npages) {
849a8c21a54SThe etnaviv authors 			etnaviv_obj->pages = pvec;
850a8c21a54SThe etnaviv authors 			mmput(mm);
851a8c21a54SThe etnaviv authors 			return 0;
852a8c21a54SThe etnaviv authors 		}
853a8c21a54SThe etnaviv authors 	}
854a8c21a54SThe etnaviv authors 
855c6f92f9fSMel Gorman 	release_pages(pvec, pinned);
8562098105eSMichal Hocko 	kvfree(pvec);
857a8c21a54SThe etnaviv authors 
858a8c21a54SThe etnaviv authors 	work = kmalloc(sizeof(*work), GFP_KERNEL);
859a8c21a54SThe etnaviv authors 	if (!work) {
860a8c21a54SThe etnaviv authors 		mmput(mm);
861a8c21a54SThe etnaviv authors 		return -ENOMEM;
862a8c21a54SThe etnaviv authors 	}
863a8c21a54SThe etnaviv authors 
864a8c21a54SThe etnaviv authors 	get_task_struct(current);
86523d1dd03SCihangir Akturk 	drm_gem_object_get(&etnaviv_obj->base);
866a8c21a54SThe etnaviv authors 
867a8c21a54SThe etnaviv authors 	work->mm = mm;
868a8c21a54SThe etnaviv authors 	work->task = current;
869a8c21a54SThe etnaviv authors 	work->etnaviv_obj = etnaviv_obj;
870a8c21a54SThe etnaviv authors 
871a8c21a54SThe etnaviv authors 	etnaviv_obj->userptr.work = &work->work;
872a8c21a54SThe etnaviv authors 	INIT_WORK(&work->work, __etnaviv_gem_userptr_get_pages);
873a8c21a54SThe etnaviv authors 
874a8c21a54SThe etnaviv authors 	etnaviv_queue_work(etnaviv_obj->base.dev, &work->work);
875a8c21a54SThe etnaviv authors 
876a8c21a54SThe etnaviv authors 	return -EAGAIN;
877a8c21a54SThe etnaviv authors }
878a8c21a54SThe etnaviv authors 
879a8c21a54SThe etnaviv authors static void etnaviv_gem_userptr_release(struct etnaviv_gem_object *etnaviv_obj)
880a8c21a54SThe etnaviv authors {
881a8c21a54SThe etnaviv authors 	if (etnaviv_obj->sgt) {
882a8c21a54SThe etnaviv authors 		etnaviv_gem_scatterlist_unmap(etnaviv_obj);
883a8c21a54SThe etnaviv authors 		sg_free_table(etnaviv_obj->sgt);
884a8c21a54SThe etnaviv authors 		kfree(etnaviv_obj->sgt);
885a8c21a54SThe etnaviv authors 	}
886a8c21a54SThe etnaviv authors 	if (etnaviv_obj->pages) {
887a8c21a54SThe etnaviv authors 		int npages = etnaviv_obj->base.size >> PAGE_SHIFT;
888a8c21a54SThe etnaviv authors 
889c6f92f9fSMel Gorman 		release_pages(etnaviv_obj->pages, npages);
8902098105eSMichal Hocko 		kvfree(etnaviv_obj->pages);
891a8c21a54SThe etnaviv authors 	}
892a8c21a54SThe etnaviv authors 	put_task_struct(etnaviv_obj->userptr.task);
893a8c21a54SThe etnaviv authors }
894a8c21a54SThe etnaviv authors 
895a10e2bdeSLucas Stach static int etnaviv_gem_userptr_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
896a10e2bdeSLucas Stach 		struct vm_area_struct *vma)
897a10e2bdeSLucas Stach {
898a10e2bdeSLucas Stach 	return -EINVAL;
899a10e2bdeSLucas Stach }
900a10e2bdeSLucas Stach 
901a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_userptr_ops = {
902a8c21a54SThe etnaviv authors 	.get_pages = etnaviv_gem_userptr_get_pages,
903a8c21a54SThe etnaviv authors 	.release = etnaviv_gem_userptr_release,
904a0a5ab3eSLucas Stach 	.vmap = etnaviv_gem_vmap_impl,
905a10e2bdeSLucas Stach 	.mmap = etnaviv_gem_userptr_mmap_obj,
906a8c21a54SThe etnaviv authors };
907a8c21a54SThe etnaviv authors 
908a8c21a54SThe etnaviv authors int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
909a8c21a54SThe etnaviv authors 	uintptr_t ptr, u32 size, u32 flags, u32 *handle)
910a8c21a54SThe etnaviv authors {
911a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj;
912a8c21a54SThe etnaviv authors 	int ret;
913a8c21a54SThe etnaviv authors 
914a8c21a54SThe etnaviv authors 	ret = etnaviv_gem_new_private(dev, size, ETNA_BO_CACHED, NULL,
915a8c21a54SThe etnaviv authors 				      &etnaviv_gem_userptr_ops, &etnaviv_obj);
916a8c21a54SThe etnaviv authors 	if (ret)
917a8c21a54SThe etnaviv authors 		return ret;
918a8c21a54SThe etnaviv authors 
919a8c21a54SThe etnaviv authors 	etnaviv_obj->userptr.ptr = ptr;
920a8c21a54SThe etnaviv authors 	etnaviv_obj->userptr.task = current;
921a8c21a54SThe etnaviv authors 	etnaviv_obj->userptr.ro = !(flags & ETNA_USERPTR_WRITE);
922a8c21a54SThe etnaviv authors 	get_task_struct(current);
923a8c21a54SThe etnaviv authors 
924a8c21a54SThe etnaviv authors 	ret = etnaviv_gem_obj_add(dev, &etnaviv_obj->base);
925d9a7ed77SMarkus Elfring 	if (ret)
926d9a7ed77SMarkus Elfring 		goto unreference;
927a8c21a54SThe etnaviv authors 
928a8c21a54SThe etnaviv authors 	ret = drm_gem_handle_create(file, &etnaviv_obj->base, handle);
929d9a7ed77SMarkus Elfring unreference:
930a8c21a54SThe etnaviv authors 	/* drop reference from allocate - handle holds it now */
93123d1dd03SCihangir Akturk 	drm_gem_object_put_unlocked(&etnaviv_obj->base);
932a8c21a54SThe etnaviv authors 	return ret;
933a8c21a54SThe etnaviv authors }
934