1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
7 #include <linux/spinlock.h>
8 #include <linux/shmem_fs.h>
9 #include <linux/dma-buf.h>
10 #include <linux/pfn_t.h>
12 #include <drm/drm_prime.h>
15 #include "msm_fence.h"
20 static void msm_gem_vunmap_locked(struct drm_gem_object
*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 if (get_dma_ops(dev
) && IS_ENABLED(CONFIG_ARM64
)) {
56 dma_sync_sg_for_device(dev
, msm_obj
->sgt
->sgl
,
57 msm_obj
->sgt
->nents
, DMA_BIDIRECTIONAL
);
59 dma_map_sg(dev
, msm_obj
->sgt
->sgl
,
60 msm_obj
->sgt
->nents
, DMA_BIDIRECTIONAL
);
64 static void sync_for_cpu(struct msm_gem_object
*msm_obj
)
66 struct device
*dev
= msm_obj
->base
.dev
->dev
;
68 if (get_dma_ops(dev
) && IS_ENABLED(CONFIG_ARM64
)) {
69 dma_sync_sg_for_cpu(dev
, msm_obj
->sgt
->sgl
,
70 msm_obj
->sgt
->nents
, DMA_BIDIRECTIONAL
);
72 dma_unmap_sg(dev
, msm_obj
->sgt
->sgl
,
73 msm_obj
->sgt
->nents
, DMA_BIDIRECTIONAL
);
77 /* allocate pages from VRAM carveout, used when no IOMMU: */
78 static struct page
**get_pages_vram(struct drm_gem_object
*obj
, int npages
)
80 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
81 struct msm_drm_private
*priv
= obj
->dev
->dev_private
;
86 p
= kvmalloc_array(npages
, sizeof(struct page
*), GFP_KERNEL
);
88 return ERR_PTR(-ENOMEM
);
90 spin_lock(&priv
->vram
.lock
);
91 ret
= drm_mm_insert_node(&priv
->vram
.mm
, msm_obj
->vram_node
, npages
);
92 spin_unlock(&priv
->vram
.lock
);
98 paddr
= physaddr(obj
);
99 for (i
= 0; i
< npages
; i
++) {
100 p
[i
] = phys_to_page(paddr
);
107 static struct page
**get_pages(struct drm_gem_object
*obj
)
109 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
111 if (!msm_obj
->pages
) {
112 struct drm_device
*dev
= obj
->dev
;
114 int npages
= obj
->size
>> PAGE_SHIFT
;
117 p
= drm_gem_get_pages(obj
);
119 p
= get_pages_vram(obj
, npages
);
122 DRM_DEV_ERROR(dev
->dev
, "could not get pages: %ld\n",
129 msm_obj
->sgt
= drm_prime_pages_to_sg(p
, npages
);
130 if (IS_ERR(msm_obj
->sgt
)) {
131 void *ptr
= ERR_CAST(msm_obj
->sgt
);
133 DRM_DEV_ERROR(dev
->dev
, "failed to allocate sgt\n");
138 /* For non-cached buffers, ensure the new pages are clean
139 * because display controller, GPU, etc. are not coherent:
141 if (msm_obj
->flags
& (MSM_BO_WC
|MSM_BO_UNCACHED
))
142 sync_for_device(msm_obj
);
145 return msm_obj
->pages
;
148 static void put_pages_vram(struct drm_gem_object
*obj
)
150 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
151 struct msm_drm_private
*priv
= obj
->dev
->dev_private
;
153 spin_lock(&priv
->vram
.lock
);
154 drm_mm_remove_node(msm_obj
->vram_node
);
155 spin_unlock(&priv
->vram
.lock
);
157 kvfree(msm_obj
->pages
);
160 static void put_pages(struct drm_gem_object
*obj
)
162 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
164 if (msm_obj
->pages
) {
166 /* For non-cached buffers, ensure the new
167 * pages are clean because display controller,
168 * GPU, etc. are not coherent:
170 if (msm_obj
->flags
& (MSM_BO_WC
|MSM_BO_UNCACHED
))
171 sync_for_cpu(msm_obj
);
173 sg_free_table(msm_obj
->sgt
);
178 drm_gem_put_pages(obj
, msm_obj
->pages
, true, false);
182 msm_obj
->pages
= NULL
;
186 struct page
**msm_gem_get_pages(struct drm_gem_object
*obj
)
188 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
191 mutex_lock(&msm_obj
->lock
);
193 if (WARN_ON(msm_obj
->madv
!= MSM_MADV_WILLNEED
)) {
194 mutex_unlock(&msm_obj
->lock
);
195 return ERR_PTR(-EBUSY
);
199 mutex_unlock(&msm_obj
->lock
);
203 void msm_gem_put_pages(struct drm_gem_object
*obj
)
205 /* when we start tracking the pin count, then do something here */
208 int msm_gem_mmap_obj(struct drm_gem_object
*obj
,
209 struct vm_area_struct
*vma
)
211 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
213 vma
->vm_flags
&= ~VM_PFNMAP
;
214 vma
->vm_flags
|= VM_MIXEDMAP
;
216 if (msm_obj
->flags
& MSM_BO_WC
) {
217 vma
->vm_page_prot
= pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
218 } else if (msm_obj
->flags
& MSM_BO_UNCACHED
) {
219 vma
->vm_page_prot
= pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
222 * Shunt off cached objs to shmem file so they have their own
223 * address_space (so unmap_mapping_range does what we want,
224 * in particular in the case of mmap'd dmabufs)
229 vma
->vm_file
= obj
->filp
;
231 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
237 int msm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
241 ret
= drm_gem_mmap(filp
, vma
);
243 DBG("mmap failed: %d", ret
);
247 return msm_gem_mmap_obj(vma
->vm_private_data
, vma
);
250 vm_fault_t
msm_gem_fault(struct vm_fault
*vmf
)
252 struct vm_area_struct
*vma
= vmf
->vma
;
253 struct drm_gem_object
*obj
= vma
->vm_private_data
;
254 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
262 * vm_ops.open/drm_gem_mmap_obj and close get and put
263 * a reference on obj. So, we dont need to hold one here.
265 err
= mutex_lock_interruptible(&msm_obj
->lock
);
267 ret
= VM_FAULT_NOPAGE
;
271 if (WARN_ON(msm_obj
->madv
!= MSM_MADV_WILLNEED
)) {
272 mutex_unlock(&msm_obj
->lock
);
273 return VM_FAULT_SIGBUS
;
276 /* make sure we have pages attached now */
277 pages
= get_pages(obj
);
279 ret
= vmf_error(PTR_ERR(pages
));
283 /* We don't use vmf->pgoff since that has the fake offset: */
284 pgoff
= (vmf
->address
- vma
->vm_start
) >> PAGE_SHIFT
;
286 pfn
= page_to_pfn(pages
[pgoff
]);
288 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf
->address
,
289 pfn
, pfn
<< PAGE_SHIFT
);
291 ret
= vmf_insert_mixed(vma
, vmf
->address
, __pfn_to_pfn_t(pfn
, PFN_DEV
));
293 mutex_unlock(&msm_obj
->lock
);
298 /** get mmap offset */
299 static uint64_t mmap_offset(struct drm_gem_object
*obj
)
301 struct drm_device
*dev
= obj
->dev
;
302 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
305 WARN_ON(!mutex_is_locked(&msm_obj
->lock
));
307 /* Make it mmapable */
308 ret
= drm_gem_create_mmap_offset(obj
);
311 DRM_DEV_ERROR(dev
->dev
, "could not allocate mmap offset\n");
315 return drm_vma_node_offset_addr(&obj
->vma_node
);
318 uint64_t msm_gem_mmap_offset(struct drm_gem_object
*obj
)
321 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
323 mutex_lock(&msm_obj
->lock
);
324 offset
= mmap_offset(obj
);
325 mutex_unlock(&msm_obj
->lock
);
329 static struct msm_gem_vma
*add_vma(struct drm_gem_object
*obj
,
330 struct msm_gem_address_space
*aspace
)
332 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
333 struct msm_gem_vma
*vma
;
335 WARN_ON(!mutex_is_locked(&msm_obj
->lock
));
337 vma
= kzalloc(sizeof(*vma
), GFP_KERNEL
);
339 return ERR_PTR(-ENOMEM
);
341 vma
->aspace
= aspace
;
343 list_add_tail(&vma
->list
, &msm_obj
->vmas
);
348 static struct msm_gem_vma
*lookup_vma(struct drm_gem_object
*obj
,
349 struct msm_gem_address_space
*aspace
)
351 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
352 struct msm_gem_vma
*vma
;
354 WARN_ON(!mutex_is_locked(&msm_obj
->lock
));
356 list_for_each_entry(vma
, &msm_obj
->vmas
, list
) {
357 if (vma
->aspace
== aspace
)
364 static void del_vma(struct msm_gem_vma
*vma
)
369 list_del(&vma
->list
);
373 /* Called with msm_obj->lock locked */
375 put_iova(struct drm_gem_object
*obj
)
377 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
378 struct msm_gem_vma
*vma
, *tmp
;
380 WARN_ON(!mutex_is_locked(&msm_obj
->lock
));
382 list_for_each_entry_safe(vma
, tmp
, &msm_obj
->vmas
, list
) {
384 msm_gem_purge_vma(vma
->aspace
, vma
);
385 msm_gem_close_vma(vma
->aspace
, vma
);
391 static int msm_gem_get_iova_locked(struct drm_gem_object
*obj
,
392 struct msm_gem_address_space
*aspace
, uint64_t *iova
)
394 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
395 struct msm_gem_vma
*vma
;
398 WARN_ON(!mutex_is_locked(&msm_obj
->lock
));
400 vma
= lookup_vma(obj
, aspace
);
403 vma
= add_vma(obj
, aspace
);
407 ret
= msm_gem_init_vma(aspace
, vma
, obj
->size
>> PAGE_SHIFT
);
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
;
424 int prot
= IOMMU_READ
;
426 if (!(msm_obj
->flags
& MSM_BO_GPU_READONLY
))
429 WARN_ON(!mutex_is_locked(&msm_obj
->lock
));
431 if (WARN_ON(msm_obj
->madv
!= MSM_MADV_WILLNEED
))
434 vma
= lookup_vma(obj
, aspace
);
438 pages
= get_pages(obj
);
440 return PTR_ERR(pages
);
442 return msm_gem_map_vma(aspace
, vma
, prot
,
443 msm_obj
->sgt
, obj
->size
>> PAGE_SHIFT
);
446 /* get iova and pin it. Should have a matching put */
447 int msm_gem_get_and_pin_iova(struct drm_gem_object
*obj
,
448 struct msm_gem_address_space
*aspace
, uint64_t *iova
)
450 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
454 mutex_lock(&msm_obj
->lock
);
456 ret
= msm_gem_get_iova_locked(obj
, aspace
, &local
);
459 ret
= msm_gem_pin_iova(obj
, aspace
);
464 mutex_unlock(&msm_obj
->lock
);
469 * Get an iova but don't pin it. Doesn't need a put because iovas are currently
470 * valid for the life of the object
472 int msm_gem_get_iova(struct drm_gem_object
*obj
,
473 struct msm_gem_address_space
*aspace
, uint64_t *iova
)
475 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
478 mutex_lock(&msm_obj
->lock
);
479 ret
= msm_gem_get_iova_locked(obj
, aspace
, iova
);
480 mutex_unlock(&msm_obj
->lock
);
485 /* get iova without taking a reference, used in places where you have
486 * already done a 'msm_gem_get_and_pin_iova' or 'msm_gem_get_iova'
488 uint64_t msm_gem_iova(struct drm_gem_object
*obj
,
489 struct msm_gem_address_space
*aspace
)
491 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
492 struct msm_gem_vma
*vma
;
494 mutex_lock(&msm_obj
->lock
);
495 vma
= lookup_vma(obj
, aspace
);
496 mutex_unlock(&msm_obj
->lock
);
499 return vma
? vma
->iova
: 0;
503 * Unpin a iova by updating the reference counts. The memory isn't actually
504 * purged until something else (shrinker, mm_notifier, destroy, etc) decides
507 void msm_gem_unpin_iova(struct drm_gem_object
*obj
,
508 struct msm_gem_address_space
*aspace
)
510 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
511 struct msm_gem_vma
*vma
;
513 mutex_lock(&msm_obj
->lock
);
514 vma
= lookup_vma(obj
, aspace
);
517 msm_gem_unmap_vma(aspace
, vma
);
519 mutex_unlock(&msm_obj
->lock
);
522 int msm_gem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
523 struct drm_mode_create_dumb
*args
)
525 args
->pitch
= align_pitch(args
->width
, args
->bpp
);
526 args
->size
= PAGE_ALIGN(args
->pitch
* args
->height
);
527 return msm_gem_new_handle(dev
, file
, args
->size
,
528 MSM_BO_SCANOUT
| MSM_BO_WC
, &args
->handle
, "dumb");
531 int msm_gem_dumb_map_offset(struct drm_file
*file
, struct drm_device
*dev
,
532 uint32_t handle
, uint64_t *offset
)
534 struct drm_gem_object
*obj
;
537 /* GEM does all our handle to object mapping */
538 obj
= drm_gem_object_lookup(file
, handle
);
544 *offset
= msm_gem_mmap_offset(obj
);
546 drm_gem_object_put_unlocked(obj
);
552 static void *get_vaddr(struct drm_gem_object
*obj
, unsigned madv
)
554 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
557 mutex_lock(&msm_obj
->lock
);
559 if (WARN_ON(msm_obj
->madv
> madv
)) {
560 DRM_DEV_ERROR(obj
->dev
->dev
, "Invalid madv state: %u vs %u\n",
561 msm_obj
->madv
, madv
);
562 mutex_unlock(&msm_obj
->lock
);
563 return ERR_PTR(-EBUSY
);
566 /* increment vmap_count *before* vmap() call, so shrinker can
567 * check vmap_count (is_vunmapable()) outside of msm_obj->lock.
568 * This guarantees that we won't try to msm_gem_vunmap() this
569 * same object from within the vmap() call (while we already
570 * hold msm_obj->lock)
572 msm_obj
->vmap_count
++;
574 if (!msm_obj
->vaddr
) {
575 struct page
**pages
= get_pages(obj
);
577 ret
= PTR_ERR(pages
);
580 msm_obj
->vaddr
= vmap(pages
, obj
->size
>> PAGE_SHIFT
,
581 VM_MAP
, pgprot_writecombine(PAGE_KERNEL
));
582 if (msm_obj
->vaddr
== NULL
) {
588 mutex_unlock(&msm_obj
->lock
);
589 return msm_obj
->vaddr
;
592 msm_obj
->vmap_count
--;
593 mutex_unlock(&msm_obj
->lock
);
597 void *msm_gem_get_vaddr(struct drm_gem_object
*obj
)
599 return get_vaddr(obj
, MSM_MADV_WILLNEED
);
603 * Don't use this! It is for the very special case of dumping
604 * submits from GPU hangs or faults, were the bo may already
605 * be MSM_MADV_DONTNEED, but we know the buffer is still on the
608 void *msm_gem_get_vaddr_active(struct drm_gem_object
*obj
)
610 return get_vaddr(obj
, __MSM_MADV_PURGED
);
613 void msm_gem_put_vaddr(struct drm_gem_object
*obj
)
615 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
617 mutex_lock(&msm_obj
->lock
);
618 WARN_ON(msm_obj
->vmap_count
< 1);
619 msm_obj
->vmap_count
--;
620 mutex_unlock(&msm_obj
->lock
);
623 /* Update madvise status, returns true if not purged, else
626 int msm_gem_madvise(struct drm_gem_object
*obj
, unsigned madv
)
628 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
630 mutex_lock(&msm_obj
->lock
);
632 WARN_ON(!mutex_is_locked(&obj
->dev
->struct_mutex
));
634 if (msm_obj
->madv
!= __MSM_MADV_PURGED
)
635 msm_obj
->madv
= madv
;
637 madv
= msm_obj
->madv
;
639 mutex_unlock(&msm_obj
->lock
);
641 return (madv
!= __MSM_MADV_PURGED
);
644 void msm_gem_purge(struct drm_gem_object
*obj
, enum msm_gem_lock subclass
)
646 struct drm_device
*dev
= obj
->dev
;
647 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
649 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
650 WARN_ON(!is_purgeable(msm_obj
));
651 WARN_ON(obj
->import_attach
);
653 mutex_lock_nested(&msm_obj
->lock
, subclass
);
657 msm_gem_vunmap_locked(obj
);
661 msm_obj
->madv
= __MSM_MADV_PURGED
;
663 drm_vma_node_unmap(&obj
->vma_node
, dev
->anon_inode
->i_mapping
);
664 drm_gem_free_mmap_offset(obj
);
666 /* Our goal here is to return as much of the memory as
667 * is possible back to the system as we are called from OOM.
668 * To do this we must instruct the shmfs to drop all of its
669 * backing pages, *now*.
671 shmem_truncate_range(file_inode(obj
->filp
), 0, (loff_t
)-1);
673 invalidate_mapping_pages(file_inode(obj
->filp
)->i_mapping
,
676 mutex_unlock(&msm_obj
->lock
);
679 static void msm_gem_vunmap_locked(struct drm_gem_object
*obj
)
681 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
683 WARN_ON(!mutex_is_locked(&msm_obj
->lock
));
685 if (!msm_obj
->vaddr
|| WARN_ON(!is_vunmapable(msm_obj
)))
688 vunmap(msm_obj
->vaddr
);
689 msm_obj
->vaddr
= NULL
;
692 void msm_gem_vunmap(struct drm_gem_object
*obj
, enum msm_gem_lock subclass
)
694 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
696 mutex_lock_nested(&msm_obj
->lock
, subclass
);
697 msm_gem_vunmap_locked(obj
);
698 mutex_unlock(&msm_obj
->lock
);
701 /* must be called before _move_to_active().. */
702 int msm_gem_sync_object(struct drm_gem_object
*obj
,
703 struct msm_fence_context
*fctx
, bool exclusive
)
705 struct dma_resv_list
*fobj
;
706 struct dma_fence
*fence
;
709 fobj
= dma_resv_get_list(obj
->resv
);
710 if (!fobj
|| (fobj
->shared_count
== 0)) {
711 fence
= dma_resv_get_excl(obj
->resv
);
712 /* don't need to wait on our own fences, since ring is fifo */
713 if (fence
&& (fence
->context
!= fctx
->context
)) {
714 ret
= dma_fence_wait(fence
, true);
720 if (!exclusive
|| !fobj
)
723 for (i
= 0; i
< fobj
->shared_count
; i
++) {
724 fence
= rcu_dereference_protected(fobj
->shared
[i
],
725 dma_resv_held(obj
->resv
));
726 if (fence
->context
!= fctx
->context
) {
727 ret
= dma_fence_wait(fence
, true);
736 void msm_gem_move_to_active(struct drm_gem_object
*obj
,
737 struct msm_gpu
*gpu
, bool exclusive
, struct dma_fence
*fence
)
739 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
740 WARN_ON(msm_obj
->madv
!= MSM_MADV_WILLNEED
);
743 dma_resv_add_excl_fence(obj
->resv
, fence
);
745 dma_resv_add_shared_fence(obj
->resv
, fence
);
746 list_del_init(&msm_obj
->mm_list
);
747 list_add_tail(&msm_obj
->mm_list
, &gpu
->active_list
);
750 void msm_gem_move_to_inactive(struct drm_gem_object
*obj
)
752 struct drm_device
*dev
= obj
->dev
;
753 struct msm_drm_private
*priv
= dev
->dev_private
;
754 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
756 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
759 list_del_init(&msm_obj
->mm_list
);
760 list_add_tail(&msm_obj
->mm_list
, &priv
->inactive_list
);
763 int msm_gem_cpu_prep(struct drm_gem_object
*obj
, uint32_t op
, ktime_t
*timeout
)
765 bool write
= !!(op
& MSM_PREP_WRITE
);
766 unsigned long remain
=
767 op
& MSM_PREP_NOSYNC
? 0 : timeout_to_jiffies(timeout
);
770 ret
= dma_resv_wait_timeout_rcu(obj
->resv
, write
,
773 return remain
== 0 ? -EBUSY
: -ETIMEDOUT
;
777 /* TODO cache maintenance */
782 int msm_gem_cpu_fini(struct drm_gem_object
*obj
)
784 /* TODO cache maintenance */
788 #ifdef CONFIG_DEBUG_FS
789 static void describe_fence(struct dma_fence
*fence
, const char *type
,
792 if (!dma_fence_is_signaled(fence
))
793 seq_printf(m
, "\t%9s: %s %s seq %llu\n", type
,
794 fence
->ops
->get_driver_name(fence
),
795 fence
->ops
->get_timeline_name(fence
),
799 void msm_gem_describe(struct drm_gem_object
*obj
, struct seq_file
*m
)
801 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
802 struct dma_resv
*robj
= obj
->resv
;
803 struct dma_resv_list
*fobj
;
804 struct dma_fence
*fence
;
805 struct msm_gem_vma
*vma
;
806 uint64_t off
= drm_vma_node_start(&obj
->vma_node
);
809 mutex_lock(&msm_obj
->lock
);
811 switch (msm_obj
->madv
) {
812 case __MSM_MADV_PURGED
:
815 case MSM_MADV_DONTNEED
:
818 case MSM_MADV_WILLNEED
:
824 seq_printf(m
, "%08x: %c %2d (%2d) %08llx %p",
825 msm_obj
->flags
, is_active(msm_obj
) ? 'A' : 'I',
826 obj
->name
, kref_read(&obj
->refcount
),
827 off
, msm_obj
->vaddr
);
829 seq_printf(m
, " %08zu %9s %-32s\n", obj
->size
, madv
, msm_obj
->name
);
831 if (!list_empty(&msm_obj
->vmas
)) {
833 seq_puts(m
, " vmas:");
835 list_for_each_entry(vma
, &msm_obj
->vmas
, list
)
836 seq_printf(m
, " [%s: %08llx,%s,inuse=%d]",
837 vma
->aspace
!= NULL
? vma
->aspace
->name
: NULL
,
838 vma
->iova
, vma
->mapped
? "mapped" : "unmapped",
845 fobj
= rcu_dereference(robj
->fence
);
847 unsigned int i
, shared_count
= fobj
->shared_count
;
849 for (i
= 0; i
< shared_count
; i
++) {
850 fence
= rcu_dereference(fobj
->shared
[i
]);
851 describe_fence(fence
, "Shared", m
);
855 fence
= rcu_dereference(robj
->fence_excl
);
857 describe_fence(fence
, "Exclusive", m
);
860 mutex_unlock(&msm_obj
->lock
);
863 void msm_gem_describe_objects(struct list_head
*list
, struct seq_file
*m
)
865 struct msm_gem_object
*msm_obj
;
869 seq_puts(m
, " flags id ref offset kaddr size madv name\n");
870 list_for_each_entry(msm_obj
, list
, mm_list
) {
871 struct drm_gem_object
*obj
= &msm_obj
->base
;
873 msm_gem_describe(obj
, m
);
878 seq_printf(m
, "Total %d objects, %zu bytes\n", count
, size
);
882 /* don't call directly! Use drm_gem_object_put() and friends */
883 void msm_gem_free_object(struct drm_gem_object
*obj
)
885 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
886 struct drm_device
*dev
= obj
->dev
;
887 struct msm_drm_private
*priv
= dev
->dev_private
;
889 if (llist_add(&msm_obj
->freed
, &priv
->free_list
))
890 queue_work(priv
->wq
, &priv
->free_work
);
893 static void free_object(struct msm_gem_object
*msm_obj
)
895 struct drm_gem_object
*obj
= &msm_obj
->base
;
896 struct drm_device
*dev
= obj
->dev
;
898 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
900 /* object should not be on active list: */
901 WARN_ON(is_active(msm_obj
));
903 list_del(&msm_obj
->mm_list
);
905 mutex_lock(&msm_obj
->lock
);
909 if (obj
->import_attach
) {
911 dma_buf_vunmap(obj
->import_attach
->dmabuf
, msm_obj
->vaddr
);
913 /* Don't drop the pages for imported dmabuf, as they are not
914 * ours, just free the array we allocated:
917 kvfree(msm_obj
->pages
);
919 drm_prime_gem_destroy(obj
, msm_obj
->sgt
);
921 msm_gem_vunmap_locked(obj
);
925 drm_gem_object_release(obj
);
927 mutex_unlock(&msm_obj
->lock
);
931 void msm_gem_free_work(struct work_struct
*work
)
933 struct msm_drm_private
*priv
=
934 container_of(work
, struct msm_drm_private
, free_work
);
935 struct drm_device
*dev
= priv
->dev
;
936 struct llist_node
*freed
;
937 struct msm_gem_object
*msm_obj
, *next
;
939 while ((freed
= llist_del_all(&priv
->free_list
))) {
941 mutex_lock(&dev
->struct_mutex
);
943 llist_for_each_entry_safe(msm_obj
, next
,
945 free_object(msm_obj
);
947 mutex_unlock(&dev
->struct_mutex
);
954 /* convenience method to construct a GEM buffer object, and userspace handle */
955 int msm_gem_new_handle(struct drm_device
*dev
, struct drm_file
*file
,
956 uint32_t size
, uint32_t flags
, uint32_t *handle
,
959 struct drm_gem_object
*obj
;
962 obj
= msm_gem_new(dev
, size
, flags
);
968 msm_gem_object_set_name(obj
, "%s", name
);
970 ret
= drm_gem_handle_create(file
, obj
, handle
);
972 /* drop reference from allocate - handle holds it now */
973 drm_gem_object_put_unlocked(obj
);
978 static int msm_gem_new_impl(struct drm_device
*dev
,
979 uint32_t size
, uint32_t flags
,
980 struct drm_gem_object
**obj
,
981 bool struct_mutex_locked
)
983 struct msm_drm_private
*priv
= dev
->dev_private
;
984 struct msm_gem_object
*msm_obj
;
986 switch (flags
& MSM_BO_CACHE_MASK
) {
987 case MSM_BO_UNCACHED
:
992 DRM_DEV_ERROR(dev
->dev
, "invalid cache flag: %x\n",
993 (flags
& MSM_BO_CACHE_MASK
));
997 msm_obj
= kzalloc(sizeof(*msm_obj
), GFP_KERNEL
);
1001 mutex_init(&msm_obj
->lock
);
1003 msm_obj
->flags
= flags
;
1004 msm_obj
->madv
= MSM_MADV_WILLNEED
;
1006 INIT_LIST_HEAD(&msm_obj
->submit_entry
);
1007 INIT_LIST_HEAD(&msm_obj
->vmas
);
1009 if (struct_mutex_locked
) {
1010 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
1011 list_add_tail(&msm_obj
->mm_list
, &priv
->inactive_list
);
1013 mutex_lock(&dev
->struct_mutex
);
1014 list_add_tail(&msm_obj
->mm_list
, &priv
->inactive_list
);
1015 mutex_unlock(&dev
->struct_mutex
);
1018 *obj
= &msm_obj
->base
;
1023 static struct drm_gem_object
*_msm_gem_new(struct drm_device
*dev
,
1024 uint32_t size
, uint32_t flags
, bool struct_mutex_locked
)
1026 struct msm_drm_private
*priv
= dev
->dev_private
;
1027 struct drm_gem_object
*obj
= NULL
;
1028 bool use_vram
= false;
1031 size
= PAGE_ALIGN(size
);
1033 if (!msm_use_mmu(dev
))
1035 else if ((flags
& (MSM_BO_STOLEN
| MSM_BO_SCANOUT
)) && priv
->vram
.size
)
1038 if (WARN_ON(use_vram
&& !priv
->vram
.size
))
1039 return ERR_PTR(-EINVAL
);
1041 /* Disallow zero sized objects as they make the underlying
1042 * infrastructure grumpy
1045 return ERR_PTR(-EINVAL
);
1047 ret
= msm_gem_new_impl(dev
, size
, flags
, &obj
, struct_mutex_locked
);
1052 struct msm_gem_vma
*vma
;
1053 struct page
**pages
;
1054 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
1056 mutex_lock(&msm_obj
->lock
);
1058 vma
= add_vma(obj
, NULL
);
1059 mutex_unlock(&msm_obj
->lock
);
1065 to_msm_bo(obj
)->vram_node
= &vma
->node
;
1067 drm_gem_private_object_init(dev
, obj
, size
);
1069 pages
= get_pages(obj
);
1070 if (IS_ERR(pages
)) {
1071 ret
= PTR_ERR(pages
);
1075 vma
->iova
= physaddr(obj
);
1077 ret
= drm_gem_object_init(dev
, obj
, size
);
1081 * Our buffers are kept pinned, so allocating them from the
1082 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1083 * See comments above new_inode() why this is required _and_
1084 * expected if you're going to pin these pages.
1086 mapping_set_gfp_mask(obj
->filp
->f_mapping
, GFP_HIGHUSER
);
1092 drm_gem_object_put_unlocked(obj
);
1093 return ERR_PTR(ret
);
1096 struct drm_gem_object
*msm_gem_new_locked(struct drm_device
*dev
,
1097 uint32_t size
, uint32_t flags
)
1099 return _msm_gem_new(dev
, size
, flags
, true);
1102 struct drm_gem_object
*msm_gem_new(struct drm_device
*dev
,
1103 uint32_t size
, uint32_t flags
)
1105 return _msm_gem_new(dev
, size
, flags
, false);
1108 struct drm_gem_object
*msm_gem_import(struct drm_device
*dev
,
1109 struct dma_buf
*dmabuf
, struct sg_table
*sgt
)
1111 struct msm_gem_object
*msm_obj
;
1112 struct drm_gem_object
*obj
;
1116 /* if we don't have IOMMU, don't bother pretending we can import: */
1117 if (!msm_use_mmu(dev
)) {
1118 DRM_DEV_ERROR(dev
->dev
, "cannot import without IOMMU\n");
1119 return ERR_PTR(-EINVAL
);
1122 size
= PAGE_ALIGN(dmabuf
->size
);
1124 ret
= msm_gem_new_impl(dev
, size
, MSM_BO_WC
, &obj
, false);
1128 drm_gem_private_object_init(dev
, obj
, size
);
1130 npages
= size
/ PAGE_SIZE
;
1132 msm_obj
= to_msm_bo(obj
);
1133 mutex_lock(&msm_obj
->lock
);
1135 msm_obj
->pages
= kvmalloc_array(npages
, sizeof(struct page
*), GFP_KERNEL
);
1136 if (!msm_obj
->pages
) {
1137 mutex_unlock(&msm_obj
->lock
);
1142 ret
= drm_prime_sg_to_page_addr_arrays(sgt
, msm_obj
->pages
, NULL
, npages
);
1144 mutex_unlock(&msm_obj
->lock
);
1148 mutex_unlock(&msm_obj
->lock
);
1152 drm_gem_object_put_unlocked(obj
);
1153 return ERR_PTR(ret
);
1156 static void *_msm_gem_kernel_new(struct drm_device
*dev
, uint32_t size
,
1157 uint32_t flags
, struct msm_gem_address_space
*aspace
,
1158 struct drm_gem_object
**bo
, uint64_t *iova
, bool locked
)
1161 struct drm_gem_object
*obj
= _msm_gem_new(dev
, size
, flags
, locked
);
1165 return ERR_CAST(obj
);
1168 ret
= msm_gem_get_and_pin_iova(obj
, aspace
, iova
);
1173 vaddr
= msm_gem_get_vaddr(obj
);
1174 if (IS_ERR(vaddr
)) {
1175 msm_gem_unpin_iova(obj
, aspace
);
1176 ret
= PTR_ERR(vaddr
);
1186 drm_gem_object_put(obj
);
1188 drm_gem_object_put_unlocked(obj
);
1190 return ERR_PTR(ret
);
1194 void *msm_gem_kernel_new(struct drm_device
*dev
, uint32_t size
,
1195 uint32_t flags
, struct msm_gem_address_space
*aspace
,
1196 struct drm_gem_object
**bo
, uint64_t *iova
)
1198 return _msm_gem_kernel_new(dev
, size
, flags
, aspace
, bo
, iova
, false);
1201 void *msm_gem_kernel_new_locked(struct drm_device
*dev
, uint32_t size
,
1202 uint32_t flags
, struct msm_gem_address_space
*aspace
,
1203 struct drm_gem_object
**bo
, uint64_t *iova
)
1205 return _msm_gem_kernel_new(dev
, size
, flags
, aspace
, bo
, iova
, true);
1208 void msm_gem_kernel_put(struct drm_gem_object
*bo
,
1209 struct msm_gem_address_space
*aspace
, bool locked
)
1211 if (IS_ERR_OR_NULL(bo
))
1214 msm_gem_put_vaddr(bo
);
1215 msm_gem_unpin_iova(bo
, aspace
);
1218 drm_gem_object_put(bo
);
1220 drm_gem_object_put_unlocked(bo
);
1223 void msm_gem_object_set_name(struct drm_gem_object
*bo
, const char *fmt
, ...)
1225 struct msm_gem_object
*msm_obj
= to_msm_bo(bo
);
1232 vsnprintf(msm_obj
->name
, sizeof(msm_obj
->name
), fmt
, ap
);