0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/fs.h>
0012 #include <linux/syscalls.h>
0013 #include <linux/moduleloader.h>
0014 #include <linux/atomic.h>
0015 #include <linux/sched/signal.h>
0016
0017 #include <asm/mipsmtregs.h>
0018 #include <asm/mips_mt.h>
0019 #include <asm/processor.h>
0020 #include <asm/rtlx.h>
0021 #include <asm/setup.h>
0022 #include <asm/vpe.h>
0023
0024 static int sp_stopping;
0025 struct rtlx_info *rtlx;
0026 struct chan_waitqueues channel_wqs[RTLX_CHANNELS];
0027 struct vpe_notifications rtlx_notify;
0028 void (*aprp_hook)(void) = NULL;
0029 EXPORT_SYMBOL(aprp_hook);
0030
0031 static void __used dump_rtlx(void)
0032 {
0033 int i;
0034
0035 pr_info("id 0x%lx state %d\n", rtlx->id, rtlx->state);
0036
0037 for (i = 0; i < RTLX_CHANNELS; i++) {
0038 struct rtlx_channel *chan = &rtlx->channel[i];
0039
0040 pr_info(" rt_state %d lx_state %d buffer_size %d\n",
0041 chan->rt_state, chan->lx_state, chan->buffer_size);
0042
0043 pr_info(" rt_read %d rt_write %d\n",
0044 chan->rt_read, chan->rt_write);
0045
0046 pr_info(" lx_read %d lx_write %d\n",
0047 chan->lx_read, chan->lx_write);
0048
0049 pr_info(" rt_buffer <%s>\n", chan->rt_buffer);
0050 pr_info(" lx_buffer <%s>\n", chan->lx_buffer);
0051 }
0052 }
0053
0054
0055 static int rtlx_init(struct rtlx_info *rtlxi)
0056 {
0057 if (rtlxi->id != RTLX_ID) {
0058 pr_err("no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
0059 return -ENOEXEC;
0060 }
0061
0062 rtlx = rtlxi;
0063
0064 return 0;
0065 }
0066
0067
0068 void rtlx_starting(int vpe)
0069 {
0070 int i;
0071 sp_stopping = 0;
0072
0073
0074 rtlx = NULL;
0075
0076
0077 for (i = 0; i < RTLX_CHANNELS; i++)
0078 wake_up_interruptible(&channel_wqs[i].lx_queue);
0079 }
0080
0081 void rtlx_stopping(int vpe)
0082 {
0083 int i;
0084
0085 sp_stopping = 1;
0086 for (i = 0; i < RTLX_CHANNELS; i++)
0087 wake_up_interruptible(&channel_wqs[i].lx_queue);
0088 }
0089
0090
0091 int rtlx_open(int index, int can_sleep)
0092 {
0093 struct rtlx_info **p;
0094 struct rtlx_channel *chan;
0095 enum rtlx_state state;
0096 int ret = 0;
0097
0098 if (index >= RTLX_CHANNELS) {
0099 pr_debug("rtlx_open index out of range\n");
0100 return -ENOSYS;
0101 }
0102
0103 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
0104 pr_debug("rtlx_open channel %d already opened\n", index);
0105 ret = -EBUSY;
0106 goto out_fail;
0107 }
0108
0109 if (rtlx == NULL) {
0110 p = vpe_get_shared(aprp_cpu_index());
0111 if (p == NULL) {
0112 if (can_sleep) {
0113 ret = __wait_event_interruptible(
0114 channel_wqs[index].lx_queue,
0115 (p = vpe_get_shared(aprp_cpu_index())));
0116 if (ret)
0117 goto out_fail;
0118 } else {
0119 pr_debug("No SP program loaded, and device opened with O_NONBLOCK\n");
0120 ret = -ENOSYS;
0121 goto out_fail;
0122 }
0123 }
0124
0125 smp_rmb();
0126 if (*p == NULL) {
0127 if (can_sleep) {
0128 DEFINE_WAIT(wait);
0129
0130 for (;;) {
0131 prepare_to_wait(
0132 &channel_wqs[index].lx_queue,
0133 &wait, TASK_INTERRUPTIBLE);
0134 smp_rmb();
0135 if (*p != NULL)
0136 break;
0137 if (!signal_pending(current)) {
0138 schedule();
0139 continue;
0140 }
0141 ret = -ERESTARTSYS;
0142 goto out_fail;
0143 }
0144 finish_wait(&channel_wqs[index].lx_queue,
0145 &wait);
0146 } else {
0147 pr_err(" *vpe_get_shared is NULL. Has an SP program been loaded?\n");
0148 ret = -ENOSYS;
0149 goto out_fail;
0150 }
0151 }
0152
0153 if ((unsigned int)*p < KSEG0) {
0154 pr_warn("vpe_get_shared returned an invalid pointer maybe an error code %d\n",
0155 (int)*p);
0156 ret = -ENOSYS;
0157 goto out_fail;
0158 }
0159
0160 ret = rtlx_init(*p);
0161 if (ret < 0)
0162 goto out_ret;
0163 }
0164
0165 chan = &rtlx->channel[index];
0166
0167 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
0168 if (state == RTLX_STATE_OPENED) {
0169 ret = -EBUSY;
0170 goto out_fail;
0171 }
0172
0173 out_fail:
0174 smp_mb();
0175 atomic_dec(&channel_wqs[index].in_open);
0176 smp_mb();
0177
0178 out_ret:
0179 return ret;
0180 }
0181
0182 int rtlx_release(int index)
0183 {
0184 if (rtlx == NULL) {
0185 pr_err("rtlx_release() with null rtlx\n");
0186 return 0;
0187 }
0188 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
0189 return 0;
0190 }
0191
0192 unsigned int rtlx_read_poll(int index, int can_sleep)
0193 {
0194 struct rtlx_channel *chan;
0195
0196 if (rtlx == NULL)
0197 return 0;
0198
0199 chan = &rtlx->channel[index];
0200
0201
0202 if (chan->lx_read == chan->lx_write) {
0203 if (can_sleep) {
0204 int ret = __wait_event_interruptible(
0205 channel_wqs[index].lx_queue,
0206 (chan->lx_read != chan->lx_write) ||
0207 sp_stopping);
0208 if (ret)
0209 return ret;
0210
0211 if (sp_stopping)
0212 return 0;
0213 } else
0214 return 0;
0215 }
0216
0217 return (chan->lx_write + chan->buffer_size - chan->lx_read)
0218 % chan->buffer_size;
0219 }
0220
0221 static inline int write_spacefree(int read, int write, int size)
0222 {
0223 if (read == write) {
0224
0225
0226
0227
0228 return size - 1;
0229 }
0230
0231 return ((read + size - write) % size) - 1;
0232 }
0233
0234 unsigned int rtlx_write_poll(int index)
0235 {
0236 struct rtlx_channel *chan = &rtlx->channel[index];
0237
0238 return write_spacefree(chan->rt_read, chan->rt_write,
0239 chan->buffer_size);
0240 }
0241
0242 ssize_t rtlx_read(int index, void __user *buff, size_t count)
0243 {
0244 size_t lx_write, fl = 0L;
0245 struct rtlx_channel *lx;
0246 unsigned long failed;
0247
0248 if (rtlx == NULL)
0249 return -ENOSYS;
0250
0251 lx = &rtlx->channel[index];
0252
0253 mutex_lock(&channel_wqs[index].mutex);
0254 smp_rmb();
0255 lx_write = lx->lx_write;
0256
0257
0258 count = min(count,
0259 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
0260 % lx->buffer_size);
0261
0262
0263 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
0264
0265 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
0266 if (failed)
0267 goto out;
0268
0269
0270 if (count - fl)
0271 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
0272
0273 out:
0274 count -= failed;
0275
0276 smp_wmb();
0277 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
0278 smp_wmb();
0279 mutex_unlock(&channel_wqs[index].mutex);
0280
0281 return count;
0282 }
0283
0284 ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
0285 {
0286 struct rtlx_channel *rt;
0287 unsigned long failed;
0288 size_t rt_read;
0289 size_t fl;
0290
0291 if (rtlx == NULL)
0292 return -ENOSYS;
0293
0294 rt = &rtlx->channel[index];
0295
0296 mutex_lock(&channel_wqs[index].mutex);
0297 smp_rmb();
0298 rt_read = rt->rt_read;
0299
0300
0301 count = min_t(size_t, count, write_spacefree(rt_read, rt->rt_write,
0302 rt->buffer_size));
0303
0304
0305 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
0306
0307 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
0308 if (failed)
0309 goto out;
0310
0311
0312 if (count - fl)
0313 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
0314
0315 out:
0316 count -= failed;
0317
0318 smp_wmb();
0319 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
0320 smp_wmb();
0321 mutex_unlock(&channel_wqs[index].mutex);
0322
0323 _interrupt_sp();
0324
0325 return count;
0326 }
0327
0328
0329 static int file_open(struct inode *inode, struct file *filp)
0330 {
0331 return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
0332 }
0333
0334 static int file_release(struct inode *inode, struct file *filp)
0335 {
0336 return rtlx_release(iminor(inode));
0337 }
0338
0339 static __poll_t file_poll(struct file *file, poll_table *wait)
0340 {
0341 int minor = iminor(file_inode(file));
0342 __poll_t mask = 0;
0343
0344 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
0345 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
0346
0347 if (rtlx == NULL)
0348 return 0;
0349
0350
0351 if (rtlx_read_poll(minor, 0))
0352 mask |= EPOLLIN | EPOLLRDNORM;
0353
0354
0355 if (rtlx_write_poll(minor))
0356 mask |= EPOLLOUT | EPOLLWRNORM;
0357
0358 return mask;
0359 }
0360
0361 static ssize_t file_read(struct file *file, char __user *buffer, size_t count,
0362 loff_t *ppos)
0363 {
0364 int minor = iminor(file_inode(file));
0365
0366
0367 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1))
0368 return 0;
0369
0370 return rtlx_read(minor, buffer, count);
0371 }
0372
0373 static ssize_t file_write(struct file *file, const char __user *buffer,
0374 size_t count, loff_t *ppos)
0375 {
0376 int minor = iminor(file_inode(file));
0377
0378
0379 if (!rtlx_write_poll(minor)) {
0380 int ret;
0381
0382 if (file->f_flags & O_NONBLOCK)
0383 return -EAGAIN;
0384
0385 ret = __wait_event_interruptible(channel_wqs[minor].rt_queue,
0386 rtlx_write_poll(minor));
0387 if (ret)
0388 return ret;
0389 }
0390
0391 return rtlx_write(minor, buffer, count);
0392 }
0393
0394 const struct file_operations rtlx_fops = {
0395 .owner = THIS_MODULE,
0396 .open = file_open,
0397 .release = file_release,
0398 .write = file_write,
0399 .read = file_read,
0400 .poll = file_poll,
0401 .llseek = noop_llseek,
0402 };
0403
0404 module_init(rtlx_module_init);
0405 module_exit(rtlx_module_exit);
0406
0407 MODULE_DESCRIPTION("MIPS RTLX");
0408 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
0409 MODULE_LICENSE("GPL");