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