omap-mailbox.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 "omap-mbox.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. void omap_mbox_save_ctx(struct omap_mbox *mbox)
  103. {
  104. if (!mbox->ops->save_ctx) {
  105. dev_err(mbox->dev, "%s:\tno save\n", __func__);
  106. return;
  107. }
  108. mbox->ops->save_ctx(mbox);
  109. }
  110. EXPORT_SYMBOL(omap_mbox_save_ctx);
  111. void omap_mbox_restore_ctx(struct omap_mbox *mbox)
  112. {
  113. if (!mbox->ops->restore_ctx) {
  114. dev_err(mbox->dev, "%s:\tno restore\n", __func__);
  115. return;
  116. }
  117. mbox->ops->restore_ctx(mbox);
  118. }
  119. EXPORT_SYMBOL(omap_mbox_restore_ctx);
  120. void omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  121. {
  122. mbox->ops->enable_irq(mbox, irq);
  123. }
  124. EXPORT_SYMBOL(omap_mbox_enable_irq);
  125. void omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  126. {
  127. mbox->ops->disable_irq(mbox, irq);
  128. }
  129. EXPORT_SYMBOL(omap_mbox_disable_irq);
  130. static void mbox_tx_tasklet(unsigned long tx_data)
  131. {
  132. struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
  133. struct omap_mbox_queue *mq = mbox->txq;
  134. mbox_msg_t msg;
  135. int ret;
  136. while (kfifo_len(&mq->fifo)) {
  137. if (__mbox_poll_for_space(mbox)) {
  138. omap_mbox_enable_irq(mbox, IRQ_TX);
  139. break;
  140. }
  141. ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
  142. sizeof(msg));
  143. WARN_ON(ret != sizeof(msg));
  144. mbox_fifo_write(mbox, msg);
  145. }
  146. }
  147. /*
  148. * Message receiver(workqueue)
  149. */
  150. static void mbox_rx_work(struct work_struct *work)
  151. {
  152. struct omap_mbox_queue *mq =
  153. container_of(work, struct omap_mbox_queue, work);
  154. mbox_msg_t msg;
  155. int len;
  156. while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
  157. len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  158. WARN_ON(len != sizeof(msg));
  159. blocking_notifier_call_chain(&mq->mbox->notifier, len,
  160. (void *)msg);
  161. spin_lock_irq(&mq->lock);
  162. if (mq->full) {
  163. mq->full = false;
  164. omap_mbox_enable_irq(mq->mbox, IRQ_RX);
  165. }
  166. spin_unlock_irq(&mq->lock);
  167. }
  168. }
  169. /*
  170. * Mailbox interrupt handler
  171. */
  172. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  173. {
  174. omap_mbox_disable_irq(mbox, IRQ_TX);
  175. ack_mbox_irq(mbox, IRQ_TX);
  176. tasklet_schedule(&mbox->txq->tasklet);
  177. }
  178. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  179. {
  180. struct omap_mbox_queue *mq = mbox->rxq;
  181. mbox_msg_t msg;
  182. int len;
  183. while (!mbox_fifo_empty(mbox)) {
  184. if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
  185. omap_mbox_disable_irq(mbox, IRQ_RX);
  186. mq->full = true;
  187. goto nomem;
  188. }
  189. msg = mbox_fifo_read(mbox);
  190. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  191. WARN_ON(len != sizeof(msg));
  192. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  193. break;
  194. }
  195. /* no more messages in the fifo. clear IRQ source. */
  196. ack_mbox_irq(mbox, IRQ_RX);
  197. nomem:
  198. schedule_work(&mbox->rxq->work);
  199. }
  200. static irqreturn_t mbox_interrupt(int irq, void *p)
  201. {
  202. struct omap_mbox *mbox = p;
  203. if (is_mbox_irq(mbox, IRQ_TX))
  204. __mbox_tx_interrupt(mbox);
  205. if (is_mbox_irq(mbox, IRQ_RX))
  206. __mbox_rx_interrupt(mbox);
  207. return IRQ_HANDLED;
  208. }
  209. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  210. void (*work) (struct work_struct *),
  211. void (*tasklet)(unsigned long))
  212. {
  213. struct omap_mbox_queue *mq;
  214. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  215. if (!mq)
  216. return NULL;
  217. spin_lock_init(&mq->lock);
  218. if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
  219. goto error;
  220. if (work)
  221. INIT_WORK(&mq->work, work);
  222. if (tasklet)
  223. tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
  224. return mq;
  225. error:
  226. kfree(mq);
  227. return NULL;
  228. }
  229. static void mbox_queue_free(struct omap_mbox_queue *q)
  230. {
  231. kfifo_free(&q->fifo);
  232. kfree(q);
  233. }
  234. static int omap_mbox_startup(struct omap_mbox *mbox)
  235. {
  236. int ret = 0;
  237. struct omap_mbox_queue *mq;
  238. mutex_lock(&mbox_configured_lock);
  239. if (!mbox_configured++) {
  240. if (likely(mbox->ops->startup)) {
  241. ret = mbox->ops->startup(mbox);
  242. if (unlikely(ret))
  243. goto fail_startup;
  244. } else
  245. goto fail_startup;
  246. }
  247. if (!mbox->use_count++) {
  248. mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
  249. if (!mq) {
  250. ret = -ENOMEM;
  251. goto fail_alloc_txq;
  252. }
  253. mbox->txq = mq;
  254. mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
  255. if (!mq) {
  256. ret = -ENOMEM;
  257. goto fail_alloc_rxq;
  258. }
  259. mbox->rxq = mq;
  260. mq->mbox = mbox;
  261. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
  262. mbox->name, mbox);
  263. if (unlikely(ret)) {
  264. pr_err("failed to register mailbox interrupt:%d\n",
  265. ret);
  266. goto fail_request_irq;
  267. }
  268. omap_mbox_enable_irq(mbox, IRQ_RX);
  269. }
  270. mutex_unlock(&mbox_configured_lock);
  271. return 0;
  272. fail_request_irq:
  273. mbox_queue_free(mbox->rxq);
  274. fail_alloc_rxq:
  275. mbox_queue_free(mbox->txq);
  276. fail_alloc_txq:
  277. if (mbox->ops->shutdown)
  278. mbox->ops->shutdown(mbox);
  279. mbox->use_count--;
  280. fail_startup:
  281. mbox_configured--;
  282. mutex_unlock(&mbox_configured_lock);
  283. return ret;
  284. }
  285. static void omap_mbox_fini(struct omap_mbox *mbox)
  286. {
  287. mutex_lock(&mbox_configured_lock);
  288. if (!--mbox->use_count) {
  289. omap_mbox_disable_irq(mbox, IRQ_RX);
  290. free_irq(mbox->irq, mbox);
  291. tasklet_kill(&mbox->txq->tasklet);
  292. flush_work(&mbox->rxq->work);
  293. mbox_queue_free(mbox->txq);
  294. mbox_queue_free(mbox->rxq);
  295. }
  296. if (likely(mbox->ops->shutdown)) {
  297. if (!--mbox_configured)
  298. mbox->ops->shutdown(mbox);
  299. }
  300. mutex_unlock(&mbox_configured_lock);
  301. }
  302. struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
  303. {
  304. struct omap_mbox *_mbox, *mbox = NULL;
  305. int i, ret;
  306. if (!mboxes)
  307. return ERR_PTR(-EINVAL);
  308. for (i = 0; (_mbox = mboxes[i]); i++) {
  309. if (!strcmp(_mbox->name, name)) {
  310. mbox = _mbox;
  311. break;
  312. }
  313. }
  314. if (!mbox)
  315. return ERR_PTR(-ENOENT);
  316. if (nb)
  317. blocking_notifier_chain_register(&mbox->notifier, nb);
  318. ret = omap_mbox_startup(mbox);
  319. if (ret) {
  320. blocking_notifier_chain_unregister(&mbox->notifier, nb);
  321. return ERR_PTR(-ENODEV);
  322. }
  323. return mbox;
  324. }
  325. EXPORT_SYMBOL(omap_mbox_get);
  326. void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
  327. {
  328. blocking_notifier_chain_unregister(&mbox->notifier, nb);
  329. omap_mbox_fini(mbox);
  330. }
  331. EXPORT_SYMBOL(omap_mbox_put);
  332. static struct class omap_mbox_class = { .name = "mbox", };
  333. int omap_mbox_register(struct device *parent, struct omap_mbox **list)
  334. {
  335. int ret;
  336. int i;
  337. mboxes = list;
  338. if (!mboxes)
  339. return -EINVAL;
  340. for (i = 0; mboxes[i]; i++) {
  341. struct omap_mbox *mbox = mboxes[i];
  342. mbox->dev = device_create(&omap_mbox_class,
  343. parent, 0, mbox, "%s", mbox->name);
  344. if (IS_ERR(mbox->dev)) {
  345. ret = PTR_ERR(mbox->dev);
  346. goto err_out;
  347. }
  348. BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
  349. }
  350. return 0;
  351. err_out:
  352. while (i--)
  353. device_unregister(mboxes[i]->dev);
  354. return ret;
  355. }
  356. EXPORT_SYMBOL(omap_mbox_register);
  357. int omap_mbox_unregister(void)
  358. {
  359. int i;
  360. if (!mboxes)
  361. return -EINVAL;
  362. for (i = 0; mboxes[i]; i++)
  363. device_unregister(mboxes[i]->dev);
  364. mboxes = NULL;
  365. return 0;
  366. }
  367. EXPORT_SYMBOL(omap_mbox_unregister);
  368. static int __init omap_mbox_init(void)
  369. {
  370. int err;
  371. err = class_register(&omap_mbox_class);
  372. if (err)
  373. return err;
  374. /* kfifo size sanity check: alignment and minimal size */
  375. mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
  376. mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
  377. sizeof(mbox_msg_t));
  378. return 0;
  379. }
  380. subsys_initcall(omap_mbox_init);
  381. static void __exit omap_mbox_exit(void)
  382. {
  383. class_unregister(&omap_mbox_class);
  384. }
  385. module_exit(omap_mbox_exit);
  386. MODULE_LICENSE("GPL v2");
  387. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  388. MODULE_AUTHOR("Toshihiro Kobayashi");
  389. MODULE_AUTHOR("Hiroshi DOYU");