rtlx.c 12 KB

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