[MIPS] Don't use max_pfn which is no longer initialized these days.
[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
RB
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/fs.h>
24#include <linux/init.h>
2600990e
RB
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>
a84c96e2 33#include <linux/interrupt.h>
e01402b1
RB
34#include <linux/poll.h>
35#include <linux/sched.h>
36#include <linux/wait.h>
37#include <asm/mipsmtregs.h>
bb3d7c7f 38#include <asm/mips_mt.h>
2600990e
RB
39#include <asm/cacheflush.h>
40#include <asm/atomic.h>
e01402b1
RB
41#include <asm/cpu.h>
42#include <asm/processor.h>
2600990e
RB
43#include <asm/system.h>
44#include <asm/vpe.h>
e01402b1
RB
45#include <asm/rtlx.h>
46
afc4841d 47static struct rtlx_info *rtlx;
e01402b1
RB
48static int major;
49static char module_name[] = "rtlx";
e01402b1
RB
50
51static struct chan_waitqueues {
52 wait_queue_head_t rt_queue;
53 wait_queue_head_t lx_queue;
c4c4018b 54 atomic_t in_open;
bc4809e9 55 struct mutex mutex;
e01402b1
RB
56} channel_wqs[RTLX_CHANNELS];
57
2600990e
RB
58static struct vpe_notifications notify;
59static int sp_stopping = 0;
60
e01402b1
RB
61extern void *vpe_get_shared(int index);
62
937a8015 63static void rtlx_dispatch(void)
e01402b1 64{
97dcb82d 65 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
e01402b1
RB
66}
67
2600990e
RB
68
69/* Interrupt handler may be called before rtlx_init has otherwise had
70 a chance to run.
71*/
937a8015 72static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
e01402b1 73{
e01402b1
RB
74 int i;
75
76 for (i = 0; i < RTLX_CHANNELS; i++) {
2600990e
RB
77 wake_up(&channel_wqs[i].lx_queue);
78 wake_up(&channel_wqs[i].rt_queue);
e01402b1
RB
79 }
80
afc4841d 81 return IRQ_HANDLED;
e01402b1
RB
82}
83
f5dbeaf5 84static void __used dump_rtlx(void)
e01402b1
RB
85{
86 int i;
87
2600990e 88 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
e01402b1 89
e01402b1 90 for (i = 0; i < RTLX_CHANNELS; i++) {
2600990e 91 struct rtlx_channel *chan = &rtlx->channel[i];
e01402b1 92
2600990e
RB
93 printk(" rt_state %d lx_state %d buffer_size %d\n",
94 chan->rt_state, chan->lx_state, chan->buffer_size);
e01402b1 95
2600990e
RB
96 printk(" rt_read %d rt_write %d\n",
97 chan->rt_read, chan->rt_write);
e01402b1 98
2600990e
RB
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. */
108static int rtlx_init(struct rtlx_info *rtlxi)
109{
110 if (rtlxi->id != RTLX_ID) {
e606c109 111 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
2600990e
RB
112 return -ENOEXEC;
113 }
e01402b1
RB
114
115 rtlx = rtlxi;
afc4841d
RB
116
117 return 0;
e01402b1
RB
118}
119
2600990e
RB
120/* notifications */
121static void starting(int vpe)
e01402b1 122{
2600990e
RB
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
134static 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
144int rtlx_open(int index, int can_sleep)
145{
9e346820 146 struct rtlx_info **p;
67e2ccce 147 struct rtlx_channel *chan;
c4c4018b 148 enum rtlx_state state;
67e2ccce 149 int ret = 0;
e01402b1 150
2600990e
RB
151 if (index >= RTLX_CHANNELS) {
152 printk(KERN_DEBUG "rtlx_open index out of range\n");
153 return -ENOSYS;
154 }
155
c4c4018b
RB
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;
2600990e
RB
161 }
162
e01402b1 163 if (rtlx == NULL) {
07cc0c9e 164 if( (p = vpe_get_shared(tclimit)) == NULL) {
2600990e 165 if (can_sleep) {
67e2ccce 166 __wait_event_interruptible(channel_wqs[index].lx_queue,
07cc0c9e 167 (p = vpe_get_shared(tclimit)),
67e2ccce
RB
168 ret);
169 if (ret)
170 goto out_fail;
2600990e 171 } else {
c4c4018b 172 printk(KERN_DEBUG "No SP program loaded, and device "
2600990e 173 "opened with O_NONBLOCK\n");
67e2ccce
RB
174 ret = -ENOSYS;
175 goto out_fail;
2600990e 176 }
e01402b1
RB
177 }
178
9e346820 179 smp_rmb();
e01402b1 180 if (*p == NULL) {
2600990e 181 if (can_sleep) {
9e346820
RB
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;
67e2ccce 194 goto out_fail;
9e346820
RB
195 }
196 finish_wait(&channel_wqs[index].lx_queue, &wait);
67e2ccce 197 } else {
2600990e
RB
198 printk(" *vpe_get_shared is NULL. "
199 "Has an SP program been loaded?\n");
67e2ccce
RB
200 ret = -ENOSYS;
201 goto out_fail;
2600990e
RB
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);
67e2ccce
RB
208 ret = -ENOSYS;
209 goto out_fail;
e01402b1
RB
210 }
211
c4c4018b
RB
212 if ((ret = rtlx_init(*p)) < 0)
213 goto out_ret;
e01402b1
RB
214 }
215
2600990e 216 chan = &rtlx->channel[index];
e01402b1 217
c4c4018b
RB
218 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
219 if (state == RTLX_STATE_OPENED) {
220 ret = -EBUSY;
221 goto out_fail;
222 }
67e2ccce
RB
223
224out_fail:
c4c4018b
RB
225 smp_mb();
226 atomic_dec(&channel_wqs[index].in_open);
227 smp_mb();
67e2ccce 228
c4c4018b 229out_ret:
67e2ccce 230 return ret;
e01402b1
RB
231}
232
2600990e 233int rtlx_release(int index)
e01402b1 234{
2600990e 235 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
afc4841d 236 return 0;
e01402b1
RB
237}
238
2600990e 239unsigned int rtlx_read_poll(int index, int can_sleep)
e01402b1 240{
2600990e 241 struct rtlx_channel *chan;
e01402b1 242
2600990e
RB
243 if (rtlx == NULL)
244 return 0;
e01402b1 245
2600990e 246 chan = &rtlx->channel[index];
e01402b1
RB
247
248 /* data available to read? */
2600990e
RB
249 if (chan->lx_read == chan->lx_write) {
250 if (can_sleep) {
67e2ccce 251 int ret = 0;
e01402b1 252
67e2ccce
RB
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;
e01402b1 258
67e2ccce
RB
259 if (sp_stopping)
260 return 0;
261 } else
2600990e
RB
262 return 0;
263 }
264
265 return (chan->lx_write + chan->buffer_size - chan->lx_read)
266 % chan->buffer_size;
e01402b1
RB
267}
268
2600990e 269static inline int write_spacefree(int read, int write, int size)
e01402b1 270{
2600990e
RB
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 }
e01402b1 278
2600990e
RB
279 return ((read + size - write) % size) - 1;
280}
e01402b1 281
2600990e
RB
282unsigned 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}
e01402b1 287
7f5a7716 288ssize_t rtlx_read(int index, void __user *buff, size_t count)
2600990e 289{
61dcc6f4 290 size_t lx_write, fl = 0L;
2600990e 291 struct rtlx_channel *lx;
46230aa6 292 unsigned long failed;
e01402b1 293
2600990e
RB
294 if (rtlx == NULL)
295 return -ENOSYS;
296
297 lx = &rtlx->channel[index];
e01402b1 298
bc4809e9 299 mutex_lock(&channel_wqs[index].mutex);
61dcc6f4
RB
300 smp_rmb();
301 lx_write = lx->lx_write;
302
e01402b1 303 /* find out how much in total */
afc4841d 304 count = min(count,
61dcc6f4 305 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
2600990e 306 % lx->buffer_size);
e01402b1
RB
307
308 /* then how much from the read pointer onwards */
61dcc6f4 309 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
e01402b1 310
46230aa6
RB
311 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
312 if (failed)
313 goto out;
e01402b1
RB
314
315 /* and if there is anything left at the beginning of the buffer */
61dcc6f4 316 if (count - fl)
46230aa6
RB
317 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
318
319out:
320 count -= failed;
e01402b1 321
61dcc6f4
RB
322 smp_wmb();
323 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
324 smp_wmb();
bc4809e9 325 mutex_unlock(&channel_wqs[index].mutex);
e01402b1 326
afc4841d 327 return count;
e01402b1
RB
328}
329
7f5a7716 330ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
2600990e
RB
331{
332 struct rtlx_channel *rt;
7f5a7716 333 unsigned long failed;
61dcc6f4 334 size_t rt_read;
2600990e
RB
335 size_t fl;
336
337 if (rtlx == NULL)
338 return(-ENOSYS);
339
340 rt = &rtlx->channel[index];
341
bc4809e9 342 mutex_lock(&channel_wqs[index].mutex);
61dcc6f4
RB
343 smp_rmb();
344 rt_read = rt->rt_read;
345
2600990e
RB
346 /* total number of bytes to copy */
347 count = min(count,
61dcc6f4 348 (size_t)write_spacefree(rt_read, rt->rt_write, rt->buffer_size));
2600990e
RB
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
46230aa6
RB
353 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
354 if (failed)
355 goto out;
2600990e
RB
356
357 /* if there's any left copy to the beginning of the buffer */
46230aa6
RB
358 if (count - fl) {
359 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
360 }
361
362out:
7f5a7716 363 count -= failed;
2600990e 364
61dcc6f4
RB
365 smp_wmb();
366 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
367 smp_wmb();
bc4809e9 368 mutex_unlock(&channel_wqs[index].mutex);
2600990e 369
61dcc6f4 370 return count;
2600990e
RB
371}
372
373
374static int file_open(struct inode *inode, struct file *filp)
375{
79e55bcf 376 int minor = iminor(inode);
2600990e
RB
377
378 return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
379}
380
381static int file_release(struct inode *inode, struct file *filp)
382{
79e55bcf 383 int minor = iminor(inode);
2600990e
RB
384
385 return rtlx_release(minor);
386}
387
388static unsigned int file_poll(struct file *file, poll_table * wait)
389{
390 int minor;
391 unsigned int mask = 0;
392
1b04fe9a 393 minor = iminor(file->f_path.dentry->d_inode);
2600990e
RB
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
412static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
413 loff_t * ppos)
414{
1b04fe9a 415 int minor = iminor(file->f_path.dentry->d_inode);
2600990e
RB
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
46230aa6 422 return rtlx_read(minor, buffer, count);
2600990e
RB
423}
424
425static ssize_t file_write(struct file *file, const char __user * buffer,
e01402b1
RB
426 size_t count, loff_t * ppos)
427{
428 int minor;
429 struct rtlx_channel *rt;
e01402b1 430
1b04fe9a 431 minor = iminor(file->f_path.dentry->d_inode);
e01402b1
RB
432 rt = &rtlx->channel[minor];
433
434 /* any space left... */
2600990e 435 if (!rtlx_write_poll(minor)) {
67e2ccce 436 int ret = 0;
e01402b1
RB
437
438 if (file->f_flags & O_NONBLOCK)
afc4841d 439 return -EAGAIN;
e01402b1 440
67e2ccce
RB
441 __wait_event_interruptible(channel_wqs[minor].rt_queue,
442 rtlx_write_poll(minor),
443 ret);
444 if (ret)
445 return ret;
e01402b1
RB
446 }
447
46230aa6 448 return rtlx_write(minor, buffer, count);
e01402b1
RB
449}
450
5dfe4c96 451static const struct file_operations rtlx_fops = {
2600990e
RB
452 .owner = THIS_MODULE,
453 .open = file_open,
454 .release = file_release,
455 .write = file_write,
456 .read = file_read,
457 .poll = file_poll
e01402b1
RB
458};
459
2600990e
RB
460static struct irqaction rtlx_irq = {
461 .handler = rtlx_interrupt,
f40298fd 462 .flags = IRQF_DISABLED,
2600990e
RB
463 .name = "RTLX",
464};
465
97dcb82d 466static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
2600990e 467
afc4841d
RB
468static char register_chrdev_failed[] __initdata =
469 KERN_ERR "rtlx_module_init: unable to register device\n";
470
9d5a3f5f 471static int __init rtlx_module_init(void)
e01402b1 472{
bb3d7c7f
RB
473 struct device *dev;
474 int i, err;
2600990e 475
07cc0c9e
RB
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
afc4841d
RB
489 major = register_chrdev(0, module_name, &rtlx_fops);
490 if (major < 0) {
491 printk(register_chrdev_failed);
492 return major;
e01402b1
RB
493 }
494
2600990e
RB
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);
c4c4018b 499 atomic_set(&channel_wqs[i].in_open, 0);
bc4809e9 500 mutex_init(&channel_wqs[i].mutex);
bb3d7c7f
RB
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 }
2600990e
RB
508 }
509
510 /* set up notifiers */
511 notify.start = starting;
512 notify.stop = stopping;
07cc0c9e 513 vpe_notify(tclimit, &notify);
2600990e
RB
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
afc4841d 521 return 0;
bb3d7c7f
RB
522
523out_chrdev:
524 for (i = 0; i < RTLX_CHANNELS; i++)
525 device_destroy(mt_class, MKDEV(major, i));
526
527 return err;
e01402b1
RB
528}
529
afc4841d 530static void __exit rtlx_module_exit(void)
e01402b1 531{
bb3d7c7f
RB
532 int i;
533
534 for (i = 0; i < RTLX_CHANNELS; i++)
535 device_destroy(mt_class, MKDEV(major, i));
536
e01402b1
RB
537 unregister_chrdev(major, module_name);
538}
539
540module_init(rtlx_module_init);
541module_exit(rtlx_module_exit);
afc4841d 542
e01402b1 543MODULE_DESCRIPTION("MIPS RTLX");
2600990e 544MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
e01402b1 545MODULE_LICENSE("GPL");
This page took 0.303095 seconds and 5 git commands to generate.