1 /*- 2 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. 3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Rickard E. (Rik) Faith <faith@valinux.com> 27 * Gareth Hughes <gareth@valinux.com> 28 * 29 */ 30 31 /** @file drm_context.c 32 * Implementation of the context management ioctls. 33 */ 34 35 #include "dev/drm/drmP.h" 36 37 /* ================================================================ 38 * Context bitmap support 39 */ 40 41 void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle) 42 { 43 if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP || 44 dev->ctx_bitmap == NULL) { 45 DRM_ERROR("Attempt to free invalid context handle: %d\n", 46 ctx_handle); 47 return; 48 } 49 50 DRM_LOCK(); 51 clear_bit(ctx_handle, dev->ctx_bitmap); 52 dev->context_sareas[ctx_handle] = NULL; 53 DRM_UNLOCK(); 54 return; 55 } 56 57 int drm_ctxbitmap_next(struct drm_device *dev) 58 { 59 int bit; 60 61 if (dev->ctx_bitmap == NULL) 62 return -1; 63 64 DRM_LOCK(); 65 bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP); 66 if (bit >= DRM_MAX_CTXBITMAP) { 67 DRM_UNLOCK(); 68 return -1; 69 } 70 71 set_bit(bit, dev->ctx_bitmap); 72 DRM_DEBUG("bit : %d\n", bit); 73 if ((bit+1) > dev->max_context) { 74 drm_local_map_t **ctx_sareas; 75 int max_ctx = (bit+1); 76 77 ctx_sareas = realloc(dev->context_sareas, 78 max_ctx * sizeof(*dev->context_sareas), 79 DRM_MEM_SAREA, M_NOWAIT); 80 if (ctx_sareas == NULL) { 81 clear_bit(bit, dev->ctx_bitmap); 82 DRM_DEBUG("failed to allocate bit : %d\n", bit); 83 DRM_UNLOCK(); 84 return -1; 85 } 86 dev->max_context = max_ctx; 87 dev->context_sareas = ctx_sareas; 88 dev->context_sareas[bit] = NULL; 89 } 90 DRM_UNLOCK(); 91 return bit; 92 } 93 94 int drm_ctxbitmap_init(struct drm_device *dev) 95 { 96 int i; 97 int temp; 98 99 DRM_LOCK(); 100 dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP, 101 M_NOWAIT | M_ZERO); 102 if (dev->ctx_bitmap == NULL) { 103 DRM_UNLOCK(); 104 return ENOMEM; 105 } 106 dev->context_sareas = NULL; 107 dev->max_context = -1; 108 DRM_UNLOCK(); 109 110 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 111 temp = drm_ctxbitmap_next(dev); 112 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp); 113 } 114 115 return 0; 116 } 117 118 void drm_ctxbitmap_cleanup(struct drm_device *dev) 119 { 120 DRM_LOCK(); 121 if (dev->context_sareas != NULL) 122 free(dev->context_sareas, DRM_MEM_SAREA); 123 free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP); 124 DRM_UNLOCK(); 125 } 126 127 /* ================================================================ 128 * Per Context SAREA Support 129 */ 130 131 int drm_getsareactx(struct drm_device *dev, void *data, 132 struct drm_file *file_priv) 133 { 134 struct drm_ctx_priv_map *request = data; 135 drm_local_map_t *map; 136 137 DRM_LOCK(); 138 if (dev->max_context < 0 || 139 request->ctx_id >= (unsigned) dev->max_context) { 140 DRM_UNLOCK(); 141 return EINVAL; 142 } 143 144 map = dev->context_sareas[request->ctx_id]; 145 DRM_UNLOCK(); 146 147 request->handle = map->handle; 148 149 return 0; 150 } 151 152 int drm_setsareactx(struct drm_device *dev, void *data, 153 struct drm_file *file_priv) 154 { 155 struct drm_ctx_priv_map *request = data; 156 drm_local_map_t *map = NULL; 157 158 DRM_LOCK(); 159 TAILQ_FOREACH(map, &dev->maplist, link) { 160 if (map->handle == request->handle) { 161 if (dev->max_context < 0) 162 goto bad; 163 if (request->ctx_id >= (unsigned) dev->max_context) 164 goto bad; 165 dev->context_sareas[request->ctx_id] = map; 166 DRM_UNLOCK(); 167 return 0; 168 } 169 } 170 171 bad: 172 DRM_UNLOCK(); 173 return EINVAL; 174 } 175 176 /* ================================================================ 177 * The actual DRM context handling routines 178 */ 179 180 int drm_context_switch(struct drm_device *dev, int old, int new) 181 { 182 if (test_and_set_bit(0, &dev->context_flag)) { 183 DRM_ERROR("Reentering -- FIXME\n"); 184 return EBUSY; 185 } 186 187 DRM_DEBUG("Context switch from %d to %d\n", old, new); 188 189 if (new == dev->last_context) { 190 clear_bit(0, &dev->context_flag); 191 return 0; 192 } 193 194 return 0; 195 } 196 197 int drm_context_switch_complete(struct drm_device *dev, int new) 198 { 199 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ 200 201 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) { 202 DRM_ERROR("Lock isn't held after context switch\n"); 203 } 204 205 /* If a context switch is ever initiated 206 when the kernel holds the lock, release 207 that lock here. */ 208 clear_bit(0, &dev->context_flag); 209 210 return 0; 211 } 212 213 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 214 { 215 struct drm_ctx_res *res = data; 216 struct drm_ctx ctx; 217 int i; 218 219 if (res->count >= DRM_RESERVED_CONTEXTS) { 220 bzero(&ctx, sizeof(ctx)); 221 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 222 ctx.handle = i; 223 if (DRM_COPY_TO_USER(&res->contexts[i], 224 &ctx, sizeof(ctx))) 225 return EFAULT; 226 } 227 } 228 res->count = DRM_RESERVED_CONTEXTS; 229 230 return 0; 231 } 232 233 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 234 { 235 struct drm_ctx *ctx = data; 236 237 ctx->handle = drm_ctxbitmap_next(dev); 238 if (ctx->handle == DRM_KERNEL_CONTEXT) { 239 /* Skip kernel's context and get a new one. */ 240 ctx->handle = drm_ctxbitmap_next(dev); 241 } 242 DRM_DEBUG("%d\n", ctx->handle); 243 if (ctx->handle == -1) { 244 DRM_DEBUG("Not enough free contexts.\n"); 245 /* Should this return -EBUSY instead? */ 246 return ENOMEM; 247 } 248 249 if (dev->driver->context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) { 250 DRM_LOCK(); 251 dev->driver->context_ctor(dev, ctx->handle); 252 DRM_UNLOCK(); 253 } 254 255 return 0; 256 } 257 258 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 259 { 260 /* This does nothing */ 261 return 0; 262 } 263 264 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 265 { 266 struct drm_ctx *ctx = data; 267 268 /* This is 0, because we don't handle any context flags */ 269 ctx->flags = 0; 270 271 return 0; 272 } 273 274 int drm_switchctx(struct drm_device *dev, void *data, 275 struct drm_file *file_priv) 276 { 277 struct drm_ctx *ctx = data; 278 279 DRM_DEBUG("%d\n", ctx->handle); 280 return drm_context_switch(dev, dev->last_context, ctx->handle); 281 } 282 283 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 284 { 285 struct drm_ctx *ctx = data; 286 287 DRM_DEBUG("%d\n", ctx->handle); 288 drm_context_switch_complete(dev, ctx->handle); 289 290 return 0; 291 } 292 293 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 294 { 295 struct drm_ctx *ctx = data; 296 297 DRM_DEBUG("%d\n", ctx->handle); 298 if (ctx->handle != DRM_KERNEL_CONTEXT) { 299 if (dev->driver->context_dtor) { 300 DRM_LOCK(); 301 dev->driver->context_dtor(dev, ctx->handle); 302 DRM_UNLOCK(); 303 } 304 305 drm_ctxbitmap_free(dev, ctx->handle); 306 } 307 308 return 0; 309 } 310