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>
29 * $FreeBSD: src/sys/dev/drm2/drm_context.c,v 1.1 2012/05/22 11:07:44 kib Exp $
32 /** @file drm_context.c
33 * Implementation of the context management ioctls.
38 /* ================================================================
39 * Context bitmap support
42 void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle)
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",
52 clear_bit(ctx_handle, dev->ctx_bitmap);
53 dev->context_sareas[ctx_handle] = NULL;
58 int drm_ctxbitmap_next(struct drm_device *dev)
62 if (dev->ctx_bitmap == NULL)
66 bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
67 if (bit >= DRM_MAX_CTXBITMAP) {
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);
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);
87 dev->max_context = max_ctx;
88 dev->context_sareas = ctx_sareas;
89 dev->context_sareas[bit] = NULL;
95 int drm_ctxbitmap_init(struct drm_device *dev)
101 dev->ctx_bitmap = kmalloc(PAGE_SIZE, DRM_MEM_CTXBITMAP,
103 if (dev->ctx_bitmap == NULL) {
107 dev->context_sareas = NULL;
108 dev->max_context = -1;
111 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
112 temp = drm_ctxbitmap_next(dev);
113 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
119 void drm_ctxbitmap_cleanup(struct drm_device *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);
128 /* ================================================================
129 * Per Context SAREA Support
132 int drm_getsareactx(struct drm_device *dev, void *data,
133 struct drm_file *file_priv)
135 struct drm_ctx_priv_map *request = data;
136 drm_local_map_t *map;
139 if (dev->max_context < 0 ||
140 request->ctx_id >= (unsigned) dev->max_context) {
145 map = dev->context_sareas[request->ctx_id];
148 request->handle = (void *)map->handle;
154 * Set per-context SAREA.
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.
162 * Searches the mapping specified in \p arg and update the entry in
163 * drm_device::ctx_idr with it.
165 int drm_setsareactx(struct drm_device *dev, void *data,
166 struct drm_file *file_priv)
168 struct drm_ctx_priv_map *request = data;
169 struct drm_local_map *map = NULL;
170 struct drm_map_list *r_list = NULL;
173 list_for_each_entry(r_list, &dev->maplist, head) {
175 && r_list->map->handle == request->handle) {
176 if (dev->max_context < 0)
178 if (request->ctx_id >= (unsigned) dev->max_context)
181 dev->context_sareas[request->ctx_id] = map;
194 /******************************************************************/
195 /** \name The actual DRM context handling routines */
201 * \param dev DRM device.
202 * \param old old context handle.
203 * \param new new context handle.
204 * \return zero on success or a negative number on failure.
206 * Attempt to set drm_device::context_flag.
208 static int drm_context_switch(struct drm_device * dev, int old, int new)
210 if (test_and_set_bit(0, &dev->context_flag)) {
211 DRM_ERROR("Reentering -- FIXME\n");
215 DRM_DEBUG("Context switch from %d to %d\n", old, new);
217 if (new == dev->last_context) {
218 clear_bit(0, &dev->context_flag);
226 * Complete context switch.
228 * \param dev DRM device.
229 * \param new new context handle.
230 * \return zero on success or a negative number on failure.
232 * Updates drm_device::last_context and drm_device::last_switch. Verifies the
233 * hardware lock is held, clears the drm_device::context_flag and wakes up
234 * drm_device::context_wait.
236 static int drm_context_switch_complete(struct drm_device *dev, int new)
238 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
240 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
241 DRM_ERROR("Lock isn't held after context switch\n");
244 /* If a context switch is ever initiated
245 when the kernel holds the lock, release
247 clear_bit(0, &dev->context_flag);
252 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
254 struct drm_ctx_res *res = data;
258 if (res->count >= DRM_RESERVED_CONTEXTS) {
259 bzero(&ctx, sizeof(ctx));
260 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
262 if (DRM_COPY_TO_USER(&res->contexts[i],
267 res->count = DRM_RESERVED_CONTEXTS;
272 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
274 struct drm_ctx *ctx = data;
276 ctx->handle = drm_ctxbitmap_next(dev);
277 if (ctx->handle == DRM_KERNEL_CONTEXT) {
278 /* Skip kernel's context and get a new one. */
279 ctx->handle = drm_ctxbitmap_next(dev);
281 DRM_DEBUG("%d\n", ctx->handle);
282 if (ctx->handle == -1) {
283 DRM_DEBUG("Not enough free contexts.\n");
284 /* Should this return -EBUSY instead? */
288 if (dev->driver->context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) {
290 dev->driver->context_ctor(dev, ctx->handle);
297 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
299 /* This does nothing */
303 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
305 struct drm_ctx *ctx = data;
307 /* This is 0, because we don't handle any context flags */
313 int drm_switchctx(struct drm_device *dev, void *data,
314 struct drm_file *file_priv)
316 struct drm_ctx *ctx = data;
318 DRM_DEBUG("%d\n", ctx->handle);
319 return drm_context_switch(dev, dev->last_context, ctx->handle);
322 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
324 struct drm_ctx *ctx = data;
326 DRM_DEBUG("%d\n", ctx->handle);
327 drm_context_switch_complete(dev, ctx->handle);
332 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
334 struct drm_ctx *ctx = data;
336 DRM_DEBUG("%d\n", ctx->handle);
337 if (ctx->handle != DRM_KERNEL_CONTEXT) {
338 if (dev->driver->context_dtor) {
340 dev->driver->context_dtor(dev, ctx->handle);
344 drm_ctxbitmap_free(dev, ctx->handle);