xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/drm_context.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /**
2  * \file drm_context.c
3  * IOCTLs for generic contexts
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8 
9 /*
10  * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
11  *
12  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14  * All Rights Reserved.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice (including the next
24  * paragraph) shall be included in all copies or substantial portions of the
25  * Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  */
35 
36 /*
37  * ChangeLog:
38  *  2001-11-16	Torsten Duwe <duwe@caldera.de>
39  *		added context constructor/destructor hooks,
40  *		needed by SiS driver's memory management.
41  */
42 
43 #include <linux/err.h>
44 
45 #include <drm/drmP.h>
46 
47 /******************************************************************/
48 /** \name Context bitmap support */
49 /*@{*/
50 
51 /**
52  * Free a handle from the context bitmap.
53  *
54  * \param dev DRM device.
55  * \param ctx_handle context handle.
56  *
57  * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
58  * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
59  * lock.
60  */
61 void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
62 {
63 	mutex_lock(&dev->struct_mutex);
64 	idr_remove(&dev->ctx_idr, ctx_handle);
65 	mutex_unlock(&dev->struct_mutex);
66 }
67 
68 /**
69  * Context bitmap allocation.
70  *
71  * \param dev DRM device.
72  * \return (non-negative) context handle on success or a negative number on failure.
73  *
74  * Allocate a new idr from drm_device::ctx_idr while holding the
75  * drm_device::struct_mutex lock.
76  */
77 static int drm_ctxbitmap_next(struct drm_device * dev)
78 {
79 	int new_id;
80 	int ret;
81 
82 again:
83 	if (idr_pre_get(&dev->ctx_idr, GFP_KERNEL) == 0) {
84 		DRM_ERROR("Out of memory expanding drawable idr\n");
85 		return -ENOMEM;
86 	}
87 	mutex_lock(&dev->struct_mutex);
88 	ret = idr_get_new_above(&dev->ctx_idr, NULL,
89 				DRM_RESERVED_CONTEXTS, &new_id);
90 	mutex_unlock(&dev->struct_mutex);
91 	if (ret == -EAGAIN)
92 		goto again;
93 	else if (ret)
94 		return ret;
95 
96 	return new_id;
97 }
98 
99 /**
100  * Context bitmap initialization.
101  *
102  * \param dev DRM device.
103  *
104  * Initialise the drm_device::ctx_idr
105  */
106 int drm_ctxbitmap_init(struct drm_device * dev)
107 {
108 	idr_init(&dev->ctx_idr);
109 	return 0;
110 }
111 
112 /**
113  * Context bitmap cleanup.
114  *
115  * \param dev DRM device.
116  *
117  * Free all idr members using drm_ctx_sarea_free helper function
118  * while holding the drm_device::struct_mutex lock.
119  */
120 void drm_ctxbitmap_cleanup(struct drm_device * dev)
121 {
122 	mutex_lock(&dev->struct_mutex);
123 	idr_remove_all(&dev->ctx_idr);
124 	idr_destroy(&dev->ctx_idr);
125 	mutex_unlock(&dev->struct_mutex);
126 }
127 
128 /*@}*/
129 
130 /******************************************************************/
131 /** \name Per Context SAREA Support */
132 /*@{*/
133 
134 /**
135  * Get per-context SAREA.
136  *
137  * \param inode device inode.
138  * \param file_priv DRM file private.
139  * \param cmd command.
140  * \param arg user argument pointing to a drm_ctx_priv_map structure.
141  * \return zero on success or a negative number on failure.
142  *
143  * Gets the map from drm_device::ctx_idr with the handle specified and
144  * returns its handle.
145  */
146 int drm_getsareactx(struct drm_device *dev, void *data,
147 		    struct drm_file *file_priv)
148 {
149 	struct drm_ctx_priv_map *request = data;
150 	struct drm_local_map *map;
151 	struct drm_map_list *_entry;
152 
153 	mutex_lock(&dev->struct_mutex);
154 
155 	map = idr_find(&dev->ctx_idr, request->ctx_id);
156 	if (!map) {
157 		mutex_unlock(&dev->struct_mutex);
158 		return -EINVAL;
159 	}
160 
161 	request->handle = NULL;
162 	list_for_each_entry(_entry, &dev->maplist, head) {
163 		if (_entry->map == map) {
164 			request->handle =
165 			    (void *)(unsigned long)_entry->user_token;
166 			break;
167 		}
168 	}
169 
170 	mutex_unlock(&dev->struct_mutex);
171 
172 	if (request->handle == NULL)
173 		return -EINVAL;
174 
175 	return 0;
176 }
177 
178 /**
179  * Set per-context SAREA.
180  *
181  * \param inode device inode.
182  * \param file_priv DRM file private.
183  * \param cmd command.
184  * \param arg user argument pointing to a drm_ctx_priv_map structure.
185  * \return zero on success or a negative number on failure.
186  *
187  * Searches the mapping specified in \p arg and update the entry in
188  * drm_device::ctx_idr with it.
189  */
190 int drm_setsareactx(struct drm_device *dev, void *data,
191 		    struct drm_file *file_priv)
192 {
193 	struct drm_ctx_priv_map *request = data;
194 	struct drm_local_map *map = NULL;
195 	struct drm_map_list *r_list = NULL;
196 
197 	mutex_lock(&dev->struct_mutex);
198 	list_for_each_entry(r_list, &dev->maplist, head) {
199 		if (r_list->map
200 		    && r_list->user_token == (unsigned long) request->handle)
201 			goto found;
202 	}
203       bad:
204 	mutex_unlock(&dev->struct_mutex);
205 	return -EINVAL;
206 
207       found:
208 	map = r_list->map;
209 	if (!map)
210 		goto bad;
211 
212 	if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
213 		goto bad;
214 
215 	mutex_unlock(&dev->struct_mutex);
216 
217 	return 0;
218 }
219 
220 /*@}*/
221 
222 /******************************************************************/
223 /** \name The actual DRM context handling routines */
224 /*@{*/
225 
226 /**
227  * Switch context.
228  *
229  * \param dev DRM device.
230  * \param old old context handle.
231  * \param new new context handle.
232  * \return zero on success or a negative number on failure.
233  *
234  * Attempt to set drm_device::context_flag.
235  */
236 static int drm_context_switch(struct drm_device * dev, int old, int new)
237 {
238 	if (test_and_set_bit(0, &dev->context_flag)) {
239 		DRM_ERROR("Reentering -- FIXME\n");
240 		return -EBUSY;
241 	}
242 
243 	DRM_DEBUG("Context switch from %d to %d\n", old, new);
244 
245 	if (new == dev->last_context) {
246 		clear_bit(0, &dev->context_flag);
247 		return 0;
248 	}
249 
250 	return 0;
251 }
252 
253 /**
254  * Complete context switch.
255  *
256  * \param dev DRM device.
257  * \param new new context handle.
258  * \return zero on success or a negative number on failure.
259  *
260  * Updates drm_device::last_context and drm_device::last_switch. Verifies the
261  * hardware lock is held, clears the drm_device::context_flag and wakes up
262  * drm_device::context_wait.
263  */
264 static int drm_context_switch_complete(struct drm_device *dev,
265 				       struct drm_file *file_priv, int new)
266 {
267 	dev->last_context = new;	/* PRE/POST: This is the _only_ writer. */
268 	dev->last_switch = jiffies;
269 
270 	spin_lock(&file_priv->master->lock.spinlock);
271 	if (file_priv->master->lock.hw_lock == NULL ||
272 	    !_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
273 		DRM_ERROR("Lock isn't held after context switch\n");
274 	}
275 	spin_unlock(&file_priv->master->lock.spinlock);
276 
277 	/* If a context switch is ever initiated
278 	   when the kernel holds the lock, release
279 	   that lock here. */
280 	clear_bit(0, &dev->context_flag);
281 #ifdef __NetBSD__
282 	DRM_WAKEUP_ONE(&dev->context_wait, &drm_global_mutex);
283 #else
284 	wake_up(&dev->context_wait);
285 #endif
286 
287 	return 0;
288 }
289 
290 /**
291  * Reserve contexts.
292  *
293  * \param inode device inode.
294  * \param file_priv DRM file private.
295  * \param cmd command.
296  * \param arg user argument pointing to a drm_ctx_res structure.
297  * \return zero on success or a negative number on failure.
298  */
299 int drm_resctx(struct drm_device *dev, void *data,
300 	       struct drm_file *file_priv)
301 {
302 	struct drm_ctx_res *res = data;
303 	struct drm_ctx ctx;
304 	int i;
305 
306 	if (res->count >= DRM_RESERVED_CONTEXTS) {
307 		memset(&ctx, 0, sizeof(ctx));
308 		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
309 			ctx.handle = i;
310 			if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
311 				return -EFAULT;
312 		}
313 	}
314 	res->count = DRM_RESERVED_CONTEXTS;
315 
316 	return 0;
317 }
318 
319 /**
320  * Add context.
321  *
322  * \param inode device inode.
323  * \param file_priv DRM file private.
324  * \param cmd command.
325  * \param arg user argument pointing to a drm_ctx structure.
326  * \return zero on success or a negative number on failure.
327  *
328  * Get a new handle for the context and copy to userspace.
329  */
330 int drm_addctx(struct drm_device *dev, void *data,
331 	       struct drm_file *file_priv)
332 {
333 	struct drm_ctx_list *ctx_entry;
334 	struct drm_ctx *ctx = data;
335 
336 	ctx->handle = drm_ctxbitmap_next(dev);
337 	if (ctx->handle == DRM_KERNEL_CONTEXT) {
338 		/* Skip kernel's context and get a new one. */
339 		ctx->handle = drm_ctxbitmap_next(dev);
340 	}
341 	DRM_DEBUG("%d\n", ctx->handle);
342 	if (ctx->handle == -1) {
343 		DRM_DEBUG("Not enough free contexts.\n");
344 		/* Should this return -EBUSY instead? */
345 		return -ENOMEM;
346 	}
347 
348 	ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
349 	if (!ctx_entry) {
350 		DRM_DEBUG("out of memory\n");
351 		return -ENOMEM;
352 	}
353 
354 	INIT_LIST_HEAD(&ctx_entry->head);
355 	ctx_entry->handle = ctx->handle;
356 	ctx_entry->tag = file_priv;
357 
358 	mutex_lock(&dev->ctxlist_mutex);
359 	list_add(&ctx_entry->head, &dev->ctxlist);
360 	++dev->ctx_count;
361 	mutex_unlock(&dev->ctxlist_mutex);
362 
363 	return 0;
364 }
365 
366 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
367 {
368 	/* This does nothing */
369 	return 0;
370 }
371 
372 /**
373  * Get context.
374  *
375  * \param inode device inode.
376  * \param file_priv DRM file private.
377  * \param cmd command.
378  * \param arg user argument pointing to a drm_ctx structure.
379  * \return zero on success or a negative number on failure.
380  */
381 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
382 {
383 	struct drm_ctx *ctx = data;
384 
385 	/* This is 0, because we don't handle any context flags */
386 	ctx->flags = 0;
387 
388 	return 0;
389 }
390 
391 /**
392  * Switch context.
393  *
394  * \param inode device inode.
395  * \param file_priv DRM file private.
396  * \param cmd command.
397  * \param arg user argument pointing to a drm_ctx structure.
398  * \return zero on success or a negative number on failure.
399  *
400  * Calls context_switch().
401  */
402 int drm_switchctx(struct drm_device *dev, void *data,
403 		  struct drm_file *file_priv)
404 {
405 	struct drm_ctx *ctx = data;
406 
407 	DRM_DEBUG("%d\n", ctx->handle);
408 	return drm_context_switch(dev, dev->last_context, ctx->handle);
409 }
410 
411 /**
412  * New context.
413  *
414  * \param inode device inode.
415  * \param file_priv DRM file private.
416  * \param cmd command.
417  * \param arg user argument pointing to a drm_ctx structure.
418  * \return zero on success or a negative number on failure.
419  *
420  * Calls context_switch_complete().
421  */
422 int drm_newctx(struct drm_device *dev, void *data,
423 	       struct drm_file *file_priv)
424 {
425 	struct drm_ctx *ctx = data;
426 
427 	DRM_DEBUG("%d\n", ctx->handle);
428 	drm_context_switch_complete(dev, file_priv, ctx->handle);
429 
430 	return 0;
431 }
432 
433 /**
434  * Remove context.
435  *
436  * \param inode device inode.
437  * \param file_priv DRM file private.
438  * \param cmd command.
439  * \param arg user argument pointing to a drm_ctx structure.
440  * \return zero on success or a negative number on failure.
441  *
442  * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
443  */
444 int drm_rmctx(struct drm_device *dev, void *data,
445 	      struct drm_file *file_priv)
446 {
447 	struct drm_ctx *ctx = data;
448 
449 	DRM_DEBUG("%d\n", ctx->handle);
450 	if (ctx->handle != DRM_KERNEL_CONTEXT) {
451 		if (dev->driver->context_dtor)
452 			dev->driver->context_dtor(dev, ctx->handle);
453 		drm_ctxbitmap_free(dev, ctx->handle);
454 	}
455 
456 	mutex_lock(&dev->ctxlist_mutex);
457 	if (!list_empty(&dev->ctxlist)) {
458 		struct drm_ctx_list *pos, *n;
459 
460 		list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
461 			if (pos->handle == ctx->handle) {
462 				list_del(&pos->head);
463 				kfree(pos);
464 				--dev->ctx_count;
465 			}
466 		}
467 	}
468 	mutex_unlock(&dev->ctxlist_mutex);
469 
470 	return 0;
471 }
472 
473 /*@}*/
474