Remove _args from gem ioctl argument structure tags.
[platform/upstream/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(&file_priv->object_idr, obj, &handle);
186         } while (ret == -EAGAIN);
187
188         if (ret != 0) {
189                 drm_gem_object_unreference(dev, obj);
190                 return -EFAULT;
191         }
192
193         args->handle = handle;
194
195         return 0;
196 }
197
198 /**
199  * Releases the handle to an mm object.
200  */
201 int
202 drm_gem_unreference_ioctl(struct drm_device *dev, void *data,
203                           struct drm_file *file_priv)
204 {
205         struct drm_gem_unreference *args = data;
206         int ret;
207
208         ret = drm_gem_handle_delete(dev, file_priv, args->handle);
209
210         return ret;
211 }
212
213 /**
214  * Reads data from the object referenced by handle.
215  *
216  * On error, the contents of *data are undefined.
217  */
218 int
219 drm_gem_pread_ioctl(struct drm_device *dev, void *data,
220                     struct drm_file *file_priv)
221 {
222         struct drm_gem_pread *args = data;
223         struct drm_gem_object *obj;
224         ssize_t read;
225         loff_t offset;
226
227         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
228         if (obj == NULL)
229                 return -EINVAL;
230
231         offset = args->offset;
232
233         read = obj->filp->f_op->read(obj->filp, (char __user *)args->data,
234                                      args->size, &offset);
235         if (read != args->size) {
236                 drm_gem_object_unreference(dev, obj);
237                 if (read < 0)
238                         return read;
239                 else
240                         return -EINVAL;
241         }
242
243         drm_gem_object_unreference(dev, obj);
244
245         return 0;
246 }
247
248 /**
249  * Maps the contents of an object, returning the address it is mapped
250  * into.
251  *
252  * While the mapping holds a reference on the contents of the object, it doesn't
253  * imply a ref on the object itself.
254  */
255 int
256 drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
257                    struct drm_file *file_priv)
258 {
259         struct drm_gem_mmap *args = data;
260         struct drm_gem_object *obj;
261         loff_t offset;
262
263         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
264         if (obj == NULL)
265                 return -EINVAL;
266
267         offset = args->offset;
268
269         down_write(&current->mm->mmap_sem);
270         args->addr = (void *)do_mmap(obj->filp, 0, args->size,
271                                     PROT_READ | PROT_WRITE, MAP_SHARED,
272                                     args->offset);
273         up_write(&current->mm->mmap_sem);
274
275         drm_gem_object_unreference(dev, obj);
276
277         return 0;
278 }
279
280 /**
281  * Writes data to the object referenced by handle.
282  *
283  * On error, the contents of the buffer that were to be modified are undefined.
284  */
285 int
286 drm_gem_pwrite_ioctl(struct drm_device *dev, void *data,
287                      struct drm_file *file_priv)
288 {
289         struct drm_gem_pwrite *args = data;
290         struct drm_gem_object *obj;
291         ssize_t written;
292         loff_t offset;
293
294         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
295         if (obj == NULL)
296                 return -EINVAL;
297
298         offset = args->offset;
299
300         written = obj->filp->f_op->write(obj->filp, (char __user *)args->data,
301                                          args->size, &offset);
302         if (written != args->size) {
303                 drm_gem_object_unreference(dev, obj);
304                 if (written < 0)
305                         return written;
306                 else
307                         return -EINVAL;
308         }
309
310         drm_gem_object_unreference(dev, obj);
311
312         return 0;
313 }
314
315 /**
316  * Called at device open time, sets up the structure for handling refcounting
317  * of mm objects.
318  */
319 void
320 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
321 {
322         idr_init(&file_private->object_idr);
323 }
324
325 /** Called at device close to release the file's references on objects. */
326 static int
327 drm_gem_object_release(int id, void *ptr, void *data)
328 {
329         struct drm_device *dev = data;
330         struct drm_gem_object *obj = ptr;
331
332         drm_gem_object_unreference(dev, obj);
333
334         return 0;
335 }
336
337 /**
338  * Called at close time when the filp is going away.
339  *
340  * Releases any remaining references on objects by this filp.
341  */
342 void
343 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
344 {
345         idr_for_each(&file_private->object_idr, &drm_gem_object_release, dev);
346
347         idr_destroy(&file_private->object_idr);
348 }
349
350 void
351 drm_gem_object_reference(struct drm_device *dev, struct drm_gem_object *obj)
352 {
353         spin_lock(&obj->lock);
354         obj->refcount++;
355         spin_unlock(&obj->lock);
356 }
357 EXPORT_SYMBOL(drm_gem_object_reference);
358
359 void
360 drm_gem_object_unreference(struct drm_device *dev, struct drm_gem_object *obj)
361 {
362         if (obj == NULL)
363                 return;
364
365         spin_lock(&obj->lock);
366         obj->refcount--;
367         spin_unlock(&obj->lock);
368         if (obj->refcount == 0) {
369                 if (dev->driver->gem_free_object != NULL)
370                         dev->driver->gem_free_object(dev, obj);
371
372                 fput(obj->filp);
373                 kfree(obj);
374         }
375 }
376 EXPORT_SYMBOL(drm_gem_object_unreference);