35123dbe8b75d76923dd9af17981e8a682e11c4b
[kernel/linux-3.0.git] / drivers / gpu / drm / drm_prime.c
1 /*
2  * Copyright © 2012 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Dave Airlie <airlied@redhat.com>
25  *      Rob Clark <rob.clark@linaro.org>
26  *
27  */
28
29 #include <linux/dma-buf.h>
30 #include "drmP.h"
31
32 /*
33  * DMA-BUF/GEM Object references and lifetime overview:
34  *
35  * On the export the dma_buf holds a reference to the exporting GEM
36  * object. It takes this reference in handle_to_fd_ioctl, when it
37  * first calls .prime_export and stores the exporting GEM object in
38  * the dma_buf priv. This reference is released when the dma_buf
39  * object goes away in the driver .release function.
40  *
41  * On the import the importing GEM object holds a reference to the
42  * dma_buf (which in turn holds a ref to the exporting GEM object).
43  * It takes that reference in the fd_to_handle ioctl.
44  * It calls dma_buf_get, creates an attachment to it and stores the
45  * attachment in the GEM object. When this attachment is destroyed
46  * when the imported object is destroyed, we remove the attachment
47  * and drop the reference to the dma_buf.
48  *
49  * Thus the chain of references always flows in one direction
50  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
51  *
52  * Self-importing: if userspace is using PRIME as a replacement for flink
53  * then it will get a fd->handle request for a GEM object that it created.
54  * Drivers should detect this situation and return back the gem object
55  * from the dma-buf private.
56  */
57
58 struct drm_prime_member {
59         struct list_head entry;
60         struct dma_buf *dma_buf;
61         uint32_t handle;
62 };
63
64 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
65                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
66                 int *prime_fd)
67 {
68         struct drm_gem_object *obj;
69         void *buf;
70
71         obj = drm_gem_object_lookup(dev, file_priv, handle);
72         if (!obj)
73                 return -ENOENT;
74
75         mutex_lock(&file_priv->prime.lock);
76         /* re-export the original imported object */
77         if (obj->import_attach) {
78                 get_dma_buf(obj->import_attach->dmabuf);
79                 *prime_fd = dma_buf_fd(obj->import_attach->dmabuf, flags);
80                 drm_gem_object_unreference_unlocked(obj);
81                 mutex_unlock(&file_priv->prime.lock);
82                 return 0;
83         }
84
85         if (obj->export_dma_buf) {
86                 get_dma_buf(obj->export_dma_buf);
87                 *prime_fd = dma_buf_fd(obj->export_dma_buf, flags);
88                 drm_gem_object_unreference_unlocked(obj);
89         } else {
90                 buf = dev->driver->gem_prime_export(dev, obj, flags);
91                 if (IS_ERR(buf)) {
92                         /* normally the created dma-buf takes ownership of the ref,
93                          * but if that fails then drop the ref
94                          */
95                         drm_gem_object_unreference_unlocked(obj);
96                         mutex_unlock(&file_priv->prime.lock);
97                         return PTR_ERR(buf);
98                 }
99                 obj->export_dma_buf = buf;
100                 *prime_fd = dma_buf_fd(buf, flags);
101         }
102         mutex_unlock(&file_priv->prime.lock);
103         return 0;
104 }
105 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
106
107 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
108                 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
109 {
110         struct dma_buf *dma_buf;
111         struct drm_gem_object *obj;
112         int ret;
113
114         dma_buf = dma_buf_get(prime_fd);
115         if (IS_ERR(dma_buf))
116                 return PTR_ERR(dma_buf);
117
118         mutex_lock(&file_priv->prime.lock);
119
120         ret = drm_prime_lookup_imported_buf_handle(&file_priv->prime,
121                         dma_buf, handle);
122         if (!ret) {
123                 ret = 0;
124                 goto out_put;
125         }
126
127         /* never seen this one, need to import */
128         obj = dev->driver->gem_prime_import(dev, dma_buf);
129         if (IS_ERR(obj)) {
130                 ret = PTR_ERR(obj);
131                 goto out_put;
132         }
133
134         ret = drm_gem_handle_create(file_priv, obj, handle);
135         drm_gem_object_unreference_unlocked(obj);
136         if (ret)
137                 goto out_put;
138
139         ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
140                         dma_buf, *handle);
141         if (ret)
142                 goto fail;
143
144         mutex_unlock(&file_priv->prime.lock);
145         return 0;
146
147 fail:
148         /* hmm, if driver attached, we are relying on the free-object path
149          * to detach.. which seems ok..
150          */
151         drm_gem_object_handle_unreference_unlocked(obj);
152 out_put:
153         dma_buf_put(dma_buf);
154         mutex_unlock(&file_priv->prime.lock);
155         return ret;
156 }
157 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
158
159 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
160                                  struct drm_file *file_priv)
161 {
162         struct drm_prime_handle *args = data;
163         uint32_t flags;
164
165         if (!drm_core_check_feature(dev, DRIVER_PRIME))
166                 return -EINVAL;
167
168         if (!dev->driver->prime_handle_to_fd)
169                 return -ENOSYS;
170
171         /* check flags are valid */
172         if (args->flags & ~DRM_CLOEXEC)
173                 return -EINVAL;
174
175         /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
176         flags = args->flags & DRM_CLOEXEC;
177
178         return dev->driver->prime_handle_to_fd(dev, file_priv,
179                         args->handle, flags, &args->fd);
180 }
181
182 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
183                                  struct drm_file *file_priv)
184 {
185         struct drm_prime_handle *args = data;
186
187         if (!drm_core_check_feature(dev, DRIVER_PRIME))
188                 return -EINVAL;
189
190         if (!dev->driver->prime_fd_to_handle)
191                 return -ENOSYS;
192
193         return dev->driver->prime_fd_to_handle(dev, file_priv,
194                         args->fd, &args->handle);
195 }
196
197 /*
198  * drm_prime_pages_to_sg
199  *
200  * this helper creates an sg table object from a set of pages
201  * the driver is responsible for mapping the pages into the
202  * importers address space
203  */
204 struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
205 {
206         struct sg_table *sg = NULL;
207         struct scatterlist *iter;
208         int i;
209         int ret;
210
211         sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
212         if (!sg)
213                 goto out;
214
215         ret = sg_alloc_table(sg, nr_pages, GFP_KERNEL);
216         if (ret)
217                 goto out;
218
219         for_each_sg(sg->sgl, iter, nr_pages, i)
220                 sg_set_page(iter, pages[i], PAGE_SIZE, 0);
221
222         return sg;
223 out:
224         kfree(sg);
225         return NULL;
226 }
227 EXPORT_SYMBOL(drm_prime_pages_to_sg);
228
229 /* helper function to cleanup a GEM/prime object */
230 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
231 {
232         struct dma_buf_attachment *attach;
233         struct dma_buf *dma_buf;
234         attach = obj->import_attach;
235         if (sg)
236                 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
237         dma_buf = attach->dmabuf;
238         dma_buf_detach(attach->dmabuf, attach);
239         /* remove the reference */
240         dma_buf_put(dma_buf);
241 }
242 EXPORT_SYMBOL(drm_prime_gem_destroy);
243
244 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
245 {
246         INIT_LIST_HEAD(&prime_fpriv->head);
247         mutex_init(&prime_fpriv->lock);
248 }
249 EXPORT_SYMBOL(drm_prime_init_file_private);
250
251 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
252 {
253         struct drm_prime_member *member, *safe;
254         list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
255                 list_del(&member->entry);
256                 kfree(member);
257         }
258 }
259 EXPORT_SYMBOL(drm_prime_destroy_file_private);
260
261 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
262 {
263         struct drm_prime_member *member;
264
265         member = kmalloc(sizeof(*member), GFP_KERNEL);
266         if (!member)
267                 return -ENOMEM;
268
269         member->dma_buf = dma_buf;
270         member->handle = handle;
271         list_add(&member->entry, &prime_fpriv->head);
272         return 0;
273 }
274 EXPORT_SYMBOL(drm_prime_add_imported_buf_handle);
275
276 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
277 {
278         struct drm_prime_member *member;
279
280         list_for_each_entry(member, &prime_fpriv->head, entry) {
281                 if (member->dma_buf == dma_buf) {
282                         *handle = member->handle;
283                         return 0;
284                 }
285         }
286         return -ENOENT;
287 }
288 EXPORT_SYMBOL(drm_prime_lookup_imported_buf_handle);
289
290 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
291 {
292         struct drm_prime_member *member, *safe;
293
294         mutex_lock(&prime_fpriv->lock);
295         list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
296                 if (member->dma_buf == dma_buf) {
297                         list_del(&member->entry);
298                         kfree(member);
299                 }
300         }
301         mutex_unlock(&prime_fpriv->lock);
302 }
303 EXPORT_SYMBOL(drm_prime_remove_imported_buf_handle);