msm_cvp.c 47 KB

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