cmdq_sec.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. #include <linux/slab.h>
  2. #include <linux/delay.h>
  3. #include "cmdq_sec.h"
  4. #include "cmdq_def.h"
  5. #include "cmdq_virtual.h"
  6. #include "cmdq_device.h"
  7. #ifdef CMDQ_MET_READY
  8. #include <linux/met_drv.h>
  9. #endif
  10. static DEFINE_MUTEX(gCmdqSecExecLock); /* lock to protect atomic secure task execution */
  11. static DEFINE_MUTEX(gCmdqSecProfileLock); /* ensure atomic enable/disable secure path profile */
  12. static struct list_head gCmdqSecContextList; /* secure context list. note each porcess has its own sec context */
  13. #if defined(CMDQ_SECURE_PATH_SUPPORT)
  14. /* mobicore driver interface */
  15. #include "mobicore_driver_api.h"
  16. /* sectrace interface */
  17. #ifdef CMDQ_SECTRACE_SUPPORT
  18. #include <linux/sectrace.h>
  19. #endif
  20. /* secure path header */
  21. #include "cmdqSecTl_Api.h"
  22. #define CMDQ_DR_UUID { { 2, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
  23. #define CMDQ_TL_UUID { { 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
  24. static cmdqSecContextHandle gCmdqSecContextHandle; /* secure context to cmdqSecTL */
  25. /* sectrace */
  26. struct mc_bulk_map gCmdqSectraceMappedInfo; /* sectrace log buffer, which mapped between NWd and SWd */
  27. /* internal control */
  28. /* Set 1 to open once for each process context, because of below reasons:
  29. * 1. mc_open_session is too slow (major)
  30. * 2. we entry secure world for config and trigger GCE, and wait in normal world
  31. */
  32. #define CMDQ_OPEN_SESSION_ONCE (1)
  33. /********************************************************************************
  34. * operator API
  35. *******************************************************************************/
  36. int32_t cmdq_sec_open_mobicore_impl(uint32_t deviceId)
  37. {
  38. int32_t status;
  39. enum mc_result mcRet = MC_DRV_ERR_UNKNOWN;
  40. int retry_cnt = 0, max_retry = 30;
  41. do {
  42. status = 0;
  43. mcRet = mc_open_device(deviceId);
  44. /* Currently, a process context limits to open mobicore device once, */
  45. /* and mc_open_device dose not support reference cout */
  46. /* so skip the false alarm error.... */
  47. if (MC_DRV_ERR_INVALID_OPERATION == mcRet) {
  48. CMDQ_MSG("[SEC]_MOBICORE_OPEN: already opened, continue to execution\n");
  49. status = -EEXIST;
  50. } else if (MC_DRV_OK != mcRet) {
  51. CMDQ_ERR("[SEC]_MOBICORE_OPEN: err[0x%x], retry[%d]\n", mcRet, retry_cnt);
  52. status = -1;
  53. msleep_interruptible(2000);
  54. retry_cnt++;
  55. continue;
  56. }
  57. break;
  58. } while (retry_cnt < max_retry);
  59. if (retry_cnt >= max_retry) {
  60. /* print error message */
  61. CMDQ_ERR("[SEC]_MOBICORE_OPEN fail: status[%d], mcRet[0x%x], retry[%d]\n", status, mcRet, retry_cnt);
  62. } else {
  63. CMDQ_MSG("[SEC]_MOBICORE_OPEN: status[%d], mcRet[0x%x], retry[%d]\n", status, mcRet, retry_cnt);
  64. }
  65. return status;
  66. }
  67. int32_t cmdq_sec_close_mobicore_impl(const uint32_t deviceId, const uint32_t openMobicoreByOther)
  68. {
  69. int32_t status = 0;
  70. enum mc_result mcRet = 0;
  71. if (1 == openMobicoreByOther) {
  72. /* do nothing */
  73. /* let last user to close mobicore.... */
  74. CMDQ_MSG("[SEC]_MOBICORE_CLOSE: opened by other, bypass device close\n");
  75. } else {
  76. mcRet = mc_close_device(deviceId);
  77. CMDQ_MSG("[SEC]_MOBICORE_CLOSE: status[%d], ret[%0x], openMobicoreByOther[%d]\n",
  78. status, mcRet, openMobicoreByOther);
  79. if (MC_DRV_OK != mcRet) {
  80. CMDQ_ERR("[SEC]_MOBICORE_CLOSE: err[0x%x]\n", mcRet);
  81. status = -1;
  82. }
  83. }
  84. return status;
  85. }
  86. int32_t cmdq_sec_allocate_wsm_impl(uint32_t deviceId, uint8_t **ppWsm, uint32_t wsmSize)
  87. {
  88. int32_t status = 0;
  89. enum mc_result mcRet = MC_DRV_OK;
  90. do {
  91. if ((*ppWsm) != NULL) {
  92. status = -1;
  93. CMDQ_ERR("[SEC]_WSM_ALLOC: err[pWsm is not NULL]");
  94. break;
  95. }
  96. /* because world shared mem(WSM) will ba managed by mobicore device, not linux kernel */
  97. /* instead of vmalloc/kmalloc, call mc_malloc_wasm to alloc WSM to prvent error such as */
  98. /* "can not resolve tci physicall address" etc */
  99. mcRet = mc_malloc_wsm(deviceId, 0, wsmSize, ppWsm, 0);
  100. if (MC_DRV_OK != mcRet) {
  101. CMDQ_ERR("[SEC]_WSM_ALLOC: err[0x%x]\n", mcRet);
  102. status = -1;
  103. break;
  104. }
  105. CMDQ_MSG("[SEC]_WSM_ALLOC: status[%d], *ppWsm: 0x%p\n", status, (*ppWsm));
  106. } while (0);
  107. return status;
  108. }
  109. int32_t cmdq_sec_free_wsm_impl(uint32_t deviceId, uint8_t **ppWsm)
  110. {
  111. int32_t status = 0;
  112. enum mc_result mcRet = mc_free_wsm(deviceId, (*ppWsm));
  113. (*ppWsm) = (MC_DRV_OK == mcRet) ? (NULL) : (*ppWsm);
  114. CMDQ_VERBOSE("_WSM_FREE: ret[0x%x], *ppWsm[0x%p]\n", mcRet, (*ppWsm));
  115. if (MC_DRV_OK != mcRet) {
  116. CMDQ_ERR("_WSM_FREE: err[0x%x]", mcRet);
  117. status = -1;
  118. }
  119. return status;
  120. }
  121. int32_t cmdq_sec_open_session_impl(uint32_t deviceId,
  122. const struct mc_uuid_t *uuid,
  123. uint8_t *pWsm,
  124. uint32_t wsmSize, struct mc_session_handle *pSessionHandle)
  125. {
  126. int32_t status = 0;
  127. enum mc_result mcRet = MC_DRV_OK;
  128. do {
  129. if (NULL == pWsm || NULL == pSessionHandle) {
  130. status = -1;
  131. CMDQ_ERR
  132. ("[SEC]_SESSION_OPEN: invalid param, pWsm[0x%p], pSessionHandle[0x%p]\n",
  133. pWsm, pSessionHandle);
  134. break;
  135. }
  136. memset(pSessionHandle, 0, sizeof(*pSessionHandle));
  137. pSessionHandle->device_id = deviceId;
  138. mcRet = mc_open_session(pSessionHandle, uuid, pWsm, wsmSize);
  139. if (MC_DRV_OK != mcRet) {
  140. CMDQ_ERR("[SEC]_SESSION_OPEN: err[0x%x]\n", mcRet);
  141. status = -1;
  142. break;
  143. }
  144. CMDQ_MSG("[SEC]_SESSION_OPEN: status[%d], mcRet[0x%x]\n", status, mcRet);
  145. } while (0);
  146. return status;
  147. }
  148. int32_t cmdq_sec_close_session_impl(struct mc_session_handle *pSessionHandle)
  149. {
  150. int32_t status = 0;
  151. enum mc_result mcRet = mc_close_session(pSessionHandle);
  152. if (MC_DRV_OK != mcRet) {
  153. CMDQ_ERR("_SESSION_CLOSE: err[0x%x]", mcRet);
  154. status = -1;
  155. }
  156. return status;
  157. }
  158. int32_t cmdq_sec_init_session_unlocked(const struct mc_uuid_t *uuid,
  159. uint8_t **ppWsm,
  160. uint32_t wsmSize,
  161. struct mc_session_handle *pSessionHandle,
  162. CMDQ_IWC_STATE_ENUM *pIwcState,
  163. uint32_t *openMobicoreByOther)
  164. {
  165. int32_t openRet = 0;
  166. int32_t status = 0;
  167. uint32_t deviceId = MC_DEVICE_ID_DEFAULT;
  168. CMDQ_MSG("[SEC]-->SESSION_INIT: iwcState[%d]\n", (*pIwcState));
  169. do {
  170. #if CMDQ_OPEN_SESSION_ONCE
  171. if (IWC_SES_OPENED <= (*pIwcState)) {
  172. CMDQ_MSG("SESSION_INIT: already opened\n");
  173. break;
  174. }
  175. CMDQ_MSG("[SEC]SESSION_INIT: open new session[%d]\n", (*pIwcState));
  176. #endif
  177. CMDQ_VERBOSE
  178. ("[SEC]SESSION_INIT: wsmSize[%d], pSessionHandle: 0x%p\n",
  179. wsmSize, pSessionHandle);
  180. CMDQ_PROF_START(current->pid, "CMDQ_SEC_INIT");
  181. if (IWC_MOBICORE_OPENED > (*pIwcState)) {
  182. /* open mobicore device */
  183. openRet = cmdq_sec_open_mobicore_impl(deviceId);
  184. if (-EEXIST == openRet) {
  185. /* mobicore has been opened in this process context */
  186. /* it is a ok case, so continue to execute */
  187. status = 0;
  188. (*openMobicoreByOther) = 1;
  189. } else if (0 > openRet) {
  190. status = -1;
  191. break;
  192. }
  193. (*pIwcState) = IWC_MOBICORE_OPENED;
  194. }
  195. if (IWC_WSM_ALLOCATED > (*pIwcState)) {
  196. /* allocate world shared memory */
  197. if (0 > cmdq_sec_allocate_wsm_impl(deviceId, ppWsm, wsmSize)) {
  198. status = -1;
  199. break;
  200. }
  201. (*pIwcState) = IWC_WSM_ALLOCATED;
  202. }
  203. /* open a secure session */
  204. if (0 >
  205. cmdq_sec_open_session_impl(deviceId, uuid, (*ppWsm), wsmSize, pSessionHandle)) {
  206. status = -1;
  207. break;
  208. }
  209. (*pIwcState) = IWC_SES_OPENED;
  210. CMDQ_PROF_END(current->pid, "CMDQ_SEC_INIT");
  211. } while (0);
  212. CMDQ_MSG("[SEC]<--SESSION_INIT[%d]\n", status);
  213. return status;
  214. }
  215. int32_t cmdq_sec_fill_iwc_command_basic_unlocked(int32_t iwcCommand, void *_pTask, int32_t thread,
  216. void *_pIwc)
  217. {
  218. iwcCmdqMessage_t *pIwc;
  219. pIwc = (iwcCmdqMessage_t *) _pIwc;
  220. /* specify command id only, don't care other other */
  221. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  222. pIwc->cmd = iwcCommand;
  223. /* medatada: debug config */
  224. pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0);
  225. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  226. return 0;
  227. }
  228. int32_t cmdq_sec_fill_iwc_command_msg_unlocked(int32_t iwcCommand, void *_pTask, int32_t thread,
  229. void *_pIwc)
  230. {
  231. int32_t status;
  232. const TaskStruct *pTask = (TaskStruct *) _pTask;
  233. iwcCmdqMessage_t *pIwc;
  234. /* cmdqSecDr will insert some instr */
  235. const uint32_t reservedCommandSize = 4 * CMDQ_INST_SIZE;
  236. status = 0;
  237. pIwc = (iwcCmdqMessage_t *) _pIwc;
  238. /* check command size first */
  239. if (pTask && (CMDQ_TZ_CMD_BLOCK_SIZE < (pTask->commandSize + reservedCommandSize))) {
  240. CMDQ_ERR("[SEC]SESSION_MSG: pTask %p commandSize %d > %d\n",
  241. pTask, pTask->commandSize, CMDQ_TZ_CMD_BLOCK_SIZE);
  242. return -EFAULT;
  243. }
  244. CMDQ_MSG("[SEC]-->SESSION_MSG: cmdId[%d]\n", iwcCommand);
  245. /* fill message buffer for inter world communication */
  246. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  247. pIwc->cmd = iwcCommand;
  248. /* metadata */
  249. pIwc->command.metadata.enginesNeedDAPC = pTask->secData.enginesNeedDAPC;
  250. pIwc->command.metadata.enginesNeedPortSecurity = pTask->secData.enginesNeedPortSecurity;
  251. if (NULL != pTask && CMDQ_INVALID_THREAD != thread) {
  252. /* basic data */
  253. pIwc->command.scenario = pTask->scenario;
  254. pIwc->command.thread = thread;
  255. pIwc->command.priority = pTask->priority;
  256. pIwc->command.engineFlag = pTask->engineFlag;
  257. pIwc->command.commandSize = pTask->commandSize;
  258. pIwc->command.hNormalTask = 0LL | ((unsigned long)pTask);
  259. memcpy((pIwc->command.pVABase), (pTask->pVABase), (pTask->commandSize));
  260. /* cookie */
  261. pIwc->command.waitCookie = pTask->secData.waitCookie;
  262. pIwc->command.resetExecCnt = pTask->secData.resetExecCnt;
  263. CMDQ_MSG("[SEC]SESSION_MSG: task 0x%p, thread: %d, size: %d, scenario:%d, flag:0x%08llx\n",
  264. pTask, thread, pTask->commandSize, pTask->scenario, pTask->engineFlag);
  265. CMDQ_VERBOSE("[SEC]SESSION_MSG: addrList[%d][0x%p]\n",
  266. pTask->secData.addrMetadataCount,
  267. CMDQ_U32_PTR(pTask->secData.addrMetadatas));
  268. if (0 < pTask->secData.addrMetadataCount) {
  269. pIwc->command.metadata.addrListLength = pTask->secData.addrMetadataCount;
  270. memcpy((pIwc->command.metadata.addrList), CMDQ_U32_PTR(pTask->secData.addrMetadatas),
  271. (pTask->secData.addrMetadataCount) * sizeof(iwcCmdqAddrMetadata_t));
  272. }
  273. /* medatada: debug config */
  274. pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0);
  275. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  276. } else {
  277. /* relase resource, or debug function will go here */
  278. CMDQ_VERBOSE("[SEC]-->SESSION_MSG: no task, cmdId[%d]\n", iwcCommand);
  279. pIwc->command.commandSize = 0;
  280. pIwc->command.metadata.addrListLength = 0;
  281. }
  282. CMDQ_MSG("[SEC]<--SESSION_MSG[%d]\n", status);
  283. return status;
  284. }
  285. /* TODO: when do release secure command buffer */
  286. int32_t cmdq_sec_fill_iwc_resource_msg_unlocked(int32_t iwcCommand, void *_pTask, int32_t thread,
  287. void *_pIwc)
  288. {
  289. iwcCmdqMessage_t *pIwc;
  290. cmdqSecSharedMemoryHandle pSharedMem;
  291. pSharedMem = cmdq_core_get_secure_shared_memory();
  292. if (NULL == pSharedMem) {
  293. CMDQ_ERR("FILL:RES, NULL shared memory\n");
  294. return -EFAULT;
  295. }
  296. if (pSharedMem && NULL == pSharedMem->pVABase) {
  297. CMDQ_ERR("FILL:RES, %p shared memory has not init\n", pSharedMem);
  298. return -EFAULT;
  299. }
  300. pIwc = (iwcCmdqMessage_t *) _pIwc;
  301. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  302. pIwc->cmd = iwcCommand;
  303. pIwc->pathResource.shareMemoyPA = 0LL | (pSharedMem->MVABase);
  304. pIwc->pathResource.size = pSharedMem->size;
  305. #if defined(CMDQ_SECURE_PATH_NORMAL_IRQ) || defined(CMDQ_SECURE_PATH_HW_LOCK)
  306. pIwc->pathResource.useNormalIRQ = 1;
  307. #else
  308. pIwc->pathResource.useNormalIRQ = 0;
  309. #endif
  310. CMDQ_MSG("FILL:RES, shared memory:%pa(0x%llx), size:%d\n",
  311. &(pSharedMem->MVABase), pIwc->pathResource.shareMemoyPA, pSharedMem->size);
  312. /* medatada: debug config */
  313. pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0);
  314. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  315. return 0;
  316. }
  317. int32_t cmdq_sec_fill_iwc_cancel_msg_unlocked(int32_t iwcCommand, void *_pTask, int32_t thread,
  318. void *_pIwc)
  319. {
  320. const TaskStruct *pTask = (TaskStruct *) _pTask;
  321. iwcCmdqMessage_t *pIwc;
  322. pIwc = (iwcCmdqMessage_t *) _pIwc;
  323. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  324. pIwc->cmd = iwcCommand;
  325. pIwc->cancelTask.waitCookie = pTask->secData.waitCookie;
  326. pIwc->cancelTask.thread = thread;
  327. /* medatada: debug config */
  328. pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0);
  329. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  330. CMDQ_LOG("FILL:CANCEL_TASK: task: %p, thread:%d, cookie:%d\n",
  331. pTask, thread, pTask->secData.waitCookie);
  332. return 0;
  333. }
  334. int32_t cmdq_sec_execute_session_unlocked(struct mc_session_handle *pSessionHandle,
  335. CMDQ_IWC_STATE_ENUM *pIwcState, int32_t timeout_ms)
  336. {
  337. enum mc_result mcRet;
  338. int32_t status = 0;
  339. const int32_t secureWoldTimeout_ms = (0 < timeout_ms) ?
  340. (timeout_ms) : (MC_INFINITE_TIMEOUT);
  341. CMDQ_PROF_START(current->pid, "CMDQ_SEC_EXE");
  342. do {
  343. /* notify to secure world */
  344. mcRet = mc_notify(pSessionHandle);
  345. if (MC_DRV_OK != mcRet) {
  346. CMDQ_ERR("[SEC]EXEC: mc_notify err[0x%x]\n", mcRet);
  347. status = -1;
  348. break;
  349. }
  350. CMDQ_MSG("[SEC]EXEC: mc_notify ret[0x%x]\n", mcRet);
  351. (*pIwcState) = IWC_SES_TRANSACTED;
  352. /* wait respond */
  353. mcRet = mc_wait_notification(pSessionHandle, secureWoldTimeout_ms);
  354. if (MC_DRV_ERR_TIMEOUT == mcRet) {
  355. CMDQ_ERR
  356. ("[SEC]EXEC: mc_wait_notification timeout, err[0x%x], secureWoldTimeout_ms[%d]\n",
  357. mcRet, secureWoldTimeout_ms);
  358. status = -ETIMEDOUT;
  359. break;
  360. }
  361. if (MC_DRV_OK != mcRet) {
  362. CMDQ_ERR("[SEC]EXEC: mc_wait_notification err[0x%x]\n", mcRet);
  363. status = -1;
  364. break;
  365. }
  366. CMDQ_MSG("[SEC]EXEC: mc_wait_notification err[%d]\n", mcRet);
  367. (*pIwcState) = IWC_SES_ON_TRANSACTED;
  368. } while (0);
  369. CMDQ_PROF_END(current->pid, "CMDQ_SEC_EXE");
  370. return status;
  371. }
  372. void cmdq_sec_deinit_session_unlocked(uint8_t **ppWsm,
  373. struct mc_session_handle *pSessionHandle,
  374. const CMDQ_IWC_STATE_ENUM iwcState,
  375. const uint32_t openMobicoreByOther)
  376. {
  377. uint32_t deviceId = MC_DEVICE_ID_DEFAULT;
  378. CMDQ_MSG("[SEC]-->SESSION_DEINIT\n");
  379. do {
  380. switch (iwcState) {
  381. case IWC_SES_ON_TRANSACTED:
  382. case IWC_SES_TRANSACTED:
  383. case IWC_SES_MSG_PACKAGED:
  384. /* continue next clean-up */
  385. case IWC_SES_OPENED:
  386. cmdq_sec_close_session_impl(pSessionHandle);
  387. /* continue next clean-up */
  388. case IWC_WSM_ALLOCATED:
  389. cmdq_sec_free_wsm_impl(deviceId, ppWsm);
  390. /* continue next clean-up */
  391. case IWC_MOBICORE_OPENED:
  392. cmdq_sec_close_mobicore_impl(deviceId, openMobicoreByOther);
  393. /* continue next clean-up */
  394. break;
  395. case IWC_INIT:
  396. /* CMDQ_ERR("open secure driver failed\n"); */
  397. break;
  398. default:
  399. break;
  400. }
  401. } while (0);
  402. CMDQ_MSG("[SEC]<--SESSION_DEINIT\n");
  403. }
  404. /********************************************************************************
  405. * context handle
  406. *******************************************************************************/
  407. int32_t cmdq_sec_setup_context_session(cmdqSecContextHandle handle)
  408. {
  409. int32_t status = 0;
  410. const struct mc_uuid_t uuid = CMDQ_TL_UUID;
  411. /* init iwc parameter */
  412. if (IWC_INIT == handle->state)
  413. handle->uuid = uuid;
  414. /* init secure session */
  415. status = cmdq_sec_init_session_unlocked(&(handle->uuid),
  416. (uint8_t **) (&(handle->iwcMessage)),
  417. sizeof(iwcCmdqMessage_t),
  418. &(handle->sessionHandle),
  419. &(handle->state), &(handle->openMobicoreByOther));
  420. CMDQ_MSG("SEC_SETUP: status[%d], tgid[%d], mobicoreOpenByOther[%d]\n",
  421. status, handle->tgid, handle->openMobicoreByOther);
  422. return status;
  423. }
  424. int32_t cmdq_sec_handle_session_reply_unlocked(const iwcCmdqMessage_t *pIwc,
  425. const int32_t iwcCommand, TaskStruct *pTask,
  426. void *data)
  427. {
  428. int32_t status;
  429. int32_t iwcRsp;
  430. cmdqSecCancelTaskResultStruct *pCancelResult = NULL;
  431. /* get secure task execution result */
  432. iwcRsp = (pIwc)->rsp;
  433. status = iwcRsp;
  434. if (CMD_CMDQ_TL_CANCEL_TASK == iwcCommand) {
  435. pCancelResult = (cmdqSecCancelTaskResultStruct *) data;
  436. if (pCancelResult) {
  437. pCancelResult->throwAEE = pIwc->cancelTask.throwAEE;
  438. pCancelResult->hasReset = pIwc->cancelTask.hasReset;
  439. pCancelResult->irqFlag = pIwc->cancelTask.irqFlag;
  440. pCancelResult->errInstr[0] = pIwc->cancelTask.errInstr[0]; /* argB */
  441. pCancelResult->errInstr[1] = pIwc->cancelTask.errInstr[1]; /* argA */
  442. pCancelResult->regValue = pIwc->cancelTask.regValue;
  443. pCancelResult->pc = pIwc->cancelTask.pc;
  444. }
  445. /* for WFE, we specifically dump the event value */
  446. if (CMDQ_CODE_WFE == ((pIwc->cancelTask.errInstr[1] & 0xFF000000) >> 24)) {
  447. const uint32_t eventID = 0x3FF & pIwc->cancelTask.errInstr[1];
  448. CMDQ_ERR
  449. ("=============== [CMDQ] Error WFE Instruction Status ===============\n");
  450. CMDQ_ERR("CMDQ_SYNC_TOKEN_VAL of %s is %d\n",
  451. cmdq_core_get_event_name(eventID), pIwc->cancelTask.regValue);
  452. }
  453. CMDQ_ERR
  454. ("CANCEL_TASK: pTask %p, INST:(0x%08x, 0x%08x), throwAEE:%d, hasReset:%d, pc:0x%08x\n",
  455. pTask, pIwc->cancelTask.errInstr[1], pIwc->cancelTask.errInstr[0],
  456. pIwc->cancelTask.throwAEE, pIwc->cancelTask.hasReset, pIwc->cancelTask.pc);
  457. } else if (CMD_CMDQ_TL_PATH_RES_ALLOCATE == iwcCommand
  458. || CMD_CMDQ_TL_PATH_RES_RELEASE == iwcCommand) {
  459. /* do nothing */
  460. } else {
  461. /* note we etnry SWd to config GCE, and wait execution result in NWd */
  462. /* update taskState only if config failed */
  463. if (pTask && 0 > iwcRsp)
  464. pTask->taskState = TASK_STATE_ERROR;
  465. }
  466. /* log print */
  467. if (0 < status) {
  468. CMDQ_ERR("SEC_SEND: status[%d], cmdId[%d], iwcRsp[%d]\n",
  469. status, iwcCommand, iwcRsp);
  470. } else {
  471. CMDQ_MSG("SEC_SEND: status[%d], cmdId[%d], iwcRsp[%d]\n",
  472. status, iwcCommand, iwcRsp);
  473. }
  474. return status;
  475. }
  476. CmdqSecFillIwcCB cmdq_sec_get_iwc_msg_fill_cb_by_iwc_command(uint32_t iwcCommand)
  477. {
  478. CmdqSecFillIwcCB cb = NULL;
  479. switch (iwcCommand) {
  480. case CMD_CMDQ_TL_CANCEL_TASK:
  481. cb = cmdq_sec_fill_iwc_cancel_msg_unlocked;
  482. break;
  483. case CMD_CMDQ_TL_PATH_RES_ALLOCATE:
  484. case CMD_CMDQ_TL_PATH_RES_RELEASE:
  485. cb = cmdq_sec_fill_iwc_resource_msg_unlocked;
  486. break;
  487. case CMD_CMDQ_TL_SUBMIT_TASK:
  488. cb = cmdq_sec_fill_iwc_command_msg_unlocked;
  489. break;
  490. case CMD_CMDQ_TL_TEST_HELLO_TL:
  491. case CMD_CMDQ_TL_TEST_DUMMY:
  492. default:
  493. cb = cmdq_sec_fill_iwc_command_basic_unlocked;
  494. break;
  495. };
  496. return cb;
  497. }
  498. int32_t cmdq_sec_send_context_session_message(cmdqSecContextHandle handle,
  499. uint32_t iwcCommand,
  500. TaskStruct *pTask,
  501. int32_t thread, CmdqSecFillIwcCB cb, void *data)
  502. {
  503. int32_t status;
  504. const int32_t timeout_ms = (3 * 1000);
  505. const CmdqSecFillIwcCB iwcFillCB = (NULL == cb) ?
  506. cmdq_sec_get_iwc_msg_fill_cb_by_iwc_command(iwcCommand) : (cb);
  507. do {
  508. /* fill message bufer */
  509. status = iwcFillCB(iwcCommand, pTask, thread, (void *)(handle->iwcMessage));
  510. if (0 > status) {
  511. CMDQ_ERR("[SEC]fill msg buffer failed[%d], pid[%d:%d], cmdId[%d]\n",
  512. status, current->tgid, current->pid, iwcCommand);
  513. break;
  514. }
  515. /* send message */
  516. status = cmdq_sec_execute_session_unlocked(&(handle->sessionHandle),
  517. &(handle->state), timeout_ms);
  518. if (0 > status) {
  519. CMDQ_ERR("[SEC]cmdq_sec_execute_session_unlocked failed[%d], pid[%d:%d]\n",
  520. status, current->tgid, current->pid);
  521. break;
  522. }
  523. status =
  524. cmdq_sec_handle_session_reply_unlocked(handle->iwcMessage, iwcCommand, pTask,
  525. data);
  526. } while (0);
  527. return status;
  528. }
  529. int32_t cmdq_sec_teardown_context_session(cmdqSecContextHandle handle)
  530. {
  531. int32_t status = 0;
  532. if (handle) {
  533. CMDQ_MSG("[SEC]SEC_TEARDOWN: state: %d, iwcMessage:0x%p\n",
  534. handle->state, handle->iwcMessage);
  535. cmdq_sec_deinit_session_unlocked((uint8_t **) (&(handle->iwcMessage)),
  536. &(handle->sessionHandle),
  537. handle->state, handle->openMobicoreByOther);
  538. /* clrean up handle's attritubes */
  539. handle->state = IWC_INIT;
  540. } else {
  541. CMDQ_ERR("[SEC]SEC_TEARDOWN: null secCtxHandle\n");
  542. status = -1;
  543. }
  544. return status;
  545. }
  546. void cmdq_sec_track_task_record(const uint32_t iwcCommand,
  547. TaskStruct *pTask, CMDQ_TIME *pEntrySec, CMDQ_TIME *pExitSec)
  548. {
  549. if (NULL == pTask)
  550. return;
  551. if (CMD_CMDQ_TL_SUBMIT_TASK != iwcCommand)
  552. return;
  553. /* record profile data */
  554. /* tbase timer/time support is not enough currently, */
  555. /* so we treats entry/exit timing to secure world as the trigger timing */
  556. pTask->entrySec = *pEntrySec;
  557. pTask->exitSec = *pExitSec;
  558. pTask->trigger = *pEntrySec;
  559. }
  560. int32_t cmdq_sec_submit_to_secure_world_async_unlocked(uint32_t iwcCommand,
  561. TaskStruct *pTask, int32_t thread,
  562. CmdqSecFillIwcCB iwcFillCB, void *data,
  563. bool throwAEE)
  564. {
  565. const int32_t tgid = current->tgid;
  566. const int32_t pid = current->pid;
  567. cmdqSecContextHandle handle = NULL;
  568. int32_t status = 0;
  569. int32_t duration = 0;
  570. char longMsg[CMDQ_LONGSTRING_MAX];
  571. uint32_t msgOffset;
  572. int32_t msgMAXSize;
  573. CMDQ_TIME tEntrySec;
  574. CMDQ_TIME tExitSec;
  575. CMDQ_MSG("[SEC]-->SEC_SUBMIT: tgid[%d:%d]\n", tgid, pid);
  576. do {
  577. /* find handle first */
  578. /* Unlike tBase user space API,
  579. * tBase kernel API maintains a GLOBAL table to control mobicore device reference count.
  580. * For kernel spece user, mc_open_device and session_handle don't depend on the process context.
  581. * Therefore we use global secssion handle to inter-world commumication. */
  582. if (NULL == gCmdqSecContextHandle)
  583. gCmdqSecContextHandle = cmdq_sec_context_handle_create(current->tgid);
  584. handle = gCmdqSecContextHandle;
  585. if (NULL == handle) {
  586. CMDQ_ERR("SEC_SUBMIT: tgid %d err[NULL secCtxHandle]\n", tgid);
  587. status = -(CMDQ_ERR_NULL_SEC_CTX_HANDLE);
  588. break;
  589. }
  590. if (0 > cmdq_sec_setup_context_session(handle)) {
  591. status = -(CMDQ_ERR_SEC_CTX_SETUP);
  592. break;
  593. }
  594. tEntrySec = sched_clock();
  595. status =
  596. cmdq_sec_send_context_session_message(handle, iwcCommand, pTask, thread,
  597. iwcFillCB, data);
  598. tExitSec = sched_clock();
  599. CMDQ_GET_TIME_IN_US_PART(tEntrySec, tExitSec, duration);
  600. cmdq_sec_track_task_record(iwcCommand, pTask, &tEntrySec, &tExitSec);
  601. /* release resource */
  602. #if !(CMDQ_OPEN_SESSION_ONCE)
  603. cmdq_sec_teardown_context_session(handle)
  604. #endif
  605. /* Note we entry secure for config only and wait result in normal world. */
  606. /* No need reset module HW for config failed case */
  607. } while (0);
  608. if (-ETIMEDOUT == status) {
  609. /* t-base strange issue, mc_wait_notification false timeout when secure world has done */
  610. /* because retry may failed, give up retry method */
  611. cmdq_core_longstring_init(longMsg, &msgOffset, &msgMAXSize);
  612. cmdqCoreLongString(false, longMsg, &msgOffset, &msgMAXSize,
  613. "[SEC]<--SEC_SUBMIT: err[%d][mc_wait_notification timeout], pTask[0x%p], THR[%d],",
  614. status, pTask, thread);
  615. cmdqCoreLongString(false, longMsg, &msgOffset, &msgMAXSize,
  616. " tgid[%d:%d], config_duration_ms[%d], cmdId[%d]\n",
  617. tgid, pid, duration, iwcCommand);
  618. if (throwAEE) {
  619. if (msgOffset > 0) {
  620. /* print message */
  621. CMDQ_AEE("CMDQ", "%s", longMsg);
  622. }
  623. } else {
  624. if (msgOffset > 0) {
  625. /* print message */
  626. CMDQ_ERR("%s", longMsg);
  627. }
  628. }
  629. } else if (0 > status) {
  630. /* dump metadata first */
  631. if (pTask)
  632. cmdq_core_dump_secure_metadata(&(pTask->secData));
  633. cmdq_core_longstring_init(longMsg, &msgOffset, &msgMAXSize);
  634. cmdqCoreLongString(false, longMsg, &msgOffset, &msgMAXSize,
  635. "[SEC]<--SEC_SUBMIT: err[%d], pTask[0x%p], THR[%d], tgid[%d:%d],",
  636. status, pTask, thread, tgid, pid);
  637. cmdqCoreLongString(false, longMsg, &msgOffset, &msgMAXSize,
  638. " config_duration_ms[%d], cmdId[%d]\n", duration, iwcCommand);
  639. if (throwAEE) {
  640. /* throw AEE */
  641. if (msgOffset > 0) {
  642. /* print message */
  643. CMDQ_AEE("CMDQ", "%s", longMsg);
  644. }
  645. } else {
  646. /* no throw AEE */
  647. if (msgOffset > 0) {
  648. /* print message */
  649. CMDQ_ERR("%s", longMsg);
  650. }
  651. }
  652. } else {
  653. cmdq_core_longstring_init(longMsg, &msgOffset, &msgMAXSize);
  654. cmdqCoreLongString(false, longMsg, &msgOffset, &msgMAXSize,
  655. "[SEC]<--SEC_SUBMIT: err[%d], pTask[0x%p], THR[%d], tgid[%d:%d],",
  656. status, pTask, thread, tgid, pid);
  657. cmdqCoreLongString(false, longMsg, &msgOffset, &msgMAXSize,
  658. " config_duration_ms[%d], cmdId[%d]\n", duration, iwcCommand);
  659. if (msgOffset > 0) {
  660. /* print message */
  661. CMDQ_LOG("%s", longMsg);
  662. }
  663. }
  664. return status;
  665. }
  666. int32_t cmdq_sec_init_allocate_resource_thread(void *data)
  667. {
  668. int32_t status = 0;
  669. cmdq_sec_lock_secure_path();
  670. gCmdqSecContextHandle = NULL;
  671. status = cmdq_sec_allocate_path_resource_unlocked(false);
  672. cmdq_sec_unlock_secure_path();
  673. return status;
  674. }
  675. /********************************************************************************
  676. * sectrace
  677. *******************************************************************************/
  678. int32_t cmdq_sec_fill_iwc_command_sectrace_unlocked(int32_t iwcCommand, void *_pTask,
  679. int32_t thread, void *_pIwc)
  680. {
  681. iwcCmdqMessage_t *pIwc;
  682. pIwc = (iwcCmdqMessage_t *) _pIwc;
  683. /* specify command id only, don't care other other */
  684. memset(pIwc, 0x0, sizeof(iwcCmdqMessage_t));
  685. pIwc->cmd = iwcCommand;
  686. switch (iwcCommand) {
  687. case CMD_CMDQ_TL_SECTRACE_MAP:
  688. pIwc->sectracBuffer.addr = (uint32_t) (gCmdqSectraceMappedInfo.secure_virt_addr);
  689. pIwc->sectracBuffer.size = (gCmdqSectraceMappedInfo.secure_virt_len);
  690. break;
  691. case CMD_CMDQ_TL_SECTRACE_UNMAP:
  692. case CMD_CMDQ_TL_SECTRACE_TRANSACT:
  693. default:
  694. pIwc->sectracBuffer.addr = 0;
  695. pIwc->sectracBuffer.size = 0;
  696. break;
  697. }
  698. /* medatada: debug config */
  699. pIwc->debug.logLevel = (cmdq_core_should_print_msg()) ? (1) : (0);
  700. pIwc->debug.enableProfile = cmdq_core_profile_enabled();
  701. CMDQ_LOG("[sectrace]SESSION_MSG: iwcCommand:%d, msg(sectraceBuffer, addr:0x%x, size:%d)\n",
  702. iwcCommand, pIwc->sectracBuffer.addr, pIwc->sectracBuffer.size);
  703. return 0;
  704. }
  705. #ifdef CMDQ_SECTRACE_SUPPORT
  706. static int cmdq_sec_sectrace_map(void *va, size_t size)
  707. {
  708. int status;
  709. enum mc_result mcRet;
  710. CMDQ_LOG("[sectrace]-->map: start, va:%p, size:%d\n", va, (int)size);
  711. status = 0;
  712. cmdq_sec_lock_secure_path();
  713. do {
  714. /* HACK: submit a dummy message to ensure secure path init done */
  715. status =
  716. cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_TEST_HELLO_TL, NULL,
  717. CMDQ_INVALID_THREAD, NULL, NULL,
  718. true);
  719. /* map log buffer in NWd */
  720. mcRet = mc_map(&(gCmdqSecContextHandle->sessionHandle),
  721. va, (uint32_t) size, &gCmdqSectraceMappedInfo);
  722. if (MC_DRV_OK != mcRet) {
  723. CMDQ_ERR("[sectrace]map: failed in NWd, mc_map err: 0x%x\n", mcRet);
  724. status = -EFAULT;
  725. break;
  726. }
  727. CMDQ_LOG
  728. ("[sectrace]map: mc_map sectrace buffer done, gCmdqSectraceMappedInfo(va:0x%08x, size:%d)\n",
  729. gCmdqSectraceMappedInfo.secure_virt_addr,
  730. gCmdqSectraceMappedInfo.secure_virt_len);
  731. /* ask secure CMDQ to map sectrace log buffer */
  732. status = cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_SECTRACE_MAP,
  733. NULL, CMDQ_INVALID_THREAD,
  734. cmdq_sec_fill_iwc_command_sectrace_unlocked,
  735. NULL, true);
  736. if (0 > status) {
  737. CMDQ_ERR("[sectrace]map: failed in SWd: %d\n", status);
  738. mc_unmap(&(gCmdqSecContextHandle->sessionHandle), va,
  739. &gCmdqSectraceMappedInfo);
  740. status = -EFAULT;
  741. break;
  742. }
  743. } while (0);
  744. cmdq_sec_unlock_secure_path();
  745. CMDQ_LOG("[sectrace]<--map: status: %d\n", status);
  746. return status;
  747. }
  748. static int cmdq_sec_sectrace_unmap(void *va, size_t size)
  749. {
  750. int status;
  751. enum mc_result mcRet;
  752. status = 0;
  753. cmdq_sec_lock_secure_path();
  754. do {
  755. if (NULL == gCmdqSecContextHandle) {
  756. status = -EFAULT;
  757. break;
  758. }
  759. /* ask secure CMDQ to unmap sectrace log buffer */
  760. status = cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_SECTRACE_UNMAP,
  761. NULL, CMDQ_INVALID_THREAD,
  762. cmdq_sec_fill_iwc_command_sectrace_unlocked,
  763. NULL, true);
  764. if (0 > status) {
  765. CMDQ_ERR("[sectrace]unmap: failed in SWd: %d\n", status);
  766. mc_unmap(&(gCmdqSecContextHandle->sessionHandle), va,
  767. &gCmdqSectraceMappedInfo);
  768. status = -EFAULT;
  769. break;
  770. }
  771. mcRet =
  772. mc_unmap(&(gCmdqSecContextHandle->sessionHandle), va, &gCmdqSectraceMappedInfo);
  773. } while (0);
  774. cmdq_sec_unlock_secure_path();
  775. CMDQ_LOG("[sectrace]unmap: status: %d\n", status);
  776. return status;
  777. }
  778. static int cmdq_sec_sectrace_transact(void)
  779. {
  780. int status;
  781. CMDQ_LOG("[sectrace]-->transact\n");
  782. status = cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_SECTRACE_TRANSACT,
  783. NULL, CMDQ_INVALID_THREAD,
  784. cmdq_sec_fill_iwc_command_sectrace_unlocked,
  785. NULL, true);
  786. CMDQ_LOG("[sectrace]<--transact: status: %d\n", status);
  787. return status;
  788. }
  789. #endif
  790. int32_t cmdq_sec_sectrace_init(void)
  791. {
  792. #ifdef CMDQ_SECTRACE_SUPPORT
  793. int32_t status;
  794. const uint32_t CMDQ_SECTRACE_BUFFER_SIZE_KB = 64;
  795. union callback_func sectraceCB;
  796. /* use callback_tl_function because CMDQ use "TCI" for inter-world communication */
  797. sectraceCB.tl.map = cmdq_sec_sectrace_map;
  798. sectraceCB.tl.unmap = cmdq_sec_sectrace_unmap;
  799. sectraceCB.tl.transact = cmdq_sec_sectrace_transact;
  800. /* create sectrace entry in debug FS */
  801. status = init_sectrace("CMDQ_SEC", if_tci, /* use TCI for inter world communication */
  802. usage_tl_dr, /* print sectrace log for tl and driver */
  803. CMDQ_SECTRACE_BUFFER_SIZE_KB, &sectraceCB);
  804. CMDQ_LOG("cmdq_sec_trace_init, status:%d\n", status);
  805. return 0;
  806. #else
  807. return 0;
  808. #endif
  809. }
  810. int32_t cmdq_sec_sectrace_deinit(void)
  811. {
  812. #ifdef CMDQ_SECTRACE_SUPPORT
  813. /* destroy sectrace entry in debug FS */
  814. deinit_sectrace("CMDQ_SEC");
  815. return 0;
  816. #else
  817. return 0;
  818. #endif
  819. }
  820. #endif /* CMDQ_SECURE_PATH_SUPPORT */
  821. /********************************************************************************
  822. * common part: for general projects
  823. *******************************************************************************/
  824. int32_t cmdq_sec_create_shared_memory(cmdqSecSharedMemoryHandle *pHandle, const uint32_t size)
  825. {
  826. cmdqSecSharedMemoryHandle handle = NULL;
  827. void *pVA = NULL;
  828. dma_addr_t PA = 0;
  829. handle = kzalloc(sizeof(uint8_t *) * sizeof(cmdqSecSharedMemoryStruct), GFP_KERNEL);
  830. if (NULL == handle)
  831. return -ENOMEM;
  832. CMDQ_LOG("%s\n", __func__);
  833. /* allocate non-cachable memory */
  834. pVA = cmdq_core_alloc_hw_buffer(cmdq_dev_get(), size, &PA, GFP_KERNEL);
  835. CMDQ_MSG("%s, MVA:%pa, pVA:%p, size:%d\n", __func__, &PA, pVA, size);
  836. if (NULL == pVA) {
  837. kfree(handle);
  838. return -ENOMEM;
  839. }
  840. /* update memory information */
  841. handle->size = size;
  842. handle->pVABase = pVA;
  843. handle->MVABase = PA;
  844. *pHandle = handle;
  845. return 0;
  846. }
  847. int32_t cmdq_sec_destroy_shared_memory(cmdqSecSharedMemoryHandle handle)
  848. {
  849. if (handle && handle->pVABase) {
  850. cmdq_core_free_hw_buffer(cmdq_dev_get(), handle->size,
  851. handle->pVABase, handle->MVABase);
  852. }
  853. kfree(handle);
  854. handle = NULL;
  855. return 0;
  856. }
  857. int32_t cmdq_sec_exec_task_async_unlocked(TaskStruct *pTask, int32_t thread)
  858. {
  859. #ifdef CMDQ_SECURE_PATH_SUPPORT
  860. int32_t status = 0;
  861. status =
  862. cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_SUBMIT_TASK, pTask, thread,
  863. NULL, NULL, true);
  864. if (0 > status) {
  865. /* Error status print */
  866. CMDQ_ERR("%s[%d]\n", __func__, status);
  867. }
  868. return status;
  869. #else
  870. CMDQ_ERR("secure path not support\n");
  871. return -EFAULT;
  872. #endif
  873. }
  874. int32_t cmdq_sec_cancel_error_task_unlocked(TaskStruct *pTask, int32_t thread,
  875. cmdqSecCancelTaskResultStruct *pResult)
  876. {
  877. #ifdef CMDQ_SECURE_PATH_SUPPORT
  878. int32_t status = 0;
  879. if ((NULL == pTask) || (false == cmdq_get_func()->isSecureThread(thread))
  880. || (NULL == pResult)) {
  881. CMDQ_ERR("%s invalid param, pTask:%p, thread:%d, pResult:%p\n",
  882. __func__, pTask, thread, pResult);
  883. return -EFAULT;
  884. }
  885. status = cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_CANCEL_TASK,
  886. pTask, thread, NULL,
  887. (void *)pResult, true);
  888. return status;
  889. #else
  890. CMDQ_ERR("secure path not support\n");
  891. return -EFAULT;
  892. #endif
  893. }
  894. #ifdef CMDQ_SECURE_PATH_SUPPORT
  895. static atomic_t gCmdqSecPathResource = ATOMIC_INIT(0);
  896. #endif
  897. int32_t cmdq_sec_allocate_path_resource_unlocked(bool throwAEE)
  898. {
  899. #ifdef CMDQ_SECURE_PATH_SUPPORT
  900. int32_t status = 0;
  901. if (1 == atomic_read(&gCmdqSecPathResource)) {
  902. /* has allocated successfully */
  903. return status;
  904. }
  905. status =
  906. cmdq_sec_submit_to_secure_world_async_unlocked(CMD_CMDQ_TL_PATH_RES_ALLOCATE, NULL,
  907. CMDQ_INVALID_THREAD, NULL, NULL,
  908. throwAEE);
  909. if (0 > status) {
  910. /* Error status print */
  911. CMDQ_ERR("%s[%d]\n", __func__, status);
  912. } else {
  913. /* Set resource successfully */
  914. atomic_set(&gCmdqSecPathResource, 1);
  915. }
  916. return status;
  917. #else
  918. CMDQ_ERR("secure path not support\n");
  919. return -EFAULT;
  920. #endif
  921. }
  922. /********************************************************************************
  923. * common part: SecContextHandle handle
  924. *******************************************************************************/
  925. cmdqSecContextHandle cmdq_sec_context_handle_create(uint32_t tgid)
  926. {
  927. cmdqSecContextHandle handle = NULL;
  928. handle = kmalloc(sizeof(uint8_t *) * sizeof(cmdqSecContextStruct), GFP_ATOMIC);
  929. if (handle) {
  930. handle->state = IWC_INIT;
  931. handle->iwcMessage = NULL;
  932. handle->tgid = tgid;
  933. handle->referCount = 0;
  934. handle->openMobicoreByOther = 0;
  935. } else {
  936. CMDQ_ERR("SecCtxHandle_CREATE: err[LOW_MEM], tgid[%d]\n", tgid);
  937. }
  938. CMDQ_MSG("SecCtxHandle_CREATE: create new, H[0x%p], tgid[%d]\n", handle, tgid);
  939. return handle;
  940. }
  941. /********************************************************************************
  942. * common part: init, deinit, path
  943. *******************************************************************************/
  944. void cmdq_sec_lock_secure_path(void)
  945. {
  946. mutex_lock(&gCmdqSecExecLock);
  947. /* set memory barrier for lock */
  948. smp_mb();
  949. }
  950. void cmdq_sec_unlock_secure_path(void)
  951. {
  952. mutex_unlock(&gCmdqSecExecLock);
  953. }
  954. void cmdqSecDeInitialize(void)
  955. {
  956. }
  957. void cmdqSecInitialize(void)
  958. {
  959. INIT_LIST_HEAD(&gCmdqSecContextList);
  960. }
  961. void cmdqSecEnableProfile(const bool enable)
  962. {
  963. #ifdef CMDQ_SECURE_PATH_SUPPORT
  964. CMDQ_LOG("[sectrace]enable profile %d\n", enable);
  965. mutex_lock(&gCmdqSecProfileLock);
  966. if (enable)
  967. cmdq_sec_sectrace_init();
  968. else
  969. cmdq_sec_sectrace_deinit();
  970. mutex_unlock(&gCmdqSecProfileLock);
  971. #endif
  972. }