rtlx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 fl = 0L;
  257. struct rtlx_channel *lx;
  258. if (rtlx == NULL)
  259. return -ENOSYS;
  260. lx = &rtlx->channel[index];
  261. /* find out how much in total */
  262. count = min(count,
  263. (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read)
  264. % lx->buffer_size);
  265. /* then how much from the read pointer onwards */
  266. fl = min( count, (size_t)lx->buffer_size - lx->lx_read);
  267. copy_to(buff, &lx->lx_buffer[lx->lx_read], fl, user);
  268. /* and if there is anything left at the beginning of the buffer */
  269. if ( count - fl )
  270. copy_to (buff + fl, lx->lx_buffer, count - fl, user);
  271. /* update the index */
  272. lx->lx_read += count;
  273. lx->lx_read %= lx->buffer_size;
  274. return count;
  275. }
  276. ssize_t rtlx_write(int index, void *buffer, size_t count, int user)
  277. {
  278. struct rtlx_channel *rt;
  279. size_t fl;
  280. if (rtlx == NULL)
  281. return(-ENOSYS);
  282. rt = &rtlx->channel[index];
  283. /* total number of bytes to copy */
  284. count = min(count,
  285. (size_t)write_spacefree(rt->rt_read, rt->rt_write,
  286. 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. copy_from (&rt->rt_buffer[rt->rt_write], buffer, fl, user);
  290. /* if there's any left copy to the beginning of the buffer */
  291. if( count - fl )
  292. copy_from (rt->rt_buffer, buffer + fl, count - fl, user);
  293. rt->rt_write += count;
  294. rt->rt_write %= rt->buffer_size;
  295. return(count);
  296. }
  297. static int file_open(struct inode *inode, struct file *filp)
  298. {
  299. int minor = iminor(inode);
  300. return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
  301. }
  302. static int file_release(struct inode *inode, struct file *filp)
  303. {
  304. int minor = iminor(inode);
  305. return rtlx_release(minor);
  306. }
  307. static unsigned int file_poll(struct file *file, poll_table * wait)
  308. {
  309. int minor;
  310. unsigned int mask = 0;
  311. minor = iminor(file->f_path.dentry->d_inode);
  312. poll_wait(file, &channel_wqs[minor].rt_queue, wait);
  313. poll_wait(file, &channel_wqs[minor].lx_queue, wait);
  314. if (rtlx == NULL)
  315. return 0;
  316. /* data available to read? */
  317. if (rtlx_read_poll(minor, 0))
  318. mask |= POLLIN | POLLRDNORM;
  319. /* space to write */
  320. if (rtlx_write_poll(minor))
  321. mask |= POLLOUT | POLLWRNORM;
  322. return mask;
  323. }
  324. static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
  325. loff_t * ppos)
  326. {
  327. int minor = iminor(file->f_path.dentry->d_inode);
  328. /* data available? */
  329. if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
  330. return 0; // -EAGAIN makes cat whinge
  331. }
  332. return rtlx_read(minor, buffer, count, 1);
  333. }
  334. static ssize_t file_write(struct file *file, const char __user * buffer,
  335. size_t count, loff_t * ppos)
  336. {
  337. int minor;
  338. struct rtlx_channel *rt;
  339. minor = iminor(file->f_path.dentry->d_inode);
  340. rt = &rtlx->channel[minor];
  341. /* any space left... */
  342. if (!rtlx_write_poll(minor)) {
  343. int ret = 0;
  344. if (file->f_flags & O_NONBLOCK)
  345. return -EAGAIN;
  346. __wait_event_interruptible(channel_wqs[minor].rt_queue,
  347. rtlx_write_poll(minor),
  348. ret);
  349. if (ret)
  350. return ret;
  351. }
  352. return rtlx_write(minor, (void *)buffer, count, 1);
  353. }
  354. static const struct file_operations rtlx_fops = {
  355. .owner = THIS_MODULE,
  356. .open = file_open,
  357. .release = file_release,
  358. .write = file_write,
  359. .read = file_read,
  360. .poll = file_poll
  361. };
  362. static struct irqaction rtlx_irq = {
  363. .handler = rtlx_interrupt,
  364. .flags = IRQF_DISABLED,
  365. .name = "RTLX",
  366. };
  367. static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
  368. static char register_chrdev_failed[] __initdata =
  369. KERN_ERR "rtlx_module_init: unable to register device\n";
  370. static int rtlx_module_init(void)
  371. {
  372. struct device *dev;
  373. int i, err;
  374. major = register_chrdev(0, module_name, &rtlx_fops);
  375. if (major < 0) {
  376. printk(register_chrdev_failed);
  377. return major;
  378. }
  379. /* initialise the wait queues */
  380. for (i = 0; i < RTLX_CHANNELS; i++) {
  381. init_waitqueue_head(&channel_wqs[i].rt_queue);
  382. init_waitqueue_head(&channel_wqs[i].lx_queue);
  383. atomic_set(&channel_wqs[i].in_open, 0);
  384. dev = device_create(mt_class, NULL, MKDEV(major, i),
  385. "%s%d", module_name, i);
  386. if (IS_ERR(dev)) {
  387. err = PTR_ERR(dev);
  388. goto out_chrdev;
  389. }
  390. }
  391. /* set up notifiers */
  392. notify.start = starting;
  393. notify.stop = stopping;
  394. vpe_notify(RTLX_TARG_VPE, &notify);
  395. if (cpu_has_vint)
  396. set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
  397. rtlx_irq.dev_id = rtlx;
  398. setup_irq(rtlx_irq_num, &rtlx_irq);
  399. return 0;
  400. out_chrdev:
  401. for (i = 0; i < RTLX_CHANNELS; i++)
  402. device_destroy(mt_class, MKDEV(major, i));
  403. return err;
  404. }
  405. static void __exit rtlx_module_exit(void)
  406. {
  407. int i;
  408. for (i = 0; i < RTLX_CHANNELS; i++)
  409. device_destroy(mt_class, MKDEV(major, i));
  410. unregister_chrdev(major, module_name);
  411. }
  412. module_init(rtlx_module_init);
  413. module_exit(rtlx_module_exit);
  414. MODULE_DESCRIPTION("MIPS RTLX");
  415. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  416. MODULE_LICENSE("GPL");