blob: 83094c73da67abff05ab809bb76f0b0dd39c7e3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_context.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IOCTLs for generic contexts
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
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:
22 *
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
25 * Software.
26 *
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.
34 */
35
36/*
37 * ChangeLog:
38 * 2001-11-16 Torsten Duwe <duwe@caldera.de>
39 * added context constructor/destructor hooks,
40 * needed by SiS driver's memory management.
41 */
42
43#include "drmP.h"
44
45/******************************************************************/
46/** \name Context bitmap support */
47/*@{*/
48
49/**
50 * Free a handle from the context bitmap.
51 *
52 * \param dev DRM device.
53 * \param ctx_handle context handle.
54 *
55 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
Dave Airlie30e2fb12006-02-02 19:37:46 +110056 * in drm_device::context_sareas, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * lock.
58 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061 if (ctx_handle < 0)
62 goto failed;
63 if (!dev->ctx_bitmap)
64 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066 if (ctx_handle < DRM_MAX_CTXBITMAP) {
Dave Airlie30e2fb12006-02-02 19:37:46 +110067 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 clear_bit(ctx_handle, dev->ctx_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 dev->context_sareas[ctx_handle] = NULL;
Dave Airlie30e2fb12006-02-02 19:37:46 +110070 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return;
72 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100073 failed:
74 DRM_ERROR("Attempt to free invalid context handle: %d\n", ctx_handle);
75 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Context bitmap allocation.
80 *
81 * \param dev DRM device.
82 * \return (non-negative) context handle on success or a negative number on failure.
83 *
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
Dave Airlie30e2fb12006-02-02 19:37:46 +110086 * drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100088static int drm_ctxbitmap_next(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int bit;
91
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 if (!dev->ctx_bitmap)
93 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Dave Airlie30e2fb12006-02-02 19:37:46 +110095 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100096 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 drm_map_t **ctx_sareas;
104
105 ctx_sareas = drm_realloc(dev->context_sareas,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 (dev->max_context -
107 1) *
108 sizeof(*dev->
109 context_sareas),
110 dev->max_context *
111 sizeof(*dev->
112 context_sareas),
113 DRM_MEM_MAPS);
114 if (!ctx_sareas) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 clear_bit(bit, dev->ctx_bitmap);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100116 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -1;
118 }
119 dev->context_sareas = ctx_sareas;
120 dev->context_sareas[bit] = NULL;
121 } else {
122 /* max_context == 1 at this point */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123 dev->context_sareas =
124 drm_alloc(dev->max_context *
125 sizeof(*dev->context_sareas),
126 DRM_MEM_MAPS);
127 if (!dev->context_sareas) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 clear_bit(bit, dev->ctx_bitmap);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100129 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -1;
131 }
132 dev->context_sareas[bit] = NULL;
133 }
134 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100135 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return bit;
137 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100138 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return -1;
140}
141
142/**
143 * Context bitmap initialization.
144 *
145 * \param dev DRM device.
146 *
147 * Allocates and initialize drm_device::ctx_bitmap and drm_device::context_sareas, while holding
Dave Airlie30e2fb12006-02-02 19:37:46 +1100148 * the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150int drm_ctxbitmap_init(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Dave Airlie30e2fb12006-02-02 19:37:46 +1100155 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156 dev->ctx_bitmap = (unsigned long *)drm_alloc(PAGE_SIZE,
157 DRM_MEM_CTXBITMAP);
158 if (dev->ctx_bitmap == NULL) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100159 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -ENOMEM;
161 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 memset((void *)dev->ctx_bitmap, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 dev->context_sareas = NULL;
164 dev->max_context = -1;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100165 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000167 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
168 temp = drm_ctxbitmap_next(dev);
169 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
172 return 0;
173}
174
175/**
176 * Context bitmap cleanup.
177 *
178 * \param dev DRM device.
179 *
180 * Frees drm_device::ctx_bitmap and drm_device::context_sareas, while holding
Dave Airlie30e2fb12006-02-02 19:37:46 +1100181 * the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183void drm_ctxbitmap_cleanup(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Dave Airlie30e2fb12006-02-02 19:37:46 +1100185 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 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);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100191 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*@}*/
195
196/******************************************************************/
197/** \name Per Context SAREA Support */
198/*@{*/
199
200/**
201 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000202 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * \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.
208 *
209 * Gets the map from drm_device::context_sareas with the handle specified and
210 * returns its handle.
211 */
212int drm_getsareactx(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000215 drm_file_t *priv = filp->private_data;
216 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 drm_ctx_priv_map_t __user *argp = (void __user *)arg;
218 drm_ctx_priv_map_t request;
219 drm_map_t *map;
Dave Airlied1f2b552005-08-05 22:11:22 +1000220 drm_map_list_t *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (copy_from_user(&request, argp, sizeof(request)))
223 return -EFAULT;
224
Dave Airlie30e2fb12006-02-02 19:37:46 +1100225 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 if (dev->max_context < 0
227 || request.ctx_id >= (unsigned)dev->max_context) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100228 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EINVAL;
230 }
231
232 map = dev->context_sareas[request.ctx_id];
Dave Airlie30e2fb12006-02-02 19:37:46 +1100233 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dave Airlieb3a83632005-09-30 18:37:36 +1000235 request.handle = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 list_for_each_entry(_entry, &dev->maplist->head, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000237 if (_entry->map == map) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 request.handle =
239 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000240 break;
241 }
242 }
Dave Airlieb3a83632005-09-30 18:37:36 +1000243 if (request.handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000244 return -EINVAL;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (copy_to_user(argp, &request, sizeof(request)))
247 return -EFAULT;
248 return 0;
249}
250
251/**
252 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * \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.
259 *
260 * Searches the mapping specified in \p arg and update the entry in
261 * drm_device::context_sareas with it.
262 */
263int drm_setsareactx(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000264 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 drm_file_t *priv = filp->private_data;
267 drm_device_t *dev = priv->head->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 drm_ctx_priv_map_t request;
269 drm_map_t *map = NULL;
270 drm_map_list_t *r_list = NULL;
271 struct list_head *list;
272
273 if (copy_from_user(&request,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 (drm_ctx_priv_map_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EFAULT;
276
Dave Airlie30e2fb12006-02-02 19:37:46 +1100277 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 list_for_each(list, &dev->maplist->head) {
279 r_list = list_entry(list, drm_map_list_t, head);
Dave Airlie9a186642005-06-23 21:29:18 +1000280 if (r_list->map
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 && r_list->user_token == (unsigned long)request.handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto found;
283 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100285 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -EINVAL;
287
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 if (!map)
291 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (dev->max_context < 0)
293 goto bad;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 if (request.ctx_id >= (unsigned)dev->max_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 goto bad;
296 dev->context_sareas[request.ctx_id] = map;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100297 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return 0;
299}
300
301/*@}*/
302
303/******************************************************************/
304/** \name The actual DRM context handling routines */
305/*@{*/
306
307/**
308 * Switch context.
309 *
310 * \param dev DRM device.
311 * \param old old context handle.
312 * \param new new context handle.
313 * \return zero on success or a negative number on failure.
314 *
315 * Attempt to set drm_device::context_flag.
316 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317static int drm_context_switch(drm_device_t * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000319 if (test_and_set_bit(0, &dev->context_flag)) {
320 DRM_ERROR("Reentering -- FIXME\n");
321 return -EBUSY;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 if (new == dev->last_context) {
327 clear_bit(0, &dev->context_flag);
328 return 0;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/**
335 * Complete context switch.
336 *
337 * \param dev DRM device.
338 * \param new new context handle.
339 * \return zero on success or a negative number on failure.
340 *
341 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
342 * hardware lock is held, clears the drm_device::context_flag and wakes up
343 * drm_device::context_wait.
344 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345static int drm_context_switch_complete(drm_device_t * dev, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
348 dev->last_switch = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
351 DRM_ERROR("Lock isn't held after context switch\n");
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 /* If a context switch is ever initiated
355 when the kernel holds the lock, release
356 that lock here. */
357 clear_bit(0, &dev->context_flag);
358 wake_up(&dev->context_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/**
364 * Reserve contexts.
365 *
366 * \param inode device inode.
367 * \param filp file pointer.
368 * \param cmd command.
369 * \param arg user argument pointing to a drm_ctx_res structure.
370 * \return zero on success or a negative number on failure.
371 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372int drm_resctx(struct inode *inode, struct file *filp,
373 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 drm_ctx_res_t res;
376 drm_ctx_t __user *argp = (void __user *)arg;
377 drm_ctx_t ctx;
378 int i;
379
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 if (copy_from_user(&res, argp, sizeof(res)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -EFAULT;
382
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 if (res.count >= DRM_RESERVED_CONTEXTS) {
384 memset(&ctx, 0, sizeof(ctx));
385 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 ctx.handle = i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 if (copy_to_user(&res.contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EFAULT;
389 }
390 }
391 res.count = DRM_RESERVED_CONTEXTS;
392
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 if (copy_to_user(argp, &res, sizeof(res)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EFAULT;
395 return 0;
396}
397
398/**
399 * Add context.
400 *
401 * \param inode device inode.
402 * \param filp file pointer.
403 * \param cmd command.
404 * \param arg user argument pointing to a drm_ctx structure.
405 * \return zero on success or a negative number on failure.
406 *
407 * Get a new handle for the context and copy to userspace.
408 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000409int drm_addctx(struct inode *inode, struct file *filp,
410 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 drm_file_t *priv = filp->private_data;
413 drm_device_t *dev = priv->head->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 drm_ctx_list_t *ctx_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 drm_ctx_t __user *argp = (void __user *)arg;
416 drm_ctx_t ctx;
417
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 if (copy_from_user(&ctx, argp, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -EFAULT;
420
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000421 ctx.handle = drm_ctxbitmap_next(dev);
422 if (ctx.handle == DRM_KERNEL_CONTEXT) {
423 /* Skip kernel's context and get a new one. */
424 ctx.handle = drm_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 DRM_DEBUG("%d\n", ctx.handle);
427 if (ctx.handle == -1) {
428 DRM_DEBUG("Not enough free contexts.\n");
429 /* Should this return -EBUSY instead? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -ENOMEM;
431 }
432
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000433 if (ctx.handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (dev->driver->context_ctor)
Dave Airlie1e7d5192006-01-02 19:25:35 +1100435 if (!dev->driver->context_ctor(dev, ctx.handle)) {
Dave Airlieb5e9fc12006-01-02 19:23:44 +1100436 DRM_DEBUG("Running out of ctxs or memory.\n");
437 return -ENOMEM;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 ctx_entry = drm_alloc(sizeof(*ctx_entry), DRM_MEM_CTXLIST);
442 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 DRM_DEBUG("out of memory\n");
444 return -ENOMEM;
445 }
446
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 INIT_LIST_HEAD(&ctx_entry->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 ctx_entry->handle = ctx.handle;
449 ctx_entry->tag = priv;
450
Dave Airlie30e2fb12006-02-02 19:37:46 +1100451 mutex_lock(&dev->ctxlist_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 list_add(&ctx_entry->head, &dev->ctxlist->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 ++dev->ctx_count;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100454 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 if (copy_to_user(argp, &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -EFAULT;
458 return 0;
459}
460
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461int drm_modctx(struct inode *inode, struct file *filp,
462 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 /* This does nothing */
465 return 0;
466}
467
468/**
469 * Get context.
470 *
471 * \param inode device inode.
472 * \param filp file pointer.
473 * \param cmd command.
474 * \param arg user argument pointing to a drm_ctx structure.
475 * \return zero on success or a negative number on failure.
476 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477int drm_getctx(struct inode *inode, struct file *filp,
478 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 drm_ctx_t __user *argp = (void __user *)arg;
481 drm_ctx_t ctx;
482
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483 if (copy_from_user(&ctx, argp, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -EFAULT;
485
486 /* This is 0, because we don't handle any context flags */
487 ctx.flags = 0;
488
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 if (copy_to_user(argp, &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -EFAULT;
491 return 0;
492}
493
494/**
495 * Switch context.
496 *
497 * \param inode device inode.
498 * \param filp file pointer.
499 * \param cmd command.
500 * \param arg user argument pointing to a drm_ctx structure.
501 * \return zero on success or a negative number on failure.
502 *
503 * Calls context_switch().
504 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505int drm_switchctx(struct inode *inode, struct file *filp,
506 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 drm_file_t *priv = filp->private_data;
509 drm_device_t *dev = priv->head->dev;
510 drm_ctx_t ctx;
511
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EFAULT;
514
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000515 DRM_DEBUG("%d\n", ctx.handle);
516 return drm_context_switch(dev, dev->last_context, ctx.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/**
520 * New context.
521 *
522 * \param inode device inode.
523 * \param filp file pointer.
524 * \param cmd command.
525 * \param arg user argument pointing to a drm_ctx structure.
526 * \return zero on success or a negative number on failure.
527 *
528 * Calls context_switch_complete().
529 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530int drm_newctx(struct inode *inode, struct file *filp,
531 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 drm_file_t *priv = filp->private_data;
534 drm_device_t *dev = priv->head->dev;
535 drm_ctx_t ctx;
536
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EFAULT;
539
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 DRM_DEBUG("%d\n", ctx.handle);
541 drm_context_switch_complete(dev, ctx.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 return 0;
544}
545
546/**
547 * Remove context.
548 *
549 * \param inode device inode.
550 * \param filp file pointer.
551 * \param cmd command.
552 * \param arg user argument pointing to a drm_ctx structure.
553 * \return zero on success or a negative number on failure.
554 *
555 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
556 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000557int drm_rmctx(struct inode *inode, struct file *filp,
558 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 drm_file_t *priv = filp->private_data;
561 drm_device_t *dev = priv->head->dev;
562 drm_ctx_t ctx;
563
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564 if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EFAULT;
566
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 DRM_DEBUG("%d\n", ctx.handle);
568 if (ctx.handle == DRM_KERNEL_CONTEXT + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 priv->remove_auth_on_close = 1;
570 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000571 if (ctx.handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (dev->driver->context_dtor)
573 dev->driver->context_dtor(dev, ctx.handle);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 drm_ctxbitmap_free(dev, ctx.handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Dave Airlie30e2fb12006-02-02 19:37:46 +1100577 mutex_lock(&dev->ctxlist_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 if (!list_empty(&dev->ctxlist->head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 drm_ctx_list_t *pos, *n;
580
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000581 list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
582 if (pos->handle == ctx.handle) {
583 list_del(&pos->head);
584 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 --dev->ctx_count;
586 }
587 }
588 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100589 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return 0;
592}
593
594/*@}*/