msm_cvp.c 46 KB

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