msm_cvp.c 48 KB

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