ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / drm / gamma_context.h
1 /* drm_context.h -- IOCTLs for generic contexts -*- linux-c -*-
2  * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
3  *
4  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Gareth Hughes <gareth@valinux.com>
30  * ChangeLog:
31  *  2001-11-16  Torsten Duwe <duwe@caldera.de>
32  *              added context constructor/destructor hooks,
33  *              needed by SiS driver's memory management.
34  */
35
36 /* ================================================================
37  * Old-style context support -- only used by gamma.  
38  */
39
40
41 /* The drm_read and drm_write_string code (especially that which manages
42    the circular buffer), is based on Alessandro Rubini's LINUX DEVICE
43    DRIVERS (Cambridge: O'Reilly, 1998), pages 111-113. */
44
45 ssize_t DRM(read)(struct file *filp, char *buf, size_t count, loff_t *off)
46 {
47         drm_file_t    *priv   = filp->private_data;
48         drm_device_t  *dev    = priv->dev;
49         int           left;
50         int           avail;
51         int           send;
52         int           cur;
53
54         DRM_DEBUG("%p, %p\n", dev->buf_rp, dev->buf_wp);
55
56         while (dev->buf_rp == dev->buf_wp) {
57                 DRM_DEBUG("  sleeping\n");
58                 if (filp->f_flags & O_NONBLOCK) {
59                         return -EAGAIN;
60                 }
61                 interruptible_sleep_on(&dev->buf_readers);
62                 if (signal_pending(current)) {
63                         DRM_DEBUG("  interrupted\n");
64                         return -ERESTARTSYS;
65                 }
66                 DRM_DEBUG("  awake\n");
67         }
68
69         left  = (dev->buf_rp + DRM_BSZ - dev->buf_wp) % DRM_BSZ;
70         avail = DRM_BSZ - left;
71         send  = DRM_MIN(avail, count);
72
73         while (send) {
74                 if (dev->buf_wp > dev->buf_rp) {
75                         cur = DRM_MIN(send, dev->buf_wp - dev->buf_rp);
76                 } else {
77                         cur = DRM_MIN(send, dev->buf_end - dev->buf_rp);
78                 }
79                 if (copy_to_user(buf, dev->buf_rp, cur))
80                         return -EFAULT;
81                 dev->buf_rp += cur;
82                 if (dev->buf_rp == dev->buf_end) dev->buf_rp = dev->buf;
83                 send -= cur;
84         }
85
86         wake_up_interruptible(&dev->buf_writers);
87         return DRM_MIN(avail, count);
88 }
89
90
91 /* In an incredibly convoluted setup, the kernel module actually calls
92  * back into the X server to perform context switches on behalf of the
93  * 3d clients.
94  */
95 int DRM(write_string)(drm_device_t *dev, const char *s)
96 {
97         int left   = (dev->buf_rp + DRM_BSZ - dev->buf_wp) % DRM_BSZ;
98         int send   = strlen(s);
99         int count;
100
101         DRM_DEBUG("%d left, %d to send (%p, %p)\n",
102                   left, send, dev->buf_rp, dev->buf_wp);
103
104         if (left == 1 || dev->buf_wp != dev->buf_rp) {
105                 DRM_ERROR("Buffer not empty (%d left, wp = %p, rp = %p)\n",
106                           left,
107                           dev->buf_wp,
108                           dev->buf_rp);
109         }
110
111         while (send) {
112                 if (dev->buf_wp >= dev->buf_rp) {
113                         count = DRM_MIN(send, dev->buf_end - dev->buf_wp);
114                         if (count == left) --count; /* Leave a hole */
115                 } else {
116                         count = DRM_MIN(send, dev->buf_rp - dev->buf_wp - 1);
117                 }
118                 strncpy(dev->buf_wp, s, count);
119                 dev->buf_wp += count;
120                 if (dev->buf_wp == dev->buf_end) dev->buf_wp = dev->buf;
121                 send -= count;
122         }
123
124         if (dev->buf_async) kill_fasync(&dev->buf_async, SIGIO, POLL_IN);
125
126         DRM_DEBUG("waking\n");
127         wake_up_interruptible(&dev->buf_readers);
128         return 0;
129 }
130
131 unsigned int DRM(poll)(struct file *filp, struct poll_table_struct *wait)
132 {
133         drm_file_t   *priv = filp->private_data;
134         drm_device_t *dev  = priv->dev;
135
136         poll_wait(filp, &dev->buf_readers, wait);
137         if (dev->buf_wp != dev->buf_rp) return POLLIN | POLLRDNORM;
138         return 0;
139 }
140
141 int DRM(context_switch)(drm_device_t *dev, int old, int new)
142 {
143         char        buf[64];
144         drm_queue_t *q;
145
146         if (test_and_set_bit(0, &dev->context_flag)) {
147                 DRM_ERROR("Reentering -- FIXME\n");
148                 return -EBUSY;
149         }
150
151         DRM_DEBUG("Context switch from %d to %d\n", old, new);
152
153         if (new >= dev->queue_count) {
154                 clear_bit(0, &dev->context_flag);
155                 return -EINVAL;
156         }
157
158         if (new == dev->last_context) {
159                 clear_bit(0, &dev->context_flag);
160                 return 0;
161         }
162
163         q = dev->queuelist[new];
164         atomic_inc(&q->use_count);
165         if (atomic_read(&q->use_count) == 1) {
166                 atomic_dec(&q->use_count);
167                 clear_bit(0, &dev->context_flag);
168                 return -EINVAL;
169         }
170
171         /* This causes the X server to wake up & do a bunch of hardware
172          * interaction to actually effect the context switch.
173          */
174         sprintf(buf, "C %d %d\n", old, new);
175         DRM(write_string)(dev, buf);
176
177         atomic_dec(&q->use_count);
178
179         return 0;
180 }
181
182 int DRM(context_switch_complete)(drm_device_t *dev, int new)
183 {
184         drm_device_dma_t *dma = dev->dma;
185
186         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
187         dev->last_switch  = jiffies;
188
189         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
190                 DRM_ERROR("Lock isn't held after context switch\n");
191         }
192
193         if (!dma || !(dma->next_buffer && dma->next_buffer->while_locked)) {
194                 if (DRM(lock_free)(dev, &dev->lock.hw_lock->lock,
195                                   DRM_KERNEL_CONTEXT)) {
196                         DRM_ERROR("Cannot free lock\n");
197                 }
198         }
199
200         clear_bit(0, &dev->context_flag);
201         wake_up_interruptible(&dev->context_wait);
202
203         return 0;
204 }
205
206 static int DRM(init_queue)(drm_device_t *dev, drm_queue_t *q, drm_ctx_t *ctx)
207 {
208         DRM_DEBUG("\n");
209
210         if (atomic_read(&q->use_count) != 1
211             || atomic_read(&q->finalization)
212             || atomic_read(&q->block_count)) {
213                 DRM_ERROR("New queue is already in use: u%d f%d b%d\n",
214                           atomic_read(&q->use_count),
215                           atomic_read(&q->finalization),
216                           atomic_read(&q->block_count));
217         }
218
219         atomic_set(&q->finalization,  0);
220         atomic_set(&q->block_count,   0);
221         atomic_set(&q->block_read,    0);
222         atomic_set(&q->block_write,   0);
223         atomic_set(&q->total_queued,  0);
224         atomic_set(&q->total_flushed, 0);
225         atomic_set(&q->total_locks,   0);
226
227         init_waitqueue_head(&q->write_queue);
228         init_waitqueue_head(&q->read_queue);
229         init_waitqueue_head(&q->flush_queue);
230
231         q->flags = ctx->flags;
232
233         DRM(waitlist_create)(&q->waitlist, dev->dma->buf_count);
234
235         return 0;
236 }
237
238
239 /* drm_alloc_queue:
240 PRE: 1) dev->queuelist[0..dev->queue_count] is allocated and will not
241         disappear (so all deallocation must be done after IOCTLs are off)
242      2) dev->queue_count < dev->queue_slots
243      3) dev->queuelist[i].use_count == 0 and
244         dev->queuelist[i].finalization == 0 if i not in use
245 POST: 1) dev->queuelist[i].use_count == 1
246       2) dev->queue_count < dev->queue_slots */
247
248 static int DRM(alloc_queue)(drm_device_t *dev)
249 {
250         int         i;
251         drm_queue_t *queue;
252         int         oldslots;
253         int         newslots;
254                                 /* Check for a free queue */
255         for (i = 0; i < dev->queue_count; i++) {
256                 atomic_inc(&dev->queuelist[i]->use_count);
257                 if (atomic_read(&dev->queuelist[i]->use_count) == 1
258                     && !atomic_read(&dev->queuelist[i]->finalization)) {
259                         DRM_DEBUG("%d (free)\n", i);
260                         return i;
261                 }
262                 atomic_dec(&dev->queuelist[i]->use_count);
263         }
264                                 /* Allocate a new queue */
265         down(&dev->struct_sem);
266
267         queue = DRM(alloc)(sizeof(*queue), DRM_MEM_QUEUES);
268         memset(queue, 0, sizeof(*queue));
269         atomic_set(&queue->use_count, 1);
270
271         ++dev->queue_count;
272         if (dev->queue_count >= dev->queue_slots) {
273                 oldslots = dev->queue_slots * sizeof(*dev->queuelist);
274                 if (!dev->queue_slots) dev->queue_slots = 1;
275                 dev->queue_slots *= 2;
276                 newslots = dev->queue_slots * sizeof(*dev->queuelist);
277
278                 dev->queuelist = DRM(realloc)(dev->queuelist,
279                                               oldslots,
280                                               newslots,
281                                               DRM_MEM_QUEUES);
282                 if (!dev->queuelist) {
283                         up(&dev->struct_sem);
284                         DRM_DEBUG("out of memory\n");
285                         return -ENOMEM;
286                 }
287         }
288         dev->queuelist[dev->queue_count-1] = queue;
289
290         up(&dev->struct_sem);
291         DRM_DEBUG("%d (new)\n", dev->queue_count - 1);
292         return dev->queue_count - 1;
293 }
294
295 int DRM(resctx)(struct inode *inode, struct file *filp,
296                 unsigned int cmd, unsigned long arg)
297 {
298         drm_ctx_res_t   res;
299         drm_ctx_t       ctx;
300         int             i;
301
302         DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
303         if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
304                 return -EFAULT;
305         if (res.count >= DRM_RESERVED_CONTEXTS) {
306                 memset(&ctx, 0, sizeof(ctx));
307                 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
308                         ctx.handle = i;
309                         if (copy_to_user(&res.contexts[i],
310                                          &i,
311                                          sizeof(i)))
312                                 return -EFAULT;
313                 }
314         }
315         res.count = DRM_RESERVED_CONTEXTS;
316         if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
317                 return -EFAULT;
318         return 0;
319 }
320
321 int DRM(addctx)(struct inode *inode, struct file *filp,
322                 unsigned int cmd, unsigned long arg)
323 {
324         drm_file_t      *priv   = filp->private_data;
325         drm_device_t    *dev    = priv->dev;
326         drm_ctx_t       ctx;
327
328         if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
329                 return -EFAULT;
330         if ((ctx.handle = DRM(alloc_queue)(dev)) == DRM_KERNEL_CONTEXT) {
331                                 /* Init kernel's context and get a new one. */
332                 DRM(init_queue)(dev, dev->queuelist[ctx.handle], &ctx);
333                 ctx.handle = DRM(alloc_queue)(dev);
334         }
335         DRM(init_queue)(dev, dev->queuelist[ctx.handle], &ctx);
336         DRM_DEBUG("%d\n", ctx.handle);
337         if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
338                 return -EFAULT;
339         return 0;
340 }
341
342 int DRM(modctx)(struct inode *inode, struct file *filp,
343                 unsigned int cmd, unsigned long arg)
344 {
345         drm_file_t      *priv   = filp->private_data;
346         drm_device_t    *dev    = priv->dev;
347         drm_ctx_t       ctx;
348         drm_queue_t     *q;
349
350         if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
351                 return -EFAULT;
352
353         DRM_DEBUG("%d\n", ctx.handle);
354
355         if (ctx.handle < 0 || ctx.handle >= dev->queue_count) return -EINVAL;
356         q = dev->queuelist[ctx.handle];
357
358         atomic_inc(&q->use_count);
359         if (atomic_read(&q->use_count) == 1) {
360                                 /* No longer in use */
361                 atomic_dec(&q->use_count);
362                 return -EINVAL;
363         }
364
365         if (DRM_BUFCOUNT(&q->waitlist)) {
366                 atomic_dec(&q->use_count);
367                 return -EBUSY;
368         }
369
370         q->flags = ctx.flags;
371
372         atomic_dec(&q->use_count);
373         return 0;
374 }
375
376 int DRM(getctx)(struct inode *inode, struct file *filp,
377                 unsigned int cmd, unsigned long arg)
378 {
379         drm_file_t      *priv   = filp->private_data;
380         drm_device_t    *dev    = priv->dev;
381         drm_ctx_t       ctx;
382         drm_queue_t     *q;
383
384         if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
385                 return -EFAULT;
386
387         DRM_DEBUG("%d\n", ctx.handle);
388
389         if (ctx.handle >= dev->queue_count) return -EINVAL;
390         q = dev->queuelist[ctx.handle];
391
392         atomic_inc(&q->use_count);
393         if (atomic_read(&q->use_count) == 1) {
394                                 /* No longer in use */
395                 atomic_dec(&q->use_count);
396                 return -EINVAL;
397         }
398
399         ctx.flags = q->flags;
400         atomic_dec(&q->use_count);
401
402         if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
403                 return -EFAULT;
404
405         return 0;
406 }
407
408 int DRM(switchctx)(struct inode *inode, struct file *filp,
409                    unsigned int cmd, unsigned long arg)
410 {
411         drm_file_t      *priv   = filp->private_data;
412         drm_device_t    *dev    = priv->dev;
413         drm_ctx_t       ctx;
414
415         if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
416                 return -EFAULT;
417         DRM_DEBUG("%d\n", ctx.handle);
418         return DRM(context_switch)(dev, dev->last_context, ctx.handle);
419 }
420
421 int DRM(newctx)(struct inode *inode, struct file *filp,
422                 unsigned int cmd, unsigned long arg)
423 {
424         drm_file_t      *priv   = filp->private_data;
425         drm_device_t    *dev    = priv->dev;
426         drm_ctx_t       ctx;
427
428         if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
429                 return -EFAULT;
430         DRM_DEBUG("%d\n", ctx.handle);
431         DRM(context_switch_complete)(dev, ctx.handle);
432
433         return 0;
434 }
435
436 int DRM(rmctx)(struct inode *inode, struct file *filp,
437                unsigned int cmd, unsigned long arg)
438 {
439         drm_file_t      *priv   = filp->private_data;
440         drm_device_t    *dev    = priv->dev;
441         drm_ctx_t       ctx;
442         drm_queue_t     *q;
443         drm_buf_t       *buf;
444
445         if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
446                 return -EFAULT;
447         DRM_DEBUG("%d\n", ctx.handle);
448
449         if (ctx.handle >= dev->queue_count) return -EINVAL;
450         q = dev->queuelist[ctx.handle];
451
452         atomic_inc(&q->use_count);
453         if (atomic_read(&q->use_count) == 1) {
454                                 /* No longer in use */
455                 atomic_dec(&q->use_count);
456                 return -EINVAL;
457         }
458
459         atomic_inc(&q->finalization); /* Mark queue in finalization state */
460         atomic_sub(2, &q->use_count); /* Mark queue as unused (pending
461                                          finalization) */
462
463         while (test_and_set_bit(0, &dev->interrupt_flag)) {
464                 schedule();
465                 if (signal_pending(current)) {
466                         clear_bit(0, &dev->interrupt_flag);
467                         return -EINTR;
468                 }
469         }
470                                 /* Remove queued buffers */
471         while ((buf = DRM(waitlist_get)(&q->waitlist))) {
472                 DRM(free_buffer)(dev, buf);
473         }
474         clear_bit(0, &dev->interrupt_flag);
475
476                                 /* Wakeup blocked processes */
477         wake_up_interruptible(&q->read_queue);
478         wake_up_interruptible(&q->write_queue);
479         wake_up_interruptible(&q->flush_queue);
480
481                                 /* Finalization over.  Queue is made
482                                    available when both use_count and
483                                    finalization become 0, which won't
484                                    happen until all the waiting processes
485                                    stop waiting. */
486         atomic_dec(&q->finalization);
487         return 0;
488 }
489