]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/drm/drm_context.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /** @file drm_context.c
35  * Implementation of the context management ioctls.
36  */
37
38 #include "dev/drm/drmP.h"
39
40 /* ================================================================
41  * Context bitmap support
42  */
43
44 void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle)
45 {
46         if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP || 
47             dev->ctx_bitmap == NULL) {
48                 DRM_ERROR("Attempt to free invalid context handle: %d\n",
49                    ctx_handle);
50                 return;
51         }
52
53         DRM_LOCK();
54         clear_bit(ctx_handle, dev->ctx_bitmap);
55         dev->context_sareas[ctx_handle] = NULL;
56         DRM_UNLOCK();
57         return;
58 }
59
60 int drm_ctxbitmap_next(struct drm_device *dev)
61 {
62         int bit;
63
64         if (dev->ctx_bitmap == NULL)
65                 return -1;
66
67         DRM_LOCK();
68         bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
69         if (bit >= DRM_MAX_CTXBITMAP) {
70                 DRM_UNLOCK();
71                 return -1;
72         }
73
74         set_bit(bit, dev->ctx_bitmap);
75         DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit);
76         if ((bit+1) > dev->max_context) {
77                 dev->max_context = (bit+1);
78                 if (dev->context_sareas != NULL) {
79                         drm_local_map_t **ctx_sareas;
80
81                         ctx_sareas = realloc(dev->context_sareas,
82                             dev->max_context * sizeof(*dev->context_sareas),
83                             DRM_MEM_SAREA, M_NOWAIT);
84                         if (ctx_sareas == NULL) {
85                                 clear_bit(bit, dev->ctx_bitmap);
86                                 DRM_UNLOCK();
87                                 return -1;
88                         }
89                         dev->context_sareas = ctx_sareas;
90                         dev->context_sareas[bit] = NULL;
91                 } else {
92                         /* max_context == 1 at this point */
93                         dev->context_sareas = malloc(dev->max_context * 
94                             sizeof(*dev->context_sareas), DRM_MEM_SAREA,
95                             M_NOWAIT);
96                         if (dev->context_sareas == NULL) {
97                                 clear_bit(bit, dev->ctx_bitmap);
98                                 DRM_UNLOCK();
99                                 return -1;
100                         }
101                         dev->context_sareas[bit] = NULL;
102                 }
103         }
104         DRM_UNLOCK();
105         return bit;
106 }
107
108 int drm_ctxbitmap_init(struct drm_device *dev)
109 {
110         int i;
111         int temp;
112
113         DRM_LOCK();
114         dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP,
115             M_NOWAIT | M_ZERO);
116         if (dev->ctx_bitmap == NULL) {
117                 DRM_UNLOCK();
118                 return ENOMEM;
119         }
120         dev->context_sareas = NULL;
121         dev->max_context = -1;
122         DRM_UNLOCK();
123
124         for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
125                 temp = drm_ctxbitmap_next(dev);
126                 DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
127         }
128
129         return 0;
130 }
131
132 void drm_ctxbitmap_cleanup(struct drm_device *dev)
133 {
134         DRM_LOCK();
135         if (dev->context_sareas != NULL)
136                 free(dev->context_sareas, DRM_MEM_SAREA);
137         free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP);
138         DRM_UNLOCK();
139 }
140
141 /* ================================================================
142  * Per Context SAREA Support
143  */
144
145 int drm_getsareactx(struct drm_device *dev, void *data,
146                     struct drm_file *file_priv)
147 {
148         struct drm_ctx_priv_map *request = data;
149         drm_local_map_t *map;
150
151         DRM_LOCK();
152         if (dev->max_context < 0 ||
153             request->ctx_id >= (unsigned) dev->max_context) {
154                 DRM_UNLOCK();
155                 return EINVAL;
156         }
157
158         map = dev->context_sareas[request->ctx_id];
159         DRM_UNLOCK();
160
161         request->handle = map->handle;
162
163         return 0;
164 }
165
166 int drm_setsareactx(struct drm_device *dev, void *data,
167                     struct drm_file *file_priv)
168 {
169         struct drm_ctx_priv_map *request = data;
170         drm_local_map_t *map = NULL;
171
172         DRM_LOCK();
173         TAILQ_FOREACH(map, &dev->maplist, link) {
174                 if (map->handle == request->handle) {
175                         if (dev->max_context < 0)
176                                 goto bad;
177                         if (request->ctx_id >= (unsigned) dev->max_context)
178                                 goto bad;
179                         dev->context_sareas[request->ctx_id] = map;
180                         DRM_UNLOCK();
181                         return 0;
182                 }
183         }
184
185 bad:
186         DRM_UNLOCK();
187         return EINVAL;
188 }
189
190 /* ================================================================
191  * The actual DRM context handling routines
192  */
193
194 int drm_context_switch(struct drm_device *dev, int old, int new)
195 {
196         if (test_and_set_bit(0, &dev->context_flag)) {
197                 DRM_ERROR("Reentering -- FIXME\n");
198                 return EBUSY;
199         }
200
201         DRM_DEBUG("Context switch from %d to %d\n", old, new);
202
203         if (new == dev->last_context) {
204                 clear_bit(0, &dev->context_flag);
205                 return 0;
206         }
207
208         return 0;
209 }
210
211 int drm_context_switch_complete(struct drm_device *dev, int new)
212 {
213         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
214
215         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
216                 DRM_ERROR("Lock isn't held after context switch\n");
217         }
218
219         /* If a context switch is ever initiated
220            when the kernel holds the lock, release
221            that lock here. */
222         clear_bit(0, &dev->context_flag);
223
224         return 0;
225 }
226
227 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
228 {
229         struct drm_ctx_res *res = data;
230         struct drm_ctx ctx;
231         int i;
232
233         if (res->count >= DRM_RESERVED_CONTEXTS) {
234                 bzero(&ctx, sizeof(ctx));
235                 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
236                         ctx.handle = i;
237                         if (DRM_COPY_TO_USER(&res->contexts[i],
238                             &ctx, sizeof(ctx)))
239                                 return EFAULT;
240                 }
241         }
242         res->count = DRM_RESERVED_CONTEXTS;
243
244         return 0;
245 }
246
247 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
248 {
249         struct drm_ctx *ctx = data;
250
251         ctx->handle = drm_ctxbitmap_next(dev);
252         if (ctx->handle == DRM_KERNEL_CONTEXT) {
253                 /* Skip kernel's context and get a new one. */
254                 ctx->handle = drm_ctxbitmap_next(dev);
255         }
256         DRM_DEBUG("%d\n", ctx->handle);
257         if (ctx->handle == -1) {
258                 DRM_DEBUG("Not enough free contexts.\n");
259                 /* Should this return -EBUSY instead? */
260                 return ENOMEM;
261         }
262
263         if (dev->driver->context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) {
264                 DRM_LOCK();
265                 dev->driver->context_ctor(dev, ctx->handle);
266                 DRM_UNLOCK();
267         }
268
269         return 0;
270 }
271
272 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
273 {
274         /* This does nothing */
275         return 0;
276 }
277
278 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
279 {
280         struct drm_ctx *ctx = data;
281
282         /* This is 0, because we don't handle any context flags */
283         ctx->flags = 0;
284
285         return 0;
286 }
287
288 int drm_switchctx(struct drm_device *dev, void *data,
289                   struct drm_file *file_priv)
290 {
291         struct drm_ctx *ctx = data;
292
293         DRM_DEBUG("%d\n", ctx->handle);
294         return drm_context_switch(dev, dev->last_context, ctx->handle);
295 }
296
297 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
298 {
299         struct drm_ctx *ctx = data;
300
301         DRM_DEBUG("%d\n", ctx->handle);
302         drm_context_switch_complete(dev, ctx->handle);
303
304         return 0;
305 }
306
307 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
308 {
309         struct drm_ctx *ctx = data;
310
311         DRM_DEBUG("%d\n", ctx->handle);
312         if (ctx->handle != DRM_KERNEL_CONTEXT) {
313                 if (dev->driver->context_dtor) {
314                         DRM_LOCK();
315                         dev->driver->context_dtor(dev, ctx->handle);
316                         DRM_UNLOCK();
317                 }
318
319                 drm_ctxbitmap_free(dev, ctx->handle);
320         }
321
322         return 0;
323 }