rtlx.c 12 KB

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