msm_cvp.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include "msm_cvp.h"
  7. #include "cvp_hfi.h"
  8. #include "cvp_core_hfi.h"
  9. #include "msm_cvp_buf.h"
  10. #include "cvp_comm_def.h"
  11. #include "cvp_power.h"
  12. #include "cvp_hfi_api.h"
  13. static int cvp_enqueue_pkt(struct msm_cvp_inst* inst,
  14. struct eva_kmd_hfi_packet *in_pkt,
  15. unsigned int in_offset,
  16. unsigned int in_buf_num);
  17. int msm_cvp_get_session_info(struct msm_cvp_inst *inst, u32 *session)
  18. {
  19. int rc = 0;
  20. struct msm_cvp_inst *s;
  21. if (!inst || !inst->core || !session) {
  22. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  23. return -EINVAL;
  24. }
  25. s = cvp_get_inst_validate(inst->core, inst);
  26. if (!s)
  27. return -ECONNRESET;
  28. *session = hash32_ptr(inst->session);
  29. dprintk(CVP_SESS, "%s: id 0x%x\n", __func__, *session);
  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 = NULL, *dummy;
  37. bool result = false;
  38. if (!sq)
  39. return false;
  40. spin_lock(&sq->lock);
  41. if (sq->state == QUEUE_INIT || sq->state == QUEUE_INVALID) {
  42. /* The session is being deleted */
  43. spin_unlock(&sq->lock);
  44. *msg = NULL;
  45. return true;
  46. }
  47. result = list_empty(&sq->msgs);
  48. if (!result) {
  49. mptr = list_first_entry(&sq->msgs,
  50. struct cvp_session_msg,
  51. node);
  52. if (!ktid) {
  53. if (mptr) {
  54. list_del_init(&mptr->node);
  55. sq->msg_count--;
  56. }
  57. } else {
  58. result = true;
  59. list_for_each_entry_safe(mptr, dummy, &sq->msgs, node) {
  60. if (*ktid == mptr->pkt.client_data.kdata) {
  61. list_del_init(&mptr->node);
  62. sq->msg_count--;
  63. result = false;
  64. break;
  65. }
  66. }
  67. if (result)
  68. mptr = NULL;
  69. }
  70. }
  71. spin_unlock(&sq->lock);
  72. *msg = mptr;
  73. return !result;
  74. }
  75. static int cvp_wait_process_message(struct msm_cvp_inst *inst,
  76. struct cvp_session_queue *sq, u64 *ktid,
  77. unsigned long timeout,
  78. struct eva_kmd_hfi_packet *out)
  79. {
  80. struct cvp_session_msg *msg = NULL;
  81. struct cvp_hfi_msg_session_hdr *hdr;
  82. int rc = 0;
  83. if (wait_event_timeout(sq->wq,
  84. cvp_msg_pending(sq, &msg, ktid), timeout) == 0) {
  85. dprintk(CVP_WARN, "session queue wait timeout\n");
  86. if (inst && inst->core && inst->core->dev_ops &&
  87. inst->state != MSM_CVP_CORE_INVALID)
  88. print_hfi_queue_info(inst->core->dev_ops);
  89. rc = -ETIMEDOUT;
  90. goto exit;
  91. }
  92. if (msg == NULL) {
  93. dprintk(CVP_WARN, "%s: queue state %d, msg cnt %d\n", __func__,
  94. sq->state, sq->msg_count);
  95. if (inst->state >= MSM_CVP_CLOSE_DONE ||
  96. (sq->state != QUEUE_ACTIVE &&
  97. sq->state != QUEUE_START)) {
  98. rc = -ECONNRESET;
  99. goto exit;
  100. }
  101. msm_cvp_comm_kill_session(inst);
  102. goto exit;
  103. }
  104. if (!out) {
  105. cvp_kmem_cache_free(&cvp_driver->msg_cache, msg);
  106. goto exit;
  107. }
  108. hdr = (struct cvp_hfi_msg_session_hdr *)&msg->pkt;
  109. memcpy(out, &msg->pkt, get_msg_size(hdr));
  110. if (hdr->client_data.kdata >= ARRAY_SIZE(cvp_hfi_defs))
  111. msm_cvp_unmap_frame(inst, hdr->client_data.kdata);
  112. cvp_kmem_cache_free(&cvp_driver->msg_cache, msg);
  113. exit:
  114. return rc;
  115. }
  116. static int msm_cvp_session_receive_hfi(struct msm_cvp_inst *inst,
  117. struct eva_kmd_hfi_packet *out_pkt)
  118. {
  119. unsigned long wait_time;
  120. struct cvp_session_queue *sq;
  121. struct msm_cvp_inst *s;
  122. int rc = 0;
  123. if (!inst) {
  124. dprintk(CVP_ERR, "%s invalid session\n", __func__);
  125. return -EINVAL;
  126. }
  127. s = cvp_get_inst_validate(inst->core, inst);
  128. if (!s)
  129. return -ECONNRESET;
  130. wait_time = msecs_to_jiffies(
  131. inst->core->resources.msm_cvp_hw_rsp_timeout);
  132. sq = &inst->session_queue;
  133. rc = cvp_wait_process_message(inst, sq, NULL, wait_time, out_pkt);
  134. cvp_put_inst(inst);
  135. return rc;
  136. }
  137. static int msm_cvp_session_process_hfi(
  138. struct msm_cvp_inst *inst,
  139. struct eva_kmd_hfi_packet *in_pkt,
  140. unsigned int in_offset,
  141. unsigned int in_buf_num)
  142. {
  143. int pkt_idx, rc = 0;
  144. unsigned int offset = 0, buf_num = 0, signal;
  145. struct cvp_session_queue *sq;
  146. struct msm_cvp_inst *s;
  147. struct cvp_hfi_cmd_session_hdr *pkt_hdr;
  148. bool is_config_pkt;
  149. if (!inst || !inst->core || !in_pkt) {
  150. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  151. return -EINVAL;
  152. }
  153. if (inst->state == MSM_CVP_CORE_INVALID) {
  154. dprintk(CVP_ERR, "sess %pK INVALIDim reject new HFIs\n", inst);
  155. return -ECONNRESET;
  156. }
  157. s = cvp_get_inst_validate(inst->core, inst);
  158. if (!s)
  159. return -ECONNRESET;
  160. pkt_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  161. dprintk(CVP_CMD, "%s: "
  162. "pkt_type %08x sess_id %08x trans_id %u ktid %llu\n",
  163. __func__, pkt_hdr->packet_type,
  164. pkt_hdr->session_id,
  165. pkt_hdr->client_data.transaction_id,
  166. pkt_hdr->client_data.kdata & (FENCE_BIT - 1));
  167. pkt_idx = get_pkt_index((struct cvp_hal_session_cmd_pkt *)in_pkt);
  168. if (pkt_idx < 0) {
  169. dprintk(CVP_ERR, "%s incorrect packet %d, %x\n", __func__,
  170. in_pkt->pkt_data[0],
  171. in_pkt->pkt_data[1]);
  172. goto exit;
  173. } else {
  174. signal = cvp_hfi_defs[pkt_idx].resp;
  175. is_config_pkt = cvp_hfi_defs[pkt_idx].is_config_pkt;
  176. }
  177. if (is_config_pkt)
  178. pr_info_ratelimited(CVP_DBG_TAG "inst %pK config %s\n", "sess",
  179. inst, cvp_hfi_defs[pkt_idx].name);
  180. if (signal == HAL_NO_RESP) {
  181. /* Frame packets are not allowed before session starts*/
  182. sq = &inst->session_queue;
  183. spin_lock(&sq->lock);
  184. if ((sq->state != QUEUE_START && !is_config_pkt) ||
  185. (sq->state >= QUEUE_INVALID)) {
  186. /*
  187. * A init packet is allowed in case of
  188. * QUEUE_ACTIVE, QUEUE_START, QUEUE_STOP
  189. * A frame packet is only allowed in case of
  190. * QUEUE_START
  191. */
  192. spin_unlock(&sq->lock);
  193. dprintk(CVP_ERR, "%s: invalid queue state %d\n",
  194. __func__, sq->state);
  195. rc = -EINVAL;
  196. goto exit;
  197. }
  198. spin_unlock(&sq->lock);
  199. }
  200. if (in_offset && in_buf_num) {
  201. offset = in_offset;
  202. buf_num = in_buf_num;
  203. }
  204. if (!is_buf_param_valid(buf_num, offset)) {
  205. dprintk(CVP_ERR, "Incorrect buffer num and offset in cmd\n");
  206. rc = -EINVAL;
  207. goto exit;
  208. }
  209. rc = msm_cvp_proc_oob(inst, in_pkt);
  210. if (rc) {
  211. dprintk(CVP_ERR, "%s: failed to process OOB buffer", __func__);
  212. goto exit;
  213. }
  214. rc = cvp_enqueue_pkt(inst, in_pkt, offset, buf_num);
  215. if (rc) {
  216. dprintk(CVP_ERR, "Failed to enqueue pkt, inst %pK "
  217. "pkt_type %08x ktid %llu transaction_id %u\n",
  218. inst, pkt_hdr->packet_type,
  219. pkt_hdr->client_data.kdata,
  220. pkt_hdr->client_data.transaction_id);
  221. }
  222. exit:
  223. cvp_put_inst(inst);
  224. return rc;
  225. }
  226. static bool cvp_fence_wait(struct cvp_fence_queue *q,
  227. struct cvp_fence_command **fence,
  228. enum queue_state *state)
  229. {
  230. struct cvp_fence_command *f;
  231. if (!q)
  232. return false;
  233. *fence = NULL;
  234. while (!mutex_trylock(&q->lock))
  235. usleep_range(100, 200);
  236. *state = q->state;
  237. if (*state != QUEUE_START) {
  238. mutex_unlock(&q->lock);
  239. return true;
  240. }
  241. if (list_empty(&q->wait_list)) {
  242. mutex_unlock(&q->lock);
  243. return false;
  244. }
  245. f = list_first_entry(&q->wait_list, struct cvp_fence_command, list);
  246. list_del_init(&f->list);
  247. list_add_tail(&f->list, &q->sched_list);
  248. mutex_unlock(&q->lock);
  249. *fence = f;
  250. return true;
  251. }
  252. static int cvp_fence_proc(struct msm_cvp_inst *inst,
  253. struct cvp_fence_command *fc,
  254. struct cvp_hfi_cmd_session_hdr *pkt)
  255. {
  256. int rc = 0;
  257. unsigned long timeout;
  258. u64 ktid;
  259. int synx_state = SYNX_STATE_SIGNALED_SUCCESS;
  260. struct cvp_hfi_ops *ops_tbl;
  261. struct cvp_session_queue *sq;
  262. u32 hfi_err = HFI_ERR_NONE;
  263. struct cvp_hfi_msg_session_hdr_ext hdr;
  264. struct iris_hfi_device *device;
  265. dprintk(CVP_SYNX, "%s %s\n", current->comm, __func__);
  266. if (!inst || !inst->core)
  267. return -EINVAL;
  268. ops_tbl = inst->core->dev_ops;
  269. sq = &inst->session_queue_fence;
  270. ktid = pkt->client_data.kdata;
  271. rc = inst->core->synx_ftbl->cvp_synx_ops(inst, CVP_INPUT_SYNX,
  272. fc, &synx_state);
  273. if (rc) {
  274. msm_cvp_unmap_frame(inst, pkt->client_data.kdata);
  275. goto exit;
  276. }
  277. rc = call_hfi_op(ops_tbl, session_send, (void *)inst->session,
  278. (struct eva_kmd_hfi_packet *)pkt);
  279. if (rc) {
  280. dprintk(CVP_ERR, "%s %s: Failed in call_hfi_op %d, %x\n",
  281. current->comm, __func__, pkt->size, pkt->packet_type);
  282. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  283. goto exit;
  284. }
  285. timeout = msecs_to_jiffies(
  286. inst->core->resources.msm_cvp_hw_rsp_timeout);
  287. rc = cvp_wait_process_message(inst, sq, &ktid, timeout,
  288. (struct eva_kmd_hfi_packet *)&hdr);
  289. hfi_err = hdr.error_type;
  290. if (rc) {
  291. dprintk(CVP_ERR, "%s %s: cvp_wait_process_message rc %d\n",
  292. current->comm, __func__, rc);
  293. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  294. goto exit;
  295. }
  296. if (hfi_err == HFI_ERR_SESSION_FLUSHED) {
  297. dprintk(CVP_SYNX, "%s %s: cvp_wait_process_message flushed\n",
  298. current->comm, __func__);
  299. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  300. } else if (hfi_err == HFI_ERR_SESSION_STREAM_CORRUPT) {
  301. dprintk(CVP_INFO, "%s %s: cvp_wait_process_msg non-fatal %d\n",
  302. current->comm, __func__, hfi_err);
  303. synx_state = SYNX_STATE_SIGNALED_SUCCESS;
  304. } else if (hfi_err == HFI_ERR_SESSION_HW_HANG_DETECTED) {
  305. dprintk(CVP_ERR, "%s %s: cvp_wait_process_message hfi HW hang err %d\n",
  306. current->comm, __func__, hfi_err);
  307. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  308. device = ops_tbl->hfi_device_data;
  309. cvp_dump_csr(device);
  310. } else if (hfi_err != HFI_ERR_NONE) {
  311. dprintk(CVP_ERR, "%s %s: cvp_wait_process_message hfi err %d\n",
  312. current->comm, __func__, hfi_err);
  313. synx_state = SYNX_STATE_SIGNALED_CANCEL;
  314. }
  315. exit:
  316. rc = inst->core->synx_ftbl->cvp_synx_ops(inst, CVP_OUTPUT_SYNX,
  317. fc, &synx_state);
  318. return rc;
  319. }
  320. static int cvp_alloc_fence_data(struct cvp_fence_command **f, u32 size)
  321. {
  322. struct cvp_fence_command *fcmd;
  323. int alloc_size = sizeof(struct cvp_hfi_msg_session_hdr_ext);
  324. fcmd = kzalloc(sizeof(struct cvp_fence_command), GFP_KERNEL);
  325. if (!fcmd)
  326. return -ENOMEM;
  327. alloc_size = (alloc_size >= size) ? alloc_size : size;
  328. fcmd->pkt = kzalloc(alloc_size, GFP_KERNEL);
  329. if (!fcmd->pkt) {
  330. kfree(fcmd);
  331. return -ENOMEM;
  332. }
  333. *f = fcmd;
  334. return 0;
  335. }
  336. static void cvp_free_fence_data(struct cvp_fence_command *f)
  337. {
  338. kfree(f->pkt);
  339. f->pkt = NULL;
  340. kfree(f);
  341. f = NULL;
  342. }
  343. static int cvp_fence_thread(void *data)
  344. {
  345. int rc = 0, num_fences;
  346. struct msm_cvp_inst *inst;
  347. struct cvp_fence_queue *q;
  348. enum queue_state state;
  349. struct cvp_fence_command *f;
  350. struct cvp_hfi_cmd_session_hdr *pkt;
  351. u32 *synx;
  352. u64 ktid = 0;
  353. dprintk(CVP_SYNX, "Enter %s\n", current->comm);
  354. inst = (struct msm_cvp_inst *)data;
  355. if (!inst || !inst->core || !inst->core->dev_ops) {
  356. dprintk(CVP_ERR, "%s invalid inst %pK\n", current->comm, inst);
  357. rc = -EINVAL;
  358. goto exit;
  359. }
  360. q = &inst->fence_cmd_queue;
  361. wait:
  362. dprintk(CVP_SYNX, "%s starts wait\n", current->comm);
  363. f = NULL;
  364. wait_event_interruptible(q->wq, cvp_fence_wait(q, &f, &state));
  365. if (state != QUEUE_START)
  366. goto exit;
  367. if (!f) {
  368. usleep_range(100, 200);
  369. goto wait;
  370. }
  371. pkt = f->pkt;
  372. synx = (u32 *)f->synx;
  373. num_fences = f->num_fences - f->output_index;
  374. /*
  375. * If there is output fence, go through fence path
  376. * Otherwise, go through non-fenced path
  377. */
  378. if (num_fences)
  379. ktid = pkt->client_data.kdata & (FENCE_BIT - 1);
  380. dprintk(CVP_SYNX, "%s pkt type %d on ktid %llu frameID %llu\n",
  381. current->comm, pkt->packet_type, ktid, f->frame_id);
  382. rc = cvp_fence_proc(inst, f, pkt);
  383. mutex_lock(&q->lock);
  384. inst->core->synx_ftbl->cvp_release_synx(inst, f);
  385. list_del_init(&f->list);
  386. state = q->state;
  387. mutex_unlock(&q->lock);
  388. dprintk(CVP_SYNX, "%s done with %d ktid %llu frameID %llu rc %d\n",
  389. current->comm, pkt->packet_type, ktid, f->frame_id, rc);
  390. cvp_free_fence_data(f);
  391. if (rc && state != QUEUE_START)
  392. goto exit;
  393. goto wait;
  394. exit:
  395. dprintk(CVP_SYNX, "%s exit\n", current->comm);
  396. cvp_put_inst(inst);
  397. return rc;
  398. }
  399. static int msm_cvp_session_process_hfi_fence(struct msm_cvp_inst *inst,
  400. struct eva_kmd_arg *arg)
  401. {
  402. dprintk(CVP_WARN, "Deprecated IOCTL command %s\n", __func__);
  403. return -EINVAL;
  404. }
  405. static int cvp_populate_fences( struct eva_kmd_hfi_packet *in_pkt,
  406. unsigned int offset, unsigned int num, struct msm_cvp_inst *inst)
  407. {
  408. u32 i, buf_offset, fence_cnt;
  409. struct eva_kmd_fence fences[MAX_HFI_FENCE_SIZE];
  410. struct cvp_fence_command *f;
  411. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  412. struct cvp_fence_queue *q;
  413. enum op_mode mode;
  414. struct cvp_buf_type *buf;
  415. bool override;
  416. int rc = 0;
  417. override = get_pkt_fenceoverride((struct cvp_hal_session_cmd_pkt*)in_pkt);
  418. dprintk(CVP_SYNX, "%s:Fence Override is %d\n",__func__, override);
  419. dprintk(CVP_SYNX, "%s:Kernel Fence is %d\n", __func__, cvp_kernel_fence_enabled);
  420. q = &inst->fence_cmd_queue;
  421. mutex_lock(&q->lock);
  422. mode = q->mode;
  423. mutex_unlock(&q->lock);
  424. if (mode == OP_DRAINING) {
  425. dprintk(CVP_SYNX, "%s: flush in progress\n", __func__);
  426. rc = -EBUSY;
  427. goto exit;
  428. }
  429. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  430. rc = cvp_alloc_fence_data((&f), cmd_hdr->size);
  431. if (rc) {
  432. dprintk(CVP_ERR,"%s: Failed to alloc fence data", __func__);
  433. goto exit;
  434. }
  435. f->type = cmd_hdr->packet_type;
  436. f->mode = OP_NORMAL;
  437. f->signature = 0xFEEDFACE;
  438. f->num_fences = 0;
  439. f->output_index = 0;
  440. buf_offset = offset;
  441. if (cvp_kernel_fence_enabled == 0)
  442. {
  443. goto soc_fence;
  444. }
  445. else if (cvp_kernel_fence_enabled == 1)
  446. {
  447. goto kernel_fence;
  448. }
  449. else if (cvp_kernel_fence_enabled == 2)
  450. {
  451. if (override == true)
  452. goto kernel_fence;
  453. else if (override == false)
  454. goto soc_fence;
  455. else
  456. {
  457. dprintk(CVP_ERR, "%s: invalid params", __func__);
  458. rc = -EINVAL;
  459. goto free_exit;
  460. }
  461. }
  462. else
  463. {
  464. dprintk(CVP_ERR, "%s: invalid params", __func__);
  465. rc = -EINVAL;
  466. goto free_exit;
  467. }
  468. soc_fence:
  469. for (i = 0; i < num; i++) {
  470. buf = (struct cvp_buf_type*)&in_pkt->pkt_data[buf_offset];
  471. buf_offset += sizeof(*buf) >> 2;
  472. if (buf->input_handle || buf->output_handle) {
  473. f->num_fences++;
  474. if (buf->input_handle)
  475. f->output_index++;
  476. }
  477. }
  478. f->signature = 0xB0BABABE;
  479. if (f->num_fences)
  480. goto fence_cmd_queue;
  481. goto free_exit;
  482. kernel_fence:
  483. /* First pass to find INPUT synx handles */
  484. for (i = 0; i < num; i++) {
  485. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[buf_offset];
  486. buf_offset += sizeof(*buf) >> 2;
  487. if (buf->input_handle) {
  488. /* Check fence_type? */
  489. fences[f->num_fences].h_synx = buf->input_handle;
  490. f->num_fences++;
  491. buf->fence_type &= ~INPUT_FENCE_BITMASK;
  492. buf->input_handle = 0;
  493. }
  494. }
  495. f->output_index = f->num_fences;
  496. dprintk(CVP_SYNX, "%s:Input Fence passed - Number of Fences is %d\n",
  497. __func__, f->num_fences);
  498. /*
  499. * Second pass to find OUTPUT synx handle
  500. * If no of fences is 0 dont execute the below portion until line 911, return 0
  501. */
  502. buf_offset = offset;
  503. for (i = 0; i < num; i++) {
  504. buf = (struct cvp_buf_type*)&in_pkt->pkt_data[buf_offset];
  505. buf_offset += sizeof(*buf) >> 2;
  506. if (buf->output_handle) {
  507. /* Check fence_type? */
  508. fences[f->num_fences].h_synx = buf->output_handle;
  509. f->num_fences++;
  510. buf->fence_type &= ~OUTPUT_FENCE_BITMASK;
  511. buf->output_handle = 0;
  512. }
  513. }
  514. dprintk(CVP_SYNX, "%s:Output Fence passed - Number of Fences is %d\n",
  515. __func__, f->num_fences);
  516. if (f->num_fences == 0)
  517. goto free_exit;
  518. rc = inst->core->synx_ftbl->cvp_import_synx(inst, f, (u32*)fences);
  519. if (rc) {
  520. dprintk(CVP_ERR,"%s: Failed to import fences", __func__);
  521. goto free_exit;
  522. }
  523. fence_cmd_queue:
  524. fence_cnt = f->num_fences;
  525. memcpy(f->pkt, cmd_hdr, cmd_hdr->size);
  526. f->pkt->client_data.kdata |= FENCE_BIT;
  527. mutex_lock(&q->lock);
  528. list_add_tail(&f->list, &inst->fence_cmd_queue.wait_list);
  529. mutex_unlock(&q->lock);
  530. wake_up(&inst->fence_cmd_queue.wq);
  531. return fence_cnt;
  532. free_exit:
  533. cvp_free_fence_data(f);
  534. exit:
  535. return rc;
  536. }
  537. static int cvp_enqueue_pkt(struct msm_cvp_inst* inst,
  538. struct eva_kmd_hfi_packet *in_pkt,
  539. unsigned int in_offset,
  540. unsigned int in_buf_num)
  541. {
  542. struct cvp_hfi_ops *ops_tbl;
  543. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  544. int pkt_type, rc = 0;
  545. enum buf_map_type map_type;
  546. ops_tbl = inst->core->dev_ops;
  547. pkt_type = in_pkt->pkt_data[1];
  548. map_type = cvp_find_map_type(pkt_type);
  549. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  550. /* The kdata will be overriden by transaction ID if the cmd has buf */
  551. cmd_hdr->client_data.kdata = 0;
  552. dprintk(CVP_CMD, "%s: "
  553. "pkt_type %08x sess_id %08x trans_id %u ktid %llu\n",
  554. __func__, cmd_hdr->packet_type,
  555. cmd_hdr->session_id,
  556. cmd_hdr->client_data.transaction_id,
  557. cmd_hdr->client_data.kdata & (FENCE_BIT - 1));
  558. if (map_type == MAP_PERSIST)
  559. rc = msm_cvp_map_user_persist(inst, in_pkt, in_offset, in_buf_num);
  560. else if (map_type == UNMAP_PERSIST)
  561. rc = msm_cvp_unmap_user_persist(inst, in_pkt, in_offset, in_buf_num);
  562. else
  563. rc = msm_cvp_map_frame(inst, in_pkt, in_offset, in_buf_num);
  564. if (rc)
  565. return rc;
  566. rc = cvp_populate_fences(in_pkt, in_offset, in_buf_num, inst);
  567. if (rc == 0) {
  568. rc = call_hfi_op(ops_tbl, session_send, (void *)inst->session,
  569. in_pkt);
  570. if (rc) {
  571. dprintk(CVP_ERR,"%s: Failed in call_hfi_op %d, %x\n",
  572. __func__, in_pkt->pkt_data[0],
  573. in_pkt->pkt_data[1]);
  574. if (map_type == MAP_FRAME)
  575. msm_cvp_unmap_frame(inst,
  576. cmd_hdr->client_data.kdata);
  577. }
  578. } else if (rc > 0) {
  579. dprintk(CVP_SYNX, "Going fenced path\n");
  580. rc = 0;
  581. } else {
  582. dprintk(CVP_ERR,"%s: Failed to populate fences\n",
  583. __func__);
  584. if (map_type == MAP_FRAME)
  585. msm_cvp_unmap_frame(inst, cmd_hdr->client_data.kdata);
  586. }
  587. return rc;
  588. }
  589. static inline int div_by_1dot5(unsigned int a)
  590. {
  591. unsigned long i = a << 1;
  592. return (unsigned int) i/3;
  593. }
  594. int msm_cvp_session_delete(struct msm_cvp_inst *inst)
  595. {
  596. return 0;
  597. }
  598. int msm_cvp_session_create(struct msm_cvp_inst *inst)
  599. {
  600. int rc = 0, rc1 = 0;
  601. struct cvp_session_queue *sq;
  602. if (!inst || !inst->core)
  603. return -EINVAL;
  604. if (inst->state >= MSM_CVP_CLOSE_DONE)
  605. return -ECONNRESET;
  606. if (inst->state != MSM_CVP_CORE_INIT_DONE ||
  607. inst->state > MSM_CVP_OPEN_DONE) {
  608. dprintk(CVP_ERR,
  609. "%s Incorrect CVP state %d to create session\n",
  610. __func__, inst->state);
  611. return -EINVAL;
  612. }
  613. rc = msm_cvp_comm_try_state(inst, MSM_CVP_OPEN_DONE);
  614. if (rc) {
  615. dprintk(CVP_ERR,
  616. "Failed to move instance to open done state\n");
  617. goto fail_create;
  618. }
  619. rc = cvp_comm_set_arp_buffers(inst);
  620. if (rc) {
  621. dprintk(CVP_ERR,
  622. "Failed to set ARP buffers\n");
  623. goto fail_init;
  624. }
  625. inst->core->synx_ftbl->cvp_sess_init_synx(inst);
  626. sq = &inst->session_queue;
  627. spin_lock(&sq->lock);
  628. sq->state = QUEUE_ACTIVE;
  629. spin_unlock(&sq->lock);
  630. return rc;
  631. fail_init:
  632. rc1 = msm_cvp_comm_try_state(inst, MSM_CVP_CLOSE_DONE);
  633. if (rc1)
  634. dprintk(CVP_ERR, "%s: close failed\n", __func__);
  635. fail_create:
  636. return rc;
  637. }
  638. static int session_state_check_init(struct msm_cvp_inst *inst)
  639. {
  640. mutex_lock(&inst->lock);
  641. if (inst->state == MSM_CVP_OPEN || inst->state == MSM_CVP_OPEN_DONE) {
  642. mutex_unlock(&inst->lock);
  643. return 0;
  644. }
  645. mutex_unlock(&inst->lock);
  646. return msm_cvp_session_create(inst);
  647. }
  648. static int cvp_fence_thread_start(struct msm_cvp_inst *inst)
  649. {
  650. u32 tnum = 0;
  651. u32 i = 0;
  652. int rc = 0;
  653. char tname[16];
  654. struct task_struct *thread;
  655. struct cvp_fence_queue *q;
  656. struct cvp_session_queue *sq;
  657. if (!inst->prop.fthread_nr)
  658. return 0;
  659. q = &inst->fence_cmd_queue;
  660. mutex_lock(&q->lock);
  661. q->state = QUEUE_START;
  662. mutex_unlock(&q->lock);
  663. for (i = 0; i < inst->prop.fthread_nr; ++i) {
  664. if (!cvp_get_inst_validate(inst->core, inst)) {
  665. rc = -ECONNRESET;
  666. goto exit;
  667. }
  668. snprintf(tname, sizeof(tname), "fthread_%d", tnum++);
  669. thread = kthread_run(cvp_fence_thread, inst, tname);
  670. if (!thread) {
  671. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  672. rc = -ECHILD;
  673. goto exit;
  674. }
  675. }
  676. sq = &inst->session_queue_fence;
  677. spin_lock(&sq->lock);
  678. sq->state = QUEUE_START;
  679. spin_unlock(&sq->lock);
  680. exit:
  681. if (rc) {
  682. mutex_lock(&q->lock);
  683. q->state = QUEUE_STOP;
  684. mutex_unlock(&q->lock);
  685. wake_up_all(&q->wq);
  686. }
  687. return rc;
  688. }
  689. static int cvp_fence_thread_stop(struct msm_cvp_inst *inst)
  690. {
  691. struct cvp_fence_queue *q;
  692. struct cvp_session_queue *sq;
  693. if (!inst->prop.fthread_nr)
  694. return 0;
  695. q = &inst->fence_cmd_queue;
  696. mutex_lock(&q->lock);
  697. q->state = QUEUE_STOP;
  698. mutex_unlock(&q->lock);
  699. sq = &inst->session_queue_fence;
  700. spin_lock(&sq->lock);
  701. sq->state = QUEUE_STOP;
  702. spin_unlock(&sq->lock);
  703. wake_up_all(&q->wq);
  704. wake_up_all(&sq->wq);
  705. return 0;
  706. }
  707. int msm_cvp_session_start(struct msm_cvp_inst *inst,
  708. struct eva_kmd_arg *arg)
  709. {
  710. struct cvp_session_queue *sq;
  711. struct cvp_hfi_ops *ops_tbl;
  712. int rc;
  713. enum queue_state old_state;
  714. if (!inst || !inst->core) {
  715. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  716. return -EINVAL;
  717. }
  718. sq = &inst->session_queue;
  719. spin_lock(&sq->lock);
  720. if (sq->msg_count) {
  721. dprintk(CVP_ERR, "session start failed queue not empty%d\n",
  722. sq->msg_count);
  723. spin_unlock(&sq->lock);
  724. rc = -EINVAL;
  725. goto exit;
  726. }
  727. old_state = sq->state;
  728. sq->state = QUEUE_START;
  729. spin_unlock(&sq->lock);
  730. ops_tbl = inst->core->dev_ops;
  731. if (inst->prop.type == HFI_SESSION_FD
  732. || inst->prop.type == HFI_SESSION_DMM) {
  733. spin_lock(&inst->core->resources.pm_qos.lock);
  734. inst->core->resources.pm_qos.off_vote_cnt++;
  735. spin_unlock(&inst->core->resources.pm_qos.lock);
  736. call_hfi_op(ops_tbl, pm_qos_update, ops_tbl->hfi_device_data);
  737. }
  738. /*
  739. * cvp_fence_thread_start will increment reference to instance.
  740. * It guarantees the EVA session won't be deleted. Use of session
  741. * functions, such as session_start requires the session to be valid.
  742. */
  743. rc = cvp_fence_thread_start(inst);
  744. if (rc)
  745. goto restore_state;
  746. /* Send SESSION_START command */
  747. rc = call_hfi_op(ops_tbl, session_start, (void *)inst->session);
  748. if (rc) {
  749. dprintk(CVP_WARN, "%s: session start failed rc %d\n",
  750. __func__, rc);
  751. goto stop_thread;
  752. }
  753. /* Wait for FW response */
  754. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_START_DONE);
  755. if (rc) {
  756. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  757. __func__, rc);
  758. goto stop_thread;
  759. }
  760. pr_info_ratelimited(CVP_DBG_TAG "session %llx (%#x) started\n",
  761. "sess", inst, hash32_ptr(inst->session));
  762. return 0;
  763. stop_thread:
  764. cvp_fence_thread_stop(inst);
  765. restore_state:
  766. spin_lock(&sq->lock);
  767. sq->state = old_state;
  768. spin_unlock(&sq->lock);
  769. exit:
  770. return rc;
  771. }
  772. int msm_cvp_session_stop(struct msm_cvp_inst *inst,
  773. struct eva_kmd_arg *arg)
  774. {
  775. struct cvp_session_queue *sq;
  776. struct eva_kmd_session_control *sc = NULL;
  777. struct msm_cvp_inst *s;
  778. struct cvp_hfi_ops *ops_tbl;
  779. int rc = 0;
  780. int curr_sq_state = -1;
  781. if (!inst || !inst->core) {
  782. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  783. return -EINVAL;
  784. }
  785. if (arg)
  786. sc = &arg->data.session_ctrl;
  787. s = cvp_get_inst_validate(inst->core, inst);
  788. if (!s)
  789. return -ECONNRESET;
  790. sq = &inst->session_queue;
  791. curr_sq_state = sq->state;
  792. spin_lock(&sq->lock);
  793. if (sq->state != QUEUE_START) {
  794. spin_unlock(&sq->lock);
  795. dprintk(CVP_ERR,
  796. "%s: Stop not allowed - curr state %d, inst %llx, sess %llx, %s type %d\n",
  797. __func__, sq->state, inst, inst->session, inst->proc_name,
  798. inst->session_type);
  799. rc = -EINVAL;
  800. return rc;
  801. }
  802. if (sq->state == QUEUE_STOP) {
  803. spin_unlock(&sq->lock);
  804. dprintk(CVP_WARN,
  805. "%s: Double stop session - inst %llx, sess %llx, %s of type %d\n",
  806. __func__, inst, inst->session, inst->proc_name, inst->session_type);
  807. return rc;
  808. }
  809. if (sq->msg_count) {
  810. dprintk(CVP_ERR, "session stop incorrect: queue not empty%d\n",
  811. sq->msg_count);
  812. if (sc)
  813. sc->ctrl_data[0] = sq->msg_count;
  814. spin_unlock(&sq->lock);
  815. rc = -EUCLEAN;
  816. goto exit;
  817. }
  818. sq->state = QUEUE_STOP;
  819. pr_info_ratelimited(CVP_DBG_TAG "Stop session: %pK session_id = %#x\n",
  820. "sess", inst, hash32_ptr(inst->session));
  821. spin_unlock(&sq->lock);
  822. ops_tbl = inst->core->dev_ops;
  823. /* Send SESSION_STOP command */
  824. rc = call_hfi_op(ops_tbl, session_stop, (void *)inst->session);
  825. if (rc) {
  826. dprintk(CVP_WARN, "%s: session stop failed rc %d\n",
  827. __func__, rc);
  828. spin_lock(&sq->lock);
  829. sq->state = curr_sq_state;
  830. spin_unlock(&sq->lock);
  831. goto stop_thread;
  832. }
  833. /* Wait for FW response */
  834. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_STOP_DONE);
  835. if (rc) {
  836. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  837. __func__, rc);
  838. spin_lock(&sq->lock);
  839. sq->state = curr_sq_state;
  840. spin_unlock(&sq->lock);
  841. goto stop_thread;
  842. }
  843. stop_thread:
  844. wake_up_all(&inst->session_queue.wq);
  845. cvp_fence_thread_stop(inst);
  846. exit:
  847. cvp_put_inst(s);
  848. return rc;
  849. }
  850. int msm_cvp_session_queue_stop(struct msm_cvp_inst *inst)
  851. {
  852. struct cvp_session_queue *sq;
  853. sq = &inst->session_queue;
  854. spin_lock(&sq->lock);
  855. if (sq->state == QUEUE_STOP) {
  856. spin_unlock(&sq->lock);
  857. return 0;
  858. }
  859. dprintk(CVP_SESS, "Stop session queue: %pK session_id = %#x\n",
  860. inst, hash32_ptr(inst->session));
  861. spin_unlock(&sq->lock);
  862. wake_up_all(&inst->session_queue.wq);
  863. return cvp_fence_thread_stop(inst);
  864. }
  865. static int msm_cvp_session_ctrl(struct msm_cvp_inst *inst,
  866. struct eva_kmd_arg *arg)
  867. {
  868. struct eva_kmd_session_control *ctrl = &arg->data.session_ctrl;
  869. int rc = 0;
  870. unsigned int ctrl_type;
  871. ctrl_type = ctrl->ctrl_type;
  872. if (!inst && ctrl_type != SESSION_CREATE) {
  873. dprintk(CVP_ERR, "%s invalid session\n", __func__);
  874. return -EINVAL;
  875. }
  876. switch (ctrl_type) {
  877. case SESSION_STOP:
  878. rc = msm_cvp_session_stop(inst, arg);
  879. break;
  880. case SESSION_START:
  881. rc = msm_cvp_session_start(inst, arg);
  882. break;
  883. case SESSION_CREATE:
  884. rc = msm_cvp_session_create(inst);
  885. break;
  886. case SESSION_DELETE:
  887. rc = msm_cvp_session_delete(inst);
  888. break;
  889. case SESSION_INFO:
  890. default:
  891. dprintk(CVP_ERR, "%s Unsupported session ctrl%d\n",
  892. __func__, ctrl->ctrl_type);
  893. rc = -EINVAL;
  894. }
  895. return rc;
  896. }
  897. static int msm_cvp_get_sysprop(struct msm_cvp_inst *inst,
  898. struct eva_kmd_arg *arg)
  899. {
  900. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  901. struct cvp_hfi_ops *ops_tbl;
  902. struct iris_hfi_device *hfi;
  903. struct cvp_session_prop *session_prop;
  904. int i, rc = 0;
  905. if (!inst || !inst->core || !inst->core->dev_ops) {
  906. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  907. return -EINVAL;
  908. }
  909. ops_tbl = inst->core->dev_ops;
  910. hfi = ops_tbl->hfi_device_data;
  911. if (props->prop_num > MAX_KMD_PROP_NUM_PER_PACKET) {
  912. dprintk(CVP_ERR, "Too many properties %d to get\n",
  913. props->prop_num);
  914. return -E2BIG;
  915. }
  916. session_prop = &inst->prop;
  917. for (i = 0; i < props->prop_num; i++) {
  918. switch (props->prop_data[i].prop_type) {
  919. case EVA_KMD_PROP_HFI_VERSION:
  920. {
  921. props->prop_data[i].data = hfi->version;
  922. break;
  923. }
  924. case EVA_KMD_PROP_SESSION_DUMPOFFSET:
  925. {
  926. props->prop_data[i].data =
  927. session_prop->dump_offset;
  928. break;
  929. }
  930. case EVA_KMD_PROP_SESSION_DUMPSIZE:
  931. {
  932. props->prop_data[i].data =
  933. session_prop->dump_size;
  934. break;
  935. }
  936. case EVA_KMD_PROP_SESSION_ERROR:
  937. {
  938. get_dma_buf(hfi->sfr.mem_data.dma_buf);
  939. rc = dma_buf_fd(hfi->sfr.mem_data.dma_buf, O_RDONLY | O_CLOEXEC);
  940. if (rc < 0) {
  941. dprintk(CVP_WARN, "Failed get dma_buf fd %d\n", rc);
  942. break;
  943. }
  944. props->prop_data[i].data = rc;
  945. rc = 0;
  946. break;
  947. }
  948. case EVA_KMD_PROP_PWR_FDU:
  949. {
  950. props->prop_data[i].data =
  951. msm_cvp_get_hw_aggregate_cycles(HFI_HW_FDU);
  952. break;
  953. }
  954. case EVA_KMD_PROP_PWR_ICA:
  955. {
  956. props->prop_data[i].data =
  957. msm_cvp_get_hw_aggregate_cycles(HFI_HW_ICA);
  958. break;
  959. }
  960. case EVA_KMD_PROP_PWR_OD:
  961. {
  962. props->prop_data[i].data =
  963. msm_cvp_get_hw_aggregate_cycles(HFI_HW_OD);
  964. break;
  965. }
  966. case EVA_KMD_PROP_PWR_MPU:
  967. {
  968. props->prop_data[i].data =
  969. msm_cvp_get_hw_aggregate_cycles(HFI_HW_MPU);
  970. break;
  971. }
  972. case EVA_KMD_PROP_PWR_VADL:
  973. {
  974. props->prop_data[i].data =
  975. msm_cvp_get_hw_aggregate_cycles(HFI_HW_VADL);
  976. break;
  977. }
  978. case EVA_KMD_PROP_PWR_TOF:
  979. {
  980. props->prop_data[i].data =
  981. msm_cvp_get_hw_aggregate_cycles(HFI_HW_TOF);
  982. break;
  983. }
  984. case EVA_KMD_PROP_PWR_RGE:
  985. {
  986. props->prop_data[i].data =
  987. msm_cvp_get_hw_aggregate_cycles(HFI_HW_RGE);
  988. break;
  989. }
  990. case EVA_KMD_PROP_PWR_XRA:
  991. {
  992. props->prop_data[i].data =
  993. msm_cvp_get_hw_aggregate_cycles(HFI_HW_XRA);
  994. break;
  995. }
  996. case EVA_KMD_PROP_PWR_LSR:
  997. {
  998. props->prop_data[i].data =
  999. msm_cvp_get_hw_aggregate_cycles(HFI_HW_LSR);
  1000. break;
  1001. }
  1002. default:
  1003. dprintk(CVP_ERR, "unrecognized sys property %d\n",
  1004. props->prop_data[i].prop_type);
  1005. rc = -EFAULT;
  1006. }
  1007. }
  1008. return rc;
  1009. }
  1010. static int msm_cvp_set_sysprop(struct msm_cvp_inst *inst,
  1011. struct eva_kmd_arg *arg)
  1012. {
  1013. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  1014. struct eva_kmd_sys_property *prop_array;
  1015. struct cvp_session_prop *session_prop;
  1016. int i, rc = 0;
  1017. if (!inst) {
  1018. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1019. return -EINVAL;
  1020. }
  1021. if (props->prop_num > MAX_KMD_PROP_NUM_PER_PACKET) {
  1022. dprintk(CVP_ERR, "Too many properties %d to set\n",
  1023. props->prop_num);
  1024. return -E2BIG;
  1025. }
  1026. prop_array = &arg->data.sys_properties.prop_data[0];
  1027. session_prop = &inst->prop;
  1028. for (i = 0; i < props->prop_num; i++) {
  1029. switch (prop_array[i].prop_type) {
  1030. case EVA_KMD_PROP_SESSION_TYPE:
  1031. session_prop->type = prop_array[i].data;
  1032. break;
  1033. case EVA_KMD_PROP_SESSION_KERNELMASK:
  1034. session_prop->kernel_mask = prop_array[i].data;
  1035. break;
  1036. case EVA_KMD_PROP_SESSION_PRIORITY:
  1037. session_prop->priority = prop_array[i].data;
  1038. break;
  1039. case EVA_KMD_PROP_SESSION_SECURITY:
  1040. session_prop->is_secure = prop_array[i].data;
  1041. break;
  1042. case EVA_KMD_PROP_SESSION_DSPMASK:
  1043. session_prop->dsp_mask = prop_array[i].data;
  1044. break;
  1045. case EVA_KMD_PROP_PWR_FDU:
  1046. session_prop->cycles[HFI_HW_FDU] = prop_array[i].data;
  1047. break;
  1048. case EVA_KMD_PROP_PWR_ICA:
  1049. session_prop->cycles[HFI_HW_ICA] =
  1050. div_by_1dot5(prop_array[i].data);
  1051. break;
  1052. case EVA_KMD_PROP_PWR_OD:
  1053. session_prop->cycles[HFI_HW_OD] = prop_array[i].data;
  1054. break;
  1055. case EVA_KMD_PROP_PWR_MPU:
  1056. session_prop->cycles[HFI_HW_MPU] = prop_array[i].data;
  1057. break;
  1058. case EVA_KMD_PROP_PWR_VADL:
  1059. session_prop->cycles[HFI_HW_VADL] = prop_array[i].data;
  1060. break;
  1061. case EVA_KMD_PROP_PWR_TOF:
  1062. session_prop->cycles[HFI_HW_TOF] = prop_array[i].data;
  1063. break;
  1064. case EVA_KMD_PROP_PWR_RGE:
  1065. session_prop->cycles[HFI_HW_RGE] = prop_array[i].data;
  1066. break;
  1067. case EVA_KMD_PROP_PWR_XRA:
  1068. session_prop->cycles[HFI_HW_XRA] = prop_array[i].data;
  1069. break;
  1070. case EVA_KMD_PROP_PWR_LSR:
  1071. session_prop->cycles[HFI_HW_LSR] = prop_array[i].data;
  1072. break;
  1073. case EVA_KMD_PROP_PWR_FW:
  1074. session_prop->fw_cycles =
  1075. div_by_1dot5(prop_array[i].data);
  1076. break;
  1077. case EVA_KMD_PROP_PWR_DDR:
  1078. session_prop->ddr_bw = prop_array[i].data;
  1079. break;
  1080. case EVA_KMD_PROP_PWR_SYSCACHE:
  1081. session_prop->ddr_cache = prop_array[i].data;
  1082. break;
  1083. case EVA_KMD_PROP_PWR_FDU_OP:
  1084. session_prop->op_cycles[HFI_HW_FDU] = prop_array[i].data;
  1085. break;
  1086. case EVA_KMD_PROP_PWR_ICA_OP:
  1087. session_prop->op_cycles[HFI_HW_ICA] =
  1088. div_by_1dot5(prop_array[i].data);
  1089. break;
  1090. case EVA_KMD_PROP_PWR_OD_OP:
  1091. session_prop->op_cycles[HFI_HW_OD] = prop_array[i].data;
  1092. break;
  1093. case EVA_KMD_PROP_PWR_MPU_OP:
  1094. session_prop->op_cycles[HFI_HW_MPU] = prop_array[i].data;
  1095. break;
  1096. case EVA_KMD_PROP_PWR_VADL_OP:
  1097. session_prop->op_cycles[HFI_HW_VADL] = prop_array[i].data;
  1098. break;
  1099. case EVA_KMD_PROP_PWR_TOF_OP:
  1100. session_prop->op_cycles[HFI_HW_TOF] = prop_array[i].data;
  1101. break;
  1102. case EVA_KMD_PROP_PWR_RGE_OP:
  1103. session_prop->op_cycles[HFI_HW_RGE] = prop_array[i].data;
  1104. break;
  1105. case EVA_KMD_PROP_PWR_XRA_OP:
  1106. session_prop->op_cycles[HFI_HW_XRA] = prop_array[i].data;
  1107. break;
  1108. case EVA_KMD_PROP_PWR_LSR_OP:
  1109. session_prop->op_cycles[HFI_HW_LSR] = prop_array[i].data;
  1110. break;
  1111. case EVA_KMD_PROP_PWR_FW_OP:
  1112. session_prop->fw_op_cycles =
  1113. div_by_1dot5(prop_array[i].data);
  1114. break;
  1115. case EVA_KMD_PROP_PWR_DDR_OP:
  1116. session_prop->ddr_op_bw = prop_array[i].data;
  1117. break;
  1118. case EVA_KMD_PROP_PWR_SYSCACHE_OP:
  1119. session_prop->ddr_op_cache = prop_array[i].data;
  1120. break;
  1121. case EVA_KMD_PROP_PWR_FPS_FDU:
  1122. session_prop->fps[HFI_HW_FDU] = prop_array[i].data;
  1123. break;
  1124. case EVA_KMD_PROP_PWR_FPS_MPU:
  1125. session_prop->fps[HFI_HW_MPU] = prop_array[i].data;
  1126. break;
  1127. case EVA_KMD_PROP_PWR_FPS_OD:
  1128. session_prop->fps[HFI_HW_OD] = prop_array[i].data;
  1129. break;
  1130. case EVA_KMD_PROP_PWR_FPS_ICA:
  1131. session_prop->fps[HFI_HW_ICA] = prop_array[i].data;
  1132. break;
  1133. case EVA_KMD_PROP_PWR_FPS_VADL:
  1134. session_prop->fps[HFI_HW_VADL] = prop_array[i].data;
  1135. break;
  1136. case EVA_KMD_PROP_PWR_FPS_TOF:
  1137. session_prop->fps[HFI_HW_TOF] = prop_array[i].data;
  1138. break;
  1139. case EVA_KMD_PROP_PWR_FPS_RGE:
  1140. session_prop->fps[HFI_HW_RGE] = prop_array[i].data;
  1141. break;
  1142. case EVA_KMD_PROP_PWR_FPS_XRA:
  1143. session_prop->fps[HFI_HW_XRA] = prop_array[i].data;
  1144. break;
  1145. case EVA_KMD_PROP_PWR_FPS_LSR:
  1146. session_prop->fps[HFI_HW_LSR] = prop_array[i].data;
  1147. break;
  1148. case EVA_KMD_PROP_SESSION_DUMPOFFSET:
  1149. session_prop->dump_offset = prop_array[i].data;
  1150. break;
  1151. case EVA_KMD_PROP_SESSION_DUMPSIZE:
  1152. session_prop->dump_size = prop_array[i].data;
  1153. break;
  1154. default:
  1155. dprintk(CVP_ERR,
  1156. "unrecognized sys property to set %d\n",
  1157. prop_array[i].prop_type);
  1158. rc = -EFAULT;
  1159. }
  1160. }
  1161. return rc;
  1162. }
  1163. static int cvp_drain_fence_sched_list(struct msm_cvp_inst *inst)
  1164. {
  1165. unsigned long wait_time;
  1166. struct cvp_fence_queue *q;
  1167. struct cvp_fence_command *f;
  1168. int rc = 0;
  1169. int count = 0, max_count = 0;
  1170. u64 ktid;
  1171. q = &inst->fence_cmd_queue;
  1172. if (!q)
  1173. return -EINVAL;
  1174. if (list_empty(&q->sched_list))
  1175. return rc;
  1176. mutex_lock(&q->lock);
  1177. list_for_each_entry(f, &q->sched_list, list) {
  1178. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1179. dprintk(CVP_SYNX, "%s: frame %llu %llu is in sched_list\n",
  1180. __func__, ktid, f->frame_id);
  1181. ++count;
  1182. }
  1183. mutex_unlock(&q->lock);
  1184. wait_time = count * 1000;
  1185. wait_time *= inst->core->resources.msm_cvp_hw_rsp_timeout;
  1186. dprintk(CVP_SYNX, "%s: wait %d us for %d fence command\n",
  1187. __func__, wait_time, count);
  1188. count = 0;
  1189. max_count = wait_time / 100;
  1190. retry:
  1191. mutex_lock(&q->lock);
  1192. if (list_empty(&q->sched_list)) {
  1193. mutex_unlock(&q->lock);
  1194. return rc;
  1195. }
  1196. mutex_unlock(&q->lock);
  1197. usleep_range(100, 200);
  1198. ++count;
  1199. if (count < max_count) {
  1200. goto retry;
  1201. } else {
  1202. rc = -ETIMEDOUT;
  1203. dprintk(CVP_ERR, "%s: timed out!\n", __func__);
  1204. }
  1205. return rc;
  1206. }
  1207. static void cvp_clean_fence_queue(struct msm_cvp_inst *inst, int synx_state)
  1208. {
  1209. struct cvp_fence_queue *q;
  1210. struct cvp_fence_command *f, *d;
  1211. u64 ktid;
  1212. q = &inst->fence_cmd_queue;
  1213. if (!q)
  1214. return;
  1215. mutex_lock(&q->lock);
  1216. q->mode = OP_DRAINING;
  1217. if (list_empty(&q->wait_list))
  1218. goto check_sched;
  1219. list_for_each_entry_safe(f, d, &q->wait_list, list) {
  1220. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1221. dprintk(CVP_SYNX, "%s: (%#x) flush frame %llu %llu wait_list\n",
  1222. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1223. list_del_init(&f->list);
  1224. msm_cvp_unmap_frame(inst, f->pkt->client_data.kdata);
  1225. inst->core->synx_ftbl->cvp_cancel_synx(inst, CVP_OUTPUT_SYNX,
  1226. f, synx_state);
  1227. inst->core->synx_ftbl->cvp_release_synx(inst, f);
  1228. cvp_free_fence_data(f);
  1229. }
  1230. check_sched:
  1231. if (list_empty(&q->sched_list)) {
  1232. mutex_unlock(&q->lock);
  1233. return;
  1234. }
  1235. list_for_each_entry(f, &q->sched_list, list) {
  1236. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1237. dprintk(CVP_SYNX, "%s: (%#x)flush frame %llu %llu sched_list\n",
  1238. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1239. inst->core->synx_ftbl->cvp_cancel_synx(inst, CVP_INPUT_SYNX,
  1240. f, synx_state);
  1241. }
  1242. mutex_unlock(&q->lock);
  1243. }
  1244. int cvp_clean_session_queues(struct msm_cvp_inst *inst)
  1245. {
  1246. struct cvp_fence_queue *q;
  1247. u32 count = 0, max_retries = 100;
  1248. q = &inst->fence_cmd_queue;
  1249. mutex_lock(&q->lock);
  1250. if (q->state == QUEUE_START || q->state == QUEUE_ACTIVE) {
  1251. mutex_unlock(&q->lock);
  1252. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_CANCEL);
  1253. } else {
  1254. dprintk(CVP_WARN, "Incorrect fence cmd queue state %d\n",
  1255. q->state);
  1256. mutex_unlock(&q->lock);
  1257. }
  1258. cvp_fence_thread_stop(inst);
  1259. /* Waiting for all output synx sent */
  1260. retry:
  1261. mutex_lock(&q->lock);
  1262. if (list_empty(&q->sched_list)) {
  1263. mutex_unlock(&q->lock);
  1264. return 0;
  1265. }
  1266. mutex_unlock(&q->lock);
  1267. usleep_range(500, 1000);
  1268. if (++count > max_retries)
  1269. return -EBUSY;
  1270. goto retry;
  1271. }
  1272. static int cvp_flush_all(struct msm_cvp_inst *inst)
  1273. {
  1274. int rc = 0;
  1275. struct msm_cvp_inst *s;
  1276. struct cvp_fence_queue *q;
  1277. struct cvp_hfi_ops *ops_tbl;
  1278. if (!inst || !inst->core) {
  1279. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1280. return -EINVAL;
  1281. }
  1282. s = cvp_get_inst_validate(inst->core, inst);
  1283. if (!s)
  1284. return -ECONNRESET;
  1285. dprintk(CVP_SESS, "session %llx (%#x)flush all starts\n",
  1286. inst, hash32_ptr(inst->session));
  1287. q = &inst->fence_cmd_queue;
  1288. ops_tbl = inst->core->dev_ops;
  1289. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_CANCEL);
  1290. dprintk(CVP_SESS, "%s: (%#x) send flush to fw\n",
  1291. __func__, hash32_ptr(inst->session));
  1292. /* Send flush to FW */
  1293. rc = call_hfi_op(ops_tbl, session_flush, (void *)inst->session);
  1294. if (rc) {
  1295. dprintk(CVP_WARN, "%s: continue flush without fw. rc %d\n",
  1296. __func__, rc);
  1297. goto exit;
  1298. }
  1299. /* Wait for FW response */
  1300. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_FLUSH_DONE);
  1301. if (rc)
  1302. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  1303. __func__, rc);
  1304. dprintk(CVP_SESS, "%s: (%#x) received flush from fw\n",
  1305. __func__, hash32_ptr(inst->session));
  1306. exit:
  1307. rc = cvp_drain_fence_sched_list(inst);
  1308. mutex_lock(&q->lock);
  1309. q->mode = OP_NORMAL;
  1310. mutex_unlock(&q->lock);
  1311. cvp_put_inst(s);
  1312. return rc;
  1313. }
  1314. int msm_cvp_handle_syscall(struct msm_cvp_inst *inst, struct eva_kmd_arg *arg)
  1315. {
  1316. int rc = 0;
  1317. if (!inst || !arg) {
  1318. dprintk(CVP_ERR, "%s: invalid args\n", __func__);
  1319. return -EINVAL;
  1320. }
  1321. dprintk(CVP_HFI, "%s: arg->type = %x", __func__, arg->type);
  1322. if (arg->type != EVA_KMD_SESSION_CONTROL &&
  1323. arg->type != EVA_KMD_SET_SYS_PROPERTY &&
  1324. arg->type != EVA_KMD_GET_SYS_PROPERTY) {
  1325. rc = session_state_check_init(inst);
  1326. if (rc) {
  1327. dprintk(CVP_ERR,
  1328. "Incorrect session state %d for command %#x",
  1329. inst->state, arg->type);
  1330. return rc;
  1331. }
  1332. }
  1333. switch (arg->type) {
  1334. case EVA_KMD_GET_SESSION_INFO:
  1335. {
  1336. struct eva_kmd_session_info *session =
  1337. (struct eva_kmd_session_info *)&arg->data.session;
  1338. rc = msm_cvp_get_session_info(inst, &session->session_id);
  1339. break;
  1340. }
  1341. case EVA_KMD_UPDATE_POWER:
  1342. {
  1343. rc = msm_cvp_update_power(inst);
  1344. break;
  1345. }
  1346. case EVA_KMD_REGISTER_BUFFER:
  1347. {
  1348. struct eva_kmd_buffer *buf =
  1349. (struct eva_kmd_buffer *)&arg->data.regbuf;
  1350. rc = msm_cvp_register_buffer(inst, buf);
  1351. break;
  1352. }
  1353. case EVA_KMD_UNREGISTER_BUFFER:
  1354. {
  1355. struct eva_kmd_buffer *buf =
  1356. (struct eva_kmd_buffer *)&arg->data.unregbuf;
  1357. rc = msm_cvp_unregister_buffer(inst, buf);
  1358. break;
  1359. }
  1360. case EVA_KMD_RECEIVE_MSG_PKT:
  1361. {
  1362. struct eva_kmd_hfi_packet *out_pkt =
  1363. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1364. rc = msm_cvp_session_receive_hfi(inst, out_pkt);
  1365. break;
  1366. }
  1367. case EVA_KMD_SEND_CMD_PKT:
  1368. {
  1369. struct eva_kmd_hfi_packet *in_pkt =
  1370. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1371. rc = msm_cvp_session_process_hfi(inst, in_pkt,
  1372. arg->buf_offset, arg->buf_num);
  1373. break;
  1374. }
  1375. case EVA_KMD_SEND_FENCE_CMD_PKT:
  1376. {
  1377. rc = msm_cvp_session_process_hfi_fence(inst, arg);
  1378. break;
  1379. }
  1380. case EVA_KMD_SESSION_CONTROL:
  1381. rc = msm_cvp_session_ctrl(inst, arg);
  1382. break;
  1383. case EVA_KMD_GET_SYS_PROPERTY:
  1384. rc = msm_cvp_get_sysprop(inst, arg);
  1385. break;
  1386. case EVA_KMD_SET_SYS_PROPERTY:
  1387. rc = msm_cvp_set_sysprop(inst, arg);
  1388. break;
  1389. case EVA_KMD_FLUSH_ALL:
  1390. rc = cvp_flush_all(inst);
  1391. break;
  1392. case EVA_KMD_FLUSH_FRAME:
  1393. dprintk(CVP_WARN, "EVA_KMD_FLUSH_FRAME IOCTL deprecated\n");
  1394. rc = 0;
  1395. break;
  1396. default:
  1397. dprintk(CVP_HFI, "%s: unknown arg type %#x\n",
  1398. __func__, arg->type);
  1399. rc = -ENOTSUPP;
  1400. break;
  1401. }
  1402. return rc;
  1403. }
  1404. int msm_cvp_session_deinit(struct msm_cvp_inst *inst)
  1405. {
  1406. int rc = 0;
  1407. struct cvp_hal_session *session;
  1408. if (!inst || !inst->core) {
  1409. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1410. return -EINVAL;
  1411. }
  1412. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1413. inst, hash32_ptr(inst->session));
  1414. session = (struct cvp_hal_session *)inst->session;
  1415. if (!session || session == (void *)0xdeadbeef)
  1416. return rc;
  1417. rc = msm_cvp_comm_try_state(inst, MSM_CVP_CLOSE_DONE);
  1418. if (rc)
  1419. dprintk(CVP_ERR, "%s: close failed\n", __func__);
  1420. rc = msm_cvp_session_deinit_buffers(inst);
  1421. return rc;
  1422. }
  1423. int msm_cvp_session_init(struct msm_cvp_inst *inst)
  1424. {
  1425. int rc = 0;
  1426. if (!inst) {
  1427. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1428. return -EINVAL;
  1429. }
  1430. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1431. inst, hash32_ptr(inst->session));
  1432. /* set default frequency */
  1433. inst->clk_data.min_freq = 1000;
  1434. inst->clk_data.ddr_bw = 1000;
  1435. inst->clk_data.sys_cache_bw = 1000;
  1436. inst->prop.type = 1;
  1437. inst->prop.kernel_mask = 0xFFFFFFFF;
  1438. inst->prop.priority = 0;
  1439. inst->prop.is_secure = 0;
  1440. inst->prop.dsp_mask = 0;
  1441. inst->prop.fthread_nr = 3;
  1442. return rc;
  1443. }