OSDN Git Service

radeon: fix fence race condition hopefully
[android-x86/external-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                 args->name = (uint64_t) obj->name;
267                 spin_unlock(&dev->object_name_lock);
268                 return 0;
269         }
270         ret = idr_get_new_above(&dev->object_name_idr, obj, 1,
271                                  &obj->name);
272         spin_unlock(&dev->object_name_lock);
273         if (ret == -EAGAIN)
274                 goto again;
275
276         if (ret != 0) {
277                 mutex_lock(&dev->struct_mutex);
278                 drm_gem_object_unreference(obj);
279                 mutex_unlock(&dev->struct_mutex);
280                 return ret;
281         }
282
283         /*
284          * Leave the reference from the lookup around as the
285          * name table now holds one
286          */
287         args->name = (uint64_t) obj->name;
288
289         return 0;
290 }
291
292 /**
293  * Open an object using the global name, returning a handle and the size.
294  *
295  * This handle (of course) holds a reference to the object, so the object
296  * will not go away until the handle is deleted.
297  */
298 int
299 drm_gem_open_ioctl(struct drm_device *dev, void *data,
300                    struct drm_file *file_priv)
301 {
302         struct drm_gem_open *args = data;
303         struct drm_gem_object *obj;
304         int ret;
305         int handle;
306
307         if (!(dev->driver->driver_features & DRIVER_GEM))
308                 return -ENODEV;
309
310         spin_lock(&dev->object_name_lock);
311         obj = idr_find(&dev->object_name_idr, (int) args->name);
312         if (obj)
313                 drm_gem_object_reference(obj);
314         spin_unlock(&dev->object_name_lock);
315         if (!obj)
316                 return -ENOENT;
317
318         ret = drm_gem_handle_create(file_priv, obj, &handle);
319         mutex_lock(&dev->struct_mutex);
320         drm_gem_object_unreference(obj);
321         mutex_unlock(&dev->struct_mutex);
322         if (ret)
323                 return ret;
324
325         args->handle = handle;
326         args->size = obj->size;
327
328         return 0;
329 }
330
331 /**
332  * Called at device open time, sets up the structure for handling refcounting
333  * of mm objects.
334  */
335 void
336 drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
337 {
338         idr_init(&file_private->object_idr);
339         spin_lock_init(&file_private->table_lock);
340 }
341
342 /**
343  * Called at device close to release the file's
344  * handle references on objects.
345  */
346 static int
347 drm_gem_object_release_handle(int id, void *ptr, void *data)
348 {
349         struct drm_gem_object *obj = ptr;
350
351         drm_gem_object_handle_unreference(obj);
352
353         return 0;
354 }
355
356 /**
357  * Called at close time when the filp is going away.
358  *
359  * Releases any remaining references on objects by this filp.
360  */
361 void
362 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
363 {
364         mutex_lock(&dev->struct_mutex);
365         idr_for_each(&file_private->object_idr,
366                      &drm_gem_object_release_handle, NULL);
367
368         idr_destroy(&file_private->object_idr);
369         mutex_unlock(&dev->struct_mutex);
370 }
371
372 /**
373  * Called after the last reference to the object has been lost.
374  *
375  * Frees the object
376  */
377 void
378 drm_gem_object_free(struct kref *kref)
379 {
380         struct drm_gem_object *obj = (struct drm_gem_object *) kref;
381         struct drm_device *dev = obj->dev;
382
383         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
384
385         if (dev->driver->gem_free_object != NULL)
386                 dev->driver->gem_free_object(obj);
387
388         fput(obj->filp);
389         atomic_dec(&dev->object_count);
390         atomic_sub(obj->size, &dev->object_memory);
391         kfree(obj);
392 }
393 EXPORT_SYMBOL(drm_gem_object_free);
394
395 /**
396  * Called after the last handle to the object has been closed
397  *
398  * Removes any name for the object. Note that this must be
399  * called before drm_gem_object_free or we'll be touching
400  * freed memory
401  */
402 void
403 drm_gem_object_handle_free(struct kref *kref)
404 {
405         struct drm_gem_object *obj = container_of(kref,
406                                                   struct drm_gem_object,
407                                                   handlecount);
408         struct drm_device *dev = obj->dev;
409
410         /* Remove any name for this object */
411         spin_lock(&dev->object_name_lock);
412         if (obj->name) {
413                 idr_remove(&dev->object_name_idr, obj->name);
414                 spin_unlock(&dev->object_name_lock);
415                 /*
416                  * The object name held a reference to this object, drop
417                  * that now.
418                  */
419                 drm_gem_object_unreference(obj);
420         } else
421                 spin_unlock(&dev->object_name_lock);
422
423 }
424 EXPORT_SYMBOL(drm_gem_object_handle_free);
425
426 #else
427
428 int drm_gem_init(struct drm_device *dev)
429 {
430         return 0;
431 }
432
433 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
434 {
435
436 }
437
438 void
439 drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
440 {
441
442 }
443
444 #endif