rtlx.c 12 KB

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