msm_cvp.c 45 KB

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