xref: /dflybsd-src/sys/dev/drm/drm_context.c (revision 450f08dbfd98cded95c51be4079ef10f5adb3241)
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 "dev/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 int drm_setsareactx(struct drm_device *dev, void *data,
154 		    struct drm_file *file_priv)
155 {
156 	struct drm_ctx_priv_map *request = data;
157 	drm_local_map_t *map = NULL;
158 
159 	DRM_LOCK(dev);
160 	TAILQ_FOREACH(map, &dev->maplist, link) {
161 		if (map->handle == request->handle) {
162 			if (dev->max_context < 0)
163 				goto bad;
164 			if (request->ctx_id >= (unsigned) dev->max_context)
165 				goto bad;
166 			dev->context_sareas[request->ctx_id] = map;
167 			DRM_UNLOCK(dev);
168 			return 0;
169 		}
170 	}
171 
172 bad:
173 	DRM_UNLOCK(dev);
174 	return EINVAL;
175 }
176 
177 /* ================================================================
178  * The actual DRM context handling routines
179  */
180 
181 int drm_context_switch(struct drm_device *dev, int old, int new)
182 {
183 	if (atomic_xchg(&dev->context_flag, 1) != 0) {
184 		DRM_ERROR("Reentering -- FIXME\n");
185 		return EBUSY;
186 	}
187 
188 	DRM_DEBUG("Context switch from %d to %d\n", old, new);
189 
190 	if (new == dev->last_context) {
191 		atomic_xchg(&dev->context_flag, 0);
192 		return 0;
193 	}
194 
195 	return 0;
196 }
197 
198 int drm_context_switch_complete(struct drm_device *dev, int new)
199 {
200 	dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
201 
202 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
203 		DRM_ERROR("Lock isn't held after context switch\n");
204 	}
205 
206 	/* If a context switch is ever initiated
207 	   when the kernel holds the lock, release
208 	   that lock here. */
209 	atomic_xchg(&dev->context_flag, 0);
210 
211 	return 0;
212 }
213 
214 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
215 {
216 	struct drm_ctx_res *res = data;
217 	struct drm_ctx ctx;
218 	int i;
219 
220 	if (res->count >= DRM_RESERVED_CONTEXTS) {
221 		bzero(&ctx, sizeof(ctx));
222 		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
223 			ctx.handle = i;
224 			if (DRM_COPY_TO_USER(&res->contexts[i],
225 			    &ctx, sizeof(ctx)))
226 				return EFAULT;
227 		}
228 	}
229 	res->count = DRM_RESERVED_CONTEXTS;
230 
231 	return 0;
232 }
233 
234 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
235 {
236 	struct drm_ctx *ctx = data;
237 
238 	ctx->handle = drm_ctxbitmap_next(dev);
239 	if (ctx->handle == DRM_KERNEL_CONTEXT) {
240 		/* Skip kernel's context and get a new one. */
241 		ctx->handle = drm_ctxbitmap_next(dev);
242 	}
243 	DRM_DEBUG("%d\n", ctx->handle);
244 	if (ctx->handle == -1) {
245 		DRM_DEBUG("Not enough free contexts.\n");
246 		/* Should this return -EBUSY instead? */
247 		return ENOMEM;
248 	}
249 
250 	if (dev->driver->context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) {
251 		DRM_LOCK(dev);
252 		dev->driver->context_ctor(dev, ctx->handle);
253 		DRM_UNLOCK(dev);
254 	}
255 
256 	return 0;
257 }
258 
259 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
260 {
261 	/* This does nothing */
262 	return 0;
263 }
264 
265 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
266 {
267 	struct drm_ctx *ctx = data;
268 
269 	/* This is 0, because we don't handle any context flags */
270 	ctx->flags = 0;
271 
272 	return 0;
273 }
274 
275 int drm_switchctx(struct drm_device *dev, void *data,
276 		  struct drm_file *file_priv)
277 {
278 	struct drm_ctx *ctx = data;
279 
280 	DRM_DEBUG("%d\n", ctx->handle);
281 	return drm_context_switch(dev, dev->last_context, ctx->handle);
282 }
283 
284 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
285 {
286 	struct drm_ctx *ctx = data;
287 
288 	DRM_DEBUG("%d\n", ctx->handle);
289 	drm_context_switch_complete(dev, ctx->handle);
290 
291 	return 0;
292 }
293 
294 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
295 {
296 	struct drm_ctx *ctx = data;
297 
298 	DRM_DEBUG("%d\n", ctx->handle);
299 	if (ctx->handle != DRM_KERNEL_CONTEXT) {
300 		if (dev->driver->context_dtor) {
301 			DRM_LOCK(dev);
302 			dev->driver->context_dtor(dev, ctx->handle);
303 			DRM_UNLOCK(dev);
304 		}
305 
306 		drm_ctxbitmap_free(dev, ctx->handle);
307 	}
308 
309 	return 0;
310 }
311