xref: /linux/drivers/gpu/drm/etnaviv/etnaviv_gem_prime.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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 
23 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj)
24 {
25 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
26 	int npages = obj->size >> PAGE_SHIFT;
27 
28 	if (WARN_ON(!etnaviv_obj->pages))  /* should have already pinned! */
29 		return NULL;
30 
31 	return drm_prime_pages_to_sg(etnaviv_obj->pages, npages);
32 }
33 
34 void *etnaviv_gem_prime_vmap(struct drm_gem_object *obj)
35 {
36 	return etnaviv_gem_vmap(obj);
37 }
38 
39 void etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
40 {
41 	/* TODO msm_gem_vunmap() */
42 }
43 
44 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
45 			   struct vm_area_struct *vma)
46 {
47 	struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
48 	int ret;
49 
50 	ret = drm_gem_mmap_obj(obj, obj->size, vma);
51 	if (ret < 0)
52 		return ret;
53 
54 	return etnaviv_obj->ops->mmap(etnaviv_obj, vma);
55 }
56 
57 int etnaviv_gem_prime_pin(struct drm_gem_object *obj)
58 {
59 	if (!obj->import_attach) {
60 		struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
61 
62 		mutex_lock(&etnaviv_obj->lock);
63 		etnaviv_gem_get_pages(etnaviv_obj);
64 		mutex_unlock(&etnaviv_obj->lock);
65 	}
66 	return 0;
67 }
68 
69 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj)
70 {
71 	if (!obj->import_attach) {
72 		struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
73 
74 		mutex_lock(&etnaviv_obj->lock);
75 		etnaviv_gem_put_pages(to_etnaviv_bo(obj));
76 		mutex_unlock(&etnaviv_obj->lock);
77 	}
78 }
79 
80 static void etnaviv_gem_prime_release(struct etnaviv_gem_object *etnaviv_obj)
81 {
82 	if (etnaviv_obj->vaddr)
83 		dma_buf_vunmap(etnaviv_obj->base.import_attach->dmabuf,
84 			       etnaviv_obj->vaddr);
85 
86 	/* Don't drop the pages for imported dmabuf, as they are not
87 	 * ours, just free the array we allocated:
88 	 */
89 	if (etnaviv_obj->pages)
90 		drm_free_large(etnaviv_obj->pages);
91 
92 	drm_prime_gem_destroy(&etnaviv_obj->base, etnaviv_obj->sgt);
93 }
94 
95 static void *etnaviv_gem_prime_vmap_impl(struct etnaviv_gem_object *etnaviv_obj)
96 {
97 	lockdep_assert_held(&etnaviv_obj->lock);
98 
99 	return dma_buf_vmap(etnaviv_obj->base.import_attach->dmabuf);
100 }
101 
102 static int etnaviv_gem_prime_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
103 		struct vm_area_struct *vma)
104 {
105 	return dma_buf_mmap(etnaviv_obj->base.dma_buf, vma, 0);
106 }
107 
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,
113 };
114 
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)
117 {
118 	struct etnaviv_gem_object *etnaviv_obj;
119 	size_t size = PAGE_ALIGN(attach->dmabuf->size);
120 	int ret, npages;
121 
122 	ret = etnaviv_gem_new_private(dev, size, ETNA_BO_WC,
123 				      attach->dmabuf->resv,
124 				      &etnaviv_gem_prime_ops, &etnaviv_obj);
125 	if (ret < 0)
126 		return ERR_PTR(ret);
127 
128 	npages = size / PAGE_SIZE;
129 
130 	etnaviv_obj->sgt = sgt;
131 	etnaviv_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
132 	if (!etnaviv_obj->pages) {
133 		ret = -ENOMEM;
134 		goto fail;
135 	}
136 
137 	ret = drm_prime_sg_to_page_addr_arrays(sgt, etnaviv_obj->pages,
138 					       NULL, npages);
139 	if (ret)
140 		goto fail;
141 
142 	ret = etnaviv_gem_obj_add(dev, &etnaviv_obj->base);
143 	if (ret)
144 		goto fail;
145 
146 	return &etnaviv_obj->base;
147 
148 fail:
149 	drm_gem_object_unreference_unlocked(&etnaviv_obj->base);
150 
151 	return ERR_PTR(ret);
152 }
153