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