blob: f685583bb99cd39918f02ab14309ba0e077116f8 [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/**
Lina Iyer3d9bd812017-09-06 11:34:01 -0600296 * mbox_write_controller_data - For client to submit a message to be
297 * written to the controller but not sent to
298 * the remote processor.
Lina Iyerc525b8d2016-08-22 13:19:26 -0600299 * @chan: Mailbox channel assigned to this client.
300 * @mssg: Client specific message typecasted.
301 *
302 * For client to submit data to the controller. There is no ACK expected
303 * from the controller. This request is not buffered in the mailbox framework.
304 *
305 * Return: Non-negative integer for successful submission (non-blocking mode)
306 * or transmission over chan (blocking mode).
307 * Negative value denotes failure.
308 */
Lina Iyer3d9bd812017-09-06 11:34:01 -0600309int mbox_write_controller_data(struct mbox_chan *chan, void *mssg)
Lina Iyerc525b8d2016-08-22 13:19:26 -0600310{
311 unsigned long flags;
312 int err;
313
314 if (!chan || !chan->cl)
315 return -EINVAL;
316
317 spin_lock_irqsave(&chan->lock, flags);
Lina Iyer3d9bd812017-09-06 11:34:01 -0600318 err = chan->mbox->ops->write_controller_data(chan, mssg);
Lina Iyerc525b8d2016-08-22 13:19:26 -0600319 spin_unlock_irqrestore(&chan->lock, flags);
320
321 return err;
322}
Lina Iyer3d9bd812017-09-06 11:34:01 -0600323EXPORT_SYMBOL(mbox_write_controller_data);
Lina Iyerfc86e1a2016-05-26 11:14:41 -0600324
325bool mbox_controller_is_idle(struct mbox_chan *chan)
326{
327 if (!chan || !chan->cl || !chan->mbox->is_idle)
328 return false;
329
330 return chan->mbox->is_idle(chan->mbox);
331}
332EXPORT_SYMBOL(mbox_controller_is_idle);
333
Lina Iyere2f42722017-07-13 12:37:50 -0600334
335void mbox_chan_debug(struct mbox_chan *chan)
336{
337 if (!chan || !chan->cl || !chan->mbox->debug)
338 return;
339
340 return chan->mbox->debug(chan);
341}
342EXPORT_SYMBOL(mbox_chan_debug);
343
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530344/**
345 * mbox_request_channel - Request a mailbox channel.
346 * @cl: Identity of the client requesting the channel.
347 * @index: Index of mailbox specifier in 'mboxes' property.
348 *
349 * The Client specifies its requirements and capabilities while asking for
350 * a mailbox channel. It can't be called from atomic context.
351 * The channel is exclusively allocated and can't be used by another
352 * client before the owner calls mbox_free_channel.
353 * After assignment, any packet received on this channel will be
354 * handed over to the client via the 'rx_callback'.
355 * The framework holds reference to the client, so the mbox_client
356 * structure shouldn't be modified until the mbox_free_channel returns.
357 *
358 * Return: Pointer to the channel assigned to the client if successful.
359 * ERR_PTR for request failure.
360 */
361struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
362{
363 struct device *dev = cl->dev;
364 struct mbox_controller *mbox;
365 struct of_phandle_args spec;
366 struct mbox_chan *chan;
367 unsigned long flags;
368 int ret;
369
370 if (!dev || !dev->of_node) {
371 pr_debug("%s: No owner device node\n", __func__);
372 return ERR_PTR(-ENODEV);
373 }
374
375 mutex_lock(&con_mutex);
376
377 if (of_parse_phandle_with_args(dev->of_node, "mboxes",
378 "#mbox-cells", index, &spec)) {
379 dev_dbg(dev, "%s: can't parse \"mboxes\" property\n", __func__);
380 mutex_unlock(&con_mutex);
381 return ERR_PTR(-ENODEV);
382 }
383
Benson Leung2d805fc2015-05-04 10:36:36 -0700384 chan = ERR_PTR(-EPROBE_DEFER);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530385 list_for_each_entry(mbox, &mbox_cons, node)
386 if (mbox->dev->of_node == spec.np) {
387 chan = mbox->of_xlate(mbox, &spec);
388 break;
389 }
390
391 of_node_put(spec.np);
392
Benson Leung2d805fc2015-05-04 10:36:36 -0700393 if (IS_ERR(chan)) {
394 mutex_unlock(&con_mutex);
395 return chan;
396 }
397
398 if (chan->cl || !try_module_get(mbox->dev->driver->owner)) {
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530399 dev_dbg(dev, "%s: mailbox not free\n", __func__);
400 mutex_unlock(&con_mutex);
401 return ERR_PTR(-EBUSY);
402 }
403
404 spin_lock_irqsave(&chan->lock, flags);
405 chan->msg_free = 0;
406 chan->msg_count = 0;
407 chan->active_req = NULL;
408 chan->cl = cl;
409 init_completion(&chan->tx_complete);
410
411 if (chan->txdone_method == TXDONE_BY_POLL && cl->knows_txdone)
412 chan->txdone_method |= TXDONE_BY_ACK;
413
414 spin_unlock_irqrestore(&chan->lock, flags);
415
416 ret = chan->mbox->ops->startup(chan);
417 if (ret) {
418 dev_err(dev, "Unable to startup the chan (%d)\n", ret);
419 mbox_free_channel(chan);
420 chan = ERR_PTR(ret);
421 }
422
423 mutex_unlock(&con_mutex);
424 return chan;
425}
426EXPORT_SYMBOL_GPL(mbox_request_channel);
427
Lee Jonesdfabde22015-05-11 17:08:50 +0100428struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
429 const char *name)
430{
431 struct device_node *np = cl->dev->of_node;
432 struct property *prop;
433 const char *mbox_name;
434 int index = 0;
435
436 if (!np) {
437 dev_err(cl->dev, "%s() currently only supports DT\n", __func__);
Lee Jones0c44d782016-03-23 14:43:43 +0000438 return ERR_PTR(-EINVAL);
Lee Jonesdfabde22015-05-11 17:08:50 +0100439 }
440
441 if (!of_get_property(np, "mbox-names", NULL)) {
442 dev_err(cl->dev,
443 "%s() requires an \"mbox-names\" property\n", __func__);
Lee Jones0c44d782016-03-23 14:43:43 +0000444 return ERR_PTR(-EINVAL);
Lee Jonesdfabde22015-05-11 17:08:50 +0100445 }
446
447 of_property_for_each_string(np, "mbox-names", prop, mbox_name) {
448 if (!strncmp(name, mbox_name, strlen(name)))
449 break;
450 index++;
451 }
452
453 return mbox_request_channel(cl, index);
454}
455EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
456
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530457/**
458 * mbox_free_channel - The client relinquishes control of a mailbox
459 * channel by this call.
460 * @chan: The mailbox channel to be freed.
461 */
462void mbox_free_channel(struct mbox_chan *chan)
463{
464 unsigned long flags;
465
466 if (!chan || !chan->cl)
467 return;
468
469 chan->mbox->ops->shutdown(chan);
470
471 /* The queued TX requests are simply aborted, no callbacks are made */
472 spin_lock_irqsave(&chan->lock, flags);
473 chan->cl = NULL;
474 chan->active_req = NULL;
475 if (chan->txdone_method == (TXDONE_BY_POLL | TXDONE_BY_ACK))
476 chan->txdone_method = TXDONE_BY_POLL;
477
478 module_put(chan->mbox->dev->driver->owner);
479 spin_unlock_irqrestore(&chan->lock, flags);
480}
481EXPORT_SYMBOL_GPL(mbox_free_channel);
482
483static struct mbox_chan *
484of_mbox_index_xlate(struct mbox_controller *mbox,
485 const struct of_phandle_args *sp)
486{
487 int ind = sp->args[0];
488
489 if (ind >= mbox->num_chans)
Benson Leung2d805fc2015-05-04 10:36:36 -0700490 return ERR_PTR(-EINVAL);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530491
492 return &mbox->chans[ind];
493}
494
495/**
496 * mbox_controller_register - Register the mailbox controller
497 * @mbox: Pointer to the mailbox controller.
498 *
499 * The controller driver registers its communication channels
500 */
501int mbox_controller_register(struct mbox_controller *mbox)
502{
503 int i, txdone;
504
505 /* Sanity check */
506 if (!mbox || !mbox->dev || !mbox->ops || !mbox->num_chans)
507 return -EINVAL;
508
509 if (mbox->txdone_irq)
510 txdone = TXDONE_BY_IRQ;
511 else if (mbox->txdone_poll)
512 txdone = TXDONE_BY_POLL;
513 else /* It has to be ACK then */
514 txdone = TXDONE_BY_ACK;
515
516 if (txdone == TXDONE_BY_POLL) {
Sudeep Holla0cc67942015-07-31 11:48:05 +0100517 hrtimer_init(&mbox->poll_hrt, CLOCK_MONOTONIC,
518 HRTIMER_MODE_REL);
519 mbox->poll_hrt.function = txdone_hrtimer;
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530520 }
521
522 for (i = 0; i < mbox->num_chans; i++) {
523 struct mbox_chan *chan = &mbox->chans[i];
524
525 chan->cl = NULL;
526 chan->mbox = mbox;
527 chan->txdone_method = txdone;
528 spin_lock_init(&chan->lock);
529 }
530
531 if (!mbox->of_xlate)
532 mbox->of_xlate = of_mbox_index_xlate;
533
534 mutex_lock(&con_mutex);
535 list_add_tail(&mbox->node, &mbox_cons);
536 mutex_unlock(&con_mutex);
537
538 return 0;
539}
540EXPORT_SYMBOL_GPL(mbox_controller_register);
541
542/**
543 * mbox_controller_unregister - Unregister the mailbox controller
544 * @mbox: Pointer to the mailbox controller.
545 */
546void mbox_controller_unregister(struct mbox_controller *mbox)
547{
548 int i;
549
550 if (!mbox)
551 return;
552
553 mutex_lock(&con_mutex);
554
555 list_del(&mbox->node);
556
557 for (i = 0; i < mbox->num_chans; i++)
558 mbox_free_channel(&mbox->chans[i]);
559
560 if (mbox->txdone_poll)
Sudeep Holla0cc67942015-07-31 11:48:05 +0100561 hrtimer_cancel(&mbox->poll_hrt);
Jassi Brar2b6d83e2014-06-12 22:31:19 +0530562
563 mutex_unlock(&con_mutex);
564}
565EXPORT_SYMBOL_GPL(mbox_controller_unregister);