Linux 2.6.25.3
[linux/fpc-iii.git] / arch / mips / kernel / rtlx.c
blob0233798f71552c4c64b85e9a61e45f3aafc9fe6c
1 /*
2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
3 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
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.
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.
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.
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>
47 static struct rtlx_info *rtlx;
48 static int major;
49 static char module_name[] = "rtlx";
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];
58 static struct vpe_notifications notify;
59 static int sp_stopping = 0;
61 extern void *vpe_get_shared(int index);
63 static void rtlx_dispatch(void)
65 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
69 /* Interrupt handler may be called before rtlx_init has otherwise had
70 a chance to run.
72 static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
74 int i;
76 for (i = 0; i < RTLX_CHANNELS; i++) {
77 wake_up(&channel_wqs[i].lx_queue);
78 wake_up(&channel_wqs[i].rt_queue);
81 return IRQ_HANDLED;
84 static void __used dump_rtlx(void)
86 int i;
88 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
90 for (i = 0; i < RTLX_CHANNELS; i++) {
91 struct rtlx_channel *chan = &rtlx->channel[i];
93 printk(" rt_state %d lx_state %d buffer_size %d\n",
94 chan->rt_state, chan->lx_state, chan->buffer_size);
96 printk(" rt_read %d rt_write %d\n",
97 chan->rt_read, chan->rt_write);
99 printk(" lx_read %d lx_write %d\n",
100 chan->lx_read, chan->lx_write);
102 printk(" rt_buffer <%s>\n", chan->rt_buffer);
103 printk(" lx_buffer <%s>\n", chan->lx_buffer);
107 /* call when we have the address of the shared structure from the SP side. */
108 static int rtlx_init(struct rtlx_info *rtlxi)
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;
115 rtlx = rtlxi;
117 return 0;
120 /* notifications */
121 static void starting(int vpe)
123 int i;
124 sp_stopping = 0;
126 /* force a reload of rtlx */
127 rtlx=NULL;
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);
134 static void stopping(int vpe)
136 int i;
138 sp_stopping = 1;
139 for (i = 0; i < RTLX_CHANNELS; i++)
140 wake_up_interruptible(&channel_wqs[i].lx_queue);
144 int rtlx_open(int index, int can_sleep)
146 struct rtlx_info **p;
147 struct rtlx_channel *chan;
148 enum rtlx_state state;
149 int ret = 0;
151 if (index >= RTLX_CHANNELS) {
152 printk(KERN_DEBUG "rtlx_open index out of range\n");
153 return -ENOSYS;
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;
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;
179 smp_rmb();
180 if (*p == NULL) {
181 if (can_sleep) {
182 DEFINE_WAIT(wait);
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;
193 ret = -ERESTARTSYS;
194 goto out_fail;
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;
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;
212 if ((ret = rtlx_init(*p)) < 0)
213 goto out_ret;
216 chan = &rtlx->channel[index];
218 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
219 if (state == RTLX_STATE_OPENED) {
220 ret = -EBUSY;
221 goto out_fail;
224 out_fail:
225 smp_mb();
226 atomic_dec(&channel_wqs[index].in_open);
227 smp_mb();
229 out_ret:
230 return ret;
233 int rtlx_release(int index)
235 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
236 return 0;
239 unsigned int rtlx_read_poll(int index, int can_sleep)
241 struct rtlx_channel *chan;
243 if (rtlx == NULL)
244 return 0;
246 chan = &rtlx->channel[index];
248 /* data available to read? */
249 if (chan->lx_read == chan->lx_write) {
250 if (can_sleep) {
251 int ret = 0;
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;
259 if (sp_stopping)
260 return 0;
261 } else
262 return 0;
265 return (chan->lx_write + chan->buffer_size - chan->lx_read)
266 % chan->buffer_size;
269 static inline int write_spacefree(int read, int write, int size)
271 if (read == write) {
273 * Never fill the buffer completely, so indexes are always
274 * equal if empty and only empty, or !equal if data available
276 return size - 1;
279 return ((read + size - write) % size) - 1;
282 unsigned int rtlx_write_poll(int index)
284 struct rtlx_channel *chan = &rtlx->channel[index];
285 return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size);
288 ssize_t rtlx_read(int index, void __user *buff, size_t count)
290 size_t lx_write, fl = 0L;
291 struct rtlx_channel *lx;
292 unsigned long failed;
294 if (rtlx == NULL)
295 return -ENOSYS;
297 lx = &rtlx->channel[index];
299 mutex_lock(&channel_wqs[index].mutex);
300 smp_rmb();
301 lx_write = lx->lx_write;
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);
308 /* then how much from the read pointer onwards */
309 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
311 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
312 if (failed)
313 goto out;
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);
319 out:
320 count -= failed;
322 smp_wmb();
323 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
324 smp_wmb();
325 mutex_unlock(&channel_wqs[index].mutex);
327 return count;
330 ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
332 struct rtlx_channel *rt;
333 unsigned long failed;
334 size_t rt_read;
335 size_t fl;
337 if (rtlx == NULL)
338 return(-ENOSYS);
340 rt = &rtlx->channel[index];
342 mutex_lock(&channel_wqs[index].mutex);
343 smp_rmb();
344 rt_read = rt->rt_read;
346 /* total number of bytes to copy */
347 count = min(count,
348 (size_t)write_spacefree(rt_read, rt->rt_write, rt->buffer_size));
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);
353 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
354 if (failed)
355 goto out;
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);
362 out:
363 count -= failed;
365 smp_wmb();
366 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
367 smp_wmb();
368 mutex_unlock(&channel_wqs[index].mutex);
370 return count;
374 static int file_open(struct inode *inode, struct file *filp)
376 int minor = iminor(inode);
378 return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
381 static int file_release(struct inode *inode, struct file *filp)
383 int minor = iminor(inode);
385 return rtlx_release(minor);
388 static unsigned int file_poll(struct file *file, poll_table * wait)
390 int minor;
391 unsigned int mask = 0;
393 minor = iminor(file->f_path.dentry->d_inode);
395 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
396 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
398 if (rtlx == NULL)
399 return 0;
401 /* data available to read? */
402 if (rtlx_read_poll(minor, 0))
403 mask |= POLLIN | POLLRDNORM;
405 /* space to write */
406 if (rtlx_write_poll(minor))
407 mask |= POLLOUT | POLLWRNORM;
409 return mask;
412 static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
413 loff_t * ppos)
415 int minor = iminor(file->f_path.dentry->d_inode);
417 /* data available? */
418 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
419 return 0; // -EAGAIN makes cat whinge
422 return rtlx_read(minor, buffer, count);
425 static ssize_t file_write(struct file *file, const char __user * buffer,
426 size_t count, loff_t * ppos)
428 int minor;
429 struct rtlx_channel *rt;
431 minor = iminor(file->f_path.dentry->d_inode);
432 rt = &rtlx->channel[minor];
434 /* any space left... */
435 if (!rtlx_write_poll(minor)) {
436 int ret = 0;
438 if (file->f_flags & O_NONBLOCK)
439 return -EAGAIN;
441 __wait_event_interruptible(channel_wqs[minor].rt_queue,
442 rtlx_write_poll(minor),
443 ret);
444 if (ret)
445 return ret;
448 return rtlx_write(minor, buffer, count);
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
460 static struct irqaction rtlx_irq = {
461 .handler = rtlx_interrupt,
462 .flags = IRQF_DISABLED,
463 .name = "RTLX",
466 static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
468 static char register_chrdev_failed[] __initdata =
469 KERN_ERR "rtlx_module_init: unable to register device\n";
471 static int __init rtlx_module_init(void)
473 struct device *dev;
474 int i, err;
476 if (!cpu_has_mipsmt) {
477 printk("VPE loader: not a MIPS MT capable processor\n");
478 return -ENODEV;
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");
486 return -ENODEV;
489 major = register_chrdev(0, module_name, &rtlx_fops);
490 if (major < 0) {
491 printk(register_chrdev_failed);
492 return major;
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);
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;
510 /* set up notifiers */
511 notify.start = starting;
512 notify.stop = stopping;
513 vpe_notify(tclimit, &notify);
515 if (cpu_has_vint)
516 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
518 rtlx_irq.dev_id = rtlx;
519 setup_irq(rtlx_irq_num, &rtlx_irq);
521 return 0;
523 out_chrdev:
524 for (i = 0; i < RTLX_CHANNELS; i++)
525 device_destroy(mt_class, MKDEV(major, i));
527 return err;
530 static void __exit rtlx_module_exit(void)
532 int i;
534 for (i = 0; i < RTLX_CHANNELS; i++)
535 device_destroy(mt_class, MKDEV(major, i));
537 unregister_chrdev(major, module_name);
540 module_init(rtlx_module_init);
541 module_exit(rtlx_module_exit);
543 MODULE_DESCRIPTION("MIPS RTLX");
544 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
545 MODULE_LICENSE("GPL");