xref: /linux/include/linux/mhi.h (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
4  *
5  */
6 #ifndef _MHI_H_
7 #define _MHI_H_
8 
9 #include <linux/device.h>
10 #include <linux/dma-direction.h>
11 #include <linux/mutex.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/wait.h>
16 #include <linux/workqueue.h>
17 #include <linux/device-id/mhi.h>
18 
19 #define MHI_MAX_OEM_PK_HASH_SEGMENTS 16
20 
21 struct mhi_chan;
22 struct mhi_event;
23 struct mhi_ctxt;
24 struct mhi_cmd;
25 struct mhi_buf_info;
26 
27 /**
28  * enum mhi_callback - MHI callback
29  * @MHI_CB_IDLE: MHI entered idle state
30  * @MHI_CB_PENDING_DATA: New data available for client to process
31  * @MHI_CB_LPM_ENTER: MHI host entered low power mode
32  * @MHI_CB_LPM_EXIT: MHI host about to exit low power mode
33  * @MHI_CB_EE_RDDM: MHI device entered RDDM exec env
34  * @MHI_CB_EE_MISSION_MODE: MHI device entered Mission Mode exec env
35  * @MHI_CB_SYS_ERROR: MHI device entered error state (may recover)
36  * @MHI_CB_FATAL_ERROR: MHI device entered fatal error state
37  * @MHI_CB_BW_REQ: Received a bandwidth switch request from device
38  */
39 enum mhi_callback {
40 	MHI_CB_IDLE,
41 	MHI_CB_PENDING_DATA,
42 	MHI_CB_LPM_ENTER,
43 	MHI_CB_LPM_EXIT,
44 	MHI_CB_EE_RDDM,
45 	MHI_CB_EE_MISSION_MODE,
46 	MHI_CB_SYS_ERROR,
47 	MHI_CB_FATAL_ERROR,
48 	MHI_CB_BW_REQ,
49 };
50 
51 /**
52  * enum mhi_flags - Transfer flags
53  * @MHI_EOB: End of buffer for bulk transfer
54  * @MHI_EOT: End of transfer
55  * @MHI_CHAIN: Linked transfer
56  */
57 enum mhi_flags {
58 	MHI_EOB = BIT(0),
59 	MHI_EOT = BIT(1),
60 	MHI_CHAIN = BIT(2),
61 };
62 
63 /**
64  * enum mhi_device_type - Device types
65  * @MHI_DEVICE_XFER: Handles data transfer
66  * @MHI_DEVICE_CONTROLLER: Control device
67  */
68 enum mhi_device_type {
69 	MHI_DEVICE_XFER,
70 	MHI_DEVICE_CONTROLLER,
71 };
72 
73 /**
74  * enum mhi_ch_type - Channel types
75  * @MHI_CH_TYPE_INVALID: Invalid channel type
76  * @MHI_CH_TYPE_OUTBOUND: Outbound channel to the device
77  * @MHI_CH_TYPE_INBOUND: Inbound channel from the device
78  * @MHI_CH_TYPE_INBOUND_COALESCED: Coalesced channel for the device to combine
79  *				   multiple packets and send them as a single
80  *				   large packet to reduce CPU consumption
81  */
82 enum mhi_ch_type {
83 	MHI_CH_TYPE_INVALID = 0,
84 	MHI_CH_TYPE_OUTBOUND = DMA_TO_DEVICE,
85 	MHI_CH_TYPE_INBOUND = DMA_FROM_DEVICE,
86 	MHI_CH_TYPE_INBOUND_COALESCED = 3,
87 };
88 
89 /**
90  * struct mhi_buf - MHI Buffer description
91  * @buf: Virtual address of the buffer
92  * @name: Buffer label. For offload channel, configurations name must be:
93  *        ECA - Event context array data
94  *        CCA - Channel context array data
95  * @dma_addr: IOMMU address of the buffer
96  * @len: # of bytes
97  */
98 struct mhi_buf {
99 	void *buf;
100 	const char *name;
101 	dma_addr_t dma_addr;
102 	size_t len;
103 };
104 
105 /**
106  * struct image_info - Firmware and RDDM table
107  * @mhi_buf: Buffer for firmware and RDDM table
108  * @entries: # of entries in table
109  */
110 struct image_info {
111 	/* private: from internal.h */
112 	struct bhi_vec_entry *bhi_vec;
113 	/* public: */
114 	u32 entries;
115 	struct mhi_buf mhi_buf[] __counted_by(entries);
116 };
117 
118 /**
119  * struct mhi_link_info - BW requirement
120  * target_link_speed - Link speed as defined by TLS bits in LinkControl reg
121  * target_link_width - Link width as defined by NLW bits in LinkStatus reg
122  */
123 struct mhi_link_info {
124 	unsigned int target_link_speed;
125 	unsigned int target_link_width;
126 };
127 
128 /**
129  * enum mhi_ee_type - Execution environment types
130  * @MHI_EE_PBL: Primary Bootloader
131  * @MHI_EE_SBL: Secondary Bootloader
132  * @MHI_EE_AMSS: Modem, aka the primary runtime EE
133  * @MHI_EE_RDDM: Ram dump download mode
134  * @MHI_EE_WFW: WLAN firmware mode
135  * @MHI_EE_PTHRU: Passthrough
136  * @MHI_EE_EDL: Embedded downloader
137  * @MHI_EE_FP: Flash Programmer Environment
138  */
139 enum mhi_ee_type {
140 	MHI_EE_PBL,
141 	MHI_EE_SBL,
142 	MHI_EE_AMSS,
143 	MHI_EE_RDDM,
144 	MHI_EE_WFW,
145 	MHI_EE_PTHRU,
146 	MHI_EE_EDL,
147 	MHI_EE_FP,
148 	MHI_EE_MAX_SUPPORTED = MHI_EE_FP,
149 	MHI_EE_DISABLE_TRANSITION, /* local EE, not related to mhi spec */
150 	MHI_EE_NOT_SUPPORTED,
151 	MHI_EE_MAX,
152 };
153 
154 /**
155  * enum mhi_state - MHI states
156  * @MHI_STATE_RESET: Reset state
157  * @MHI_STATE_READY: Ready state
158  * @MHI_STATE_M0: M0 state
159  * @MHI_STATE_M1: M1 state
160  * @MHI_STATE_M2: M2 state
161  * @MHI_STATE_M3: M3 state
162  * @MHI_STATE_M3_FAST: M3 Fast state
163  * @MHI_STATE_BHI: BHI state
164  * @MHI_STATE_SYS_ERR: System Error state
165  */
166 enum mhi_state {
167 	MHI_STATE_RESET = 0x0,
168 	MHI_STATE_READY = 0x1,
169 	MHI_STATE_M0 = 0x2,
170 	MHI_STATE_M1 = 0x3,
171 	MHI_STATE_M2 = 0x4,
172 	MHI_STATE_M3 = 0x5,
173 	MHI_STATE_M3_FAST = 0x6,
174 	MHI_STATE_BHI = 0x7,
175 	MHI_STATE_SYS_ERR = 0xFF,
176 	MHI_STATE_MAX,
177 };
178 
179 /**
180  * enum mhi_ch_ee_mask - Execution environment mask for channel
181  * @MHI_CH_EE_PBL: Allow channel to be used in PBL EE
182  * @MHI_CH_EE_SBL: Allow channel to be used in SBL EE
183  * @MHI_CH_EE_AMSS: Allow channel to be used in AMSS EE
184  * @MHI_CH_EE_RDDM: Allow channel to be used in RDDM EE
185  * @MHI_CH_EE_PTHRU: Allow channel to be used in PTHRU EE
186  * @MHI_CH_EE_WFW: Allow channel to be used in WFW EE
187  * @MHI_CH_EE_EDL: Allow channel to be used in EDL EE
188  */
189 enum mhi_ch_ee_mask {
190 	MHI_CH_EE_PBL = BIT(MHI_EE_PBL),
191 	MHI_CH_EE_SBL = BIT(MHI_EE_SBL),
192 	MHI_CH_EE_AMSS = BIT(MHI_EE_AMSS),
193 	MHI_CH_EE_RDDM = BIT(MHI_EE_RDDM),
194 	MHI_CH_EE_PTHRU = BIT(MHI_EE_PTHRU),
195 	MHI_CH_EE_WFW = BIT(MHI_EE_WFW),
196 	MHI_CH_EE_EDL = BIT(MHI_EE_EDL),
197 };
198 
199 /**
200  * enum mhi_er_data_type - Event ring data types
201  * @MHI_ER_DATA: Only client data over this ring
202  * @MHI_ER_CTRL: MHI control data and client data
203  */
204 enum mhi_er_data_type {
205 	MHI_ER_DATA,
206 	MHI_ER_CTRL,
207 };
208 
209 /**
210  * enum mhi_db_brst_mode - Doorbell mode
211  * @MHI_DB_BRST_DISABLE: Burst mode disable
212  * @MHI_DB_BRST_ENABLE: Burst mode enable
213  */
214 enum mhi_db_brst_mode {
215 	MHI_DB_BRST_DISABLE = 0x2,
216 	MHI_DB_BRST_ENABLE = 0x3,
217 };
218 
219 /**
220  * struct mhi_channel_config - Channel configuration structure for controller
221  * @name: The name of this channel
222  * @num: The number assigned to this channel
223  * @num_elements: The number of elements that can be queued to this channel
224  * @local_elements: The local ring length of the channel
225  * @event_ring: The event ring index that services this channel
226  * @dir: Direction that data may flow on this channel
227  * @type: Channel type
228  * @ee_mask: Execution Environment mask for this channel
229  * @pollcfg: Polling configuration for burst mode.  0 is default.  milliseconds
230 	     for UL channels, multiple of 8 ring elements for DL channels
231  * @doorbell: Doorbell mode
232  * @lpm_notify: The channel master requires low power mode notifications
233  * @offload_channel: The client manages the channel completely
234  * @doorbell_mode_switch: Channel switches to doorbell mode on M0 transition
235  * @wake-capable: Channel capable of waking up the system
236  */
237 struct mhi_channel_config {
238 	char *name;
239 	u32 num;
240 	u32 num_elements;
241 	u32 local_elements;
242 	u32 event_ring;
243 	enum dma_data_direction dir;
244 	enum mhi_ch_type type;
245 	u32 ee_mask;
246 	u32 pollcfg;
247 	enum mhi_db_brst_mode doorbell;
248 	bool lpm_notify;
249 	bool offload_channel;
250 	bool doorbell_mode_switch;
251 	bool wake_capable;
252 };
253 
254 /**
255  * struct mhi_event_config - Event ring configuration structure for controller
256  * @num_elements: The number of elements that can be queued to this ring
257  * @irq_moderation_ms: Delay irq for additional events to be aggregated
258  * @irq: IRQ associated with this ring
259  * @channel: Dedicated channel number. U32_MAX indicates a non-dedicated ring
260  * @priority: Priority of this ring. Use 1 for now
261  * @mode: Doorbell mode
262  * @data_type: Type of data this ring will process
263  * @hardware_event: This ring is associated with hardware channels
264  * @client_managed: This ring is client managed
265  * @offload_channel: This ring is associated with an offloaded channel
266  */
267 struct mhi_event_config {
268 	u32 num_elements;
269 	u32 irq_moderation_ms;
270 	u32 irq;
271 	u32 channel;
272 	u32 priority;
273 	enum mhi_db_brst_mode mode;
274 	enum mhi_er_data_type data_type;
275 	bool hardware_event;
276 	bool client_managed;
277 	bool offload_channel;
278 };
279 
280 /**
281  * struct mhi_controller_config - Root MHI controller configuration
282  * @max_channels: Maximum number of channels supported
283  * @timeout_ms: Timeout value for operations. 0 means use default
284  * @ready_timeout_ms: Timeout value for waiting device to be ready (optional)
285  * @buf_len: Size of automatically allocated buffers. 0 means use default
286  * @num_channels: Number of channels defined in @ch_cfg
287  * @ch_cfg: Array of defined channels
288  * @num_events: Number of event rings defined in @event_cfg
289  * @event_cfg: Array of defined event rings
290  * @use_bounce_buf: Use a bounce buffer pool due to limited DDR access
291  * @m2_no_db: Host is not allowed to ring DB in M2 state
292  */
293 struct mhi_controller_config {
294 	u32 max_channels;
295 	u32 timeout_ms;
296 	u32 ready_timeout_ms;
297 	u32 buf_len;
298 	u32 num_channels;
299 	const struct mhi_channel_config *ch_cfg;
300 	u32 num_events;
301 	struct mhi_event_config *event_cfg;
302 	bool use_bounce_buf;
303 	bool m2_no_db;
304 };
305 
306 /**
307  * struct mhi_controller - Master MHI controller structure
308  * @name: Device name of the MHI controller
309  * @cntrl_dev: Pointer to the struct device of physical bus acting as the MHI
310  *            controller (required)
311  * @mhi_dev: MHI device instance for the controller
312  * @debugfs_dentry: MHI controller debugfs directory
313  * @regs: Base address of MHI MMIO register space (required)
314  * @bhi: Points to base of MHI BHI register space
315  * @bhie: Points to base of MHI BHIe register space
316  * @wake_db: MHI WAKE doorbell register address
317  * @iova_start: IOMMU starting address for data (required)
318  * @iova_stop: IOMMU stop address for data (required)
319  * @fw_image: Firmware image name for normal booting (optional)
320  * @fw_data: Firmware image data content for normal booting, used only
321  *           if fw_image is NULL and fbc_download is true (optional)
322  * @fw_sz: Firmware image data size for normal booting, used only if fw_image
323  *         is NULL and fbc_download is true (optional)
324  * @edl_image: Firmware image name for emergency download mode (optional)
325  * @rddm_size: RAM dump size that host should allocate for debugging purpose
326  * @sbl_size: SBL image size downloaded through BHIe (optional)
327  * @seg_len: BHIe vector size (optional)
328  * @reg_len: Length of the MHI MMIO region (required)
329  * @fbc_image: Points to firmware image buffer
330  * @rddm_image: Points to RAM dump buffer
331  * @mhi_chan: Points to the channel configuration table
332  * @lpm_chans: List of channels that require LPM notifications
333  * @irq: base irq # to request (required)
334  * @max_chan: Maximum number of channels the controller supports
335  * @total_ev_rings: Total # of event rings allocated
336  * @hw_ev_rings: Number of hardware event rings
337  * @sw_ev_rings: Number of software event rings
338  * @nr_irqs: Number of IRQ allocated by bus master (required)
339  * @serial_number: MHI controller serial number obtained from BHI
340  * @mhi_event: MHI event ring configurations table
341  * @mhi_cmd: MHI command ring configurations table
342  * @mhi_ctxt: MHI device context, shared memory between host and device
343  * @pm_mutex: Mutex for suspend/resume operation
344  * @pm_lock: Lock for protecting MHI power management state
345  * @timeout_ms: Timeout in ms for state transitions
346  * @ready_timeout_ms: Timeout in ms for waiting device to be ready (optional)
347  * @pm_state: MHI power management state
348  * @db_access: DB access states
349  * @ee: MHI device execution environment
350  * @dev_state: MHI device state
351  * @dev_wake: Device wakeup count
352  * @pending_pkts: Pending packets for the controller
353  * @M0, M2, M3: Counters to track number of device MHI state changes
354  * @transition_list: List of MHI state transitions
355  * @transition_lock: Lock for protecting MHI state transition list
356  * @wlock: Lock for protecting device wakeup
357  * @mhi_link_info: Device bandwidth info
358  * @st_worker: State transition worker
359  * @hiprio_wq: High priority workqueue for MHI work such as state transitions
360  * @state_event: State change event
361  * @status_cb: CB function to notify power states of the device (required)
362  * @wake_get: CB function to assert device wake (optional)
363  * @wake_put: CB function to de-assert device wake (optional)
364  * @wake_toggle: CB function to assert and de-assert device wake (optional)
365  * @runtime_get: CB function to controller runtime resume (required)
366  * @runtime_put: CB function to decrement pm usage (required)
367  * @map_single: CB function to create TRE buffer
368  * @unmap_single: CB function to destroy TRE buffer
369  * @read_reg: Read a MHI register via the physical link (required)
370  * @write_reg: Write a MHI register via the physical link (required)
371  * @reset: Controller specific reset function (optional)
372  * @edl_trigger: CB function to trigger EDL mode (optional)
373  * @buffer_len: Bounce buffer length
374  * @index: Index of the MHI controller instance
375  * @bounce_buf: Use of bounce buffer
376  * @fbc_download: MHI host needs to do complete image transfer (optional)
377  * @wake_set: Device wakeup set flag
378  * @irq_flags: irq flags passed to request_irq (optional)
379  * @mru: the default MRU for the MHI device
380  *
381  * Fields marked as (required) need to be populated by the controller driver
382  * before calling mhi_register_controller(). For the fields marked as (optional)
383  * they can be populated depending on the usecase.
384  */
385 struct mhi_controller {
386 	const char *name;
387 	struct device *cntrl_dev;
388 	struct mhi_device *mhi_dev;
389 	struct dentry *debugfs_dentry;
390 	void __iomem *regs;
391 	void __iomem *bhi;
392 	void __iomem *bhie;
393 	void __iomem *wake_db;
394 
395 	dma_addr_t iova_start;
396 	dma_addr_t iova_stop;
397 	const char *fw_image;
398 	const u8 *fw_data;
399 	size_t fw_sz;
400 	const char *edl_image;
401 	size_t rddm_size;
402 	size_t sbl_size;
403 	size_t seg_len;
404 	size_t reg_len;
405 	struct image_info *fbc_image;
406 	struct image_info *rddm_image;
407 	struct mhi_chan *mhi_chan;
408 	struct list_head lpm_chans;
409 	int *irq;
410 	u32 max_chan;
411 	u32 total_ev_rings;
412 	u32 hw_ev_rings;
413 	u32 sw_ev_rings;
414 	u32 nr_irqs;
415 	u32 serial_number;
416 
417 	struct mhi_event *mhi_event;
418 	struct mhi_cmd *mhi_cmd;
419 	struct mhi_ctxt *mhi_ctxt;
420 
421 	struct mutex pm_mutex;
422 	rwlock_t pm_lock;
423 	u32 timeout_ms;
424 	u32 ready_timeout_ms;
425 	u32 pm_state;
426 	u32 db_access;
427 	enum mhi_ee_type ee;
428 	enum mhi_state dev_state;
429 	atomic_t dev_wake;
430 	atomic_t pending_pkts;
431 	u32 M0, M2, M3;
432 	struct list_head transition_list;
433 	spinlock_t transition_lock;
434 	spinlock_t wlock;
435 	struct mhi_link_info mhi_link_info;
436 	struct work_struct st_worker;
437 	struct workqueue_struct *hiprio_wq;
438 	wait_queue_head_t state_event;
439 
440 	void (*status_cb)(struct mhi_controller *mhi_cntrl,
441 			  enum mhi_callback cb);
442 	void (*wake_get)(struct mhi_controller *mhi_cntrl, bool override);
443 	void (*wake_put)(struct mhi_controller *mhi_cntrl, bool override);
444 	void (*wake_toggle)(struct mhi_controller *mhi_cntrl);
445 	int (*runtime_get)(struct mhi_controller *mhi_cntrl);
446 	void (*runtime_put)(struct mhi_controller *mhi_cntrl);
447 	int (*map_single)(struct mhi_controller *mhi_cntrl,
448 			  struct mhi_buf_info *buf);
449 	void (*unmap_single)(struct mhi_controller *mhi_cntrl,
450 			     struct mhi_buf_info *buf);
451 	int (*read_reg)(struct mhi_controller *mhi_cntrl, void __iomem *addr,
452 			u32 *out);
453 	void (*write_reg)(struct mhi_controller *mhi_cntrl, void __iomem *addr,
454 			  u32 val);
455 	void (*reset)(struct mhi_controller *mhi_cntrl);
456 	int (*edl_trigger)(struct mhi_controller *mhi_cntrl);
457 
458 	size_t buffer_len;
459 	int index;
460 	bool bounce_buf;
461 	bool fbc_download;
462 	bool wake_set;
463 	unsigned long irq_flags;
464 	u32 mru;
465 };
466 
467 /**
468  * struct mhi_device - Structure representing an MHI device which binds
469  *                     to channels or is associated with controllers
470  * @id: Pointer to MHI device ID struct
471  * @name: Name of the associated MHI device
472  * @mhi_cntrl: Controller the device belongs to
473  * @ul_chan: UL channel for the device
474  * @dl_chan: DL channel for the device
475  * @dev: Driver model device node for the MHI device
476  * @dev_type: MHI device type
477  * @ul_chan_id: MHI channel id for UL transfer
478  * @dl_chan_id: MHI channel id for DL transfer
479  * @dev_wake: Device wakeup counter
480  */
481 struct mhi_device {
482 	const struct mhi_device_id *id;
483 	const char *name;
484 	struct mhi_controller *mhi_cntrl;
485 	struct mhi_chan *ul_chan;
486 	struct mhi_chan *dl_chan;
487 	struct device dev;
488 	enum mhi_device_type dev_type;
489 	int ul_chan_id;
490 	int dl_chan_id;
491 	u32 dev_wake;
492 };
493 
494 /**
495  * struct mhi_result - Completed buffer information
496  * @buf_addr: Address of data buffer
497  * @bytes_xferd: # of bytes transferred
498  * @dir: Channel direction
499  * @transaction_status: Status of last transaction
500  */
501 struct mhi_result {
502 	void *buf_addr;
503 	size_t bytes_xferd;
504 	enum dma_data_direction dir;
505 	int transaction_status;
506 };
507 
508 /**
509  * struct mhi_driver - Structure representing a MHI client driver
510  * @probe: CB function for client driver probe function
511  * @remove: CB function for client driver remove function
512  * @ul_xfer_cb: CB function for UL data transfer
513  * @dl_xfer_cb: CB function for DL data transfer
514  * @status_cb: CB functions for asynchronous status
515  * @driver: Device driver model driver
516  */
517 struct mhi_driver {
518 	const struct mhi_device_id *id_table;
519 	int (*probe)(struct mhi_device *mhi_dev,
520 		     const struct mhi_device_id *id);
521 	void (*remove)(struct mhi_device *mhi_dev);
522 	void (*ul_xfer_cb)(struct mhi_device *mhi_dev,
523 			   struct mhi_result *result);
524 	void (*dl_xfer_cb)(struct mhi_device *mhi_dev,
525 			   struct mhi_result *result);
526 	void (*status_cb)(struct mhi_device *mhi_dev, enum mhi_callback mhi_cb);
527 	struct device_driver driver;
528 };
529 
530 #define to_mhi_driver(drv) container_of_const(drv, struct mhi_driver, driver)
531 #define to_mhi_device(dev) container_of(dev, struct mhi_device, dev)
532 
533 /**
534  * mhi_alloc_controller - Allocate the MHI Controller structure
535  * Allocate the mhi_controller structure using zero initialized memory
536  */
537 struct mhi_controller *mhi_alloc_controller(void);
538 
539 /**
540  * mhi_free_controller - Free the MHI Controller structure
541  * Free the mhi_controller structure which was previously allocated
542  */
543 void mhi_free_controller(struct mhi_controller *mhi_cntrl);
544 
545 /**
546  * mhi_register_controller - Register MHI controller
547  * @mhi_cntrl: MHI controller to register
548  * @config: Configuration to use for the controller
549  */
550 int mhi_register_controller(struct mhi_controller *mhi_cntrl,
551 			const struct mhi_controller_config *config);
552 
553 /**
554  * mhi_unregister_controller - Unregister MHI controller
555  * @mhi_cntrl: MHI controller to unregister
556  */
557 void mhi_unregister_controller(struct mhi_controller *mhi_cntrl);
558 
559 /*
560  * module_mhi_driver() - Helper macro for drivers that don't do
561  * anything special other than using default mhi_driver_register() and
562  * mhi_driver_unregister().  This eliminates a lot of boilerplate.
563  * Each module may only use this macro once.
564  */
565 #define module_mhi_driver(mhi_drv) \
566 	module_driver(mhi_drv, mhi_driver_register, \
567 		      mhi_driver_unregister)
568 
569 /*
570  * Macro to avoid include chaining to get THIS_MODULE
571  */
572 #define mhi_driver_register(mhi_drv) \
573 	__mhi_driver_register(mhi_drv, THIS_MODULE)
574 
575 /**
576  * __mhi_driver_register - Register driver with MHI framework
577  * @mhi_drv: Driver associated with the device
578  * @owner: The module owner
579  */
580 int __mhi_driver_register(struct mhi_driver *mhi_drv, struct module *owner);
581 
582 /**
583  * mhi_driver_unregister - Unregister a driver for mhi_devices
584  * @mhi_drv: Driver associated with the device
585  */
586 void mhi_driver_unregister(struct mhi_driver *mhi_drv);
587 
588 /**
589  * mhi_set_mhi_state - Set MHI device state
590  * @mhi_cntrl: MHI controller
591  * @state: State to set
592  */
593 void mhi_set_mhi_state(struct mhi_controller *mhi_cntrl,
594 		       enum mhi_state state);
595 
596 /**
597  * mhi_notify - Notify the MHI client driver about client device status
598  * @mhi_dev: MHI device instance
599  * @cb_reason: MHI callback reason
600  */
601 void mhi_notify(struct mhi_device *mhi_dev, enum mhi_callback cb_reason);
602 
603 /**
604  * mhi_get_free_desc_count - Get transfer ring length
605  * Get # of TD available to queue buffers
606  * @mhi_dev: Device associated with the channels
607  * @dir: Direction of the channel
608  */
609 int mhi_get_free_desc_count(struct mhi_device *mhi_dev,
610 				enum dma_data_direction dir);
611 
612 /**
613  * mhi_prepare_for_power_up - Do pre-initialization before power up.
614  *                            This is optional, call this before power up if
615  *                            the controller does not want bus framework to
616  *                            automatically free any allocated memory during
617  *                            shutdown process.
618  * @mhi_cntrl: MHI controller
619  */
620 int mhi_prepare_for_power_up(struct mhi_controller *mhi_cntrl);
621 
622 /**
623  * mhi_async_power_up - Start MHI power up sequence
624  * @mhi_cntrl: MHI controller
625  */
626 int mhi_async_power_up(struct mhi_controller *mhi_cntrl);
627 
628 /**
629  * mhi_sync_power_up - Start MHI power up sequence and wait till the device
630  *                     enters valid EE state
631  * @mhi_cntrl: MHI controller
632  */
633 int mhi_sync_power_up(struct mhi_controller *mhi_cntrl);
634 
635 /**
636  * mhi_power_down - Power down the MHI device and also destroy the
637  *                  'struct device' for the channels associated with it.
638  *                  See also mhi_power_down_keep_dev() which is a variant
639  *                  of this API that keeps the 'struct device' for channels
640  *                  (useful during suspend/hibernation).
641  * @mhi_cntrl: MHI controller
642  * @graceful: Link is still accessible, so do a graceful shutdown process
643  */
644 void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful);
645 
646 /**
647  * mhi_power_down_keep_dev - Power down the MHI device but keep the 'struct
648  *                           device' for the channels associated with it.
649  *                           This is a variant of 'mhi_power_down()' and
650  *                           useful in scenarios such as suspend/hibernation
651  *                           where destroying of the 'struct device' is not
652  *                           needed.
653  * @mhi_cntrl: MHI controller
654  * @graceful: Link is still accessible, so do a graceful shutdown process
655  */
656 void mhi_power_down_keep_dev(struct mhi_controller *mhi_cntrl, bool graceful);
657 
658 /**
659  * mhi_unprepare_after_power_down - Free any allocated memory after power down
660  * @mhi_cntrl: MHI controller
661  */
662 void mhi_unprepare_after_power_down(struct mhi_controller *mhi_cntrl);
663 
664 /**
665  * mhi_pm_suspend - Move MHI into a suspended state
666  * @mhi_cntrl: MHI controller
667  */
668 int mhi_pm_suspend(struct mhi_controller *mhi_cntrl);
669 
670 /**
671  * mhi_pm_resume - Resume MHI from suspended state
672  * @mhi_cntrl: MHI controller
673  */
674 int mhi_pm_resume(struct mhi_controller *mhi_cntrl);
675 
676 /**
677  * mhi_pm_resume_force - Force resume MHI from suspended state
678  * @mhi_cntrl: MHI controller
679  *
680  * Resume the device irrespective of its MHI state. As per the MHI spec, devices
681  * has to be in M3 state during resume. But some devices seem to be in a
682  * different MHI state other than M3 but they continue working fine if allowed.
683  * This API is intented to be used for such devices.
684  *
685  * Return: 0 if the resume succeeds, a negative error code otherwise
686  */
687 int mhi_pm_resume_force(struct mhi_controller *mhi_cntrl);
688 
689 /**
690  * mhi_download_rddm_image - Download ramdump image from device for
691  *                           debugging purpose.
692  * @mhi_cntrl: MHI controller
693  * @in_panic: Download rddm image during kernel panic
694  */
695 int mhi_download_rddm_image(struct mhi_controller *mhi_cntrl, bool in_panic);
696 
697 /**
698  * mhi_force_rddm_mode - Force device into rddm mode
699  * @mhi_cntrl: MHI controller
700  */
701 int mhi_force_rddm_mode(struct mhi_controller *mhi_cntrl);
702 
703 /**
704  * mhi_get_exec_env - Get BHI execution environment of the device
705  * @mhi_cntrl: MHI controller
706  */
707 enum mhi_ee_type mhi_get_exec_env(struct mhi_controller *mhi_cntrl);
708 
709 /**
710  * mhi_get_mhi_state - Get MHI state of the device
711  * @mhi_cntrl: MHI controller
712  */
713 enum mhi_state mhi_get_mhi_state(struct mhi_controller *mhi_cntrl);
714 
715 /**
716  * mhi_soc_reset - Trigger a device reset. This can be used as a last resort
717  *		   to reset and recover a device.
718  * @mhi_cntrl: MHI controller
719  */
720 void mhi_soc_reset(struct mhi_controller *mhi_cntrl);
721 
722 /**
723  * mhi_device_get_sync - Disable device low power mode. Synchronously
724  *                       take the controller out of suspended state
725  * @mhi_dev: Device associated with the channel
726  */
727 int mhi_device_get_sync(struct mhi_device *mhi_dev);
728 
729 /**
730  * mhi_device_put - Re-enable device low power mode
731  * @mhi_dev: Device associated with the channel
732  */
733 void mhi_device_put(struct mhi_device *mhi_dev);
734 
735 /**
736  * mhi_prepare_for_transfer - Setup UL and DL channels for data transfer.
737  * @mhi_dev: Device associated with the channels
738  *
739  * Allocate and initialize the channel context and also issue the START channel
740  * command to both channels. Channels can be started only if both host and
741  * device execution environments match and channels are in a DISABLED state.
742  */
743 int mhi_prepare_for_transfer(struct mhi_device *mhi_dev);
744 
745 /**
746  * mhi_unprepare_from_transfer - Reset UL and DL channels for data transfer.
747  *                               Issue the RESET channel command and let the
748  *                               device clean-up the context so no incoming
749  *                               transfers are seen on the host. Free memory
750  *                               associated with the context on host. If device
751  *                               is unresponsive, only perform a host side
752  *                               clean-up. Channels can be reset only if both
753  *                               host and device execution environments match
754  *                               and channels are in an ENABLED, STOPPED or
755  *                               SUSPENDED state.
756  * @mhi_dev: Device associated with the channels
757  */
758 void mhi_unprepare_from_transfer(struct mhi_device *mhi_dev);
759 
760 /**
761  * mhi_queue_buf - Send or receive raw buffers from client device over MHI
762  *                 channel
763  * @mhi_dev: Device associated with the channels
764  * @dir: DMA direction for the channel
765  * @buf: Buffer for holding the data
766  * @len: Buffer length
767  * @mflags: MHI transfer flags used for the transfer
768  */
769 int mhi_queue_buf(struct mhi_device *mhi_dev, enum dma_data_direction dir,
770 		  void *buf, size_t len, enum mhi_flags mflags);
771 
772 /**
773  * mhi_queue_skb - Send or receive SKBs from client device over MHI channel
774  * @mhi_dev: Device associated with the channels
775  * @dir: DMA direction for the channel
776  * @skb: Buffer for holding SKBs
777  * @len: Buffer length
778  * @mflags: MHI transfer flags used for the transfer
779  */
780 int mhi_queue_skb(struct mhi_device *mhi_dev, enum dma_data_direction dir,
781 		  struct sk_buff *skb, size_t len, enum mhi_flags mflags);
782 
783 /**
784  * mhi_queue_is_full - Determine whether queueing new elements is possible
785  * @mhi_dev: Device associated with the channels
786  * @dir: DMA direction for the channel
787  */
788 bool mhi_queue_is_full(struct mhi_device *mhi_dev, enum dma_data_direction dir);
789 
790 /**
791  * mhi_get_channel_doorbell_offset - Get the channel doorbell offset
792  * @mhi_cntrl: MHI controller
793  * @chdb_offset: Read channel doorbell offset
794  *
795  * Return: 0 if the read succeeds, a negative error code otherwise
796  */
797 int mhi_get_channel_doorbell_offset(struct mhi_controller *mhi_cntrl, u32 *chdb_offset);
798 
799 #endif /* _MHI_H_ */
800