rtlx.c 13 KB

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