2 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 /** @file drm_context.c
35 * Implementation of the context management ioctls.
38 #include <dev/drm2/drmP.h>
40 /* ================================================================
41 * Context bitmap support
44 void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle)
46 if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP ||
47 dev->ctx_bitmap == NULL) {
48 DRM_ERROR("Attempt to free invalid context handle: %d\n",
54 clear_bit(ctx_handle, dev->ctx_bitmap);
55 dev->context_sareas[ctx_handle] = NULL;
60 int drm_ctxbitmap_next(struct drm_device *dev)
64 if (dev->ctx_bitmap == NULL)
68 bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
69 if (bit >= DRM_MAX_CTXBITMAP) {
74 set_bit(bit, dev->ctx_bitmap);
75 DRM_DEBUG("bit : %d\n", bit);
76 if ((bit+1) > dev->max_context) {
77 drm_local_map_t **ctx_sareas;
78 int max_ctx = (bit+1);
80 ctx_sareas = realloc(dev->context_sareas,
81 max_ctx * sizeof(*dev->context_sareas),
82 DRM_MEM_SAREA, M_NOWAIT);
83 if (ctx_sareas == NULL) {
84 clear_bit(bit, dev->ctx_bitmap);
85 DRM_DEBUG("failed to allocate bit : %d\n", bit);
89 dev->max_context = max_ctx;
90 dev->context_sareas = ctx_sareas;
91 dev->context_sareas[bit] = NULL;
97 int drm_ctxbitmap_init(struct drm_device *dev)
103 dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP,
105 if (dev->ctx_bitmap == NULL) {
109 dev->context_sareas = NULL;
110 dev->max_context = -1;
113 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
114 temp = drm_ctxbitmap_next(dev);
115 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
121 void drm_ctxbitmap_cleanup(struct drm_device *dev)
124 if (dev->context_sareas != NULL)
125 free(dev->context_sareas, DRM_MEM_SAREA);
126 free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP);
130 /* ================================================================
131 * Per Context SAREA Support
134 int drm_getsareactx(struct drm_device *dev, void *data,
135 struct drm_file *file_priv)
137 struct drm_ctx_priv_map *request = data;
138 drm_local_map_t *map;
141 if (dev->max_context < 0 ||
142 request->ctx_id >= (unsigned) dev->max_context) {
147 map = dev->context_sareas[request->ctx_id];
150 request->handle = (void *)map->handle;
155 int drm_setsareactx(struct drm_device *dev, void *data,
156 struct drm_file *file_priv)
158 struct drm_ctx_priv_map *request = data;
159 drm_local_map_t *map = NULL;
162 TAILQ_FOREACH(map, &dev->maplist, link) {
163 if (map->handle == request->handle) {
164 if (dev->max_context < 0)
166 if (request->ctx_id >= (unsigned) dev->max_context)
168 dev->context_sareas[request->ctx_id] = map;
179 /* ================================================================
180 * The actual DRM context handling routines
183 int drm_context_switch(struct drm_device *dev, int old, int new)
185 if (test_and_set_bit(0, &dev->context_flag)) {
186 DRM_ERROR("Reentering -- FIXME\n");
190 DRM_DEBUG("Context switch from %d to %d\n", old, new);
192 if (new == dev->last_context) {
193 clear_bit(0, &dev->context_flag);
200 int drm_context_switch_complete(struct drm_device *dev, int new)
202 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
204 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
205 DRM_ERROR("Lock isn't held after context switch\n");
208 /* If a context switch is ever initiated
209 when the kernel holds the lock, release
211 clear_bit(0, &dev->context_flag);
216 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
218 struct drm_ctx_res *res = data;
222 if (res->count >= DRM_RESERVED_CONTEXTS) {
223 bzero(&ctx, sizeof(ctx));
224 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
226 if (DRM_COPY_TO_USER(&res->contexts[i],
231 res->count = DRM_RESERVED_CONTEXTS;
236 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
238 struct drm_ctx *ctx = data;
240 ctx->handle = drm_ctxbitmap_next(dev);
241 if (ctx->handle == DRM_KERNEL_CONTEXT) {
242 /* Skip kernel's context and get a new one. */
243 ctx->handle = drm_ctxbitmap_next(dev);
245 DRM_DEBUG("%d\n", ctx->handle);
246 if (ctx->handle == -1) {
247 DRM_DEBUG("Not enough free contexts.\n");
248 /* Should this return -EBUSY instead? */
252 if (dev->driver->context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) {
254 dev->driver->context_ctor(dev, ctx->handle);
261 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
263 /* This does nothing */
267 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
269 struct drm_ctx *ctx = data;
271 /* This is 0, because we don't handle any context flags */
277 int drm_switchctx(struct drm_device *dev, void *data,
278 struct drm_file *file_priv)
280 struct drm_ctx *ctx = data;
282 DRM_DEBUG("%d\n", ctx->handle);
283 return drm_context_switch(dev, dev->last_context, ctx->handle);
286 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
288 struct drm_ctx *ctx = data;
290 DRM_DEBUG("%d\n", ctx->handle);
291 drm_context_switch_complete(dev, ctx->handle);
296 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
298 struct drm_ctx *ctx = data;
300 DRM_DEBUG("%d\n", ctx->handle);
301 if (ctx->handle != DRM_KERNEL_CONTEXT) {
302 if (dev->driver->context_dtor) {
304 dev->driver->context_dtor(dev, ctx->handle);
308 drm_ctxbitmap_free(dev, ctx->handle);