rtlx.c 12 KB

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