mailbox.c 12 KB

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