1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2014-2018 Etnaviv Project 4 */ 5 6 #include <linux/dma-buf.h> 7 #include "etnaviv_drv.h" 8 #include "etnaviv_gem.h" 9 10 static struct lock_class_key etnaviv_prime_lock_class; 11 12 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj) 13 { 14 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 15 int npages = obj->size >> PAGE_SHIFT; 16 17 if (WARN_ON(!etnaviv_obj->pages)) /* should have already pinned! */ 18 return ERR_PTR(-EINVAL); 19 20 return drm_prime_pages_to_sg(etnaviv_obj->pages, npages); 21 } 22 23 void *etnaviv_gem_prime_vmap(struct drm_gem_object *obj) 24 { 25 return etnaviv_gem_vmap(obj); 26 } 27 28 void etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) 29 { 30 /* TODO msm_gem_vunmap() */ 31 } 32 33 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj, 34 struct vm_area_struct *vma) 35 { 36 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 37 int ret; 38 39 ret = drm_gem_mmap_obj(obj, obj->size, vma); 40 if (ret < 0) 41 return ret; 42 43 return etnaviv_obj->ops->mmap(etnaviv_obj, vma); 44 } 45 46 int etnaviv_gem_prime_pin(struct drm_gem_object *obj) 47 { 48 if (!obj->import_attach) { 49 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 50 51 mutex_lock(&etnaviv_obj->lock); 52 etnaviv_gem_get_pages(etnaviv_obj); 53 mutex_unlock(&etnaviv_obj->lock); 54 } 55 return 0; 56 } 57 58 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj) 59 { 60 if (!obj->import_attach) { 61 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 62 63 mutex_lock(&etnaviv_obj->lock); 64 etnaviv_gem_put_pages(to_etnaviv_bo(obj)); 65 mutex_unlock(&etnaviv_obj->lock); 66 } 67 } 68 69 static void etnaviv_gem_prime_release(struct etnaviv_gem_object *etnaviv_obj) 70 { 71 if (etnaviv_obj->vaddr) 72 dma_buf_vunmap(etnaviv_obj->base.import_attach->dmabuf, 73 etnaviv_obj->vaddr); 74 75 /* Don't drop the pages for imported dmabuf, as they are not 76 * ours, just free the array we allocated: 77 */ 78 if (etnaviv_obj->pages) 79 kvfree(etnaviv_obj->pages); 80 81 drm_prime_gem_destroy(&etnaviv_obj->base, etnaviv_obj->sgt); 82 } 83 84 static void *etnaviv_gem_prime_vmap_impl(struct etnaviv_gem_object *etnaviv_obj) 85 { 86 lockdep_assert_held(&etnaviv_obj->lock); 87 88 return dma_buf_vmap(etnaviv_obj->base.import_attach->dmabuf); 89 } 90 91 static int etnaviv_gem_prime_mmap_obj(struct etnaviv_gem_object *etnaviv_obj, 92 struct vm_area_struct *vma) 93 { 94 return dma_buf_mmap(etnaviv_obj->base.dma_buf, vma, 0); 95 } 96 97 static const struct etnaviv_gem_ops etnaviv_gem_prime_ops = { 98 /* .get_pages should never be called */ 99 .release = etnaviv_gem_prime_release, 100 .vmap = etnaviv_gem_prime_vmap_impl, 101 .mmap = etnaviv_gem_prime_mmap_obj, 102 }; 103 104 struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev, 105 struct dma_buf_attachment *attach, struct sg_table *sgt) 106 { 107 struct etnaviv_gem_object *etnaviv_obj; 108 size_t size = PAGE_ALIGN(attach->dmabuf->size); 109 int ret, npages; 110 111 ret = etnaviv_gem_new_private(dev, size, ETNA_BO_WC, 112 attach->dmabuf->resv, 113 &etnaviv_gem_prime_ops, &etnaviv_obj); 114 if (ret < 0) 115 return ERR_PTR(ret); 116 117 lockdep_set_class(&etnaviv_obj->lock, &etnaviv_prime_lock_class); 118 119 npages = size / PAGE_SIZE; 120 121 etnaviv_obj->sgt = sgt; 122 etnaviv_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 123 if (!etnaviv_obj->pages) { 124 ret = -ENOMEM; 125 goto fail; 126 } 127 128 ret = drm_prime_sg_to_page_addr_arrays(sgt, etnaviv_obj->pages, 129 NULL, npages); 130 if (ret) 131 goto fail; 132 133 etnaviv_gem_obj_add(dev, &etnaviv_obj->base); 134 135 return &etnaviv_obj->base; 136 137 fail: 138 drm_gem_object_put_unlocked(&etnaviv_obj->base); 139 140 return ERR_PTR(ret); 141 } 142