msm_cvp.c 38 KB

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