rtlx.c 11 KB

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