rtlx.c 12 KB

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