1 /* 2 * Copyright (C) 2013 Red Hat 3 * Author: Rob Clark <robdclark@gmail.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include <linux/dma-buf.h> 19 #include "etnaviv_drv.h" 20 #include "etnaviv_gem.h" 21 22 static struct lock_class_key etnaviv_prime_lock_class; 23 24 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj) 25 { 26 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 27 int npages = obj->size >> PAGE_SHIFT; 28 29 if (WARN_ON(!etnaviv_obj->pages)) /* should have already pinned! */ 30 return NULL; 31 32 return drm_prime_pages_to_sg(etnaviv_obj->pages, npages); 33 } 34 35 void *etnaviv_gem_prime_vmap(struct drm_gem_object *obj) 36 { 37 return etnaviv_gem_vmap(obj); 38 } 39 40 void etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) 41 { 42 /* TODO msm_gem_vunmap() */ 43 } 44 45 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj, 46 struct vm_area_struct *vma) 47 { 48 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 49 int ret; 50 51 ret = drm_gem_mmap_obj(obj, obj->size, vma); 52 if (ret < 0) 53 return ret; 54 55 return etnaviv_obj->ops->mmap(etnaviv_obj, vma); 56 } 57 58 int etnaviv_gem_prime_pin(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_get_pages(etnaviv_obj); 65 mutex_unlock(&etnaviv_obj->lock); 66 } 67 return 0; 68 } 69 70 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj) 71 { 72 if (!obj->import_attach) { 73 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 74 75 mutex_lock(&etnaviv_obj->lock); 76 etnaviv_gem_put_pages(to_etnaviv_bo(obj)); 77 mutex_unlock(&etnaviv_obj->lock); 78 } 79 } 80 81 static void etnaviv_gem_prime_release(struct etnaviv_gem_object *etnaviv_obj) 82 { 83 if (etnaviv_obj->vaddr) 84 dma_buf_vunmap(etnaviv_obj->base.import_attach->dmabuf, 85 etnaviv_obj->vaddr); 86 87 /* Don't drop the pages for imported dmabuf, as they are not 88 * ours, just free the array we allocated: 89 */ 90 if (etnaviv_obj->pages) 91 kvfree(etnaviv_obj->pages); 92 93 drm_prime_gem_destroy(&etnaviv_obj->base, etnaviv_obj->sgt); 94 } 95 96 static void *etnaviv_gem_prime_vmap_impl(struct etnaviv_gem_object *etnaviv_obj) 97 { 98 lockdep_assert_held(&etnaviv_obj->lock); 99 100 return dma_buf_vmap(etnaviv_obj->base.import_attach->dmabuf); 101 } 102 103 static int etnaviv_gem_prime_mmap_obj(struct etnaviv_gem_object *etnaviv_obj, 104 struct vm_area_struct *vma) 105 { 106 return dma_buf_mmap(etnaviv_obj->base.dma_buf, vma, 0); 107 } 108 109 static const struct etnaviv_gem_ops etnaviv_gem_prime_ops = { 110 /* .get_pages should never be called */ 111 .release = etnaviv_gem_prime_release, 112 .vmap = etnaviv_gem_prime_vmap_impl, 113 .mmap = etnaviv_gem_prime_mmap_obj, 114 }; 115 116 struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev, 117 struct dma_buf_attachment *attach, struct sg_table *sgt) 118 { 119 struct etnaviv_gem_object *etnaviv_obj; 120 size_t size = PAGE_ALIGN(attach->dmabuf->size); 121 int ret, npages; 122 123 ret = etnaviv_gem_new_private(dev, size, ETNA_BO_WC, 124 attach->dmabuf->resv, 125 &etnaviv_gem_prime_ops, &etnaviv_obj); 126 if (ret < 0) 127 return ERR_PTR(ret); 128 129 lockdep_set_class(&etnaviv_obj->lock, &etnaviv_prime_lock_class); 130 131 npages = size / PAGE_SIZE; 132 133 etnaviv_obj->sgt = sgt; 134 etnaviv_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 135 if (!etnaviv_obj->pages) { 136 ret = -ENOMEM; 137 goto fail; 138 } 139 140 ret = drm_prime_sg_to_page_addr_arrays(sgt, etnaviv_obj->pages, 141 NULL, npages); 142 if (ret) 143 goto fail; 144 145 etnaviv_gem_obj_add(dev, &etnaviv_obj->base); 146 147 return &etnaviv_obj->base; 148 149 fail: 150 drm_gem_object_put_unlocked(&etnaviv_obj->base); 151 152 return ERR_PTR(ret); 153 } 154 155 struct reservation_object *etnaviv_gem_prime_res_obj(struct drm_gem_object *obj) 156 { 157 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); 158 159 return etnaviv_obj->resv; 160 } 161