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