Merge branches 'release' and 'menlo' into release
[linux-2.6] / arch / mips / kernel / rtlx.c
1 /*
2  * Copyright (C) 2005 MIPS Technologies, Inc.  All rights reserved.
3  * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
4  *
5  *  This program is free software; you can distribute it and/or modify it
6  *  under the terms of the GNU General Public License (Version 2) as
7  *  published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope it will be useful, but WITHOUT
10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17  *
18  */
19
20 #include <linux/device.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/fs.h>
24 #include <linux/init.h>
25 #include <asm/uaccess.h>
26 #include <linux/slab.h>
27 #include <linux/list.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/seq_file.h>
31 #include <linux/syscalls.h>
32 #include <linux/moduleloader.h>
33 #include <linux/interrupt.h>
34 #include <linux/poll.h>
35 #include <linux/sched.h>
36 #include <linux/wait.h>
37 #include <asm/mipsmtregs.h>
38 #include <asm/mips_mt.h>
39 #include <asm/cacheflush.h>
40 #include <asm/atomic.h>
41 #include <asm/cpu.h>
42 #include <asm/processor.h>
43 #include <asm/system.h>
44 #include <asm/vpe.h>
45 #include <asm/rtlx.h>
46
47 static struct rtlx_info *rtlx;
48 static int major;
49 static char module_name[] = "rtlx";
50
51 static struct chan_waitqueues {
52         wait_queue_head_t rt_queue;
53         wait_queue_head_t lx_queue;
54         atomic_t in_open;
55         struct mutex mutex;
56 } channel_wqs[RTLX_CHANNELS];
57
58 static struct vpe_notifications notify;
59 static int sp_stopping = 0;
60
61 extern void *vpe_get_shared(int index);
62
63 static void rtlx_dispatch(void)
64 {
65         do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
66 }
67
68
69 /* Interrupt handler may be called before rtlx_init has otherwise had
70    a chance to run.
71 */
72 static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
73 {
74         int i;
75
76         for (i = 0; i < RTLX_CHANNELS; i++) {
77                         wake_up(&channel_wqs[i].lx_queue);
78                         wake_up(&channel_wqs[i].rt_queue);
79         }
80
81         return IRQ_HANDLED;
82 }
83
84 static void __used dump_rtlx(void)
85 {
86         int i;
87
88         printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
89
90         for (i = 0; i < RTLX_CHANNELS; i++) {
91                 struct rtlx_channel *chan = &rtlx->channel[i];
92
93                 printk(" rt_state %d lx_state %d buffer_size %d\n",
94                        chan->rt_state, chan->lx_state, chan->buffer_size);
95
96                 printk(" rt_read %d rt_write %d\n",
97                        chan->rt_read, chan->rt_write);
98
99                 printk(" lx_read %d lx_write %d\n",
100                        chan->lx_read, chan->lx_write);
101
102                 printk(" rt_buffer <%s>\n", chan->rt_buffer);
103                 printk(" lx_buffer <%s>\n", chan->lx_buffer);
104         }
105 }
106
107 /* call when we have the address of the shared structure from the SP side. */
108 static int rtlx_init(struct rtlx_info *rtlxi)
109 {
110         if (rtlxi->id != RTLX_ID) {
111                 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
112                 return -ENOEXEC;
113         }
114
115         rtlx = rtlxi;
116
117         return 0;
118 }
119
120 /* notifications */
121 static void starting(int vpe)
122 {
123         int i;
124         sp_stopping = 0;
125
126         /* force a reload of rtlx */
127         rtlx=NULL;
128
129         /* wake up any sleeping rtlx_open's */
130         for (i = 0; i < RTLX_CHANNELS; i++)
131                 wake_up_interruptible(&channel_wqs[i].lx_queue);
132 }
133
134 static void stopping(int vpe)
135 {
136         int i;
137
138         sp_stopping = 1;
139         for (i = 0; i < RTLX_CHANNELS; i++)
140                 wake_up_interruptible(&channel_wqs[i].lx_queue);
141 }
142
143
144 int rtlx_open(int index, int can_sleep)
145 {
146         struct rtlx_info **p;
147         struct rtlx_channel *chan;
148         enum rtlx_state state;
149         int ret = 0;
150
151         if (index >= RTLX_CHANNELS) {
152                 printk(KERN_DEBUG "rtlx_open index out of range\n");
153                 return -ENOSYS;
154         }
155
156         if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
157                 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
158                        index);
159                 ret = -EBUSY;
160                 goto out_fail;
161         }
162
163         if (rtlx == NULL) {
164                 if( (p = vpe_get_shared(tclimit)) == NULL) {
165                         if (can_sleep) {
166                                 __wait_event_interruptible(channel_wqs[index].lx_queue,
167                                                            (p = vpe_get_shared(tclimit)),
168                                                            ret);
169                                 if (ret)
170                                         goto out_fail;
171                         } else {
172                                 printk(KERN_DEBUG "No SP program loaded, and device "
173                                         "opened with O_NONBLOCK\n");
174                                 ret = -ENOSYS;
175                                 goto out_fail;
176                         }
177                 }
178
179                 smp_rmb();
180                 if (*p == NULL) {
181                         if (can_sleep) {
182                                 DEFINE_WAIT(wait);
183
184                                 for (;;) {
185                                         prepare_to_wait(&channel_wqs[index].lx_queue, &wait, TASK_INTERRUPTIBLE);
186                                         smp_rmb();
187                                         if (*p != NULL)
188                                                 break;
189                                         if (!signal_pending(current)) {
190                                                 schedule();
191                                                 continue;
192                                         }
193                                         ret = -ERESTARTSYS;
194                                         goto out_fail;
195                                 }
196                                 finish_wait(&channel_wqs[index].lx_queue, &wait);
197                         } else {
198                                 printk(" *vpe_get_shared is NULL. "
199                                        "Has an SP program been loaded?\n");
200                                 ret = -ENOSYS;
201                                 goto out_fail;
202                         }
203                 }
204
205                 if ((unsigned int)*p < KSEG0) {
206                         printk(KERN_WARNING "vpe_get_shared returned an invalid pointer "
207                                "maybe an error code %d\n", (int)*p);
208                         ret = -ENOSYS;
209                         goto out_fail;
210                 }
211
212                 if ((ret = rtlx_init(*p)) < 0)
213                         goto out_ret;
214         }
215
216         chan = &rtlx->channel[index];
217
218         state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
219         if (state == RTLX_STATE_OPENED) {
220                 ret = -EBUSY;
221                 goto out_fail;
222         }
223
224 out_fail:
225         smp_mb();
226         atomic_dec(&channel_wqs[index].in_open);
227         smp_mb();
228
229 out_ret:
230         return ret;
231 }
232
233 int rtlx_release(int index)
234 {
235         rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
236         return 0;
237 }
238
239 unsigned int rtlx_read_poll(int index, int can_sleep)
240 {
241         struct rtlx_channel *chan;
242
243         if (rtlx == NULL)
244                 return 0;
245
246         chan = &rtlx->channel[index];
247
248         /* data available to read? */
249         if (chan->lx_read == chan->lx_write) {
250                 if (can_sleep) {
251                         int ret = 0;
252
253                         __wait_event_interruptible(channel_wqs[index].lx_queue,
254                                                    chan->lx_read != chan->lx_write || sp_stopping,
255                                                    ret);
256                         if (ret)
257                                 return ret;
258
259                         if (sp_stopping)
260                                 return 0;
261                 } else
262                         return 0;
263         }
264
265         return (chan->lx_write + chan->buffer_size - chan->lx_read)
266                % chan->buffer_size;
267 }
268
269 static inline int write_spacefree(int read, int write, int size)
270 {
271         if (read == write) {
272                 /*
273                  * Never fill the buffer completely, so indexes are always
274                  * equal if empty and only empty, or !equal if data available
275                  */
276                 return size - 1;
277         }
278
279         return ((read + size - write) % size) - 1;
280 }
281
282 unsigned int rtlx_write_poll(int index)
283 {
284         struct rtlx_channel *chan = &rtlx->channel[index];
285         return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size);
286 }
287
288 ssize_t rtlx_read(int index, void __user *buff, size_t count)
289 {
290         size_t lx_write, fl = 0L;
291         struct rtlx_channel *lx;
292         unsigned long failed;
293
294         if (rtlx == NULL)
295                 return -ENOSYS;
296
297         lx = &rtlx->channel[index];
298
299         mutex_lock(&channel_wqs[index].mutex);
300         smp_rmb();
301         lx_write = lx->lx_write;
302
303         /* find out how much in total */
304         count = min(count,
305                      (size_t)(lx_write + lx->buffer_size - lx->lx_read)
306                      % lx->buffer_size);
307
308         /* then how much from the read pointer onwards */
309         fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
310
311         failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
312         if (failed)
313                 goto out;
314
315         /* and if there is anything left at the beginning of the buffer */
316         if (count - fl)
317                 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
318
319 out:
320         count -= failed;
321
322         smp_wmb();
323         lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
324         smp_wmb();
325         mutex_unlock(&channel_wqs[index].mutex);
326
327         return count;
328 }
329
330 ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
331 {
332         struct rtlx_channel *rt;
333         unsigned long failed;
334         size_t rt_read;
335         size_t fl;
336
337         if (rtlx == NULL)
338                 return(-ENOSYS);
339
340         rt = &rtlx->channel[index];
341
342         mutex_lock(&channel_wqs[index].mutex);
343         smp_rmb();
344         rt_read = rt->rt_read;
345
346         /* total number of bytes to copy */
347         count = min(count,
348                     (size_t)write_spacefree(rt_read, rt->rt_write, rt->buffer_size));
349
350         /* first bit from write pointer to the end of the buffer, or count */
351         fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
352
353         failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
354         if (failed)
355                 goto out;
356
357         /* if there's any left copy to the beginning of the buffer */
358         if (count - fl) {
359                 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
360         }
361
362 out:
363         count -= failed;
364
365         smp_wmb();
366         rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
367         smp_wmb();
368         mutex_unlock(&channel_wqs[index].mutex);
369
370         return count;
371 }
372
373
374 static int file_open(struct inode *inode, struct file *filp)
375 {
376         int minor = iminor(inode);
377
378         return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
379 }
380
381 static int file_release(struct inode *inode, struct file *filp)
382 {
383         int minor = iminor(inode);
384
385         return rtlx_release(minor);
386 }
387
388 static unsigned int file_poll(struct file *file, poll_table * wait)
389 {
390         int minor;
391         unsigned int mask = 0;
392
393         minor = iminor(file->f_path.dentry->d_inode);
394
395         poll_wait(file, &channel_wqs[minor].rt_queue, wait);
396         poll_wait(file, &channel_wqs[minor].lx_queue, wait);
397
398         if (rtlx == NULL)
399                 return 0;
400
401         /* data available to read? */
402         if (rtlx_read_poll(minor, 0))
403                 mask |= POLLIN | POLLRDNORM;
404
405         /* space to write */
406         if (rtlx_write_poll(minor))
407                 mask |= POLLOUT | POLLWRNORM;
408
409         return mask;
410 }
411
412 static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
413                          loff_t * ppos)
414 {
415         int minor = iminor(file->f_path.dentry->d_inode);
416
417         /* data available? */
418         if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
419                 return 0;       // -EAGAIN makes cat whinge
420         }
421
422         return rtlx_read(minor, buffer, count);
423 }
424
425 static ssize_t file_write(struct file *file, const char __user * buffer,
426                           size_t count, loff_t * ppos)
427 {
428         int minor;
429         struct rtlx_channel *rt;
430
431         minor = iminor(file->f_path.dentry->d_inode);
432         rt = &rtlx->channel[minor];
433
434         /* any space left... */
435         if (!rtlx_write_poll(minor)) {
436                 int ret = 0;
437
438                 if (file->f_flags & O_NONBLOCK)
439                         return -EAGAIN;
440
441                 __wait_event_interruptible(channel_wqs[minor].rt_queue,
442                                            rtlx_write_poll(minor),
443                                            ret);
444                 if (ret)
445                         return ret;
446         }
447
448         return rtlx_write(minor, buffer, count);
449 }
450
451 static const struct file_operations rtlx_fops = {
452         .owner =   THIS_MODULE,
453         .open =    file_open,
454         .release = file_release,
455         .write =   file_write,
456         .read =    file_read,
457         .poll =    file_poll
458 };
459
460 static struct irqaction rtlx_irq = {
461         .handler        = rtlx_interrupt,
462         .flags          = IRQF_DISABLED,
463         .name           = "RTLX",
464 };
465
466 static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
467
468 static char register_chrdev_failed[] __initdata =
469         KERN_ERR "rtlx_module_init: unable to register device\n";
470
471 static int __init rtlx_module_init(void)
472 {
473         struct device *dev;
474         int i, err;
475
476         if (!cpu_has_mipsmt) {
477                 printk("VPE loader: not a MIPS MT capable processor\n");
478                 return -ENODEV;
479         }
480
481         if (tclimit == 0) {
482                 printk(KERN_WARNING "No TCs reserved for AP/SP, not "
483                        "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
484                        "argument\n");
485
486                 return -ENODEV;
487         }
488
489         major = register_chrdev(0, module_name, &rtlx_fops);
490         if (major < 0) {
491                 printk(register_chrdev_failed);
492                 return major;
493         }
494
495         /* initialise the wait queues */
496         for (i = 0; i < RTLX_CHANNELS; i++) {
497                 init_waitqueue_head(&channel_wqs[i].rt_queue);
498                 init_waitqueue_head(&channel_wqs[i].lx_queue);
499                 atomic_set(&channel_wqs[i].in_open, 0);
500                 mutex_init(&channel_wqs[i].mutex);
501
502                 dev = device_create(mt_class, NULL, MKDEV(major, i),
503                                     "%s%d", module_name, i);
504                 if (IS_ERR(dev)) {
505                         err = PTR_ERR(dev);
506                         goto out_chrdev;
507                 }
508         }
509
510         /* set up notifiers */
511         notify.start = starting;
512         notify.stop = stopping;
513         vpe_notify(tclimit, &notify);
514
515         if (cpu_has_vint)
516                 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
517
518         rtlx_irq.dev_id = rtlx;
519         setup_irq(rtlx_irq_num, &rtlx_irq);
520
521         return 0;
522
523 out_chrdev:
524         for (i = 0; i < RTLX_CHANNELS; i++)
525                 device_destroy(mt_class, MKDEV(major, i));
526
527         return err;
528 }
529
530 static void __exit rtlx_module_exit(void)
531 {
532         int i;
533
534         for (i = 0; i < RTLX_CHANNELS; i++)
535                 device_destroy(mt_class, MKDEV(major, i));
536
537         unregister_chrdev(major, module_name);
538 }
539
540 module_init(rtlx_module_init);
541 module_exit(rtlx_module_exit);
542
543 MODULE_DESCRIPTION("MIPS RTLX");
544 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
545 MODULE_LICENSE("GPL");