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