blob: 325365f6d355c3c12bf2386bb354bd095d873061 [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 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);
88 if (ret == -EAGAIN) {
Dave Airlie30e2fb12006-02-02 19:37:46 +110089 mutex_unlock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +100090 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Dave Airlie30e2fb12006-02-02 19:37:46 +110092 mutex_unlock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +100093 return new_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96/**
97 * Context bitmap initialization.
98 *
99 * \param dev DRM device.
100 *
Dave Airlie62968142007-07-17 10:46:52 +1000101 * Initialise the drm_device::ctx_idr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000103int drm_ctxbitmap_init(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Dave Airlie62968142007-07-17 10:46:52 +1000105 idr_init(&dev->ctx_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107}
108
109/**
110 * Context bitmap cleanup.
111 *
112 * \param dev DRM device.
113 *
Dave Airlie62968142007-07-17 10:46:52 +1000114 * Free all idr members using drm_ctx_sarea_free helper function
115 * while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000117void drm_ctxbitmap_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Dave Airlie30e2fb12006-02-02 19:37:46 +1100119 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000120 idr_remove_all(&dev->ctx_idr);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100121 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/*@}*/
125
126/******************************************************************/
127/** \name Per Context SAREA Support */
128/*@{*/
129
130/**
131 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000134 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * \param cmd command.
136 * \param arg user argument pointing to a drm_ctx_priv_map structure.
137 * \return zero on success or a negative number on failure.
138 *
Dave Airlie62968142007-07-17 10:46:52 +1000139 * Gets the map from drm_device::ctx_idr with the handle specified and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * returns its handle.
141 */
Eric Anholtc153f452007-09-03 12:06:45 +1000142int drm_getsareactx(struct drm_device *dev, void *data,
143 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Eric Anholtc153f452007-09-03 12:06:45 +1000145 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100146 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000147 struct drm_map_list *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dave Airlie30e2fb12006-02-02 19:37:46 +1100149 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000150
Eric Anholtc153f452007-09-03 12:06:45 +1000151 map = idr_find(&dev->ctx_idr, request->ctx_id);
Dave Airlie62968142007-07-17 10:46:52 +1000152 if (!map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100153 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -EINVAL;
155 }
156
Eric Anholtc153f452007-09-03 12:06:45 +1000157 request->handle = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000158 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000159 if (_entry->map == map) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000160 request->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000161 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000162 break;
163 }
164 }
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400165
166 mutex_unlock(&dev->struct_mutex);
167
Eric Anholtc153f452007-09-03 12:06:45 +1000168 if (request->handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000169 return -EINVAL;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
174/**
175 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000178 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * \param cmd command.
180 * \param arg user argument pointing to a drm_ctx_priv_map structure.
181 * \return zero on success or a negative number on failure.
182 *
183 * Searches the mapping specified in \p arg and update the entry in
Dave Airlie62968142007-07-17 10:46:52 +1000184 * drm_device::ctx_idr with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Eric Anholtc153f452007-09-03 12:06:45 +1000186int drm_setsareactx(struct drm_device *dev, void *data,
187 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Eric Anholtc153f452007-09-03 12:06:45 +1000189 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100190 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000191 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Dave Airlie30e2fb12006-02-02 19:37:46 +1100193 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000194 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie9a186642005-06-23 21:29:18 +1000195 if (r_list->map
Eric Anholtc153f452007-09-03 12:06:45 +1000196 && r_list->user_token == (unsigned long) request->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto found;
198 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100200 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -EINVAL;
202
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000203 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000205 if (!map)
206 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000207
Eric Anholtc153f452007-09-03 12:06:45 +1000208 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000210
Dave Airlie30e2fb12006-02-02 19:37:46 +1100211 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214}
215
216/*@}*/
217
218/******************************************************************/
219/** \name The actual DRM context handling routines */
220/*@{*/
221
222/**
223 * Switch context.
224 *
225 * \param dev DRM device.
226 * \param old old context handle.
227 * \param new new context handle.
228 * \return zero on success or a negative number on failure.
229 *
230 * Attempt to set drm_device::context_flag.
231 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000232static int drm_context_switch(struct drm_device * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234 if (test_and_set_bit(0, &dev->context_flag)) {
235 DRM_ERROR("Reentering -- FIXME\n");
236 return -EBUSY;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (new == dev->last_context) {
242 clear_bit(0, &dev->context_flag);
243 return 0;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/**
250 * Complete context switch.
251 *
252 * \param dev DRM device.
253 * \param new new context handle.
254 * \return zero on success or a negative number on failure.
255 *
256 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
257 * hardware lock is held, clears the drm_device::context_flag and wakes up
258 * drm_device::context_wait.
259 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000260static int drm_context_switch_complete(struct drm_device *dev,
261 struct drm_file *file_priv, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
264 dev->last_switch = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Dave Airlie7c1c2872008-11-28 14:22:24 +1000266 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267 DRM_ERROR("Lock isn't held after context switch\n");
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 /* If a context switch is ever initiated
271 when the kernel holds the lock, release
272 that lock here. */
273 clear_bit(0, &dev->context_flag);
274 wake_up(&dev->context_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/**
280 * Reserve contexts.
281 *
282 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000283 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * \param cmd command.
285 * \param arg user argument pointing to a drm_ctx_res structure.
286 * \return zero on success or a negative number on failure.
287 */
Eric Anholtc153f452007-09-03 12:06:45 +1000288int drm_resctx(struct drm_device *dev, void *data,
289 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Eric Anholtc153f452007-09-03 12:06:45 +1000291 struct drm_ctx_res *res = data;
Dave Airliec60ce622007-07-11 15:27:12 +1000292 struct drm_ctx ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int i;
294
Eric Anholtc153f452007-09-03 12:06:45 +1000295 if (res->count >= DRM_RESERVED_CONTEXTS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 memset(&ctx, 0, sizeof(ctx));
297 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ctx.handle = i;
Eric Anholtc153f452007-09-03 12:06:45 +1000299 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -EFAULT;
301 }
302 }
Eric Anholtc153f452007-09-03 12:06:45 +1000303 res->count = DRM_RESERVED_CONTEXTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306}
307
308/**
309 * Add context.
310 *
311 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000312 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * \param cmd command.
314 * \param arg user argument pointing to a drm_ctx structure.
315 * \return zero on success or a negative number on failure.
316 *
317 * Get a new handle for the context and copy to userspace.
318 */
Eric Anholtc153f452007-09-03 12:06:45 +1000319int drm_addctx(struct drm_device *dev, void *data,
320 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Dave Airlie55910512007-07-11 16:53:40 +1000322 struct drm_ctx_list *ctx_entry;
Eric Anholtc153f452007-09-03 12:06:45 +1000323 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Eric Anholtc153f452007-09-03 12:06:45 +1000325 ctx->handle = drm_ctxbitmap_next(dev);
326 if (ctx->handle == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 /* Skip kernel's context and get a new one. */
Eric Anholtc153f452007-09-03 12:06:45 +1000328 ctx->handle = drm_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Eric Anholtc153f452007-09-03 12:06:45 +1000330 DRM_DEBUG("%d\n", ctx->handle);
331 if (ctx->handle == -1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 DRM_DEBUG("Not enough free contexts.\n");
333 /* Should this return -EBUSY instead? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -ENOMEM;
335 }
336
Eric Anholt9a298b22009-03-24 12:23:04 -0700337 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 DRM_DEBUG("out of memory\n");
340 return -ENOMEM;
341 }
342
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 INIT_LIST_HEAD(&ctx_entry->head);
Eric Anholtc153f452007-09-03 12:06:45 +1000344 ctx_entry->handle = ctx->handle;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000345 ctx_entry->tag = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Dave Airlie30e2fb12006-02-02 19:37:46 +1100347 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000348 list_add(&ctx_entry->head, &dev->ctxlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 ++dev->ctx_count;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100350 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353}
354
Eric Anholtc153f452007-09-03 12:06:45 +1000355int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 /* This does nothing */
358 return 0;
359}
360
361/**
362 * Get context.
363 *
364 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000365 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * \param cmd command.
367 * \param arg user argument pointing to a drm_ctx structure.
368 * \return zero on success or a negative number on failure.
369 */
Eric Anholtc153f452007-09-03 12:06:45 +1000370int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Eric Anholtc153f452007-09-03 12:06:45 +1000372 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* This is 0, because we don't handle any context flags */
Eric Anholtc153f452007-09-03 12:06:45 +1000375 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
378}
379
380/**
381 * Switch context.
382 *
383 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000384 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * \param cmd command.
386 * \param arg user argument pointing to a drm_ctx structure.
387 * \return zero on success or a negative number on failure.
388 *
389 * Calls context_switch().
390 */
Eric Anholtc153f452007-09-03 12:06:45 +1000391int drm_switchctx(struct drm_device *dev, void *data,
392 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Eric Anholtc153f452007-09-03 12:06:45 +1000394 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Eric Anholtc153f452007-09-03 12:06:45 +1000396 DRM_DEBUG("%d\n", ctx->handle);
397 return drm_context_switch(dev, dev->last_context, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/**
401 * New context.
402 *
403 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000404 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * \param cmd command.
406 * \param arg user argument pointing to a drm_ctx structure.
407 * \return zero on success or a negative number on failure.
408 *
409 * Calls context_switch_complete().
410 */
Eric Anholtc153f452007-09-03 12:06:45 +1000411int drm_newctx(struct drm_device *dev, void *data,
412 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Eric Anholtc153f452007-09-03 12:06:45 +1000414 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Eric Anholtc153f452007-09-03 12:06:45 +1000416 DRM_DEBUG("%d\n", ctx->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000417 drm_context_switch_complete(dev, file_priv, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 return 0;
420}
421
422/**
423 * Remove context.
424 *
425 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000426 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * \param cmd command.
428 * \param arg user argument pointing to a drm_ctx structure.
429 * \return zero on success or a negative number on failure.
430 *
431 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
432 */
Eric Anholtc153f452007-09-03 12:06:45 +1000433int drm_rmctx(struct drm_device *dev, void *data,
434 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Eric Anholtc153f452007-09-03 12:06:45 +1000436 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Eric Anholtc153f452007-09-03 12:06:45 +1000438 DRM_DEBUG("%d\n", ctx->handle);
Eric Anholtc153f452007-09-03 12:06:45 +1000439 if (ctx->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (dev->driver->context_dtor)
Eric Anholtc153f452007-09-03 12:06:45 +1000441 dev->driver->context_dtor(dev, ctx->handle);
442 drm_ctxbitmap_free(dev, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Dave Airlie30e2fb12006-02-02 19:37:46 +1100445 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000446 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000447 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Airliebd1b3312007-05-26 05:01:51 +1000449 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000450 if (pos->handle == ctx->handle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 list_del(&pos->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700452 kfree(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 --dev->ctx_count;
454 }
455 }
456 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100457 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 return 0;
460}
461
462/*@}*/