rtlx.c 12 KB

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