drm: Turn off Legacy Context Functions
[linux-2.6-block.git] / drivers / gpu / drm / drm_context.c
CommitLineData
1da177e4 1/*
e7b96070 2 * Legacy: Generic DRM Contexts
1da177e4
LT
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 *
e7b96070
DH
8 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author: Gareth Hughes <gareth@valinux.com>
10 *
1da177e4
LT
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
760285e7 31#include <drm/drmP.h>
e7b96070
DH
32#include "drm_legacy.h"
33
34struct drm_ctx_list {
35 struct list_head head;
36 drm_context_t handle;
37 struct drm_file *tag;
38};
1da177e4 39
c21eb21c
DA
40/******************************************************************/
41/** \name Context bitmap support */
42/*@{*/
43
1da177e4
LT
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
62968144 51 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
1da177e4
LT
52 * lock.
53 */
e7b96070 54void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
1da177e4 55{
0e975980
PA
56 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
57 drm_core_check_feature(dev, DRIVER_MODESET))
58 return;
59
62968144
DA
60 mutex_lock(&dev->struct_mutex);
61 idr_remove(&dev->ctx_idr, ctx_handle);
62 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
63}
64
b5e89ed5 65/**
1da177e4
LT
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 *
62968144 71 * Allocate a new idr from drm_device::ctx_idr while holding the
30e2fb18 72 * drm_device::struct_mutex lock.
1da177e4 73 */
e7b96070 74static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
1da177e4 75{
62968144 76 int ret;
1da177e4 77
30e2fb18 78 mutex_lock(&dev->struct_mutex);
2e928815
TH
79 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
80 GFP_KERNEL);
30e2fb18 81 mutex_unlock(&dev->struct_mutex);
2e928815 82 return ret;
1da177e4
LT
83}
84
85/**
86 * Context bitmap initialization.
87 *
88 * \param dev DRM device.
89 *
62968144 90 * Initialise the drm_device::ctx_idr
1da177e4 91 */
e7b96070 92int drm_legacy_ctxbitmap_init(struct drm_device * dev)
1da177e4 93{
0e975980
PA
94 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
95 drm_core_check_feature(dev, DRIVER_MODESET))
96 return -EINVAL;
97
62968144 98 idr_init(&dev->ctx_idr);
c21eb21c 99 return 0;
1da177e4
LT
100}
101
102/**
103 * Context bitmap cleanup.
104 *
105 * \param dev DRM device.
106 *
62968144
DA
107 * Free all idr members using drm_ctx_sarea_free helper function
108 * while holding the drm_device::struct_mutex lock.
1da177e4 109 */
e7b96070 110void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
1da177e4 111{
0e975980
PA
112 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
113 drm_core_check_feature(dev, DRIVER_MODESET))
114 return;
115
30e2fb18 116 mutex_lock(&dev->struct_mutex);
4d53233a 117 idr_destroy(&dev->ctx_idr);
30e2fb18 118 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
119}
120
9f8d21ea
DH
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 */
e7b96070 130void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
9f8d21ea
DH
131{
132 struct drm_ctx_list *pos, *tmp;
133
0e975980
PA
134 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
135 drm_core_check_feature(dev, DRIVER_MODESET))
136 return;
137
9f8d21ea
DH
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
e7b96070 146 drm_legacy_ctxbitmap_free(dev, pos->handle);
9f8d21ea
DH
147 list_del(&pos->head);
148 kfree(pos);
149 }
150 }
151
152 mutex_unlock(&dev->ctxlist_mutex);
153}
154
1da177e4
LT
155/*@}*/
156
157/******************************************************************/
158/** \name Per Context SAREA Support */
159/*@{*/
160
161/**
162 * Get per-context SAREA.
b5e89ed5 163 *
1da177e4 164 * \param inode device inode.
6c340eac 165 * \param file_priv DRM file private.
1da177e4
LT
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 *
62968144 170 * Gets the map from drm_device::ctx_idr with the handle specified and
1da177e4
LT
171 * returns its handle.
172 */
e7b96070
DH
173int drm_legacy_getsareactx(struct drm_device *dev, void *data,
174 struct drm_file *file_priv)
1da177e4 175{
c153f45f 176 struct drm_ctx_priv_map *request = data;
f77d390c 177 struct drm_local_map *map;
55910517 178 struct drm_map_list *_entry;
1da177e4 179
0e975980
PA
180 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
181 drm_core_check_feature(dev, DRIVER_MODESET))
182 return -EINVAL;
183
30e2fb18 184 mutex_lock(&dev->struct_mutex);
62968144 185
c153f45f 186 map = idr_find(&dev->ctx_idr, request->ctx_id);
62968144 187 if (!map) {
30e2fb18 188 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
189 return -EINVAL;
190 }
191
c153f45f 192 request->handle = NULL;
bd1b331f 193 list_for_each_entry(_entry, &dev->maplist, head) {
d1f2b55a 194 if (_entry->map == map) {
bc5f4523 195 request->handle =
b5e89ed5 196 (void *)(unsigned long)_entry->user_token;
d1f2b55a
DA
197 break;
198 }
199 }
09b4ea47
IH
200
201 mutex_unlock(&dev->struct_mutex);
202
c153f45f 203 if (request->handle == NULL)
d1f2b55a
DA
204 return -EINVAL;
205
1da177e4
LT
206 return 0;
207}
208
209/**
210 * Set per-context SAREA.
b5e89ed5 211 *
1da177e4 212 * \param inode device inode.
6c340eac 213 * \param file_priv DRM file private.
1da177e4
LT
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
62968144 219 * drm_device::ctx_idr with it.
1da177e4 220 */
e7b96070
DH
221int drm_legacy_setsareactx(struct drm_device *dev, void *data,
222 struct drm_file *file_priv)
1da177e4 223{
c153f45f 224 struct drm_ctx_priv_map *request = data;
f77d390c 225 struct drm_local_map *map = NULL;
55910517 226 struct drm_map_list *r_list = NULL;
1da177e4 227
0e975980
PA
228 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
229 drm_core_check_feature(dev, DRIVER_MODESET))
230 return -EINVAL;
231
30e2fb18 232 mutex_lock(&dev->struct_mutex);
bd1b331f 233 list_for_each_entry(r_list, &dev->maplist, head) {
9a186645 234 if (r_list->map
c153f45f 235 && r_list->user_token == (unsigned long) request->handle)
1da177e4
LT
236 goto found;
237 }
b5e89ed5 238 bad:
30e2fb18 239 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
240 return -EINVAL;
241
b5e89ed5 242 found:
1da177e4 243 map = r_list->map;
b5e89ed5
DA
244 if (!map)
245 goto bad;
62968144 246
c153f45f 247 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
1da177e4 248 goto bad;
62968144 249
30e2fb18 250 mutex_unlock(&dev->struct_mutex);
c153f45f 251
1da177e4
LT
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 */
84b1fd10 271static int drm_context_switch(struct drm_device * dev, int old, int new)
1da177e4 272{
b5e89ed5
DA
273 if (test_and_set_bit(0, &dev->context_flag)) {
274 DRM_ERROR("Reentering -- FIXME\n");
275 return -EBUSY;
276 }
1da177e4 277
b5e89ed5 278 DRM_DEBUG("Context switch from %d to %d\n", old, new);
1da177e4 279
b5e89ed5
DA
280 if (new == dev->last_context) {
281 clear_bit(0, &dev->context_flag);
282 return 0;
283 }
1da177e4 284
b5e89ed5 285 return 0;
1da177e4
LT
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 */
7c1c2871
DA
299static int drm_context_switch_complete(struct drm_device *dev,
300 struct drm_file *file_priv, int new)
1da177e4 301{
b5e89ed5 302 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
1da177e4 303
7c1c2871 304 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
b5e89ed5
DA
305 DRM_ERROR("Lock isn't held after context switch\n");
306 }
1da177e4 307
b5e89ed5
DA
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);
1da177e4 312
b5e89ed5 313 return 0;
1da177e4
LT
314}
315
316/**
317 * Reserve contexts.
318 *
319 * \param inode device inode.
6c340eac 320 * \param file_priv DRM file private.
1da177e4
LT
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 */
e7b96070
DH
325int drm_legacy_resctx(struct drm_device *dev, void *data,
326 struct drm_file *file_priv)
1da177e4 327{
c153f45f 328 struct drm_ctx_res *res = data;
c60ce623 329 struct drm_ctx ctx;
1da177e4
LT
330 int i;
331
0e975980
PA
332 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
333 drm_core_check_feature(dev, DRIVER_MODESET))
334 return -EINVAL;
335
c153f45f 336 if (res->count >= DRM_RESERVED_CONTEXTS) {
b5e89ed5
DA
337 memset(&ctx, 0, sizeof(ctx));
338 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
1da177e4 339 ctx.handle = i;
c153f45f 340 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
1da177e4
LT
341 return -EFAULT;
342 }
343 }
c153f45f 344 res->count = DRM_RESERVED_CONTEXTS;
1da177e4 345
1da177e4
LT
346 return 0;
347}
348
349/**
350 * Add context.
351 *
352 * \param inode device inode.
6c340eac 353 * \param file_priv DRM file private.
1da177e4
LT
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 */
e7b96070
DH
360int drm_legacy_addctx(struct drm_device *dev, void *data,
361 struct drm_file *file_priv)
1da177e4 362{
55910517 363 struct drm_ctx_list *ctx_entry;
c153f45f 364 struct drm_ctx *ctx = data;
1da177e4 365
0e975980
PA
366 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
367 drm_core_check_feature(dev, DRIVER_MODESET))
368 return -EINVAL;
369
e7b96070 370 ctx->handle = drm_legacy_ctxbitmap_next(dev);
c153f45f 371 if (ctx->handle == DRM_KERNEL_CONTEXT) {
b5e89ed5 372 /* Skip kernel's context and get a new one. */
e7b96070 373 ctx->handle = drm_legacy_ctxbitmap_next(dev);
1da177e4 374 }
c153f45f
EA
375 DRM_DEBUG("%d\n", ctx->handle);
376 if (ctx->handle == -1) {
b5e89ed5
DA
377 DRM_DEBUG("Not enough free contexts.\n");
378 /* Should this return -EBUSY instead? */
1da177e4
LT
379 return -ENOMEM;
380 }
381
9a298b2a 382 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
b5e89ed5 383 if (!ctx_entry) {
1da177e4
LT
384 DRM_DEBUG("out of memory\n");
385 return -ENOMEM;
386 }
387
b5e89ed5 388 INIT_LIST_HEAD(&ctx_entry->head);
c153f45f 389 ctx_entry->handle = ctx->handle;
6c340eac 390 ctx_entry->tag = file_priv;
1da177e4 391
30e2fb18 392 mutex_lock(&dev->ctxlist_mutex);
bd1b331f 393 list_add(&ctx_entry->head, &dev->ctxlist);
30e2fb18 394 mutex_unlock(&dev->ctxlist_mutex);
1da177e4 395
1da177e4
LT
396 return 0;
397}
398
1da177e4
LT
399/**
400 * Get context.
401 *
402 * \param inode device inode.
6c340eac 403 * \param file_priv DRM file private.
1da177e4
LT
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 */
e7b96070
DH
408int drm_legacy_getctx(struct drm_device *dev, void *data,
409 struct drm_file *file_priv)
1da177e4 410{
c153f45f 411 struct drm_ctx *ctx = data;
1da177e4 412
0e975980
PA
413 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
414 drm_core_check_feature(dev, DRIVER_MODESET))
415 return -EINVAL;
416
1da177e4 417 /* This is 0, because we don't handle any context flags */
c153f45f 418 ctx->flags = 0;
1da177e4 419
1da177e4
LT
420 return 0;
421}
422
423/**
424 * Switch context.
425 *
426 * \param inode device inode.
6c340eac 427 * \param file_priv DRM file private.
1da177e4
LT
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 */
e7b96070
DH
434int drm_legacy_switchctx(struct drm_device *dev, void *data,
435 struct drm_file *file_priv)
1da177e4 436{
c153f45f 437 struct drm_ctx *ctx = data;
1da177e4 438
0e975980
PA
439 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
440 drm_core_check_feature(dev, DRIVER_MODESET))
441 return -EINVAL;
442
c153f45f
EA
443 DRM_DEBUG("%d\n", ctx->handle);
444 return drm_context_switch(dev, dev->last_context, ctx->handle);
1da177e4
LT
445}
446
447/**
448 * New context.
449 *
450 * \param inode device inode.
6c340eac 451 * \param file_priv DRM file private.
1da177e4
LT
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 */
e7b96070
DH
458int drm_legacy_newctx(struct drm_device *dev, void *data,
459 struct drm_file *file_priv)
1da177e4 460{
c153f45f 461 struct drm_ctx *ctx = data;
1da177e4 462
0e975980
PA
463 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
464 drm_core_check_feature(dev, DRIVER_MODESET))
465 return -EINVAL;
466
c153f45f 467 DRM_DEBUG("%d\n", ctx->handle);
7c1c2871 468 drm_context_switch_complete(dev, file_priv, ctx->handle);
1da177e4
LT
469
470 return 0;
471}
472
473/**
474 * Remove context.
475 *
476 * \param inode device inode.
6c340eac 477 * \param file_priv DRM file private.
1da177e4
LT
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 */
e7b96070
DH
484int drm_legacy_rmctx(struct drm_device *dev, void *data,
485 struct drm_file *file_priv)
1da177e4 486{
c153f45f 487 struct drm_ctx *ctx = data;
1da177e4 488
0e975980
PA
489 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
490 drm_core_check_feature(dev, DRIVER_MODESET))
491 return -EINVAL;
492
c153f45f 493 DRM_DEBUG("%d\n", ctx->handle);
c153f45f 494 if (ctx->handle != DRM_KERNEL_CONTEXT) {
1da177e4 495 if (dev->driver->context_dtor)
c153f45f 496 dev->driver->context_dtor(dev, ctx->handle);
e7b96070 497 drm_legacy_ctxbitmap_free(dev, ctx->handle);
1da177e4
LT
498 }
499
30e2fb18 500 mutex_lock(&dev->ctxlist_mutex);
bd1b331f 501 if (!list_empty(&dev->ctxlist)) {
55910517 502 struct drm_ctx_list *pos, *n;
1da177e4 503
bd1b331f 504 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
c153f45f 505 if (pos->handle == ctx->handle) {
b5e89ed5 506 list_del(&pos->head);
9a298b2a 507 kfree(pos);
1da177e4
LT
508 }
509 }
510 }
30e2fb18 511 mutex_unlock(&dev->ctxlist_mutex);
1da177e4
LT
512
513 return 0;
514}
515
516/*@}*/