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 * $FreeBSD: src/sys/dev/drm2/drm_context.c,v 1.1 2012/05/22 11:07:44 kib Exp $ 30 */ 31 32 /** @file drm_context.c 33 * Implementation of the context management ioctls. 34 */ 35 36 #include <drm/drmP.h> 37 38 /* ================================================================ 39 * Context bitmap support 40 */ 41 42 void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle) 43 { 44 if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP || 45 dev->ctx_bitmap == NULL) { 46 DRM_ERROR("Attempt to free invalid context handle: %d\n", 47 ctx_handle); 48 return; 49 } 50 51 DRM_LOCK(dev); 52 clear_bit(ctx_handle, dev->ctx_bitmap); 53 dev->context_sareas[ctx_handle] = NULL; 54 DRM_UNLOCK(dev); 55 return; 56 } 57 58 int drm_ctxbitmap_next(struct drm_device *dev) 59 { 60 int bit; 61 62 if (dev->ctx_bitmap == NULL) 63 return -1; 64 65 DRM_LOCK(dev); 66 bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP); 67 if (bit >= DRM_MAX_CTXBITMAP) { 68 DRM_UNLOCK(dev); 69 return -1; 70 } 71 72 set_bit(bit, dev->ctx_bitmap); 73 DRM_DEBUG("bit : %d\n", bit); 74 if ((bit+1) > dev->max_context) { 75 drm_local_map_t **ctx_sareas; 76 int max_ctx = (bit+1); 77 78 ctx_sareas = krealloc(dev->context_sareas, 79 max_ctx * sizeof(*dev->context_sareas), 80 DRM_MEM_SAREA, M_NOWAIT); 81 if (ctx_sareas == NULL) { 82 clear_bit(bit, dev->ctx_bitmap); 83 DRM_DEBUG("failed to allocate bit : %d\n", bit); 84 DRM_UNLOCK(dev); 85 return -1; 86 } 87 dev->max_context = max_ctx; 88 dev->context_sareas = ctx_sareas; 89 dev->context_sareas[bit] = NULL; 90 } 91 DRM_UNLOCK(dev); 92 return bit; 93 } 94 95 int drm_ctxbitmap_init(struct drm_device *dev) 96 { 97 int i; 98 int temp; 99 100 DRM_LOCK(dev); 101 dev->ctx_bitmap = kmalloc(PAGE_SIZE, DRM_MEM_CTXBITMAP, 102 M_NOWAIT | M_ZERO); 103 if (dev->ctx_bitmap == NULL) { 104 DRM_UNLOCK(dev); 105 return ENOMEM; 106 } 107 dev->context_sareas = NULL; 108 dev->max_context = -1; 109 DRM_UNLOCK(dev); 110 111 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 112 temp = drm_ctxbitmap_next(dev); 113 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp); 114 } 115 116 return 0; 117 } 118 119 void drm_ctxbitmap_cleanup(struct drm_device *dev) 120 { 121 DRM_LOCK(dev); 122 if (dev->context_sareas != NULL) 123 drm_free(dev->context_sareas, DRM_MEM_SAREA); 124 drm_free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP); 125 DRM_UNLOCK(dev); 126 } 127 128 /* ================================================================ 129 * Per Context SAREA Support 130 */ 131 132 int drm_getsareactx(struct drm_device *dev, void *data, 133 struct drm_file *file_priv) 134 { 135 struct drm_ctx_priv_map *request = data; 136 drm_local_map_t *map; 137 138 DRM_LOCK(dev); 139 if (dev->max_context < 0 || 140 request->ctx_id >= (unsigned) dev->max_context) { 141 DRM_UNLOCK(dev); 142 return EINVAL; 143 } 144 145 map = dev->context_sareas[request->ctx_id]; 146 DRM_UNLOCK(dev); 147 148 request->handle = (void *)map->handle; 149 150 return 0; 151 } 152 153 /** 154 * Set per-context SAREA. 155 * 156 * \param inode device inode. 157 * \param file_priv DRM file private. 158 * \param cmd command. 159 * \param arg user argument pointing to a drm_ctx_priv_map structure. 160 * \return zero on success or a negative number on failure. 161 * 162 * Searches the mapping specified in \p arg and update the entry in 163 * drm_device::ctx_idr with it. 164 */ 165 int drm_setsareactx(struct drm_device *dev, void *data, 166 struct drm_file *file_priv) 167 { 168 struct drm_ctx_priv_map *request = data; 169 struct drm_local_map *map = NULL; 170 struct drm_map_list *r_list = NULL; 171 172 DRM_LOCK(dev); 173 list_for_each_entry(r_list, &dev->maplist, head) { 174 if (r_list->map 175 && r_list->map->handle == request->handle) { 176 if (dev->max_context < 0) 177 goto bad; 178 if (request->ctx_id >= (unsigned) dev->max_context) 179 goto bad; 180 map = r_list->map; 181 dev->context_sareas[request->ctx_id] = map; 182 DRM_UNLOCK(dev); 183 return 0; 184 } 185 } 186 187 bad: 188 DRM_UNLOCK(dev); 189 return EINVAL; 190 } 191 192 /* ================================================================ 193 * The actual DRM context handling routines 194 */ 195 196 int drm_context_switch(struct drm_device *dev, int old, int new) 197 { 198 if (atomic_xchg(&dev->context_flag, 1) != 0) { 199 DRM_ERROR("Reentering -- FIXME\n"); 200 return EBUSY; 201 } 202 203 DRM_DEBUG("Context switch from %d to %d\n", old, new); 204 205 if (new == dev->last_context) { 206 atomic_xchg(&dev->context_flag, 0); 207 return 0; 208 } 209 210 return 0; 211 } 212 213 int drm_context_switch_complete(struct drm_device *dev, int new) 214 { 215 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */ 216 217 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) { 218 DRM_ERROR("Lock isn't held after context switch\n"); 219 } 220 221 /* If a context switch is ever initiated 222 when the kernel holds the lock, release 223 that lock here. */ 224 atomic_xchg(&dev->context_flag, 0); 225 226 return 0; 227 } 228 229 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 230 { 231 struct drm_ctx_res *res = data; 232 struct drm_ctx ctx; 233 int i; 234 235 if (res->count >= DRM_RESERVED_CONTEXTS) { 236 bzero(&ctx, sizeof(ctx)); 237 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 238 ctx.handle = i; 239 if (DRM_COPY_TO_USER(&res->contexts[i], 240 &ctx, sizeof(ctx))) 241 return EFAULT; 242 } 243 } 244 res->count = DRM_RESERVED_CONTEXTS; 245 246 return 0; 247 } 248 249 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 250 { 251 struct drm_ctx *ctx = data; 252 253 ctx->handle = drm_ctxbitmap_next(dev); 254 if (ctx->handle == DRM_KERNEL_CONTEXT) { 255 /* Skip kernel's context and get a new one. */ 256 ctx->handle = drm_ctxbitmap_next(dev); 257 } 258 DRM_DEBUG("%d\n", ctx->handle); 259 if (ctx->handle == -1) { 260 DRM_DEBUG("Not enough free contexts.\n"); 261 /* Should this return -EBUSY instead? */ 262 return ENOMEM; 263 } 264 265 if (dev->driver->context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) { 266 DRM_LOCK(dev); 267 dev->driver->context_ctor(dev, ctx->handle); 268 DRM_UNLOCK(dev); 269 } 270 271 return 0; 272 } 273 274 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 275 { 276 /* This does nothing */ 277 return 0; 278 } 279 280 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 281 { 282 struct drm_ctx *ctx = data; 283 284 /* This is 0, because we don't handle any context flags */ 285 ctx->flags = 0; 286 287 return 0; 288 } 289 290 int drm_switchctx(struct drm_device *dev, void *data, 291 struct drm_file *file_priv) 292 { 293 struct drm_ctx *ctx = data; 294 295 DRM_DEBUG("%d\n", ctx->handle); 296 return drm_context_switch(dev, dev->last_context, ctx->handle); 297 } 298 299 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 300 { 301 struct drm_ctx *ctx = data; 302 303 DRM_DEBUG("%d\n", ctx->handle); 304 drm_context_switch_complete(dev, ctx->handle); 305 306 return 0; 307 } 308 309 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 310 { 311 struct drm_ctx *ctx = data; 312 313 DRM_DEBUG("%d\n", ctx->handle); 314 if (ctx->handle != DRM_KERNEL_CONTEXT) { 315 if (dev->driver->context_dtor) { 316 DRM_LOCK(dev); 317 dev->driver->context_dtor(dev, ctx->handle); 318 DRM_UNLOCK(dev); 319 } 320 321 drm_ctxbitmap_free(dev, ctx->handle); 322 } 323 324 return 0; 325 } 326