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/vmalloc.h>
9 #include <linux/spinlock.h>
10 #include <linux/shmem_fs.h>
11 #include <linux/dma-buf.h>
12 #include <linux/pfn_t.h>
14 #include <drm/drm_prime.h>
17 #include "msm_fence.h"
22 static void update_inactive(struct msm_gem_object *msm_obj);
24 static dma_addr_t physaddr(struct drm_gem_object *obj)
26 struct msm_gem_object *msm_obj = to_msm_bo(obj);
27 struct msm_drm_private *priv = obj->dev->dev_private;
28 return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
32 static bool use_pages(struct drm_gem_object *obj)
34 struct msm_gem_object *msm_obj = to_msm_bo(obj);
35 return !msm_obj->vram_node;
39 * Cache sync.. this is a bit over-complicated, to fit dma-mapping
40 * API. Really GPU cache is out of scope here (handled on cmdstream)
41 * and all we need to do is invalidate newly allocated pages before
42 * mapping to CPU as uncached/writecombine.
44 * On top of this, we have the added headache, that depending on
45 * display generation, the display's iommu may be wired up to either
46 * the toplevel drm device (mdss), or to the mdp sub-node, meaning
47 * that here we either have dma-direct or iommu ops.
49 * Let this be a cautionary tail of abstraction gone wrong.
52 static void sync_for_device(struct msm_gem_object *msm_obj)
54 struct device *dev = msm_obj->base.dev->dev;
56 dma_map_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0);
59 static void sync_for_cpu(struct msm_gem_object *msm_obj)
61 struct device *dev = msm_obj->base.dev->dev;
63 dma_unmap_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0);
66 /* allocate pages from VRAM carveout, used when no IOMMU: */
67 static struct page **get_pages_vram(struct drm_gem_object *obj, int npages)
69 struct msm_gem_object *msm_obj = to_msm_bo(obj);
70 struct msm_drm_private *priv = obj->dev->dev_private;
75 p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
77 return ERR_PTR(-ENOMEM);
79 spin_lock(&priv->vram.lock);
80 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages);
81 spin_unlock(&priv->vram.lock);
87 paddr = physaddr(obj);
88 for (i = 0; i < npages; i++) {
89 p[i] = pfn_to_page(__phys_to_pfn(paddr));
96 static struct page **get_pages(struct drm_gem_object *obj)
98 struct msm_gem_object *msm_obj = to_msm_bo(obj);
100 GEM_WARN_ON(!msm_gem_is_locked(obj));
102 if (!msm_obj->pages) {
103 struct drm_device *dev = obj->dev;
105 int npages = obj->size >> PAGE_SHIFT;
108 p = drm_gem_get_pages(obj);
110 p = get_pages_vram(obj, npages);
113 DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n",
120 msm_obj->sgt = drm_prime_pages_to_sg(obj->dev, p, npages);
121 if (IS_ERR(msm_obj->sgt)) {
122 void *ptr = ERR_CAST(msm_obj->sgt);
124 DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n");
129 /* For non-cached buffers, ensure the new pages are clean
130 * because display controller, GPU, etc. are not coherent:
132 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
133 sync_for_device(msm_obj);
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 static vm_fault_t msm_gem_fault(struct vm_fault *vmf)
223 struct vm_area_struct *vma = vmf->vma;
224 struct drm_gem_object *obj = vma->vm_private_data;
225 struct msm_gem_object *msm_obj = to_msm_bo(obj);
233 * vm_ops.open/drm_gem_mmap_obj and close get and put
234 * a reference on obj. So, we dont need to hold one here.
236 err = msm_gem_lock_interruptible(obj);
238 ret = VM_FAULT_NOPAGE;
242 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
244 return VM_FAULT_SIGBUS;
247 /* make sure we have pages attached now */
248 pages = get_pages(obj);
250 ret = vmf_error(PTR_ERR(pages));
254 /* We don't use vmf->pgoff since that has the fake offset: */
255 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
257 pfn = page_to_pfn(pages[pgoff]);
259 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
260 pfn, pfn << PAGE_SHIFT);
262 ret = vmf_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
269 /** get mmap offset */
270 static uint64_t mmap_offset(struct drm_gem_object *obj)
272 struct drm_device *dev = obj->dev;
275 GEM_WARN_ON(!msm_gem_is_locked(obj));
277 /* Make it mmapable */
278 ret = drm_gem_create_mmap_offset(obj);
281 DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
285 return drm_vma_node_offset_addr(&obj->vma_node);
288 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
293 offset = mmap_offset(obj);
298 static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
299 struct msm_gem_address_space *aspace)
301 struct msm_gem_object *msm_obj = to_msm_bo(obj);
302 struct msm_gem_vma *vma;
304 GEM_WARN_ON(!msm_gem_is_locked(obj));
306 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
308 return ERR_PTR(-ENOMEM);
310 vma->aspace = aspace;
312 list_add_tail(&vma->list, &msm_obj->vmas);
317 static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj,
318 struct msm_gem_address_space *aspace)
320 struct msm_gem_object *msm_obj = to_msm_bo(obj);
321 struct msm_gem_vma *vma;
323 GEM_WARN_ON(!msm_gem_is_locked(obj));
325 list_for_each_entry(vma, &msm_obj->vmas, list) {
326 if (vma->aspace == aspace)
333 static void del_vma(struct msm_gem_vma *vma)
338 list_del(&vma->list);
343 * If close is true, this also closes the VMA (releasing the allocated
344 * iova range) in addition to removing the iommu mapping. In the eviction
345 * case (!close), we keep the iova allocated, but only remove the iommu
349 put_iova_spaces(struct drm_gem_object *obj, bool close)
351 struct msm_gem_object *msm_obj = to_msm_bo(obj);
352 struct msm_gem_vma *vma;
354 GEM_WARN_ON(!msm_gem_is_locked(obj));
356 list_for_each_entry(vma, &msm_obj->vmas, list) {
358 msm_gem_purge_vma(vma->aspace, vma);
360 msm_gem_close_vma(vma->aspace, vma);
365 /* Called with msm_obj locked */
367 put_iova_vmas(struct drm_gem_object *obj)
369 struct msm_gem_object *msm_obj = to_msm_bo(obj);
370 struct msm_gem_vma *vma, *tmp;
372 GEM_WARN_ON(!msm_gem_is_locked(obj));
374 list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
379 static struct msm_gem_vma *get_vma_locked(struct drm_gem_object *obj,
380 struct msm_gem_address_space *aspace,
381 u64 range_start, u64 range_end)
383 struct msm_gem_vma *vma;
385 GEM_WARN_ON(!msm_gem_is_locked(obj));
387 vma = lookup_vma(obj, aspace);
392 vma = add_vma(obj, aspace);
396 ret = msm_gem_init_vma(aspace, vma, obj->size,
397 range_start, range_end);
403 GEM_WARN_ON(vma->iova < range_start);
404 GEM_WARN_ON((vma->iova + obj->size) > range_end);
410 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma)
412 struct msm_gem_object *msm_obj = to_msm_bo(obj);
414 int ret, prot = IOMMU_READ;
416 if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
419 if (msm_obj->flags & MSM_BO_MAP_PRIV)
422 if (msm_obj->flags & MSM_BO_CACHED_COHERENT)
425 GEM_WARN_ON(!msm_gem_is_locked(obj));
427 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
430 pages = get_pages(obj);
432 return PTR_ERR(pages);
434 ret = msm_gem_map_vma(vma->aspace, vma, prot, msm_obj->sgt, obj->size);
437 msm_obj->pin_count++;
442 void msm_gem_unpin_locked(struct drm_gem_object *obj)
444 struct msm_gem_object *msm_obj = to_msm_bo(obj);
446 GEM_WARN_ON(!msm_gem_is_locked(obj));
448 msm_obj->pin_count--;
449 GEM_WARN_ON(msm_obj->pin_count < 0);
451 update_inactive(msm_obj);
454 struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj,
455 struct msm_gem_address_space *aspace)
457 return get_vma_locked(obj, aspace, 0, U64_MAX);
460 static int get_and_pin_iova_range_locked(struct drm_gem_object *obj,
461 struct msm_gem_address_space *aspace, uint64_t *iova,
462 u64 range_start, u64 range_end)
464 struct msm_gem_vma *vma;
467 GEM_WARN_ON(!msm_gem_is_locked(obj));
469 vma = get_vma_locked(obj, aspace, range_start, range_end);
473 ret = msm_gem_pin_vma_locked(obj, vma);
481 * get iova and pin it. Should have a matching put
482 * limits iova to specified range (in pages)
484 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
485 struct msm_gem_address_space *aspace, uint64_t *iova,
486 u64 range_start, u64 range_end)
491 ret = get_and_pin_iova_range_locked(obj, aspace, iova, range_start, range_end);
497 /* get iova and pin it. Should have a matching put */
498 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
499 struct msm_gem_address_space *aspace, uint64_t *iova)
501 return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX);
505 * Get an iova but don't pin it. Doesn't need a put because iovas are currently
506 * valid for the life of the object
508 int msm_gem_get_iova(struct drm_gem_object *obj,
509 struct msm_gem_address_space *aspace, uint64_t *iova)
511 struct msm_gem_vma *vma;
515 vma = get_vma_locked(obj, aspace, 0, U64_MAX);
526 static int clear_iova(struct drm_gem_object *obj,
527 struct msm_gem_address_space *aspace)
529 struct msm_gem_vma *vma = lookup_vma(obj, aspace);
534 if (msm_gem_vma_inuse(vma))
537 msm_gem_purge_vma(vma->aspace, vma);
538 msm_gem_close_vma(vma->aspace, vma);
545 * Get the requested iova but don't pin it. Fails if the requested iova is
546 * not available. Doesn't need a put because iovas are currently valid for
547 * the life of the object.
549 * Setting an iova of zero will clear the vma.
551 int msm_gem_set_iova(struct drm_gem_object *obj,
552 struct msm_gem_address_space *aspace, uint64_t iova)
558 ret = clear_iova(obj, aspace);
560 struct msm_gem_vma *vma;
561 vma = get_vma_locked(obj, aspace, iova, iova + obj->size);
564 } else if (GEM_WARN_ON(vma->iova != iova)) {
565 clear_iova(obj, aspace);
575 * Unpin a iova by updating the reference counts. The memory isn't actually
576 * purged until something else (shrinker, mm_notifier, destroy, etc) decides
579 void msm_gem_unpin_iova(struct drm_gem_object *obj,
580 struct msm_gem_address_space *aspace)
582 struct msm_gem_vma *vma;
585 vma = lookup_vma(obj, aspace);
586 if (!GEM_WARN_ON(!vma)) {
587 msm_gem_unpin_vma(vma);
588 msm_gem_unpin_locked(obj);
593 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
594 struct drm_mode_create_dumb *args)
596 args->pitch = align_pitch(args->width, args->bpp);
597 args->size = PAGE_ALIGN(args->pitch * args->height);
598 return msm_gem_new_handle(dev, file, args->size,
599 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb");
602 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
603 uint32_t handle, uint64_t *offset)
605 struct drm_gem_object *obj;
608 /* GEM does all our handle to object mapping */
609 obj = drm_gem_object_lookup(file, handle);
615 *offset = msm_gem_mmap_offset(obj);
617 drm_gem_object_put(obj);
623 static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
625 struct msm_gem_object *msm_obj = to_msm_bo(obj);
628 GEM_WARN_ON(!msm_gem_is_locked(obj));
630 if (obj->import_attach)
631 return ERR_PTR(-ENODEV);
633 if (GEM_WARN_ON(msm_obj->madv > madv)) {
634 DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n",
635 msm_obj->madv, madv);
636 return ERR_PTR(-EBUSY);
639 /* increment vmap_count *before* vmap() call, so shrinker can
640 * check vmap_count (is_vunmapable()) outside of msm_obj lock.
641 * This guarantees that we won't try to msm_gem_vunmap() this
642 * same object from within the vmap() call (while we already
645 msm_obj->vmap_count++;
647 if (!msm_obj->vaddr) {
648 struct page **pages = get_pages(obj);
650 ret = PTR_ERR(pages);
653 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
654 VM_MAP, msm_gem_pgprot(msm_obj, PAGE_KERNEL));
655 if (msm_obj->vaddr == NULL) {
660 update_inactive(msm_obj);
663 return msm_obj->vaddr;
666 msm_obj->vmap_count--;
670 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
672 return get_vaddr(obj, MSM_MADV_WILLNEED);
675 void *msm_gem_get_vaddr(struct drm_gem_object *obj)
680 ret = msm_gem_get_vaddr_locked(obj);
687 * Don't use this! It is for the very special case of dumping
688 * submits from GPU hangs or faults, were the bo may already
689 * be MSM_MADV_DONTNEED, but we know the buffer is still on the
692 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
694 return get_vaddr(obj, __MSM_MADV_PURGED);
697 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
699 struct msm_gem_object *msm_obj = to_msm_bo(obj);
701 GEM_WARN_ON(!msm_gem_is_locked(obj));
702 GEM_WARN_ON(msm_obj->vmap_count < 1);
704 msm_obj->vmap_count--;
707 void msm_gem_put_vaddr(struct drm_gem_object *obj)
710 msm_gem_put_vaddr_locked(obj);
714 /* Update madvise status, returns true if not purged, else
717 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
719 struct msm_gem_object *msm_obj = to_msm_bo(obj);
723 if (msm_obj->madv != __MSM_MADV_PURGED)
724 msm_obj->madv = madv;
726 madv = msm_obj->madv;
728 /* If the obj is inactive, we might need to move it
729 * between inactive lists
731 if (msm_obj->active_count == 0)
732 update_inactive(msm_obj);
736 return (madv != __MSM_MADV_PURGED);
739 void msm_gem_purge(struct drm_gem_object *obj)
741 struct drm_device *dev = obj->dev;
742 struct msm_gem_object *msm_obj = to_msm_bo(obj);
744 GEM_WARN_ON(!msm_gem_is_locked(obj));
745 GEM_WARN_ON(!is_purgeable(msm_obj));
747 /* Get rid of any iommu mapping(s): */
748 put_iova_spaces(obj, true);
752 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
758 msm_obj->madv = __MSM_MADV_PURGED;
759 update_inactive(msm_obj);
761 drm_gem_free_mmap_offset(obj);
763 /* Our goal here is to return as much of the memory as
764 * is possible back to the system as we are called from OOM.
765 * To do this we must instruct the shmfs to drop all of its
766 * backing pages, *now*.
768 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
770 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
775 * Unpin the backing pages and make them available to be swapped out.
777 void msm_gem_evict(struct drm_gem_object *obj)
779 struct drm_device *dev = obj->dev;
780 struct msm_gem_object *msm_obj = to_msm_bo(obj);
782 GEM_WARN_ON(!msm_gem_is_locked(obj));
783 GEM_WARN_ON(is_unevictable(msm_obj));
784 GEM_WARN_ON(!msm_obj->evictable);
785 GEM_WARN_ON(msm_obj->active_count);
787 /* Get rid of any iommu mapping(s): */
788 put_iova_spaces(obj, false);
790 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
794 update_inactive(msm_obj);
797 void msm_gem_vunmap(struct drm_gem_object *obj)
799 struct msm_gem_object *msm_obj = to_msm_bo(obj);
801 GEM_WARN_ON(!msm_gem_is_locked(obj));
803 if (!msm_obj->vaddr || GEM_WARN_ON(!is_vunmapable(msm_obj)))
806 vunmap(msm_obj->vaddr);
807 msm_obj->vaddr = NULL;
810 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu)
812 struct msm_gem_object *msm_obj = to_msm_bo(obj);
813 struct msm_drm_private *priv = obj->dev->dev_private;
816 GEM_WARN_ON(!msm_gem_is_locked(obj));
817 GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
818 GEM_WARN_ON(msm_obj->dontneed);
820 if (msm_obj->active_count++ == 0) {
821 mutex_lock(&priv->mm_lock);
822 if (msm_obj->evictable)
823 mark_unevictable(msm_obj);
824 list_move_tail(&msm_obj->mm_list, &gpu->active_list);
825 mutex_unlock(&priv->mm_lock);
829 void msm_gem_active_put(struct drm_gem_object *obj)
831 struct msm_gem_object *msm_obj = to_msm_bo(obj);
834 GEM_WARN_ON(!msm_gem_is_locked(obj));
836 if (--msm_obj->active_count == 0) {
837 update_inactive(msm_obj);
841 static void update_inactive(struct msm_gem_object *msm_obj)
843 struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
845 GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
847 if (msm_obj->active_count != 0)
850 mutex_lock(&priv->mm_lock);
852 if (msm_obj->dontneed)
853 mark_unpurgeable(msm_obj);
854 if (msm_obj->evictable)
855 mark_unevictable(msm_obj);
857 list_del(&msm_obj->mm_list);
858 if ((msm_obj->madv == MSM_MADV_WILLNEED) && msm_obj->sgt) {
859 list_add_tail(&msm_obj->mm_list, &priv->inactive_willneed);
860 mark_evictable(msm_obj);
861 } else if (msm_obj->madv == MSM_MADV_DONTNEED) {
862 list_add_tail(&msm_obj->mm_list, &priv->inactive_dontneed);
863 mark_purgeable(msm_obj);
865 GEM_WARN_ON((msm_obj->madv != __MSM_MADV_PURGED) && msm_obj->sgt);
866 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
869 mutex_unlock(&priv->mm_lock);
872 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
874 bool write = !!(op & MSM_PREP_WRITE);
875 unsigned long remain =
876 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
879 ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(write),
882 return remain == 0 ? -EBUSY : -ETIMEDOUT;
886 /* TODO cache maintenance */
891 int msm_gem_cpu_fini(struct drm_gem_object *obj)
893 /* TODO cache maintenance */
897 #ifdef CONFIG_DEBUG_FS
898 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
899 struct msm_gem_stats *stats)
901 struct msm_gem_object *msm_obj = to_msm_bo(obj);
902 struct dma_resv *robj = obj->resv;
903 struct msm_gem_vma *vma;
904 uint64_t off = drm_vma_node_start(&obj->vma_node);
910 stats->all.size += obj->size;
912 if (is_active(msm_obj)) {
913 stats->active.count++;
914 stats->active.size += obj->size;
917 if (msm_obj->pages) {
918 stats->resident.count++;
919 stats->resident.size += obj->size;
922 switch (msm_obj->madv) {
923 case __MSM_MADV_PURGED:
924 stats->purged.count++;
925 stats->purged.size += obj->size;
928 case MSM_MADV_DONTNEED:
929 stats->purgeable.count++;
930 stats->purgeable.size += obj->size;
933 case MSM_MADV_WILLNEED:
939 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p",
940 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
941 obj->name, kref_read(&obj->refcount),
942 off, msm_obj->vaddr);
944 seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
946 if (!list_empty(&msm_obj->vmas)) {
948 seq_puts(m, " vmas:");
950 list_for_each_entry(vma, &msm_obj->vmas, list) {
951 const char *name, *comm;
953 struct msm_gem_address_space *aspace = vma->aspace;
954 struct task_struct *task =
955 get_pid_task(aspace->pid, PIDTYPE_PID);
957 comm = kstrdup(task->comm, GFP_KERNEL);
958 put_task_struct(task);
966 seq_printf(m, " [%s%s%s: aspace=%p, %08llx,%s,inuse=%d]",
967 name, comm ? ":" : "", comm ? comm : "",
968 vma->aspace, vma->iova,
969 vma->mapped ? "mapped" : "unmapped",
970 msm_gem_vma_inuse(vma));
977 dma_resv_describe(robj, m);
981 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
983 struct msm_gem_stats stats = {};
984 struct msm_gem_object *msm_obj;
986 seq_puts(m, " flags id ref offset kaddr size madv name\n");
987 list_for_each_entry(msm_obj, list, node) {
988 struct drm_gem_object *obj = &msm_obj->base;
990 msm_gem_describe(obj, m, &stats);
993 seq_printf(m, "Total: %4d objects, %9zu bytes\n",
994 stats.all.count, stats.all.size);
995 seq_printf(m, "Active: %4d objects, %9zu bytes\n",
996 stats.active.count, stats.active.size);
997 seq_printf(m, "Resident: %4d objects, %9zu bytes\n",
998 stats.resident.count, stats.resident.size);
999 seq_printf(m, "Purgeable: %4d objects, %9zu bytes\n",
1000 stats.purgeable.count, stats.purgeable.size);
1001 seq_printf(m, "Purged: %4d objects, %9zu bytes\n",
1002 stats.purged.count, stats.purged.size);
1006 /* don't call directly! Use drm_gem_object_put() */
1007 void msm_gem_free_object(struct drm_gem_object *obj)
1009 struct msm_gem_object *msm_obj = to_msm_bo(obj);
1010 struct drm_device *dev = obj->dev;
1011 struct msm_drm_private *priv = dev->dev_private;
1013 mutex_lock(&priv->obj_lock);
1014 list_del(&msm_obj->node);
1015 mutex_unlock(&priv->obj_lock);
1017 mutex_lock(&priv->mm_lock);
1018 if (msm_obj->dontneed)
1019 mark_unpurgeable(msm_obj);
1020 list_del(&msm_obj->mm_list);
1021 mutex_unlock(&priv->mm_lock);
1025 /* object should not be on active list: */
1026 GEM_WARN_ON(is_active(msm_obj));
1028 put_iova_spaces(obj, true);
1030 if (obj->import_attach) {
1031 GEM_WARN_ON(msm_obj->vaddr);
1033 /* Don't drop the pages for imported dmabuf, as they are not
1034 * ours, just free the array we allocated:
1036 kvfree(msm_obj->pages);
1040 /* dma_buf_detach() grabs resv lock, so we need to unlock
1041 * prior to drm_prime_gem_destroy
1043 msm_gem_unlock(obj);
1045 drm_prime_gem_destroy(obj, msm_obj->sgt);
1047 msm_gem_vunmap(obj);
1050 msm_gem_unlock(obj);
1053 drm_gem_object_release(obj);
1058 static int msm_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
1060 struct msm_gem_object *msm_obj = to_msm_bo(obj);
1062 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
1063 vma->vm_page_prot = msm_gem_pgprot(msm_obj, vm_get_page_prot(vma->vm_flags));
1068 /* convenience method to construct a GEM buffer object, and userspace handle */
1069 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1070 uint32_t size, uint32_t flags, uint32_t *handle,
1073 struct drm_gem_object *obj;
1076 obj = msm_gem_new(dev, size, flags);
1079 return PTR_ERR(obj);
1082 msm_gem_object_set_name(obj, "%s", name);
1084 ret = drm_gem_handle_create(file, obj, handle);
1086 /* drop reference from allocate - handle holds it now */
1087 drm_gem_object_put(obj);
1092 static const struct vm_operations_struct vm_ops = {
1093 .fault = msm_gem_fault,
1094 .open = drm_gem_vm_open,
1095 .close = drm_gem_vm_close,
1098 static const struct drm_gem_object_funcs msm_gem_object_funcs = {
1099 .free = msm_gem_free_object,
1100 .pin = msm_gem_prime_pin,
1101 .unpin = msm_gem_prime_unpin,
1102 .get_sg_table = msm_gem_prime_get_sg_table,
1103 .vmap = msm_gem_prime_vmap,
1104 .vunmap = msm_gem_prime_vunmap,
1105 .mmap = msm_gem_object_mmap,
1109 static int msm_gem_new_impl(struct drm_device *dev,
1110 uint32_t size, uint32_t flags,
1111 struct drm_gem_object **obj)
1113 struct msm_drm_private *priv = dev->dev_private;
1114 struct msm_gem_object *msm_obj;
1116 switch (flags & MSM_BO_CACHE_MASK) {
1117 case MSM_BO_UNCACHED:
1121 case MSM_BO_CACHED_COHERENT:
1122 if (priv->has_cached_coherent)
1126 DRM_DEV_DEBUG(dev->dev, "invalid cache flag: %x\n",
1127 (flags & MSM_BO_CACHE_MASK));
1131 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
1135 msm_obj->flags = flags;
1136 msm_obj->madv = MSM_MADV_WILLNEED;
1138 INIT_LIST_HEAD(&msm_obj->node);
1139 INIT_LIST_HEAD(&msm_obj->vmas);
1141 *obj = &msm_obj->base;
1142 (*obj)->funcs = &msm_gem_object_funcs;
1147 struct drm_gem_object *msm_gem_new(struct drm_device *dev, uint32_t size, uint32_t flags)
1149 struct msm_drm_private *priv = dev->dev_private;
1150 struct msm_gem_object *msm_obj;
1151 struct drm_gem_object *obj = NULL;
1152 bool use_vram = false;
1155 size = PAGE_ALIGN(size);
1157 if (!msm_use_mmu(dev))
1159 else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
1162 if (GEM_WARN_ON(use_vram && !priv->vram.size))
1163 return ERR_PTR(-EINVAL);
1165 /* Disallow zero sized objects as they make the underlying
1166 * infrastructure grumpy
1169 return ERR_PTR(-EINVAL);
1171 ret = msm_gem_new_impl(dev, size, flags, &obj);
1173 return ERR_PTR(ret);
1175 msm_obj = to_msm_bo(obj);
1178 struct msm_gem_vma *vma;
1179 struct page **pages;
1181 drm_gem_private_object_init(dev, obj, size);
1185 vma = add_vma(obj, NULL);
1186 msm_gem_unlock(obj);
1192 to_msm_bo(obj)->vram_node = &vma->node;
1194 /* Call chain get_pages() -> update_inactive() tries to
1195 * access msm_obj->mm_list, but it is not initialized yet.
1196 * To avoid NULL pointer dereference error, initialize
1197 * mm_list to be empty.
1199 INIT_LIST_HEAD(&msm_obj->mm_list);
1202 pages = get_pages(obj);
1203 msm_gem_unlock(obj);
1204 if (IS_ERR(pages)) {
1205 ret = PTR_ERR(pages);
1209 vma->iova = physaddr(obj);
1211 ret = drm_gem_object_init(dev, obj, size);
1215 * Our buffers are kept pinned, so allocating them from the
1216 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1217 * See comments above new_inode() why this is required _and_
1218 * expected if you're going to pin these pages.
1220 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
1223 mutex_lock(&priv->mm_lock);
1224 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
1225 mutex_unlock(&priv->mm_lock);
1227 mutex_lock(&priv->obj_lock);
1228 list_add_tail(&msm_obj->node, &priv->objects);
1229 mutex_unlock(&priv->obj_lock);
1234 drm_gem_object_put(obj);
1235 return ERR_PTR(ret);
1238 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
1239 struct dma_buf *dmabuf, struct sg_table *sgt)
1241 struct msm_drm_private *priv = dev->dev_private;
1242 struct msm_gem_object *msm_obj;
1243 struct drm_gem_object *obj;
1247 /* if we don't have IOMMU, don't bother pretending we can import: */
1248 if (!msm_use_mmu(dev)) {
1249 DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
1250 return ERR_PTR(-EINVAL);
1253 size = PAGE_ALIGN(dmabuf->size);
1255 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
1257 return ERR_PTR(ret);
1259 drm_gem_private_object_init(dev, obj, size);
1261 npages = size / PAGE_SIZE;
1263 msm_obj = to_msm_bo(obj);
1266 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1267 if (!msm_obj->pages) {
1268 msm_gem_unlock(obj);
1273 ret = drm_prime_sg_to_page_array(sgt, msm_obj->pages, npages);
1275 msm_gem_unlock(obj);
1279 msm_gem_unlock(obj);
1281 mutex_lock(&priv->mm_lock);
1282 list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
1283 mutex_unlock(&priv->mm_lock);
1285 mutex_lock(&priv->obj_lock);
1286 list_add_tail(&msm_obj->node, &priv->objects);
1287 mutex_unlock(&priv->obj_lock);
1292 drm_gem_object_put(obj);
1293 return ERR_PTR(ret);
1296 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1297 uint32_t flags, struct msm_gem_address_space *aspace,
1298 struct drm_gem_object **bo, uint64_t *iova)
1301 struct drm_gem_object *obj = msm_gem_new(dev, size, flags);
1305 return ERR_CAST(obj);
1308 ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
1313 vaddr = msm_gem_get_vaddr(obj);
1314 if (IS_ERR(vaddr)) {
1315 msm_gem_unpin_iova(obj, aspace);
1316 ret = PTR_ERR(vaddr);
1325 drm_gem_object_put(obj);
1327 return ERR_PTR(ret);
1331 void msm_gem_kernel_put(struct drm_gem_object *bo,
1332 struct msm_gem_address_space *aspace)
1334 if (IS_ERR_OR_NULL(bo))
1337 msm_gem_put_vaddr(bo);
1338 msm_gem_unpin_iova(bo, aspace);
1339 drm_gem_object_put(bo);
1342 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
1344 struct msm_gem_object *msm_obj = to_msm_bo(bo);
1351 vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);