mailbox.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 int add_to_rbuf(struct mbox_chan *chan, void *mssg)
  26. {
  27. int idx;
  28. unsigned long flags;
  29. spin_lock_irqsave(&chan->lock, flags);
  30. /* See if there is any space left */
  31. if (chan->msg_count == MBOX_TX_QUEUE_LEN) {
  32. spin_unlock_irqrestore(&chan->lock, flags);
  33. return -ENOBUFS;
  34. }
  35. idx = chan->msg_free;
  36. chan->msg_data[idx] = mssg;
  37. chan->msg_count++;
  38. if (idx == MBOX_TX_QUEUE_LEN - 1)
  39. chan->msg_free = 0;
  40. else
  41. chan->msg_free++;
  42. spin_unlock_irqrestore(&chan->lock, flags);
  43. return idx;
  44. }
  45. static void msg_submit(struct mbox_chan *chan)
  46. {
  47. unsigned count, idx;
  48. unsigned long flags;
  49. void *data;
  50. int err = -EBUSY;
  51. spin_lock_irqsave(&chan->lock, flags);
  52. if (!chan->msg_count || chan->active_req)
  53. goto exit;
  54. count = chan->msg_count;
  55. idx = chan->msg_free;
  56. if (idx >= count)
  57. idx -= count;
  58. else
  59. idx += MBOX_TX_QUEUE_LEN - count;
  60. data = chan->msg_data[idx];
  61. if (chan->cl->tx_prepare)
  62. chan->cl->tx_prepare(chan->cl, data);
  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. if (!err && (chan->txdone_method & TXDONE_BY_POLL))
  72. /* kick start the timer immediately to avoid delays */
  73. hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL);
  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. if (!mssg)
  86. return;
  87. /* Notify the client */
  88. if (chan->cl->tx_done)
  89. chan->cl->tx_done(chan->cl, mssg, r);
  90. if (r != -ETIME && chan->cl->tx_block)
  91. complete(&chan->tx_complete);
  92. }
  93. static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
  94. {
  95. struct mbox_controller *mbox =
  96. container_of(hrtimer, struct mbox_controller, poll_hrt);
  97. bool txdone, resched = false;
  98. int i;
  99. for (i = 0; i < mbox->num_chans; i++) {
  100. struct mbox_chan *chan = &mbox->chans[i];
  101. if (chan->active_req && chan->cl) {
  102. txdone = chan->mbox->ops->last_tx_done(chan);
  103. if (txdone)
  104. tx_tick(chan, 0);
  105. else
  106. resched = true;
  107. }
  108. }
  109. if (resched) {
  110. hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
  111. return HRTIMER_RESTART;
  112. }
  113. return HRTIMER_NORESTART;
  114. }
  115. /**
  116. * mbox_chan_received_data - A way for controller driver to push data
  117. * received from remote to the upper layer.
  118. * @chan: Pointer to the mailbox channel on which RX happened.
  119. * @mssg: Client specific message typecasted as void *
  120. *
  121. * After startup and before shutdown any data received on the chan
  122. * is passed on to the API via atomic mbox_chan_received_data().
  123. * The controller should ACK the RX only after this call returns.
  124. */
  125. void mbox_chan_received_data(struct mbox_chan *chan, void *mssg)
  126. {
  127. /* No buffering the received data */
  128. if (chan->cl->rx_callback)
  129. chan->cl->rx_callback(chan->cl, mssg);
  130. }
  131. EXPORT_SYMBOL_GPL(mbox_chan_received_data);
  132. /**
  133. * mbox_chan_txdone - A way for controller driver to notify the
  134. * framework that the last TX has completed.
  135. * @chan: Pointer to the mailbox chan on which TX happened.
  136. * @r: Status of last TX - OK or ERROR
  137. *
  138. * The controller that has IRQ for TX ACK calls this atomic API
  139. * to tick the TX state machine. It works only if txdone_irq
  140. * is set by the controller.
  141. */
  142. void mbox_chan_txdone(struct mbox_chan *chan, int r)
  143. {
  144. if (unlikely(!(chan->txdone_method & TXDONE_BY_IRQ))) {
  145. dev_err(chan->mbox->dev,
  146. "Controller can't run the TX ticker\n");
  147. return;
  148. }
  149. tx_tick(chan, r);
  150. }
  151. EXPORT_SYMBOL_GPL(mbox_chan_txdone);
  152. /**
  153. * mbox_client_txdone - The way for a client to run the TX state machine.
  154. * @chan: Mailbox channel assigned to this client.
  155. * @r: Success status of last transmission.
  156. *
  157. * The client/protocol had received some 'ACK' packet and it notifies
  158. * the API that the last packet was sent successfully. This only works
  159. * if the controller can't sense TX-Done.
  160. */
  161. void mbox_client_txdone(struct mbox_chan *chan, int r)
  162. {
  163. if (unlikely(!(chan->txdone_method & TXDONE_BY_ACK))) {
  164. dev_err(chan->mbox->dev, "Client can't run the TX ticker\n");
  165. return;
  166. }
  167. tx_tick(chan, r);
  168. }
  169. EXPORT_SYMBOL_GPL(mbox_client_txdone);
  170. /**
  171. * mbox_client_peek_data - A way for client driver to pull data
  172. * received from remote by the controller.
  173. * @chan: Mailbox channel assigned to this client.
  174. *
  175. * A poke to controller driver for any received data.
  176. * The data is actually passed onto client via the
  177. * mbox_chan_received_data()
  178. * The call can be made from atomic context, so the controller's
  179. * implementation of peek_data() must not sleep.
  180. *
  181. * Return: True, if controller has, and is going to push after this,
  182. * some data.
  183. * False, if controller doesn't have any data to be read.
  184. */
  185. bool mbox_client_peek_data(struct mbox_chan *chan)
  186. {
  187. if (chan->mbox->ops->peek_data)
  188. return chan->mbox->ops->peek_data(chan);
  189. return false;
  190. }
  191. EXPORT_SYMBOL_GPL(mbox_client_peek_data);
  192. /**
  193. * mbox_send_message - For client to submit a message to be
  194. * sent to the remote.
  195. * @chan: Mailbox channel assigned to this client.
  196. * @mssg: Client specific message typecasted.
  197. *
  198. * For client to submit data to the controller destined for a remote
  199. * processor. If the client had set 'tx_block', the call will return
  200. * either when the remote receives the data or when 'tx_tout' millisecs
  201. * run out.
  202. * In non-blocking mode, the requests are buffered by the API and a
  203. * non-negative token is returned for each queued request. If the request
  204. * is not queued, a negative token is returned. Upon failure or successful
  205. * TX, the API calls 'tx_done' from atomic context, from which the client
  206. * could submit yet another request.
  207. * The pointer to message should be preserved until it is sent
  208. * over the chan, i.e, tx_done() is made.
  209. * This function could be called from atomic context as it simply
  210. * queues the data and returns a token against the request.
  211. *
  212. * Return: Non-negative integer for successful submission (non-blocking mode)
  213. * or transmission over chan (blocking mode).
  214. * Negative value denotes failure.
  215. */
  216. int mbox_send_message(struct mbox_chan *chan, void *mssg)
  217. {
  218. int t;
  219. if (!chan || !chan->cl)
  220. return -EINVAL;
  221. t = add_to_rbuf(chan, mssg);
  222. if (t < 0) {
  223. dev_err(chan->mbox->dev, "Try increasing MBOX_TX_QUEUE_LEN\n");
  224. return t;
  225. }
  226. msg_submit(chan);
  227. if (chan->cl->tx_block) {
  228. unsigned long wait;
  229. int ret;
  230. if (!chan->cl->tx_tout) /* wait forever */
  231. wait = msecs_to_jiffies(3600000);
  232. else
  233. wait = msecs_to_jiffies(chan->cl->tx_tout);
  234. ret = wait_for_completion_timeout(&chan->tx_complete, wait);
  235. if (ret == 0) {
  236. t = -ETIME;
  237. tx_tick(chan, t);
  238. }
  239. }
  240. return t;
  241. }
  242. EXPORT_SYMBOL_GPL(mbox_send_message);
  243. #ifdef CONFIG_ACPI
  244. struct mbox_chan *mbox_request_channel_acpi(struct mbox_client *cl, int index)
  245. {
  246. struct device *dev = cl->dev;
  247. struct mbox_controller *mbox;
  248. struct fwnode_reference_args fwnode_args;
  249. struct mbox_chan *chan;
  250. unsigned long flags;
  251. int ret;
  252. if (!dev) {
  253. pr_debug("%s: No owner device node\n", __func__);
  254. return ERR_PTR(-ENODEV);
  255. }
  256. mutex_lock(&con_mutex);
  257. if (fwnode_property_get_reference_args(dev->fwnode, "mbox", NULL,
  258. 0, 0, &fwnode_args)) {
  259. dev_dbg(dev, "%s: can't parse \"mbox\" property\n", __func__);
  260. mutex_unlock(&con_mutex);
  261. return ERR_PTR(-ENODEV);
  262. }
  263. chan = ERR_PTR(-EPROBE_DEFER);
  264. list_for_each_entry(mbox, &mbox_cons, node) {
  265. if (mbox->dev->fwnode == fwnode_args.fwnode) {
  266. chan = &(mbox->chans[0]);
  267. break;
  268. }
  269. }
  270. fwnode_handle_put(fwnode_args.fwnode);
  271. if (IS_ERR(chan)) {
  272. mutex_unlock(&con_mutex);
  273. return chan;
  274. }
  275. if (chan->cl || !try_module_get(mbox->dev->driver->owner)) {
  276. dev_dbg(dev, "%s: mailbox not free\n", __func__);
  277. mutex_unlock(&con_mutex);
  278. return ERR_PTR(-EBUSY);
  279. }
  280. spin_lock_irqsave(&chan->lock, flags);
  281. chan->msg_free = 0;
  282. chan->msg_count = 0;
  283. chan->active_req = NULL;
  284. chan->cl = cl;
  285. init_completion(&chan->tx_complete);
  286. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  287. chan->txdone_method = TXDONE_BY_ACK;
  288. spin_unlock_irqrestore(&chan->lock, flags);
  289. if (chan->mbox->ops->startup) {
  290. ret = chan->mbox->ops->startup(chan);
  291. if (ret) {
  292. dev_err(dev, "Unable to startup the chan (%d)\n", ret);
  293. mbox_free_channel(chan);
  294. chan = ERR_PTR(ret);
  295. }
  296. }
  297. mutex_unlock(&con_mutex);
  298. return chan;
  299. }
  300. #else
  301. struct mbox_chan *mbox_request_channel_acpi(struct mbox_client *cl, int index)
  302. {
  303. return ERR_PTR(-ENODEV);
  304. }
  305. #endif
  306. struct mbox_chan *mbox_request_channel_dt(struct mbox_client *cl, int index)
  307. {
  308. struct device *dev = cl->dev;
  309. struct mbox_controller *mbox;
  310. struct of_phandle_args spec;
  311. struct mbox_chan *chan;
  312. unsigned long flags;
  313. int ret;
  314. if (!dev || !dev->of_node) {
  315. pr_debug("%s: No owner device node\n", __func__);
  316. return ERR_PTR(-ENODEV);
  317. }
  318. mutex_lock(&con_mutex);
  319. if (of_parse_phandle_with_args(dev->of_node, "mboxes",
  320. "#mbox-cells", index, &spec)) {
  321. dev_dbg(dev, "%s: can't parse \"mboxes\" property\n", __func__);
  322. mutex_unlock(&con_mutex);
  323. return ERR_PTR(-ENODEV);
  324. }
  325. chan = ERR_PTR(-EPROBE_DEFER);
  326. list_for_each_entry(mbox, &mbox_cons, node)
  327. if (mbox->dev->of_node == spec.np) {
  328. chan = mbox->of_xlate(mbox, &spec);
  329. break;
  330. }
  331. of_node_put(spec.np);
  332. if (IS_ERR(chan)) {
  333. mutex_unlock(&con_mutex);
  334. return chan;
  335. }
  336. if (chan->cl || !try_module_get(mbox->dev->driver->owner)) {
  337. dev_dbg(dev, "%s: mailbox not free\n", __func__);
  338. mutex_unlock(&con_mutex);
  339. return ERR_PTR(-EBUSY);
  340. }
  341. spin_lock_irqsave(&chan->lock, flags);
  342. chan->msg_free = 0;
  343. chan->msg_count = 0;
  344. chan->active_req = NULL;
  345. chan->cl = cl;
  346. init_completion(&chan->tx_complete);
  347. if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
  348. chan->txdone_method = TXDONE_BY_ACK;
  349. spin_unlock_irqrestore(&chan->lock, flags);
  350. if (chan->mbox->ops->startup) {
  351. ret = chan->mbox->ops->startup(chan);
  352. if (ret) {
  353. dev_err(dev, "Unable to startup the chan (%d)\n", ret);
  354. mbox_free_channel(chan);
  355. chan = ERR_PTR(ret);
  356. }
  357. }
  358. mutex_unlock(&con_mutex);
  359. return chan;
  360. }
  361. /**
  362. * mbox_request_channel - Request a mailbox channel.
  363. * @cl: Identity of the client requesting the channel.
  364. * @index: Index of mailbox specifier in 'mboxes' property.
  365. *
  366. * The Client specifies its requirements and capabilities while asking for
  367. * a mailbox channel. It can't be called from atomic context.
  368. * The channel is exclusively allocated and can't be used by another
  369. * client before the owner calls mbox_free_channel.
  370. * After assignment, any packet received on this channel will be
  371. * handed over to the client via the 'rx_callback'.
  372. * The framework holds reference to the client, so the mbox_client
  373. * structure shouldn't be modified until the mbox_free_channel returns.
  374. *
  375. * Return: Pointer to the channel assigned to the client if successful.
  376. * ERR_PTR for request failure.
  377. */
  378. struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
  379. {
  380. if (cl->dev && cl->dev->of_node)
  381. return mbox_request_channel_dt(cl, index);
  382. else
  383. return mbox_request_channel_acpi(cl, index);
  384. }
  385. EXPORT_SYMBOL_GPL(mbox_request_channel);
  386. struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
  387. const char *name)
  388. {
  389. struct device_node *np = cl->dev->of_node;
  390. struct property *prop;
  391. const char *mbox_name;
  392. int index = 0;
  393. if (!np) {
  394. dev_err(cl->dev, "%s() currently only supports DT\n", __func__);
  395. return ERR_PTR(-EINVAL);
  396. }
  397. if (!of_get_property(np, "mbox-names", NULL)) {
  398. dev_err(cl->dev,
  399. "%s() requires an \"mbox-names\" property\n", __func__);
  400. return ERR_PTR(-EINVAL);
  401. }
  402. of_property_for_each_string(np, "mbox-names", prop, mbox_name) {
  403. if (!strncmp(name, mbox_name, strlen(name)))
  404. return mbox_request_channel(cl, index);
  405. index++;
  406. }
  407. dev_err(cl->dev, "%s() could not locate channel named \"%s\"\n",
  408. __func__, name);
  409. return ERR_PTR(-EINVAL);
  410. }
  411. EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
  412. /**
  413. * mbox_free_channel - The client relinquishes control of a mailbox
  414. * channel by this call.
  415. * @chan: The mailbox channel to be freed.
  416. */
  417. void mbox_free_channel(struct mbox_chan *chan)
  418. {
  419. unsigned long flags;
  420. if (!chan || !chan->cl)
  421. return;
  422. if (chan->mbox->ops->shutdown)
  423. chan->mbox->ops->shutdown(chan);
  424. /* The queued TX requests are simply aborted, no callbacks are made */
  425. spin_lock_irqsave(&chan->lock, flags);
  426. chan->cl = NULL;
  427. chan->active_req = NULL;
  428. if (chan->txdone_method == TXDONE_BY_ACK)
  429. chan->txdone_method = TXDONE_BY_POLL;
  430. module_put(chan->mbox->dev->driver->owner);
  431. spin_unlock_irqrestore(&chan->lock, flags);
  432. }
  433. EXPORT_SYMBOL_GPL(mbox_free_channel);
  434. static struct mbox_chan *
  435. of_mbox_index_xlate(struct mbox_controller *mbox,
  436. const struct of_phandle_args *sp)
  437. {
  438. int ind = sp->args[0];
  439. if (ind >= mbox->num_chans)
  440. return ERR_PTR(-EINVAL);
  441. return &mbox->chans[ind];
  442. }
  443. /**
  444. * mbox_controller_register - Register the mailbox controller
  445. * @mbox: Pointer to the mailbox controller.
  446. *
  447. * The controller driver registers its communication channels
  448. */
  449. int mbox_controller_register(struct mbox_controller *mbox)
  450. {
  451. int i, txdone;
  452. /* Sanity check */
  453. if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
  454. return -EINVAL;
  455. if (mbox->txdone_irq)
  456. txdone = TXDONE_BY_IRQ;
  457. else if (mbox->txdone_poll)
  458. txdone = TXDONE_BY_POLL;
  459. else /* It has to be ACK then */
  460. txdone = TXDONE_BY_ACK;
  461. if (txdone == TXDONE_BY_POLL) {
  462. if (!mbox->ops->last_tx_done) {
  463. dev_err(mbox->dev, "last_tx_done method is absent\n");
  464. return -EINVAL;
  465. }
  466. hrtimer_init(&mbox->poll_hrt, CLOCK_MONOTONIC,
  467. HRTIMER_MODE_REL);
  468. mbox->poll_hrt.function = txdone_hrtimer;
  469. }
  470. for (i = 0; i < mbox->num_chans; i++) {
  471. struct mbox_chan *chan = &mbox->chans[i];
  472. chan->cl = NULL;
  473. chan->mbox = mbox;
  474. chan->txdone_method = txdone;
  475. spin_lock_init(&chan->lock);
  476. }
  477. if (!mbox->of_xlate)
  478. mbox->of_xlate = of_mbox_index_xlate;
  479. mutex_lock(&con_mutex);
  480. list_add_tail(&mbox->node, &mbox_cons);
  481. mutex_unlock(&con_mutex);
  482. return 0;
  483. }
  484. EXPORT_SYMBOL_GPL(mbox_controller_register);
  485. /**
  486. * mbox_controller_unregister - Unregister the mailbox controller
  487. * @mbox: Pointer to the mailbox controller.
  488. */
  489. void mbox_controller_unregister(struct mbox_controller *mbox)
  490. {
  491. int i;
  492. if (!mbox)
  493. return;
  494. mutex_lock(&con_mutex);
  495. list_del(&mbox->node);
  496. for (i = 0; i < mbox->num_chans; i++)
  497. mbox_free_channel(&mbox->chans[i]);
  498. if (mbox->txdone_poll)
  499. hrtimer_cancel(&mbox->poll_hrt);
  500. mutex_unlock(&con_mutex);
  501. }
  502. EXPORT_SYMBOL_GPL(mbox_controller_unregister);