blob: 751b35407166ebf78a1fdeb358f6871d684fb2d2 [file] [log] [blame]
Jassi Brar2b6d83e2014-06-12 22:31:19 +05301/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 */
6
7#ifndef __MAILBOX_CONTROLLER_H
8#define __MAILBOX_CONTROLLER_H
9
10#include <linux/of.h>
11#include <linux/types.h>
Sudeep Holla0cc67942015-07-31 11:48:05 +010012#include <linux/hrtimer.h>
Jassi Brar2b6d83e2014-06-12 22:31:19 +053013#include <linux/device.h>
14#include <linux/completion.h>
15
16struct mbox_chan;
17
18/**
19 * struct mbox_chan_ops - methods to control mailbox channels
20 * @send_data: The API asks the MBOX controller driver, in atomic
21 * context try to transmit a message on the bus. Returns 0 if
22 * data is accepted for transmission, -EBUSY while rejecting
23 * if the remote hasn't yet read the last data sent. Actual
24 * transmission of data is reported by the controller via
25 * mbox_chan_txdone (if it has some TX ACK irq). It must not
26 * sleep.
Lina Iyer3d9bd812017-09-06 11:34:01 -060027 * @write_controller_data:
28 * Write data for the controller driver. This could be data to
Lina Iyerc525b8d2016-08-22 13:19:26 -060029 * configure the controller or data that may be cached in the
30 * controller and not transmitted immediately. There is no ACK
31 * for this request and the request is not buffered in the
32 * controller. Must not sleep.
Jassi Brar2b6d83e2014-06-12 22:31:19 +053033 * @startup: Called when a client requests the chan. The controller
34 * could ask clients for additional parameters of communication
35 * to be provided via client's chan_data. This call may
36 * block. After this call the Controller must forward any
37 * data received on the chan by calling mbox_chan_received_data.
38 * The controller may do stuff that need to sleep.
39 * @shutdown: Called when a client relinquishes control of a chan.
40 * This call may block too. The controller must not forward
41 * any received data anymore.
42 * The controller may do stuff that need to sleep.
43 * @last_tx_done: If the controller sets 'txdone_poll', the API calls
44 * this to poll status of last TX. The controller must
45 * give priority to IRQ method over polling and never
46 * set both txdone_poll and txdone_irq. Only in polling
47 * mode 'send_data' is expected to return -EBUSY.
48 * The controller may do stuff that need to sleep/block.
49 * Used only if txdone_poll:=true && txdone_irq:=false
50 * @peek_data: Atomic check for any received data. Return true if controller
51 * has some data to push to the client. False otherwise.
Lina Iyere2f42722017-07-13 12:37:50 -060052 * @debug: Allow chan to be debugged when the client detects a channel is
53 * locked up.
Jassi Brar2b6d83e2014-06-12 22:31:19 +053054 */
55struct mbox_chan_ops {
56 int (*send_data)(struct mbox_chan *chan, void *data);
Lina Iyer3d9bd812017-09-06 11:34:01 -060057 int (*write_controller_data)(struct mbox_chan *chan, void *data);
Jassi Brar2b6d83e2014-06-12 22:31:19 +053058 int (*startup)(struct mbox_chan *chan);
59 void (*shutdown)(struct mbox_chan *chan);
60 bool (*last_tx_done)(struct mbox_chan *chan);
61 bool (*peek_data)(struct mbox_chan *chan);
62};
63
64/**
65 * struct mbox_controller - Controller of a class of communication channels
66 * @dev: Device backing this controller
67 * @ops: Operators that work on each communication chan
68 * @chans: Array of channels
69 * @num_chans: Number of channels in the 'chans' array.
70 * @txdone_irq: Indicates if the controller can report to API when
71 * the last transmitted data was read by the remote.
72 * Eg, if it has some TX ACK irq.
73 * @txdone_poll: If the controller can read but not report the TX
74 * done. Ex, some register shows the TX status but
75 * no interrupt rises. Ignored if 'txdone_irq' is set.
76 * @txpoll_period: If 'txdone_poll' is in effect, the API polls for
77 * last TX's status after these many millisecs
78 * @of_xlate: Controller driver specific mapping of channel via DT
Lina Iyer0d81e942016-05-26 11:18:23 -060079 * @is_idle: Return if the controller is idle.
Sudeep Holla0cc67942015-07-31 11:48:05 +010080 * @poll_hrt: API private. hrtimer used to poll for TXDONE on all
81 * channels.
Jassi Brar2b6d83e2014-06-12 22:31:19 +053082 * @node: API private. To hook into list of controllers.
83 */
84struct mbox_controller {
85 struct device *dev;
Andrew Bresticker05ae7972015-05-04 10:36:35 -070086 const struct mbox_chan_ops *ops;
Jassi Brar2b6d83e2014-06-12 22:31:19 +053087 struct mbox_chan *chans;
88 int num_chans;
89 bool txdone_irq;
90 bool txdone_poll;
91 unsigned txpoll_period;
92 struct mbox_chan *(*of_xlate)(struct mbox_controller *mbox,
93 const struct of_phandle_args *sp);
Lina Iyerfc86e1a2016-05-26 11:14:41 -060094 bool (*is_idle)(struct mbox_controller *mbox);
Lina Iyere2f42722017-07-13 12:37:50 -060095 void (*debug)(struct mbox_chan *chan);
Jassi Brar2b6d83e2014-06-12 22:31:19 +053096 /* Internal to API */
Sudeep Holla0cc67942015-07-31 11:48:05 +010097 struct hrtimer poll_hrt;
Jassi Brar2b6d83e2014-06-12 22:31:19 +053098 struct list_head node;
99};
100
101/*
102 * The length of circular buffer for queuing messages from a client.
103 * 'msg_count' tracks the number of buffered messages while 'msg_free'
104 * is the index where the next message would be buffered.
105 * We shouldn't need it too big because every transfer is interrupt
106 * triggered and if we have lots of data to transfer, the interrupt
107 * latencies are going to be the bottleneck, not the buffer length.
108 * Besides, mbox_send_message could be called from atomic context and
109 * the client could also queue another message from the notifier 'tx_done'
110 * of the last transfer done.
111 * REVISIT: If too many platforms see the "Try increasing MBOX_TX_QUEUE_LEN"
112 * print, it needs to be taken from config option or somesuch.
113 */
114#define MBOX_TX_QUEUE_LEN 20
115
116/**
117 * struct mbox_chan - s/w representation of a communication chan
118 * @mbox: Pointer to the parent/provider of this channel
119 * @txdone_method: Way to detect TXDone chosen by the API
120 * @cl: Pointer to the current owner of this channel
121 * @tx_complete: Transmission completion
122 * @active_req: Currently active request hook
123 * @msg_count: No. of mssg currently queued
124 * @msg_free: Index of next available mssg slot
125 * @msg_data: Hook for data packet
126 * @lock: Serialise access to the channel
127 * @con_priv: Hook for controller driver to attach private data
128 */
129struct mbox_chan {
130 struct mbox_controller *mbox;
131 unsigned txdone_method;
132 struct mbox_client *cl;
133 struct completion tx_complete;
134 void *active_req;
135 unsigned msg_count, msg_free;
136 void *msg_data[MBOX_TX_QUEUE_LEN];
137 spinlock_t lock; /* Serialise access to the channel */
138 void *con_priv;
139};
140
141int mbox_controller_register(struct mbox_controller *mbox); /* can sleep */
142void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */
143void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */
144void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */
145
146#endif /* __MAILBOX_CONTROLLER_H */