mailbox.c 9.1 KB

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