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