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