msm_cvp.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include "msm_cvp.h"
  6. #include "cvp_hfi.h"
  7. #include "cvp_core_hfi.h"
  8. #include "msm_cvp_buf.h"
  9. #include "cvp_comm_def.h"
  10. #include "cvp_power.h"
  11. #include "cvp_hfi_api.h"
  12. static int cvp_enqueue_pkt(struct msm_cvp_inst* inst,
  13. struct eva_kmd_hfi_packet *in_pkt,
  14. unsigned int in_offset,
  15. unsigned int in_buf_num);
  16. int msm_cvp_get_session_info(struct msm_cvp_inst *inst, u32 *session)
  17. {
  18. int rc = 0;
  19. struct msm_cvp_inst *s;
  20. if (!inst || !inst->core || !session) {
  21. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  22. return -EINVAL;
  23. }
  24. s = cvp_get_inst_validate(inst->core, inst);
  25. if (!s)
  26. return -ECONNRESET;
  27. *session = hash32_ptr(inst->session);
  28. dprintk(CVP_SESS, "%s: id 0x%x\n", __func__, *session);
  29. cvp_put_inst(s);
  30. return rc;
  31. }
  32. static bool cvp_msg_pending(struct cvp_session_queue *sq,
  33. struct cvp_session_msg **msg, u64 *ktid)
  34. {
  35. struct cvp_session_msg *mptr = NULL, *dummy;
  36. bool result = false;
  37. if (!sq)
  38. return false;
  39. spin_lock(&sq->lock);
  40. if (sq->state == QUEUE_INIT || sq->state == QUEUE_INVALID) {
  41. /* The session is being deleted */
  42. spin_unlock(&sq->lock);
  43. *msg = NULL;
  44. return true;
  45. }
  46. result = list_empty(&sq->msgs);
  47. if (!result) {
  48. mptr = list_first_entry(&sq->msgs,
  49. struct cvp_session_msg,
  50. node);
  51. if (!ktid) {
  52. if (mptr) {
  53. list_del_init(&mptr->node);
  54. sq->msg_count--;
  55. }
  56. } else {
  57. result = true;
  58. list_for_each_entry_safe(mptr, dummy, &sq->msgs, node) {
  59. if (*ktid == mptr->pkt.client_data.kdata) {
  60. list_del_init(&mptr->node);
  61. sq->msg_count--;
  62. result = false;
  63. break;
  64. }
  65. }
  66. if (result)
  67. mptr = NULL;
  68. }
  69. }
  70. spin_unlock(&sq->lock);
  71. *msg = mptr;
  72. return !result;
  73. }
  74. static int cvp_wait_process_message(struct msm_cvp_inst *inst,
  75. struct cvp_session_queue *sq, u64 *ktid,
  76. unsigned long timeout,
  77. struct eva_kmd_hfi_packet *out)
  78. {
  79. struct cvp_session_msg *msg = NULL;
  80. struct cvp_hfi_msg_session_hdr *hdr;
  81. int rc = 0;
  82. if (wait_event_timeout(sq->wq,
  83. cvp_msg_pending(sq, &msg, ktid), timeout) == 0) {
  84. dprintk(CVP_WARN, "session queue wait timeout\n");
  85. if(inst && inst->core && inst->core->device){
  86. print_hfi_queue_info(inst->core->device);
  87. }
  88. rc = -ETIMEDOUT;
  89. goto exit;
  90. }
  91. if (msg == NULL) {
  92. dprintk(CVP_WARN, "%s: queue state %d, msg cnt %d\n", __func__,
  93. sq->state, sq->msg_count);
  94. if (inst->state >= MSM_CVP_CLOSE_DONE ||
  95. (sq->state != QUEUE_ACTIVE &&
  96. sq->state != QUEUE_START)) {
  97. rc = -ECONNRESET;
  98. goto exit;
  99. }
  100. msm_cvp_comm_kill_session(inst);
  101. goto exit;
  102. }
  103. if (!out) {
  104. cvp_kmem_cache_free(&cvp_driver->msg_cache, msg);
  105. goto exit;
  106. }
  107. hdr = (struct cvp_hfi_msg_session_hdr *)&msg->pkt;
  108. memcpy(out, &msg->pkt, get_msg_size(hdr));
  109. if (hdr->client_data.kdata >= ARRAY_SIZE(cvp_hfi_defs))
  110. msm_cvp_unmap_frame(inst, hdr->client_data.kdata);
  111. cvp_kmem_cache_free(&cvp_driver->msg_cache, msg);
  112. exit:
  113. return rc;
  114. }
  115. static int msm_cvp_session_receive_hfi(struct msm_cvp_inst *inst,
  116. struct eva_kmd_hfi_packet *out_pkt)
  117. {
  118. unsigned long wait_time;
  119. struct cvp_session_queue *sq;
  120. struct msm_cvp_inst *s;
  121. int rc = 0;
  122. bool clock_check = false;
  123. if (!inst) {
  124. dprintk(CVP_ERR, "%s invalid session\n", __func__);
  125. return -EINVAL;
  126. }
  127. s = cvp_get_inst_validate(inst->core, inst);
  128. if (!s)
  129. return -ECONNRESET;
  130. wait_time = msecs_to_jiffies(
  131. inst->core->resources.msm_cvp_hw_rsp_timeout);
  132. sq = &inst->session_queue;
  133. rc = cvp_wait_process_message(inst, sq, NULL, wait_time, out_pkt);
  134. clock_check = check_clock_required(inst, out_pkt);
  135. if (clock_check)
  136. cvp_check_clock(inst,
  137. (struct cvp_hfi_msg_session_hdr_ext *)out_pkt);
  138. cvp_put_inst(inst);
  139. return rc;
  140. }
  141. static int msm_cvp_session_process_hfi(
  142. struct msm_cvp_inst *inst,
  143. struct eva_kmd_hfi_packet *in_pkt,
  144. unsigned int in_offset,
  145. unsigned int in_buf_num)
  146. {
  147. int pkt_idx, rc = 0;
  148. unsigned int offset = 0, buf_num = 0, signal;
  149. struct cvp_session_queue *sq;
  150. struct msm_cvp_inst *s;
  151. struct cvp_hfi_cmd_session_hdr *pkt_hdr;
  152. bool is_config_pkt;
  153. if (!inst || !inst->core || !in_pkt) {
  154. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  155. return -EINVAL;
  156. }
  157. s = cvp_get_inst_validate(inst->core, inst);
  158. if (!s)
  159. return -ECONNRESET;
  160. pkt_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  161. dprintk(CVP_CMD, "%s: "
  162. "pkt_type %08x sess_id %08x trans_id %u ktid %llu\n",
  163. __func__, pkt_hdr->packet_type,
  164. pkt_hdr->session_id,
  165. pkt_hdr->client_data.transaction_id,
  166. pkt_hdr->client_data.kdata & (FENCE_BIT - 1));
  167. pkt_idx = get_pkt_index((struct cvp_hal_session_cmd_pkt *)in_pkt);
  168. if (pkt_idx < 0) {
  169. dprintk(CVP_ERR, "%s incorrect packet %d, %x\n", __func__,
  170. in_pkt->pkt_data[0],
  171. in_pkt->pkt_data[1]);
  172. goto exit;
  173. } else {
  174. signal = cvp_hfi_defs[pkt_idx].resp;
  175. is_config_pkt = cvp_hfi_defs[pkt_idx].is_config_pkt;
  176. }
  177. if (is_config_pkt)
  178. pr_info(CVP_DBG_TAG "inst %pK config %s\n", "sess",
  179. inst, cvp_hfi_defs[pkt_idx].name);
  180. if (signal == HAL_NO_RESP) {
  181. /* Frame packets are not allowed before session starts*/
  182. sq = &inst->session_queue;
  183. spin_lock(&sq->lock);
  184. if ((sq->state != QUEUE_START && !is_config_pkt) ||
  185. (sq->state >= QUEUE_INVALID)) {
  186. /*
  187. * A init packet is allowed in case of
  188. * QUEUE_ACTIVE, QUEUE_START, QUEUE_STOP
  189. * A frame packet is only allowed in case of
  190. * QUEUE_START
  191. */
  192. spin_unlock(&sq->lock);
  193. dprintk(CVP_ERR, "%s: invalid queue state %d\n",
  194. __func__, sq->state);
  195. rc = -EINVAL;
  196. goto exit;
  197. }
  198. spin_unlock(&sq->lock);
  199. }
  200. if (in_offset && in_buf_num) {
  201. offset = in_offset;
  202. buf_num = in_buf_num;
  203. }
  204. if (!is_buf_param_valid(buf_num, offset)) {
  205. dprintk(CVP_ERR, "Incorrect buffer num and offset in cmd\n");
  206. rc = -EINVAL;
  207. goto exit;
  208. }
  209. rc = msm_cvp_proc_oob(inst, in_pkt);
  210. if (rc) {
  211. dprintk(CVP_ERR, "%s: failed to process OOB buffer", __func__);
  212. goto exit;
  213. }
  214. rc = cvp_enqueue_pkt(inst, in_pkt, offset, buf_num);
  215. if (rc) {
  216. dprintk(CVP_ERR, "Failed to enqueue pkt, inst %pK "
  217. "pkt_type %08x ktid %llu transaction_id %u\n",
  218. inst, pkt_hdr->packet_type,
  219. pkt_hdr->client_data.kdata,
  220. pkt_hdr->client_data.transaction_id);
  221. }
  222. exit:
  223. cvp_put_inst(inst);
  224. return rc;
  225. }
  226. static bool cvp_fence_wait(struct cvp_fence_queue *q,
  227. struct cvp_fence_command **fence,
  228. enum queue_state *state)
  229. {
  230. struct cvp_fence_command *f;
  231. if (!q)
  232. return false;
  233. *fence = NULL;
  234. mutex_lock(&q->lock);
  235. *state = q->state;
  236. if (*state != QUEUE_START) {
  237. mutex_unlock(&q->lock);
  238. return true;
  239. }
  240. if (list_empty(&q->wait_list)) {
  241. mutex_unlock(&q->lock);
  242. return false;
  243. }
  244. f = list_first_entry(&q->wait_list, struct cvp_fence_command, list);
  245. list_del_init(&f->list);
  246. list_add_tail(&f->list, &q->sched_list);
  247. mutex_unlock(&q->lock);
  248. *fence = f;
  249. return true;
  250. }
  251. static int cvp_fence_proc(struct msm_cvp_inst *inst,
  252. struct cvp_fence_command *fc,
  253. struct cvp_hfi_cmd_session_hdr *pkt)
  254. {
  255. int rc = 0;
  256. unsigned long timeout;
  257. u64 ktid;
  258. int synx_state = SYNX_STATE_SIGNALED_SUCCESS;
  259. struct cvp_hfi_device *hdev;
  260. struct cvp_session_queue *sq;
  261. u32 hfi_err = HFI_ERR_NONE;
  262. struct cvp_hfi_msg_session_hdr_ext hdr;
  263. bool clock_check = false;
  264. dprintk(CVP_SYNX, "%s %s\n", current->comm, __func__);
  265. if (!inst || !inst->core)
  266. return -EINVAL;
  267. hdev = inst->core->device;
  268. sq = &inst->session_queue_fence;
  269. ktid = pkt->client_data.kdata;
  270. rc = inst->core->synx_ftbl->cvp_synx_ops(inst, CVP_INPUT_SYNX,
  271. fc, &synx_state);
  272. if (rc) {
  273. msm_cvp_unmap_frame(inst, pkt->client_data.kdata);
  274. goto exit;
  275. }
  276. rc = call_hfi_op(hdev, session_send, (void *)inst->session,
  277. (struct eva_kmd_hfi_packet *)pkt);
  278. if (rc) {
  279. dprintk(CVP_ERR, "%s %s: Failed in call_hfi_op %d, %x\n",
  280. current->comm, __func__, pkt->size, pkt->packet_type);
  281. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  282. goto exit;
  283. }
  284. timeout = msecs_to_jiffies(
  285. inst->core->resources.msm_cvp_hw_rsp_timeout);
  286. rc = cvp_wait_process_message(inst, sq, &ktid, timeout,
  287. (struct eva_kmd_hfi_packet *)&hdr);
  288. /* Only FD support dcvs at certain FW */
  289. clock_check = check_clock_required(inst,
  290. (struct eva_kmd_hfi_packet *)&hdr);
  291. hfi_err = hdr.error_type;
  292. if (rc) {
  293. dprintk(CVP_ERR, "%s %s: cvp_wait_process_message rc %d\n",
  294. current->comm, __func__, rc);
  295. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  296. goto exit;
  297. }
  298. if (hfi_err == HFI_ERR_SESSION_FLUSHED) {
  299. dprintk(CVP_SYNX, "%s %s: cvp_wait_process_message flushed\n",
  300. current->comm, __func__);
  301. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  302. } else if (hfi_err == HFI_ERR_SESSION_STREAM_CORRUPT) {
  303. dprintk(CVP_INFO, "%s %s: cvp_wait_process_msg non-fatal %d\n",
  304. current->comm, __func__, hfi_err);
  305. synx_state = SYNX_STATE_SIGNALED_SUCCESS;
  306. } else if (hfi_err != HFI_ERR_NONE) {
  307. dprintk(CVP_ERR, "%s %s: cvp_wait_process_message hfi err %d\n",
  308. current->comm, __func__, hfi_err);
  309. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  310. }
  311. exit:
  312. rc = inst->core->synx_ftbl->cvp_synx_ops(inst, CVP_OUTPUT_SYNX,
  313. fc, &synx_state);
  314. if (clock_check)
  315. cvp_check_clock(inst,
  316. (struct cvp_hfi_msg_session_hdr_ext *)&hdr);
  317. return rc;
  318. }
  319. static int cvp_alloc_fence_data(struct cvp_fence_command **f, u32 size)
  320. {
  321. struct cvp_fence_command *fcmd;
  322. int alloc_size = sizeof(struct cvp_hfi_msg_session_hdr_ext);
  323. fcmd = kzalloc(sizeof(struct cvp_fence_command), GFP_KERNEL);
  324. if (!fcmd)
  325. return -ENOMEM;
  326. alloc_size = (alloc_size >= size) ? alloc_size : size;
  327. fcmd->pkt = kzalloc(alloc_size, GFP_KERNEL);
  328. if (!fcmd->pkt) {
  329. kfree(fcmd);
  330. return -ENOMEM;
  331. }
  332. *f = fcmd;
  333. return 0;
  334. }
  335. static void cvp_free_fence_data(struct cvp_fence_command *f)
  336. {
  337. kfree(f->pkt);
  338. f->pkt = NULL;
  339. kfree(f);
  340. f = NULL;
  341. }
  342. static int cvp_fence_thread(void *data)
  343. {
  344. int rc = 0, num_fences;
  345. struct msm_cvp_inst *inst;
  346. struct cvp_fence_queue *q;
  347. enum queue_state state;
  348. struct cvp_fence_command *f;
  349. struct cvp_hfi_cmd_session_hdr *pkt;
  350. u32 *synx;
  351. u64 ktid = 0;
  352. dprintk(CVP_SYNX, "Enter %s\n", current->comm);
  353. inst = (struct msm_cvp_inst *)data;
  354. if (!inst || !inst->core || !inst->core->device) {
  355. dprintk(CVP_ERR, "%s invalid inst %pK\n", current->comm, inst);
  356. rc = -EINVAL;
  357. goto exit;
  358. }
  359. q = &inst->fence_cmd_queue;
  360. wait:
  361. dprintk(CVP_SYNX, "%s starts wait\n", current->comm);
  362. f = NULL;
  363. wait_event_interruptible(q->wq, cvp_fence_wait(q, &f, &state));
  364. if (state != QUEUE_START)
  365. goto exit;
  366. if (!f)
  367. goto wait;
  368. pkt = f->pkt;
  369. synx = (u32 *)f->synx;
  370. num_fences = f->num_fences - f->output_index;
  371. /*
  372. * If there is output fence, go through fence path
  373. * Otherwise, go through non-fenced path
  374. */
  375. if (num_fences)
  376. ktid = pkt->client_data.kdata & (FENCE_BIT - 1);
  377. dprintk(CVP_SYNX, "%s pkt type %d on ktid %llu frameID %llu\n",
  378. current->comm, pkt->packet_type, ktid, f->frame_id);
  379. rc = cvp_fence_proc(inst, f, pkt);
  380. mutex_lock(&q->lock);
  381. inst->core->synx_ftbl->cvp_release_synx(inst, f);
  382. list_del_init(&f->list);
  383. state = q->state;
  384. mutex_unlock(&q->lock);
  385. dprintk(CVP_SYNX, "%s done with %d ktid %llu frameID %llu rc %d\n",
  386. current->comm, pkt->packet_type, ktid, f->frame_id, rc);
  387. cvp_free_fence_data(f);
  388. if (rc && state != QUEUE_START)
  389. goto exit;
  390. goto wait;
  391. exit:
  392. dprintk(CVP_SYNX, "%s exit\n", current->comm);
  393. cvp_put_inst(inst);
  394. return rc;
  395. }
  396. static int msm_cvp_session_process_hfi_fence(struct msm_cvp_inst *inst,
  397. struct eva_kmd_arg *arg)
  398. {
  399. int rc = 0;
  400. int idx;
  401. struct eva_kmd_hfi_fence_packet *fence_pkt;
  402. struct eva_kmd_hfi_synx_packet *synx_pkt;
  403. struct eva_kmd_fence_ctrl *kfc;
  404. struct cvp_hfi_cmd_session_hdr *pkt;
  405. unsigned int offset = 0, buf_num = 0, in_offset, in_buf_num;
  406. struct msm_cvp_inst *s;
  407. struct cvp_fence_command *f;
  408. struct cvp_fence_queue *q;
  409. u32 *fence;
  410. enum op_mode mode;
  411. bool is_config_pkt;
  412. if (!inst || !inst->core || !arg || !inst->core->device) {
  413. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  414. return -EINVAL;
  415. }
  416. s = cvp_get_inst_validate(inst->core, inst);
  417. if (!s)
  418. return -ECONNRESET;
  419. q = &inst->fence_cmd_queue;
  420. mutex_lock(&q->lock);
  421. mode = q->mode;
  422. mutex_unlock(&q->lock);
  423. if (mode == OP_DRAINING) {
  424. dprintk(CVP_SYNX, "%s: flush in progress\n", __func__);
  425. rc = -EBUSY;
  426. goto exit;
  427. }
  428. in_offset = arg->buf_offset;
  429. in_buf_num = arg->buf_num;
  430. fence_pkt = &arg->data.hfi_fence_pkt;
  431. pkt = (struct cvp_hfi_cmd_session_hdr *)&fence_pkt->pkt_data;
  432. idx = get_pkt_index((struct cvp_hal_session_cmd_pkt *)pkt);
  433. if (idx < 0 ||
  434. (pkt->size > MAX_HFI_FENCE_OFFSET * sizeof(unsigned int))) {
  435. dprintk(CVP_ERR, "%s incorrect packet %d %#x\n", __func__,
  436. pkt->size, pkt->packet_type);
  437. goto exit;
  438. } else {
  439. is_config_pkt = cvp_hfi_defs[idx].is_config_pkt;
  440. }
  441. if (in_offset && in_buf_num) {
  442. offset = in_offset;
  443. buf_num = in_buf_num;
  444. }
  445. if (!is_buf_param_valid(buf_num, offset)) {
  446. dprintk(CVP_ERR, "Incorrect buf num and offset in cmd\n");
  447. goto exit;
  448. }
  449. if (is_config_pkt)
  450. pr_info(CVP_DBG_TAG "inst %pK config %s\n",
  451. "pkt", inst, cvp_hfi_defs[idx].name);
  452. rc = msm_cvp_map_frame(inst, (struct eva_kmd_hfi_packet *)pkt, offset,
  453. buf_num);
  454. if (rc)
  455. goto exit;
  456. rc = cvp_alloc_fence_data(&f, pkt->size);
  457. if (rc)
  458. goto exit;
  459. f->type = cvp_hfi_defs[idx].type;
  460. f->mode = OP_NORMAL;
  461. synx_pkt = &arg->data.hfi_synx_pkt;
  462. if (synx_pkt->fence_data[0] != 0xFEEDFACE) {
  463. dprintk(CVP_ERR, "%s deprecated synx path\n", __func__);
  464. cvp_free_fence_data(f);
  465. msm_cvp_unmap_frame(inst, pkt->client_data.kdata);
  466. goto exit;
  467. } else {
  468. kfc = &synx_pkt->fc;
  469. fence = (u32 *)&kfc->fences;
  470. f->frame_id = kfc->frame_id;
  471. f->signature = 0xFEEDFACE;
  472. f->num_fences = kfc->num_fences;
  473. f->output_index = kfc->output_index;
  474. }
  475. dprintk(CVP_SYNX, "%s: frameID %llu ktid %llu\n",
  476. __func__, f->frame_id, pkt->client_data.kdata);
  477. memcpy(f->pkt, pkt, pkt->size);
  478. f->pkt->client_data.kdata |= FENCE_BIT;
  479. rc = inst->core->synx_ftbl->cvp_import_synx(inst, f, fence);
  480. if (rc) {
  481. cvp_free_fence_data(f);
  482. goto exit;
  483. }
  484. mutex_lock(&q->lock);
  485. list_add_tail(&f->list, &inst->fence_cmd_queue.wait_list);
  486. mutex_unlock(&q->lock);
  487. wake_up(&inst->fence_cmd_queue.wq);
  488. exit:
  489. cvp_put_inst(s);
  490. return rc;
  491. }
  492. static int cvp_populate_fences( struct eva_kmd_hfi_packet *in_pkt,
  493. unsigned int offset, unsigned int num, struct msm_cvp_inst *inst)
  494. {
  495. #ifdef CVP_CONFIG_SYNX_V2
  496. u32 i, buf_offset, fence_cnt;
  497. struct eva_kmd_fence fences[MAX_HFI_FENCE_SIZE];
  498. struct cvp_fence_command *f;
  499. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  500. struct cvp_fence_queue *q;
  501. enum op_mode mode;
  502. struct cvp_buf_type *buf;
  503. bool override;
  504. int rc = 0;
  505. override = get_pkt_fenceoverride((struct cvp_hal_session_cmd_pkt*)in_pkt);
  506. dprintk(CVP_SYNX, "%s:Fence Override is %d\n",__func__, override);
  507. dprintk(CVP_SYNX, "%s:Kernel Fence is %d\n", __func__, cvp_kernel_fence_enabled);
  508. q = &inst->fence_cmd_queue;
  509. mutex_lock(&q->lock);
  510. mode = q->mode;
  511. mutex_unlock(&q->lock);
  512. if (mode == OP_DRAINING) {
  513. dprintk(CVP_SYNX, "%s: flush in progress\n", __func__);
  514. rc = -EBUSY;
  515. goto exit;
  516. }
  517. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  518. rc = cvp_alloc_fence_data((&f), cmd_hdr->size);
  519. if (rc) {
  520. dprintk(CVP_ERR,"%s: Failed to alloc fence data", __func__);
  521. goto exit;
  522. }
  523. f->type = cmd_hdr->packet_type;
  524. f->mode = OP_NORMAL;
  525. f->signature = 0xFEEDFACE;
  526. f->num_fences = 0;
  527. f->output_index = 0;
  528. buf_offset = offset;
  529. if (cvp_kernel_fence_enabled == 0)
  530. {
  531. goto soc_fence;
  532. }
  533. else if (cvp_kernel_fence_enabled == 1)
  534. {
  535. goto kernel_fence;
  536. }
  537. else if (cvp_kernel_fence_enabled == 2)
  538. {
  539. if (override == true)
  540. goto kernel_fence;
  541. else if (override == false)
  542. goto soc_fence;
  543. else
  544. {
  545. dprintk(CVP_ERR, "%s: invalid params", __func__);
  546. rc = -EINVAL;
  547. goto exit;
  548. }
  549. }
  550. else
  551. {
  552. dprintk(CVP_ERR, "%s: invalid params", __func__);
  553. rc = -EINVAL;
  554. goto exit;
  555. }
  556. soc_fence:
  557. for (i = 0; i < num; i++) {
  558. buf = (struct cvp_buf_type*)&in_pkt->pkt_data[buf_offset];
  559. buf_offset += sizeof(*buf) >> 2;
  560. if (buf->input_handle || buf->output_handle) {
  561. f->num_fences++;
  562. if (buf->input_handle)
  563. f->output_index++;
  564. }
  565. }
  566. f->signature = 0xB0BABABE;
  567. if (f->num_fences)
  568. goto fence_cmd_queue;
  569. goto free_exit;
  570. kernel_fence:
  571. /* First pass to find INPUT synx handles */
  572. for (i = 0; i < num; i++) {
  573. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[buf_offset];
  574. buf_offset += sizeof(*buf) >> 2;
  575. if (buf->input_handle) {
  576. /* Check fence_type? */
  577. fences[f->num_fences].h_synx = buf->input_handle;
  578. f->num_fences++;
  579. buf->fence_type &= ~INPUT_FENCE_BITMASK;
  580. buf->input_handle = 0;
  581. }
  582. }
  583. f->output_index = f->num_fences;
  584. dprintk(CVP_SYNX, "%s:Input Fence passed - Number of Fences is %d\n",
  585. __func__, f->num_fences);
  586. /*
  587. * Second pass to find OUTPUT synx handle
  588. * If no of fences is 0 dont execute the below portion until line 911, return 0
  589. */
  590. buf_offset = offset;
  591. for (i = 0; i < num; i++) {
  592. buf = (struct cvp_buf_type*)&in_pkt->pkt_data[buf_offset];
  593. buf_offset += sizeof(*buf) >> 2;
  594. if (buf->output_handle) {
  595. /* Check fence_type? */
  596. fences[f->num_fences].h_synx = buf->output_handle;
  597. f->num_fences++;
  598. buf->fence_type &= ~OUTPUT_FENCE_BITMASK;
  599. buf->output_handle = 0;
  600. }
  601. }
  602. dprintk(CVP_SYNX, "%s:Output Fence passed - Number of Fences is %d\n",
  603. __func__, f->num_fences);
  604. if (f->num_fences == 0)
  605. goto free_exit;
  606. rc = inst->core->synx_ftbl->cvp_import_synx(inst, f, (u32*)fences);
  607. if (rc) {
  608. dprintk(CVP_ERR,"%s: Failed to import fences", __func__);
  609. goto free_exit;
  610. }
  611. fence_cmd_queue:
  612. fence_cnt = f->num_fences;
  613. memcpy(f->pkt, cmd_hdr, cmd_hdr->size);
  614. f->pkt->client_data.kdata |= FENCE_BIT;
  615. mutex_lock(&q->lock);
  616. list_add_tail(&f->list, &inst->fence_cmd_queue.wait_list);
  617. mutex_unlock(&q->lock);
  618. wake_up(&inst->fence_cmd_queue.wq);
  619. return fence_cnt;
  620. free_exit:
  621. cvp_free_fence_data(f);
  622. exit:
  623. #endif /* CVP_CONFIG_SYNX_V2 */
  624. return rc;
  625. }
  626. static int cvp_enqueue_pkt(struct msm_cvp_inst* inst,
  627. struct eva_kmd_hfi_packet *in_pkt,
  628. unsigned int in_offset,
  629. unsigned int in_buf_num)
  630. {
  631. struct cvp_hfi_device *hdev;
  632. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  633. int pkt_type, rc = 0;
  634. enum buf_map_type map_type;
  635. hdev = inst->core->device;
  636. pkt_type = in_pkt->pkt_data[1];
  637. map_type = cvp_find_map_type(pkt_type);
  638. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  639. /* The kdata will be overriden by transaction ID if the cmd has buf */
  640. cmd_hdr->client_data.kdata = 0;
  641. dprintk(CVP_CMD, "%s: "
  642. "pkt_type %08x sess_id %08x trans_id %u ktid %llu\n",
  643. __func__, cmd_hdr->packet_type,
  644. cmd_hdr->session_id,
  645. cmd_hdr->client_data.transaction_id,
  646. cmd_hdr->client_data.kdata & (FENCE_BIT - 1));
  647. if (map_type == MAP_PERSIST)
  648. rc = msm_cvp_map_user_persist(inst, in_pkt, in_offset, in_buf_num);
  649. else if (map_type == UNMAP_PERSIST)
  650. rc = msm_cvp_mark_user_persist(inst, in_pkt, in_offset, in_buf_num);
  651. else
  652. rc = msm_cvp_map_frame(inst, in_pkt, in_offset, in_buf_num);
  653. if (rc)
  654. return rc;
  655. rc = cvp_populate_fences(in_pkt, in_offset, in_buf_num, inst);
  656. if (rc == 0) {
  657. rc = call_hfi_op(hdev, session_send, (void*)inst->session,
  658. in_pkt);
  659. if (rc) {
  660. dprintk(CVP_ERR,"%s: Failed in call_hfi_op %d, %x\n",
  661. __func__, in_pkt->pkt_data[0],
  662. in_pkt->pkt_data[1]);
  663. if (map_type == MAP_FRAME)
  664. msm_cvp_unmap_frame(inst,
  665. cmd_hdr->client_data.kdata);
  666. }
  667. } else if (rc > 0) {
  668. dprintk(CVP_SYNX, "Going fenced path\n");
  669. rc = 0;
  670. } else {
  671. dprintk(CVP_ERR,"%s: Failed to populate fences\n",
  672. __func__);
  673. if (map_type == MAP_FRAME)
  674. msm_cvp_unmap_frame(inst, cmd_hdr->client_data.kdata);
  675. }
  676. return rc;
  677. }
  678. static inline int div_by_1dot5(unsigned int a)
  679. {
  680. unsigned long i = a << 1;
  681. return (unsigned int) i/3;
  682. }
  683. int msm_cvp_session_delete(struct msm_cvp_inst *inst)
  684. {
  685. return 0;
  686. }
  687. int msm_cvp_session_create(struct msm_cvp_inst *inst)
  688. {
  689. int rc = 0;
  690. struct cvp_session_queue *sq;
  691. if (!inst || !inst->core)
  692. return -EINVAL;
  693. if (inst->state >= MSM_CVP_CLOSE_DONE)
  694. return -ECONNRESET;
  695. if (inst->state != MSM_CVP_CORE_INIT_DONE ||
  696. inst->state > MSM_CVP_OPEN_DONE) {
  697. dprintk(CVP_ERR,
  698. "%s Incorrect CVP state %d to create session\n",
  699. __func__, inst->state);
  700. return -EINVAL;
  701. }
  702. rc = msm_cvp_comm_try_state(inst, MSM_CVP_OPEN_DONE);
  703. if (rc) {
  704. dprintk(CVP_ERR,
  705. "Failed to move instance to open done state\n");
  706. goto fail_init;
  707. }
  708. rc = cvp_comm_set_arp_buffers(inst);
  709. if (rc) {
  710. dprintk(CVP_ERR,
  711. "Failed to set ARP buffers\n");
  712. goto fail_init;
  713. }
  714. inst->core->synx_ftbl->cvp_sess_init_synx(inst);
  715. sq = &inst->session_queue;
  716. spin_lock(&sq->lock);
  717. sq->state = QUEUE_ACTIVE;
  718. spin_unlock(&sq->lock);
  719. fail_init:
  720. return rc;
  721. }
  722. static int session_state_check_init(struct msm_cvp_inst *inst)
  723. {
  724. mutex_lock(&inst->lock);
  725. if (inst->state == MSM_CVP_OPEN || inst->state == MSM_CVP_OPEN_DONE) {
  726. mutex_unlock(&inst->lock);
  727. return 0;
  728. }
  729. mutex_unlock(&inst->lock);
  730. return msm_cvp_session_create(inst);
  731. }
  732. static int cvp_fence_thread_start(struct msm_cvp_inst *inst)
  733. {
  734. u32 tnum = 0;
  735. u32 i = 0;
  736. int rc = 0;
  737. char tname[16];
  738. struct task_struct *thread;
  739. struct cvp_fence_queue *q;
  740. struct cvp_session_queue *sq;
  741. if (!inst->prop.fthread_nr)
  742. return 0;
  743. q = &inst->fence_cmd_queue;
  744. mutex_lock(&q->lock);
  745. q->state = QUEUE_START;
  746. mutex_unlock(&q->lock);
  747. for (i = 0; i < inst->prop.fthread_nr; ++i) {
  748. if (!cvp_get_inst_validate(inst->core, inst)) {
  749. rc = -ECONNRESET;
  750. goto exit;
  751. }
  752. snprintf(tname, sizeof(tname), "fthread_%d", tnum++);
  753. thread = kthread_run(cvp_fence_thread, inst, tname);
  754. if (!thread) {
  755. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  756. rc = -ECHILD;
  757. goto exit;
  758. }
  759. }
  760. sq = &inst->session_queue_fence;
  761. spin_lock(&sq->lock);
  762. sq->state = QUEUE_START;
  763. spin_unlock(&sq->lock);
  764. exit:
  765. if (rc) {
  766. mutex_lock(&q->lock);
  767. q->state = QUEUE_STOP;
  768. mutex_unlock(&q->lock);
  769. wake_up_all(&q->wq);
  770. }
  771. return rc;
  772. }
  773. static int cvp_fence_thread_stop(struct msm_cvp_inst *inst)
  774. {
  775. struct cvp_fence_queue *q;
  776. struct cvp_session_queue *sq;
  777. if (!inst->prop.fthread_nr)
  778. return 0;
  779. q = &inst->fence_cmd_queue;
  780. mutex_lock(&q->lock);
  781. q->state = QUEUE_STOP;
  782. mutex_unlock(&q->lock);
  783. sq = &inst->session_queue_fence;
  784. spin_lock(&sq->lock);
  785. sq->state = QUEUE_STOP;
  786. spin_unlock(&sq->lock);
  787. wake_up_all(&q->wq);
  788. wake_up_all(&sq->wq);
  789. return 0;
  790. }
  791. int msm_cvp_session_start(struct msm_cvp_inst *inst,
  792. struct eva_kmd_arg *arg)
  793. {
  794. struct cvp_session_queue *sq;
  795. struct cvp_hfi_device *hdev;
  796. int rc;
  797. enum queue_state old_state;
  798. if (!inst || !inst->core) {
  799. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  800. return -EINVAL;
  801. }
  802. sq = &inst->session_queue;
  803. spin_lock(&sq->lock);
  804. if (sq->msg_count) {
  805. dprintk(CVP_ERR, "session start failed queue not empty%d\n",
  806. sq->msg_count);
  807. spin_unlock(&sq->lock);
  808. rc = -EINVAL;
  809. goto exit;
  810. }
  811. old_state = sq->state;
  812. sq->state = QUEUE_START;
  813. spin_unlock(&sq->lock);
  814. hdev = inst->core->device;
  815. if (inst->prop.type == HFI_SESSION_FD
  816. || inst->prop.type == HFI_SESSION_DMM) {
  817. spin_lock(&inst->core->resources.pm_qos.lock);
  818. inst->core->resources.pm_qos.off_vote_cnt++;
  819. spin_unlock(&inst->core->resources.pm_qos.lock);
  820. call_hfi_op(hdev, pm_qos_update, hdev->hfi_device_data);
  821. }
  822. /*
  823. * cvp_fence_thread_start will increment reference to instance.
  824. * It guarantees the EVA session won't be deleted. Use of session
  825. * functions, such as session_start requires the session to be valid.
  826. */
  827. rc = cvp_fence_thread_start(inst);
  828. if (rc)
  829. goto restore_state;
  830. /* Send SESSION_START command */
  831. rc = call_hfi_op(hdev, session_start, (void *)inst->session);
  832. if (rc) {
  833. dprintk(CVP_WARN, "%s: session start failed rc %d\n",
  834. __func__, rc);
  835. goto stop_thread;
  836. }
  837. /* Wait for FW response */
  838. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_START_DONE);
  839. if (rc) {
  840. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  841. __func__, rc);
  842. goto stop_thread;
  843. }
  844. dprintk(CVP_SESS, "session %llx (%#x) started\n", inst, hash32_ptr(inst->session));
  845. return 0;
  846. stop_thread:
  847. cvp_fence_thread_stop(inst);
  848. restore_state:
  849. spin_lock(&sq->lock);
  850. sq->state = old_state;
  851. spin_unlock(&sq->lock);
  852. exit:
  853. return rc;
  854. }
  855. int msm_cvp_session_stop(struct msm_cvp_inst *inst,
  856. struct eva_kmd_arg *arg)
  857. {
  858. struct cvp_session_queue *sq;
  859. struct eva_kmd_session_control *sc = NULL;
  860. struct msm_cvp_inst *s;
  861. struct cvp_hfi_device *hdev;
  862. int rc;
  863. if (!inst || !inst->core) {
  864. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  865. return -EINVAL;
  866. }
  867. if (arg)
  868. sc = &arg->data.session_ctrl;
  869. s = cvp_get_inst_validate(inst->core, inst);
  870. if (!s)
  871. return -ECONNRESET;
  872. sq = &inst->session_queue;
  873. spin_lock(&sq->lock);
  874. if (sq->msg_count) {
  875. dprintk(CVP_ERR, "session stop incorrect: queue not empty%d\n",
  876. sq->msg_count);
  877. if (sc)
  878. sc->ctrl_data[0] = sq->msg_count;
  879. spin_unlock(&sq->lock);
  880. rc = -EUCLEAN;
  881. goto exit;
  882. }
  883. sq->state = QUEUE_STOP;
  884. dprintk(CVP_SESS, "Stop session: %pK session_id = %d\n",
  885. inst, hash32_ptr(inst->session));
  886. spin_unlock(&sq->lock);
  887. hdev = inst->core->device;
  888. /* Send SESSION_STOP command */
  889. rc = call_hfi_op(hdev, session_stop, (void *)inst->session);
  890. if (rc) {
  891. dprintk(CVP_WARN, "%s: session stop failed rc %d\n",
  892. __func__, rc);
  893. goto stop_thread;
  894. }
  895. /* Wait for FW response */
  896. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_STOP_DONE);
  897. if (rc) {
  898. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  899. __func__, rc);
  900. goto stop_thread;
  901. }
  902. stop_thread:
  903. wake_up_all(&inst->session_queue.wq);
  904. cvp_fence_thread_stop(inst);
  905. exit:
  906. cvp_put_inst(s);
  907. return rc;
  908. }
  909. int msm_cvp_session_queue_stop(struct msm_cvp_inst *inst)
  910. {
  911. struct cvp_session_queue *sq;
  912. sq = &inst->session_queue;
  913. spin_lock(&sq->lock);
  914. if (sq->state == QUEUE_STOP) {
  915. spin_unlock(&sq->lock);
  916. return 0;
  917. }
  918. sq->state = QUEUE_STOP;
  919. dprintk(CVP_SESS, "Stop session queue: %pK session_id = %d\n",
  920. inst, hash32_ptr(inst->session));
  921. spin_unlock(&sq->lock);
  922. wake_up_all(&inst->session_queue.wq);
  923. return cvp_fence_thread_stop(inst);
  924. }
  925. static int msm_cvp_session_ctrl(struct msm_cvp_inst *inst,
  926. struct eva_kmd_arg *arg)
  927. {
  928. struct eva_kmd_session_control *ctrl = &arg->data.session_ctrl;
  929. int rc = 0;
  930. unsigned int ctrl_type;
  931. ctrl_type = ctrl->ctrl_type;
  932. if (!inst && ctrl_type != SESSION_CREATE) {
  933. dprintk(CVP_ERR, "%s invalid session\n", __func__);
  934. return -EINVAL;
  935. }
  936. switch (ctrl_type) {
  937. case SESSION_STOP:
  938. rc = msm_cvp_session_stop(inst, arg);
  939. break;
  940. case SESSION_START:
  941. rc = msm_cvp_session_start(inst, arg);
  942. break;
  943. case SESSION_CREATE:
  944. rc = msm_cvp_session_create(inst);
  945. break;
  946. case SESSION_DELETE:
  947. rc = msm_cvp_session_delete(inst);
  948. break;
  949. case SESSION_INFO:
  950. default:
  951. dprintk(CVP_ERR, "%s Unsupported session ctrl%d\n",
  952. __func__, ctrl->ctrl_type);
  953. rc = -EINVAL;
  954. }
  955. return rc;
  956. }
  957. static int msm_cvp_get_sysprop(struct msm_cvp_inst *inst,
  958. struct eva_kmd_arg *arg)
  959. {
  960. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  961. struct cvp_hfi_device *hdev;
  962. struct iris_hfi_device *hfi;
  963. struct cvp_session_prop *session_prop;
  964. int i, rc = 0;
  965. if (!inst || !inst->core || !inst->core->device) {
  966. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  967. return -EINVAL;
  968. }
  969. hdev = inst->core->device;
  970. hfi = hdev->hfi_device_data;
  971. if (props->prop_num > MAX_KMD_PROP_NUM_PER_PACKET) {
  972. dprintk(CVP_ERR, "Too many properties %d to get\n",
  973. props->prop_num);
  974. return -E2BIG;
  975. }
  976. session_prop = &inst->prop;
  977. for (i = 0; i < props->prop_num; i++) {
  978. switch (props->prop_data[i].prop_type) {
  979. case EVA_KMD_PROP_HFI_VERSION:
  980. {
  981. props->prop_data[i].data = hfi->version;
  982. break;
  983. }
  984. case EVA_KMD_PROP_SESSION_DUMPOFFSET:
  985. {
  986. props->prop_data[i].data =
  987. session_prop->dump_offset;
  988. break;
  989. }
  990. case EVA_KMD_PROP_SESSION_DUMPSIZE:
  991. {
  992. props->prop_data[i].data =
  993. session_prop->dump_size;
  994. break;
  995. }
  996. case EVA_KMD_PROP_PWR_FDU:
  997. {
  998. props->prop_data[i].data =
  999. msm_cvp_get_hw_aggregate_cycles(HFI_HW_FDU);
  1000. break;
  1001. }
  1002. case EVA_KMD_PROP_PWR_ICA:
  1003. {
  1004. props->prop_data[i].data =
  1005. msm_cvp_get_hw_aggregate_cycles(HFI_HW_ICA);
  1006. break;
  1007. }
  1008. case EVA_KMD_PROP_PWR_OD:
  1009. {
  1010. props->prop_data[i].data =
  1011. msm_cvp_get_hw_aggregate_cycles(HFI_HW_OD);
  1012. break;
  1013. }
  1014. case EVA_KMD_PROP_PWR_MPU:
  1015. {
  1016. props->prop_data[i].data =
  1017. msm_cvp_get_hw_aggregate_cycles(HFI_HW_MPU);
  1018. break;
  1019. }
  1020. case EVA_KMD_PROP_PWR_VADL:
  1021. {
  1022. props->prop_data[i].data =
  1023. msm_cvp_get_hw_aggregate_cycles(HFI_HW_VADL);
  1024. break;
  1025. }
  1026. case EVA_KMD_PROP_PWR_TOF:
  1027. {
  1028. props->prop_data[i].data =
  1029. msm_cvp_get_hw_aggregate_cycles(HFI_HW_TOF);
  1030. break;
  1031. }
  1032. case EVA_KMD_PROP_PWR_RGE:
  1033. {
  1034. props->prop_data[i].data =
  1035. msm_cvp_get_hw_aggregate_cycles(HFI_HW_RGE);
  1036. break;
  1037. }
  1038. case EVA_KMD_PROP_PWR_XRA:
  1039. {
  1040. props->prop_data[i].data =
  1041. msm_cvp_get_hw_aggregate_cycles(HFI_HW_XRA);
  1042. break;
  1043. }
  1044. case EVA_KMD_PROP_PWR_LSR:
  1045. {
  1046. props->prop_data[i].data =
  1047. msm_cvp_get_hw_aggregate_cycles(HFI_HW_LSR);
  1048. break;
  1049. }
  1050. default:
  1051. dprintk(CVP_ERR, "unrecognized sys property %d\n",
  1052. props->prop_data[i].prop_type);
  1053. rc = -EFAULT;
  1054. }
  1055. }
  1056. return rc;
  1057. }
  1058. static int msm_cvp_set_sysprop(struct msm_cvp_inst *inst,
  1059. struct eva_kmd_arg *arg)
  1060. {
  1061. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  1062. struct eva_kmd_sys_property *prop_array;
  1063. struct cvp_session_prop *session_prop;
  1064. int i, rc = 0;
  1065. if (!inst) {
  1066. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1067. return -EINVAL;
  1068. }
  1069. if (props->prop_num > MAX_KMD_PROP_NUM_PER_PACKET) {
  1070. dprintk(CVP_ERR, "Too many properties %d to set\n",
  1071. props->prop_num);
  1072. return -E2BIG;
  1073. }
  1074. prop_array = &arg->data.sys_properties.prop_data[0];
  1075. session_prop = &inst->prop;
  1076. for (i = 0; i < props->prop_num; i++) {
  1077. switch (prop_array[i].prop_type) {
  1078. case EVA_KMD_PROP_SESSION_TYPE:
  1079. session_prop->type = prop_array[i].data;
  1080. break;
  1081. case EVA_KMD_PROP_SESSION_KERNELMASK:
  1082. session_prop->kernel_mask = prop_array[i].data;
  1083. break;
  1084. case EVA_KMD_PROP_SESSION_PRIORITY:
  1085. session_prop->priority = prop_array[i].data;
  1086. break;
  1087. case EVA_KMD_PROP_SESSION_SECURITY:
  1088. session_prop->is_secure = prop_array[i].data;
  1089. break;
  1090. case EVA_KMD_PROP_SESSION_DSPMASK:
  1091. session_prop->dsp_mask = prop_array[i].data;
  1092. break;
  1093. case EVA_KMD_PROP_PWR_FDU:
  1094. session_prop->cycles[HFI_HW_FDU] = prop_array[i].data;
  1095. break;
  1096. case EVA_KMD_PROP_PWR_ICA:
  1097. session_prop->cycles[HFI_HW_ICA] =
  1098. div_by_1dot5(prop_array[i].data);
  1099. break;
  1100. case EVA_KMD_PROP_PWR_OD:
  1101. session_prop->cycles[HFI_HW_OD] = prop_array[i].data;
  1102. break;
  1103. case EVA_KMD_PROP_PWR_MPU:
  1104. session_prop->cycles[HFI_HW_MPU] = prop_array[i].data;
  1105. break;
  1106. case EVA_KMD_PROP_PWR_VADL:
  1107. session_prop->cycles[HFI_HW_VADL] = prop_array[i].data;
  1108. break;
  1109. case EVA_KMD_PROP_PWR_TOF:
  1110. session_prop->cycles[HFI_HW_TOF] = prop_array[i].data;
  1111. break;
  1112. case EVA_KMD_PROP_PWR_RGE:
  1113. session_prop->cycles[HFI_HW_RGE] = prop_array[i].data;
  1114. break;
  1115. case EVA_KMD_PROP_PWR_XRA:
  1116. session_prop->cycles[HFI_HW_XRA] = prop_array[i].data;
  1117. break;
  1118. case EVA_KMD_PROP_PWR_LSR:
  1119. session_prop->cycles[HFI_HW_LSR] = prop_array[i].data;
  1120. break;
  1121. case EVA_KMD_PROP_PWR_FW:
  1122. session_prop->fw_cycles =
  1123. div_by_1dot5(prop_array[i].data);
  1124. break;
  1125. case EVA_KMD_PROP_PWR_DDR:
  1126. session_prop->ddr_bw = prop_array[i].data;
  1127. break;
  1128. case EVA_KMD_PROP_PWR_SYSCACHE:
  1129. session_prop->ddr_cache = prop_array[i].data;
  1130. break;
  1131. case EVA_KMD_PROP_PWR_FDU_OP:
  1132. session_prop->op_cycles[HFI_HW_FDU] = prop_array[i].data;
  1133. break;
  1134. case EVA_KMD_PROP_PWR_ICA_OP:
  1135. session_prop->op_cycles[HFI_HW_ICA] =
  1136. div_by_1dot5(prop_array[i].data);
  1137. break;
  1138. case EVA_KMD_PROP_PWR_OD_OP:
  1139. session_prop->op_cycles[HFI_HW_OD] = prop_array[i].data;
  1140. break;
  1141. case EVA_KMD_PROP_PWR_MPU_OP:
  1142. session_prop->op_cycles[HFI_HW_MPU] = prop_array[i].data;
  1143. break;
  1144. case EVA_KMD_PROP_PWR_VADL_OP:
  1145. session_prop->op_cycles[HFI_HW_VADL] = prop_array[i].data;
  1146. break;
  1147. case EVA_KMD_PROP_PWR_TOF_OP:
  1148. session_prop->op_cycles[HFI_HW_TOF] = prop_array[i].data;
  1149. break;
  1150. case EVA_KMD_PROP_PWR_RGE_OP:
  1151. session_prop->op_cycles[HFI_HW_RGE] = prop_array[i].data;
  1152. break;
  1153. case EVA_KMD_PROP_PWR_XRA_OP:
  1154. session_prop->op_cycles[HFI_HW_XRA] = prop_array[i].data;
  1155. break;
  1156. case EVA_KMD_PROP_PWR_LSR_OP:
  1157. session_prop->op_cycles[HFI_HW_LSR] = prop_array[i].data;
  1158. break;
  1159. case EVA_KMD_PROP_PWR_FW_OP:
  1160. session_prop->fw_op_cycles =
  1161. div_by_1dot5(prop_array[i].data);
  1162. break;
  1163. case EVA_KMD_PROP_PWR_DDR_OP:
  1164. session_prop->ddr_op_bw = prop_array[i].data;
  1165. break;
  1166. case EVA_KMD_PROP_PWR_SYSCACHE_OP:
  1167. session_prop->ddr_op_cache = prop_array[i].data;
  1168. break;
  1169. case EVA_KMD_PROP_PWR_FPS_FDU:
  1170. session_prop->fps[HFI_HW_FDU] = prop_array[i].data;
  1171. break;
  1172. case EVA_KMD_PROP_PWR_FPS_MPU:
  1173. session_prop->fps[HFI_HW_MPU] = prop_array[i].data;
  1174. break;
  1175. case EVA_KMD_PROP_PWR_FPS_OD:
  1176. session_prop->fps[HFI_HW_OD] = prop_array[i].data;
  1177. break;
  1178. case EVA_KMD_PROP_PWR_FPS_ICA:
  1179. session_prop->fps[HFI_HW_ICA] = prop_array[i].data;
  1180. break;
  1181. case EVA_KMD_PROP_PWR_FPS_VADL:
  1182. session_prop->fps[HFI_HW_VADL] = prop_array[i].data;
  1183. break;
  1184. case EVA_KMD_PROP_PWR_FPS_TOF:
  1185. session_prop->fps[HFI_HW_TOF] = prop_array[i].data;
  1186. break;
  1187. case EVA_KMD_PROP_PWR_FPS_RGE:
  1188. session_prop->fps[HFI_HW_RGE] = prop_array[i].data;
  1189. break;
  1190. case EVA_KMD_PROP_PWR_FPS_XRA:
  1191. session_prop->fps[HFI_HW_XRA] = prop_array[i].data;
  1192. break;
  1193. case EVA_KMD_PROP_PWR_FPS_LSR:
  1194. session_prop->fps[HFI_HW_LSR] = prop_array[i].data;
  1195. break;
  1196. case EVA_KMD_PROP_SESSION_DUMPOFFSET:
  1197. session_prop->dump_offset = prop_array[i].data;
  1198. break;
  1199. case EVA_KMD_PROP_SESSION_DUMPSIZE:
  1200. session_prop->dump_size = prop_array[i].data;
  1201. break;
  1202. default:
  1203. dprintk(CVP_ERR,
  1204. "unrecognized sys property to set %d\n",
  1205. prop_array[i].prop_type);
  1206. rc = -EFAULT;
  1207. }
  1208. }
  1209. return rc;
  1210. }
  1211. static int cvp_drain_fence_sched_list(struct msm_cvp_inst *inst)
  1212. {
  1213. unsigned long wait_time;
  1214. struct cvp_fence_queue *q;
  1215. struct cvp_fence_command *f;
  1216. int rc = 0;
  1217. int count = 0, max_count = 0;
  1218. u64 ktid;
  1219. q = &inst->fence_cmd_queue;
  1220. if (!q)
  1221. return -EINVAL;
  1222. f = list_first_entry(&q->sched_list,
  1223. struct cvp_fence_command,
  1224. list);
  1225. if (!f)
  1226. return rc;
  1227. mutex_lock(&q->lock);
  1228. list_for_each_entry(f, &q->sched_list, list) {
  1229. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1230. dprintk(CVP_SYNX, "%s: frame %llu %llu is in sched_list\n",
  1231. __func__, ktid, f->frame_id);
  1232. ++count;
  1233. }
  1234. mutex_unlock(&q->lock);
  1235. wait_time = count * 1000;
  1236. wait_time *= inst->core->resources.msm_cvp_hw_rsp_timeout;
  1237. dprintk(CVP_SYNX, "%s: wait %d us for %d fence command\n",
  1238. __func__, wait_time, count);
  1239. count = 0;
  1240. max_count = wait_time / 100;
  1241. retry:
  1242. mutex_lock(&q->lock);
  1243. if (list_empty(&q->sched_list)) {
  1244. mutex_unlock(&q->lock);
  1245. return rc;
  1246. }
  1247. mutex_unlock(&q->lock);
  1248. usleep_range(100, 200);
  1249. ++count;
  1250. if (count < max_count) {
  1251. goto retry;
  1252. } else {
  1253. rc = -ETIMEDOUT;
  1254. dprintk(CVP_ERR, "%s: timed out!\n", __func__);
  1255. }
  1256. return rc;
  1257. }
  1258. static void cvp_clean_fence_queue(struct msm_cvp_inst *inst, int synx_state)
  1259. {
  1260. struct cvp_fence_queue *q;
  1261. struct cvp_fence_command *f, *d;
  1262. u64 ktid;
  1263. q = &inst->fence_cmd_queue;
  1264. if (!q)
  1265. return;
  1266. mutex_lock(&q->lock);
  1267. q->mode = OP_DRAINING;
  1268. f = list_first_entry(&q->wait_list,
  1269. struct cvp_fence_command,
  1270. list);
  1271. if (!f)
  1272. goto check_sched;
  1273. list_for_each_entry_safe(f, d, &q->wait_list, list) {
  1274. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1275. dprintk(CVP_SYNX, "%s: (%#x) flush frame %llu %llu wait_list\n",
  1276. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1277. list_del_init(&f->list);
  1278. msm_cvp_unmap_frame(inst, f->pkt->client_data.kdata);
  1279. inst->core->synx_ftbl->cvp_cancel_synx(inst, CVP_OUTPUT_SYNX,
  1280. f, synx_state);
  1281. inst->core->synx_ftbl->cvp_release_synx(inst, f);
  1282. cvp_free_fence_data(f);
  1283. }
  1284. check_sched:
  1285. f = list_first_entry(&q->sched_list,
  1286. struct cvp_fence_command,
  1287. list);
  1288. if (!f) {
  1289. mutex_unlock(&q->lock);
  1290. return;
  1291. }
  1292. list_for_each_entry(f, &q->sched_list, list) {
  1293. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1294. dprintk(CVP_SYNX, "%s: (%#x)flush frame %llu %llu sched_list\n",
  1295. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1296. inst->core->synx_ftbl->cvp_cancel_synx(inst, CVP_INPUT_SYNX,
  1297. f, synx_state);
  1298. }
  1299. mutex_unlock(&q->lock);
  1300. }
  1301. int cvp_clean_session_queues(struct msm_cvp_inst *inst)
  1302. {
  1303. struct cvp_fence_queue *q;
  1304. struct cvp_session_queue *sq;
  1305. u32 count = 0, max_retries = 100;
  1306. q = &inst->fence_cmd_queue;
  1307. mutex_lock(&q->lock);
  1308. if (q->state == QUEUE_START) {
  1309. mutex_unlock(&q->lock);
  1310. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_CANCEL);
  1311. } else {
  1312. dprintk(CVP_WARN, "Incorrect fence cmd queue state %d\n",
  1313. q->state);
  1314. mutex_unlock(&q->lock);
  1315. }
  1316. cvp_fence_thread_stop(inst);
  1317. /* Waiting for all output synx sent */
  1318. retry:
  1319. mutex_lock(&q->lock);
  1320. if (list_empty(&q->sched_list)) {
  1321. mutex_unlock(&q->lock);
  1322. return 0;
  1323. }
  1324. mutex_unlock(&q->lock);
  1325. usleep_range(500, 1000);
  1326. if (++count > max_retries)
  1327. return -EBUSY;
  1328. goto retry;
  1329. sq = &inst->session_queue_fence;
  1330. spin_lock(&sq->lock);
  1331. sq->state = QUEUE_INVALID;
  1332. spin_unlock(&sq->lock);
  1333. }
  1334. static int cvp_flush_all(struct msm_cvp_inst *inst)
  1335. {
  1336. int rc = 0;
  1337. struct msm_cvp_inst *s;
  1338. struct cvp_fence_queue *q;
  1339. struct cvp_hfi_device *hdev;
  1340. if (!inst || !inst->core) {
  1341. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1342. return -EINVAL;
  1343. }
  1344. s = cvp_get_inst_validate(inst->core, inst);
  1345. if (!s)
  1346. return -ECONNRESET;
  1347. dprintk(CVP_SESS, "session %llx (%#x)flush all starts\n",
  1348. inst, hash32_ptr(inst->session));
  1349. q = &inst->fence_cmd_queue;
  1350. hdev = inst->core->device;
  1351. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_CANCEL);
  1352. dprintk(CVP_SESS, "%s: (%#x) send flush to fw\n",
  1353. __func__, hash32_ptr(inst->session));
  1354. /* Send flush to FW */
  1355. rc = call_hfi_op(hdev, session_flush, (void *)inst->session);
  1356. if (rc) {
  1357. dprintk(CVP_WARN, "%s: continue flush without fw. rc %d\n",
  1358. __func__, rc);
  1359. goto exit;
  1360. }
  1361. /* Wait for FW response */
  1362. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_FLUSH_DONE);
  1363. if (rc)
  1364. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  1365. __func__, rc);
  1366. dprintk(CVP_SESS, "%s: (%#x) received flush from fw\n",
  1367. __func__, hash32_ptr(inst->session));
  1368. exit:
  1369. rc = cvp_drain_fence_sched_list(inst);
  1370. mutex_lock(&q->lock);
  1371. q->mode = OP_NORMAL;
  1372. mutex_unlock(&q->lock);
  1373. cvp_put_inst(s);
  1374. return rc;
  1375. }
  1376. int msm_cvp_handle_syscall(struct msm_cvp_inst *inst, struct eva_kmd_arg *arg)
  1377. {
  1378. int rc = 0;
  1379. if (!inst || !arg) {
  1380. dprintk(CVP_ERR, "%s: invalid args\n", __func__);
  1381. return -EINVAL;
  1382. }
  1383. dprintk(CVP_HFI, "%s: arg->type = %x", __func__, arg->type);
  1384. if (arg->type != EVA_KMD_SESSION_CONTROL &&
  1385. arg->type != EVA_KMD_SET_SYS_PROPERTY &&
  1386. arg->type != EVA_KMD_GET_SYS_PROPERTY) {
  1387. rc = session_state_check_init(inst);
  1388. if (rc) {
  1389. dprintk(CVP_ERR,
  1390. "Incorrect session state %d for command %#x",
  1391. inst->state, arg->type);
  1392. return rc;
  1393. }
  1394. }
  1395. switch (arg->type) {
  1396. case EVA_KMD_GET_SESSION_INFO:
  1397. {
  1398. struct eva_kmd_session_info *session =
  1399. (struct eva_kmd_session_info *)&arg->data.session;
  1400. rc = msm_cvp_get_session_info(inst, &session->session_id);
  1401. break;
  1402. }
  1403. case EVA_KMD_UPDATE_POWER:
  1404. {
  1405. rc = msm_cvp_update_power(inst);
  1406. break;
  1407. }
  1408. case EVA_KMD_REGISTER_BUFFER:
  1409. {
  1410. struct eva_kmd_buffer *buf =
  1411. (struct eva_kmd_buffer *)&arg->data.regbuf;
  1412. rc = msm_cvp_register_buffer(inst, buf);
  1413. break;
  1414. }
  1415. case EVA_KMD_UNREGISTER_BUFFER:
  1416. {
  1417. struct eva_kmd_buffer *buf =
  1418. (struct eva_kmd_buffer *)&arg->data.unregbuf;
  1419. rc = msm_cvp_unregister_buffer(inst, buf);
  1420. break;
  1421. }
  1422. case EVA_KMD_RECEIVE_MSG_PKT:
  1423. {
  1424. struct eva_kmd_hfi_packet *out_pkt =
  1425. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1426. rc = msm_cvp_session_receive_hfi(inst, out_pkt);
  1427. break;
  1428. }
  1429. case EVA_KMD_SEND_CMD_PKT:
  1430. {
  1431. struct eva_kmd_hfi_packet *in_pkt =
  1432. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1433. rc = msm_cvp_session_process_hfi(inst, in_pkt,
  1434. arg->buf_offset, arg->buf_num);
  1435. break;
  1436. }
  1437. case EVA_KMD_SEND_FENCE_CMD_PKT:
  1438. {
  1439. rc = msm_cvp_session_process_hfi_fence(inst, arg);
  1440. break;
  1441. }
  1442. case EVA_KMD_SESSION_CONTROL:
  1443. rc = msm_cvp_session_ctrl(inst, arg);
  1444. break;
  1445. case EVA_KMD_GET_SYS_PROPERTY:
  1446. rc = msm_cvp_get_sysprop(inst, arg);
  1447. break;
  1448. case EVA_KMD_SET_SYS_PROPERTY:
  1449. rc = msm_cvp_set_sysprop(inst, arg);
  1450. break;
  1451. case EVA_KMD_FLUSH_ALL:
  1452. rc = cvp_flush_all(inst);
  1453. break;
  1454. case EVA_KMD_FLUSH_FRAME:
  1455. dprintk(CVP_WARN, "EVA_KMD_FLUSH_FRAME IOCTL deprecated\n");
  1456. rc = 0;
  1457. break;
  1458. default:
  1459. dprintk(CVP_HFI, "%s: unknown arg type %#x\n",
  1460. __func__, arg->type);
  1461. rc = -ENOTSUPP;
  1462. break;
  1463. }
  1464. return rc;
  1465. }
  1466. int msm_cvp_session_deinit(struct msm_cvp_inst *inst)
  1467. {
  1468. int rc = 0;
  1469. struct cvp_hal_session *session;
  1470. if (!inst || !inst->core) {
  1471. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1472. return -EINVAL;
  1473. }
  1474. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1475. inst, hash32_ptr(inst->session));
  1476. session = (struct cvp_hal_session *)inst->session;
  1477. if (!session)
  1478. return rc;
  1479. rc = msm_cvp_comm_try_state(inst, MSM_CVP_CLOSE_DONE);
  1480. if (rc)
  1481. dprintk(CVP_ERR, "%s: close failed\n", __func__);
  1482. rc = msm_cvp_session_deinit_buffers(inst);
  1483. return rc;
  1484. }
  1485. int msm_cvp_session_init(struct msm_cvp_inst *inst)
  1486. {
  1487. int rc = 0;
  1488. if (!inst) {
  1489. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1490. return -EINVAL;
  1491. }
  1492. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1493. inst, hash32_ptr(inst->session));
  1494. /* set default frequency */
  1495. inst->clk_data.core_id = 0;
  1496. inst->clk_data.min_freq = 1000;
  1497. inst->clk_data.ddr_bw = 1000;
  1498. inst->clk_data.sys_cache_bw = 1000;
  1499. inst->prop.type = 1;
  1500. inst->prop.kernel_mask = 0xFFFFFFFF;
  1501. inst->prop.priority = 0;
  1502. inst->prop.is_secure = 0;
  1503. inst->prop.dsp_mask = 0;
  1504. inst->prop.fthread_nr = 3;
  1505. return rc;
  1506. }