blob: 32958dabd7b0fe10b5e53e1f7134d973285294bf [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{
Peter Antoine0e975982015-06-23 08:18:49 +010056 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
57 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
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * Context bitmap allocation.
67 *
68 * \param dev DRM device.
69 * \return (non-negative) context handle on success or a negative number on failure.
70 *
Dave Airlie62968142007-07-17 10:46:52 +100071 * Allocate a new idr from drm_device::ctx_idr while holding the
Dave Airlie30e2fb12006-02-02 19:37:46 +110072 * drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
David Herrmanne7b960702014-07-24 12:10:04 +020074static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Dave Airlie62968142007-07-17 10:46:52 +100076 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Dave Airlie30e2fb12006-02-02 19:37:46 +110078 mutex_lock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -080079 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
80 GFP_KERNEL);
Dave Airlie30e2fb12006-02-02 19:37:46 +110081 mutex_unlock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -080082 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85/**
86 * Context bitmap initialization.
87 *
88 * \param dev DRM device.
89 *
Dave Airlie62968142007-07-17 10:46:52 +100090 * Initialise the drm_device::ctx_idr
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
David Herrmanne7b960702014-07-24 12:10:04 +020092int drm_legacy_ctxbitmap_init(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Peter Antoine0e975982015-06-23 08:18:49 +010094 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
95 drm_core_check_feature(dev, DRIVER_MODESET))
96 return -EINVAL;
97
Dave Airlie62968142007-07-17 10:46:52 +100098 idr_init(&dev->ctx_idr);
Dave Airliec21eb212013-09-20 08:32:59 +100099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102/**
103 * Context bitmap cleanup.
104 *
105 * \param dev DRM device.
106 *
Dave Airlie62968142007-07-17 10:46:52 +1000107 * Free all idr members using drm_ctx_sarea_free helper function
108 * while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
David Herrmanne7b960702014-07-24 12:10:04 +0200110void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Peter Antoine0e975982015-06-23 08:18:49 +0100112 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
113 drm_core_check_feature(dev, DRIVER_MODESET))
114 return;
115
Dave Airlie30e2fb12006-02-02 19:37:46 +1100116 mutex_lock(&dev->struct_mutex);
Tejun Heo4d532332013-02-27 17:03:39 -0800117 idr_destroy(&dev->ctx_idr);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100118 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
David Herrmann9f8d21e2014-07-23 09:01:12 +0200121/**
122 * drm_ctxbitmap_flush() - Flush all contexts owned by a file
123 * @dev: DRM device to operate on
124 * @file: Open file to flush contexts for
125 *
126 * This iterates over all contexts on @dev and drops them if they're owned by
127 * @file. Note that after this call returns, new contexts might be added if
128 * the file is still alive.
129 */
David Herrmanne7b960702014-07-24 12:10:04 +0200130void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
David Herrmann9f8d21e2014-07-23 09:01:12 +0200131{
132 struct drm_ctx_list *pos, *tmp;
133
Peter Antoine0e975982015-06-23 08:18:49 +0100134 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
135 drm_core_check_feature(dev, DRIVER_MODESET))
136 return;
137
David Herrmann9f8d21e2014-07-23 09:01:12 +0200138 mutex_lock(&dev->ctxlist_mutex);
139
140 list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
141 if (pos->tag == file &&
142 pos->handle != DRM_KERNEL_CONTEXT) {
143 if (dev->driver->context_dtor)
144 dev->driver->context_dtor(dev, pos->handle);
145
David Herrmanne7b960702014-07-24 12:10:04 +0200146 drm_legacy_ctxbitmap_free(dev, pos->handle);
David Herrmann9f8d21e2014-07-23 09:01:12 +0200147 list_del(&pos->head);
148 kfree(pos);
149 }
150 }
151
152 mutex_unlock(&dev->ctxlist_mutex);
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*@}*/
156
157/******************************************************************/
158/** \name Per Context SAREA Support */
159/*@{*/
160
161/**
162 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000165 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * \param cmd command.
167 * \param arg user argument pointing to a drm_ctx_priv_map structure.
168 * \return zero on success or a negative number on failure.
169 *
Dave Airlie62968142007-07-17 10:46:52 +1000170 * Gets the map from drm_device::ctx_idr with the handle specified and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * returns its handle.
172 */
David Herrmanne7b960702014-07-24 12:10:04 +0200173int drm_legacy_getsareactx(struct drm_device *dev, void *data,
174 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Eric Anholtc153f452007-09-03 12:06:45 +1000176 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100177 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000178 struct drm_map_list *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Peter Antoine0e975982015-06-23 08:18:49 +0100180 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
181 drm_core_check_feature(dev, DRIVER_MODESET))
182 return -EINVAL;
183
Dave Airlie30e2fb12006-02-02 19:37:46 +1100184 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000185
Eric Anholtc153f452007-09-03 12:06:45 +1000186 map = idr_find(&dev->ctx_idr, request->ctx_id);
Dave Airlie62968142007-07-17 10:46:52 +1000187 if (!map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100188 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190 }
191
Eric Anholtc153f452007-09-03 12:06:45 +1000192 request->handle = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000193 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000194 if (_entry->map == map) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000195 request->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000197 break;
198 }
199 }
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400200
201 mutex_unlock(&dev->struct_mutex);
202
Eric Anholtc153f452007-09-03 12:06:45 +1000203 if (request->handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000204 return -EINVAL;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207}
208
209/**
210 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000213 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * \param cmd command.
215 * \param arg user argument pointing to a drm_ctx_priv_map structure.
216 * \return zero on success or a negative number on failure.
217 *
218 * Searches the mapping specified in \p arg and update the entry in
Dave Airlie62968142007-07-17 10:46:52 +1000219 * drm_device::ctx_idr with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
David Herrmanne7b960702014-07-24 12:10:04 +0200221int drm_legacy_setsareactx(struct drm_device *dev, void *data,
222 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Eric Anholtc153f452007-09-03 12:06:45 +1000224 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100225 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000226 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Peter Antoine0e975982015-06-23 08:18:49 +0100228 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
229 drm_core_check_feature(dev, DRIVER_MODESET))
230 return -EINVAL;
231
Dave Airlie30e2fb12006-02-02 19:37:46 +1100232 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000233 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie9a186642005-06-23 21:29:18 +1000234 if (r_list->map
Eric Anholtc153f452007-09-03 12:06:45 +1000235 && r_list->user_token == (unsigned long) request->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto found;
237 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100239 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -EINVAL;
241
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244 if (!map)
245 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000246
Eric Anholtc153f452007-09-03 12:06:45 +1000247 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000249
Dave Airlie30e2fb12006-02-02 19:37:46 +1100250 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
255/*@}*/
256
257/******************************************************************/
258/** \name The actual DRM context handling routines */
259/*@{*/
260
261/**
262 * Switch context.
263 *
264 * \param dev DRM device.
265 * \param old old context handle.
266 * \param new new context handle.
267 * \return zero on success or a negative number on failure.
268 *
269 * Attempt to set drm_device::context_flag.
270 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000271static int drm_context_switch(struct drm_device * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 if (test_and_set_bit(0, &dev->context_flag)) {
274 DRM_ERROR("Reentering -- FIXME\n");
275 return -EBUSY;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280 if (new == dev->last_context) {
281 clear_bit(0, &dev->context_flag);
282 return 0;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/**
289 * Complete context switch.
290 *
291 * \param dev DRM device.
292 * \param new new context handle.
293 * \return zero on success or a negative number on failure.
294 *
295 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
296 * hardware lock is held, clears the drm_device::context_flag and wakes up
297 * drm_device::context_wait.
298 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000299static int drm_context_switch_complete(struct drm_device *dev,
300 struct drm_file *file_priv, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Dave Airlie7c1c2872008-11-28 14:22:24 +1000304 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 DRM_ERROR("Lock isn't held after context switch\n");
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308 /* If a context switch is ever initiated
309 when the kernel holds the lock, release
310 that lock here. */
311 clear_bit(0, &dev->context_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/**
317 * Reserve contexts.
318 *
319 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000320 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * \param cmd command.
322 * \param arg user argument pointing to a drm_ctx_res structure.
323 * \return zero on success or a negative number on failure.
324 */
David Herrmanne7b960702014-07-24 12:10:04 +0200325int drm_legacy_resctx(struct drm_device *dev, void *data,
326 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Eric Anholtc153f452007-09-03 12:06:45 +1000328 struct drm_ctx_res *res = data;
Dave Airliec60ce622007-07-11 15:27:12 +1000329 struct drm_ctx ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int i;
331
Peter Antoine0e975982015-06-23 08:18:49 +0100332 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
333 drm_core_check_feature(dev, DRIVER_MODESET))
334 return -EINVAL;
335
Eric Anholtc153f452007-09-03 12:06:45 +1000336 if (res->count >= DRM_RESERVED_CONTEXTS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 memset(&ctx, 0, sizeof(ctx));
338 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 ctx.handle = i;
Eric Anholtc153f452007-09-03 12:06:45 +1000340 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EFAULT;
342 }
343 }
Eric Anholtc153f452007-09-03 12:06:45 +1000344 res->count = DRM_RESERVED_CONTEXTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347}
348
349/**
350 * Add context.
351 *
352 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000353 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * \param cmd command.
355 * \param arg user argument pointing to a drm_ctx structure.
356 * \return zero on success or a negative number on failure.
357 *
358 * Get a new handle for the context and copy to userspace.
359 */
David Herrmanne7b960702014-07-24 12:10:04 +0200360int drm_legacy_addctx(struct drm_device *dev, void *data,
361 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Dave Airlie55910512007-07-11 16:53:40 +1000363 struct drm_ctx_list *ctx_entry;
Eric Anholtc153f452007-09-03 12:06:45 +1000364 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Peter Antoine0e975982015-06-23 08:18:49 +0100366 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
367 drm_core_check_feature(dev, DRIVER_MODESET))
368 return -EINVAL;
369
David Herrmanne7b960702014-07-24 12:10:04 +0200370 ctx->handle = drm_legacy_ctxbitmap_next(dev);
Eric Anholtc153f452007-09-03 12:06:45 +1000371 if (ctx->handle == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 /* Skip kernel's context and get a new one. */
David Herrmanne7b960702014-07-24 12:10:04 +0200373 ctx->handle = drm_legacy_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Eric Anholtc153f452007-09-03 12:06:45 +1000375 DRM_DEBUG("%d\n", ctx->handle);
376 if (ctx->handle == -1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000377 DRM_DEBUG("Not enough free contexts.\n");
378 /* Should this return -EBUSY instead? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -ENOMEM;
380 }
381
Eric Anholt9a298b22009-03-24 12:23:04 -0700382 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 DRM_DEBUG("out of memory\n");
385 return -ENOMEM;
386 }
387
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 INIT_LIST_HEAD(&ctx_entry->head);
Eric Anholtc153f452007-09-03 12:06:45 +1000389 ctx_entry->handle = ctx->handle;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000390 ctx_entry->tag = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Dave Airlie30e2fb12006-02-02 19:37:46 +1100392 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000393 list_add(&ctx_entry->head, &dev->ctxlist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100394 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/**
400 * Get context.
401 *
402 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000403 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * \param cmd command.
405 * \param arg user argument pointing to a drm_ctx structure.
406 * \return zero on success or a negative number on failure.
407 */
David Herrmanne7b960702014-07-24 12:10:04 +0200408int drm_legacy_getctx(struct drm_device *dev, void *data,
409 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Eric Anholtc153f452007-09-03 12:06:45 +1000411 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Peter Antoine0e975982015-06-23 08:18:49 +0100413 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
414 drm_core_check_feature(dev, DRIVER_MODESET))
415 return -EINVAL;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* This is 0, because we don't handle any context flags */
Eric Anholtc153f452007-09-03 12:06:45 +1000418 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421}
422
423/**
424 * Switch 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 * Calls context_switch().
433 */
David Herrmanne7b960702014-07-24 12:10:04 +0200434int drm_legacy_switchctx(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
Peter Antoine0e975982015-06-23 08:18:49 +0100439 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
440 drm_core_check_feature(dev, DRIVER_MODESET))
441 return -EINVAL;
442
Eric Anholtc153f452007-09-03 12:06:45 +1000443 DRM_DEBUG("%d\n", ctx->handle);
444 return drm_context_switch(dev, dev->last_context, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/**
448 * New context.
449 *
450 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000451 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * \param cmd command.
453 * \param arg user argument pointing to a drm_ctx structure.
454 * \return zero on success or a negative number on failure.
455 *
456 * Calls context_switch_complete().
457 */
David Herrmanne7b960702014-07-24 12:10:04 +0200458int drm_legacy_newctx(struct drm_device *dev, void *data,
459 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Eric Anholtc153f452007-09-03 12:06:45 +1000461 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Peter Antoine0e975982015-06-23 08:18:49 +0100463 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
464 drm_core_check_feature(dev, DRIVER_MODESET))
465 return -EINVAL;
466
Eric Anholtc153f452007-09-03 12:06:45 +1000467 DRM_DEBUG("%d\n", ctx->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000468 drm_context_switch_complete(dev, file_priv, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 return 0;
471}
472
473/**
474 * Remove context.
475 *
476 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000477 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * \param cmd command.
479 * \param arg user argument pointing to a drm_ctx structure.
480 * \return zero on success or a negative number on failure.
481 *
482 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
483 */
David Herrmanne7b960702014-07-24 12:10:04 +0200484int drm_legacy_rmctx(struct drm_device *dev, void *data,
485 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Eric Anholtc153f452007-09-03 12:06:45 +1000487 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Peter Antoine0e975982015-06-23 08:18:49 +0100489 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
490 drm_core_check_feature(dev, DRIVER_MODESET))
491 return -EINVAL;
492
Eric Anholtc153f452007-09-03 12:06:45 +1000493 DRM_DEBUG("%d\n", ctx->handle);
Eric Anholtc153f452007-09-03 12:06:45 +1000494 if (ctx->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (dev->driver->context_dtor)
Eric Anholtc153f452007-09-03 12:06:45 +1000496 dev->driver->context_dtor(dev, ctx->handle);
David Herrmanne7b960702014-07-24 12:10:04 +0200497 drm_legacy_ctxbitmap_free(dev, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Dave Airlie30e2fb12006-02-02 19:37:46 +1100500 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000501 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000502 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Dave Airliebd1b3312007-05-26 05:01:51 +1000504 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000505 if (pos->handle == ctx->handle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 list_del(&pos->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700507 kfree(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
510 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100511 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 return 0;
514}
515
516/*@}*/