msm_cvp.c 38 KB

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