blob: b4fb86d89850a31c3cc9424f4b4148831c59922f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/**
46 * Free a handle from the context bitmap.
47 *
48 * \param dev DRM device.
49 * \param ctx_handle context handle.
50 *
51 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
Dave Airlie62968142007-07-17 10:46:52 +100052 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * lock.
54 */
Daniel Vetter7c510132013-08-08 15:41:21 +020055static void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Daniel Vetter7c510132013-08-08 15:41:21 +020057 if (drm_core_check_feature(dev, DRIVER_MODESET))
58 return;
59
Dave Airlie62968142007-07-17 10:46:52 +100060 mutex_lock(&dev->struct_mutex);
61 idr_remove(&dev->ctx_idr, ctx_handle);
62 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Daniel Vetter7c510132013-08-08 15:41:21 +020065/******************************************************************/
66/** \name Context bitmap support */
67/*@{*/
68
69void drm_legacy_ctxbitmap_release(struct drm_device *dev,
70 struct drm_file *file_priv)
71{
72 if (drm_core_check_feature(dev, DRIVER_MODESET))
73 return;
74
75 mutex_lock(&dev->ctxlist_mutex);
76 if (!list_empty(&dev->ctxlist)) {
77 struct drm_ctx_list *pos, *n;
78
79 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
80 if (pos->tag == file_priv &&
81 pos->handle != DRM_KERNEL_CONTEXT) {
82 if (dev->driver->context_dtor)
83 dev->driver->context_dtor(dev,
84 pos->handle);
85
86 drm_ctxbitmap_free(dev, pos->handle);
87
88 list_del(&pos->head);
89 kfree(pos);
90 --dev->ctx_count;
91 }
92 }
93 }
94 mutex_unlock(&dev->ctxlist_mutex);
95}
96
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * Context bitmap allocation.
99 *
100 * \param dev DRM device.
101 * \return (non-negative) context handle on success or a negative number on failure.
102 *
Dave Airlie62968142007-07-17 10:46:52 +1000103 * Allocate a new idr from drm_device::ctx_idr while holding the
Dave Airlie30e2fb12006-02-02 19:37:46 +1100104 * drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000106static int drm_ctxbitmap_next(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Dave Airlie62968142007-07-17 10:46:52 +1000108 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dave Airlie30e2fb12006-02-02 19:37:46 +1100110 mutex_lock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800111 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
112 GFP_KERNEL);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100113 mutex_unlock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800114 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/**
118 * Context bitmap initialization.
119 *
120 * \param dev DRM device.
121 *
Dave Airlie62968142007-07-17 10:46:52 +1000122 * Initialise the drm_device::ctx_idr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
Daniel Vetter7c510132013-08-08 15:41:21 +0200124void drm_legacy_ctxbitmap_init(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Daniel Vetter7c510132013-08-08 15:41:21 +0200126 if (drm_core_check_feature(dev, DRIVER_MODESET))
127 return;
128
Dave Airlie62968142007-07-17 10:46:52 +1000129 idr_init(&dev->ctx_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/**
133 * Context bitmap cleanup.
134 *
135 * \param dev DRM device.
136 *
Dave Airlie62968142007-07-17 10:46:52 +1000137 * Free all idr members using drm_ctx_sarea_free helper function
138 * while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
Daniel Vetter7c510132013-08-08 15:41:21 +0200140void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Dave Airlie30e2fb12006-02-02 19:37:46 +1100142 mutex_lock(&dev->struct_mutex);
Tejun Heo4d532332013-02-27 17:03:39 -0800143 idr_destroy(&dev->ctx_idr);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100144 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/*@}*/
148
149/******************************************************************/
150/** \name Per Context SAREA Support */
151/*@{*/
152
153/**
154 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000155 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000157 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * \param cmd command.
159 * \param arg user argument pointing to a drm_ctx_priv_map structure.
160 * \return zero on success or a negative number on failure.
161 *
Dave Airlie62968142007-07-17 10:46:52 +1000162 * Gets the map from drm_device::ctx_idr with the handle specified and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * returns its handle.
164 */
Eric Anholtc153f452007-09-03 12:06:45 +1000165int drm_getsareactx(struct drm_device *dev, void *data,
166 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Eric Anholtc153f452007-09-03 12:06:45 +1000168 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100169 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000170 struct drm_map_list *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Daniel Vetter7c510132013-08-08 15:41:21 +0200172 if (drm_core_check_feature(dev, DRIVER_MODESET))
173 return -EINVAL;
174
Dave Airlie30e2fb12006-02-02 19:37:46 +1100175 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000176
Eric Anholtc153f452007-09-03 12:06:45 +1000177 map = idr_find(&dev->ctx_idr, request->ctx_id);
Dave Airlie62968142007-07-17 10:46:52 +1000178 if (!map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100179 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EINVAL;
181 }
182
Eric Anholtc153f452007-09-03 12:06:45 +1000183 request->handle = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000184 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000185 if (_entry->map == map) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000186 request->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000187 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000188 break;
189 }
190 }
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400191
192 mutex_unlock(&dev->struct_mutex);
193
Eric Anholtc153f452007-09-03 12:06:45 +1000194 if (request->handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000195 return -EINVAL;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
198}
199
200/**
201 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000202 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000204 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * \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 * Searches the mapping specified in \p arg and update the entry in
Dave Airlie62968142007-07-17 10:46:52 +1000210 * drm_device::ctx_idr with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Eric Anholtc153f452007-09-03 12:06:45 +1000212int drm_setsareactx(struct drm_device *dev, void *data,
213 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Eric Anholtc153f452007-09-03 12:06:45 +1000215 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100216 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000217 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Daniel Vetter7c510132013-08-08 15:41:21 +0200219 if (drm_core_check_feature(dev, DRIVER_MODESET))
220 return -EINVAL;
221
Dave Airlie30e2fb12006-02-02 19:37:46 +1100222 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000223 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie9a186642005-06-23 21:29:18 +1000224 if (r_list->map
Eric Anholtc153f452007-09-03 12:06:45 +1000225 && r_list->user_token == (unsigned long) request->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 goto found;
227 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100229 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -EINVAL;
231
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234 if (!map)
235 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000236
Eric Anholtc153f452007-09-03 12:06:45 +1000237 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000239
Dave Airlie30e2fb12006-02-02 19:37:46 +1100240 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
245/*@}*/
246
247/******************************************************************/
248/** \name The actual DRM context handling routines */
249/*@{*/
250
251/**
252 * Switch context.
253 *
254 * \param dev DRM device.
255 * \param old old context handle.
256 * \param new new context handle.
257 * \return zero on success or a negative number on failure.
258 *
259 * Attempt to set drm_device::context_flag.
260 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000261static int drm_context_switch(struct drm_device * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263 if (test_and_set_bit(0, &dev->context_flag)) {
264 DRM_ERROR("Reentering -- FIXME\n");
265 return -EBUSY;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 if (new == dev->last_context) {
271 clear_bit(0, &dev->context_flag);
272 return 0;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
279 * Complete context switch.
280 *
281 * \param dev DRM device.
282 * \param new new context handle.
283 * \return zero on success or a negative number on failure.
284 *
285 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
286 * hardware lock is held, clears the drm_device::context_flag and wakes up
287 * drm_device::context_wait.
288 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000289static int drm_context_switch_complete(struct drm_device *dev,
290 struct drm_file *file_priv, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Dave Airlie7c1c2872008-11-28 14:22:24 +1000294 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295 DRM_ERROR("Lock isn't held after context switch\n");
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000298 /* If a context switch is ever initiated
299 when the kernel holds the lock, release
300 that lock here. */
301 clear_bit(0, &dev->context_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/**
307 * Reserve contexts.
308 *
309 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000310 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * \param cmd command.
312 * \param arg user argument pointing to a drm_ctx_res structure.
313 * \return zero on success or a negative number on failure.
314 */
Eric Anholtc153f452007-09-03 12:06:45 +1000315int drm_resctx(struct drm_device *dev, void *data,
316 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Eric Anholtc153f452007-09-03 12:06:45 +1000318 struct drm_ctx_res *res = data;
Dave Airliec60ce622007-07-11 15:27:12 +1000319 struct drm_ctx ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int i;
321
Daniel Vetter7c510132013-08-08 15:41:21 +0200322 if (drm_core_check_feature(dev, DRIVER_MODESET))
323 return -EINVAL;
324
Eric Anholtc153f452007-09-03 12:06:45 +1000325 if (res->count >= DRM_RESERVED_CONTEXTS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 memset(&ctx, 0, sizeof(ctx));
327 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 ctx.handle = i;
Eric Anholtc153f452007-09-03 12:06:45 +1000329 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EFAULT;
331 }
332 }
Eric Anholtc153f452007-09-03 12:06:45 +1000333 res->count = DRM_RESERVED_CONTEXTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
336}
337
338/**
339 * Add context.
340 *
341 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000342 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * \param cmd command.
344 * \param arg user argument pointing to a drm_ctx structure.
345 * \return zero on success or a negative number on failure.
346 *
347 * Get a new handle for the context and copy to userspace.
348 */
Eric Anholtc153f452007-09-03 12:06:45 +1000349int drm_addctx(struct drm_device *dev, void *data,
350 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Dave Airlie55910512007-07-11 16:53:40 +1000352 struct drm_ctx_list *ctx_entry;
Eric Anholtc153f452007-09-03 12:06:45 +1000353 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Daniel Vetter7c510132013-08-08 15:41:21 +0200355 if (drm_core_check_feature(dev, DRIVER_MODESET))
356 return -EINVAL;
357
Eric Anholtc153f452007-09-03 12:06:45 +1000358 ctx->handle = drm_ctxbitmap_next(dev);
359 if (ctx->handle == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000360 /* Skip kernel's context and get a new one. */
Eric Anholtc153f452007-09-03 12:06:45 +1000361 ctx->handle = drm_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Eric Anholtc153f452007-09-03 12:06:45 +1000363 DRM_DEBUG("%d\n", ctx->handle);
364 if (ctx->handle == -1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 DRM_DEBUG("Not enough free contexts.\n");
366 /* Should this return -EBUSY instead? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -ENOMEM;
368 }
369
Eric Anholt9a298b22009-03-24 12:23:04 -0700370 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 DRM_DEBUG("out of memory\n");
373 return -ENOMEM;
374 }
375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 INIT_LIST_HEAD(&ctx_entry->head);
Eric Anholtc153f452007-09-03 12:06:45 +1000377 ctx_entry->handle = ctx->handle;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000378 ctx_entry->tag = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Airlie30e2fb12006-02-02 19:37:46 +1100380 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000381 list_add(&ctx_entry->head, &dev->ctxlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ++dev->ctx_count;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100383 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/**
389 * Get context.
390 *
391 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000392 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * \param cmd command.
394 * \param arg user argument pointing to a drm_ctx structure.
395 * \return zero on success or a negative number on failure.
396 */
Eric Anholtc153f452007-09-03 12:06:45 +1000397int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Eric Anholtc153f452007-09-03 12:06:45 +1000399 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Daniel Vetter7c510132013-08-08 15:41:21 +0200401 if (drm_core_check_feature(dev, DRIVER_MODESET))
402 return -EINVAL;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* This is 0, because we don't handle any context flags */
Eric Anholtc153f452007-09-03 12:06:45 +1000405 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408}
409
410/**
411 * Switch context.
412 *
413 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000414 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * \param cmd command.
416 * \param arg user argument pointing to a drm_ctx structure.
417 * \return zero on success or a negative number on failure.
418 *
419 * Calls context_switch().
420 */
Eric Anholtc153f452007-09-03 12:06:45 +1000421int drm_switchctx(struct drm_device *dev, void *data,
422 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Eric Anholtc153f452007-09-03 12:06:45 +1000424 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Daniel Vetter7c510132013-08-08 15:41:21 +0200426 if (drm_core_check_feature(dev, DRIVER_MODESET))
427 return -EINVAL;
428
Eric Anholtc153f452007-09-03 12:06:45 +1000429 DRM_DEBUG("%d\n", ctx->handle);
430 return drm_context_switch(dev, dev->last_context, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433/**
434 * New context.
435 *
436 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000437 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * \param cmd command.
439 * \param arg user argument pointing to a drm_ctx structure.
440 * \return zero on success or a negative number on failure.
441 *
442 * Calls context_switch_complete().
443 */
Eric Anholtc153f452007-09-03 12:06:45 +1000444int drm_newctx(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Eric Anholtc153f452007-09-03 12:06:45 +1000447 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Daniel Vetter7c510132013-08-08 15:41:21 +0200449 if (drm_core_check_feature(dev, DRIVER_MODESET))
450 return -EINVAL;
451
Eric Anholtc153f452007-09-03 12:06:45 +1000452 DRM_DEBUG("%d\n", ctx->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000453 drm_context_switch_complete(dev, file_priv, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 return 0;
456}
457
458/**
459 * Remove context.
460 *
461 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000462 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * \param cmd command.
464 * \param arg user argument pointing to a drm_ctx structure.
465 * \return zero on success or a negative number on failure.
466 *
467 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
468 */
Eric Anholtc153f452007-09-03 12:06:45 +1000469int drm_rmctx(struct drm_device *dev, void *data,
470 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Eric Anholtc153f452007-09-03 12:06:45 +1000472 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Daniel Vetter7c510132013-08-08 15:41:21 +0200474 if (drm_core_check_feature(dev, DRIVER_MODESET))
475 return -EINVAL;
476
Eric Anholtc153f452007-09-03 12:06:45 +1000477 DRM_DEBUG("%d\n", ctx->handle);
Eric Anholtc153f452007-09-03 12:06:45 +1000478 if (ctx->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (dev->driver->context_dtor)
Eric Anholtc153f452007-09-03 12:06:45 +1000480 dev->driver->context_dtor(dev, ctx->handle);
481 drm_ctxbitmap_free(dev, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
Dave Airlie30e2fb12006-02-02 19:37:46 +1100484 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000485 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000486 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Dave Airliebd1b3312007-05-26 05:01:51 +1000488 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000489 if (pos->handle == ctx->handle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 list_del(&pos->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700491 kfree(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 --dev->ctx_count;
493 }
494 }
495 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100496 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 return 0;
499}
500
501/*@}*/