msm_cvp.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023-2024, 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. unsigned int total_fence_count = 0;
  417. int rc = 0;
  418. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  419. if (!offset || !num)
  420. return 0;
  421. if (offset < (sizeof(struct cvp_hfi_cmd_session_hdr)/sizeof(u32))) {
  422. dprintk(CVP_ERR, "%s: Incorrect offset in cmd %d\n", __func__, offset);
  423. rc = -EINVAL;
  424. goto exit;
  425. }
  426. override = get_pkt_fenceoverride((struct cvp_hal_session_cmd_pkt*)in_pkt);
  427. dprintk(CVP_SYNX, "%s:Fence Override is %d\n",__func__, override);
  428. dprintk(CVP_SYNX, "%s:Kernel Fence is %d\n", __func__, cvp_kernel_fence_enabled);
  429. q = &inst->fence_cmd_queue;
  430. mutex_lock(&q->lock);
  431. mode = q->mode;
  432. mutex_unlock(&q->lock);
  433. if (mode == OP_DRAINING) {
  434. dprintk(CVP_SYNX, "%s: flush in progress\n", __func__);
  435. rc = -EBUSY;
  436. goto exit;
  437. }
  438. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  439. rc = cvp_alloc_fence_data((&f), cmd_hdr->size);
  440. if (rc) {
  441. dprintk(CVP_ERR,"%s: Failed to alloc fence data", __func__);
  442. goto exit;
  443. }
  444. f->type = cmd_hdr->packet_type;
  445. f->mode = OP_NORMAL;
  446. f->signature = 0xFEEDFACE;
  447. f->num_fences = 0;
  448. f->output_index = 0;
  449. buf_offset = offset;
  450. if (cvp_kernel_fence_enabled == 0)
  451. {
  452. goto soc_fence;
  453. }
  454. else if (cvp_kernel_fence_enabled == 1)
  455. {
  456. goto kernel_fence;
  457. }
  458. else if (cvp_kernel_fence_enabled == 2)
  459. {
  460. if (override == true)
  461. goto kernel_fence;
  462. else if (override == false)
  463. goto soc_fence;
  464. else
  465. {
  466. dprintk(CVP_ERR, "%s: invalid params", __func__);
  467. rc = -EINVAL;
  468. goto free_exit;
  469. }
  470. }
  471. else
  472. {
  473. dprintk(CVP_ERR, "%s: invalid params", __func__);
  474. rc = -EINVAL;
  475. goto free_exit;
  476. }
  477. soc_fence:
  478. for (i = 0; i < num; i++) {
  479. buf = (struct cvp_buf_type*)&in_pkt->pkt_data[buf_offset];
  480. buf_offset += sizeof(*buf) >> 2;
  481. if (buf->input_handle || buf->output_handle) {
  482. f->num_fences++;
  483. if (buf->input_handle)
  484. f->output_index++;
  485. }
  486. }
  487. f->signature = 0xB0BABABE;
  488. if (f->num_fences)
  489. goto fence_cmd_queue;
  490. goto free_exit;
  491. kernel_fence:
  492. /* First pass to find INPUT synx handles */
  493. for (i = 0; i < num; i++) {
  494. buf = (struct cvp_buf_type *)&in_pkt->pkt_data[buf_offset];
  495. buf_offset += sizeof(*buf) >> 2;
  496. if (buf->input_handle) {
  497. /* Check fence_type? */
  498. fences[f->num_fences].h_synx = buf->input_handle;
  499. f->num_fences++;
  500. buf->fence_type &= ~INPUT_FENCE_BITMASK;
  501. buf->input_handle = 0;
  502. total_fence_count++;
  503. }
  504. if (buf->output_handle)
  505. total_fence_count++;
  506. }
  507. if (total_fence_count > MAX_HFI_FENCE_SIZE) {
  508. dprintk(CVP_ERR, "Invalid total_fence_count %d\n", total_fence_count);
  509. rc = -EINVAL;
  510. goto free_exit;
  511. }
  512. f->output_index = f->num_fences;
  513. dprintk(CVP_SYNX, "%s:Input Fence passed - Number of Fences is %d\n",
  514. __func__, f->num_fences);
  515. /*
  516. * Second pass to find OUTPUT synx handle
  517. * If no of fences is 0 dont execute the below portion until line 911, return 0
  518. */
  519. buf_offset = offset;
  520. for (i = 0; i < num; i++) {
  521. buf = (struct cvp_buf_type*)&in_pkt->pkt_data[buf_offset];
  522. buf_offset += sizeof(*buf) >> 2;
  523. if (buf->output_handle) {
  524. /* Check fence_type? */
  525. fences[f->num_fences].h_synx = buf->output_handle;
  526. f->num_fences++;
  527. buf->fence_type &= ~OUTPUT_FENCE_BITMASK;
  528. buf->output_handle = 0;
  529. }
  530. }
  531. dprintk(CVP_SYNX, "%s:Output Fence passed - Number of Fences is %d\n",
  532. __func__, f->num_fences);
  533. if (f->num_fences == 0)
  534. goto free_exit;
  535. rc = inst->core->synx_ftbl->cvp_import_synx(inst, f, (u32*)fences);
  536. if (rc) {
  537. dprintk(CVP_ERR,"%s: Failed to import fences", __func__);
  538. goto free_exit;
  539. }
  540. fence_cmd_queue:
  541. fence_cnt = f->num_fences;
  542. memcpy(f->pkt, cmd_hdr, cmd_hdr->size);
  543. f->pkt->client_data.kdata |= FENCE_BIT;
  544. mutex_lock(&q->lock);
  545. list_add_tail(&f->list, &inst->fence_cmd_queue.wait_list);
  546. mutex_unlock(&q->lock);
  547. wake_up(&inst->fence_cmd_queue.wq);
  548. return fence_cnt;
  549. free_exit:
  550. cvp_free_fence_data(f);
  551. exit:
  552. return rc;
  553. }
  554. static int cvp_enqueue_pkt(struct msm_cvp_inst* inst,
  555. struct eva_kmd_hfi_packet *in_pkt,
  556. unsigned int in_offset,
  557. unsigned int in_buf_num)
  558. {
  559. struct cvp_hfi_ops *ops_tbl;
  560. struct cvp_hfi_cmd_session_hdr *cmd_hdr;
  561. int pkt_type, rc = 0;
  562. enum buf_map_type map_type;
  563. ops_tbl = inst->core->dev_ops;
  564. pkt_type = in_pkt->pkt_data[1];
  565. map_type = cvp_find_map_type(pkt_type);
  566. cmd_hdr = (struct cvp_hfi_cmd_session_hdr *)in_pkt;
  567. /* The kdata will be overriden by transaction ID if the cmd has buf */
  568. cmd_hdr->client_data.kdata = 0;
  569. dprintk(CVP_CMD, "%s: "
  570. "pkt_type %08x sess_id %08x trans_id %u ktid %llu\n",
  571. __func__, cmd_hdr->packet_type,
  572. cmd_hdr->session_id,
  573. cmd_hdr->client_data.transaction_id,
  574. cmd_hdr->client_data.kdata & (FENCE_BIT - 1));
  575. if (map_type == MAP_PERSIST)
  576. rc = msm_cvp_map_user_persist(inst, in_pkt, in_offset, in_buf_num);
  577. else if (map_type == UNMAP_PERSIST)
  578. rc = msm_cvp_unmap_user_persist(inst, in_pkt, in_offset, in_buf_num);
  579. else
  580. rc = msm_cvp_map_frame(inst, in_pkt, in_offset, in_buf_num);
  581. if (rc)
  582. return rc;
  583. rc = cvp_populate_fences(in_pkt, in_offset, in_buf_num, inst);
  584. if (rc == 0) {
  585. rc = call_hfi_op(ops_tbl, session_send, (void *)inst->session,
  586. in_pkt);
  587. if (rc) {
  588. dprintk(CVP_ERR,"%s: Failed in call_hfi_op %d, %x\n",
  589. __func__, in_pkt->pkt_data[0],
  590. in_pkt->pkt_data[1]);
  591. if (map_type == MAP_FRAME)
  592. msm_cvp_unmap_frame(inst,
  593. cmd_hdr->client_data.kdata);
  594. }
  595. } else if (rc > 0) {
  596. dprintk(CVP_SYNX, "Going fenced path\n");
  597. rc = 0;
  598. } else {
  599. dprintk(CVP_ERR,"%s: Failed to populate fences\n",
  600. __func__);
  601. if (map_type == MAP_FRAME)
  602. msm_cvp_unmap_frame(inst, cmd_hdr->client_data.kdata);
  603. }
  604. return rc;
  605. }
  606. static inline int div_by_1dot5(unsigned int a)
  607. {
  608. unsigned long i = a << 1;
  609. return (unsigned int) i/3;
  610. }
  611. int msm_cvp_session_delete(struct msm_cvp_inst *inst)
  612. {
  613. return 0;
  614. }
  615. int msm_cvp_session_create(struct msm_cvp_inst *inst)
  616. {
  617. int rc = 0, rc1 = 0;
  618. struct cvp_session_queue *sq;
  619. if (!inst || !inst->core)
  620. return -EINVAL;
  621. if (inst->state >= MSM_CVP_CLOSE_DONE)
  622. return -ECONNRESET;
  623. if (inst->state != MSM_CVP_CORE_INIT_DONE ||
  624. inst->state > MSM_CVP_OPEN_DONE) {
  625. dprintk(CVP_ERR,
  626. "%s Incorrect CVP state %d to create session\n",
  627. __func__, inst->state);
  628. return -EINVAL;
  629. }
  630. rc = msm_cvp_comm_try_state(inst, MSM_CVP_OPEN_DONE);
  631. if (rc) {
  632. dprintk(CVP_ERR,
  633. "Failed to move instance to open done state\n");
  634. goto fail_create;
  635. }
  636. rc = cvp_comm_set_arp_buffers(inst);
  637. if (rc) {
  638. dprintk(CVP_ERR,
  639. "Failed to set ARP buffers\n");
  640. goto fail_init;
  641. }
  642. inst->core->synx_ftbl->cvp_sess_init_synx(inst);
  643. sq = &inst->session_queue;
  644. spin_lock(&sq->lock);
  645. sq->state = QUEUE_ACTIVE;
  646. spin_unlock(&sq->lock);
  647. return rc;
  648. fail_init:
  649. rc1 = msm_cvp_comm_try_state(inst, MSM_CVP_CLOSE_DONE);
  650. if (rc1)
  651. dprintk(CVP_ERR, "%s: close failed\n", __func__);
  652. fail_create:
  653. return rc;
  654. }
  655. static int session_state_check_init(struct msm_cvp_inst *inst)
  656. {
  657. mutex_lock(&inst->lock);
  658. if (inst->state == MSM_CVP_OPEN || inst->state == MSM_CVP_OPEN_DONE) {
  659. mutex_unlock(&inst->lock);
  660. return 0;
  661. }
  662. mutex_unlock(&inst->lock);
  663. return msm_cvp_session_create(inst);
  664. }
  665. static int cvp_fence_thread_start(struct msm_cvp_inst *inst)
  666. {
  667. u32 tnum = 0;
  668. u32 i = 0;
  669. int rc = 0;
  670. char tname[16];
  671. struct task_struct *thread;
  672. struct cvp_fence_queue *q;
  673. struct cvp_session_queue *sq;
  674. if (!inst->prop.fthread_nr)
  675. return 0;
  676. q = &inst->fence_cmd_queue;
  677. mutex_lock(&q->lock);
  678. q->state = QUEUE_START;
  679. mutex_unlock(&q->lock);
  680. for (i = 0; i < inst->prop.fthread_nr; ++i) {
  681. if (!cvp_get_inst_validate(inst->core, inst)) {
  682. rc = -ECONNRESET;
  683. goto exit;
  684. }
  685. snprintf(tname, sizeof(tname), "fthread_%d", tnum++);
  686. thread = kthread_run(cvp_fence_thread, inst, tname);
  687. if (!thread) {
  688. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  689. rc = -ECHILD;
  690. goto exit;
  691. }
  692. }
  693. sq = &inst->session_queue_fence;
  694. spin_lock(&sq->lock);
  695. sq->state = QUEUE_START;
  696. spin_unlock(&sq->lock);
  697. exit:
  698. if (rc) {
  699. mutex_lock(&q->lock);
  700. q->state = QUEUE_STOP;
  701. mutex_unlock(&q->lock);
  702. wake_up_all(&q->wq);
  703. }
  704. return rc;
  705. }
  706. static int cvp_fence_thread_stop(struct msm_cvp_inst *inst)
  707. {
  708. struct cvp_fence_queue *q;
  709. struct cvp_session_queue *sq;
  710. if (!inst->prop.fthread_nr)
  711. return 0;
  712. q = &inst->fence_cmd_queue;
  713. mutex_lock(&q->lock);
  714. q->state = QUEUE_STOP;
  715. mutex_unlock(&q->lock);
  716. sq = &inst->session_queue_fence;
  717. spin_lock(&sq->lock);
  718. sq->state = QUEUE_STOP;
  719. spin_unlock(&sq->lock);
  720. wake_up_all(&q->wq);
  721. wake_up_all(&sq->wq);
  722. return 0;
  723. }
  724. int msm_cvp_session_start(struct msm_cvp_inst *inst,
  725. struct eva_kmd_arg *arg)
  726. {
  727. struct cvp_session_queue *sq;
  728. struct cvp_hfi_ops *ops_tbl;
  729. int rc;
  730. enum queue_state old_state;
  731. if (!inst || !inst->core) {
  732. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  733. return -EINVAL;
  734. }
  735. sq = &inst->session_queue;
  736. spin_lock(&sq->lock);
  737. if (sq->msg_count) {
  738. dprintk(CVP_ERR, "session start failed queue not empty%d\n",
  739. sq->msg_count);
  740. spin_unlock(&sq->lock);
  741. rc = -EINVAL;
  742. goto exit;
  743. }
  744. old_state = sq->state;
  745. sq->state = QUEUE_START;
  746. spin_unlock(&sq->lock);
  747. ops_tbl = inst->core->dev_ops;
  748. if (inst->prop.type == HFI_SESSION_FD
  749. || inst->prop.type == HFI_SESSION_DMM) {
  750. spin_lock(&inst->core->resources.pm_qos.lock);
  751. inst->core->resources.pm_qos.off_vote_cnt++;
  752. spin_unlock(&inst->core->resources.pm_qos.lock);
  753. call_hfi_op(ops_tbl, pm_qos_update, ops_tbl->hfi_device_data);
  754. }
  755. /*
  756. * cvp_fence_thread_start will increment reference to instance.
  757. * It guarantees the EVA session won't be deleted. Use of session
  758. * functions, such as session_start requires the session to be valid.
  759. */
  760. rc = cvp_fence_thread_start(inst);
  761. if (rc)
  762. goto restore_state;
  763. /* Send SESSION_START command */
  764. rc = call_hfi_op(ops_tbl, session_start, (void *)inst->session);
  765. if (rc) {
  766. dprintk(CVP_WARN, "%s: session start failed rc %d\n",
  767. __func__, rc);
  768. goto stop_thread;
  769. }
  770. /* Wait for FW response */
  771. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_START_DONE);
  772. if (rc) {
  773. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  774. __func__, rc);
  775. goto stop_thread;
  776. }
  777. pr_info_ratelimited(CVP_DBG_TAG "session %llx (%#x) started\n",
  778. "sess", inst, hash32_ptr(inst->session));
  779. return 0;
  780. stop_thread:
  781. cvp_fence_thread_stop(inst);
  782. restore_state:
  783. spin_lock(&sq->lock);
  784. sq->state = old_state;
  785. spin_unlock(&sq->lock);
  786. exit:
  787. return rc;
  788. }
  789. int msm_cvp_session_stop(struct msm_cvp_inst *inst,
  790. struct eva_kmd_arg *arg)
  791. {
  792. struct cvp_session_queue *sq;
  793. struct eva_kmd_session_control *sc = NULL;
  794. struct msm_cvp_inst *s;
  795. struct cvp_hfi_ops *ops_tbl;
  796. int rc = 0;
  797. int curr_sq_state = -1;
  798. if (!inst || !inst->core) {
  799. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  800. return -EINVAL;
  801. }
  802. if (arg)
  803. sc = &arg->data.session_ctrl;
  804. s = cvp_get_inst_validate(inst->core, inst);
  805. if (!s)
  806. return -ECONNRESET;
  807. sq = &inst->session_queue;
  808. curr_sq_state = sq->state;
  809. spin_lock(&sq->lock);
  810. if (sq->state != QUEUE_START) {
  811. spin_unlock(&sq->lock);
  812. dprintk(CVP_ERR,
  813. "%s: Stop not allowed - curr state %d, inst %llx, sess %llx, %s type %d\n",
  814. __func__, sq->state, inst, inst->session, inst->proc_name,
  815. inst->session_type);
  816. rc = -EINVAL;
  817. return rc;
  818. }
  819. if (sq->state == QUEUE_STOP) {
  820. spin_unlock(&sq->lock);
  821. dprintk(CVP_WARN,
  822. "%s: Double stop session - inst %llx, sess %llx, %s of type %d\n",
  823. __func__, inst, inst->session, inst->proc_name, inst->session_type);
  824. return rc;
  825. }
  826. if (sq->msg_count) {
  827. dprintk(CVP_ERR, "session stop incorrect: queue not empty%d\n",
  828. sq->msg_count);
  829. if (sc)
  830. sc->ctrl_data[0] = sq->msg_count;
  831. spin_unlock(&sq->lock);
  832. rc = -EUCLEAN;
  833. goto exit;
  834. }
  835. sq->state = QUEUE_STOP;
  836. pr_info_ratelimited(CVP_DBG_TAG "Stop session: %pK session_id = %#x\n",
  837. "sess", inst, hash32_ptr(inst->session));
  838. spin_unlock(&sq->lock);
  839. ops_tbl = inst->core->dev_ops;
  840. /* Send SESSION_STOP command */
  841. rc = call_hfi_op(ops_tbl, session_stop, (void *)inst->session);
  842. if (rc) {
  843. dprintk(CVP_WARN, "%s: session stop failed rc %d\n",
  844. __func__, rc);
  845. spin_lock(&sq->lock);
  846. sq->state = curr_sq_state;
  847. spin_unlock(&sq->lock);
  848. goto stop_thread;
  849. }
  850. /* Wait for FW response */
  851. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_STOP_DONE);
  852. if (rc) {
  853. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  854. __func__, rc);
  855. spin_lock(&sq->lock);
  856. sq->state = curr_sq_state;
  857. spin_unlock(&sq->lock);
  858. goto stop_thread;
  859. }
  860. stop_thread:
  861. wake_up_all(&inst->session_queue.wq);
  862. cvp_fence_thread_stop(inst);
  863. exit:
  864. cvp_put_inst(s);
  865. return rc;
  866. }
  867. int msm_cvp_session_queue_stop(struct msm_cvp_inst *inst)
  868. {
  869. struct cvp_session_queue *sq;
  870. sq = &inst->session_queue;
  871. spin_lock(&sq->lock);
  872. if (sq->state == QUEUE_STOP) {
  873. spin_unlock(&sq->lock);
  874. return 0;
  875. }
  876. dprintk(CVP_SESS, "Stop session queue: %pK session_id = %#x\n",
  877. inst, hash32_ptr(inst->session));
  878. spin_unlock(&sq->lock);
  879. wake_up_all(&inst->session_queue.wq);
  880. return cvp_fence_thread_stop(inst);
  881. }
  882. static int msm_cvp_session_ctrl(struct msm_cvp_inst *inst,
  883. struct eva_kmd_arg *arg)
  884. {
  885. struct eva_kmd_session_control *ctrl = &arg->data.session_ctrl;
  886. int rc = 0;
  887. unsigned int ctrl_type;
  888. ctrl_type = ctrl->ctrl_type;
  889. if (!inst && ctrl_type != SESSION_CREATE) {
  890. dprintk(CVP_ERR, "%s invalid session\n", __func__);
  891. return -EINVAL;
  892. }
  893. switch (ctrl_type) {
  894. case SESSION_STOP:
  895. rc = msm_cvp_session_stop(inst, arg);
  896. break;
  897. case SESSION_START:
  898. rc = msm_cvp_session_start(inst, arg);
  899. break;
  900. case SESSION_CREATE:
  901. rc = msm_cvp_session_create(inst);
  902. break;
  903. case SESSION_DELETE:
  904. rc = msm_cvp_session_delete(inst);
  905. break;
  906. case SESSION_INFO:
  907. default:
  908. dprintk(CVP_ERR, "%s Unsupported session ctrl%d\n",
  909. __func__, ctrl->ctrl_type);
  910. rc = -EINVAL;
  911. }
  912. return rc;
  913. }
  914. static int msm_cvp_get_sysprop(struct msm_cvp_inst *inst,
  915. struct eva_kmd_arg *arg)
  916. {
  917. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  918. struct cvp_hfi_ops *ops_tbl;
  919. struct iris_hfi_device *hfi;
  920. struct cvp_session_prop *session_prop;
  921. int i, rc = 0;
  922. if (!inst || !inst->core || !inst->core->dev_ops) {
  923. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  924. return -EINVAL;
  925. }
  926. ops_tbl = inst->core->dev_ops;
  927. hfi = ops_tbl->hfi_device_data;
  928. if (props->prop_num > MAX_KMD_PROP_NUM_PER_PACKET) {
  929. dprintk(CVP_ERR, "Too many properties %d to get\n",
  930. props->prop_num);
  931. return -E2BIG;
  932. }
  933. session_prop = &inst->prop;
  934. for (i = 0; i < props->prop_num; i++) {
  935. switch (props->prop_data[i].prop_type) {
  936. case EVA_KMD_PROP_HFI_VERSION:
  937. {
  938. props->prop_data[i].data = hfi->version;
  939. break;
  940. }
  941. case EVA_KMD_PROP_SESSION_DUMPOFFSET:
  942. {
  943. props->prop_data[i].data =
  944. session_prop->dump_offset;
  945. break;
  946. }
  947. case EVA_KMD_PROP_SESSION_DUMPSIZE:
  948. {
  949. props->prop_data[i].data =
  950. session_prop->dump_size;
  951. break;
  952. }
  953. case EVA_KMD_PROP_SESSION_ERROR:
  954. {
  955. get_dma_buf(hfi->sfr.mem_data.dma_buf);
  956. rc = dma_buf_fd(hfi->sfr.mem_data.dma_buf, O_RDONLY | O_CLOEXEC);
  957. if (rc < 0) {
  958. dprintk(CVP_WARN, "Failed get dma_buf fd %d\n", rc);
  959. break;
  960. }
  961. props->prop_data[i].data = rc;
  962. rc = 0;
  963. break;
  964. }
  965. case EVA_KMD_PROP_PWR_FDU:
  966. {
  967. props->prop_data[i].data =
  968. msm_cvp_get_hw_aggregate_cycles(HFI_HW_FDU);
  969. break;
  970. }
  971. case EVA_KMD_PROP_PWR_ICA:
  972. {
  973. props->prop_data[i].data =
  974. msm_cvp_get_hw_aggregate_cycles(HFI_HW_ICA);
  975. break;
  976. }
  977. case EVA_KMD_PROP_PWR_OD:
  978. {
  979. props->prop_data[i].data =
  980. msm_cvp_get_hw_aggregate_cycles(HFI_HW_OD);
  981. break;
  982. }
  983. case EVA_KMD_PROP_PWR_MPU:
  984. {
  985. props->prop_data[i].data =
  986. msm_cvp_get_hw_aggregate_cycles(HFI_HW_MPU);
  987. break;
  988. }
  989. case EVA_KMD_PROP_PWR_VADL:
  990. {
  991. props->prop_data[i].data =
  992. msm_cvp_get_hw_aggregate_cycles(HFI_HW_VADL);
  993. break;
  994. }
  995. case EVA_KMD_PROP_PWR_TOF:
  996. {
  997. props->prop_data[i].data =
  998. msm_cvp_get_hw_aggregate_cycles(HFI_HW_TOF);
  999. break;
  1000. }
  1001. case EVA_KMD_PROP_PWR_RGE:
  1002. {
  1003. props->prop_data[i].data =
  1004. msm_cvp_get_hw_aggregate_cycles(HFI_HW_RGE);
  1005. break;
  1006. }
  1007. case EVA_KMD_PROP_PWR_XRA:
  1008. {
  1009. props->prop_data[i].data =
  1010. msm_cvp_get_hw_aggregate_cycles(HFI_HW_XRA);
  1011. break;
  1012. }
  1013. case EVA_KMD_PROP_PWR_LSR:
  1014. {
  1015. props->prop_data[i].data =
  1016. msm_cvp_get_hw_aggregate_cycles(HFI_HW_LSR);
  1017. break;
  1018. }
  1019. default:
  1020. dprintk(CVP_ERR, "unrecognized sys property %d\n",
  1021. props->prop_data[i].prop_type);
  1022. rc = -EFAULT;
  1023. }
  1024. }
  1025. return rc;
  1026. }
  1027. static int msm_cvp_set_sysprop(struct msm_cvp_inst *inst,
  1028. struct eva_kmd_arg *arg)
  1029. {
  1030. struct eva_kmd_sys_properties *props = &arg->data.sys_properties;
  1031. struct eva_kmd_sys_property *prop_array;
  1032. struct cvp_session_prop *session_prop;
  1033. int i, rc = 0;
  1034. if (!inst) {
  1035. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1036. return -EINVAL;
  1037. }
  1038. if (props->prop_num > MAX_KMD_PROP_NUM_PER_PACKET) {
  1039. dprintk(CVP_ERR, "Too many properties %d to set\n",
  1040. props->prop_num);
  1041. return -E2BIG;
  1042. }
  1043. prop_array = &arg->data.sys_properties.prop_data[0];
  1044. session_prop = &inst->prop;
  1045. for (i = 0; i < props->prop_num; i++) {
  1046. switch (prop_array[i].prop_type) {
  1047. case EVA_KMD_PROP_SESSION_TYPE:
  1048. session_prop->type = prop_array[i].data;
  1049. break;
  1050. case EVA_KMD_PROP_SESSION_KERNELMASK:
  1051. session_prop->kernel_mask = prop_array[i].data;
  1052. break;
  1053. case EVA_KMD_PROP_SESSION_PRIORITY:
  1054. session_prop->priority = prop_array[i].data;
  1055. break;
  1056. case EVA_KMD_PROP_SESSION_SECURITY:
  1057. session_prop->is_secure = prop_array[i].data;
  1058. break;
  1059. case EVA_KMD_PROP_SESSION_DSPMASK:
  1060. session_prop->dsp_mask = prop_array[i].data;
  1061. break;
  1062. case EVA_KMD_PROP_PWR_FDU:
  1063. session_prop->cycles[HFI_HW_FDU] = prop_array[i].data;
  1064. break;
  1065. case EVA_KMD_PROP_PWR_ICA:
  1066. session_prop->cycles[HFI_HW_ICA] =
  1067. div_by_1dot5(prop_array[i].data);
  1068. break;
  1069. case EVA_KMD_PROP_PWR_OD:
  1070. session_prop->cycles[HFI_HW_OD] = prop_array[i].data;
  1071. break;
  1072. case EVA_KMD_PROP_PWR_MPU:
  1073. session_prop->cycles[HFI_HW_MPU] = prop_array[i].data;
  1074. break;
  1075. case EVA_KMD_PROP_PWR_VADL:
  1076. session_prop->cycles[HFI_HW_VADL] = prop_array[i].data;
  1077. break;
  1078. case EVA_KMD_PROP_PWR_TOF:
  1079. session_prop->cycles[HFI_HW_TOF] = prop_array[i].data;
  1080. break;
  1081. case EVA_KMD_PROP_PWR_RGE:
  1082. session_prop->cycles[HFI_HW_RGE] = prop_array[i].data;
  1083. break;
  1084. case EVA_KMD_PROP_PWR_XRA:
  1085. session_prop->cycles[HFI_HW_XRA] = prop_array[i].data;
  1086. break;
  1087. case EVA_KMD_PROP_PWR_LSR:
  1088. session_prop->cycles[HFI_HW_LSR] = prop_array[i].data;
  1089. break;
  1090. case EVA_KMD_PROP_PWR_FW:
  1091. session_prop->fw_cycles =
  1092. div_by_1dot5(prop_array[i].data);
  1093. break;
  1094. case EVA_KMD_PROP_PWR_DDR:
  1095. session_prop->ddr_bw = prop_array[i].data;
  1096. break;
  1097. case EVA_KMD_PROP_PWR_SYSCACHE:
  1098. session_prop->ddr_cache = prop_array[i].data;
  1099. break;
  1100. case EVA_KMD_PROP_PWR_FDU_OP:
  1101. session_prop->op_cycles[HFI_HW_FDU] = prop_array[i].data;
  1102. break;
  1103. case EVA_KMD_PROP_PWR_ICA_OP:
  1104. session_prop->op_cycles[HFI_HW_ICA] =
  1105. div_by_1dot5(prop_array[i].data);
  1106. break;
  1107. case EVA_KMD_PROP_PWR_OD_OP:
  1108. session_prop->op_cycles[HFI_HW_OD] = prop_array[i].data;
  1109. break;
  1110. case EVA_KMD_PROP_PWR_MPU_OP:
  1111. session_prop->op_cycles[HFI_HW_MPU] = prop_array[i].data;
  1112. break;
  1113. case EVA_KMD_PROP_PWR_VADL_OP:
  1114. session_prop->op_cycles[HFI_HW_VADL] = prop_array[i].data;
  1115. break;
  1116. case EVA_KMD_PROP_PWR_TOF_OP:
  1117. session_prop->op_cycles[HFI_HW_TOF] = prop_array[i].data;
  1118. break;
  1119. case EVA_KMD_PROP_PWR_RGE_OP:
  1120. session_prop->op_cycles[HFI_HW_RGE] = prop_array[i].data;
  1121. break;
  1122. case EVA_KMD_PROP_PWR_XRA_OP:
  1123. session_prop->op_cycles[HFI_HW_XRA] = prop_array[i].data;
  1124. break;
  1125. case EVA_KMD_PROP_PWR_LSR_OP:
  1126. session_prop->op_cycles[HFI_HW_LSR] = prop_array[i].data;
  1127. break;
  1128. case EVA_KMD_PROP_PWR_FW_OP:
  1129. session_prop->fw_op_cycles =
  1130. div_by_1dot5(prop_array[i].data);
  1131. break;
  1132. case EVA_KMD_PROP_PWR_DDR_OP:
  1133. session_prop->ddr_op_bw = prop_array[i].data;
  1134. break;
  1135. case EVA_KMD_PROP_PWR_SYSCACHE_OP:
  1136. session_prop->ddr_op_cache = prop_array[i].data;
  1137. break;
  1138. case EVA_KMD_PROP_PWR_FPS_FDU:
  1139. session_prop->fps[HFI_HW_FDU] = prop_array[i].data;
  1140. break;
  1141. case EVA_KMD_PROP_PWR_FPS_MPU:
  1142. session_prop->fps[HFI_HW_MPU] = prop_array[i].data;
  1143. break;
  1144. case EVA_KMD_PROP_PWR_FPS_OD:
  1145. session_prop->fps[HFI_HW_OD] = prop_array[i].data;
  1146. break;
  1147. case EVA_KMD_PROP_PWR_FPS_ICA:
  1148. session_prop->fps[HFI_HW_ICA] = prop_array[i].data;
  1149. break;
  1150. case EVA_KMD_PROP_PWR_FPS_VADL:
  1151. session_prop->fps[HFI_HW_VADL] = prop_array[i].data;
  1152. break;
  1153. case EVA_KMD_PROP_PWR_FPS_TOF:
  1154. session_prop->fps[HFI_HW_TOF] = prop_array[i].data;
  1155. break;
  1156. case EVA_KMD_PROP_PWR_FPS_RGE:
  1157. session_prop->fps[HFI_HW_RGE] = prop_array[i].data;
  1158. break;
  1159. case EVA_KMD_PROP_PWR_FPS_XRA:
  1160. session_prop->fps[HFI_HW_XRA] = prop_array[i].data;
  1161. break;
  1162. case EVA_KMD_PROP_PWR_FPS_LSR:
  1163. session_prop->fps[HFI_HW_LSR] = prop_array[i].data;
  1164. break;
  1165. case EVA_KMD_PROP_SESSION_DUMPOFFSET:
  1166. session_prop->dump_offset = prop_array[i].data;
  1167. break;
  1168. case EVA_KMD_PROP_SESSION_DUMPSIZE:
  1169. session_prop->dump_size = prop_array[i].data;
  1170. break;
  1171. default:
  1172. dprintk(CVP_ERR,
  1173. "unrecognized sys property to set %d\n",
  1174. prop_array[i].prop_type);
  1175. rc = -EFAULT;
  1176. }
  1177. }
  1178. return rc;
  1179. }
  1180. static int cvp_drain_fence_sched_list(struct msm_cvp_inst *inst)
  1181. {
  1182. unsigned long wait_time;
  1183. struct cvp_fence_queue *q;
  1184. struct cvp_fence_command *f;
  1185. int rc = 0;
  1186. int count = 0, max_count = 0;
  1187. u64 ktid;
  1188. q = &inst->fence_cmd_queue;
  1189. if (!q)
  1190. return -EINVAL;
  1191. if (list_empty(&q->sched_list))
  1192. return rc;
  1193. mutex_lock(&q->lock);
  1194. list_for_each_entry(f, &q->sched_list, list) {
  1195. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1196. dprintk(CVP_SYNX, "%s: frame %llu %llu is in sched_list\n",
  1197. __func__, ktid, f->frame_id);
  1198. ++count;
  1199. }
  1200. mutex_unlock(&q->lock);
  1201. wait_time = count * 1000;
  1202. wait_time *= inst->core->resources.msm_cvp_hw_rsp_timeout;
  1203. dprintk(CVP_SYNX, "%s: wait %d us for %d fence command\n",
  1204. __func__, wait_time, count);
  1205. count = 0;
  1206. max_count = wait_time / 100;
  1207. retry:
  1208. mutex_lock(&q->lock);
  1209. if (list_empty(&q->sched_list)) {
  1210. mutex_unlock(&q->lock);
  1211. return rc;
  1212. }
  1213. mutex_unlock(&q->lock);
  1214. usleep_range(100, 200);
  1215. ++count;
  1216. if (count < max_count) {
  1217. goto retry;
  1218. } else {
  1219. rc = -ETIMEDOUT;
  1220. dprintk(CVP_ERR, "%s: timed out!\n", __func__);
  1221. }
  1222. return rc;
  1223. }
  1224. static void cvp_clean_fence_queue(struct msm_cvp_inst *inst, int synx_state)
  1225. {
  1226. struct cvp_fence_queue *q;
  1227. struct cvp_fence_command *f, *d;
  1228. u64 ktid;
  1229. q = &inst->fence_cmd_queue;
  1230. if (!q)
  1231. return;
  1232. mutex_lock(&q->lock);
  1233. q->mode = OP_DRAINING;
  1234. if (list_empty(&q->wait_list))
  1235. goto check_sched;
  1236. list_for_each_entry_safe(f, d, &q->wait_list, list) {
  1237. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1238. dprintk(CVP_SYNX, "%s: (%#x) flush frame %llu %llu wait_list\n",
  1239. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1240. list_del_init(&f->list);
  1241. msm_cvp_unmap_frame(inst, f->pkt->client_data.kdata);
  1242. inst->core->synx_ftbl->cvp_cancel_synx(inst, CVP_OUTPUT_SYNX,
  1243. f, synx_state);
  1244. inst->core->synx_ftbl->cvp_release_synx(inst, f);
  1245. cvp_free_fence_data(f);
  1246. }
  1247. check_sched:
  1248. if (list_empty(&q->sched_list)) {
  1249. mutex_unlock(&q->lock);
  1250. return;
  1251. }
  1252. list_for_each_entry(f, &q->sched_list, list) {
  1253. ktid = f->pkt->client_data.kdata & (FENCE_BIT - 1);
  1254. dprintk(CVP_SYNX, "%s: (%#x)flush frame %llu %llu sched_list\n",
  1255. __func__, hash32_ptr(inst->session), ktid, f->frame_id);
  1256. inst->core->synx_ftbl->cvp_cancel_synx(inst, CVP_INPUT_SYNX,
  1257. f, synx_state);
  1258. }
  1259. mutex_unlock(&q->lock);
  1260. }
  1261. int cvp_clean_session_queues(struct msm_cvp_inst *inst)
  1262. {
  1263. struct cvp_fence_queue *q;
  1264. u32 count = 0, max_retries = 100;
  1265. q = &inst->fence_cmd_queue;
  1266. mutex_lock(&q->lock);
  1267. if (q->state == QUEUE_START || q->state == QUEUE_ACTIVE) {
  1268. mutex_unlock(&q->lock);
  1269. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_CANCEL);
  1270. } else {
  1271. dprintk(CVP_WARN, "Incorrect fence cmd queue state %d\n",
  1272. q->state);
  1273. mutex_unlock(&q->lock);
  1274. }
  1275. cvp_fence_thread_stop(inst);
  1276. /* Waiting for all output synx sent */
  1277. retry:
  1278. mutex_lock(&q->lock);
  1279. if (list_empty(&q->sched_list)) {
  1280. mutex_unlock(&q->lock);
  1281. return 0;
  1282. }
  1283. mutex_unlock(&q->lock);
  1284. usleep_range(500, 1000);
  1285. if (++count > max_retries)
  1286. return -EBUSY;
  1287. goto retry;
  1288. }
  1289. static int cvp_flush_all(struct msm_cvp_inst *inst)
  1290. {
  1291. int rc = 0;
  1292. struct msm_cvp_inst *s;
  1293. struct cvp_fence_queue *q;
  1294. struct cvp_hfi_ops *ops_tbl;
  1295. if (!inst || !inst->core) {
  1296. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1297. return -EINVAL;
  1298. }
  1299. s = cvp_get_inst_validate(inst->core, inst);
  1300. if (!s)
  1301. return -ECONNRESET;
  1302. dprintk(CVP_SESS, "session %llx (%#x)flush all starts\n",
  1303. inst, hash32_ptr(inst->session));
  1304. q = &inst->fence_cmd_queue;
  1305. ops_tbl = inst->core->dev_ops;
  1306. cvp_clean_fence_queue(inst, SYNX_STATE_SIGNALED_CANCEL);
  1307. dprintk(CVP_SESS, "%s: (%#x) send flush to fw\n",
  1308. __func__, hash32_ptr(inst->session));
  1309. /* Send flush to FW */
  1310. rc = call_hfi_op(ops_tbl, session_flush, (void *)inst->session);
  1311. if (rc) {
  1312. dprintk(CVP_WARN, "%s: continue flush without fw. rc %d\n",
  1313. __func__, rc);
  1314. goto exit;
  1315. }
  1316. /* Wait for FW response */
  1317. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_FLUSH_DONE);
  1318. if (rc)
  1319. dprintk(CVP_WARN, "%s: wait for signal failed, rc %d\n",
  1320. __func__, rc);
  1321. dprintk(CVP_SESS, "%s: (%#x) received flush from fw\n",
  1322. __func__, hash32_ptr(inst->session));
  1323. exit:
  1324. rc = cvp_drain_fence_sched_list(inst);
  1325. mutex_lock(&q->lock);
  1326. q->mode = OP_NORMAL;
  1327. mutex_unlock(&q->lock);
  1328. cvp_put_inst(s);
  1329. return rc;
  1330. }
  1331. int msm_cvp_handle_syscall(struct msm_cvp_inst *inst, struct eva_kmd_arg *arg)
  1332. {
  1333. int rc = 0;
  1334. if (!inst || !arg) {
  1335. dprintk(CVP_ERR, "%s: invalid args\n", __func__);
  1336. return -EINVAL;
  1337. }
  1338. dprintk(CVP_HFI, "%s: arg->type = %x", __func__, arg->type);
  1339. if (arg->type != EVA_KMD_SESSION_CONTROL &&
  1340. arg->type != EVA_KMD_SET_SYS_PROPERTY &&
  1341. arg->type != EVA_KMD_GET_SYS_PROPERTY) {
  1342. rc = session_state_check_init(inst);
  1343. if (rc) {
  1344. dprintk(CVP_ERR,
  1345. "Incorrect session state %d for command %#x",
  1346. inst->state, arg->type);
  1347. return rc;
  1348. }
  1349. }
  1350. switch (arg->type) {
  1351. case EVA_KMD_GET_SESSION_INFO:
  1352. {
  1353. struct eva_kmd_session_info *session =
  1354. (struct eva_kmd_session_info *)&arg->data.session;
  1355. rc = msm_cvp_get_session_info(inst, &session->session_id);
  1356. break;
  1357. }
  1358. case EVA_KMD_UPDATE_POWER:
  1359. {
  1360. rc = msm_cvp_update_power(inst);
  1361. break;
  1362. }
  1363. case EVA_KMD_REGISTER_BUFFER:
  1364. {
  1365. struct eva_kmd_buffer *buf =
  1366. (struct eva_kmd_buffer *)&arg->data.regbuf;
  1367. rc = msm_cvp_register_buffer(inst, buf);
  1368. break;
  1369. }
  1370. case EVA_KMD_UNREGISTER_BUFFER:
  1371. {
  1372. struct eva_kmd_buffer *buf =
  1373. (struct eva_kmd_buffer *)&arg->data.unregbuf;
  1374. rc = msm_cvp_unregister_buffer(inst, buf);
  1375. break;
  1376. }
  1377. case EVA_KMD_RECEIVE_MSG_PKT:
  1378. {
  1379. struct eva_kmd_hfi_packet *out_pkt =
  1380. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1381. rc = msm_cvp_session_receive_hfi(inst, out_pkt);
  1382. break;
  1383. }
  1384. case EVA_KMD_SEND_CMD_PKT:
  1385. {
  1386. struct eva_kmd_hfi_packet *in_pkt =
  1387. (struct eva_kmd_hfi_packet *)&arg->data.hfi_pkt;
  1388. rc = msm_cvp_session_process_hfi(inst, in_pkt,
  1389. arg->buf_offset, arg->buf_num);
  1390. break;
  1391. }
  1392. case EVA_KMD_SEND_FENCE_CMD_PKT:
  1393. {
  1394. rc = msm_cvp_session_process_hfi_fence(inst, arg);
  1395. break;
  1396. }
  1397. case EVA_KMD_SESSION_CONTROL:
  1398. rc = msm_cvp_session_ctrl(inst, arg);
  1399. break;
  1400. case EVA_KMD_GET_SYS_PROPERTY:
  1401. rc = msm_cvp_get_sysprop(inst, arg);
  1402. break;
  1403. case EVA_KMD_SET_SYS_PROPERTY:
  1404. rc = msm_cvp_set_sysprop(inst, arg);
  1405. break;
  1406. case EVA_KMD_FLUSH_ALL:
  1407. rc = cvp_flush_all(inst);
  1408. break;
  1409. case EVA_KMD_FLUSH_FRAME:
  1410. dprintk(CVP_WARN, "EVA_KMD_FLUSH_FRAME IOCTL deprecated\n");
  1411. rc = 0;
  1412. break;
  1413. default:
  1414. dprintk(CVP_HFI, "%s: unknown arg type %#x\n",
  1415. __func__, arg->type);
  1416. rc = -ENOTSUPP;
  1417. break;
  1418. }
  1419. return rc;
  1420. }
  1421. int msm_cvp_session_deinit(struct msm_cvp_inst *inst)
  1422. {
  1423. int rc = 0;
  1424. struct cvp_hal_session *session;
  1425. if (!inst || !inst->core) {
  1426. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1427. return -EINVAL;
  1428. }
  1429. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1430. inst, hash32_ptr(inst->session));
  1431. session = (struct cvp_hal_session *)inst->session;
  1432. if (!session || session == (void *)0xdeadbeef)
  1433. return rc;
  1434. rc = msm_cvp_comm_try_state(inst, MSM_CVP_CLOSE_DONE);
  1435. if (rc)
  1436. dprintk(CVP_ERR, "%s: close failed\n", __func__);
  1437. rc = msm_cvp_session_deinit_buffers(inst);
  1438. return rc;
  1439. }
  1440. int msm_cvp_session_init(struct msm_cvp_inst *inst)
  1441. {
  1442. int rc = 0;
  1443. if (!inst) {
  1444. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  1445. return -EINVAL;
  1446. }
  1447. dprintk(CVP_SESS, "%s: inst %pK (%#x)\n", __func__,
  1448. inst, hash32_ptr(inst->session));
  1449. /* set default frequency */
  1450. inst->clk_data.min_freq = 1000;
  1451. inst->clk_data.ddr_bw = 1000;
  1452. inst->clk_data.sys_cache_bw = 1000;
  1453. inst->prop.type = 1;
  1454. inst->prop.kernel_mask = 0xFFFFFFFF;
  1455. inst->prop.priority = 0;
  1456. inst->prop.is_secure = 0;
  1457. inst->prop.dsp_mask = 0;
  1458. inst->prop.fthread_nr = 3;
  1459. return rc;
  1460. }