rtlx.c 12 KB

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