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