msm_cvp.c 40 KB

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