spsi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2011-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. /**
  7. * Smart-Peripheral-Switch (SPS) internal API.
  8. */
  9. #ifndef _SPSI_H_
  10. #define _SPSI_H_
  11. #include <linux/types.h>
  12. #include <linux/list.h>
  13. #include <linux/kernel.h>
  14. #include <linux/compiler.h>
  15. #include <linux/ratelimit.h>
  16. #include <linux/ipc_logging.h>
  17. #include <linux/msm-sps.h>
  18. #include "sps_map.h"
  19. #if defined(CONFIG_PHYS_ADDR_T_64BIT) || defined(CONFIG_ARM_LPAE)
  20. #define SPS_LPAE (true)
  21. #else
  22. #define SPS_LPAE (false)
  23. #endif
  24. #define BAM_MAX_PIPES 31
  25. #define BAM_MAX_P_LOCK_GROUP_NUM 31
  26. /* Adjust for offset of struct sps_q_event */
  27. #define SPS_EVENT_INDEX(e) ((e) - 1)
  28. #define SPS_ERROR -1
  29. /* BAM identifier used in log messages */
  30. #define BAM_ID(dev) (&(dev)->props.phys_addr)
  31. /* "Clear" value for the connection parameter struct */
  32. #define SPSRM_CLEAR 0xccccccccUL
  33. #define SPSRM_ADDR_CLR \
  34. ((sizeof(int) == sizeof(long)) ? 0 : (SPSRM_CLEAR << 32))
  35. #define MAX_MSG_LEN 80
  36. #define SPS_IPC_LOGPAGES 10
  37. #define SPS_IPC_REG_DUMP_FACTOR 3
  38. #define SPS_IPC_DEFAULT_LOGLEVEL 3
  39. #define SPS_IPC_MAX_LOGLEVEL 4
  40. /* Connection mapping control struct */
  41. struct sps_rm {
  42. struct list_head connections_q;
  43. struct mutex lock;
  44. };
  45. /* SPS driver state struct */
  46. struct sps_drv {
  47. struct class *dev_class;
  48. dev_t dev_num;
  49. struct device *dev;
  50. struct clk *pmem_clk;
  51. struct clk *bamdma_clk;
  52. struct clk *dfab_clk;
  53. int is_ready;
  54. /* Platform data */
  55. phys_addr_t pipemem_phys_base;
  56. u32 pipemem_size;
  57. phys_addr_t bamdma_bam_phys_base;
  58. u32 bamdma_bam_size;
  59. phys_addr_t bamdma_dma_phys_base;
  60. u32 bamdma_dma_size;
  61. u32 bamdma_irq;
  62. u32 bamdma_restricted_pipes;
  63. /* Driver options bitflags (see SPS_OPT_*) */
  64. u32 options;
  65. /* Mutex to protect BAM and connection queues */
  66. struct mutex lock;
  67. /* BAM devices */
  68. struct list_head bams_q;
  69. char *hal_bam_version;
  70. /* Connection control state */
  71. struct sps_rm connection_ctrl;
  72. void *ipc_log0;
  73. void *ipc_log1;
  74. void *ipc_log2;
  75. void *ipc_log3;
  76. void *ipc_log4;
  77. u32 ipc_loglevel;
  78. };
  79. extern struct sps_drv *sps;
  80. extern u32 d_type;
  81. extern bool enhd_pipe;
  82. extern bool imem;
  83. extern enum sps_bam_type bam_type;
  84. #ifdef CONFIG_DEBUG_FS
  85. extern u8 debugfs_record_enabled;
  86. extern u8 logging_option;
  87. extern u8 debug_level_option;
  88. extern u8 print_limit_option;
  89. #define SPS_IPC(idx, dev, msg, ...) do { \
  90. if (dev) { \
  91. if (idx == 0) \
  92. ipc_log_string((dev)->ipc_log0, \
  93. "%s: " msg, __func__, ##__VA_ARGS__); \
  94. else if (idx == 1) \
  95. ipc_log_string((dev)->ipc_log1, \
  96. "%s: " msg, __func__, ##__VA_ARGS__); \
  97. else if (idx == 2) \
  98. ipc_log_string((dev)->ipc_log2, \
  99. "%s: " msg, __func__, ##__VA_ARGS__); \
  100. else if (idx == 3) \
  101. ipc_log_string((dev)->ipc_log3, \
  102. "%s: " msg, __func__, ##__VA_ARGS__); \
  103. else if (idx == 4) \
  104. ipc_log_string((dev)->ipc_log4, \
  105. "%s: " msg, __func__, ##__VA_ARGS__); \
  106. } \
  107. } while (0)
  108. #define SPS_DUMP(msg, ...) do { \
  109. SPS_IPC(4, sps, msg, ##__VA_ARGS__); \
  110. if (sps) { \
  111. if (sps->ipc_log4 == NULL) \
  112. pr_info("%s: " msg, __func__, ##__VA_ARGS__); \
  113. } \
  114. } while (0)
  115. #define SPS_ERR(dev, msg, ...) do { \
  116. if (logging_option != 1) { \
  117. if (unlikely(print_limit_option > 2)) \
  118. pr_err_ratelimited( \
  119. "%s: " msg, __func__, ##__VA_ARGS__); \
  120. else \
  121. pr_err("%s: " msg, __func__, ##__VA_ARGS__); \
  122. } \
  123. SPS_IPC(3, dev, msg, ##__VA_ARGS__); \
  124. } while (0)
  125. #define SPS_INFO(dev, msg, ...) do { \
  126. if (logging_option != 1) { \
  127. if (unlikely(print_limit_option > 1)) \
  128. pr_info_ratelimited( \
  129. "%s: " msg, __func__, ##__VA_ARGS__); \
  130. else \
  131. pr_info("%s: " msg, __func__, ##__VA_ARGS__); \
  132. } \
  133. SPS_IPC(3, dev, msg, ##__VA_ARGS__); \
  134. } while (0)
  135. #define SPS_DBG(dev, msg, ...) do { \
  136. if ((unlikely(logging_option > 1)) \
  137. && (unlikely(debug_level_option > 3))) {\
  138. if (unlikely(print_limit_option > 0)) \
  139. pr_info_ratelimited( \
  140. "%s: " msg, __func__, ##__VA_ARGS__); \
  141. else \
  142. pr_info("%s: " msg, __func__, ##__VA_ARGS__); \
  143. } else \
  144. pr_debug("%s: " msg, __func__, ##__VA_ARGS__); \
  145. if (dev) { \
  146. if ((dev)->ipc_loglevel <= 0) \
  147. SPS_IPC(0, dev, msg, ##__VA_ARGS__); \
  148. } \
  149. } while (0)
  150. #define SPS_DBG1(dev, msg, ...) do { \
  151. if ((unlikely(logging_option > 1)) \
  152. && (unlikely(debug_level_option > 2))) {\
  153. if (unlikely(print_limit_option > 0)) \
  154. pr_info_ratelimited( \
  155. "%s: " msg, __func__, ##__VA_ARGS__); \
  156. else \
  157. pr_info("%s: " msg, __func__, ##__VA_ARGS__); \
  158. } else \
  159. pr_debug("%s: " msg, __func__, ##__VA_ARGS__); \
  160. if (dev) { \
  161. if ((dev)->ipc_loglevel <= 1) \
  162. SPS_IPC(1, dev, msg, ##__VA_ARGS__); \
  163. } \
  164. } while (0)
  165. #define SPS_DBG2(dev, msg, ...) do { \
  166. if ((unlikely(logging_option > 1)) \
  167. && (unlikely(debug_level_option > 1))) {\
  168. if (unlikely(print_limit_option > 0)) \
  169. pr_info_ratelimited( \
  170. "%s: " msg, __func__, ##__VA_ARGS__); \
  171. else \
  172. pr_info("%s: " msg, __func__, ##__VA_ARGS__); \
  173. } else \
  174. pr_debug("%s: " msg, __func__, ##__VA_ARGS__); \
  175. if (dev) { \
  176. if ((dev)->ipc_loglevel <= 2) \
  177. SPS_IPC(2, dev, msg, ##__VA_ARGS__); \
  178. } \
  179. } while (0)
  180. #define SPS_DBG3(dev, msg, ...) do { \
  181. if ((unlikely(logging_option > 1)) \
  182. && (unlikely(debug_level_option > 0))) {\
  183. if (unlikely(print_limit_option > 0)) \
  184. pr_info_ratelimited( \
  185. "%s: " msg, __func__, ##__VA_ARGS__); \
  186. else \
  187. pr_info("%s: " msg, __func__, ##__VA_ARGS__); \
  188. } else \
  189. pr_debug("%s: " msg, __func__, ##__VA_ARGS__); \
  190. if (dev) { \
  191. if ((dev)->ipc_loglevel <= 3) \
  192. SPS_IPC(3, dev, msg, ##__VA_ARGS__); \
  193. } \
  194. } while (0)
  195. #else
  196. #define SPS_DBG3(dev, msg, args...) pr_debug(msg, ##args)
  197. #define SPS_DBG2(dev, msg, args...) pr_debug(msg, ##args)
  198. #define SPS_DBG1(dev, msg, args...) pr_debug(msg, ##args)
  199. #define SPS_DBG(dev, msg, args...) pr_debug(msg, ##args)
  200. #define SPS_INFO(dev, msg, args...) pr_info(msg, ##args)
  201. #define SPS_ERR(dev, msg, args...) pr_err(msg, ##args)
  202. #define SPS_DUMP(msg, args...) pr_info(msg, ##args)
  203. #endif
  204. /* End point parameters */
  205. struct sps_conn_end_pt {
  206. unsigned long dev; /* Device handle of BAM */
  207. phys_addr_t bam_phys; /* Physical address of BAM. */
  208. u32 pipe_index; /* Pipe index */
  209. u32 event_threshold; /* Pipe event threshold */
  210. u32 lock_group; /* The lock group this pipe belongs to */
  211. void *bam;
  212. };
  213. /* Connection bookkeeping descriptor struct */
  214. struct sps_connection {
  215. struct list_head list;
  216. /* Source end point parameters */
  217. struct sps_conn_end_pt src;
  218. /* Destination end point parameters */
  219. struct sps_conn_end_pt dest;
  220. /* Resource parameters */
  221. struct sps_mem_buffer desc; /* Descriptor FIFO */
  222. struct sps_mem_buffer data; /* Data FIFO (BAM-to-BAM mode only) */
  223. u32 config; /* Client specified connection configuration */
  224. /* Connection state */
  225. void *client_src;
  226. void *client_dest;
  227. int refs; /* Reference counter */
  228. /* Dynamically allocated resources, if required */
  229. u32 alloc_src_pipe; /* Source pipe index */
  230. u32 alloc_dest_pipe; /* Destination pipe index */
  231. /* Physical address of descriptor FIFO */
  232. phys_addr_t alloc_desc_base;
  233. phys_addr_t alloc_data_base; /* Physical address of data FIFO */
  234. };
  235. /* Event bookkeeping descriptor struct */
  236. struct sps_q_event {
  237. struct list_head list;
  238. /* Event payload data */
  239. struct sps_event_notify notify;
  240. };
  241. /* Memory heap statistics */
  242. struct sps_mem_stats {
  243. u32 base_addr;
  244. u32 size;
  245. u32 blocks_used;
  246. u32 bytes_used;
  247. u32 max_bytes_used;
  248. };
  249. enum sps_bam_type {
  250. SPS_BAM_LEGACY,
  251. SPS_BAM_NDP,
  252. SPS_BAM_NDP_4K
  253. };
  254. #ifdef CONFIG_DEBUG_FS
  255. /* record debug info for debugfs */
  256. void sps_debugfs_record(const char *msg);
  257. #endif
  258. /* output the content of BAM-level registers */
  259. void print_bam_reg(void *virt_addr);
  260. /* output the content of BAM pipe registers */
  261. void print_bam_pipe_reg(void *virt_addr, u32 pipe_index);
  262. /* output the content of selected BAM-level registers */
  263. void print_bam_selected_reg(void *virt_addr, u32 pipe_index);
  264. /* output the content of selected BAM pipe registers */
  265. void print_bam_pipe_selected_reg(void *virt_addr, u32 pipe_index);
  266. /* output descriptor FIFO of a pipe */
  267. void print_bam_pipe_desc_fifo(void *virt_addr, u32 pipe_index, u32 option);
  268. /* output BAM_TEST_BUS_REG */
  269. void print_bam_test_bus_reg(void *base, u32 tb_sel);
  270. /* halt and un-halt a pipe */
  271. void bam_pipe_halt(void *base, u32 pipe, bool halt);
  272. /**
  273. * Translate physical to virtual address
  274. *
  275. * This Function translates physical to virtual address.
  276. *
  277. * @phys_addr - physical address to translate
  278. *
  279. * @return virtual memory pointer
  280. *
  281. */
  282. void *spsi_get_mem_ptr(phys_addr_t phys_addr);
  283. /**
  284. * Allocate I/O (pipe) memory
  285. *
  286. * This function allocates target I/O (pipe) memory.
  287. *
  288. * @bytes - number of bytes to allocate
  289. *
  290. * @return physical address of allocated memory, or SPS_ADDR_INVALID on error
  291. */
  292. phys_addr_t sps_mem_alloc_io(u32 bytes);
  293. /**
  294. * Free I/O (pipe) memory
  295. *
  296. * This function frees target I/O (pipe) memory.
  297. *
  298. * @phys_addr - physical address of memory to free
  299. *
  300. * @bytes - number of bytes to free.
  301. */
  302. void sps_mem_free_io(phys_addr_t phys_addr, u32 bytes);
  303. /**
  304. * Find matching connection mapping
  305. *
  306. * This function searches for a connection mapping that matches the
  307. * parameters supplied by the client. If a match is found, the client's
  308. * parameter struct is updated with the values specified in the mapping.
  309. *
  310. * @connect - pointer to client connection parameters
  311. *
  312. * @return 0 if match is found, negative value otherwise
  313. *
  314. */
  315. int sps_map_find(struct sps_connect *connect);
  316. /**
  317. * Allocate a BAM DMA pipe
  318. *
  319. * This function allocates a BAM DMA pipe, and is intended to be called
  320. * internally from the BAM resource manager. Allocation implies that
  321. * the pipe has been referenced by a client Connect() and is in use.
  322. *
  323. * BAM DMA is permissive with activations, and allows a pipe to be allocated
  324. * with or without a client-initiated allocation. This allows the client to
  325. * specify exactly which pipe should be used directly through the Connect() API.
  326. * sps_dma_alloc_chan() does not allow the client to specify the pipes/channel.
  327. *
  328. * @bam - pointer to BAM device descriptor
  329. *
  330. * @pipe_index - pipe index
  331. *
  332. * @dir - pipe direction
  333. *
  334. * @return 0 on success, negative value on error
  335. */
  336. int sps_dma_pipe_alloc(void *bam, u32 pipe_index, enum sps_mode dir);
  337. /**
  338. * Enable a BAM DMA pipe
  339. *
  340. * This function enables the channel associated with a BAM DMA pipe, and
  341. * is intended to be called internally from the BAM resource manager.
  342. * Enable must occur *after* the pipe has been enabled so that proper
  343. * sequencing between pipe and DMA channel enables can be enforced.
  344. *
  345. * @bam - pointer to BAM device descriptor
  346. *
  347. * @pipe_index - pipe index
  348. *
  349. * @return 0 on success, negative value on error
  350. *
  351. */
  352. int sps_dma_pipe_enable(void *bam, u32 pipe_index);
  353. /**
  354. * Free a BAM DMA pipe
  355. *
  356. * This function disables and frees a BAM DMA pipe, and is intended to be
  357. * called internally from the BAM resource manager. This must occur *after*
  358. * the pipe has been disabled/reset so that proper sequencing between pipe and
  359. * DMA channel resets can be enforced.
  360. *
  361. * @bam_arg - pointer to BAM device descriptor
  362. *
  363. * @pipe_index - pipe index
  364. *
  365. * @return 0 on success, negative value on error
  366. *
  367. */
  368. int sps_dma_pipe_free(void *bam, u32 pipe_index);
  369. /**
  370. * Initialize driver memory module
  371. *
  372. * This function initializes the driver memory module.
  373. *
  374. * @pipemem_phys_base - Pipe-Memory physical base.
  375. *
  376. * @pipemem_size - Pipe-Memory size.
  377. *
  378. * @return 0 on success, negative value on error
  379. *
  380. */
  381. int sps_mem_init(phys_addr_t pipemem_phys_base, u32 pipemem_size);
  382. /**
  383. * De-initialize driver memory module
  384. *
  385. * This function de-initializes the driver memory module.
  386. *
  387. * @return 0 on success, negative value on error
  388. *
  389. */
  390. int sps_mem_de_init(void);
  391. /**
  392. * Initialize BAM DMA module
  393. *
  394. * This function initializes the BAM DMA module.
  395. *
  396. * @bam_props - pointer to BAM DMA devices BSP configuration properties
  397. *
  398. * @return 0 on success, negative value on error
  399. *
  400. */
  401. int sps_dma_init(const struct sps_bam_props *bam_props);
  402. /**
  403. * De-initialize BAM DMA module
  404. *
  405. * This function de-initializes the SPS BAM DMA module.
  406. *
  407. */
  408. void sps_dma_de_init(void);
  409. /**
  410. * Initialize BAM DMA device
  411. *
  412. * This function initializes a BAM DMA device.
  413. *
  414. * @h - BAM handle
  415. *
  416. * @return 0 on success, negative value on error
  417. *
  418. */
  419. int sps_dma_device_init(unsigned long h);
  420. /**
  421. * De-initialize BAM DMA device
  422. *
  423. * This function de-initializes a BAM DMA device.
  424. *
  425. * @h - BAM handle
  426. *
  427. * @return 0 on success, negative value on error
  428. *
  429. */
  430. int sps_dma_device_de_init(unsigned long h);
  431. /**
  432. * Initialize connection mapping module
  433. *
  434. * This function initializes the SPS connection mapping module.
  435. *
  436. * @map_props - pointer to connection mapping BSP configuration properties
  437. *
  438. * @options - driver options bitflags (see SPS_OPT_*)
  439. *
  440. * @return 0 on success, negative value on error
  441. *
  442. */
  443. int sps_map_init(const struct sps_map *map_props, u32 options);
  444. /**
  445. * De-initialize connection mapping module
  446. *
  447. * This function de-initializes the SPS connection mapping module.
  448. *
  449. */
  450. void sps_map_de_init(void);
  451. /*
  452. * bam_pipe_reset - reset a BAM pipe.
  453. * @base: BAM virtual address
  454. * @pipe: pipe index
  455. *
  456. * This function resets a BAM pipe.
  457. */
  458. void bam_pipe_reset(void *base, u32 pipe);
  459. /*
  460. * bam_disable_pipe - disable a BAM pipe.
  461. * @base: BAM virtual address
  462. * @pipe: pipe index
  463. *
  464. * This function disables a BAM pipe.
  465. */
  466. void bam_disable_pipe(void *base, u32 pipe);
  467. #endif /* _SPSI_H_ */