a590de2b63fb5285e43ee0a28815806583069f3b
[dragonfly.git] / sys / dev / drm / drm_context.c
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 <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                     M_DRM, M_WAITOK | M_NULLOK);
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, M_DRM,
102                                   M_WAITOK | M_NULLOK | 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, M_DRM);
124         drm_free(dev->ctx_bitmap, M_DRM);
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 /**
154  * Set per-context SAREA.
155  *
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.
161  *
162  * Searches the mapping specified in \p arg and update the entry in
163  * drm_device::ctx_idr with it.
164  */
165 int drm_setsareactx(struct drm_device *dev, void *data,
166                     struct drm_file *file_priv)
167 {
168         struct drm_ctx_priv_map *request = data;
169         struct drm_local_map *map = NULL;
170         struct drm_map_list *r_list = NULL;
171
172         DRM_LOCK(dev);
173         list_for_each_entry(r_list, &dev->maplist, head) {
174                 if (r_list->map
175                     && r_list->map->handle == request->handle) {
176                         if (dev->max_context < 0)
177                                 goto bad;
178                         if (request->ctx_id >= (unsigned) dev->max_context)
179                                 goto bad;
180                         map = r_list->map;
181                         dev->context_sareas[request->ctx_id] = map;
182                         DRM_UNLOCK(dev);
183                         return 0;
184                 }
185         }
186
187 bad:
188         DRM_UNLOCK(dev);
189         return EINVAL;
190 }
191
192 /*@}*/
193
194 /******************************************************************/
195 /** \name The actual DRM context handling routines */
196 /*@{*/
197
198 /**
199  * Switch context.
200  *
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.
205  *
206  * Attempt to set drm_device::context_flag.
207  */
208 static int drm_context_switch(struct drm_device * dev, int old, int new)
209 {
210         if (test_and_set_bit(0, &dev->context_flag)) {
211                 DRM_ERROR("Reentering -- FIXME\n");
212                 return -EBUSY;
213         }
214
215         DRM_DEBUG("Context switch from %d to %d\n", old, new);
216
217         if (new == dev->last_context) {
218                 clear_bit(0, &dev->context_flag);
219                 return 0;
220         }
221
222         return 0;
223 }
224
225 /**
226  * Complete context switch.
227  *
228  * \param dev DRM device.
229  * \param new new context handle.
230  * \return zero on success or a negative number on failure.
231  *
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.
235  */
236 static int drm_context_switch_complete(struct drm_device *dev, int new)
237 {
238         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
239
240         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
241                 DRM_ERROR("Lock isn't held after context switch\n");
242         }
243
244         /* If a context switch is ever initiated
245            when the kernel holds the lock, release
246            that lock here. */
247         clear_bit(0, &dev->context_flag);
248
249         return 0;
250 }
251
252 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
253 {
254         struct drm_ctx_res *res = data;
255         struct drm_ctx ctx;
256         int i;
257
258         if (res->count >= DRM_RESERVED_CONTEXTS) {
259                 bzero(&ctx, sizeof(ctx));
260                 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
261                         ctx.handle = i;
262                         if (DRM_COPY_TO_USER(&res->contexts[i],
263                             &ctx, sizeof(ctx)))
264                                 return EFAULT;
265                 }
266         }
267         res->count = DRM_RESERVED_CONTEXTS;
268
269         return 0;
270 }
271
272 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
273 {
274         struct drm_ctx *ctx = data;
275
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);
280         }
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? */
285                 return ENOMEM;
286         }
287
288         if (dev->driver->context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) {
289                 DRM_LOCK(dev);
290                 dev->driver->context_ctor(dev, ctx->handle);
291                 DRM_UNLOCK(dev);
292         }
293
294         return 0;
295 }
296
297 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
298 {
299         /* This does nothing */
300         return 0;
301 }
302
303 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
304 {
305         struct drm_ctx *ctx = data;
306
307         /* This is 0, because we don't handle any context flags */
308         ctx->flags = 0;
309
310         return 0;
311 }
312
313 int drm_switchctx(struct drm_device *dev, void *data,
314                   struct drm_file *file_priv)
315 {
316         struct drm_ctx *ctx = data;
317
318         DRM_DEBUG("%d\n", ctx->handle);
319         return drm_context_switch(dev, dev->last_context, ctx->handle);
320 }
321
322 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
323 {
324         struct drm_ctx *ctx = data;
325
326         DRM_DEBUG("%d\n", ctx->handle);
327         drm_context_switch_complete(dev, ctx->handle);
328
329         return 0;
330 }
331
332 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
333 {
334         struct drm_ctx *ctx = data;
335
336         DRM_DEBUG("%d\n", ctx->handle);
337         if (ctx->handle != DRM_KERNEL_CONTEXT) {
338                 if (dev->driver->context_dtor) {
339                         DRM_LOCK(dev);
340                         dev->driver->context_dtor(dev, ctx->handle);
341                         DRM_UNLOCK(dev);
342                 }
343
344                 drm_ctxbitmap_free(dev, ctx->handle);
345         }
346
347         return 0;
348 }