blob: 3c4000facb360a012f78b12599d918a276145ca6 [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) &&
Daniel Vetterfa538642016-08-03 21:11:10 +020057 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +010058 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 */
Daniel Vetterba6976c2015-06-23 11:22:36 +020092void 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) &&
Daniel Vetterfa538642016-08-03 21:11:10 +020095 !drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetterba6976c2015-06-23 11:22:36 +020096 return;
Peter Antoine0e975982015-06-23 08:18:49 +010097
Dave Airlie62968142007-07-17 10:46:52 +100098 idr_init(&dev->ctx_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101/**
102 * Context bitmap cleanup.
103 *
104 * \param dev DRM device.
105 *
Dave Airlie62968142007-07-17 10:46:52 +1000106 * Free all idr members using drm_ctx_sarea_free helper function
107 * while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
David Herrmanne7b960702014-07-24 12:10:04 +0200109void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Peter Antoine0e975982015-06-23 08:18:49 +0100111 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200112 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100113 return;
114
Dave Airlie30e2fb12006-02-02 19:37:46 +1100115 mutex_lock(&dev->struct_mutex);
Tejun Heo4d532332013-02-27 17:03:39 -0800116 idr_destroy(&dev->ctx_idr);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100117 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
David Herrmann9f8d21e2014-07-23 09:01:12 +0200120/**
121 * drm_ctxbitmap_flush() - Flush all contexts owned by a file
122 * @dev: DRM device to operate on
123 * @file: Open file to flush contexts for
124 *
125 * This iterates over all contexts on @dev and drops them if they're owned by
126 * @file. Note that after this call returns, new contexts might be added if
127 * the file is still alive.
128 */
David Herrmanne7b960702014-07-24 12:10:04 +0200129void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
David Herrmann9f8d21e2014-07-23 09:01:12 +0200130{
131 struct drm_ctx_list *pos, *tmp;
132
Peter Antoine0e975982015-06-23 08:18:49 +0100133 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200134 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100135 return;
136
David Herrmann9f8d21e2014-07-23 09:01:12 +0200137 mutex_lock(&dev->ctxlist_mutex);
138
139 list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
140 if (pos->tag == file &&
141 pos->handle != DRM_KERNEL_CONTEXT) {
142 if (dev->driver->context_dtor)
143 dev->driver->context_dtor(dev, pos->handle);
144
David Herrmanne7b960702014-07-24 12:10:04 +0200145 drm_legacy_ctxbitmap_free(dev, pos->handle);
David Herrmann9f8d21e2014-07-23 09:01:12 +0200146 list_del(&pos->head);
147 kfree(pos);
148 }
149 }
150
151 mutex_unlock(&dev->ctxlist_mutex);
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*@}*/
155
156/******************************************************************/
157/** \name Per Context SAREA Support */
158/*@{*/
159
160/**
161 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000164 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * \param cmd command.
166 * \param arg user argument pointing to a drm_ctx_priv_map structure.
167 * \return zero on success or a negative number on failure.
168 *
Dave Airlie62968142007-07-17 10:46:52 +1000169 * Gets the map from drm_device::ctx_idr with the handle specified and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * returns its handle.
171 */
David Herrmanne7b960702014-07-24 12:10:04 +0200172int drm_legacy_getsareactx(struct drm_device *dev, void *data,
173 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Eric Anholtc153f452007-09-03 12:06:45 +1000175 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100176 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000177 struct drm_map_list *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Peter Antoine0e975982015-06-23 08:18:49 +0100179 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200180 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100181 return -EINVAL;
182
Dave Airlie30e2fb12006-02-02 19:37:46 +1100183 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000184
Eric Anholtc153f452007-09-03 12:06:45 +1000185 map = idr_find(&dev->ctx_idr, request->ctx_id);
Dave Airlie62968142007-07-17 10:46:52 +1000186 if (!map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100187 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189 }
190
Eric Anholtc153f452007-09-03 12:06:45 +1000191 request->handle = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000192 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000193 if (_entry->map == map) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000194 request->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000195 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000196 break;
197 }
198 }
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400199
200 mutex_unlock(&dev->struct_mutex);
201
Eric Anholtc153f452007-09-03 12:06:45 +1000202 if (request->handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000203 return -EINVAL;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
206}
207
208/**
209 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000212 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * \param cmd command.
214 * \param arg user argument pointing to a drm_ctx_priv_map structure.
215 * \return zero on success or a negative number on failure.
216 *
217 * Searches the mapping specified in \p arg and update the entry in
Dave Airlie62968142007-07-17 10:46:52 +1000218 * drm_device::ctx_idr with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
David Herrmanne7b960702014-07-24 12:10:04 +0200220int drm_legacy_setsareactx(struct drm_device *dev, void *data,
221 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Eric Anholtc153f452007-09-03 12:06:45 +1000223 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100224 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000225 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Peter Antoine0e975982015-06-23 08:18:49 +0100227 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200228 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100229 return -EINVAL;
230
Dave Airlie30e2fb12006-02-02 19:37:46 +1100231 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000232 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie9a186642005-06-23 21:29:18 +1000233 if (r_list->map
Eric Anholtc153f452007-09-03 12:06:45 +1000234 && r_list->user_token == (unsigned long) request->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto found;
236 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100238 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EINVAL;
240
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 if (!map)
244 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000245
Eric Anholtc153f452007-09-03 12:06:45 +1000246 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000248
Dave Airlie30e2fb12006-02-02 19:37:46 +1100249 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252}
253
254/*@}*/
255
256/******************************************************************/
257/** \name The actual DRM context handling routines */
258/*@{*/
259
260/**
261 * Switch context.
262 *
263 * \param dev DRM device.
264 * \param old old context handle.
265 * \param new new context handle.
266 * \return zero on success or a negative number on failure.
267 *
268 * Attempt to set drm_device::context_flag.
269 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000270static int drm_context_switch(struct drm_device * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 if (test_and_set_bit(0, &dev->context_flag)) {
273 DRM_ERROR("Reentering -- FIXME\n");
274 return -EBUSY;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 if (new == dev->last_context) {
280 clear_bit(0, &dev->context_flag);
281 return 0;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/**
288 * Complete context switch.
289 *
290 * \param dev DRM device.
291 * \param new new context handle.
292 * \return zero on success or a negative number on failure.
293 *
294 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
295 * hardware lock is held, clears the drm_device::context_flag and wakes up
296 * drm_device::context_wait.
297 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000298static int drm_context_switch_complete(struct drm_device *dev,
299 struct drm_file *file_priv, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Dave Airlie7c1c2872008-11-28 14:22:24 +1000303 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 DRM_ERROR("Lock isn't held after context switch\n");
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000307 /* If a context switch is ever initiated
308 when the kernel holds the lock, release
309 that lock here. */
310 clear_bit(0, &dev->context_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/**
316 * Reserve contexts.
317 *
318 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000319 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * \param cmd command.
321 * \param arg user argument pointing to a drm_ctx_res structure.
322 * \return zero on success or a negative number on failure.
323 */
David Herrmanne7b960702014-07-24 12:10:04 +0200324int drm_legacy_resctx(struct drm_device *dev, void *data,
325 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Eric Anholtc153f452007-09-03 12:06:45 +1000327 struct drm_ctx_res *res = data;
Dave Airliec60ce622007-07-11 15:27:12 +1000328 struct drm_ctx ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int i;
330
Peter Antoine0e975982015-06-23 08:18:49 +0100331 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200332 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100333 return -EINVAL;
334
Eric Anholtc153f452007-09-03 12:06:45 +1000335 if (res->count >= DRM_RESERVED_CONTEXTS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 memset(&ctx, 0, sizeof(ctx));
337 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ctx.handle = i;
Eric Anholtc153f452007-09-03 12:06:45 +1000339 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EFAULT;
341 }
342 }
Eric Anholtc153f452007-09-03 12:06:45 +1000343 res->count = DRM_RESERVED_CONTEXTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
348/**
349 * Add context.
350 *
351 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000352 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * \param cmd command.
354 * \param arg user argument pointing to a drm_ctx structure.
355 * \return zero on success or a negative number on failure.
356 *
357 * Get a new handle for the context and copy to userspace.
358 */
David Herrmanne7b960702014-07-24 12:10:04 +0200359int drm_legacy_addctx(struct drm_device *dev, void *data,
360 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Dave Airlie55910512007-07-11 16:53:40 +1000362 struct drm_ctx_list *ctx_entry;
Eric Anholtc153f452007-09-03 12:06:45 +1000363 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Peter Antoine0e975982015-06-23 08:18:49 +0100365 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200366 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100367 return -EINVAL;
368
David Herrmanne7b960702014-07-24 12:10:04 +0200369 ctx->handle = drm_legacy_ctxbitmap_next(dev);
Eric Anholtc153f452007-09-03 12:06:45 +1000370 if (ctx->handle == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 /* Skip kernel's context and get a new one. */
David Herrmanne7b960702014-07-24 12:10:04 +0200372 ctx->handle = drm_legacy_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Eric Anholtc153f452007-09-03 12:06:45 +1000374 DRM_DEBUG("%d\n", ctx->handle);
375 if (ctx->handle == -1) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 DRM_DEBUG("Not enough free contexts.\n");
377 /* Should this return -EBUSY instead? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -ENOMEM;
379 }
380
Eric Anholt9a298b22009-03-24 12:23:04 -0700381 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000382 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 DRM_DEBUG("out of memory\n");
384 return -ENOMEM;
385 }
386
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 INIT_LIST_HEAD(&ctx_entry->head);
Eric Anholtc153f452007-09-03 12:06:45 +1000388 ctx_entry->handle = ctx->handle;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000389 ctx_entry->tag = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Dave Airlie30e2fb12006-02-02 19:37:46 +1100391 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000392 list_add(&ctx_entry->head, &dev->ctxlist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100393 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/**
399 * Get context.
400 *
401 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000402 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * \param cmd command.
404 * \param arg user argument pointing to a drm_ctx structure.
405 * \return zero on success or a negative number on failure.
406 */
David Herrmanne7b960702014-07-24 12:10:04 +0200407int drm_legacy_getctx(struct drm_device *dev, void *data,
408 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Eric Anholtc153f452007-09-03 12:06:45 +1000410 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Peter Antoine0e975982015-06-23 08:18:49 +0100412 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200413 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100414 return -EINVAL;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* This is 0, because we don't handle any context flags */
Eric Anholtc153f452007-09-03 12:06:45 +1000417 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return 0;
420}
421
422/**
423 * Switch 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 * Calls context_switch().
432 */
David Herrmanne7b960702014-07-24 12:10:04 +0200433int drm_legacy_switchctx(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
Peter Antoine0e975982015-06-23 08:18:49 +0100438 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200439 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100440 return -EINVAL;
441
Eric Anholtc153f452007-09-03 12:06:45 +1000442 DRM_DEBUG("%d\n", ctx->handle);
443 return drm_context_switch(dev, dev->last_context, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/**
447 * New context.
448 *
449 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000450 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * \param cmd command.
452 * \param arg user argument pointing to a drm_ctx structure.
453 * \return zero on success or a negative number on failure.
454 *
455 * Calls context_switch_complete().
456 */
David Herrmanne7b960702014-07-24 12:10:04 +0200457int drm_legacy_newctx(struct drm_device *dev, void *data,
458 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Eric Anholtc153f452007-09-03 12:06:45 +1000460 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Peter Antoine0e975982015-06-23 08:18:49 +0100462 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200463 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100464 return -EINVAL;
465
Eric Anholtc153f452007-09-03 12:06:45 +1000466 DRM_DEBUG("%d\n", ctx->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000467 drm_context_switch_complete(dev, file_priv, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 return 0;
470}
471
472/**
473 * Remove context.
474 *
475 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000476 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * \param cmd command.
478 * \param arg user argument pointing to a drm_ctx structure.
479 * \return zero on success or a negative number on failure.
480 *
481 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
482 */
David Herrmanne7b960702014-07-24 12:10:04 +0200483int drm_legacy_rmctx(struct drm_device *dev, void *data,
484 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Eric Anholtc153f452007-09-03 12:06:45 +1000486 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Peter Antoine0e975982015-06-23 08:18:49 +0100488 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200489 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100490 return -EINVAL;
491
Eric Anholtc153f452007-09-03 12:06:45 +1000492 DRM_DEBUG("%d\n", ctx->handle);
Eric Anholtc153f452007-09-03 12:06:45 +1000493 if (ctx->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (dev->driver->context_dtor)
Eric Anholtc153f452007-09-03 12:06:45 +1000495 dev->driver->context_dtor(dev, ctx->handle);
David Herrmanne7b960702014-07-24 12:10:04 +0200496 drm_legacy_ctxbitmap_free(dev, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Dave Airlie30e2fb12006-02-02 19:37:46 +1100499 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000500 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000501 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Dave Airliebd1b3312007-05-26 05:01:51 +1000503 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000504 if (pos->handle == ctx->handle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 list_del(&pos->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700506 kfree(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 }
509 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100510 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 return 0;
513}
514
515/*@}*/