drm_context.c 13.2 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Legacy: Generic DRM Contexts
L
Linus Torvalds 已提交
3 4 5 6 7
 *
 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
8 9 10
 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
 * Author: Gareth Hughes <gareth@valinux.com>
 *
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

31
#include <drm/drmP.h>
32 33 34 35 36 37 38
#include "drm_legacy.h"

struct drm_ctx_list {
	struct list_head head;
	drm_context_t handle;
	struct drm_file *tag;
};
L
Linus Torvalds 已提交
39

40 41 42 43
/******************************************************************/
/** \name Context bitmap support */
/*@{*/

L
Linus Torvalds 已提交
44 45 46 47 48 49 50
/**
 * Free a handle from the context bitmap.
 *
 * \param dev DRM device.
 * \param ctx_handle context handle.
 *
 * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
51
 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
L
Linus Torvalds 已提交
52 53
 * lock.
 */
54
void drm_legacy_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
L
Linus Torvalds 已提交
55
{
56
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
57
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
58 59
		return;

60 61 62
	mutex_lock(&dev->struct_mutex);
	idr_remove(&dev->ctx_idr, ctx_handle);
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
63 64
}

D
Dave Airlie 已提交
65
/**
L
Linus Torvalds 已提交
66 67 68 69 70
 * Context bitmap allocation.
 *
 * \param dev DRM device.
 * \return (non-negative) context handle on success or a negative number on failure.
 *
71
 * Allocate a new idr from drm_device::ctx_idr while holding the
D
Dave Airlie 已提交
72
 * drm_device::struct_mutex lock.
L
Linus Torvalds 已提交
73
 */
74
static int drm_legacy_ctxbitmap_next(struct drm_device * dev)
L
Linus Torvalds 已提交
75
{
76
	int ret;
L
Linus Torvalds 已提交
77

D
Dave Airlie 已提交
78
	mutex_lock(&dev->struct_mutex);
T
Tejun Heo 已提交
79 80
	ret = idr_alloc(&dev->ctx_idr, NULL, DRM_RESERVED_CONTEXTS, 0,
			GFP_KERNEL);
D
Dave Airlie 已提交
81
	mutex_unlock(&dev->struct_mutex);
T
Tejun Heo 已提交
82
	return ret;
L
Linus Torvalds 已提交
83 84 85 86 87 88 89
}

/**
 * Context bitmap initialization.
 *
 * \param dev DRM device.
 *
90
 * Initialise the drm_device::ctx_idr
L
Linus Torvalds 已提交
91
 */
92
void drm_legacy_ctxbitmap_init(struct drm_device * dev)
L
Linus Torvalds 已提交
93
{
94
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
95
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
96
		return;
97

98
	idr_init(&dev->ctx_idr);
L
Linus Torvalds 已提交
99 100 101 102 103 104 105
}

/**
 * Context bitmap cleanup.
 *
 * \param dev DRM device.
 *
106 107
 * Free all idr members using drm_ctx_sarea_free helper function
 * while holding the drm_device::struct_mutex lock.
L
Linus Torvalds 已提交
108
 */
109
void drm_legacy_ctxbitmap_cleanup(struct drm_device * dev)
L
Linus Torvalds 已提交
110
{
111
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
112
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
113 114
		return;

D
Dave Airlie 已提交
115
	mutex_lock(&dev->struct_mutex);
T
Tejun Heo 已提交
116
	idr_destroy(&dev->ctx_idr);
D
Dave Airlie 已提交
117
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
118 119
}

120 121 122 123 124 125 126 127 128
/**
 * drm_ctxbitmap_flush() - Flush all contexts owned by a file
 * @dev: DRM device to operate on
 * @file: Open file to flush contexts for
 *
 * This iterates over all contexts on @dev and drops them if they're owned by
 * @file. Note that after this call returns, new contexts might be added if
 * the file is still alive.
 */
129
void drm_legacy_ctxbitmap_flush(struct drm_device *dev, struct drm_file *file)
130 131 132
{
	struct drm_ctx_list *pos, *tmp;

133
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
134
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
135 136
		return;

137 138 139 140 141 142 143 144
	mutex_lock(&dev->ctxlist_mutex);

	list_for_each_entry_safe(pos, tmp, &dev->ctxlist, head) {
		if (pos->tag == file &&
		    pos->handle != DRM_KERNEL_CONTEXT) {
			if (dev->driver->context_dtor)
				dev->driver->context_dtor(dev, pos->handle);

145
			drm_legacy_ctxbitmap_free(dev, pos->handle);
146 147 148 149 150 151 152 153
			list_del(&pos->head);
			kfree(pos);
		}
	}

	mutex_unlock(&dev->ctxlist_mutex);
}

L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161
/*@}*/

/******************************************************************/
/** \name Per Context SAREA Support */
/*@{*/

/**
 * Get per-context SAREA.
D
Dave Airlie 已提交
162
 *
L
Linus Torvalds 已提交
163
 * \param inode device inode.
164
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
165 166 167 168
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx_priv_map structure.
 * \return zero on success or a negative number on failure.
 *
169
 * Gets the map from drm_device::ctx_idr with the handle specified and
L
Linus Torvalds 已提交
170 171
 * returns its handle.
 */
172 173
int drm_legacy_getsareactx(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
L
Linus Torvalds 已提交
174
{
175
	struct drm_ctx_priv_map *request = data;
176
	struct drm_local_map *map;
D
Dave Airlie 已提交
177
	struct drm_map_list *_entry;
L
Linus Torvalds 已提交
178

179
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
180
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
181 182
		return -EINVAL;

D
Dave Airlie 已提交
183
	mutex_lock(&dev->struct_mutex);
184

185
	map = idr_find(&dev->ctx_idr, request->ctx_id);
186
	if (!map) {
D
Dave Airlie 已提交
187
		mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
188 189 190
		return -EINVAL;
	}

191
	request->handle = NULL;
192
	list_for_each_entry(_entry, &dev->maplist, head) {
193
		if (_entry->map == map) {
D
Dave Airlie 已提交
194
			request->handle =
D
Dave Airlie 已提交
195
			    (void *)(unsigned long)_entry->user_token;
196 197 198
			break;
		}
	}
199 200 201

	mutex_unlock(&dev->struct_mutex);

202
	if (request->handle == NULL)
203 204
		return -EINVAL;

L
Linus Torvalds 已提交
205 206 207 208 209
	return 0;
}

/**
 * Set per-context SAREA.
D
Dave Airlie 已提交
210
 *
L
Linus Torvalds 已提交
211
 * \param inode device inode.
212
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
213 214 215 216 217
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx_priv_map structure.
 * \return zero on success or a negative number on failure.
 *
 * Searches the mapping specified in \p arg and update the entry in
218
 * drm_device::ctx_idr with it.
L
Linus Torvalds 已提交
219
 */
220 221
int drm_legacy_setsareactx(struct drm_device *dev, void *data,
			   struct drm_file *file_priv)
L
Linus Torvalds 已提交
222
{
223
	struct drm_ctx_priv_map *request = data;
224
	struct drm_local_map *map = NULL;
D
Dave Airlie 已提交
225
	struct drm_map_list *r_list = NULL;
L
Linus Torvalds 已提交
226

227
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
228
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
229 230
		return -EINVAL;

D
Dave Airlie 已提交
231
	mutex_lock(&dev->struct_mutex);
232
	list_for_each_entry(r_list, &dev->maplist, head) {
233
		if (r_list->map
234
		    && r_list->user_token == (unsigned long) request->handle)
L
Linus Torvalds 已提交
235 236
			goto found;
	}
D
Dave Airlie 已提交
237
      bad:
D
Dave Airlie 已提交
238
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
239 240
	return -EINVAL;

D
Dave Airlie 已提交
241
      found:
L
Linus Torvalds 已提交
242
	map = r_list->map;
D
Dave Airlie 已提交
243 244
	if (!map)
		goto bad;
245

246
	if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
L
Linus Torvalds 已提交
247
		goto bad;
248

D
Dave Airlie 已提交
249
	mutex_unlock(&dev->struct_mutex);
250

L
Linus Torvalds 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	return 0;
}

/*@}*/

/******************************************************************/
/** \name The actual DRM context handling routines */
/*@{*/

/**
 * Switch context.
 *
 * \param dev DRM device.
 * \param old old context handle.
 * \param new new context handle.
 * \return zero on success or a negative number on failure.
 *
 * Attempt to set drm_device::context_flag.
 */
270
static int drm_context_switch(struct drm_device * dev, int old, int new)
L
Linus Torvalds 已提交
271
{
D
Dave Airlie 已提交
272 273 274 275
	if (test_and_set_bit(0, &dev->context_flag)) {
		DRM_ERROR("Reentering -- FIXME\n");
		return -EBUSY;
	}
L
Linus Torvalds 已提交
276

D
Dave Airlie 已提交
277
	DRM_DEBUG("Context switch from %d to %d\n", old, new);
L
Linus Torvalds 已提交
278

D
Dave Airlie 已提交
279 280 281 282
	if (new == dev->last_context) {
		clear_bit(0, &dev->context_flag);
		return 0;
	}
L
Linus Torvalds 已提交
283

D
Dave Airlie 已提交
284
	return 0;
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297
}

/**
 * Complete context switch.
 *
 * \param dev DRM device.
 * \param new new context handle.
 * \return zero on success or a negative number on failure.
 *
 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
 * hardware lock is held, clears the drm_device::context_flag and wakes up
 * drm_device::context_wait.
 */
298 299
static int drm_context_switch_complete(struct drm_device *dev,
				       struct drm_file *file_priv, int new)
L
Linus Torvalds 已提交
300
{
D
Dave Airlie 已提交
301
	dev->last_context = new;	/* PRE/POST: This is the _only_ writer. */
L
Linus Torvalds 已提交
302

303
	if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
D
Dave Airlie 已提交
304 305
		DRM_ERROR("Lock isn't held after context switch\n");
	}
L
Linus Torvalds 已提交
306

D
Dave Airlie 已提交
307 308 309 310
	/* If a context switch is ever initiated
	   when the kernel holds the lock, release
	   that lock here. */
	clear_bit(0, &dev->context_flag);
L
Linus Torvalds 已提交
311

D
Dave Airlie 已提交
312
	return 0;
L
Linus Torvalds 已提交
313 314 315 316 317 318
}

/**
 * Reserve contexts.
 *
 * \param inode device inode.
319
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
320 321 322 323
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx_res structure.
 * \return zero on success or a negative number on failure.
 */
324 325
int drm_legacy_resctx(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
L
Linus Torvalds 已提交
326
{
327
	struct drm_ctx_res *res = data;
328
	struct drm_ctx ctx;
L
Linus Torvalds 已提交
329 330
	int i;

331
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
332
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
333 334
		return -EINVAL;

335
	if (res->count >= DRM_RESERVED_CONTEXTS) {
D
Dave Airlie 已提交
336 337
		memset(&ctx, 0, sizeof(ctx));
		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
L
Linus Torvalds 已提交
338
			ctx.handle = i;
339
			if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
L
Linus Torvalds 已提交
340 341 342
				return -EFAULT;
		}
	}
343
	res->count = DRM_RESERVED_CONTEXTS;
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351

	return 0;
}

/**
 * Add context.
 *
 * \param inode device inode.
352
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
353 354 355 356 357 358
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx structure.
 * \return zero on success or a negative number on failure.
 *
 * Get a new handle for the context and copy to userspace.
 */
359 360
int drm_legacy_addctx(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
L
Linus Torvalds 已提交
361
{
D
Dave Airlie 已提交
362
	struct drm_ctx_list *ctx_entry;
363
	struct drm_ctx *ctx = data;
L
Linus Torvalds 已提交
364

365
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
366
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
367 368
		return -EINVAL;

369
	ctx->handle = drm_legacy_ctxbitmap_next(dev);
370
	if (ctx->handle == DRM_KERNEL_CONTEXT) {
D
Dave Airlie 已提交
371
		/* Skip kernel's context and get a new one. */
372
		ctx->handle = drm_legacy_ctxbitmap_next(dev);
L
Linus Torvalds 已提交
373
	}
374
	DRM_DEBUG("%d\n", ctx->handle);
375
	if (ctx->handle < 0) {
D
Dave Airlie 已提交
376 377
		DRM_DEBUG("Not enough free contexts.\n");
		/* Should this return -EBUSY instead? */
L
Linus Torvalds 已提交
378 379 380
		return -ENOMEM;
	}

381
	ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
D
Dave Airlie 已提交
382
	if (!ctx_entry) {
L
Linus Torvalds 已提交
383 384 385 386
		DRM_DEBUG("out of memory\n");
		return -ENOMEM;
	}

D
Dave Airlie 已提交
387
	INIT_LIST_HEAD(&ctx_entry->head);
388
	ctx_entry->handle = ctx->handle;
389
	ctx_entry->tag = file_priv;
L
Linus Torvalds 已提交
390

D
Dave Airlie 已提交
391
	mutex_lock(&dev->ctxlist_mutex);
392
	list_add(&ctx_entry->head, &dev->ctxlist);
D
Dave Airlie 已提交
393
	mutex_unlock(&dev->ctxlist_mutex);
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401

	return 0;
}

/**
 * Get context.
 *
 * \param inode device inode.
402
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
403 404 405 406
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx structure.
 * \return zero on success or a negative number on failure.
 */
407 408
int drm_legacy_getctx(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
L
Linus Torvalds 已提交
409
{
410
	struct drm_ctx *ctx = data;
L
Linus Torvalds 已提交
411

412
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
413
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
414 415
		return -EINVAL;

L
Linus Torvalds 已提交
416
	/* This is 0, because we don't handle any context flags */
417
	ctx->flags = 0;
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425

	return 0;
}

/**
 * Switch context.
 *
 * \param inode device inode.
426
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
427 428 429 430 431 432
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx structure.
 * \return zero on success or a negative number on failure.
 *
 * Calls context_switch().
 */
433 434
int drm_legacy_switchctx(struct drm_device *dev, void *data,
			 struct drm_file *file_priv)
L
Linus Torvalds 已提交
435
{
436
	struct drm_ctx *ctx = data;
L
Linus Torvalds 已提交
437

438
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
439
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
440 441
		return -EINVAL;

442 443
	DRM_DEBUG("%d\n", ctx->handle);
	return drm_context_switch(dev, dev->last_context, ctx->handle);
L
Linus Torvalds 已提交
444 445 446 447 448 449
}

/**
 * New context.
 *
 * \param inode device inode.
450
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
451 452 453 454 455 456
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx structure.
 * \return zero on success or a negative number on failure.
 *
 * Calls context_switch_complete().
 */
457 458
int drm_legacy_newctx(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
L
Linus Torvalds 已提交
459
{
460
	struct drm_ctx *ctx = data;
L
Linus Torvalds 已提交
461

462
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
463
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
464 465
		return -EINVAL;

466
	DRM_DEBUG("%d\n", ctx->handle);
467
	drm_context_switch_complete(dev, file_priv, ctx->handle);
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475

	return 0;
}

/**
 * Remove context.
 *
 * \param inode device inode.
476
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
477 478 479 480 481 482
 * \param cmd command.
 * \param arg user argument pointing to a drm_ctx structure.
 * \return zero on success or a negative number on failure.
 *
 * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
 */
483 484
int drm_legacy_rmctx(struct drm_device *dev, void *data,
		     struct drm_file *file_priv)
L
Linus Torvalds 已提交
485
{
486
	struct drm_ctx *ctx = data;
L
Linus Torvalds 已提交
487

488
	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
489
	    !drm_core_check_feature(dev, DRIVER_LEGACY))
490 491
		return -EINVAL;

492 493
	DRM_DEBUG("%d\n", ctx->handle);
	if (ctx->handle != DRM_KERNEL_CONTEXT) {
L
Linus Torvalds 已提交
494
		if (dev->driver->context_dtor)
495
			dev->driver->context_dtor(dev, ctx->handle);
496
		drm_legacy_ctxbitmap_free(dev, ctx->handle);
L
Linus Torvalds 已提交
497 498
	}

D
Dave Airlie 已提交
499
	mutex_lock(&dev->ctxlist_mutex);
500
	if (!list_empty(&dev->ctxlist)) {
D
Dave Airlie 已提交
501
		struct drm_ctx_list *pos, *n;
L
Linus Torvalds 已提交
502

503
		list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
504
			if (pos->handle == ctx->handle) {
D
Dave Airlie 已提交
505
				list_del(&pos->head);
506
				kfree(pos);
L
Linus Torvalds 已提交
507 508 509
			}
		}
	}
D
Dave Airlie 已提交
510
	mutex_unlock(&dev->ctxlist_mutex);
L
Linus Torvalds 已提交
511 512 513 514 515

	return 0;
}

/*@}*/