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