msm_cvp.c 46 KB

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