msm_cvp.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, 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. static int msm_cvp_get_session_info(struct msm_cvp_inst *inst,
  15. struct eva_kmd_session_info *session)
  16. {
  17. int rc = 0;
  18. struct msm_cvp_inst *s;
  19. if (!inst || !inst->core || !session) {
  20. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  21. return -EINVAL;
  22. }
  23. s = cvp_get_inst_validate(inst->core, inst);
  24. if (!s)
  25. return -ECONNRESET;
  26. s->cur_cmd_type = EVA_KMD_GET_SESSION_INFO;
  27. session->session_id = hash32_ptr(inst->session);
  28. dprintk(CVP_SESS, "%s: id 0x%x\n", __func__, session->session_id);
  29. s->cur_cmd_type = 0;
  30. cvp_put_inst(s);
  31. return rc;
  32. }
  33. static bool cvp_msg_pending(struct cvp_session_queue *sq,
  34. struct cvp_session_msg **msg, u64 *ktid)
  35. {
  36. struct cvp_session_msg *mptr, *dummy;
  37. bool result = false;
  38. mptr = NULL;
  39. spin_lock(&sq->lock);
  40. if (sq->state != QUEUE_ACTIVE) {
  41. /* The session is being deleted */
  42. spin_unlock(&sq->lock);
  43. *msg = NULL;
  44. return true;
  45. }
  46. result = list_empty(&sq->msgs);
  47. if (!result) {
  48. if (!ktid) {
  49. mptr =
  50. list_first_entry(&sq->msgs, struct cvp_session_msg,
  51. node);
  52. list_del_init(&mptr->node);
  53. sq->msg_count--;
  54. } else {
  55. result = true;
  56. list_for_each_entry_safe(mptr, dummy, &sq->msgs, node) {
  57. if (*ktid == mptr->pkt.client_data.kdata) {
  58. list_del_init(&mptr->node);
  59. sq->msg_count--;
  60. result = false;
  61. break;
  62. }
  63. }
  64. if (result)
  65. mptr = NULL;
  66. }
  67. }
  68. spin_unlock(&sq->lock);
  69. *msg = mptr;
  70. return !result;
  71. }
  72. static int cvp_wait_process_message(struct msm_cvp_inst *inst,
  73. struct cvp_session_queue *sq, u64 *ktid,
  74. unsigned long timeout,
  75. struct eva_kmd_hfi_packet *out)
  76. {
  77. struct cvp_session_msg *msg = NULL;
  78. struct cvp_hfi_msg_session_hdr *hdr;
  79. int rc = 0;
  80. if (wait_event_timeout(sq->wq,
  81. cvp_msg_pending(sq, &msg, ktid), timeout) == 0) {
  82. dprintk(CVP_WARN, "session queue wait timeout\n");
  83. rc = -ETIMEDOUT;
  84. goto exit;
  85. }
  86. if (msg == NULL) {
  87. dprintk(CVP_WARN, "%s: queue state %d, msg cnt %d\n", __func__,
  88. sq->state, sq->msg_count);
  89. if (inst->state >= MSM_CVP_CLOSE_DONE ||
  90. sq->state != QUEUE_ACTIVE) {
  91. rc = -ECONNRESET;
  92. goto exit;
  93. }
  94. msm_cvp_comm_kill_session(inst);
  95. goto exit;
  96. }
  97. if (!out) {
  98. kmem_cache_free(cvp_driver->msg_cache, msg);
  99. goto exit;
  100. }
  101. hdr = (struct cvp_hfi_msg_session_hdr *)&msg->pkt;
  102. memcpy(out, &msg->pkt, get_msg_size(hdr));
  103. msm_cvp_unmap_frame(inst, hdr->client_data.kdata);
  104. kmem_cache_free(cvp_driver->msg_cache, msg);
  105. exit:
  106. return rc;
  107. }
  108. static int msm_cvp_session_receive_hfi(struct msm_cvp_inst *inst,
  109. struct eva_kmd_hfi_packet *out_pkt)
  110. {
  111. unsigned long wait_time;
  112. struct cvp_session_queue *sq;
  113. struct msm_cvp_inst *s;
  114. int rc = 0;
  115. if (!inst) {
  116. dprintk(CVP_ERR, "%s invalid session\n", __func__);
  117. return -EINVAL;
  118. }
  119. s = cvp_get_inst_validate(inst->core, inst);
  120. if (!s)
  121. return -ECONNRESET;
  122. s->cur_cmd_type = EVA_KMD_RECEIVE_MSG_PKT;
  123. wait_time = msecs_to_jiffies(CVP_MAX_WAIT_TIME);
  124. sq = &inst->session_queue;
  125. rc = cvp_wait_process_message(inst, sq, NULL, wait_time, out_pkt);
  126. s->cur_cmd_type = 0;
  127. cvp_put_inst(inst);
  128. return rc;
  129. }
  130. static int msm_cvp_session_process_hfi(
  131. struct msm_cvp_inst *inst,
  132. struct eva_kmd_hfi_packet *in_pkt,
  133. unsigned int in_offset,
  134. unsigned int in_buf_num)
  135. {
  136. int pkt_idx, pkt_type, rc = 0;
  137. struct cvp_hfi_device *hdev;
  138. unsigned int offset, buf_num, signal;
  139. struct cvp_session_queue *sq;
  140. struct msm_cvp_inst *s;
  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. inst->cur_cmd_type = EVA_KMD_SEND_CMD_PKT;
  149. hdev = inst->core->device;
  150. pkt_idx = get_pkt_index((struct cvp_hal_session_cmd_pkt *)in_pkt);
  151. if (pkt_idx < 0) {
  152. dprintk(CVP_ERR, "%s incorrect packet %d, %x\n", __func__,
  153. in_pkt->pkt_data[0],
  154. in_pkt->pkt_data[1]);
  155. goto exit;
  156. } else {
  157. offset = cvp_hfi_defs[pkt_idx].buf_offset;
  158. buf_num = cvp_hfi_defs[pkt_idx].buf_num;
  159. signal = cvp_hfi_defs[pkt_idx].resp;
  160. }
  161. if (signal == HAL_NO_RESP) {
  162. /* Frame packets are not allowed before session starts*/
  163. sq = &inst->session_queue;
  164. spin_lock(&sq->lock);
  165. if (sq->state != QUEUE_ACTIVE) {
  166. spin_unlock(&sq->lock);
  167. dprintk(CVP_ERR, "%s: invalid queue state\n", __func__);
  168. rc = -EINVAL;
  169. goto exit;
  170. }
  171. spin_unlock(&sq->lock);
  172. }
  173. if (in_offset && in_buf_num) {
  174. offset = in_offset;
  175. buf_num = in_buf_num;
  176. }
  177. if (!is_buf_param_valid(buf_num, offset)) {
  178. dprintk(CVP_ERR, "Incorrect buffer num and offset in cmd\n");
  179. return -EINVAL;
  180. }
  181. pkt_type = in_pkt->pkt_data[1];
  182. if (pkt_type == HFI_CMD_SESSION_CVP_SET_PERSIST_BUFFERS ||
  183. pkt_type == HFI_CMD_SESSION_CVP_SET_MODEL_BUFFERS ||
  184. pkt_type == HFI_CMD_SESSION_CVP_DMM_PARAMS ||
  185. pkt_type == HFI_CMD_SESSION_CVP_WARP_DS_PARAMS)
  186. rc = msm_cvp_map_user_persist(inst, in_pkt, offset, buf_num);
  187. else if (pkt_type == HFI_CMD_SESSION_CVP_RELEASE_PERSIST_BUFFERS)
  188. rc = msm_cvp_mark_user_persist(inst, in_pkt, offset, buf_num);
  189. else
  190. rc = msm_cvp_map_frame(inst, in_pkt, offset, buf_num);
  191. if (rc)
  192. goto exit;
  193. rc = call_hfi_op(hdev, session_send, (void *)inst->session, in_pkt);
  194. if (rc) {
  195. dprintk(CVP_ERR,
  196. "%s: Failed in call_hfi_op %d, %x\n",
  197. __func__, in_pkt->pkt_data[0], in_pkt->pkt_data[1]);
  198. goto exit;
  199. }
  200. if (signal != HAL_NO_RESP) {
  201. rc = wait_for_sess_signal_receipt(inst, signal);
  202. if (rc) {
  203. dprintk(CVP_ERR,
  204. "%s: wait for signal failed, rc %d %d, %x %d\n",
  205. __func__, rc,
  206. in_pkt->pkt_data[0],
  207. in_pkt->pkt_data[1],
  208. signal);
  209. goto exit;
  210. }
  211. if (pkt_type == HFI_CMD_SESSION_CVP_RELEASE_PERSIST_BUFFERS)
  212. rc = msm_cvp_unmap_user_persist(inst, in_pkt,
  213. offset, buf_num);
  214. }
  215. exit:
  216. inst->cur_cmd_type = 0;
  217. cvp_put_inst(inst);
  218. return rc;
  219. }
  220. static bool cvp_fence_wait(struct cvp_fence_queue *q,
  221. struct cvp_fence_command **fence,
  222. enum queue_state *state)
  223. {
  224. struct cvp_fence_command *f;
  225. *fence = NULL;
  226. mutex_lock(&q->lock);
  227. *state = q->state;
  228. if (*state != QUEUE_ACTIVE) {
  229. mutex_unlock(&q->lock);
  230. return true;
  231. }
  232. if (list_empty(&q->wait_list)) {
  233. mutex_unlock(&q->lock);
  234. return false;
  235. }
  236. f = list_first_entry(&q->wait_list, struct cvp_fence_command, list);
  237. list_del_init(&f->list);
  238. list_add_tail(&f->list, &q->sched_list);
  239. mutex_unlock(&q->lock);
  240. *fence = f;
  241. return true;
  242. }
  243. static int cvp_readjust_clock(struct msm_cvp_core *core,
  244. u32 avg_cycles, enum hfi_hw_thread i)
  245. {
  246. int rc = 0;
  247. struct allowed_clock_rates_table *tbl = NULL;
  248. unsigned int tbl_size = 0;
  249. unsigned int cvp_min_rate = 0, cvp_max_rate = 0;
  250. unsigned long tmp = core->curr_freq;
  251. unsigned long lo_freq = 0;
  252. u32 j;
  253. dprintk(CVP_PWR,
  254. "%s:%d - %d - avg_cycles %u > hi_tresh %u\n",
  255. __func__, __LINE__, i, avg_cycles,
  256. core->dyn_clk.hi_ctrl_lim[i]);
  257. core->curr_freq = ((avg_cycles * core->dyn_clk.sum_fps[i]) << 1)/3;
  258. dprintk(CVP_PWR,
  259. "%s - cycles tot %u, avg %u. sum_fps %u, cur_freq %u\n",
  260. __func__,
  261. core->dyn_clk.cycle[i].total,
  262. avg_cycles,
  263. core->dyn_clk.sum_fps[i],
  264. core->curr_freq);
  265. tbl = core->resources.allowed_clks_tbl;
  266. tbl_size = core->resources.allowed_clks_tbl_size;
  267. cvp_min_rate = tbl[0].clock_rate;
  268. cvp_max_rate = tbl[tbl_size - 1].clock_rate;
  269. if (core->curr_freq > cvp_max_rate) {
  270. core->curr_freq = cvp_max_rate;
  271. lo_freq = (tbl_size > 1) ?
  272. tbl[tbl_size - 2].clock_rate :
  273. cvp_min_rate;
  274. } else if (core->curr_freq <= cvp_min_rate) {
  275. core->curr_freq = cvp_min_rate;
  276. lo_freq = cvp_min_rate;
  277. } else {
  278. for (j = 1; j < tbl_size; j++)
  279. if (core->curr_freq <= tbl[j].clock_rate)
  280. break;
  281. core->curr_freq = tbl[j].clock_rate;
  282. lo_freq = tbl[j-1].clock_rate;
  283. }
  284. dprintk(CVP_PWR,
  285. "%s:%d - %d - Readjust to %u\n",
  286. __func__, __LINE__, i, core->curr_freq);
  287. rc = msm_cvp_set_clocks(core);
  288. if (rc) {
  289. dprintk(CVP_ERR,
  290. "Failed to set clock rate %u: %d %s\n",
  291. core->curr_freq, rc, __func__);
  292. core->curr_freq = tmp;
  293. } else {
  294. lo_freq = (lo_freq < core->dyn_clk.conf_freq) ?
  295. core->dyn_clk.conf_freq : lo_freq;
  296. core->dyn_clk.hi_ctrl_lim[i] = core->dyn_clk.sum_fps[i] ?
  297. ((core->curr_freq*3)>>1)/core->dyn_clk.sum_fps[i] : 0;
  298. core->dyn_clk.lo_ctrl_lim[i] =
  299. core->dyn_clk.sum_fps[i] ?
  300. ((lo_freq*3)>>1)/core->dyn_clk.sum_fps[i] : 0;
  301. dprintk(CVP_PWR,
  302. "%s - Readjust clk to %u. New lim [%d] hi %u lo %u\n",
  303. __func__, core->curr_freq, i,
  304. core->dyn_clk.hi_ctrl_lim[i],
  305. core->dyn_clk.lo_ctrl_lim[i]);
  306. }
  307. return rc;
  308. }
  309. static int cvp_check_clock(struct msm_cvp_inst *inst,
  310. struct cvp_hfi_msg_session_hdr_ext *hdr)
  311. {
  312. int rc = 0;
  313. u32 i, j;
  314. u32 hw_cycles[HFI_MAX_HW_THREADS] = {0};
  315. u32 fw_cycles = 0;
  316. struct msm_cvp_core *core = inst->core;
  317. for (i = 0; i < HFI_MAX_HW_ACTIVATIONS_PER_FRAME; ++i)
  318. fw_cycles += hdr->fw_cycles[i];
  319. for (i = 0; i < HFI_MAX_HW_THREADS; ++i)
  320. for (j = 0; j < HFI_MAX_HW_ACTIVATIONS_PER_FRAME; ++j)
  321. hw_cycles[i] += hdr->hw_cycles[i][j];
  322. dprintk(CVP_PWR, "%s - cycles fw %u. FDU %d MPU %d ODU %d ICA %d\n",
  323. __func__, fw_cycles, hw_cycles[0],
  324. hw_cycles[1], hw_cycles[2], hw_cycles[3]);
  325. mutex_lock(&core->clk_lock);
  326. for (i = 0; i < HFI_MAX_HW_THREADS; ++i) {
  327. dprintk(CVP_PWR, "%s - %d: hw_cycles %u, tens_thresh %u\n",
  328. __func__, i, hw_cycles[i],
  329. core->dyn_clk.hi_ctrl_lim[i]);
  330. if (core->dyn_clk.hi_ctrl_lim[i]) {
  331. if (core->dyn_clk.cycle[i].size < CVP_CYCLE_STAT_SIZE)
  332. core->dyn_clk.cycle[i].size++;
  333. else
  334. core->dyn_clk.cycle[i].total -=
  335. core->dyn_clk.cycle[i].busy[
  336. core->dyn_clk.cycle[i].idx];
  337. if (hw_cycles[i]) {
  338. core->dyn_clk.cycle[i].busy[
  339. core->dyn_clk.cycle[i].idx]
  340. = hw_cycles[i] + fw_cycles;
  341. core->dyn_clk.cycle[i].total
  342. += hw_cycles[i] + fw_cycles;
  343. dprintk(CVP_PWR,
  344. "%s: busy (hw + fw) cycles = %u\n",
  345. __func__,
  346. core->dyn_clk.cycle[i].busy[
  347. core->dyn_clk.cycle[i].idx]);
  348. dprintk(CVP_PWR, "total cycles %u\n",
  349. core->dyn_clk.cycle[i].total);
  350. } else {
  351. core->dyn_clk.cycle[i].busy[
  352. core->dyn_clk.cycle[i].idx] =
  353. hdr->busy_cycles;
  354. core->dyn_clk.cycle[i].total +=
  355. hdr->busy_cycles;
  356. dprintk(CVP_PWR,
  357. "%s - busy cycles = %u total %u\n",
  358. __func__,
  359. core->dyn_clk.cycle[i].busy[
  360. core->dyn_clk.cycle[i].idx],
  361. core->dyn_clk.cycle[i].total);
  362. }
  363. core->dyn_clk.cycle[i].idx =
  364. (core->dyn_clk.cycle[i].idx ==
  365. CVP_CYCLE_STAT_SIZE-1) ?
  366. 0 : core->dyn_clk.cycle[i].idx+1;
  367. dprintk(CVP_PWR, "%s - %d: size %u, tens_thresh %u\n",
  368. __func__, i, core->dyn_clk.cycle[i].size,
  369. core->dyn_clk.hi_ctrl_lim[i]);
  370. if (core->dyn_clk.cycle[i].size == CVP_CYCLE_STAT_SIZE
  371. && core->dyn_clk.hi_ctrl_lim[i] != 0) {
  372. u32 avg_cycles =
  373. core->dyn_clk.cycle[i].total>>3;
  374. if ((avg_cycles > core->dyn_clk.hi_ctrl_lim[i])
  375. || (avg_cycles <=
  376. core->dyn_clk.lo_ctrl_lim[i])) {
  377. rc = cvp_readjust_clock(core,
  378. avg_cycles,
  379. i);
  380. }
  381. }
  382. }
  383. }
  384. mutex_unlock(&core->clk_lock);
  385. return rc;
  386. }
  387. static int cvp_fence_proc(struct msm_cvp_inst *inst,
  388. struct cvp_fence_command *fc,
  389. struct cvp_hfi_cmd_session_hdr *pkt)
  390. {
  391. int rc = 0;
  392. unsigned long timeout;
  393. u64 ktid;
  394. int synx_state = SYNX_STATE_SIGNALED_SUCCESS;
  395. struct cvp_hfi_device *hdev;
  396. struct cvp_session_queue *sq;
  397. u32 hfi_err = HFI_ERR_NONE;
  398. struct cvp_hfi_msg_session_hdr_ext hdr;
  399. bool clock_check = false;
  400. dprintk(CVP_SYNX, "%s %s\n", current->comm, __func__);
  401. hdev = inst->core->device;
  402. sq = &inst->session_queue_fence;
  403. ktid = pkt->client_data.kdata;
  404. rc = cvp_synx_ops(inst, CVP_INPUT_SYNX, fc, &synx_state);
  405. if (rc) {
  406. msm_cvp_unmap_frame(inst, pkt->client_data.kdata);
  407. goto exit;
  408. }
  409. rc = call_hfi_op(hdev, session_send, (void *)inst->session,
  410. (struct eva_kmd_hfi_packet *)pkt);
  411. if (rc) {
  412. dprintk(CVP_ERR, "%s %s: Failed in call_hfi_op %d, %x\n",
  413. current->comm, __func__, pkt->size, pkt->packet_type);
  414. synx_state = SYNX_STATE_SIGNALED_ERROR;
  415. goto exit;
  416. }
  417. timeout = msecs_to_jiffies(CVP_MAX_WAIT_TIME);
  418. rc = cvp_wait_process_message(inst, sq, &ktid, timeout,
  419. (struct eva_kmd_hfi_packet *)&hdr);
  420. if (get_msg_size((struct cvp_hfi_msg_session_hdr *) &hdr)
  421. == sizeof(struct cvp_hfi_msg_session_hdr_ext)) {
  422. struct cvp_hfi_msg_session_hdr_ext *fhdr =
  423. (struct cvp_hfi_msg_session_hdr_ext *)&hdr;
  424. dprintk(CVP_HFI, "busy cycle 0x%x, total 0x%x\n",
  425. fhdr->busy_cycles, fhdr->total_cycles);
  426. clock_check = true;
  427. }
  428. hfi_err = hdr.error_type;
  429. if (rc) {
  430. dprintk(CVP_ERR, "%s %s: cvp_wait_process_message rc %d\n",
  431. current->comm, __func__, rc);
  432. synx_state = SYNX_STATE_SIGNALED_ERROR;
  433. goto exit;
  434. }
  435. if (hfi_err == HFI_ERR_SESSION_FLUSHED) {
  436. dprintk(CVP_SYNX, "%s %s: cvp_wait_process_message flushed\n",
  437. current->comm, __func__);
  438. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  439. } else if (hfi_err == HFI_ERR_SESSION_STREAM_CORRUPT) {
  440. dprintk(CVP_WARN, "%s %s: cvp_wait_process_msg non-fatal %d\n",
  441. current->comm, __func__, hfi_err);
  442. synx_state = SYNX_STATE_SIGNALED_SUCCESS;
  443. } else if (hfi_err != HFI_ERR_NONE) {
  444. dprintk(CVP_ERR, "%s %s: cvp_wait_process_message hfi err %d\n",
  445. current->comm, __func__, hfi_err);
  446. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  447. }
  448. exit:
  449. rc = cvp_synx_ops(inst, CVP_OUTPUT_SYNX, fc, &synx_state);
  450. if (clock_check)
  451. cvp_check_clock(inst,
  452. (struct cvp_hfi_msg_session_hdr_ext *)&hdr);
  453. return rc;
  454. }
  455. static int cvp_alloc_fence_data(struct cvp_fence_command **f, u32 size)
  456. {
  457. struct cvp_fence_command *fcmd;
  458. int alloc_size = sizeof(struct cvp_hfi_msg_session_hdr_ext);
  459. fcmd = kzalloc(sizeof(struct cvp_fence_command), GFP_KERNEL);
  460. if (!fcmd)
  461. return -ENOMEM;
  462. alloc_size = (alloc_size >= size) ? alloc_size : size;
  463. fcmd->pkt = kzalloc(alloc_size, GFP_KERNEL);
  464. if (!fcmd->pkt) {
  465. kfree(fcmd);
  466. return -ENOMEM;
  467. }
  468. *f = fcmd;
  469. return 0;
  470. }
  471. static void cvp_free_fence_data(struct cvp_fence_command *f)
  472. {
  473. kfree(f->pkt);
  474. f->pkt = NULL;
  475. kfree(f);
  476. f = NULL;
  477. }
  478. static int cvp_fence_thread(void *data)
  479. {
  480. int rc = 0;
  481. struct msm_cvp_inst *inst;
  482. struct cvp_fence_queue *q;
  483. enum queue_state state;
  484. struct cvp_fence_command *f;
  485. struct cvp_hfi_cmd_session_hdr *pkt;
  486. u32 *synx;
  487. u64 ktid;
  488. dprintk(CVP_SYNX, "Enter %s\n", current->comm);
  489. inst = (struct msm_cvp_inst *)data;
  490. if (!inst || !inst->core || !inst->core->device) {
  491. dprintk(CVP_ERR, "%s invalid inst %pK\n", current->comm, inst);
  492. rc = -EINVAL;
  493. goto exit;
  494. }
  495. q = &inst->fence_cmd_queue;
  496. wait:
  497. dprintk(CVP_SYNX, "%s starts wait\n", current->comm);
  498. f = NULL;
  499. wait_event_interruptible(q->wq, cvp_fence_wait(q, &f, &state));
  500. if (state != QUEUE_ACTIVE)
  501. goto exit;
  502. if (!f)
  503. goto wait;
  504. pkt = f->pkt;
  505. synx = (u32 *)f->synx;
  506. ktid = pkt->client_data.kdata & (FENCE_BIT - 1);
  507. dprintk(CVP_SYNX, "%s pkt type %d on ktid %llu frameID %llu\n",
  508. current->comm, pkt->packet_type, ktid, f->frame_id);
  509. rc = cvp_fence_proc(inst, f, pkt);
  510. mutex_lock(&q->lock);
  511. cvp_release_synx(inst, f);
  512. list_del_init(&f->list);
  513. state = q->state;
  514. mutex_unlock(&q->lock);
  515. dprintk(CVP_SYNX, "%s done with %d ktid %llu frameID %llu rc %d\n",
  516. current->comm, pkt->packet_type, ktid, f->frame_id, rc);
  517. cvp_free_fence_data(f);
  518. if (rc && state != QUEUE_ACTIVE)
  519. goto exit;
  520. goto wait;
  521. exit:
  522. dprintk(CVP_SYNX, "%s exit\n", current->comm);
  523. cvp_put_inst(inst);
  524. do_exit(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. static 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. static int msm_cvp_register_buffer(struct msm_cvp_inst *inst,
  852. struct eva_kmd_buffer *buf)
  853. {
  854. struct cvp_hfi_device *hdev;
  855. struct cvp_hal_session *session;
  856. struct msm_cvp_inst *s;
  857. int rc = 0;
  858. if (!inst || !inst->core || !buf) {
  859. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  860. return -EINVAL;
  861. }
  862. if (!buf->index)
  863. return 0;
  864. s = cvp_get_inst_validate(inst->core, inst);
  865. if (!s)
  866. return -ECONNRESET;
  867. inst->cur_cmd_type = EVA_KMD_REGISTER_BUFFER;
  868. session = (struct cvp_hal_session *)inst->session;
  869. if (!session) {
  870. dprintk(CVP_ERR, "%s: invalid session\n", __func__);
  871. rc = -EINVAL;
  872. goto exit;
  873. }
  874. hdev = inst->core->device;
  875. print_client_buffer(CVP_HFI, "register", inst, buf);
  876. rc = msm_cvp_map_buf_dsp(inst, buf);
  877. exit:
  878. inst->cur_cmd_type = 0;
  879. cvp_put_inst(s);
  880. return rc;
  881. }
  882. static int msm_cvp_unregister_buffer(struct msm_cvp_inst *inst,
  883. struct eva_kmd_buffer *buf)
  884. {
  885. struct msm_cvp_inst *s;
  886. int rc = 0;
  887. if (!inst || !inst->core || !buf) {
  888. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  889. return -EINVAL;
  890. }
  891. if (!buf->index)
  892. return 0;
  893. s = cvp_get_inst_validate(inst->core, inst);
  894. if (!s)
  895. return -ECONNRESET;
  896. inst->cur_cmd_type = EVA_KMD_UNREGISTER_BUFFER;
  897. print_client_buffer(CVP_HFI, "unregister", inst, buf);
  898. rc = msm_cvp_unmap_buf_dsp(inst, buf);
  899. inst->cur_cmd_type = 0;
  900. cvp_put_inst(s);
  901. return rc;
  902. }
  903. static int msm_cvp_session_create(struct msm_cvp_inst *inst)
  904. {
  905. int rc = 0;
  906. struct synx_initialization_params params;
  907. if (!inst || !inst->core)
  908. return -EINVAL;
  909. if (inst->state >= MSM_CVP_CLOSE_DONE)
  910. return -ECONNRESET;
  911. if (inst->state != MSM_CVP_CORE_INIT_DONE ||
  912. inst->state > MSM_CVP_OPEN_DONE) {
  913. dprintk(CVP_ERR,
  914. "%s Incorrect CVP state %d to create session\n",
  915. __func__, inst->state);
  916. return -EINVAL;
  917. }
  918. rc = msm_cvp_comm_try_state(inst, MSM_CVP_OPEN_DONE);
  919. if (rc) {
  920. dprintk(CVP_ERR,
  921. "Failed to move instance to open done state\n");
  922. goto fail_init;
  923. }
  924. rc = cvp_comm_set_arp_buffers(inst);
  925. if (rc) {
  926. dprintk(CVP_ERR,
  927. "Failed to set ARP buffers\n");
  928. goto fail_init;
  929. }
  930. params.name = "cvp-kernel-client";
  931. if (synx_initialize(&inst->synx_session_id, &params)) {
  932. dprintk(CVP_ERR, "%s synx_initialize failed\n", __func__);
  933. rc = -EFAULT;
  934. }
  935. fail_init:
  936. return rc;
  937. }
  938. static int session_state_check_init(struct msm_cvp_inst *inst)
  939. {
  940. mutex_lock(&inst->lock);
  941. if (inst->state == MSM_CVP_OPEN || inst->state == MSM_CVP_OPEN_DONE) {
  942. mutex_unlock(&inst->lock);
  943. return 0;
  944. }
  945. mutex_unlock(&inst->lock);
  946. return msm_cvp_session_create(inst);
  947. }
  948. static int cvp_fence_thread_start(struct msm_cvp_inst *inst)
  949. {
  950. u32 tnum = 0;
  951. u32 i = 0;
  952. int rc = 0;
  953. char tname[16];
  954. struct task_struct *thread;
  955. struct cvp_fence_queue *q;
  956. struct cvp_session_queue *sq;
  957. if (!inst->prop.fthread_nr)
  958. return 0;
  959. q = &inst->fence_cmd_queue;
  960. mutex_lock(&q->lock);
  961. q->state = QUEUE_ACTIVE;
  962. mutex_unlock(&q->lock);
  963. for (i = 0; i < inst->prop.fthread_nr; ++i) {
  964. if (!cvp_get_inst_validate(inst->core, inst)) {
  965. rc = -ECONNRESET;
  966. goto exit;
  967. }
  968. snprintf(tname, sizeof(tname), "fthread_%d", tnum++);
  969. thread = kthread_run(cvp_fence_thread, inst, tname);
  970. if (!thread) {
  971. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  972. rc = -ECHILD;
  973. goto exit;
  974. }
  975. }
  976. sq = &inst->session_queue_fence;
  977. spin_lock(&sq->lock);
  978. sq->state = QUEUE_ACTIVE;
  979. spin_unlock(&sq->lock);
  980. exit:
  981. if (rc) {
  982. mutex_lock(&q->lock);
  983. q->state = QUEUE_STOP;
  984. mutex_unlock(&q->lock);
  985. wake_up_all(&q->wq);
  986. }
  987. return rc;
  988. }
  989. static int cvp_fence_thread_stop(struct msm_cvp_inst *inst)
  990. {
  991. struct cvp_fence_queue *q;
  992. struct cvp_session_queue *sq;
  993. if (!inst->prop.fthread_nr)
  994. return 0;
  995. q = &inst->fence_cmd_queue;
  996. mutex_lock(&q->lock);
  997. q->state = QUEUE_STOP;
  998. mutex_unlock(&q->lock);
  999. sq = &inst->session_queue_fence;
  1000. spin_lock(&sq->lock);
  1001. sq->state = QUEUE_STOP;
  1002. spin_unlock(&sq->lock);
  1003. wake_up_all(&q->wq);
  1004. wake_up_all(&sq->wq);
  1005. return 0;
  1006. }
  1007. static int msm_cvp_session_start(struct msm_cvp_inst *inst,
  1008. struct eva_kmd_arg *arg)
  1009. {
  1010. struct cvp_session_queue *sq;
  1011. sq = &inst->session_queue;
  1012. spin_lock(&sq->lock);
  1013. if (sq->msg_count) {
  1014. dprintk(CVP_ERR, "session start failed queue not empty%d\n",
  1015. sq->msg_count);
  1016. spin_unlock(&sq->lock);
  1017. return -EINVAL;
  1018. }
  1019. sq->state = QUEUE_ACTIVE;
  1020. spin_unlock(&sq->lock);
  1021. return cvp_fence_thread_start(inst);
  1022. }
  1023. static int msm_cvp_session_stop(struct msm_cvp_inst *inst,
  1024. struct eva_kmd_arg *arg)
  1025. {
  1026. struct cvp_session_queue *sq;
  1027. struct eva_kmd_session_control *sc = &arg->data.session_ctrl;
  1028. sq = &inst->session_queue;
  1029. spin_lock(&sq->lock);
  1030. if (sq->msg_count) {
  1031. dprintk(CVP_ERR, "session stop incorrect: queue not empty%d\n",
  1032. sq->msg_count);
  1033. sc->ctrl_data[0] = sq->msg_count;
  1034. spin_unlock(&sq->lock);
  1035. return -EUCLEAN;
  1036. }
  1037. sq->state = QUEUE_STOP;
  1038. pr_info(CVP_DBG_TAG "Stop session: %pK session_id = %d\n",
  1039. "sess", inst, hash32_ptr(inst->session));
  1040. spin_unlock(&sq->lock);
  1041. wake_up_all(&inst->session_queue.wq);
  1042. return cvp_fence_thread_stop(inst);
  1043. }
  1044. int msm_cvp_session_queue_stop(struct msm_cvp_inst *inst)
  1045. {
  1046. struct cvp_session_queue *sq;
  1047. sq = &inst->session_queue;
  1048. spin_lock(&sq->lock);
  1049. if (sq->state == QUEUE_STOP) {
  1050. spin_unlock(&sq->lock);
  1051. return 0;
  1052. }
  1053. sq->state = QUEUE_STOP;
  1054. dprintk(CVP_SESS, "Stop session queue: %pK session_id = %d\n",
  1055. inst, hash32_ptr(inst->session));
  1056. spin_unlock(&sq->lock);
  1057. wake_up_all(&inst->session_queue.wq);
  1058. return cvp_fence_thread_stop(inst);
  1059. }
  1060. static int msm_cvp_session_ctrl(struct msm_cvp_inst *inst,
  1061. struct eva_kmd_arg *arg)
  1062. {
  1063. struct eva_kmd_session_control *ctrl = &arg->data.session_ctrl;
  1064. int rc = 0;
  1065. unsigned int ctrl_type;
  1066. ctrl_type = ctrl->ctrl_type;
  1067. if (!inst && ctrl_type != SESSION_CREATE) {
  1068. dprintk(CVP_ERR, "%s invalid session\n", __func__);
  1069. return -EINVAL;
  1070. }
  1071. switch (ctrl_type) {
  1072. case SESSION_STOP:
  1073. rc = msm_cvp_session_stop(inst, arg);
  1074. break;
  1075. case SESSION_START:
  1076. rc = msm_cvp_session_start(inst, arg);
  1077. break;
  1078. case SESSION_CREATE:
  1079. rc = msm_cvp_session_create(inst);
  1080. case SESSION_DELETE:
  1081. break;
  1082. case SESSION_INFO:
  1083. default:
  1084. dprintk(CVP_ERR, "%s Unsupported session ctrl%d\n",
  1085. __func__, ctrl->ctrl_type);
  1086. rc = -EINVAL;
  1087. }
  1088. return rc;
  1089. }
  1090. static int msm_cvp_get_sysprop(struct msm_cvp_inst *inst,
  1091. struct eva_kmd_arg *arg)
  1092. {
  1093. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  1094. struct cvp_hfi_device *hdev;
  1095. struct iris_hfi_device *hfi;
  1096. int i, rc = 0;
  1097. if (!inst || !inst->core || !inst->core->device) {
  1098. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1099. return -EINVAL;
  1100. }
  1101. hdev = inst->core->device;
  1102. hfi = hdev->hfi_device_data;
  1103. for (i = 0; i < props->prop_num; i++) {
  1104. switch (props->prop_data[i].prop_type) {
  1105. case EVA_KMD_PROP_HFI_VERSION:
  1106. {
  1107. props->prop_data[i].data = hfi->version;
  1108. break;
  1109. }
  1110. default:
  1111. dprintk(CVP_ERR, "unrecognized sys property %d\n",
  1112. props->prop_data[i].prop_type);
  1113. rc = -EFAULT;
  1114. }
  1115. }
  1116. return rc;
  1117. }
  1118. static int msm_cvp_set_sysprop(struct msm_cvp_inst *inst,
  1119. struct eva_kmd_arg *arg)
  1120. {
  1121. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  1122. struct eva_kmd_sys_property *prop_array;
  1123. struct cvp_session_prop *session_prop;
  1124. int i, rc = 0;
  1125. if (!inst) {
  1126. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1127. return -EINVAL;
  1128. }
  1129. if (props->prop_num > MAX_KMD_PROP_NUM_PER_PACKET) {
  1130. dprintk(CVP_ERR, "Too many properties %d to set\n",
  1131. props->prop_num);
  1132. return -E2BIG;
  1133. }
  1134. prop_array = &arg->data.sys_properties.prop_data[0];
  1135. session_prop = &inst->prop;
  1136. for (i = 0; i < props->prop_num; i++) {
  1137. switch (prop_array[i].prop_type) {
  1138. case EVA_KMD_PROP_SESSION_TYPE:
  1139. session_prop->type = prop_array[i].data;
  1140. break;
  1141. case EVA_KMD_PROP_SESSION_KERNELMASK:
  1142. session_prop->kernel_mask = prop_array[i].data;
  1143. break;
  1144. case EVA_KMD_PROP_SESSION_PRIORITY:
  1145. session_prop->priority = prop_array[i].data;
  1146. break;
  1147. case EVA_KMD_PROP_SESSION_SECURITY:
  1148. session_prop->is_secure = prop_array[i].data;
  1149. break;
  1150. case EVA_KMD_PROP_SESSION_DSPMASK:
  1151. session_prop->dsp_mask = prop_array[i].data;
  1152. break;
  1153. case EVA_KMD_PROP_PWR_FDU:
  1154. session_prop->fdu_cycles = prop_array[i].data;
  1155. break;
  1156. case EVA_KMD_PROP_PWR_ICA:
  1157. session_prop->ica_cycles =
  1158. div_by_1dot5(prop_array[i].data);
  1159. break;
  1160. case EVA_KMD_PROP_PWR_OD:
  1161. session_prop->od_cycles = prop_array[i].data;
  1162. break;
  1163. case EVA_KMD_PROP_PWR_MPU:
  1164. session_prop->mpu_cycles = prop_array[i].data;
  1165. break;
  1166. case EVA_KMD_PROP_PWR_FW:
  1167. session_prop->fw_cycles =
  1168. div_by_1dot5(prop_array[i].data);
  1169. break;
  1170. case EVA_KMD_PROP_PWR_DDR:
  1171. session_prop->ddr_bw = prop_array[i].data;
  1172. break;
  1173. case EVA_KMD_PROP_PWR_SYSCACHE:
  1174. session_prop->ddr_cache = prop_array[i].data;
  1175. break;
  1176. case EVA_KMD_PROP_PWR_FDU_OP:
  1177. session_prop->fdu_op_cycles = prop_array[i].data;
  1178. break;
  1179. case EVA_KMD_PROP_PWR_ICA_OP:
  1180. session_prop->ica_op_cycles =
  1181. div_by_1dot5(prop_array[i].data);
  1182. break;
  1183. case EVA_KMD_PROP_PWR_OD_OP:
  1184. session_prop->od_op_cycles = prop_array[i].data;
  1185. break;
  1186. case EVA_KMD_PROP_PWR_MPU_OP:
  1187. session_prop->mpu_op_cycles = prop_array[i].data;
  1188. break;
  1189. case EVA_KMD_PROP_PWR_FW_OP:
  1190. session_prop->fw_op_cycles =
  1191. div_by_1dot5(prop_array[i].data);
  1192. break;
  1193. case EVA_KMD_PROP_PWR_DDR_OP:
  1194. session_prop->ddr_op_bw = prop_array[i].data;
  1195. break;
  1196. case EVA_KMD_PROP_PWR_SYSCACHE_OP:
  1197. session_prop->ddr_op_cache = prop_array[i].data;
  1198. break;
  1199. case EVA_KMD_PROP_PWR_FPS_FDU:
  1200. session_prop->fps[HFI_HW_FDU] = prop_array[i].data;
  1201. break;
  1202. case EVA_KMD_PROP_PWR_FPS_MPU:
  1203. session_prop->fps[HFI_HW_MPU] = prop_array[i].data;
  1204. break;
  1205. case EVA_KMD_PROP_PWR_FPS_OD:
  1206. session_prop->fps[HFI_HW_OD] = prop_array[i].data;
  1207. break;
  1208. case EVA_KMD_PROP_PWR_FPS_ICA:
  1209. session_prop->fps[HFI_HW_ICA] = prop_array[i].data;
  1210. break;
  1211. default:
  1212. dprintk(CVP_ERR,
  1213. "unrecognized sys property to set %d\n",
  1214. prop_array[i].prop_type);
  1215. rc = -EFAULT;
  1216. }
  1217. }
  1218. return rc;
  1219. }
  1220. static int cvp_drain_fence_cmd_queue_partial(struct msm_cvp_inst *inst)
  1221. {
  1222. unsigned long wait_time;
  1223. struct cvp_fence_queue *q;
  1224. struct cvp_fence_command *f;
  1225. int rc = 0;
  1226. int count = 0, max_count = 0;
  1227. q = &inst->fence_cmd_queue;
  1228. mutex_lock(&q->lock);
  1229. list_for_each_entry(f, &q->sched_list, list) {
  1230. if (f->mode == OP_FLUSH)
  1231. continue;
  1232. ++count;
  1233. }
  1234. list_for_each_entry(f, &q->wait_list, list) {
  1235. if (f->mode == OP_FLUSH)
  1236. continue;
  1237. ++count;
  1238. }
  1239. mutex_unlock(&q->lock);
  1240. wait_time = count * CVP_MAX_WAIT_TIME * 1000;
  1241. dprintk(CVP_SYNX, "%s: wait %d us for %d fence command\n",
  1242. __func__, wait_time, count);
  1243. count = 0;
  1244. max_count = wait_time / 100;
  1245. retry:
  1246. mutex_lock(&q->lock);
  1247. f = list_first_entry(&q->sched_list, struct cvp_fence_command, list);
  1248. /* Wait for all normal frames to finish before return */
  1249. if ((f && f->mode == OP_FLUSH) ||
  1250. (list_empty(&q->sched_list) && list_empty(&q->wait_list))) {
  1251. mutex_unlock(&q->lock);
  1252. return rc;
  1253. }
  1254. mutex_unlock(&q->lock);
  1255. usleep_range(100, 200);
  1256. ++count;
  1257. if (count < max_count) {
  1258. goto retry;
  1259. } else {
  1260. rc = -ETIMEDOUT;
  1261. dprintk(CVP_ERR, "%s: timed out!\n", __func__);
  1262. }
  1263. return rc;
  1264. }
  1265. static int cvp_drain_fence_sched_list(struct msm_cvp_inst *inst)
  1266. {
  1267. unsigned long wait_time;
  1268. struct cvp_fence_queue *q;
  1269. struct cvp_fence_command *f;
  1270. int rc = 0;
  1271. int count = 0, max_count = 0;
  1272. u64 ktid;
  1273. q = &inst->fence_cmd_queue;
  1274. mutex_lock(&q->lock);
  1275. list_for_each_entry(f, &q->sched_list, list) {
  1276. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1277. dprintk(CVP_SYNX, "%s: frame %llu %llu is in sched_list\n",
  1278. __func__, ktid, f->frame_id);
  1279. ++count;
  1280. }
  1281. mutex_unlock(&q->lock);
  1282. wait_time = count * CVP_MAX_WAIT_TIME * 1000;
  1283. dprintk(CVP_SYNX, "%s: wait %d us for %d fence command\n",
  1284. __func__, wait_time, count);
  1285. count = 0;
  1286. max_count = wait_time / 100;
  1287. retry:
  1288. mutex_lock(&q->lock);
  1289. if (list_empty(&q->sched_list)) {
  1290. mutex_unlock(&q->lock);
  1291. return rc;
  1292. }
  1293. mutex_unlock(&q->lock);
  1294. usleep_range(100, 200);
  1295. ++count;
  1296. if (count < max_count) {
  1297. goto retry;
  1298. } else {
  1299. rc = -ETIMEDOUT;
  1300. dprintk(CVP_ERR, "%s: timed out!\n", __func__);
  1301. }
  1302. return rc;
  1303. }
  1304. static void cvp_clean_fence_queue(struct msm_cvp_inst *inst, int synx_state)
  1305. {
  1306. struct cvp_fence_queue *q;
  1307. struct cvp_fence_command *f, *d;
  1308. u64 ktid;
  1309. q = &inst->fence_cmd_queue;
  1310. mutex_lock(&q->lock);
  1311. q->mode = OP_DRAINING;
  1312. list_for_each_entry_safe(f, d, &q->wait_list, list) {
  1313. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1314. dprintk(CVP_SYNX, "%s: (%#x) flush frame %llu %llu wait_list\n",
  1315. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1316. list_del_init(&f->list);
  1317. msm_cvp_unmap_frame(inst, f->pkt->client_data.kdata);
  1318. cvp_cancel_synx(inst, CVP_OUTPUT_SYNX, f, synx_state);
  1319. cvp_release_synx(inst, f);
  1320. cvp_free_fence_data(f);
  1321. }
  1322. list_for_each_entry(f, &q->sched_list, list) {
  1323. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1324. dprintk(CVP_SYNX, "%s: (%#x)flush frame %llu %llu sched_list\n",
  1325. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1326. cvp_cancel_synx(inst, CVP_INPUT_SYNX, f, synx_state);
  1327. }
  1328. mutex_unlock(&q->lock);
  1329. }
  1330. int cvp_stop_clean_fence_queue(struct msm_cvp_inst *inst)
  1331. {
  1332. struct cvp_fence_queue *q;
  1333. u32 count = 0, max_retries = 100;
  1334. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_ERROR);
  1335. cvp_fence_thread_stop(inst);
  1336. /* Waiting for all output synx sent */
  1337. q = &inst->fence_cmd_queue;
  1338. retry:
  1339. mutex_lock(&q->lock);
  1340. if (list_empty(&q->sched_list)) {
  1341. mutex_unlock(&q->lock);
  1342. return 0;
  1343. }
  1344. mutex_unlock(&q->lock);
  1345. usleep_range(500, 1000);
  1346. if (++count > max_retries)
  1347. return -EBUSY;
  1348. goto retry;
  1349. }
  1350. static int cvp_flush_all(struct msm_cvp_inst *inst)
  1351. {
  1352. int rc = 0;
  1353. struct msm_cvp_inst *s;
  1354. struct cvp_fence_queue *q;
  1355. struct cvp_hfi_device *hdev;
  1356. if (!inst || !inst->core) {
  1357. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1358. return -EINVAL;
  1359. }
  1360. s = cvp_get_inst_validate(inst->core, inst);
  1361. if (!s)
  1362. return -ECONNRESET;
  1363. dprintk(CVP_SESS, "session %llx (%#x)flush all starts\n",
  1364. inst, hash32_ptr(inst->session));
  1365. q = &inst->fence_cmd_queue;
  1366. hdev = inst->core->device;
  1367. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_CANCEL);
  1368. dprintk(CVP_SESS, "%s: (%#x) send flush to fw\n",
  1369. __func__, hash32_ptr(inst->session));
  1370. /* Send flush to FW */
  1371. rc = call_hfi_op(hdev, session_flush, (void *)inst->session);
  1372. if (rc) {
  1373. dprintk(CVP_WARN, "%s: continue flush without fw. rc %d\n",
  1374. __func__, rc);
  1375. goto exit;
  1376. }
  1377. /* Wait for FW response */
  1378. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_FLUSH_DONE);
  1379. if (rc)
  1380. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  1381. __func__, rc);
  1382. dprintk(CVP_SESS, "%s: (%#x) received flush from fw\n",
  1383. __func__, hash32_ptr(inst->session));
  1384. exit:
  1385. rc = cvp_drain_fence_sched_list(inst);
  1386. mutex_lock(&q->lock);
  1387. q->mode = OP_NORMAL;
  1388. mutex_unlock(&q->lock);
  1389. cvp_put_inst(s);
  1390. return rc;
  1391. }
  1392. static void cvp_mark_fence_command(struct msm_cvp_inst *inst, u64 frame_id)
  1393. {
  1394. int found = false;
  1395. struct cvp_fence_queue *q;
  1396. struct cvp_fence_command *f;
  1397. q = &inst->fence_cmd_queue;
  1398. list_for_each_entry(f, &q->sched_list, list) {
  1399. if (found) {
  1400. f->mode = OP_FLUSH;
  1401. continue;
  1402. }
  1403. if (f->frame_id >= frame_id) {
  1404. found = true;
  1405. f->mode = OP_FLUSH;
  1406. }
  1407. }
  1408. list_for_each_entry(f, &q->wait_list, list) {
  1409. if (found) {
  1410. f->mode = OP_FLUSH;
  1411. continue;
  1412. }
  1413. if (f->frame_id >= frame_id) {
  1414. found = true;
  1415. f->mode = OP_FLUSH;
  1416. }
  1417. }
  1418. }
  1419. static int cvp_flush_frame(struct msm_cvp_inst *inst, u64 frame_id)
  1420. {
  1421. int rc = 0;
  1422. struct msm_cvp_inst *s;
  1423. struct cvp_fence_queue *q;
  1424. struct cvp_fence_command *f, *d;
  1425. u64 ktid;
  1426. if (!inst || !inst->core) {
  1427. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1428. return -EINVAL;
  1429. }
  1430. s = cvp_get_inst_validate(inst->core, inst);
  1431. if (!s)
  1432. return -ECONNRESET;
  1433. dprintk(CVP_SESS, "Session %llx, flush frame with id %llu\n",
  1434. inst, frame_id);
  1435. q = &inst->fence_cmd_queue;
  1436. mutex_lock(&q->lock);
  1437. q->mode = OP_DRAINING;
  1438. cvp_mark_fence_command(inst, frame_id);
  1439. list_for_each_entry_safe(f, d, &q->wait_list, list) {
  1440. if (f->mode != OP_FLUSH)
  1441. continue;
  1442. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1443. dprintk(CVP_SYNX, "%s: flush frame %llu %llu from wait_list\n",
  1444. __func__, ktid, f->frame_id);
  1445. list_del_init(&f->list);
  1446. msm_cvp_unmap_frame(inst, f->pkt->client_data.kdata);
  1447. cvp_cancel_synx(inst, CVP_OUTPUT_SYNX, f,
  1448. SYNX_STATE_SIGNALED_CANCEL);
  1449. cvp_release_synx(inst, f);
  1450. cvp_free_fence_data(f);
  1451. }
  1452. list_for_each_entry(f, &q->sched_list, list) {
  1453. if (f->mode != OP_FLUSH)
  1454. continue;
  1455. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1456. dprintk(CVP_SYNX, "%s: flush frame %llu %llu from sched_list\n",
  1457. __func__, ktid, f->frame_id);
  1458. cvp_cancel_synx(inst, CVP_INPUT_SYNX, f,
  1459. SYNX_STATE_SIGNALED_CANCEL);
  1460. }
  1461. mutex_unlock(&q->lock);
  1462. rc = cvp_drain_fence_cmd_queue_partial(inst);
  1463. if (rc)
  1464. dprintk(CVP_WARN, "%s: continue flush. rc %d\n",
  1465. __func__, rc);
  1466. rc = cvp_flush_all(inst);
  1467. cvp_put_inst(s);
  1468. return rc;
  1469. }
  1470. int msm_cvp_handle_syscall(struct msm_cvp_inst *inst, struct eva_kmd_arg *arg)
  1471. {
  1472. int rc = 0;
  1473. if (!inst || !arg) {
  1474. dprintk(CVP_ERR, "%s: invalid args\n", __func__);
  1475. return -EINVAL;
  1476. }
  1477. dprintk(CVP_HFI, "%s: arg->type = %x", __func__, arg->type);
  1478. if (arg->type != EVA_KMD_SESSION_CONTROL &&
  1479. arg->type != EVA_KMD_SET_SYS_PROPERTY &&
  1480. arg->type != EVA_KMD_GET_SYS_PROPERTY) {
  1481. rc = session_state_check_init(inst);
  1482. if (rc) {
  1483. dprintk(CVP_ERR,
  1484. "Incorrect session state %d for command %#x",
  1485. inst->state, arg->type);
  1486. return rc;
  1487. }
  1488. }
  1489. switch (arg->type) {
  1490. case EVA_KMD_GET_SESSION_INFO:
  1491. {
  1492. struct eva_kmd_session_info *session =
  1493. (struct eva_kmd_session_info *)&arg->data.session;
  1494. rc = msm_cvp_get_session_info(inst, session);
  1495. break;
  1496. }
  1497. case EVA_KMD_UPDATE_POWER:
  1498. {
  1499. rc = msm_cvp_update_power(inst);
  1500. break;
  1501. }
  1502. case EVA_KMD_REGISTER_BUFFER:
  1503. {
  1504. struct eva_kmd_buffer *buf =
  1505. (struct eva_kmd_buffer *)&arg->data.regbuf;
  1506. rc = msm_cvp_register_buffer(inst, buf);
  1507. break;
  1508. }
  1509. case EVA_KMD_UNREGISTER_BUFFER:
  1510. {
  1511. struct eva_kmd_buffer *buf =
  1512. (struct eva_kmd_buffer *)&arg->data.unregbuf;
  1513. rc = msm_cvp_unregister_buffer(inst, buf);
  1514. break;
  1515. }
  1516. case EVA_KMD_RECEIVE_MSG_PKT:
  1517. {
  1518. struct eva_kmd_hfi_packet *out_pkt =
  1519. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1520. rc = msm_cvp_session_receive_hfi(inst, out_pkt);
  1521. break;
  1522. }
  1523. case EVA_KMD_SEND_CMD_PKT:
  1524. {
  1525. struct eva_kmd_hfi_packet *in_pkt =
  1526. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1527. rc = msm_cvp_session_process_hfi(inst, in_pkt,
  1528. arg->buf_offset, arg->buf_num);
  1529. break;
  1530. }
  1531. case EVA_KMD_SEND_FENCE_CMD_PKT:
  1532. {
  1533. rc = msm_cvp_session_process_hfi_fence(inst, arg);
  1534. break;
  1535. }
  1536. case EVA_KMD_SESSION_CONTROL:
  1537. rc = msm_cvp_session_ctrl(inst, arg);
  1538. break;
  1539. case EVA_KMD_GET_SYS_PROPERTY:
  1540. rc = msm_cvp_get_sysprop(inst, arg);
  1541. break;
  1542. case EVA_KMD_SET_SYS_PROPERTY:
  1543. rc = msm_cvp_set_sysprop(inst, arg);
  1544. break;
  1545. case EVA_KMD_FLUSH_ALL:
  1546. rc = cvp_flush_all(inst);
  1547. break;
  1548. case EVA_KMD_FLUSH_FRAME:
  1549. rc = cvp_flush_frame(inst, arg->data.frame_id);
  1550. break;
  1551. default:
  1552. dprintk(CVP_HFI, "%s: unknown arg type %#x\n",
  1553. __func__, arg->type);
  1554. rc = -ENOTSUPP;
  1555. break;
  1556. }
  1557. return rc;
  1558. }
  1559. int msm_cvp_session_deinit(struct msm_cvp_inst *inst)
  1560. {
  1561. int rc = 0;
  1562. struct cvp_hal_session *session;
  1563. if (!inst || !inst->core) {
  1564. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1565. return -EINVAL;
  1566. }
  1567. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1568. inst, hash32_ptr(inst->session));
  1569. session = (struct cvp_hal_session *)inst->session;
  1570. if (!session)
  1571. return rc;
  1572. rc = msm_cvp_comm_try_state(inst, MSM_CVP_CLOSE_DONE);
  1573. if (rc)
  1574. dprintk(CVP_ERR, "%s: close failed\n", __func__);
  1575. rc = msm_cvp_session_deinit_buffers(inst);
  1576. return rc;
  1577. }
  1578. int msm_cvp_session_init(struct msm_cvp_inst *inst)
  1579. {
  1580. int rc = 0;
  1581. if (!inst) {
  1582. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1583. return -EINVAL;
  1584. }
  1585. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1586. inst, hash32_ptr(inst->session));
  1587. /* set default frequency */
  1588. inst->clk_data.core_id = 0;
  1589. inst->clk_data.min_freq = 1000;
  1590. inst->clk_data.ddr_bw = 1000;
  1591. inst->clk_data.sys_cache_bw = 1000;
  1592. inst->prop.type = HFI_SESSION_CV;
  1593. if (inst->session_type == MSM_CVP_KERNEL)
  1594. inst->prop.type = HFI_SESSION_DMM;
  1595. inst->prop.kernel_mask = 0xFFFFFFFF;
  1596. inst->prop.priority = 0;
  1597. inst->prop.is_secure = 0;
  1598. inst->prop.dsp_mask = 0;
  1599. inst->prop.fthread_nr = 3;
  1600. return rc;
  1601. }