3 * IOCTLs for generic contexts
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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
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.
38 * 2001-11-16 Torsten Duwe <duwe@caldera.de>
39 * added context constructor/destructor hooks,
40 * needed by SiS driver's memory management.
46 * Free a handle from the context bitmap.
48 * \param dev DRM device.
49 * \param ctx_handle context handle.
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
55 static void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
57 if (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);
65 /******************************************************************/
66 /** \name Context bitmap support */
69 void drm_legacy_ctxbitmap_release(struct drm_device *dev,
70 struct drm_file *file_priv)
72 if (drm_core_check_feature(dev, DRIVER_MODESET))
75 mutex_lock(&dev->ctxlist_mutex);
76 if (!list_empty(&dev->ctxlist)) {
77 struct drm_ctx_list *pos, *n;
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,
86 drm_ctxbitmap_free(dev, pos->handle);
94 mutex_unlock(&dev->ctxlist_mutex);
98 * Context bitmap allocation.
100 * \param dev DRM device.
101 * \return (non-negative) context handle on success or a negative number on failure.
103 * Allocate a new idr from drm_device::ctx_idr while holding the
104 * drm_device::struct_mutex lock.
106 static int drm_ctxbitmap_next(struct drm_device * dev)
110 mutex_lock(&dev->struct_mutex);
111 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
113 mutex_unlock(&dev->struct_mutex);
118 * Context bitmap initialization.
120 * \param dev DRM device.
122 * Initialise the drm_device::ctx_idr
124 void drm_legacy_ctxbitmap_init(struct drm_device * dev)
126 if (drm_core_check_feature(dev, DRIVER_MODESET))
129 idr_init(&dev->ctx_idr);
133 * Context bitmap cleanup.
135 * \param dev DRM device.
137 * Free all idr members using drm_ctx_sarea_free helper function
138 * while holding the drm_device::struct_mutex lock.
140 void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
142 mutex_lock(&dev->struct_mutex);
143 idr_destroy(&dev->ctx_idr);
144 mutex_unlock(&dev->struct_mutex);
149 /******************************************************************/
150 /** \name Per Context SAREA Support */
154 * Get per-context SAREA.
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.
162 * Gets the map from drm_device::ctx_idr with the handle specified and
163 * returns its handle.
165 int drm_getsareactx(struct drm_device *dev, void *data,
166 struct drm_file *file_priv)
168 struct drm_ctx_priv_map *request = data;
169 struct drm_local_map *map;
170 struct drm_map_list *_entry;
172 if (drm_core_check_feature(dev, DRIVER_MODESET))
175 mutex_lock(&dev->struct_mutex);
177 map = idr_find(&dev->ctx_idr, request->ctx_id);
179 mutex_unlock(&dev->struct_mutex);
183 request->handle = NULL;
184 list_for_each_entry(_entry, &dev->maplist, head) {
185 if (_entry->map == map) {
187 (void *)(unsigned long)_entry->user_token;
192 mutex_unlock(&dev->struct_mutex);
194 if (request->handle == NULL)
201 * Set per-context SAREA.
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.
209 * Searches the mapping specified in \p arg and update the entry in
210 * drm_device::ctx_idr with it.
212 int drm_setsareactx(struct drm_device *dev, void *data,
213 struct drm_file *file_priv)
215 struct drm_ctx_priv_map *request = data;
216 struct drm_local_map *map = NULL;
217 struct drm_map_list *r_list = NULL;
219 if (drm_core_check_feature(dev, DRIVER_MODESET))
222 mutex_lock(&dev->struct_mutex);
223 list_for_each_entry(r_list, &dev->maplist, head) {
225 && r_list->user_token == (unsigned long) request->handle)
229 mutex_unlock(&dev->struct_mutex);
237 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
240 mutex_unlock(&dev->struct_mutex);
247 /******************************************************************/
248 /** \name The actual DRM context handling routines */
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.
259 * Attempt to set drm_device::context_flag.
261 static int drm_context_switch(struct drm_device * dev, int old, int new)
263 if (test_and_set_bit(0, &dev->context_flag)) {
264 DRM_ERROR("Reentering -- FIXME\n");
268 DRM_DEBUG("Context switch from %d to %d\n", old, new);
270 if (new == dev->last_context) {
271 clear_bit(0, &dev->context_flag);
279 * Complete context switch.
281 * \param dev DRM device.
282 * \param new new context handle.
283 * \return zero on success or a negative number on failure.
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.
289 static int drm_context_switch_complete(struct drm_device *dev,
290 struct drm_file *file_priv, int new)
292 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
294 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
295 DRM_ERROR("Lock isn't held after context switch\n");
298 /* If a context switch is ever initiated
299 when the kernel holds the lock, release
301 clear_bit(0, &dev->context_flag);
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.
315 int drm_resctx(struct drm_device *dev, void *data,
316 struct drm_file *file_priv)
318 struct drm_ctx_res *res = data;
322 if (drm_core_check_feature(dev, DRIVER_MODESET))
325 if (res->count >= DRM_RESERVED_CONTEXTS) {
326 memset(&ctx, 0, sizeof(ctx));
327 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
329 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
333 res->count = DRM_RESERVED_CONTEXTS;
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.
347 * Get a new handle for the context and copy to userspace.
349 int drm_addctx(struct drm_device *dev, void *data,
350 struct drm_file *file_priv)
352 struct drm_ctx_list *ctx_entry;
353 struct drm_ctx *ctx = data;
355 if (drm_core_check_feature(dev, DRIVER_MODESET))
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);
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? */
370 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
372 DRM_DEBUG("out of memory\n");
376 INIT_LIST_HEAD(&ctx_entry->head);
377 ctx_entry->handle = ctx->handle;
378 ctx_entry->tag = file_priv;
380 mutex_lock(&dev->ctxlist_mutex);
381 list_add(&ctx_entry->head, &dev->ctxlist);
383 mutex_unlock(&dev->ctxlist_mutex);
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.
397 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
399 struct drm_ctx *ctx = data;
401 if (drm_core_check_feature(dev, DRIVER_MODESET))
404 /* This is 0, because we don't handle any context flags */
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.
419 * Calls context_switch().
421 int drm_switchctx(struct drm_device *dev, void *data,
422 struct drm_file *file_priv)
424 struct drm_ctx *ctx = data;
426 if (drm_core_check_feature(dev, DRIVER_MODESET))
429 DRM_DEBUG("%d\n", ctx->handle);
430 return drm_context_switch(dev, dev->last_context, ctx->handle);
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.
442 * Calls context_switch_complete().
444 int drm_newctx(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
447 struct drm_ctx *ctx = data;
449 if (drm_core_check_feature(dev, DRIVER_MODESET))
452 DRM_DEBUG("%d\n", ctx->handle);
453 drm_context_switch_complete(dev, file_priv, ctx->handle);
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.
467 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
469 int drm_rmctx(struct drm_device *dev, void *data,
470 struct drm_file *file_priv)
472 struct drm_ctx *ctx = data;
474 if (drm_core_check_feature(dev, DRIVER_MODESET))
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);
484 mutex_lock(&dev->ctxlist_mutex);
485 if (!list_empty(&dev->ctxlist)) {
486 struct drm_ctx_list *pos, *n;
488 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
489 if (pos->handle == ctx->handle) {
490 list_del(&pos->head);
496 mutex_unlock(&dev->ctxlist_mutex);