Jassi Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 1 | /* |
| 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 Holla | 0cc6794 | 2015-07-31 11:48:05 +0100 | [diff] [blame] | 12 | #include <linux/hrtimer.h> |
Jassi Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/completion.h> |
| 15 | |
| 16 | struct 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 Iyer | c525b8d | 2016-08-22 13:19:26 -0600 | [diff] [blame] | 27 | * @send_controller_data: |
| 28 | * Send data for the controller driver. This could be data to |
| 29 | * 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 Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 33 | * @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. |
| 52 | */ |
| 53 | struct mbox_chan_ops { |
| 54 | int (*send_data)(struct mbox_chan *chan, void *data); |
Lina Iyer | c525b8d | 2016-08-22 13:19:26 -0600 | [diff] [blame] | 55 | int (*send_controller_data)(struct mbox_chan *chan, void *data); |
Jassi Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 56 | int (*startup)(struct mbox_chan *chan); |
| 57 | void (*shutdown)(struct mbox_chan *chan); |
| 58 | bool (*last_tx_done)(struct mbox_chan *chan); |
| 59 | bool (*peek_data)(struct mbox_chan *chan); |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * struct mbox_controller - Controller of a class of communication channels |
| 64 | * @dev: Device backing this controller |
| 65 | * @ops: Operators that work on each communication chan |
| 66 | * @chans: Array of channels |
| 67 | * @num_chans: Number of channels in the 'chans' array. |
| 68 | * @txdone_irq: Indicates if the controller can report to API when |
| 69 | * the last transmitted data was read by the remote. |
| 70 | * Eg, if it has some TX ACK irq. |
| 71 | * @txdone_poll: If the controller can read but not report the TX |
| 72 | * done. Ex, some register shows the TX status but |
| 73 | * no interrupt rises. Ignored if 'txdone_irq' is set. |
| 74 | * @txpoll_period: If 'txdone_poll' is in effect, the API polls for |
| 75 | * last TX's status after these many millisecs |
| 76 | * @of_xlate: Controller driver specific mapping of channel via DT |
Lina Iyer | 0d81e94 | 2016-05-26 11:18:23 -0600 | [diff] [blame] | 77 | * @is_idle: Return if the controller is idle. |
Sudeep Holla | 0cc6794 | 2015-07-31 11:48:05 +0100 | [diff] [blame] | 78 | * @poll_hrt: API private. hrtimer used to poll for TXDONE on all |
| 79 | * channels. |
Jassi Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 80 | * @node: API private. To hook into list of controllers. |
| 81 | */ |
| 82 | struct mbox_controller { |
| 83 | struct device *dev; |
Andrew Bresticker | 05ae797 | 2015-05-04 10:36:35 -0700 | [diff] [blame] | 84 | const struct mbox_chan_ops *ops; |
Jassi Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 85 | struct mbox_chan *chans; |
| 86 | int num_chans; |
| 87 | bool txdone_irq; |
| 88 | bool txdone_poll; |
| 89 | unsigned txpoll_period; |
| 90 | struct mbox_chan *(*of_xlate)(struct mbox_controller *mbox, |
| 91 | const struct of_phandle_args *sp); |
Lina Iyer | fc86e1a | 2016-05-26 11:14:41 -0600 | [diff] [blame] | 92 | bool (*is_idle)(struct mbox_controller *mbox); |
Jassi Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 93 | /* Internal to API */ |
Sudeep Holla | 0cc6794 | 2015-07-31 11:48:05 +0100 | [diff] [blame] | 94 | struct hrtimer poll_hrt; |
Jassi Brar | 2b6d83e | 2014-06-12 22:31:19 +0530 | [diff] [blame] | 95 | struct list_head node; |
| 96 | }; |
| 97 | |
| 98 | /* |
| 99 | * The length of circular buffer for queuing messages from a client. |
| 100 | * 'msg_count' tracks the number of buffered messages while 'msg_free' |
| 101 | * is the index where the next message would be buffered. |
| 102 | * We shouldn't need it too big because every transfer is interrupt |
| 103 | * triggered and if we have lots of data to transfer, the interrupt |
| 104 | * latencies are going to be the bottleneck, not the buffer length. |
| 105 | * Besides, mbox_send_message could be called from atomic context and |
| 106 | * the client could also queue another message from the notifier 'tx_done' |
| 107 | * of the last transfer done. |
| 108 | * REVISIT: If too many platforms see the "Try increasing MBOX_TX_QUEUE_LEN" |
| 109 | * print, it needs to be taken from config option or somesuch. |
| 110 | */ |
| 111 | #define MBOX_TX_QUEUE_LEN 20 |
| 112 | |
| 113 | /** |
| 114 | * struct mbox_chan - s/w representation of a communication chan |
| 115 | * @mbox: Pointer to the parent/provider of this channel |
| 116 | * @txdone_method: Way to detect TXDone chosen by the API |
| 117 | * @cl: Pointer to the current owner of this channel |
| 118 | * @tx_complete: Transmission completion |
| 119 | * @active_req: Currently active request hook |
| 120 | * @msg_count: No. of mssg currently queued |
| 121 | * @msg_free: Index of next available mssg slot |
| 122 | * @msg_data: Hook for data packet |
| 123 | * @lock: Serialise access to the channel |
| 124 | * @con_priv: Hook for controller driver to attach private data |
| 125 | */ |
| 126 | struct mbox_chan { |
| 127 | struct mbox_controller *mbox; |
| 128 | unsigned txdone_method; |
| 129 | struct mbox_client *cl; |
| 130 | struct completion tx_complete; |
| 131 | void *active_req; |
| 132 | unsigned msg_count, msg_free; |
| 133 | void *msg_data[MBOX_TX_QUEUE_LEN]; |
| 134 | spinlock_t lock; /* Serialise access to the channel */ |
| 135 | void *con_priv; |
| 136 | }; |
| 137 | |
| 138 | int mbox_controller_register(struct mbox_controller *mbox); /* can sleep */ |
| 139 | void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */ |
| 140 | void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */ |
| 141 | void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */ |
| 142 | |
| 143 | #endif /* __MAILBOX_CONTROLLER_H */ |