mailbox.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Mailbox: Common code for Mailbox controllers and users
  4. *
  5. * Copyright (C) 2013-2014 Linaro Ltd.
  6. * Author: Jassi Brar <jassisinghbrar@gmail.com>
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/mutex.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/bitops.h>
  17. #include <linux/mailbox_client.h>
  18. #include <linux/mailbox_controller.h>
  19. #include <linux/of.h>
  20. #include "mailbox.h"
  21. static LIST_HEAD(mbox_cons);
  22. static DEFINE_MUTEX(con_mutex);
  23. static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
  24. {
  25. int idx;
  26. unsigned long flags;
  27. spin_lock_irqsave(&chan->lock, flags);
  28. /* See if there is any space left */
  29. if (chan->msg_count == MBOX_TX_QUEUE_LEN) {
  30. spin_unlock_irqrestore(&chan->lock, flags);
  31. return -ENOBUFS;
  32. }
  33. idx = chan->msg_free;
  34. chan->msg_data[idx] = mssg;
  35. chan->msg_count++;
  36. if (idx == MBOX_TX_QUEUE_LEN - 1)
  37. chan->msg_free = 0;
  38. else
  39. chan->msg_free++;
  40. spin_unlock_irqrestore(&chan->lock, flags);
  41. return idx;
  42. }
  43. static void msg_submit(struct mbox_chan *chan)
  44. {
  45. unsigned count, idx;
  46. unsigned long flags;
  47. void *data;
  48. int err = -EBUSY;
  49. spin_lock_irqsave(&chan->lock, flags);
  50. if (!chan->msg_count || chan->active_req)
  51. goto exit;
  52. count = chan->msg_count;
  53. idx = chan->msg_free;
  54. if (idx >= count)
  55. idx -= count;
  56. else
  57. idx += MBOX_TX_QUEUE_LEN - count;
  58. data = chan->msg_data[idx];
  59. if (chan->cl->tx_prepare)
  60. chan->cl->tx_prepare(chan->cl, data);
  61. /* Try to submit a message to the MBOX controller */
  62. err = chan->mbox->ops->send_data(chan, data);
  63. if (!err) {
  64. chan->active_req = data;
  65. chan->msg_count--;
  66. }
  67. exit:
  68. spin_unlock_irqrestore(&chan->lock, flags);
  69. if (!err && (chan->txdone_method & TXDONE_BY_POLL)) {
  70. /* kick start the timer immediately to avoid delays */
  71. spin_lock_irqsave(&chan->mbox->poll_hrt_lock, flags);
  72. hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL);
  73. spin_unlock_irqrestore(&chan->mbox->poll_hrt_lock, flags);
  74. }
  75. }
  76. static void tx_tick(struct mbox_chan *chan, int r)
  77. {
  78. unsigned long flags;
  79. void *mssg;
  80. spin_lock_irqsave(&chan->lock, flags);
  81. mssg = chan->active_req;
  82. chan->active_req = NULL;
  83. spin_unlock_irqrestore(&chan->lock, flags);
  84. /* Submit next message */
  85. msg_submit(chan);
  86. if (!mssg)
  87. return;
  88. /* Notify the client */
  89. if (chan->cl->tx_done)
  90. chan->cl->tx_done(chan->cl, mssg, r);
  91. if (r != -ETIME && chan->cl->tx_block)
  92. complete(&chan->tx_complete);
  93. }
  94. static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
  95. {
  96. struct mbox_controller *mbox =
  97. container_of(hrtimer, struct mbox_controller, poll_hrt);
  98. bool txdone, resched = false;
  99. int i;
  100. unsigned long flags;
  101. for (i = 0; i < mbox->num_chans; i++) {
  102. struct mbox_chan *chan = &mbox->chans[i];
  103. if (chan->active_req && chan->cl) {
  104. txdone = chan->mbox->ops->last_tx_done(chan);
  105. if (txdone)
  106. tx_tick(chan, 0);
  107. else
  108. resched = true;
  109. }
  110. }
  111. if (resched) {
  112. spin_lock_irqsave(&mbox->poll_hrt_lock, flags);
  113. if (!hrtimer_is_queued(hrtimer))
  114. hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
  115. spin_unlock_irqrestore(&mbox->poll_hrt_lock, flags);
  116. return HRTIMER_RESTART;
  117. }
  118. return HRTIMER_NORESTART;
  119. }
  120. /**
  121. * mbox_chan_received_data - A way for controller driver to push data
  122. * received from remote to the upper layer.
  123. * @chan: Pointer to the mailbox channel on which RX happened.
  124. * @mssg: Client specific message typecasted as void *
  125. *
  126. * After startup and before shutdown any data received on the chan
  127. * is passed on to the API via atomic mbox_chan_received_data().
  128. * The controller should ACK the RX only after this call returns.
  129. */
  130. void mbox_chan_received_data(struct mbox_chan *chan, void *mssg)
  131. {
  132. /* No buffering the received data */
  133. if (chan->cl->rx_callback)
  134. chan->cl->rx_callback(chan->cl, mssg);
  135. }
  136. EXPORT_SYMBOL_GPL(mbox_chan_received_data);
  137. /**
  138. * mbox_chan_txdone - A way for controller driver to notify the
  139. * framework that the last TX has completed.
  140. * @chan: Pointer to the mailbox chan on which TX happened.
  141. * @r: Status of last TX - OK or ERROR
  142. *
  143. * The controller that has IRQ for TX ACK calls this atomic API
  144. * to tick the TX state machine. It works only if txdone_irq
  145. * is set by the controller.
  146. */
  147. void mbox_chan_txdone(struct mbox_chan *chan, int r)
  148. {
  149. if (unlikely(!(chan->txdone_method & TXDONE_BY_IRQ))) {
  150. dev_err(chan->mbox->dev,
  151. "Controller can't run the TX ticker\n");
  152. return;
  153. }
  154. tx_tick(chan, r);
  155. }
  156. EXPORT_SYMBOL_GPL(mbox_chan_txdone);
  157. /**
  158. * mbox_client_txdone - The way for a client to run the TX state machine.
  159. * @chan: Mailbox channel assigned to this client.
  160. * @r: Success status of last transmission.
  161. *
  162. * The client/protocol had received some 'ACK' packet and it notifies
  163. * the API that the last packet was sent successfully. This only works
  164. * if the controller can't sense TX-Done.
  165. */
  166. void mbox_client_txdone(struct mbox_chan *chan, int r)
  167. {
  168. if (unlikely(!(chan->txdone_method & TXDONE_BY_ACK))) {
  169. dev_err(chan->mbox->dev, "Client can't run the TX ticker\n");
  170. return;
  171. }
  172. tx_tick(chan, r);
  173. }
  174. EXPORT_SYMBOL_GPL(mbox_client_txdone);
  175. /**
  176. * mbox_client_peek_data - A way for client driver to pull data
  177. * received from remote by the controller.
  178. * @chan: Mailbox channel assigned to this client.
  179. *
  180. * A poke to controller driver for any received data.
  181. * The data is actually passed onto client via the
  182. * mbox_chan_received_data()
  183. * The call can be made from atomic context, so the controller's
  184. * implementation of peek_data() must not sleep.
  185. *
  186. * Return: True, if controller has, and is going to push after this,
  187. * some data.
  188. * False, if controller doesn't have any data to be read.
  189. */
  190. bool mbox_client_peek_data(struct mbox_chan *chan)
  191. {
  192. if (chan->mbox->ops->peek_data)
  193. return chan->mbox->ops->peek_data(chan);
  194. return false;
  195. }
  196. EXPORT_SYMBOL_GPL(mbox_client_peek_data);
  197. /**
  198. * mbox_send_message - For client to submit a message to be
  199. * sent to the remote.
  200. * @chan: Mailbox channel assigned to this client.
  201. * @mssg: Client specific message typecasted.
  202. *
  203. * For client to submit data to the controller destined for a remote
  204. * processor. If the client had set 'tx_block', the call will return
  205. * either when the remote receives the data or when 'tx_tout' millisecs
  206. * run out.
  207. * In non-blocking mode, the requests are buffered by the API and a
  208. * non-negative token is returned for each queued request. If the request
  209. * is not queued, a negative token is returned. Upon failure or successful
  210. * TX, the API calls 'tx_done' from atomic context, from which the client
  211. * could submit yet another request.
  212. * The pointer to message should be preserved until it is sent
  213. * over the chan, i.e, tx_done() is made.
  214. * This function could be called from atomic context as it simply
  215. * queues the data and returns a token against the request.
  216. *
  217. * Return: Non-negative integer for successful submission (non-blocking mode)
  218. * or transmission over chan (blocking mode).
  219. * Negative value denotes failure.
  220. */
  221. int mbox_send_message(struct mbox_chan *chan, void *mssg)
  222. {
  223. int t;
  224. if (!chan || !chan->cl)
  225. return -EINVAL;
  226. t = add_to_rbuf(chan, mssg);
  227. if (t < 0) {
  228. dev_err(chan->mbox->dev, "Try increasing MBOX_TX_QUEUE_LEN\n");
  229. return t;
  230. }
  231. msg_submit(chan);
  232. if (chan->cl->tx_block) {
  233. unsigned long wait;
  234. int ret;
  235. if (!chan->cl->tx_tout) /* wait forever */
  236. wait = msecs_to_jiffies(3600000);
  237. else
  238. wait = msecs_to_jiffies(chan->cl->tx_tout);
  239. ret = wait_for_completion_timeout(&chan->tx_complete, wait);
  240. if (ret == 0) {
  241. t = -ETIME;
  242. tx_tick(chan, t);
  243. }
  244. }
  245. return t;
  246. }
  247. EXPORT_SYMBOL_GPL(mbox_send_message);
  248. /**
  249. * mbox_flush - flush a mailbox channel
  250. * @chan: mailbox channel to flush
  251. * @timeout: time, in milliseconds, to allow the flush operation to succeed
  252. *
  253. * Mailbox controllers that need to work in atomic context can implement the
  254. * ->flush() callback to busy loop until a transmission has been completed.
  255. * The implementation must call mbox_chan_txdone() upon success. Clients can
  256. * call the mbox_flush() function at any time after mbox_send_message() to
  257. * flush the transmission. After the function returns success, the mailbox
  258. * transmission is guaranteed to have completed.
  259. *
  260. * Returns: 0 on success or a negative error code on failure.
  261. */
  262. int mbox_flush(struct mbox_chan *chan, unsigned long timeout)
  263. {
  264. int ret;
  265. if (!chan->mbox->ops->flush)
  266. return -ENOTSUPP;
  267. ret = chan->mbox->ops->flush(chan, timeout);
  268. if (ret < 0)
  269. tx_tick(chan, ret);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(mbox_flush);
  273. static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
  274. {
  275. struct device *dev = cl->dev;
  276. unsigned long flags;
  277. int ret;
  278. if (chan->cl || !try_module_get(chan->mbox->dev->driver->owner)) {
  279. dev_dbg(dev, "%s: mailbox not free\n", __func__);
  280. return -EBUSY;
  281. }
  282. spin_lock_irqsave(&chan->lock, flags);
  283. chan->msg_free = 0;
  284. chan->msg_count = 0;
  285. chan->active_req = NULL;
  286. chan->cl = cl;
  287. init_completion(&chan->tx_complete);
  288. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  289. chan->txdone_method = TXDONE_BY_ACK;
  290. spin_unlock_irqrestore(&chan->lock, flags);
  291. if (chan->mbox->ops->startup) {
  292. ret = chan->mbox->ops->startup(chan);
  293. if (ret) {
  294. dev_err(dev, "Unable to startup the chan (%d)\n", ret);
  295. mbox_free_channel(chan);
  296. return ret;
  297. }
  298. }
  299. return 0;
  300. }
  301. /**
  302. * mbox_bind_client - Request a mailbox channel.
  303. * @chan: The mailbox channel to bind the client to.
  304. * @cl: Identity of the client requesting the channel.
  305. *
  306. * The Client specifies its requirements and capabilities while asking for
  307. * a mailbox channel. It can't be called from atomic context.
  308. * The channel is exclusively allocated and can't be used by another
  309. * client before the owner calls mbox_free_channel.
  310. * After assignment, any packet received on this channel will be
  311. * handed over to the client via the 'rx_callback'.
  312. * The framework holds reference to the client, so the mbox_client
  313. * structure shouldn't be modified until the mbox_free_channel returns.
  314. *
  315. * Return: 0 if the channel was assigned to the client successfully.
  316. * <0 for request failure.
  317. */
  318. int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
  319. {
  320. int ret;
  321. mutex_lock(&con_mutex);
  322. ret = __mbox_bind_client(chan, cl);
  323. mutex_unlock(&con_mutex);
  324. return ret;
  325. }
  326. EXPORT_SYMBOL_GPL(mbox_bind_client);
  327. /**
  328. * mbox_request_channel - Request a mailbox channel.
  329. * @cl: Identity of the client requesting the channel.
  330. * @index: Index of mailbox specifier in 'mboxes' property.
  331. *
  332. * The Client specifies its requirements and capabilities while asking for
  333. * a mailbox channel. It can't be called from atomic context.
  334. * The channel is exclusively allocated and can't be used by another
  335. * client before the owner calls mbox_free_channel.
  336. * After assignment, any packet received on this channel will be
  337. * handed over to the client via the 'rx_callback'.
  338. * The framework holds reference to the client, so the mbox_client
  339. * structure shouldn't be modified until the mbox_free_channel returns.
  340. *
  341. * Return: Pointer to the channel assigned to the client if successful.
  342. * ERR_PTR for request failure.
  343. */
  344. struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
  345. {
  346. struct device *dev = cl->dev;
  347. struct mbox_controller *mbox;
  348. struct of_phandle_args spec;
  349. struct mbox_chan *chan;
  350. int ret;
  351. if (!dev || !dev->of_node) {
  352. pr_debug("%s: No owner device node\n", __func__);
  353. return ERR_PTR(-ENODEV);
  354. }
  355. mutex_lock(&con_mutex);
  356. if (of_parse_phandle_with_args(dev->of_node, "mboxes",
  357. "#mbox-cells", index, &spec)) {
  358. dev_dbg(dev, "%s: can't parse \"mboxes\" property\n", __func__);
  359. mutex_unlock(&con_mutex);
  360. return ERR_PTR(-ENODEV);
  361. }
  362. chan = ERR_PTR(-EPROBE_DEFER);
  363. list_for_each_entry(mbox, &mbox_cons, node)
  364. if (mbox->dev->of_node == spec.np) {
  365. chan = mbox->of_xlate(mbox, &spec);
  366. if (!IS_ERR(chan))
  367. break;
  368. }
  369. of_node_put(spec.np);
  370. if (IS_ERR(chan)) {
  371. mutex_unlock(&con_mutex);
  372. return chan;
  373. }
  374. ret = __mbox_bind_client(chan, cl);
  375. if (ret)
  376. chan = ERR_PTR(ret);
  377. mutex_unlock(&con_mutex);
  378. return chan;
  379. }
  380. EXPORT_SYMBOL_GPL(mbox_request_channel);
  381. struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
  382. const char *name)
  383. {
  384. struct device_node *np = cl->dev->of_node;
  385. int index;
  386. if (!np) {
  387. dev_err(cl->dev, "%s() currently only supports DT\n", __func__);
  388. return ERR_PTR(-EINVAL);
  389. }
  390. index = of_property_match_string(np, "mbox-names", name);
  391. if (index < 0) {
  392. dev_err(cl->dev, "%s() could not locate channel named \"%s\"\n",
  393. __func__, name);
  394. return ERR_PTR(-EINVAL);
  395. }
  396. return mbox_request_channel(cl, index);
  397. }
  398. EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
  399. /**
  400. * mbox_free_channel - The client relinquishes control of a mailbox
  401. * channel by this call.
  402. * @chan: The mailbox channel to be freed.
  403. */
  404. void mbox_free_channel(struct mbox_chan *chan)
  405. {
  406. unsigned long flags;
  407. if (!chan || !chan->cl)
  408. return;
  409. if (chan->mbox->ops->shutdown)
  410. chan->mbox->ops->shutdown(chan);
  411. /* The queued TX requests are simply aborted, no callbacks are made */
  412. spin_lock_irqsave(&chan->lock, flags);
  413. chan->cl = NULL;
  414. chan->active_req = NULL;
  415. if (chan->txdone_method == TXDONE_BY_ACK)
  416. chan->txdone_method = TXDONE_BY_POLL;
  417. module_put(chan->mbox->dev->driver->owner);
  418. spin_unlock_irqrestore(&chan->lock, flags);
  419. }
  420. EXPORT_SYMBOL_GPL(mbox_free_channel);
  421. static struct mbox_chan *
  422. of_mbox_index_xlate(struct mbox_controller *mbox,
  423. const struct of_phandle_args *sp)
  424. {
  425. int ind = sp->args[0];
  426. if (ind >= mbox->num_chans)
  427. return ERR_PTR(-EINVAL);
  428. return &mbox->chans[ind];
  429. }
  430. /**
  431. * mbox_controller_register - Register the mailbox controller
  432. * @mbox: Pointer to the mailbox controller.
  433. *
  434. * The controller driver registers its communication channels
  435. */
  436. int mbox_controller_register(struct mbox_controller *mbox)
  437. {
  438. int i, txdone;
  439. /* Sanity check */
  440. if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
  441. return -EINVAL;
  442. if (mbox->txdone_irq)
  443. txdone = TXDONE_BY_IRQ;
  444. else if (mbox->txdone_poll)
  445. txdone = TXDONE_BY_POLL;
  446. else /* It has to be ACK then */
  447. txdone = TXDONE_BY_ACK;
  448. if (txdone == TXDONE_BY_POLL) {
  449. if (!mbox->ops->last_tx_done) {
  450. dev_err(mbox->dev, "last_tx_done method is absent\n");
  451. return -EINVAL;
  452. }
  453. hrtimer_init(&mbox->poll_hrt, CLOCK_MONOTONIC,
  454. HRTIMER_MODE_REL);
  455. mbox->poll_hrt.function = txdone_hrtimer;
  456. spin_lock_init(&mbox->poll_hrt_lock);
  457. }
  458. for (i = 0; i < mbox->num_chans; i++) {
  459. struct mbox_chan *chan = &mbox->chans[i];
  460. chan->cl = NULL;
  461. chan->mbox = mbox;
  462. chan->txdone_method = txdone;
  463. spin_lock_init(&chan->lock);
  464. }
  465. if (!mbox->of_xlate)
  466. mbox->of_xlate = of_mbox_index_xlate;
  467. mutex_lock(&con_mutex);
  468. list_add_tail(&mbox->node, &mbox_cons);
  469. mutex_unlock(&con_mutex);
  470. return 0;
  471. }
  472. EXPORT_SYMBOL_GPL(mbox_controller_register);
  473. /**
  474. * mbox_controller_unregister - Unregister the mailbox controller
  475. * @mbox: Pointer to the mailbox controller.
  476. */
  477. void mbox_controller_unregister(struct mbox_controller *mbox)
  478. {
  479. int i;
  480. if (!mbox)
  481. return;
  482. mutex_lock(&con_mutex);
  483. list_del(&mbox->node);
  484. for (i = 0; i < mbox->num_chans; i++)
  485. mbox_free_channel(&mbox->chans[i]);
  486. if (mbox->txdone_poll)
  487. hrtimer_cancel(&mbox->poll_hrt);
  488. mutex_unlock(&con_mutex);
  489. }
  490. EXPORT_SYMBOL_GPL(mbox_controller_unregister);
  491. static void __devm_mbox_controller_unregister(struct device *dev, void *res)
  492. {
  493. struct mbox_controller **mbox = res;
  494. mbox_controller_unregister(*mbox);
  495. }
  496. static int devm_mbox_controller_match(struct device *dev, void *res, void *data)
  497. {
  498. struct mbox_controller **mbox = res;
  499. if (WARN_ON(!mbox || !*mbox))
  500. return 0;
  501. return *mbox == data;
  502. }
  503. /**
  504. * devm_mbox_controller_register() - managed mbox_controller_register()
  505. * @dev: device owning the mailbox controller being registered
  506. * @mbox: mailbox controller being registered
  507. *
  508. * This function adds a device-managed resource that will make sure that the
  509. * mailbox controller, which is registered using mbox_controller_register()
  510. * as part of this function, will be unregistered along with the rest of
  511. * device-managed resources upon driver probe failure or driver removal.
  512. *
  513. * Returns 0 on success or a negative error code on failure.
  514. */
  515. int devm_mbox_controller_register(struct device *dev,
  516. struct mbox_controller *mbox)
  517. {
  518. struct mbox_controller **ptr;
  519. int err;
  520. ptr = devres_alloc(__devm_mbox_controller_unregister, sizeof(*ptr),
  521. GFP_KERNEL);
  522. if (!ptr)
  523. return -ENOMEM;
  524. err = mbox_controller_register(mbox);
  525. if (err < 0) {
  526. devres_free(ptr);
  527. return err;
  528. }
  529. devres_add(dev, ptr);
  530. *ptr = mbox;
  531. return 0;
  532. }
  533. EXPORT_SYMBOL_GPL(devm_mbox_controller_register);
  534. /**
  535. * devm_mbox_controller_unregister() - managed mbox_controller_unregister()
  536. * @dev: device owning the mailbox controller being unregistered
  537. * @mbox: mailbox controller being unregistered
  538. *
  539. * This function unregisters the mailbox controller and removes the device-
  540. * managed resource that was set up to automatically unregister the mailbox
  541. * controller on driver probe failure or driver removal. It's typically not
  542. * necessary to call this function.
  543. */
  544. void devm_mbox_controller_unregister(struct device *dev, struct mbox_controller *mbox)
  545. {
  546. WARN_ON(devres_release(dev, __devm_mbox_controller_unregister,
  547. devm_mbox_controller_match, mbox));
  548. }
  549. EXPORT_SYMBOL_GPL(devm_mbox_controller_unregister);