ipclite.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved..
  4. */
  5. #include <linux/hwspinlock.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <dt-bindings/soc/qcom,ipcc.h>
  9. #include <linux/mailbox_client.h>
  10. #include <linux/mailbox_controller.h>
  11. #include "ipclite_client.h"
  12. #define IPCMEM_INIT_COMPLETED 0x1
  13. #define ACTIVE_CHANNEL 0x1
  14. #define IPCMEM_TOC_SIZE (4*1024)
  15. #define IPCMEM_TOC_VAR_OFFSET 0x100
  16. #define MAX_CHANNEL_SIGNALS 6
  17. #define GLOBAL_ATOMIC_SUPPORT_BMSK 0x1UL
  18. #define IPCLITE_MSG_SIGNAL 0
  19. #define IPCLITE_MEM_INIT_SIGNAL 1
  20. #define IPCLITE_VERSION_SIGNAL 2
  21. #define IPCLITE_TEST_SIGNAL 3
  22. #define IPCLITE_SSR_SIGNAL 4
  23. #define IPCLITE_DEBUG_SIGNAL 5
  24. /** Flag definitions for the entries */
  25. #define IPCMEM_FLAGS_ENABLE_READ_PROTECTION (0x01)
  26. #define IPCMEM_FLAGS_ENABLE_WRITE_PROTECTION (0x02)
  27. #define IPCMEM_FLAGS_ENABLE_RW_PROTECTION \
  28. (IPCMEM_FLAGS_ENABLE_READ_PROTECTION | \
  29. IPCMEM_FLAGS_ENABLE_WRITE_PROTECTION)
  30. #define IPCMEM_FLAGS_IGNORE_PARTITION (0x00000004)
  31. /*Hardcoded macro to identify local host on each core*/
  32. #define LOCAL_HOST IPCMEM_APPS
  33. /* Timeout (ms) for the trylock of remote spinlocks */
  34. #define HWSPINLOCK_TIMEOUT 1000
  35. #define IPCLITE_DEBUG_INFO_SIZE 256
  36. #define IPCLITE_CORE_DBG_LABEL "APSS:"
  37. #define IPCLITE_LOG_MSG_SIZE 100
  38. #define IPCLITE_LOG_BUF_SIZE 512
  39. #define IPCLITE_DBG_LABEL_SIZE 5
  40. #define IPCLITE_SIGNAL_LABEL_SIZE 10
  41. #define PREV_INDEX 2
  42. #define ADD_OFFSET(x, y) ((void *)((size_t)x + y))
  43. #define IPCLITE_OS_LOG(__level, __fmt, arg...) \
  44. do { \
  45. if (ipclite_debug_level & __level) { \
  46. if (ipclite_debug_control & IPCLITE_DMESG_LOG) \
  47. pr_info(IPCLITE_CORE_DBG_LABEL "%s:"__fmt, \
  48. ipclite_dbg_label[__level], ## arg); \
  49. if (ipclite_debug_control & IPCLITE_INMEM_LOG) \
  50. ipclite_inmem_log(IPCLITE_CORE_DBG_LABEL "%s:"__fmt, \
  51. ipclite_dbg_label[__level], ## arg); \
  52. } \
  53. } while (0)
  54. #define ATOMIC_HW_MUTEX_ACQUIRE (global_atomic_support ?: ipclite_hw_mutex_acquire())
  55. #define ATOMIC_HW_MUTEX_RELEASE (global_atomic_support ?: ipclite_hw_mutex_release())
  56. /**
  57. * enum ipclite_channel_status - channel status
  58. *
  59. * INACTIVE : Channel uninitialized or init failed
  60. * IN_PROGRESS : Channel init passed, awaiting confirmation from remote host
  61. * ACTIVE : Channel init passed in local and remote host, thus active
  62. */
  63. enum ipclite_channel_status {
  64. INACTIVE = 0,
  65. IN_PROGRESS = 1,
  66. ACTIVE = 2,
  67. };
  68. enum ipclite_debug_level {
  69. IPCLITE_ERR = 0x0001,
  70. IPCLITE_WARN = 0x0002,
  71. IPCLITE_INFO = 0x0004,
  72. IPCLITE_DBG = 0x0008,
  73. };
  74. enum ipclite_debug_control {
  75. IPCLITE_DMESG_LOG = 0x0001,
  76. IPCLITE_DBG_STRUCT = 0x0002,
  77. IPCLITE_INMEM_LOG = 0x0004,
  78. };
  79. enum ipclite_debug_dump {
  80. IPCLITE_DUMP_DBG_STRUCT = 0x0001,
  81. IPCLITE_DUMP_INMEM_LOG = 0x0002,
  82. IPCLITE_DUMP_SSR = 0x0004,
  83. };
  84. static const char ipclite_dbg_label[][IPCLITE_DBG_LABEL_SIZE] = {
  85. [IPCLITE_ERR] = "err",
  86. [IPCLITE_WARN] = "warn",
  87. [IPCLITE_INFO] = "info",
  88. [IPCLITE_DBG] = "dbg"
  89. };
  90. /**
  91. * IPCMEM Debug Structure Definitions
  92. * - Present in Local Memory
  93. */
  94. struct ipclite_debug_info_host {
  95. uint32_t numsig_sent; //no. of signals sent from the core
  96. uint32_t numsig_recv; //no. of signals received on the core
  97. uint32_t tx_wr_index; //write index of tx queue
  98. uint32_t tx_rd_index; //read index of tx queue
  99. uint32_t rx_wr_index; //write index of rx queue
  100. uint32_t rx_rd_index; //read index of rx queue
  101. uint32_t num_intr; //no. of interrupts received on the core
  102. uint32_t prev_tx_wr_index[PREV_INDEX]; //previous write index of tx queue
  103. uint32_t prev_tx_rd_index[PREV_INDEX]; //previous read index of tx queue
  104. uint32_t prev_rx_wr_index[PREV_INDEX]; //previous write index of rx queue
  105. uint32_t prev_rx_rd_index[PREV_INDEX]; //previous read index of rx queue
  106. };
  107. struct ipclite_debug_info_overall {
  108. uint32_t total_numsig_sent; //total no. of signals sent
  109. uint32_t total_numsig_recv; //total no. of signals received
  110. uint32_t last_sent_host_id; //last signal sent to host
  111. uint32_t last_recv_host_id; //last signal received from host
  112. uint32_t last_sigid_sent; //last sent signal id
  113. uint32_t last_sigid_recv; //last received signal id
  114. };
  115. struct ipclite_debug_info {
  116. uint32_t debug_version;
  117. uint32_t debug_level;
  118. uint32_t debug_control;
  119. uint32_t debug_dump;
  120. uint32_t debug_log_index;
  121. };
  122. struct ipclite_debug_inmem_buf {
  123. char IPCLITELog[IPCLITE_LOG_BUF_SIZE][IPCLITE_LOG_MSG_SIZE];
  124. };
  125. struct ipclite_debug_struct {
  126. struct ipclite_debug_info_overall dbg_info_overall;
  127. struct ipclite_debug_info_host dbg_info_host[IPCMEM_NUM_HOSTS];
  128. };
  129. /**
  130. * IPCMEM TOC Structure Definitions
  131. * - Present in toc in shared memory
  132. */
  133. struct ipcmem_host_info {
  134. uint32_t hwlock_owner;
  135. uint32_t configured_host;
  136. };
  137. struct ipcmem_partition_entry {
  138. uint32_t base_offset; /*partition offset from IPCMEM base*/
  139. uint32_t size; /*partition size*/
  140. uint32_t flags; /*partition flags if required*/
  141. uint32_t host0; /*subsystem 0 who can access this partition*/
  142. uint32_t host1; /*subsystem 1 who can access this partition*/
  143. uint32_t reserved; /*legacy partition active status*/
  144. };
  145. struct ipcmem_partition_info {
  146. uint32_t num_entries; /* Number of channel partitions */
  147. uint32_t entry_size; /* Size of partition_entry structure */
  148. };
  149. struct ipcmem_offsets {
  150. uint32_t host_info;
  151. uint32_t global_entry;
  152. uint32_t partition_info;
  153. uint32_t partition_entry;
  154. uint32_t debug;
  155. uint32_t reserved; /*Padded for 64-bit alignment*/
  156. };
  157. /**
  158. * Any change in TOC header size can only be accomodated with
  159. * major version change, as it is not backward compatible.
  160. */
  161. struct ipcmem_toc_header {
  162. uint32_t magic_number; /*Checksum of TOC*/
  163. uint32_t init_done; /*TOC initialization status*/
  164. uint32_t major_version;
  165. uint32_t minor_version;
  166. uint64_t feature_mask;
  167. uint32_t reserved[6]; /*Padded for future use and 64-bit alignment*/
  168. };
  169. /**
  170. * struct ipcmem_toc - Table of contents in ipcmem
  171. *
  172. * @hdr : Header to check for toc integrity, version and features
  173. * @offsets : List of offsetted structures and partition entries
  174. * available in the toc data region (ipcmem_toc_data)
  175. */
  176. struct ipcmem_toc {
  177. struct ipcmem_toc_header hdr;
  178. struct ipcmem_offsets offsets;
  179. /* ---------------------------------------
  180. * ipcmem_toc_data @ 256-byte offset
  181. * struct ipcmem_host_info host_info;
  182. * struct ipcmem_partition_entry global_entry;
  183. * struct ipcmem_partition_info partition_info;
  184. * struct ipcmem_partition_entry partition_entry[num_entries];
  185. * ---------------------------------------
  186. */
  187. };
  188. /**
  189. * IPCMEM Partition Structure Definitions
  190. * - Present in partitions in shared memory
  191. */
  192. struct global_partition_header {
  193. uint32_t partition_type;
  194. uint32_t region_offset;
  195. uint32_t region_size;
  196. };
  197. struct ipcmem_global_partition {
  198. struct global_partition_header hdr;
  199. };
  200. struct ipcmem_partition_header {
  201. uint32_t type; /*partition type*/
  202. uint32_t desc_offset; /*descriptor offset*/
  203. uint32_t desc_size; /*descriptor size*/
  204. uint32_t fifo0_offset; /*fifo 0 offset*/
  205. uint32_t fifo0_size; /*fifo 0 size*/
  206. uint32_t fifo1_offset; /*fifo 1 offset*/
  207. uint32_t fifo1_size; /*fifo 1 size*/
  208. uint32_t status; /*partition status*/
  209. };
  210. struct ipcmem_partition {
  211. struct ipcmem_partition_header hdr;
  212. };
  213. /**
  214. * IPCMEM Helper Structure Definitions
  215. * - Present in local memory
  216. * - Can have pointers to toc and partitions in shared memory
  217. */
  218. /*Pointers to offsetted structures in TOC*/
  219. struct ipcmem_toc_data {
  220. struct ipcmem_host_info *host_info;
  221. struct ipcmem_partition_entry *global_entry;
  222. struct ipcmem_partition_info *partition_info;
  223. struct ipcmem_partition_entry *partition_entry;
  224. };
  225. struct ipcmem_region {
  226. u64 aux_base;
  227. void __iomem *virt_base;
  228. uint32_t size;
  229. };
  230. struct ipclite_mem {
  231. struct ipcmem_toc *toc;
  232. struct ipcmem_toc_data toc_data;
  233. struct ipcmem_region mem;
  234. struct ipcmem_global_partition *global_partition;
  235. struct ipcmem_partition **partition;
  236. };
  237. /**
  238. * IPCLite Structure Definitions
  239. * - Present in local memory
  240. * - Can have pointers to partitions in shared memory
  241. */
  242. struct ipclite_fifo {
  243. uint32_t length;
  244. __le32 *tail;
  245. __le32 *head;
  246. void *fifo;
  247. size_t (*avail)(struct ipclite_fifo *fifo);
  248. void (*peak)(struct ipclite_fifo *fifo,
  249. void *data, size_t count);
  250. void (*advance)(struct ipclite_fifo *fifo,
  251. size_t count, uint32_t core_id);
  252. void (*write)(struct ipclite_fifo *fifo,
  253. const void *data, size_t dlen, uint32_t core_id, uint32_t signal_id);
  254. void (*reset)(struct ipclite_fifo *fifo);
  255. };
  256. struct ipclite_irq_info {
  257. struct mbox_client mbox_client;
  258. struct mbox_chan *mbox_chan;
  259. int irq;
  260. int signal_id;
  261. char irqname[32];
  262. };
  263. struct ipclite_client {
  264. IPCLite_Client callback;
  265. void *priv_data;
  266. int reg_complete;
  267. };
  268. struct ipclite_channel {
  269. uint32_t remote_pid;
  270. struct ipclite_fifo *tx_fifo;
  271. struct ipclite_fifo *rx_fifo;
  272. spinlock_t tx_lock;
  273. struct ipclite_irq_info irq_info[MAX_CHANNEL_SIGNALS];
  274. struct ipclite_client client;
  275. uint32_t channel_version;
  276. uint32_t version_finalised;
  277. uint32_t *gstatus_ptr;
  278. uint32_t status;
  279. };
  280. /*Single structure that defines everything about IPCLite*/
  281. struct ipclite_info {
  282. struct device *dev;
  283. struct ipclite_channel channel[IPCMEM_NUM_HOSTS];
  284. struct ipclite_mem ipcmem;
  285. struct hwspinlock *hwlock;
  286. unsigned long hw_mutex_flags;
  287. };
  288. /*Default partition parameters*/
  289. #define DEFAULT_PARTITION_TYPE 0x0
  290. #define DEFAULT_PARTITION_STATUS INACTIVE
  291. #define DEFAULT_PARTITION_HDR_SIZE 1024
  292. #define DEFAULT_DESCRIPTOR_OFFSET 1024
  293. #define DEFAULT_DESCRIPTOR_SIZE (3*1024)
  294. #define DEFAULT_FIFO0_OFFSET (4*1024)
  295. #define DEFAULT_FIFO0_SIZE (8*1024)
  296. #define DEFAULT_FIFO1_OFFSET (12*1024)
  297. #define DEFAULT_FIFO1_SIZE (8*1024)
  298. #define DEFAULT_PARTITION_SIZE (32*1024)
  299. #define DEFAULT_PARTITION_FLAGS IPCMEM_FLAGS_ENABLE_RW_PROTECTION
  300. /*Loopback partition parameters*/
  301. #define LOOPBACK_PARTITION_TYPE 0x1
  302. /*Global partition parameters*/
  303. #define GLOBAL_PARTITION_TYPE 0xFF
  304. #define GLOBAL_PARTITION_HDR_SIZE (4*1024)
  305. #define GLOBAL_REGION_OFFSET (4*1024)
  306. #define GLOBAL_REGION_SIZE (124*1024)
  307. #define GLOBAL_PARTITION_SIZE (128*1024)
  308. #define GLOBAL_PARTITION_FLAGS IPCMEM_FLAGS_ENABLE_RW_PROTECTION
  309. /*Debug partition parameters*/
  310. #define DEBUG_PARTITION_SIZE (64*1024)
  311. const struct ipcmem_partition_header default_partition_hdr = {
  312. DEFAULT_PARTITION_TYPE,
  313. DEFAULT_DESCRIPTOR_OFFSET,
  314. DEFAULT_DESCRIPTOR_SIZE,
  315. DEFAULT_FIFO0_OFFSET,
  316. DEFAULT_FIFO0_SIZE,
  317. DEFAULT_FIFO1_OFFSET,
  318. DEFAULT_FIFO1_SIZE,
  319. DEFAULT_PARTITION_STATUS,
  320. };
  321. /* TX and RX FIFO point to same location for such loopback partition type
  322. * (FIFO0 offset = FIFO1 offset)
  323. */
  324. const struct ipcmem_partition_header loopback_partition_hdr = {
  325. LOOPBACK_PARTITION_TYPE,
  326. DEFAULT_DESCRIPTOR_OFFSET,
  327. DEFAULT_DESCRIPTOR_SIZE,
  328. DEFAULT_FIFO0_OFFSET,
  329. DEFAULT_FIFO0_SIZE,
  330. DEFAULT_FIFO0_OFFSET,
  331. DEFAULT_FIFO0_SIZE,
  332. DEFAULT_PARTITION_STATUS,
  333. };
  334. const struct global_partition_header global_partition_hdr = {
  335. GLOBAL_PARTITION_TYPE,
  336. GLOBAL_REGION_OFFSET,
  337. GLOBAL_REGION_SIZE,
  338. };