1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
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>
16 #include "msm_fence.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) +
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;
74 p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
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);
86 paddr = physaddr(obj);
87 for (i = 0; i < npages; i++) {
88 p[i] = phys_to_page(paddr);
95 static struct page **get_pages(struct drm_gem_object *obj)
97 struct msm_gem_object *msm_obj = to_msm_bo(obj);
99 GEM_WARN_ON(!msm_gem_is_locked(obj));
101 if (!msm_obj->pages) {
102 struct drm_device *dev = obj->dev;
104 int npages = obj->size >> PAGE_SHIFT;
107 p = drm_gem_get_pages(obj);
109 p = get_pages_vram(obj, npages);
112 DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n",
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");
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);
134 GEM_WARN_ON(msm_obj->active_count);
135 update_inactive(msm_obj);
138 return msm_obj->pages;
141 static void put_pages_vram(struct drm_gem_object *obj)
143 struct msm_gem_object *msm_obj = to_msm_bo(obj);
144 struct msm_drm_private *priv = obj->dev->dev_private;
146 spin_lock(&priv->vram.lock);
147 drm_mm_remove_node(msm_obj->vram_node);
148 spin_unlock(&priv->vram.lock);
150 kvfree(msm_obj->pages);
153 static void put_pages(struct drm_gem_object *obj)
155 struct msm_gem_object *msm_obj = to_msm_bo(obj);
157 if (msm_obj->pages) {
159 /* For non-cached buffers, ensure the new
160 * pages are clean because display controller,
161 * GPU, etc. are not coherent:
163 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
164 sync_for_cpu(msm_obj);
166 sg_free_table(msm_obj->sgt);
172 drm_gem_put_pages(obj, msm_obj->pages, true, false);
176 msm_obj->pages = NULL;
180 struct page **msm_gem_get_pages(struct drm_gem_object *obj)
182 struct msm_gem_object *msm_obj = to_msm_bo(obj);
187 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
189 return ERR_PTR(-EBUSY);
195 msm_obj->pin_count++;
196 update_inactive(msm_obj);
203 void msm_gem_put_pages(struct drm_gem_object *obj)
205 struct msm_gem_object *msm_obj = to_msm_bo(obj);
208 msm_obj->pin_count--;
209 GEM_WARN_ON(msm_obj->pin_count < 0);
210 update_inactive(msm_obj);
214 static pgprot_t msm_gem_pgprot(struct msm_gem_object *msm_obj, pgprot_t prot)
216 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
217 return pgprot_writecombine(prot);
221 int msm_gem_mmap_obj(struct drm_gem_object *obj,
222 struct vm_area_struct *vma)
224 struct msm_gem_object *msm_obj = to_msm_bo(obj);
226 vma->vm_flags &= ~VM_PFNMAP;
227 vma->vm_flags |= VM_MIXEDMAP;
228 vma->vm_page_prot = msm_gem_pgprot(msm_obj, vm_get_page_prot(vma->vm_flags));
233 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
237 ret = drm_gem_mmap(filp, vma);
239 DBG("mmap failed: %d", ret);
243 return msm_gem_mmap_obj(vma->vm_private_data, vma);
246 static vm_fault_t msm_gem_fault(struct vm_fault *vmf)
248 struct vm_area_struct *vma = vmf->vma;
249 struct drm_gem_object *obj = vma->vm_private_data;
250 struct msm_gem_object *msm_obj = to_msm_bo(obj);
258 * vm_ops.open/drm_gem_mmap_obj and close get and put
259 * a reference on obj. So, we dont need to hold one here.
261 err = msm_gem_lock_interruptible(obj);
263 ret = VM_FAULT_NOPAGE;
267 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
269 return VM_FAULT_SIGBUS;
272 /* make sure we have pages attached now */
273 pages = get_pages(obj);
275 ret = vmf_error(PTR_ERR(pages));
279 /* We don't use vmf->pgoff since that has the fake offset: */
280 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
282 pfn = page_to_pfn(pages[pgoff]);
284 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
285 pfn, pfn << PAGE_SHIFT);
287 ret = vmf_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
294 /** get mmap offset */
295 static uint64_t mmap_offset(struct drm_gem_object *obj)
297 struct drm_device *dev = obj->dev;
300 GEM_WARN_ON(!msm_gem_is_locked(obj));
302 /* Make it mmapable */
303 ret = drm_gem_create_mmap_offset(obj);
306 DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
310 return drm_vma_node_offset_addr(&obj->vma_node);
313 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
318 offset = mmap_offset(obj);
323 static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
324 struct msm_gem_address_space *aspace)
326 struct msm_gem_object *msm_obj = to_msm_bo(obj);
327 struct msm_gem_vma *vma;
329 GEM_WARN_ON(!msm_gem_is_locked(obj));
331 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
333 return ERR_PTR(-ENOMEM);
335 vma->aspace = aspace;
337 list_add_tail(&vma->list, &msm_obj->vmas);
342 static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj,
343 struct msm_gem_address_space *aspace)
345 struct msm_gem_object *msm_obj = to_msm_bo(obj);
346 struct msm_gem_vma *vma;
348 GEM_WARN_ON(!msm_gem_is_locked(obj));
350 list_for_each_entry(vma, &msm_obj->vmas, list) {
351 if (vma->aspace == aspace)
358 static void del_vma(struct msm_gem_vma *vma)
363 list_del(&vma->list);
368 * If close is true, this also closes the VMA (releasing the allocated
369 * iova range) in addition to removing the iommu mapping. In the eviction
370 * case (!close), we keep the iova allocated, but only remove the iommu
374 put_iova_spaces(struct drm_gem_object *obj, bool close)
376 struct msm_gem_object *msm_obj = to_msm_bo(obj);
377 struct msm_gem_vma *vma;
379 GEM_WARN_ON(!msm_gem_is_locked(obj));
381 list_for_each_entry(vma, &msm_obj->vmas, list) {
383 msm_gem_purge_vma(vma->aspace, vma);
385 msm_gem_close_vma(vma->aspace, vma);
390 /* Called with msm_obj locked */
392 put_iova_vmas(struct drm_gem_object *obj)
394 struct msm_gem_object *msm_obj = to_msm_bo(obj);
395 struct msm_gem_vma *vma, *tmp;
397 GEM_WARN_ON(!msm_gem_is_locked(obj));
399 list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
404 static int get_iova_locked(struct drm_gem_object *obj,
405 struct msm_gem_address_space *aspace, uint64_t *iova,
406 u64 range_start, u64 range_end)
408 struct msm_gem_vma *vma;
411 GEM_WARN_ON(!msm_gem_is_locked(obj));
413 vma = lookup_vma(obj, aspace);
416 vma = add_vma(obj, aspace);
420 ret = msm_gem_init_vma(aspace, vma, obj->size >> PAGE_SHIFT,
421 range_start, range_end);
432 static int msm_gem_pin_iova(struct drm_gem_object *obj,
433 struct msm_gem_address_space *aspace)
435 struct msm_gem_object *msm_obj = to_msm_bo(obj);
436 struct msm_gem_vma *vma;
438 int ret, prot = IOMMU_READ;
440 if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
443 if (msm_obj->flags & MSM_BO_MAP_PRIV)
446 if (msm_obj->flags & MSM_BO_CACHED_COHERENT)
449 GEM_WARN_ON(!msm_gem_is_locked(obj));
451 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
454 vma = lookup_vma(obj, aspace);
455 if (GEM_WARN_ON(!vma))
458 pages = get_pages(obj);
460 return PTR_ERR(pages);
462 ret = msm_gem_map_vma(aspace, vma, prot,
463 msm_obj->sgt, obj->size >> PAGE_SHIFT);
466 msm_obj->pin_count++;
471 static int get_and_pin_iova_range_locked(struct drm_gem_object *obj,
472 struct msm_gem_address_space *aspace, uint64_t *iova,
473 u64 range_start, u64 range_end)
478 GEM_WARN_ON(!msm_gem_is_locked(obj));
480 ret = get_iova_locked(obj, aspace, &local,
481 range_start, range_end);
484 ret = msm_gem_pin_iova(obj, aspace);
493 * get iova and pin it. Should have a matching put
494 * limits iova to specified range (in pages)
496 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
497 struct msm_gem_address_space *aspace, uint64_t *iova,
498 u64 range_start, u64 range_end)
503 ret = get_and_pin_iova_range_locked(obj, aspace, iova, range_start, range_end);
509 int msm_gem_get_and_pin_iova_locked(struct drm_gem_object *obj,
510 struct msm_gem_address_space *aspace, uint64_t *iova)
512 return get_and_pin_iova_range_locked(obj, aspace, iova, 0, U64_MAX);
515 /* get iova and pin it. Should have a matching put */
516 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
517 struct msm_gem_address_space *aspace, uint64_t *iova)
519 return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX);
523 * Get an iova but don't pin it. Doesn't need a put because iovas are currently
524 * valid for the life of the object
526 int msm_gem_get_iova(struct drm_gem_object *obj,
527 struct msm_gem_address_space *aspace, uint64_t *iova)
532 ret = get_iova_locked(obj, aspace, iova, 0, U64_MAX);
538 /* get iova without taking a reference, used in places where you have
539 * already done a 'msm_gem_get_and_pin_iova' or 'msm_gem_get_iova'
541 uint64_t msm_gem_iova(struct drm_gem_object *obj,
542 struct msm_gem_address_space *aspace)
544 struct msm_gem_vma *vma;
547 vma = lookup_vma(obj, aspace);
551 return vma ? vma->iova : 0;
555 * Locked variant of msm_gem_unpin_iova()
557 void msm_gem_unpin_iova_locked(struct drm_gem_object *obj,
558 struct msm_gem_address_space *aspace)
560 struct msm_gem_object *msm_obj = to_msm_bo(obj);
561 struct msm_gem_vma *vma;
563 GEM_WARN_ON(!msm_gem_is_locked(obj));
565 vma = lookup_vma(obj, aspace);
567 if (!GEM_WARN_ON(!vma)) {
568 msm_gem_unmap_vma(aspace, vma);
570 msm_obj->pin_count--;
571 GEM_WARN_ON(msm_obj->pin_count < 0);
573 update_inactive(msm_obj);
578 * Unpin a iova by updating the reference counts. The memory isn't actually
579 * purged until something else (shrinker, mm_notifier, destroy, etc) decides
582 void msm_gem_unpin_iova(struct drm_gem_object *obj,
583 struct msm_gem_address_space *aspace)
586 msm_gem_unpin_iova_locked(obj, aspace);
590 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
591 struct drm_mode_create_dumb *args)
593 args->pitch = align_pitch(args->width, args->bpp);
594 args->size = PAGE_ALIGN(args->pitch * args->height);
595 return msm_gem_new_handle(dev, file, args->size,
596 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb");
599 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
600 uint32_t handle, uint64_t *offset)
602 struct drm_gem_object *obj;
605 /* GEM does all our handle to object mapping */
606 obj = drm_gem_object_lookup(file, handle);
612 *offset = msm_gem_mmap_offset(obj);
614 drm_gem_object_put(obj);
620 static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
622 struct msm_gem_object *msm_obj = to_msm_bo(obj);
625 GEM_WARN_ON(!msm_gem_is_locked(obj));
627 if (obj->import_attach)
628 return ERR_PTR(-ENODEV);
630 if (GEM_WARN_ON(msm_obj->madv > madv)) {
631 DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n",
632 msm_obj->madv, madv);
633 return ERR_PTR(-EBUSY);
636 /* increment vmap_count *before* vmap() call, so shrinker can
637 * check vmap_count (is_vunmapable()) outside of msm_obj lock.
638 * This guarantees that we won't try to msm_gem_vunmap() this
639 * same object from within the vmap() call (while we already
642 msm_obj->vmap_count++;
644 if (!msm_obj->vaddr) {
645 struct page **pages = get_pages(obj);
647 ret = PTR_ERR(pages);
650 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
651 VM_MAP, msm_gem_pgprot(msm_obj, PAGE_KERNEL));
652 if (msm_obj->vaddr == NULL) {
657 update_inactive(msm_obj);
660 return msm_obj->vaddr;
663 msm_obj->vmap_count--;
667 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
669 return get_vaddr(obj, MSM_MADV_WILLNEED);
672 void *msm_gem_get_vaddr(struct drm_gem_object *obj)
677 ret = msm_gem_get_vaddr_locked(obj);
684 * Don't use this! It is for the very special case of dumping
685 * submits from GPU hangs or faults, were the bo may already
686 * be MSM_MADV_DONTNEED, but we know the buffer is still on the
689 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
691 return get_vaddr(obj, __MSM_MADV_PURGED);
694 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
696 struct msm_gem_object *msm_obj = to_msm_bo(obj);
698 GEM_WARN_ON(!msm_gem_is_locked(obj));
699 GEM_WARN_ON(msm_obj->vmap_count < 1);
701 msm_obj->vmap_count--;
704 void msm_gem_put_vaddr(struct drm_gem_object *obj)
707 msm_gem_put_vaddr_locked(obj);
711 /* Update madvise status, returns true if not purged, else
714 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
716 struct msm_gem_object *msm_obj = to_msm_bo(obj);
720 if (msm_obj->madv != __MSM_MADV_PURGED)
721 msm_obj->madv = madv;
723 madv = msm_obj->madv;
725 /* If the obj is inactive, we might need to move it
726 * between inactive lists
728 if (msm_obj->active_count == 0)
729 update_inactive(msm_obj);
733 return (madv != __MSM_MADV_PURGED);
736 void msm_gem_purge(struct drm_gem_object *obj)
738 struct drm_device *dev = obj->dev;
739 struct msm_gem_object *msm_obj = to_msm_bo(obj);
741 GEM_WARN_ON(!msm_gem_is_locked(obj));
742 GEM_WARN_ON(!is_purgeable(msm_obj));
744 /* Get rid of any iommu mapping(s): */
745 put_iova_spaces(obj, true);
749 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
755 msm_obj->madv = __MSM_MADV_PURGED;
756 update_inactive(msm_obj);
758 drm_gem_free_mmap_offset(obj);
760 /* Our goal here is to return as much of the memory as
761 * is possible back to the system as we are called from OOM.
762 * To do this we must instruct the shmfs to drop all of its
763 * backing pages, *now*.
765 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
767 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
772 * Unpin the backing pages and make them available to be swapped out.
774 void msm_gem_evict(struct drm_gem_object *obj)
776 struct drm_device *dev = obj->dev;
777 struct msm_gem_object *msm_obj = to_msm_bo(obj);
779 GEM_WARN_ON(!msm_gem_is_locked(obj));
780 GEM_WARN_ON(is_unevictable(msm_obj));
781 GEM_WARN_ON(!msm_obj->evictable);
782 GEM_WARN_ON(msm_obj->active_count);
784 /* Get rid of any iommu mapping(s): */
785 put_iova_spaces(obj, false);
787 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
791 update_inactive(msm_obj);
794 void msm_gem_vunmap(struct drm_gem_object *obj)
796 struct msm_gem_object *msm_obj = to_msm_bo(obj);
798 GEM_WARN_ON(!msm_gem_is_locked(obj));
800 if (!msm_obj->vaddr || GEM_WARN_ON(!is_vunmapable(msm_obj)))
803 vunmap(msm_obj->vaddr);
804 msm_obj->vaddr = NULL;
807 /* must be called before _move_to_active().. */
808 int msm_gem_sync_object(struct drm_gem_object *obj,
809 struct msm_fence_context *fctx, bool exclusive)
811 struct dma_resv_list *fobj;
812 struct dma_fence *fence;
815 fobj = dma_resv_shared_list(obj->resv);
816 if (!fobj || (fobj->shared_count == 0)) {
817 fence = dma_resv_excl_fence(obj->resv);
818 /* don't need to wait on our own fences, since ring is fifo */
819 if (fence && (fence->context != fctx->context)) {
820 ret = dma_fence_wait(fence, true);
826 if (!exclusive || !fobj)
829 for (i = 0; i < fobj->shared_count; i++) {
830 fence = rcu_dereference_protected(fobj->shared[i],
831 dma_resv_held(obj->resv));
832 if (fence->context != fctx->context) {
833 ret = dma_fence_wait(fence, true);
842 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu)
844 struct msm_gem_object *msm_obj = to_msm_bo(obj);
845 struct msm_drm_private *priv = obj->dev->dev_private;
848 GEM_WARN_ON(!msm_gem_is_locked(obj));
849 GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
850 GEM_WARN_ON(msm_obj->dontneed);
851 GEM_WARN_ON(!msm_obj->sgt);
853 if (msm_obj->active_count++ == 0) {
854 mutex_lock(&priv->mm_lock);
855 if (msm_obj->evictable)
856 mark_unevictable(msm_obj);
857 list_del(&msm_obj->mm_list);
858 list_add_tail(&msm_obj->mm_list, &gpu->active_list);
859 mutex_unlock(&priv->mm_lock);
863 void msm_gem_active_put(struct drm_gem_object *obj)
865 struct msm_gem_object *msm_obj = to_msm_bo(obj);
868 GEM_WARN_ON(!msm_gem_is_locked(obj));
870 if (--msm_obj->active_count == 0) {
871 update_inactive(msm_obj);
875 static void update_inactive(struct msm_gem_object *msm_obj)
877 struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
879 GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
881 if (msm_obj->active_count != 0)
884 mutex_lock(&priv->mm_lock);
886 if (msm_obj->dontneed)
887 mark_unpurgeable(msm_obj);
888 if (msm_obj->evictable)
889 mark_unevictable(msm_obj);
891 list_del(&msm_obj->mm_list);
892 if ((msm_obj->madv == MSM_MADV_WILLNEED) && msm_obj->sgt) {
893 list_add_tail(&msm_obj->mm_list, &priv->inactive_willneed);
894 mark_evictable(msm_obj);
895 } else if (msm_obj->madv == MSM_MADV_DONTNEED) {
896 list_add_tail(&msm_obj->mm_list, &priv->inactive_dontneed);
897 mark_purgeable(msm_obj);
899 GEM_WARN_ON((msm_obj->madv != __MSM_MADV_PURGED) && msm_obj->sgt);
900 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
903 mutex_unlock(&priv->mm_lock);
906 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
908 bool write = !!(op & MSM_PREP_WRITE);
909 unsigned long remain =
910 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
913 ret = dma_resv_wait_timeout(obj->resv, write, true, remain);
915 return remain == 0 ? -EBUSY : -ETIMEDOUT;
919 /* TODO cache maintenance */
924 int msm_gem_cpu_fini(struct drm_gem_object *obj)
926 /* TODO cache maintenance */
930 #ifdef CONFIG_DEBUG_FS
931 static void describe_fence(struct dma_fence *fence, const char *type,
934 if (!dma_fence_is_signaled(fence))
935 seq_printf(m, "\t%9s: %s %s seq %llu\n", type,
936 fence->ops->get_driver_name(fence),
937 fence->ops->get_timeline_name(fence),
941 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
942 struct msm_gem_stats *stats)
944 struct msm_gem_object *msm_obj = to_msm_bo(obj);
945 struct dma_resv *robj = obj->resv;
946 struct dma_resv_list *fobj;
947 struct dma_fence *fence;
948 struct msm_gem_vma *vma;
949 uint64_t off = drm_vma_node_start(&obj->vma_node);
955 stats->all.size += obj->size;
957 if (is_active(msm_obj)) {
958 stats->active.count++;
959 stats->active.size += obj->size;
962 if (msm_obj->pages) {
963 stats->resident.count++;
964 stats->resident.size += obj->size;
967 switch (msm_obj->madv) {
968 case __MSM_MADV_PURGED:
969 stats->purged.count++;
970 stats->purged.size += obj->size;
973 case MSM_MADV_DONTNEED:
974 stats->purgeable.count++;
975 stats->purgeable.size += obj->size;
978 case MSM_MADV_WILLNEED:
984 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p",
985 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
986 obj->name, kref_read(&obj->refcount),
987 off, msm_obj->vaddr);
989 seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
991 if (!list_empty(&msm_obj->vmas)) {
993 seq_puts(m, " vmas:");
995 list_for_each_entry(vma, &msm_obj->vmas, list) {
996 const char *name, *comm;
998 struct msm_gem_address_space *aspace = vma->aspace;
999 struct task_struct *task =
1000 get_pid_task(aspace->pid, PIDTYPE_PID);
1002 comm = kstrdup(task->comm, GFP_KERNEL);
1006 name = aspace->name;
1010 seq_printf(m, " [%s%s%s: aspace=%p, %08llx,%s,inuse=%d]",
1011 name, comm ? ":" : "", comm ? comm : "",
1012 vma->aspace, vma->iova,
1013 vma->mapped ? "mapped" : "unmapped",
1022 fobj = dma_resv_shared_list(robj);
1024 unsigned int i, shared_count = fobj->shared_count;
1026 for (i = 0; i < shared_count; i++) {
1027 fence = rcu_dereference(fobj->shared[i]);
1028 describe_fence(fence, "Shared", m);
1032 fence = dma_resv_excl_fence(robj);
1034 describe_fence(fence, "Exclusive", m);
1037 msm_gem_unlock(obj);
1040 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
1042 struct msm_gem_stats stats = {};
1043 struct msm_gem_object *msm_obj;
1045 seq_puts(m, " flags id ref offset kaddr size madv name\n");
1046 list_for_each_entry(msm_obj, list, node) {
1047 struct drm_gem_object *obj = &msm_obj->base;
1049 msm_gem_describe(obj, m, &stats);
1052 seq_printf(m, "Total: %4d objects, %9zu bytes\n",
1053 stats.all.count, stats.all.size);
1054 seq_printf(m, "Active: %4d objects, %9zu bytes\n",
1055 stats.active.count, stats.active.size);
1056 seq_printf(m, "Resident: %4d objects, %9zu bytes\n",
1057 stats.resident.count, stats.resident.size);
1058 seq_printf(m, "Purgeable: %4d objects, %9zu bytes\n",
1059 stats.purgeable.count, stats.purgeable.size);
1060 seq_printf(m, "Purged: %4d objects, %9zu bytes\n",
1061 stats.purged.count, stats.purged.size);
1065 /* don't call directly! Use drm_gem_object_put_locked() and friends */
1066 void msm_gem_free_object(struct drm_gem_object *obj)
1068 struct msm_gem_object *msm_obj = to_msm_bo(obj);
1069 struct drm_device *dev = obj->dev;
1070 struct msm_drm_private *priv = dev->dev_private;
1072 mutex_lock(&priv->obj_lock);
1073 list_del(&msm_obj->node);
1074 mutex_unlock(&priv->obj_lock);
1076 mutex_lock(&priv->mm_lock);
1077 if (msm_obj->dontneed)
1078 mark_unpurgeable(msm_obj);
1079 list_del(&msm_obj->mm_list);
1080 mutex_unlock(&priv->mm_lock);
1084 /* object should not be on active list: */
1085 GEM_WARN_ON(is_active(msm_obj));
1087 put_iova_spaces(obj, true);
1089 if (obj->import_attach) {
1090 GEM_WARN_ON(msm_obj->vaddr);
1092 /* Don't drop the pages for imported dmabuf, as they are not
1093 * ours, just free the array we allocated:
1095 kvfree(msm_obj->pages);
1099 /* dma_buf_detach() grabs resv lock, so we need to unlock
1100 * prior to drm_prime_gem_destroy
1102 msm_gem_unlock(obj);
1104 drm_prime_gem_destroy(obj, msm_obj->sgt);
1106 msm_gem_vunmap(obj);
1109 msm_gem_unlock(obj);
1112 drm_gem_object_release(obj);
1117 /* convenience method to construct a GEM buffer object, and userspace handle */
1118 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1119 uint32_t size, uint32_t flags, uint32_t *handle,
1122 struct drm_gem_object *obj;
1125 obj = msm_gem_new(dev, size, flags);
1128 return PTR_ERR(obj);
1131 msm_gem_object_set_name(obj, "%s", name);
1133 ret = drm_gem_handle_create(file, obj, handle);
1135 /* drop reference from allocate - handle holds it now */
1136 drm_gem_object_put(obj);
1141 static const struct vm_operations_struct vm_ops = {
1142 .fault = msm_gem_fault,
1143 .open = drm_gem_vm_open,
1144 .close = drm_gem_vm_close,
1147 static const struct drm_gem_object_funcs msm_gem_object_funcs = {
1148 .free = msm_gem_free_object,
1149 .pin = msm_gem_prime_pin,
1150 .unpin = msm_gem_prime_unpin,
1151 .get_sg_table = msm_gem_prime_get_sg_table,
1152 .vmap = msm_gem_prime_vmap,
1153 .vunmap = msm_gem_prime_vunmap,
1157 static int msm_gem_new_impl(struct drm_device *dev,
1158 uint32_t size, uint32_t flags,
1159 struct drm_gem_object **obj)
1161 struct msm_drm_private *priv = dev->dev_private;
1162 struct msm_gem_object *msm_obj;
1164 switch (flags & MSM_BO_CACHE_MASK) {
1165 case MSM_BO_UNCACHED:
1169 case MSM_BO_CACHED_COHERENT:
1170 if (priv->has_cached_coherent)
1174 DRM_DEV_ERROR(dev->dev, "invalid cache flag: %x\n",
1175 (flags & MSM_BO_CACHE_MASK));
1179 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
1183 msm_obj->flags = flags;
1184 msm_obj->madv = MSM_MADV_WILLNEED;
1186 INIT_LIST_HEAD(&msm_obj->submit_entry);
1187 INIT_LIST_HEAD(&msm_obj->vmas);
1189 *obj = &msm_obj->base;
1190 (*obj)->funcs = &msm_gem_object_funcs;
1195 static struct drm_gem_object *_msm_gem_new(struct drm_device *dev,
1196 uint32_t size, uint32_t flags, bool struct_mutex_locked)
1198 struct msm_drm_private *priv = dev->dev_private;
1199 struct msm_gem_object *msm_obj;
1200 struct drm_gem_object *obj = NULL;
1201 bool use_vram = false;
1204 size = PAGE_ALIGN(size);
1206 if (!msm_use_mmu(dev))
1208 else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
1211 if (GEM_WARN_ON(use_vram && !priv->vram.size))
1212 return ERR_PTR(-EINVAL);
1214 /* Disallow zero sized objects as they make the underlying
1215 * infrastructure grumpy
1218 return ERR_PTR(-EINVAL);
1220 ret = msm_gem_new_impl(dev, size, flags, &obj);
1224 msm_obj = to_msm_bo(obj);
1227 struct msm_gem_vma *vma;
1228 struct page **pages;
1230 drm_gem_private_object_init(dev, obj, size);
1234 vma = add_vma(obj, NULL);
1235 msm_gem_unlock(obj);
1241 to_msm_bo(obj)->vram_node = &vma->node;
1243 /* Call chain get_pages() -> update_inactive() tries to
1244 * access msm_obj->mm_list, but it is not initialized yet.
1245 * To avoid NULL pointer dereference error, initialize
1246 * mm_list to be empty.
1248 INIT_LIST_HEAD(&msm_obj->mm_list);
1251 pages = get_pages(obj);
1252 msm_gem_unlock(obj);
1253 if (IS_ERR(pages)) {
1254 ret = PTR_ERR(pages);
1258 vma->iova = physaddr(obj);
1260 ret = drm_gem_object_init(dev, obj, size);
1264 * Our buffers are kept pinned, so allocating them from the
1265 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1266 * See comments above new_inode() why this is required _and_
1267 * expected if you're going to pin these pages.
1269 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
1272 mutex_lock(&priv->mm_lock);
1273 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
1274 mutex_unlock(&priv->mm_lock);
1276 mutex_lock(&priv->obj_lock);
1277 list_add_tail(&msm_obj->node, &priv->objects);
1278 mutex_unlock(&priv->obj_lock);
1283 if (struct_mutex_locked) {
1284 drm_gem_object_put_locked(obj);
1286 drm_gem_object_put(obj);
1288 return ERR_PTR(ret);
1291 struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev,
1292 uint32_t size, uint32_t flags)
1294 return _msm_gem_new(dev, size, flags, true);
1297 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
1298 uint32_t size, uint32_t flags)
1300 return _msm_gem_new(dev, size, flags, false);
1303 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
1304 struct dma_buf *dmabuf, struct sg_table *sgt)
1306 struct msm_drm_private *priv = dev->dev_private;
1307 struct msm_gem_object *msm_obj;
1308 struct drm_gem_object *obj;
1312 /* if we don't have IOMMU, don't bother pretending we can import: */
1313 if (!msm_use_mmu(dev)) {
1314 DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
1315 return ERR_PTR(-EINVAL);
1318 size = PAGE_ALIGN(dmabuf->size);
1320 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
1324 drm_gem_private_object_init(dev, obj, size);
1326 npages = size / PAGE_SIZE;
1328 msm_obj = to_msm_bo(obj);
1331 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1332 if (!msm_obj->pages) {
1333 msm_gem_unlock(obj);
1338 ret = drm_prime_sg_to_page_array(sgt, msm_obj->pages, npages);
1340 msm_gem_unlock(obj);
1344 msm_gem_unlock(obj);
1346 mutex_lock(&priv->mm_lock);
1347 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
1348 mutex_unlock(&priv->mm_lock);
1350 mutex_lock(&priv->obj_lock);
1351 list_add_tail(&msm_obj->node, &priv->objects);
1352 mutex_unlock(&priv->obj_lock);
1357 drm_gem_object_put(obj);
1358 return ERR_PTR(ret);
1361 static void *_msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1362 uint32_t flags, struct msm_gem_address_space *aspace,
1363 struct drm_gem_object **bo, uint64_t *iova, bool locked)
1366 struct drm_gem_object *obj = _msm_gem_new(dev, size, flags, locked);
1370 return ERR_CAST(obj);
1373 ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
1378 vaddr = msm_gem_get_vaddr(obj);
1379 if (IS_ERR(vaddr)) {
1380 msm_gem_unpin_iova(obj, aspace);
1381 ret = PTR_ERR(vaddr);
1391 drm_gem_object_put_locked(obj);
1393 drm_gem_object_put(obj);
1395 return ERR_PTR(ret);
1399 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1400 uint32_t flags, struct msm_gem_address_space *aspace,
1401 struct drm_gem_object **bo, uint64_t *iova)
1403 return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, false);
1406 void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size,
1407 uint32_t flags, struct msm_gem_address_space *aspace,
1408 struct drm_gem_object **bo, uint64_t *iova)
1410 return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, true);
1413 void msm_gem_kernel_put(struct drm_gem_object *bo,
1414 struct msm_gem_address_space *aspace, bool locked)
1416 if (IS_ERR_OR_NULL(bo))
1419 msm_gem_put_vaddr(bo);
1420 msm_gem_unpin_iova(bo, aspace);
1423 drm_gem_object_put_locked(bo);
1425 drm_gem_object_put(bo);
1428 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
1430 struct msm_gem_object *msm_obj = to_msm_bo(bo);
1437 vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);