mailbox.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Mailbox: Common code for Mailbox controllers and users
  3. *
  4. * Copyright (C) 2013-2014 Linaro Ltd.
  5. * Author: Jassi Brar <jassisinghbrar@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/mutex.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/bitops.h>
  20. #include <linux/mailbox_client.h>
  21. #include <linux/mailbox_controller.h>
  22. #include "mailbox.h"
  23. static LIST_HEAD(mbox_cons);
  24. static DEFINE_MUTEX(con_mutex);
  25. static void poll_txdone(unsigned long data);
  26. static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
  27. {
  28. int idx;
  29. unsigned long flags;
  30. spin_lock_irqsave(&chan->lock, flags);
  31. /* See if there is any space left */
  32. if (chan->msg_count == MBOX_TX_QUEUE_LEN) {
  33. spin_unlock_irqrestore(&chan->lock, flags);
  34. return -ENOBUFS;
  35. }
  36. idx = chan->msg_free;
  37. chan->msg_data[idx] = mssg;
  38. chan->msg_count++;
  39. if (idx == MBOX_TX_QUEUE_LEN - 1)
  40. chan->msg_free = 0;
  41. else
  42. chan->msg_free++;
  43. spin_unlock_irqrestore(&chan->lock, flags);
  44. return idx;
  45. }
  46. static void msg_submit(struct mbox_chan *chan)
  47. {
  48. unsigned count, idx;
  49. unsigned long flags;
  50. void *data;
  51. int err = -EBUSY;
  52. spin_lock_irqsave(&chan->lock, flags);
  53. if (!chan->msg_count || chan->active_req)
  54. goto exit;
  55. count = chan->msg_count;
  56. idx = chan->msg_free;
  57. if (idx >= count)
  58. idx -= count;
  59. else
  60. idx += MBOX_TX_QUEUE_LEN - count;
  61. data = chan->msg_data[idx];
  62. if (chan->cl->tx_prepare)
  63. chan->cl->tx_prepare(chan->cl, data);
  64. /* Try to submit a message to the MBOX controller */
  65. err = chan->mbox->ops->send_data(chan, data);
  66. if (!err) {
  67. chan->active_req = data;
  68. chan->msg_count--;
  69. }
  70. exit:
  71. spin_unlock_irqrestore(&chan->lock, flags);
  72. if (!err && (chan->txdone_method & TXDONE_BY_POLL))
  73. poll_txdone((unsigned long)chan->mbox);
  74. }
  75. static void tx_tick(struct mbox_chan *chan, int r)
  76. {
  77. unsigned long flags;
  78. void *mssg;
  79. spin_lock_irqsave(&chan->lock, flags);
  80. mssg = chan->active_req;
  81. chan->active_req = NULL;
  82. spin_unlock_irqrestore(&chan->lock, flags);
  83. /* Submit next message */
  84. msg_submit(chan);
  85. /* Notify the client */
  86. if (mssg && chan->cl->tx_done)
  87. chan->cl->tx_done(chan->cl, mssg, r);
  88. if (chan->cl->tx_block)
  89. complete(&chan->tx_complete);
  90. }
  91. static void poll_txdone(unsigned long data)
  92. {
  93. struct mbox_controller *mbox = (struct mbox_controller *)data;
  94. bool txdone, resched = false;
  95. int i;
  96. for (i = 0; i < mbox->num_chans; i++) {
  97. struct mbox_chan *chan = &mbox->chans[i];
  98. if (chan->active_req && chan->cl) {
  99. txdone = chan->mbox->ops->last_tx_done(chan);
  100. if (txdone)
  101. tx_tick(chan, 0);
  102. else
  103. resched = true;
  104. }
  105. }
  106. if (resched)
  107. mod_timer(&mbox->poll, jiffies +
  108. msecs_to_jiffies(mbox->txpoll_period));
  109. }
  110. /**
  111. * mbox_chan_received_data - A way for controller driver to push data
  112. * received from remote to the upper layer.
  113. * @chan: Pointer to the mailbox channel on which RX happened.
  114. * @mssg: Client specific message typecasted as void *
  115. *
  116. * After startup and before shutdown any data received on the chan
  117. * is passed on to the API via atomic mbox_chan_received_data().
  118. * The controller should ACK the RX only after this call returns.
  119. */
  120. void mbox_chan_received_data(struct mbox_chan *chan, void *mssg)
  121. {
  122. /* No buffering the received data */
  123. if (chan->cl->rx_callback)
  124. chan->cl->rx_callback(chan->cl, mssg);
  125. }
  126. EXPORT_SYMBOL_GPL(mbox_chan_received_data);
  127. /**
  128. * mbox_chan_txdone - A way for controller driver to notify the
  129. * framework that the last TX has completed.
  130. * @chan: Pointer to the mailbox chan on which TX happened.
  131. * @r: Status of last TX - OK or ERROR
  132. *
  133. * The controller that has IRQ for TX ACK calls this atomic API
  134. * to tick the TX state machine. It works only if txdone_irq
  135. * is set by the controller.
  136. */
  137. void mbox_chan_txdone(struct mbox_chan *chan, int r)
  138. {
  139. if (unlikely(!(chan->txdone_method & TXDONE_BY_IRQ))) {
  140. dev_err(chan->mbox->dev,
  141. "Controller can't run the TX ticker\n");
  142. return;
  143. }
  144. tx_tick(chan, r);
  145. }
  146. EXPORT_SYMBOL_GPL(mbox_chan_txdone);
  147. /**
  148. * mbox_client_txdone - The way for a client to run the TX state machine.
  149. * @chan: Mailbox channel assigned to this client.
  150. * @r: Success status of last transmission.
  151. *
  152. * The client/protocol had received some 'ACK' packet and it notifies
  153. * the API that the last packet was sent successfully. This only works
  154. * if the controller can't sense TX-Done.
  155. */
  156. void mbox_client_txdone(struct mbox_chan *chan, int r)
  157. {
  158. if (unlikely(!(chan->txdone_method & TXDONE_BY_ACK))) {
  159. dev_err(chan->mbox->dev, "Client can't run the TX ticker\n");
  160. return;
  161. }
  162. tx_tick(chan, r);
  163. }
  164. EXPORT_SYMBOL_GPL(mbox_client_txdone);
  165. /**
  166. * mbox_client_peek_data - A way for client driver to pull data
  167. * received from remote by the controller.
  168. * @chan: Mailbox channel assigned to this client.
  169. *
  170. * A poke to controller driver for any received data.
  171. * The data is actually passed onto client via the
  172. * mbox_chan_received_data()
  173. * The call can be made from atomic context, so the controller's
  174. * implementation of peek_data() must not sleep.
  175. *
  176. * Return: True, if controller has, and is going to push after this,
  177. * some data.
  178. * False, if controller doesn't have any data to be read.
  179. */
  180. bool mbox_client_peek_data(struct mbox_chan *chan)
  181. {
  182. if (chan->mbox->ops->peek_data)
  183. return chan->mbox->ops->peek_data(chan);
  184. return false;
  185. }
  186. EXPORT_SYMBOL_GPL(mbox_client_peek_data);
  187. /**
  188. * mbox_send_message - For client to submit a message to be
  189. * sent to the remote.
  190. * @chan: Mailbox channel assigned to this client.
  191. * @mssg: Client specific message typecasted.
  192. *
  193. * For client to submit data to the controller destined for a remote
  194. * processor. If the client had set 'tx_block', the call will return
  195. * either when the remote receives the data or when 'tx_tout' millisecs
  196. * run out.
  197. * In non-blocking mode, the requests are buffered by the API and a
  198. * non-negative token is returned for each queued request. If the request
  199. * is not queued, a negative token is returned. Upon failure or successful
  200. * TX, the API calls 'tx_done' from atomic context, from which the client
  201. * could submit yet another request.
  202. * The pointer to message should be preserved until it is sent
  203. * over the chan, i.e, tx_done() is made.
  204. * This function could be called from atomic context as it simply
  205. * queues the data and returns a token against the request.
  206. *
  207. * Return: Non-negative integer for successful submission (non-blocking mode)
  208. * or transmission over chan (blocking mode).
  209. * Negative value denotes failure.
  210. */
  211. int mbox_send_message(struct mbox_chan *chan, void *mssg)
  212. {
  213. int t;
  214. if (!chan || !chan->cl)
  215. return -EINVAL;
  216. t = add_to_rbuf(chan, mssg);
  217. if (t < 0) {
  218. dev_err(chan->mbox->dev, "Try increasing MBOX_TX_QUEUE_LEN\n");
  219. return t;
  220. }
  221. msg_submit(chan);
  222. if (chan->cl->tx_block && chan->active_req) {
  223. unsigned long wait;
  224. int ret;
  225. if (!chan->cl->tx_tout) /* wait forever */
  226. wait = msecs_to_jiffies(3600000);
  227. else
  228. wait = msecs_to_jiffies(chan->cl->tx_tout);
  229. ret = wait_for_completion_timeout(&chan->tx_complete, wait);
  230. if (ret == 0) {
  231. t = -EIO;
  232. tx_tick(chan, -EIO);
  233. }
  234. }
  235. return t;
  236. }
  237. EXPORT_SYMBOL_GPL(mbox_send_message);
  238. /**
  239. * mbox_request_channel - Request a mailbox channel.
  240. * @cl: Identity of the client requesting the channel.
  241. * @index: Index of mailbox specifier in 'mboxes' property.
  242. *
  243. * The Client specifies its requirements and capabilities while asking for
  244. * a mailbox channel. It can't be called from atomic context.
  245. * The channel is exclusively allocated and can't be used by another
  246. * client before the owner calls mbox_free_channel.
  247. * After assignment, any packet received on this channel will be
  248. * handed over to the client via the 'rx_callback'.
  249. * The framework holds reference to the client, so the mbox_client
  250. * structure shouldn't be modified until the mbox_free_channel returns.
  251. *
  252. * Return: Pointer to the channel assigned to the client if successful.
  253. * ERR_PTR for request failure.
  254. */
  255. struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
  256. {
  257. struct device *dev = cl->dev;
  258. struct mbox_controller *mbox;
  259. struct of_phandle_args spec;
  260. struct mbox_chan *chan;
  261. unsigned long flags;
  262. int ret;
  263. if (!dev || !dev->of_node) {
  264. pr_debug("%s: No owner device node\n", __func__);
  265. return ERR_PTR(-ENODEV);
  266. }
  267. mutex_lock(&con_mutex);
  268. if (of_parse_phandle_with_args(dev->of_node, "mboxes",
  269. "#mbox-cells", index, &spec)) {
  270. dev_dbg(dev, "%s: can't parse \"mboxes\" property\n", __func__);
  271. mutex_unlock(&con_mutex);
  272. return ERR_PTR(-ENODEV);
  273. }
  274. chan = ERR_PTR(-EPROBE_DEFER);
  275. list_for_each_entry(mbox, &mbox_cons, node)
  276. if (mbox->dev->of_node == spec.np) {
  277. chan = mbox->of_xlate(mbox, &spec);
  278. break;
  279. }
  280. of_node_put(spec.np);
  281. if (IS_ERR(chan)) {
  282. mutex_unlock(&con_mutex);
  283. return chan;
  284. }
  285. if (chan->cl || !try_module_get(mbox->dev->driver->owner)) {
  286. dev_dbg(dev, "%s: mailbox not free\n", __func__);
  287. mutex_unlock(&con_mutex);
  288. return ERR_PTR(-EBUSY);
  289. }
  290. spin_lock_irqsave(&chan->lock, flags);
  291. chan->msg_free = 0;
  292. chan->msg_count = 0;
  293. chan->active_req = NULL;
  294. chan->cl = cl;
  295. init_completion(&chan->tx_complete);
  296. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  297. chan->txdone_method |= TXDONE_BY_ACK;
  298. spin_unlock_irqrestore(&chan->lock, flags);
  299. ret = chan->mbox->ops->startup(chan);
  300. if (ret) {
  301. dev_err(dev, "Unable to startup the chan (%d)\n", ret);
  302. mbox_free_channel(chan);
  303. chan = ERR_PTR(ret);
  304. }
  305. mutex_unlock(&con_mutex);
  306. return chan;
  307. }
  308. EXPORT_SYMBOL_GPL(mbox_request_channel);
  309. struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
  310. const char *name)
  311. {
  312. struct device_node *np = cl->dev->of_node;
  313. struct property *prop;
  314. const char *mbox_name;
  315. int index = 0;
  316. if (!np) {
  317. dev_err(cl->dev, "%s() currently only supports DT\n", __func__);
  318. return ERR_PTR(-ENOSYS);
  319. }
  320. if (!of_get_property(np, "mbox-names", NULL)) {
  321. dev_err(cl->dev,
  322. "%s() requires an \"mbox-names\" property\n", __func__);
  323. return ERR_PTR(-ENOSYS);
  324. }
  325. of_property_for_each_string(np, "mbox-names", prop, mbox_name) {
  326. if (!strncmp(name, mbox_name, strlen(name)))
  327. break;
  328. index++;
  329. }
  330. return mbox_request_channel(cl, index);
  331. }
  332. EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
  333. /**
  334. * mbox_free_channel - The client relinquishes control of a mailbox
  335. * channel by this call.
  336. * @chan: The mailbox channel to be freed.
  337. */
  338. void mbox_free_channel(struct mbox_chan *chan)
  339. {
  340. unsigned long flags;
  341. if (!chan || !chan->cl)
  342. return;
  343. chan->mbox->ops->shutdown(chan);
  344. /* The queued TX requests are simply aborted, no callbacks are made */
  345. spin_lock_irqsave(&chan->lock, flags);
  346. chan->cl = NULL;
  347. chan->active_req = NULL;
  348. if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
  349. chan->txdone_method = TXDONE_BY_POLL;
  350. module_put(chan->mbox->dev->driver->owner);
  351. spin_unlock_irqrestore(&chan->lock, flags);
  352. }
  353. EXPORT_SYMBOL_GPL(mbox_free_channel);
  354. static struct mbox_chan *
  355. of_mbox_index_xlate(struct mbox_controller *mbox,
  356. const struct of_phandle_args *sp)
  357. {
  358. int ind = sp->args[0];
  359. if (ind >= mbox->num_chans)
  360. return ERR_PTR(-EINVAL);
  361. return &mbox->chans[ind];
  362. }
  363. /**
  364. * mbox_controller_register - Register the mailbox controller
  365. * @mbox: Pointer to the mailbox controller.
  366. *
  367. * The controller driver registers its communication channels
  368. */
  369. int mbox_controller_register(struct mbox_controller *mbox)
  370. {
  371. int i, txdone;
  372. /* Sanity check */
  373. if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
  374. return -EINVAL;
  375. if (mbox->txdone_irq)
  376. txdone = TXDONE_BY_IRQ;
  377. else if (mbox->txdone_poll)
  378. txdone = TXDONE_BY_POLL;
  379. else /* It has to be ACK then */
  380. txdone = TXDONE_BY_ACK;
  381. if (txdone == TXDONE_BY_POLL) {
  382. mbox->poll.function = &poll_txdone;
  383. mbox->poll.data = (unsigned long)mbox;
  384. init_timer(&mbox->poll);
  385. }
  386. for (i = 0; i < mbox->num_chans; i++) {
  387. struct mbox_chan *chan = &mbox->chans[i];
  388. chan->cl = NULL;
  389. chan->mbox = mbox;
  390. chan->txdone_method = txdone;
  391. spin_lock_init(&chan->lock);
  392. }
  393. if (!mbox->of_xlate)
  394. mbox->of_xlate = of_mbox_index_xlate;
  395. mutex_lock(&con_mutex);
  396. list_add_tail(&mbox->node, &mbox_cons);
  397. mutex_unlock(&con_mutex);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(mbox_controller_register);
  401. /**
  402. * mbox_controller_unregister - Unregister the mailbox controller
  403. * @mbox: Pointer to the mailbox controller.
  404. */
  405. void mbox_controller_unregister(struct mbox_controller *mbox)
  406. {
  407. int i;
  408. if (!mbox)
  409. return;
  410. mutex_lock(&con_mutex);
  411. list_del(&mbox->node);
  412. for (i = 0; i < mbox->num_chans; i++)
  413. mbox_free_channel(&mbox->chans[i]);
  414. if (mbox->txdone_poll)
  415. del_timer_sync(&mbox->poll);
  416. mutex_unlock(&con_mutex);
  417. }
  418. EXPORT_SYMBOL_GPL(mbox_controller_unregister);