cmdq_record.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. #include <linux/slab.h>
  2. #include <linux/errno.h>
  3. #include <linux/memory.h>
  4. #include "cmdq_record.h"
  5. #include "cmdq_core.h"
  6. #include "cmdq_virtual.h"
  7. #include "cmdq_reg.h"
  8. #include "cmdq_prof.h"
  9. #ifdef CMDQ_SECURE_PATH_SUPPORT
  10. #include "cmdq_sec_iwc_common.h"
  11. #endif
  12. #ifdef _MTK_USER_
  13. #define DISABLE_LOOP_IRQ
  14. #endif
  15. int32_t cmdq_rec_realloc_addr_metadata_buffer(cmdqRecHandle handle, const uint32_t size)
  16. {
  17. void *pNewBuf = NULL;
  18. void *pOriginalBuf = (void *)CMDQ_U32_PTR(handle->secData.addrMetadatas);
  19. const uint32_t originalSize =
  20. sizeof(cmdqSecAddrMetadataStruct) * (handle->secData.addrMetadataMaxCount);
  21. if (size <= originalSize)
  22. return 0;
  23. pNewBuf = kzalloc(size, GFP_KERNEL);
  24. if (NULL == pNewBuf) {
  25. CMDQ_ERR("REC: secAddrMetadata, kzalloc %d bytes addr_metadata buffer failed\n",
  26. size);
  27. return -ENOMEM;
  28. }
  29. if (pOriginalBuf && originalSize > 0)
  30. memcpy(pNewBuf, pOriginalBuf, originalSize);
  31. CMDQ_VERBOSE("REC: secAddrMetadata, realloc size from %d to %d bytes\n", originalSize,
  32. size);
  33. kfree(pOriginalBuf);
  34. handle->secData.addrMetadatas = (cmdqU32Ptr_t) (unsigned long)(pNewBuf);
  35. handle->secData.addrMetadataMaxCount = size / sizeof(cmdqSecAddrMetadataStruct);
  36. return 0;
  37. }
  38. int cmdq_rec_realloc_cmd_buffer(cmdqRecHandle handle, uint32_t size)
  39. {
  40. void *pNewBuf = NULL;
  41. if (size <= handle->bufferSize)
  42. return 0;
  43. pNewBuf = kzalloc(size, GFP_KERNEL);
  44. if (NULL == pNewBuf) {
  45. CMDQ_ERR("REC: kzalloc %d bytes cmd_buffer failed\n", size);
  46. return -ENOMEM;
  47. }
  48. memset(pNewBuf, 0, size);
  49. if (handle->pBuffer && handle->blockSize > 0)
  50. memcpy(pNewBuf, handle->pBuffer, handle->blockSize);
  51. CMDQ_VERBOSE("REC: realloc size from %d to %d bytes\n", handle->bufferSize, size);
  52. kfree(handle->pBuffer);
  53. handle->pBuffer = pNewBuf;
  54. handle->bufferSize = size;
  55. return 0;
  56. }
  57. static int32_t cmdq_rec_reset_profile_maker_data(cmdqRecHandle handle)
  58. {
  59. #ifdef CMDQ_PROFILE_MARKER_SUPPORT
  60. int32_t i = 0;
  61. if (NULL == handle)
  62. return -EFAULT;
  63. handle->profileMarker.count = 0;
  64. handle->profileMarker.hSlot = 0LL;
  65. for (i = 0; i < CMDQ_MAX_PROFILE_MARKER_IN_TASK; i++)
  66. handle->profileMarker.tag[i] = (cmdqU32Ptr_t) (unsigned long)(NULL);
  67. return 0;
  68. #endif
  69. return 0;
  70. }
  71. int32_t cmdqRecCreate(CMDQ_SCENARIO_ENUM scenario, cmdqRecHandle *pHandle)
  72. {
  73. cmdqRecHandle handle = NULL;
  74. if (scenario < 0 || scenario >= CMDQ_MAX_SCENARIO_COUNT) {
  75. CMDQ_ERR("Unknown scenario type %d\n", scenario);
  76. return -EINVAL;
  77. }
  78. handle = kzalloc(sizeof(uint8_t *) * sizeof(cmdqRecStruct), GFP_KERNEL);
  79. if (NULL == handle)
  80. return -ENOMEM;
  81. handle->scenario = scenario;
  82. handle->pBuffer = NULL;
  83. handle->bufferSize = 0;
  84. handle->blockSize = 0;
  85. handle->engineFlag = cmdq_get_func()->flagFromScenario(scenario);
  86. handle->priority = CMDQ_THR_PRIO_NORMAL;
  87. handle->prefetchCount = 0;
  88. handle->finalized = false;
  89. handle->pRunningTask = NULL;
  90. /* secure path */
  91. handle->secData.isSecure = false;
  92. handle->secData.enginesNeedDAPC = 0LL;
  93. handle->secData.enginesNeedPortSecurity = 0LL;
  94. handle->secData.addrMetadatas = (cmdqU32Ptr_t) (unsigned long)NULL;
  95. handle->secData.addrMetadataMaxCount = 0;
  96. handle->secData.addrMetadataCount = 0;
  97. /* profile marker */
  98. cmdq_rec_reset_profile_maker_data(handle);
  99. /* CMD */
  100. if (0 != cmdq_rec_realloc_cmd_buffer(handle, CMDQ_INITIAL_CMD_BLOCK_SIZE)) {
  101. kfree(handle);
  102. return -ENOMEM;
  103. }
  104. *pHandle = handle;
  105. return 0;
  106. }
  107. #ifdef CMDQ_SECURE_PATH_SUPPORT
  108. int32_t cmdq_append_addr_metadata(cmdqRecHandle handle, const cmdqSecAddrMetadataStruct *pMetadata)
  109. {
  110. cmdqSecAddrMetadataStruct *pAddrs;
  111. int32_t status;
  112. uint32_t size;
  113. /* element index of the New appended addr metadat */
  114. const uint32_t index = handle->secData.addrMetadataCount;
  115. pAddrs = NULL;
  116. status = 0;
  117. if (0 >= handle->secData.addrMetadataMaxCount) {
  118. /* not init yet, initialize to allow max 8 addr metadata */
  119. size = sizeof(cmdqSecAddrMetadataStruct) * 8;
  120. status = cmdq_rec_realloc_addr_metadata_buffer(handle, size);
  121. } else if (handle->secData.addrMetadataCount >= (handle->secData.addrMetadataMaxCount)) {
  122. /* enlarge metadata buffer to twice as */
  123. size =
  124. sizeof(cmdqSecAddrMetadataStruct) * (handle->secData.addrMetadataMaxCount) * 2;
  125. status = cmdq_rec_realloc_addr_metadata_buffer(handle, size);
  126. }
  127. if (0 > status)
  128. return -ENOMEM;
  129. if (handle->secData.addrMetadataCount >= CMDQ_IWC_MAX_ADDR_LIST_LENGTH) {
  130. uint32_t maxMetaDataCount = CMDQ_IWC_MAX_ADDR_LIST_LENGTH;
  131. CMDQ_ERR("Metadata idx = %d reach the max allowed number = %d.\n",
  132. handle->secData.addrMetadataCount, maxMetaDataCount);
  133. CMDQ_MSG("ADDR: type:%d, baseHandle:%x, offset:%d, size:%d, port:%d\n",
  134. pMetadata->type, pMetadata->baseHandle, pMetadata->offset, pMetadata->size,
  135. pMetadata->port);
  136. status = -EFAULT;
  137. } else {
  138. pAddrs =
  139. (cmdqSecAddrMetadataStruct *) (CMDQ_U32_PTR(handle->secData.addrMetadatas));
  140. /* append meatadata */
  141. pAddrs[index].instrIndex = pMetadata->instrIndex;
  142. pAddrs[index].baseHandle = pMetadata->baseHandle;
  143. pAddrs[index].offset = pMetadata->offset;
  144. pAddrs[index].size = pMetadata->size;
  145. pAddrs[index].port = pMetadata->port;
  146. pAddrs[index].type = pMetadata->type;
  147. /* meatadata count ++ */
  148. handle->secData.addrMetadataCount += 1;
  149. }
  150. return status;
  151. }
  152. #endif
  153. /**
  154. * centralize the write/polling/read command for APB and GPR handle
  155. * this function must be called inside cmdq_append_command
  156. * because we ignore buffer and pre-fetch check here.
  157. * Parameter:
  158. * same as cmdq_append_command
  159. * Return:
  160. * same as cmdq_append_command
  161. */
  162. static int32_t cmdq_append_wpr_command(cmdqRecHandle handle, CMDQ_CODE_ENUM code,
  163. uint32_t argA, uint32_t argB, uint32_t argAType,
  164. uint32_t argBType)
  165. {
  166. int32_t status = 0;
  167. int32_t subsys;
  168. uint32_t *pCommand;
  169. bool bUseGPR = false;
  170. /* use new argA to present final inserted argA */
  171. uint32_t newArgA;
  172. uint32_t newArgAType = argAType;
  173. uint32_t argType = 0;
  174. /* be careful that subsys encoding position is different among platforms */
  175. const uint32_t subsysBit = cmdq_get_func()->getSubsysLSBArgA();
  176. pCommand = (uint32_t *) ((uint8_t *) handle->pBuffer + handle->blockSize);
  177. if (CMDQ_CODE_READ != code && CMDQ_CODE_WRITE != code && CMDQ_CODE_POLL != code) {
  178. CMDQ_ERR("Record 0x%p, flow error, should not append comment in wpr API", handle);
  179. return -EFAULT;
  180. }
  181. /* we must re-calculate current PC at first. */
  182. pCommand = (uint32_t *) ((uint8_t *) handle->pBuffer + handle->blockSize);
  183. CMDQ_VERBOSE("REC: 0x%p CMD: 0x%p, op: 0x%02x\n", handle, pCommand, code);
  184. CMDQ_VERBOSE("REC: 0x%p CMD: argA: 0x%08x, argB: 0x%08x, argAType: %d, argBType: %d\n",
  185. handle, argA, argB, argAType, argBType);
  186. if (0 == argAType) {
  187. /* argA is the HW register address to read from */
  188. subsys = cmdq_core_subsys_from_phys_addr(argA);
  189. if (CMDQ_SPECIAL_SUBSYS_ADDR == subsys) {
  190. #ifdef CMDQ_GPR_SUPPORT
  191. bUseGPR = true;
  192. CMDQ_MSG("REC: Special handle memory base address 0x%08x\n", argA);
  193. /* Wait and clear for GPR mutex token to enter mutex */
  194. *pCommand++ = ((1 << 31) | (1 << 15) | 1);
  195. *pCommand++ = (CMDQ_CODE_WFE << 24) | CMDQ_SYNC_TOKEN_GPR_SET_4;
  196. handle->blockSize += CMDQ_INST_SIZE;
  197. /* Move extra handle APB address to GPR */
  198. *pCommand++ = argA;
  199. *pCommand++ = (CMDQ_CODE_MOVE << 24) |
  200. ((CMDQ_DATA_REG_DEBUG & 0x1f) << 16) | (4 << 21);
  201. handle->blockSize += CMDQ_INST_SIZE;
  202. /* change final argA to GPR */
  203. newArgA = ((CMDQ_DATA_REG_DEBUG & 0x1f) << 16);
  204. if (argA & 0x1) {
  205. /* MASK case, set final bit to 1 */
  206. newArgA = newArgA | 0x1;
  207. }
  208. /* change argA type to 1 */
  209. newArgAType = 1;
  210. #else
  211. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  212. status = -EFAULT;
  213. #endif
  214. } else if (0 == argAType && 0 > subsys) {
  215. CMDQ_ERR("REC: Unsupported memory base address 0x%08x\n", argA);
  216. status = -EFAULT;
  217. } else {
  218. /* compose final argA according to subsys table */
  219. newArgA = (argA & 0xffff) | ((subsys & 0x1f) << subsysBit);
  220. }
  221. } else {
  222. /* compose final argA according GPR value */
  223. newArgA = ((argA & 0x1f) << 16);
  224. }
  225. if (status < 0)
  226. return status;
  227. argType = (newArgAType << 2) | (argBType << 1);
  228. /* newArgA is the HW register address to access from or GPR value store the HW register address */
  229. /* argB is the value or register id */
  230. /* bit 55: argA type, 1 for GPR */
  231. /* bit 54: argB type, 1 for GPR */
  232. /* argType: ('newArgAType', 'argBType', '0') */
  233. *pCommand++ = argB;
  234. *pCommand++ = (code << 24) | newArgA | (argType << 21);
  235. handle->blockSize += CMDQ_INST_SIZE;
  236. if (bUseGPR) {
  237. /* Set for GPR mutex token to leave mutex */
  238. *pCommand++ = ((1 << 31) | (1 << 16));
  239. *pCommand++ = (CMDQ_CODE_WFE << 24) | CMDQ_SYNC_TOKEN_GPR_SET_4;
  240. handle->blockSize += CMDQ_INST_SIZE;
  241. }
  242. return 0;
  243. }
  244. int32_t cmdq_append_command(cmdqRecHandle handle, CMDQ_CODE_ENUM code,
  245. uint32_t argA, uint32_t argB, uint32_t argAType, uint32_t argBType)
  246. {
  247. uint32_t *pCommand;
  248. if (NULL == handle)
  249. return -EFAULT;
  250. pCommand = (uint32_t *) ((uint8_t *) handle->pBuffer + handle->blockSize);
  251. if (handle->finalized) {
  252. CMDQ_ERR("Finalized record 0x%p (scenario:%d)\n", handle, handle->scenario);
  253. CMDQ_ERR(" cannot add command (op: 0x%02x, argA: 0x%08x, argB: 0x%08x)\n",
  254. code, argA, argB);
  255. return -EBUSY;
  256. }
  257. /* check if we have sufficient buffer size */
  258. /* we leave a 4 instruction (4 bytes each) margin. */
  259. if ((handle->blockSize + 32) >= handle->bufferSize) {
  260. if (0 != cmdq_rec_realloc_cmd_buffer(handle, handle->bufferSize * 2))
  261. return -ENOMEM;
  262. }
  263. /* force insert MARKER if prefetch memory is full */
  264. /* GCE deadlocks if we don't do so */
  265. if (CMDQ_CODE_EOC != code && cmdq_get_func()->shouldEnablePrefetch(handle->scenario)) {
  266. uint32_t prefetchSize = 0;
  267. int32_t threadNo = cmdq_get_func()->getThreadID(handle->scenario, handle->secData.isSecure);
  268. prefetchSize = cmdq_core_thread_prefetch_size(threadNo);
  269. if (prefetchSize > 0 && handle->prefetchCount >= prefetchSize) {
  270. CMDQ_MSG
  271. ("prefetchCount(%d) > %d, force insert disable prefetch marker\n",
  272. handle->prefetchCount, prefetchSize);
  273. /* Mark END of prefetch section */
  274. cmdqRecDisablePrefetch(handle);
  275. /* BEGING of next prefetch section */
  276. cmdqRecMark(handle);
  277. } else {
  278. /* prefetch enabled marker exist */
  279. if (1 <= handle->prefetchCount) {
  280. ++handle->prefetchCount;
  281. CMDQ_VERBOSE("handle->prefetchCount: %d, %s, %d\n",
  282. handle->prefetchCount, __func__, __LINE__);
  283. }
  284. }
  285. }
  286. /* we must re-calculate current PC because we may already insert MARKER inst. */
  287. pCommand = (uint32_t *) ((uint8_t *) handle->pBuffer + handle->blockSize);
  288. CMDQ_VERBOSE("REC: 0x%p CMD: 0x%p, op: 0x%02x, argA: 0x%08x, argB: 0x%08x\n", handle,
  289. pCommand, code, argA, argB);
  290. if (CMDQ_CODE_READ == code || CMDQ_CODE_WRITE == code || CMDQ_CODE_POLL == code) {
  291. /* Because read/write/poll have similar format, handle them together */
  292. return cmdq_append_wpr_command(handle, code, argA, argB, argAType, argBType);
  293. }
  294. switch (code) {
  295. case CMDQ_CODE_MOVE:
  296. *pCommand++ = argB;
  297. *pCommand++ = CMDQ_CODE_MOVE << 24 | (argA & 0xffffff);
  298. break;
  299. case CMDQ_CODE_JUMP:
  300. *pCommand++ = argB;
  301. *pCommand++ = (CMDQ_CODE_JUMP << 24) | (argA & 0x0FFFFFF);
  302. break;
  303. case CMDQ_CODE_WFE:
  304. /* bit 0-11: wait_value, 1 */
  305. /* bit 15: to_wait, true */
  306. /* bit 31: to_update, true */
  307. /* bit 16-27: update_value, 0 */
  308. *pCommand++ = ((1 << 31) | (1 << 15) | 1);
  309. *pCommand++ = (CMDQ_CODE_WFE << 24) | argA;
  310. break;
  311. case CMDQ_CODE_SET_TOKEN:
  312. /* this is actually WFE(SYNC) but with different parameter */
  313. /* interpretation */
  314. /* bit 15: to_wait, false */
  315. /* bit 31: to_update, true */
  316. /* bit 16-27: update_value, 1 */
  317. *pCommand++ = ((1 << 31) | (1 << 16));
  318. *pCommand++ = (CMDQ_CODE_WFE << 24) | argA;
  319. break;
  320. case CMDQ_CODE_WAIT_NO_CLEAR:
  321. /* bit 0-11: wait_value, 1 */
  322. /* bit 15: to_wait, true */
  323. /* bit 31: to_update, false */
  324. *pCommand++ = ((0 << 31) | (1 << 15) | 1);
  325. *pCommand++ = (CMDQ_CODE_WFE << 24) | argA;
  326. break;
  327. case CMDQ_CODE_CLEAR_TOKEN:
  328. /* this is actually WFE(SYNC) but with different parameter */
  329. /* interpretation */
  330. /* bit 15: to_wait, false */
  331. /* bit 31: to_update, true */
  332. /* bit 16-27: update_value, 0 */
  333. *pCommand++ = ((1 << 31) | (0 << 16));
  334. *pCommand++ = (CMDQ_CODE_WFE << 24) | argA;
  335. break;
  336. case CMDQ_CODE_EOC:
  337. *pCommand++ = argB;
  338. *pCommand++ = (CMDQ_CODE_EOC << 24) | (argA & 0x0FFFFFF);
  339. break;
  340. case CMDQ_CODE_RAW:
  341. *pCommand++ = argB;
  342. *pCommand++ = argA;
  343. break;
  344. default:
  345. return -EFAULT;
  346. }
  347. handle->blockSize += CMDQ_INST_SIZE;
  348. return 0;
  349. }
  350. int32_t cmdqRecSetEngine(cmdqRecHandle handle, uint64_t engineFlag)
  351. {
  352. if (NULL == handle)
  353. return -EFAULT;
  354. CMDQ_VERBOSE("REC: %p, engineFlag: 0x%llx\n", handle, engineFlag);
  355. handle->engineFlag = engineFlag;
  356. return 0;
  357. }
  358. int32_t cmdqRecReset(cmdqRecHandle handle)
  359. {
  360. if (NULL == handle)
  361. return -EFAULT;
  362. if (NULL != handle->pRunningTask)
  363. cmdqRecStopLoop(handle);
  364. handle->blockSize = 0;
  365. handle->prefetchCount = 0;
  366. handle->finalized = false;
  367. /* reset secure path data */
  368. handle->secData.isSecure = false;
  369. handle->secData.enginesNeedDAPC = 0LL;
  370. handle->secData.enginesNeedPortSecurity = 0LL;
  371. if (handle->secData.addrMetadatas) {
  372. kfree(CMDQ_U32_PTR(handle->secData.addrMetadatas));
  373. handle->secData.addrMetadatas = (cmdqU32Ptr_t) (unsigned long)NULL;
  374. handle->secData.addrMetadataMaxCount = 0;
  375. handle->secData.addrMetadataCount = 0;
  376. }
  377. /* profile marker */
  378. cmdq_rec_reset_profile_maker_data(handle);
  379. return 0;
  380. }
  381. int32_t cmdqRecSetSecure(cmdqRecHandle handle, const bool isSecure)
  382. {
  383. if (NULL == handle)
  384. return -EFAULT;
  385. if (false == isSecure) {
  386. handle->secData.isSecure = isSecure;
  387. return 0;
  388. }
  389. #ifdef CMDQ_SECURE_PATH_SUPPORT
  390. CMDQ_VERBOSE("REC: %p secure:%d\n", handle, isSecure);
  391. handle->secData.isSecure = isSecure;
  392. return 0;
  393. #else
  394. CMDQ_ERR("%s failed since not support secure path\n", __func__);
  395. return -EFAULT;
  396. #endif
  397. }
  398. int32_t cmdqRecIsSecure(cmdqRecHandle handle)
  399. {
  400. if (NULL == handle)
  401. return -EFAULT;
  402. return handle->secData.isSecure;
  403. }
  404. int32_t cmdqRecSecureEnableDAPC(cmdqRecHandle handle, const uint64_t engineFlag)
  405. {
  406. #ifdef CMDQ_SECURE_PATH_SUPPORT
  407. if (NULL == handle)
  408. return -EFAULT;
  409. handle->secData.enginesNeedDAPC |= engineFlag;
  410. return 0;
  411. #else
  412. CMDQ_ERR("%s failed since not support secure path\n", __func__);
  413. return -EFAULT;
  414. #endif
  415. }
  416. int32_t cmdqRecSecureEnablePortSecurity(cmdqRecHandle handle, const uint64_t engineFlag)
  417. {
  418. #ifdef CMDQ_SECURE_PATH_SUPPORT
  419. if (NULL == handle)
  420. return -EFAULT;
  421. handle->secData.enginesNeedPortSecurity |= engineFlag;
  422. return 0;
  423. #else
  424. CMDQ_ERR("%s failed since not support secure path\n", __func__);
  425. return -EFAULT;
  426. #endif
  427. }
  428. int32_t cmdqRecMark(cmdqRecHandle handle)
  429. {
  430. int32_t status;
  431. /* Do not check prefetch-ability here. */
  432. /* because cmdqRecMark may have other purposes. */
  433. /* bit 53: non-suspendable. set to 1 because we don't want */
  434. /* CPU suspend this thread during pre-fetching. */
  435. /* If CPU change PC, then there will be a mess, */
  436. /* because prefetch buffer is not properly cleared. */
  437. /* bit 48: do not increase CMD_COUNTER (because this is not the end of the task) */
  438. /* bit 20: prefetch_marker */
  439. /* bit 17: prefetch_marker_en */
  440. /* bit 16: prefetch_en */
  441. /* bit 0: irq_en (set to 0 since we don't want EOC interrupt) */
  442. status = cmdq_append_command(handle,
  443. CMDQ_CODE_EOC,
  444. (0x1 << (53 - 32)) | (0x1 << (48 - 32)), 0x00130000, 0, 0);
  445. /* if we're in a prefetch region, */
  446. /* this ends the region so set count to 0. */
  447. /* otherwise we start the region by setting count to 1. */
  448. handle->prefetchCount = 1;
  449. if (0 != status)
  450. return -EFAULT;
  451. return 0;
  452. }
  453. int32_t cmdqRecWrite(cmdqRecHandle handle, uint32_t addr, uint32_t value, uint32_t mask)
  454. {
  455. int32_t status;
  456. if (0xFFFFFFFF != mask) {
  457. status = cmdq_append_command(handle, CMDQ_CODE_MOVE, 0, ~mask, 0, 0);
  458. if (0 != status)
  459. return status;
  460. addr = addr | 0x1;
  461. }
  462. status = cmdq_append_command(handle, CMDQ_CODE_WRITE, addr, value, 0, 0);
  463. if (0 != status)
  464. return status;
  465. return 0;
  466. }
  467. int32_t cmdqRecWriteSecure(cmdqRecHandle handle, uint32_t addr,
  468. CMDQ_SEC_ADDR_METADATA_TYPE type,
  469. uint32_t baseHandle, uint32_t offset, uint32_t size, uint32_t port)
  470. {
  471. #ifdef CMDQ_SECURE_PATH_SUPPORT
  472. int32_t status;
  473. int32_t writeInstrIndex;
  474. cmdqSecAddrMetadataStruct metadata;
  475. const uint32_t mask = 0xFFFFFFFF;
  476. /* append command */
  477. status = cmdqRecWrite(handle, addr, baseHandle, mask);
  478. if (0 != status)
  479. return status;
  480. /* append to metadata list */
  481. writeInstrIndex = (handle->blockSize) / CMDQ_INST_SIZE - 1; /* start from 0 */
  482. memset(&metadata, 0, sizeof(cmdqSecAddrMetadataStruct));
  483. metadata.instrIndex = writeInstrIndex;
  484. metadata.type = type;
  485. metadata.baseHandle = baseHandle;
  486. metadata.offset = offset;
  487. metadata.size = size;
  488. metadata.port = port;
  489. status = cmdq_append_addr_metadata(handle, &metadata);
  490. return 0;
  491. #else
  492. CMDQ_ERR("%s failed since not support secure path\n", __func__);
  493. return -EFAULT;
  494. #endif
  495. }
  496. int32_t cmdqRecPoll(cmdqRecHandle handle, uint32_t addr, uint32_t value, uint32_t mask)
  497. {
  498. int32_t status;
  499. status = cmdq_append_command(handle, CMDQ_CODE_MOVE, 0, ~mask, 0, 0);
  500. if (0 != status)
  501. return status;
  502. status = cmdq_append_command(handle, CMDQ_CODE_POLL, (addr | 0x1), value, 0, 0);
  503. if (0 != status)
  504. return status;
  505. return 0;
  506. }
  507. int32_t cmdqRecWait(cmdqRecHandle handle, CMDQ_EVENT_ENUM event)
  508. {
  509. uint32_t argA;
  510. if (CMDQ_SYNC_TOKEN_INVALID == event || CMDQ_SYNC_TOKEN_MAX <= event || 0 > event)
  511. return -EINVAL;
  512. argA = cmdq_core_get_event_value(event);
  513. return cmdq_append_command(handle, CMDQ_CODE_WFE, argA, 0, 0, 0);
  514. }
  515. int32_t cmdqRecWaitNoClear(cmdqRecHandle handle, CMDQ_EVENT_ENUM event)
  516. {
  517. uint32_t argA;
  518. if (CMDQ_SYNC_TOKEN_INVALID == event || CMDQ_SYNC_TOKEN_MAX <= event || 0 > event)
  519. return -EINVAL;
  520. argA = cmdq_core_get_event_value(event);
  521. return cmdq_append_command(handle, CMDQ_CODE_WAIT_NO_CLEAR, argA, 0, 0, 0);
  522. }
  523. int32_t cmdqRecClearEventToken(cmdqRecHandle handle, CMDQ_EVENT_ENUM event)
  524. {
  525. uint32_t argA;
  526. if (CMDQ_SYNC_TOKEN_INVALID == event || CMDQ_SYNC_TOKEN_MAX <= event || 0 > event)
  527. return -EINVAL;
  528. argA = cmdq_core_get_event_value(event);
  529. return cmdq_append_command(handle, CMDQ_CODE_CLEAR_TOKEN, argA, 1, /* actually this param is ignored. */
  530. 0, 0);
  531. }
  532. int32_t cmdqRecSetEventToken(cmdqRecHandle handle, CMDQ_EVENT_ENUM event)
  533. {
  534. uint32_t argA;
  535. if (CMDQ_SYNC_TOKEN_INVALID == event || CMDQ_SYNC_TOKEN_MAX <= event || 0 > event)
  536. return -EINVAL;
  537. argA = cmdq_core_get_event_value(event);
  538. return cmdq_append_command(handle, CMDQ_CODE_SET_TOKEN, argA, 1, /* actually this param is ignored. */
  539. 0, 0);
  540. }
  541. int32_t cmdqRecReadToDataRegister(cmdqRecHandle handle, uint32_t hwRegAddr,
  542. CMDQ_DATA_REGISTER_ENUM dstDataReg)
  543. {
  544. #ifdef CMDQ_GPR_SUPPORT
  545. /* read from hwRegAddr(argA) to dstDataReg(argB) */
  546. return cmdq_append_command(handle, CMDQ_CODE_READ, hwRegAddr, dstDataReg, 0, 1);
  547. #else
  548. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  549. return -EFAULT;
  550. #endif
  551. }
  552. int32_t cmdqRecWriteFromDataRegister(cmdqRecHandle handle,
  553. CMDQ_DATA_REGISTER_ENUM srcDataReg, uint32_t hwRegAddr)
  554. {
  555. #ifdef CMDQ_GPR_SUPPORT
  556. /* write HW register(argA) with data of GPR data register(argB) */
  557. return cmdq_append_command(handle, CMDQ_CODE_WRITE, hwRegAddr, srcDataReg, 0, 1);
  558. #else
  559. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  560. return -EFAULT;
  561. #endif /* CMDQ_GPR_SUPPORT */
  562. }
  563. /**
  564. * Allocate 32-bit register backup slot
  565. *
  566. */
  567. int32_t cmdqBackupAllocateSlot(cmdqBackupSlotHandle *phBackupSlot, uint32_t slotCount)
  568. {
  569. #ifdef CMDQ_GPR_SUPPORT
  570. dma_addr_t paStart = 0;
  571. int status = 0;
  572. if (NULL == phBackupSlot)
  573. return -EINVAL;
  574. status = cmdqCoreAllocWriteAddress(slotCount, &paStart);
  575. *phBackupSlot = paStart;
  576. return status;
  577. #else
  578. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  579. return -EFAULT;
  580. #endif /* CMDQ_GPR_SUPPORT */
  581. }
  582. /**
  583. * Read 32-bit register backup slot by index
  584. *
  585. */
  586. int32_t cmdqBackupReadSlot(cmdqBackupSlotHandle hBackupSlot, uint32_t slotIndex, uint32_t *value)
  587. {
  588. #ifdef CMDQ_GPR_SUPPORT
  589. if (NULL == value)
  590. return -EINVAL;
  591. if (0 == hBackupSlot) {
  592. CMDQ_ERR("%s, hBackupSlot is NULL\n", __func__);
  593. return -EINVAL;
  594. }
  595. *value = cmdqCoreReadWriteAddress(hBackupSlot + slotIndex * sizeof(uint32_t));
  596. return 0;
  597. #else
  598. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  599. return -EFAULT;
  600. #endif /* CMDQ_GPR_SUPPORT */
  601. }
  602. int32_t cmdqBackupWriteSlot(cmdqBackupSlotHandle hBackupSlot, uint32_t slotIndex, uint32_t value)
  603. {
  604. #ifdef CMDQ_GPR_SUPPORT
  605. int status = 0;
  606. /* set the slot value directly */
  607. status = cmdqCoreWriteWriteAddress(hBackupSlot + slotIndex * sizeof(uint32_t), value);
  608. return status;
  609. #else
  610. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  611. return -EFAULT;
  612. #endif /* CMDQ_GPR_SUPPORT */
  613. }
  614. /**
  615. * Free allocated backup slot. DO NOT free them before corresponding
  616. * task finishes. Becareful on AsyncFlush use cases.
  617. *
  618. */
  619. int32_t cmdqBackupFreeSlot(cmdqBackupSlotHandle hBackupSlot)
  620. {
  621. #ifdef CMDQ_GPR_SUPPORT
  622. return cmdqCoreFreeWriteAddress(hBackupSlot);
  623. #else
  624. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  625. return -EFAULT;
  626. #endif /* CMDQ_GPR_SUPPORT */
  627. }
  628. /**
  629. * Insert instructions to backup given 32-bit HW register
  630. * to a backup slot.
  631. * You can use cmdqBackupReadSlot() to retrieve the result
  632. * AFTER cmdqRecFlush() returns, or INSIDE the callback of cmdqRecFlushAsyncCallback().
  633. *
  634. */
  635. int32_t cmdqRecBackupRegisterToSlot(cmdqRecHandle handle,
  636. cmdqBackupSlotHandle hBackupSlot,
  637. uint32_t slotIndex, uint32_t regAddr)
  638. {
  639. #ifdef CMDQ_GPR_SUPPORT
  640. const CMDQ_DATA_REGISTER_ENUM valueRegId = CMDQ_DATA_REG_DEBUG;
  641. const CMDQ_DATA_REGISTER_ENUM destRegId = CMDQ_DATA_REG_DEBUG_DST;
  642. const CMDQ_EVENT_ENUM regAccessToken = CMDQ_SYNC_TOKEN_GPR_SET_4;
  643. const dma_addr_t dramAddr = hBackupSlot + slotIndex * sizeof(uint32_t);
  644. /* lock GPR because we may access it in multiple CMDQ HW threads */
  645. cmdqRecWait(handle, regAccessToken);
  646. /* Load into 32-bit GPR (R0-R15) */
  647. cmdq_append_command(handle, CMDQ_CODE_READ, regAddr, valueRegId, 0, 1);
  648. /* Note that <MOVE> argB is 48-bit */
  649. /* so writeAddress is split into 2 parts */
  650. /* and we store address in 64-bit GPR (P0-P7) */
  651. cmdq_append_command(handle, CMDQ_CODE_MOVE,
  652. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  653. ((dramAddr >> 32) & 0xffff) |
  654. #endif
  655. ((destRegId & 0x1f) << 16) | (4 << 21), (uint32_t) dramAddr, 0, 0);
  656. /* write value in GPR to memory pointed by GPR */
  657. cmdq_append_command(handle, CMDQ_CODE_WRITE, destRegId, valueRegId, 1, 1);
  658. /* release the GPR lock */
  659. cmdqRecSetEventToken(handle, regAccessToken);
  660. return 0;
  661. #else
  662. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  663. return -EFAULT;
  664. #endif /* CMDQ_GPR_SUPPORT */
  665. }
  666. int32_t cmdqRecBackupWriteRegisterFromSlot(cmdqRecHandle handle,
  667. cmdqBackupSlotHandle hBackupSlot,
  668. uint32_t slotIndex, uint32_t addr)
  669. {
  670. #ifdef CMDQ_GPR_SUPPORT
  671. const CMDQ_DATA_REGISTER_ENUM valueRegId = CMDQ_DATA_REG_DEBUG;
  672. const CMDQ_DATA_REGISTER_ENUM addrRegId = CMDQ_DATA_REG_DEBUG_DST;
  673. const CMDQ_EVENT_ENUM regAccessToken = CMDQ_SYNC_TOKEN_GPR_SET_4;
  674. const dma_addr_t dramAddr = hBackupSlot + slotIndex * sizeof(uint32_t);
  675. /* lock GPR because we may access it in multiple CMDQ HW threads */
  676. cmdqRecWait(handle, regAccessToken);
  677. /* 1. MOVE slot address to addr GPR */
  678. /* Note that <MOVE> argB is 48-bit */
  679. /* so writeAddress is split into 2 parts */
  680. /* and we store address in 64-bit GPR (P0-P7) */
  681. cmdq_append_command(handle, CMDQ_CODE_MOVE,
  682. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  683. ((dramAddr >> 32) & 0xffff) |
  684. #endif
  685. ((addrRegId & 0x1f) << 16) | (4 << 21), (uint32_t) dramAddr, 0, 0); /* argA is GPR */
  686. /* 2. read value from src address, which is stroed in GPR, to valueRegId */
  687. cmdq_append_command(handle, CMDQ_CODE_READ, addrRegId, valueRegId, 1, 1);
  688. /* 3. write from data register */
  689. cmdqRecWriteFromDataRegister(handle, valueRegId, addr);
  690. /* release the GPR lock */
  691. cmdqRecSetEventToken(handle, regAccessToken);
  692. return 0;
  693. #else
  694. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  695. return -EFAULT;
  696. #endif /* CMDQ_GPR_SUPPORT */
  697. }
  698. int32_t cmdqRecBackupUpdateSlot(cmdqRecHandle handle,
  699. cmdqBackupSlotHandle hBackupSlot,
  700. uint32_t slotIndex, uint32_t value)
  701. {
  702. #ifdef CMDQ_GPR_SUPPORT
  703. const CMDQ_DATA_REGISTER_ENUM valueRegId = CMDQ_DATA_REG_DEBUG;
  704. const CMDQ_DATA_REGISTER_ENUM destRegId = CMDQ_DATA_REG_DEBUG_DST;
  705. const CMDQ_EVENT_ENUM regAccessToken = CMDQ_SYNC_TOKEN_GPR_SET_4;
  706. const dma_addr_t dramAddr = hBackupSlot + slotIndex * sizeof(uint32_t);
  707. uint32_t argA;
  708. /* lock GPR because we may access it in multiple CMDQ HW threads */
  709. cmdqRecWait(handle, regAccessToken);
  710. /* Assign 32-bit GRP with value */
  711. argA = (CMDQ_CODE_MOVE << 24) | (valueRegId << 16) | (4 << 21); /* argA is GPR */
  712. cmdq_append_command(handle, CMDQ_CODE_RAW, argA, value, 0, 0);
  713. /* Note that <MOVE> argB is 48-bit */
  714. /* so writeAddress is split into 2 parts */
  715. /* and we store address in 64-bit GPR (P0-P7) */
  716. cmdq_append_command(handle, CMDQ_CODE_MOVE,
  717. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  718. ((dramAddr >> 32) & 0xffff) |
  719. #endif
  720. ((destRegId & 0x1f) << 16) | (4 << 21), (uint32_t) dramAddr, 0, 0);
  721. /* write value in GPR to memory pointed by GPR */
  722. cmdq_append_command(handle, CMDQ_CODE_WRITE, destRegId, valueRegId, 1, 1);
  723. /* release the GPR lock */
  724. cmdqRecSetEventToken(handle, regAccessToken);
  725. return 0;
  726. #else
  727. CMDQ_ERR("func:%s failed since CMDQ doesn't support GPR\n", __func__);
  728. return -EFAULT;
  729. #endif /* CMDQ_GPR_SUPPORT */
  730. }
  731. int32_t cmdqRecEnablePrefetch(cmdqRecHandle handle)
  732. {
  733. #ifdef _CMDQ_DISABLE_MARKER_
  734. /* disable pre-fetch marker feature but use auto prefetch mechanism */
  735. CMDQ_MSG("not allow enable prefetch, scenario: %d\n", handle->scenario);
  736. return true;
  737. #else
  738. if (NULL == handle)
  739. return -EFAULT;
  740. if (cmdq_get_func()->shouldEnablePrefetch(handle->scenario)) {
  741. /* enable prefetch */
  742. CMDQ_VERBOSE("REC: enable prefetch\n");
  743. cmdqRecMark(handle);
  744. return true;
  745. }
  746. CMDQ_ERR("not allow enable prefetch, scenario: %d\n", handle->scenario);
  747. return -EFAULT;
  748. #endif
  749. }
  750. int32_t cmdqRecDisablePrefetch(cmdqRecHandle handle)
  751. {
  752. uint32_t argB = 0;
  753. uint32_t argA = 0;
  754. int32_t status = 0;
  755. if (NULL == handle)
  756. return -EFAULT;
  757. if (!handle->finalized) {
  758. if (handle->prefetchCount > 0) {
  759. /* with prefetch threads we should end with */
  760. /* bit 48: no_inc_exec_cmds_cnt = 1 */
  761. /* bit 20: prefetch_mark = 1 */
  762. /* bit 17: prefetch_mark_en = 0 */
  763. /* bit 16: prefetch_en = 0 */
  764. argB = 0x00100000;
  765. argA = (0x1 << 16); /* not increse execute counter */
  766. /* since we're finalized, no more prefetch */
  767. handle->prefetchCount = 0;
  768. status = cmdq_append_command(handle, CMDQ_CODE_EOC, argA, argB, 0, 0);
  769. }
  770. if (0 != status)
  771. return status;
  772. }
  773. CMDQ_MSG("cmdqRecDisablePrefetch, status:%d\n", status);
  774. return status;
  775. }
  776. int32_t cmdq_rec_finalize_command(cmdqRecHandle handle, bool loop)
  777. {
  778. int32_t status = 0;
  779. uint32_t argB = 0;
  780. if (NULL == handle)
  781. return -EFAULT;
  782. if (!handle->finalized) {
  783. if ((handle->prefetchCount > 0)
  784. && cmdq_get_func()->shouldEnablePrefetch(handle->scenario)) {
  785. CMDQ_ERR
  786. ("not insert prefetch disble marker when prefetch enabled, prefetchCount:%d\n",
  787. handle->prefetchCount);
  788. cmdqRecDumpCommand(handle);
  789. status = -EFAULT;
  790. return status;
  791. }
  792. /* insert EOF instruction */
  793. argB = 0x1; /* generate IRQ for each command iteration */
  794. #ifdef DISABLE_LOOP_IRQ
  795. if (loop && !cmdq_get_func()->forceLoopIRQ(handle->scenario))
  796. argB = 0x0; /* no generate IRQ for loop thread to save power */
  797. #endif
  798. status = cmdq_append_command(handle, CMDQ_CODE_EOC, 0, argB, 0, 0);
  799. if (0 != status)
  800. return status;
  801. /* insert JUMP to loop to beginning or as a scheduling mark(8) */
  802. status = cmdq_append_command(handle, CMDQ_CODE_JUMP, 0, /* not absolute */
  803. loop ? -handle->blockSize : 8, 0, 0);
  804. if (0 != status)
  805. return status;
  806. handle->finalized = true;
  807. }
  808. return status;
  809. }
  810. int32_t cmdq_rec_setup_sec_data_of_command_desc_by_rec_handle(cmdqCommandStruct *pDesc,
  811. cmdqRecHandle handle)
  812. {
  813. /* fill field from user's request */
  814. pDesc->secData.isSecure = handle->secData.isSecure;
  815. pDesc->secData.enginesNeedDAPC = handle->secData.enginesNeedDAPC;
  816. pDesc->secData.enginesNeedPortSecurity = handle->secData.enginesNeedPortSecurity;
  817. pDesc->secData.addrMetadataCount = handle->secData.addrMetadataCount;
  818. pDesc->secData.addrMetadatas = handle->secData.addrMetadatas;
  819. pDesc->secData.addrMetadataMaxCount = handle->secData.addrMetadataMaxCount;
  820. /* init reserved field */
  821. pDesc->secData.resetExecCnt = false;
  822. pDesc->secData.waitCookie = 0;
  823. return 0;
  824. }
  825. int32_t cmdq_rec_setup_profile_marker_data(cmdqCommandStruct *pDesc, cmdqRecHandle handle)
  826. {
  827. #ifdef CMDQ_PROFILE_MARKER_SUPPORT
  828. uint32_t i;
  829. pDesc->profileMarker.count = handle->profileMarker.count;
  830. pDesc->profileMarker.hSlot = handle->profileMarker.hSlot;
  831. for (i = 0; i < CMDQ_MAX_PROFILE_MARKER_IN_TASK; i++)
  832. pDesc->profileMarker.tag[i] = handle->profileMarker.tag[i];
  833. #endif
  834. return 0;
  835. }
  836. int32_t cmdqRecFlush(cmdqRecHandle handle)
  837. {
  838. int32_t status;
  839. cmdqCommandStruct desc = { 0 };
  840. status = cmdq_rec_finalize_command(handle, false);
  841. if (status < 0)
  842. return status;
  843. CMDQ_MSG("Submit task scenario: %d, priority: %d, engine: 0x%llx, buffer: 0x%p, size: %d\n",
  844. handle->scenario, handle->priority, handle->engineFlag, handle->pBuffer,
  845. handle->blockSize);
  846. desc.scenario = handle->scenario;
  847. desc.priority = handle->priority;
  848. desc.engineFlag = handle->engineFlag;
  849. desc.pVABase = (cmdqU32Ptr_t) (unsigned long)handle->pBuffer;
  850. desc.blockSize = handle->blockSize;
  851. /* secure path */
  852. cmdq_rec_setup_sec_data_of_command_desc_by_rec_handle(&desc, handle);
  853. /* profile marker */
  854. cmdq_rec_setup_profile_marker_data(&desc, handle);
  855. return cmdqCoreSubmitTask(&desc);
  856. }
  857. int32_t cmdqRecFlushAndReadRegister(cmdqRecHandle handle, uint32_t regCount, uint32_t *addrArray,
  858. uint32_t *valueArray)
  859. {
  860. int32_t status;
  861. cmdqCommandStruct desc = { 0 };
  862. status = cmdq_rec_finalize_command(handle, false);
  863. if (status < 0)
  864. return status;
  865. CMDQ_MSG("Submit task scenario: %d, priority: %d, engine: 0x%llx, buffer: 0x%p, size: %d\n",
  866. handle->scenario, handle->priority, handle->engineFlag, handle->pBuffer,
  867. handle->blockSize);
  868. desc.scenario = handle->scenario;
  869. desc.priority = handle->priority;
  870. desc.engineFlag = handle->engineFlag;
  871. desc.pVABase = (cmdqU32Ptr_t) (unsigned long)handle->pBuffer;
  872. desc.blockSize = handle->blockSize;
  873. desc.regRequest.count = regCount;
  874. desc.regRequest.regAddresses = (cmdqU32Ptr_t) (unsigned long)addrArray;
  875. desc.regValue.count = regCount;
  876. desc.regValue.regValues = (cmdqU32Ptr_t) (unsigned long)valueArray;
  877. /* secure path */
  878. cmdq_rec_setup_sec_data_of_command_desc_by_rec_handle(&desc, handle);
  879. /* profile marker */
  880. cmdq_rec_setup_profile_marker_data(&desc, handle);
  881. return cmdqCoreSubmitTask(&desc);
  882. }
  883. int32_t cmdqRecFlushAsync(cmdqRecHandle handle)
  884. {
  885. int32_t status = 0;
  886. cmdqCommandStruct desc = { 0 };
  887. TaskStruct *pTask = NULL;
  888. status = cmdq_rec_finalize_command(handle, false);
  889. if (status < 0)
  890. return status;
  891. desc.scenario = handle->scenario;
  892. desc.priority = handle->priority;
  893. desc.engineFlag = handle->engineFlag;
  894. desc.pVABase = (cmdqU32Ptr_t) (unsigned long)handle->pBuffer;
  895. desc.blockSize = handle->blockSize;
  896. desc.regRequest.count = 0;
  897. desc.regRequest.regAddresses = (cmdqU32Ptr_t) (unsigned long)NULL;
  898. desc.regValue.count = 0;
  899. desc.regValue.regValues = (cmdqU32Ptr_t) (unsigned long)NULL;
  900. /* secure path */
  901. cmdq_rec_setup_sec_data_of_command_desc_by_rec_handle(&desc, handle);
  902. /* profile marker */
  903. cmdq_rec_setup_profile_marker_data(&desc, handle);
  904. status = cmdqCoreSubmitTaskAsync(&desc, NULL, 0, &pTask);
  905. CMDQ_MSG
  906. ("[Auto Release] Submit ASYNC task scenario: %d, priority: %d, engine: 0x%llx, buffer: 0x%p, size: %d\n",
  907. handle->scenario, handle->priority, handle->engineFlag, handle->pBuffer,
  908. handle->blockSize);
  909. if (pTask) {
  910. pTask->flushCallback = NULL;
  911. pTask->flushData = 0;
  912. }
  913. /* insert the task into auto-release queue */
  914. if (pTask)
  915. status = cmdqCoreAutoReleaseTask(pTask);
  916. else
  917. status = -ENOMEM;
  918. return status;
  919. }
  920. int32_t cmdqRecFlushAsyncCallback(cmdqRecHandle handle, CmdqAsyncFlushCB callback,
  921. uint32_t userData)
  922. {
  923. int32_t status = 0;
  924. cmdqCommandStruct desc = { 0 };
  925. TaskStruct *pTask = NULL;
  926. status = cmdq_rec_finalize_command(handle, false);
  927. if (status < 0)
  928. return status;
  929. desc.scenario = handle->scenario;
  930. desc.priority = handle->priority;
  931. desc.engineFlag = handle->engineFlag;
  932. desc.pVABase = (cmdqU32Ptr_t) (unsigned long)handle->pBuffer;
  933. desc.blockSize = handle->blockSize;
  934. desc.regRequest.count = 0;
  935. desc.regRequest.regAddresses = (cmdqU32Ptr_t) (unsigned long)NULL;
  936. desc.regValue.count = 0;
  937. desc.regValue.regValues = (cmdqU32Ptr_t) (unsigned long)NULL;
  938. /* secure path */
  939. cmdq_rec_setup_sec_data_of_command_desc_by_rec_handle(&desc, handle);
  940. /* profile marker */
  941. cmdq_rec_setup_profile_marker_data(&desc, handle);
  942. status = cmdqCoreSubmitTaskAsync(&desc, NULL, 0, &pTask);
  943. /* insert the callback here. */
  944. /* note that, the task may be already completed at this point. */
  945. if (pTask) {
  946. pTask->flushCallback = callback;
  947. pTask->flushData = userData;
  948. }
  949. CMDQ_MSG
  950. ("[Auto Release] Submit ASYNC task scenario: %d, priority: %d, engine: 0x%llx, buffer: 0x%p, size: %d\n",
  951. handle->scenario, handle->priority, handle->engineFlag, handle->pBuffer,
  952. handle->blockSize);
  953. /* insert the task into auto-release queue */
  954. if (pTask)
  955. status = cmdqCoreAutoReleaseTask(pTask);
  956. else
  957. status = -ENOMEM;
  958. return status;
  959. }
  960. static int32_t cmdqRecIRQCallback(unsigned long data)
  961. {
  962. return 0;
  963. }
  964. int32_t cmdqRecStartLoop(cmdqRecHandle handle)
  965. {
  966. return cmdqRecStartLoopWithCallback(handle, &cmdqRecIRQCallback, 0);
  967. }
  968. int32_t cmdqRecStartLoopWithCallback(cmdqRecHandle handle, CmdqInterruptCB loopCB, unsigned long loopData)
  969. {
  970. int32_t status = 0;
  971. cmdqCommandStruct desc = { 0 };
  972. if (NULL == handle)
  973. return -EFAULT;
  974. if (NULL != handle->pRunningTask)
  975. return -EBUSY;
  976. status = cmdq_rec_finalize_command(handle, true);
  977. if (status < 0)
  978. return status;
  979. CMDQ_MSG("Submit task loop: scenario: %d, priority: %d, engine: 0x%llx,",
  980. handle->scenario, handle->priority, handle->engineFlag);
  981. CMDQ_MSG("Submit task loop: buffer: 0x%p, size: %d, callback: 0x%p, data: %ld\n",
  982. handle->pBuffer, handle->blockSize, loopCB, loopData);
  983. desc.scenario = handle->scenario;
  984. desc.priority = handle->priority;
  985. desc.engineFlag = handle->engineFlag;
  986. desc.pVABase = (cmdqU32Ptr_t) (unsigned long)handle->pBuffer;
  987. desc.blockSize = handle->blockSize;
  988. /* secure path */
  989. cmdq_rec_setup_sec_data_of_command_desc_by_rec_handle(&desc, handle);
  990. /* profile marker */
  991. cmdq_rec_setup_profile_marker_data(&desc, handle);
  992. status = cmdqCoreSubmitTaskAsync(&desc, loopCB, loopData, &handle->pRunningTask);
  993. return status;
  994. }
  995. int32_t cmdqRecStopLoop(cmdqRecHandle handle)
  996. {
  997. int32_t status = 0;
  998. struct TaskStruct *pTask;
  999. if (NULL == handle)
  1000. return -EFAULT;
  1001. pTask = handle->pRunningTask;
  1002. if (NULL == pTask)
  1003. return -EFAULT;
  1004. status = cmdqCoreReleaseTask(pTask);
  1005. handle->pRunningTask = NULL;
  1006. return status;
  1007. }
  1008. int32_t cmdqRecGetInstructionCount(cmdqRecHandle handle)
  1009. {
  1010. int32_t InstructionCount;
  1011. if (NULL == handle)
  1012. return 0;
  1013. InstructionCount = handle->blockSize / CMDQ_INST_SIZE;
  1014. return InstructionCount;
  1015. }
  1016. int32_t cmdqRecProfileMarker(cmdqRecHandle handle, const char *tag)
  1017. {
  1018. #ifdef CMDQ_PROFILE_MARKER_SUPPORT
  1019. int32_t status;
  1020. int32_t index;
  1021. cmdqBackupSlotHandle hSlot;
  1022. dma_addr_t allocatedStartPA;
  1023. do {
  1024. allocatedStartPA = 0;
  1025. status = 0;
  1026. /* allocate temp slot for GCE to store timestamp info */
  1027. /* those timestamp info will copy to record strute after task execute done */
  1028. if ((0 == handle->profileMarker.count) && (0 == handle->profileMarker.hSlot)) {
  1029. status =
  1030. cmdqCoreAllocWriteAddress(CMDQ_MAX_PROFILE_MARKER_IN_TASK,
  1031. &allocatedStartPA);
  1032. if (0 > status) {
  1033. CMDQ_ERR("[REC][PROF_MARKER]allocate failed, status:%d\n", status);
  1034. break;
  1035. }
  1036. handle->profileMarker.hSlot = 0LL | (allocatedStartPA);
  1037. CMDQ_VERBOSE
  1038. ("[REC][PROF_MARKER]update handle(%p) slot start PA:%pa(0x%llx)\n",
  1039. handle, &allocatedStartPA, handle->profileMarker.hSlot);
  1040. }
  1041. /* insert instruciton */
  1042. index = handle->profileMarker.count;
  1043. hSlot = (cmdqBackupSlotHandle) (handle->profileMarker.hSlot);
  1044. if (index >= CMDQ_MAX_PROFILE_MARKER_IN_TASK) {
  1045. CMDQ_ERR
  1046. ("[REC][PROF_MARKER]insert profile maker failed since already reach max count\n");
  1047. status = -EFAULT;
  1048. break;
  1049. }
  1050. CMDQ_VERBOSE
  1051. ("[REC][PROF_MARKER]inserting profile instr, handle:%p, slot:%pa(0x%llx), index:%d, tag:%s\n",
  1052. handle, &hSlot, handle->profileMarker.hSlot, index, tag);
  1053. cmdqRecBackupRegisterToSlot(handle, hSlot, index, CMDQ_APXGPT2_COUNT);
  1054. handle->profileMarker.tag[index] = (cmdqU32Ptr_t) (unsigned long)tag;
  1055. handle->profileMarker.count += 1;
  1056. } while (0);
  1057. return status;
  1058. #else
  1059. CMDQ_ERR("func:%s failed since CMDQ doesn't enable profile marker\n", __func__);
  1060. return -EFAULT;
  1061. #endif
  1062. }
  1063. int32_t cmdqRecDumpCommand(cmdqRecHandle handle)
  1064. {
  1065. int32_t status = 0;
  1066. struct TaskStruct *pTask;
  1067. if (NULL == handle)
  1068. return -EFAULT;
  1069. pTask = handle->pRunningTask;
  1070. if (pTask) {
  1071. /* running, so dump from core task direct */
  1072. status = cmdqCoreDebugDumpCommand(pTask);
  1073. } else {
  1074. /* not running, dump from rec->pBuffer */
  1075. const uint32_t *pCmd = NULL;
  1076. static char textBuf[128] = { 0 };
  1077. int i = 0;
  1078. CMDQ_LOG("======REC 0x%p command buffer:\n", handle->pBuffer);
  1079. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 4,
  1080. handle->pBuffer, handle->blockSize, false);
  1081. CMDQ_LOG("======REC 0x%p command buffer END\n", handle->pBuffer);
  1082. CMDQ_LOG("REC 0x%p command buffer TRANSLATED:\n", handle->pBuffer);
  1083. for (i = 0, pCmd = handle->pBuffer; i < handle->blockSize; i += 8, pCmd += 2) {
  1084. cmdq_core_parse_instruction(pCmd, textBuf, 128);
  1085. CMDQ_LOG("%s", textBuf);
  1086. }
  1087. CMDQ_LOG("======REC 0x%p command END\n", handle->pBuffer);
  1088. return 0;
  1089. }
  1090. return status;
  1091. }
  1092. int32_t cmdqRecEstimateCommandExecTime(const cmdqRecHandle handle)
  1093. {
  1094. int32_t time = 0;
  1095. if (NULL == handle)
  1096. return -EFAULT;
  1097. CMDQ_LOG("======REC 0x%p command execution time ESTIMATE:\n", handle);
  1098. time = cmdq_prof_estimate_command_exe_time(handle->pBuffer, handle->blockSize);
  1099. CMDQ_LOG("======REC 0x%p END\n", handle);
  1100. return time;
  1101. }
  1102. void cmdqRecDestroy(cmdqRecHandle handle)
  1103. {
  1104. if (NULL == handle)
  1105. return;
  1106. if (NULL != handle->pRunningTask)
  1107. cmdqRecStopLoop(handle);
  1108. /* Free command buffer */
  1109. kfree(handle->pBuffer);
  1110. handle->pBuffer = NULL;
  1111. /* Free command handle */
  1112. kfree(handle);
  1113. }
  1114. int32_t cmdqRecSetNOP(cmdqRecHandle handle, uint32_t index)
  1115. {
  1116. uint32_t *pCommand;
  1117. uint32_t offsetIndex = index * CMDQ_INST_SIZE;
  1118. if (NULL == handle || offsetIndex > (handle->blockSize - CMDQ_INST_SIZE))
  1119. return -EFAULT;
  1120. CMDQ_MSG("======REC 0x%p Set NOP to index: %d\n", handle, index);
  1121. pCommand = (uint32_t *) ((uint8_t *) handle->pBuffer + offsetIndex);
  1122. *pCommand++ = 8;
  1123. *pCommand++ = (CMDQ_CODE_JUMP << 24) | (0 & 0x0FFFFFF);
  1124. CMDQ_MSG("======REC 0x%p END\n", handle);
  1125. return index;
  1126. }
  1127. int32_t cmdqRecQueryOffset(cmdqRecHandle handle, uint32_t startIndex, const CMDQ_CODE_ENUM opCode,
  1128. CMDQ_EVENT_ENUM event)
  1129. {
  1130. int32_t Offset = -1;
  1131. uint32_t argA, argB;
  1132. uint32_t *pCommand;
  1133. uint32_t QueryIndex, MaxIndex;
  1134. if (NULL == handle || (startIndex * CMDQ_INST_SIZE) > (handle->blockSize - CMDQ_INST_SIZE))
  1135. return -EFAULT;
  1136. switch (opCode) {
  1137. case CMDQ_CODE_WFE:
  1138. /* bit 0-11: wait_value, 1 */
  1139. /* bit 15: to_wait, true */
  1140. /* bit 31: to_update, true */
  1141. /* bit 16-27: update_value, 0 */
  1142. argB = ((1 << 31) | (1 << 15) | 1);
  1143. argA = (CMDQ_CODE_WFE << 24) | cmdq_core_get_event_value(event);
  1144. break;
  1145. case CMDQ_CODE_SET_TOKEN:
  1146. /* this is actually WFE(SYNC) but with different parameter */
  1147. /* interpretation */
  1148. /* bit 15: to_wait, false */
  1149. /* bit 31: to_update, true */
  1150. /* bit 16-27: update_value, 1 */
  1151. argB = ((1 << 31) | (1 << 16));
  1152. argA = (CMDQ_CODE_WFE << 24) | cmdq_core_get_event_value(event);
  1153. break;
  1154. case CMDQ_CODE_WAIT_NO_CLEAR:
  1155. /* bit 0-11: wait_value, 1 */
  1156. /* bit 15: to_wait, true */
  1157. /* bit 31: to_update, false */
  1158. argB = ((0 << 31) | (1 << 15) | 1);
  1159. argA = (CMDQ_CODE_WFE << 24) | cmdq_core_get_event_value(event);
  1160. break;
  1161. case CMDQ_CODE_CLEAR_TOKEN:
  1162. /* this is actually WFE(SYNC) but with different parameter */
  1163. /* interpretation */
  1164. /* bit 15: to_wait, false */
  1165. /* bit 31: to_update, true */
  1166. /* bit 16-27: update_value, 0 */
  1167. argB = ((1 << 31) | (0 << 16));
  1168. argA = (CMDQ_CODE_WFE << 24) | cmdq_core_get_event_value(event);
  1169. break;
  1170. case CMDQ_CODE_PREFETCH_ENABLE:
  1171. /* this is actually MARKER but with different parameter */
  1172. /* interpretation */
  1173. /* bit 53: non_suspendable, true */
  1174. /* bit 48: no_inc_exec_cmds_cnt, true */
  1175. /* bit 20: prefetch_marker, true */
  1176. /* bit 17: prefetch_marker_en, true */
  1177. /* bit 16: prefetch_en, true */
  1178. argB = ((1 << 20) | (1 << 17) | (1 << 16));
  1179. argA = (CMDQ_CODE_EOC << 24) | (0x1 << (53 - 32)) | (0x1 << (48 - 32));
  1180. break;
  1181. case CMDQ_CODE_PREFETCH_DISABLE:
  1182. /* this is actually MARKER but with different parameter */
  1183. /* interpretation */
  1184. /* bit 48: no_inc_exec_cmds_cnt, true */
  1185. /* bit 20: prefetch_marker, true */
  1186. argB = (1 << 20);
  1187. argA = (CMDQ_CODE_EOC << 24) | (0x1 << (48 - 32));
  1188. break;
  1189. default:
  1190. CMDQ_MSG("This offset of instruction can not be queried.\n");
  1191. return -EFAULT;
  1192. }
  1193. MaxIndex = handle->blockSize / CMDQ_INST_SIZE;
  1194. for (QueryIndex = startIndex; QueryIndex < MaxIndex; QueryIndex++) {
  1195. pCommand = (uint32_t *) ((uint8_t *) handle->pBuffer + QueryIndex * CMDQ_INST_SIZE);
  1196. if ((argB == *pCommand++) && (argA == *pCommand)) {
  1197. Offset = (int32_t) QueryIndex;
  1198. CMDQ_MSG("Get offset = %d\n", Offset);
  1199. break;
  1200. }
  1201. }
  1202. if (Offset < 0) {
  1203. /* Can not find the offset of desired instruction */
  1204. CMDQ_LOG("Can not find the offset of desired instruction\n");
  1205. }
  1206. return Offset;
  1207. }
  1208. int32_t cmdqRecAcquireResource(cmdqRecHandle handle, CMDQ_EVENT_ENUM resourceEvent)
  1209. {
  1210. bool acquireResult;
  1211. acquireResult = cmdqCoreAcquireResource(resourceEvent);
  1212. if (!acquireResult) {
  1213. CMDQ_LOG("Acquire resource (event:%d) failed, handle:0x%p\n", resourceEvent, handle);
  1214. return -EFAULT;
  1215. }
  1216. return 0;
  1217. }
  1218. int32_t cmdqRecWriteForResource(cmdqRecHandle handle, CMDQ_EVENT_ENUM resourceEvent,
  1219. uint32_t addr, uint32_t value, uint32_t mask)
  1220. {
  1221. bool acquireResult;
  1222. acquireResult = cmdqCoreAcquireResource(resourceEvent);
  1223. if (!acquireResult) {
  1224. CMDQ_LOG("Acquire resource (event:%d) failed, handle:0x%p\n", resourceEvent, handle);
  1225. return -EFAULT;
  1226. }
  1227. return cmdqRecWrite(handle, addr, value, mask);
  1228. }
  1229. int32_t cmdqRecReleaseResource(cmdqRecHandle handle, CMDQ_EVENT_ENUM resourceEvent)
  1230. {
  1231. cmdqCoreReleaseResource(resourceEvent);
  1232. return cmdqRecSetEventToken(handle, resourceEvent);
  1233. }
  1234. int32_t cmdqRecWriteAndReleaseResource(cmdqRecHandle handle, CMDQ_EVENT_ENUM resourceEvent,
  1235. uint32_t addr, uint32_t value, uint32_t mask)
  1236. {
  1237. int32_t result;
  1238. cmdqCoreReleaseResource(resourceEvent);
  1239. result = cmdqRecWrite(handle, addr, value, mask);
  1240. if (result >= 0)
  1241. return cmdqRecSetEventToken(handle, resourceEvent);
  1242. CMDQ_ERR("Write instruction fail and not release resource!\n");
  1243. return result;
  1244. }