xref: /openbsd/sys/dev/pci/drm/drm_context.c (revision 17df1aa7)
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 	if ((bitmap = drm_calloc(1, PAGE_SIZE)) == NULL)
86 		return (ENOMEM);
87 	DRM_LOCK();
88 	dev->ctx_bitmap = bitmap;
89 	DRM_UNLOCK();
90 
91 	for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
92 		temp = drm_ctxbitmap_next(dev);
93 	   	DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
94 	}
95 
96 	return (0);
97 }
98 
99 void
100 drm_ctxbitmap_cleanup(struct drm_device *dev)
101 {
102 	atomic_t *bitmap;
103 
104 	DRM_LOCK();
105 	bitmap = dev->ctx_bitmap;
106 	dev->ctx_bitmap = NULL;
107 	DRM_UNLOCK();
108 	drm_free(bitmap);
109 }
110 
111 int
112 drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
113 {
114 	struct drm_ctx_res	*res = data;
115 	struct drm_ctx		 ctx;
116 	int			 i;
117 
118 	if (res->count >= DRM_RESERVED_CONTEXTS) {
119 		bzero(&ctx, sizeof(ctx));
120 		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
121 			ctx.handle = i;
122 			if (DRM_COPY_TO_USER(&res->contexts[i],
123 			    &ctx, sizeof(ctx)))
124 				return (EFAULT);
125 		}
126 	}
127 	res->count = DRM_RESERVED_CONTEXTS;
128 
129 	return (0);
130 }
131 
132 int
133 drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
134 {
135 	struct drm_ctx	*ctx = data;
136 
137 	ctx->handle = drm_ctxbitmap_next(dev);
138 	if (ctx->handle == DRM_KERNEL_CONTEXT) {
139 		/* Skip kernel's context and get a new one. */
140 		ctx->handle = drm_ctxbitmap_next(dev);
141 	}
142 	DRM_DEBUG("%d\n", ctx->handle);
143 	if (ctx->handle == -1) {
144 		DRM_DEBUG("Not enough free contexts.\n");
145 		/* Should this return -EBUSY instead? */
146 		return (ENOMEM);
147 	}
148 
149 	return (0);
150 }
151 
152 int
153 drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
154 {
155 	struct drm_ctx	*ctx = data;
156 
157 	/* This is 0, because we don't handle any context flags */
158 	ctx->flags = 0;
159 
160 	return (0);
161 }
162 
163 int
164 drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
165 {
166 	struct drm_ctx	*ctx = data;
167 
168 	DRM_DEBUG("%d\n", ctx->handle);
169 	if (ctx->handle != DRM_KERNEL_CONTEXT)
170 		drm_ctxbitmap_free(dev, ctx->handle);
171 
172 	return (0);
173 }
174