053203764484cc2954b1db421ce45f764daa75b4
[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 date, and as our interface with shmfs for memory allocation.
65  */
66
67 /**
68  * Initialize the GEM device fields
69  */
70
71 int
72 drm_gem_init(struct drm_device *dev)
73 {
74         spin_lock_init(&dev->object_name_lock);
75         idr_init(&dev->object_name_idr);
76         atomic_set(&dev->object_count, 0);
77         atomic_set(&dev->object_memory, 0);
78         atomic_set(&dev->pin_count, 0);
79         atomic_set(&dev->pin_memory, 0);
80         atomic_set(&dev->gtt_count, 0);
81         atomic_set(&dev->gtt_memory, 0);
82         return 0;
83 }
84
85 /**
86  * Allocate a GEM object of the specified size with shmfs backing store
87  */
88 struct drm_gem_object *
89 drm_gem_object_alloc(struct drm_device *dev, size_t size)
90 {
91         struct drm_gem_object *obj;
92
93         BUG_ON((size & (PAGE_SIZE - 1)) != 0);
94
95         obj = kcalloc(1, sizeof(*obj), GFP_KERNEL);
96
97         obj->dev = dev;
98         obj->filp = shmem_file_setup("drm mm object", size, 0);
99         if (IS_ERR(obj->filp)) {
100                 kfree(obj);
101                 return NULL;
102         }
103         kref_init(&obj->refcount);
104         kref_init(&obj->handlecount);
105         obj->size = size;
106         if (dev->driver->gem_init_object != NULL &&
107             dev->driver->gem_init_object(obj) != 0) {
108                 fput(obj->filp);
109                 kfree(obj);
110                 return NULL;
111         }
112         atomic_inc(&dev->object_count);
113         atomic_add(obj->size, &dev->object_memory);
114         return obj;
115 }
116 EXPORT_SYMBOL(drm_gem_object_alloc);
117
118 /**
119  * Removes the mapping from handle to filp for this object.
120  */
121 static int
122 drm_gem_handle_delete(struct drm_file *filp, int handle)
123 {
124         struct drm_device *dev;
125         struct drm_gem_object *obj;
126
127         /* This is gross. The idr system doesn't let us try a delete and
128          * return an error code.  It just spews if you fail at deleting.
129          * So, we have to grab a lock around finding the object and then
130          * doing the delete on it and dropping the refcount, or the user
131          * could race us to double-decrement the refcount and cause a
132          * use-after-free later.  Given the frequency of our handle lookups,
133          * we may want to use ida for number allocation and a hash table
134          * for the pointers, anyway.
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 -EINVAL;
143         }
144         dev = obj->dev;
145
146         /* Release reference and decrement refcount. */
147         idr_remove(&filp->object_idr, handle);
148         spin_unlock(&filp->table_lock);
149
150         mutex_lock(&dev->struct_mutex);
151         drm_gem_object_handle_unreference(obj);
152         mutex_unlock(&dev->struct_mutex);
153
154         return 0;
155 }
156
157 /**
158  * Create a handle for this object. This adds a handle reference
159  * to the object, which includes a regular reference count. Callers
160  * will likely want to dereference the object afterwards.
161  */
162 int
163 drm_gem_handle_create(struct drm_file *file_priv,
164                        struct drm_gem_object *obj,
165                        int *handlep)
166 {
167         int     ret;
168
169         /*
170          * Get the user-visible handle using idr.
171          */
172 again:
173         /* ensure there is space available to allocate a handle */
174         if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
175                 return -ENOMEM;
176
177         /* do the allocation under our spinlock */
178         spin_lock(&file_priv->table_lock);
179         ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep);
180         spin_unlock(&file_priv->table_lock);
181         if (ret == -EAGAIN)
182                 goto again;
183
184         if (ret != 0)
185                 return ret;
186
187         drm_gem_object_handle_reference(obj);
188         return 0;
189 }
190 EXPORT_SYMBOL(drm_gem_handle_create);
191
192 /** Returns a reference to the object named by the handle. */
193 struct drm_gem_object *
194 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
195                       int handle)
196 {
197         struct drm_gem_object *obj;
198
199         spin_lock(&filp->table_lock);
200
201         /* Check if we currently have a reference on the object */
202         obj = idr_find(&filp->object_idr, handle);
203         if (obj == NULL) {
204                 spin_unlock(&filp->table_lock);
205                 return NULL;
206         }
207
208         drm_gem_object_reference(obj);
209
210         spin_unlock(&filp->table_lock);
211
212         return obj;
213 }
214 EXPORT_SYMBOL(drm_gem_object_lookup);
215
216 /**
217  * Releases the handle to an mm object.
218  */
219 int
220 drm_gem_close_ioctl(struct drm_device *dev, void *data,
221                     struct drm_file *file_priv)
222 {
223         struct drm_gem_close *args = data;
224         int ret;
225
226         if (!(dev->driver->driver_features & DRIVER_GEM))
227                 return -ENODEV;
228
229         ret = drm_gem_handle_delete(file_priv, args->handle);
230
231         return ret;
232 }
233
234 /**
235  * Create a global name for an object, returning the name.
236  *
237  * Note that the name does not hold a reference; when the object
238  * is freed, the name goes away.
239  */
240 int
241 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
242                     struct drm_file *file_priv)
243 {
244         struct drm_gem_flink *args = data;
245         struct drm_gem_object *obj;
246         int ret;
247
248         if (!(dev->driver->driver_features & DRIVER_GEM))
249                 return -ENODEV;
250
251         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
252         if (obj == NULL)
253                 return -EINVAL;
254
255 again:
256         if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0)
257                 return -ENOMEM;
258
259         spin_lock(&dev->object_name_lock);
260         if (obj->name) {
261                 spin_unlock(&dev->object_name_lock);
262                 return -EEXIST;
263         }
264         ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
265                                  &obj->name);
266         spin_unlock(&dev->object_name_lock);
267         if (ret == -EAGAIN)
268                 goto again;
269
270         if (ret != 0) {
271                 mutex_lock(&dev->struct_mutex);
272                 drm_gem_object_unreference(obj);
273                 mutex_unlock(&dev->struct_mutex);
274                 return ret;
275         }
276
277         /*
278          * Leave the reference from the lookup around as the
279          * name table now holds one
280          */
281         args->name = (uint64_t) obj->name;
282
283         return 0;
284 }
285
286 /**
287  * Open an object using the global name, returning a handle and the size.
288  *
289  * This handle (of course) holds a reference to the object, so the object
290  * will not go away until the handle is deleted.
291  */
292 int
293 drm_gem_open_ioctl(struct drm_device *dev, void *data,
294                    struct drm_file *file_priv)
295 {
296         struct drm_gem_open *args = data;
297         struct drm_gem_object *obj;
298         int ret;
299         int handle;
300
301         if (!(dev->driver->driver_features & DRIVER_GEM))
302                 return -ENODEV;
303
304         spin_lock(&dev->object_name_lock);
305         obj = idr_find(&dev->object_name_idr, (int) args->name);
306         if (obj)
307                 drm_gem_object_reference(obj);
308         spin_unlock(&dev->object_name_lock);
309         if (!obj)
310                 return -ENOENT;
311
312         ret = drm_gem_handle_create(file_priv, obj, &handle);
313         mutex_lock(&dev->struct_mutex);
314         drm_gem_object_unreference(obj);
315         mutex_unlock(&dev->struct_mutex);
316         if (ret)
317                 return ret;
318
319         args->handle = handle;
320         args->size = obj->size;
321
322         return 0;
323 }
324
325 /**
326  * Called at device open time, sets up the structure for handling refcounting
327  * of mm objects.
328  */
329 void
330 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
331 {
332         idr_init(&file_private->object_idr);
333         spin_lock_init(&file_private->table_lock);
334 }
335
336 /**
337  * Called at device close to release the file's
338  * handle references on objects.
339  */
340 static int
341 drm_gem_object_release_handle(int id, void *ptr, void *data)
342 {
343         struct drm_gem_object *obj = ptr;
344
345         drm_gem_object_handle_unreference(obj);
346
347         return 0;
348 }
349
350 /**
351  * Called at close time when the filp is going away.
352  *
353  * Releases any remaining references on objects by this filp.
354  */
355 void
356 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
357 {
358         mutex_lock(&dev->struct_mutex);
359         idr_for_each(&file_private->object_idr,
360                      &drm_gem_object_release_handle, NULL);
361
362         idr_destroy(&file_private->object_idr);
363         mutex_unlock(&dev->struct_mutex);
364 }
365
366 /**
367  * Called after the last reference to the object has been lost.
368  *
369  * Frees the object
370  */
371 void
372 drm_gem_object_free(struct kref *kref)
373 {
374         struct drm_gem_object *obj = (struct drm_gem_object *) kref;
375         struct drm_device *dev = obj->dev;
376
377         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
378
379         if (dev->driver->gem_free_object != NULL)
380                 dev->driver->gem_free_object(obj);
381
382         fput(obj->filp);
383         atomic_dec(&dev->object_count);
384         atomic_sub(obj->size, &dev->object_memory);
385         kfree(obj);
386 }
387 EXPORT_SYMBOL(drm_gem_object_free);
388
389 /**
390  * Called after the last handle to the object has been closed
391  *
392  * Removes any name for the object. Note that this must be
393  * called before drm_gem_object_free or we'll be touching
394  * freed memory
395  */
396 void
397 drm_gem_object_handle_free(struct kref *kref)
398 {
399         struct drm_gem_object *obj = container_of(kref,
400                                                   struct drm_gem_object,
401                                                   handlecount);
402         struct drm_device *dev = obj->dev;
403
404         /* Remove any name for this object */
405         spin_lock(&dev->object_name_lock);
406         if (obj->name) {
407                 idr_remove(&dev->object_name_idr, obj->name);
408                 spin_unlock(&dev->object_name_lock);
409                 /*
410                  * The object name held a reference to this object, drop
411                  * that now.
412                  */
413                 drm_gem_object_unreference(obj);
414         } else
415                 spin_unlock(&dev->object_name_lock);
416
417 }
418 EXPORT_SYMBOL(drm_gem_object_handle_free);
419