blob: 45adf97e678f9ea887fa46e51eb64c1c0dd93bbc [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
David Howells760285e2012-10-02 18:01:07 +010043#include <drm/drmP.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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 Airlie62968142007-07-17 10:46:52 +100056 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * lock.
58 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100059void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Dave Airlie62968142007-07-17 10:46:52 +100061 mutex_lock(&dev->struct_mutex);
62 idr_remove(&dev->ctx_idr, ctx_handle);
63 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * Context bitmap allocation.
68 *
69 * \param dev DRM device.
70 * \return (non-negative) context handle on success or a negative number on failure.
71 *
Dave Airlie62968142007-07-17 10:46:52 +100072 * Allocate a new idr from drm_device::ctx_idr while holding the
Dave Airlie30e2fb12006-02-02 19:37:46 +110073 * drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100075static int drm_ctxbitmap_next(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Dave Airlie62968142007-07-17 10:46:52 +100077 int new_id;
78 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Dave Airlie62968142007-07-17 10:46:52 +100080again:
81 if (idr_pre_get(&dev->ctx_idr, GFP_KERNEL) == 0) {
82 DRM_ERROR("Out of memory expanding drawable idr\n");
83 return -ENOMEM;
84 }
Dave Airlie30e2fb12006-02-02 19:37:46 +110085 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +100086 ret = idr_get_new_above(&dev->ctx_idr, NULL,
87 DRM_RESERVED_CONTEXTS, &new_id);
Dave Airlie30e2fb12006-02-02 19:37:46 +110088 mutex_unlock(&dev->struct_mutex);
Ville Syrjäläf1ae1262012-03-15 19:58:31 +020089 if (ret == -EAGAIN)
90 goto again;
91 else if (ret)
92 return ret;
93
Dave Airlie62968142007-07-17 10:46:52 +100094 return new_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/**
98 * Context bitmap initialization.
99 *
100 * \param dev DRM device.
101 *
Dave Airlie62968142007-07-17 10:46:52 +1000102 * Initialise the drm_device::ctx_idr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000104int drm_ctxbitmap_init(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Dave Airlie62968142007-07-17 10:46:52 +1000106 idr_init(&dev->ctx_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
110/**
111 * Context bitmap cleanup.
112 *
113 * \param dev DRM device.
114 *
Dave Airlie62968142007-07-17 10:46:52 +1000115 * Free all idr members using drm_ctx_sarea_free helper function
116 * while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000118void drm_ctxbitmap_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Dave Airlie30e2fb12006-02-02 19:37:46 +1100120 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000121 idr_remove_all(&dev->ctx_idr);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100122 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125/*@}*/
126
127/******************************************************************/
128/** \name Per Context SAREA Support */
129/*@{*/
130
131/**
132 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000135 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * \param cmd command.
137 * \param arg user argument pointing to a drm_ctx_priv_map structure.
138 * \return zero on success or a negative number on failure.
139 *
Dave Airlie62968142007-07-17 10:46:52 +1000140 * Gets the map from drm_device::ctx_idr with the handle specified and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * returns its handle.
142 */
Eric Anholtc153f452007-09-03 12:06:45 +1000143int drm_getsareactx(struct drm_device *dev, void *data,
144 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Eric Anholtc153f452007-09-03 12:06:45 +1000146 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100147 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000148 struct drm_map_list *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Dave Airlie30e2fb12006-02-02 19:37:46 +1100150 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000151
Eric Anholtc153f452007-09-03 12:06:45 +1000152 map = idr_find(&dev->ctx_idr, request->ctx_id);
Dave Airlie62968142007-07-17 10:46:52 +1000153 if (!map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100154 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -EINVAL;
156 }
157
Eric Anholtc153f452007-09-03 12:06:45 +1000158 request->handle = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000159 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000160 if (_entry->map == map) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000161 request->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000163 break;
164 }
165 }
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400166
167 mutex_unlock(&dev->struct_mutex);
168
Eric Anholtc153f452007-09-03 12:06:45 +1000169 if (request->handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000170 return -EINVAL;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173}
174
175/**
176 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000179 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * \param cmd command.
181 * \param arg user argument pointing to a drm_ctx_priv_map structure.
182 * \return zero on success or a negative number on failure.
183 *
184 * Searches the mapping specified in \p arg and update the entry in
Dave Airlie62968142007-07-17 10:46:52 +1000185 * drm_device::ctx_idr with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Eric Anholtc153f452007-09-03 12:06:45 +1000187int drm_setsareactx(struct drm_device *dev, void *data,
188 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Eric Anholtc153f452007-09-03 12:06:45 +1000190 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100191 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000192 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dave Airlie30e2fb12006-02-02 19:37:46 +1100194 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000195 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie9a186642005-06-23 21:29:18 +1000196 if (r_list->map
Eric Anholtc153f452007-09-03 12:06:45 +1000197 && r_list->user_token == (unsigned long) request->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto found;
199 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100201 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -EINVAL;
203
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 if (!map)
207 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000208
Eric Anholtc153f452007-09-03 12:06:45 +1000209 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000211
Dave Airlie30e2fb12006-02-02 19:37:46 +1100212 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215}
216
217/*@}*/
218
219/******************************************************************/
220/** \name The actual DRM context handling routines */
221/*@{*/
222
223/**
224 * Switch context.
225 *
226 * \param dev DRM device.
227 * \param old old context handle.
228 * \param new new context handle.
229 * \return zero on success or a negative number on failure.
230 *
231 * Attempt to set drm_device::context_flag.
232 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000233static int drm_context_switch(struct drm_device * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235 if (test_and_set_bit(0, &dev->context_flag)) {
236 DRM_ERROR("Reentering -- FIXME\n");
237 return -EBUSY;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000240 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 if (new == dev->last_context) {
243 clear_bit(0, &dev->context_flag);
244 return 0;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/**
251 * Complete context switch.
252 *
253 * \param dev DRM device.
254 * \param new new context handle.
255 * \return zero on success or a negative number on failure.
256 *
257 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
258 * hardware lock is held, clears the drm_device::context_flag and wakes up
259 * drm_device::context_wait.
260 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000261static int drm_context_switch_complete(struct drm_device *dev,
262 struct drm_file *file_priv, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000264 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
265 dev->last_switch = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dave Airlie7c1c2872008-11-28 14:22:24 +1000267 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 DRM_ERROR("Lock isn't held after context switch\n");
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271 /* If a context switch is ever initiated
272 when the kernel holds the lock, release
273 that lock here. */
274 clear_bit(0, &dev->context_flag);
275 wake_up(&dev->context_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
281 * Reserve contexts.
282 *
283 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000284 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * \param cmd command.
286 * \param arg user argument pointing to a drm_ctx_res structure.
287 * \return zero on success or a negative number on failure.
288 */
Eric Anholtc153f452007-09-03 12:06:45 +1000289int drm_resctx(struct drm_device *dev, void *data,
290 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Eric Anholtc153f452007-09-03 12:06:45 +1000292 struct drm_ctx_res *res = data;
Dave Airliec60ce622007-07-11 15:27:12 +1000293 struct drm_ctx ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int i;
295
Eric Anholtc153f452007-09-03 12:06:45 +1000296 if (res->count >= DRM_RESERVED_CONTEXTS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 memset(&ctx, 0, sizeof(ctx));
298 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ctx.handle = i;
Eric Anholtc153f452007-09-03 12:06:45 +1000300 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -EFAULT;
302 }
303 }
Eric Anholtc153f452007-09-03 12:06:45 +1000304 res->count = DRM_RESERVED_CONTEXTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
307}
308
309/**
310 * Add context.
311 *
312 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000313 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * \param cmd command.
315 * \param arg user argument pointing to a drm_ctx structure.
316 * \return zero on success or a negative number on failure.
317 *
318 * Get a new handle for the context and copy to userspace.
319 */
Eric Anholtc153f452007-09-03 12:06:45 +1000320int drm_addctx(struct drm_device *dev, void *data,
321 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Dave Airlie55910512007-07-11 16:53:40 +1000323 struct drm_ctx_list *ctx_entry;
Eric Anholtc153f452007-09-03 12:06:45 +1000324 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Eric Anholtc153f452007-09-03 12:06:45 +1000326 ctx->handle = drm_ctxbitmap_next(dev);
327 if (ctx->handle == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 /* Skip kernel's context and get a new one. */
Eric Anholtc153f452007-09-03 12:06:45 +1000329 ctx->handle = drm_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Eric Anholtc153f452007-09-03 12:06:45 +1000331 DRM_DEBUG("%d\n", ctx->handle);
332 if (ctx->handle == -1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 DRM_DEBUG("Not enough free contexts.\n");
334 /* Should this return -EBUSY instead? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -ENOMEM;
336 }
337
Eric Anholt9a298b22009-03-24 12:23:04 -0700338 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 DRM_DEBUG("out of memory\n");
341 return -ENOMEM;
342 }
343
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 INIT_LIST_HEAD(&ctx_entry->head);
Eric Anholtc153f452007-09-03 12:06:45 +1000345 ctx_entry->handle = ctx->handle;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000346 ctx_entry->tag = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Dave Airlie30e2fb12006-02-02 19:37:46 +1100348 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000349 list_add(&ctx_entry->head, &dev->ctxlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ++dev->ctx_count;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100351 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355
Eric Anholtc153f452007-09-03 12:06:45 +1000356int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 /* This does nothing */
359 return 0;
360}
361
362/**
363 * Get context.
364 *
365 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000366 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * \param cmd command.
368 * \param arg user argument pointing to a drm_ctx structure.
369 * \return zero on success or a negative number on failure.
370 */
Eric Anholtc153f452007-09-03 12:06:45 +1000371int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Eric Anholtc153f452007-09-03 12:06:45 +1000373 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* This is 0, because we don't handle any context flags */
Eric Anholtc153f452007-09-03 12:06:45 +1000376 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379}
380
381/**
382 * Switch context.
383 *
384 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000385 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * \param cmd command.
387 * \param arg user argument pointing to a drm_ctx structure.
388 * \return zero on success or a negative number on failure.
389 *
390 * Calls context_switch().
391 */
Eric Anholtc153f452007-09-03 12:06:45 +1000392int drm_switchctx(struct drm_device *dev, void *data,
393 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Eric Anholtc153f452007-09-03 12:06:45 +1000395 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Eric Anholtc153f452007-09-03 12:06:45 +1000397 DRM_DEBUG("%d\n", ctx->handle);
398 return drm_context_switch(dev, dev->last_context, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/**
402 * New context.
403 *
404 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000405 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * \param cmd command.
407 * \param arg user argument pointing to a drm_ctx structure.
408 * \return zero on success or a negative number on failure.
409 *
410 * Calls context_switch_complete().
411 */
Eric Anholtc153f452007-09-03 12:06:45 +1000412int drm_newctx(struct drm_device *dev, void *data,
413 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Eric Anholtc153f452007-09-03 12:06:45 +1000415 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Eric Anholtc153f452007-09-03 12:06:45 +1000417 DRM_DEBUG("%d\n", ctx->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000418 drm_context_switch_complete(dev, file_priv, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 return 0;
421}
422
423/**
424 * Remove context.
425 *
426 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000427 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * \param cmd command.
429 * \param arg user argument pointing to a drm_ctx structure.
430 * \return zero on success or a negative number on failure.
431 *
432 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
433 */
Eric Anholtc153f452007-09-03 12:06:45 +1000434int drm_rmctx(struct drm_device *dev, void *data,
435 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Eric Anholtc153f452007-09-03 12:06:45 +1000437 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Eric Anholtc153f452007-09-03 12:06:45 +1000439 DRM_DEBUG("%d\n", ctx->handle);
Eric Anholtc153f452007-09-03 12:06:45 +1000440 if (ctx->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (dev->driver->context_dtor)
Eric Anholtc153f452007-09-03 12:06:45 +1000442 dev->driver->context_dtor(dev, ctx->handle);
443 drm_ctxbitmap_free(dev, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Dave Airlie30e2fb12006-02-02 19:37:46 +1100446 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000447 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000448 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dave Airliebd1b3312007-05-26 05:01:51 +1000450 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000451 if (pos->handle == ctx->handle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 list_del(&pos->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700453 kfree(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 --dev->ctx_count;
455 }
456 }
457 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100458 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 return 0;
461}
462
463/*@}*/