rtlx.c 12 KB

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