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.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 * 2001-11-16 Torsten Duwe <duwe@caldera.de>
42 * added context constructor/destructor hooks,
43 * needed by SiS driver's memory management.
46 #include <dev/drm2/drmP.h>
48 /******************************************************************/
49 /** \name Context bitmap support */
53 * Free a handle from the context bitmap.
55 * \param dev DRM device.
56 * \param ctx_handle context handle.
58 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
59 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
62 void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
64 if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP ||
65 dev->ctx_bitmap == NULL) {
66 DRM_ERROR("Attempt to free invalid context handle: %d\n",
72 clear_bit(ctx_handle, dev->ctx_bitmap);
73 dev->context_sareas[ctx_handle] = NULL;
78 * Context bitmap allocation.
80 * \param dev DRM device.
81 * \return (non-negative) context handle on success or a negative number on failure.
83 * Allocate a new idr from drm_device::ctx_idr while holding the
84 * drm_device::struct_mutex lock.
86 static int drm_ctxbitmap_next(struct drm_device * dev)
90 if (dev->ctx_bitmap == NULL)
94 bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
95 if (bit >= DRM_MAX_CTXBITMAP) {
100 set_bit(bit, dev->ctx_bitmap);
101 DRM_DEBUG("bit : %d\n", bit);
102 if ((bit+1) > dev->max_context) {
103 struct drm_local_map **ctx_sareas;
104 int max_ctx = (bit+1);
106 ctx_sareas = realloc(dev->context_sareas,
107 max_ctx * sizeof(*dev->context_sareas),
108 DRM_MEM_SAREA, M_NOWAIT);
109 if (ctx_sareas == NULL) {
110 clear_bit(bit, dev->ctx_bitmap);
111 DRM_DEBUG("failed to allocate bit : %d\n", bit);
115 dev->max_context = max_ctx;
116 dev->context_sareas = ctx_sareas;
117 dev->context_sareas[bit] = NULL;
124 * Context bitmap initialization.
126 * \param dev DRM device.
128 * Initialise the drm_device::ctx_idr
130 int drm_ctxbitmap_init(struct drm_device * dev)
136 dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP,
138 if (dev->ctx_bitmap == NULL) {
142 dev->context_sareas = NULL;
143 dev->max_context = -1;
146 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
147 temp = drm_ctxbitmap_next(dev);
148 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
155 * Context bitmap cleanup.
157 * \param dev DRM device.
159 * Free all idr members using drm_ctx_sarea_free helper function
160 * while holding the drm_device::struct_mutex lock.
162 void drm_ctxbitmap_cleanup(struct drm_device * dev)
165 if (dev->context_sareas != NULL)
166 free(dev->context_sareas, DRM_MEM_SAREA);
167 free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP);
173 /******************************************************************/
174 /** \name Per Context SAREA Support */
178 * Get per-context SAREA.
180 * \param inode device inode.
181 * \param file_priv DRM file private.
182 * \param cmd command.
183 * \param arg user argument pointing to a drm_ctx_priv_map structure.
184 * \return zero on success or a negative number on failure.
186 * Gets the map from drm_device::ctx_idr with the handle specified and
187 * returns its handle.
189 int drm_getsareactx(struct drm_device *dev, void *data,
190 struct drm_file *file_priv)
192 struct drm_ctx_priv_map *request = data;
193 struct drm_local_map *map;
196 if (dev->max_context < 0 ||
197 request->ctx_id >= (unsigned) dev->max_context) {
202 map = dev->context_sareas[request->ctx_id];
205 request->handle = (void *)map->handle;
211 * Set per-context SAREA.
213 * \param inode device inode.
214 * \param file_priv DRM file private.
215 * \param cmd command.
216 * \param arg user argument pointing to a drm_ctx_priv_map structure.
217 * \return zero on success or a negative number on failure.
219 * Searches the mapping specified in \p arg and update the entry in
220 * drm_device::ctx_idr with it.
222 int drm_setsareactx(struct drm_device *dev, void *data,
223 struct drm_file *file_priv)
225 struct drm_ctx_priv_map *request = data;
226 struct drm_local_map *map = NULL;
227 struct drm_map_list *r_list = NULL;
230 list_for_each_entry(r_list, &dev->maplist, head) {
232 && r_list->user_token == (unsigned long) request->handle) {
233 if (dev->max_context < 0)
235 if (request->ctx_id >= (unsigned) dev->max_context)
237 dev->context_sareas[request->ctx_id] = map;
250 /******************************************************************/
251 /** \name The actual DRM context handling routines */
257 * \param dev DRM device.
258 * \param old old context handle.
259 * \param new new context handle.
260 * \return zero on success or a negative number on failure.
262 * Attempt to set drm_device::context_flag.
264 static int drm_context_switch(struct drm_device * dev, int old, int new)
266 if (test_and_set_bit(0, &dev->context_flag)) {
267 DRM_ERROR("Reentering -- FIXME\n");
271 DRM_DEBUG("Context switch from %d to %d\n", old, new);
273 if (new == dev->last_context) {
274 clear_bit(0, &dev->context_flag);
282 * Complete context switch.
284 * \param dev DRM device.
285 * \param new new context handle.
286 * \return zero on success or a negative number on failure.
288 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
289 * hardware lock is held, clears the drm_device::context_flag and wakes up
290 * drm_device::context_wait.
292 static int drm_context_switch_complete(struct drm_device *dev,
293 struct drm_file *file_priv, int new)
295 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
296 dev->last_switch = jiffies;
298 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
299 DRM_ERROR("Lock isn't held after context switch\n");
302 /* If a context switch is ever initiated
303 when the kernel holds the lock, release
305 clear_bit(0, &dev->context_flag);
306 wakeup(&dev->context_wait);
314 * \param inode device inode.
315 * \param file_priv DRM file private.
316 * \param cmd command.
317 * \param arg user argument pointing to a drm_ctx_res structure.
318 * \return zero on success or a negative number on failure.
320 int drm_resctx(struct drm_device *dev, void *data,
321 struct drm_file *file_priv)
323 struct drm_ctx_res *res = data;
327 if (res->count >= DRM_RESERVED_CONTEXTS) {
328 memset(&ctx, 0, sizeof(ctx));
329 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
331 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
335 res->count = DRM_RESERVED_CONTEXTS;
343 * \param inode device inode.
344 * \param file_priv DRM file private.
345 * \param cmd command.
346 * \param arg user argument pointing to a drm_ctx structure.
347 * \return zero on success or a negative number on failure.
349 * Get a new handle for the context and copy to userspace.
351 int drm_addctx(struct drm_device *dev, void *data,
352 struct drm_file *file_priv)
354 struct drm_ctx_list *ctx_entry;
355 struct drm_ctx *ctx = data;
357 ctx->handle = drm_ctxbitmap_next(dev);
358 if (ctx->handle == DRM_KERNEL_CONTEXT) {
359 /* Skip kernel's context and get a new one. */
360 ctx->handle = drm_ctxbitmap_next(dev);
362 DRM_DEBUG("%d\n", ctx->handle);
363 if (ctx->handle == -1) {
364 DRM_DEBUG("Not enough free contexts.\n");
365 /* Should this return -EBUSY instead? */
369 ctx_entry = malloc(sizeof(*ctx_entry), DRM_MEM_CTXBITMAP, M_NOWAIT);
371 DRM_DEBUG("out of memory\n");
375 INIT_LIST_HEAD(&ctx_entry->head);
376 ctx_entry->handle = ctx->handle;
377 ctx_entry->tag = file_priv;
380 list_add(&ctx_entry->head, &dev->ctxlist);
387 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
389 /* This does nothing */
396 * \param inode device inode.
397 * \param file_priv DRM file private.
398 * \param cmd command.
399 * \param arg user argument pointing to a drm_ctx structure.
400 * \return zero on success or a negative number on failure.
402 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
404 struct drm_ctx *ctx = data;
406 /* This is 0, because we don't handle any context flags */
415 * \param inode device inode.
416 * \param file_priv DRM file private.
417 * \param cmd command.
418 * \param arg user argument pointing to a drm_ctx structure.
419 * \return zero on success or a negative number on failure.
421 * Calls context_switch().
423 int drm_switchctx(struct drm_device *dev, void *data,
424 struct drm_file *file_priv)
426 struct drm_ctx *ctx = data;
428 DRM_DEBUG("%d\n", ctx->handle);
429 return drm_context_switch(dev, dev->last_context, ctx->handle);
435 * \param inode device inode.
436 * \param file_priv DRM file private.
437 * \param cmd command.
438 * \param arg user argument pointing to a drm_ctx structure.
439 * \return zero on success or a negative number on failure.
441 * Calls context_switch_complete().
443 int drm_newctx(struct drm_device *dev, void *data,
444 struct drm_file *file_priv)
446 struct drm_ctx *ctx = data;
448 DRM_DEBUG("%d\n", ctx->handle);
449 drm_context_switch_complete(dev, file_priv, ctx->handle);
457 * \param inode device inode.
458 * \param file_priv DRM file private.
459 * \param cmd command.
460 * \param arg user argument pointing to a drm_ctx structure.
461 * \return zero on success or a negative number on failure.
463 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
465 int drm_rmctx(struct drm_device *dev, void *data,
466 struct drm_file *file_priv)
468 struct drm_ctx *ctx = data;
470 DRM_DEBUG("%d\n", ctx->handle);
471 if (ctx->handle != DRM_KERNEL_CONTEXT) {
472 if (dev->driver->context_dtor)
473 dev->driver->context_dtor(dev, ctx->handle);
474 drm_ctxbitmap_free(dev, ctx->handle);
478 if (!list_empty(&dev->ctxlist)) {
479 struct drm_ctx_list *pos, *n;
481 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
482 if (pos->handle == ctx->handle) {
483 list_del(&pos->head);
484 free(pos, DRM_MEM_CTXBITMAP);