Linux 4.19.133
[linux/fpc-iii.git] / drivers / gpu / drm / etnaviv / etnaviv_gem_prime.c
blobf21529e635e3d8d9ae16ffd440a425ae5a93abf0
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2014-2018 Etnaviv Project
4 */
6 #include <linux/dma-buf.h>
7 #include "etnaviv_drv.h"
8 #include "etnaviv_gem.h"
10 static struct lock_class_key etnaviv_prime_lock_class;
12 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj)
14 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
15 int npages = obj->size >> PAGE_SHIFT;
17 if (WARN_ON(!etnaviv_obj->pages)) /* should have already pinned! */
18 return ERR_PTR(-EINVAL);
20 return drm_prime_pages_to_sg(etnaviv_obj->pages, npages);
23 void *etnaviv_gem_prime_vmap(struct drm_gem_object *obj)
25 return etnaviv_gem_vmap(obj);
28 void etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
30 /* TODO msm_gem_vunmap() */
33 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
34 struct vm_area_struct *vma)
36 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
37 int ret;
39 ret = drm_gem_mmap_obj(obj, obj->size, vma);
40 if (ret < 0)
41 return ret;
43 return etnaviv_obj->ops->mmap(etnaviv_obj, vma);
46 int etnaviv_gem_prime_pin(struct drm_gem_object *obj)
48 if (!obj->import_attach) {
49 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
51 mutex_lock(&etnaviv_obj->lock);
52 etnaviv_gem_get_pages(etnaviv_obj);
53 mutex_unlock(&etnaviv_obj->lock);
55 return 0;
58 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj)
60 if (!obj->import_attach) {
61 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
63 mutex_lock(&etnaviv_obj->lock);
64 etnaviv_gem_put_pages(to_etnaviv_bo(obj));
65 mutex_unlock(&etnaviv_obj->lock);
69 static void etnaviv_gem_prime_release(struct etnaviv_gem_object *etnaviv_obj)
71 if (etnaviv_obj->vaddr)
72 dma_buf_vunmap(etnaviv_obj->base.import_attach->dmabuf,
73 etnaviv_obj->vaddr);
75 /* Don't drop the pages for imported dmabuf, as they are not
76 * ours, just free the array we allocated:
78 if (etnaviv_obj->pages)
79 kvfree(etnaviv_obj->pages);
81 drm_prime_gem_destroy(&etnaviv_obj->base, etnaviv_obj->sgt);
84 static void *etnaviv_gem_prime_vmap_impl(struct etnaviv_gem_object *etnaviv_obj)
86 lockdep_assert_held(&etnaviv_obj->lock);
88 return dma_buf_vmap(etnaviv_obj->base.import_attach->dmabuf);
91 static int etnaviv_gem_prime_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
92 struct vm_area_struct *vma)
94 return dma_buf_mmap(etnaviv_obj->base.dma_buf, vma, 0);
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,
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)
107 struct etnaviv_gem_object *etnaviv_obj;
108 size_t size = PAGE_ALIGN(attach->dmabuf->size);
109 int ret, npages;
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);
117 lockdep_set_class(&etnaviv_obj->lock, &etnaviv_prime_lock_class);
119 npages = size / PAGE_SIZE;
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;
128 ret = drm_prime_sg_to_page_addr_arrays(sgt, etnaviv_obj->pages,
129 NULL, npages);
130 if (ret)
131 goto fail;
133 etnaviv_gem_obj_add(dev, &etnaviv_obj->base);
135 return &etnaviv_obj->base;
137 fail:
138 drm_gem_object_put_unlocked(&etnaviv_obj->base);
140 return ERR_PTR(ret);
143 struct reservation_object *etnaviv_gem_prime_res_obj(struct drm_gem_object *obj)
145 struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
147 return etnaviv_obj->resv;