msm_cvp.c 46 KB

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