mailbox.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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/cleanup.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/mailbox_client.h>
  13. #include <linux/mailbox_controller.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/property.h>
  18. #include <linux/spinlock.h>
  19. #include "mailbox.h"
  20. static LIST_HEAD(mbox_cons);
  21. static DEFINE_MUTEX(con_mutex);
  22. static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
  23. {
  24. int idx;
  25. guard(spinlock_irqsave)(&chan->lock);
  26. /* See if there is any space left */
  27. if (chan->msg_count == MBOX_TX_QUEUE_LEN)
  28. return -ENOBUFS;
  29. idx = chan->msg_free;
  30. chan->msg_data[idx] = mssg;
  31. chan->msg_count++;
  32. if (idx == MBOX_TX_QUEUE_LEN - 1)
  33. chan->msg_free = 0;
  34. else
  35. chan->msg_free++;
  36. return idx;
  37. }
  38. static void msg_submit(struct mbox_chan *chan)
  39. {
  40. unsigned count, idx;
  41. void *data;
  42. int err = -EBUSY;
  43. scoped_guard(spinlock_irqsave, &chan->lock) {
  44. if (!chan->msg_count || chan->active_req)
  45. break;
  46. count = chan->msg_count;
  47. idx = chan->msg_free;
  48. if (idx >= count)
  49. idx -= count;
  50. else
  51. idx += MBOX_TX_QUEUE_LEN - count;
  52. data = chan->msg_data[idx];
  53. if (chan->cl->tx_prepare)
  54. chan->cl->tx_prepare(chan->cl, data);
  55. /* Try to submit a message to the MBOX controller */
  56. err = chan->mbox->ops->send_data(chan, data);
  57. if (!err) {
  58. chan->active_req = data;
  59. chan->msg_count--;
  60. }
  61. }
  62. if (!err && (chan->txdone_method & TXDONE_BY_POLL)) {
  63. /* kick start the timer immediately to avoid delays */
  64. scoped_guard(spinlock_irqsave, &chan->mbox->poll_hrt_lock)
  65. hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL);
  66. }
  67. }
  68. static void tx_tick(struct mbox_chan *chan, int r)
  69. {
  70. void *mssg;
  71. scoped_guard(spinlock_irqsave, &chan->lock) {
  72. mssg = chan->active_req;
  73. chan->active_req = NULL;
  74. }
  75. /* Submit next message */
  76. msg_submit(chan);
  77. if (!mssg)
  78. return;
  79. /* Notify the client */
  80. if (chan->cl->tx_done)
  81. chan->cl->tx_done(chan->cl, mssg, r);
  82. if (r != -ETIME && chan->cl->tx_block)
  83. complete(&chan->tx_complete);
  84. }
  85. static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
  86. {
  87. struct mbox_controller *mbox =
  88. container_of(hrtimer, struct mbox_controller, poll_hrt);
  89. bool txdone, resched = false;
  90. int i;
  91. for (i = 0; i < mbox->num_chans; i++) {
  92. struct mbox_chan *chan = &mbox->chans[i];
  93. if (chan->active_req && chan->cl) {
  94. txdone = chan->mbox->ops->last_tx_done(chan);
  95. if (txdone)
  96. tx_tick(chan, 0);
  97. else
  98. resched = true;
  99. }
  100. }
  101. if (resched) {
  102. scoped_guard(spinlock_irqsave, &mbox->poll_hrt_lock) {
  103. if (!hrtimer_is_queued(hrtimer))
  104. hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
  105. }
  106. return HRTIMER_RESTART;
  107. }
  108. return HRTIMER_NORESTART;
  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) {
  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 = -ETIME;
  232. tx_tick(chan, t);
  233. }
  234. }
  235. return t;
  236. }
  237. EXPORT_SYMBOL_GPL(mbox_send_message);
  238. /**
  239. * mbox_flush - flush a mailbox channel
  240. * @chan: mailbox channel to flush
  241. * @timeout: time, in milliseconds, to allow the flush operation to succeed
  242. *
  243. * Mailbox controllers that need to work in atomic context can implement the
  244. * ->flush() callback to busy loop until a transmission has been completed.
  245. * The implementation must call mbox_chan_txdone() upon success. Clients can
  246. * call the mbox_flush() function at any time after mbox_send_message() to
  247. * flush the transmission. After the function returns success, the mailbox
  248. * transmission is guaranteed to have completed.
  249. *
  250. * Returns: 0 on success or a negative error code on failure.
  251. */
  252. int mbox_flush(struct mbox_chan *chan, unsigned long timeout)
  253. {
  254. int ret;
  255. if (!chan->mbox->ops->flush)
  256. return -ENOTSUPP;
  257. ret = chan->mbox->ops->flush(chan, timeout);
  258. if (ret < 0)
  259. tx_tick(chan, ret);
  260. return ret;
  261. }
  262. EXPORT_SYMBOL_GPL(mbox_flush);
  263. static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
  264. {
  265. struct device *dev = cl->dev;
  266. int ret;
  267. if (chan->cl || !try_module_get(chan->mbox->dev->driver->owner)) {
  268. dev_err(dev, "%s: mailbox not free\n", __func__);
  269. return -EBUSY;
  270. }
  271. scoped_guard(spinlock_irqsave, &chan->lock) {
  272. chan->msg_free = 0;
  273. chan->msg_count = 0;
  274. chan->active_req = NULL;
  275. chan->cl = cl;
  276. init_completion(&chan->tx_complete);
  277. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  278. chan->txdone_method = TXDONE_BY_ACK;
  279. }
  280. if (chan->mbox->ops->startup) {
  281. ret = chan->mbox->ops->startup(chan);
  282. if (ret) {
  283. dev_err(dev, "Unable to startup the chan (%d)\n", ret);
  284. mbox_free_channel(chan);
  285. return ret;
  286. }
  287. }
  288. return 0;
  289. }
  290. /**
  291. * mbox_bind_client - Request a mailbox channel.
  292. * @chan: The mailbox channel to bind the client to.
  293. * @cl: Identity of the client requesting the channel.
  294. *
  295. * The Client specifies its requirements and capabilities while asking for
  296. * a mailbox channel. It can't be called from atomic context.
  297. * The channel is exclusively allocated and can't be used by another
  298. * client before the owner calls mbox_free_channel.
  299. * After assignment, any packet received on this channel will be
  300. * handed over to the client via the 'rx_callback'.
  301. * The framework holds reference to the client, so the mbox_client
  302. * structure shouldn't be modified until the mbox_free_channel returns.
  303. *
  304. * Return: 0 if the channel was assigned to the client successfully.
  305. * <0 for request failure.
  306. */
  307. int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
  308. {
  309. guard(mutex)(&con_mutex);
  310. return __mbox_bind_client(chan, cl);
  311. }
  312. EXPORT_SYMBOL_GPL(mbox_bind_client);
  313. /**
  314. * mbox_request_channel - Request a mailbox channel.
  315. * @cl: Identity of the client requesting the channel.
  316. * @index: Index of mailbox specifier in 'mboxes' property.
  317. *
  318. * The Client specifies its requirements and capabilities while asking for
  319. * a mailbox channel. It can't be called from atomic context.
  320. * The channel is exclusively allocated and can't be used by another
  321. * client before the owner calls mbox_free_channel.
  322. * After assignment, any packet received on this channel will be
  323. * handed over to the client via the 'rx_callback'.
  324. * The framework holds reference to the client, so the mbox_client
  325. * structure shouldn't be modified until the mbox_free_channel returns.
  326. *
  327. * Return: Pointer to the channel assigned to the client if successful.
  328. * ERR_PTR for request failure.
  329. */
  330. struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
  331. {
  332. struct fwnode_reference_args fwspec;
  333. struct fwnode_handle *fwnode;
  334. struct mbox_controller *mbox;
  335. struct of_phandle_args spec;
  336. struct mbox_chan *chan;
  337. struct device *dev;
  338. unsigned int i;
  339. int ret;
  340. dev = cl->dev;
  341. if (!dev) {
  342. pr_debug("No owner device\n");
  343. return ERR_PTR(-ENODEV);
  344. }
  345. fwnode = dev_fwnode(dev);
  346. if (!fwnode) {
  347. dev_dbg(dev, "No owner fwnode\n");
  348. return ERR_PTR(-ENODEV);
  349. }
  350. ret = fwnode_property_get_reference_args(fwnode, "mboxes", "#mbox-cells",
  351. 0, index, &fwspec);
  352. if (ret) {
  353. dev_err(dev, "%s: can't parse \"%s\" property\n", __func__, "mboxes");
  354. return ERR_PTR(ret);
  355. }
  356. spec.np = to_of_node(fwspec.fwnode);
  357. spec.args_count = fwspec.nargs;
  358. for (i = 0; i < spec.args_count; i++)
  359. spec.args[i] = fwspec.args[i];
  360. scoped_guard(mutex, &con_mutex) {
  361. chan = ERR_PTR(-EPROBE_DEFER);
  362. list_for_each_entry(mbox, &mbox_cons, node) {
  363. if (device_match_fwnode(mbox->dev, fwspec.fwnode)) {
  364. if (mbox->fw_xlate) {
  365. chan = mbox->fw_xlate(mbox, &fwspec);
  366. if (!IS_ERR(chan))
  367. break;
  368. } else if (mbox->of_xlate) {
  369. chan = mbox->of_xlate(mbox, &spec);
  370. if (!IS_ERR(chan))
  371. break;
  372. }
  373. }
  374. }
  375. fwnode_handle_put(fwspec.fwnode);
  376. if (IS_ERR(chan))
  377. return chan;
  378. ret = __mbox_bind_client(chan, cl);
  379. if (ret)
  380. chan = ERR_PTR(ret);
  381. }
  382. return chan;
  383. }
  384. EXPORT_SYMBOL_GPL(mbox_request_channel);
  385. struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
  386. const char *name)
  387. {
  388. int index = device_property_match_string(cl->dev, "mbox-names", name);
  389. if (index < 0) {
  390. dev_err(cl->dev, "%s() could not locate channel named \"%s\"\n",
  391. __func__, name);
  392. return ERR_PTR(index);
  393. }
  394. return mbox_request_channel(cl, index);
  395. }
  396. EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
  397. /**
  398. * mbox_free_channel - The client relinquishes control of a mailbox
  399. * channel by this call.
  400. * @chan: The mailbox channel to be freed.
  401. */
  402. void mbox_free_channel(struct mbox_chan *chan)
  403. {
  404. if (!chan || !chan->cl)
  405. return;
  406. if (chan->mbox->ops->shutdown)
  407. chan->mbox->ops->shutdown(chan);
  408. /* The queued TX requests are simply aborted, no callbacks are made */
  409. scoped_guard(spinlock_irqsave, &chan->lock) {
  410. chan->cl = NULL;
  411. chan->active_req = NULL;
  412. if (chan->txdone_method == TXDONE_BY_ACK)
  413. chan->txdone_method = TXDONE_BY_POLL;
  414. }
  415. module_put(chan->mbox->dev->driver->owner);
  416. }
  417. EXPORT_SYMBOL_GPL(mbox_free_channel);
  418. static struct mbox_chan *fw_mbox_index_xlate(struct mbox_controller *mbox,
  419. const struct fwnode_reference_args *sp)
  420. {
  421. if (sp->nargs < 1 || sp->args[0] >= mbox->num_chans)
  422. return ERR_PTR(-EINVAL);
  423. return &mbox->chans[sp->args[0]];
  424. }
  425. /**
  426. * mbox_controller_register - Register the mailbox controller
  427. * @mbox: Pointer to the mailbox controller.
  428. *
  429. * The controller driver registers its communication channels
  430. */
  431. int mbox_controller_register(struct mbox_controller *mbox)
  432. {
  433. int i, txdone;
  434. /* Sanity check */
  435. if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
  436. return -EINVAL;
  437. if (mbox->txdone_irq)
  438. txdone = TXDONE_BY_IRQ;
  439. else if (mbox->txdone_poll)
  440. txdone = TXDONE_BY_POLL;
  441. else /* It has to be ACK then */
  442. txdone = TXDONE_BY_ACK;
  443. if (txdone == TXDONE_BY_POLL) {
  444. if (!mbox->ops->last_tx_done) {
  445. dev_err(mbox->dev, "last_tx_done method is absent\n");
  446. return -EINVAL;
  447. }
  448. hrtimer_setup(&mbox->poll_hrt, txdone_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  449. spin_lock_init(&mbox->poll_hrt_lock);
  450. }
  451. for (i = 0; i < mbox->num_chans; i++) {
  452. struct mbox_chan *chan = &mbox->chans[i];
  453. chan->cl = NULL;
  454. chan->mbox = mbox;
  455. chan->txdone_method = txdone;
  456. spin_lock_init(&chan->lock);
  457. }
  458. if (!mbox->fw_xlate && !mbox->of_xlate)
  459. mbox->fw_xlate = fw_mbox_index_xlate;
  460. scoped_guard(mutex, &con_mutex)
  461. list_add_tail(&mbox->node, &mbox_cons);
  462. return 0;
  463. }
  464. EXPORT_SYMBOL_GPL(mbox_controller_register);
  465. /**
  466. * mbox_controller_unregister - Unregister the mailbox controller
  467. * @mbox: Pointer to the mailbox controller.
  468. */
  469. void mbox_controller_unregister(struct mbox_controller *mbox)
  470. {
  471. int i;
  472. if (!mbox)
  473. return;
  474. scoped_guard(mutex, &con_mutex) {
  475. list_del(&mbox->node);
  476. for (i = 0; i < mbox->num_chans; i++)
  477. mbox_free_channel(&mbox->chans[i]);
  478. if (mbox->txdone_poll)
  479. hrtimer_cancel(&mbox->poll_hrt);
  480. }
  481. }
  482. EXPORT_SYMBOL_GPL(mbox_controller_unregister);
  483. static void __devm_mbox_controller_unregister(struct device *dev, void *res)
  484. {
  485. struct mbox_controller **mbox = res;
  486. mbox_controller_unregister(*mbox);
  487. }
  488. /**
  489. * devm_mbox_controller_register() - managed mbox_controller_register()
  490. * @dev: device owning the mailbox controller being registered
  491. * @mbox: mailbox controller being registered
  492. *
  493. * This function adds a device-managed resource that will make sure that the
  494. * mailbox controller, which is registered using mbox_controller_register()
  495. * as part of this function, will be unregistered along with the rest of
  496. * device-managed resources upon driver probe failure or driver removal.
  497. *
  498. * Returns 0 on success or a negative error code on failure.
  499. */
  500. int devm_mbox_controller_register(struct device *dev,
  501. struct mbox_controller *mbox)
  502. {
  503. struct mbox_controller **ptr;
  504. int err;
  505. ptr = devres_alloc(__devm_mbox_controller_unregister, sizeof(*ptr),
  506. GFP_KERNEL);
  507. if (!ptr)
  508. return -ENOMEM;
  509. err = mbox_controller_register(mbox);
  510. if (err < 0) {
  511. devres_free(ptr);
  512. return err;
  513. }
  514. devres_add(dev, ptr);
  515. *ptr = mbox;
  516. return 0;
  517. }
  518. EXPORT_SYMBOL_GPL(devm_mbox_controller_register);