mhi.h 28 KB

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