OSDN Git Service

drm: Turn off Legacy Context Functions
[android-x86/kernel.git] / drivers / gpu / drm / drm_context.c
1 /*
2  * Legacy: Generic DRM Contexts
3  *
4  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
9  * Author: Gareth Hughes <gareth@valinux.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next
19  * paragraph) shall be included in all copies or substantial portions of the
20  * Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  */
30
31 #include <drm/drmP.h>
32 #include "drm_legacy.h"
33
34 struct drm_ctx_list {
35         struct list_head head;
36         drm_context_t handle;
37         struct drm_file *tag;
38 };
39
40 /******************************************************************/
41 /** \name Context bitmap support */
42 /*@{*/
43
44 /**
45  * Free a handle from the context bitmap.
46  *
47  * \param dev DRM device.
48  * \param ctx_handle context handle.
49  *
50  * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
51  * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
52  * lock.
53  */
54 void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
55 {
56         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
57             drm_core_check_feature(dev, DRIVER_MODESET))
58                 return;
59
60         mutex_lock(&dev->struct_mutex);
61         idr_remove(&dev->ctx_idr, ctx_handle);
62         mutex_unlock(&dev->struct_mutex);
63 }
64
65 /**
66  * Context bitmap allocation.
67  *
68  * \param dev DRM device.
69  * \return (non-negative) context handle on success or a negative number on failure.
70  *
71  * Allocate a new idr from drm_device::ctx_idr while holding the
72  * drm_device::struct_mutex lock.
73  */
74 static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
75 {
76         int ret;
77
78         mutex_lock(&dev->struct_mutex);
79         ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
80                         GFP_KERNEL);
81         mutex_unlock(&dev->struct_mutex);
82         return ret;
83 }
84
85 /**
86  * Context bitmap initialization.
87  *
88  * \param dev DRM device.
89  *
90  * Initialise the drm_device::ctx_idr
91  */
92 int drm_legacy_ctxbitmap_init(struct drm_device * dev)
93 {
94         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
95             drm_core_check_feature(dev, DRIVER_MODESET))
96                 return -EINVAL;
97
98         idr_init(&dev->ctx_idr);
99         return 0;
100 }
101
102 /**
103  * Context bitmap cleanup.
104  *
105  * \param dev DRM device.
106  *
107  * Free all idr members using drm_ctx_sarea_free helper function
108  * while holding the drm_device::struct_mutex lock.
109  */
110 void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
111 {
112         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
113             drm_core_check_feature(dev, DRIVER_MODESET))
114                 return;
115
116         mutex_lock(&dev->struct_mutex);
117         idr_destroy(&dev->ctx_idr);
118         mutex_unlock(&dev->struct_mutex);
119 }
120
121 /**
122  * drm_ctxbitmap_flush() - Flush all contexts owned by a file
123  * @dev: DRM device to operate on
124  * @file: Open file to flush contexts for
125  *
126  * This iterates over all contexts on @dev and drops them if they're owned by
127  * @file. Note that after this call returns, new contexts might be added if
128  * the file is still alive.
129  */
130 void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
131 {
132         struct drm_ctx_list *pos, *tmp;
133
134         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
135             drm_core_check_feature(dev, DRIVER_MODESET))
136                 return;
137
138         mutex_lock(&dev->ctxlist_mutex);
139
140         list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
141                 if (pos->tag == file &&
142                     pos->handle != DRM_KERNEL_CONTEXT) {
143                         if (dev->driver->context_dtor)
144                                 dev->driver->context_dtor(dev, pos->handle);
145
146                         drm_legacy_ctxbitmap_free(dev, pos->handle);
147                         list_del(&pos->head);
148                         kfree(pos);
149                 }
150         }
151
152         mutex_unlock(&dev->ctxlist_mutex);
153 }
154
155 /*@}*/
156
157 /******************************************************************/
158 /** \name Per Context SAREA Support */
159 /*@{*/
160
161 /**
162  * Get per-context SAREA.
163  *
164  * \param inode device inode.
165  * \param file_priv DRM file private.
166  * \param cmd command.
167  * \param arg user argument pointing to a drm_ctx_priv_map structure.
168  * \return zero on success or a negative number on failure.
169  *
170  * Gets the map from drm_device::ctx_idr with the handle specified and
171  * returns its handle.
172  */
173 int drm_legacy_getsareactx(struct drm_device *dev, void *data,
174                            struct drm_file *file_priv)
175 {
176         struct drm_ctx_priv_map *request = data;
177         struct drm_local_map *map;
178         struct drm_map_list *_entry;
179
180         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
181             drm_core_check_feature(dev, DRIVER_MODESET))
182                 return -EINVAL;
183
184         mutex_lock(&dev->struct_mutex);
185
186         map = idr_find(&dev->ctx_idr, request->ctx_id);
187         if (!map) {
188                 mutex_unlock(&dev->struct_mutex);
189                 return -EINVAL;
190         }
191
192         request->handle = NULL;
193         list_for_each_entry(_entry, &dev->maplist, head) {
194                 if (_entry->map == map) {
195                         request->handle =
196                             (void *)(unsigned long)_entry->user_token;
197                         break;
198                 }
199         }
200
201         mutex_unlock(&dev->struct_mutex);
202
203         if (request->handle == NULL)
204                 return -EINVAL;
205
206         return 0;
207 }
208
209 /**
210  * Set per-context SAREA.
211  *
212  * \param inode device inode.
213  * \param file_priv DRM file private.
214  * \param cmd command.
215  * \param arg user argument pointing to a drm_ctx_priv_map structure.
216  * \return zero on success or a negative number on failure.
217  *
218  * Searches the mapping specified in \p arg and update the entry in
219  * drm_device::ctx_idr with it.
220  */
221 int drm_legacy_setsareactx(struct drm_device *dev, void *data,
222                            struct drm_file *file_priv)
223 {
224         struct drm_ctx_priv_map *request = data;
225         struct drm_local_map *map = NULL;
226         struct drm_map_list *r_list = NULL;
227
228         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
229             drm_core_check_feature(dev, DRIVER_MODESET))
230                 return -EINVAL;
231
232         mutex_lock(&dev->struct_mutex);
233         list_for_each_entry(r_list, &dev->maplist, head) {
234                 if (r_list->map
235                     && r_list->user_token == (unsigned long) request->handle)
236                         goto found;
237         }
238       bad:
239         mutex_unlock(&dev->struct_mutex);
240         return -EINVAL;
241
242       found:
243         map = r_list->map;
244         if (!map)
245                 goto bad;
246
247         if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
248                 goto bad;
249
250         mutex_unlock(&dev->struct_mutex);
251
252         return 0;
253 }
254
255 /*@}*/
256
257 /******************************************************************/
258 /** \name The actual DRM context handling routines */
259 /*@{*/
260
261 /**
262  * Switch context.
263  *
264  * \param dev DRM device.
265  * \param old old context handle.
266  * \param new new context handle.
267  * \return zero on success or a negative number on failure.
268  *
269  * Attempt to set drm_device::context_flag.
270  */
271 static int drm_context_switch(struct drm_device * dev, int old, int new)
272 {
273         if (test_and_set_bit(0, &dev->context_flag)) {
274                 DRM_ERROR("Reentering -- FIXME\n");
275                 return -EBUSY;
276         }
277
278         DRM_DEBUG("Context switch from %d to %d\n", old, new);
279
280         if (new == dev->last_context) {
281                 clear_bit(0, &dev->context_flag);
282                 return 0;
283         }
284
285         return 0;
286 }
287
288 /**
289  * Complete context switch.
290  *
291  * \param dev DRM device.
292  * \param new new context handle.
293  * \return zero on success or a negative number on failure.
294  *
295  * Updates drm_device::last_context and drm_device::last_switch. Verifies the
296  * hardware lock is held, clears the drm_device::context_flag and wakes up
297  * drm_device::context_wait.
298  */
299 static int drm_context_switch_complete(struct drm_device *dev,
300                                        struct drm_file *file_priv, int new)
301 {
302         dev->last_context = new;        /* PRE/POST: This is the _only_ writer. */
303
304         if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
305                 DRM_ERROR("Lock isn't held after context switch\n");
306         }
307
308         /* If a context switch is ever initiated
309            when the kernel holds the lock, release
310            that lock here. */
311         clear_bit(0, &dev->context_flag);
312
313         return 0;
314 }
315
316 /**
317  * Reserve contexts.
318  *
319  * \param inode device inode.
320  * \param file_priv DRM file private.
321  * \param cmd command.
322  * \param arg user argument pointing to a drm_ctx_res structure.
323  * \return zero on success or a negative number on failure.
324  */
325 int drm_legacy_resctx(struct drm_device *dev, void *data,
326                       struct drm_file *file_priv)
327 {
328         struct drm_ctx_res *res = data;
329         struct drm_ctx ctx;
330         int i;
331
332         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
333             drm_core_check_feature(dev, DRIVER_MODESET))
334                 return -EINVAL;
335
336         if (res->count >= DRM_RESERVED_CONTEXTS) {
337                 memset(&ctx, 0, sizeof(ctx));
338                 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
339                         ctx.handle = i;
340                         if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
341                                 return -EFAULT;
342                 }
343         }
344         res->count = DRM_RESERVED_CONTEXTS;
345
346         return 0;
347 }
348
349 /**
350  * Add context.
351  *
352  * \param inode device inode.
353  * \param file_priv DRM file private.
354  * \param cmd command.
355  * \param arg user argument pointing to a drm_ctx structure.
356  * \return zero on success or a negative number on failure.
357  *
358  * Get a new handle for the context and copy to userspace.
359  */
360 int drm_legacy_addctx(struct drm_device *dev, void *data,
361                       struct drm_file *file_priv)
362 {
363         struct drm_ctx_list *ctx_entry;
364         struct drm_ctx *ctx = data;
365
366         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
367             drm_core_check_feature(dev, DRIVER_MODESET))
368                 return -EINVAL;
369
370         ctx->handle = drm_legacy_ctxbitmap_next(dev);
371         if (ctx->handle == DRM_KERNEL_CONTEXT) {
372                 /* Skip kernel's context and get a new one. */
373                 ctx->handle = drm_legacy_ctxbitmap_next(dev);
374         }
375         DRM_DEBUG("%d\n", ctx->handle);
376         if (ctx->handle == -1) {
377                 DRM_DEBUG("Not enough free contexts.\n");
378                 /* Should this return -EBUSY instead? */
379                 return -ENOMEM;
380         }
381
382         ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
383         if (!ctx_entry) {
384                 DRM_DEBUG("out of memory\n");
385                 return -ENOMEM;
386         }
387
388         INIT_LIST_HEAD(&ctx_entry->head);
389         ctx_entry->handle = ctx->handle;
390         ctx_entry->tag = file_priv;
391
392         mutex_lock(&dev->ctxlist_mutex);
393         list_add(&ctx_entry->head, &dev->ctxlist);
394         mutex_unlock(&dev->ctxlist_mutex);
395
396         return 0;
397 }
398
399 /**
400  * Get context.
401  *
402  * \param inode device inode.
403  * \param file_priv DRM file private.
404  * \param cmd command.
405  * \param arg user argument pointing to a drm_ctx structure.
406  * \return zero on success or a negative number on failure.
407  */
408 int drm_legacy_getctx(struct drm_device *dev, void *data,
409                       struct drm_file *file_priv)
410 {
411         struct drm_ctx *ctx = data;
412
413         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
414             drm_core_check_feature(dev, DRIVER_MODESET))
415                 return -EINVAL;
416
417         /* This is 0, because we don't handle any context flags */
418         ctx->flags = 0;
419
420         return 0;
421 }
422
423 /**
424  * Switch context.
425  *
426  * \param inode device inode.
427  * \param file_priv DRM file private.
428  * \param cmd command.
429  * \param arg user argument pointing to a drm_ctx structure.
430  * \return zero on success or a negative number on failure.
431  *
432  * Calls context_switch().
433  */
434 int drm_legacy_switchctx(struct drm_device *dev, void *data,
435                          struct drm_file *file_priv)
436 {
437         struct drm_ctx *ctx = data;
438
439         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
440             drm_core_check_feature(dev, DRIVER_MODESET))
441                 return -EINVAL;
442
443         DRM_DEBUG("%d\n", ctx->handle);
444         return drm_context_switch(dev, dev->last_context, ctx->handle);
445 }
446
447 /**
448  * New context.
449  *
450  * \param inode device inode.
451  * \param file_priv DRM file private.
452  * \param cmd command.
453  * \param arg user argument pointing to a drm_ctx structure.
454  * \return zero on success or a negative number on failure.
455  *
456  * Calls context_switch_complete().
457  */
458 int drm_legacy_newctx(struct drm_device *dev, void *data,
459                       struct drm_file *file_priv)
460 {
461         struct drm_ctx *ctx = data;
462
463         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
464             drm_core_check_feature(dev, DRIVER_MODESET))
465                 return -EINVAL;
466
467         DRM_DEBUG("%d\n", ctx->handle);
468         drm_context_switch_complete(dev, file_priv, ctx->handle);
469
470         return 0;
471 }
472
473 /**
474  * Remove context.
475  *
476  * \param inode device inode.
477  * \param file_priv DRM file private.
478  * \param cmd command.
479  * \param arg user argument pointing to a drm_ctx structure.
480  * \return zero on success or a negative number on failure.
481  *
482  * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
483  */
484 int drm_legacy_rmctx(struct drm_device *dev, void *data,
485                      struct drm_file *file_priv)
486 {
487         struct drm_ctx *ctx = data;
488
489         if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
490             drm_core_check_feature(dev, DRIVER_MODESET))
491                 return -EINVAL;
492
493         DRM_DEBUG("%d\n", ctx->handle);
494         if (ctx->handle != DRM_KERNEL_CONTEXT) {
495                 if (dev->driver->context_dtor)
496                         dev->driver->context_dtor(dev, ctx->handle);
497                 drm_legacy_ctxbitmap_free(dev, ctx->handle);
498         }
499
500         mutex_lock(&dev->ctxlist_mutex);
501         if (!list_empty(&dev->ctxlist)) {
502                 struct drm_ctx_list *pos, *n;
503
504                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
505                         if (pos->handle == ctx->handle) {
506                                 list_del(&pos->head);
507                                 kfree(pos);
508                         }
509                 }
510         }
511         mutex_unlock(&dev->ctxlist_mutex);
512
513         return 0;
514 }
515
516 /*@}*/