blob: d2cb1e86863c8189adb6972bea0d9c4ebf11f708 [file] [log] [blame]
Jassi Brar2b6d83e2014-06-12 22:31:19 +05301/*
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
12#include <linux/interrupt.h>
13#include <linux/spinlock.h>
14#include <linux/mutex.h>
15#include <linux/delay.h>
16#include <linux/slab.h>
17#include <linux/err.h>
18#include <linux/module.h>
19#include <linux/device.h>
20#include <linux/bitops.h>
21#include <linux/mailbox_client.h>
22#include <linux/mailbox_controller.h>
23
Ashwin Chaugule86c22f82014-11-12 19:59:38 -050024#include "mailbox.h"
Jassi Brar2b6d83e2014-06-12 22:31:19 +053025
26static LIST_HEAD(mbox_cons);
27static DEFINE_MUTEX(con_mutex);
28
29static int add_to_rbuf(struct mbox_chan *chan, void *mssg)
30{
31 int idx;
32 unsigned long flags;
33
34 spin_lock_irqsave(&chan->lock, flags);
35
36 /* See if there is any space left */
37 if (chan->msg_count == MBOX_TX_QUEUE_LEN) {
38 spin_unlock_irqrestore(&chan->lock, flags);
39 return -ENOBUFS;
40 }
41
42 idx = chan->msg_free;
43 chan->msg_data[idx] = mssg;
44 chan->msg_count++;
45
46 if (idx == MBOX_TX_QUEUE_LEN - 1)
47 chan->msg_free = 0;
48 else
49 chan->msg_free++;
50
51 spin_unlock_irqrestore(&chan->lock, flags);
52
53 return idx;
54}
55
56static void msg_submit(struct mbox_chan *chan)
57{
58 unsigned count, idx;
59 unsigned long flags;
60 void *data;
Andrew Bresticker52a49302014-10-30 13:01:07 -070061 int err = -EBUSY;
Jassi Brar2b6d83e2014-06-12 22:31:19 +053062
Lina Iyer348b2712017-05-30 22:13:59 -060063again:
Jassi Brar2b6d83e2014-06-12 22:31:19 +053064 spin_lock_irqsave(&chan->lock, flags);
65
66 if (!chan->msg_count || chan->active_req)
67 goto exit;
68
69 count = chan->msg_count;
70 idx = chan->msg_free;
71 if (idx >= count)
72 idx -= count;
73 else
74 idx += MBOX_TX_QUEUE_LEN - count;
75
76 data = chan->msg_data[idx];
77
Sudeep Holla97b0c7b2014-11-11 18:33:01 +000078 if (chan->cl->tx_prepare)
79 chan->cl->tx_prepare(chan->cl, data);
Jassi Brar2b6d83e2014-06-12 22:31:19 +053080 /* Try to submit a message to the MBOX controller */
81 err = chan->mbox->ops->send_data(chan, data);
82 if (!err) {
83 chan->active_req = data;
84 chan->msg_count--;
85 }
86exit:
87 spin_unlock_irqrestore(&chan->lock, flags);
Andrew Bresticker52a49302014-10-30 13:01:07 -070088
Lina Iyer348b2712017-05-30 22:13:59 -060089 /*
90 * If the controller returns -EAGAIN, then it means, our spinlock
91 * here is preventing the controller from receiving its interrupt,
92 * that would help clear the controller channels that are currently
93 * blocked waiting on the interrupt response.
94 * Unlock and retry again.
95 */
96 if (err == -EAGAIN)
97 goto again;
98
Jassi Brar01340df2014-12-12 15:22:49 +053099 if (!err && (chan->txdone_method & TXDONE_BY_POLL))
Sudeep Holla0cc67942015-07-31 11:48:05 +0100100 /* kick start the timer immediately to avoid delays */
101 hrtimer_start(&chan->mbox->poll_hrt, ktime_set(0, 0),
102 HRTIMER_MODE_REL);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530103}
104
105static void tx_tick(struct mbox_chan *chan, int r)
106{
107 unsigned long flags;
108 void *mssg;
109
110 spin_lock_irqsave(&chan->lock, flags);
111 mssg = chan->active_req;
112 chan->active_req = NULL;
113 spin_unlock_irqrestore(&chan->lock, flags);
114
115 /* Submit next message */
116 msg_submit(chan);
117
118 /* Notify the client */
119 if (mssg && chan->cl->tx_done)
120 chan->cl->tx_done(chan->cl, mssg, r);
121
122 if (chan->cl->tx_block)
123 complete(&chan->tx_complete);
124}
125
Sudeep Holla0cc67942015-07-31 11:48:05 +0100126static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530127{
Sudeep Holla0cc67942015-07-31 11:48:05 +0100128 struct mbox_controller *mbox =
129 container_of(hrtimer, struct mbox_controller, poll_hrt);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530130 bool txdone, resched = false;
131 int i;
132
133 for (i = 0; i < mbox->num_chans; i++) {
134 struct mbox_chan *chan = &mbox->chans[i];
135
136 if (chan->active_req && chan->cl) {
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530137 txdone = chan->mbox->ops->last_tx_done(chan);
138 if (txdone)
139 tx_tick(chan, 0);
Andrew Bresticker52a49302014-10-30 13:01:07 -0700140 else
141 resched = true;
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530142 }
143 }
144
Sudeep Holla0cc67942015-07-31 11:48:05 +0100145 if (resched) {
146 hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period));
147 return HRTIMER_RESTART;
148 }
149 return HRTIMER_NORESTART;
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530150}
151
152/**
153 * mbox_chan_received_data - A way for controller driver to push data
154 * received from remote to the upper layer.
155 * @chan: Pointer to the mailbox channel on which RX happened.
156 * @mssg: Client specific message typecasted as void *
157 *
158 * After startup and before shutdown any data received on the chan
159 * is passed on to the API via atomic mbox_chan_received_data().
160 * The controller should ACK the RX only after this call returns.
161 */
162void mbox_chan_received_data(struct mbox_chan *chan, void *mssg)
163{
164 /* No buffering the received data */
165 if (chan->cl->rx_callback)
166 chan->cl->rx_callback(chan->cl, mssg);
167}
168EXPORT_SYMBOL_GPL(mbox_chan_received_data);
169
170/**
171 * mbox_chan_txdone - A way for controller driver to notify the
172 * framework that the last TX has completed.
173 * @chan: Pointer to the mailbox chan on which TX happened.
174 * @r: Status of last TX - OK or ERROR
175 *
176 * The controller that has IRQ for TX ACK calls this atomic API
177 * to tick the TX state machine. It works only if txdone_irq
178 * is set by the controller.
179 */
180void mbox_chan_txdone(struct mbox_chan *chan, int r)
181{
182 if (unlikely(!(chan->txdone_method & TXDONE_BY_IRQ))) {
183 dev_err(chan->mbox->dev,
184 "Controller can't run the TX ticker\n");
185 return;
186 }
187
188 tx_tick(chan, r);
189}
190EXPORT_SYMBOL_GPL(mbox_chan_txdone);
191
192/**
193 * mbox_client_txdone - The way for a client to run the TX state machine.
194 * @chan: Mailbox channel assigned to this client.
195 * @r: Success status of last transmission.
196 *
197 * The client/protocol had received some 'ACK' packet and it notifies
198 * the API that the last packet was sent successfully. This only works
199 * if the controller can't sense TX-Done.
200 */
201void mbox_client_txdone(struct mbox_chan *chan, int r)
202{
203 if (unlikely(!(chan->txdone_method & TXDONE_BY_ACK))) {
204 dev_err(chan->mbox->dev, "Client can't run the TX ticker\n");
205 return;
206 }
207
208 tx_tick(chan, r);
209}
210EXPORT_SYMBOL_GPL(mbox_client_txdone);
211
212/**
213 * mbox_client_peek_data - A way for client driver to pull data
214 * received from remote by the controller.
215 * @chan: Mailbox channel assigned to this client.
216 *
217 * A poke to controller driver for any received data.
218 * The data is actually passed onto client via the
219 * mbox_chan_received_data()
220 * The call can be made from atomic context, so the controller's
221 * implementation of peek_data() must not sleep.
222 *
223 * Return: True, if controller has, and is going to push after this,
224 * some data.
225 * False, if controller doesn't have any data to be read.
226 */
227bool mbox_client_peek_data(struct mbox_chan *chan)
228{
229 if (chan->mbox->ops->peek_data)
230 return chan->mbox->ops->peek_data(chan);
231
232 return false;
233}
234EXPORT_SYMBOL_GPL(mbox_client_peek_data);
235
236/**
237 * mbox_send_message - For client to submit a message to be
238 * sent to the remote.
239 * @chan: Mailbox channel assigned to this client.
240 * @mssg: Client specific message typecasted.
241 *
242 * For client to submit data to the controller destined for a remote
243 * processor. If the client had set 'tx_block', the call will return
244 * either when the remote receives the data or when 'tx_tout' millisecs
245 * run out.
246 * In non-blocking mode, the requests are buffered by the API and a
247 * non-negative token is returned for each queued request. If the request
248 * is not queued, a negative token is returned. Upon failure or successful
249 * TX, the API calls 'tx_done' from atomic context, from which the client
250 * could submit yet another request.
251 * The pointer to message should be preserved until it is sent
252 * over the chan, i.e, tx_done() is made.
253 * This function could be called from atomic context as it simply
254 * queues the data and returns a token against the request.
255 *
256 * Return: Non-negative integer for successful submission (non-blocking mode)
257 * or transmission over chan (blocking mode).
258 * Negative value denotes failure.
259 */
260int mbox_send_message(struct mbox_chan *chan, void *mssg)
261{
262 int t;
263
264 if (!chan || !chan->cl)
265 return -EINVAL;
266
267 t = add_to_rbuf(chan, mssg);
268 if (t < 0) {
269 dev_err(chan->mbox->dev, "Try increasing MBOX_TX_QUEUE_LEN\n");
270 return t;
271 }
272
273 msg_submit(chan);
274
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530275 if (chan->cl->tx_block && chan->active_req) {
276 unsigned long wait;
277 int ret;
278
279 if (!chan->cl->tx_tout) /* wait forever */
280 wait = msecs_to_jiffies(3600000);
281 else
282 wait = msecs_to_jiffies(chan->cl->tx_tout);
283
284 ret = wait_for_completion_timeout(&chan->tx_complete, wait);
285 if (ret == 0) {
286 t = -EIO;
287 tx_tick(chan, -EIO);
288 }
289 }
290
291 return t;
292}
293EXPORT_SYMBOL_GPL(mbox_send_message);
294
Lina Iyerc525b8d2016-08-22 13:19:26 -0600295/**
296 * mbox_send_controller_data- For client to submit a message to be
297 * sent only to the controller.
298 * @chan: Mailbox channel assigned to this client.
299 * @mssg: Client specific message typecasted.
300 *
301 * For client to submit data to the controller. There is no ACK expected
302 * from the controller. This request is not buffered in the mailbox framework.
303 *
304 * Return: Non-negative integer for successful submission (non-blocking mode)
305 * or transmission over chan (blocking mode).
306 * Negative value denotes failure.
307 */
308int mbox_send_controller_data(struct mbox_chan *chan, void *mssg)
309{
310 unsigned long flags;
311 int err;
312
313 if (!chan || !chan->cl)
314 return -EINVAL;
315
316 spin_lock_irqsave(&chan->lock, flags);
317 err = chan->mbox->ops->send_controller_data(chan, mssg);
318 spin_unlock_irqrestore(&chan->lock, flags);
319
320 return err;
321}
322EXPORT_SYMBOL(mbox_send_controller_data);
Lina Iyerfc86e1a2016-05-26 11:14:41 -0600323
324bool mbox_controller_is_idle(struct mbox_chan *chan)
325{
326 if (!chan || !chan->cl || !chan->mbox->is_idle)
327 return false;
328
329 return chan->mbox->is_idle(chan->mbox);
330}
331EXPORT_SYMBOL(mbox_controller_is_idle);
332
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530333/**
334 * mbox_request_channel - Request a mailbox channel.
335 * @cl: Identity of the client requesting the channel.
336 * @index: Index of mailbox specifier in 'mboxes' property.
337 *
338 * The Client specifies its requirements and capabilities while asking for
339 * a mailbox channel. It can't be called from atomic context.
340 * The channel is exclusively allocated and can't be used by another
341 * client before the owner calls mbox_free_channel.
342 * After assignment, any packet received on this channel will be
343 * handed over to the client via the 'rx_callback'.
344 * The framework holds reference to the client, so the mbox_client
345 * structure shouldn't be modified until the mbox_free_channel returns.
346 *
347 * Return: Pointer to the channel assigned to the client if successful.
348 * ERR_PTR for request failure.
349 */
350struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
351{
352 struct device *dev = cl->dev;
353 struct mbox_controller *mbox;
354 struct of_phandle_args spec;
355 struct mbox_chan *chan;
356 unsigned long flags;
357 int ret;
358
359 if (!dev || !dev->of_node) {
360 pr_debug("%s: No owner device node\n", __func__);
361 return ERR_PTR(-ENODEV);
362 }
363
364 mutex_lock(&con_mutex);
365
366 if (of_parse_phandle_with_args(dev->of_node, "mboxes",
367 "#mbox-cells", index, &spec)) {
368 dev_dbg(dev, "%s: can't parse \"mboxes\" property\n", __func__);
369 mutex_unlock(&con_mutex);
370 return ERR_PTR(-ENODEV);
371 }
372
Benson Leung2d805fc2015-05-04 10:36:36 -0700373 chan = ERR_PTR(-EPROBE_DEFER);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530374 list_for_each_entry(mbox, &mbox_cons, node)
375 if (mbox->dev->of_node == spec.np) {
376 chan = mbox->of_xlate(mbox, &spec);
377 break;
378 }
379
380 of_node_put(spec.np);
381
Benson Leung2d805fc2015-05-04 10:36:36 -0700382 if (IS_ERR(chan)) {
383 mutex_unlock(&con_mutex);
384 return chan;
385 }
386
387 if (chan->cl || !try_module_get(mbox->dev->driver->owner)) {
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530388 dev_dbg(dev, "%s: mailbox not free\n", __func__);
389 mutex_unlock(&con_mutex);
390 return ERR_PTR(-EBUSY);
391 }
392
393 spin_lock_irqsave(&chan->lock, flags);
394 chan->msg_free = 0;
395 chan->msg_count = 0;
396 chan->active_req = NULL;
397 chan->cl = cl;
398 init_completion(&chan->tx_complete);
399
400 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
401 chan->txdone_method |= TXDONE_BY_ACK;
402
403 spin_unlock_irqrestore(&chan->lock, flags);
404
405 ret = chan->mbox->ops->startup(chan);
406 if (ret) {
407 dev_err(dev, "Unable to startup the chan (%d)\n", ret);
408 mbox_free_channel(chan);
409 chan = ERR_PTR(ret);
410 }
411
412 mutex_unlock(&con_mutex);
413 return chan;
414}
415EXPORT_SYMBOL_GPL(mbox_request_channel);
416
Lee Jonesdfabde22015-05-11 17:08:50 +0100417struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
418 const char *name)
419{
420 struct device_node *np = cl->dev->of_node;
421 struct property *prop;
422 const char *mbox_name;
423 int index = 0;
424
425 if (!np) {
426 dev_err(cl->dev, "%s() currently only supports DT\n", __func__);
Lee Jones0c44d782016-03-23 14:43:43 +0000427 return ERR_PTR(-EINVAL);
Lee Jonesdfabde22015-05-11 17:08:50 +0100428 }
429
430 if (!of_get_property(np, "mbox-names", NULL)) {
431 dev_err(cl->dev,
432 "%s() requires an \"mbox-names\" property\n", __func__);
Lee Jones0c44d782016-03-23 14:43:43 +0000433 return ERR_PTR(-EINVAL);
Lee Jonesdfabde22015-05-11 17:08:50 +0100434 }
435
436 of_property_for_each_string(np, "mbox-names", prop, mbox_name) {
437 if (!strncmp(name, mbox_name, strlen(name)))
438 break;
439 index++;
440 }
441
442 return mbox_request_channel(cl, index);
443}
444EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
445
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530446/**
447 * mbox_free_channel - The client relinquishes control of a mailbox
448 * channel by this call.
449 * @chan: The mailbox channel to be freed.
450 */
451void mbox_free_channel(struct mbox_chan *chan)
452{
453 unsigned long flags;
454
455 if (!chan || !chan->cl)
456 return;
457
458 chan->mbox->ops->shutdown(chan);
459
460 /* The queued TX requests are simply aborted, no callbacks are made */
461 spin_lock_irqsave(&chan->lock, flags);
462 chan->cl = NULL;
463 chan->active_req = NULL;
464 if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
465 chan->txdone_method = TXDONE_BY_POLL;
466
467 module_put(chan->mbox->dev->driver->owner);
468 spin_unlock_irqrestore(&chan->lock, flags);
469}
470EXPORT_SYMBOL_GPL(mbox_free_channel);
471
472static struct mbox_chan *
473of_mbox_index_xlate(struct mbox_controller *mbox,
474 const struct of_phandle_args *sp)
475{
476 int ind = sp->args[0];
477
478 if (ind >= mbox->num_chans)
Benson Leung2d805fc2015-05-04 10:36:36 -0700479 return ERR_PTR(-EINVAL);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530480
481 return &mbox->chans[ind];
482}
483
484/**
485 * mbox_controller_register - Register the mailbox controller
486 * @mbox: Pointer to the mailbox controller.
487 *
488 * The controller driver registers its communication channels
489 */
490int mbox_controller_register(struct mbox_controller *mbox)
491{
492 int i, txdone;
493
494 /* Sanity check */
495 if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
496 return -EINVAL;
497
498 if (mbox->txdone_irq)
499 txdone = TXDONE_BY_IRQ;
500 else if (mbox->txdone_poll)
501 txdone = TXDONE_BY_POLL;
502 else /* It has to be ACK then */
503 txdone = TXDONE_BY_ACK;
504
505 if (txdone == TXDONE_BY_POLL) {
Sudeep Holla0cc67942015-07-31 11:48:05 +0100506 hrtimer_init(&mbox->poll_hrt, CLOCK_MONOTONIC,
507 HRTIMER_MODE_REL);
508 mbox->poll_hrt.function = txdone_hrtimer;
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530509 }
510
511 for (i = 0; i < mbox->num_chans; i++) {
512 struct mbox_chan *chan = &mbox->chans[i];
513
514 chan->cl = NULL;
515 chan->mbox = mbox;
516 chan->txdone_method = txdone;
517 spin_lock_init(&chan->lock);
518 }
519
520 if (!mbox->of_xlate)
521 mbox->of_xlate = of_mbox_index_xlate;
522
523 mutex_lock(&con_mutex);
524 list_add_tail(&mbox->node, &mbox_cons);
525 mutex_unlock(&con_mutex);
526
527 return 0;
528}
529EXPORT_SYMBOL_GPL(mbox_controller_register);
530
531/**
532 * mbox_controller_unregister - Unregister the mailbox controller
533 * @mbox: Pointer to the mailbox controller.
534 */
535void mbox_controller_unregister(struct mbox_controller *mbox)
536{
537 int i;
538
539 if (!mbox)
540 return;
541
542 mutex_lock(&con_mutex);
543
544 list_del(&mbox->node);
545
546 for (i = 0; i < mbox->num_chans; i++)
547 mbox_free_channel(&mbox->chans[i]);
548
549 if (mbox->txdone_poll)
Sudeep Holla0cc67942015-07-31 11:48:05 +0100550 hrtimer_cancel(&mbox->poll_hrt);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530551
552 mutex_unlock(&con_mutex);
553}
554EXPORT_SYMBOL_GPL(mbox_controller_unregister);