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::context_sareas, while holding the drm_device::struct_mutex
59 void drm_ctxbitmap_free(struct drm_device
* dev
, int ctx_handle
)
66 if (ctx_handle
< DRM_MAX_CTXBITMAP
) {
67 mutex_lock(&dev
->struct_mutex
);
68 clear_bit(ctx_handle
, dev
->ctx_bitmap
);
69 dev
->context_sareas
[ctx_handle
] = NULL
;
70 mutex_unlock(&dev
->struct_mutex
);
74 DRM_ERROR("Attempt to free invalid context handle: %d\n", ctx_handle
);
79 * Context bitmap allocation.
81 * \param dev DRM device.
82 * \return (non-negative) context handle on success or a negative number on failure.
84 * Find the first zero bit in drm_device::ctx_bitmap and (re)allocates
85 * drm_device::context_sareas to accommodate the new entry while holding the
86 * drm_device::struct_mutex lock.
88 static int drm_ctxbitmap_next(struct drm_device
* dev
)
95 mutex_lock(&dev
->struct_mutex
);
96 bit
= find_first_zero_bit(dev
->ctx_bitmap
, DRM_MAX_CTXBITMAP
);
97 if (bit
< DRM_MAX_CTXBITMAP
) {
98 set_bit(bit
, dev
->ctx_bitmap
);
99 DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit
);
100 if ((bit
+ 1) > dev
->max_context
) {
101 dev
->max_context
= (bit
+ 1);
102 if (dev
->context_sareas
) {
103 struct drm_map
**ctx_sareas
;
105 ctx_sareas
= drm_realloc(dev
->context_sareas
,
115 clear_bit(bit
, dev
->ctx_bitmap
);
116 mutex_unlock(&dev
->struct_mutex
);
119 dev
->context_sareas
= ctx_sareas
;
120 dev
->context_sareas
[bit
] = NULL
;
122 /* max_context == 1 at this point */
123 dev
->context_sareas
=
124 drm_alloc(dev
->max_context
*
125 sizeof(*dev
->context_sareas
),
127 if (!dev
->context_sareas
) {
128 clear_bit(bit
, dev
->ctx_bitmap
);
129 mutex_unlock(&dev
->struct_mutex
);
132 dev
->context_sareas
[bit
] = NULL
;
135 mutex_unlock(&dev
->struct_mutex
);
138 mutex_unlock(&dev
->struct_mutex
);
143 * Context bitmap initialization.
145 * \param dev DRM device.
147 * Allocates and initialize drm_device::ctx_bitmap and drm_device::context_sareas, while holding
148 * the drm_device::struct_mutex lock.
150 int drm_ctxbitmap_init(struct drm_device
* dev
)
155 mutex_lock(&dev
->struct_mutex
);
156 dev
->ctx_bitmap
= (unsigned long *)drm_alloc(PAGE_SIZE
,
158 if (dev
->ctx_bitmap
== NULL
) {
159 mutex_unlock(&dev
->struct_mutex
);
162 memset((void *)dev
->ctx_bitmap
, 0, PAGE_SIZE
);
163 dev
->context_sareas
= NULL
;
164 dev
->max_context
= -1;
165 mutex_unlock(&dev
->struct_mutex
);
167 for (i
= 0; i
< DRM_RESERVED_CONTEXTS
; i
++) {
168 temp
= drm_ctxbitmap_next(dev
);
169 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp
);
176 * Context bitmap cleanup.
178 * \param dev DRM device.
180 * Frees drm_device::ctx_bitmap and drm_device::context_sareas, while holding
181 * the drm_device::struct_mutex lock.
183 void drm_ctxbitmap_cleanup(struct drm_device
* dev
)
185 mutex_lock(&dev
->struct_mutex
);
186 if (dev
->context_sareas
)
187 drm_free(dev
->context_sareas
,
188 sizeof(*dev
->context_sareas
) *
189 dev
->max_context
, DRM_MEM_MAPS
);
190 drm_free((void *)dev
->ctx_bitmap
, PAGE_SIZE
, DRM_MEM_CTXBITMAP
);
191 mutex_unlock(&dev
->struct_mutex
);
196 /******************************************************************/
197 /** \name Per Context SAREA Support */
201 * Get per-context SAREA.
203 * \param inode device inode.
204 * \param filp file pointer.
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 * Gets the map from drm_device::context_sareas with the handle specified and
210 * returns its handle.
212 int drm_getsareactx(struct inode
*inode
, struct file
*filp
,
213 unsigned int cmd
, unsigned long arg
)
215 struct drm_file
*priv
= filp
->private_data
;
216 struct drm_device
*dev
= priv
->head
->dev
;
217 struct drm_ctx_priv_map __user
*argp
= (void __user
*)arg
;
218 struct drm_ctx_priv_map request
;
220 drm_map_list_t
*_entry
;
222 if (copy_from_user(&request
, argp
, sizeof(request
)))
225 mutex_lock(&dev
->struct_mutex
);
226 if (dev
->max_context
< 0
227 || request
.ctx_id
>= (unsigned)dev
->max_context
) {
228 mutex_unlock(&dev
->struct_mutex
);
232 map
= dev
->context_sareas
[request
.ctx_id
];
233 mutex_unlock(&dev
->struct_mutex
);
235 request
.handle
= NULL
;
236 list_for_each_entry(_entry
, &dev
->maplist
, head
) {
237 if (_entry
->map
== map
) {
239 (void *)(unsigned long)_entry
->user_token
;
243 if (request
.handle
== NULL
)
246 if (copy_to_user(argp
, &request
, sizeof(request
)))
252 * Set per-context SAREA.
254 * \param inode device inode.
255 * \param filp file pointer.
256 * \param cmd command.
257 * \param arg user argument pointing to a drm_ctx_priv_map structure.
258 * \return zero on success or a negative number on failure.
260 * Searches the mapping specified in \p arg and update the entry in
261 * drm_device::context_sareas with it.
263 int drm_setsareactx(struct inode
*inode
, struct file
*filp
,
264 unsigned int cmd
, unsigned long arg
)
266 struct drm_file
*priv
= filp
->private_data
;
267 struct drm_device
*dev
= priv
->head
->dev
;
268 struct drm_ctx_priv_map request
;
269 struct drm_map
*map
= NULL
;
270 drm_map_list_t
*r_list
= NULL
;
272 if (copy_from_user(&request
,
273 (struct drm_ctx_priv_map __user
*) arg
,
277 mutex_lock(&dev
->struct_mutex
);
278 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
280 && r_list
->user_token
== (unsigned long)request
.handle
)
284 mutex_unlock(&dev
->struct_mutex
);
291 if (dev
->max_context
< 0)
293 if (request
.ctx_id
>= (unsigned)dev
->max_context
)
295 dev
->context_sareas
[request
.ctx_id
] = map
;
296 mutex_unlock(&dev
->struct_mutex
);
302 /******************************************************************/
303 /** \name The actual DRM context handling routines */
309 * \param dev DRM device.
310 * \param old old context handle.
311 * \param new new context handle.
312 * \return zero on success or a negative number on failure.
314 * Attempt to set drm_device::context_flag.
316 static int drm_context_switch(struct drm_device
* dev
, int old
, int new)
318 if (test_and_set_bit(0, &dev
->context_flag
)) {
319 DRM_ERROR("Reentering -- FIXME\n");
323 DRM_DEBUG("Context switch from %d to %d\n", old
, new);
325 if (new == dev
->last_context
) {
326 clear_bit(0, &dev
->context_flag
);
334 * Complete context switch.
336 * \param dev DRM device.
337 * \param new new context handle.
338 * \return zero on success or a negative number on failure.
340 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
341 * hardware lock is held, clears the drm_device::context_flag and wakes up
342 * drm_device::context_wait.
344 static int drm_context_switch_complete(struct drm_device
* dev
, int new)
346 dev
->last_context
= new; /* PRE/POST: This is the _only_ writer. */
347 dev
->last_switch
= jiffies
;
349 if (!_DRM_LOCK_IS_HELD(dev
->lock
.hw_lock
->lock
)) {
350 DRM_ERROR("Lock isn't held after context switch\n");
353 /* If a context switch is ever initiated
354 when the kernel holds the lock, release
356 clear_bit(0, &dev
->context_flag
);
357 wake_up(&dev
->context_wait
);
365 * \param inode device inode.
366 * \param filp file pointer.
367 * \param cmd command.
368 * \param arg user argument pointing to a drm_ctx_res structure.
369 * \return zero on success or a negative number on failure.
371 int drm_resctx(struct inode
*inode
, struct file
*filp
,
372 unsigned int cmd
, unsigned long arg
)
374 struct drm_ctx_res res
;
375 struct drm_ctx_res __user
*argp
= (void __user
*)arg
;
379 if (copy_from_user(&res
, argp
, sizeof(res
)))
382 if (res
.count
>= DRM_RESERVED_CONTEXTS
) {
383 memset(&ctx
, 0, sizeof(ctx
));
384 for (i
= 0; i
< DRM_RESERVED_CONTEXTS
; i
++) {
386 if (copy_to_user(&res
.contexts
[i
], &ctx
, sizeof(ctx
)))
390 res
.count
= DRM_RESERVED_CONTEXTS
;
392 if (copy_to_user(argp
, &res
, sizeof(res
)))
400 * \param inode device inode.
401 * \param filp file pointer.
402 * \param cmd command.
403 * \param arg user argument pointing to a drm_ctx structure.
404 * \return zero on success or a negative number on failure.
406 * Get a new handle for the context and copy to userspace.
408 int drm_addctx(struct inode
*inode
, struct file
*filp
,
409 unsigned int cmd
, unsigned long arg
)
411 struct drm_file
*priv
= filp
->private_data
;
412 struct drm_device
*dev
= priv
->head
->dev
;
413 drm_ctx_list_t
*ctx_entry
;
414 struct drm_ctx __user
*argp
= (void __user
*)arg
;
417 if (copy_from_user(&ctx
, argp
, sizeof(ctx
)))
420 ctx
.handle
= drm_ctxbitmap_next(dev
);
421 if (ctx
.handle
== DRM_KERNEL_CONTEXT
) {
422 /* Skip kernel's context and get a new one. */
423 ctx
.handle
= drm_ctxbitmap_next(dev
);
425 DRM_DEBUG("%d\n", ctx
.handle
);
426 if (ctx
.handle
== -1) {
427 DRM_DEBUG("Not enough free contexts.\n");
428 /* Should this return -EBUSY instead? */
432 if (ctx
.handle
!= DRM_KERNEL_CONTEXT
) {
433 if (dev
->driver
->context_ctor
)
434 if (!dev
->driver
->context_ctor(dev
, ctx
.handle
)) {
435 DRM_DEBUG("Running out of ctxs or memory.\n");
440 ctx_entry
= drm_alloc(sizeof(*ctx_entry
), DRM_MEM_CTXLIST
);
442 DRM_DEBUG("out of memory\n");
446 INIT_LIST_HEAD(&ctx_entry
->head
);
447 ctx_entry
->handle
= ctx
.handle
;
448 ctx_entry
->tag
= priv
;
450 mutex_lock(&dev
->ctxlist_mutex
);
451 list_add(&ctx_entry
->head
, &dev
->ctxlist
);
453 mutex_unlock(&dev
->ctxlist_mutex
);
455 if (copy_to_user(argp
, &ctx
, sizeof(ctx
)))
460 int drm_modctx(struct inode
*inode
, struct file
*filp
,
461 unsigned int cmd
, unsigned long arg
)
463 /* This does nothing */
470 * \param inode device inode.
471 * \param filp file pointer.
472 * \param cmd command.
473 * \param arg user argument pointing to a drm_ctx structure.
474 * \return zero on success or a negative number on failure.
476 int drm_getctx(struct inode
*inode
, struct file
*filp
,
477 unsigned int cmd
, unsigned long arg
)
479 struct drm_ctx __user
*argp
= (void __user
*)arg
;
482 if (copy_from_user(&ctx
, argp
, sizeof(ctx
)))
485 /* This is 0, because we don't handle any context flags */
488 if (copy_to_user(argp
, &ctx
, sizeof(ctx
)))
496 * \param inode device inode.
497 * \param filp file pointer.
498 * \param cmd command.
499 * \param arg user argument pointing to a drm_ctx structure.
500 * \return zero on success or a negative number on failure.
502 * Calls context_switch().
504 int drm_switchctx(struct inode
*inode
, struct file
*filp
,
505 unsigned int cmd
, unsigned long arg
)
507 struct drm_file
*priv
= filp
->private_data
;
508 struct drm_device
*dev
= priv
->head
->dev
;
511 if (copy_from_user(&ctx
, (struct drm_ctx __user
*) arg
, sizeof(ctx
)))
514 DRM_DEBUG("%d\n", ctx
.handle
);
515 return drm_context_switch(dev
, dev
->last_context
, ctx
.handle
);
521 * \param inode device inode.
522 * \param filp file pointer.
523 * \param cmd command.
524 * \param arg user argument pointing to a drm_ctx structure.
525 * \return zero on success or a negative number on failure.
527 * Calls context_switch_complete().
529 int drm_newctx(struct inode
*inode
, struct file
*filp
,
530 unsigned int cmd
, unsigned long arg
)
532 struct drm_file
*priv
= filp
->private_data
;
533 struct drm_device
*dev
= priv
->head
->dev
;
536 if (copy_from_user(&ctx
, (struct drm_ctx __user
*) arg
, sizeof(ctx
)))
539 DRM_DEBUG("%d\n", ctx
.handle
);
540 drm_context_switch_complete(dev
, ctx
.handle
);
548 * \param inode device inode.
549 * \param filp file pointer.
550 * \param cmd command.
551 * \param arg user argument pointing to a drm_ctx structure.
552 * \return zero on success or a negative number on failure.
554 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
556 int drm_rmctx(struct inode
*inode
, struct file
*filp
,
557 unsigned int cmd
, unsigned long arg
)
559 struct drm_file
*priv
= filp
->private_data
;
560 struct drm_device
*dev
= priv
->head
->dev
;
563 if (copy_from_user(&ctx
, (struct drm_ctx __user
*) arg
, sizeof(ctx
)))
566 DRM_DEBUG("%d\n", ctx
.handle
);
567 if (ctx
.handle
== DRM_KERNEL_CONTEXT
+ 1) {
568 priv
->remove_auth_on_close
= 1;
570 if (ctx
.handle
!= DRM_KERNEL_CONTEXT
) {
571 if (dev
->driver
->context_dtor
)
572 dev
->driver
->context_dtor(dev
, ctx
.handle
);
573 drm_ctxbitmap_free(dev
, ctx
.handle
);
576 mutex_lock(&dev
->ctxlist_mutex
);
577 if (!list_empty(&dev
->ctxlist
)) {
578 drm_ctx_list_t
*pos
, *n
;
580 list_for_each_entry_safe(pos
, n
, &dev
->ctxlist
, head
) {
581 if (pos
->handle
== ctx
.handle
) {
582 list_del(&pos
->head
);
583 drm_free(pos
, sizeof(*pos
), DRM_MEM_CTXLIST
);
588 mutex_unlock(&dev
->ctxlist_mutex
);
This page took 0.045898 seconds and 5 git commands to generate.