msm_cvp.c 46 KB

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