msm_cvp.c 38 KB

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