cmdq_sec.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. #include "cmdq_sec.h"
  2. static atomic_t gDebugSecSwCopy = ATOMIC_INIT(0);
  3. static atomic_t gDebugSecCmdId = ATOMIC_INIT(0);
  4. static KREE_SESSION_HANDLE cmdq_session;
  5. static KREE_SESSION_HANDLE cmdq_mem_session;
  6. #if 1
  7. static DEFINE_MUTEX(gCmdqSecExecLock); /* lock to protect atomic secure task execution */
  8. static DEFINE_MUTEX(gCmdqSecContextLock); /* lock to protext atomic access gCmdqSecContextList */
  9. static struct list_head gCmdqSecContextList; /* secure context list. note each porcess has its own sec context */
  10. static cmdqSecContextHandle gCmdqSecContextHandle; /* secure context to cmdqSecTL */
  11. /* Set 1 to open once for each process context, because of below reasons:
  12. * 1. kmalloc size > 4KB, need pre-allocation to avoid memory fragmentation and causes kmalloc fail.
  13. * 2. we entry secure world for config and trigger GCE, and wait in normal world
  14. */
  15. #define CMDQ_OPEN_SESSION_ONCE (1)
  16. /* function declaretion */
  17. /* #if defined(CMDQ_SECURE_PATH_SUPPORT) */
  18. /***********add from k2 START ***********/
  19. void cmdq_sec_lock_secure_path(void)
  20. {
  21. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  22. mutex_lock(&gCmdqSecExecLock);
  23. smp_mb(); /*memory barrier */
  24. #else
  25. CMDQ_ERR("SVP feature is not on\n");
  26. #endif
  27. }
  28. void cmdq_sec_unlock_secure_path(void)
  29. {
  30. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  31. mutex_unlock(&gCmdqSecExecLock);
  32. #else
  33. CMDQ_ERR("SVP feature is not on\n");
  34. #endif
  35. }
  36. int32_t cmdq_sec_create_shared_memory(cmdqSecSharedMemoryHandle *pHandle, const uint32_t size)
  37. {
  38. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  39. cmdqSecSharedMemoryHandle handle = NULL;
  40. handle = kzalloc(sizeof(uint8_t *) * sizeof(cmdqSecSharedMemoryStruct), GFP_KERNEL);
  41. if (NULL == handle)
  42. return -ENOMEM;
  43. CMDQ_LOG("%s\n", __func__);
  44. handle->pVABase = kcalloc(size, sizeof(uint8_t), GFP_KERNEL);
  45. handle->size = size;
  46. *pHandle = handle;
  47. #else
  48. CMDQ_ERR("SVP feature is not on\n");
  49. #endif
  50. return 0;
  51. }
  52. int32_t cmdq_sec_destroy_shared_memory(cmdqSecSharedMemoryHandle handle)
  53. {
  54. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  55. if (handle && handle->pVABase)
  56. kfree(handle->pVABase);
  57. kfree(handle);
  58. #else
  59. CMDQ_ERR("SVP feature is not on\n");
  60. #endif
  61. return 0;
  62. }
  63. /***********add from k2 END ***********/
  64. /* the API for other code to acquire cmdq_mem session handle */
  65. /* a NULL handle is returned when it fails */
  66. KREE_SESSION_HANDLE cmdq_session_handle(void)
  67. {
  68. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  69. CMDQ_MSG("cmdq_session_handle() acquire TEE session\n");
  70. if (0 == cmdq_session) {
  71. TZ_RESULT ret;
  72. CMDQ_MSG("cmdq_session_handle() create session\n");
  73. CMDQ_LOG("TZ_TA_CMDQ_UUID:%s\n", TZ_TA_CMDQ_UUID);
  74. ret = KREE_CreateSession(TZ_TA_CMDQ_UUID, &cmdq_session);
  75. if (ret != TZ_RESULT_SUCCESS) {
  76. CMDQ_ERR("cmdq_session_handle() failed to create session, ret=%d\n", ret);
  77. return 0;
  78. }
  79. }
  80. CMDQ_MSG("cmdq_session_handle() session=%x\n", (unsigned int)cmdq_session);
  81. return cmdq_session;
  82. #else
  83. CMDQ_ERR("SVP feature is not on\n");
  84. return 0;
  85. #endif
  86. }
  87. KREE_SESSION_HANDLE cmdq_mem_session_handle(void)
  88. {
  89. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  90. CMDQ_MSG("cmdq_mem_session_handle() acquires TEE memory session\n");
  91. if (0 == cmdq_mem_session) {
  92. TZ_RESULT ret;
  93. CMDQ_MSG("cmdq_mem_session_handle() create memory session\n");
  94. ret = KREE_CreateSession(TZ_TA_MEM_UUID, &cmdq_mem_session);
  95. if (ret != TZ_RESULT_SUCCESS) {
  96. CMDQ_ERR("cmdq_mem_session_handle() failed to create session: ret=%d\n",
  97. ret);
  98. return 0;
  99. }
  100. }
  101. CMDQ_MSG("cmdq_mem_session_handle() session=%x\n", (unsigned int)cmdq_mem_session);
  102. return cmdq_mem_session;
  103. #else
  104. CMDQ_ERR("SVP feature is not on\n");
  105. return 0;
  106. #endif
  107. }
  108. static int32_t cmdq_sec_setup_context_session(cmdqSecContextHandle handle)
  109. {
  110. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  111. /* init handle:iwcMessage sessionHandle memSessionHandle */
  112. #if CMDQ_OPEN_SESSION_ONCE
  113. if (NULL == handle->iwcMessage) {
  114. #endif
  115. /* alloc message bufer */
  116. handle->iwcMessage = kmalloc(sizeof(iwcCmdqMessage_t), GFP_KERNEL);
  117. if (NULL == handle->iwcMessage) {
  118. CMDQ_ERR("handle->iwcMessage kmalloc failed!\n");
  119. return -ENOMEM;
  120. }
  121. #if CMDQ_OPEN_SESSION_ONCE
  122. }
  123. #endif
  124. CMDQ_MSG("handle->iwcMessage 0x%p\n", handle->iwcMessage);
  125. /* init session handle */
  126. handle->sessionHandle = cmdq_session_handle();
  127. if (0 == handle->sessionHandle)
  128. return -1;
  129. /* init memory session handle */
  130. handle->memSessionHandle = cmdq_mem_session_handle();
  131. if (0 == handle->memSessionHandle) {
  132. CMDQ_ERR("create cmdq_mem_session_handle error\n");
  133. return -2;
  134. }
  135. CMDQ_MSG("handle->sessionHandle 0x%x\n", handle->sessionHandle);
  136. CMDQ_MSG("handle->memSessionHandle 0x%x\n", handle->memSessionHandle);
  137. return 0;
  138. #else
  139. CMDQ_ERR("SVP feature is not on\n");
  140. return 0;
  141. #endif
  142. }
  143. static void cmdq_sec_deinit_session_unlocked(cmdqSecContextHandle handle)
  144. {
  145. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  146. CMDQ_MSG("[SEC]-->SESSION_DEINIT\n");
  147. do {
  148. if (NULL != handle->iwcMessage) {
  149. kfree(handle->iwcMessage);
  150. handle->iwcMessage = NULL;
  151. }
  152. } while (0);
  153. CMDQ_MSG("[SEC]<--SESSION_DEINIT\n");
  154. #else
  155. CMDQ_ERR("SVP feature is not on\n");
  156. #endif
  157. }
  158. static int32_t cmdq_sec_fill_iwc_command_basic_unlocked(iwcCmdqMessage_t *_pIwc,
  159. uint32_t iwcCommand,
  160. struct TaskStruct *_pTask, int32_t thread)
  161. {
  162. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  163. iwcCmdqMessage_t *pIwc;
  164. pIwc = (iwcCmdqMessage_t *) _pIwc;
  165. /* specify command id only, don't care other other */
  166. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  167. pIwc->cmd = iwcCommand;
  168. /* medatada: debug config */
  169. /*pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0); */
  170. pIwc->debug.logLevel =
  171. cmdq_core_get_sec_print_count() ? LOG_LEVEL_MSG : cmdq_core_get_log_level();
  172. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  173. return 0;
  174. #else
  175. CMDQ_ERR("SVP feature is not on\n");
  176. return 0;
  177. #endif
  178. }
  179. static int32_t cmdq_sec_fill_iwc_cancel_msg_unlocked(iwcCmdqMessage_t *_pIwc,
  180. uint32_t iwcCommand,
  181. struct TaskStruct *_pTask, int32_t thread)
  182. {
  183. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  184. const struct TaskStruct *pTask = (struct TaskStruct *)_pTask;
  185. iwcCmdqMessage_t *pIwc;
  186. pIwc = (iwcCmdqMessage_t *) _pIwc;
  187. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  188. pIwc->cmd = iwcCommand;
  189. pIwc->cancelTask.waitCookie = pTask->secData.waitCookie;
  190. pIwc->cancelTask.thread = thread;
  191. /* medatada: debug config */
  192. /* pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0);*/
  193. pIwc->debug.logLevel =
  194. cmdq_core_get_sec_print_count() ? LOG_LEVEL_MSG : cmdq_core_get_log_level();
  195. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  196. CMDQ_LOG("FILL:CANCEL_TASK: task: %p, thread:%d, cookie:%d, resetExecCnt:%d\n",
  197. pTask, thread, pTask->secData.waitCookie, pTask->secData.resetExecCnt);
  198. #else
  199. CMDQ_ERR("SVP feature is not on\n");
  200. #endif
  201. return 0;
  202. }
  203. static int32_t cmdq_sec_fill_iwc_command_msg_unlocked(iwcCmdqMessage_t *pIwc,
  204. uint32_t iwcCommand,
  205. struct TaskStruct *pTask, int32_t thread)
  206. {
  207. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  208. int32_t status = 0;
  209. /* TEE will insert some instr,DAPC and M4U configuration */
  210. const uint32_t reservedCommandSize = 4 * CMDQ_INST_SIZE;
  211. CMDQ_MSG("enter fill_iwc_command_msg_unlock\n");
  212. /* check command size first */
  213. if (pTask && (CMDQ_TZ_CMD_BLOCK_SIZE < (pTask->commandSize + reservedCommandSize))) {
  214. CMDQ_ERR("[SEC]SESSION_MSG: pTask %p commandSize %d > %d\n",
  215. pTask, pTask->commandSize, CMDQ_TZ_CMD_BLOCK_SIZE);
  216. return -EFAULT;
  217. }
  218. CMDQ_MSG("[SEC]-->SESSION_MSG: cmdId[%d]\n", iwcCommand);
  219. /* fill message buffer for inter world communication */
  220. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  221. pIwc->cmd = iwcCommand;
  222. /* metadata */
  223. pIwc->command.metadata.enginesNeedDAPC = pTask->secData.enginesNeedDAPC;
  224. pIwc->command.metadata.enginesNeedPortSecurity = pTask->secData.enginesNeedPortSecurity;
  225. pIwc->command.metadata.secMode = pTask->secData.secMode;
  226. if (NULL != pTask && CMDQ_INVALID_THREAD != thread) {
  227. /* basic data */
  228. pIwc->command.scenario = pTask->scenario;
  229. pIwc->command.thread = thread;
  230. pIwc->command.priority = pTask->priority;
  231. pIwc->command.engineFlag = pTask->engineFlag;
  232. pIwc->command.commandSize = pTask->commandSize;
  233. pIwc->command.hNormalTask = (0LL | (uint64_t) (pTask));
  234. memcpy((pIwc->command.pVABase), (pTask->pVABase), (pTask->commandSize));
  235. /* cookie */
  236. pIwc->command.waitCookie = pTask->secData.waitCookie;
  237. pIwc->command.resetExecCnt = pTask->secData.resetExecCnt;
  238. CMDQ_MSG
  239. ("[SEC]SESSION_MSG: task 0x%p, thread: %d, size: %d, bufferSize: %d, scenario:%d, flag:0x%08llx ,hNormalTask:0x%llx\n",
  240. pTask, thread, pTask->commandSize, pTask->bufferSize, pTask->scenario,
  241. pTask->engineFlag, pIwc->command.hNormalTask);
  242. CMDQ_VERBOSE("[SEC]SESSION_MSG: addrList[%d][0x%llx]\n",
  243. pTask->secData.addrMetadataCount, pTask->secData.addrMetadatas);
  244. if (0 < pTask->secData.addrMetadataCount) {
  245. pIwc->command.metadata.addrListLength = pTask->secData.addrMetadataCount;
  246. memcpy((pIwc->command.metadata.addrList),
  247. CMDQ_U32_PTR(pTask->secData.addrMetadatas),
  248. (pTask->secData.addrMetadataCount) * sizeof(iwcCmdqAddrMetadata_t));
  249. pIwc->command.metadata.srcHandle = pTask->secData.srcHandle;
  250. pIwc->command.metadata.dstHandle = pTask->secData.dstHandle;
  251. }
  252. /* medatada: debug config */
  253. /*pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0); */
  254. pIwc->debug.logLevel =
  255. cmdq_core_get_sec_print_count() ? LOG_LEVEL_MSG : cmdq_core_get_log_level();
  256. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  257. } else {
  258. /* relase resource, or debug function will go here */
  259. CMDQ_VERBOSE("[SEC]-->SESSION_MSG: no task, cmdId[%d]\n", iwcCommand);
  260. pIwc->command.commandSize = 0;
  261. pIwc->command.metadata.addrListLength = 0;
  262. }
  263. CMDQ_MSG("[SEC]<--SESSION_MSG[%d]\n", status);
  264. CMDQ_MSG("leaving fill_iwc_command_msg_unlock\n");
  265. return status;
  266. #else
  267. CMDQ_ERR("SVP feature is not on\n");
  268. return 0;
  269. #endif
  270. }
  271. void dump_message(const iwcCmdqMessage_t *message)
  272. {
  273. #ifdef DEBUG_SVP_INFO
  274. CMDQ_LOG("dump iwcCmdqMessage info\n");
  275. CMDQ_LOG("cmdID:%d,thread:%d,scenario:%d,priority:%d,commandSize:%d,engineFlag:0x%llx\n",
  276. message->cmd,
  277. message->command.thread,
  278. message->command.scenario,
  279. message->command.priority,
  280. message->command.commandSize, message->command.engineFlag);
  281. CMDQ_LOG("pVABase:0x%p\n", message->command.pVABase);
  282. CMDQ_LOG("addrlistLen:%d,needDAPC:0x%llx,needPortSecurity:0x%llx\n",
  283. message->command.metadata.addrListLength,
  284. message->command.metadata.enginesNeedDAPC,
  285. message->command.metadata.enginesNeedPortSecurity);
  286. #else
  287. CMDQ_ERR("please open DEBUG_SVP_INFO macro first\n");
  288. #endif
  289. }
  290. static int32_t cmdq_sec_execute_session_unlocked(cmdqSecContextHandle handle)
  291. {
  292. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  293. TZ_RESULT tzRes;
  294. CMDQ_PROF_START("CMDQ_SEC_EXE");
  295. do {
  296. /* Register share memory */
  297. MTEEC_PARAM cmdq_param[4];
  298. unsigned int paramTypes;
  299. KREE_SHAREDMEM_HANDLE cmdq_share_handle = 0;
  300. KREE_SHAREDMEM_PARAM cmdq_shared_param;
  301. #if 1
  302. /* allocate path init for shared cookie */
  303. if (CMD_CMDQ_TL_INIT_SHARED_MEMORY ==
  304. ((iwcCmdqMessage_t *) (handle->iwcMessage))->cmd) {
  305. cmdq_shared_param.buffer = gCmdqContext.hSecSharedMem->pVABase;
  306. cmdq_shared_param.size = gCmdqContext.hSecSharedMem->size;
  307. CMDQ_MSG("cmdq_shared_param.buffer %p\n", cmdq_shared_param.buffer);
  308. CMDQ_MSG("handle->memSessionHandle:%d\n",
  309. (uint32_t) handle->memSessionHandle);
  310. tzRes =
  311. KREE_RegisterSharedmem(handle->memSessionHandle, &cmdq_share_handle,
  312. &cmdq_shared_param);
  313. /* save for unregister */
  314. gCmdqContext.hSecSharedMem->cmdq_share_cookie_handle = cmdq_share_handle;
  315. if (tzRes != TZ_RESULT_SUCCESS) {
  316. CMDQ_ERR
  317. ("cmdq register share memory Error: %d, line:%d, cmdq_mem_session_handle(%x)\n",
  318. tzRes, __LINE__, (unsigned int)(handle->memSessionHandle));
  319. return tzRes;
  320. }
  321. /* KREE_Tee service call */
  322. cmdq_param[0].memref.handle = (uint32_t) cmdq_share_handle;
  323. cmdq_param[0].memref.offset = 0;
  324. cmdq_param[0].memref.size = cmdq_shared_param.size;
  325. paramTypes = TZ_ParamTypes1(TZPT_MEMREF_INOUT);
  326. tzRes =
  327. KREE_TeeServiceCall(handle->sessionHandle,
  328. CMD_CMDQ_TL_INIT_SHARED_MEMORY, paramTypes,
  329. cmdq_param);
  330. if (tzRes != TZ_RESULT_SUCCESS) {
  331. CMDQ_ERR("CMD_CMDQ_TL_INIT_SHARED_MEMORY fail, ret=0x%x\n", tzRes);
  332. return tzRes;
  333. }
  334. CMDQ_MSG("KREE_TeeServiceCall tzRes =0x%x\n", tzRes);
  335. break;
  336. }
  337. #endif
  338. cmdq_shared_param.buffer = handle->iwcMessage;
  339. cmdq_shared_param.size = (sizeof(iwcCmdqMessage_t));
  340. CMDQ_MSG("cmdq_shared_param.buffer %p\n", cmdq_shared_param.buffer);
  341. /* dump_message((iwcCmdqMessage_t *) handle->iwcMessage); */
  342. #if 0 /* add for debug */
  343. CMDQ_ERR("dump secure task instructions in Normal world\n");
  344. cmdq_core_dump_instructions((uint64_t
  345. *) (((iwcCmdqMessage_t *) (handle->
  346. iwcMessage))->command.
  347. pVABase),
  348. ((iwcCmdqMessage_t *) (handle->iwcMessage))->
  349. command.commandSize);
  350. #endif
  351. tzRes =
  352. KREE_RegisterSharedmem(handle->memSessionHandle, &cmdq_share_handle,
  353. &cmdq_shared_param);
  354. if (tzRes != TZ_RESULT_SUCCESS) {
  355. CMDQ_ERR
  356. ("cmdq register share memory Error: %d, line:%d, cmdq_mem_session_handle(%x)\n",
  357. tzRes, __LINE__, (unsigned int)(handle->memSessionHandle));
  358. return tzRes;
  359. }
  360. /* KREE_Tee service call */
  361. cmdq_param[0].memref.handle = (uint32_t) cmdq_share_handle;
  362. cmdq_param[0].memref.offset = 0;
  363. cmdq_param[0].memref.size = cmdq_shared_param.size;
  364. paramTypes = TZ_ParamTypes1(TZPT_MEMREF_INPUT);
  365. CMDQ_MSG("commandID:%d\n", ((iwcCmdqMessage_t *) (handle->iwcMessage))->cmd);
  366. CMDQ_MSG("handle->sessionHandle:%x\n", handle->sessionHandle);
  367. CMDQ_MSG("start to enter Secure World\n");
  368. tzRes =
  369. KREE_TeeServiceCall(handle->sessionHandle,
  370. ((iwcCmdqMessage_t *) (handle->iwcMessage))->cmd,
  371. paramTypes, cmdq_param);
  372. if (tzRes != TZ_RESULT_SUCCESS) {
  373. CMDQ_ERR("leave secure world KREE_TeeServiceCall fail, ret=0x%x\n", tzRes);
  374. return tzRes;
  375. }
  376. CMDQ_MSG("leave secure world KREE_TeeServiceCall tzRes =0x%x\n", tzRes);
  377. /* Unregister share memory */
  378. KREE_UnregisterSharedmem(handle->memSessionHandle, cmdq_share_handle);
  379. /* wait respond */
  380. /* config with timeout */
  381. } while (0);
  382. CMDQ_PROF_END("CMDQ_SEC_EXE");
  383. /* return tee service call result */
  384. return tzRes;
  385. #else
  386. CMDQ_ERR("SVP feature is not on\n");
  387. return 0;
  388. #endif
  389. }
  390. CmdqSecFillIwcCB cmdq_sec_get_iwc_msg_fill_cb_by_iwc_command(uint32_t iwcCommand)
  391. {
  392. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  393. CmdqSecFillIwcCB cb = NULL;
  394. switch (iwcCommand) {
  395. case CMD_CMDQ_TL_CANCEL_TASK:
  396. cb = cmdq_sec_fill_iwc_cancel_msg_unlocked;
  397. break;
  398. case CMD_CMDQ_TL_SUBMIT_TASK:
  399. cb = cmdq_sec_fill_iwc_command_msg_unlocked;
  400. break;
  401. case CMD_CMDQ_TL_TEST_HELLO_TL:
  402. case CMD_CMDQ_TL_TEST_DUMMY:
  403. default:
  404. cb = cmdq_sec_fill_iwc_command_basic_unlocked;
  405. break;
  406. };
  407. return cb;
  408. #else
  409. CMDQ_ERR("SVP feature is not on\n");
  410. return NULL;
  411. #endif
  412. }
  413. int32_t cmdq_sec_send_context_session_message(cmdqSecContextHandle handle,
  414. uint32_t iwcCommand,
  415. struct TaskStruct *pTask,
  416. int32_t thread,
  417. CmdqSecFillIwcCB iwcFillCB, void *data)
  418. {
  419. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  420. int32_t status = 0;
  421. int32_t iwcRsp = 0;
  422. const CmdqSecFillIwcCB icwcFillCB = (NULL == iwcFillCB) ?
  423. cmdq_sec_get_iwc_msg_fill_cb_by_iwc_command(iwcCommand) : (iwcFillCB);
  424. CMDQ_MSG("enter cmdq_sec_send_context_session_message()");
  425. do {
  426. /* fill message bufer */
  427. /*debug level */
  428. ((iwcCmdqMessage_t *) (handle->iwcMessage))->debug.logLevel =
  429. cmdq_core_get_sec_print_count() ? LOG_LEVEL_MSG : cmdq_core_get_log_level();
  430. status =
  431. icwcFillCB((iwcCmdqMessage_ptr) (handle->iwcMessage), iwcCommand, pTask,
  432. thread);
  433. if (0 > status)
  434. break;
  435. /* send message */
  436. status = cmdq_sec_execute_session_unlocked(handle);
  437. if (TZ_RESULT_SUCCESS != status) {
  438. CMDQ_ERR("cmdq_sec_execute_session_unlocked status is %d\n", status);
  439. break;
  440. }
  441. /* get secure task execution result */
  442. /*
  443. iwcRsp = ((iwcCmdqMessage_t*)(handle->iwcMessage))->rsp;
  444. status = iwcRsp; //(0 == iwcRsp)? (0):(-EFAULT);
  445. */
  446. /* and then, update task state */
  447. /* log print */
  448. if (0 < status) {
  449. CMDQ_ERR("SEC_SEND: status[%d], cmdId[%d], iwcRsp[%d]\n", status,
  450. iwcCommand, iwcRsp);
  451. } else {
  452. CMDQ_MSG("SEC_SEND: status[%d], cmdId[%d], iwcRsp[%d]\n", status,
  453. iwcCommand, iwcRsp);
  454. }
  455. } while (0);
  456. return status;
  457. #else
  458. CMDQ_ERR("SVP feature is not on\n");
  459. return 0;
  460. #endif
  461. }
  462. #if 0
  463. /*added only for cmdq unit test begin */
  464. int32_t cmdq_sec_test_proc(int testValue)
  465. {
  466. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  467. const int32_t tgid = current->tgid;
  468. cmdqSecContextHandle handle = NULL;
  469. /* fill param */
  470. MTEEC_PARAM param[4];
  471. unsigned int paramTypes;
  472. TZ_RESULT ret;
  473. CMDQ_MSG("DISP_IOCTL_CMDQ_SEC_TEST cmdq_test_proc\n");
  474. do {
  475. /* find handle first */
  476. /* handle = cmdq_sec_find_context_handle_unlocked(tgid); */
  477. handle = cmdq_sec_acquire_context_handle(tgid);
  478. if (NULL == handle) {
  479. CMDQ_ERR("SEC_SUBMIT: tgid %d err[NULL secCtxHandle]\n", tgid);
  480. ret = -(CMDQ_ERR_NULL_SEC_CTX_HANDLE);
  481. break;
  482. }
  483. param[0].value.a = (uint32_t) 111;
  484. param[1].value.a = (uint32_t) testValue;
  485. paramTypes = TZ_ParamTypes2(TZPT_VALUE_INPUT, TZPT_VALUE_INPUT);
  486. ret =
  487. KREE_TeeServiceCall(cmdq_session_handle(), CMD_CMDQ_TL_TEST_HELLO_TL,
  488. paramTypes, param);
  489. if (ret != TZ_RESULT_SUCCESS)
  490. CMDQ_ERR("TZCMD_CMDQ_TEST_HELLO fail, ret=%x\n", ret);
  491. else
  492. CMDQ_MSG("KREE_TeeServiceCall OK:%d\n", ret);
  493. } while (0);
  494. return ret;
  495. #else
  496. CMDQ_ERR("SVP feature is not on\n");
  497. return 0;
  498. #endif
  499. }
  500. /*added only for cmdq unit test end */
  501. #endif
  502. #if !(CMDQ_OPEN_SESSION_ONCE)
  503. static int32_t cmdq_sec_teardown_context_session(cmdqSecContextHandle handle)
  504. {
  505. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  506. int32_t status = 0;
  507. if (handle) {
  508. CMDQ_MSG("SEC_TEARDOWN:iwcMessage:0x%p\n", handle->iwcMessage);
  509. cmdq_sec_deinit_session_unlocked(handle);
  510. } else {
  511. CMDQ_ERR("SEC_TEARDOWN: null secCtxHandle\n");
  512. status = -1;
  513. }
  514. return status;
  515. #else
  516. CMDQ_ERR("SVP feature is not on\n");
  517. return 0;
  518. #endif
  519. }
  520. #endif
  521. int32_t cmdq_sec_submit_to_secure_world_async_unlocked(uint32_t iwcCommand,
  522. struct TaskStruct *pTask,
  523. int32_t thread,
  524. CmdqSecFillIwcCB iwcFillCB, void *data)
  525. {
  526. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  527. const int32_t tgid = current->tgid;
  528. const int32_t pid = current->pid;
  529. cmdqSecContextHandle handle = NULL;
  530. int32_t status = 0;
  531. int32_t duration = 0;
  532. CMDQ_TIME tEntrySec;
  533. CMDQ_TIME tExitSec;
  534. #if 0
  535. if (NULL != pTask)
  536. cmdq_core_dump_secure_metadata(&(pTask->secData));
  537. #endif
  538. CMDQ_MSG("[SEC]-->SEC_SUBMIT: tgid[%d:%d]\n", tgid, pid);
  539. do {
  540. /* find handle first */
  541. if (NULL == gCmdqSecContextHandle)
  542. gCmdqSecContextHandle = cmdq_sec_context_handle_create(current->tgid);
  543. handle = gCmdqSecContextHandle;
  544. /* find handle first */
  545. /* handle = cmdq_sec_find_context_handle_unlocked(tgid); */
  546. /* handle = cmdq_sec_acquire_context_handle(tgid); */
  547. if (NULL == handle) {
  548. CMDQ_ERR("SEC_SUBMIT: tgid %d err[NULL secCtxHandle]\n", tgid);
  549. status = -(CMDQ_ERR_NULL_SEC_CTX_HANDLE);
  550. break;
  551. }
  552. /* alloc iwc buffer */
  553. if (0 > cmdq_sec_setup_context_session(handle)) {
  554. status = -(CMDQ_ERR_SEC_CTX_SETUP);
  555. break;
  556. }
  557. CMDQ_MSG("leaving cmdq_sec_setup_context_session()\n");
  558. /* */
  559. /* record profile data */
  560. /* tbase timer/time support is not enough currently, */
  561. /* so we treats entry/exit timing to secure world as the trigger/gotIRQ_&_wakeup timing */
  562. /* */
  563. if (CMD_CMDQ_TL_INIT_SHARED_MEMORY == iwcCommand)
  564. gCmdqContext.hSecSharedMem->handle = handle;
  565. tEntrySec = sched_clock();
  566. status =
  567. cmdq_sec_send_context_session_message(handle, iwcCommand, pTask, thread,
  568. iwcFillCB, data);
  569. tExitSec = sched_clock();
  570. CMDQ_GET_TIME_IN_US_PART(tEntrySec, tExitSec, duration);
  571. if (pTask) {
  572. pTask->trigger = tEntrySec;
  573. pTask->gotIRQ = tExitSec;
  574. pTask->wakedUp = tExitSec;
  575. }
  576. #if !(CMDQ_OPEN_SESSION_ONCE)
  577. cmdq_sec_teardown_context_session(handle); /* teardown context session in Deinit */
  578. #endif
  579. /* Note we entry secure for config only and wait result in normal world. */
  580. /* No need reset module HW for config failed case */
  581. } while (0);
  582. if (-ETIMEDOUT == status) {
  583. /* t-base strange issue, mc_wait_notification false timeout when secure world has done */
  584. /* because retry may failed, give up retry method */
  585. CMDQ_ERR
  586. ("CMDQ [SEC]<--SEC_SUBMIT: err[%d][mc_wait_notification timeout], pTask[0x%p], THR[%d], tgid[%d:%d], config_duration_ms[%d], cmdId[%d]\n",
  587. status, pTask, thread, tgid, pid, duration, iwcCommand);
  588. } else if (0 > status) {
  589. #if 0
  590. if (!skipSecCtxDump) {
  591. mutex_lock(&gCmdqSecContextLock);
  592. cmdq_sec_dump_context_list();
  593. mutex_unlock(&gCmdqSecContextLock);
  594. }
  595. #endif
  596. /* throw AEE */
  597. CMDQ_AEE("CMDQ",
  598. "[SEC]<--SEC_SUBMIT: err[%d], pTask[0x%p], THR[%d], tgid[%d:%d], config_duration_ms[%d], cmdId[%d]\n",
  599. status, pTask, thread, tgid, pid, duration, iwcCommand);
  600. } else {
  601. CMDQ_MSG
  602. ("[SEC]<--SEC_SUBMIT: err[%d], pTask[0x%p], THR[%d], tgid[%d:%d], config_duration_ms[%d], cmdId[%d]\n",
  603. status, pTask, thread, tgid, pid, duration, iwcCommand);
  604. }
  605. return status;
  606. #else
  607. CMDQ_ERR("SVP feature is not supported\n");
  608. return 0;
  609. #endif
  610. }
  611. int32_t cmdq_sec_exec_task_async_unlocked(struct TaskStruct *pTask, int32_t thread)
  612. {
  613. #ifdef CMDQ_SECURE_PATH_SUPPORT
  614. int32_t status = 0;
  615. status =
  616. cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_SUBMIT_TASK, pTask, thread,
  617. NULL, NULL);
  618. if (0 > status)
  619. CMDQ_ERR("%s[%d]\n", __func__, status);
  620. return status;
  621. #else
  622. CMDQ_ERR("SVP feature is not on\n");
  623. return -EFAULT;
  624. #endif
  625. }
  626. int32_t cmdq_sec_cancel_error_task_unlocked(struct TaskStruct *pTask, int32_t thread,
  627. cmdqSecCancelTaskResultStruct *pResult)
  628. {
  629. #ifdef CMDQ_SECURE_PATH_SUPPORT
  630. int32_t status = 0;
  631. if ((NULL == pTask) || (false == cmdq_core_is_a_secure_thread(thread)) || (NULL == pResult)) {
  632. CMDQ_ERR("%s invalid param, pTask:%p, thread:%d, pResult:%p\n",
  633. __func__, pTask, thread, pResult);
  634. return -EFAULT;
  635. }
  636. status = cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_CANCEL_TASK,
  637. pTask, thread, NULL,
  638. (void *)pResult);
  639. return status;
  640. #else
  641. CMDQ_ERR("secure path not support\n");
  642. return -EFAULT;
  643. #endif
  644. }
  645. static atomic_t gCmdqSecPathResource = ATOMIC_INIT(0);
  646. int32_t cmdq_sec_allocate_path_resource_unlocked(void)
  647. {
  648. #ifdef CMDQ_SECURE_PATH_SUPPORT
  649. int32_t status = 0;
  650. if (1 == atomic_read(&gCmdqSecPathResource)) {
  651. /* has allocated successfully */
  652. CMDQ_MSG("allocate path resource already\n");
  653. return status;
  654. }
  655. CMDQ_MSG("begine to allocate path resource\n");
  656. status =
  657. cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_PATH_RES_ALLOCATE, NULL, -1,
  658. NULL, NULL);
  659. if (0 > status)
  660. CMDQ_ERR("%s[%d]\n", __func__, status);
  661. else
  662. atomic_set(&gCmdqSecPathResource, 1);
  663. return status;
  664. #else
  665. CMDQ_ERR("secure path not support\n");
  666. return -EFAULT;
  667. #endif
  668. }
  669. int32_t cmdq_sec_init_share_memory(void)
  670. {
  671. #ifdef CMDQ_SECURE_PATH_SUPPORT
  672. int32_t status = 0;
  673. CMDQ_MSG("starting test init share memory\n");
  674. status =
  675. cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_INIT_SHARED_MEMORY, NULL, -1,
  676. NULL, NULL);
  677. if (0 > status)
  678. CMDQ_ERR("%s[%d]\n", __func__, status);
  679. CMDQ_MSG("test init share memory end\n");
  680. return status;
  681. #else
  682. CMDQ_ERR("secure path not support\n");
  683. return -EFAULT;
  684. #endif
  685. }
  686. /* -------------------------------------------------------------------------------------------- */
  687. cmdqSecContextHandle cmdq_sec_find_context_handle_unlocked(uint32_t tgid)
  688. {
  689. #ifdef CMDQ_SECURE_PATH_SUPPORT
  690. cmdqSecContextHandle handle = NULL;
  691. do {
  692. struct cmdqSecContextStruct *secContextEntry = NULL;
  693. struct list_head *pos = NULL;
  694. list_for_each(pos, &gCmdqSecContextList) {
  695. secContextEntry = list_entry(pos, struct cmdqSecContextStruct, listEntry);
  696. if (secContextEntry && tgid == secContextEntry->tgid) {
  697. handle = secContextEntry;
  698. break;
  699. }
  700. }
  701. } while (0);
  702. CMDQ_MSG("SecCtxHandle_SEARCH: Handle[0x%p], tgid[%d]\n", handle, tgid);
  703. return handle;
  704. #else
  705. CMDQ_ERR("secure path not support\n");
  706. return NULL;
  707. #endif
  708. }
  709. int32_t cmdq_sec_release_context_handle_unlocked(cmdqSecContextHandle handle)
  710. {
  711. #ifdef CMDQ_SECURE_PATH_SUPPORT
  712. int32_t status = 0;
  713. do {
  714. handle->referCount--;
  715. if (0 < handle->referCount)
  716. break;
  717. /* 1. clean up secure path in secure world */
  718. /* 2. delete secContext from list */
  719. list_del(&(handle->listEntry));
  720. /* 3. release secure path resource in normal world */
  721. kfree(handle);
  722. } while (0);
  723. return status;
  724. #else
  725. CMDQ_ERR("secure path not support\n");
  726. return 0;
  727. #endif
  728. }
  729. int32_t cmdq_sec_release_context_handle(uint32_t tgid)
  730. {
  731. #ifdef CMDQ_SECURE_PATH_SUPPORT
  732. int32_t status = 0;
  733. cmdqSecContextHandle handle = NULL;
  734. mutex_lock(&gCmdqSecContextLock);
  735. smp_mb(); /*memory barrier */
  736. handle = cmdq_sec_find_context_handle_unlocked(tgid);
  737. if (handle) {
  738. CMDQ_MSG("SecCtxHandle_RELEASE: +tgid[%d], handle[0x%p]\n", tgid, handle);
  739. status = cmdq_sec_release_context_handle_unlocked(handle);
  740. CMDQ_MSG("SecCtxHandle_RELEASE: -tgid[%d], status[%d]\n", tgid, status);
  741. } else {
  742. status = -1;
  743. CMDQ_ERR("SecCtxHandle_RELEASE: err[secCtxHandle not exist], tgid[%d]\n", tgid);
  744. }
  745. mutex_unlock(&gCmdqSecContextLock);
  746. return status;
  747. #else
  748. CMDQ_ERR("secure path not support\n");
  749. return 0;
  750. #endif
  751. }
  752. cmdqSecContextHandle cmdq_sec_context_handle_create(uint32_t tgid)
  753. {
  754. #ifdef CMDQ_SECURE_PATH_SUPPORT
  755. cmdqSecContextHandle handle = NULL;
  756. handle = kmalloc(sizeof(uint8_t *) * sizeof(cmdqSecContextStruct), GFP_ATOMIC);
  757. if (handle) {
  758. handle->iwcMessage = NULL;
  759. handle->tgid = tgid;
  760. handle->referCount = 0;
  761. } else {
  762. CMDQ_ERR("SecCtxHandle_CREATE: err[LOW_MEM], tgid[%d]\n", tgid);
  763. }
  764. CMDQ_MSG("SecCtxHandle_CREATE: create new, Handle[0x%p], tgid[%d]\n", handle, tgid);
  765. return handle;
  766. #else
  767. CMDQ_ERR("secure path not support\n");
  768. return NULL;
  769. #endif
  770. }
  771. cmdqSecContextHandle cmdq_sec_acquire_context_handle(uint32_t tgid)
  772. {
  773. #ifdef CMDQ_SECURE_PATH_SUPPORT
  774. cmdqSecContextHandle handle = NULL;
  775. mutex_lock(&gCmdqSecContextLock);
  776. smp_mb(); /*memory barrier */
  777. do {
  778. /* find sec context of a process */
  779. handle = cmdq_sec_find_context_handle_unlocked(tgid);
  780. /* if it dose not exist, create new one */
  781. if (NULL == handle) {
  782. handle = cmdq_sec_context_handle_create(tgid);
  783. list_add_tail(&(handle->listEntry), &gCmdqSecContextList);
  784. }
  785. } while (0);
  786. /* increase caller referCount */
  787. if (handle)
  788. handle->referCount++;
  789. CMDQ_MSG("[CMDQ]SecCtxHandle_ACQUIRE, Handle[0x%p], tgid[%d], refCount[%d]\n", handle, tgid,
  790. handle->referCount);
  791. mutex_unlock(&gCmdqSecContextLock);
  792. return handle;
  793. #else
  794. CMDQ_ERR("secure path not support\n");
  795. return NULL;
  796. #endif
  797. }
  798. void cmdq_sec_dump_context_list(void)
  799. {
  800. #ifdef CMDQ_SECURE_PATH_SUPPORT
  801. struct cmdqSecContextStruct *secContextEntry = NULL;
  802. struct list_head *pos = NULL;
  803. CMDQ_ERR("=============== [CMDQ] sec context ===============\n");
  804. list_for_each(pos, &gCmdqSecContextList) {
  805. secContextEntry = list_entry(pos, struct cmdqSecContextStruct, listEntry);
  806. CMDQ_ERR("secCtxHandle[0x%p], tgid_%d[referCount: %d], state[%d], iwc[0x%p]\n",
  807. secContextEntry,
  808. secContextEntry->tgid,
  809. secContextEntry->referCount,
  810. secContextEntry->state, secContextEntry->iwcMessage);
  811. }
  812. #else
  813. CMDQ_ERR("secure path not support\n");
  814. #endif
  815. }
  816. void cmdqSecDeInitialize(void)
  817. {
  818. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  819. /* .TEE. close SVP CMDQ TEE serivice session */
  820. /* the sessions are created and accessed using [cmdq_session_handle()] / */
  821. /* [cmdq_mem_session_handle()] API, and closed here. */
  822. struct cmdqSecContextStruct *secContextEntry = NULL;
  823. struct list_head *pos = NULL;
  824. TZ_RESULT ret;
  825. ret = KREE_UnregisterSharedmem(gCmdqContext.hSecSharedMem->handle->memSessionHandle,
  826. gCmdqContext.hSecSharedMem->cmdq_share_cookie_handle);
  827. if (ret != TZ_RESULT_SUCCESS) {
  828. CMDQ_ERR("deinit unregister share memory failed ret=%d\n", ret);
  829. return;
  830. }
  831. ret = KREE_CloseSession(cmdq_session);
  832. if (ret != TZ_RESULT_SUCCESS) {
  833. CMDQ_ERR("DDP close ddp_session fail ret=%d\n", ret);
  834. return;
  835. }
  836. ret = KREE_CloseSession(cmdq_mem_session);
  837. if (ret != TZ_RESULT_SUCCESS) {
  838. CMDQ_ERR("DDP close ddp_mem_session fail ret=%d\n", ret);
  839. return;
  840. }
  841. /* release shared memory */
  842. cmdq_sec_destroy_shared_memory(gCmdqContext.hSecSharedMem);
  843. #if CMDQ_OPEN_SESSION_ONCE
  844. cmdq_sec_deinit_session_unlocked(gCmdqSecContextHandle);
  845. #endif
  846. if (NULL != gCmdqSecContextHandle) {
  847. kfree(gCmdqSecContextHandle);
  848. gCmdqSecContextHandle = NULL;
  849. }
  850. /*release cmdqSecContextHandle */
  851. mutex_lock(&gCmdqSecContextLock);
  852. smp_mb(); /*memory barrier */
  853. list_for_each(pos, &gCmdqSecContextList) {
  854. secContextEntry = list_entry(pos, struct cmdqSecContextStruct, listEntry);
  855. if (NULL != secContextEntry) {
  856. secContextEntry->referCount = 0;
  857. cmdq_sec_release_context_handle_unlocked(secContextEntry);
  858. }
  859. }
  860. mutex_unlock(&gCmdqSecContextLock);
  861. #else
  862. CMDQ_ERR("secure path not support\n");
  863. #endif
  864. }
  865. int32_t cmdqSecRegisterSecureBuffer(struct transmitBufferStruct *pSecureData)
  866. {
  867. int32_t status = 0;
  868. KREE_SHAREDMEM_PARAM cmdq_shared_param;
  869. TZ_RESULT tzRes = TZ_RESULT_SUCCESS;
  870. do {
  871. if (NULL == pSecureData) {
  872. status = -1;
  873. break;
  874. }
  875. cmdq_shared_param.buffer = pSecureData->pBuffer;
  876. cmdq_shared_param.size = pSecureData->size;
  877. if (0 == pSecureData->memSessionHandle) {
  878. pSecureData->memSessionHandle = cmdq_mem_session_handle();
  879. if (0 == pSecureData->memSessionHandle) {
  880. status = -2;
  881. break;
  882. }
  883. }
  884. tzRes = KREE_RegisterSharedmem(pSecureData->memSessionHandle, &(pSecureData->shareMemHandle),
  885. &cmdq_shared_param);
  886. if (tzRes != TZ_RESULT_SUCCESS) {
  887. status = -3;
  888. break;
  889. }
  890. } while (0);
  891. if (0 != status)
  892. CMDQ_ERR("cmdqSecRegisterSecureBuffer failed, status[%d]\n", status);
  893. return status;
  894. }
  895. int32_t cmdqSecServiceCall(struct transmitBufferStruct *pSecureData, int32_t cmd)
  896. {
  897. MTEEC_PARAM cmdq_param[4];
  898. unsigned int paramTypes = TZ_ParamTypes1(TZPT_MEMREF_INPUT);
  899. TZ_RESULT tzRes = TZ_RESULT_SUCCESS;
  900. if (0 == pSecureData->cmdqHandle) {
  901. pSecureData->cmdqHandle = cmdq_session_handle();
  902. if (0 == pSecureData->cmdqHandle) {
  903. CMDQ_ERR("get cmdq handle failed\n");
  904. return -1;
  905. }
  906. }
  907. cmdq_param[0].memref.handle = (uint32_t) pSecureData->shareMemHandle;
  908. cmdq_param[0].memref.offset = 0;
  909. cmdq_param[0].memref.size = pSecureData->size;
  910. tzRes = KREE_TeeServiceCall(pSecureData->cmdqHandle, cmd, paramTypes, cmdq_param);
  911. if (tzRes != TZ_RESULT_SUCCESS) {
  912. CMDQ_ERR("leave secure world cmdqSecServiceCall fail, ret=0x%x\n", tzRes);
  913. return -2;
  914. }
  915. return 0;
  916. }
  917. int32_t cmdqSecUnRegisterSecureBuffer(struct transmitBufferStruct *pSecureData)
  918. {
  919. TZ_RESULT ret = TZ_RESULT_SUCCESS;
  920. ret = KREE_UnregisterSharedmem(pSecureData->memSessionHandle,
  921. pSecureData->shareMemHandle);
  922. if (ret != TZ_RESULT_SUCCESS) {
  923. CMDQ_ERR("deinit unregister share memory failed ret=%d\n", ret);
  924. return -1;
  925. }
  926. return 0;
  927. }
  928. void cmdq_sec_register_secure_irq(void)
  929. {
  930. /*
  931. ** pass SECURE IRQ ID to trustzone
  932. ** 1 prepare buffer to transfer to secure world
  933. ** 2 register secure buffer
  934. ** 3 service call
  935. ** 4 unregister secure buffer
  936. */
  937. /* 1 prepare buffer to transfer to secure world */
  938. uint32_t secureIrqID = cmdq_dev_get_irq_secure_id();
  939. struct transmitBufferStruct secureData;
  940. memset(&secureData, 0, sizeof(secureData));
  941. secureData.pBuffer = &secureIrqID;
  942. secureData.size = sizeof(secureIrqID);
  943. /* 2 register secure buffer */
  944. if (0 != cmdqSecRegisterSecureBuffer(&secureData))
  945. return;
  946. CMDQ_LOG("register secure irq normal\n");
  947. /* 3 service call */
  948. cmdqSecServiceCall(&secureData, CMD_CMDQ_TL_REGISTER_SECURE_IRQ);
  949. /* 4 unregister secure buffer */
  950. cmdqSecUnRegisterSecureBuffer(&secureData);
  951. }
  952. void cmdqSecInitialize(void)
  953. {
  954. #ifdef CMDQ_SECURE_PATH_SUPPORT
  955. INIT_LIST_HEAD(&gCmdqSecContextList);
  956. /* cmdq_sec_allocate_path_resource_unlocked(); */
  957. #if 0
  958. /*
  959. ** no need to pass virtual irq id to secure world. MTEE need hardware irq id instead of virtual irq id
  960. */
  961. /* register secure IRQ handle */
  962. cmdq_sec_lock_secure_path();
  963. cmdq_sec_register_secure_irq();
  964. cmdq_sec_unlock_secure_path();
  965. #endif
  966. #if 0
  967. /* allocate shared memory */
  968. gCmdqContext.hSecSharedMem = NULL;
  969. cmdq_sec_create_shared_memory(&(gCmdqContext.hSecSharedMem), PAGE_SIZE);
  970. /* init share memory */
  971. cmdq_sec_lock_secure_path();
  972. cmdq_sec_init_share_memory();
  973. cmdq_sec_unlock_secure_path();
  974. #endif
  975. #endif
  976. }
  977. void cmdq_sec_init_secure_path(void)
  978. {
  979. #ifdef CMDQ_SECURE_PATH_SUPPORT
  980. CMDQ_LOG("begin to init secure path\n");
  981. /* allocate shared memory */
  982. gCmdqContext.hSecSharedMem = NULL;
  983. cmdq_sec_create_shared_memory(&(gCmdqContext.hSecSharedMem), PAGE_SIZE);
  984. /* init share memory */
  985. cmdq_sec_lock_secure_path();
  986. cmdq_sec_init_share_memory();
  987. cmdq_sec_unlock_secure_path();
  988. CMDQ_LOG("init secure path done\n");
  989. #endif
  990. }
  991. int32_t cmdq_sec_sync_handle_hdcp_unlock(struct cmdqSyncHandleHdcpStruct syncHandle)
  992. {
  993. #ifdef CMDQ_SECURE_PATH_SUPPORT
  994. TZ_RESULT tzRes;
  995. int32_t status = 0;
  996. MTEEC_PARAM cmdq_param[4];
  997. unsigned int paramTypes;
  998. cmdq_param[0].value.a = syncHandle.srcHandle;
  999. cmdq_param[0].value.b = syncHandle.dstHandle;
  1000. paramTypes = TZ_ParamTypes1(TZPT_VALUE_INPUT);
  1001. tzRes =
  1002. KREE_TeeServiceCall(cmdq_session_handle(),
  1003. CMD_CMDQ_TL_SYNC_HANDLE_HDCP, paramTypes, cmdq_param);
  1004. if (tzRes != TZ_RESULT_SUCCESS) {
  1005. CMDQ_ERR("CMD_CMDQ_TL_SYNC_HANDLE_HDCP fail, ret=0x%x\n", tzRes);
  1006. return tzRes;
  1007. }
  1008. status = tzRes;
  1009. CMDQ_MSG("KREE_TeeServiceCall tzRes =0x%x\n", tzRes);
  1010. return status;
  1011. #else
  1012. CMDQ_ERR("secure path not support\n");
  1013. return -EFAULT;
  1014. #endif
  1015. }
  1016. /* ------------------------------------------------------------------------------------------ */
  1017. /* debug */
  1018. /* */
  1019. void cmdq_sec_set_commandId(uint32_t cmdId)
  1020. {
  1021. atomic_set(&gDebugSecCmdId, cmdId);
  1022. }
  1023. const uint32_t cmdq_sec_get_commandId(void)
  1024. {
  1025. return (uint32_t) (atomic_read(&gDebugSecCmdId));
  1026. }
  1027. void cmdq_debug_set_sw_copy(int32_t value)
  1028. {
  1029. atomic_set(&gDebugSecSwCopy, value);
  1030. }
  1031. int32_t cmdq_debug_get_sw_copy(void)
  1032. {
  1033. return atomic_read(&gDebugSecSwCopy);
  1034. }
  1035. #endif