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