Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / etnaviv / etnaviv_gem_prime.c
blobd9bd83203a15e5d4cb10f0875e1067a6afbe4c49
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2014-2018 Etnaviv Project
4 */
6 #include <drm/drm_prime.h>
7 #include <linux/dma-buf.h>
9 #include "etnaviv_drv.h"
10 #include "etnaviv_gem.h"
12 static struct lock_class_key etnaviv_prime_lock_class;
14 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj)
16 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
17 int npages = obj->size >> PAGE_SHIFT;
19 if (WARN_ON(!etnaviv_obj->pages)) /* should have already pinned! */
20 return ERR_PTR(-EINVAL);
22 return drm_prime_pages_to_sg(obj->dev, etnaviv_obj->pages, npages);
25 int etnaviv_gem_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
27 void *vaddr;
29 vaddr = etnaviv_gem_vmap(obj);
30 if (!vaddr)
31 return -ENOMEM;
32 dma_buf_map_set_vaddr(map, vaddr);
34 return 0;
37 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
38 struct vm_area_struct *vma)
40 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
41 int ret;
43 ret = drm_gem_mmap_obj(obj, obj->size, vma);
44 if (ret < 0)
45 return ret;
47 return etnaviv_obj->ops->mmap(etnaviv_obj, vma);
50 int etnaviv_gem_prime_pin(struct drm_gem_object *obj)
52 if (!obj->import_attach) {
53 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
55 mutex_lock(&etnaviv_obj->lock);
56 etnaviv_gem_get_pages(etnaviv_obj);
57 mutex_unlock(&etnaviv_obj->lock);
59 return 0;
62 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj)
64 if (!obj->import_attach) {
65 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
67 mutex_lock(&etnaviv_obj->lock);
68 etnaviv_gem_put_pages(to_etnaviv_bo(obj));
69 mutex_unlock(&etnaviv_obj->lock);
73 static void etnaviv_gem_prime_release(struct etnaviv_gem_object *etnaviv_obj)
75 struct dma_buf_map map = DMA_BUF_MAP_INIT_VADDR(etnaviv_obj->vaddr);
77 if (etnaviv_obj->vaddr)
78 dma_buf_vunmap(etnaviv_obj->base.import_attach->dmabuf, &map);
80 /* Don't drop the pages for imported dmabuf, as they are not
81 * ours, just free the array we allocated:
83 if (etnaviv_obj->pages)
84 kvfree(etnaviv_obj->pages);
86 drm_prime_gem_destroy(&etnaviv_obj->base, etnaviv_obj->sgt);
89 static void *etnaviv_gem_prime_vmap_impl(struct etnaviv_gem_object *etnaviv_obj)
91 struct dma_buf_map map;
92 int ret;
94 lockdep_assert_held(&etnaviv_obj->lock);
96 ret = dma_buf_vmap(etnaviv_obj->base.import_attach->dmabuf, &map);
97 if (ret)
98 return NULL;
99 return map.vaddr;
102 static int etnaviv_gem_prime_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
103 struct vm_area_struct *vma)
105 return dma_buf_mmap(etnaviv_obj->base.dma_buf, vma, 0);
108 static const struct etnaviv_gem_ops etnaviv_gem_prime_ops = {
109 /* .get_pages should never be called */
110 .release = etnaviv_gem_prime_release,
111 .vmap = etnaviv_gem_prime_vmap_impl,
112 .mmap = etnaviv_gem_prime_mmap_obj,
115 struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev,
116 struct dma_buf_attachment *attach, struct sg_table *sgt)
118 struct etnaviv_gem_object *etnaviv_obj;
119 size_t size = PAGE_ALIGN(attach->dmabuf->size);
120 int ret, npages;
122 ret = etnaviv_gem_new_private(dev, size, ETNA_BO_WC,
123 &etnaviv_gem_prime_ops, &etnaviv_obj);
124 if (ret < 0)
125 return ERR_PTR(ret);
127 lockdep_set_class(&etnaviv_obj->lock, &etnaviv_prime_lock_class);
129 npages = size / PAGE_SIZE;
131 etnaviv_obj->sgt = sgt;
132 etnaviv_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
133 if (!etnaviv_obj->pages) {
134 ret = -ENOMEM;
135 goto fail;
138 ret = drm_prime_sg_to_page_addr_arrays(sgt, etnaviv_obj->pages,
139 NULL, npages);
140 if (ret)
141 goto fail;
143 etnaviv_gem_obj_add(dev, &etnaviv_obj->base);
145 return &etnaviv_obj->base;
147 fail:
148 drm_gem_object_put(&etnaviv_obj->base);
150 return ERR_PTR(ret);