0cbf9cab24cabbd19b34ca837e27b04672fc6f84
[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/version.h>
29
30 #include "drmP.h"
31
32 #if OS_HAS_GEM
33
34 #include <linux/types.h>
35 #include <linux/slab.h>
36 #include <linux/mm.h>
37 #include <linux/uaccess.h>
38 #include <linux/fs.h>
39 #include <linux/file.h>
40 #include <linux/module.h>
41 #include <linux/mman.h>
42 #include <linux/pagemap.h>
43
44 /** @file drm_gem.c
45  *
46  * This file provides some of the base ioctls and library routines for
47  * the graphics memory manager implemented by each device driver.
48  *
49  * Because various devices have different requirements in terms of
50  * synchronization and migration strategies, implementing that is left up to
51  * the driver, and all that the general API provides should be generic --
52  * allocating objects, reading/writing data with the cpu, freeing objects.
53  * Even there, platform-dependent optimizations for reading/writing data with
54  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
55  * the DRI2 implementation wants to have at least allocate/mmap be generic.
56  *
57  * The goal was to have swap-backed object allocation managed through
58  * struct file.  However, file descriptors as handles to a struct file have
59  * two major failings:
60  * - Process limits prevent more than 1024 or so being used at a time by
61  *   default.
62  * - Inability to allocate high fds will aggravate the X Server's select()
63  *   handling, and likely that of many GL client applications as well.
64  *
65  * This led to a plan of using our own integer IDs (called handles, following
66  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
67  * ioctls.  The objects themselves will still include the struct file so
68  * that we can transition to fds if the required kernel infrastructure shows
69  * up at a later date, and as our interface with shmfs for memory allocation.
70  */
71
72 /**
73  * Initialize the GEM device fields
74  */
75
76 int
77 drm_gem_init(struct drm_device *dev)
78 {
79         spin_lock_init(&dev->object_name_lock);
80         idr_init(&dev->object_name_idr);
81         atomic_set(&dev->object_count, 0);
82         atomic_set(&dev->object_memory, 0);
83         atomic_set(&dev->pin_count, 0);
84         atomic_set(&dev->pin_memory, 0);
85         atomic_set(&dev->gtt_count, 0);
86         atomic_set(&dev->gtt_memory, 0);
87         return 0;
88 }
89
90 /**
91  * Allocate a GEM object of the specified size with shmfs backing store
92  */
93 struct drm_gem_object *
94 drm_gem_object_alloc(struct drm_device *dev, size_t size)
95 {
96         struct drm_gem_object *obj;
97
98         BUG_ON((size & (PAGE_SIZE - 1)) != 0);
99
100         obj = kcalloc(1, sizeof(*obj), GFP_KERNEL);
101
102         obj->dev = dev;
103         obj->filp = shmem_file_setup("drm mm object", size, 0);
104         if (IS_ERR(obj->filp)) {
105                 kfree(obj);
106                 return NULL;
107         }
108         kref_init(&obj->refcount);
109         kref_init(&obj->handlecount);
110         obj->size = size;
111         if (dev->driver->gem_init_object != NULL &&
112             dev->driver->gem_init_object(obj) != 0) {
113                 fput(obj->filp);
114                 kfree(obj);
115                 return NULL;
116         }
117         atomic_inc(&dev->object_count);
118         atomic_add(obj->size, &dev->object_memory);
119         return obj;
120 }
121 EXPORT_SYMBOL(drm_gem_object_alloc);
122
123 /**
124  * Removes the mapping from handle to filp for this object.
125  */
126 static int
127 drm_gem_handle_delete(struct drm_file *filp, int handle)
128 {
129         struct drm_device *dev;
130         struct drm_gem_object *obj;
131
132         /* This is gross. The idr system doesn't let us try a delete and
133          * return an error code.  It just spews if you fail at deleting.
134          * So, we have to grab a lock around finding the object and then
135          * doing the delete on it and dropping the refcount, or the user
136          * could race us to double-decrement the refcount and cause a
137          * use-after-free later.  Given the frequency of our handle lookups,
138          * we may want to use ida for number allocation and a hash table
139          * for the pointers, anyway.
140          */
141         spin_lock(&filp->table_lock);
142
143         /* Check if we currently have a reference on the object */
144         obj = idr_find(&filp->object_idr, handle);
145         if (obj == NULL) {
146                 spin_unlock(&filp->table_lock);
147                 return -EINVAL;
148         }
149         dev = obj->dev;
150
151         /* Release reference and decrement refcount. */
152         idr_remove(&filp->object_idr, handle);
153         spin_unlock(&filp->table_lock);
154
155         mutex_lock(&dev->struct_mutex);
156         drm_gem_object_handle_unreference(obj);
157         mutex_unlock(&dev->struct_mutex);
158
159         return 0;
160 }
161
162 /**
163  * Create a handle for this object. This adds a handle reference
164  * to the object, which includes a regular reference count. Callers
165  * will likely want to dereference the object afterwards.
166  */
167 int
168 drm_gem_handle_create(struct drm_file *file_priv,
169                        struct drm_gem_object *obj,
170                        int *handlep)
171 {
172         int     ret;
173
174         /*
175          * Get the user-visible handle using idr.
176          */
177 again:
178         /* ensure there is space available to allocate a handle */
179         if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0)
180                 return -ENOMEM;
181
182         /* do the allocation under our spinlock */
183         spin_lock(&file_priv->table_lock);
184         ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep);
185         spin_unlock(&file_priv->table_lock);
186         if (ret == -EAGAIN)
187                 goto again;
188
189         if (ret != 0)
190                 return ret;
191
192         drm_gem_object_handle_reference(obj);
193         return 0;
194 }
195 EXPORT_SYMBOL(drm_gem_handle_create);
196
197 /** Returns a reference to the object named by the handle. */
198 struct drm_gem_object *
199 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
200                       int handle)
201 {
202         struct drm_gem_object *obj;
203
204         spin_lock(&filp->table_lock);
205
206         /* Check if we currently have a reference on the object */
207         obj = idr_find(&filp->object_idr, handle);
208         if (obj == NULL) {
209                 spin_unlock(&filp->table_lock);
210                 return NULL;
211         }
212
213         drm_gem_object_reference(obj);
214
215         spin_unlock(&filp->table_lock);
216
217         return obj;
218 }
219 EXPORT_SYMBOL(drm_gem_object_lookup);
220
221 /**
222  * Releases the handle to an mm object.
223  */
224 int
225 drm_gem_close_ioctl(struct drm_device *dev, void *data,
226                     struct drm_file *file_priv)
227 {
228         struct drm_gem_close *args = data;
229         int ret;
230
231         if (!(dev->driver->driver_features & DRIVER_GEM))
232                 return -ENODEV;
233
234         ret = drm_gem_handle_delete(file_priv, args->handle);
235
236         return ret;
237 }
238
239 /**
240  * Create a global name for an object, returning the name.
241  *
242  * Note that the name does not hold a reference; when the object
243  * is freed, the name goes away.
244  */
245 int
246 drm_gem_flink_ioctl(struct drm_device *dev, void *data,
247                     struct drm_file *file_priv)
248 {
249         struct drm_gem_flink *args = data;
250         struct drm_gem_object *obj;
251         int ret;
252
253         if (!(dev->driver->driver_features & DRIVER_GEM))
254                 return -ENODEV;
255
256         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
257         if (obj == NULL)
258                 return -EINVAL;
259
260 again:
261         if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0)
262                 return -ENOMEM;
263
264         spin_lock(&dev->object_name_lock);
265         if (obj->name) {
266                 spin_unlock(&dev->object_name_lock);
267                 return -EEXIST;
268         }
269         ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
270                                  &obj->name);
271         spin_unlock(&dev->object_name_lock);
272         if (ret == -EAGAIN)
273                 goto again;
274
275         if (ret != 0) {
276                 mutex_lock(&dev->struct_mutex);
277                 drm_gem_object_unreference(obj);
278                 mutex_unlock(&dev->struct_mutex);
279                 return ret;
280         }
281
282         /*
283          * Leave the reference from the lookup around as the
284          * name table now holds one
285          */
286         args->name = (uint64_t) obj->name;
287
288         return 0;
289 }
290
291 /**
292  * Open an object using the global name, returning a handle and the size.
293  *
294  * This handle (of course) holds a reference to the object, so the object
295  * will not go away until the handle is deleted.
296  */
297 int
298 drm_gem_open_ioctl(struct drm_device *dev, void *data,
299                    struct drm_file *file_priv)
300 {
301         struct drm_gem_open *args = data;
302         struct drm_gem_object *obj;
303         int ret;
304         int handle;
305
306         if (!(dev->driver->driver_features & DRIVER_GEM))
307                 return -ENODEV;
308
309         spin_lock(&dev->object_name_lock);
310         obj = idr_find(&dev->object_name_idr, (int) args->name);
311         if (obj)
312                 drm_gem_object_reference(obj);
313         spin_unlock(&dev->object_name_lock);
314         if (!obj)
315                 return -ENOENT;
316
317         ret = drm_gem_handle_create(file_priv, obj, &handle);
318         mutex_lock(&dev->struct_mutex);
319         drm_gem_object_unreference(obj);
320         mutex_unlock(&dev->struct_mutex);
321         if (ret)
322                 return ret;
323
324         args->handle = handle;
325         args->size = obj->size;
326
327         return 0;
328 }
329
330 /**
331  * Called at device open time, sets up the structure for handling refcounting
332  * of mm objects.
333  */
334 void
335 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
336 {
337         idr_init(&file_private->object_idr);
338         spin_lock_init(&file_private->table_lock);
339 }
340
341 /**
342  * Called at device close to release the file's
343  * handle references on objects.
344  */
345 static int
346 drm_gem_object_release_handle(int id, void *ptr, void *data)
347 {
348         struct drm_gem_object *obj = ptr;
349
350         drm_gem_object_handle_unreference(obj);
351
352         return 0;
353 }
354
355 /**
356  * Called at close time when the filp is going away.
357  *
358  * Releases any remaining references on objects by this filp.
359  */
360 void
361 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
362 {
363         mutex_lock(&dev->struct_mutex);
364         idr_for_each(&file_private->object_idr,
365                      &drm_gem_object_release_handle, NULL);
366
367         idr_destroy(&file_private->object_idr);
368         mutex_unlock(&dev->struct_mutex);
369 }
370
371 /**
372  * Called after the last reference to the object has been lost.
373  *
374  * Frees the object
375  */
376 void
377 drm_gem_object_free(struct kref *kref)
378 {
379         struct drm_gem_object *obj = (struct drm_gem_object *) kref;
380         struct drm_device *dev = obj->dev;
381
382         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
383
384         if (dev->driver->gem_free_object != NULL)
385                 dev->driver->gem_free_object(obj);
386
387         fput(obj->filp);
388         atomic_dec(&dev->object_count);
389         atomic_sub(obj->size, &dev->object_memory);
390         kfree(obj);
391 }
392 EXPORT_SYMBOL(drm_gem_object_free);
393
394 /**
395  * Called after the last handle to the object has been closed
396  *
397  * Removes any name for the object. Note that this must be
398  * called before drm_gem_object_free or we'll be touching
399  * freed memory
400  */
401 void
402 drm_gem_object_handle_free(struct kref *kref)
403 {
404         struct drm_gem_object *obj = container_of(kref,
405                                                   struct drm_gem_object,
406                                                   handlecount);
407         struct drm_device *dev = obj->dev;
408
409         /* Remove any name for this object */
410         spin_lock(&dev->object_name_lock);
411         if (obj->name) {
412                 idr_remove(&dev->object_name_idr, obj->name);
413                 spin_unlock(&dev->object_name_lock);
414                 /*
415                  * The object name held a reference to this object, drop
416                  * that now.
417                  */
418                 drm_gem_object_unreference(obj);
419         } else
420                 spin_unlock(&dev->object_name_lock);
421
422 }
423 EXPORT_SYMBOL(drm_gem_object_handle_free);
424
425 #else
426
427 int drm_gem_init(struct drm_device *dev)
428 {
429         return 0;
430 }
431
432 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
433 {
434
435 }
436
437 void
438 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
439 {
440
441 }
442
443 #endif