1282199Sdumbbell/** 2282199Sdumbbell * \file drm_context.c 3282199Sdumbbell * IOCTLs for generic contexts 4282199Sdumbbell * 5282199Sdumbbell * \author Rickard E. (Rik) Faith <faith@valinux.com> 6282199Sdumbbell * \author Gareth Hughes <gareth@valinux.com> 7282199Sdumbbell */ 8282199Sdumbbell 9282199Sdumbbell/* 10282199Sdumbbell * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com 11282199Sdumbbell * 12235783Skib * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. 13235783Skib * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 14235783Skib * All Rights Reserved. 15235783Skib * 16235783Skib * Permission is hereby granted, free of charge, to any person obtaining a 17235783Skib * copy of this software and associated documentation files (the "Software"), 18235783Skib * to deal in the Software without restriction, including without limitation 19235783Skib * the rights to use, copy, modify, merge, publish, distribute, sublicense, 20235783Skib * and/or sell copies of the Software, and to permit persons to whom the 21235783Skib * Software is furnished to do so, subject to the following conditions: 22235783Skib * 23235783Skib * The above copyright notice and this permission notice (including the next 24235783Skib * paragraph) shall be included in all copies or substantial portions of the 25235783Skib * Software. 26235783Skib * 27235783Skib * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28235783Skib * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29235783Skib * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30235783Skib * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 31235783Skib * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 32235783Skib * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 33235783Skib * OTHER DEALINGS IN THE SOFTWARE. 34235783Skib */ 35235783Skib 36235783Skib#include <sys/cdefs.h> 37235783Skib__FBSDID("$FreeBSD: releng/10.2/sys/dev/drm2/drm_context.c 282199 2015-04-28 19:35:05Z dumbbell $"); 38235783Skib 39282199Sdumbbell/* 40282199Sdumbbell * ChangeLog: 41282199Sdumbbell * 2001-11-16 Torsten Duwe <duwe@caldera.de> 42282199Sdumbbell * added context constructor/destructor hooks, 43282199Sdumbbell * needed by SiS driver's memory management. 44235783Skib */ 45235783Skib 46235783Skib#include <dev/drm2/drmP.h> 47235783Skib 48282199Sdumbbell/******************************************************************/ 49282199Sdumbbell/** \name Context bitmap support */ 50282199Sdumbbell/*@{*/ 51282199Sdumbbell 52282199Sdumbbell/** 53282199Sdumbbell * Free a handle from the context bitmap. 54282199Sdumbbell * 55282199Sdumbbell * \param dev DRM device. 56282199Sdumbbell * \param ctx_handle context handle. 57282199Sdumbbell * 58282199Sdumbbell * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry 59282199Sdumbbell * in drm_device::ctx_idr, while holding the drm_device::struct_mutex 60282199Sdumbbell * lock. 61235783Skib */ 62282199Sdumbbellvoid drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle) 63235783Skib{ 64282199Sdumbbell if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP || 65235783Skib dev->ctx_bitmap == NULL) { 66235783Skib DRM_ERROR("Attempt to free invalid context handle: %d\n", 67235783Skib ctx_handle); 68235783Skib return; 69235783Skib } 70235783Skib 71235783Skib DRM_LOCK(dev); 72235783Skib clear_bit(ctx_handle, dev->ctx_bitmap); 73235783Skib dev->context_sareas[ctx_handle] = NULL; 74235783Skib DRM_UNLOCK(dev); 75235783Skib} 76235783Skib 77282199Sdumbbell/** 78282199Sdumbbell * Context bitmap allocation. 79282199Sdumbbell * 80282199Sdumbbell * \param dev DRM device. 81282199Sdumbbell * \return (non-negative) context handle on success or a negative number on failure. 82282199Sdumbbell * 83282199Sdumbbell * Allocate a new idr from drm_device::ctx_idr while holding the 84282199Sdumbbell * drm_device::struct_mutex lock. 85282199Sdumbbell */ 86282199Sdumbbellstatic int drm_ctxbitmap_next(struct drm_device * dev) 87235783Skib{ 88235783Skib int bit; 89235783Skib 90235783Skib if (dev->ctx_bitmap == NULL) 91235783Skib return -1; 92235783Skib 93235783Skib DRM_LOCK(dev); 94235783Skib bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP); 95235783Skib if (bit >= DRM_MAX_CTXBITMAP) { 96235783Skib DRM_UNLOCK(dev); 97235783Skib return -1; 98235783Skib } 99235783Skib 100235783Skib set_bit(bit, dev->ctx_bitmap); 101235783Skib DRM_DEBUG("bit : %d\n", bit); 102235783Skib if ((bit+1) > dev->max_context) { 103282199Sdumbbell struct drm_local_map **ctx_sareas; 104235783Skib int max_ctx = (bit+1); 105235783Skib 106235783Skib ctx_sareas = realloc(dev->context_sareas, 107235783Skib max_ctx * sizeof(*dev->context_sareas), 108235783Skib DRM_MEM_SAREA, M_NOWAIT); 109235783Skib if (ctx_sareas == NULL) { 110235783Skib clear_bit(bit, dev->ctx_bitmap); 111235783Skib DRM_DEBUG("failed to allocate bit : %d\n", bit); 112235783Skib DRM_UNLOCK(dev); 113235783Skib return -1; 114235783Skib } 115235783Skib dev->max_context = max_ctx; 116235783Skib dev->context_sareas = ctx_sareas; 117235783Skib dev->context_sareas[bit] = NULL; 118235783Skib } 119235783Skib DRM_UNLOCK(dev); 120235783Skib return bit; 121235783Skib} 122235783Skib 123282199Sdumbbell/** 124282199Sdumbbell * Context bitmap initialization. 125282199Sdumbbell * 126282199Sdumbbell * \param dev DRM device. 127282199Sdumbbell * 128282199Sdumbbell * Initialise the drm_device::ctx_idr 129282199Sdumbbell */ 130282199Sdumbbellint drm_ctxbitmap_init(struct drm_device * dev) 131235783Skib{ 132235783Skib int i; 133235783Skib int temp; 134235783Skib 135235783Skib DRM_LOCK(dev); 136235783Skib dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP, 137235783Skib M_NOWAIT | M_ZERO); 138235783Skib if (dev->ctx_bitmap == NULL) { 139235783Skib DRM_UNLOCK(dev); 140235783Skib return ENOMEM; 141235783Skib } 142235783Skib dev->context_sareas = NULL; 143235783Skib dev->max_context = -1; 144235783Skib DRM_UNLOCK(dev); 145235783Skib 146235783Skib for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 147235783Skib temp = drm_ctxbitmap_next(dev); 148235783Skib DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp); 149235783Skib } 150235783Skib 151235783Skib return 0; 152235783Skib} 153235783Skib 154282199Sdumbbell/** 155282199Sdumbbell * Context bitmap cleanup. 156282199Sdumbbell * 157282199Sdumbbell * \param dev DRM device. 158282199Sdumbbell * 159282199Sdumbbell * Free all idr members using drm_ctx_sarea_free helper function 160282199Sdumbbell * while holding the drm_device::struct_mutex lock. 161282199Sdumbbell */ 162282199Sdumbbellvoid drm_ctxbitmap_cleanup(struct drm_device * dev) 163235783Skib{ 164235783Skib DRM_LOCK(dev); 165235783Skib if (dev->context_sareas != NULL) 166235783Skib free(dev->context_sareas, DRM_MEM_SAREA); 167235783Skib free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP); 168235783Skib DRM_UNLOCK(dev); 169235783Skib} 170235783Skib 171282199Sdumbbell/*@}*/ 172282199Sdumbbell 173282199Sdumbbell/******************************************************************/ 174282199Sdumbbell/** \name Per Context SAREA Support */ 175282199Sdumbbell/*@{*/ 176282199Sdumbbell 177282199Sdumbbell/** 178282199Sdumbbell * Get per-context SAREA. 179282199Sdumbbell * 180282199Sdumbbell * \param inode device inode. 181282199Sdumbbell * \param file_priv DRM file private. 182282199Sdumbbell * \param cmd command. 183282199Sdumbbell * \param arg user argument pointing to a drm_ctx_priv_map structure. 184282199Sdumbbell * \return zero on success or a negative number on failure. 185282199Sdumbbell * 186282199Sdumbbell * Gets the map from drm_device::ctx_idr with the handle specified and 187282199Sdumbbell * returns its handle. 188235783Skib */ 189235783Skibint drm_getsareactx(struct drm_device *dev, void *data, 190235783Skib struct drm_file *file_priv) 191235783Skib{ 192235783Skib struct drm_ctx_priv_map *request = data; 193282199Sdumbbell struct drm_local_map *map; 194235783Skib 195235783Skib DRM_LOCK(dev); 196235783Skib if (dev->max_context < 0 || 197235783Skib request->ctx_id >= (unsigned) dev->max_context) { 198235783Skib DRM_UNLOCK(dev); 199235783Skib return EINVAL; 200235783Skib } 201235783Skib 202235783Skib map = dev->context_sareas[request->ctx_id]; 203235783Skib DRM_UNLOCK(dev); 204235783Skib 205235783Skib request->handle = (void *)map->handle; 206235783Skib 207235783Skib return 0; 208235783Skib} 209235783Skib 210282199Sdumbbell/** 211282199Sdumbbell * Set per-context SAREA. 212282199Sdumbbell * 213282199Sdumbbell * \param inode device inode. 214282199Sdumbbell * \param file_priv DRM file private. 215282199Sdumbbell * \param cmd command. 216282199Sdumbbell * \param arg user argument pointing to a drm_ctx_priv_map structure. 217282199Sdumbbell * \return zero on success or a negative number on failure. 218282199Sdumbbell * 219282199Sdumbbell * Searches the mapping specified in \p arg and update the entry in 220282199Sdumbbell * drm_device::ctx_idr with it. 221282199Sdumbbell */ 222235783Skibint drm_setsareactx(struct drm_device *dev, void *data, 223235783Skib struct drm_file *file_priv) 224235783Skib{ 225235783Skib struct drm_ctx_priv_map *request = data; 226282199Sdumbbell struct drm_local_map *map = NULL; 227282199Sdumbbell struct drm_map_list *r_list = NULL; 228235783Skib 229235783Skib DRM_LOCK(dev); 230282199Sdumbbell list_for_each_entry(r_list, &dev->maplist, head) { 231282199Sdumbbell if (r_list->map 232282199Sdumbbell && r_list->user_token == (unsigned long) request->handle) { 233235783Skib if (dev->max_context < 0) 234235783Skib goto bad; 235235783Skib if (request->ctx_id >= (unsigned) dev->max_context) 236235783Skib goto bad; 237235783Skib dev->context_sareas[request->ctx_id] = map; 238235783Skib DRM_UNLOCK(dev); 239235783Skib return 0; 240235783Skib } 241235783Skib } 242235783Skib 243235783Skibbad: 244235783Skib DRM_UNLOCK(dev); 245235783Skib return EINVAL; 246235783Skib} 247235783Skib 248282199Sdumbbell/*@}*/ 249282199Sdumbbell 250282199Sdumbbell/******************************************************************/ 251282199Sdumbbell/** \name The actual DRM context handling routines */ 252282199Sdumbbell/*@{*/ 253282199Sdumbbell 254282199Sdumbbell/** 255282199Sdumbbell * Switch context. 256282199Sdumbbell * 257282199Sdumbbell * \param dev DRM device. 258282199Sdumbbell * \param old old context handle. 259282199Sdumbbell * \param new new context handle. 260282199Sdumbbell * \return zero on success or a negative number on failure. 261282199Sdumbbell * 262282199Sdumbbell * Attempt to set drm_device::context_flag. 263235783Skib */ 264282199Sdumbbellstatic int drm_context_switch(struct drm_device * dev, int old, int new) 265235783Skib{ 266282199Sdumbbell if (test_and_set_bit(0, &dev->context_flag)) { 267235783Skib DRM_ERROR("Reentering -- FIXME\n"); 268282199Sdumbbell return -EBUSY; 269235783Skib } 270235783Skib 271235783Skib DRM_DEBUG("Context switch from %d to %d\n", old, new); 272235783Skib 273235783Skib if (new == dev->last_context) { 274282199Sdumbbell clear_bit(0, &dev->context_flag); 275235783Skib return 0; 276235783Skib } 277235783Skib 278235783Skib return 0; 279235783Skib} 280235783Skib 281282199Sdumbbell/** 282282199Sdumbbell * Complete context switch. 283282199Sdumbbell * 284282199Sdumbbell * \param dev DRM device. 285282199Sdumbbell * \param new new context handle. 286282199Sdumbbell * \return zero on success or a negative number on failure. 287282199Sdumbbell * 288282199Sdumbbell * Updates drm_device::last_context and drm_device::last_switch. Verifies the 289282199Sdumbbell * hardware lock is held, clears the drm_device::context_flag and wakes up 290282199Sdumbbell * drm_device::context_wait. 291282199Sdumbbell */ 292282199Sdumbbellstatic int drm_context_switch_complete(struct drm_device *dev, 293282199Sdumbbell struct drm_file *file_priv, int new) 294235783Skib{ 295282199Sdumbbell dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ 296282199Sdumbbell dev->last_switch = jiffies; 297235783Skib 298282199Sdumbbell if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) { 299235783Skib DRM_ERROR("Lock isn't held after context switch\n"); 300235783Skib } 301235783Skib 302235783Skib /* If a context switch is ever initiated 303235783Skib when the kernel holds the lock, release 304235783Skib that lock here. */ 305282199Sdumbbell clear_bit(0, &dev->context_flag); 306282199Sdumbbell wakeup(&dev->context_wait); 307235783Skib 308235783Skib return 0; 309235783Skib} 310235783Skib 311282199Sdumbbell/** 312282199Sdumbbell * Reserve contexts. 313282199Sdumbbell * 314282199Sdumbbell * \param inode device inode. 315282199Sdumbbell * \param file_priv DRM file private. 316282199Sdumbbell * \param cmd command. 317282199Sdumbbell * \param arg user argument pointing to a drm_ctx_res structure. 318282199Sdumbbell * \return zero on success or a negative number on failure. 319282199Sdumbbell */ 320282199Sdumbbellint drm_resctx(struct drm_device *dev, void *data, 321282199Sdumbbell struct drm_file *file_priv) 322235783Skib{ 323235783Skib struct drm_ctx_res *res = data; 324235783Skib struct drm_ctx ctx; 325235783Skib int i; 326235783Skib 327235783Skib if (res->count >= DRM_RESERVED_CONTEXTS) { 328282199Sdumbbell memset(&ctx, 0, sizeof(ctx)); 329235783Skib for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 330235783Skib ctx.handle = i; 331282199Sdumbbell if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx))) 332282199Sdumbbell return -EFAULT; 333235783Skib } 334235783Skib } 335235783Skib res->count = DRM_RESERVED_CONTEXTS; 336235783Skib 337235783Skib return 0; 338235783Skib} 339235783Skib 340282199Sdumbbell/** 341282199Sdumbbell * Add context. 342282199Sdumbbell * 343282199Sdumbbell * \param inode device inode. 344282199Sdumbbell * \param file_priv DRM file private. 345282199Sdumbbell * \param cmd command. 346282199Sdumbbell * \param arg user argument pointing to a drm_ctx structure. 347282199Sdumbbell * \return zero on success or a negative number on failure. 348282199Sdumbbell * 349282199Sdumbbell * Get a new handle for the context and copy to userspace. 350282199Sdumbbell */ 351282199Sdumbbellint drm_addctx(struct drm_device *dev, void *data, 352282199Sdumbbell struct drm_file *file_priv) 353235783Skib{ 354282199Sdumbbell struct drm_ctx_list *ctx_entry; 355235783Skib struct drm_ctx *ctx = data; 356235783Skib 357235783Skib ctx->handle = drm_ctxbitmap_next(dev); 358235783Skib if (ctx->handle == DRM_KERNEL_CONTEXT) { 359235783Skib /* Skip kernel's context and get a new one. */ 360235783Skib ctx->handle = drm_ctxbitmap_next(dev); 361235783Skib } 362235783Skib DRM_DEBUG("%d\n", ctx->handle); 363235783Skib if (ctx->handle == -1) { 364235783Skib DRM_DEBUG("Not enough free contexts.\n"); 365235783Skib /* Should this return -EBUSY instead? */ 366282199Sdumbbell return -ENOMEM; 367235783Skib } 368235783Skib 369282199Sdumbbell ctx_entry = malloc(sizeof(*ctx_entry), DRM_MEM_CTXBITMAP, M_NOWAIT); 370282199Sdumbbell if (!ctx_entry) { 371282199Sdumbbell DRM_DEBUG("out of memory\n"); 372282199Sdumbbell return -ENOMEM; 373235783Skib } 374235783Skib 375282199Sdumbbell INIT_LIST_HEAD(&ctx_entry->head); 376282199Sdumbbell ctx_entry->handle = ctx->handle; 377282199Sdumbbell ctx_entry->tag = file_priv; 378282199Sdumbbell 379282199Sdumbbell DRM_LOCK(dev); 380282199Sdumbbell list_add(&ctx_entry->head, &dev->ctxlist); 381282199Sdumbbell ++dev->ctx_count; 382282199Sdumbbell DRM_UNLOCK(dev); 383282199Sdumbbell 384235783Skib return 0; 385235783Skib} 386235783Skib 387235783Skibint drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 388235783Skib{ 389235783Skib /* This does nothing */ 390235783Skib return 0; 391235783Skib} 392235783Skib 393282199Sdumbbell/** 394282199Sdumbbell * Get context. 395282199Sdumbbell * 396282199Sdumbbell * \param inode device inode. 397282199Sdumbbell * \param file_priv DRM file private. 398282199Sdumbbell * \param cmd command. 399282199Sdumbbell * \param arg user argument pointing to a drm_ctx structure. 400282199Sdumbbell * \return zero on success or a negative number on failure. 401282199Sdumbbell */ 402235783Skibint drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 403235783Skib{ 404235783Skib struct drm_ctx *ctx = data; 405235783Skib 406235783Skib /* This is 0, because we don't handle any context flags */ 407235783Skib ctx->flags = 0; 408235783Skib 409235783Skib return 0; 410235783Skib} 411235783Skib 412282199Sdumbbell/** 413282199Sdumbbell * Switch context. 414282199Sdumbbell * 415282199Sdumbbell * \param inode device inode. 416282199Sdumbbell * \param file_priv DRM file private. 417282199Sdumbbell * \param cmd command. 418282199Sdumbbell * \param arg user argument pointing to a drm_ctx structure. 419282199Sdumbbell * \return zero on success or a negative number on failure. 420282199Sdumbbell * 421282199Sdumbbell * Calls context_switch(). 422282199Sdumbbell */ 423235783Skibint drm_switchctx(struct drm_device *dev, void *data, 424235783Skib struct drm_file *file_priv) 425235783Skib{ 426235783Skib struct drm_ctx *ctx = data; 427235783Skib 428235783Skib DRM_DEBUG("%d\n", ctx->handle); 429235783Skib return drm_context_switch(dev, dev->last_context, ctx->handle); 430235783Skib} 431235783Skib 432282199Sdumbbell/** 433282199Sdumbbell * New context. 434282199Sdumbbell * 435282199Sdumbbell * \param inode device inode. 436282199Sdumbbell * \param file_priv DRM file private. 437282199Sdumbbell * \param cmd command. 438282199Sdumbbell * \param arg user argument pointing to a drm_ctx structure. 439282199Sdumbbell * \return zero on success or a negative number on failure. 440282199Sdumbbell * 441282199Sdumbbell * Calls context_switch_complete(). 442282199Sdumbbell */ 443282199Sdumbbellint drm_newctx(struct drm_device *dev, void *data, 444282199Sdumbbell struct drm_file *file_priv) 445235783Skib{ 446235783Skib struct drm_ctx *ctx = data; 447235783Skib 448235783Skib DRM_DEBUG("%d\n", ctx->handle); 449282199Sdumbbell drm_context_switch_complete(dev, file_priv, ctx->handle); 450235783Skib 451235783Skib return 0; 452235783Skib} 453235783Skib 454282199Sdumbbell/** 455282199Sdumbbell * Remove context. 456282199Sdumbbell * 457282199Sdumbbell * \param inode device inode. 458282199Sdumbbell * \param file_priv DRM file private. 459282199Sdumbbell * \param cmd command. 460282199Sdumbbell * \param arg user argument pointing to a drm_ctx structure. 461282199Sdumbbell * \return zero on success or a negative number on failure. 462282199Sdumbbell * 463282199Sdumbbell * If not the special kernel context, calls ctxbitmap_free() to free the specified context. 464282199Sdumbbell */ 465282199Sdumbbellint drm_rmctx(struct drm_device *dev, void *data, 466282199Sdumbbell struct drm_file *file_priv) 467235783Skib{ 468235783Skib struct drm_ctx *ctx = data; 469235783Skib 470235783Skib DRM_DEBUG("%d\n", ctx->handle); 471235783Skib if (ctx->handle != DRM_KERNEL_CONTEXT) { 472282199Sdumbbell if (dev->driver->context_dtor) 473235783Skib dev->driver->context_dtor(dev, ctx->handle); 474235783Skib drm_ctxbitmap_free(dev, ctx->handle); 475235783Skib } 476235783Skib 477282199Sdumbbell DRM_LOCK(dev); 478282199Sdumbbell if (!list_empty(&dev->ctxlist)) { 479282199Sdumbbell struct drm_ctx_list *pos, *n; 480282199Sdumbbell 481282199Sdumbbell list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { 482282199Sdumbbell if (pos->handle == ctx->handle) { 483282199Sdumbbell list_del(&pos->head); 484282199Sdumbbell free(pos, DRM_MEM_CTXBITMAP); 485282199Sdumbbell --dev->ctx_count; 486282199Sdumbbell } 487282199Sdumbbell } 488282199Sdumbbell } 489282199Sdumbbell DRM_UNLOCK(dev); 490282199Sdumbbell 491235783Skib return 0; 492235783Skib} 493282199Sdumbbell 494282199Sdumbbell/*@}*/ 495