2 * Legacy: Generic DRM Contexts
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author: Gareth Hughes <gareth@valinux.com>
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:
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
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.
32 #include "drm_legacy.h"
35 struct list_head head;
40 /******************************************************************/
41 /** \name Context bitmap support */
45 * Free a handle from the context bitmap.
47 * \param dev DRM device.
48 * \param ctx_handle context handle.
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
54 void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
56 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
57 drm_core_check_feature(dev, DRIVER_MODESET))
60 mutex_lock(&dev->struct_mutex);
61 idr_remove(&dev->ctx_idr, ctx_handle);
62 mutex_unlock(&dev->struct_mutex);
66 * Context bitmap allocation.
68 * \param dev DRM device.
69 * \return (non-negative) context handle on success or a negative number on failure.
71 * Allocate a new idr from drm_device::ctx_idr while holding the
72 * drm_device::struct_mutex lock.
74 static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
78 mutex_lock(&dev->struct_mutex);
79 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
81 mutex_unlock(&dev->struct_mutex);
86 * Context bitmap initialization.
88 * \param dev DRM device.
90 * Initialise the drm_device::ctx_idr
92 int drm_legacy_ctxbitmap_init(struct drm_device * dev)
94 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
95 drm_core_check_feature(dev, DRIVER_MODESET))
98 idr_init(&dev->ctx_idr);
103 * Context bitmap cleanup.
105 * \param dev DRM device.
107 * Free all idr members using drm_ctx_sarea_free helper function
108 * while holding the drm_device::struct_mutex lock.
110 void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
112 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
113 drm_core_check_feature(dev, DRIVER_MODESET))
116 mutex_lock(&dev->struct_mutex);
117 idr_destroy(&dev->ctx_idr);
118 mutex_unlock(&dev->struct_mutex);
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
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.
130 void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
132 struct drm_ctx_list *pos, *tmp;
134 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
135 drm_core_check_feature(dev, DRIVER_MODESET))
138 mutex_lock(&dev->ctxlist_mutex);
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);
146 drm_legacy_ctxbitmap_free(dev, pos->handle);
147 list_del(&pos->head);
152 mutex_unlock(&dev->ctxlist_mutex);
157 /******************************************************************/
158 /** \name Per Context SAREA Support */
162 * Get per-context SAREA.
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.
170 * Gets the map from drm_device::ctx_idr with the handle specified and
171 * returns its handle.
173 int drm_legacy_getsareactx(struct drm_device *dev, void *data,
174 struct drm_file *file_priv)
176 struct drm_ctx_priv_map *request = data;
177 struct drm_local_map *map;
178 struct drm_map_list *_entry;
180 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
181 drm_core_check_feature(dev, DRIVER_MODESET))
184 mutex_lock(&dev->struct_mutex);
186 map = idr_find(&dev->ctx_idr, request->ctx_id);
188 mutex_unlock(&dev->struct_mutex);
192 request->handle = NULL;
193 list_for_each_entry(_entry, &dev->maplist, head) {
194 if (_entry->map == map) {
196 (void *)(unsigned long)_entry->user_token;
201 mutex_unlock(&dev->struct_mutex);
203 if (request->handle == NULL)
210 * Set per-context SAREA.
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.
218 * Searches the mapping specified in \p arg and update the entry in
219 * drm_device::ctx_idr with it.
221 int drm_legacy_setsareactx(struct drm_device *dev, void *data,
222 struct drm_file *file_priv)
224 struct drm_ctx_priv_map *request = data;
225 struct drm_local_map *map = NULL;
226 struct drm_map_list *r_list = NULL;
228 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
229 drm_core_check_feature(dev, DRIVER_MODESET))
232 mutex_lock(&dev->struct_mutex);
233 list_for_each_entry(r_list, &dev->maplist, head) {
235 && r_list->user_token == (unsigned long) request->handle)
239 mutex_unlock(&dev->struct_mutex);
247 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
250 mutex_unlock(&dev->struct_mutex);
257 /******************************************************************/
258 /** \name The actual DRM context handling routines */
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.
269 * Attempt to set drm_device::context_flag.
271 static int drm_context_switch(struct drm_device * dev, int old, int new)
273 if (test_and_set_bit(0, &dev->context_flag)) {
274 DRM_ERROR("Reentering -- FIXME\n");
278 DRM_DEBUG("Context switch from %d to %d\n", old, new);
280 if (new == dev->last_context) {
281 clear_bit(0, &dev->context_flag);
289 * Complete context switch.
291 * \param dev DRM device.
292 * \param new new context handle.
293 * \return zero on success or a negative number on failure.
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.
299 static int drm_context_switch_complete(struct drm_device *dev,
300 struct drm_file *file_priv, int new)
302 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
304 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
305 DRM_ERROR("Lock isn't held after context switch\n");
308 /* If a context switch is ever initiated
309 when the kernel holds the lock, release
311 clear_bit(0, &dev->context_flag);
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.
325 int drm_legacy_resctx(struct drm_device *dev, void *data,
326 struct drm_file *file_priv)
328 struct drm_ctx_res *res = data;
332 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
333 drm_core_check_feature(dev, DRIVER_MODESET))
336 if (res->count >= DRM_RESERVED_CONTEXTS) {
337 memset(&ctx, 0, sizeof(ctx));
338 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
340 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
344 res->count = DRM_RESERVED_CONTEXTS;
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.
358 * Get a new handle for the context and copy to userspace.
360 int drm_legacy_addctx(struct drm_device *dev, void *data,
361 struct drm_file *file_priv)
363 struct drm_ctx_list *ctx_entry;
364 struct drm_ctx *ctx = data;
366 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
367 drm_core_check_feature(dev, DRIVER_MODESET))
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);
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? */
382 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
384 DRM_DEBUG("out of memory\n");
388 INIT_LIST_HEAD(&ctx_entry->head);
389 ctx_entry->handle = ctx->handle;
390 ctx_entry->tag = file_priv;
392 mutex_lock(&dev->ctxlist_mutex);
393 list_add(&ctx_entry->head, &dev->ctxlist);
394 mutex_unlock(&dev->ctxlist_mutex);
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.
408 int drm_legacy_getctx(struct drm_device *dev, void *data,
409 struct drm_file *file_priv)
411 struct drm_ctx *ctx = data;
413 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
414 drm_core_check_feature(dev, DRIVER_MODESET))
417 /* This is 0, because we don't handle any context flags */
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.
432 * Calls context_switch().
434 int drm_legacy_switchctx(struct drm_device *dev, void *data,
435 struct drm_file *file_priv)
437 struct drm_ctx *ctx = data;
439 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
440 drm_core_check_feature(dev, DRIVER_MODESET))
443 DRM_DEBUG("%d\n", ctx->handle);
444 return drm_context_switch(dev, dev->last_context, ctx->handle);
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.
456 * Calls context_switch_complete().
458 int drm_legacy_newctx(struct drm_device *dev, void *data,
459 struct drm_file *file_priv)
461 struct drm_ctx *ctx = data;
463 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
464 drm_core_check_feature(dev, DRIVER_MODESET))
467 DRM_DEBUG("%d\n", ctx->handle);
468 drm_context_switch_complete(dev, file_priv, ctx->handle);
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.
482 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
484 int drm_legacy_rmctx(struct drm_device *dev, void *data,
485 struct drm_file *file_priv)
487 struct drm_ctx *ctx = data;
489 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
490 drm_core_check_feature(dev, DRIVER_MODESET))
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);
500 mutex_lock(&dev->ctxlist_mutex);
501 if (!list_empty(&dev->ctxlist)) {
502 struct drm_ctx_list *pos, *n;
504 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
505 if (pos->handle == ctx->handle) {
506 list_del(&pos->head);
511 mutex_unlock(&dev->ctxlist_mutex);