adsprpc_shared.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef ADSPRPC_SHARED_H
  7. #define ADSPRPC_SHARED_H
  8. #include <linux/types.h>
  9. #include <linux/cdev.h>
  10. #include "fastrpc_shared.h"
  11. #define FASTRPC_GLINK_GUID "fastrpcglink-apps-dsp"
  12. #define FASTRPC_SMD_GUID "fastrpcsmd-apps-dsp"
  13. #define DEVICE_NAME "adsprpc-smd"
  14. #define DEVICE_NAME_SECURE "adsprpc-smd-secure"
  15. /* Pre-defined parameter for print gfa structure*/
  16. #define smq_invoke_ctx_params "pid: %d, tgid: %d, handle: %d, sc: 0x%x, fl: %p, fd: %p, magic: %d\n"
  17. #define fastrpc_file_params "fl->tgid: %d, fl->cid: %d, fl->ssrcount: %p, fl->pd: %d, fl->profile: %p, fl->mode: %p, fl->tgid_open: %d, fl->num_cached_buf: %d, num_pers_hdrs: %d, fl->sessionid: %d, fl->servloc_name: %s, fl->file_close: %d, fl->dsp_proc_init: %d,fl->apps: %p, fl->qos_request: %d, fl->dev_minor: %d, fl->debug_buf: %s fl->debug_buf_alloced_attempted: %d, fl->wake_enable: %d, fl->ws_timeout: %d, fl->untrusted_process: %d\n"
  18. #define fastrpc_mmap_params "fd: %d, flags: %p, buf: %p, phys: %p, size : %d, va : %p, map->raddr: %p, len : %d, refs : %d, secure: %d\n"
  19. #define fastrpc_buf_params "buf->fl: %p, buf->phys: %p, buf->virt: %p, buf->size: %d, buf->dma_attr: %ld, buf->raddr: %p, buf->flags: %d, buf->type: %d, buf->in_use: %d\n"
  20. /* Set for buffers that have no virtual mapping in userspace */
  21. #define FASTRPC_ATTR_NOVA 0x1
  22. /* Set for buffers that are NOT dma coherent */
  23. #define FASTRPC_ATTR_NON_COHERENT 0x2
  24. /* Set for buffers that are dma coherent */
  25. #define FASTRPC_ATTR_COHERENT 0x4
  26. /* Fastrpc attribute for keeping the map persistent */
  27. #define FASTRPC_ATTR_KEEP_MAP 0x8
  28. /* Fastrpc attribute for no mapping of fd */
  29. #define FASTRPC_ATTR_NOMAP (16)
  30. /*
  31. * Fastrpc attribute to skip flush by fastrpc
  32. */
  33. #define FASTRPC_ATTR_FORCE_NOFLUSH (32)
  34. /*
  35. * Fastrpc attribute to skip invalidate by fastrpc
  36. */
  37. #define FASTRPC_ATTR_FORCE_NOINVALIDATE (64)
  38. /* Driver should operate in parallel with the co-processor */
  39. #define FASTRPC_MODE_PARALLEL 0
  40. /* Driver should operate in serial mode with the co-processor */
  41. #define FASTRPC_MODE_SERIAL 1
  42. /* Driver should operate in profile mode with the co-processor */
  43. #define FASTRPC_MODE_PROFILE 2
  44. /* Set FastRPC session ID to 1 */
  45. #define FASTRPC_MODE_SESSION 4
  46. /* Retrives number of input buffers from the scalars parameter */
  47. #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
  48. /* Retrives number of output buffers from the scalars parameter */
  49. #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
  50. /* Retrives number of input handles from the scalars parameter */
  51. #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
  52. /* Retrives number of output handles from the scalars parameter */
  53. #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
  54. /* Remote domains ID */
  55. #define ADSP_DOMAIN_ID (0)
  56. #define MDSP_DOMAIN_ID (1)
  57. #define SDSP_DOMAIN_ID (2)
  58. #define CDSP_DOMAIN_ID (3)
  59. #define MAX_DOMAIN_ID CDSP_DOMAIN_ID
  60. #define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/
  61. #define NUM_SESSIONS 14 /* max 11 compute, 3 cpz */
  62. #define VALID_FASTRPC_CID(cid) \
  63. (cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS)
  64. #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) +\
  65. REMOTE_SCALARS_OUTBUFS(sc) +\
  66. REMOTE_SCALARS_INHANDLES(sc) +\
  67. REMOTE_SCALARS_OUTHANDLES(sc))
  68. #define REMOTE_SCALARS_MAKEX(attr, method, in, out, oin, oout) \
  69. ((((uint32_t) (attr) & 0x7) << 29) | \
  70. (((uint32_t) (method) & 0x1f) << 24) | \
  71. (((uint32_t) (in) & 0xff) << 16) | \
  72. (((uint32_t) (out) & 0xff) << 8) | \
  73. (((uint32_t) (oin) & 0x0f) << 4) | \
  74. ((uint32_t) (oout) & 0x0f))
  75. #define REMOTE_SCALARS_MAKE(method, in, out) \
  76. REMOTE_SCALARS_MAKEX(0, method, in, out, 0, 0)
  77. #ifdef VERIFY_PRINT_ERROR
  78. #define VERIFY_EPRINTF(format, ...) pr_err(format, ##__VA_ARGS__)
  79. #else
  80. #define VERIFY_EPRINTF(format, args) ((void)0)
  81. #endif
  82. #ifndef VERIFY_PRINT_INFO
  83. #define VERIFY_IPRINTF(args) ((void)0)
  84. #endif
  85. #ifndef VERIFY
  86. #define __STR__(x) #x ":"
  87. #define __TOSTR__(x) __STR__(x)
  88. #define __FILE_LINE__ __FILE__ ":" __TOSTR__(__LINE__)
  89. #define __ADSPRPC_LINE__ "adsprpc:" __TOSTR__(__LINE__)
  90. #define VERIFY(err, val) \
  91. do {\
  92. VERIFY_IPRINTF(__FILE_LINE__"info: calling: " #val "\n");\
  93. if ((val) == 0) {\
  94. (err) = (err) == 0 ? -1 : (err);\
  95. VERIFY_EPRINTF(__ADSPRPC_LINE__" error: %d: "#val "\n", (err));\
  96. } else {\
  97. VERIFY_IPRINTF(__FILE_LINE__"info: passed: " #val "\n");\
  98. } \
  99. } while (0)
  100. #endif
  101. #define ADSPRPC_ERR(fmt, args...)\
  102. pr_err("Error: adsprpc (%d): %s: %s: " fmt, __LINE__,\
  103. current->comm, __func__, ##args)
  104. #define ADSPRPC_INFO(fmt, args...)\
  105. pr_info("Info: adsprpc (%d): %s: %s: " fmt, __LINE__,\
  106. current->comm, __func__, ##args)
  107. #define ADSPRPC_WARN(fmt, args...)\
  108. pr_warn("Warning: adsprpc (%d): %s: %s: " fmt, __LINE__,\
  109. current->comm, __func__, ##args)
  110. #define ADSPRPC_DEBUG(fmt, args...)\
  111. pr_debug("Debug: adsprpc (%d): %s: %s: " fmt, __LINE__,\
  112. current->comm, __func__, ##args)
  113. #define DEBUG_PRINT_SIZE_LIMIT (512*1024)
  114. #define remote_arg64_t union remote_arg64
  115. struct remote_buf64 {
  116. uint64_t pv;
  117. uint64_t len;
  118. };
  119. struct remote_dma_handle64 {
  120. int fd;
  121. uint32_t offset;
  122. uint32_t len;
  123. };
  124. union remote_arg64 {
  125. struct remote_buf64 buf;
  126. struct remote_dma_handle64 dma;
  127. uint32_t h;
  128. };
  129. struct remote_buf {
  130. void *pv; /* buffer pointer */
  131. size_t len; /* length of buffer */
  132. };
  133. /* structure to hold fd and size of buffer shared with DSP,
  134. * which contains inital debug parameters that needs to be passed
  135. * during process initialization.
  136. */
  137. struct fastrpc_proc_sharedbuf_info {
  138. int buf_fd;
  139. int buf_size;
  140. };
  141. struct remote_dma_handle {
  142. int fd;
  143. uint32_t offset;
  144. };
  145. union remote_arg {
  146. struct remote_buf buf; /* buffer info */
  147. struct remote_dma_handle dma;
  148. uint32_t h; /* remote handle */
  149. };
  150. struct fastrpc_async_job {
  151. uint32_t isasyncjob; /* flag to distinguish async job */
  152. uint64_t jobid; /* job id generated by user */
  153. uint32_t reserved; /* reserved */
  154. };
  155. /* FastRPC ioctl structure to set session related info */
  156. struct fastrpc_proc_sess_info {
  157. uint32_t domain_id; /* Set the remote subsystem, Domain ID of the session */
  158. uint32_t session_id; /* Unused, Set the Session ID on remote subsystem */
  159. uint32_t pd_type; /* Set the process type on remote subsystem */
  160. uint32_t sharedcb; /* Unused, Session can share context bank with other sessions */
  161. };
  162. /* INIT a new process or attach to guestos */
  163. enum fastrpc_init_flags {
  164. FASTRPC_INIT_NO_CREATE = -1,
  165. FASTRPC_INIT_ATTACH = 0,
  166. FASTRPC_INIT_CREATE = 1,
  167. FASTRPC_INIT_CREATE_STATIC = 2,
  168. FASTRPC_INIT_ATTACH_SENSORS = 3,
  169. };
  170. enum fastrpc_invoke2_type {
  171. FASTRPC_INVOKE2_ASYNC = 1,
  172. FASTRPC_INVOKE2_ASYNC_RESPONSE = 2,
  173. FASTRPC_INVOKE2_KERNEL_OPTIMIZATIONS,
  174. FASTRPC_INVOKE2_STATUS_NOTIF,
  175. FASTRPC_INVOKE2_PROC_SHAREDBUF_INFO,
  176. /* Set session info of remote sub system */
  177. FASTRPC_INVOKE2_SESS_INFO,
  178. };
  179. /**
  180. * Control flags for mapping memory on DSP user process
  181. */
  182. enum fastrpc_map_flags {
  183. /**
  184. * Map memory pages with RW- permission and CACHE WRITEBACK.
  185. * The driver is responsible for cache maintenance when passed
  186. * the buffer to FastRPC calls. Same virtual address will be
  187. * assigned for subsequent FastRPC calls.
  188. */
  189. FASTRPC_MAP_STATIC = 0,
  190. /* Reserved */
  191. FASTRPC_MAP_RESERVED,
  192. /**
  193. * Map memory pages with RW- permission and CACHE WRITEBACK.
  194. * Mapping tagged with a file descriptor. User is responsible for
  195. * CPU and DSP cache maintenance for the buffer. Get virtual address
  196. * of buffer on DSP using HAP_mmap_get() and HAP_mmap_put() APIs.
  197. */
  198. FASTRPC_MAP_FD = 2,
  199. /**
  200. * Mapping delayed until user call HAP_mmap() and HAP_munmap()
  201. * functions on DSP. It is useful to map a buffer with cache modes
  202. * other than default modes. User is responsible for CPU and DSP
  203. * cache maintenance for the buffer.
  204. */
  205. FASTRPC_MAP_FD_DELAYED,
  206. /**
  207. * This flag is used to skip CPU mapping,
  208. * otherwise behaves similar to FASTRPC_MAP_FD_DELAYED flag.
  209. */
  210. FASTRPC_MAP_FD_NOMAP = 16,
  211. FASTRPC_MAP_MAX,
  212. };
  213. /*
  214. * This enum is shared with DSP. So, existing values should NOT
  215. * be modified. Only new members can be added.
  216. */
  217. enum dsp_map_flags {
  218. /* Add memory to static PD pool, protection thru XPU */
  219. ADSP_MMAP_HEAP_ADDR = 4,
  220. /* MAP static DMA buffer on DSP User PD */
  221. ADSP_MMAP_DMA_BUFFER = 6,
  222. /* Add memory to static PD pool, protection thru hypervisor */
  223. ADSP_MMAP_REMOTE_HEAP_ADDR = 8,
  224. /* Add memory to userPD pool, for user heap */
  225. ADSP_MMAP_ADD_PAGES = 0x1000,
  226. /* Add memory to userPD pool, for LLC heap */
  227. ADSP_MMAP_ADD_PAGES_LLC = 0x3000,
  228. /* Map persistent header buffer on DSP */
  229. ADSP_MMAP_PERSIST_HDR = 0x4000,
  230. };
  231. enum fastrpc_control_type {
  232. FASTRPC_CONTROL_LATENCY = 1,
  233. /* Share SMMU context bank */
  234. FASTRPC_CONTROL_SMMU = 2,
  235. FASTRPC_CONTROL_KALLOC = 3,
  236. FASTRPC_CONTROL_WAKELOCK = 4,
  237. FASTRPC_CONTROL_PM = 5,
  238. /* Clean process on DSP */
  239. FASTRPC_CONTROL_DSPPROCESS_CLEAN = 6,
  240. FASTRPC_CONTROL_RPC_POLL = 7,
  241. FASTRPC_CONTROL_ASYNC_WAKE = 8,
  242. FASTRPC_CONTROL_NOTIF_WAKE = 9,
  243. };
  244. #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
  245. #define FASTRPC_MAX_ATTRIBUTES (260)
  246. enum fastrpc_dsp_capability {
  247. ASYNC_FASTRPC_CAP = 9,
  248. DMA_HANDLE_REVERSE_RPC_CAP = 129,
  249. };
  250. struct smq_null_invoke {
  251. uint64_t ctx; /* invoke caller context */
  252. uint32_t handle; /* handle to invoke */
  253. uint32_t sc; /* scalars structure describing the data */
  254. };
  255. struct smq_phy_page {
  256. uint64_t addr; /* physical address */
  257. uint64_t size; /* size of contiguous region */
  258. };
  259. struct smq_invoke_buf {
  260. int num; /* number of contiguous regions */
  261. int pgidx; /* index to start of contiguous region */
  262. };
  263. struct smq_invoke {
  264. struct smq_null_invoke header;
  265. struct smq_phy_page page; /* remote arg and list of pages address */
  266. };
  267. struct smq_msg {
  268. uint32_t pid; /* process group id */
  269. uint32_t tid; /* thread id */
  270. struct smq_invoke invoke;
  271. };
  272. struct smq_invoke_rsp {
  273. uint64_t ctx; /* invoke caller context */
  274. int retval; /* invoke return value */
  275. };
  276. enum fastrpc_response_flags {
  277. NORMAL_RESPONSE = 0,
  278. EARLY_RESPONSE = 1,
  279. USER_EARLY_SIGNAL = 2,
  280. COMPLETE_SIGNAL = 3,
  281. STATUS_RESPONSE = 4,
  282. POLL_MODE = 5,
  283. };
  284. enum fastrpc_process_create_state {
  285. PROCESS_CREATE_DEFAULT = 0, /* Process is not created */
  286. PROCESS_CREATE_IS_INPROGRESS = 1, /* Process creation is in progress */
  287. PROCESS_CREATE_SUCCESS = 2, /* Process creation is successful */
  288. };
  289. struct smq_invoke_rspv2 {
  290. uint64_t ctx; /* invoke caller context */
  291. int retval; /* invoke return value */
  292. uint32_t flags; /* early response flags */
  293. uint32_t early_wake_time; /* user predicted early wakeup time in us */
  294. uint32_t version; /* Version number for validation */
  295. };
  296. enum fastrpc_status_flags {
  297. FASTRPC_USERPD_UP = 0,
  298. FASTRPC_USERPD_EXIT = 1,
  299. FASTRPC_USERPD_FORCE_KILL = 2,
  300. FASTRPC_USERPD_EXCEPTION = 3,
  301. FASTRPC_DSP_SSR = 4,
  302. };
  303. struct smq_notif_rspv3 {
  304. uint64_t ctx; /* response context */
  305. uint32_t type; /* Notification type */
  306. int pid; /* user process pid */
  307. uint32_t status; /* userpd status notification */
  308. };
  309. enum fastrpc_process_exit_states {
  310. /* Process Default State */
  311. FASTRPC_PROCESS_DEFAULT_STATE = 0,
  312. /* Process exit initiated */
  313. FASTRPC_PROCESS_EXIT_START = 1,
  314. /* Process exit issued to DSP */
  315. FASTRPC_PROCESS_DSP_EXIT_INIT = 2,
  316. /* Process exit in DSP complete */
  317. FASTRPC_PROCESS_DSP_EXIT_COMPLETE = 3,
  318. /* Process exit in DSP error */
  319. FASTRPC_PROCESS_DSP_EXIT_ERROR = 4,
  320. };
  321. /*
  322. * Process types on remote subsystem
  323. * Always add new PD types at the end, before MAX_PD_TYPE
  324. */
  325. #define DEFAULT_UNUSED 0 /* pd type not configured for context banks */
  326. #define ROOT_PD 1 /* Root PD */
  327. #define AUDIO_STATICPD 2 /* ADSP Audio Static PD */
  328. #define SENSORS_STATICPD 3 /* ADSP Sensors Static PD */
  329. #define SECURE_STATICPD 4 /* CDSP Secure Static PD */
  330. #define OIS_STATICPD 5 /* ADSP OIS Static PD */
  331. #define CPZ_USERPD 6 /* CDSP CPZ USER PD */
  332. #define USERPD 7 /* DSP User Dynamic PD */
  333. #define GUEST_OS_SHARED 8 /* Legacy Guest OS Shared */
  334. #define MAX_PD_TYPE 9 /* Max PD type */
  335. struct fastrpc_file;
  336. int fastrpc_transport_send(int cid, void *rpc_msg, uint32_t rpc_msg_size, int tvm_remote_domain);
  337. inline int fastrpc_handle_rpc_response(void *data, int len, int cid);
  338. inline int verify_transport_device(int cid, int tvm_remote_domain);
  339. int fastrpc_transport_init(void);
  340. void fastrpc_transport_deinit(void);
  341. void fastrpc_transport_session_init(int cid, char *subsys);
  342. void fastrpc_transport_session_deinit(int cid);
  343. int fastrpc_wait_for_transport_interrupt(int cid, unsigned int flags);
  344. int fastrpc_set_tvm_remote_domain(struct fastrpc_file *fl, struct fastrpc_ioctl_init *init);
  345. void fastrpc_restart_drivers(int cid);
  346. static inline struct smq_invoke_buf *smq_invoke_buf_start(remote_arg64_t *pra,
  347. uint32_t sc)
  348. {
  349. unsigned int len = REMOTE_SCALARS_LENGTH(sc);
  350. return (struct smq_invoke_buf *)(&pra[len]);
  351. }
  352. static inline struct smq_phy_page *smq_phy_page_start(uint32_t sc,
  353. struct smq_invoke_buf *buf)
  354. {
  355. unsigned int nTotal = REMOTE_SCALARS_LENGTH(sc);
  356. return (struct smq_phy_page *)(&buf[nTotal]);
  357. }
  358. /*
  359. * Fastrpc context ID bit-map:
  360. *
  361. * bits 0-3 : type of remote PD
  362. * bit 4 : type of job (sync/async)
  363. * bit 5 : reserved
  364. * bits 6-15 : index in context table
  365. * bits 16-63 : incrementing context ID
  366. */
  367. #define FASTRPC_CTX_MAX (1024)
  368. /* Length of glink transaction history to store */
  369. #define GLINK_MSG_HISTORY_LEN (128)
  370. /* Type of fastrpc DMA bufs sent to DSP */
  371. enum fastrpc_buf_type {
  372. METADATA_BUF,
  373. COPYDATA_BUF,
  374. INITMEM_BUF,
  375. USERHEAP_BUF,
  376. };
  377. /* Types of RPC calls to DSP */
  378. enum fastrpc_msg_type {
  379. USER_MSG = 0,
  380. KERNEL_MSG_WITH_ZERO_PID,
  381. KERNEL_MSG_WITH_NONZERO_PID,
  382. };
  383. /* Fastrpc remote pd type */
  384. enum fastrpc_remote_pd_type {
  385. FASTRPC_ROOT_PD = 0,
  386. FASTRPC_USER_PD,
  387. FASTRPC_SENSORS_PD,
  388. };
  389. #define DSPSIGNAL_TIMEOUT_NONE 0xffffffff
  390. #define DSPSIGNAL_NUM_SIGNALS 1024
  391. // Signal state and completions are stored in groups of DSPSIGNAL_GROUP_SIZE.
  392. // Must be a power of two.
  393. #define DSPSIGNAL_GROUP_SIZE 256
  394. struct secure_vm {
  395. int *vmid;
  396. int *vmperm;
  397. int vmcount;
  398. };
  399. struct gid_list {
  400. unsigned int *gids;
  401. unsigned int gidcount;
  402. };
  403. struct fastrpc_buf {
  404. struct hlist_node hn;
  405. struct hlist_node hn_rem;
  406. struct hlist_node hn_init;
  407. struct fastrpc_file *fl;
  408. void *virt;
  409. uint64_t phys;
  410. size_t size;
  411. unsigned long dma_attr;
  412. uintptr_t raddr;
  413. uint32_t flags;
  414. int type; /* One of "fastrpc_buf_type" */
  415. bool in_use; /* Used only for persistent header buffers */
  416. struct timespec64 buf_start_time;
  417. struct timespec64 buf_end_time;
  418. };
  419. struct fastrpc_ctx_lst;
  420. struct fastrpc_tx_msg {
  421. struct smq_msg msg; /* Msg sent to remote subsystem */
  422. int transport_send_err; /* transport error */
  423. int64_t ns; /* Timestamp (in ns) of msg */
  424. uint64_t xo_time_in_us; /* XO Timestamp (in us) of sent message */
  425. uint64_t xo_time_in_us_interrupted; /* XO Timestamp (in us) of interrupted ctx */
  426. uint64_t xo_time_in_us_restored; /* XO Timestamp (in us) of restored ctx */
  427. };
  428. struct fastrpc_rx_msg {
  429. struct smq_invoke_rspv2 rsp; /* Response from remote subsystem */
  430. int64_t ns; /* Timestamp (in ns) of response */
  431. uint64_t xo_time_in_us; /* XO Timestamp (in us) of response */
  432. };
  433. struct fastrpc_transport_log {
  434. unsigned int tx_index; /* Current index of 'tx_msgs' array */
  435. unsigned int rx_index; /* Current index of 'rx_msgs' array */
  436. /* Rolling history of messages sent to remote subsystem */
  437. struct fastrpc_tx_msg tx_msgs[GLINK_MSG_HISTORY_LEN];
  438. /* Rolling history of responses from remote subsystem */
  439. struct fastrpc_rx_msg rx_msgs[GLINK_MSG_HISTORY_LEN];
  440. spinlock_t lock;
  441. };
  442. struct overlap {
  443. uintptr_t start;
  444. uintptr_t end;
  445. int raix;
  446. uintptr_t mstart;
  447. uintptr_t mend;
  448. uintptr_t offset;
  449. int do_cmo; /*used for cache maintenance of inrout buffers*/
  450. };
  451. struct fastrpc_perf {
  452. uint64_t count;
  453. uint64_t flush;
  454. uint64_t map;
  455. uint64_t copy;
  456. uint64_t link;
  457. uint64_t getargs;
  458. uint64_t putargs;
  459. uint64_t invargs;
  460. uint64_t invoke;
  461. uint64_t tid;
  462. };
  463. struct smq_notif_rsp {
  464. struct list_head notifn;
  465. int domain;
  466. int session;
  467. enum fastrpc_status_flags status;
  468. };
  469. struct smq_invoke_ctx {
  470. struct hlist_node hn;
  471. /* Async node to add to async job ctx list */
  472. struct list_head asyncn;
  473. struct completion work;
  474. int retval;
  475. int pid;
  476. int tgid;
  477. remote_arg_t *lpra;
  478. remote_arg64_t *rpra;
  479. remote_arg64_t *lrpra; /* Local copy of rpra for put_args */
  480. int *fds;
  481. unsigned int *attrs;
  482. struct fastrpc_mmap **maps;
  483. struct fastrpc_buf *buf;
  484. struct fastrpc_buf *copybuf; /*used to copy non-ion buffers */
  485. size_t used;
  486. struct fastrpc_file *fl;
  487. uint32_t handle;
  488. uint32_t sc;
  489. struct overlap *overs;
  490. struct overlap **overps;
  491. struct smq_msg msg;
  492. uint32_t *crc;
  493. uint64_t *perf_kernel;
  494. uint64_t *perf_dsp;
  495. unsigned int magic;
  496. uint64_t ctxid;
  497. struct fastrpc_perf *perf;
  498. /* response flags from remote processor */
  499. enum fastrpc_response_flags rsp_flags;
  500. /* user hint of completion time in us */
  501. uint32_t early_wake_time;
  502. /* work done status flag */
  503. bool is_work_done;
  504. /* Store Async job in the context*/
  505. struct fastrpc_async_job asyncjob;
  506. /* Async early flag to check the state of context */
  507. bool is_early_wakeup;
  508. uint32_t sc_interrupted;
  509. struct fastrpc_file *fl_interrupted;
  510. uint32_t handle_interrupted;
  511. uint64_t xo_time_in_us_created; /* XO Timestamp (in us) of ctx creation */
  512. uint64_t xo_time_in_us_interrupted; /* XO Timestamp (in us) of interrupted ctx */
  513. uint64_t xo_time_in_us_restored; /* XO Timestamp (in us) of restored ctx */
  514. int tx_index; /* index of current ctx in channel gmsg_log array */
  515. bool is_job_sent_to_remote_ss; /* Flag to check if job is sent to remote sub system */
  516. };
  517. struct fastrpc_ctx_lst {
  518. struct hlist_head pending;
  519. struct hlist_head interrupted;
  520. /* Number of active contexts queued to DSP */
  521. uint32_t num_active_ctxs;
  522. /* Queue which holds all async job contexts of process */
  523. struct list_head async_queue;
  524. /* Queue which holds all status notifications of process */
  525. struct list_head notif_queue;
  526. };
  527. struct fastrpc_smmu {
  528. struct device *dev;
  529. const char *dev_name;
  530. int cb;
  531. int enabled;
  532. int faults;
  533. int secure;
  534. int coherent;
  535. int sharedcb;
  536. int pd_type; /* Process type on remote sub system */
  537. /* gen pool for QRTR */
  538. struct gen_pool *frpc_genpool;
  539. /* fastrpc gen pool buffer */
  540. struct fastrpc_buf *frpc_genpool_buf;
  541. /* fastrpc gen pool buffer fixed IOVA */
  542. unsigned long genpool_iova;
  543. /* fastrpc gen pool buffer size */
  544. size_t genpool_size;
  545. };
  546. struct fastrpc_session_ctx {
  547. struct device *dev;
  548. struct fastrpc_smmu smmu;
  549. int used;
  550. };
  551. struct fastrpc_static_pd {
  552. char *servloc_name;
  553. char *spdname;
  554. void *pdrhandle;
  555. uint64_t pdrcount;
  556. uint64_t prevpdrcount;
  557. atomic_t ispdup;
  558. int cid;
  559. wait_queue_head_t wait_for_pdup;
  560. };
  561. struct fastrpc_dsp_capabilities {
  562. uint32_t is_cached; //! Flag if dsp attributes are cached
  563. uint32_t dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
  564. };
  565. struct fastrpc_channel_ctx {
  566. char *name;
  567. char *subsys;
  568. struct device *dev;
  569. struct fastrpc_session_ctx session[NUM_SESSIONS];
  570. struct fastrpc_static_pd spd[NUM_SESSIONS];
  571. struct completion work;
  572. struct completion workport;
  573. struct notifier_block nb;
  574. struct mutex smd_mutex;
  575. uint64_t sesscount;
  576. uint64_t ssrcount;
  577. int in_hib;
  578. void *handle;
  579. uint64_t prevssrcount;
  580. int subsystemstate;
  581. int vmid;
  582. struct secure_vm rhvm;
  583. void *rh_dump_dev;
  584. /* Indicates, if channel is restricted to secure node only */
  585. int secure;
  586. /* Indicates whether the channel supports unsigned PD */
  587. bool unsigned_support;
  588. struct fastrpc_dsp_capabilities dsp_cap_kernel;
  589. /* cpu capabilities shared to DSP */
  590. uint64_t cpuinfo_todsp;
  591. bool cpuinfo_status;
  592. struct smq_invoke_ctx *ctxtable[FASTRPC_CTX_MAX];
  593. spinlock_t ctxlock;
  594. struct fastrpc_transport_log gmsg_log;
  595. struct hlist_head initmems;
  596. /* Store gfa structure debug details */
  597. struct fastrpc_buf *buf;
  598. };
  599. struct fastrpc_apps {
  600. struct fastrpc_channel_ctx *channel;
  601. struct cdev cdev;
  602. struct class *class;
  603. struct smq_phy_page range;
  604. struct hlist_head maps;
  605. uint32_t staticpd_flags;
  606. dev_t dev_no;
  607. int compat;
  608. struct hlist_head drivers;
  609. spinlock_t hlock;
  610. struct device *dev;
  611. /* Indicates fastrpc device node info */
  612. struct device *dev_fastrpc;
  613. unsigned int latency;
  614. int transport_initialized;
  615. /* Flag to determine fastrpc bus registration */
  616. int fastrpc_bus_register;
  617. bool legacy_remote_heap;
  618. /* Unique job id for each message */
  619. uint64_t jobid[NUM_CHANNELS];
  620. struct gid_list gidlist;
  621. struct device *secure_dev;
  622. struct device *non_secure_dev;
  623. /* Secure subsystems like ADSP/SLPI will use secure client */
  624. struct wakeup_source *wake_source_secure;
  625. /* Non-secure subsystem like CDSP will use regular client */
  626. struct wakeup_source *wake_source;
  627. uint32_t duplicate_rsp_err_cnt;
  628. uint32_t max_size_limit;
  629. struct hlist_head frpc_devices;
  630. struct hlist_head frpc_drivers;
  631. struct mutex mut_uid;
  632. /* Indicates cdsp device status */
  633. int remote_cdsp_status;
  634. /* Indicates secure context bank to be shared */
  635. int share_securecb;
  636. /* Indicates process type is configured for SMMU context bank */
  637. bool cb_pd_type;
  638. /* Number of lowest capacity cores for given platform */
  639. unsigned int lowest_capacity_core_count;
  640. /* Flag to check if PM QoS vote needs to be done for only one core */
  641. bool single_core_latency_vote;
  642. };
  643. struct fastrpc_mmap {
  644. struct hlist_node hn;
  645. struct fastrpc_file *fl;
  646. struct fastrpc_apps *apps;
  647. int fd;
  648. uint32_t flags;
  649. struct dma_buf *buf;
  650. struct sg_table *table;
  651. struct dma_buf_attachment *attach;
  652. struct ion_handle *handle;
  653. uint64_t phys;
  654. size_t size;
  655. uintptr_t va;
  656. size_t len;
  657. int refs;
  658. uintptr_t raddr;
  659. int secure;
  660. bool is_persistent; /* the map is persistenet across sessions */
  661. int frpc_md_index; /* Minidump unique index */
  662. uintptr_t attr;
  663. bool in_use; /* Indicates if persistent map is in use*/
  664. struct timespec64 map_start_time;
  665. struct timespec64 map_end_time;
  666. /* Mapping for fastrpc shell */
  667. bool is_filemap;
  668. char *servloc_name; /* Indicate which daemon mapped this */
  669. /* Indicates map is being used by a pending RPC call */
  670. unsigned int ctx_refs;
  671. };
  672. enum fastrpc_perfkeys {
  673. PERF_COUNT = 0,
  674. PERF_FLUSH = 1,
  675. PERF_MAP = 2,
  676. PERF_COPY = 3,
  677. PERF_LINK = 4,
  678. PERF_GETARGS = 5,
  679. PERF_PUTARGS = 6,
  680. PERF_INVARGS = 7,
  681. PERF_INVOKE = 8,
  682. PERF_TID = 9,
  683. PERF_KEY_MAX = 10,
  684. };
  685. struct fastrpc_notif_queue {
  686. /* Number of pending status notifications in queue */
  687. atomic_t notif_queue_count;
  688. /* Wait queue to synchronize notifier thread and response */
  689. wait_queue_head_t notif_wait_queue;
  690. /* IRQ safe spin lock for protecting notif queue */
  691. spinlock_t nqlock;
  692. };
  693. enum fastrpc_dspsignal_state {
  694. DSPSIGNAL_STATE_UNUSED = 0,
  695. DSPSIGNAL_STATE_PENDING,
  696. DSPSIGNAL_STATE_SIGNALED,
  697. DSPSIGNAL_STATE_CANCELED
  698. };
  699. struct fastrpc_dspsignal {
  700. struct completion comp;
  701. int state;
  702. };
  703. struct fastrpc_file {
  704. struct hlist_node hn;
  705. spinlock_t hlock;
  706. struct hlist_head maps;
  707. struct hlist_head cached_bufs;
  708. uint32_t num_cached_buf;
  709. struct hlist_head remote_bufs;
  710. struct fastrpc_ctx_lst clst;
  711. struct fastrpc_session_ctx *sctx;
  712. struct fastrpc_buf *init_mem;
  713. /* No. of persistent headers */
  714. unsigned int num_pers_hdrs;
  715. /* Pre-allocated header buffer */
  716. struct fastrpc_buf *pers_hdr_buf;
  717. /* Pre-allocated buffer divided into N chunks */
  718. struct fastrpc_buf *hdr_bufs;
  719. struct fastrpc_session_ctx *secsctx;
  720. uint32_t mode;
  721. uint32_t profile;
  722. int sessionid;
  723. int tgid_open; /* Process ID during device open */
  724. int tgid; /* Process ID that uses device for RPC calls */
  725. int cid;
  726. int tvm_remote_domain;
  727. uint64_t ssrcount;
  728. int pd;
  729. char *servloc_name;
  730. int file_close;
  731. int dsp_proc_init;
  732. int sharedcb;
  733. int pd_type; /* Process type on remote subsystem */
  734. struct fastrpc_apps *apps;
  735. struct dentry *debugfs_file;
  736. struct dev_pm_qos_request *dev_pm_qos_req;
  737. int qos_request;
  738. struct mutex map_mutex;
  739. struct mutex internal_map_mutex;
  740. /* Identifies the device (MINOR_NUM_DEV / MINOR_NUM_SECURE_DEV) */
  741. int dev_minor;
  742. char *debug_buf;
  743. /* Flag to indicate attempt has been made to allocate memory for debug_buf*/
  744. int debug_buf_alloced_attempted;
  745. /* Flag to enable PM wake/relax voting for every remote invoke */
  746. int wake_enable;
  747. struct gid_list gidlist;
  748. /* Number of jobs pending in Async Queue */
  749. atomic_t async_queue_job_count;
  750. /* Async wait queue to synchronize glink response and async thread */
  751. wait_queue_head_t async_wait_queue;
  752. /* IRQ safe spin lock for protecting async queue */
  753. spinlock_t aqlock;
  754. /* Process status notification queue */
  755. struct fastrpc_notif_queue proc_state_notif;
  756. uint32_t ws_timeout;
  757. bool untrusted_process;
  758. struct fastrpc_device *device;
  759. /* Process kill will wait on work when ram dump collection in progress */
  760. struct completion work;
  761. /* Flag to indicate ram dump collection status*/
  762. bool is_ramdump_pend;
  763. /* Process kill will wait on bus driver invoke thread to complete its process */
  764. struct completion dma_invoke;
  765. /* Flag to indicate invoke pending */
  766. bool is_dma_invoke_pend;
  767. /* Flag to indicate type of process (static, dynamic) */
  768. uint32_t proc_flags;
  769. /* If set, threads will poll for DSP response instead of glink wait */
  770. bool poll_mode;
  771. /* Threads poll for specified timeout and fall back to glink wait */
  772. uint32_t poll_timeout;
  773. /* Flag to indicate dynamic process creation status*/
  774. enum fastrpc_process_create_state dsp_process_state;
  775. bool is_unsigned_pd;
  776. /* Flag to indicate 32 bit driver*/
  777. bool is_compat;
  778. /* Completion objects and state for dspsignals */
  779. struct fastrpc_dspsignal *signal_groups[DSPSIGNAL_NUM_SIGNALS / DSPSIGNAL_GROUP_SIZE];
  780. spinlock_t dspsignals_lock;
  781. struct mutex signal_create_mutex;
  782. struct completion shutdown;
  783. /* Flag to indicate notif thread exit requested*/
  784. bool exit_notif;
  785. /* Flag to indicate async thread exit requested*/
  786. bool exit_async;
  787. /*
  788. * structure to hold fd and size of buffer shared with DSP,
  789. * which contains initial debug configurations and other initial
  790. * config paramters.
  791. */
  792. struct fastrpc_proc_sharedbuf_info sharedbuf_info;
  793. };
  794. int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
  795. uint32_t kernel,
  796. struct fastrpc_ioctl_invoke_async *inv);
  797. int fastrpc_internal_invoke2(struct fastrpc_file *fl,
  798. struct fastrpc_ioctl_invoke2 *inv2);
  799. int fastrpc_internal_munmap(struct fastrpc_file *fl,
  800. struct fastrpc_ioctl_munmap *ud);
  801. int fastrpc_internal_mem_map(struct fastrpc_file *fl,
  802. struct fastrpc_ioctl_mem_map *ud);
  803. int fastrpc_internal_mem_unmap(struct fastrpc_file *fl,
  804. struct fastrpc_ioctl_mem_unmap *ud);
  805. int fastrpc_internal_mmap(struct fastrpc_file *fl,
  806. struct fastrpc_ioctl_mmap *ud);
  807. int fastrpc_init_process(struct fastrpc_file *fl,
  808. struct fastrpc_ioctl_init_attrs *uproc);
  809. int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info);
  810. int fastrpc_internal_control(struct fastrpc_file *fl,
  811. struct fastrpc_ioctl_control *cp);
  812. int fastrpc_setmode(unsigned long ioctl_param,
  813. struct fastrpc_file *fl);
  814. int fastrpc_get_info_from_kernel(
  815. struct fastrpc_ioctl_capability *cap,
  816. struct fastrpc_file *fl);
  817. int fastrpc_dspsignal_signal(struct fastrpc_file *fl,
  818. struct fastrpc_ioctl_dspsignal_signal *sig);
  819. int fastrpc_dspsignal_wait(struct fastrpc_file *fl,
  820. struct fastrpc_ioctl_dspsignal_wait *wait);
  821. int fastrpc_dspsignal_create(struct fastrpc_file *fl,
  822. struct fastrpc_ioctl_dspsignal_create *create);
  823. int fastrpc_dspsignal_destroy(struct fastrpc_file *fl,
  824. struct fastrpc_ioctl_dspsignal_destroy *destroy);
  825. int fastrpc_dspsignal_cancel_wait(struct fastrpc_file *fl,
  826. struct fastrpc_ioctl_dspsignal_cancel_wait *cancel);
  827. void fastrpc_rproc_trace_events(const char *name, const char *event,
  828. const char *subevent);
  829. #endif