drm_context.c revision 1.1.1.3 1 /* $NetBSD: drm_context.c,v 1.1.1.3 2018/08/27 01:34:40 riastradh Exp $ */
2
3 /*
4 * Legacy: Generic DRM Contexts
5 *
6 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
7 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * All Rights Reserved.
9 *
10 * Author: Rickard E. (Rik) Faith <faith (at) valinux.com>
11 * Author: Gareth Hughes <gareth (at) valinux.com>
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice (including the next
21 * paragraph) shall be included in all copies or substantial portions of the
22 * Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: drm_context.c,v 1.1.1.3 2018/08/27 01:34:40 riastradh Exp $");
35
36 #include <drm/drmP.h>
37 #include "drm_legacy.h"
38
39 struct drm_ctx_list {
40 struct list_head head;
41 drm_context_t handle;
42 struct drm_file *tag;
43 };
44
45 /******************************************************************/
46 /** \name Context bitmap support */
47 /*@{*/
48
49 /**
50 * Free a handle from the context bitmap.
51 *
52 * \param dev DRM device.
53 * \param ctx_handle context handle.
54 *
55 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
56 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
57 * lock.
58 */
59 void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
60 {
61 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
62 drm_core_check_feature(dev, DRIVER_MODESET))
63 return;
64
65 mutex_lock(&dev->struct_mutex);
66 idr_remove(&dev->ctx_idr, ctx_handle);
67 mutex_unlock(&dev->struct_mutex);
68 }
69
70 /**
71 * Context bitmap allocation.
72 *
73 * \param dev DRM device.
74 * \return (non-negative) context handle on success or a negative number on failure.
75 *
76 * Allocate a new idr from drm_device::ctx_idr while holding the
77 * drm_device::struct_mutex lock.
78 */
79 static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
80 {
81 int ret;
82
83 mutex_lock(&dev->struct_mutex);
84 ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
85 GFP_KERNEL);
86 mutex_unlock(&dev->struct_mutex);
87 return ret;
88 }
89
90 /**
91 * Context bitmap initialization.
92 *
93 * \param dev DRM device.
94 *
95 * Initialise the drm_device::ctx_idr
96 */
97 void drm_legacy_ctxbitmap_init(struct drm_device * dev)
98 {
99 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
100 drm_core_check_feature(dev, DRIVER_MODESET))
101 return;
102
103 idr_init(&dev->ctx_idr);
104 }
105
106 /**
107 * Context bitmap cleanup.
108 *
109 * \param dev DRM device.
110 *
111 * Free all idr members using drm_ctx_sarea_free helper function
112 * while holding the drm_device::struct_mutex lock.
113 */
114 void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
115 {
116 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
117 drm_core_check_feature(dev, DRIVER_MODESET))
118 return;
119
120 mutex_lock(&dev->struct_mutex);
121 idr_destroy(&dev->ctx_idr);
122 mutex_unlock(&dev->struct_mutex);
123 }
124
125 /**
126 * drm_ctxbitmap_flush() - Flush all contexts owned by a file
127 * @dev: DRM device to operate on
128 * @file: Open file to flush contexts for
129 *
130 * This iterates over all contexts on @dev and drops them if they're owned by
131 * @file. Note that after this call returns, new contexts might be added if
132 * the file is still alive.
133 */
134 void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
135 {
136 struct drm_ctx_list *pos, *tmp;
137
138 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
139 drm_core_check_feature(dev, DRIVER_MODESET))
140 return;
141
142 mutex_lock(&dev->ctxlist_mutex);
143
144 list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
145 if (pos->tag == file &&
146 pos->handle != DRM_KERNEL_CONTEXT) {
147 if (dev->driver->context_dtor)
148 dev->driver->context_dtor(dev, pos->handle);
149
150 drm_legacy_ctxbitmap_free(dev, pos->handle);
151 list_del(&pos->head);
152 kfree(pos);
153 }
154 }
155
156 mutex_unlock(&dev->ctxlist_mutex);
157 }
158
159 /*@}*/
160
161 /******************************************************************/
162 /** \name Per Context SAREA Support */
163 /*@{*/
164
165 /**
166 * Get per-context SAREA.
167 *
168 * \param inode device inode.
169 * \param file_priv DRM file private.
170 * \param cmd command.
171 * \param arg user argument pointing to a drm_ctx_priv_map structure.
172 * \return zero on success or a negative number on failure.
173 *
174 * Gets the map from drm_device::ctx_idr with the handle specified and
175 * returns its handle.
176 */
177 int drm_legacy_getsareactx(struct drm_device *dev, void *data,
178 struct drm_file *file_priv)
179 {
180 struct drm_ctx_priv_map *request = data;
181 struct drm_local_map *map;
182 struct drm_map_list *_entry;
183
184 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
185 drm_core_check_feature(dev, DRIVER_MODESET))
186 return -EINVAL;
187
188 mutex_lock(&dev->struct_mutex);
189
190 map = idr_find(&dev->ctx_idr, request->ctx_id);
191 if (!map) {
192 mutex_unlock(&dev->struct_mutex);
193 return -EINVAL;
194 }
195
196 request->handle = NULL;
197 list_for_each_entry(_entry, &dev->maplist, head) {
198 if (_entry->map == map) {
199 request->handle =
200 (void *)(unsigned long)_entry->user_token;
201 break;
202 }
203 }
204
205 mutex_unlock(&dev->struct_mutex);
206
207 if (request->handle == NULL)
208 return -EINVAL;
209
210 return 0;
211 }
212
213 /**
214 * Set per-context SAREA.
215 *
216 * \param inode device inode.
217 * \param file_priv DRM file private.
218 * \param cmd command.
219 * \param arg user argument pointing to a drm_ctx_priv_map structure.
220 * \return zero on success or a negative number on failure.
221 *
222 * Searches the mapping specified in \p arg and update the entry in
223 * drm_device::ctx_idr with it.
224 */
225 int drm_legacy_setsareactx(struct drm_device *dev, void *data,
226 struct drm_file *file_priv)
227 {
228 struct drm_ctx_priv_map *request = data;
229 struct drm_local_map *map = NULL;
230 struct drm_map_list *r_list = NULL;
231
232 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
233 drm_core_check_feature(dev, DRIVER_MODESET))
234 return -EINVAL;
235
236 mutex_lock(&dev->struct_mutex);
237 list_for_each_entry(r_list, &dev->maplist, head) {
238 if (r_list->map
239 && r_list->user_token == (unsigned long) request->handle)
240 goto found;
241 }
242 bad:
243 mutex_unlock(&dev->struct_mutex);
244 return -EINVAL;
245
246 found:
247 map = r_list->map;
248 if (!map)
249 goto bad;
250
251 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
252 goto bad;
253
254 mutex_unlock(&dev->struct_mutex);
255
256 return 0;
257 }
258
259 /*@}*/
260
261 /******************************************************************/
262 /** \name The actual DRM context handling routines */
263 /*@{*/
264
265 /**
266 * Switch context.
267 *
268 * \param dev DRM device.
269 * \param old old context handle.
270 * \param new new context handle.
271 * \return zero on success or a negative number on failure.
272 *
273 * Attempt to set drm_device::context_flag.
274 */
275 static int drm_context_switch(struct drm_device * dev, int old, int new)
276 {
277 if (test_and_set_bit(0, &dev->context_flag)) {
278 DRM_ERROR("Reentering -- FIXME\n");
279 return -EBUSY;
280 }
281
282 DRM_DEBUG("Context switch from %d to %d\n", old, new);
283
284 if (new == dev->last_context) {
285 clear_bit(0, &dev->context_flag);
286 return 0;
287 }
288
289 return 0;
290 }
291
292 /**
293 * Complete context switch.
294 *
295 * \param dev DRM device.
296 * \param new new context handle.
297 * \return zero on success or a negative number on failure.
298 *
299 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
300 * hardware lock is held, clears the drm_device::context_flag and wakes up
301 * drm_device::context_wait.
302 */
303 static int drm_context_switch_complete(struct drm_device *dev,
304 struct drm_file *file_priv, int new)
305 {
306 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
307
308 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
309 DRM_ERROR("Lock isn't held after context switch\n");
310 }
311
312 /* If a context switch is ever initiated
313 when the kernel holds the lock, release
314 that lock here. */
315 clear_bit(0, &dev->context_flag);
316
317 return 0;
318 }
319
320 /**
321 * Reserve contexts.
322 *
323 * \param inode device inode.
324 * \param file_priv DRM file private.
325 * \param cmd command.
326 * \param arg user argument pointing to a drm_ctx_res structure.
327 * \return zero on success or a negative number on failure.
328 */
329 int drm_legacy_resctx(struct drm_device *dev, void *data,
330 struct drm_file *file_priv)
331 {
332 struct drm_ctx_res *res = data;
333 struct drm_ctx ctx;
334 int i;
335
336 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
337 drm_core_check_feature(dev, DRIVER_MODESET))
338 return -EINVAL;
339
340 if (res->count >= DRM_RESERVED_CONTEXTS) {
341 memset(&ctx, 0, sizeof(ctx));
342 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
343 ctx.handle = i;
344 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
345 return -EFAULT;
346 }
347 }
348 res->count = DRM_RESERVED_CONTEXTS;
349
350 return 0;
351 }
352
353 /**
354 * Add context.
355 *
356 * \param inode device inode.
357 * \param file_priv DRM file private.
358 * \param cmd command.
359 * \param arg user argument pointing to a drm_ctx structure.
360 * \return zero on success or a negative number on failure.
361 *
362 * Get a new handle for the context and copy to userspace.
363 */
364 int drm_legacy_addctx(struct drm_device *dev, void *data,
365 struct drm_file *file_priv)
366 {
367 struct drm_ctx_list *ctx_entry;
368 struct drm_ctx *ctx = data;
369
370 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
371 drm_core_check_feature(dev, DRIVER_MODESET))
372 return -EINVAL;
373
374 ctx->handle = drm_legacy_ctxbitmap_next(dev);
375 if (ctx->handle == DRM_KERNEL_CONTEXT) {
376 /* Skip kernel's context and get a new one. */
377 ctx->handle = drm_legacy_ctxbitmap_next(dev);
378 }
379 DRM_DEBUG("%d\n", ctx->handle);
380 if (ctx->handle == -1) {
381 DRM_DEBUG("Not enough free contexts.\n");
382 /* Should this return -EBUSY instead? */
383 return -ENOMEM;
384 }
385
386 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
387 if (!ctx_entry) {
388 DRM_DEBUG("out of memory\n");
389 return -ENOMEM;
390 }
391
392 INIT_LIST_HEAD(&ctx_entry->head);
393 ctx_entry->handle = ctx->handle;
394 ctx_entry->tag = file_priv;
395
396 mutex_lock(&dev->ctxlist_mutex);
397 list_add(&ctx_entry->head, &dev->ctxlist);
398 mutex_unlock(&dev->ctxlist_mutex);
399
400 return 0;
401 }
402
403 /**
404 * Get context.
405 *
406 * \param inode device inode.
407 * \param file_priv DRM file private.
408 * \param cmd command.
409 * \param arg user argument pointing to a drm_ctx structure.
410 * \return zero on success or a negative number on failure.
411 */
412 int drm_legacy_getctx(struct drm_device *dev, void *data,
413 struct drm_file *file_priv)
414 {
415 struct drm_ctx *ctx = data;
416
417 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
418 drm_core_check_feature(dev, DRIVER_MODESET))
419 return -EINVAL;
420
421 /* This is 0, because we don't handle any context flags */
422 ctx->flags = 0;
423
424 return 0;
425 }
426
427 /**
428 * Switch context.
429 *
430 * \param inode device inode.
431 * \param file_priv DRM file private.
432 * \param cmd command.
433 * \param arg user argument pointing to a drm_ctx structure.
434 * \return zero on success or a negative number on failure.
435 *
436 * Calls context_switch().
437 */
438 int drm_legacy_switchctx(struct drm_device *dev, void *data,
439 struct drm_file *file_priv)
440 {
441 struct drm_ctx *ctx = data;
442
443 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
444 drm_core_check_feature(dev, DRIVER_MODESET))
445 return -EINVAL;
446
447 DRM_DEBUG("%d\n", ctx->handle);
448 return drm_context_switch(dev, dev->last_context, ctx->handle);
449 }
450
451 /**
452 * New context.
453 *
454 * \param inode device inode.
455 * \param file_priv DRM file private.
456 * \param cmd command.
457 * \param arg user argument pointing to a drm_ctx structure.
458 * \return zero on success or a negative number on failure.
459 *
460 * Calls context_switch_complete().
461 */
462 int drm_legacy_newctx(struct drm_device *dev, void *data,
463 struct drm_file *file_priv)
464 {
465 struct drm_ctx *ctx = data;
466
467 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
468 drm_core_check_feature(dev, DRIVER_MODESET))
469 return -EINVAL;
470
471 DRM_DEBUG("%d\n", ctx->handle);
472 drm_context_switch_complete(dev, file_priv, ctx->handle);
473
474 return 0;
475 }
476
477 /**
478 * Remove context.
479 *
480 * \param inode device inode.
481 * \param file_priv DRM file private.
482 * \param cmd command.
483 * \param arg user argument pointing to a drm_ctx structure.
484 * \return zero on success or a negative number on failure.
485 *
486 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
487 */
488 int drm_legacy_rmctx(struct drm_device *dev, void *data,
489 struct drm_file *file_priv)
490 {
491 struct drm_ctx *ctx = data;
492
493 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
494 drm_core_check_feature(dev, DRIVER_MODESET))
495 return -EINVAL;
496
497 DRM_DEBUG("%d\n", ctx->handle);
498 if (ctx->handle != DRM_KERNEL_CONTEXT) {
499 if (dev->driver->context_dtor)
500 dev->driver->context_dtor(dev, ctx->handle);
501 drm_legacy_ctxbitmap_free(dev, ctx->handle);
502 }
503
504 mutex_lock(&dev->ctxlist_mutex);
505 if (!list_empty(&dev->ctxlist)) {
506 struct drm_ctx_list *pos, *n;
507
508 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
509 if (pos->handle == ctx->handle) {
510 list_del(&pos->head);
511 kfree(pos);
512 }
513 }
514 }
515 mutex_unlock(&dev->ctxlist_mutex);
516
517 return 0;
518 }
519
520 /*@}*/
521