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