1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
22b6d83e2SJassi Brar 
32b6d83e2SJassi Brar #ifndef __MAILBOX_CONTROLLER_H
42b6d83e2SJassi Brar #define __MAILBOX_CONTROLLER_H
52b6d83e2SJassi Brar 
62b6d83e2SJassi Brar #include <linux/of.h>
72b6d83e2SJassi Brar #include <linux/types.h>
80cc67945SSudeep Holla #include <linux/hrtimer.h>
92b6d83e2SJassi Brar #include <linux/device.h>
102b6d83e2SJassi Brar #include <linux/completion.h>
112b6d83e2SJassi Brar 
122b6d83e2SJassi Brar struct mbox_chan;
132b6d83e2SJassi Brar 
142b6d83e2SJassi Brar /**
152b6d83e2SJassi Brar  * struct mbox_chan_ops - methods to control mailbox channels
162b6d83e2SJassi Brar  * @send_data:	The API asks the MBOX controller driver, in atomic
172b6d83e2SJassi Brar  *		context try to transmit a message on the bus. Returns 0 if
182b6d83e2SJassi Brar  *		data is accepted for transmission, -EBUSY while rejecting
192b6d83e2SJassi Brar  *		if the remote hasn't yet read the last data sent. Actual
202b6d83e2SJassi Brar  *		transmission of data is reported by the controller via
212b6d83e2SJassi Brar  *		mbox_chan_txdone (if it has some TX ACK irq). It must not
222b6d83e2SJassi Brar  *		sleep.
23a8803d74SThierry Reding  * @flush:	Called when a client requests transmissions to be blocking but
24a8803d74SThierry Reding  *		the context doesn't allow sleeping. Typically the controller
25a8803d74SThierry Reding  *		will implement a busy loop waiting for the data to flush out.
262b6d83e2SJassi Brar  * @startup:	Called when a client requests the chan. The controller
272b6d83e2SJassi Brar  *		could ask clients for additional parameters of communication
282b6d83e2SJassi Brar  *		to be provided via client's chan_data. This call may
292b6d83e2SJassi Brar  *		block. After this call the Controller must forward any
302b6d83e2SJassi Brar  *		data received on the chan by calling mbox_chan_received_data.
312b6d83e2SJassi Brar  *		The controller may do stuff that need to sleep.
322b6d83e2SJassi Brar  * @shutdown:	Called when a client relinquishes control of a chan.
332b6d83e2SJassi Brar  *		This call may block too. The controller must not forward
342b6d83e2SJassi Brar  *		any received data anymore.
352b6d83e2SJassi Brar  *		The controller may do stuff that need to sleep.
362b6d83e2SJassi Brar  * @last_tx_done: If the controller sets 'txdone_poll', the API calls
372b6d83e2SJassi Brar  *		  this to poll status of last TX. The controller must
382b6d83e2SJassi Brar  *		  give priority to IRQ method over polling and never
392b6d83e2SJassi Brar  *		  set both txdone_poll and txdone_irq. Only in polling
402b6d83e2SJassi Brar  *		  mode 'send_data' is expected to return -EBUSY.
412b6d83e2SJassi Brar  *		  The controller may do stuff that need to sleep/block.
422b6d83e2SJassi Brar  *		  Used only if txdone_poll:=true && txdone_irq:=false
432b6d83e2SJassi Brar  * @peek_data: Atomic check for any received data. Return true if controller
442b6d83e2SJassi Brar  *		  has some data to push to the client. False otherwise.
452b6d83e2SJassi Brar  */
462b6d83e2SJassi Brar struct mbox_chan_ops {
472b6d83e2SJassi Brar 	int (*send_data)(struct mbox_chan *chan, void *data);
48a8803d74SThierry Reding 	int (*flush)(struct mbox_chan *chan, unsigned long timeout);
492b6d83e2SJassi Brar 	int (*startup)(struct mbox_chan *chan);
502b6d83e2SJassi Brar 	void (*shutdown)(struct mbox_chan *chan);
512b6d83e2SJassi Brar 	bool (*last_tx_done)(struct mbox_chan *chan);
522b6d83e2SJassi Brar 	bool (*peek_data)(struct mbox_chan *chan);
532b6d83e2SJassi Brar };
542b6d83e2SJassi Brar 
552b6d83e2SJassi Brar /**
562b6d83e2SJassi Brar  * struct mbox_controller - Controller of a class of communication channels
572b6d83e2SJassi Brar  * @dev:		Device backing this controller
582b6d83e2SJassi Brar  * @ops:		Operators that work on each communication chan
592b6d83e2SJassi Brar  * @chans:		Array of channels
602b6d83e2SJassi Brar  * @num_chans:		Number of channels in the 'chans' array.
612b6d83e2SJassi Brar  * @txdone_irq:		Indicates if the controller can report to API when
622b6d83e2SJassi Brar  *			the last transmitted data was read by the remote.
632b6d83e2SJassi Brar  *			Eg, if it has some TX ACK irq.
642b6d83e2SJassi Brar  * @txdone_poll:	If the controller can read but not report the TX
652b6d83e2SJassi Brar  *			done. Ex, some register shows the TX status but
662b6d83e2SJassi Brar  *			no interrupt rises. Ignored if 'txdone_irq' is set.
672b6d83e2SJassi Brar  * @txpoll_period:	If 'txdone_poll' is in effect, the API polls for
682b6d83e2SJassi Brar  *			last TX's status after these many millisecs
692b6d83e2SJassi Brar  * @of_xlate:		Controller driver specific mapping of channel via DT
700cc67945SSudeep Holla  * @poll_hrt:		API private. hrtimer used to poll for TXDONE on all
710cc67945SSudeep Holla  *			channels.
722b6d83e2SJassi Brar  * @node:		API private. To hook into list of controllers.
732b6d83e2SJassi Brar  */
742b6d83e2SJassi Brar struct mbox_controller {
752b6d83e2SJassi Brar 	struct device *dev;
7605ae7975SAndrew Bresticker 	const struct mbox_chan_ops *ops;
772b6d83e2SJassi Brar 	struct mbox_chan *chans;
782b6d83e2SJassi Brar 	int num_chans;
792b6d83e2SJassi Brar 	bool txdone_irq;
802b6d83e2SJassi Brar 	bool txdone_poll;
812b6d83e2SJassi Brar 	unsigned txpoll_period;
822b6d83e2SJassi Brar 	struct mbox_chan *(*of_xlate)(struct mbox_controller *mbox,
832b6d83e2SJassi Brar 				      const struct of_phandle_args *sp);
842b6d83e2SJassi Brar 	/* Internal to API */
850cc67945SSudeep Holla 	struct hrtimer poll_hrt;
86119f9920SBjörn Ardö 	spinlock_t poll_hrt_lock;
872b6d83e2SJassi Brar 	struct list_head node;
882b6d83e2SJassi Brar };
892b6d83e2SJassi Brar 
902b6d83e2SJassi Brar /*
912b6d83e2SJassi Brar  * The length of circular buffer for queuing messages from a client.
922b6d83e2SJassi Brar  * 'msg_count' tracks the number of buffered messages while 'msg_free'
932b6d83e2SJassi Brar  * is the index where the next message would be buffered.
942b6d83e2SJassi Brar  * We shouldn't need it too big because every transfer is interrupt
952b6d83e2SJassi Brar  * triggered and if we have lots of data to transfer, the interrupt
962b6d83e2SJassi Brar  * latencies are going to be the bottleneck, not the buffer length.
972b6d83e2SJassi Brar  * Besides, mbox_send_message could be called from atomic context and
982b6d83e2SJassi Brar  * the client could also queue another message from the notifier 'tx_done'
992b6d83e2SJassi Brar  * of the last transfer done.
1002b6d83e2SJassi Brar  * REVISIT: If too many platforms see the "Try increasing MBOX_TX_QUEUE_LEN"
1012b6d83e2SJassi Brar  * print, it needs to be taken from config option or somesuch.
1022b6d83e2SJassi Brar  */
1032b6d83e2SJassi Brar #define MBOX_TX_QUEUE_LEN	20
1042b6d83e2SJassi Brar 
1052b6d83e2SJassi Brar /**
1062b6d83e2SJassi Brar  * struct mbox_chan - s/w representation of a communication chan
1072b6d83e2SJassi Brar  * @mbox:		Pointer to the parent/provider of this channel
1082b6d83e2SJassi Brar  * @txdone_method:	Way to detect TXDone chosen by the API
1092b6d83e2SJassi Brar  * @cl:			Pointer to the current owner of this channel
1102b6d83e2SJassi Brar  * @tx_complete:	Transmission completion
1112b6d83e2SJassi Brar  * @active_req:		Currently active request hook
1122b6d83e2SJassi Brar  * @msg_count:		No. of mssg currently queued
1132b6d83e2SJassi Brar  * @msg_free:		Index of next available mssg slot
1142b6d83e2SJassi Brar  * @msg_data:		Hook for data packet
1152b6d83e2SJassi Brar  * @lock:		Serialise access to the channel
1162b6d83e2SJassi Brar  * @con_priv:		Hook for controller driver to attach private data
1172b6d83e2SJassi Brar  */
1182b6d83e2SJassi Brar struct mbox_chan {
1192b6d83e2SJassi Brar 	struct mbox_controller *mbox;
1202b6d83e2SJassi Brar 	unsigned txdone_method;
1212b6d83e2SJassi Brar 	struct mbox_client *cl;
1222b6d83e2SJassi Brar 	struct completion tx_complete;
1232b6d83e2SJassi Brar 	void *active_req;
1242b6d83e2SJassi Brar 	unsigned msg_count, msg_free;
1252b6d83e2SJassi Brar 	void *msg_data[MBOX_TX_QUEUE_LEN];
1262b6d83e2SJassi Brar 	spinlock_t lock; /* Serialise access to the channel */
1272b6d83e2SJassi Brar 	void *con_priv;
1282b6d83e2SJassi Brar };
1292b6d83e2SJassi Brar 
1302b6d83e2SJassi Brar int mbox_controller_register(struct mbox_controller *mbox); /* can sleep */
1312b6d83e2SJassi Brar void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */
1322b6d83e2SJassi Brar void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */
1332b6d83e2SJassi Brar void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */
1342b6d83e2SJassi Brar 
135e898d9cdSThierry Reding int devm_mbox_controller_register(struct device *dev,
136e898d9cdSThierry Reding 				  struct mbox_controller *mbox);
137e898d9cdSThierry Reding void devm_mbox_controller_unregister(struct device *dev,
138e898d9cdSThierry Reding 				     struct mbox_controller *mbox);
139e898d9cdSThierry Reding 
1402b6d83e2SJassi Brar #endif /* __MAILBOX_CONTROLLER_H */
141