rtlx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
  3. * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
  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. #include <linux/device.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/slab.h>
  26. #include <linux/list.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/elf.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/moduleloader.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/poll.h>
  34. #include <linux/sched.h>
  35. #include <linux/wait.h>
  36. #include <asm/mipsmtregs.h>
  37. #include <asm/mips_mt.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/atomic.h>
  40. #include <asm/cpu.h>
  41. #include <asm/processor.h>
  42. #include <asm/system.h>
  43. #include <asm/vpe.h>
  44. #include <asm/rtlx.h>
  45. #define RTLX_TARG_VPE 1
  46. static struct rtlx_info *rtlx;
  47. static int major;
  48. static char module_name[] = "rtlx";
  49. static struct chan_waitqueues {
  50. wait_queue_head_t rt_queue;
  51. wait_queue_head_t lx_queue;
  52. atomic_t in_open;
  53. struct mutex mutex;
  54. } channel_wqs[RTLX_CHANNELS];
  55. static struct irqaction irq;
  56. static int irq_num;
  57. static struct vpe_notifications notify;
  58. static int sp_stopping = 0;
  59. extern void *vpe_get_shared(int index);
  60. static void rtlx_dispatch(void)
  61. {
  62. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
  63. }
  64. /* Interrupt handler may be called before rtlx_init has otherwise had
  65. a chance to run.
  66. */
  67. static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
  68. {
  69. int i;
  70. for (i = 0; i < RTLX_CHANNELS; i++) {
  71. wake_up(&channel_wqs[i].lx_queue);
  72. wake_up(&channel_wqs[i].rt_queue);
  73. }
  74. return IRQ_HANDLED;
  75. }
  76. static __attribute_used__ void dump_rtlx(void)
  77. {
  78. int i;
  79. printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
  80. for (i = 0; i < RTLX_CHANNELS; i++) {
  81. struct rtlx_channel *chan = &rtlx->channel[i];
  82. printk(" rt_state %d lx_state %d buffer_size %d\n",
  83. chan->rt_state, chan->lx_state, chan->buffer_size);
  84. printk(" rt_read %d rt_write %d\n",
  85. chan->rt_read, chan->rt_write);
  86. printk(" lx_read %d lx_write %d\n",
  87. chan->lx_read, chan->lx_write);
  88. printk(" rt_buffer <%s>\n", chan->rt_buffer);
  89. printk(" lx_buffer <%s>\n", chan->lx_buffer);
  90. }
  91. }
  92. /* call when we have the address of the shared structure from the SP side. */
  93. static int rtlx_init(struct rtlx_info *rtlxi)
  94. {
  95. if (rtlxi->id != RTLX_ID) {
  96. printk(KERN_ERR "no valid RTLX id at 0x%p 0x%x\n", rtlxi, rtlxi->id);
  97. return -ENOEXEC;
  98. }
  99. rtlx = rtlxi;
  100. return 0;
  101. }
  102. /* notifications */
  103. static void starting(int vpe)
  104. {
  105. int i;
  106. sp_stopping = 0;
  107. /* force a reload of rtlx */
  108. rtlx=NULL;
  109. /* wake up any sleeping rtlx_open's */
  110. for (i = 0; i < RTLX_CHANNELS; i++)
  111. wake_up_interruptible(&channel_wqs[i].lx_queue);
  112. }
  113. static void stopping(int vpe)
  114. {
  115. int i;
  116. sp_stopping = 1;
  117. for (i = 0; i < RTLX_CHANNELS; i++)
  118. wake_up_interruptible(&channel_wqs[i].lx_queue);
  119. }
  120. int rtlx_open(int index, int can_sleep)
  121. {
  122. struct rtlx_info **p;
  123. struct rtlx_channel *chan;
  124. enum rtlx_state state;
  125. int ret = 0;
  126. if (index >= RTLX_CHANNELS) {
  127. printk(KERN_DEBUG "rtlx_open index out of range\n");
  128. return -ENOSYS;
  129. }
  130. if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
  131. printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
  132. index);
  133. ret = -EBUSY;
  134. goto out_fail;
  135. }
  136. if (rtlx == NULL) {
  137. if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) {
  138. if (can_sleep) {
  139. __wait_event_interruptible(channel_wqs[index].lx_queue,
  140. (p = vpe_get_shared(RTLX_TARG_VPE)),
  141. ret);
  142. if (ret)
  143. goto out_fail;
  144. } else {
  145. printk(KERN_DEBUG "No SP program loaded, and device "
  146. "opened with O_NONBLOCK\n");
  147. ret = -ENOSYS;
  148. goto out_fail;
  149. }
  150. }
  151. smp_rmb();
  152. if (*p == NULL) {
  153. if (can_sleep) {
  154. DEFINE_WAIT(wait);
  155. for (;;) {
  156. prepare_to_wait(&channel_wqs[index].lx_queue, &wait, TASK_INTERRUPTIBLE);
  157. smp_rmb();
  158. if (*p != NULL)
  159. break;
  160. if (!signal_pending(current)) {
  161. schedule();
  162. continue;
  163. }
  164. ret = -ERESTARTSYS;
  165. goto out_fail;
  166. }
  167. finish_wait(&channel_wqs[index].lx_queue, &wait);
  168. } else {
  169. printk(" *vpe_get_shared is NULL. "
  170. "Has an SP program been loaded?\n");
  171. ret = -ENOSYS;
  172. goto out_fail;
  173. }
  174. }
  175. if ((unsigned int)*p < KSEG0) {
  176. printk(KERN_WARNING "vpe_get_shared returned an invalid pointer "
  177. "maybe an error code %d\n", (int)*p);
  178. ret = -ENOSYS;
  179. goto out_fail;
  180. }
  181. if ((ret = rtlx_init(*p)) < 0)
  182. goto out_ret;
  183. }
  184. chan = &rtlx->channel[index];
  185. state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
  186. if (state == RTLX_STATE_OPENED) {
  187. ret = -EBUSY;
  188. goto out_fail;
  189. }
  190. out_fail:
  191. smp_mb();
  192. atomic_dec(&channel_wqs[index].in_open);
  193. smp_mb();
  194. out_ret:
  195. return ret;
  196. }
  197. int rtlx_release(int index)
  198. {
  199. rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
  200. return 0;
  201. }
  202. unsigned int rtlx_read_poll(int index, int can_sleep)
  203. {
  204. struct rtlx_channel *chan;
  205. if (rtlx == NULL)
  206. return 0;
  207. chan = &rtlx->channel[index];
  208. /* data available to read? */
  209. if (chan->lx_read == chan->lx_write) {
  210. if (can_sleep) {
  211. int ret = 0;
  212. __wait_event_interruptible(channel_wqs[index].lx_queue,
  213. chan->lx_read != chan->lx_write || sp_stopping,
  214. ret);
  215. if (ret)
  216. return ret;
  217. if (sp_stopping)
  218. return 0;
  219. } else
  220. return 0;
  221. }
  222. return (chan->lx_write + chan->buffer_size - chan->lx_read)
  223. % chan->buffer_size;
  224. }
  225. static inline int write_spacefree(int read, int write, int size)
  226. {
  227. if (read == write) {
  228. /*
  229. * Never fill the buffer completely, so indexes are always
  230. * equal if empty and only empty, or !equal if data available
  231. */
  232. return size - 1;
  233. }
  234. return ((read + size - write) % size) - 1;
  235. }
  236. unsigned int rtlx_write_poll(int index)
  237. {
  238. struct rtlx_channel *chan = &rtlx->channel[index];
  239. return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size);
  240. }
  241. ssize_t rtlx_read(int index, void __user *buff, size_t count)
  242. {
  243. size_t lx_write, fl = 0L;
  244. struct rtlx_channel *lx;
  245. unsigned long failed;
  246. if (rtlx == NULL)
  247. return -ENOSYS;
  248. lx = &rtlx->channel[index];
  249. mutex_lock(&channel_wqs[index].mutex);
  250. smp_rmb();
  251. lx_write = lx->lx_write;
  252. /* find out how much in total */
  253. count = min(count,
  254. (size_t)(lx_write + lx->buffer_size - lx->lx_read)
  255. % lx->buffer_size);
  256. /* then how much from the read pointer onwards */
  257. fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
  258. failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
  259. if (failed)
  260. goto out;
  261. /* and if there is anything left at the beginning of the buffer */
  262. if (count - fl)
  263. failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
  264. out:
  265. count -= failed;
  266. smp_wmb();
  267. lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
  268. smp_wmb();
  269. mutex_unlock(&channel_wqs[index].mutex);
  270. return count;
  271. }
  272. ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
  273. {
  274. struct rtlx_channel *rt;
  275. unsigned long failed;
  276. size_t rt_read;
  277. size_t fl;
  278. if (rtlx == NULL)
  279. return(-ENOSYS);
  280. rt = &rtlx->channel[index];
  281. mutex_lock(&channel_wqs[index].mutex);
  282. smp_rmb();
  283. rt_read = rt->rt_read;
  284. /* total number of bytes to copy */
  285. count = min(count,
  286. (size_t)write_spacefree(rt_read, rt->rt_write, rt->buffer_size));
  287. /* first bit from write pointer to the end of the buffer, or count */
  288. fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
  289. failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
  290. if (failed)
  291. goto out;
  292. /* if there's any left copy to the beginning of the buffer */
  293. if (count - fl) {
  294. failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
  295. }
  296. out:
  297. count -= failed;
  298. smp_wmb();
  299. rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
  300. smp_wmb();
  301. mutex_unlock(&channel_wqs[index].mutex);
  302. return count;
  303. }
  304. static int file_open(struct inode *inode, struct file *filp)
  305. {
  306. int minor = iminor(inode);
  307. return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
  308. }
  309. static int file_release(struct inode *inode, struct file *filp)
  310. {
  311. int minor = iminor(inode);
  312. return rtlx_release(minor);
  313. }
  314. static unsigned int file_poll(struct file *file, poll_table * wait)
  315. {
  316. int minor;
  317. unsigned int mask = 0;
  318. minor = iminor(file->f_path.dentry->d_inode);
  319. poll_wait(file, &channel_wqs[minor].rt_queue, wait);
  320. poll_wait(file, &channel_wqs[minor].lx_queue, wait);
  321. if (rtlx == NULL)
  322. return 0;
  323. /* data available to read? */
  324. if (rtlx_read_poll(minor, 0))
  325. mask |= POLLIN | POLLRDNORM;
  326. /* space to write */
  327. if (rtlx_write_poll(minor))
  328. mask |= POLLOUT | POLLWRNORM;
  329. return mask;
  330. }
  331. static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
  332. loff_t * ppos)
  333. {
  334. int minor = iminor(file->f_path.dentry->d_inode);
  335. /* data available? */
  336. if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
  337. return 0; // -EAGAIN makes cat whinge
  338. }
  339. return rtlx_read(minor, buffer, count);
  340. }
  341. static ssize_t file_write(struct file *file, const char __user * buffer,
  342. size_t count, loff_t * ppos)
  343. {
  344. int minor;
  345. struct rtlx_channel *rt;
  346. minor = iminor(file->f_path.dentry->d_inode);
  347. rt = &rtlx->channel[minor];
  348. /* any space left... */
  349. if (!rtlx_write_poll(minor)) {
  350. int ret = 0;
  351. if (file->f_flags & O_NONBLOCK)
  352. return -EAGAIN;
  353. __wait_event_interruptible(channel_wqs[minor].rt_queue,
  354. rtlx_write_poll(minor),
  355. ret);
  356. if (ret)
  357. return ret;
  358. }
  359. return rtlx_write(minor, buffer, count);
  360. }
  361. static const struct file_operations rtlx_fops = {
  362. .owner = THIS_MODULE,
  363. .open = file_open,
  364. .release = file_release,
  365. .write = file_write,
  366. .read = file_read,
  367. .poll = file_poll
  368. };
  369. static struct irqaction rtlx_irq = {
  370. .handler = rtlx_interrupt,
  371. .flags = IRQF_DISABLED,
  372. .name = "RTLX",
  373. };
  374. static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
  375. static char register_chrdev_failed[] __initdata =
  376. KERN_ERR "rtlx_module_init: unable to register device\n";
  377. static int rtlx_module_init(void)
  378. {
  379. struct device *dev;
  380. int i, err;
  381. major = register_chrdev(0, module_name, &rtlx_fops);
  382. if (major < 0) {
  383. printk(register_chrdev_failed);
  384. return major;
  385. }
  386. /* initialise the wait queues */
  387. for (i = 0; i < RTLX_CHANNELS; i++) {
  388. init_waitqueue_head(&channel_wqs[i].rt_queue);
  389. init_waitqueue_head(&channel_wqs[i].lx_queue);
  390. atomic_set(&channel_wqs[i].in_open, 0);
  391. mutex_init(&channel_wqs[i].mutex);
  392. dev = device_create(mt_class, NULL, MKDEV(major, i),
  393. "%s%d", module_name, i);
  394. if (IS_ERR(dev)) {
  395. err = PTR_ERR(dev);
  396. goto out_chrdev;
  397. }
  398. }
  399. /* set up notifiers */
  400. notify.start = starting;
  401. notify.stop = stopping;
  402. vpe_notify(RTLX_TARG_VPE, &notify);
  403. if (cpu_has_vint)
  404. set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
  405. rtlx_irq.dev_id = rtlx;
  406. setup_irq(rtlx_irq_num, &rtlx_irq);
  407. return 0;
  408. out_chrdev:
  409. for (i = 0; i < RTLX_CHANNELS; i++)
  410. device_destroy(mt_class, MKDEV(major, i));
  411. return err;
  412. }
  413. static void __exit rtlx_module_exit(void)
  414. {
  415. int i;
  416. for (i = 0; i < RTLX_CHANNELS; i++)
  417. device_destroy(mt_class, MKDEV(major, i));
  418. unregister_chrdev(major, module_name);
  419. }
  420. module_init(rtlx_module_init);
  421. module_exit(rtlx_module_exit);
  422. MODULE_DESCRIPTION("MIPS RTLX");
  423. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  424. MODULE_LICENSE("GPL");