msm_cvp.c 41 KB

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