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.
45 /******************************************************************/
46 /** \name Context bitmap support */
50 * Free a handle from the context bitmap.
52 * \param dev DRM device.
53 * \param ctx_handle context handle.
55 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
56 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
59 void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
61 mutex_lock(&dev->struct_mutex);
62 idr_remove(&dev->ctx_idr, ctx_handle);
63 mutex_unlock(&dev->struct_mutex);
67 * Context bitmap allocation.
69 * \param dev DRM device.
70 * \return (non-negative) context handle on success or a negative number on failure.
72 * Allocate a new idr from drm_device::ctx_idr while holding the
73 * drm_device::struct_mutex lock.
75 static int drm_ctxbitmap_next(struct drm_device * dev)
79 mutex_lock(&dev->struct_mutex);
80 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
82 mutex_unlock(&dev->struct_mutex);
87 * Context bitmap initialization.
89 * \param dev DRM device.
91 * Initialise the drm_device::ctx_idr
93 int drm_ctxbitmap_init(struct drm_device * dev)
95 idr_init(&dev->ctx_idr);
100 * Context bitmap cleanup.
102 * \param dev DRM device.
104 * Free all idr members using drm_ctx_sarea_free helper function
105 * while holding the drm_device::struct_mutex lock.
107 void drm_ctxbitmap_cleanup(struct drm_device * dev)
109 mutex_lock(&dev->struct_mutex);
110 idr_destroy(&dev->ctx_idr);
111 mutex_unlock(&dev->struct_mutex);
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
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.
123 void drm_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
125 struct drm_ctx_list *pos, *tmp;
127 mutex_lock(&dev->ctxlist_mutex);
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);
135 drm_ctxbitmap_free(dev, pos->handle);
136 list_del(&pos->head);
141 mutex_unlock(&dev->ctxlist_mutex);
146 /******************************************************************/
147 /** \name Per Context SAREA Support */
151 * Get per-context SAREA.
153 * \param inode device inode.
154 * \param file_priv DRM file private.
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.
159 * Gets the map from drm_device::ctx_idr with the handle specified and
160 * returns its handle.
162 int drm_getsareactx(struct drm_device *dev, void *data,
163 struct drm_file *file_priv)
165 struct drm_ctx_priv_map *request = data;
166 struct drm_local_map *map;
167 struct drm_map_list *_entry;
169 mutex_lock(&dev->struct_mutex);
171 map = idr_find(&dev->ctx_idr, request->ctx_id);
173 mutex_unlock(&dev->struct_mutex);
177 request->handle = NULL;
178 list_for_each_entry(_entry, &dev->maplist, head) {
179 if (_entry->map == map) {
181 (void *)(unsigned long)_entry->user_token;
186 mutex_unlock(&dev->struct_mutex);
188 if (request->handle == NULL)
195 * Set per-context SAREA.
197 * \param inode device inode.
198 * \param file_priv DRM file private.
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.
203 * Searches the mapping specified in \p arg and update the entry in
204 * drm_device::ctx_idr with it.
206 int drm_setsareactx(struct drm_device *dev, void *data,
207 struct drm_file *file_priv)
209 struct drm_ctx_priv_map *request = data;
210 struct drm_local_map *map = NULL;
211 struct drm_map_list *r_list = NULL;
213 mutex_lock(&dev->struct_mutex);
214 list_for_each_entry(r_list, &dev->maplist, head) {
216 && r_list->user_token == (unsigned long) request->handle)
220 mutex_unlock(&dev->struct_mutex);
228 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
231 mutex_unlock(&dev->struct_mutex);
238 /******************************************************************/
239 /** \name The actual DRM context handling routines */
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.
250 * Attempt to set drm_device::context_flag.
252 static int drm_context_switch(struct drm_device * dev, int old, int new)
254 if (test_and_set_bit(0, &dev->context_flag)) {
255 DRM_ERROR("Reentering -- FIXME\n");
259 DRM_DEBUG("Context switch from %d to %d\n", old, new);
261 if (new == dev->last_context) {
262 clear_bit(0, &dev->context_flag);
270 * Complete context switch.
272 * \param dev DRM device.
273 * \param new new context handle.
274 * \return zero on success or a negative number on failure.
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.
280 static int drm_context_switch_complete(struct drm_device *dev,
281 struct drm_file *file_priv, int new)
283 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
285 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
286 DRM_ERROR("Lock isn't held after context switch\n");
289 /* If a context switch is ever initiated
290 when the kernel holds the lock, release
292 clear_bit(0, &dev->context_flag);
300 * \param inode device inode.
301 * \param file_priv DRM file private.
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.
306 int drm_resctx(struct drm_device *dev, void *data,
307 struct drm_file *file_priv)
309 struct drm_ctx_res *res = data;
313 if (res->count >= DRM_RESERVED_CONTEXTS) {
314 memset(&ctx, 0, sizeof(ctx));
315 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
317 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
321 res->count = DRM_RESERVED_CONTEXTS;
329 * \param inode device inode.
330 * \param file_priv DRM file private.
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.
335 * Get a new handle for the context and copy to userspace.
337 int drm_addctx(struct drm_device *dev, void *data,
338 struct drm_file *file_priv)
340 struct drm_ctx_list *ctx_entry;
341 struct drm_ctx *ctx = data;
343 ctx->handle = drm_ctxbitmap_next(dev);
344 if (ctx->handle == DRM_KERNEL_CONTEXT) {
345 /* Skip kernel's context and get a new one. */
346 ctx->handle = drm_ctxbitmap_next(dev);
348 DRM_DEBUG("%d\n", ctx->handle);
349 if (ctx->handle == -1) {
350 DRM_DEBUG("Not enough free contexts.\n");
351 /* Should this return -EBUSY instead? */
355 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
357 DRM_DEBUG("out of memory\n");
361 INIT_LIST_HEAD(&ctx_entry->head);
362 ctx_entry->handle = ctx->handle;
363 ctx_entry->tag = file_priv;
365 mutex_lock(&dev->ctxlist_mutex);
366 list_add(&ctx_entry->head, &dev->ctxlist);
367 mutex_unlock(&dev->ctxlist_mutex);
375 * \param inode device inode.
376 * \param file_priv DRM file private.
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.
381 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
383 struct drm_ctx *ctx = data;
385 /* This is 0, because we don't handle any context flags */
394 * \param inode device inode.
395 * \param file_priv DRM file private.
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.
400 * Calls context_switch().
402 int drm_switchctx(struct drm_device *dev, void *data,
403 struct drm_file *file_priv)
405 struct drm_ctx *ctx = data;
407 DRM_DEBUG("%d\n", ctx->handle);
408 return drm_context_switch(dev, dev->last_context, ctx->handle);
414 * \param inode device inode.
415 * \param file_priv DRM file private.
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.
420 * Calls context_switch_complete().
422 int drm_newctx(struct drm_device *dev, void *data,
423 struct drm_file *file_priv)
425 struct drm_ctx *ctx = data;
427 DRM_DEBUG("%d\n", ctx->handle);
428 drm_context_switch_complete(dev, file_priv, 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 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
444 int drm_rmctx(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
447 struct drm_ctx *ctx = data;
449 DRM_DEBUG("%d\n", ctx->handle);
450 if (ctx->handle != DRM_KERNEL_CONTEXT) {
451 if (dev->driver->context_dtor)
452 dev->driver->context_dtor(dev, ctx->handle);
453 drm_ctxbitmap_free(dev, ctx->handle);
456 mutex_lock(&dev->ctxlist_mutex);
457 if (!list_empty(&dev->ctxlist)) {
458 struct drm_ctx_list *pos, *n;
460 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
461 if (pos->handle == ctx->handle) {
462 list_del(&pos->head);
467 mutex_unlock(&dev->ctxlist_mutex);