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