Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /** |
b5e89ed5 | 2 | * \file drm_context.c |
1da177e4 | 3 | * IOCTLs for generic contexts |
b5e89ed5 | 4 | * |
1da177e4 LT |
5 | * \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 | |
30e2fb18 | 56 | * in drm_device::context_sareas, while holding the drm_device::struct_mutex |
1da177e4 LT |
57 | * lock. |
58 | */ | |
b5e89ed5 | 59 | void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle) |
1da177e4 | 60 | { |
b5e89ed5 DA |
61 | if (ctx_handle < 0) |
62 | goto failed; | |
63 | if (!dev->ctx_bitmap) | |
64 | goto failed; | |
1da177e4 | 65 | |
b5e89ed5 | 66 | if (ctx_handle < DRM_MAX_CTXBITMAP) { |
30e2fb18 | 67 | mutex_lock(&dev->struct_mutex); |
b5e89ed5 | 68 | clear_bit(ctx_handle, dev->ctx_bitmap); |
1da177e4 | 69 | dev->context_sareas[ctx_handle] = NULL; |
30e2fb18 | 70 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
71 | return; |
72 | } | |
b5e89ed5 DA |
73 | failed: |
74 | DRM_ERROR("Attempt to free invalid context handle: %d\n", ctx_handle); | |
75 | return; | |
1da177e4 LT |
76 | } |
77 | ||
b5e89ed5 | 78 | /** |
1da177e4 LT |
79 | * 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 | |
30e2fb18 | 86 | * drm_device::struct_mutex lock. |
1da177e4 | 87 | */ |
b5e89ed5 | 88 | static int drm_ctxbitmap_next(drm_device_t * dev) |
1da177e4 LT |
89 | { |
90 | int bit; | |
91 | ||
b5e89ed5 DA |
92 | if (!dev->ctx_bitmap) |
93 | return -1; | |
1da177e4 | 94 | |
30e2fb18 | 95 | mutex_lock(&dev->struct_mutex); |
b5e89ed5 DA |
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) { | |
1da177e4 LT |
103 | drm_map_t **ctx_sareas; |
104 | ||
105 | ctx_sareas = drm_realloc(dev->context_sareas, | |
b5e89ed5 DA |
106 | (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) { | |
1da177e4 | 115 | clear_bit(bit, dev->ctx_bitmap); |
30e2fb18 | 116 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
117 | return -1; |
118 | } | |
119 | dev->context_sareas = ctx_sareas; | |
120 | dev->context_sareas[bit] = NULL; | |
121 | } else { | |
122 | /* max_context == 1 at this point */ | |
b5e89ed5 DA |
123 | dev->context_sareas = |
124 | drm_alloc(dev->max_context * | |
125 | sizeof(*dev->context_sareas), | |
126 | DRM_MEM_MAPS); | |
127 | if (!dev->context_sareas) { | |
1da177e4 | 128 | clear_bit(bit, dev->ctx_bitmap); |
30e2fb18 | 129 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
130 | return -1; |
131 | } | |
132 | dev->context_sareas[bit] = NULL; | |
133 | } | |
134 | } | |
30e2fb18 | 135 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
136 | return bit; |
137 | } | |
30e2fb18 | 138 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
139 | 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 | |
30e2fb18 | 148 | * the drm_device::struct_mutex lock. |
1da177e4 | 149 | */ |
b5e89ed5 | 150 | int drm_ctxbitmap_init(drm_device_t * dev) |
1da177e4 LT |
151 | { |
152 | int i; | |
b5e89ed5 | 153 | int temp; |
1da177e4 | 154 | |
30e2fb18 | 155 | mutex_lock(&dev->struct_mutex); |
b5e89ed5 DA |
156 | dev->ctx_bitmap = (unsigned long *)drm_alloc(PAGE_SIZE, |
157 | DRM_MEM_CTXBITMAP); | |
158 | if (dev->ctx_bitmap == NULL) { | |
30e2fb18 | 159 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
160 | return -ENOMEM; |
161 | } | |
b5e89ed5 | 162 | memset((void *)dev->ctx_bitmap, 0, PAGE_SIZE); |
1da177e4 LT |
163 | dev->context_sareas = NULL; |
164 | dev->max_context = -1; | |
30e2fb18 | 165 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 166 | |
b5e89ed5 DA |
167 | for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { |
168 | temp = drm_ctxbitmap_next(dev); | |
169 | DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp); | |
1da177e4 LT |
170 | } |
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 | |
30e2fb18 | 181 | * the drm_device::struct_mutex lock. |
1da177e4 | 182 | */ |
b5e89ed5 | 183 | void drm_ctxbitmap_cleanup(drm_device_t * dev) |
1da177e4 | 184 | { |
30e2fb18 | 185 | mutex_lock(&dev->struct_mutex); |
b5e89ed5 DA |
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); | |
30e2fb18 | 191 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
192 | } |
193 | ||
194 | /*@}*/ | |
195 | ||
196 | /******************************************************************/ | |
197 | /** \name Per Context SAREA Support */ | |
198 | /*@{*/ | |
199 | ||
200 | /** | |
201 | * Get per-context SAREA. | |
b5e89ed5 | 202 | * |
1da177e4 LT |
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. | |
208 | * | |
209 | * Gets the map from drm_device::context_sareas with the handle specified and | |
210 | * returns its handle. | |
211 | */ | |
212 | int drm_getsareactx(struct inode *inode, struct file *filp, | |
b5e89ed5 | 213 | unsigned int cmd, unsigned long arg) |
1da177e4 | 214 | { |
b5e89ed5 DA |
215 | drm_file_t *priv = filp->private_data; |
216 | drm_device_t *dev = priv->head->dev; | |
1da177e4 LT |
217 | drm_ctx_priv_map_t __user *argp = (void __user *)arg; |
218 | drm_ctx_priv_map_t request; | |
219 | drm_map_t *map; | |
d1f2b55a | 220 | drm_map_list_t *_entry; |
1da177e4 LT |
221 | |
222 | if (copy_from_user(&request, argp, sizeof(request))) | |
223 | return -EFAULT; | |
224 | ||
30e2fb18 | 225 | mutex_lock(&dev->struct_mutex); |
b5e89ed5 DA |
226 | if (dev->max_context < 0 |
227 | || request.ctx_id >= (unsigned)dev->max_context) { | |
30e2fb18 | 228 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
229 | return -EINVAL; |
230 | } | |
231 | ||
232 | map = dev->context_sareas[request.ctx_id]; | |
30e2fb18 | 233 | mutex_unlock(&dev->struct_mutex); |
1da177e4 | 234 | |
b3a83639 | 235 | request.handle = NULL; |
bd1b331f | 236 | list_for_each_entry(_entry, &dev->maplist, head) { |
d1f2b55a | 237 | if (_entry->map == map) { |
b5e89ed5 DA |
238 | request.handle = |
239 | (void *)(unsigned long)_entry->user_token; | |
d1f2b55a DA |
240 | break; |
241 | } | |
242 | } | |
b3a83639 | 243 | if (request.handle == NULL) |
d1f2b55a DA |
244 | return -EINVAL; |
245 | ||
1da177e4 LT |
246 | if (copy_to_user(argp, &request, sizeof(request))) |
247 | return -EFAULT; | |
248 | return 0; | |
249 | } | |
250 | ||
251 | /** | |
252 | * Set per-context SAREA. | |
b5e89ed5 | 253 | * |
1da177e4 LT |
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. | |
259 | * | |
260 | * Searches the mapping specified in \p arg and update the entry in | |
261 | * drm_device::context_sareas with it. | |
262 | */ | |
263 | int drm_setsareactx(struct inode *inode, struct file *filp, | |
b5e89ed5 | 264 | unsigned int cmd, unsigned long arg) |
1da177e4 | 265 | { |
b5e89ed5 DA |
266 | drm_file_t *priv = filp->private_data; |
267 | drm_device_t *dev = priv->head->dev; | |
1da177e4 LT |
268 | drm_ctx_priv_map_t request; |
269 | drm_map_t *map = NULL; | |
270 | drm_map_list_t *r_list = NULL; | |
1da177e4 LT |
271 | |
272 | if (copy_from_user(&request, | |
b5e89ed5 | 273 | (drm_ctx_priv_map_t __user *) arg, sizeof(request))) |
1da177e4 LT |
274 | return -EFAULT; |
275 | ||
30e2fb18 | 276 | mutex_lock(&dev->struct_mutex); |
bd1b331f | 277 | list_for_each_entry(r_list, &dev->maplist, head) { |
9a186645 | 278 | if (r_list->map |
b5e89ed5 | 279 | && r_list->user_token == (unsigned long)request.handle) |
1da177e4 LT |
280 | goto found; |
281 | } | |
b5e89ed5 | 282 | bad: |
30e2fb18 | 283 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
284 | return -EINVAL; |
285 | ||
b5e89ed5 | 286 | found: |
1da177e4 | 287 | map = r_list->map; |
b5e89ed5 DA |
288 | if (!map) |
289 | goto bad; | |
1da177e4 LT |
290 | if (dev->max_context < 0) |
291 | goto bad; | |
b5e89ed5 | 292 | if (request.ctx_id >= (unsigned)dev->max_context) |
1da177e4 LT |
293 | goto bad; |
294 | dev->context_sareas[request.ctx_id] = map; | |
30e2fb18 | 295 | mutex_unlock(&dev->struct_mutex); |
1da177e4 LT |
296 | return 0; |
297 | } | |
298 | ||
299 | /*@}*/ | |
300 | ||
301 | /******************************************************************/ | |
302 | /** \name The actual DRM context handling routines */ | |
303 | /*@{*/ | |
304 | ||
305 | /** | |
306 | * Switch context. | |
307 | * | |
308 | * \param dev DRM device. | |
309 | * \param old old context handle. | |
310 | * \param new new context handle. | |
311 | * \return zero on success or a negative number on failure. | |
312 | * | |
313 | * Attempt to set drm_device::context_flag. | |
314 | */ | |
b5e89ed5 | 315 | static int drm_context_switch(drm_device_t * dev, int old, int new) |
1da177e4 | 316 | { |
b5e89ed5 DA |
317 | if (test_and_set_bit(0, &dev->context_flag)) { |
318 | DRM_ERROR("Reentering -- FIXME\n"); | |
319 | return -EBUSY; | |
320 | } | |
1da177e4 | 321 | |
b5e89ed5 | 322 | DRM_DEBUG("Context switch from %d to %d\n", old, new); |
1da177e4 | 323 | |
b5e89ed5 DA |
324 | if (new == dev->last_context) { |
325 | clear_bit(0, &dev->context_flag); | |
326 | return 0; | |
327 | } | |
1da177e4 | 328 | |
b5e89ed5 | 329 | return 0; |
1da177e4 LT |
330 | } |
331 | ||
332 | /** | |
333 | * Complete context switch. | |
334 | * | |
335 | * \param dev DRM device. | |
336 | * \param new new context handle. | |
337 | * \return zero on success or a negative number on failure. | |
338 | * | |
339 | * Updates drm_device::last_context and drm_device::last_switch. Verifies the | |
340 | * hardware lock is held, clears the drm_device::context_flag and wakes up | |
341 | * drm_device::context_wait. | |
342 | */ | |
b5e89ed5 | 343 | static int drm_context_switch_complete(drm_device_t * dev, int new) |
1da177e4 | 344 | { |
b5e89ed5 DA |
345 | dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ |
346 | dev->last_switch = jiffies; | |
1da177e4 | 347 | |
b5e89ed5 DA |
348 | if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) { |
349 | DRM_ERROR("Lock isn't held after context switch\n"); | |
350 | } | |
1da177e4 | 351 | |
b5e89ed5 DA |
352 | /* If a context switch is ever initiated |
353 | when the kernel holds the lock, release | |
354 | that lock here. */ | |
355 | clear_bit(0, &dev->context_flag); | |
356 | wake_up(&dev->context_wait); | |
1da177e4 | 357 | |
b5e89ed5 | 358 | return 0; |
1da177e4 LT |
359 | } |
360 | ||
361 | /** | |
362 | * Reserve contexts. | |
363 | * | |
364 | * \param inode device inode. | |
365 | * \param filp file pointer. | |
366 | * \param cmd command. | |
367 | * \param arg user argument pointing to a drm_ctx_res structure. | |
368 | * \return zero on success or a negative number on failure. | |
369 | */ | |
b5e89ed5 DA |
370 | int drm_resctx(struct inode *inode, struct file *filp, |
371 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
372 | { |
373 | drm_ctx_res_t res; | |
374 | drm_ctx_t __user *argp = (void __user *)arg; | |
375 | drm_ctx_t ctx; | |
376 | int i; | |
377 | ||
b5e89ed5 | 378 | if (copy_from_user(&res, argp, sizeof(res))) |
1da177e4 LT |
379 | return -EFAULT; |
380 | ||
b5e89ed5 DA |
381 | if (res.count >= DRM_RESERVED_CONTEXTS) { |
382 | memset(&ctx, 0, sizeof(ctx)); | |
383 | for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { | |
1da177e4 | 384 | ctx.handle = i; |
b5e89ed5 | 385 | if (copy_to_user(&res.contexts[i], &ctx, sizeof(ctx))) |
1da177e4 LT |
386 | return -EFAULT; |
387 | } | |
388 | } | |
389 | res.count = DRM_RESERVED_CONTEXTS; | |
390 | ||
b5e89ed5 | 391 | if (copy_to_user(argp, &res, sizeof(res))) |
1da177e4 LT |
392 | return -EFAULT; |
393 | return 0; | |
394 | } | |
395 | ||
396 | /** | |
397 | * Add context. | |
398 | * | |
399 | * \param inode device inode. | |
400 | * \param filp file pointer. | |
401 | * \param cmd command. | |
402 | * \param arg user argument pointing to a drm_ctx structure. | |
403 | * \return zero on success or a negative number on failure. | |
404 | * | |
405 | * Get a new handle for the context and copy to userspace. | |
406 | */ | |
b5e89ed5 DA |
407 | int drm_addctx(struct inode *inode, struct file *filp, |
408 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
409 | { |
410 | drm_file_t *priv = filp->private_data; | |
411 | drm_device_t *dev = priv->head->dev; | |
b5e89ed5 | 412 | drm_ctx_list_t *ctx_entry; |
1da177e4 LT |
413 | drm_ctx_t __user *argp = (void __user *)arg; |
414 | drm_ctx_t ctx; | |
415 | ||
b5e89ed5 | 416 | if (copy_from_user(&ctx, argp, sizeof(ctx))) |
1da177e4 LT |
417 | return -EFAULT; |
418 | ||
b5e89ed5 DA |
419 | ctx.handle = drm_ctxbitmap_next(dev); |
420 | if (ctx.handle == DRM_KERNEL_CONTEXT) { | |
421 | /* Skip kernel's context and get a new one. */ | |
422 | ctx.handle = drm_ctxbitmap_next(dev); | |
1da177e4 | 423 | } |
b5e89ed5 DA |
424 | DRM_DEBUG("%d\n", ctx.handle); |
425 | if (ctx.handle == -1) { | |
426 | DRM_DEBUG("Not enough free contexts.\n"); | |
427 | /* Should this return -EBUSY instead? */ | |
1da177e4 LT |
428 | return -ENOMEM; |
429 | } | |
430 | ||
b5e89ed5 | 431 | if (ctx.handle != DRM_KERNEL_CONTEXT) { |
1da177e4 | 432 | if (dev->driver->context_ctor) |
1e7d5190 | 433 | if (!dev->driver->context_ctor(dev, ctx.handle)) { |
b5e9fc13 DA |
434 | DRM_DEBUG("Running out of ctxs or memory.\n"); |
435 | return -ENOMEM; | |
436 | } | |
1da177e4 LT |
437 | } |
438 | ||
b5e89ed5 DA |
439 | ctx_entry = drm_alloc(sizeof(*ctx_entry), DRM_MEM_CTXLIST); |
440 | if (!ctx_entry) { | |
1da177e4 LT |
441 | DRM_DEBUG("out of memory\n"); |
442 | return -ENOMEM; | |
443 | } | |
444 | ||
b5e89ed5 | 445 | INIT_LIST_HEAD(&ctx_entry->head); |
1da177e4 LT |
446 | ctx_entry->handle = ctx.handle; |
447 | ctx_entry->tag = priv; | |
448 | ||
30e2fb18 | 449 | mutex_lock(&dev->ctxlist_mutex); |
bd1b331f | 450 | list_add(&ctx_entry->head, &dev->ctxlist); |
1da177e4 | 451 | ++dev->ctx_count; |
30e2fb18 | 452 | mutex_unlock(&dev->ctxlist_mutex); |
1da177e4 | 453 | |
b5e89ed5 | 454 | if (copy_to_user(argp, &ctx, sizeof(ctx))) |
1da177e4 LT |
455 | return -EFAULT; |
456 | return 0; | |
457 | } | |
458 | ||
b5e89ed5 DA |
459 | int drm_modctx(struct inode *inode, struct file *filp, |
460 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
461 | { |
462 | /* This does nothing */ | |
463 | return 0; | |
464 | } | |
465 | ||
466 | /** | |
467 | * Get context. | |
468 | * | |
469 | * \param inode device inode. | |
470 | * \param filp file pointer. | |
471 | * \param cmd command. | |
472 | * \param arg user argument pointing to a drm_ctx structure. | |
473 | * \return zero on success or a negative number on failure. | |
474 | */ | |
b5e89ed5 DA |
475 | int drm_getctx(struct inode *inode, struct file *filp, |
476 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
477 | { |
478 | drm_ctx_t __user *argp = (void __user *)arg; | |
479 | drm_ctx_t ctx; | |
480 | ||
b5e89ed5 | 481 | if (copy_from_user(&ctx, argp, sizeof(ctx))) |
1da177e4 LT |
482 | return -EFAULT; |
483 | ||
484 | /* This is 0, because we don't handle any context flags */ | |
485 | ctx.flags = 0; | |
486 | ||
b5e89ed5 | 487 | if (copy_to_user(argp, &ctx, sizeof(ctx))) |
1da177e4 LT |
488 | return -EFAULT; |
489 | return 0; | |
490 | } | |
491 | ||
492 | /** | |
493 | * Switch context. | |
494 | * | |
495 | * \param inode device inode. | |
496 | * \param filp file pointer. | |
497 | * \param cmd command. | |
498 | * \param arg user argument pointing to a drm_ctx structure. | |
499 | * \return zero on success or a negative number on failure. | |
500 | * | |
501 | * Calls context_switch(). | |
502 | */ | |
b5e89ed5 DA |
503 | int drm_switchctx(struct inode *inode, struct file *filp, |
504 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
505 | { |
506 | drm_file_t *priv = filp->private_data; | |
507 | drm_device_t *dev = priv->head->dev; | |
508 | drm_ctx_t ctx; | |
509 | ||
b5e89ed5 | 510 | if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx))) |
1da177e4 LT |
511 | return -EFAULT; |
512 | ||
b5e89ed5 DA |
513 | DRM_DEBUG("%d\n", ctx.handle); |
514 | return drm_context_switch(dev, dev->last_context, ctx.handle); | |
1da177e4 LT |
515 | } |
516 | ||
517 | /** | |
518 | * New context. | |
519 | * | |
520 | * \param inode device inode. | |
521 | * \param filp file pointer. | |
522 | * \param cmd command. | |
523 | * \param arg user argument pointing to a drm_ctx structure. | |
524 | * \return zero on success or a negative number on failure. | |
525 | * | |
526 | * Calls context_switch_complete(). | |
527 | */ | |
b5e89ed5 DA |
528 | int drm_newctx(struct inode *inode, struct file *filp, |
529 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
530 | { |
531 | drm_file_t *priv = filp->private_data; | |
532 | drm_device_t *dev = priv->head->dev; | |
533 | drm_ctx_t ctx; | |
534 | ||
b5e89ed5 | 535 | if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx))) |
1da177e4 LT |
536 | return -EFAULT; |
537 | ||
b5e89ed5 DA |
538 | DRM_DEBUG("%d\n", ctx.handle); |
539 | drm_context_switch_complete(dev, ctx.handle); | |
1da177e4 LT |
540 | |
541 | return 0; | |
542 | } | |
543 | ||
544 | /** | |
545 | * Remove context. | |
546 | * | |
547 | * \param inode device inode. | |
548 | * \param filp file pointer. | |
549 | * \param cmd command. | |
550 | * \param arg user argument pointing to a drm_ctx structure. | |
551 | * \return zero on success or a negative number on failure. | |
552 | * | |
553 | * If not the special kernel context, calls ctxbitmap_free() to free the specified context. | |
554 | */ | |
b5e89ed5 DA |
555 | int drm_rmctx(struct inode *inode, struct file *filp, |
556 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
557 | { |
558 | drm_file_t *priv = filp->private_data; | |
559 | drm_device_t *dev = priv->head->dev; | |
560 | drm_ctx_t ctx; | |
561 | ||
b5e89ed5 | 562 | if (copy_from_user(&ctx, (drm_ctx_t __user *) arg, sizeof(ctx))) |
1da177e4 LT |
563 | return -EFAULT; |
564 | ||
b5e89ed5 DA |
565 | DRM_DEBUG("%d\n", ctx.handle); |
566 | if (ctx.handle == DRM_KERNEL_CONTEXT + 1) { | |
1da177e4 LT |
567 | priv->remove_auth_on_close = 1; |
568 | } | |
b5e89ed5 | 569 | if (ctx.handle != DRM_KERNEL_CONTEXT) { |
1da177e4 LT |
570 | if (dev->driver->context_dtor) |
571 | dev->driver->context_dtor(dev, ctx.handle); | |
b5e89ed5 | 572 | drm_ctxbitmap_free(dev, ctx.handle); |
1da177e4 LT |
573 | } |
574 | ||
30e2fb18 | 575 | mutex_lock(&dev->ctxlist_mutex); |
bd1b331f | 576 | if (!list_empty(&dev->ctxlist)) { |
1da177e4 LT |
577 | drm_ctx_list_t *pos, *n; |
578 | ||
bd1b331f | 579 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
b5e89ed5 DA |
580 | if (pos->handle == ctx.handle) { |
581 | list_del(&pos->head); | |
582 | drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST); | |
1da177e4 LT |
583 | --dev->ctx_count; |
584 | } | |
585 | } | |
586 | } | |
30e2fb18 | 587 | mutex_unlock(&dev->ctxlist_mutex); |
1da177e4 LT |
588 | |
589 | return 0; | |
590 | } | |
591 | ||
592 | /*@}*/ |