msm_cvp.c 45 KB

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