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_lru(struct drm_gem_object *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)
133 sync_for_device(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)
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;
181 static struct page **msm_gem_pin_pages_locked(struct drm_gem_object *obj)
183 struct msm_gem_object *msm_obj = to_msm_bo(obj);
186 GEM_WARN_ON(!msm_gem_is_locked(obj));
188 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
189 return ERR_PTR(-EBUSY);
194 to_msm_bo(obj)->pin_count++;
201 struct page **msm_gem_pin_pages(struct drm_gem_object *obj)
206 p = msm_gem_pin_pages_locked(obj);
212 void msm_gem_unpin_pages(struct drm_gem_object *obj)
215 msm_gem_unpin_locked(obj);
219 static pgprot_t msm_gem_pgprot(struct msm_gem_object *msm_obj, pgprot_t prot)
221 if (msm_obj->flags & MSM_BO_WC)
222 return pgprot_writecombine(prot);
226 static vm_fault_t msm_gem_fault(struct vm_fault *vmf)
228 struct vm_area_struct *vma = vmf->vma;
229 struct drm_gem_object *obj = vma->vm_private_data;
230 struct msm_gem_object *msm_obj = to_msm_bo(obj);
238 * vm_ops.open/drm_gem_mmap_obj and close get and put
239 * a reference on obj. So, we dont need to hold one here.
241 err = msm_gem_lock_interruptible(obj);
243 ret = VM_FAULT_NOPAGE;
247 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
249 return VM_FAULT_SIGBUS;
252 /* make sure we have pages attached now */
253 pages = get_pages(obj);
255 ret = vmf_error(PTR_ERR(pages));
259 /* We don't use vmf->pgoff since that has the fake offset: */
260 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
262 pfn = page_to_pfn(pages[pgoff]);
264 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
265 pfn, pfn << PAGE_SHIFT);
267 ret = vmf_insert_pfn(vma, vmf->address, pfn);
275 /** get mmap offset */
276 static uint64_t mmap_offset(struct drm_gem_object *obj)
278 struct drm_device *dev = obj->dev;
281 GEM_WARN_ON(!msm_gem_is_locked(obj));
283 /* Make it mmapable */
284 ret = drm_gem_create_mmap_offset(obj);
287 DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
291 return drm_vma_node_offset_addr(&obj->vma_node);
294 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
299 offset = mmap_offset(obj);
304 static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
305 struct msm_gem_address_space *aspace)
307 struct msm_gem_object *msm_obj = to_msm_bo(obj);
308 struct msm_gem_vma *vma;
310 GEM_WARN_ON(!msm_gem_is_locked(obj));
312 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
314 return ERR_PTR(-ENOMEM);
316 vma->aspace = aspace;
318 list_add_tail(&vma->list, &msm_obj->vmas);
323 static struct msm_gem_vma *lookup_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 list_for_each_entry(vma, &msm_obj->vmas, list) {
332 if (vma->aspace == aspace)
339 static void del_vma(struct msm_gem_vma *vma)
344 list_del(&vma->list);
349 * If close is true, this also closes the VMA (releasing the allocated
350 * iova range) in addition to removing the iommu mapping. In the eviction
351 * case (!close), we keep the iova allocated, but only remove the iommu
355 put_iova_spaces(struct drm_gem_object *obj, bool close)
357 struct msm_gem_object *msm_obj = to_msm_bo(obj);
358 struct msm_gem_vma *vma;
360 GEM_WARN_ON(!msm_gem_is_locked(obj));
362 list_for_each_entry(vma, &msm_obj->vmas, list) {
364 msm_gem_purge_vma(vma->aspace, vma);
366 msm_gem_close_vma(vma->aspace, vma);
371 /* Called with msm_obj locked */
373 put_iova_vmas(struct drm_gem_object *obj)
375 struct msm_gem_object *msm_obj = to_msm_bo(obj);
376 struct msm_gem_vma *vma, *tmp;
378 GEM_WARN_ON(!msm_gem_is_locked(obj));
380 list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
385 static struct msm_gem_vma *get_vma_locked(struct drm_gem_object *obj,
386 struct msm_gem_address_space *aspace,
387 u64 range_start, u64 range_end)
389 struct msm_gem_vma *vma;
391 GEM_WARN_ON(!msm_gem_is_locked(obj));
393 vma = lookup_vma(obj, aspace);
398 vma = add_vma(obj, aspace);
402 ret = msm_gem_init_vma(aspace, vma, obj->size,
403 range_start, range_end);
409 GEM_WARN_ON(vma->iova < range_start);
410 GEM_WARN_ON((vma->iova + obj->size) > range_end);
416 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma)
418 struct msm_gem_object *msm_obj = to_msm_bo(obj);
420 int ret, prot = IOMMU_READ;
422 if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
425 if (msm_obj->flags & MSM_BO_MAP_PRIV)
428 if (msm_obj->flags & MSM_BO_CACHED_COHERENT)
431 GEM_WARN_ON(!msm_gem_is_locked(obj));
433 if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
436 pages = msm_gem_pin_pages_locked(obj);
438 return PTR_ERR(pages);
440 ret = msm_gem_map_vma(vma->aspace, vma, prot, msm_obj->sgt, obj->size);
442 msm_gem_unpin_locked(obj);
447 void msm_gem_unpin_locked(struct drm_gem_object *obj)
449 struct msm_gem_object *msm_obj = to_msm_bo(obj);
451 GEM_WARN_ON(!msm_gem_is_locked(obj));
453 msm_obj->pin_count--;
454 GEM_WARN_ON(msm_obj->pin_count < 0);
459 struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj,
460 struct msm_gem_address_space *aspace)
462 return get_vma_locked(obj, aspace, 0, U64_MAX);
465 static int get_and_pin_iova_range_locked(struct drm_gem_object *obj,
466 struct msm_gem_address_space *aspace, uint64_t *iova,
467 u64 range_start, u64 range_end)
469 struct msm_gem_vma *vma;
472 GEM_WARN_ON(!msm_gem_is_locked(obj));
474 vma = get_vma_locked(obj, aspace, range_start, range_end);
478 ret = msm_gem_pin_vma_locked(obj, vma);
486 * get iova and pin it. Should have a matching put
487 * limits iova to specified range (in pages)
489 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
490 struct msm_gem_address_space *aspace, uint64_t *iova,
491 u64 range_start, u64 range_end)
496 ret = get_and_pin_iova_range_locked(obj, aspace, iova, range_start, range_end);
502 /* get iova and pin it. Should have a matching put */
503 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
504 struct msm_gem_address_space *aspace, uint64_t *iova)
506 return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX);
510 * Get an iova but don't pin it. Doesn't need a put because iovas are currently
511 * valid for the life of the object
513 int msm_gem_get_iova(struct drm_gem_object *obj,
514 struct msm_gem_address_space *aspace, uint64_t *iova)
516 struct msm_gem_vma *vma;
520 vma = get_vma_locked(obj, aspace, 0, U64_MAX);
531 static int clear_iova(struct drm_gem_object *obj,
532 struct msm_gem_address_space *aspace)
534 struct msm_gem_vma *vma = lookup_vma(obj, aspace);
539 if (msm_gem_vma_inuse(vma))
542 msm_gem_purge_vma(vma->aspace, vma);
543 msm_gem_close_vma(vma->aspace, vma);
550 * Get the requested iova but don't pin it. Fails if the requested iova is
551 * not available. Doesn't need a put because iovas are currently valid for
552 * the life of the object.
554 * Setting an iova of zero will clear the vma.
556 int msm_gem_set_iova(struct drm_gem_object *obj,
557 struct msm_gem_address_space *aspace, uint64_t iova)
563 ret = clear_iova(obj, aspace);
565 struct msm_gem_vma *vma;
566 vma = get_vma_locked(obj, aspace, iova, iova + obj->size);
569 } else if (GEM_WARN_ON(vma->iova != iova)) {
570 clear_iova(obj, aspace);
580 * Unpin a iova by updating the reference counts. The memory isn't actually
581 * purged until something else (shrinker, mm_notifier, destroy, etc) decides
584 void msm_gem_unpin_iova(struct drm_gem_object *obj,
585 struct msm_gem_address_space *aspace)
587 struct msm_gem_vma *vma;
590 vma = lookup_vma(obj, aspace);
591 if (!GEM_WARN_ON(!vma)) {
592 msm_gem_unpin_vma(vma);
593 msm_gem_unpin_locked(obj);
598 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
599 struct drm_mode_create_dumb *args)
601 args->pitch = align_pitch(args->width, args->bpp);
602 args->size = PAGE_ALIGN(args->pitch * args->height);
603 return msm_gem_new_handle(dev, file, args->size,
604 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb");
607 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
608 uint32_t handle, uint64_t *offset)
610 struct drm_gem_object *obj;
613 /* GEM does all our handle to object mapping */
614 obj = drm_gem_object_lookup(file, handle);
620 *offset = msm_gem_mmap_offset(obj);
622 drm_gem_object_put(obj);
628 static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
630 struct msm_gem_object *msm_obj = to_msm_bo(obj);
633 GEM_WARN_ON(!msm_gem_is_locked(obj));
635 if (obj->import_attach)
636 return ERR_PTR(-ENODEV);
638 if (GEM_WARN_ON(msm_obj->madv > madv)) {
639 DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n",
640 msm_obj->madv, madv);
641 return ERR_PTR(-EBUSY);
644 /* increment vmap_count *before* vmap() call, so shrinker can
645 * check vmap_count (is_vunmapable()) outside of msm_obj lock.
646 * This guarantees that we won't try to msm_gem_vunmap() this
647 * same object from within the vmap() call (while we already
650 msm_obj->vmap_count++;
652 if (!msm_obj->vaddr) {
653 struct page **pages = get_pages(obj);
655 ret = PTR_ERR(pages);
658 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
659 VM_MAP, msm_gem_pgprot(msm_obj, PAGE_KERNEL));
660 if (msm_obj->vaddr == NULL) {
668 return msm_obj->vaddr;
671 msm_obj->vmap_count--;
675 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
677 return get_vaddr(obj, MSM_MADV_WILLNEED);
680 void *msm_gem_get_vaddr(struct drm_gem_object *obj)
685 ret = msm_gem_get_vaddr_locked(obj);
692 * Don't use this! It is for the very special case of dumping
693 * submits from GPU hangs or faults, were the bo may already
694 * be MSM_MADV_DONTNEED, but we know the buffer is still on the
697 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
699 return get_vaddr(obj, __MSM_MADV_PURGED);
702 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
704 struct msm_gem_object *msm_obj = to_msm_bo(obj);
706 GEM_WARN_ON(!msm_gem_is_locked(obj));
707 GEM_WARN_ON(msm_obj->vmap_count < 1);
709 msm_obj->vmap_count--;
712 void msm_gem_put_vaddr(struct drm_gem_object *obj)
715 msm_gem_put_vaddr_locked(obj);
719 /* Update madvise status, returns true if not purged, else
722 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
724 struct msm_gem_object *msm_obj = to_msm_bo(obj);
728 if (msm_obj->madv != __MSM_MADV_PURGED)
729 msm_obj->madv = madv;
731 madv = msm_obj->madv;
733 /* If the obj is inactive, we might need to move it
734 * between inactive lists
740 return (madv != __MSM_MADV_PURGED);
743 void msm_gem_purge(struct drm_gem_object *obj)
745 struct drm_device *dev = obj->dev;
746 struct msm_gem_object *msm_obj = to_msm_bo(obj);
748 GEM_WARN_ON(!msm_gem_is_locked(obj));
749 GEM_WARN_ON(!is_purgeable(msm_obj));
751 /* Get rid of any iommu mapping(s): */
752 put_iova_spaces(obj, true);
756 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
762 msm_obj->madv = __MSM_MADV_PURGED;
764 drm_gem_free_mmap_offset(obj);
766 /* Our goal here is to return as much of the memory as
767 * is possible back to the system as we are called from OOM.
768 * To do this we must instruct the shmfs to drop all of its
769 * backing pages, *now*.
771 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
773 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
778 * Unpin the backing pages and make them available to be swapped out.
780 void msm_gem_evict(struct drm_gem_object *obj)
782 struct drm_device *dev = obj->dev;
783 struct msm_gem_object *msm_obj = to_msm_bo(obj);
785 GEM_WARN_ON(!msm_gem_is_locked(obj));
786 GEM_WARN_ON(is_unevictable(msm_obj));
788 /* Get rid of any iommu mapping(s): */
789 put_iova_spaces(obj, false);
791 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
796 void msm_gem_vunmap(struct drm_gem_object *obj)
798 struct msm_gem_object *msm_obj = to_msm_bo(obj);
800 GEM_WARN_ON(!msm_gem_is_locked(obj));
802 if (!msm_obj->vaddr || GEM_WARN_ON(!is_vunmapable(msm_obj)))
805 vunmap(msm_obj->vaddr);
806 msm_obj->vaddr = NULL;
809 static void update_lru(struct drm_gem_object *obj)
811 struct msm_drm_private *priv = obj->dev->dev_private;
812 struct msm_gem_object *msm_obj = to_msm_bo(obj);
814 GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
816 if (!msm_obj->pages) {
817 GEM_WARN_ON(msm_obj->pin_count);
818 GEM_WARN_ON(msm_obj->vmap_count);
820 drm_gem_lru_move_tail(&priv->lru.unbacked, obj);
821 } else if (msm_obj->pin_count || msm_obj->vmap_count) {
822 drm_gem_lru_move_tail(&priv->lru.pinned, obj);
823 } else if (msm_obj->madv == MSM_MADV_WILLNEED) {
824 drm_gem_lru_move_tail(&priv->lru.willneed, obj);
826 GEM_WARN_ON(msm_obj->madv != MSM_MADV_DONTNEED);
828 drm_gem_lru_move_tail(&priv->lru.dontneed, obj);
832 bool msm_gem_active(struct drm_gem_object *obj)
834 GEM_WARN_ON(!msm_gem_is_locked(obj));
836 if (to_msm_bo(obj)->pin_count)
839 return !dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true));
842 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
844 bool write = !!(op & MSM_PREP_WRITE);
845 unsigned long remain =
846 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
849 ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(write),
852 return remain == 0 ? -EBUSY : -ETIMEDOUT;
856 /* TODO cache maintenance */
861 int msm_gem_cpu_fini(struct drm_gem_object *obj)
863 /* TODO cache maintenance */
867 #ifdef CONFIG_DEBUG_FS
868 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
869 struct msm_gem_stats *stats)
871 struct msm_gem_object *msm_obj = to_msm_bo(obj);
872 struct dma_resv *robj = obj->resv;
873 struct msm_gem_vma *vma;
874 uint64_t off = drm_vma_node_start(&obj->vma_node);
880 stats->all.size += obj->size;
882 if (msm_gem_active(obj)) {
883 stats->active.count++;
884 stats->active.size += obj->size;
887 if (msm_obj->pages) {
888 stats->resident.count++;
889 stats->resident.size += obj->size;
892 switch (msm_obj->madv) {
893 case __MSM_MADV_PURGED:
894 stats->purged.count++;
895 stats->purged.size += obj->size;
898 case MSM_MADV_DONTNEED:
899 stats->purgeable.count++;
900 stats->purgeable.size += obj->size;
903 case MSM_MADV_WILLNEED:
909 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p",
910 msm_obj->flags, msm_gem_active(obj) ? 'A' : 'I',
911 obj->name, kref_read(&obj->refcount),
912 off, msm_obj->vaddr);
914 seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
916 if (!list_empty(&msm_obj->vmas)) {
918 seq_puts(m, " vmas:");
920 list_for_each_entry(vma, &msm_obj->vmas, list) {
921 const char *name, *comm;
923 struct msm_gem_address_space *aspace = vma->aspace;
924 struct task_struct *task =
925 get_pid_task(aspace->pid, PIDTYPE_PID);
927 comm = kstrdup(task->comm, GFP_KERNEL);
928 put_task_struct(task);
936 seq_printf(m, " [%s%s%s: aspace=%p, %08llx,%s,inuse=%d]",
937 name, comm ? ":" : "", comm ? comm : "",
938 vma->aspace, vma->iova,
939 vma->mapped ? "mapped" : "unmapped",
940 msm_gem_vma_inuse(vma));
947 dma_resv_describe(robj, m);
951 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
953 struct msm_gem_stats stats = {};
954 struct msm_gem_object *msm_obj;
956 seq_puts(m, " flags id ref offset kaddr size madv name\n");
957 list_for_each_entry(msm_obj, list, node) {
958 struct drm_gem_object *obj = &msm_obj->base;
960 msm_gem_describe(obj, m, &stats);
963 seq_printf(m, "Total: %4d objects, %9zu bytes\n",
964 stats.all.count, stats.all.size);
965 seq_printf(m, "Active: %4d objects, %9zu bytes\n",
966 stats.active.count, stats.active.size);
967 seq_printf(m, "Resident: %4d objects, %9zu bytes\n",
968 stats.resident.count, stats.resident.size);
969 seq_printf(m, "Purgeable: %4d objects, %9zu bytes\n",
970 stats.purgeable.count, stats.purgeable.size);
971 seq_printf(m, "Purged: %4d objects, %9zu bytes\n",
972 stats.purged.count, stats.purged.size);
976 /* don't call directly! Use drm_gem_object_put() */
977 static void msm_gem_free_object(struct drm_gem_object *obj)
979 struct msm_gem_object *msm_obj = to_msm_bo(obj);
980 struct drm_device *dev = obj->dev;
981 struct msm_drm_private *priv = dev->dev_private;
983 mutex_lock(&priv->obj_lock);
984 list_del(&msm_obj->node);
985 mutex_unlock(&priv->obj_lock);
987 put_iova_spaces(obj, true);
989 if (obj->import_attach) {
990 GEM_WARN_ON(msm_obj->vaddr);
992 /* Don't drop the pages for imported dmabuf, as they are not
993 * ours, just free the array we allocated:
995 kvfree(msm_obj->pages);
999 drm_prime_gem_destroy(obj, msm_obj->sgt);
1001 msm_gem_vunmap(obj);
1006 drm_gem_object_release(obj);
1011 static int msm_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
1013 struct msm_gem_object *msm_obj = to_msm_bo(obj);
1015 vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1016 vma->vm_page_prot = msm_gem_pgprot(msm_obj, vm_get_page_prot(vma->vm_flags));
1021 /* convenience method to construct a GEM buffer object, and userspace handle */
1022 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1023 uint32_t size, uint32_t flags, uint32_t *handle,
1026 struct drm_gem_object *obj;
1029 obj = msm_gem_new(dev, size, flags);
1032 return PTR_ERR(obj);
1035 msm_gem_object_set_name(obj, "%s", name);
1037 ret = drm_gem_handle_create(file, obj, handle);
1039 /* drop reference from allocate - handle holds it now */
1040 drm_gem_object_put(obj);
1045 static const struct vm_operations_struct vm_ops = {
1046 .fault = msm_gem_fault,
1047 .open = drm_gem_vm_open,
1048 .close = drm_gem_vm_close,
1051 static const struct drm_gem_object_funcs msm_gem_object_funcs = {
1052 .free = msm_gem_free_object,
1053 .pin = msm_gem_prime_pin,
1054 .unpin = msm_gem_prime_unpin,
1055 .get_sg_table = msm_gem_prime_get_sg_table,
1056 .vmap = msm_gem_prime_vmap,
1057 .vunmap = msm_gem_prime_vunmap,
1058 .mmap = msm_gem_object_mmap,
1062 static int msm_gem_new_impl(struct drm_device *dev,
1063 uint32_t size, uint32_t flags,
1064 struct drm_gem_object **obj)
1066 struct msm_drm_private *priv = dev->dev_private;
1067 struct msm_gem_object *msm_obj;
1069 switch (flags & MSM_BO_CACHE_MASK) {
1073 case MSM_BO_CACHED_COHERENT:
1074 if (priv->has_cached_coherent)
1078 DRM_DEV_DEBUG(dev->dev, "invalid cache flag: %x\n",
1079 (flags & MSM_BO_CACHE_MASK));
1083 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
1087 msm_obj->flags = flags;
1088 msm_obj->madv = MSM_MADV_WILLNEED;
1090 INIT_LIST_HEAD(&msm_obj->node);
1091 INIT_LIST_HEAD(&msm_obj->vmas);
1093 *obj = &msm_obj->base;
1094 (*obj)->funcs = &msm_gem_object_funcs;
1099 struct drm_gem_object *msm_gem_new(struct drm_device *dev, uint32_t size, uint32_t flags)
1101 struct msm_drm_private *priv = dev->dev_private;
1102 struct msm_gem_object *msm_obj;
1103 struct drm_gem_object *obj = NULL;
1104 bool use_vram = false;
1107 size = PAGE_ALIGN(size);
1109 if (!msm_use_mmu(dev))
1111 else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
1114 if (GEM_WARN_ON(use_vram && !priv->vram.size))
1115 return ERR_PTR(-EINVAL);
1117 /* Disallow zero sized objects as they make the underlying
1118 * infrastructure grumpy
1121 return ERR_PTR(-EINVAL);
1123 ret = msm_gem_new_impl(dev, size, flags, &obj);
1125 return ERR_PTR(ret);
1127 msm_obj = to_msm_bo(obj);
1130 struct msm_gem_vma *vma;
1131 struct page **pages;
1133 drm_gem_private_object_init(dev, obj, size);
1137 vma = add_vma(obj, NULL);
1138 msm_gem_unlock(obj);
1144 to_msm_bo(obj)->vram_node = &vma->node;
1147 pages = get_pages(obj);
1148 msm_gem_unlock(obj);
1149 if (IS_ERR(pages)) {
1150 ret = PTR_ERR(pages);
1154 vma->iova = physaddr(obj);
1156 ret = drm_gem_object_init(dev, obj, size);
1160 * Our buffers are kept pinned, so allocating them from the
1161 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1162 * See comments above new_inode() why this is required _and_
1163 * expected if you're going to pin these pages.
1165 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
1168 drm_gem_lru_move_tail(&priv->lru.unbacked, obj);
1170 mutex_lock(&priv->obj_lock);
1171 list_add_tail(&msm_obj->node, &priv->objects);
1172 mutex_unlock(&priv->obj_lock);
1177 drm_gem_object_put(obj);
1178 return ERR_PTR(ret);
1181 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
1182 struct dma_buf *dmabuf, struct sg_table *sgt)
1184 struct msm_drm_private *priv = dev->dev_private;
1185 struct msm_gem_object *msm_obj;
1186 struct drm_gem_object *obj;
1190 /* if we don't have IOMMU, don't bother pretending we can import: */
1191 if (!msm_use_mmu(dev)) {
1192 DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
1193 return ERR_PTR(-EINVAL);
1196 size = PAGE_ALIGN(dmabuf->size);
1198 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
1200 return ERR_PTR(ret);
1202 drm_gem_private_object_init(dev, obj, size);
1204 npages = size / PAGE_SIZE;
1206 msm_obj = to_msm_bo(obj);
1209 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1210 if (!msm_obj->pages) {
1211 msm_gem_unlock(obj);
1216 ret = drm_prime_sg_to_page_array(sgt, msm_obj->pages, npages);
1218 msm_gem_unlock(obj);
1222 msm_gem_unlock(obj);
1224 drm_gem_lru_move_tail(&priv->lru.pinned, obj);
1226 mutex_lock(&priv->obj_lock);
1227 list_add_tail(&msm_obj->node, &priv->objects);
1228 mutex_unlock(&priv->obj_lock);
1233 drm_gem_object_put(obj);
1234 return ERR_PTR(ret);
1237 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1238 uint32_t flags, struct msm_gem_address_space *aspace,
1239 struct drm_gem_object **bo, uint64_t *iova)
1242 struct drm_gem_object *obj = msm_gem_new(dev, size, flags);
1246 return ERR_CAST(obj);
1249 ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
1254 vaddr = msm_gem_get_vaddr(obj);
1255 if (IS_ERR(vaddr)) {
1256 msm_gem_unpin_iova(obj, aspace);
1257 ret = PTR_ERR(vaddr);
1266 drm_gem_object_put(obj);
1268 return ERR_PTR(ret);
1272 void msm_gem_kernel_put(struct drm_gem_object *bo,
1273 struct msm_gem_address_space *aspace)
1275 if (IS_ERR_OR_NULL(bo))
1278 msm_gem_put_vaddr(bo);
1279 msm_gem_unpin_iova(bo, aspace);
1280 drm_gem_object_put(bo);
1283 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
1285 struct msm_gem_object *msm_obj = to_msm_bo(bo);
1292 vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);