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