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 "drmP.h" 36 37 /* ================================================================ 38 * Context bitmap support 39 */ 40 41 void 42 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(); 52 clear_bit(ctx_handle, dev->ctx_bitmap); 53 DRM_UNLOCK(); 54 return; 55 } 56 57 int 58 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(); 66 bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP); 67 if (bit >= DRM_MAX_CTXBITMAP) { 68 DRM_UNLOCK(); 69 return -1; 70 } 71 72 set_bit(bit, dev->ctx_bitmap); 73 DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit); 74 DRM_UNLOCK(); 75 return bit; 76 } 77 78 int 79 drm_ctxbitmap_init(struct drm_device *dev) 80 { 81 atomic_t *bitmap; 82 int i, temp; 83 84 85 bitmap = drm_calloc(1, PAGE_SIZE, DRM_MEM_CTXBITMAP); 86 if (bitmap == NULL) 87 return (ENOMEM); 88 DRM_LOCK(); 89 dev->ctx_bitmap = bitmap; 90 DRM_UNLOCK(); 91 92 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 93 temp = drm_ctxbitmap_next(dev); 94 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp); 95 } 96 97 return 0; 98 } 99 100 void 101 drm_ctxbitmap_cleanup(struct drm_device *dev) 102 { 103 atomic_t *bitmap; 104 105 DRM_LOCK(); 106 bitmap = dev->ctx_bitmap; 107 dev->ctx_bitmap = NULL; 108 DRM_UNLOCK(); 109 drm_free(bitmap, PAGE_SIZE, DRM_MEM_CTXBITMAP); 110 } 111 112 int 113 drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 114 { 115 struct drm_ctx_res *res = data; 116 struct drm_ctx ctx; 117 int i; 118 119 if (res->count >= DRM_RESERVED_CONTEXTS) { 120 bzero(&ctx, sizeof(ctx)); 121 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) { 122 ctx.handle = i; 123 if (DRM_COPY_TO_USER(&res->contexts[i], 124 &ctx, sizeof(ctx))) 125 return EFAULT; 126 } 127 } 128 res->count = DRM_RESERVED_CONTEXTS; 129 130 return 0; 131 } 132 133 int 134 drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 135 { 136 struct drm_ctx *ctx = data; 137 138 ctx->handle = drm_ctxbitmap_next(dev); 139 if (ctx->handle == DRM_KERNEL_CONTEXT) { 140 /* Skip kernel's context and get a new one. */ 141 ctx->handle = drm_ctxbitmap_next(dev); 142 } 143 DRM_DEBUG("%d\n", ctx->handle); 144 if (ctx->handle == -1) { 145 DRM_DEBUG("Not enough free contexts.\n"); 146 /* Should this return -EBUSY instead? */ 147 return ENOMEM; 148 } 149 150 if (dev->driver.context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) 151 dev->driver.context_ctor(dev, ctx->handle); 152 153 return 0; 154 } 155 156 int 157 drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 158 { 159 struct drm_ctx *ctx = data; 160 161 /* This is 0, because we don't handle any context flags */ 162 ctx->flags = 0; 163 164 return 0; 165 } 166 167 int 168 drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv) 169 { 170 struct drm_ctx *ctx = data; 171 172 DRM_DEBUG("%d\n", ctx->handle); 173 if (ctx->handle != DRM_KERNEL_CONTEXT) { 174 if (dev->driver.context_dtor) 175 dev->driver.context_dtor(dev, ctx->handle); 176 177 drm_ctxbitmap_free(dev, ctx->handle); 178 } 179 180 return 0; 181 } 182