blob: c6e6a3e7219a6084bad566d95f7e54e2fc8b8e6d [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
Sam Ravnborg0500c042019-05-26 19:35:35 +020031#include <linux/slab.h>
32#include <linux/uaccess.h>
33
34#include <drm/drm_drv.h>
35#include <drm/drm_file.h>
36#include <drm/drm_print.h>
37
David Herrmanne7b960702014-07-24 12:10:04 +020038#include "drm_legacy.h"
39
40struct drm_ctx_list {
41 struct list_head head;
42 drm_context_t handle;
43 struct drm_file *tag;
44};
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Dave Airliec21eb212013-09-20 08:32:59 +100046/******************************************************************/
47/** \name Context bitmap support */
48/*@{*/
49
Benjamin Gaignardcc994822020-03-06 11:29:37 +010050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * Free a handle from the context bitmap.
52 *
53 * \param dev DRM device.
54 * \param ctx_handle context handle.
55 *
56 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
Dave Airlie62968142007-07-17 10:46:52 +100057 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * lock.
59 */
David Herrmanne7b960702014-07-24 12:10:04 +020060void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Peter Antoine0e975982015-06-23 08:18:49 +010062 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +020063 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +010064 return;
65
Dave Airlie62968142007-07-17 10:46:52 +100066 mutex_lock(&dev->struct_mutex);
67 idr_remove(&dev->ctx_idr, ctx_handle);
68 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Benjamin Gaignardcc994822020-03-06 11:29:37 +010071/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * Context bitmap allocation.
73 *
74 * \param dev DRM device.
75 * \return (non-negative) context handle on success or a negative number on failure.
76 *
Dave Airlie62968142007-07-17 10:46:52 +100077 * Allocate a new idr from drm_device::ctx_idr while holding the
Dave Airlie30e2fb12006-02-02 19:37:46 +110078 * drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
David Herrmanne7b960702014-07-24 12:10:04 +020080static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Dave Airlie62968142007-07-17 10:46:52 +100082 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Dave Airlie30e2fb12006-02-02 19:37:46 +110084 mutex_lock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -080085 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
86 GFP_KERNEL);
Dave Airlie30e2fb12006-02-02 19:37:46 +110087 mutex_unlock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -080088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Benjamin Gaignardcc994822020-03-06 11:29:37 +010091/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * Context bitmap initialization.
93 *
94 * \param dev DRM device.
95 *
Dave Airlie62968142007-07-17 10:46:52 +100096 * Initialise the drm_device::ctx_idr
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Daniel Vetterba6976c2015-06-23 11:22:36 +020098void drm_legacy_ctxbitmap_init(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Peter Antoine0e975982015-06-23 08:18:49 +0100100 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200101 !drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetterba6976c2015-06-23 11:22:36 +0200102 return;
Peter Antoine0e975982015-06-23 08:18:49 +0100103
Dave Airlie62968142007-07-17 10:46:52 +1000104 idr_init(&dev->ctx_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * Context bitmap cleanup.
109 *
110 * \param dev DRM device.
111 *
Dave Airlie62968142007-07-17 10:46:52 +1000112 * Free all idr members using drm_ctx_sarea_free helper function
113 * while holding the drm_device::struct_mutex lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
David Herrmanne7b960702014-07-24 12:10:04 +0200115void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Peter Antoine0e975982015-06-23 08:18:49 +0100117 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200118 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100119 return;
120
Dave Airlie30e2fb12006-02-02 19:37:46 +1100121 mutex_lock(&dev->struct_mutex);
Tejun Heo4d532332013-02-27 17:03:39 -0800122 idr_destroy(&dev->ctx_idr);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100123 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
David Herrmann9f8d21e2014-07-23 09:01:12 +0200126/**
Yang Yingliang76fb3512021-05-13 15:19:18 +0800127 * drm_legacy_ctxbitmap_flush() - Flush all contexts owned by a file
David Herrmann9f8d21e2014-07-23 09:01:12 +0200128 * @dev: DRM device to operate on
129 * @file: Open file to flush contexts for
130 *
131 * This iterates over all contexts on @dev and drops them if they're owned by
132 * @file. Note that after this call returns, new contexts might be added if
133 * the file is still alive.
134 */
David Herrmanne7b960702014-07-24 12:10:04 +0200135void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
David Herrmann9f8d21e2014-07-23 09:01:12 +0200136{
137 struct drm_ctx_list *pos, *tmp;
138
Peter Antoine0e975982015-06-23 08:18:49 +0100139 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200140 !drm_core_check_feature(dev, DRIVER_LEGACY))
Peter Antoine0e975982015-06-23 08:18:49 +0100141 return;
142
David Herrmann9f8d21e2014-07-23 09:01:12 +0200143 mutex_lock(&dev->ctxlist_mutex);
144
145 list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
146 if (pos->tag == file &&
147 pos->handle != DRM_KERNEL_CONTEXT) {
148 if (dev->driver->context_dtor)
149 dev->driver->context_dtor(dev, pos->handle);
150
David Herrmanne7b960702014-07-24 12:10:04 +0200151 drm_legacy_ctxbitmap_free(dev, pos->handle);
David Herrmann9f8d21e2014-07-23 09:01:12 +0200152 list_del(&pos->head);
153 kfree(pos);
154 }
155 }
156
157 mutex_unlock(&dev->ctxlist_mutex);
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*@}*/
161
162/******************************************************************/
163/** \name Per Context SAREA Support */
164/*@{*/
165
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100166/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * Get per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000170 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * \param cmd command.
172 * \param arg user argument pointing to a drm_ctx_priv_map structure.
173 * \return zero on success or a negative number on failure.
174 *
Dave Airlie62968142007-07-17 10:46:52 +1000175 * Gets the map from drm_device::ctx_idr with the handle specified and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * returns its handle.
177 */
David Herrmanne7b960702014-07-24 12:10:04 +0200178int drm_legacy_getsareactx(struct drm_device *dev, void *data,
179 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Eric Anholtc153f452007-09-03 12:06:45 +1000181 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100182 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000183 struct drm_map_list *_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Peter Antoine0e975982015-06-23 08:18:49 +0100185 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200186 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100187 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100188
Dave Airlie30e2fb12006-02-02 19:37:46 +1100189 mutex_lock(&dev->struct_mutex);
Dave Airlie62968142007-07-17 10:46:52 +1000190
Eric Anholtc153f452007-09-03 12:06:45 +1000191 map = idr_find(&dev->ctx_idr, request->ctx_id);
Dave Airlie62968142007-07-17 10:46:52 +1000192 if (!map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100193 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -EINVAL;
195 }
196
Eric Anholtc153f452007-09-03 12:06:45 +1000197 request->handle = NULL;
Dave Airliebd1b3312007-05-26 05:01:51 +1000198 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +1000199 if (_entry->map == map) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000200 request->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201 (void *)(unsigned long)_entry->user_token;
Dave Airlied1f2b552005-08-05 22:11:22 +1000202 break;
203 }
204 }
Ilija Hadzic09b4ea42011-10-28 17:43:28 -0400205
206 mutex_unlock(&dev->struct_mutex);
207
Eric Anholtc153f452007-09-03 12:06:45 +1000208 if (request->handle == NULL)
Dave Airlied1f2b552005-08-05 22:11:22 +1000209 return -EINVAL;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
213
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Set per-context SAREA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000218 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * \param cmd command.
220 * \param arg user argument pointing to a drm_ctx_priv_map structure.
221 * \return zero on success or a negative number on failure.
222 *
223 * Searches the mapping specified in \p arg and update the entry in
Dave Airlie62968142007-07-17 10:46:52 +1000224 * drm_device::ctx_idr with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
David Herrmanne7b960702014-07-24 12:10:04 +0200226int drm_legacy_setsareactx(struct drm_device *dev, void *data,
227 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Eric Anholtc153f452007-09-03 12:06:45 +1000229 struct drm_ctx_priv_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100230 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000231 struct drm_map_list *r_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Peter Antoine0e975982015-06-23 08:18:49 +0100233 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200234 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100235 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100236
Dave Airlie30e2fb12006-02-02 19:37:46 +1100237 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000238 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie9a186642005-06-23 21:29:18 +1000239 if (r_list->map
Eric Anholtc153f452007-09-03 12:06:45 +1000240 && r_list->user_token == (unsigned long) request->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto found;
242 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 bad:
Dave Airlie30e2fb12006-02-02 19:37:46 +1100244 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EINVAL;
246
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 map = r_list->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 if (!map)
250 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000251
Eric Anholtc153f452007-09-03 12:06:45 +1000252 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto bad;
Dave Airlie62968142007-07-17 10:46:52 +1000254
Dave Airlie30e2fb12006-02-02 19:37:46 +1100255 mutex_unlock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258}
259
260/*@}*/
261
262/******************************************************************/
263/** \name The actual DRM context handling routines */
264/*@{*/
265
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100266/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * Switch context.
268 *
269 * \param dev DRM device.
270 * \param old old context handle.
271 * \param new new context handle.
272 * \return zero on success or a negative number on failure.
273 *
274 * Attempt to set drm_device::context_flag.
275 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000276static int drm_context_switch(struct drm_device * dev, int old, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 if (test_and_set_bit(0, &dev->context_flag)) {
279 DRM_ERROR("Reentering -- FIXME\n");
280 return -EBUSY;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 DRM_DEBUG("Context switch from %d to %d\n", old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 if (new == dev->last_context) {
286 clear_bit(0, &dev->context_flag);
287 return 0;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * Complete context switch.
295 *
296 * \param dev DRM device.
297 * \param new new context handle.
298 * \return zero on success or a negative number on failure.
299 *
300 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
301 * hardware lock is held, clears the drm_device::context_flag and wakes up
302 * drm_device::context_wait.
303 */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000304static int drm_context_switch_complete(struct drm_device *dev,
305 struct drm_file *file_priv, int new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000307 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Dave Airlie7c1c2872008-11-28 14:22:24 +1000309 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 DRM_ERROR("Lock isn't held after context switch\n");
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 /* If a context switch is ever initiated
314 when the kernel holds the lock, release
Beatriz Martins de Carvalhof0ce78e2021-04-18 15:48:25 +0100315 that lock here.
316 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317 clear_bit(0, &dev->context_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100322/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * Reserve contexts.
324 *
325 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000326 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * \param cmd command.
328 * \param arg user argument pointing to a drm_ctx_res structure.
329 * \return zero on success or a negative number on failure.
330 */
David Herrmanne7b960702014-07-24 12:10:04 +0200331int drm_legacy_resctx(struct drm_device *dev, void *data,
332 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Eric Anholtc153f452007-09-03 12:06:45 +1000334 struct drm_ctx_res *res = data;
Dave Airliec60ce622007-07-11 15:27:12 +1000335 struct drm_ctx ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int i;
337
Peter Antoine0e975982015-06-23 08:18:49 +0100338 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200339 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100340 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100341
Eric Anholtc153f452007-09-03 12:06:45 +1000342 if (res->count >= DRM_RESERVED_CONTEXTS) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 memset(&ctx, 0, sizeof(ctx));
344 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ctx.handle = i;
Eric Anholtc153f452007-09-03 12:06:45 +1000346 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -EFAULT;
348 }
349 }
Eric Anholtc153f452007-09-03 12:06:45 +1000350 res->count = DRM_RESERVED_CONTEXTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353}
354
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100355/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * Add context.
357 *
358 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000359 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * \param cmd command.
361 * \param arg user argument pointing to a drm_ctx structure.
362 * \return zero on success or a negative number on failure.
363 *
364 * Get a new handle for the context and copy to userspace.
365 */
David Herrmanne7b960702014-07-24 12:10:04 +0200366int drm_legacy_addctx(struct drm_device *dev, void *data,
367 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Dave Airlie55910512007-07-11 16:53:40 +1000369 struct drm_ctx_list *ctx_entry;
Eric Anholtc153f452007-09-03 12:06:45 +1000370 struct drm_ctx *ctx = data;
YueHaibingc39191f2018-12-29 10:49:07 +0800371 int tmp_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Peter Antoine0e975982015-06-23 08:18:49 +0100373 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200374 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100375 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100376
YueHaibingc39191f2018-12-29 10:49:07 +0800377 tmp_handle = drm_legacy_ctxbitmap_next(dev);
378 if (tmp_handle == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379 /* Skip kernel's context and get a new one. */
YueHaibingc39191f2018-12-29 10:49:07 +0800380 tmp_handle = drm_legacy_ctxbitmap_next(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
YueHaibingc39191f2018-12-29 10:49:07 +0800382 DRM_DEBUG("%d\n", tmp_handle);
383 if (tmp_handle < 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000384 DRM_DEBUG("Not enough free contexts.\n");
385 /* Should this return -EBUSY instead? */
YueHaibingc39191f2018-12-29 10:49:07 +0800386 return tmp_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
YueHaibingc39191f2018-12-29 10:49:07 +0800389 ctx->handle = tmp_handle;
390
Eric Anholt9a298b22009-03-24 12:23:04 -0700391 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 if (!ctx_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 DRM_DEBUG("out of memory\n");
394 return -ENOMEM;
395 }
396
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 INIT_LIST_HEAD(&ctx_entry->head);
Eric Anholtc153f452007-09-03 12:06:45 +1000398 ctx_entry->handle = ctx->handle;
Eric Anholt6c340ea2007-08-25 20:23:09 +1000399 ctx_entry->tag = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Dave Airlie30e2fb12006-02-02 19:37:46 +1100401 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000402 list_add(&ctx_entry->head, &dev->ctxlist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100403 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
406}
407
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100408/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * Get context.
410 *
411 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000412 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * \param cmd command.
414 * \param arg user argument pointing to a drm_ctx structure.
415 * \return zero on success or a negative number on failure.
416 */
David Herrmanne7b960702014-07-24 12:10:04 +0200417int drm_legacy_getctx(struct drm_device *dev, void *data,
418 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Eric Anholtc153f452007-09-03 12:06:45 +1000420 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Peter Antoine0e975982015-06-23 08:18:49 +0100422 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200423 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100424 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* This is 0, because we don't handle any context flags */
Eric Anholtc153f452007-09-03 12:06:45 +1000427 ctx->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * Switch context.
434 *
435 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000436 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * \param cmd command.
438 * \param arg user argument pointing to a drm_ctx structure.
439 * \return zero on success or a negative number on failure.
440 *
441 * Calls context_switch().
442 */
David Herrmanne7b960702014-07-24 12:10:04 +0200443int drm_legacy_switchctx(struct drm_device *dev, void *data,
444 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Eric Anholtc153f452007-09-03 12:06:45 +1000446 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Peter Antoine0e975982015-06-23 08:18:49 +0100448 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200449 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100450 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100451
Eric Anholtc153f452007-09-03 12:06:45 +1000452 DRM_DEBUG("%d\n", ctx->handle);
453 return drm_context_switch(dev, dev->last_context, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100456/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * New context.
458 *
459 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000460 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * \param cmd command.
462 * \param arg user argument pointing to a drm_ctx structure.
463 * \return zero on success or a negative number on failure.
464 *
465 * Calls context_switch_complete().
466 */
David Herrmanne7b960702014-07-24 12:10:04 +0200467int drm_legacy_newctx(struct drm_device *dev, void *data,
468 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Eric Anholtc153f452007-09-03 12:06:45 +1000470 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Peter Antoine0e975982015-06-23 08:18:49 +0100472 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200473 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100474 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100475
Eric Anholtc153f452007-09-03 12:06:45 +1000476 DRM_DEBUG("%d\n", ctx->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000477 drm_context_switch_complete(dev, file_priv, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 return 0;
480}
481
Benjamin Gaignardcc994822020-03-06 11:29:37 +0100482/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * Remove context.
484 *
485 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000486 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * \param cmd command.
488 * \param arg user argument pointing to a drm_ctx structure.
489 * \return zero on success or a negative number on failure.
490 *
491 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
492 */
David Herrmanne7b960702014-07-24 12:10:04 +0200493int drm_legacy_rmctx(struct drm_device *dev, void *data,
494 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Eric Anholtc153f452007-09-03 12:06:45 +1000496 struct drm_ctx *ctx = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Peter Antoine0e975982015-06-23 08:18:49 +0100498 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
Daniel Vetterfa538642016-08-03 21:11:10 +0200499 !drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100500 return -EOPNOTSUPP;
Peter Antoine0e975982015-06-23 08:18:49 +0100501
Eric Anholtc153f452007-09-03 12:06:45 +1000502 DRM_DEBUG("%d\n", ctx->handle);
Eric Anholtc153f452007-09-03 12:06:45 +1000503 if (ctx->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (dev->driver->context_dtor)
Eric Anholtc153f452007-09-03 12:06:45 +1000505 dev->driver->context_dtor(dev, ctx->handle);
David Herrmanne7b960702014-07-24 12:10:04 +0200506 drm_legacy_ctxbitmap_free(dev, ctx->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
Dave Airlie30e2fb12006-02-02 19:37:46 +1100509 mutex_lock(&dev->ctxlist_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000510 if (!list_empty(&dev->ctxlist)) {
Dave Airlie55910512007-07-11 16:53:40 +1000511 struct drm_ctx_list *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Dave Airliebd1b3312007-05-26 05:01:51 +1000513 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000514 if (pos->handle == ctx->handle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000515 list_del(&pos->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700516 kfree(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 }
519 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100520 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 return 0;
523}
524
525/*@}*/