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