f6038d5c21a4529b6f3727134201b50e3cbd10b8
[profile/ivi/libdrm.git] / linux-core / drm_gem.c
1 /*
2  * Copyright © 2008 Intel Corporation
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  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31 #include <linux/uaccess.h>
32 #include <linux/fs.h>
33 #include <linux/file.h>
34 #include <linux/module.h>
35 #include <linux/mman.h>
36 #include <linux/pagemap.h>
37 #include "drmP.h"
38
39 /** @file drm_gem.c
40  *
41  * This file provides some of the base ioctls and library routines for
42  * the graphics memory manager implemented by each device driver.
43  *
44  * Because various devices have different requirements in terms of
45  * synchronization and migration strategies, implementing that is left up to
46  * the driver, and all that the general API provides should be generic --
47  * allocating objects, reading/writing data with the cpu, freeing objects.
48  * Even there, platform-dependent optimizations for reading/writing data with
49  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
50  * the DRI2 implementation wants to have at least allocate/mmap be generic.
51  *
52  * The goal was to have swap-backed object allocation managed through
53  * struct file.  However, file descriptors as handles to a struct file have
54  * two major failings:
55  * - Process limits prevent more than 1024 or so being used at a time by
56  *   default.
57  * - Inability to allocate high fds will aggravate the X Server's select()
58  *   handling, and likely that of many GL client applications as well.
59  *
60  * This led to a plan of using our own integer IDs (called handles, following
61  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
62  * ioctls.  The objects themselves will still include the struct file so
63  * that we can transition to fds if the required kernel infrastructure shows
64  * up at a later data, and as our interface with shmfs for memory allocation.
65  */
66
67 static struct drm_gem_object *
68 drm_gem_object_alloc(struct drm_device *dev, size_t size)
69 {
70         struct drm_gem_object *obj;
71
72         BUG_ON((size & (PAGE_SIZE - 1)) != 0);
73
74         obj = kcalloc(1, sizeof(*obj), GFP_KERNEL);
75
76         obj->filp = shmem_file_setup("drm mm object", size, 0);
77         if (IS_ERR(obj->filp)) {
78                 kfree(obj);
79                 return NULL;
80         }
81
82         obj->refcount = 1;
83
84         if (dev->driver->gem_init_object != NULL &&
85             dev->driver->gem_init_object(dev, obj) != 0) {
86                 fput(obj->filp);
87                 kfree(obj);
88                 return NULL;
89         }
90         return obj;
91 }
92
93 /**
94  * Removes the mapping from handle to filp for this object.
95  */
96 static int
97 drm_gem_handle_delete(struct drm_device *dev, struct drm_file *filp,
98                       int handle)
99 {
100         struct drm_gem_object *obj;
101
102         /* This is gross. The idr system doesn't let us try a delete and
103          * return an error code.  It just spews if you fail at deleting.
104          * So, we have to grab a lock around finding the object and then
105          * doing the delete on it and dropping the refcount, or the user
106          * could race us to double-decrement the refcount and cause a
107          * use-after-free later.  Given the frequency of our handle lookups,
108          * we may want to use ida for number allocation and a hash table
109          * for the pointers, anyway.
110          */
111         spin_lock(&filp->table_lock);
112
113         /* Check if we currently have a reference on the object */
114         obj = idr_find(&filp->object_idr, handle);
115         if (obj == NULL) {
116                 spin_unlock(&filp->table_lock);
117                 return -EINVAL;
118         }
119
120         /* Release reference and decrement refcount. */
121         idr_remove(&filp->object_idr, handle);
122         drm_gem_object_unreference(dev, obj);
123
124         spin_unlock(&filp->table_lock);
125
126         return 0;
127 }
128
129 /** Returns a reference to the object named by the handle. */
130 struct drm_gem_object *
131 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
132                       int handle)
133 {
134         struct drm_gem_object *obj;
135
136         spin_lock(&filp->table_lock);
137
138         /* Check if we currently have a reference on the object */
139         obj = idr_find(&filp->object_idr, handle);
140         if (obj == NULL) {
141                 spin_unlock(&filp->table_lock);
142                 return NULL;
143         }
144
145         drm_gem_object_reference(dev, obj);
146
147         spin_unlock(&filp->table_lock);
148
149         return obj;
150 }
151 EXPORT_SYMBOL(drm_gem_object_lookup);
152
153 /**
154  * Allocates a new mm object and returns a handle to it.
155  */
156 int
157 drm_gem_alloc_ioctl(struct drm_device *dev, void *data,
158                     struct drm_file *file_priv)
159 {
160         struct drm_gem_alloc *args = data;
161         struct drm_gem_object *obj;
162         int handle, ret;
163
164         /* Round requested size up to page size */
165         args->size = (args->size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
166
167         /* Allocate the new object */
168         obj = drm_gem_object_alloc(dev, args->size);
169         if (obj == NULL)
170                 return -ENOMEM;
171
172         /* Get the user-visible handle using idr.
173          *
174          * I'm not really sure why the idr api needs us to do this in two
175          * repeating steps.  It handles internal locking of its data
176          * structure, yet insists that we keep its memory allocation step
177          * separate from its slot-finding step for locking purposes.
178          */
179         do {
180                 if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) {
181                         kfree(obj);
182                         return -EFAULT;
183                 }
184
185                 ret = idr_get_new_above(&file_priv->object_idr, obj, 1,
186                                         &handle);
187         } while (ret == -EAGAIN);
188
189         if (ret != 0) {
190                 drm_gem_object_unreference(dev, obj);
191                 return -EFAULT;
192         }
193
194         args->handle = handle;
195
196         return 0;
197 }
198
199 /**
200  * Releases the handle to an mm object.
201  */
202 int
203 drm_gem_unreference_ioctl(struct drm_device *dev, void *data,
204                           struct drm_file *file_priv)
205 {
206         struct drm_gem_unreference *args = data;
207         int ret;
208
209         ret = drm_gem_handle_delete(dev, file_priv, args->handle);
210
211         return ret;
212 }
213
214 /**
215  * Reads data from the object referenced by handle.
216  *
217  * On error, the contents of *data are undefined.
218  */
219 int
220 drm_gem_pread_ioctl(struct drm_device *dev, void *data,
221                     struct drm_file *file_priv)
222 {
223         struct drm_gem_pread *args = data;
224         struct drm_gem_object *obj;
225         ssize_t read;
226         loff_t offset;
227
228         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
229         if (obj == NULL)
230                 return -EINVAL;
231
232         offset = args->offset;
233
234         read = obj->filp->f_op->read(obj->filp, (char __user *)args->data,
235                                      args->size, &offset);
236         if (read != args->size) {
237                 drm_gem_object_unreference(dev, obj);
238                 if (read < 0)
239                         return read;
240                 else
241                         return -EINVAL;
242         }
243
244         drm_gem_object_unreference(dev, obj);
245
246         return 0;
247 }
248
249 /**
250  * Maps the contents of an object, returning the address it is mapped
251  * into.
252  *
253  * While the mapping holds a reference on the contents of the object, it doesn't
254  * imply a ref on the object itself.
255  */
256 int
257 drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
258                    struct drm_file *file_priv)
259 {
260         struct drm_gem_mmap *args = data;
261         struct drm_gem_object *obj;
262         loff_t offset;
263
264         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
265         if (obj == NULL)
266                 return -EINVAL;
267
268         offset = args->offset;
269
270         down_write(&current->mm->mmap_sem);
271         args->addr = (void *)do_mmap(obj->filp, 0, args->size,
272                                     PROT_READ | PROT_WRITE, MAP_SHARED,
273                                     args->offset);
274         up_write(&current->mm->mmap_sem);
275
276         drm_gem_object_unreference(dev, obj);
277
278         return 0;
279 }
280
281 /**
282  * Writes data to the object referenced by handle.
283  *
284  * On error, the contents of the buffer that were to be modified are undefined.
285  */
286 int
287 drm_gem_pwrite_ioctl(struct drm_device *dev, void *data,
288                      struct drm_file *file_priv)
289 {
290         struct drm_gem_pwrite *args = data;
291         struct drm_gem_object *obj;
292         ssize_t written;
293         loff_t offset;
294
295         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
296         if (obj == NULL)
297                 return -EINVAL;
298
299         offset = args->offset;
300
301         written = obj->filp->f_op->write(obj->filp, (char __user *)args->data,
302                                          args->size, &offset);
303         if (written != args->size) {
304                 drm_gem_object_unreference(dev, obj);
305                 if (written < 0)
306                         return written;
307                 else
308                         return -EINVAL;
309         }
310
311         drm_gem_object_unreference(dev, obj);
312
313         return 0;
314 }
315
316 /**
317  * Called at device open time, sets up the structure for handling refcounting
318  * of mm objects.
319  */
320 void
321 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
322 {
323         idr_init(&file_private->object_idr);
324 }
325
326 /** Called at device close to release the file's references on objects. */
327 static int
328 drm_gem_object_release(int id, void *ptr, void *data)
329 {
330         struct drm_device *dev = data;
331         struct drm_gem_object *obj = ptr;
332
333         drm_gem_object_unreference(dev, obj);
334
335         return 0;
336 }
337
338 /**
339  * Called at close time when the filp is going away.
340  *
341  * Releases any remaining references on objects by this filp.
342  */
343 void
344 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
345 {
346         idr_for_each(&file_private->object_idr, &drm_gem_object_release, dev);
347
348         idr_destroy(&file_private->object_idr);
349 }
350
351 void
352 drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj)
353 {
354         spin_lock(&obj->lock);
355         obj->refcount++;
356         spin_unlock(&obj->lock);
357 }
358 EXPORT_SYMBOL(drm_gem_object_reference);
359
360 void
361 drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj)
362 {
363         if (obj == NULL)
364                 return;
365
366         spin_lock(&obj->lock);
367         obj->refcount--;
368         spin_unlock(&obj->lock);
369         if (obj->refcount == 0) {
370                 if (dev->driver->gem_free_object != NULL)
371                         dev->driver->gem_free_object(dev, obj);
372
373                 fput(obj->filp);
374                 kfree(obj);
375         }
376 }
377 EXPORT_SYMBOL(drm_gem_object_unreference);