xref: /openbmc/linux/drivers/gpu/drm/msm/msm_gem.c (revision f97769fd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <linux/spinlock.h>
8 #include <linux/shmem_fs.h>
9 #include <linux/dma-buf.h>
10 #include <linux/pfn_t.h>
11 
12 #include <drm/drm_prime.h>
13 
14 #include "msm_drv.h"
15 #include "msm_fence.h"
16 #include "msm_gem.h"
17 #include "msm_gpu.h"
18 #include "msm_mmu.h"
19 
20 static void msm_gem_vunmap_locked(struct drm_gem_object *obj);
21 
22 
23 static dma_addr_t physaddr(struct drm_gem_object *obj)
24 {
25 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
26 	struct msm_drm_private *priv = obj->dev->dev_private;
27 	return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
28 			priv->vram.paddr;
29 }
30 
31 static bool use_pages(struct drm_gem_object *obj)
32 {
33 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
34 	return !msm_obj->vram_node;
35 }
36 
37 /*
38  * Cache sync.. this is a bit over-complicated, to fit dma-mapping
39  * API.  Really GPU cache is out of scope here (handled on cmdstream)
40  * and all we need to do is invalidate newly allocated pages before
41  * mapping to CPU as uncached/writecombine.
42  *
43  * On top of this, we have the added headache, that depending on
44  * display generation, the display's iommu may be wired up to either
45  * the toplevel drm device (mdss), or to the mdp sub-node, meaning
46  * that here we either have dma-direct or iommu ops.
47  *
48  * Let this be a cautionary tail of abstraction gone wrong.
49  */
50 
51 static void sync_for_device(struct msm_gem_object *msm_obj)
52 {
53 	struct device *dev = msm_obj->base.dev->dev;
54 
55 	if (get_dma_ops(dev) && IS_ENABLED(CONFIG_ARM64)) {
56 		dma_sync_sg_for_device(dev, msm_obj->sgt->sgl,
57 			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
58 	} else {
59 		dma_map_sg(dev, msm_obj->sgt->sgl,
60 			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
61 	}
62 }
63 
64 static void sync_for_cpu(struct msm_gem_object *msm_obj)
65 {
66 	struct device *dev = msm_obj->base.dev->dev;
67 
68 	if (get_dma_ops(dev) && IS_ENABLED(CONFIG_ARM64)) {
69 		dma_sync_sg_for_cpu(dev, msm_obj->sgt->sgl,
70 			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
71 	} else {
72 		dma_unmap_sg(dev, msm_obj->sgt->sgl,
73 			msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
74 	}
75 }
76 
77 /* allocate pages from VRAM carveout, used when no IOMMU: */
78 static struct page **get_pages_vram(struct drm_gem_object *obj, int npages)
79 {
80 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
81 	struct msm_drm_private *priv = obj->dev->dev_private;
82 	dma_addr_t paddr;
83 	struct page **p;
84 	int ret, i;
85 
86 	p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
87 	if (!p)
88 		return ERR_PTR(-ENOMEM);
89 
90 	spin_lock(&priv->vram.lock);
91 	ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages);
92 	spin_unlock(&priv->vram.lock);
93 	if (ret) {
94 		kvfree(p);
95 		return ERR_PTR(ret);
96 	}
97 
98 	paddr = physaddr(obj);
99 	for (i = 0; i < npages; i++) {
100 		p[i] = phys_to_page(paddr);
101 		paddr += PAGE_SIZE;
102 	}
103 
104 	return p;
105 }
106 
107 static struct page **get_pages(struct drm_gem_object *obj)
108 {
109 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
110 
111 	if (!msm_obj->pages) {
112 		struct drm_device *dev = obj->dev;
113 		struct page **p;
114 		int npages = obj->size >> PAGE_SHIFT;
115 
116 		if (use_pages(obj))
117 			p = drm_gem_get_pages(obj);
118 		else
119 			p = get_pages_vram(obj, npages);
120 
121 		if (IS_ERR(p)) {
122 			DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n",
123 					PTR_ERR(p));
124 			return p;
125 		}
126 
127 		msm_obj->pages = p;
128 
129 		msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
130 		if (IS_ERR(msm_obj->sgt)) {
131 			void *ptr = ERR_CAST(msm_obj->sgt);
132 
133 			DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n");
134 			msm_obj->sgt = NULL;
135 			return ptr;
136 		}
137 
138 		/* For non-cached buffers, ensure the new pages are clean
139 		 * because display controller, GPU, etc. are not coherent:
140 		 */
141 		if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
142 			sync_for_device(msm_obj);
143 	}
144 
145 	return msm_obj->pages;
146 }
147 
148 static void put_pages_vram(struct drm_gem_object *obj)
149 {
150 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
151 	struct msm_drm_private *priv = obj->dev->dev_private;
152 
153 	spin_lock(&priv->vram.lock);
154 	drm_mm_remove_node(msm_obj->vram_node);
155 	spin_unlock(&priv->vram.lock);
156 
157 	kvfree(msm_obj->pages);
158 }
159 
160 static void put_pages(struct drm_gem_object *obj)
161 {
162 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
163 
164 	if (msm_obj->pages) {
165 		if (msm_obj->sgt) {
166 			/* For non-cached buffers, ensure the new
167 			 * pages are clean because display controller,
168 			 * GPU, etc. are not coherent:
169 			 */
170 			if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
171 				sync_for_cpu(msm_obj);
172 
173 			sg_free_table(msm_obj->sgt);
174 			kfree(msm_obj->sgt);
175 		}
176 
177 		if (use_pages(obj))
178 			drm_gem_put_pages(obj, msm_obj->pages, true, false);
179 		else
180 			put_pages_vram(obj);
181 
182 		msm_obj->pages = NULL;
183 	}
184 }
185 
186 struct page **msm_gem_get_pages(struct drm_gem_object *obj)
187 {
188 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
189 	struct page **p;
190 
191 	mutex_lock(&msm_obj->lock);
192 
193 	if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
194 		mutex_unlock(&msm_obj->lock);
195 		return ERR_PTR(-EBUSY);
196 	}
197 
198 	p = get_pages(obj);
199 	mutex_unlock(&msm_obj->lock);
200 	return p;
201 }
202 
203 void msm_gem_put_pages(struct drm_gem_object *obj)
204 {
205 	/* when we start tracking the pin count, then do something here */
206 }
207 
208 int msm_gem_mmap_obj(struct drm_gem_object *obj,
209 		struct vm_area_struct *vma)
210 {
211 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
212 
213 	vma->vm_flags &= ~VM_PFNMAP;
214 	vma->vm_flags |= VM_MIXEDMAP;
215 
216 	if (msm_obj->flags & MSM_BO_WC) {
217 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
218 	} else if (msm_obj->flags & MSM_BO_UNCACHED) {
219 		vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
220 	} else {
221 		/*
222 		 * Shunt off cached objs to shmem file so they have their own
223 		 * address_space (so unmap_mapping_range does what we want,
224 		 * in particular in the case of mmap'd dmabufs)
225 		 */
226 		fput(vma->vm_file);
227 		get_file(obj->filp);
228 		vma->vm_pgoff = 0;
229 		vma->vm_file  = obj->filp;
230 
231 		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
232 	}
233 
234 	return 0;
235 }
236 
237 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
238 {
239 	int ret;
240 
241 	ret = drm_gem_mmap(filp, vma);
242 	if (ret) {
243 		DBG("mmap failed: %d", ret);
244 		return ret;
245 	}
246 
247 	return msm_gem_mmap_obj(vma->vm_private_data, vma);
248 }
249 
250 vm_fault_t msm_gem_fault(struct vm_fault *vmf)
251 {
252 	struct vm_area_struct *vma = vmf->vma;
253 	struct drm_gem_object *obj = vma->vm_private_data;
254 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
255 	struct page **pages;
256 	unsigned long pfn;
257 	pgoff_t pgoff;
258 	int err;
259 	vm_fault_t ret;
260 
261 	/*
262 	 * vm_ops.open/drm_gem_mmap_obj and close get and put
263 	 * a reference on obj. So, we dont need to hold one here.
264 	 */
265 	err = mutex_lock_interruptible(&msm_obj->lock);
266 	if (err) {
267 		ret = VM_FAULT_NOPAGE;
268 		goto out;
269 	}
270 
271 	if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
272 		mutex_unlock(&msm_obj->lock);
273 		return VM_FAULT_SIGBUS;
274 	}
275 
276 	/* make sure we have pages attached now */
277 	pages = get_pages(obj);
278 	if (IS_ERR(pages)) {
279 		ret = vmf_error(PTR_ERR(pages));
280 		goto out_unlock;
281 	}
282 
283 	/* We don't use vmf->pgoff since that has the fake offset: */
284 	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
285 
286 	pfn = page_to_pfn(pages[pgoff]);
287 
288 	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
289 			pfn, pfn << PAGE_SHIFT);
290 
291 	ret = vmf_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
292 out_unlock:
293 	mutex_unlock(&msm_obj->lock);
294 out:
295 	return ret;
296 }
297 
298 /** get mmap offset */
299 static uint64_t mmap_offset(struct drm_gem_object *obj)
300 {
301 	struct drm_device *dev = obj->dev;
302 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
303 	int ret;
304 
305 	WARN_ON(!mutex_is_locked(&msm_obj->lock));
306 
307 	/* Make it mmapable */
308 	ret = drm_gem_create_mmap_offset(obj);
309 
310 	if (ret) {
311 		DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
312 		return 0;
313 	}
314 
315 	return drm_vma_node_offset_addr(&obj->vma_node);
316 }
317 
318 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
319 {
320 	uint64_t offset;
321 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
322 
323 	mutex_lock(&msm_obj->lock);
324 	offset = mmap_offset(obj);
325 	mutex_unlock(&msm_obj->lock);
326 	return offset;
327 }
328 
329 static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
330 		struct msm_gem_address_space *aspace)
331 {
332 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
333 	struct msm_gem_vma *vma;
334 
335 	WARN_ON(!mutex_is_locked(&msm_obj->lock));
336 
337 	vma = kzalloc(sizeof(*vma), GFP_KERNEL);
338 	if (!vma)
339 		return ERR_PTR(-ENOMEM);
340 
341 	vma->aspace = aspace;
342 
343 	list_add_tail(&vma->list, &msm_obj->vmas);
344 
345 	return vma;
346 }
347 
348 static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj,
349 		struct msm_gem_address_space *aspace)
350 {
351 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
352 	struct msm_gem_vma *vma;
353 
354 	WARN_ON(!mutex_is_locked(&msm_obj->lock));
355 
356 	list_for_each_entry(vma, &msm_obj->vmas, list) {
357 		if (vma->aspace == aspace)
358 			return vma;
359 	}
360 
361 	return NULL;
362 }
363 
364 static void del_vma(struct msm_gem_vma *vma)
365 {
366 	if (!vma)
367 		return;
368 
369 	list_del(&vma->list);
370 	kfree(vma);
371 }
372 
373 /* Called with msm_obj->lock locked */
374 static void
375 put_iova(struct drm_gem_object *obj)
376 {
377 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
378 	struct msm_gem_vma *vma, *tmp;
379 
380 	WARN_ON(!mutex_is_locked(&msm_obj->lock));
381 
382 	list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
383 		if (vma->aspace) {
384 			msm_gem_purge_vma(vma->aspace, vma);
385 			msm_gem_close_vma(vma->aspace, vma);
386 		}
387 		del_vma(vma);
388 	}
389 }
390 
391 static int msm_gem_get_iova_locked(struct drm_gem_object *obj,
392 		struct msm_gem_address_space *aspace, uint64_t *iova,
393 		u64 range_start, u64 range_end)
394 {
395 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
396 	struct msm_gem_vma *vma;
397 	int ret = 0;
398 
399 	WARN_ON(!mutex_is_locked(&msm_obj->lock));
400 
401 	vma = lookup_vma(obj, aspace);
402 
403 	if (!vma) {
404 		vma = add_vma(obj, aspace);
405 		if (IS_ERR(vma))
406 			return PTR_ERR(vma);
407 
408 		ret = msm_gem_init_vma(aspace, vma, obj->size >> PAGE_SHIFT,
409 			range_start, range_end);
410 		if (ret) {
411 			del_vma(vma);
412 			return ret;
413 		}
414 	}
415 
416 	*iova = vma->iova;
417 	return 0;
418 }
419 
420 static int msm_gem_pin_iova(struct drm_gem_object *obj,
421 		struct msm_gem_address_space *aspace)
422 {
423 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
424 	struct msm_gem_vma *vma;
425 	struct page **pages;
426 	int prot = IOMMU_READ;
427 
428 	if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
429 		prot |= IOMMU_WRITE;
430 
431 	if (msm_obj->flags & MSM_BO_MAP_PRIV)
432 		prot |= IOMMU_PRIV;
433 
434 	WARN_ON(!mutex_is_locked(&msm_obj->lock));
435 
436 	if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
437 		return -EBUSY;
438 
439 	vma = lookup_vma(obj, aspace);
440 	if (WARN_ON(!vma))
441 		return -EINVAL;
442 
443 	pages = get_pages(obj);
444 	if (IS_ERR(pages))
445 		return PTR_ERR(pages);
446 
447 	return msm_gem_map_vma(aspace, vma, prot,
448 			msm_obj->sgt, obj->size >> PAGE_SHIFT);
449 }
450 
451 /*
452  * get iova and pin it. Should have a matching put
453  * limits iova to specified range (in pages)
454  */
455 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
456 		struct msm_gem_address_space *aspace, uint64_t *iova,
457 		u64 range_start, u64 range_end)
458 {
459 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
460 	u64 local;
461 	int ret;
462 
463 	mutex_lock(&msm_obj->lock);
464 
465 	ret = msm_gem_get_iova_locked(obj, aspace, &local,
466 		range_start, range_end);
467 
468 	if (!ret)
469 		ret = msm_gem_pin_iova(obj, aspace);
470 
471 	if (!ret)
472 		*iova = local;
473 
474 	mutex_unlock(&msm_obj->lock);
475 	return ret;
476 }
477 
478 /* get iova and pin it. Should have a matching put */
479 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
480 		struct msm_gem_address_space *aspace, uint64_t *iova)
481 {
482 	return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX);
483 }
484 
485 /*
486  * Get an iova but don't pin it. Doesn't need a put because iovas are currently
487  * valid for the life of the object
488  */
489 int msm_gem_get_iova(struct drm_gem_object *obj,
490 		struct msm_gem_address_space *aspace, uint64_t *iova)
491 {
492 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
493 	int ret;
494 
495 	mutex_lock(&msm_obj->lock);
496 	ret = msm_gem_get_iova_locked(obj, aspace, iova, 0, U64_MAX);
497 	mutex_unlock(&msm_obj->lock);
498 
499 	return ret;
500 }
501 
502 /* get iova without taking a reference, used in places where you have
503  * already done a 'msm_gem_get_and_pin_iova' or 'msm_gem_get_iova'
504  */
505 uint64_t msm_gem_iova(struct drm_gem_object *obj,
506 		struct msm_gem_address_space *aspace)
507 {
508 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
509 	struct msm_gem_vma *vma;
510 
511 	mutex_lock(&msm_obj->lock);
512 	vma = lookup_vma(obj, aspace);
513 	mutex_unlock(&msm_obj->lock);
514 	WARN_ON(!vma);
515 
516 	return vma ? vma->iova : 0;
517 }
518 
519 /*
520  * Unpin a iova by updating the reference counts. The memory isn't actually
521  * purged until something else (shrinker, mm_notifier, destroy, etc) decides
522  * to get rid of it
523  */
524 void msm_gem_unpin_iova(struct drm_gem_object *obj,
525 		struct msm_gem_address_space *aspace)
526 {
527 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
528 	struct msm_gem_vma *vma;
529 
530 	mutex_lock(&msm_obj->lock);
531 	vma = lookup_vma(obj, aspace);
532 
533 	if (!WARN_ON(!vma))
534 		msm_gem_unmap_vma(aspace, vma);
535 
536 	mutex_unlock(&msm_obj->lock);
537 }
538 
539 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
540 		struct drm_mode_create_dumb *args)
541 {
542 	args->pitch = align_pitch(args->width, args->bpp);
543 	args->size  = PAGE_ALIGN(args->pitch * args->height);
544 	return msm_gem_new_handle(dev, file, args->size,
545 			MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb");
546 }
547 
548 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
549 		uint32_t handle, uint64_t *offset)
550 {
551 	struct drm_gem_object *obj;
552 	int ret = 0;
553 
554 	/* GEM does all our handle to object mapping */
555 	obj = drm_gem_object_lookup(file, handle);
556 	if (obj == NULL) {
557 		ret = -ENOENT;
558 		goto fail;
559 	}
560 
561 	*offset = msm_gem_mmap_offset(obj);
562 
563 	drm_gem_object_put(obj);
564 
565 fail:
566 	return ret;
567 }
568 
569 static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
570 {
571 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
572 	int ret = 0;
573 
574 	if (obj->import_attach)
575 		return ERR_PTR(-ENODEV);
576 
577 	mutex_lock(&msm_obj->lock);
578 
579 	if (WARN_ON(msm_obj->madv > madv)) {
580 		DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n",
581 			msm_obj->madv, madv);
582 		mutex_unlock(&msm_obj->lock);
583 		return ERR_PTR(-EBUSY);
584 	}
585 
586 	/* increment vmap_count *before* vmap() call, so shrinker can
587 	 * check vmap_count (is_vunmapable()) outside of msm_obj->lock.
588 	 * This guarantees that we won't try to msm_gem_vunmap() this
589 	 * same object from within the vmap() call (while we already
590 	 * hold msm_obj->lock)
591 	 */
592 	msm_obj->vmap_count++;
593 
594 	if (!msm_obj->vaddr) {
595 		struct page **pages = get_pages(obj);
596 		if (IS_ERR(pages)) {
597 			ret = PTR_ERR(pages);
598 			goto fail;
599 		}
600 		msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
601 				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
602 		if (msm_obj->vaddr == NULL) {
603 			ret = -ENOMEM;
604 			goto fail;
605 		}
606 	}
607 
608 	mutex_unlock(&msm_obj->lock);
609 	return msm_obj->vaddr;
610 
611 fail:
612 	msm_obj->vmap_count--;
613 	mutex_unlock(&msm_obj->lock);
614 	return ERR_PTR(ret);
615 }
616 
617 void *msm_gem_get_vaddr(struct drm_gem_object *obj)
618 {
619 	return get_vaddr(obj, MSM_MADV_WILLNEED);
620 }
621 
622 /*
623  * Don't use this!  It is for the very special case of dumping
624  * submits from GPU hangs or faults, were the bo may already
625  * be MSM_MADV_DONTNEED, but we know the buffer is still on the
626  * active list.
627  */
628 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
629 {
630 	return get_vaddr(obj, __MSM_MADV_PURGED);
631 }
632 
633 void msm_gem_put_vaddr(struct drm_gem_object *obj)
634 {
635 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
636 
637 	mutex_lock(&msm_obj->lock);
638 	WARN_ON(msm_obj->vmap_count < 1);
639 	msm_obj->vmap_count--;
640 	mutex_unlock(&msm_obj->lock);
641 }
642 
643 /* Update madvise status, returns true if not purged, else
644  * false or -errno.
645  */
646 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
647 {
648 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
649 
650 	mutex_lock(&msm_obj->lock);
651 
652 	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
653 
654 	if (msm_obj->madv != __MSM_MADV_PURGED)
655 		msm_obj->madv = madv;
656 
657 	madv = msm_obj->madv;
658 
659 	mutex_unlock(&msm_obj->lock);
660 
661 	return (madv != __MSM_MADV_PURGED);
662 }
663 
664 void msm_gem_purge(struct drm_gem_object *obj, enum msm_gem_lock subclass)
665 {
666 	struct drm_device *dev = obj->dev;
667 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
668 
669 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
670 	WARN_ON(!is_purgeable(msm_obj));
671 	WARN_ON(obj->import_attach);
672 
673 	mutex_lock_nested(&msm_obj->lock, subclass);
674 
675 	put_iova(obj);
676 
677 	msm_gem_vunmap_locked(obj);
678 
679 	put_pages(obj);
680 
681 	msm_obj->madv = __MSM_MADV_PURGED;
682 
683 	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
684 	drm_gem_free_mmap_offset(obj);
685 
686 	/* Our goal here is to return as much of the memory as
687 	 * is possible back to the system as we are called from OOM.
688 	 * To do this we must instruct the shmfs to drop all of its
689 	 * backing pages, *now*.
690 	 */
691 	shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
692 
693 	invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
694 			0, (loff_t)-1);
695 
696 	mutex_unlock(&msm_obj->lock);
697 }
698 
699 static void msm_gem_vunmap_locked(struct drm_gem_object *obj)
700 {
701 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
702 
703 	WARN_ON(!mutex_is_locked(&msm_obj->lock));
704 
705 	if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj)))
706 		return;
707 
708 	vunmap(msm_obj->vaddr);
709 	msm_obj->vaddr = NULL;
710 }
711 
712 void msm_gem_vunmap(struct drm_gem_object *obj, enum msm_gem_lock subclass)
713 {
714 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
715 
716 	mutex_lock_nested(&msm_obj->lock, subclass);
717 	msm_gem_vunmap_locked(obj);
718 	mutex_unlock(&msm_obj->lock);
719 }
720 
721 /* must be called before _move_to_active().. */
722 int msm_gem_sync_object(struct drm_gem_object *obj,
723 		struct msm_fence_context *fctx, bool exclusive)
724 {
725 	struct dma_resv_list *fobj;
726 	struct dma_fence *fence;
727 	int i, ret;
728 
729 	fobj = dma_resv_get_list(obj->resv);
730 	if (!fobj || (fobj->shared_count == 0)) {
731 		fence = dma_resv_get_excl(obj->resv);
732 		/* don't need to wait on our own fences, since ring is fifo */
733 		if (fence && (fence->context != fctx->context)) {
734 			ret = dma_fence_wait(fence, true);
735 			if (ret)
736 				return ret;
737 		}
738 	}
739 
740 	if (!exclusive || !fobj)
741 		return 0;
742 
743 	for (i = 0; i < fobj->shared_count; i++) {
744 		fence = rcu_dereference_protected(fobj->shared[i],
745 						dma_resv_held(obj->resv));
746 		if (fence->context != fctx->context) {
747 			ret = dma_fence_wait(fence, true);
748 			if (ret)
749 				return ret;
750 		}
751 	}
752 
753 	return 0;
754 }
755 
756 void msm_gem_move_to_active(struct drm_gem_object *obj,
757 		struct msm_gpu *gpu, bool exclusive, struct dma_fence *fence)
758 {
759 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
760 	WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
761 	msm_obj->gpu = gpu;
762 	if (exclusive)
763 		dma_resv_add_excl_fence(obj->resv, fence);
764 	else
765 		dma_resv_add_shared_fence(obj->resv, fence);
766 	list_del_init(&msm_obj->mm_list);
767 	list_add_tail(&msm_obj->mm_list, &gpu->active_list);
768 }
769 
770 void msm_gem_move_to_inactive(struct drm_gem_object *obj)
771 {
772 	struct drm_device *dev = obj->dev;
773 	struct msm_drm_private *priv = dev->dev_private;
774 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
775 
776 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
777 
778 	msm_obj->gpu = NULL;
779 	list_del_init(&msm_obj->mm_list);
780 	list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
781 }
782 
783 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
784 {
785 	bool write = !!(op & MSM_PREP_WRITE);
786 	unsigned long remain =
787 		op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
788 	long ret;
789 
790 	ret = dma_resv_wait_timeout_rcu(obj->resv, write,
791 						  true,  remain);
792 	if (ret == 0)
793 		return remain == 0 ? -EBUSY : -ETIMEDOUT;
794 	else if (ret < 0)
795 		return ret;
796 
797 	/* TODO cache maintenance */
798 
799 	return 0;
800 }
801 
802 int msm_gem_cpu_fini(struct drm_gem_object *obj)
803 {
804 	/* TODO cache maintenance */
805 	return 0;
806 }
807 
808 #ifdef CONFIG_DEBUG_FS
809 static void describe_fence(struct dma_fence *fence, const char *type,
810 		struct seq_file *m)
811 {
812 	if (!dma_fence_is_signaled(fence))
813 		seq_printf(m, "\t%9s: %s %s seq %llu\n", type,
814 				fence->ops->get_driver_name(fence),
815 				fence->ops->get_timeline_name(fence),
816 				fence->seqno);
817 }
818 
819 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
820 {
821 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
822 	struct dma_resv *robj = obj->resv;
823 	struct dma_resv_list *fobj;
824 	struct dma_fence *fence;
825 	struct msm_gem_vma *vma;
826 	uint64_t off = drm_vma_node_start(&obj->vma_node);
827 	const char *madv;
828 
829 	mutex_lock(&msm_obj->lock);
830 
831 	switch (msm_obj->madv) {
832 	case __MSM_MADV_PURGED:
833 		madv = " purged";
834 		break;
835 	case MSM_MADV_DONTNEED:
836 		madv = " purgeable";
837 		break;
838 	case MSM_MADV_WILLNEED:
839 	default:
840 		madv = "";
841 		break;
842 	}
843 
844 	seq_printf(m, "%08x: %c %2d (%2d) %08llx %p",
845 			msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
846 			obj->name, kref_read(&obj->refcount),
847 			off, msm_obj->vaddr);
848 
849 	seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
850 
851 	if (!list_empty(&msm_obj->vmas)) {
852 
853 		seq_puts(m, "      vmas:");
854 
855 		list_for_each_entry(vma, &msm_obj->vmas, list)
856 			seq_printf(m, " [%s: %08llx,%s,inuse=%d]",
857 				vma->aspace != NULL ? vma->aspace->name : NULL,
858 				vma->iova, vma->mapped ? "mapped" : "unmapped",
859 				vma->inuse);
860 
861 		seq_puts(m, "\n");
862 	}
863 
864 	rcu_read_lock();
865 	fobj = rcu_dereference(robj->fence);
866 	if (fobj) {
867 		unsigned int i, shared_count = fobj->shared_count;
868 
869 		for (i = 0; i < shared_count; i++) {
870 			fence = rcu_dereference(fobj->shared[i]);
871 			describe_fence(fence, "Shared", m);
872 		}
873 	}
874 
875 	fence = rcu_dereference(robj->fence_excl);
876 	if (fence)
877 		describe_fence(fence, "Exclusive", m);
878 	rcu_read_unlock();
879 
880 	mutex_unlock(&msm_obj->lock);
881 }
882 
883 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
884 {
885 	struct msm_gem_object *msm_obj;
886 	int count = 0;
887 	size_t size = 0;
888 
889 	seq_puts(m, "   flags       id ref  offset   kaddr            size     madv      name\n");
890 	list_for_each_entry(msm_obj, list, mm_list) {
891 		struct drm_gem_object *obj = &msm_obj->base;
892 		seq_puts(m, "   ");
893 		msm_gem_describe(obj, m);
894 		count++;
895 		size += obj->size;
896 	}
897 
898 	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
899 }
900 #endif
901 
902 /* don't call directly!  Use drm_gem_object_put_locked() and friends */
903 void msm_gem_free_object(struct drm_gem_object *obj)
904 {
905 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
906 	struct drm_device *dev = obj->dev;
907 	struct msm_drm_private *priv = dev->dev_private;
908 
909 	if (llist_add(&msm_obj->freed, &priv->free_list))
910 		queue_work(priv->wq, &priv->free_work);
911 }
912 
913 static void free_object(struct msm_gem_object *msm_obj)
914 {
915 	struct drm_gem_object *obj = &msm_obj->base;
916 	struct drm_device *dev = obj->dev;
917 
918 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
919 
920 	/* object should not be on active list: */
921 	WARN_ON(is_active(msm_obj));
922 
923 	list_del(&msm_obj->mm_list);
924 
925 	mutex_lock(&msm_obj->lock);
926 
927 	put_iova(obj);
928 
929 	if (obj->import_attach) {
930 		WARN_ON(msm_obj->vaddr);
931 
932 		/* Don't drop the pages for imported dmabuf, as they are not
933 		 * ours, just free the array we allocated:
934 		 */
935 		if (msm_obj->pages)
936 			kvfree(msm_obj->pages);
937 
938 		drm_prime_gem_destroy(obj, msm_obj->sgt);
939 	} else {
940 		msm_gem_vunmap_locked(obj);
941 		put_pages(obj);
942 	}
943 
944 	drm_gem_object_release(obj);
945 
946 	mutex_unlock(&msm_obj->lock);
947 	kfree(msm_obj);
948 }
949 
950 void msm_gem_free_work(struct work_struct *work)
951 {
952 	struct msm_drm_private *priv =
953 		container_of(work, struct msm_drm_private, free_work);
954 	struct drm_device *dev = priv->dev;
955 	struct llist_node *freed;
956 	struct msm_gem_object *msm_obj, *next;
957 
958 	while ((freed = llist_del_all(&priv->free_list))) {
959 
960 		mutex_lock(&dev->struct_mutex);
961 
962 		llist_for_each_entry_safe(msm_obj, next,
963 					  freed, freed)
964 			free_object(msm_obj);
965 
966 		mutex_unlock(&dev->struct_mutex);
967 
968 		if (need_resched())
969 			break;
970 	}
971 }
972 
973 /* convenience method to construct a GEM buffer object, and userspace handle */
974 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
975 		uint32_t size, uint32_t flags, uint32_t *handle,
976 		char *name)
977 {
978 	struct drm_gem_object *obj;
979 	int ret;
980 
981 	obj = msm_gem_new(dev, size, flags);
982 
983 	if (IS_ERR(obj))
984 		return PTR_ERR(obj);
985 
986 	if (name)
987 		msm_gem_object_set_name(obj, "%s", name);
988 
989 	ret = drm_gem_handle_create(file, obj, handle);
990 
991 	/* drop reference from allocate - handle holds it now */
992 	drm_gem_object_put(obj);
993 
994 	return ret;
995 }
996 
997 static int msm_gem_new_impl(struct drm_device *dev,
998 		uint32_t size, uint32_t flags,
999 		struct drm_gem_object **obj)
1000 {
1001 	struct msm_gem_object *msm_obj;
1002 
1003 	switch (flags & MSM_BO_CACHE_MASK) {
1004 	case MSM_BO_UNCACHED:
1005 	case MSM_BO_CACHED:
1006 	case MSM_BO_WC:
1007 		break;
1008 	default:
1009 		DRM_DEV_ERROR(dev->dev, "invalid cache flag: %x\n",
1010 				(flags & MSM_BO_CACHE_MASK));
1011 		return -EINVAL;
1012 	}
1013 
1014 	msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
1015 	if (!msm_obj)
1016 		return -ENOMEM;
1017 
1018 	mutex_init(&msm_obj->lock);
1019 
1020 	msm_obj->flags = flags;
1021 	msm_obj->madv = MSM_MADV_WILLNEED;
1022 
1023 	INIT_LIST_HEAD(&msm_obj->submit_entry);
1024 	INIT_LIST_HEAD(&msm_obj->vmas);
1025 
1026 	*obj = &msm_obj->base;
1027 
1028 	return 0;
1029 }
1030 
1031 static struct drm_gem_object *_msm_gem_new(struct drm_device *dev,
1032 		uint32_t size, uint32_t flags, bool struct_mutex_locked)
1033 {
1034 	struct msm_drm_private *priv = dev->dev_private;
1035 	struct msm_gem_object *msm_obj;
1036 	struct drm_gem_object *obj = NULL;
1037 	bool use_vram = false;
1038 	int ret;
1039 
1040 	size = PAGE_ALIGN(size);
1041 
1042 	if (!msm_use_mmu(dev))
1043 		use_vram = true;
1044 	else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
1045 		use_vram = true;
1046 
1047 	if (WARN_ON(use_vram && !priv->vram.size))
1048 		return ERR_PTR(-EINVAL);
1049 
1050 	/* Disallow zero sized objects as they make the underlying
1051 	 * infrastructure grumpy
1052 	 */
1053 	if (size == 0)
1054 		return ERR_PTR(-EINVAL);
1055 
1056 	ret = msm_gem_new_impl(dev, size, flags, &obj);
1057 	if (ret)
1058 		goto fail;
1059 
1060 	msm_obj = to_msm_bo(obj);
1061 
1062 	if (use_vram) {
1063 		struct msm_gem_vma *vma;
1064 		struct page **pages;
1065 
1066 		mutex_lock(&msm_obj->lock);
1067 
1068 		vma = add_vma(obj, NULL);
1069 		mutex_unlock(&msm_obj->lock);
1070 		if (IS_ERR(vma)) {
1071 			ret = PTR_ERR(vma);
1072 			goto fail;
1073 		}
1074 
1075 		to_msm_bo(obj)->vram_node = &vma->node;
1076 
1077 		drm_gem_private_object_init(dev, obj, size);
1078 
1079 		pages = get_pages(obj);
1080 		if (IS_ERR(pages)) {
1081 			ret = PTR_ERR(pages);
1082 			goto fail;
1083 		}
1084 
1085 		vma->iova = physaddr(obj);
1086 	} else {
1087 		ret = drm_gem_object_init(dev, obj, size);
1088 		if (ret)
1089 			goto fail;
1090 		/*
1091 		 * Our buffers are kept pinned, so allocating them from the
1092 		 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1093 		 * See comments above new_inode() why this is required _and_
1094 		 * expected if you're going to pin these pages.
1095 		 */
1096 		mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
1097 	}
1098 
1099 	if (struct_mutex_locked) {
1100 		WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1101 		list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
1102 	} else {
1103 		mutex_lock(&dev->struct_mutex);
1104 		list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
1105 		mutex_unlock(&dev->struct_mutex);
1106 	}
1107 
1108 	return obj;
1109 
1110 fail:
1111 	drm_gem_object_put(obj);
1112 	return ERR_PTR(ret);
1113 }
1114 
1115 struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev,
1116 		uint32_t size, uint32_t flags)
1117 {
1118 	return _msm_gem_new(dev, size, flags, true);
1119 }
1120 
1121 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
1122 		uint32_t size, uint32_t flags)
1123 {
1124 	return _msm_gem_new(dev, size, flags, false);
1125 }
1126 
1127 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
1128 		struct dma_buf *dmabuf, struct sg_table *sgt)
1129 {
1130 	struct msm_drm_private *priv = dev->dev_private;
1131 	struct msm_gem_object *msm_obj;
1132 	struct drm_gem_object *obj;
1133 	uint32_t size;
1134 	int ret, npages;
1135 
1136 	/* if we don't have IOMMU, don't bother pretending we can import: */
1137 	if (!msm_use_mmu(dev)) {
1138 		DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
1139 		return ERR_PTR(-EINVAL);
1140 	}
1141 
1142 	size = PAGE_ALIGN(dmabuf->size);
1143 
1144 	ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
1145 	if (ret)
1146 		goto fail;
1147 
1148 	drm_gem_private_object_init(dev, obj, size);
1149 
1150 	npages = size / PAGE_SIZE;
1151 
1152 	msm_obj = to_msm_bo(obj);
1153 	mutex_lock(&msm_obj->lock);
1154 	msm_obj->sgt = sgt;
1155 	msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1156 	if (!msm_obj->pages) {
1157 		mutex_unlock(&msm_obj->lock);
1158 		ret = -ENOMEM;
1159 		goto fail;
1160 	}
1161 
1162 	ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
1163 	if (ret) {
1164 		mutex_unlock(&msm_obj->lock);
1165 		goto fail;
1166 	}
1167 
1168 	mutex_unlock(&msm_obj->lock);
1169 
1170 	mutex_lock(&dev->struct_mutex);
1171 	list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
1172 	mutex_unlock(&dev->struct_mutex);
1173 
1174 	return obj;
1175 
1176 fail:
1177 	drm_gem_object_put(obj);
1178 	return ERR_PTR(ret);
1179 }
1180 
1181 static void *_msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1182 		uint32_t flags, struct msm_gem_address_space *aspace,
1183 		struct drm_gem_object **bo, uint64_t *iova, bool locked)
1184 {
1185 	void *vaddr;
1186 	struct drm_gem_object *obj = _msm_gem_new(dev, size, flags, locked);
1187 	int ret;
1188 
1189 	if (IS_ERR(obj))
1190 		return ERR_CAST(obj);
1191 
1192 	if (iova) {
1193 		ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
1194 		if (ret)
1195 			goto err;
1196 	}
1197 
1198 	vaddr = msm_gem_get_vaddr(obj);
1199 	if (IS_ERR(vaddr)) {
1200 		msm_gem_unpin_iova(obj, aspace);
1201 		ret = PTR_ERR(vaddr);
1202 		goto err;
1203 	}
1204 
1205 	if (bo)
1206 		*bo = obj;
1207 
1208 	return vaddr;
1209 err:
1210 	if (locked)
1211 		drm_gem_object_put_locked(obj);
1212 	else
1213 		drm_gem_object_put(obj);
1214 
1215 	return ERR_PTR(ret);
1216 
1217 }
1218 
1219 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1220 		uint32_t flags, struct msm_gem_address_space *aspace,
1221 		struct drm_gem_object **bo, uint64_t *iova)
1222 {
1223 	return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, false);
1224 }
1225 
1226 void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size,
1227 		uint32_t flags, struct msm_gem_address_space *aspace,
1228 		struct drm_gem_object **bo, uint64_t *iova)
1229 {
1230 	return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, true);
1231 }
1232 
1233 void msm_gem_kernel_put(struct drm_gem_object *bo,
1234 		struct msm_gem_address_space *aspace, bool locked)
1235 {
1236 	if (IS_ERR_OR_NULL(bo))
1237 		return;
1238 
1239 	msm_gem_put_vaddr(bo);
1240 	msm_gem_unpin_iova(bo, aspace);
1241 
1242 	if (locked)
1243 		drm_gem_object_put_locked(bo);
1244 	else
1245 		drm_gem_object_put(bo);
1246 }
1247 
1248 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
1249 {
1250 	struct msm_gem_object *msm_obj = to_msm_bo(bo);
1251 	va_list ap;
1252 
1253 	if (!fmt)
1254 		return;
1255 
1256 	va_start(ap, fmt);
1257 	vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);
1258 	va_end(ap);
1259 }
1260