gsi.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef GSI_H
  6. #define GSI_H
  7. #include <linux/device.h>
  8. #include <linux/types.h>
  9. #include <linux/completion.h>
  10. #include <linux/mutex.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/msm_gsi.h>
  13. #include <linux/errno.h>
  14. #include <linux/ipc_logging.h>
  15. /*
  16. * The following for adding code (ie. for EMULATION) not found on x86.
  17. */
  18. #if defined(CONFIG_IPA_EMULATION)
  19. # include "gsi_emulation_stubs.h"
  20. #endif
  21. #define GSI_ASSERT() \
  22. BUG()
  23. #define GSI_CHAN_MAX 31
  24. #define GSI_EVT_RING_MAX 24
  25. #define GSI_NO_EVT_ERINDEX 31
  26. #define gsi_readl(c) (readl_relaxed(c))
  27. #define gsi_writel(v, c) ({ __iowmb(); writel_relaxed((v), (c)); })
  28. #define GSI_IPC_LOGGING(buf, fmt, args...) \
  29. do { \
  30. if (buf) \
  31. ipc_log_string((buf), fmt, __func__, __LINE__, \
  32. ## args); \
  33. } while (0)
  34. #define GSIDBG(fmt, args...) \
  35. do { \
  36. dev_dbg(gsi_ctx->dev, "%s:%d " fmt, __func__, __LINE__, \
  37. ## args);\
  38. if (gsi_ctx) { \
  39. GSI_IPC_LOGGING(gsi_ctx->ipc_logbuf, \
  40. "%s:%d " fmt, ## args); \
  41. GSI_IPC_LOGGING(gsi_ctx->ipc_logbuf_low, \
  42. "%s:%d " fmt, ## args); \
  43. } \
  44. } while (0)
  45. #define GSIDBG_LOW(fmt, args...) \
  46. do { \
  47. dev_dbg(gsi_ctx->dev, "%s:%d " fmt, __func__, __LINE__, \
  48. ## args);\
  49. if (gsi_ctx) { \
  50. GSI_IPC_LOGGING(gsi_ctx->ipc_logbuf_low, \
  51. "%s:%d " fmt, ## args); \
  52. } \
  53. } while (0)
  54. #define GSIERR(fmt, args...) \
  55. do { \
  56. dev_err(gsi_ctx->dev, "%s:%d " fmt, __func__, __LINE__, \
  57. ## args);\
  58. if (gsi_ctx) { \
  59. GSI_IPC_LOGGING(gsi_ctx->ipc_logbuf, \
  60. "%s:%d " fmt, ## args); \
  61. GSI_IPC_LOGGING(gsi_ctx->ipc_logbuf_low, \
  62. "%s:%d " fmt, ## args); \
  63. } \
  64. } while (0)
  65. #define GSI_IPC_LOG_PAGES 50
  66. enum gsi_evt_ring_state {
  67. GSI_EVT_RING_STATE_NOT_ALLOCATED = 0x0,
  68. GSI_EVT_RING_STATE_ALLOCATED = 0x1,
  69. GSI_EVT_RING_STATE_ERROR = 0xf
  70. };
  71. enum gsi_chan_state {
  72. GSI_CHAN_STATE_NOT_ALLOCATED = 0x0,
  73. GSI_CHAN_STATE_ALLOCATED = 0x1,
  74. GSI_CHAN_STATE_STARTED = 0x2,
  75. GSI_CHAN_STATE_STOPPED = 0x3,
  76. GSI_CHAN_STATE_STOP_IN_PROC = 0x4,
  77. GSI_CHAN_STATE_ERROR = 0xf
  78. };
  79. struct gsi_ring_ctx {
  80. spinlock_t slock;
  81. unsigned long base_va;
  82. uint64_t base;
  83. uint64_t wp;
  84. uint64_t rp;
  85. uint64_t wp_local;
  86. uint64_t rp_local;
  87. uint16_t len;
  88. uint8_t elem_sz;
  89. uint16_t max_num_elem;
  90. uint64_t end;
  91. };
  92. struct gsi_chan_dp_stats {
  93. unsigned long ch_below_lo;
  94. unsigned long ch_below_hi;
  95. unsigned long ch_above_hi;
  96. unsigned long empty_time;
  97. unsigned long last_timestamp;
  98. };
  99. struct gsi_chan_stats {
  100. unsigned long queued;
  101. unsigned long completed;
  102. unsigned long callback_to_poll;
  103. unsigned long poll_to_callback;
  104. unsigned long poll_pending_irq;
  105. unsigned long invalid_tre_error;
  106. unsigned long poll_ok;
  107. unsigned long poll_empty;
  108. unsigned long userdata_in_use;
  109. struct gsi_chan_dp_stats dp;
  110. };
  111. /**
  112. * struct gsi_user_data - user_data element pointed by the TRE
  113. * @valid: valid to be cleaned. if its true that means it is being used.
  114. * false means its free to overwrite
  115. * @p: pointer to the user data array element
  116. */
  117. struct gsi_user_data {
  118. bool valid;
  119. void *p;
  120. };
  121. struct gsi_chan_ctx {
  122. struct gsi_chan_props props;
  123. enum gsi_chan_state state;
  124. struct gsi_ring_ctx ring;
  125. struct gsi_user_data *user_data;
  126. struct gsi_evt_ctx *evtr;
  127. struct mutex mlock;
  128. struct completion compl;
  129. bool allocated;
  130. atomic_t poll_mode;
  131. union __packed gsi_channel_scratch scratch;
  132. struct gsi_chan_stats stats;
  133. bool enable_dp_stats;
  134. bool print_dp_stats;
  135. };
  136. struct gsi_evt_stats {
  137. unsigned long completed;
  138. };
  139. struct gsi_evt_ctx {
  140. struct gsi_evt_ring_props props;
  141. enum gsi_evt_ring_state state;
  142. uint8_t id;
  143. struct gsi_ring_ctx ring;
  144. struct mutex mlock;
  145. struct completion compl;
  146. struct gsi_chan_ctx *chan;
  147. atomic_t chan_ref_cnt;
  148. union __packed gsi_evt_scratch scratch;
  149. struct gsi_evt_stats stats;
  150. };
  151. struct gsi_ee_scratch {
  152. union __packed {
  153. struct {
  154. uint32_t inter_ee_cmd_return_code:3;
  155. uint32_t resvd1:2;
  156. uint32_t generic_ee_cmd_return_code:3;
  157. uint32_t resvd2:7;
  158. uint32_t max_usb_pkt_size:1;
  159. uint32_t resvd3:8;
  160. uint32_t mhi_base_chan_idx:8;
  161. } s;
  162. uint32_t val;
  163. } word0;
  164. uint32_t word1;
  165. };
  166. struct ch_debug_stats {
  167. unsigned long ch_allocate;
  168. unsigned long ch_start;
  169. unsigned long ch_stop;
  170. unsigned long ch_reset;
  171. unsigned long ch_de_alloc;
  172. unsigned long ch_db_stop;
  173. unsigned long cmd_completed;
  174. };
  175. struct gsi_generic_ee_cmd_debug_stats {
  176. unsigned long halt_channel;
  177. };
  178. struct gsi_coal_chan_info {
  179. uint8_t ch_id;
  180. uint8_t evchid;
  181. };
  182. struct gsi_ctx {
  183. void __iomem *base;
  184. struct device *dev;
  185. struct gsi_per_props per;
  186. bool per_registered;
  187. struct gsi_chan_ctx chan[GSI_CHAN_MAX];
  188. struct ch_debug_stats ch_dbg[GSI_CHAN_MAX];
  189. struct gsi_evt_ctx evtr[GSI_EVT_RING_MAX];
  190. struct gsi_generic_ee_cmd_debug_stats gen_ee_cmd_dbg;
  191. struct mutex mlock;
  192. spinlock_t slock;
  193. unsigned long evt_bmap;
  194. bool enabled;
  195. atomic_t num_chan;
  196. atomic_t num_evt_ring;
  197. struct gsi_ee_scratch scratch;
  198. int num_ch_dp_stats;
  199. struct workqueue_struct *dp_stat_wq;
  200. u32 max_ch;
  201. u32 max_ev;
  202. struct completion gen_ee_cmd_compl;
  203. void *ipc_logbuf;
  204. void *ipc_logbuf_low;
  205. struct gsi_coal_chan_info coal_info;
  206. /*
  207. * The following used only on emulation systems.
  208. */
  209. void __iomem *intcntrlr_base;
  210. u32 intcntrlr_mem_size;
  211. irq_handler_t intcntrlr_gsi_isr;
  212. irq_handler_t intcntrlr_client_isr;
  213. atomic_t num_unclock_irq;
  214. };
  215. enum gsi_re_type {
  216. GSI_RE_XFER = 0x2,
  217. GSI_RE_IMMD_CMD = 0x3,
  218. GSI_RE_NOP = 0x4,
  219. GSI_RE_COAL = 0x8,
  220. };
  221. struct __packed gsi_tre {
  222. uint64_t buffer_ptr;
  223. uint16_t buf_len;
  224. uint16_t resvd1;
  225. uint16_t chain:1;
  226. uint16_t resvd4:7;
  227. uint16_t ieob:1;
  228. uint16_t ieot:1;
  229. uint16_t bei:1;
  230. uint16_t resvd3:5;
  231. uint8_t re_type;
  232. uint8_t resvd2;
  233. };
  234. struct __packed gsi_gci_tre {
  235. uint64_t buffer_ptr:41;
  236. uint64_t resvd1:7;
  237. uint64_t buf_len:16;
  238. uint64_t cookie:40;
  239. uint64_t resvd2:8;
  240. uint64_t re_type:8;
  241. uint64_t resvd3:8;
  242. };
  243. #define GSI_XFER_COMPL_TYPE_GCI 0x28
  244. struct __packed gsi_xfer_compl_evt {
  245. union {
  246. uint64_t xfer_ptr;
  247. struct {
  248. uint64_t cookie:40;
  249. uint64_t resvd1:24;
  250. };
  251. };
  252. uint16_t len;
  253. uint8_t veid;
  254. uint8_t code; /* see gsi_chan_evt */
  255. uint16_t resvd;
  256. uint8_t type;
  257. uint8_t chid;
  258. };
  259. enum gsi_err_type {
  260. GSI_ERR_TYPE_GLOB = 0x1,
  261. GSI_ERR_TYPE_CHAN = 0x2,
  262. GSI_ERR_TYPE_EVT = 0x3,
  263. };
  264. enum gsi_err_code {
  265. GSI_INVALID_TRE_ERR = 0x1,
  266. GSI_OUT_OF_BUFFERS_ERR = 0x2,
  267. GSI_OUT_OF_RESOURCES_ERR = 0x3,
  268. GSI_UNSUPPORTED_INTER_EE_OP_ERR = 0x4,
  269. GSI_EVT_RING_EMPTY_ERR = 0x5,
  270. GSI_NON_ALLOCATED_EVT_ACCESS_ERR = 0x6,
  271. GSI_HWO_1_ERR = 0x8
  272. };
  273. struct __packed gsi_log_err {
  274. uint32_t arg3:4;
  275. uint32_t arg2:4;
  276. uint32_t arg1:4;
  277. uint32_t code:4;
  278. uint32_t resvd:3;
  279. uint32_t virt_idx:5;
  280. uint32_t err_type:4;
  281. uint32_t ee:4;
  282. };
  283. enum gsi_ch_cmd_opcode {
  284. GSI_CH_ALLOCATE = 0x0,
  285. GSI_CH_START = 0x1,
  286. GSI_CH_STOP = 0x2,
  287. GSI_CH_RESET = 0x9,
  288. GSI_CH_DE_ALLOC = 0xa,
  289. GSI_CH_DB_STOP = 0xb,
  290. };
  291. enum gsi_evt_ch_cmd_opcode {
  292. GSI_EVT_ALLOCATE = 0x0,
  293. GSI_EVT_RESET = 0x9,
  294. GSI_EVT_DE_ALLOC = 0xa,
  295. };
  296. enum gsi_generic_ee_cmd_opcode {
  297. GSI_GEN_EE_CMD_HALT_CHANNEL = 0x1,
  298. GSI_GEN_EE_CMD_ALLOC_CHANNEL = 0x2,
  299. };
  300. enum gsi_generic_ee_cmd_return_code {
  301. GSI_GEN_EE_CMD_RETURN_CODE_SUCCESS = 0x1,
  302. GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING = 0x2,
  303. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_DIRECTION = 0x3,
  304. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_TYPE = 0x4,
  305. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_INDEX = 0x5,
  306. GSI_GEN_EE_CMD_RETURN_CODE_RETRY = 0x6,
  307. GSI_GEN_EE_CMD_RETURN_CODE_OUT_OF_RESOURCES = 0x7,
  308. };
  309. extern struct gsi_ctx *gsi_ctx;
  310. void gsi_debugfs_init(void);
  311. uint16_t gsi_find_idx_from_addr(struct gsi_ring_ctx *ctx, uint64_t addr);
  312. void gsi_update_ch_dp_stats(struct gsi_chan_ctx *ctx, uint16_t used);
  313. #endif