blob: 9b23525c0ed043f0220760010c2584e86ed163f6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Herrmanne7b960702014-07-24 12:10:04 +02002 * Legacy: Generic DRM Contexts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
David Herrmanne7b960702014-07-24 12:10:04 +02008 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author: Gareth Hughes <gareth@valinux.com>
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
20 * Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
David Herrmanne7b960702014-07-24 12:10:04 +020032#include "drm_legacy.h"
33
34struct drm_ctx_list {
35 struct list_head head;
36 drm_context_t handle;
37 struct drm_file *tag;
38};
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Dave Airliec21eb212013-09-20 08:32:59 +100040/******************************************************************/
41/** \name Context bitmap support */
42/*@{*/
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/**
45 * Free a handle from the context bitmap.
46 *
47 * \param dev DRM device.
48 * \param ctx_handle context handle.
49 *
50 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
Dave Airlie62968142007-07-17 10:46:52 +100051 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * lock.
53 */
David Herrmanne7b960702014-07-24 12:10:04 +020054void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Dave Airlie62968142007-07-17 10:46:52 +100056 mutex_lock(&dev->struct_mutex);
57 idr_remove(&dev->ctx_idr, ctx_handle);
58 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Dave Airlieb5e89ed2005-09-25 14:28:13 +100061/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Context bitmap allocation.
63 *
64 * \param dev DRM device.
65 * \return (non-negative) context handle on success or a negative number on failure.
66 *
Dave Airlie62968142007-07-17 10:46:52 +100067 * Allocate a new idr from drm_device::ctx_idr while holding the
Dave Airlie30e2fb12006-02-02 19:37:46 +110068 * drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
David Herrmanne7b960702014-07-24 12:10:04 +020070static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Dave Airlie62968142007-07-17 10:46:52 +100072 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Dave Airlie30e2fb12006-02-02 19:37:46 +110074 mutex_lock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -080075 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
76 GFP_KERNEL);
Dave Airlie30e2fb12006-02-02 19:37:46 +110077 mutex_unlock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -080078 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81/**
82 * Context bitmap initialization.
83 *
84 * \param dev DRM device.
85 *
Dave Airlie62968142007-07-17 10:46:52 +100086 * Initialise the drm_device::ctx_idr
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
David Herrmanne7b960702014-07-24 12:10:04 +020088int drm_legacy_ctxbitmap_init(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Dave Airlie62968142007-07-17 10:46:52 +100090 idr_init(&dev->ctx_idr);
Dave Airliec21eb212013-09-20 08:32:59 +100091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94/**
95 * Context bitmap cleanup.
96 *
97 * \param dev DRM device.
98 *
Dave Airlie62968142007-07-17 10:46:52 +100099 * Free all idr members using drm_ctx_sarea_free helper function
100 * while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
David Herrmanne7b960702014-07-24 12:10:04 +0200102void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Dave Airlie30e2fb12006-02-02 19:37:46 +1100104 mutex_lock(&dev->struct_mutex);
Tejun Heo4d532332013-02-27 17:03:39 -0800105 idr_destroy(&dev->ctx_idr);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100106 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
David Herrmann9f8d21e2014-07-23 09:01:12 +0200109/**
110 * drm_ctxbitmap_flush() - Flush all contexts owned by a file
111 * @dev: DRM device to operate on
112 * @file: Open file to flush contexts for
113 *
114 * This iterates over all contexts on @dev and drops them if they're owned by
115 * @file. Note that after this call returns, new contexts might be added if
116 * the file is still alive.
117 */
David Herrmanne7b960702014-07-24 12:10:04 +0200118void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
David Herrmann9f8d21e2014-07-23 09:01:12 +0200119{
120 struct drm_ctx_list *pos, *tmp;
121
122 mutex_lock(&dev->ctxlist_mutex);
123
124 list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
125 if (pos->tag == file &&
126 pos->handle != DRM_KERNEL_CONTEXT) {
127 if (dev->driver->context_dtor)
128 dev->driver->context_dtor(dev, pos->handle);
129
David Herrmanne7b960702014-07-24 12:10:04 +0200130 drm_legacy_ctxbitmap_free(dev, pos->handle);
David Herrmann9f8d21e2014-07-23 09:01:12 +0200131 list_del(&pos->head);
132 kfree(pos);
133 }
134 }
135
136 mutex_unlock(&dev->ctxlist_mutex);
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/*@}*/
140
141/******************************************************************/
142/** \name Per Context SAREA Support */
143/*@{*/
144
145/**
146 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000149 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * \param cmd command.
151 * \param arg user argument pointing to a drm_ctx_priv_map structure.
152 * \return zero on success or a negative number on failure.
153 *
Dave Airlie62968142007-07-17 10:46:52 +1000154 * Gets the map from drm_device::ctx_idr with the handle specified and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * returns its handle.
156 */
David Herrmanne7b960702014-07-24 12:10:04 +0200157int drm_legacy_getsareactx(struct drm_device *dev, void *data,
158 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Eric Anholtc153f452007-09-03 12:06:45 +1000160 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100161 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000162 struct drm_map_list *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Dave Airlie30e2fb12006-02-02 19:37:46 +1100164 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000165
Eric Anholtc153f452007-09-03 12:06:45 +1000166 map = idr_find(&dev->ctx_idr, request->ctx_id);
Dave Airlie62968142007-07-17 10:46:52 +1000167 if (!map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100168 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -EINVAL;
170 }
171
Eric Anholtc153f452007-09-03 12:06:45 +1000172 request->handle = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000173 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000174 if (_entry->map == map) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000175 request->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000177 break;
178 }
179 }
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400180
181 mutex_unlock(&dev->struct_mutex);
182
Eric Anholtc153f452007-09-03 12:06:45 +1000183 if (request->handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000184 return -EINVAL;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
187}
188
189/**
190 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000193 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * \param cmd command.
195 * \param arg user argument pointing to a drm_ctx_priv_map structure.
196 * \return zero on success or a negative number on failure.
197 *
198 * Searches the mapping specified in \p arg and update the entry in
Dave Airlie62968142007-07-17 10:46:52 +1000199 * drm_device::ctx_idr with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
David Herrmanne7b960702014-07-24 12:10:04 +0200201int drm_legacy_setsareactx(struct drm_device *dev, void *data,
202 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Eric Anholtc153f452007-09-03 12:06:45 +1000204 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100205 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000206 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Dave Airlie30e2fb12006-02-02 19:37:46 +1100208 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000209 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie9a186642005-06-23 21:29:18 +1000210 if (r_list->map
Eric Anholtc153f452007-09-03 12:06:45 +1000211 && r_list->user_token == (unsigned long) request->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto found;
213 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100215 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -EINVAL;
217
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000220 if (!map)
221 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000222
Eric Anholtc153f452007-09-03 12:06:45 +1000223 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000225
Dave Airlie30e2fb12006-02-02 19:37:46 +1100226 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229}
230
231/*@}*/
232
233/******************************************************************/
234/** \name The actual DRM context handling routines */
235/*@{*/
236
237/**
238 * Switch context.
239 *
240 * \param dev DRM device.
241 * \param old old context handle.
242 * \param new new context handle.
243 * \return zero on success or a negative number on failure.
244 *
245 * Attempt to set drm_device::context_flag.
246 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000247static int drm_context_switch(struct drm_device * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 if (test_and_set_bit(0, &dev->context_flag)) {
250 DRM_ERROR("Reentering -- FIXME\n");
251 return -EBUSY;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 if (new == dev->last_context) {
257 clear_bit(0, &dev->context_flag);
258 return 0;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/**
265 * Complete context switch.
266 *
267 * \param dev DRM device.
268 * \param new new context handle.
269 * \return zero on success or a negative number on failure.
270 *
271 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
272 * hardware lock is held, clears the drm_device::context_flag and wakes up
273 * drm_device::context_wait.
274 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000275static int drm_context_switch_complete(struct drm_device *dev,
276 struct drm_file *file_priv, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Dave Airlie7c1c2872008-11-28 14:22:24 +1000280 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 DRM_ERROR("Lock isn't held after context switch\n");
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 /* If a context switch is ever initiated
285 when the kernel holds the lock, release
286 that lock here. */
287 clear_bit(0, &dev->context_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/**
293 * Reserve contexts.
294 *
295 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000296 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * \param cmd command.
298 * \param arg user argument pointing to a drm_ctx_res structure.
299 * \return zero on success or a negative number on failure.
300 */
David Herrmanne7b960702014-07-24 12:10:04 +0200301int drm_legacy_resctx(struct drm_device *dev, void *data,
302 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Eric Anholtc153f452007-09-03 12:06:45 +1000304 struct drm_ctx_res *res = data;
Dave Airliec60ce622007-07-11 15:27:12 +1000305 struct drm_ctx ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int i;
307
Eric Anholtc153f452007-09-03 12:06:45 +1000308 if (res->count >= DRM_RESERVED_CONTEXTS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000309 memset(&ctx, 0, sizeof(ctx));
310 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 ctx.handle = i;
Eric Anholtc153f452007-09-03 12:06:45 +1000312 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -EFAULT;
314 }
315 }
Eric Anholtc153f452007-09-03 12:06:45 +1000316 res->count = DRM_RESERVED_CONTEXTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
320
321/**
322 * Add context.
323 *
324 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000325 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * \param cmd command.
327 * \param arg user argument pointing to a drm_ctx structure.
328 * \return zero on success or a negative number on failure.
329 *
330 * Get a new handle for the context and copy to userspace.
331 */
David Herrmanne7b960702014-07-24 12:10:04 +0200332int drm_legacy_addctx(struct drm_device *dev, void *data,
333 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Dave Airlie55910512007-07-11 16:53:40 +1000335 struct drm_ctx_list *ctx_entry;
Eric Anholtc153f452007-09-03 12:06:45 +1000336 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
David Herrmanne7b960702014-07-24 12:10:04 +0200338 ctx->handle = drm_legacy_ctxbitmap_next(dev);
Eric Anholtc153f452007-09-03 12:06:45 +1000339 if (ctx->handle == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000340 /* Skip kernel's context and get a new one. */
David Herrmanne7b960702014-07-24 12:10:04 +0200341 ctx->handle = drm_legacy_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Eric Anholtc153f452007-09-03 12:06:45 +1000343 DRM_DEBUG("%d\n", ctx->handle);
344 if (ctx->handle == -1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345 DRM_DEBUG("Not enough free contexts.\n");
346 /* Should this return -EBUSY instead? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -ENOMEM;
348 }
349
Eric Anholt9a298b22009-03-24 12:23:04 -0700350 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000351 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 DRM_DEBUG("out of memory\n");
353 return -ENOMEM;
354 }
355
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356 INIT_LIST_HEAD(&ctx_entry->head);
Eric Anholtc153f452007-09-03 12:06:45 +1000357 ctx_entry->handle = ctx->handle;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000358 ctx_entry->tag = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Dave Airlie30e2fb12006-02-02 19:37:46 +1100360 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000361 list_add(&ctx_entry->head, &dev->ctxlist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100362 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
368 * Get context.
369 *
370 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000371 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * \param cmd command.
373 * \param arg user argument pointing to a drm_ctx structure.
374 * \return zero on success or a negative number on failure.
375 */
David Herrmanne7b960702014-07-24 12:10:04 +0200376int drm_legacy_getctx(struct drm_device *dev, void *data,
377 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Eric Anholtc153f452007-09-03 12:06:45 +1000379 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* This is 0, because we don't handle any context flags */
Eric Anholtc153f452007-09-03 12:06:45 +1000382 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385}
386
387/**
388 * Switch context.
389 *
390 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000391 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * \param cmd command.
393 * \param arg user argument pointing to a drm_ctx structure.
394 * \return zero on success or a negative number on failure.
395 *
396 * Calls context_switch().
397 */
David Herrmanne7b960702014-07-24 12:10:04 +0200398int drm_legacy_switchctx(struct drm_device *dev, void *data,
399 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Eric Anholtc153f452007-09-03 12:06:45 +1000401 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Eric Anholtc153f452007-09-03 12:06:45 +1000403 DRM_DEBUG("%d\n", ctx->handle);
404 return drm_context_switch(dev, dev->last_context, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/**
408 * New context.
409 *
410 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000411 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * \param cmd command.
413 * \param arg user argument pointing to a drm_ctx structure.
414 * \return zero on success or a negative number on failure.
415 *
416 * Calls context_switch_complete().
417 */
David Herrmanne7b960702014-07-24 12:10:04 +0200418int drm_legacy_newctx(struct drm_device *dev, void *data,
419 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Eric Anholtc153f452007-09-03 12:06:45 +1000421 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Eric Anholtc153f452007-09-03 12:06:45 +1000423 DRM_DEBUG("%d\n", ctx->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000424 drm_context_switch_complete(dev, file_priv, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return 0;
427}
428
429/**
430 * Remove context.
431 *
432 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000433 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * \param cmd command.
435 * \param arg user argument pointing to a drm_ctx structure.
436 * \return zero on success or a negative number on failure.
437 *
438 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
439 */
David Herrmanne7b960702014-07-24 12:10:04 +0200440int drm_legacy_rmctx(struct drm_device *dev, void *data,
441 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Eric Anholtc153f452007-09-03 12:06:45 +1000443 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Eric Anholtc153f452007-09-03 12:06:45 +1000445 DRM_DEBUG("%d\n", ctx->handle);
Eric Anholtc153f452007-09-03 12:06:45 +1000446 if (ctx->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (dev->driver->context_dtor)
Eric Anholtc153f452007-09-03 12:06:45 +1000448 dev->driver->context_dtor(dev, ctx->handle);
David Herrmanne7b960702014-07-24 12:10:04 +0200449 drm_legacy_ctxbitmap_free(dev, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
Dave Airlie30e2fb12006-02-02 19:37:46 +1100452 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000453 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000454 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Airliebd1b3312007-05-26 05:01:51 +1000456 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000457 if (pos->handle == ctx->handle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 list_del(&pos->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700459 kfree(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 }
462 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100463 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 return 0;
466}
467
468/*@}*/