msm_cvp.c 47 KB

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