rtlx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. volatile 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. if (*p == NULL) {
  151. if (can_sleep) {
  152. __wait_event_interruptible(channel_wqs[index].lx_queue,
  153. *p != NULL,
  154. ret);
  155. if (ret)
  156. goto out_fail;
  157. } else {
  158. printk(" *vpe_get_shared is NULL. "
  159. "Has an SP program been loaded?\n");
  160. ret = -ENOSYS;
  161. goto out_fail;
  162. }
  163. }
  164. if ((unsigned int)*p < KSEG0) {
  165. printk(KERN_WARNING "vpe_get_shared returned an invalid pointer "
  166. "maybe an error code %d\n", (int)*p);
  167. ret = -ENOSYS;
  168. goto out_fail;
  169. }
  170. if ((ret = rtlx_init(*p)) < 0)
  171. goto out_ret;
  172. }
  173. chan = &rtlx->channel[index];
  174. state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
  175. if (state == RTLX_STATE_OPENED) {
  176. ret = -EBUSY;
  177. goto out_fail;
  178. }
  179. out_fail:
  180. smp_mb();
  181. atomic_dec(&channel_wqs[index].in_open);
  182. smp_mb();
  183. out_ret:
  184. return ret;
  185. }
  186. int rtlx_release(int index)
  187. {
  188. rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
  189. return 0;
  190. }
  191. unsigned int rtlx_read_poll(int index, int can_sleep)
  192. {
  193. struct rtlx_channel *chan;
  194. if (rtlx == NULL)
  195. return 0;
  196. chan = &rtlx->channel[index];
  197. /* data available to read? */
  198. if (chan->lx_read == chan->lx_write) {
  199. if (can_sleep) {
  200. int ret = 0;
  201. __wait_event_interruptible(channel_wqs[index].lx_queue,
  202. chan->lx_read != chan->lx_write || sp_stopping,
  203. ret);
  204. if (ret)
  205. return ret;
  206. if (sp_stopping)
  207. return 0;
  208. } else
  209. return 0;
  210. }
  211. return (chan->lx_write + chan->buffer_size - chan->lx_read)
  212. % chan->buffer_size;
  213. }
  214. static inline int write_spacefree(int read, int write, int size)
  215. {
  216. if (read == write) {
  217. /*
  218. * Never fill the buffer completely, so indexes are always
  219. * equal if empty and only empty, or !equal if data available
  220. */
  221. return size - 1;
  222. }
  223. return ((read + size - write) % size) - 1;
  224. }
  225. unsigned int rtlx_write_poll(int index)
  226. {
  227. struct rtlx_channel *chan = &rtlx->channel[index];
  228. return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size);
  229. }
  230. static inline void copy_to(void *dst, void *src, size_t count, int user)
  231. {
  232. if (user)
  233. copy_to_user(dst, src, count);
  234. else
  235. memcpy(dst, src, count);
  236. }
  237. static inline void copy_from(void *dst, void *src, size_t count, int user)
  238. {
  239. if (user)
  240. copy_from_user(dst, src, count);
  241. else
  242. memcpy(dst, src, count);
  243. }
  244. ssize_t rtlx_read(int index, void *buff, size_t count, int user)
  245. {
  246. size_t fl = 0L;
  247. struct rtlx_channel *lx;
  248. if (rtlx == NULL)
  249. return -ENOSYS;
  250. lx = &rtlx->channel[index];
  251. /* find out how much in total */
  252. count = min(count,
  253. (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read)
  254. % lx->buffer_size);
  255. /* then how much from the read pointer onwards */
  256. fl = min( count, (size_t)lx->buffer_size - lx->lx_read);
  257. copy_to(buff, &lx->lx_buffer[lx->lx_read], fl, user);
  258. /* and if there is anything left at the beginning of the buffer */
  259. if ( count - fl )
  260. copy_to (buff + fl, lx->lx_buffer, count - fl, user);
  261. /* update the index */
  262. lx->lx_read += count;
  263. lx->lx_read %= lx->buffer_size;
  264. return count;
  265. }
  266. ssize_t rtlx_write(int index, void *buffer, size_t count, int user)
  267. {
  268. struct rtlx_channel *rt;
  269. size_t fl;
  270. if (rtlx == NULL)
  271. return(-ENOSYS);
  272. rt = &rtlx->channel[index];
  273. /* total number of bytes to copy */
  274. count = min(count,
  275. (size_t)write_spacefree(rt->rt_read, rt->rt_write,
  276. rt->buffer_size));
  277. /* first bit from write pointer to the end of the buffer, or count */
  278. fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
  279. copy_from (&rt->rt_buffer[rt->rt_write], buffer, fl, user);
  280. /* if there's any left copy to the beginning of the buffer */
  281. if( count - fl )
  282. copy_from (rt->rt_buffer, buffer + fl, count - fl, user);
  283. rt->rt_write += count;
  284. rt->rt_write %= rt->buffer_size;
  285. return(count);
  286. }
  287. static int file_open(struct inode *inode, struct file *filp)
  288. {
  289. int minor = iminor(inode);
  290. return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
  291. }
  292. static int file_release(struct inode *inode, struct file *filp)
  293. {
  294. int minor = iminor(inode);
  295. return rtlx_release(minor);
  296. }
  297. static unsigned int file_poll(struct file *file, poll_table * wait)
  298. {
  299. int minor;
  300. unsigned int mask = 0;
  301. minor = iminor(file->f_path.dentry->d_inode);
  302. poll_wait(file, &channel_wqs[minor].rt_queue, wait);
  303. poll_wait(file, &channel_wqs[minor].lx_queue, wait);
  304. if (rtlx == NULL)
  305. return 0;
  306. /* data available to read? */
  307. if (rtlx_read_poll(minor, 0))
  308. mask |= POLLIN | POLLRDNORM;
  309. /* space to write */
  310. if (rtlx_write_poll(minor))
  311. mask |= POLLOUT | POLLWRNORM;
  312. return mask;
  313. }
  314. static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
  315. loff_t * ppos)
  316. {
  317. int minor = iminor(file->f_path.dentry->d_inode);
  318. /* data available? */
  319. if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
  320. return 0; // -EAGAIN makes cat whinge
  321. }
  322. return rtlx_read(minor, buffer, count, 1);
  323. }
  324. static ssize_t file_write(struct file *file, const char __user * buffer,
  325. size_t count, loff_t * ppos)
  326. {
  327. int minor;
  328. struct rtlx_channel *rt;
  329. minor = iminor(file->f_path.dentry->d_inode);
  330. rt = &rtlx->channel[minor];
  331. /* any space left... */
  332. if (!rtlx_write_poll(minor)) {
  333. int ret = 0;
  334. if (file->f_flags & O_NONBLOCK)
  335. return -EAGAIN;
  336. __wait_event_interruptible(channel_wqs[minor].rt_queue,
  337. rtlx_write_poll(minor),
  338. ret);
  339. if (ret)
  340. return ret;
  341. }
  342. return rtlx_write(minor, (void *)buffer, count, 1);
  343. }
  344. static const struct file_operations rtlx_fops = {
  345. .owner = THIS_MODULE,
  346. .open = file_open,
  347. .release = file_release,
  348. .write = file_write,
  349. .read = file_read,
  350. .poll = file_poll
  351. };
  352. static struct irqaction rtlx_irq = {
  353. .handler = rtlx_interrupt,
  354. .flags = IRQF_DISABLED,
  355. .name = "RTLX",
  356. };
  357. static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
  358. static char register_chrdev_failed[] __initdata =
  359. KERN_ERR "rtlx_module_init: unable to register device\n";
  360. static int rtlx_module_init(void)
  361. {
  362. struct device *dev;
  363. int i, err;
  364. major = register_chrdev(0, module_name, &rtlx_fops);
  365. if (major < 0) {
  366. printk(register_chrdev_failed);
  367. return major;
  368. }
  369. /* initialise the wait queues */
  370. for (i = 0; i < RTLX_CHANNELS; i++) {
  371. init_waitqueue_head(&channel_wqs[i].rt_queue);
  372. init_waitqueue_head(&channel_wqs[i].lx_queue);
  373. atomic_set(&channel_wqs[i].in_open, 0);
  374. dev = device_create(mt_class, NULL, MKDEV(major, i),
  375. "%s%d", module_name, i);
  376. if (IS_ERR(dev)) {
  377. err = PTR_ERR(dev);
  378. goto out_chrdev;
  379. }
  380. }
  381. /* set up notifiers */
  382. notify.start = starting;
  383. notify.stop = stopping;
  384. vpe_notify(RTLX_TARG_VPE, &notify);
  385. if (cpu_has_vint)
  386. set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
  387. rtlx_irq.dev_id = rtlx;
  388. setup_irq(rtlx_irq_num, &rtlx_irq);
  389. return 0;
  390. out_chrdev:
  391. for (i = 0; i < RTLX_CHANNELS; i++)
  392. device_destroy(mt_class, MKDEV(major, i));
  393. return err;
  394. }
  395. static void __exit rtlx_module_exit(void)
  396. {
  397. int i;
  398. for (i = 0; i < RTLX_CHANNELS; i++)
  399. device_destroy(mt_class, MKDEV(major, i));
  400. unregister_chrdev(major, module_name);
  401. }
  402. module_init(rtlx_module_init);
  403. module_exit(rtlx_module_exit);
  404. MODULE_DESCRIPTION("MIPS RTLX");
  405. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  406. MODULE_LICENSE("GPL");