Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[deliverable/linux.git] / drivers / gpu / drm / drm_context.c
1 /**
2 * \file drm_context.c
3 * IOCTLs for generic contexts
4 *
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
43 #include <drm/drmP.h>
44
45 /**
46 * Free a handle from the context bitmap.
47 *
48 * \param dev DRM device.
49 * \param ctx_handle context handle.
50 *
51 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
52 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
53 * lock.
54 */
55 static void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
56 {
57 if (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 /** \name Context bitmap support */
67 /*@{*/
68
69 void drm_legacy_ctxbitmap_release(struct drm_device *dev,
70 struct drm_file *file_priv)
71 {
72 if (drm_core_check_feature(dev, DRIVER_MODESET))
73 return;
74
75 mutex_lock(&dev->ctxlist_mutex);
76 if (!list_empty(&dev->ctxlist)) {
77 struct drm_ctx_list *pos, *n;
78
79 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
80 if (pos->tag == file_priv &&
81 pos->handle != DRM_KERNEL_CONTEXT) {
82 if (dev->driver->context_dtor)
83 dev->driver->context_dtor(dev,
84 pos->handle);
85
86 drm_ctxbitmap_free(dev, pos->handle);
87
88 list_del(&pos->head);
89 kfree(pos);
90 --dev->ctx_count;
91 }
92 }
93 }
94 mutex_unlock(&dev->ctxlist_mutex);
95 }
96
97 /**
98 * Context bitmap allocation.
99 *
100 * \param dev DRM device.
101 * \return (non-negative) context handle on success or a negative number on failure.
102 *
103 * Allocate a new idr from drm_device::ctx_idr while holding the
104 * drm_device::struct_mutex lock.
105 */
106 static int drm_ctxbitmap_next(struct drm_device * dev)
107 {
108 int ret;
109
110 mutex_lock(&dev->struct_mutex);
111 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
112 GFP_KERNEL);
113 mutex_unlock(&dev->struct_mutex);
114 return ret;
115 }
116
117 /**
118 * Context bitmap initialization.
119 *
120 * \param dev DRM device.
121 *
122 * Initialise the drm_device::ctx_idr
123 */
124 void drm_legacy_ctxbitmap_init(struct drm_device * dev)
125 {
126 if (drm_core_check_feature(dev, DRIVER_MODESET))
127 return;
128
129 idr_init(&dev->ctx_idr);
130 }
131
132 /**
133 * Context bitmap cleanup.
134 *
135 * \param dev DRM device.
136 *
137 * Free all idr members using drm_ctx_sarea_free helper function
138 * while holding the drm_device::struct_mutex lock.
139 */
140 void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
141 {
142 mutex_lock(&dev->struct_mutex);
143 idr_destroy(&dev->ctx_idr);
144 mutex_unlock(&dev->struct_mutex);
145 }
146
147 /*@}*/
148
149 /******************************************************************/
150 /** \name Per Context SAREA Support */
151 /*@{*/
152
153 /**
154 * Get per-context SAREA.
155 *
156 * \param inode device inode.
157 * \param file_priv DRM file private.
158 * \param cmd command.
159 * \param arg user argument pointing to a drm_ctx_priv_map structure.
160 * \return zero on success or a negative number on failure.
161 *
162 * Gets the map from drm_device::ctx_idr with the handle specified and
163 * returns its handle.
164 */
165 int drm_getsareactx(struct drm_device *dev, void *data,
166 struct drm_file *file_priv)
167 {
168 struct drm_ctx_priv_map *request = data;
169 struct drm_local_map *map;
170 struct drm_map_list *_entry;
171
172 if (drm_core_check_feature(dev, DRIVER_MODESET))
173 return -EINVAL;
174
175 mutex_lock(&dev->struct_mutex);
176
177 map = idr_find(&dev->ctx_idr, request->ctx_id);
178 if (!map) {
179 mutex_unlock(&dev->struct_mutex);
180 return -EINVAL;
181 }
182
183 request->handle = NULL;
184 list_for_each_entry(_entry, &dev->maplist, head) {
185 if (_entry->map == map) {
186 request->handle =
187 (void *)(unsigned long)_entry->user_token;
188 break;
189 }
190 }
191
192 mutex_unlock(&dev->struct_mutex);
193
194 if (request->handle == NULL)
195 return -EINVAL;
196
197 return 0;
198 }
199
200 /**
201 * Set per-context SAREA.
202 *
203 * \param inode device inode.
204 * \param file_priv DRM file private.
205 * \param cmd command.
206 * \param arg user argument pointing to a drm_ctx_priv_map structure.
207 * \return zero on success or a negative number on failure.
208 *
209 * Searches the mapping specified in \p arg and update the entry in
210 * drm_device::ctx_idr with it.
211 */
212 int drm_setsareactx(struct drm_device *dev, void *data,
213 struct drm_file *file_priv)
214 {
215 struct drm_ctx_priv_map *request = data;
216 struct drm_local_map *map = NULL;
217 struct drm_map_list *r_list = NULL;
218
219 if (drm_core_check_feature(dev, DRIVER_MODESET))
220 return -EINVAL;
221
222 mutex_lock(&dev->struct_mutex);
223 list_for_each_entry(r_list, &dev->maplist, head) {
224 if (r_list->map
225 && r_list->user_token == (unsigned long) request->handle)
226 goto found;
227 }
228 bad:
229 mutex_unlock(&dev->struct_mutex);
230 return -EINVAL;
231
232 found:
233 map = r_list->map;
234 if (!map)
235 goto bad;
236
237 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
238 goto bad;
239
240 mutex_unlock(&dev->struct_mutex);
241
242 return 0;
243 }
244
245 /*@}*/
246
247 /******************************************************************/
248 /** \name The actual DRM context handling routines */
249 /*@{*/
250
251 /**
252 * Switch context.
253 *
254 * \param dev DRM device.
255 * \param old old context handle.
256 * \param new new context handle.
257 * \return zero on success or a negative number on failure.
258 *
259 * Attempt to set drm_device::context_flag.
260 */
261 static int drm_context_switch(struct drm_device * dev, int old, int new)
262 {
263 if (test_and_set_bit(0, &dev->context_flag)) {
264 DRM_ERROR("Reentering -- FIXME\n");
265 return -EBUSY;
266 }
267
268 DRM_DEBUG("Context switch from %d to %d\n", old, new);
269
270 if (new == dev->last_context) {
271 clear_bit(0, &dev->context_flag);
272 return 0;
273 }
274
275 return 0;
276 }
277
278 /**
279 * Complete context switch.
280 *
281 * \param dev DRM device.
282 * \param new new context handle.
283 * \return zero on success or a negative number on failure.
284 *
285 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
286 * hardware lock is held, clears the drm_device::context_flag and wakes up
287 * drm_device::context_wait.
288 */
289 static int drm_context_switch_complete(struct drm_device *dev,
290 struct drm_file *file_priv, int new)
291 {
292 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
293
294 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
295 DRM_ERROR("Lock isn't held after context switch\n");
296 }
297
298 /* If a context switch is ever initiated
299 when the kernel holds the lock, release
300 that lock here. */
301 clear_bit(0, &dev->context_flag);
302
303 return 0;
304 }
305
306 /**
307 * Reserve contexts.
308 *
309 * \param inode device inode.
310 * \param file_priv DRM file private.
311 * \param cmd command.
312 * \param arg user argument pointing to a drm_ctx_res structure.
313 * \return zero on success or a negative number on failure.
314 */
315 int drm_resctx(struct drm_device *dev, void *data,
316 struct drm_file *file_priv)
317 {
318 struct drm_ctx_res *res = data;
319 struct drm_ctx ctx;
320 int i;
321
322 if (drm_core_check_feature(dev, DRIVER_MODESET))
323 return -EINVAL;
324
325 if (res->count >= DRM_RESERVED_CONTEXTS) {
326 memset(&ctx, 0, sizeof(ctx));
327 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
328 ctx.handle = i;
329 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
330 return -EFAULT;
331 }
332 }
333 res->count = DRM_RESERVED_CONTEXTS;
334
335 return 0;
336 }
337
338 /**
339 * Add context.
340 *
341 * \param inode device inode.
342 * \param file_priv DRM file private.
343 * \param cmd command.
344 * \param arg user argument pointing to a drm_ctx structure.
345 * \return zero on success or a negative number on failure.
346 *
347 * Get a new handle for the context and copy to userspace.
348 */
349 int drm_addctx(struct drm_device *dev, void *data,
350 struct drm_file *file_priv)
351 {
352 struct drm_ctx_list *ctx_entry;
353 struct drm_ctx *ctx = data;
354
355 if (drm_core_check_feature(dev, DRIVER_MODESET))
356 return -EINVAL;
357
358 ctx->handle = drm_ctxbitmap_next(dev);
359 if (ctx->handle == DRM_KERNEL_CONTEXT) {
360 /* Skip kernel's context and get a new one. */
361 ctx->handle = drm_ctxbitmap_next(dev);
362 }
363 DRM_DEBUG("%d\n", ctx->handle);
364 if (ctx->handle == -1) {
365 DRM_DEBUG("Not enough free contexts.\n");
366 /* Should this return -EBUSY instead? */
367 return -ENOMEM;
368 }
369
370 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
371 if (!ctx_entry) {
372 DRM_DEBUG("out of memory\n");
373 return -ENOMEM;
374 }
375
376 INIT_LIST_HEAD(&ctx_entry->head);
377 ctx_entry->handle = ctx->handle;
378 ctx_entry->tag = file_priv;
379
380 mutex_lock(&dev->ctxlist_mutex);
381 list_add(&ctx_entry->head, &dev->ctxlist);
382 ++dev->ctx_count;
383 mutex_unlock(&dev->ctxlist_mutex);
384
385 return 0;
386 }
387
388 /**
389 * Get context.
390 *
391 * \param inode device inode.
392 * \param file_priv DRM file private.
393 * \param cmd command.
394 * \param arg user argument pointing to a drm_ctx structure.
395 * \return zero on success or a negative number on failure.
396 */
397 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
398 {
399 struct drm_ctx *ctx = data;
400
401 if (drm_core_check_feature(dev, DRIVER_MODESET))
402 return -EINVAL;
403
404 /* This is 0, because we don't handle any context flags */
405 ctx->flags = 0;
406
407 return 0;
408 }
409
410 /**
411 * Switch context.
412 *
413 * \param inode device inode.
414 * \param file_priv DRM file private.
415 * \param cmd command.
416 * \param arg user argument pointing to a drm_ctx structure.
417 * \return zero on success or a negative number on failure.
418 *
419 * Calls context_switch().
420 */
421 int drm_switchctx(struct drm_device *dev, void *data,
422 struct drm_file *file_priv)
423 {
424 struct drm_ctx *ctx = data;
425
426 if (drm_core_check_feature(dev, DRIVER_MODESET))
427 return -EINVAL;
428
429 DRM_DEBUG("%d\n", ctx->handle);
430 return drm_context_switch(dev, dev->last_context, ctx->handle);
431 }
432
433 /**
434 * New context.
435 *
436 * \param inode device inode.
437 * \param file_priv DRM file private.
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 * Calls context_switch_complete().
443 */
444 int drm_newctx(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
446 {
447 struct drm_ctx *ctx = data;
448
449 if (drm_core_check_feature(dev, DRIVER_MODESET))
450 return -EINVAL;
451
452 DRM_DEBUG("%d\n", ctx->handle);
453 drm_context_switch_complete(dev, file_priv, ctx->handle);
454
455 return 0;
456 }
457
458 /**
459 * Remove context.
460 *
461 * \param inode device inode.
462 * \param file_priv DRM file private.
463 * \param cmd command.
464 * \param arg user argument pointing to a drm_ctx structure.
465 * \return zero on success or a negative number on failure.
466 *
467 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
468 */
469 int drm_rmctx(struct drm_device *dev, void *data,
470 struct drm_file *file_priv)
471 {
472 struct drm_ctx *ctx = data;
473
474 if (drm_core_check_feature(dev, DRIVER_MODESET))
475 return -EINVAL;
476
477 DRM_DEBUG("%d\n", ctx->handle);
478 if (ctx->handle != DRM_KERNEL_CONTEXT) {
479 if (dev->driver->context_dtor)
480 dev->driver->context_dtor(dev, ctx->handle);
481 drm_ctxbitmap_free(dev, ctx->handle);
482 }
483
484 mutex_lock(&dev->ctxlist_mutex);
485 if (!list_empty(&dev->ctxlist)) {
486 struct drm_ctx_list *pos, *n;
487
488 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
489 if (pos->handle == ctx->handle) {
490 list_del(&pos->head);
491 kfree(pos);
492 --dev->ctx_count;
493 }
494 }
495 }
496 mutex_unlock(&dev->ctxlist_mutex);
497
498 return 0;
499 }
500
501 /*@}*/
This page took 0.045187 seconds and 6 git commands to generate.