mailbox.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * OMAP mailbox driver
  3. *
  4. * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
  5. *
  6. * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/device.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/kfifo.h>
  30. #include <linux/err.h>
  31. #include <plat/mailbox.h>
  32. static struct workqueue_struct *mboxd;
  33. static struct omap_mbox *mboxes;
  34. static DEFINE_SPINLOCK(mboxes_lock);
  35. static bool rq_full;
  36. static int mbox_configured;
  37. static DEFINE_MUTEX(mbox_configured_lock);
  38. static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
  39. module_param(mbox_kfifo_size, uint, S_IRUGO);
  40. MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
  41. /* Mailbox FIFO handle functions */
  42. static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
  43. {
  44. return mbox->ops->fifo_read(mbox);
  45. }
  46. static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  47. {
  48. mbox->ops->fifo_write(mbox, msg);
  49. }
  50. static inline int mbox_fifo_empty(struct omap_mbox *mbox)
  51. {
  52. return mbox->ops->fifo_empty(mbox);
  53. }
  54. static inline int mbox_fifo_full(struct omap_mbox *mbox)
  55. {
  56. return mbox->ops->fifo_full(mbox);
  57. }
  58. /* Mailbox IRQ handle functions */
  59. static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  60. {
  61. if (mbox->ops->ack_irq)
  62. mbox->ops->ack_irq(mbox, irq);
  63. }
  64. static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  65. {
  66. return mbox->ops->is_irq(mbox, irq);
  67. }
  68. /*
  69. * message sender
  70. */
  71. static int __mbox_poll_for_space(struct omap_mbox *mbox)
  72. {
  73. int ret = 0, i = 1000;
  74. while (mbox_fifo_full(mbox)) {
  75. if (mbox->ops->type == OMAP_MBOX_TYPE2)
  76. return -1;
  77. if (--i == 0)
  78. return -1;
  79. udelay(1);
  80. }
  81. return ret;
  82. }
  83. int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
  84. {
  85. struct omap_mbox_queue *mq = mbox->txq;
  86. int ret = 0, len;
  87. spin_lock(&mq->lock);
  88. if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
  89. ret = -ENOMEM;
  90. goto out;
  91. }
  92. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  93. WARN_ON(len != sizeof(msg));
  94. tasklet_schedule(&mbox->txq->tasklet);
  95. out:
  96. spin_unlock(&mq->lock);
  97. return ret;
  98. }
  99. EXPORT_SYMBOL(omap_mbox_msg_send);
  100. static void mbox_tx_tasklet(unsigned long tx_data)
  101. {
  102. struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
  103. struct omap_mbox_queue *mq = mbox->txq;
  104. mbox_msg_t msg;
  105. int ret;
  106. while (kfifo_len(&mq->fifo)) {
  107. if (__mbox_poll_for_space(mbox)) {
  108. omap_mbox_enable_irq(mbox, IRQ_TX);
  109. break;
  110. }
  111. ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
  112. sizeof(msg));
  113. WARN_ON(ret != sizeof(msg));
  114. mbox_fifo_write(mbox, msg);
  115. }
  116. }
  117. /*
  118. * Message receiver(workqueue)
  119. */
  120. static void mbox_rx_work(struct work_struct *work)
  121. {
  122. struct omap_mbox_queue *mq =
  123. container_of(work, struct omap_mbox_queue, work);
  124. mbox_msg_t msg;
  125. int len;
  126. while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
  127. len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  128. WARN_ON(len != sizeof(msg));
  129. if (mq->callback)
  130. mq->callback((void *)msg);
  131. }
  132. }
  133. /*
  134. * Mailbox interrupt handler
  135. */
  136. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  137. {
  138. omap_mbox_disable_irq(mbox, IRQ_TX);
  139. ack_mbox_irq(mbox, IRQ_TX);
  140. tasklet_schedule(&mbox->txq->tasklet);
  141. }
  142. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  143. {
  144. struct omap_mbox_queue *mq = mbox->rxq;
  145. mbox_msg_t msg;
  146. int len;
  147. while (!mbox_fifo_empty(mbox)) {
  148. if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
  149. omap_mbox_disable_irq(mbox, IRQ_RX);
  150. rq_full = true;
  151. goto nomem;
  152. }
  153. msg = mbox_fifo_read(mbox);
  154. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  155. WARN_ON(len != sizeof(msg));
  156. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  157. break;
  158. }
  159. /* no more messages in the fifo. clear IRQ source. */
  160. ack_mbox_irq(mbox, IRQ_RX);
  161. nomem:
  162. queue_work(mboxd, &mbox->rxq->work);
  163. }
  164. static irqreturn_t mbox_interrupt(int irq, void *p)
  165. {
  166. struct omap_mbox *mbox = p;
  167. if (is_mbox_irq(mbox, IRQ_TX))
  168. __mbox_tx_interrupt(mbox);
  169. if (is_mbox_irq(mbox, IRQ_RX))
  170. __mbox_rx_interrupt(mbox);
  171. return IRQ_HANDLED;
  172. }
  173. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  174. void (*work) (struct work_struct *),
  175. void (*tasklet)(unsigned long))
  176. {
  177. struct omap_mbox_queue *mq;
  178. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  179. if (!mq)
  180. return NULL;
  181. spin_lock_init(&mq->lock);
  182. if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
  183. goto error;
  184. if (work)
  185. INIT_WORK(&mq->work, work);
  186. if (tasklet)
  187. tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
  188. return mq;
  189. error:
  190. kfree(mq);
  191. return NULL;
  192. }
  193. static void mbox_queue_free(struct omap_mbox_queue *q)
  194. {
  195. kfifo_free(&q->fifo);
  196. kfree(q);
  197. }
  198. static int omap_mbox_startup(struct omap_mbox *mbox)
  199. {
  200. int ret = 0;
  201. struct omap_mbox_queue *mq;
  202. if (mbox->ops->startup) {
  203. mutex_lock(&mbox_configured_lock);
  204. if (!mbox_configured)
  205. ret = mbox->ops->startup(mbox);
  206. if (ret) {
  207. mutex_unlock(&mbox_configured_lock);
  208. return ret;
  209. }
  210. mbox_configured++;
  211. mutex_unlock(&mbox_configured_lock);
  212. }
  213. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
  214. mbox->name, mbox);
  215. if (ret) {
  216. printk(KERN_ERR
  217. "failed to register mailbox interrupt:%d\n", ret);
  218. goto fail_request_irq;
  219. }
  220. mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
  221. if (!mq) {
  222. ret = -ENOMEM;
  223. goto fail_alloc_txq;
  224. }
  225. mbox->txq = mq;
  226. mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
  227. if (!mq) {
  228. ret = -ENOMEM;
  229. goto fail_alloc_rxq;
  230. }
  231. mbox->rxq = mq;
  232. return 0;
  233. fail_alloc_rxq:
  234. mbox_queue_free(mbox->txq);
  235. fail_alloc_txq:
  236. free_irq(mbox->irq, mbox);
  237. fail_request_irq:
  238. if (mbox->ops->shutdown)
  239. mbox->ops->shutdown(mbox);
  240. return ret;
  241. }
  242. static void omap_mbox_fini(struct omap_mbox *mbox)
  243. {
  244. free_irq(mbox->irq, mbox);
  245. tasklet_kill(&mbox->txq->tasklet);
  246. flush_work(&mbox->rxq->work);
  247. mbox_queue_free(mbox->txq);
  248. mbox_queue_free(mbox->rxq);
  249. if (mbox->ops->shutdown) {
  250. mutex_lock(&mbox_configured_lock);
  251. if (mbox_configured > 0)
  252. mbox_configured--;
  253. if (!mbox_configured)
  254. mbox->ops->shutdown(mbox);
  255. mutex_unlock(&mbox_configured_lock);
  256. }
  257. }
  258. static struct omap_mbox **find_mboxes(const char *name)
  259. {
  260. struct omap_mbox **p;
  261. for (p = &mboxes; *p; p = &(*p)->next) {
  262. if (strcmp((*p)->name, name) == 0)
  263. break;
  264. }
  265. return p;
  266. }
  267. struct omap_mbox *omap_mbox_get(const char *name)
  268. {
  269. struct omap_mbox *mbox;
  270. int ret;
  271. spin_lock(&mboxes_lock);
  272. mbox = *(find_mboxes(name));
  273. if (mbox == NULL) {
  274. spin_unlock(&mboxes_lock);
  275. return ERR_PTR(-ENOENT);
  276. }
  277. spin_unlock(&mboxes_lock);
  278. ret = omap_mbox_startup(mbox);
  279. if (ret)
  280. return ERR_PTR(-ENODEV);
  281. return mbox;
  282. }
  283. EXPORT_SYMBOL(omap_mbox_get);
  284. void omap_mbox_put(struct omap_mbox *mbox)
  285. {
  286. omap_mbox_fini(mbox);
  287. }
  288. EXPORT_SYMBOL(omap_mbox_put);
  289. int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
  290. {
  291. int ret = 0;
  292. struct omap_mbox **tmp;
  293. if (!mbox)
  294. return -EINVAL;
  295. if (mbox->next)
  296. return -EBUSY;
  297. spin_lock(&mboxes_lock);
  298. tmp = find_mboxes(mbox->name);
  299. if (*tmp) {
  300. ret = -EBUSY;
  301. spin_unlock(&mboxes_lock);
  302. goto err_find;
  303. }
  304. *tmp = mbox;
  305. spin_unlock(&mboxes_lock);
  306. return 0;
  307. err_find:
  308. return ret;
  309. }
  310. EXPORT_SYMBOL(omap_mbox_register);
  311. int omap_mbox_unregister(struct omap_mbox *mbox)
  312. {
  313. struct omap_mbox **tmp;
  314. spin_lock(&mboxes_lock);
  315. tmp = &mboxes;
  316. while (*tmp) {
  317. if (mbox == *tmp) {
  318. *tmp = mbox->next;
  319. mbox->next = NULL;
  320. spin_unlock(&mboxes_lock);
  321. return 0;
  322. }
  323. tmp = &(*tmp)->next;
  324. }
  325. spin_unlock(&mboxes_lock);
  326. return -EINVAL;
  327. }
  328. EXPORT_SYMBOL(omap_mbox_unregister);
  329. static int __init omap_mbox_init(void)
  330. {
  331. mboxd = create_workqueue("mboxd");
  332. if (!mboxd)
  333. return -ENOMEM;
  334. /* kfifo size sanity check: alignment and minimal size */
  335. mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
  336. mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, sizeof(mbox_msg_t));
  337. return 0;
  338. }
  339. module_init(omap_mbox_init);
  340. static void __exit omap_mbox_exit(void)
  341. {
  342. destroy_workqueue(mboxd);
  343. }
  344. module_exit(omap_mbox_exit);
  345. MODULE_LICENSE("GPL v2");
  346. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  347. MODULE_AUTHOR("Toshihiro Kobayashi");
  348. MODULE_AUTHOR("Hiroshi DOYU");