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