mailbox.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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/interrupt.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/mutex.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/kfifo.h>
  29. #include <linux/err.h>
  30. #include <linux/notifier.h>
  31. #include <linux/module.h>
  32. #include <plat/mailbox.h>
  33. static struct omap_mbox **mboxes;
  34. static int mbox_configured;
  35. static DEFINE_MUTEX(mbox_configured_lock);
  36. static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
  37. module_param(mbox_kfifo_size, uint, S_IRUGO);
  38. MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
  39. /* Mailbox FIFO handle functions */
  40. static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
  41. {
  42. return mbox->ops->fifo_read(mbox);
  43. }
  44. static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  45. {
  46. mbox->ops->fifo_write(mbox, msg);
  47. }
  48. static inline int mbox_fifo_empty(struct omap_mbox *mbox)
  49. {
  50. return mbox->ops->fifo_empty(mbox);
  51. }
  52. static inline int mbox_fifo_full(struct omap_mbox *mbox)
  53. {
  54. return mbox->ops->fifo_full(mbox);
  55. }
  56. /* Mailbox IRQ handle functions */
  57. static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  58. {
  59. if (mbox->ops->ack_irq)
  60. mbox->ops->ack_irq(mbox, irq);
  61. }
  62. static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  63. {
  64. return mbox->ops->is_irq(mbox, irq);
  65. }
  66. /*
  67. * message sender
  68. */
  69. static int __mbox_poll_for_space(struct omap_mbox *mbox)
  70. {
  71. int ret = 0, i = 1000;
  72. while (mbox_fifo_full(mbox)) {
  73. if (mbox->ops->type == OMAP_MBOX_TYPE2)
  74. return -1;
  75. if (--i == 0)
  76. return -1;
  77. udelay(1);
  78. }
  79. return ret;
  80. }
  81. int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
  82. {
  83. struct omap_mbox_queue *mq = mbox->txq;
  84. int ret = 0, len;
  85. spin_lock_bh(&mq->lock);
  86. if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
  87. ret = -ENOMEM;
  88. goto out;
  89. }
  90. if (kfifo_is_empty(&mq->fifo) && !__mbox_poll_for_space(mbox)) {
  91. mbox_fifo_write(mbox, msg);
  92. goto out;
  93. }
  94. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  95. WARN_ON(len != sizeof(msg));
  96. tasklet_schedule(&mbox->txq->tasklet);
  97. out:
  98. spin_unlock_bh(&mq->lock);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL(omap_mbox_msg_send);
  102. static void mbox_tx_tasklet(unsigned long tx_data)
  103. {
  104. struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
  105. struct omap_mbox_queue *mq = mbox->txq;
  106. mbox_msg_t msg;
  107. int ret;
  108. while (kfifo_len(&mq->fifo)) {
  109. if (__mbox_poll_for_space(mbox)) {
  110. omap_mbox_enable_irq(mbox, IRQ_TX);
  111. break;
  112. }
  113. ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
  114. sizeof(msg));
  115. WARN_ON(ret != sizeof(msg));
  116. mbox_fifo_write(mbox, msg);
  117. }
  118. }
  119. /*
  120. * Message receiver(workqueue)
  121. */
  122. static void mbox_rx_work(struct work_struct *work)
  123. {
  124. struct omap_mbox_queue *mq =
  125. container_of(work, struct omap_mbox_queue, work);
  126. mbox_msg_t msg;
  127. int len;
  128. while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
  129. len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  130. WARN_ON(len != sizeof(msg));
  131. blocking_notifier_call_chain(&mq->mbox->notifier, len,
  132. (void *)msg);
  133. spin_lock_irq(&mq->lock);
  134. if (mq->full) {
  135. mq->full = false;
  136. omap_mbox_enable_irq(mq->mbox, IRQ_RX);
  137. }
  138. spin_unlock_irq(&mq->lock);
  139. }
  140. }
  141. /*
  142. * Mailbox interrupt handler
  143. */
  144. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  145. {
  146. omap_mbox_disable_irq(mbox, IRQ_TX);
  147. ack_mbox_irq(mbox, IRQ_TX);
  148. tasklet_schedule(&mbox->txq->tasklet);
  149. }
  150. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  151. {
  152. struct omap_mbox_queue *mq = mbox->rxq;
  153. mbox_msg_t msg;
  154. int len;
  155. while (!mbox_fifo_empty(mbox)) {
  156. if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
  157. omap_mbox_disable_irq(mbox, IRQ_RX);
  158. mq->full = true;
  159. goto nomem;
  160. }
  161. msg = mbox_fifo_read(mbox);
  162. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  163. WARN_ON(len != sizeof(msg));
  164. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  165. break;
  166. }
  167. /* no more messages in the fifo. clear IRQ source. */
  168. ack_mbox_irq(mbox, IRQ_RX);
  169. nomem:
  170. schedule_work(&mbox->rxq->work);
  171. }
  172. static irqreturn_t mbox_interrupt(int irq, void *p)
  173. {
  174. struct omap_mbox *mbox = p;
  175. if (is_mbox_irq(mbox, IRQ_TX))
  176. __mbox_tx_interrupt(mbox);
  177. if (is_mbox_irq(mbox, IRQ_RX))
  178. __mbox_rx_interrupt(mbox);
  179. return IRQ_HANDLED;
  180. }
  181. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  182. void (*work) (struct work_struct *),
  183. void (*tasklet)(unsigned long))
  184. {
  185. struct omap_mbox_queue *mq;
  186. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  187. if (!mq)
  188. return NULL;
  189. spin_lock_init(&mq->lock);
  190. if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
  191. goto error;
  192. if (work)
  193. INIT_WORK(&mq->work, work);
  194. if (tasklet)
  195. tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
  196. return mq;
  197. error:
  198. kfree(mq);
  199. return NULL;
  200. }
  201. static void mbox_queue_free(struct omap_mbox_queue *q)
  202. {
  203. kfifo_free(&q->fifo);
  204. kfree(q);
  205. }
  206. static int omap_mbox_startup(struct omap_mbox *mbox)
  207. {
  208. int ret = 0;
  209. struct omap_mbox_queue *mq;
  210. mutex_lock(&mbox_configured_lock);
  211. if (!mbox_configured++) {
  212. if (likely(mbox->ops->startup)) {
  213. ret = mbox->ops->startup(mbox);
  214. if (unlikely(ret))
  215. goto fail_startup;
  216. } else
  217. goto fail_startup;
  218. }
  219. if (!mbox->use_count++) {
  220. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
  221. mbox->name, mbox);
  222. if (unlikely(ret)) {
  223. pr_err("failed to register mailbox interrupt:%d\n",
  224. ret);
  225. goto fail_request_irq;
  226. }
  227. mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
  228. if (!mq) {
  229. ret = -ENOMEM;
  230. goto fail_alloc_txq;
  231. }
  232. mbox->txq = mq;
  233. mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
  234. if (!mq) {
  235. ret = -ENOMEM;
  236. goto fail_alloc_rxq;
  237. }
  238. mbox->rxq = mq;
  239. mq->mbox = mbox;
  240. }
  241. mutex_unlock(&mbox_configured_lock);
  242. return 0;
  243. fail_alloc_rxq:
  244. mbox_queue_free(mbox->txq);
  245. fail_alloc_txq:
  246. free_irq(mbox->irq, mbox);
  247. fail_request_irq:
  248. if (mbox->ops->shutdown)
  249. mbox->ops->shutdown(mbox);
  250. mbox->use_count--;
  251. fail_startup:
  252. mbox_configured--;
  253. mutex_unlock(&mbox_configured_lock);
  254. return ret;
  255. }
  256. static void omap_mbox_fini(struct omap_mbox *mbox)
  257. {
  258. mutex_lock(&mbox_configured_lock);
  259. if (!--mbox->use_count) {
  260. free_irq(mbox->irq, mbox);
  261. tasklet_kill(&mbox->txq->tasklet);
  262. flush_work_sync(&mbox->rxq->work);
  263. mbox_queue_free(mbox->txq);
  264. mbox_queue_free(mbox->rxq);
  265. }
  266. if (likely(mbox->ops->shutdown)) {
  267. if (!--mbox_configured)
  268. mbox->ops->shutdown(mbox);
  269. }
  270. mutex_unlock(&mbox_configured_lock);
  271. }
  272. struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
  273. {
  274. struct omap_mbox *_mbox, *mbox = NULL;
  275. int i, ret;
  276. if (!mboxes)
  277. return ERR_PTR(-EINVAL);
  278. for (i = 0; (_mbox = mboxes[i]); i++) {
  279. if (!strcmp(_mbox->name, name)) {
  280. mbox = _mbox;
  281. break;
  282. }
  283. }
  284. if (!mbox)
  285. return ERR_PTR(-ENOENT);
  286. ret = omap_mbox_startup(mbox);
  287. if (ret)
  288. return ERR_PTR(-ENODEV);
  289. if (nb)
  290. blocking_notifier_chain_register(&mbox->notifier, nb);
  291. return mbox;
  292. }
  293. EXPORT_SYMBOL(omap_mbox_get);
  294. void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
  295. {
  296. blocking_notifier_chain_unregister(&mbox->notifier, nb);
  297. omap_mbox_fini(mbox);
  298. }
  299. EXPORT_SYMBOL(omap_mbox_put);
  300. static struct class omap_mbox_class = { .name = "mbox", };
  301. int omap_mbox_register(struct device *parent, struct omap_mbox **list)
  302. {
  303. int ret;
  304. int i;
  305. mboxes = list;
  306. if (!mboxes)
  307. return -EINVAL;
  308. for (i = 0; mboxes[i]; i++) {
  309. struct omap_mbox *mbox = mboxes[i];
  310. mbox->dev = device_create(&omap_mbox_class,
  311. parent, 0, mbox, "%s", mbox->name);
  312. if (IS_ERR(mbox->dev)) {
  313. ret = PTR_ERR(mbox->dev);
  314. goto err_out;
  315. }
  316. BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
  317. }
  318. return 0;
  319. err_out:
  320. while (i--)
  321. device_unregister(mboxes[i]->dev);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL(omap_mbox_register);
  325. int omap_mbox_unregister(void)
  326. {
  327. int i;
  328. if (!mboxes)
  329. return -EINVAL;
  330. for (i = 0; mboxes[i]; i++)
  331. device_unregister(mboxes[i]->dev);
  332. mboxes = NULL;
  333. return 0;
  334. }
  335. EXPORT_SYMBOL(omap_mbox_unregister);
  336. static int __init omap_mbox_init(void)
  337. {
  338. int err;
  339. err = class_register(&omap_mbox_class);
  340. if (err)
  341. return err;
  342. /* kfifo size sanity check: alignment and minimal size */
  343. mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
  344. mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
  345. sizeof(mbox_msg_t));
  346. return 0;
  347. }
  348. subsys_initcall(omap_mbox_init);
  349. static void __exit omap_mbox_exit(void)
  350. {
  351. class_unregister(&omap_mbox_class);
  352. }
  353. module_exit(omap_mbox_exit);
  354. MODULE_LICENSE("GPL v2");
  355. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  356. MODULE_AUTHOR("Toshihiro Kobayashi");
  357. MODULE_AUTHOR("Hiroshi DOYU");