1 // SPDX-License-Identifier: GPL-2.0-only
3 * Mailbox: Common code for Mailbox controllers and users
5 * Copyright (C) 2013-2014 Linaro Ltd.
6 * Author: Jassi Brar <jassisinghbrar@gmail.com>
9 #include <linux/cleanup.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/mailbox_controller.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
18 #include <linux/spinlock.h>
22 static LIST_HEAD(mbox_cons);
23 static DEFINE_MUTEX(con_mutex);
25 static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
29 guard(spinlock_irqsave)(&chan->lock);
31 /* See if there is any space left */
32 if (chan->msg_count == MBOX_TX_QUEUE_LEN)
36 chan->msg_data[idx] = mssg;
39 if (idx == MBOX_TX_QUEUE_LEN - 1)
47 static void msg_submit(struct mbox_chan *chan)
53 scoped_guard(spinlock_irqsave, &chan->lock) {
54 if (!chan->msg_count || chan->active_req)
57 count = chan->msg_count;
62 idx += MBOX_TX_QUEUE_LEN - count;
64 data = chan->msg_data[idx];
66 if (chan->cl->tx_prepare)
67 chan->cl->tx_prepare(chan->cl, data);
68 /* Try to submit a message to the MBOX controller */
69 err = chan->mbox->ops->send_data(chan, data);
71 chan->active_req = data;
76 if (!err && (chan->txdone_method & TXDONE_BY_POLL)) {
77 /* kick start the timer immediately to avoid delays */
78 scoped_guard(spinlock_irqsave, &chan->mbox->poll_hrt_lock)
79 hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL);
83 static void tx_tick(struct mbox_chan *chan, int r)
87 scoped_guard(spinlock_irqsave, &chan->lock) {
88 mssg = chan->active_req;
89 chan->active_req = NULL;
92 /* Submit next message */
98 /* Notify the client */
99 if (chan->cl->tx_done)
100 chan->cl->tx_done(chan->cl, mssg, r);
102 if (r != -ETIME && chan->cl->tx_block)
103 complete(&chan->tx_complete);
106 static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
108 struct mbox_controller *mbox =
109 container_of(hrtimer, struct mbox_controller, poll_hrt);
110 bool txdone, resched = false;
113 for (i = 0; i < mbox->num_chans; i++) {
114 struct mbox_chan *chan = &mbox->chans[i];
116 if (chan->active_req && chan->cl) {
117 txdone = chan->mbox->ops->last_tx_done(chan);
126 scoped_guard(spinlock_irqsave, &mbox->poll_hrt_lock) {
127 if (!hrtimer_is_queued(hrtimer))
128 hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
131 return HRTIMER_RESTART;
133 return HRTIMER_NORESTART;
137 * mbox_chan_received_data - A way for controller driver to push data
138 * received from remote to the upper layer.
139 * @chan: Pointer to the mailbox channel on which RX happened.
140 * @mssg: Client specific message typecasted as void *
142 * After startup and before shutdown any data received on the chan
143 * is passed on to the API via atomic mbox_chan_received_data().
144 * The controller should ACK the RX only after this call returns.
146 void mbox_chan_received_data(struct mbox_chan *chan, void *mssg)
148 /* No buffering the received data */
149 if (chan->cl->rx_callback)
150 chan->cl->rx_callback(chan->cl, mssg);
152 EXPORT_SYMBOL_GPL(mbox_chan_received_data);
155 * mbox_chan_txdone - A way for controller driver to notify the
156 * framework that the last TX has completed.
157 * @chan: Pointer to the mailbox chan on which TX happened.
158 * @r: Status of last TX - OK or ERROR
160 * The controller that has IRQ for TX ACK calls this atomic API
161 * to tick the TX state machine. It works only if txdone_irq
162 * is set by the controller.
164 void mbox_chan_txdone(struct mbox_chan *chan, int r)
166 if (unlikely(!(chan->txdone_method & TXDONE_BY_IRQ))) {
167 dev_err(chan->mbox->dev,
168 "Controller can't run the TX ticker\n");
174 EXPORT_SYMBOL_GPL(mbox_chan_txdone);
177 * mbox_client_txdone - The way for a client to run the TX state machine.
178 * @chan: Mailbox channel assigned to this client.
179 * @r: Success status of last transmission.
181 * The client/protocol had received some 'ACK' packet and it notifies
182 * the API that the last packet was sent successfully. This only works
183 * if the controller can't sense TX-Done.
185 void mbox_client_txdone(struct mbox_chan *chan, int r)
187 if (unlikely(!(chan->txdone_method & TXDONE_BY_ACK))) {
188 dev_err(chan->mbox->dev, "Client can't run the TX ticker\n");
194 EXPORT_SYMBOL_GPL(mbox_client_txdone);
197 * mbox_client_peek_data - A way for client driver to pull data
198 * received from remote by the controller.
199 * @chan: Mailbox channel assigned to this client.
201 * A poke to controller driver for any received data.
202 * The data is actually passed onto client via the
203 * mbox_chan_received_data()
204 * The call can be made from atomic context, so the controller's
205 * implementation of peek_data() must not sleep.
207 * Return: True, if controller has, and is going to push after this,
209 * False, if controller doesn't have any data to be read.
211 bool mbox_client_peek_data(struct mbox_chan *chan)
213 if (chan->mbox->ops->peek_data)
214 return chan->mbox->ops->peek_data(chan);
218 EXPORT_SYMBOL_GPL(mbox_client_peek_data);
221 * mbox_send_message - For client to submit a message to be
222 * sent to the remote.
223 * @chan: Mailbox channel assigned to this client.
224 * @mssg: Client specific message typecasted.
226 * For client to submit data to the controller destined for a remote
227 * processor. If the client had set 'tx_block', the call will return
228 * either when the remote receives the data or when 'tx_tout' millisecs
230 * In non-blocking mode, the requests are buffered by the API and a
231 * non-negative token is returned for each queued request. If the request
232 * is not queued, a negative token is returned. Upon failure or successful
233 * TX, the API calls 'tx_done' from atomic context, from which the client
234 * could submit yet another request.
235 * The pointer to message should be preserved until it is sent
236 * over the chan, i.e, tx_done() is made.
237 * This function could be called from atomic context as it simply
238 * queues the data and returns a token against the request.
240 * Return: Non-negative integer for successful submission (non-blocking mode)
241 * or transmission over chan (blocking mode).
242 * Negative value denotes failure.
244 int mbox_send_message(struct mbox_chan *chan, void *mssg)
248 if (!chan || !chan->cl)
251 t = add_to_rbuf(chan, mssg);
253 dev_err(chan->mbox->dev, "Try increasing MBOX_TX_QUEUE_LEN\n");
259 if (chan->cl->tx_block) {
263 if (!chan->cl->tx_tout) /* wait forever */
264 wait = msecs_to_jiffies(3600000);
266 wait = msecs_to_jiffies(chan->cl->tx_tout);
268 ret = wait_for_completion_timeout(&chan->tx_complete, wait);
277 EXPORT_SYMBOL_GPL(mbox_send_message);
280 * mbox_flush - flush a mailbox channel
281 * @chan: mailbox channel to flush
282 * @timeout: time, in milliseconds, to allow the flush operation to succeed
284 * Mailbox controllers that need to work in atomic context can implement the
285 * ->flush() callback to busy loop until a transmission has been completed.
286 * The implementation must call mbox_chan_txdone() upon success. Clients can
287 * call the mbox_flush() function at any time after mbox_send_message() to
288 * flush the transmission. After the function returns success, the mailbox
289 * transmission is guaranteed to have completed.
291 * Returns: 0 on success or a negative error code on failure.
293 int mbox_flush(struct mbox_chan *chan, unsigned long timeout)
297 if (!chan->mbox->ops->flush)
300 ret = chan->mbox->ops->flush(chan, timeout);
306 EXPORT_SYMBOL_GPL(mbox_flush);
308 static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
310 struct device *dev = cl->dev;
313 if (chan->cl || !try_module_get(chan->mbox->dev->driver->owner)) {
314 dev_err(dev, "%s: mailbox not free\n", __func__);
318 scoped_guard(spinlock_irqsave, &chan->lock) {
321 chan->active_req = NULL;
323 init_completion(&chan->tx_complete);
325 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
326 chan->txdone_method = TXDONE_BY_ACK;
329 if (chan->mbox->ops->startup) {
330 ret = chan->mbox->ops->startup(chan);
333 dev_err(dev, "Unable to startup the chan (%d)\n", ret);
334 mbox_free_channel(chan);
343 * mbox_bind_client - Request a mailbox channel.
344 * @chan: The mailbox channel to bind the client to.
345 * @cl: Identity of the client requesting the channel.
347 * The Client specifies its requirements and capabilities while asking for
348 * a mailbox channel. It can't be called from atomic context.
349 * The channel is exclusively allocated and can't be used by another
350 * client before the owner calls mbox_free_channel.
351 * After assignment, any packet received on this channel will be
352 * handed over to the client via the 'rx_callback'.
353 * The framework holds reference to the client, so the mbox_client
354 * structure shouldn't be modified until the mbox_free_channel returns.
356 * Return: 0 if the channel was assigned to the client successfully.
357 * <0 for request failure.
359 int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
361 guard(mutex)(&con_mutex);
363 return __mbox_bind_client(chan, cl);
365 EXPORT_SYMBOL_GPL(mbox_bind_client);
368 * mbox_request_channel - Request a mailbox channel.
369 * @cl: Identity of the client requesting the channel.
370 * @index: Index of mailbox specifier in 'mboxes' property.
372 * The Client specifies its requirements and capabilities while asking for
373 * a mailbox channel. It can't be called from atomic context.
374 * The channel is exclusively allocated and can't be used by another
375 * client before the owner calls mbox_free_channel.
376 * After assignment, any packet received on this channel will be
377 * handed over to the client via the 'rx_callback'.
378 * The framework holds reference to the client, so the mbox_client
379 * structure shouldn't be modified until the mbox_free_channel returns.
381 * Return: Pointer to the channel assigned to the client if successful.
382 * ERR_PTR for request failure.
384 struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
386 struct device *dev = cl->dev;
387 struct mbox_controller *mbox;
388 struct of_phandle_args spec;
389 struct mbox_chan *chan;
392 if (!dev || !dev->of_node) {
393 pr_debug("%s: No owner device node\n", __func__);
394 return ERR_PTR(-ENODEV);
397 ret = of_parse_phandle_with_args(dev->of_node, "mboxes", "#mbox-cells",
400 dev_err(dev, "%s: can't parse \"mboxes\" property\n", __func__);
404 scoped_guard(mutex, &con_mutex) {
405 chan = ERR_PTR(-EPROBE_DEFER);
406 list_for_each_entry(mbox, &mbox_cons, node)
407 if (mbox->dev->of_node == spec.np) {
408 chan = mbox->of_xlate(mbox, &spec);
413 of_node_put(spec.np);
418 ret = __mbox_bind_client(chan, cl);
425 EXPORT_SYMBOL_GPL(mbox_request_channel);
427 struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
430 struct device_node *np = cl->dev->of_node;
434 dev_err(cl->dev, "%s() currently only supports DT\n", __func__);
435 return ERR_PTR(-EINVAL);
438 index = of_property_match_string(np, "mbox-names", name);
440 dev_err(cl->dev, "%s() could not locate channel named \"%s\"\n",
442 return ERR_PTR(index);
444 return mbox_request_channel(cl, index);
446 EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
449 * mbox_free_channel - The client relinquishes control of a mailbox
450 * channel by this call.
451 * @chan: The mailbox channel to be freed.
453 void mbox_free_channel(struct mbox_chan *chan)
455 if (!chan || !chan->cl)
458 if (chan->mbox->ops->shutdown)
459 chan->mbox->ops->shutdown(chan);
461 /* The queued TX requests are simply aborted, no callbacks are made */
462 scoped_guard(spinlock_irqsave, &chan->lock) {
464 chan->active_req = NULL;
465 if (chan->txdone_method == TXDONE_BY_ACK)
466 chan->txdone_method = TXDONE_BY_POLL;
469 module_put(chan->mbox->dev->driver->owner);
471 EXPORT_SYMBOL_GPL(mbox_free_channel);
473 static struct mbox_chan *
474 of_mbox_index_xlate(struct mbox_controller *mbox,
475 const struct of_phandle_args *sp)
477 int ind = sp->args[0];
479 if (ind >= mbox->num_chans)
480 return ERR_PTR(-EINVAL);
482 return &mbox->chans[ind];
486 * mbox_controller_register - Register the mailbox controller
487 * @mbox: Pointer to the mailbox controller.
489 * The controller driver registers its communication channels
491 int mbox_controller_register(struct mbox_controller *mbox)
496 if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
499 if (mbox->txdone_irq)
500 txdone = TXDONE_BY_IRQ;
501 else if (mbox->txdone_poll)
502 txdone = TXDONE_BY_POLL;
503 else /* It has to be ACK then */
504 txdone = TXDONE_BY_ACK;
506 if (txdone == TXDONE_BY_POLL) {
508 if (!mbox->ops->last_tx_done) {
509 dev_err(mbox->dev, "last_tx_done method is absent\n");
513 hrtimer_setup(&mbox->poll_hrt, txdone_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
514 spin_lock_init(&mbox->poll_hrt_lock);
517 for (i = 0; i < mbox->num_chans; i++) {
518 struct mbox_chan *chan = &mbox->chans[i];
522 chan->txdone_method = txdone;
523 spin_lock_init(&chan->lock);
527 mbox->of_xlate = of_mbox_index_xlate;
529 scoped_guard(mutex, &con_mutex)
530 list_add_tail(&mbox->node, &mbox_cons);
534 EXPORT_SYMBOL_GPL(mbox_controller_register);
537 * mbox_controller_unregister - Unregister the mailbox controller
538 * @mbox: Pointer to the mailbox controller.
540 void mbox_controller_unregister(struct mbox_controller *mbox)
547 scoped_guard(mutex, &con_mutex) {
548 list_del(&mbox->node);
550 for (i = 0; i < mbox->num_chans; i++)
551 mbox_free_channel(&mbox->chans[i]);
553 if (mbox->txdone_poll)
554 hrtimer_cancel(&mbox->poll_hrt);
557 EXPORT_SYMBOL_GPL(mbox_controller_unregister);
559 static void __devm_mbox_controller_unregister(struct device *dev, void *res)
561 struct mbox_controller **mbox = res;
563 mbox_controller_unregister(*mbox);
567 * devm_mbox_controller_register() - managed mbox_controller_register()
568 * @dev: device owning the mailbox controller being registered
569 * @mbox: mailbox controller being registered
571 * This function adds a device-managed resource that will make sure that the
572 * mailbox controller, which is registered using mbox_controller_register()
573 * as part of this function, will be unregistered along with the rest of
574 * device-managed resources upon driver probe failure or driver removal.
576 * Returns 0 on success or a negative error code on failure.
578 int devm_mbox_controller_register(struct device *dev,
579 struct mbox_controller *mbox)
581 struct mbox_controller **ptr;
584 ptr = devres_alloc(__devm_mbox_controller_unregister, sizeof(*ptr),
589 err = mbox_controller_register(mbox);
595 devres_add(dev, ptr);
600 EXPORT_SYMBOL_GPL(devm_mbox_controller_register);