Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / msm / msm_gem.c
blob9d10739c4eb2dab23cb27257e2f8a0addc31ec68
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
5 */
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>
13 #include <drm/drm_prime.h>
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"
21 static void update_inactive(struct msm_gem_object *msm_obj);
23 static dma_addr_t physaddr(struct drm_gem_object *obj)
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;
31 static bool use_pages(struct drm_gem_object *obj)
33 struct msm_gem_object *msm_obj = to_msm_bo(obj);
34 return !msm_obj->vram_node;
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.
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.
48 * Let this be a cautionary tail of abstraction gone wrong.
51 static void sync_for_device(struct msm_gem_object *msm_obj)
53 struct device *dev = msm_obj->base.dev->dev;
55 dma_map_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0);
58 static void sync_for_cpu(struct msm_gem_object *msm_obj)
60 struct device *dev = msm_obj->base.dev->dev;
62 dma_unmap_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0);
65 /* allocate pages from VRAM carveout, used when no IOMMU: */
66 static struct page **get_pages_vram(struct drm_gem_object *obj, int npages)
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;
74 p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
75 if (!p)
76 return ERR_PTR(-ENOMEM);
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);
86 paddr = physaddr(obj);
87 for (i = 0; i < npages; i++) {
88 p[i] = phys_to_page(paddr);
89 paddr += PAGE_SIZE;
92 return p;
95 static struct page **get_pages(struct drm_gem_object *obj)
97 struct msm_gem_object *msm_obj = to_msm_bo(obj);
99 WARN_ON(!msm_gem_is_locked(obj));
101 if (!msm_obj->pages) {
102 struct drm_device *dev = obj->dev;
103 struct page **p;
104 int npages = obj->size >> PAGE_SHIFT;
106 if (use_pages(obj))
107 p = drm_gem_get_pages(obj);
108 else
109 p = get_pages_vram(obj, npages);
111 if (IS_ERR(p)) {
112 DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n",
113 PTR_ERR(p));
114 return p;
117 msm_obj->pages = p;
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);
123 DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n");
124 msm_obj->sgt = NULL;
125 return ptr;
128 /* For non-cached buffers, ensure the new pages are clean
129 * because display controller, GPU, etc. are not coherent:
131 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
132 sync_for_device(msm_obj);
135 return msm_obj->pages;
138 static void put_pages_vram(struct drm_gem_object *obj)
140 struct msm_gem_object *msm_obj = to_msm_bo(obj);
141 struct msm_drm_private *priv = obj->dev->dev_private;
143 spin_lock(&priv->vram.lock);
144 drm_mm_remove_node(msm_obj->vram_node);
145 spin_unlock(&priv->vram.lock);
147 kvfree(msm_obj->pages);
150 static void put_pages(struct drm_gem_object *obj)
152 struct msm_gem_object *msm_obj = to_msm_bo(obj);
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:
160 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
161 sync_for_cpu(msm_obj);
163 sg_free_table(msm_obj->sgt);
164 kfree(msm_obj->sgt);
167 if (use_pages(obj))
168 drm_gem_put_pages(obj, msm_obj->pages, true, false);
169 else
170 put_pages_vram(obj);
172 msm_obj->pages = NULL;
176 struct page **msm_gem_get_pages(struct drm_gem_object *obj)
178 struct msm_gem_object *msm_obj = to_msm_bo(obj);
179 struct page **p;
181 msm_gem_lock(obj);
183 if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
184 msm_gem_unlock(obj);
185 return ERR_PTR(-EBUSY);
188 p = get_pages(obj);
189 msm_gem_unlock(obj);
190 return p;
193 void msm_gem_put_pages(struct drm_gem_object *obj)
195 /* when we start tracking the pin count, then do something here */
198 int msm_gem_mmap_obj(struct drm_gem_object *obj,
199 struct vm_area_struct *vma)
201 struct msm_gem_object *msm_obj = to_msm_bo(obj);
203 vma->vm_flags &= ~VM_PFNMAP;
204 vma->vm_flags |= VM_MIXEDMAP;
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 {
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)
216 vma->vm_pgoff = 0;
217 vma_set_file(vma, obj->filp);
219 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
222 return 0;
225 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
227 int ret;
229 ret = drm_gem_mmap(filp, vma);
230 if (ret) {
231 DBG("mmap failed: %d", ret);
232 return ret;
235 return msm_gem_mmap_obj(vma->vm_private_data, vma);
238 static vm_fault_t msm_gem_fault(struct vm_fault *vmf)
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;
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.
253 err = msm_gem_lock_interruptible(obj);
254 if (err) {
255 ret = VM_FAULT_NOPAGE;
256 goto out;
259 if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
260 msm_gem_unlock(obj);
261 return VM_FAULT_SIGBUS;
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;
271 /* We don't use vmf->pgoff since that has the fake offset: */
272 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
274 pfn = page_to_pfn(pages[pgoff]);
276 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
277 pfn, pfn << PAGE_SHIFT);
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;
286 /** get mmap offset */
287 static uint64_t mmap_offset(struct drm_gem_object *obj)
289 struct drm_device *dev = obj->dev;
290 int ret;
292 WARN_ON(!msm_gem_is_locked(obj));
294 /* Make it mmapable */
295 ret = drm_gem_create_mmap_offset(obj);
297 if (ret) {
298 DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
299 return 0;
302 return drm_vma_node_offset_addr(&obj->vma_node);
305 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
307 uint64_t offset;
309 msm_gem_lock(obj);
310 offset = mmap_offset(obj);
311 msm_gem_unlock(obj);
312 return offset;
315 static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
316 struct msm_gem_address_space *aspace)
318 struct msm_gem_object *msm_obj = to_msm_bo(obj);
319 struct msm_gem_vma *vma;
321 WARN_ON(!msm_gem_is_locked(obj));
323 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
324 if (!vma)
325 return ERR_PTR(-ENOMEM);
327 vma->aspace = aspace;
329 list_add_tail(&vma->list, &msm_obj->vmas);
331 return vma;
334 static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj,
335 struct msm_gem_address_space *aspace)
337 struct msm_gem_object *msm_obj = to_msm_bo(obj);
338 struct msm_gem_vma *vma;
340 WARN_ON(!msm_gem_is_locked(obj));
342 list_for_each_entry(vma, &msm_obj->vmas, list) {
343 if (vma->aspace == aspace)
344 return vma;
347 return NULL;
350 static void del_vma(struct msm_gem_vma *vma)
352 if (!vma)
353 return;
355 list_del(&vma->list);
356 kfree(vma);
359 /* Called with msm_obj locked */
360 static void
361 put_iova_spaces(struct drm_gem_object *obj)
363 struct msm_gem_object *msm_obj = to_msm_bo(obj);
364 struct msm_gem_vma *vma;
366 WARN_ON(!msm_gem_is_locked(obj));
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);
376 /* Called with msm_obj locked */
377 static void
378 put_iova_vmas(struct drm_gem_object *obj)
380 struct msm_gem_object *msm_obj = to_msm_bo(obj);
381 struct msm_gem_vma *vma, *tmp;
383 WARN_ON(!msm_gem_is_locked(obj));
385 list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
386 del_vma(vma);
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)
394 struct msm_gem_vma *vma;
395 int ret = 0;
397 WARN_ON(!msm_gem_is_locked(obj));
399 vma = lookup_vma(obj, aspace);
401 if (!vma) {
402 vma = add_vma(obj, aspace);
403 if (IS_ERR(vma))
404 return PTR_ERR(vma);
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;
414 *iova = vma->iova;
415 return 0;
418 static int msm_gem_pin_iova(struct drm_gem_object *obj,
419 struct msm_gem_address_space *aspace)
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;
426 if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
427 prot |= IOMMU_WRITE;
429 if (msm_obj->flags & MSM_BO_MAP_PRIV)
430 prot |= IOMMU_PRIV;
432 WARN_ON(!msm_gem_is_locked(obj));
434 if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
435 return -EBUSY;
437 vma = lookup_vma(obj, aspace);
438 if (WARN_ON(!vma))
439 return -EINVAL;
441 pages = get_pages(obj);
442 if (IS_ERR(pages))
443 return PTR_ERR(pages);
445 return msm_gem_map_vma(aspace, vma, prot,
446 msm_obj->sgt, obj->size >> PAGE_SHIFT);
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)
453 u64 local;
454 int ret;
456 WARN_ON(!msm_gem_is_locked(obj));
458 ret = get_iova_locked(obj, aspace, &local,
459 range_start, range_end);
461 if (!ret)
462 ret = msm_gem_pin_iova(obj, aspace);
464 if (!ret)
465 *iova = local;
467 return ret;
471 * get iova and pin it. Should have a matching put
472 * limits iova to specified range (in pages)
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)
478 int ret;
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);
484 return ret;
487 int msm_gem_get_and_pin_iova_locked(struct drm_gem_object *obj,
488 struct msm_gem_address_space *aspace, uint64_t *iova)
490 return get_and_pin_iova_range_locked(obj, aspace, iova, 0, U64_MAX);
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)
497 return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX);
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
504 int msm_gem_get_iova(struct drm_gem_object *obj,
505 struct msm_gem_address_space *aspace, uint64_t *iova)
507 int ret;
509 msm_gem_lock(obj);
510 ret = get_iova_locked(obj, aspace, iova, 0, U64_MAX);
511 msm_gem_unlock(obj);
513 return ret;
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'
519 uint64_t msm_gem_iova(struct drm_gem_object *obj,
520 struct msm_gem_address_space *aspace)
522 struct msm_gem_vma *vma;
524 msm_gem_lock(obj);
525 vma = lookup_vma(obj, aspace);
526 msm_gem_unlock(obj);
527 WARN_ON(!vma);
529 return vma ? vma->iova : 0;
533 * Locked variant of msm_gem_unpin_iova()
535 void msm_gem_unpin_iova_locked(struct drm_gem_object *obj,
536 struct msm_gem_address_space *aspace)
538 struct msm_gem_vma *vma;
540 WARN_ON(!msm_gem_is_locked(obj));
542 vma = lookup_vma(obj, aspace);
544 if (!WARN_ON(!vma))
545 msm_gem_unmap_vma(aspace, vma);
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
553 void msm_gem_unpin_iova(struct drm_gem_object *obj,
554 struct msm_gem_address_space *aspace)
556 msm_gem_lock(obj);
557 msm_gem_unpin_iova_locked(obj, aspace);
558 msm_gem_unlock(obj);
561 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
562 struct drm_mode_create_dumb *args)
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");
570 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
571 uint32_t handle, uint64_t *offset)
573 struct drm_gem_object *obj;
574 int ret = 0;
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;
583 *offset = msm_gem_mmap_offset(obj);
585 drm_gem_object_put(obj);
587 fail:
588 return ret;
591 static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
593 struct msm_gem_object *msm_obj = to_msm_bo(obj);
594 int ret = 0;
596 WARN_ON(!msm_gem_is_locked(obj));
598 if (obj->import_attach)
599 return ERR_PTR(-ENODEV);
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);
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)
613 msm_obj->vmap_count++;
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;
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;
629 return msm_obj->vaddr;
631 fail:
632 msm_obj->vmap_count--;
633 return ERR_PTR(ret);
636 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
638 return get_vaddr(obj, MSM_MADV_WILLNEED);
641 void *msm_gem_get_vaddr(struct drm_gem_object *obj)
643 void *ret;
645 msm_gem_lock(obj);
646 ret = msm_gem_get_vaddr_locked(obj);
647 msm_gem_unlock(obj);
649 return ret;
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.
658 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
660 return get_vaddr(obj, __MSM_MADV_PURGED);
663 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
665 struct msm_gem_object *msm_obj = to_msm_bo(obj);
667 WARN_ON(!msm_gem_is_locked(obj));
668 WARN_ON(msm_obj->vmap_count < 1);
670 msm_obj->vmap_count--;
673 void msm_gem_put_vaddr(struct drm_gem_object *obj)
675 msm_gem_lock(obj);
676 msm_gem_put_vaddr_locked(obj);
677 msm_gem_unlock(obj);
680 /* Update madvise status, returns true if not purged, else
681 * false or -errno.
683 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
685 struct msm_gem_object *msm_obj = to_msm_bo(obj);
687 msm_gem_lock(obj);
689 if (msm_obj->madv != __MSM_MADV_PURGED)
690 msm_obj->madv = madv;
692 madv = msm_obj->madv;
694 /* If the obj is inactive, we might need to move it
695 * between inactive lists
697 if (msm_obj->active_count == 0)
698 update_inactive(msm_obj);
700 msm_gem_unlock(obj);
702 return (madv != __MSM_MADV_PURGED);
705 void msm_gem_purge(struct drm_gem_object *obj)
707 struct drm_device *dev = obj->dev;
708 struct msm_gem_object *msm_obj = to_msm_bo(obj);
710 WARN_ON(!is_purgeable(msm_obj));
711 WARN_ON(obj->import_attach);
713 put_iova_spaces(obj);
715 msm_gem_vunmap(obj);
717 put_pages(obj);
719 put_iova_vmas(obj);
721 msm_obj->madv = __MSM_MADV_PURGED;
723 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
724 drm_gem_free_mmap_offset(obj);
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*.
731 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
733 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
734 0, (loff_t)-1);
737 void msm_gem_vunmap(struct drm_gem_object *obj)
739 struct msm_gem_object *msm_obj = to_msm_bo(obj);
741 WARN_ON(!msm_gem_is_locked(obj));
743 if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj)))
744 return;
746 vunmap(msm_obj->vaddr);
747 msm_obj->vaddr = NULL;
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)
754 struct dma_resv_list *fobj;
755 struct dma_fence *fence;
756 int i, ret;
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;
769 if (!exclusive || !fobj)
770 return 0;
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;
782 return 0;
785 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu)
787 struct msm_gem_object *msm_obj = to_msm_bo(obj);
788 struct msm_drm_private *priv = obj->dev->dev_private;
790 might_sleep();
791 WARN_ON(!msm_gem_is_locked(obj));
792 WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
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);
802 void msm_gem_active_put(struct drm_gem_object *obj)
804 struct msm_gem_object *msm_obj = to_msm_bo(obj);
806 might_sleep();
807 WARN_ON(!msm_gem_is_locked(obj));
809 if (--msm_obj->active_count == 0) {
810 update_inactive(msm_obj);
814 static void update_inactive(struct msm_gem_object *msm_obj)
816 struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
818 mutex_lock(&priv->mm_lock);
819 WARN_ON(msm_obj->active_count != 0);
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);
827 mutex_unlock(&priv->mm_lock);
830 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
832 bool write = !!(op & MSM_PREP_WRITE);
833 unsigned long remain =
834 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
835 long ret;
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;
844 /* TODO cache maintenance */
846 return 0;
849 int msm_gem_cpu_fini(struct drm_gem_object *obj)
851 /* TODO cache maintenance */
852 return 0;
855 #ifdef CONFIG_DEBUG_FS
856 static void describe_fence(struct dma_fence *fence, const char *type,
857 struct seq_file *m)
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);
866 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
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;
876 msm_gem_lock(obj);
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;
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);
896 seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
898 if (!list_empty(&msm_obj->vmas)) {
900 seq_puts(m, " vmas:");
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;
913 name = aspace->name;
914 } else {
915 name = comm = NULL;
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);
925 seq_puts(m, "\n");
928 rcu_read_lock();
929 fobj = rcu_dereference(robj->fence);
930 if (fobj) {
931 unsigned int i, shared_count = fobj->shared_count;
933 for (i = 0; i < shared_count; i++) {
934 fence = rcu_dereference(fobj->shared[i]);
935 describe_fence(fence, "Shared", m);
939 fence = rcu_dereference(robj->fence_excl);
940 if (fence)
941 describe_fence(fence, "Exclusive", m);
942 rcu_read_unlock();
944 msm_gem_unlock(obj);
947 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
949 struct msm_gem_object *msm_obj;
950 int count = 0;
951 size_t size = 0;
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;
962 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
964 #endif
966 /* don't call directly! Use drm_gem_object_put_locked() and friends */
967 void msm_gem_free_object(struct drm_gem_object *obj)
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;
973 mutex_lock(&priv->mm_lock);
974 list_del(&msm_obj->mm_list);
975 mutex_unlock(&priv->mm_lock);
977 msm_gem_lock(obj);
979 /* object should not be on active list: */
980 WARN_ON(is_active(msm_obj));
982 put_iova_spaces(obj);
984 if (obj->import_attach) {
985 WARN_ON(msm_obj->vaddr);
987 /* Don't drop the pages for imported dmabuf, as they are not
988 * ours, just free the array we allocated:
990 if (msm_obj->pages)
991 kvfree(msm_obj->pages);
993 put_iova_vmas(obj);
995 /* dma_buf_detach() grabs resv lock, so we need to unlock
996 * prior to drm_prime_gem_destroy
998 msm_gem_unlock(obj);
1000 drm_prime_gem_destroy(obj, msm_obj->sgt);
1001 } else {
1002 msm_gem_vunmap(obj);
1003 put_pages(obj);
1004 put_iova_vmas(obj);
1005 msm_gem_unlock(obj);
1008 drm_gem_object_release(obj);
1010 kfree(msm_obj);
1013 /* convenience method to construct a GEM buffer object, and userspace handle */
1014 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1015 uint32_t size, uint32_t flags, uint32_t *handle,
1016 char *name)
1018 struct drm_gem_object *obj;
1019 int ret;
1021 obj = msm_gem_new(dev, size, flags);
1023 if (IS_ERR(obj))
1024 return PTR_ERR(obj);
1026 if (name)
1027 msm_gem_object_set_name(obj, "%s", name);
1029 ret = drm_gem_handle_create(file, obj, handle);
1031 /* drop reference from allocate - handle holds it now */
1032 drm_gem_object_put(obj);
1034 return ret;
1037 static const struct vm_operations_struct vm_ops = {
1038 .fault = msm_gem_fault,
1039 .open = drm_gem_vm_open,
1040 .close = drm_gem_vm_close,
1043 static const struct drm_gem_object_funcs msm_gem_object_funcs = {
1044 .free = msm_gem_free_object,
1045 .pin = msm_gem_prime_pin,
1046 .unpin = msm_gem_prime_unpin,
1047 .get_sg_table = msm_gem_prime_get_sg_table,
1048 .vmap = msm_gem_prime_vmap,
1049 .vunmap = msm_gem_prime_vunmap,
1050 .vm_ops = &vm_ops,
1053 static int msm_gem_new_impl(struct drm_device *dev,
1054 uint32_t size, uint32_t flags,
1055 struct drm_gem_object **obj)
1057 struct msm_gem_object *msm_obj;
1059 switch (flags & MSM_BO_CACHE_MASK) {
1060 case MSM_BO_UNCACHED:
1061 case MSM_BO_CACHED:
1062 case MSM_BO_WC:
1063 break;
1064 default:
1065 DRM_DEV_ERROR(dev->dev, "invalid cache flag: %x\n",
1066 (flags & MSM_BO_CACHE_MASK));
1067 return -EINVAL;
1070 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
1071 if (!msm_obj)
1072 return -ENOMEM;
1074 msm_obj->flags = flags;
1075 msm_obj->madv = MSM_MADV_WILLNEED;
1077 INIT_LIST_HEAD(&msm_obj->submit_entry);
1078 INIT_LIST_HEAD(&msm_obj->vmas);
1080 *obj = &msm_obj->base;
1081 (*obj)->funcs = &msm_gem_object_funcs;
1083 return 0;
1086 static struct drm_gem_object *_msm_gem_new(struct drm_device *dev,
1087 uint32_t size, uint32_t flags, bool struct_mutex_locked)
1089 struct msm_drm_private *priv = dev->dev_private;
1090 struct msm_gem_object *msm_obj;
1091 struct drm_gem_object *obj = NULL;
1092 bool use_vram = false;
1093 int ret;
1095 size = PAGE_ALIGN(size);
1097 if (!msm_use_mmu(dev))
1098 use_vram = true;
1099 else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
1100 use_vram = true;
1102 if (WARN_ON(use_vram && !priv->vram.size))
1103 return ERR_PTR(-EINVAL);
1105 /* Disallow zero sized objects as they make the underlying
1106 * infrastructure grumpy
1108 if (size == 0)
1109 return ERR_PTR(-EINVAL);
1111 ret = msm_gem_new_impl(dev, size, flags, &obj);
1112 if (ret)
1113 goto fail;
1115 msm_obj = to_msm_bo(obj);
1117 if (use_vram) {
1118 struct msm_gem_vma *vma;
1119 struct page **pages;
1121 drm_gem_private_object_init(dev, obj, size);
1123 msm_gem_lock(obj);
1125 vma = add_vma(obj, NULL);
1126 msm_gem_unlock(obj);
1127 if (IS_ERR(vma)) {
1128 ret = PTR_ERR(vma);
1129 goto fail;
1132 to_msm_bo(obj)->vram_node = &vma->node;
1134 msm_gem_lock(obj);
1135 pages = get_pages(obj);
1136 msm_gem_unlock(obj);
1137 if (IS_ERR(pages)) {
1138 ret = PTR_ERR(pages);
1139 goto fail;
1142 vma->iova = physaddr(obj);
1143 } else {
1144 ret = drm_gem_object_init(dev, obj, size);
1145 if (ret)
1146 goto fail;
1148 * Our buffers are kept pinned, so allocating them from the
1149 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1150 * See comments above new_inode() why this is required _and_
1151 * expected if you're going to pin these pages.
1153 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
1156 mutex_lock(&priv->mm_lock);
1157 /* Initially obj is idle, obj->madv == WILLNEED: */
1158 list_add_tail(&msm_obj->mm_list, &priv->inactive_willneed);
1159 mutex_unlock(&priv->mm_lock);
1161 return obj;
1163 fail:
1164 if (struct_mutex_locked) {
1165 drm_gem_object_put_locked(obj);
1166 } else {
1167 drm_gem_object_put(obj);
1169 return ERR_PTR(ret);
1172 struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev,
1173 uint32_t size, uint32_t flags)
1175 return _msm_gem_new(dev, size, flags, true);
1178 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
1179 uint32_t size, uint32_t flags)
1181 return _msm_gem_new(dev, size, flags, false);
1184 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
1185 struct dma_buf *dmabuf, struct sg_table *sgt)
1187 struct msm_drm_private *priv = dev->dev_private;
1188 struct msm_gem_object *msm_obj;
1189 struct drm_gem_object *obj;
1190 uint32_t size;
1191 int ret, npages;
1193 /* if we don't have IOMMU, don't bother pretending we can import: */
1194 if (!msm_use_mmu(dev)) {
1195 DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
1196 return ERR_PTR(-EINVAL);
1199 size = PAGE_ALIGN(dmabuf->size);
1201 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
1202 if (ret)
1203 goto fail;
1205 drm_gem_private_object_init(dev, obj, size);
1207 npages = size / PAGE_SIZE;
1209 msm_obj = to_msm_bo(obj);
1210 msm_gem_lock(obj);
1211 msm_obj->sgt = sgt;
1212 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1213 if (!msm_obj->pages) {
1214 msm_gem_unlock(obj);
1215 ret = -ENOMEM;
1216 goto fail;
1219 ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
1220 if (ret) {
1221 msm_gem_unlock(obj);
1222 goto fail;
1225 msm_gem_unlock(obj);
1227 mutex_lock(&priv->mm_lock);
1228 list_add_tail(&msm_obj->mm_list, &priv->inactive_willneed);
1229 mutex_unlock(&priv->mm_lock);
1231 return obj;
1233 fail:
1234 drm_gem_object_put(obj);
1235 return ERR_PTR(ret);
1238 static void *_msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1239 uint32_t flags, struct msm_gem_address_space *aspace,
1240 struct drm_gem_object **bo, uint64_t *iova, bool locked)
1242 void *vaddr;
1243 struct drm_gem_object *obj = _msm_gem_new(dev, size, flags, locked);
1244 int ret;
1246 if (IS_ERR(obj))
1247 return ERR_CAST(obj);
1249 if (iova) {
1250 ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
1251 if (ret)
1252 goto err;
1255 vaddr = msm_gem_get_vaddr(obj);
1256 if (IS_ERR(vaddr)) {
1257 msm_gem_unpin_iova(obj, aspace);
1258 ret = PTR_ERR(vaddr);
1259 goto err;
1262 if (bo)
1263 *bo = obj;
1265 return vaddr;
1266 err:
1267 if (locked)
1268 drm_gem_object_put_locked(obj);
1269 else
1270 drm_gem_object_put(obj);
1272 return ERR_PTR(ret);
1276 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1277 uint32_t flags, struct msm_gem_address_space *aspace,
1278 struct drm_gem_object **bo, uint64_t *iova)
1280 return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, false);
1283 void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size,
1284 uint32_t flags, struct msm_gem_address_space *aspace,
1285 struct drm_gem_object **bo, uint64_t *iova)
1287 return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, true);
1290 void msm_gem_kernel_put(struct drm_gem_object *bo,
1291 struct msm_gem_address_space *aspace, bool locked)
1293 if (IS_ERR_OR_NULL(bo))
1294 return;
1296 msm_gem_put_vaddr(bo);
1297 msm_gem_unpin_iova(bo, aspace);
1299 if (locked)
1300 drm_gem_object_put_locked(bo);
1301 else
1302 drm_gem_object_put(bo);
1305 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
1307 struct msm_gem_object *msm_obj = to_msm_bo(bo);
1308 va_list ap;
1310 if (!fmt)
1311 return;
1313 va_start(ap, fmt);
1314 vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);
1315 va_end(ap);