blob: 3989bc6839d3c00b688397e447e9481d035e5a29 [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
Lina Iyere2f42722017-07-13 12:37:50 -0600333
334void mbox_chan_debug(struct mbox_chan *chan)
335{
336 if (!chan || !chan->cl || !chan->mbox->debug)
337 return;
338
339 return chan->mbox->debug(chan);
340}
341EXPORT_SYMBOL(mbox_chan_debug);
342
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530343/**
344 * mbox_request_channel - Request a mailbox channel.
345 * @cl: Identity of the client requesting the channel.
346 * @index: Index of mailbox specifier in 'mboxes' property.
347 *
348 * The Client specifies its requirements and capabilities while asking for
349 * a mailbox channel. It can't be called from atomic context.
350 * The channel is exclusively allocated and can't be used by another
351 * client before the owner calls mbox_free_channel.
352 * After assignment, any packet received on this channel will be
353 * handed over to the client via the 'rx_callback'.
354 * The framework holds reference to the client, so the mbox_client
355 * structure shouldn't be modified until the mbox_free_channel returns.
356 *
357 * Return: Pointer to the channel assigned to the client if successful.
358 * ERR_PTR for request failure.
359 */
360struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
361{
362 struct device *dev = cl->dev;
363 struct mbox_controller *mbox;
364 struct of_phandle_args spec;
365 struct mbox_chan *chan;
366 unsigned long flags;
367 int ret;
368
369 if (!dev || !dev->of_node) {
370 pr_debug("%s: No owner device node\n", __func__);
371 return ERR_PTR(-ENODEV);
372 }
373
374 mutex_lock(&con_mutex);
375
376 if (of_parse_phandle_with_args(dev->of_node, "mboxes",
377 "#mbox-cells", index, &spec)) {
378 dev_dbg(dev, "%s: can't parse \"mboxes\" property\n", __func__);
379 mutex_unlock(&con_mutex);
380 return ERR_PTR(-ENODEV);
381 }
382
Benson Leung2d805fc2015-05-04 10:36:36 -0700383 chan = ERR_PTR(-EPROBE_DEFER);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530384 list_for_each_entry(mbox, &mbox_cons, node)
385 if (mbox->dev->of_node == spec.np) {
386 chan = mbox->of_xlate(mbox, &spec);
387 break;
388 }
389
390 of_node_put(spec.np);
391
Benson Leung2d805fc2015-05-04 10:36:36 -0700392 if (IS_ERR(chan)) {
393 mutex_unlock(&con_mutex);
394 return chan;
395 }
396
397 if (chan->cl || !try_module_get(mbox->dev->driver->owner)) {
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530398 dev_dbg(dev, "%s: mailbox not free\n", __func__);
399 mutex_unlock(&con_mutex);
400 return ERR_PTR(-EBUSY);
401 }
402
403 spin_lock_irqsave(&chan->lock, flags);
404 chan->msg_free = 0;
405 chan->msg_count = 0;
406 chan->active_req = NULL;
407 chan->cl = cl;
408 init_completion(&chan->tx_complete);
409
410 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
411 chan->txdone_method |= TXDONE_BY_ACK;
412
413 spin_unlock_irqrestore(&chan->lock, flags);
414
415 ret = chan->mbox->ops->startup(chan);
416 if (ret) {
417 dev_err(dev, "Unable to startup the chan (%d)\n", ret);
418 mbox_free_channel(chan);
419 chan = ERR_PTR(ret);
420 }
421
422 mutex_unlock(&con_mutex);
423 return chan;
424}
425EXPORT_SYMBOL_GPL(mbox_request_channel);
426
Lee Jonesdfabde22015-05-11 17:08:50 +0100427struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
428 const char *name)
429{
430 struct device_node *np = cl->dev->of_node;
431 struct property *prop;
432 const char *mbox_name;
433 int index = 0;
434
435 if (!np) {
436 dev_err(cl->dev, "%s() currently only supports DT\n", __func__);
Lee Jones0c44d782016-03-23 14:43:43 +0000437 return ERR_PTR(-EINVAL);
Lee Jonesdfabde22015-05-11 17:08:50 +0100438 }
439
440 if (!of_get_property(np, "mbox-names", NULL)) {
441 dev_err(cl->dev,
442 "%s() requires an \"mbox-names\" property\n", __func__);
Lee Jones0c44d782016-03-23 14:43:43 +0000443 return ERR_PTR(-EINVAL);
Lee Jonesdfabde22015-05-11 17:08:50 +0100444 }
445
446 of_property_for_each_string(np, "mbox-names", prop, mbox_name) {
447 if (!strncmp(name, mbox_name, strlen(name)))
448 break;
449 index++;
450 }
451
452 return mbox_request_channel(cl, index);
453}
454EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
455
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530456/**
457 * mbox_free_channel - The client relinquishes control of a mailbox
458 * channel by this call.
459 * @chan: The mailbox channel to be freed.
460 */
461void mbox_free_channel(struct mbox_chan *chan)
462{
463 unsigned long flags;
464
465 if (!chan || !chan->cl)
466 return;
467
468 chan->mbox->ops->shutdown(chan);
469
470 /* The queued TX requests are simply aborted, no callbacks are made */
471 spin_lock_irqsave(&chan->lock, flags);
472 chan->cl = NULL;
473 chan->active_req = NULL;
474 if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
475 chan->txdone_method = TXDONE_BY_POLL;
476
477 module_put(chan->mbox->dev->driver->owner);
478 spin_unlock_irqrestore(&chan->lock, flags);
479}
480EXPORT_SYMBOL_GPL(mbox_free_channel);
481
482static struct mbox_chan *
483of_mbox_index_xlate(struct mbox_controller *mbox,
484 const struct of_phandle_args *sp)
485{
486 int ind = sp->args[0];
487
488 if (ind >= mbox->num_chans)
Benson Leung2d805fc2015-05-04 10:36:36 -0700489 return ERR_PTR(-EINVAL);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530490
491 return &mbox->chans[ind];
492}
493
494/**
495 * mbox_controller_register - Register the mailbox controller
496 * @mbox: Pointer to the mailbox controller.
497 *
498 * The controller driver registers its communication channels
499 */
500int mbox_controller_register(struct mbox_controller *mbox)
501{
502 int i, txdone;
503
504 /* Sanity check */
505 if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
506 return -EINVAL;
507
508 if (mbox->txdone_irq)
509 txdone = TXDONE_BY_IRQ;
510 else if (mbox->txdone_poll)
511 txdone = TXDONE_BY_POLL;
512 else /* It has to be ACK then */
513 txdone = TXDONE_BY_ACK;
514
515 if (txdone == TXDONE_BY_POLL) {
Sudeep Holla0cc67942015-07-31 11:48:05 +0100516 hrtimer_init(&mbox->poll_hrt, CLOCK_MONOTONIC,
517 HRTIMER_MODE_REL);
518 mbox->poll_hrt.function = txdone_hrtimer;
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530519 }
520
521 for (i = 0; i < mbox->num_chans; i++) {
522 struct mbox_chan *chan = &mbox->chans[i];
523
524 chan->cl = NULL;
525 chan->mbox = mbox;
526 chan->txdone_method = txdone;
527 spin_lock_init(&chan->lock);
528 }
529
530 if (!mbox->of_xlate)
531 mbox->of_xlate = of_mbox_index_xlate;
532
533 mutex_lock(&con_mutex);
534 list_add_tail(&mbox->node, &mbox_cons);
535 mutex_unlock(&con_mutex);
536
537 return 0;
538}
539EXPORT_SYMBOL_GPL(mbox_controller_register);
540
541/**
542 * mbox_controller_unregister - Unregister the mailbox controller
543 * @mbox: Pointer to the mailbox controller.
544 */
545void mbox_controller_unregister(struct mbox_controller *mbox)
546{
547 int i;
548
549 if (!mbox)
550 return;
551
552 mutex_lock(&con_mutex);
553
554 list_del(&mbox->node);
555
556 for (i = 0; i < mbox->num_chans; i++)
557 mbox_free_channel(&mbox->chans[i]);
558
559 if (mbox->txdone_poll)
Sudeep Holla0cc67942015-07-31 11:48:05 +0100560 hrtimer_cancel(&mbox->poll_hrt);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530561
562 mutex_unlock(&con_mutex);
563}
564EXPORT_SYMBOL_GPL(mbox_controller_unregister);