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>
22 #include <linux/module.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>
42 #include <asm/processor.h>
43 #include <asm/system.h>
47 static struct rtlx_info
*rtlx
;
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
;
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
72 static irqreturn_t
rtlx_interrupt(int irq
, void *dev_id
)
76 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
77 wake_up(&channel_wqs
[i
].lx_queue
);
78 wake_up(&channel_wqs
[i
].rt_queue
);
84 static void __used
dump_rtlx(void)
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
);
121 static void starting(int vpe
)
126 /* force a reload of rtlx */
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
)
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
;
151 if (index
>= RTLX_CHANNELS
) {
152 printk(KERN_DEBUG
"rtlx_open index out of range\n");
156 if (atomic_inc_return(&channel_wqs
[index
].in_open
) > 1) {
157 printk(KERN_DEBUG
"rtlx_open channel %d already opened\n",
164 if( (p
= vpe_get_shared(tclimit
)) == NULL
) {
166 __wait_event_interruptible(channel_wqs
[index
].lx_queue
,
167 (p
= vpe_get_shared(tclimit
)),
172 printk(KERN_DEBUG
"No SP program loaded, and device "
173 "opened with O_NONBLOCK\n");
185 prepare_to_wait(&channel_wqs
[index
].lx_queue
, &wait
, TASK_INTERRUPTIBLE
);
189 if (!signal_pending(current
)) {
196 finish_wait(&channel_wqs
[index
].lx_queue
, &wait
);
198 printk(" *vpe_get_shared is NULL. "
199 "Has an SP program been loaded?\n");
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
);
212 if ((ret
= rtlx_init(*p
)) < 0)
216 chan
= &rtlx
->channel
[index
];
218 state
= xchg(&chan
->lx_state
, RTLX_STATE_OPENED
);
219 if (state
== RTLX_STATE_OPENED
) {
226 atomic_dec(&channel_wqs
[index
].in_open
);
233 int rtlx_release(int index
)
235 rtlx
->channel
[index
].lx_state
= RTLX_STATE_UNUSED
;
239 unsigned int rtlx_read_poll(int index
, int can_sleep
)
241 struct rtlx_channel
*chan
;
246 chan
= &rtlx
->channel
[index
];
248 /* data available to read? */
249 if (chan
->lx_read
== chan
->lx_write
) {
253 __wait_event_interruptible(channel_wqs
[index
].lx_queue
,
254 chan
->lx_read
!= chan
->lx_write
|| sp_stopping
,
265 return (chan
->lx_write
+ chan
->buffer_size
- chan
->lx_read
)
269 static inline int write_spacefree(int read
, int write
, int size
)
273 * Never fill the buffer completely, so indexes are always
274 * equal if empty and only empty, or !equal if data available
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
;
297 lx
= &rtlx
->channel
[index
];
299 mutex_lock(&channel_wqs
[index
].mutex
);
301 lx_write
= lx
->lx_write
;
303 /* find out how much in total */
305 (size_t)(lx_write
+ lx
->buffer_size
- lx
->lx_read
)
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
);
315 /* and if there is anything left at the beginning of the buffer */
317 failed
= copy_to_user(buff
+ fl
, lx
->lx_buffer
, count
- fl
);
323 lx
->lx_read
= (lx
->lx_read
+ count
) % lx
->buffer_size
;
325 mutex_unlock(&channel_wqs
[index
].mutex
);
330 ssize_t
rtlx_write(int index
, const void __user
*buffer
, size_t count
)
332 struct rtlx_channel
*rt
;
333 unsigned long failed
;
340 rt
= &rtlx
->channel
[index
];
342 mutex_lock(&channel_wqs
[index
].mutex
);
344 rt_read
= rt
->rt_read
;
346 /* total number of bytes to copy */
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
);
357 /* if there's any left copy to the beginning of the buffer */
359 failed
= copy_from_user(rt
->rt_buffer
, buffer
+ fl
, count
- fl
);
366 rt
->rt_write
= (rt
->rt_write
+ count
) % rt
->buffer_size
;
368 mutex_unlock(&channel_wqs
[index
].mutex
);
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
)
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
);
401 /* data available to read? */
402 if (rtlx_read_poll(minor
, 0))
403 mask
|= POLLIN
| POLLRDNORM
;
406 if (rtlx_write_poll(minor
))
407 mask
|= POLLOUT
| POLLWRNORM
;
412 static ssize_t
file_read(struct file
*file
, char __user
* buffer
, size_t count
,
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
)
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
)) {
438 if (file
->f_flags
& O_NONBLOCK
)
441 __wait_event_interruptible(channel_wqs
[minor
].rt_queue
,
442 rtlx_write_poll(minor
),
448 return rtlx_write(minor
, buffer
, count
);
451 static const struct file_operations rtlx_fops
= {
452 .owner
= THIS_MODULE
,
454 .release
= file_release
,
460 static struct irqaction rtlx_irq
= {
461 .handler
= rtlx_interrupt
,
462 .flags
= IRQF_DISABLED
,
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)
476 if (!cpu_has_mipsmt
) {
477 printk("VPE loader: not a MIPS MT capable processor\n");
482 printk(KERN_WARNING
"No TCs reserved for AP/SP, not "
483 "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
489 major
= register_chrdev(0, module_name
, &rtlx_fops
);
491 printk(register_chrdev_failed
);
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
);
510 /* set up notifiers */
511 notify
.start
= starting
;
512 notify
.stop
= stopping
;
513 vpe_notify(tclimit
, ¬ify
);
516 set_vi_handler(MIPS_CPU_RTLX_IRQ
, rtlx_dispatch
);
518 rtlx_irq
.dev_id
= rtlx
;
519 setup_irq(rtlx_irq_num
, &rtlx_irq
);
524 for (i
= 0; i
< RTLX_CHANNELS
; i
++)
525 device_destroy(mt_class
, MKDEV(major
, i
));
530 static void __exit
rtlx_module_exit(void)
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");