vfs: do bulk POLL* -> EPOLL* replacement
[linux-2.6-block.git] / arch / mips / kernel / rtlx.c
CommitLineData
e01402b1 1/*
5792bf64
SH
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
e01402b1 6 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
a84c96e2 7 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
5792bf64 8 * Copyright (C) 2013 Imagination Technologies Ltd.
e01402b1 9 */
e01402b1 10#include <linux/kernel.h>
e01402b1 11#include <linux/fs.h>
2600990e
RB
12#include <linux/syscalls.h>
13#include <linux/moduleloader.h>
5792bf64 14#include <linux/atomic.h>
174cd4b1
IM
15#include <linux/sched/signal.h>
16
e01402b1 17#include <asm/mipsmtregs.h>
bb3d7c7f 18#include <asm/mips_mt.h>
e01402b1 19#include <asm/processor.h>
e01402b1 20#include <asm/rtlx.h>
406b5ee2 21#include <asm/setup.h>
5792bf64 22#include <asm/vpe.h>
e01402b1 23
982f6ffe 24static int sp_stopping;
2c973ef0
DCZ
25struct rtlx_info *rtlx;
26struct chan_waitqueues channel_wqs[RTLX_CHANNELS];
27struct vpe_notifications rtlx_notify;
28void (*aprp_hook)(void) = NULL;
29EXPORT_SYMBOL(aprp_hook);
e01402b1 30
f5dbeaf5 31static void __used dump_rtlx(void)
e01402b1
RB
32{
33 int i;
34
5792bf64 35 pr_info("id 0x%lx state %d\n", rtlx->id, rtlx->state);
e01402b1 36
e01402b1 37 for (i = 0; i < RTLX_CHANNELS; i++) {
2600990e 38 struct rtlx_channel *chan = &rtlx->channel[i];
e01402b1 39
5792bf64
SH
40 pr_info(" rt_state %d lx_state %d buffer_size %d\n",
41 chan->rt_state, chan->lx_state, chan->buffer_size);
e01402b1 42
5792bf64
SH
43 pr_info(" rt_read %d rt_write %d\n",
44 chan->rt_read, chan->rt_write);
e01402b1 45
5792bf64
SH
46 pr_info(" lx_read %d lx_write %d\n",
47 chan->lx_read, chan->lx_write);
2600990e 48
5792bf64
SH
49 pr_info(" rt_buffer <%s>\n", chan->rt_buffer);
50 pr_info(" lx_buffer <%s>\n", chan->lx_buffer);
2600990e
RB
51 }
52}
53
54/* call when we have the address of the shared structure from the SP side. */
55static int rtlx_init(struct rtlx_info *rtlxi)
56{
57 if (rtlxi->id != RTLX_ID) {
5792bf64 58 pr_err("no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
2600990e
RB
59 return -ENOEXEC;
60 }
e01402b1
RB
61
62 rtlx = rtlxi;
afc4841d
RB
63
64 return 0;
e01402b1
RB
65}
66
2600990e 67/* notifications */
2c973ef0 68void rtlx_starting(int vpe)
e01402b1 69{
2600990e
RB
70 int i;
71 sp_stopping = 0;
72
73 /* force a reload of rtlx */
5792bf64 74 rtlx = NULL;
2600990e
RB
75
76 /* wake up any sleeping rtlx_open's */
77 for (i = 0; i < RTLX_CHANNELS; i++)
78 wake_up_interruptible(&channel_wqs[i].lx_queue);
79}
80
2c973ef0 81void rtlx_stopping(int vpe)
2600990e
RB
82{
83 int i;
84
85 sp_stopping = 1;
86 for (i = 0; i < RTLX_CHANNELS; i++)
87 wake_up_interruptible(&channel_wqs[i].lx_queue);
88}
89
90
91int rtlx_open(int index, int can_sleep)
92{
9e346820 93 struct rtlx_info **p;
67e2ccce 94 struct rtlx_channel *chan;
c4c4018b 95 enum rtlx_state state;
67e2ccce 96 int ret = 0;
e01402b1 97
2600990e 98 if (index >= RTLX_CHANNELS) {
3dc4bf31 99 pr_debug("rtlx_open index out of range\n");
2600990e
RB
100 return -ENOSYS;
101 }
102
c4c4018b 103 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
3dc4bf31 104 pr_debug("rtlx_open channel %d already opened\n", index);
c4c4018b
RB
105 ret = -EBUSY;
106 goto out_fail;
2600990e
RB
107 }
108
e01402b1 109 if (rtlx == NULL) {
5792bf64
SH
110 p = vpe_get_shared(aprp_cpu_index());
111 if (p == NULL) {
112 if (can_sleep) {
113 ret = __wait_event_interruptible(
35a2af94 114 channel_wqs[index].lx_queue,
5792bf64
SH
115 (p = vpe_get_shared(aprp_cpu_index())));
116 if (ret)
117 goto out_fail;
118 } else {
119 pr_debug("No SP program loaded, and device opened with O_NONBLOCK\n");
120 ret = -ENOSYS;
67e2ccce 121 goto out_fail;
5792bf64 122 }
e01402b1
RB
123 }
124
9e346820 125 smp_rmb();
e01402b1 126 if (*p == NULL) {
2600990e 127 if (can_sleep) {
9e346820
RB
128 DEFINE_WAIT(wait);
129
130 for (;;) {
1928cc84
KK
131 prepare_to_wait(
132 &channel_wqs[index].lx_queue,
133 &wait, TASK_INTERRUPTIBLE);
9e346820
RB
134 smp_rmb();
135 if (*p != NULL)
136 break;
137 if (!signal_pending(current)) {
138 schedule();
139 continue;
140 }
141 ret = -ERESTARTSYS;
67e2ccce 142 goto out_fail;
9e346820 143 }
5792bf64
SH
144 finish_wait(&channel_wqs[index].lx_queue,
145 &wait);
67e2ccce 146 } else {
5792bf64 147 pr_err(" *vpe_get_shared is NULL. Has an SP program been loaded?\n");
67e2ccce
RB
148 ret = -ENOSYS;
149 goto out_fail;
2600990e
RB
150 }
151 }
152
153 if ((unsigned int)*p < KSEG0) {
5792bf64
SH
154 pr_warn("vpe_get_shared returned an invalid pointer maybe an error code %d\n",
155 (int)*p);
67e2ccce
RB
156 ret = -ENOSYS;
157 goto out_fail;
e01402b1
RB
158 }
159
5792bf64
SH
160 ret = rtlx_init(*p);
161 if (ret < 0)
c4c4018b 162 goto out_ret;
e01402b1
RB
163 }
164
2600990e 165 chan = &rtlx->channel[index];
e01402b1 166
c4c4018b
RB
167 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
168 if (state == RTLX_STATE_OPENED) {
169 ret = -EBUSY;
170 goto out_fail;
171 }
67e2ccce
RB
172
173out_fail:
c4c4018b
RB
174 smp_mb();
175 atomic_dec(&channel_wqs[index].in_open);
176 smp_mb();
67e2ccce 177
c4c4018b 178out_ret:
67e2ccce 179 return ret;
e01402b1
RB
180}
181
2600990e 182int rtlx_release(int index)
e01402b1 183{
1928cc84
KK
184 if (rtlx == NULL) {
185 pr_err("rtlx_release() with null rtlx\n");
186 return 0;
187 }
2600990e 188 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
afc4841d 189 return 0;
e01402b1
RB
190}
191
2600990e 192unsigned int rtlx_read_poll(int index, int can_sleep)
e01402b1 193{
70342287 194 struct rtlx_channel *chan;
e01402b1 195
70342287
RB
196 if (rtlx == NULL)
197 return 0;
e01402b1 198
70342287 199 chan = &rtlx->channel[index];
e01402b1
RB
200
201 /* data available to read? */
2600990e
RB
202 if (chan->lx_read == chan->lx_write) {
203 if (can_sleep) {
35a2af94
PZ
204 int ret = __wait_event_interruptible(
205 channel_wqs[index].lx_queue,
1928cc84 206 (chan->lx_read != chan->lx_write) ||
35a2af94 207 sp_stopping);
67e2ccce
RB
208 if (ret)
209 return ret;
e01402b1 210
67e2ccce
RB
211 if (sp_stopping)
212 return 0;
213 } else
2600990e
RB
214 return 0;
215 }
216
217 return (chan->lx_write + chan->buffer_size - chan->lx_read)
218 % chan->buffer_size;
e01402b1
RB
219}
220
2600990e 221static inline int write_spacefree(int read, int write, int size)
e01402b1 222{
2600990e
RB
223 if (read == write) {
224 /*
225 * Never fill the buffer completely, so indexes are always
226 * equal if empty and only empty, or !equal if data available
227 */
228 return size - 1;
229 }
e01402b1 230
2600990e
RB
231 return ((read + size - write) % size) - 1;
232}
e01402b1 233
2600990e
RB
234unsigned int rtlx_write_poll(int index)
235{
236 struct rtlx_channel *chan = &rtlx->channel[index];
1928cc84
KK
237
238 return write_spacefree(chan->rt_read, chan->rt_write,
239 chan->buffer_size);
2600990e 240}
e01402b1 241
7f5a7716 242ssize_t rtlx_read(int index, void __user *buff, size_t count)
2600990e 243{
61dcc6f4 244 size_t lx_write, fl = 0L;
2600990e 245 struct rtlx_channel *lx;
46230aa6 246 unsigned long failed;
e01402b1 247
2600990e
RB
248 if (rtlx == NULL)
249 return -ENOSYS;
250
251 lx = &rtlx->channel[index];
e01402b1 252
bc4809e9 253 mutex_lock(&channel_wqs[index].mutex);
61dcc6f4
RB
254 smp_rmb();
255 lx_write = lx->lx_write;
256
e01402b1 257 /* find out how much in total */
afc4841d 258 count = min(count,
61dcc6f4 259 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
2600990e 260 % lx->buffer_size);
e01402b1
RB
261
262 /* then how much from the read pointer onwards */
61dcc6f4 263 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
e01402b1 264
46230aa6
RB
265 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
266 if (failed)
267 goto out;
e01402b1
RB
268
269 /* and if there is anything left at the beginning of the buffer */
61dcc6f4 270 if (count - fl)
46230aa6
RB
271 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
272
273out:
274 count -= failed;
e01402b1 275
61dcc6f4
RB
276 smp_wmb();
277 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
278 smp_wmb();
bc4809e9 279 mutex_unlock(&channel_wqs[index].mutex);
e01402b1 280
afc4841d 281 return count;
e01402b1
RB
282}
283
7f5a7716 284ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
2600990e
RB
285{
286 struct rtlx_channel *rt;
7f5a7716 287 unsigned long failed;
61dcc6f4 288 size_t rt_read;
2600990e
RB
289 size_t fl;
290
291 if (rtlx == NULL)
5792bf64 292 return -ENOSYS;
2600990e
RB
293
294 rt = &rtlx->channel[index];
295
bc4809e9 296 mutex_lock(&channel_wqs[index].mutex);
61dcc6f4
RB
297 smp_rmb();
298 rt_read = rt->rt_read;
299
2600990e 300 /* total number of bytes to copy */
5792bf64
SH
301 count = min_t(size_t, count, write_spacefree(rt_read, rt->rt_write,
302 rt->buffer_size));
2600990e
RB
303
304 /* first bit from write pointer to the end of the buffer, or count */
305 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
306
46230aa6
RB
307 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
308 if (failed)
309 goto out;
2600990e
RB
310
311 /* if there's any left copy to the beginning of the buffer */
5792bf64 312 if (count - fl)
46230aa6 313 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
46230aa6
RB
314
315out:
7f5a7716 316 count -= failed;
2600990e 317
61dcc6f4
RB
318 smp_wmb();
319 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
320 smp_wmb();
bc4809e9 321 mutex_unlock(&channel_wqs[index].mutex);
2600990e 322
2c973ef0
DCZ
323 _interrupt_sp();
324
61dcc6f4 325 return count;
2600990e
RB
326}
327
328
329static int file_open(struct inode *inode, struct file *filp)
330{
1bbfc20d 331 return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
2600990e
RB
332}
333
334static int file_release(struct inode *inode, struct file *filp)
335{
1bbfc20d 336 return rtlx_release(iminor(inode));
2600990e
RB
337}
338
8b9aab09 339static __poll_t file_poll(struct file *file, poll_table *wait)
2600990e 340{
496ad9aa 341 int minor = iminor(file_inode(file));
8b9aab09 342 __poll_t mask = 0;
2600990e 343
2600990e
RB
344 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
345 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
346
347 if (rtlx == NULL)
348 return 0;
349
350 /* data available to read? */
351 if (rtlx_read_poll(minor, 0))
a9a08845 352 mask |= EPOLLIN | EPOLLRDNORM;
2600990e
RB
353
354 /* space to write */
355 if (rtlx_write_poll(minor))
a9a08845 356 mask |= EPOLLOUT | EPOLLWRNORM;
2600990e
RB
357
358 return mask;
359}
360
5792bf64
SH
361static ssize_t file_read(struct file *file, char __user *buffer, size_t count,
362 loff_t *ppos)
2600990e 363{
496ad9aa 364 int minor = iminor(file_inode(file));
2600990e
RB
365
366 /* data available? */
5792bf64
SH
367 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1))
368 return 0; /* -EAGAIN makes 'cat' whine */
2600990e 369
46230aa6 370 return rtlx_read(minor, buffer, count);
2600990e
RB
371}
372
5792bf64
SH
373static ssize_t file_write(struct file *file, const char __user *buffer,
374 size_t count, loff_t *ppos)
e01402b1 375{
496ad9aa 376 int minor = iminor(file_inode(file));
e01402b1
RB
377
378 /* any space left... */
2600990e 379 if (!rtlx_write_poll(minor)) {
35a2af94 380 int ret;
e01402b1
RB
381
382 if (file->f_flags & O_NONBLOCK)
afc4841d 383 return -EAGAIN;
e01402b1 384
35a2af94
PZ
385 ret = __wait_event_interruptible(channel_wqs[minor].rt_queue,
386 rtlx_write_poll(minor));
67e2ccce
RB
387 if (ret)
388 return ret;
e01402b1
RB
389 }
390
46230aa6 391 return rtlx_write(minor, buffer, count);
e01402b1
RB
392}
393
2c973ef0 394const struct file_operations rtlx_fops = {
2600990e 395 .owner = THIS_MODULE,
5792bf64 396 .open = file_open,
2600990e
RB
397 .release = file_release,
398 .write = file_write,
5792bf64
SH
399 .read = file_read,
400 .poll = file_poll,
6038f373 401 .llseek = noop_llseek,
e01402b1
RB
402};
403
e01402b1
RB
404module_init(rtlx_module_init);
405module_exit(rtlx_module_exit);
afc4841d 406
e01402b1 407MODULE_DESCRIPTION("MIPS RTLX");
2600990e 408MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
e01402b1 409MODULE_LICENSE("GPL");