msm_cvp.c 40 KB

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