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
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>
23 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <linux/list.h>
26 #include <linux/vmalloc.h>
27 #include <linux/elf.h>
28 #include <linux/seq_file.h>
29 #include <linux/syscalls.h>
30 #include <linux/moduleloader.h>
31 #include <linux/interrupt.h>
32 #include <linux/poll.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <asm/mipsmtregs.h>
36 #include <asm/mips_mt.h>
37 #include <asm/cacheflush.h>
38 #include <linux/atomic.h>
40 #include <asm/processor.h>
41 #include <asm/system.h>
45 static struct rtlx_info
*rtlx
;
47 static char module_name
[] = "rtlx";
49 static struct chan_waitqueues
{
50 wait_queue_head_t rt_queue
;
51 wait_queue_head_t lx_queue
;
54 } channel_wqs
[RTLX_CHANNELS
];
56 static struct vpe_notifications notify
;
57 static int sp_stopping
;
59 extern void *vpe_get_shared(int index
);
61 static void rtlx_dispatch(void)
63 do_IRQ(MIPS_CPU_IRQ_BASE
+ MIPS_CPU_RTLX_IRQ
);
67 /* Interrupt handler may be called before rtlx_init has otherwise had
70 static irqreturn_t
rtlx_interrupt(int irq
, void *dev_id
)
72 unsigned int vpeflags
;
76 /* Ought not to be strictly necessary for SMTC builds */
77 local_irq_save(flags
);
79 set_c0_status(0x100 << MIPS_CPU_RTLX_IRQ
);
82 local_irq_restore(flags
);
84 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
85 wake_up(&channel_wqs
[i
].lx_queue
);
86 wake_up(&channel_wqs
[i
].rt_queue
);
92 static void __used
dump_rtlx(void)
96 printk("id 0x%lx state %d\n", rtlx
->id
, rtlx
->state
);
98 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
99 struct rtlx_channel
*chan
= &rtlx
->channel
[i
];
101 printk(" rt_state %d lx_state %d buffer_size %d\n",
102 chan
->rt_state
, chan
->lx_state
, chan
->buffer_size
);
104 printk(" rt_read %d rt_write %d\n",
105 chan
->rt_read
, chan
->rt_write
);
107 printk(" lx_read %d lx_write %d\n",
108 chan
->lx_read
, chan
->lx_write
);
110 printk(" rt_buffer <%s>\n", chan
->rt_buffer
);
111 printk(" lx_buffer <%s>\n", chan
->lx_buffer
);
115 /* call when we have the address of the shared structure from the SP side. */
116 static int rtlx_init(struct rtlx_info
*rtlxi
)
118 if (rtlxi
->id
!= RTLX_ID
) {
119 printk(KERN_ERR
"no valid RTLX id at 0x%p 0x%lx\n",
130 static void starting(int vpe
)
135 /* force a reload of rtlx */
138 /* wake up any sleeping rtlx_open's */
139 for (i
= 0; i
< RTLX_CHANNELS
; i
++)
140 wake_up_interruptible(&channel_wqs
[i
].lx_queue
);
143 static void stopping(int vpe
)
148 for (i
= 0; i
< RTLX_CHANNELS
; i
++)
149 wake_up_interruptible(&channel_wqs
[i
].lx_queue
);
153 int rtlx_open(int index
, int can_sleep
)
155 struct rtlx_info
**p
;
156 struct rtlx_channel
*chan
;
157 enum rtlx_state state
;
160 if (index
>= RTLX_CHANNELS
) {
161 printk(KERN_DEBUG
"rtlx_open index out of range\n");
165 if (atomic_inc_return(&channel_wqs
[index
].in_open
) > 1) {
166 printk(KERN_DEBUG
"rtlx_open channel %d already opened\n",
173 if( (p
= vpe_get_shared(tclimit
)) == NULL
) {
175 __wait_event_interruptible(channel_wqs
[index
].lx_queue
,
176 (p
= vpe_get_shared(tclimit
)), ret
);
180 printk(KERN_DEBUG
"No SP program loaded, and device "
181 "opened with O_NONBLOCK\n");
194 &channel_wqs
[index
].lx_queue
,
195 &wait
, TASK_INTERRUPTIBLE
);
199 if (!signal_pending(current
)) {
206 finish_wait(&channel_wqs
[index
].lx_queue
, &wait
);
208 pr_err(" *vpe_get_shared is NULL. "
209 "Has an SP program been loaded?\n");
215 if ((unsigned int)*p
< KSEG0
) {
216 printk(KERN_WARNING
"vpe_get_shared returned an "
217 "invalid pointer maybe an error code %d\n",
223 if ((ret
= rtlx_init(*p
)) < 0)
227 chan
= &rtlx
->channel
[index
];
229 state
= xchg(&chan
->lx_state
, RTLX_STATE_OPENED
);
230 if (state
== RTLX_STATE_OPENED
) {
237 atomic_dec(&channel_wqs
[index
].in_open
);
244 int rtlx_release(int index
)
247 pr_err("rtlx_release() with null rtlx\n");
250 rtlx
->channel
[index
].lx_state
= RTLX_STATE_UNUSED
;
254 unsigned int rtlx_read_poll(int index
, int can_sleep
)
256 struct rtlx_channel
*chan
;
261 chan
= &rtlx
->channel
[index
];
263 /* data available to read? */
264 if (chan
->lx_read
== chan
->lx_write
) {
268 __wait_event_interruptible(channel_wqs
[index
].lx_queue
,
269 (chan
->lx_read
!= chan
->lx_write
) ||
280 return (chan
->lx_write
+ chan
->buffer_size
- chan
->lx_read
)
284 static inline int write_spacefree(int read
, int write
, int size
)
288 * Never fill the buffer completely, so indexes are always
289 * equal if empty and only empty, or !equal if data available
294 return ((read
+ size
- write
) % size
) - 1;
297 unsigned int rtlx_write_poll(int index
)
299 struct rtlx_channel
*chan
= &rtlx
->channel
[index
];
301 return write_spacefree(chan
->rt_read
, chan
->rt_write
,
305 ssize_t
rtlx_read(int index
, void __user
*buff
, size_t count
)
307 size_t lx_write
, fl
= 0L;
308 struct rtlx_channel
*lx
;
309 unsigned long failed
;
314 lx
= &rtlx
->channel
[index
];
316 mutex_lock(&channel_wqs
[index
].mutex
);
318 lx_write
= lx
->lx_write
;
320 /* find out how much in total */
322 (size_t)(lx_write
+ lx
->buffer_size
- lx
->lx_read
)
325 /* then how much from the read pointer onwards */
326 fl
= min(count
, (size_t)lx
->buffer_size
- lx
->lx_read
);
328 failed
= copy_to_user(buff
, lx
->lx_buffer
+ lx
->lx_read
, fl
);
332 /* and if there is anything left at the beginning of the buffer */
334 failed
= copy_to_user(buff
+ fl
, lx
->lx_buffer
, count
- fl
);
340 lx
->lx_read
= (lx
->lx_read
+ count
) % lx
->buffer_size
;
342 mutex_unlock(&channel_wqs
[index
].mutex
);
347 ssize_t
rtlx_write(int index
, const void __user
*buffer
, size_t count
)
349 struct rtlx_channel
*rt
;
350 unsigned long failed
;
357 rt
= &rtlx
->channel
[index
];
359 mutex_lock(&channel_wqs
[index
].mutex
);
361 rt_read
= rt
->rt_read
;
363 /* total number of bytes to copy */
364 count
= min(count
, (size_t)write_spacefree(rt_read
, rt
->rt_write
,
367 /* first bit from write pointer to the end of the buffer, or count */
368 fl
= min(count
, (size_t) rt
->buffer_size
- rt
->rt_write
);
370 failed
= copy_from_user(rt
->rt_buffer
+ rt
->rt_write
, buffer
, fl
);
374 /* if there's any left copy to the beginning of the buffer */
376 failed
= copy_from_user(rt
->rt_buffer
, buffer
+ fl
, count
- fl
);
383 rt
->rt_write
= (rt
->rt_write
+ count
) % rt
->buffer_size
;
385 mutex_unlock(&channel_wqs
[index
].mutex
);
391 static int file_open(struct inode
*inode
, struct file
*filp
)
393 return rtlx_open(iminor(inode
), (filp
->f_flags
& O_NONBLOCK
) ? 0 : 1);
396 static int file_release(struct inode
*inode
, struct file
*filp
)
398 return rtlx_release(iminor(inode
));
401 static unsigned int file_poll(struct file
*file
, poll_table
* wait
)
404 unsigned int mask
= 0;
406 minor
= iminor(file
->f_path
.dentry
->d_inode
);
408 poll_wait(file
, &channel_wqs
[minor
].rt_queue
, wait
);
409 poll_wait(file
, &channel_wqs
[minor
].lx_queue
, wait
);
414 /* data available to read? */
415 if (rtlx_read_poll(minor
, 0))
416 mask
|= POLLIN
| POLLRDNORM
;
419 if (rtlx_write_poll(minor
))
420 mask
|= POLLOUT
| POLLWRNORM
;
425 static ssize_t
file_read(struct file
*file
, char __user
* buffer
, size_t count
,
428 int minor
= iminor(file
->f_path
.dentry
->d_inode
);
430 /* data available? */
431 if (!rtlx_read_poll(minor
, (file
->f_flags
& O_NONBLOCK
) ? 0 : 1)) {
432 return 0; // -EAGAIN makes cat whinge
435 return rtlx_read(minor
, buffer
, count
);
438 static ssize_t
file_write(struct file
*file
, const char __user
* buffer
,
439 size_t count
, loff_t
* ppos
)
442 struct rtlx_channel
*rt
;
444 minor
= iminor(file
->f_path
.dentry
->d_inode
);
445 rt
= &rtlx
->channel
[minor
];
447 /* any space left... */
448 if (!rtlx_write_poll(minor
)) {
451 if (file
->f_flags
& O_NONBLOCK
)
454 __wait_event_interruptible(channel_wqs
[minor
].rt_queue
,
455 rtlx_write_poll(minor
),
461 return rtlx_write(minor
, buffer
, count
);
464 static const struct file_operations rtlx_fops
= {
465 .owner
= THIS_MODULE
,
467 .release
= file_release
,
471 .llseek
= noop_llseek
,
474 static struct irqaction rtlx_irq
= {
475 .handler
= rtlx_interrupt
,
479 static int rtlx_irq_num
= MIPS_CPU_IRQ_BASE
+ MIPS_CPU_RTLX_IRQ
;
481 static char register_chrdev_failed
[] __initdata
=
482 KERN_ERR
"rtlx_module_init: unable to register device\n";
484 static int __init
rtlx_module_init(void)
489 if (!cpu_has_mipsmt
) {
490 printk("VPE loader: not a MIPS MT capable processor\n");
495 printk(KERN_WARNING
"No TCs reserved for AP/SP, not "
496 "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
502 major
= register_chrdev(0, module_name
, &rtlx_fops
);
504 printk(register_chrdev_failed
);
508 /* initialise the wait queues */
509 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
510 init_waitqueue_head(&channel_wqs
[i
].rt_queue
);
511 init_waitqueue_head(&channel_wqs
[i
].lx_queue
);
512 atomic_set(&channel_wqs
[i
].in_open
, 0);
513 mutex_init(&channel_wqs
[i
].mutex
);
515 dev
= device_create(mt_class
, NULL
, MKDEV(major
, i
), NULL
,
516 "%s%d", module_name
, i
);
523 /* set up notifiers */
524 notify
.start
= starting
;
525 notify
.stop
= stopping
;
526 vpe_notify(tclimit
, ¬ify
);
529 set_vi_handler(MIPS_CPU_RTLX_IRQ
, rtlx_dispatch
);
531 pr_err("APRP RTLX init on non-vectored-interrupt processor\n");
536 rtlx_irq
.dev_id
= rtlx
;
537 setup_irq(rtlx_irq_num
, &rtlx_irq
);
542 for (i
= 0; i
< RTLX_CHANNELS
; i
++)
543 device_destroy(mt_class
, MKDEV(major
, i
));
548 static void __exit
rtlx_module_exit(void)
552 for (i
= 0; i
< RTLX_CHANNELS
; i
++)
553 device_destroy(mt_class
, MKDEV(major
, i
));
555 unregister_chrdev(major
, module_name
);
558 module_init(rtlx_module_init
);
559 module_exit(rtlx_module_exit
);
561 MODULE_DESCRIPTION("MIPS RTLX");
562 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
563 MODULE_LICENSE("GPL");