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