rtlx.c 12 KB

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