1 /** 2 * \file drm_context.c 3 * IOCTLs for generic contexts 4 * 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 #include <sys/cdefs.h> 37 /* 38 * ChangeLog: 39 * 2001-11-16 Torsten Duwe <duwe@caldera.de> 40 * added context constructor/destructor hooks, 41 * needed by SiS driver's memory management. 42 */ 43 44 #include <dev/drm2/drmP.h> 45 46 /******************************************************************/ 47 /** \name Context bitmap support */ 48 /*@{*/ 49 50 /** 51 * 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 57 * in drm_device::ctx_idr, while holding the drm_device::struct_mutex 58 * lock. 59 */ 60 void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle) 61 { 62 if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP || 63 dev->ctx_bitmap == NULL) { 64 DRM_ERROR("Attempt to free invalid context handle: %d\n", 65 ctx_handle); 66 return; 67 } 68 69 DRM_LOCK(dev); 70 clear_bit(ctx_handle, dev->ctx_bitmap); 71 dev->context_sareas[ctx_handle] = NULL; 72 DRM_UNLOCK(dev); 73 } 74 75 /** 76 * Context bitmap allocation. 77 * 78 * \param dev DRM device. 79 * \return (non-negative) context handle on success or a negative number on failure. 80 * 81 * Allocate a new idr from drm_device::ctx_idr while holding the 82 * drm_device::struct_mutex lock. 83 */ 84 static int drm_ctxbitmap_next(struct drm_device * dev) 85 { 86 int bit; 87 88 if (dev->ctx_bitmap == NULL) 89 return -1; 90 91 DRM_LOCK(dev); 92 bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP); 93 if (bit >= DRM_MAX_CTXBITMAP) { 94 DRM_UNLOCK(dev); 95 return -1; 96 } 97 98 set_bit(bit, dev->ctx_bitmap); 99 DRM_DEBUG("bit : %d\n", bit); 100 if ((bit+1) > dev->max_context) { 101 struct drm_local_map **ctx_sareas; 102 int max_ctx = (bit+1); 103 104 ctx_sareas = realloc(dev->context_sareas, 105 max_ctx * sizeof(*dev->context_sareas), 106 DRM_MEM_SAREA, M_NOWAIT); 107 if (ctx_sareas == NULL) { 108 clear_bit(bit, dev->ctx_bitmap); 109 DRM_DEBUG("failed to allocate bit : %d\n", bit); 110 DRM_UNLOCK(dev); 111 return -1; 112 } 113 dev->max_context = max_ctx; 114 dev->context_sareas = ctx_sareas; 115 dev->context_sareas[bit] = NULL; 116 } 117 DRM_UNLOCK(dev); 118 return bit; 119 } 120 121 /** 122 * Context bitmap initialization. 123 * 124 * \param dev DRM device. 125 * 126 * Initialise the drm_device::ctx_idr 127 */ 128 int drm_ctxbitmap_init(struct drm_device * dev) 129 { 130 int i; 131 int temp; 132 133 DRM_LOCK(dev); 134 dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP, 135 M_NOWAIT | M_ZERO); 136 if (dev->ctx_bitmap == NULL) { 137 DRM_UNLOCK(dev); 138 return ENOMEM; 139 } 140 dev->context_sareas = NULL; 141 dev->max_context = -1; 142 DRM_UNLOCK(dev); 143 144 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 145 temp = drm_ctxbitmap_next(dev); 146 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp); 147 } 148 149 return 0; 150 } 151 152 /** 153 * Context bitmap cleanup. 154 * 155 * \param dev DRM device. 156 * 157 * Free all idr members using drm_ctx_sarea_free helper function 158 * while holding the drm_device::struct_mutex lock. 159 */ 160 void drm_ctxbitmap_cleanup(struct drm_device * dev) 161 { 162 DRM_LOCK(dev); 163 if (dev->context_sareas != NULL) 164 free(dev->context_sareas, DRM_MEM_SAREA); 165 free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP); 166 DRM_UNLOCK(dev); 167 } 168 169 /*@}*/ 170 171 /******************************************************************/ 172 /** \name Per Context SAREA Support */ 173 /*@{*/ 174 175 /** 176 * Get per-context SAREA. 177 * 178 * \param inode device inode. 179 * \param file_priv DRM file private. 180 * \param cmd command. 181 * \param arg user argument pointing to a drm_ctx_priv_map structure. 182 * \return zero on success or a negative number on failure. 183 * 184 * Gets the map from drm_device::ctx_idr with the handle specified and 185 * returns its handle. 186 */ 187 int drm_getsareactx(struct drm_device *dev, void *data, 188 struct drm_file *file_priv) 189 { 190 struct drm_ctx_priv_map *request = data; 191 struct drm_local_map *map; 192 193 DRM_LOCK(dev); 194 if (dev->max_context < 0 || 195 request->ctx_id >= (unsigned) dev->max_context) { 196 DRM_UNLOCK(dev); 197 return EINVAL; 198 } 199 200 map = dev->context_sareas[request->ctx_id]; 201 DRM_UNLOCK(dev); 202 203 request->handle = (void *)map->handle; 204 205 return 0; 206 } 207 208 /** 209 * Set per-context SAREA. 210 * 211 * \param inode device inode. 212 * \param file_priv DRM file private. 213 * \param cmd command. 214 * \param arg user argument pointing to a drm_ctx_priv_map structure. 215 * \return zero on success or a negative number on failure. 216 * 217 * Searches the mapping specified in \p arg and update the entry in 218 * drm_device::ctx_idr with it. 219 */ 220 int drm_setsareactx(struct drm_device *dev, void *data, 221 struct drm_file *file_priv) 222 { 223 struct drm_ctx_priv_map *request = data; 224 struct drm_local_map *map = NULL; 225 struct drm_map_list *r_list = NULL; 226 227 DRM_LOCK(dev); 228 list_for_each_entry(r_list, &dev->maplist, head) { 229 if (r_list->map 230 && r_list->user_token == (unsigned long) request->handle) { 231 if (dev->max_context < 0) 232 goto bad; 233 if (request->ctx_id >= (unsigned) dev->max_context) 234 goto bad; 235 dev->context_sareas[request->ctx_id] = map; 236 DRM_UNLOCK(dev); 237 return 0; 238 } 239 } 240 241 bad: 242 DRM_UNLOCK(dev); 243 return EINVAL; 244 } 245 246 /*@}*/ 247 248 /******************************************************************/ 249 /** \name The actual DRM context handling routines */ 250 /*@{*/ 251 252 /** 253 * Switch context. 254 * 255 * \param dev DRM device. 256 * \param old old context handle. 257 * \param new new context handle. 258 * \return zero on success or a negative number on failure. 259 * 260 * Attempt to set drm_device::context_flag. 261 */ 262 static int drm_context_switch(struct drm_device * dev, int old, int new) 263 { 264 if (test_and_set_bit(0, &dev->context_flag)) { 265 DRM_ERROR("Reentering -- FIXME\n"); 266 return -EBUSY; 267 } 268 269 DRM_DEBUG("Context switch from %d to %d\n", old, new); 270 271 if (new == dev->last_context) { 272 clear_bit(0, &dev->context_flag); 273 return 0; 274 } 275 276 return 0; 277 } 278 279 /** 280 * Complete context switch. 281 * 282 * \param dev DRM device. 283 * \param new new context handle. 284 * \return zero on success or a negative number on failure. 285 * 286 * Updates drm_device::last_context and drm_device::last_switch. Verifies the 287 * hardware lock is held, clears the drm_device::context_flag and wakes up 288 * drm_device::context_wait. 289 */ 290 static int drm_context_switch_complete(struct drm_device *dev, 291 struct drm_file *file_priv, int new) 292 { 293 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ 294 dev->last_switch = jiffies; 295 296 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) { 297 DRM_ERROR("Lock isn't held after context switch\n"); 298 } 299 300 /* If a context switch is ever initiated 301 when the kernel holds the lock, release 302 that lock here. */ 303 clear_bit(0, &dev->context_flag); 304 wakeup(&dev->context_wait); 305 306 return 0; 307 } 308 309 /** 310 * Reserve contexts. 311 * 312 * \param inode device inode. 313 * \param file_priv DRM file private. 314 * \param cmd command. 315 * \param arg user argument pointing to a drm_ctx_res structure. 316 * \return zero on success or a negative number on failure. 317 */ 318 int drm_resctx(struct drm_device *dev, void *data, 319 struct drm_file *file_priv) 320 { 321 struct drm_ctx_res *res = data; 322 struct drm_ctx ctx; 323 int i; 324 325 if (res->count >= DRM_RESERVED_CONTEXTS) { 326 memset(&ctx, 0, sizeof(ctx)); 327 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 328 ctx.handle = i; 329 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx))) 330 return -EFAULT; 331 } 332 } 333 res->count = DRM_RESERVED_CONTEXTS; 334 335 return 0; 336 } 337 338 /** 339 * Add context. 340 * 341 * \param inode device inode. 342 * \param file_priv DRM file private. 343 * \param cmd command. 344 * \param arg user argument pointing to a drm_ctx structure. 345 * \return zero on success or a negative number on failure. 346 * 347 * Get a new handle for the context and copy to userspace. 348 */ 349 int drm_addctx(struct drm_device *dev, void *data, 350 struct drm_file *file_priv) 351 { 352 struct drm_ctx_list *ctx_entry; 353 struct drm_ctx *ctx = data; 354 355 ctx->handle = drm_ctxbitmap_next(dev); 356 if (ctx->handle == DRM_KERNEL_CONTEXT) { 357 /* Skip kernel's context and get a new one. */ 358 ctx->handle = drm_ctxbitmap_next(dev); 359 } 360 DRM_DEBUG("%d\n", ctx->handle); 361 if (ctx->handle == -1) { 362 DRM_DEBUG("Not enough free contexts.\n"); 363 /* Should this return -EBUSY instead? */ 364 return -ENOMEM; 365 } 366 367 ctx_entry = malloc(sizeof(*ctx_entry), DRM_MEM_CTXBITMAP, M_NOWAIT); 368 if (!ctx_entry) { 369 DRM_DEBUG("out of memory\n"); 370 return -ENOMEM; 371 } 372 373 INIT_LIST_HEAD(&ctx_entry->head); 374 ctx_entry->handle = ctx->handle; 375 ctx_entry->tag = file_priv; 376 377 DRM_LOCK(dev); 378 list_add(&ctx_entry->head, &dev->ctxlist); 379 ++dev->ctx_count; 380 DRM_UNLOCK(dev); 381 382 return 0; 383 } 384 385 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 386 { 387 /* This does nothing */ 388 return 0; 389 } 390 391 /** 392 * Get context. 393 * 394 * \param inode device inode. 395 * \param file_priv DRM file private. 396 * \param cmd command. 397 * \param arg user argument pointing to a drm_ctx structure. 398 * \return zero on success or a negative number on failure. 399 */ 400 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 401 { 402 struct drm_ctx *ctx = data; 403 404 /* This is 0, because we don't handle any context flags */ 405 ctx->flags = 0; 406 407 return 0; 408 } 409 410 /** 411 * Switch 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 * Calls context_switch(). 420 */ 421 int drm_switchctx(struct drm_device *dev, void *data, 422 struct drm_file *file_priv) 423 { 424 struct drm_ctx *ctx = data; 425 426 DRM_DEBUG("%d\n", ctx->handle); 427 return drm_context_switch(dev, dev->last_context, ctx->handle); 428 } 429 430 /** 431 * New context. 432 * 433 * \param inode device inode. 434 * \param file_priv DRM file private. 435 * \param cmd command. 436 * \param arg user argument pointing to a drm_ctx structure. 437 * \return zero on success or a negative number on failure. 438 * 439 * Calls context_switch_complete(). 440 */ 441 int drm_newctx(struct drm_device *dev, void *data, 442 struct drm_file *file_priv) 443 { 444 struct drm_ctx *ctx = data; 445 446 DRM_DEBUG("%d\n", ctx->handle); 447 drm_context_switch_complete(dev, file_priv, ctx->handle); 448 449 return 0; 450 } 451 452 /** 453 * Remove context. 454 * 455 * \param inode device inode. 456 * \param file_priv DRM file private. 457 * \param cmd command. 458 * \param arg user argument pointing to a drm_ctx structure. 459 * \return zero on success or a negative number on failure. 460 * 461 * If not the special kernel context, calls ctxbitmap_free() to free the specified context. 462 */ 463 int drm_rmctx(struct drm_device *dev, void *data, 464 struct drm_file *file_priv) 465 { 466 struct drm_ctx *ctx = data; 467 468 DRM_DEBUG("%d\n", ctx->handle); 469 if (ctx->handle != DRM_KERNEL_CONTEXT) { 470 if (dev->driver->context_dtor) 471 dev->driver->context_dtor(dev, ctx->handle); 472 drm_ctxbitmap_free(dev, ctx->handle); 473 } 474 475 DRM_LOCK(dev); 476 if (!list_empty(&dev->ctxlist)) { 477 struct drm_ctx_list *pos, *n; 478 479 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { 480 if (pos->handle == ctx->handle) { 481 list_del(&pos->head); 482 free(pos, DRM_MEM_CTXBITMAP); 483 --dev->ctx_count; 484 } 485 } 486 } 487 DRM_UNLOCK(dev); 488 489 return 0; 490 } 491 492 /*@}*/ 493