rtlx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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/fs.h>
  22. #include <linux/init.h>
  23. #include <asm/uaccess.h>
  24. #include <linux/list.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/elf.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/moduleloader.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/poll.h>
  32. #include <linux/sched.h>
  33. #include <linux/wait.h>
  34. #include <asm/mipsmtregs.h>
  35. #include <asm/mips_mt.h>
  36. #include <asm/cacheflush.h>
  37. #include <linux/atomic.h>
  38. #include <asm/cpu.h>
  39. #include <asm/processor.h>
  40. #include <asm/vpe.h>
  41. #include <asm/rtlx.h>
  42. #include <asm/setup.h>
  43. static struct rtlx_info *rtlx;
  44. static int major;
  45. static char module_name[] = "rtlx";
  46. static struct chan_waitqueues {
  47. wait_queue_head_t rt_queue;
  48. wait_queue_head_t lx_queue;
  49. atomic_t in_open;
  50. struct mutex mutex;
  51. } channel_wqs[RTLX_CHANNELS];
  52. static struct vpe_notifications notify;
  53. static int sp_stopping;
  54. extern void *vpe_get_shared(int index);
  55. static void rtlx_dispatch(void)
  56. {
  57. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
  58. }
  59. /* Interrupt handler may be called before rtlx_init has otherwise had
  60. a chance to run.
  61. */
  62. static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
  63. {
  64. unsigned int vpeflags;
  65. unsigned long flags;
  66. int i;
  67. /* Ought not to be strictly necessary for SMTC builds */
  68. local_irq_save(flags);
  69. vpeflags = dvpe();
  70. set_c0_status(0x100 << MIPS_CPU_RTLX_IRQ);
  71. irq_enable_hazard();
  72. evpe(vpeflags);
  73. local_irq_restore(flags);
  74. for (i = 0; i < RTLX_CHANNELS; i++) {
  75. wake_up(&channel_wqs[i].lx_queue);
  76. wake_up(&channel_wqs[i].rt_queue);
  77. }
  78. return IRQ_HANDLED;
  79. }
  80. static void __used dump_rtlx(void)
  81. {
  82. int i;
  83. printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
  84. for (i = 0; i < RTLX_CHANNELS; i++) {
  85. struct rtlx_channel *chan = &rtlx->channel[i];
  86. printk(" rt_state %d lx_state %d buffer_size %d\n",
  87. chan->rt_state, chan->lx_state, chan->buffer_size);
  88. printk(" rt_read %d rt_write %d\n",
  89. chan->rt_read, chan->rt_write);
  90. printk(" lx_read %d lx_write %d\n",
  91. chan->lx_read, chan->lx_write);
  92. printk(" rt_buffer <%s>\n", chan->rt_buffer);
  93. printk(" lx_buffer <%s>\n", chan->lx_buffer);
  94. }
  95. }
  96. /* call when we have the address of the shared structure from the SP side. */
  97. static int rtlx_init(struct rtlx_info *rtlxi)
  98. {
  99. if (rtlxi->id != RTLX_ID) {
  100. printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n",
  101. rtlxi, rtlxi->id);
  102. return -ENOEXEC;
  103. }
  104. rtlx = rtlxi;
  105. return 0;
  106. }
  107. /* notifications */
  108. static void starting(int vpe)
  109. {
  110. int i;
  111. sp_stopping = 0;
  112. /* force a reload of rtlx */
  113. rtlx=NULL;
  114. /* wake up any sleeping rtlx_open's */
  115. for (i = 0; i < RTLX_CHANNELS; i++)
  116. wake_up_interruptible(&channel_wqs[i].lx_queue);
  117. }
  118. static void stopping(int vpe)
  119. {
  120. int i;
  121. sp_stopping = 1;
  122. for (i = 0; i < RTLX_CHANNELS; i++)
  123. wake_up_interruptible(&channel_wqs[i].lx_queue);
  124. }
  125. int rtlx_open(int index, int can_sleep)
  126. {
  127. struct rtlx_info **p;
  128. struct rtlx_channel *chan;
  129. enum rtlx_state state;
  130. int ret = 0;
  131. if (index >= RTLX_CHANNELS) {
  132. printk(KERN_DEBUG "rtlx_open index out of range\n");
  133. return -ENOSYS;
  134. }
  135. if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
  136. printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
  137. index);
  138. ret = -EBUSY;
  139. goto out_fail;
  140. }
  141. if (rtlx == NULL) {
  142. if( (p = vpe_get_shared(tclimit)) == NULL) {
  143. if (can_sleep) {
  144. ret = __wait_event_interruptible(
  145. channel_wqs[index].lx_queue,
  146. (p = vpe_get_shared(tclimit)));
  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 = __wait_event_interruptible(
  224. channel_wqs[index].lx_queue,
  225. (chan->lx_read != chan->lx_write) ||
  226. sp_stopping);
  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 = iminor(file_inode(file));
  328. unsigned int mask = 0;
  329. poll_wait(file, &channel_wqs[minor].rt_queue, wait);
  330. poll_wait(file, &channel_wqs[minor].lx_queue, wait);
  331. if (rtlx == NULL)
  332. return 0;
  333. /* data available to read? */
  334. if (rtlx_read_poll(minor, 0))
  335. mask |= POLLIN | POLLRDNORM;
  336. /* space to write */
  337. if (rtlx_write_poll(minor))
  338. mask |= POLLOUT | POLLWRNORM;
  339. return mask;
  340. }
  341. static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
  342. loff_t * ppos)
  343. {
  344. int minor = iminor(file_inode(file));
  345. /* data available? */
  346. if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
  347. return 0; // -EAGAIN makes cat whinge
  348. }
  349. return rtlx_read(minor, buffer, count);
  350. }
  351. static ssize_t file_write(struct file *file, const char __user * buffer,
  352. size_t count, loff_t * ppos)
  353. {
  354. int minor = iminor(file_inode(file));
  355. /* any space left... */
  356. if (!rtlx_write_poll(minor)) {
  357. int ret;
  358. if (file->f_flags & O_NONBLOCK)
  359. return -EAGAIN;
  360. ret = __wait_event_interruptible(channel_wqs[minor].rt_queue,
  361. rtlx_write_poll(minor));
  362. if (ret)
  363. return ret;
  364. }
  365. return rtlx_write(minor, buffer, count);
  366. }
  367. static const struct file_operations rtlx_fops = {
  368. .owner = THIS_MODULE,
  369. .open = file_open,
  370. .release = file_release,
  371. .write = file_write,
  372. .read = file_read,
  373. .poll = file_poll,
  374. .llseek = noop_llseek,
  375. };
  376. static struct irqaction rtlx_irq = {
  377. .handler = rtlx_interrupt,
  378. .name = "RTLX",
  379. };
  380. static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
  381. static char register_chrdev_failed[] __initdata =
  382. KERN_ERR "rtlx_module_init: unable to register device\n";
  383. static int __init rtlx_module_init(void)
  384. {
  385. struct device *dev;
  386. int i, err;
  387. if (!cpu_has_mipsmt) {
  388. printk("VPE loader: not a MIPS MT capable processor\n");
  389. return -ENODEV;
  390. }
  391. if (tclimit == 0) {
  392. printk(KERN_WARNING "No TCs reserved for AP/SP, not "
  393. "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
  394. "argument\n");
  395. return -ENODEV;
  396. }
  397. major = register_chrdev(0, module_name, &rtlx_fops);
  398. if (major < 0) {
  399. printk(register_chrdev_failed);
  400. return major;
  401. }
  402. /* initialise the wait queues */
  403. for (i = 0; i < RTLX_CHANNELS; i++) {
  404. init_waitqueue_head(&channel_wqs[i].rt_queue);
  405. init_waitqueue_head(&channel_wqs[i].lx_queue);
  406. atomic_set(&channel_wqs[i].in_open, 0);
  407. mutex_init(&channel_wqs[i].mutex);
  408. dev = device_create(mt_class, NULL, MKDEV(major, i), NULL,
  409. "%s%d", module_name, i);
  410. if (IS_ERR(dev)) {
  411. err = PTR_ERR(dev);
  412. goto out_chrdev;
  413. }
  414. }
  415. /* set up notifiers */
  416. notify.start = starting;
  417. notify.stop = stopping;
  418. vpe_notify(tclimit, &notify);
  419. if (cpu_has_vint)
  420. set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
  421. else {
  422. pr_err("APRP RTLX init on non-vectored-interrupt processor\n");
  423. err = -ENODEV;
  424. goto out_chrdev;
  425. }
  426. rtlx_irq.dev_id = rtlx;
  427. setup_irq(rtlx_irq_num, &rtlx_irq);
  428. return 0;
  429. out_chrdev:
  430. for (i = 0; i < RTLX_CHANNELS; i++)
  431. device_destroy(mt_class, MKDEV(major, i));
  432. return err;
  433. }
  434. static void __exit rtlx_module_exit(void)
  435. {
  436. int i;
  437. for (i = 0; i < RTLX_CHANNELS; i++)
  438. device_destroy(mt_class, MKDEV(major, i));
  439. unregister_chrdev(major, module_name);
  440. }
  441. module_init(rtlx_module_init);
  442. module_exit(rtlx_module_exit);
  443. MODULE_DESCRIPTION("MIPS RTLX");
  444. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  445. MODULE_LICENSE("GPL");