msm_cvp_dsp.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/rpmsg.h>
  7. #include <linux/of_platform.h>
  8. #include <linux/of_fdt.h>
  9. #include <soc/qcom/secure_buffer.h>
  10. #include "msm_cvp_core.h"
  11. #include "msm_cvp.h"
  12. #include "cvp_hfi.h"
  13. struct cvp_dsp_apps gfa_cv;
  14. static int hlosVM[HLOS_VM_NUM] = {VMID_HLOS};
  15. static int dspVM[DSP_VM_NUM] = {VMID_HLOS, VMID_CDSP_Q6};
  16. static int dspVMperm[DSP_VM_NUM] = { PERM_READ | PERM_WRITE | PERM_EXEC,
  17. PERM_READ | PERM_WRITE | PERM_EXEC };
  18. static int hlosVMperm[HLOS_VM_NUM] = { PERM_READ | PERM_WRITE | PERM_EXEC };
  19. static int cvp_reinit_dsp(void);
  20. static int cvp_dsp_send_cmd(struct cvp_dsp_cmd_msg *cmd, uint32_t len)
  21. {
  22. int rc = 0;
  23. struct cvp_dsp_apps *me = &gfa_cv;
  24. dprintk(CVP_DSP, "%s: cmd = %d\n", __func__, cmd->type);
  25. if (IS_ERR_OR_NULL(me->chan)) {
  26. dprintk(CVP_ERR, "%s: DSP GLink is not ready\n", __func__);
  27. rc = -EINVAL;
  28. goto exit;
  29. }
  30. rc = rpmsg_send(me->chan->ept, cmd, len);
  31. if (rc) {
  32. dprintk(CVP_ERR, "%s: DSP rpmsg_send failed rc=%d\n",
  33. __func__, rc);
  34. goto exit;
  35. }
  36. exit:
  37. return rc;
  38. }
  39. static int cvp_dsp_send_cmd_sync(struct cvp_dsp_cmd_msg *cmd,
  40. uint32_t len, struct cvp_dsp_rsp_msg *rsp)
  41. {
  42. int rc = 0;
  43. struct cvp_dsp_apps *me = &gfa_cv;
  44. dprintk(CVP_DSP, "%s: cmd = %d\n", __func__, cmd->type);
  45. me->pending_dsp2cpu_rsp.type = cmd->type;
  46. rc = cvp_dsp_send_cmd(cmd, len);
  47. if (rc) {
  48. dprintk(CVP_ERR, "%s: cvp_dsp_send_cmd failed rc=%d\n",
  49. __func__, rc);
  50. goto exit;
  51. }
  52. if (!wait_for_completion_timeout(&me->completions[cmd->type],
  53. msecs_to_jiffies(CVP_DSP_RESPONSE_TIMEOUT))) {
  54. dprintk(CVP_ERR, "%s cmd %d timeout\n", __func__, cmd->type);
  55. rc = -ETIMEDOUT;
  56. goto exit;
  57. }
  58. exit:
  59. rsp->ret = me->pending_dsp2cpu_rsp.ret;
  60. rsp->dsp_state = me->pending_dsp2cpu_rsp.dsp_state;
  61. me->pending_dsp2cpu_rsp.type = CVP_INVALID_RPMSG_TYPE;
  62. return rc;
  63. }
  64. static int cvp_dsp_send_cmd_hfi_queue(phys_addr_t *phys_addr,
  65. uint32_t size_in_bytes,
  66. struct cvp_dsp_rsp_msg *rsp)
  67. {
  68. int rc = 0;
  69. struct cvp_dsp_cmd_msg cmd;
  70. cmd.type = CPU2DSP_SEND_HFI_QUEUE;
  71. cmd.msg_ptr = (uint64_t)phys_addr;
  72. cmd.msg_ptr_len = size_in_bytes;
  73. cmd.ddr_type = of_fdt_get_ddrtype();
  74. if (cmd.ddr_type < 0) {
  75. dprintk(CVP_WARN,
  76. "%s: Incorrect DDR type value %d, use default %d\n",
  77. __func__, cmd.ddr_type, DDR_TYPE_LPDDR5);
  78. /*return -EINVAL;*/
  79. cmd.ddr_type = DDR_TYPE_LPDDR5;
  80. }
  81. dprintk(CVP_DSP,
  82. "%s: address of buffer, PA=0x%pK size_buff=%d ddr_type=%d\n",
  83. __func__, phys_addr, size_in_bytes, cmd.ddr_type);
  84. rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg), rsp);
  85. if (rc) {
  86. dprintk(CVP_ERR,
  87. "%s: cvp_dsp_send_cmd failed rc = %d\n",
  88. __func__, rc);
  89. goto exit;
  90. }
  91. exit:
  92. return rc;
  93. }
  94. static int cvp_hyp_assign_to_dsp(uint64_t addr, uint32_t size)
  95. {
  96. int rc = 0;
  97. struct cvp_dsp_apps *me = &gfa_cv;
  98. if (!me->hyp_assigned) {
  99. rc = hyp_assign_phys(addr, size, hlosVM, HLOS_VM_NUM, dspVM,
  100. dspVMperm, DSP_VM_NUM);
  101. if (rc) {
  102. dprintk(CVP_ERR, "%s failed. rc=%d\n", __func__, rc);
  103. return rc;
  104. }
  105. me->addr = addr;
  106. me->size = size;
  107. me->hyp_assigned = true;
  108. }
  109. return rc;
  110. }
  111. static int cvp_hyp_assign_from_dsp(void)
  112. {
  113. int rc = 0;
  114. struct cvp_dsp_apps *me = &gfa_cv;
  115. if (me->hyp_assigned) {
  116. rc = hyp_assign_phys(me->addr, me->size, dspVM, DSP_VM_NUM,
  117. hlosVM, hlosVMperm, HLOS_VM_NUM);
  118. if (rc) {
  119. dprintk(CVP_ERR, "%s failed. rc=%d\n", __func__, rc);
  120. return rc;
  121. }
  122. me->addr = 0;
  123. me->size = 0;
  124. me->hyp_assigned = false;
  125. }
  126. return rc;
  127. }
  128. static int cvp_dsp_rpmsg_probe(struct rpmsg_device *rpdev)
  129. {
  130. struct cvp_dsp_apps *me = &gfa_cv;
  131. const char *edge_name = NULL;
  132. int ret = 0;
  133. ret = of_property_read_string(rpdev->dev.parent->of_node,
  134. "label", &edge_name);
  135. if (ret) {
  136. dprintk(CVP_ERR, "glink edge 'label' not found in node\n");
  137. return ret;
  138. }
  139. if (strcmp(edge_name, "cdsp")) {
  140. dprintk(CVP_ERR,
  141. "%s: Failed to probe rpmsg device.Node name:%s\n",
  142. __func__, edge_name);
  143. return -EINVAL;
  144. }
  145. mutex_lock(&me->lock);
  146. me->chan = rpdev;
  147. me->state = DSP_PROBED;
  148. complete(&me->completions[CPU2DSP_MAX_CMD]);
  149. mutex_unlock(&me->lock);
  150. return ret;
  151. }
  152. static void cvp_dsp_rpmsg_remove(struct rpmsg_device *rpdev)
  153. {
  154. struct cvp_dsp_apps *me = &gfa_cv;
  155. dprintk(CVP_WARN, "%s: CDSP SSR triggered\n", __func__);
  156. mutex_lock(&me->lock);
  157. cvp_hyp_assign_from_dsp();
  158. me->chan = NULL;
  159. me->state = DSP_UNINIT;
  160. mutex_unlock(&me->lock);
  161. /* kernel driver needs clean all dsp sessions */
  162. }
  163. static int cvp_dsp_rpmsg_callback(struct rpmsg_device *rpdev,
  164. void *data, int len, void *priv, u32 addr)
  165. {
  166. struct cvp_dsp_rsp_msg *rsp = (struct cvp_dsp_rsp_msg *)data;
  167. struct cvp_dsp_apps *me = &gfa_cv;
  168. dprintk(CVP_DSP, "%s: type = 0x%x ret = 0x%x len = 0x%x\n",
  169. __func__, rsp->type, rsp->ret, len);
  170. if (rsp->type < CPU2DSP_MAX_CMD && len == sizeof(*rsp)) {
  171. if (me->pending_dsp2cpu_rsp.type == rsp->type) {
  172. memcpy(&me->pending_dsp2cpu_rsp, rsp,
  173. sizeof(struct cvp_dsp_rsp_msg));
  174. complete(&me->completions[rsp->type]);
  175. } else {
  176. dprintk(CVP_ERR, "%s: CPU2DSP resp %d, pending %d\n",
  177. __func__, rsp->type,
  178. me->pending_dsp2cpu_rsp.type);
  179. goto exit;
  180. }
  181. } else if (rsp->type < CVP_DSP_MAX_CMD &&
  182. len == sizeof(struct cvp_dsp2cpu_cmd_msg)) {
  183. if (me->pending_dsp2cpu_cmd.type != CVP_INVALID_RPMSG_TYPE) {
  184. dprintk(CVP_ERR,
  185. "%s: DSP2CPU cmd:%d pending %d %d expect %d\n",
  186. __func__, rsp->type,
  187. me->pending_dsp2cpu_cmd.type, len,
  188. sizeof(struct cvp_dsp2cpu_cmd_msg));
  189. goto exit;
  190. }
  191. memcpy(&me->pending_dsp2cpu_cmd, rsp,
  192. sizeof(struct cvp_dsp2cpu_cmd_msg));
  193. complete(&me->completions[CPU2DSP_MAX_CMD]);
  194. } else {
  195. dprintk(CVP_ERR, "%s: Invalid type: %d\n", __func__, rsp->type);
  196. return 0;
  197. }
  198. return 0;
  199. exit:
  200. dprintk(CVP_ERR, "concurrent dsp cmd type = %d, rsp type = %d\n",
  201. me->pending_dsp2cpu_cmd.type,
  202. me->pending_dsp2cpu_rsp.type);
  203. return 0;
  204. }
  205. int cvp_dsp_suspend(uint32_t session_flag)
  206. {
  207. int rc = 0;
  208. struct cvp_dsp_cmd_msg cmd;
  209. struct cvp_dsp_apps *me = &gfa_cv;
  210. struct cvp_dsp_rsp_msg rsp;
  211. bool retried = false;
  212. cmd.type = CPU2DSP_SUSPEND;
  213. mutex_lock(&me->lock);
  214. if (me->state != DSP_READY)
  215. goto exit;
  216. retry:
  217. /* Use cvp_dsp_send_cmd_sync after dsp driver is ready */
  218. rc = cvp_dsp_send_cmd_sync(&cmd,
  219. sizeof(struct cvp_dsp_cmd_msg),
  220. &rsp);
  221. if (rc) {
  222. dprintk(CVP_ERR,
  223. "%s: cvp_dsp_send_cmd failed rc = %d\n",
  224. __func__, rc);
  225. goto exit;
  226. }
  227. if (rsp.ret == CPU2DSP_EUNAVAILABLE)
  228. goto fatal_exit;
  229. if (rsp.ret == CPU2DSP_EFATAL) {
  230. if (!retried) {
  231. mutex_unlock(&me->lock);
  232. retried = true;
  233. rc = cvp_reinit_dsp();
  234. mutex_lock(&me->lock);
  235. if (rc)
  236. goto fatal_exit;
  237. else
  238. goto retry;
  239. } else {
  240. goto fatal_exit;
  241. }
  242. }
  243. me->state = DSP_SUSPEND;
  244. goto exit;
  245. fatal_exit:
  246. me->state = DSP_INVALID;
  247. cvp_hyp_assign_from_dsp();
  248. rc = -ENOTSUPP;
  249. exit:
  250. mutex_unlock(&me->lock);
  251. return rc;
  252. }
  253. int cvp_dsp_resume(uint32_t session_flag)
  254. {
  255. int rc = 0;
  256. struct cvp_dsp_cmd_msg cmd;
  257. struct cvp_dsp_apps *me = &gfa_cv;
  258. cmd.type = CPU2DSP_RESUME;
  259. /*
  260. * Deadlock against DSP2CPU_CREATE_SESSION in dsp_thread
  261. * Probably get rid of this entirely as discussed before
  262. */
  263. if (me->state != DSP_SUSPEND)
  264. goto exit;
  265. me->state = DSP_READY;
  266. exit:
  267. return rc;
  268. }
  269. int cvp_dsp_shutdown(uint32_t session_flag)
  270. {
  271. struct cvp_dsp_apps *me = &gfa_cv;
  272. int rc = 0;
  273. struct cvp_dsp_cmd_msg cmd;
  274. struct cvp_dsp_rsp_msg rsp;
  275. cmd.type = CPU2DSP_SHUTDOWN;
  276. mutex_lock(&me->lock);
  277. if (me->state == DSP_INVALID)
  278. goto exit;
  279. me->state = DSP_INACTIVE;
  280. rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg), &rsp);
  281. if (rc) {
  282. dprintk(CVP_ERR,
  283. "%s: cvp_dsp_send_cmd failed with rc = %d\n",
  284. __func__, rc);
  285. cvp_hyp_assign_from_dsp();
  286. goto exit;
  287. }
  288. rc = cvp_hyp_assign_from_dsp();
  289. exit:
  290. mutex_unlock(&me->lock);
  291. return rc;
  292. }
  293. int cvp_dsp_register_buffer(uint32_t session_id, uint32_t buff_fd,
  294. uint32_t buff_fd_size, uint32_t buff_size,
  295. uint32_t buff_offset, uint32_t buff_index,
  296. uint32_t buff_fd_iova)
  297. {
  298. struct cvp_dsp_cmd_msg cmd;
  299. int rc;
  300. struct cvp_dsp_apps *me = &gfa_cv;
  301. struct cvp_dsp_rsp_msg rsp;
  302. bool retried = false;
  303. cmd.type = CPU2DSP_REGISTER_BUFFER;
  304. cmd.session_id = session_id;
  305. cmd.buff_fd = buff_fd;
  306. cmd.buff_fd_size = buff_fd_size;
  307. cmd.buff_size = buff_size;
  308. cmd.buff_offset = buff_offset;
  309. cmd.buff_index = buff_index;
  310. cmd.buff_fd_iova = buff_fd_iova;
  311. dprintk(CVP_DSP,
  312. "%s: type=0x%x, buff_fd_iova=0x%x buff_index=0x%x\n",
  313. __func__, cmd.type, buff_fd_iova,
  314. cmd.buff_index);
  315. dprintk(CVP_DSP, "%s: buff_size=0x%x session_id=0x%x\n",
  316. __func__, cmd.buff_size, cmd.session_id);
  317. mutex_lock(&me->lock);
  318. retry:
  319. rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg), &rsp);
  320. if (rc) {
  321. dprintk(CVP_ERR, "%s send failed rc = %d\n", __func__, rc);
  322. goto exit;
  323. }
  324. if (rsp.ret == CPU2DSP_EFAIL || rsp.ret == CPU2DSP_EUNSUPPORTED) {
  325. dprintk(CVP_WARN, "%s, DSP return err %d\n", __func__, rsp.ret);
  326. rc = -EINVAL;
  327. goto exit;
  328. }
  329. if (rsp.ret == CPU2DSP_EUNAVAILABLE)
  330. goto fatal_exit;
  331. if (rsp.ret == CPU2DSP_EFATAL) {
  332. if (!retried) {
  333. mutex_unlock(&me->lock);
  334. retried = true;
  335. rc = cvp_reinit_dsp();
  336. mutex_lock(&me->lock);
  337. if (rc)
  338. goto fatal_exit;
  339. else
  340. goto retry;
  341. } else {
  342. goto fatal_exit;
  343. }
  344. }
  345. goto exit;
  346. fatal_exit:
  347. me->state = DSP_INVALID;
  348. cvp_hyp_assign_from_dsp();
  349. rc = -ENOTSUPP;
  350. exit:
  351. mutex_unlock(&me->lock);
  352. return rc;
  353. }
  354. int cvp_dsp_deregister_buffer(uint32_t session_id, uint32_t buff_fd,
  355. uint32_t buff_fd_size, uint32_t buff_size,
  356. uint32_t buff_offset, uint32_t buff_index,
  357. uint32_t buff_fd_iova)
  358. {
  359. struct cvp_dsp_cmd_msg cmd;
  360. int rc;
  361. struct cvp_dsp_apps *me = &gfa_cv;
  362. struct cvp_dsp_rsp_msg rsp;
  363. bool retried = false;
  364. cmd.type = CPU2DSP_DEREGISTER_BUFFER;
  365. cmd.session_id = session_id;
  366. cmd.buff_fd = buff_fd;
  367. cmd.buff_fd_size = buff_fd_size;
  368. cmd.buff_size = buff_size;
  369. cmd.buff_offset = buff_offset;
  370. cmd.buff_index = buff_index;
  371. cmd.buff_fd_iova = buff_fd_iova;
  372. dprintk(CVP_DSP,
  373. "%s: type=0x%x, buff_fd_iova=0x%x buff_index=0x%x\n",
  374. __func__, cmd.type, buff_fd_iova,
  375. cmd.buff_index);
  376. dprintk(CVP_DSP, "%s: buff_size=0x%x session_id=0x%x\n",
  377. __func__, cmd.buff_size, cmd.session_id);
  378. mutex_lock(&me->lock);
  379. retry:
  380. rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg), &rsp);
  381. if (rc) {
  382. dprintk(CVP_ERR, "%s send failed rc = %d\n", __func__, rc);
  383. goto exit;
  384. }
  385. if (rsp.ret == CPU2DSP_EFAIL || rsp.ret == CPU2DSP_EUNSUPPORTED) {
  386. dprintk(CVP_WARN, "%s, DSP return err %d\n", __func__, rsp.ret);
  387. rc = -EINVAL;
  388. goto exit;
  389. }
  390. if (rsp.ret == CPU2DSP_EUNAVAILABLE)
  391. goto fatal_exit;
  392. if (rsp.ret == CPU2DSP_EFATAL) {
  393. if (!retried) {
  394. mutex_unlock(&me->lock);
  395. retried = true;
  396. rc = cvp_reinit_dsp();
  397. mutex_lock(&me->lock);
  398. if (rc)
  399. goto fatal_exit;
  400. else
  401. goto retry;
  402. } else {
  403. goto fatal_exit;
  404. }
  405. }
  406. goto exit;
  407. fatal_exit:
  408. me->state = DSP_INVALID;
  409. cvp_hyp_assign_from_dsp();
  410. rc = -ENOTSUPP;
  411. exit:
  412. mutex_unlock(&me->lock);
  413. return rc;
  414. }
  415. static const struct rpmsg_device_id cvp_dsp_rpmsg_match[] = {
  416. { CVP_APPS_DSP_GLINK_GUID },
  417. { },
  418. };
  419. static struct rpmsg_driver cvp_dsp_rpmsg_client = {
  420. .id_table = cvp_dsp_rpmsg_match,
  421. .probe = cvp_dsp_rpmsg_probe,
  422. .remove = cvp_dsp_rpmsg_remove,
  423. .callback = cvp_dsp_rpmsg_callback,
  424. .drv = {
  425. .name = "qcom,msm_cvp_dsp_rpmsg",
  426. },
  427. };
  428. static void cvp_dsp_set_queue_hdr_defaults(struct cvp_hfi_queue_header *q_hdr)
  429. {
  430. q_hdr->qhdr_status = 0x1;
  431. q_hdr->qhdr_type = CVP_IFACEQ_DFLT_QHDR;
  432. q_hdr->qhdr_q_size = CVP_IFACEQ_QUEUE_SIZE / 4;
  433. q_hdr->qhdr_pkt_size = 0;
  434. q_hdr->qhdr_rx_wm = 0x1;
  435. q_hdr->qhdr_tx_wm = 0x1;
  436. q_hdr->qhdr_rx_req = 0x1;
  437. q_hdr->qhdr_tx_req = 0x0;
  438. q_hdr->qhdr_rx_irq_status = 0x0;
  439. q_hdr->qhdr_tx_irq_status = 0x0;
  440. q_hdr->qhdr_read_idx = 0x0;
  441. q_hdr->qhdr_write_idx = 0x0;
  442. }
  443. void cvp_dsp_init_hfi_queue_hdr(struct iris_hfi_device *device)
  444. {
  445. u32 i;
  446. struct cvp_hfi_queue_table_header *q_tbl_hdr;
  447. struct cvp_hfi_queue_header *q_hdr;
  448. struct cvp_iface_q_info *iface_q;
  449. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  450. iface_q = &device->dsp_iface_queues[i];
  451. iface_q->q_hdr = CVP_IFACEQ_GET_QHDR_START_ADDR(
  452. device->dsp_iface_q_table.align_virtual_addr, i);
  453. cvp_dsp_set_queue_hdr_defaults(iface_q->q_hdr);
  454. }
  455. q_tbl_hdr = (struct cvp_hfi_queue_table_header *)
  456. device->dsp_iface_q_table.align_virtual_addr;
  457. q_tbl_hdr->qtbl_version = 0;
  458. q_tbl_hdr->device_addr = (void *)device;
  459. strlcpy(q_tbl_hdr->name, "msm_cvp", sizeof(q_tbl_hdr->name));
  460. q_tbl_hdr->qtbl_size = CVP_IFACEQ_TABLE_SIZE;
  461. q_tbl_hdr->qtbl_qhdr0_offset =
  462. sizeof(struct cvp_hfi_queue_table_header);
  463. q_tbl_hdr->qtbl_qhdr_size = sizeof(struct cvp_hfi_queue_header);
  464. q_tbl_hdr->qtbl_num_q = CVP_IFACEQ_NUMQ;
  465. q_tbl_hdr->qtbl_num_active_q = CVP_IFACEQ_NUMQ;
  466. iface_q = &device->dsp_iface_queues[CVP_IFACEQ_CMDQ_IDX];
  467. q_hdr = iface_q->q_hdr;
  468. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  469. q_hdr->qhdr_type |= HFI_Q_ID_HOST_TO_CTRL_CMD_Q;
  470. iface_q = &device->dsp_iface_queues[CVP_IFACEQ_MSGQ_IDX];
  471. q_hdr = iface_q->q_hdr;
  472. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  473. q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_MSG_Q;
  474. iface_q = &device->dsp_iface_queues[CVP_IFACEQ_DBGQ_IDX];
  475. q_hdr = iface_q->q_hdr;
  476. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  477. q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_DEBUG_Q;
  478. /*
  479. * Set receive request to zero on debug queue as there is no
  480. * need of interrupt from cvp hardware for debug messages
  481. */
  482. q_hdr->qhdr_rx_req = 0;
  483. }
  484. static int __reinit_dsp(void)
  485. {
  486. int rc;
  487. uint32_t flag = 0;
  488. uint64_t addr;
  489. uint32_t size;
  490. struct cvp_dsp_apps *me = &gfa_cv;
  491. struct cvp_dsp_rsp_msg rsp;
  492. struct msm_cvp_core *core;
  493. struct iris_hfi_device *device;
  494. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  495. if (core && core->device)
  496. device = core->device->hfi_device_data;
  497. else
  498. return -EINVAL;
  499. if (!device) {
  500. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  501. return -EINVAL;
  502. }
  503. /* Force shutdown DSP */
  504. rc = cvp_dsp_shutdown(flag);
  505. if (rc)
  506. return rc;
  507. /* Resend HFI queue */
  508. mutex_lock(&me->lock);
  509. if (!device->dsp_iface_q_table.align_virtual_addr) {
  510. dprintk(CVP_ERR, "%s: DSP HFI queue released\n", __func__);
  511. rc = -EINVAL;
  512. goto exit;
  513. }
  514. addr = (uint64_t)device->dsp_iface_q_table.mem_data.dma_handle;
  515. size = device->dsp_iface_q_table.mem_data.size;
  516. if (!addr || !size) {
  517. dprintk(CVP_DSP, "%s: HFI queue is not ready\n", __func__);
  518. goto exit;
  519. }
  520. rc = cvp_hyp_assign_to_dsp(addr, size);
  521. if (rc) {
  522. dprintk(CVP_ERR, "%s: cvp_hyp_assign_to_dsp. rc=%d\n",
  523. __func__, rc);
  524. goto exit;
  525. }
  526. rc = cvp_dsp_send_cmd_hfi_queue((phys_addr_t *)addr, size, &rsp);
  527. if (rc) {
  528. dprintk(CVP_WARN, "%s: Send HFI Queue failed rc = %d\n",
  529. __func__, rc);
  530. goto exit;
  531. }
  532. if (rsp.ret) {
  533. dprintk(CVP_ERR, "%s: DSP error %d %d\n", __func__,
  534. rsp.ret, rsp.dsp_state);
  535. rc = -ENODEV;
  536. }
  537. exit:
  538. mutex_unlock(&me->lock);
  539. return rc;
  540. }
  541. static int cvp_reinit_dsp(void)
  542. {
  543. int rc;
  544. struct cvp_dsp_apps *me = &gfa_cv;
  545. rc = __reinit_dsp();
  546. if (rc) {
  547. mutex_lock(&me->lock);
  548. me->state = DSP_INVALID;
  549. cvp_hyp_assign_from_dsp();
  550. mutex_unlock(&me->lock);
  551. }
  552. return rc;
  553. }
  554. #ifdef FASTRPC_DRIVER_AVAILABLE
  555. static struct cvp_dsp_fastrpc_driver_entry *cvp_find_fastrpc_node_with_handle(
  556. uint32_t handle)
  557. {
  558. struct cvp_dsp_apps *me = &gfa_cv;
  559. struct list_head *ptr = NULL, *next = NULL;
  560. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  561. mutex_lock(&me->fastrpc_driver_list.lock);
  562. list_for_each_safe(ptr, next, &me->fastrpc_driver_list.list) {
  563. frpc_node = list_entry(ptr,
  564. struct cvp_dsp_fastrpc_driver_entry, list);
  565. if (handle == frpc_node->handle) {
  566. dprintk(CVP_DSP, "Find frpc_node with handle 0x%x\n",
  567. handle);
  568. break;
  569. }
  570. }
  571. mutex_unlock(&me->fastrpc_driver_list.lock);
  572. return frpc_node;
  573. }
  574. static void eva_fastrpc_driver_unregister(struct msm_cvp_inst *inst,
  575. uint32_t handle,
  576. bool force_exit);
  577. static int cvp_fastrpc_probe(struct fastrpc_device *rpc_dev)
  578. {
  579. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  580. dprintk(CVP_DSP, "%s fastrpc probe handle 0x%x\n",
  581. __func__, rpc_dev->handle);
  582. frpc_node = cvp_find_fastrpc_node_with_handle(rpc_dev->handle);
  583. if (frpc_node) {
  584. frpc_node->cvp_fastrpc_device = rpc_dev;
  585. // static structure with signal and pid
  586. complete(&frpc_node->fastrpc_probe_completion);
  587. }
  588. return 0;
  589. }
  590. static int cvp_fastrpc_callback(struct fastrpc_device *rpc_dev,
  591. enum fastrpc_driver_status fastrpc_proc_num)
  592. {
  593. dprintk(CVP_DSP, "%s handle 0x%x, proc %d\n", __func__,
  594. rpc_dev->handle, fastrpc_proc_num);
  595. /* fastrpc drive down when process gone
  596. * any handling can happen here, such as
  597. * eva_fastrpc_driver_unregister(rpc_dev->handle, true);
  598. */
  599. return 0;
  600. }
  601. static struct fastrpc_driver cvp_fastrpc_client = {
  602. .probe = cvp_fastrpc_probe,
  603. .callback = cvp_fastrpc_callback,
  604. .driver = {
  605. .name = "qcom,fastcv",
  606. },
  607. };
  608. static int eva_fastrpc_dev_map_dma(struct fastrpc_device *frpc_device,
  609. struct cvp_internal_buf *buf,
  610. uint32_t dsp_remote_map,
  611. uint64_t *v_dsp_addr)
  612. {
  613. struct fastrpc_dev_map_dma frpc_map_buf = {0};
  614. int rc = 0;
  615. if (dsp_remote_map == 1) {
  616. frpc_map_buf.buf = buf->smem->dma_buf;
  617. frpc_map_buf.size = buf->smem->size;
  618. frpc_map_buf.attrs = 0;
  619. dprintk(CVP_DSP,
  620. "%s frpc_map_buf size %d, dma_buf %pK, map %pK, 0x%x\n",
  621. __func__, frpc_map_buf.size, frpc_map_buf.buf,
  622. &frpc_map_buf, (unsigned long)&frpc_map_buf);
  623. rc = fastrpc_driver_invoke(frpc_device, FASTRPC_DEV_MAP_DMA,
  624. (unsigned long)(&frpc_map_buf));
  625. if (rc) {
  626. dprintk(CVP_ERR,
  627. "%s Failed to map buffer 0x%x\n", __func__, rc);
  628. return rc;
  629. }
  630. buf->fd = (s32)frpc_map_buf.v_dsp_addr;
  631. *v_dsp_addr = frpc_map_buf.v_dsp_addr;
  632. } else {
  633. dprintk(CVP_DSP, "%s Buffer not mapped to dsp\n", __func__);
  634. buf->fd = 0;
  635. }
  636. return rc;
  637. }
  638. static int eva_fastrpc_dev_unmap_dma(struct fastrpc_device *frpc_device,
  639. struct cvp_internal_buf *buf)
  640. {
  641. struct fastrpc_dev_unmap_dma frpc_unmap_buf = {0};
  642. int rc = 0;
  643. /* Only if buffer is mapped to dsp */
  644. if (buf->fd != 0) {
  645. frpc_unmap_buf.buf = buf->smem->dma_buf;
  646. rc = fastrpc_driver_invoke(frpc_device, FASTRPC_DEV_UNMAP_DMA,
  647. (unsigned long)(&frpc_unmap_buf));
  648. if (rc) {
  649. dprintk(CVP_ERR, "%s Failed to unmap buffer 0x%x\n",
  650. __func__, rc);
  651. return rc;
  652. }
  653. } else {
  654. dprintk(CVP_DSP, "%s buffer not mapped to dsp\n", __func__);
  655. }
  656. return rc;
  657. }
  658. static int eva_fastrpc_driver_register(uint32_t handle)
  659. {
  660. struct cvp_dsp_apps *me = &gfa_cv;
  661. int rc = 0;
  662. //struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  663. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  664. frpc_node = cvp_find_fastrpc_node_with_handle(handle);
  665. if (frpc_node == NULL) {
  666. frpc_node = kzalloc(sizeof(*frpc_node), GFP_KERNEL);
  667. if (!frpc_node) {
  668. dprintk(CVP_DSP, "%s allocate frpc node fail\n",
  669. __func__);
  670. return -EINVAL;
  671. }
  672. memset(frpc_node, 0, sizeof(*frpc_node));
  673. /* Init completion */
  674. init_completion(&frpc_node->fastrpc_probe_completion);
  675. mutex_lock(&me->fastrpc_driver_list.lock);
  676. dprintk(CVP_DSP, "Add frpc node 0x%x to list\n", frpc_node);
  677. list_add_tail(&frpc_node->list, &me->fastrpc_driver_list.list);
  678. mutex_unlock(&me->fastrpc_driver_list.lock);
  679. /* register fastrpc device to this session */
  680. frpc_node->handle = handle;
  681. frpc_node->cvp_fastrpc_driver = cvp_fastrpc_client;
  682. frpc_node->cvp_fastrpc_driver.handle = handle;
  683. rc = fastrpc_driver_register(&frpc_node->cvp_fastrpc_driver);
  684. if (rc) {
  685. dprintk(CVP_ERR, "%s fastrpc driver reg fail err %d\n",
  686. __func__, rc);
  687. goto fail_fastrpc_driver_register;
  688. }
  689. /* signal wait reuse dsp timeout setup for now */
  690. if (!wait_for_completion_timeout(
  691. &frpc_node->fastrpc_probe_completion,
  692. msecs_to_jiffies(CVP_DSP_RESPONSE_TIMEOUT))) {
  693. dprintk(CVP_ERR, "%s fastrpc driver_register timeout\n",
  694. __func__);
  695. goto fail_fastrpc_driver_timeout;
  696. }
  697. /* initialize dspbuf list */
  698. INIT_MSM_CVP_LIST(&frpc_node->dspbufs);
  699. }
  700. frpc_node->session_cnt++;
  701. return rc;
  702. fail_fastrpc_driver_timeout:
  703. /* remove list if this is the last session */
  704. mutex_lock(&me->fastrpc_driver_list.lock);
  705. list_del(&frpc_node->list);
  706. mutex_unlock(&me->fastrpc_driver_list.lock);
  707. fastrpc_driver_unregister(&frpc_node->cvp_fastrpc_driver);
  708. fail_fastrpc_driver_register:
  709. kfree(frpc_node);
  710. return -EINVAL;
  711. }
  712. static void eva_fastrpc_driver_unregister(struct msm_cvp_inst *inst,
  713. uint32_t handle,
  714. bool force_exit)
  715. {
  716. struct cvp_dsp_apps *me = &gfa_cv;
  717. struct list_head *ptr = NULL, *next = NULL;
  718. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  719. struct msm_cvp_list *buf_list = NULL;
  720. struct cvp_internal_buf *buf = NULL;
  721. struct fastrpc_device *frpc_device = NULL;
  722. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  723. int rc = 0;
  724. dprintk(CVP_DSP, "%s Unregister fastrpc driver handle 0x%x, force %d\n",
  725. __func__, handle, (uint32_t)force_exit);
  726. /* Foundd fastrpc node */
  727. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  728. if (frpc_node == NULL)
  729. return;
  730. frpc_node->session_cnt--;
  731. if ((frpc_node->session_cnt == 0) || force_exit) {
  732. dprintk(CVP_DSP, "%s session cnt %d, force %d\n",
  733. __func__, frpc_node->session_cnt, (uint32_t)force_exit);
  734. //Free any left over buffers
  735. ptr = NULL;
  736. next = NULL;
  737. buf_list = &frpc_node->dspbufs;
  738. mutex_lock(&buf_list->lock);
  739. list_for_each_safe(ptr, next, &buf_list->list) {
  740. buf = list_entry(ptr, struct cvp_internal_buf, list);
  741. if (!buf->smem) {
  742. dprintk(CVP_DSP, "%s Empyt smem\n", __func__);
  743. list_del(&buf->list);
  744. kfree(buf);
  745. continue;
  746. }
  747. rc = eva_fastrpc_dev_unmap_dma(frpc_device, buf);
  748. if (rc) {
  749. dprintk(CVP_ERR,
  750. "%s Fail to unmap buffer 0x%x\n",
  751. __func__, rc);
  752. continue;
  753. }
  754. rc = cvp_release_dsp_buffers(inst, buf);
  755. if (rc) {
  756. dprintk(CVP_ERR,
  757. "%s Fail to free buffer 0x%x\n",
  758. __func__, rc);
  759. continue;
  760. }
  761. list_del(&buf->list);
  762. kfree(buf);
  763. }
  764. mutex_unlock(&buf_list->lock);
  765. DEINIT_MSM_CVP_LIST(&frpc_node->dspbufs);
  766. /* remove list if this is the last session */
  767. mutex_lock(&me->fastrpc_driver_list.lock);
  768. list_del(&frpc_node->list);
  769. mutex_unlock(&me->fastrpc_driver_list.lock);
  770. fastrpc_driver_unregister(&frpc_node->cvp_fastrpc_driver);
  771. kfree(frpc_node);
  772. }
  773. }
  774. #endif
  775. void cvp_dsp_send_hfi_queue(void)
  776. {
  777. struct msm_cvp_core *core;
  778. struct iris_hfi_device *device;
  779. struct cvp_dsp_apps *me = &gfa_cv;
  780. struct cvp_dsp_rsp_msg rsp = {0};
  781. uint64_t addr;
  782. uint32_t size;
  783. int rc;
  784. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  785. if (core && core->device)
  786. device = core->device->hfi_device_data;
  787. else
  788. return;
  789. if (!device) {
  790. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  791. return;
  792. }
  793. dprintk(CVP_DSP, "Entering %s\n", __func__);
  794. mutex_lock(&device->lock);
  795. mutex_lock(&me->lock);
  796. if (!device->dsp_iface_q_table.align_virtual_addr) {
  797. dprintk(CVP_ERR, "%s: DSP HFI queue released\n", __func__);
  798. mutex_unlock(&me->lock);
  799. mutex_unlock(&device->lock);
  800. return;
  801. }
  802. addr = (uint64_t)device->dsp_iface_q_table.mem_data.dma_handle;
  803. size = device->dsp_iface_q_table.mem_data.size;
  804. if (!addr || !size) {
  805. dprintk(CVP_DSP, "%s: HFI queue is not ready\n", __func__);
  806. goto exit;
  807. }
  808. if (me->state != DSP_PROBED && me->state != DSP_INACTIVE)
  809. goto exit;
  810. rc = cvp_hyp_assign_to_dsp(addr, size);
  811. if (rc) {
  812. dprintk(CVP_ERR, "%s: cvp_hyp_assign_to_dsp. rc=%d\n",
  813. __func__, rc);
  814. goto exit;
  815. }
  816. if (me->state == DSP_PROBED) {
  817. cvp_dsp_init_hfi_queue_hdr(device);
  818. dprintk(CVP_WARN,
  819. "%s: Done init of HFI queue headers\n", __func__);
  820. }
  821. rc = cvp_dsp_send_cmd_hfi_queue((phys_addr_t *)addr, size, &rsp);
  822. if (rc) {
  823. dprintk(CVP_WARN, "%s: Send HFI Queue failed rc = %d\n",
  824. __func__, rc);
  825. goto exit;
  826. }
  827. if (rsp.ret == CPU2DSP_EUNSUPPORTED) {
  828. dprintk(CVP_WARN, "%s unsupported cmd %d\n",
  829. __func__, rsp.type);
  830. goto exit;
  831. }
  832. if (rsp.ret == CPU2DSP_EFATAL || rsp.ret == CPU2DSP_EUNAVAILABLE) {
  833. dprintk(CVP_ERR, "%s fatal error returned %d\n",
  834. __func__, rsp.dsp_state);
  835. me->state = DSP_INVALID;
  836. cvp_hyp_assign_from_dsp();
  837. goto exit;
  838. } else if (rsp.ret == CPU2DSP_EINVALSTATE) {
  839. dprintk(CVP_ERR, "%s dsp invalid state %d\n",
  840. __func__, rsp.dsp_state);
  841. mutex_unlock(&me->lock);
  842. if (cvp_reinit_dsp()) {
  843. dprintk(CVP_ERR, "%s reinit dsp fail\n", __func__);
  844. mutex_unlock(&device->lock);
  845. return;
  846. }
  847. mutex_lock(&me->lock);
  848. }
  849. dprintk(CVP_DSP, "%s: dsp initialized\n", __func__);
  850. me->state = DSP_READY;
  851. exit:
  852. mutex_unlock(&me->lock);
  853. mutex_unlock(&device->lock);
  854. }
  855. /* 32 or 64 bit CPU Side Ptr <-> 2 32 bit DSP Pointers. Dirty Fix. */
  856. static void *ptr_dsp2cpu(uint32_t session_cpu_high, uint32_t session_cpu_low)
  857. {
  858. void *inst;
  859. if ((session_cpu_high == 0) && (sizeof(void *) == BITPTRSIZE32)) {
  860. inst = (void *)((uintptr_t)session_cpu_low);
  861. } else if ((session_cpu_high != 0) && (sizeof(void *) == BITPTRSIZE64)) {
  862. inst = (void *)((uintptr_t)(((uint64_t)session_cpu_high) << 32
  863. | session_cpu_low));
  864. } else {
  865. dprintk(CVP_ERR,
  866. "%s Invalid _cpu_high = 0x%x _cpu_low = 0x%x\n",
  867. __func__, session_cpu_high, session_cpu_low);
  868. inst = NULL;
  869. }
  870. return inst;
  871. }
  872. static void print_power(const struct eva_power_req *pwr_req)
  873. {
  874. if (pwr_req) {
  875. dprintk(CVP_DSP, "Clock: Fdu %d Ica %d Od %d Mpu %d Fw %d",
  876. pwr_req->clock_fdu, pwr_req->clock_ica,
  877. pwr_req->clock_od, pwr_req->clock_mpu,
  878. pwr_req->clock_fw);
  879. dprintk(CVP_DSP, "OpClock: Fdu %d Ica %d Od %d Mpu %d Fw %d",
  880. pwr_req->op_clock_fdu, pwr_req->op_clock_ica,
  881. pwr_req->op_clock_od, pwr_req->op_clock_mpu,
  882. pwr_req->op_clock_fw);
  883. dprintk(CVP_DSP, "Actual Bw: Ddr %d, SysCache %d",
  884. pwr_req->bw_ddr, pwr_req->bw_sys_cache);
  885. dprintk(CVP_DSP, "OpBw: Ddr %d, SysCache %d",
  886. pwr_req->op_bw_ddr, pwr_req->op_bw_sys_cache);
  887. }
  888. }
  889. static int msm_cvp_register_buffer_dsp(struct msm_cvp_inst *inst,
  890. struct eva_kmd_buffer *buf,
  891. int32_t pid,
  892. uint32_t *iova)
  893. {
  894. struct cvp_hfi_device *hdev;
  895. struct cvp_hal_session *session;
  896. struct msm_cvp_inst *s;
  897. int rc = 0;
  898. if (!inst || !inst->core || !buf) {
  899. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  900. return -EINVAL;
  901. }
  902. if (!buf->index)
  903. return 0;
  904. s = cvp_get_inst_validate(inst->core, inst);
  905. if (!s)
  906. return -ECONNRESET;
  907. inst->cur_cmd_type = EVA_KMD_REGISTER_BUFFER;
  908. session = (struct cvp_hal_session *)inst->session;
  909. if (!session) {
  910. dprintk(CVP_ERR, "%s: invalid session\n", __func__);
  911. rc = -EINVAL;
  912. goto exit;
  913. }
  914. hdev = inst->core->device;
  915. print_client_buffer(CVP_HFI, "register", inst, buf);
  916. rc = msm_cvp_map_buf_dsp_new(inst, buf, pid, iova);
  917. dprintk(CVP_DSP, "%s: fd %d, iova 0x%x\n", __func__, buf->fd, *iova);
  918. exit:
  919. inst->cur_cmd_type = 0;
  920. cvp_put_inst(s);
  921. return rc;
  922. }
  923. static int msm_cvp_unregister_buffer_dsp(struct msm_cvp_inst *inst,
  924. struct eva_kmd_buffer *buf)
  925. {
  926. struct msm_cvp_inst *s;
  927. int rc = 0;
  928. if (!inst || !inst->core || !buf) {
  929. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  930. return -EINVAL;
  931. }
  932. if (!buf->index)
  933. return 0;
  934. s = cvp_get_inst_validate(inst->core, inst);
  935. if (!s)
  936. return -ECONNRESET;
  937. inst->cur_cmd_type = EVA_KMD_UNREGISTER_BUFFER;
  938. print_client_buffer(CVP_HFI, "unregister", inst, buf);
  939. rc = msm_cvp_unmap_buf_dsp_new(inst, buf);
  940. inst->cur_cmd_type = 0;
  941. cvp_put_inst(s);
  942. return rc;
  943. }
  944. static void __dsp_cvp_sess_create(struct cvp_dsp_cmd_msg *cmd)
  945. {
  946. struct cvp_dsp_apps *me = &gfa_cv;
  947. struct msm_cvp_inst *inst = NULL;
  948. uint64_t inst_handle = 0;
  949. struct eva_kmd_arg *kmd;
  950. struct eva_kmd_sys_properties *sys_prop = NULL;
  951. struct eva_kmd_session_control *sys_ctrl = NULL;
  952. int rc = 0;
  953. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  954. cmd->ret = 0;
  955. dprintk(CVP_DSP,
  956. "%s sess Type %d Mask %d Prio %d Sec %d pid 0x%x\n",
  957. __func__, dsp2cpu_cmd->session_type,
  958. dsp2cpu_cmd->kernel_mask,
  959. dsp2cpu_cmd->session_prio,
  960. dsp2cpu_cmd->is_secure,
  961. dsp2cpu_cmd->pid);
  962. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  963. if (!kmd) {
  964. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  965. goto fail_frpc_driver_reg;
  966. }
  967. #ifdef FASTRPC_DRIVER_AVAILABLE
  968. rc = eva_fastrpc_driver_register(dsp2cpu_cmd->pid);
  969. #endif
  970. if (rc) {
  971. dprintk(CVP_ERR, "%s Register fastrpc driver fail\n", __func__);
  972. goto fail_frpc_driver_reg;
  973. }
  974. inst = msm_cvp_open(MSM_CORE_CVP, MSM_CVP_USER);
  975. if (!inst) {
  976. dprintk(CVP_ERR, "%s Failed create instance\n", __func__);
  977. goto fail_msm_cvp_open;
  978. }
  979. kmd->type = EVA_KMD_SET_SYS_PROPERTY;
  980. sys_prop = (struct eva_kmd_sys_properties *)&kmd->data.sys_properties;
  981. sys_prop->prop_num = 5;
  982. sys_prop->prop_data[0].prop_type = EVA_KMD_PROP_SESSION_KERNELMASK;
  983. sys_prop->prop_data[0].data = dsp2cpu_cmd->kernel_mask;
  984. sys_prop->prop_data[1].prop_type = EVA_KMD_PROP_SESSION_TYPE;
  985. sys_prop->prop_data[1].data = dsp2cpu_cmd->session_type;
  986. sys_prop->prop_data[2].prop_type = EVA_KMD_PROP_SESSION_PRIORITY;
  987. sys_prop->prop_data[2].data = dsp2cpu_cmd->session_prio;
  988. sys_prop->prop_data[3].prop_type = EVA_KMD_PROP_SESSION_SECURITY;
  989. sys_prop->prop_data[3].data = dsp2cpu_cmd->is_secure;
  990. sys_prop->prop_data[4].prop_type = EVA_KMD_PROP_SESSION_DSPMASK;
  991. sys_prop->prop_data[4].data = dsp2cpu_cmd->dsp_access_mask;
  992. rc = msm_cvp_handle_syscall(inst, kmd);
  993. if (rc) {
  994. dprintk(CVP_ERR, "%s Failed to set sys property\n", __func__);
  995. goto fail_set_sys_property;
  996. }
  997. dprintk(CVP_DSP, "%s set sys property done\n", __func__);
  998. /* EVA_KMD_SESSION_CONTROL from DSP */
  999. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1000. kmd->type = EVA_KMD_SESSION_CONTROL;
  1001. sys_ctrl = (struct eva_kmd_session_control *)&kmd->data.session_ctrl;
  1002. sys_ctrl->ctrl_type = SESSION_CREATE;
  1003. rc = msm_cvp_handle_syscall(inst, kmd);
  1004. if (rc) {
  1005. dprintk(CVP_ERR, "Warning: send Session Create failed\n");
  1006. goto fail_session_create;
  1007. }
  1008. dprintk(CVP_DSP, "%s send Session Create done\n", __func__);
  1009. /* Get session id */
  1010. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1011. kmd->type = EVA_KMD_GET_SESSION_INFO;
  1012. rc = msm_cvp_handle_syscall(inst, kmd);
  1013. if (rc) {
  1014. dprintk(CVP_ERR, "Warning: get session index failed\n");
  1015. goto fail_get_session_info;
  1016. }
  1017. cmd->session_id = kmd->data.session.session_id;
  1018. inst_handle = (uint64_t)inst;
  1019. cmd->session_cpu_high = (uint32_t)((inst_handle & HIGH32) >> 32);
  1020. cmd->session_cpu_low = (uint32_t)(inst_handle & LOW32);
  1021. dprintk(CVP_DSP,
  1022. "%s CREATE_SESS id 0x%x, cpu_low 0x%x, cpu_high 0x%x\n",
  1023. __func__, cmd->session_id, cmd->session_cpu_low,
  1024. cmd->session_cpu_high);
  1025. kfree(kmd);
  1026. return;
  1027. fail_get_session_info:
  1028. fail_session_create:
  1029. fail_set_sys_property:
  1030. fail_msm_cvp_open:
  1031. /* unregister fastrpc driver */
  1032. fail_frpc_driver_reg:
  1033. cmd->ret = -1;
  1034. kfree(kmd);
  1035. }
  1036. static void __dsp_cvp_sess_delete(struct cvp_dsp_cmd_msg *cmd)
  1037. {
  1038. struct cvp_dsp_apps *me = &gfa_cv;
  1039. struct msm_cvp_inst *inst;
  1040. struct eva_kmd_arg *kmd;
  1041. struct eva_kmd_session_control *sys_ctrl;
  1042. int rc;
  1043. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1044. cmd->ret = 0;
  1045. dprintk(CVP_DSP,
  1046. "%s sess id 0x%x low 0x%x high 0x%x, pid 0x%x\n",
  1047. __func__, dsp2cpu_cmd->session_id,
  1048. dsp2cpu_cmd->session_cpu_low,
  1049. dsp2cpu_cmd->session_cpu_high,
  1050. dsp2cpu_cmd->pid);
  1051. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1052. if (!kmd) {
  1053. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1054. cmd->ret = -1;
  1055. return;
  1056. }
  1057. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1058. dsp2cpu_cmd->session_cpu_high,
  1059. dsp2cpu_cmd->session_cpu_low);
  1060. #ifdef FASTRPC_DRIVER_AVAILABLE
  1061. /* unregister fastrpc driver */
  1062. eva_fastrpc_driver_unregister(inst, dsp2cpu_cmd->pid, false);
  1063. #endif
  1064. kmd->type = EVA_KMD_SESSION_CONTROL;
  1065. sys_ctrl = (struct eva_kmd_session_control *)&kmd->data.session_ctrl;
  1066. /* Session delete does nothing here */
  1067. sys_ctrl->ctrl_type = SESSION_DELETE;
  1068. rc = msm_cvp_handle_syscall(inst, kmd);
  1069. if (rc) {
  1070. dprintk(CVP_ERR, "Warning: send Delete Session failed\n");
  1071. cmd->ret = -1;
  1072. goto dsp_fail_delete;
  1073. }
  1074. rc = msm_cvp_close(inst);
  1075. if (rc) {
  1076. dprintk(CVP_ERR, "Warning: Failed to close cvp instance\n");
  1077. cmd->ret = -1;
  1078. goto dsp_fail_delete;
  1079. }
  1080. dprintk(CVP_DSP, "%s DSP2CPU_DETELE_SESSION Done\n", __func__);
  1081. dsp_fail_delete:
  1082. kfree(kmd);
  1083. }
  1084. static void __dsp_cvp_power_req(struct cvp_dsp_cmd_msg *cmd)
  1085. {
  1086. struct cvp_dsp_apps *me = &gfa_cv;
  1087. struct msm_cvp_inst *inst;
  1088. struct eva_kmd_arg *kmd;
  1089. struct eva_kmd_sys_properties *sys_prop;
  1090. int rc;
  1091. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1092. cmd->ret = 0;
  1093. dprintk(CVP_DSP,
  1094. "%s sess id 0x%x, low 0x%x, high 0x%x\n",
  1095. __func__, dsp2cpu_cmd->session_id,
  1096. dsp2cpu_cmd->session_cpu_low,
  1097. dsp2cpu_cmd->session_cpu_high);
  1098. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1099. if (!kmd) {
  1100. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1101. cmd->ret = -1;
  1102. return;
  1103. }
  1104. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1105. dsp2cpu_cmd->session_cpu_high,
  1106. dsp2cpu_cmd->session_cpu_low);
  1107. print_power(&dsp2cpu_cmd->power_req);
  1108. /* EVA_KMD_SET_SYS_PROPERTY
  1109. * Total 14 properties, 8 max once
  1110. * Need to do 2 rounds
  1111. */
  1112. kmd->type = EVA_KMD_SET_SYS_PROPERTY;
  1113. sys_prop = (struct eva_kmd_sys_properties *)&kmd->data.sys_properties;
  1114. sys_prop->prop_num = 7;
  1115. sys_prop->prop_data[0].prop_type = EVA_KMD_PROP_PWR_FDU;
  1116. sys_prop->prop_data[0].data =
  1117. dsp2cpu_cmd->power_req.clock_fdu;
  1118. sys_prop->prop_data[1].prop_type = EVA_KMD_PROP_PWR_ICA;
  1119. sys_prop->prop_data[1].data =
  1120. dsp2cpu_cmd->power_req.clock_ica;
  1121. sys_prop->prop_data[2].prop_type = EVA_KMD_PROP_PWR_OD;
  1122. sys_prop->prop_data[2].data =
  1123. dsp2cpu_cmd->power_req.clock_od;
  1124. sys_prop->prop_data[3].prop_type = EVA_KMD_PROP_PWR_MPU;
  1125. sys_prop->prop_data[3].data =
  1126. dsp2cpu_cmd->power_req.clock_mpu;
  1127. sys_prop->prop_data[4].prop_type = EVA_KMD_PROP_PWR_FW;
  1128. sys_prop->prop_data[4].data =
  1129. dsp2cpu_cmd->power_req.clock_fw;
  1130. sys_prop->prop_data[5].prop_type = EVA_KMD_PROP_PWR_DDR;
  1131. sys_prop->prop_data[5].data =
  1132. dsp2cpu_cmd->power_req.bw_ddr;
  1133. sys_prop->prop_data[6].prop_type = EVA_KMD_PROP_PWR_SYSCACHE;
  1134. sys_prop->prop_data[6].data =
  1135. dsp2cpu_cmd->power_req.bw_sys_cache;
  1136. rc = msm_cvp_handle_syscall(inst, kmd);
  1137. if (rc) {
  1138. dprintk(CVP_ERR, "%s Failed to set sys property\n", __func__);
  1139. cmd->ret = -1;
  1140. goto dsp_fail_power_req;
  1141. }
  1142. dprintk(CVP_DSP, "%s set sys property done part 1\n", __func__);
  1143. /* EVA_KMD_SET_SYS_PROPERTY Round 2 */
  1144. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1145. kmd->type = EVA_KMD_SET_SYS_PROPERTY;
  1146. sys_prop = (struct eva_kmd_sys_properties *)&kmd->data.sys_properties;
  1147. sys_prop->prop_num = 7;
  1148. sys_prop->prop_data[0].prop_type = EVA_KMD_PROP_PWR_FDU_OP;
  1149. sys_prop->prop_data[0].data =
  1150. dsp2cpu_cmd->power_req.op_clock_fdu;
  1151. sys_prop->prop_data[1].prop_type = EVA_KMD_PROP_PWR_ICA_OP;
  1152. sys_prop->prop_data[1].data =
  1153. dsp2cpu_cmd->power_req.op_clock_ica;
  1154. sys_prop->prop_data[2].prop_type = EVA_KMD_PROP_PWR_OD_OP;
  1155. sys_prop->prop_data[2].data =
  1156. dsp2cpu_cmd->power_req.op_clock_od;
  1157. sys_prop->prop_data[3].prop_type = EVA_KMD_PROP_PWR_MPU_OP;
  1158. sys_prop->prop_data[3].data =
  1159. dsp2cpu_cmd->power_req.op_clock_mpu;
  1160. sys_prop->prop_data[4].prop_type = EVA_KMD_PROP_PWR_FW_OP;
  1161. sys_prop->prop_data[4].data =
  1162. dsp2cpu_cmd->power_req.op_clock_fw;
  1163. sys_prop->prop_data[5].prop_type = EVA_KMD_PROP_PWR_DDR_OP;
  1164. sys_prop->prop_data[5].data =
  1165. dsp2cpu_cmd->power_req.op_bw_ddr;
  1166. sys_prop->prop_data[6].prop_type = EVA_KMD_PROP_PWR_SYSCACHE_OP;
  1167. sys_prop->prop_data[6].data =
  1168. dsp2cpu_cmd->power_req.op_bw_sys_cache;
  1169. rc = msm_cvp_handle_syscall(inst, kmd);
  1170. if (rc) {
  1171. dprintk(CVP_ERR, "%s Failed to set sys property\n", __func__);
  1172. cmd->ret = -1;
  1173. goto dsp_fail_power_req;
  1174. }
  1175. dprintk(CVP_DSP, "%s set sys property done part 2\n", __func__);
  1176. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1177. kmd->type = EVA_KMD_UPDATE_POWER;
  1178. rc = msm_cvp_handle_syscall(inst, kmd);
  1179. if (rc) {
  1180. /* May need to define more error types
  1181. * Check UMD implementation here:
  1182. * https://opengrok.qualcomm.com/source/xref/LA.UM.9.14/vendor/qcom/proprietary/cv-noship/cvp/cpurev/src/cvpcpuRev_skel_imp_cvp2.cpp#380
  1183. */
  1184. dprintk(CVP_ERR, "%s Failed to send update power numbers\n", __func__);
  1185. cmd->ret = -1;
  1186. goto dsp_fail_power_req;
  1187. }
  1188. dprintk(CVP_DSP, "%s DSP2CPU_POWER_REQUEST Done\n", __func__);
  1189. dsp_fail_power_req:
  1190. kfree(kmd);
  1191. }
  1192. static void __dsp_cvp_buf_register(struct cvp_dsp_cmd_msg *cmd)
  1193. {
  1194. struct cvp_dsp_apps *me = &gfa_cv;
  1195. struct msm_cvp_inst *inst;
  1196. struct eva_kmd_arg *kmd;
  1197. struct eva_kmd_buffer *kmd_buf;
  1198. int rc;
  1199. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1200. cmd->ret = 0;
  1201. dprintk(CVP_DSP,
  1202. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1203. __func__, dsp2cpu_cmd->session_id,
  1204. dsp2cpu_cmd->session_cpu_low,
  1205. dsp2cpu_cmd->session_cpu_high,
  1206. dsp2cpu_cmd->pid);
  1207. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1208. if (!kmd) {
  1209. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1210. cmd->ret = -1;
  1211. return;
  1212. }
  1213. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1214. dsp2cpu_cmd->session_cpu_high,
  1215. dsp2cpu_cmd->session_cpu_low);
  1216. kmd->type = EVA_KMD_REGISTER_BUFFER;
  1217. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1218. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1219. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1220. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1221. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1222. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1223. kmd_buf->pixelformat = 0;
  1224. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1225. rc = msm_cvp_register_buffer_dsp(inst, kmd_buf,
  1226. dsp2cpu_cmd->pid, &cmd->sbuf.iova);
  1227. if (rc) {
  1228. dprintk(CVP_ERR, "%s Failed to register buffer\n", __func__);
  1229. cmd->ret = -1;
  1230. goto dsp_fail_buf_reg;
  1231. }
  1232. dprintk(CVP_DSP, "%s register buffer done\n", __func__);
  1233. cmd->sbuf.size = kmd_buf->size;
  1234. cmd->sbuf.fd = kmd_buf->fd;
  1235. cmd->sbuf.index = kmd_buf->index;
  1236. cmd->sbuf.offset = kmd_buf->offset;
  1237. dprintk(CVP_DSP, "%s: fd %d, iova 0x%x\n", __func__,
  1238. cmd->sbuf.fd, cmd->sbuf.iova);
  1239. dsp_fail_buf_reg:
  1240. kfree(kmd);
  1241. }
  1242. static void __dsp_cvp_buf_deregister(struct cvp_dsp_cmd_msg *cmd)
  1243. {
  1244. struct cvp_dsp_apps *me = &gfa_cv;
  1245. struct msm_cvp_inst *inst;
  1246. struct eva_kmd_arg *kmd;
  1247. struct eva_kmd_buffer *kmd_buf;
  1248. int rc;
  1249. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1250. cmd->ret = 0;
  1251. dprintk(CVP_DSP,
  1252. "%s : sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1253. __func__, dsp2cpu_cmd->session_id,
  1254. dsp2cpu_cmd->session_cpu_low,
  1255. dsp2cpu_cmd->session_cpu_high,
  1256. dsp2cpu_cmd->pid);
  1257. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1258. if (!kmd) {
  1259. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1260. cmd->ret = -1;
  1261. return;
  1262. }
  1263. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1264. dsp2cpu_cmd->session_cpu_high,
  1265. dsp2cpu_cmd->session_cpu_low);
  1266. kmd->type = EVA_KMD_UNREGISTER_BUFFER;
  1267. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1268. kmd_buf->type = EVA_KMD_UNREGISTER_BUFFER;
  1269. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1270. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1271. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1272. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1273. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1274. kmd_buf->pixelformat = 0;
  1275. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1276. rc = msm_cvp_unregister_buffer_dsp(inst, kmd_buf);
  1277. if (rc) {
  1278. dprintk(CVP_ERR, "%s Failed to deregister buffer\n", __func__);
  1279. cmd->ret = -1;
  1280. goto fail_dsp_buf_dereg;
  1281. }
  1282. dprintk(CVP_DSP, "%s deregister buffer done\n", __func__);
  1283. fail_dsp_buf_dereg:
  1284. kfree(kmd);
  1285. }
  1286. static void __dsp_cvp_mem_alloc(struct cvp_dsp_cmd_msg *cmd)
  1287. {
  1288. struct cvp_dsp_apps *me = &gfa_cv;
  1289. struct msm_cvp_inst *inst;
  1290. int rc;
  1291. struct cvp_internal_buf *buf = NULL;
  1292. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1293. uint64_t v_dsp_addr = 0;
  1294. #ifdef FASTRPC_DRIVER_AVAILABLE
  1295. struct fastrpc_device *frpc_device = NULL;
  1296. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1297. struct msm_cvp_list *buf_list = NULL;
  1298. #endif
  1299. cmd->ret = 0;
  1300. dprintk(CVP_DSP,
  1301. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1302. __func__, dsp2cpu_cmd->session_id,
  1303. dsp2cpu_cmd->session_cpu_low,
  1304. dsp2cpu_cmd->session_cpu_high,
  1305. dsp2cpu_cmd->pid);
  1306. #ifdef FASTRPC_DRIVER_AVAILABLE
  1307. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1308. if (!frpc_node) {
  1309. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1310. __func__, dsp2cpu_cmd->pid);
  1311. goto fail_fastrpc_node;
  1312. }
  1313. frpc_device = frpc_node->cvp_fastrpc_device;
  1314. #endif
  1315. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1316. dsp2cpu_cmd->session_cpu_high,
  1317. dsp2cpu_cmd->session_cpu_low);
  1318. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  1319. if (!buf)
  1320. goto fail_kzalloc_buf;
  1321. rc = cvp_allocate_dsp_bufs(inst, buf,
  1322. dsp2cpu_cmd->sbuf.size,
  1323. dsp2cpu_cmd->sbuf.type);
  1324. if (rc != 0)
  1325. goto fail_allocate_dsp_buf;
  1326. #ifdef FASTRPC_DRIVER_AVAILABLE
  1327. rc = eva_fastrpc_dev_map_dma(frpc_device, buf,
  1328. dsp2cpu_cmd->sbuf.dsp_remote_map,
  1329. &v_dsp_addr);
  1330. #endif
  1331. if (rc) {
  1332. dprintk(CVP_ERR, "%s Failed to map buffer 0x%x\n", __func__,
  1333. rc);
  1334. goto fail_fastrpc_dev_map_dma;
  1335. }
  1336. #ifdef FASTRPC_DRIVER_AVAILABLE
  1337. buf_list = &frpc_node->dspbufs;
  1338. mutex_lock(&buf_list->lock);
  1339. list_add_tail(&buf->list, &buf_list->list);
  1340. mutex_unlock(&buf_list->lock);
  1341. #endif
  1342. dprintk(CVP_DSP, "%s allocate buffer done, addr 0x%llx\n",
  1343. __func__, v_dsp_addr);
  1344. cmd->sbuf.size = buf->smem->size;
  1345. cmd->sbuf.fd = buf->fd;
  1346. cmd->sbuf.offset = 0;
  1347. cmd->sbuf.iova = buf->smem->device_addr;
  1348. cmd->sbuf.v_dsp_addr = v_dsp_addr;
  1349. dprintk(CVP_DSP, "%s: size %d, iova 0x%x, v_dsp_addr 0x%llx\n",
  1350. __func__, cmd->sbuf.size, cmd->sbuf.iova,
  1351. cmd->sbuf.v_dsp_addr);
  1352. return;
  1353. fail_fastrpc_dev_map_dma:
  1354. cvp_release_dsp_buffers(inst, buf);
  1355. fail_allocate_dsp_buf:
  1356. kfree(buf);
  1357. fail_kzalloc_buf:
  1358. #ifdef FASTRPC_DRIVER_AVAILABLE
  1359. fail_fastrpc_node:
  1360. #endif
  1361. cmd->ret = -1;
  1362. return;
  1363. }
  1364. static void __dsp_cvp_mem_free(struct cvp_dsp_cmd_msg *cmd)
  1365. {
  1366. #ifdef FASTRPC_DRIVER_AVAILABLE
  1367. struct cvp_dsp_apps *me = &gfa_cv;
  1368. struct msm_cvp_inst *inst;
  1369. int rc;
  1370. struct cvp_internal_buf *buf = NULL;
  1371. struct list_head *ptr = NULL, *next = NULL;
  1372. struct msm_cvp_list *buf_list = NULL;
  1373. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1374. struct fastrpc_device *frpc_device = NULL;
  1375. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1376. cmd->ret = 0;
  1377. dprintk(CVP_DSP,
  1378. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1379. __func__, dsp2cpu_cmd->session_id,
  1380. dsp2cpu_cmd->session_cpu_low,
  1381. dsp2cpu_cmd->session_cpu_high,
  1382. dsp2cpu_cmd->pid);
  1383. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1384. dsp2cpu_cmd->session_cpu_high,
  1385. dsp2cpu_cmd->session_cpu_low);
  1386. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1387. if (!frpc_node) {
  1388. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1389. __func__, dsp2cpu_cmd->pid);
  1390. cmd->ret = -1;
  1391. return;
  1392. }
  1393. frpc_device = frpc_node->cvp_fastrpc_device;
  1394. buf_list = &frpc_node->dspbufs;
  1395. mutex_lock(&buf_list->lock);
  1396. list_for_each_safe(ptr, next, &buf_list->list) {
  1397. buf = list_entry(ptr, struct cvp_internal_buf, list);
  1398. dprintk(CVP_DSP, "fd in list 0x%x, fd from dsp 0x%x\n",
  1399. buf->fd, dsp2cpu_cmd->sbuf.fd);
  1400. if (!buf->smem) {
  1401. dprintk(CVP_DSP, "Empyt smem\n");
  1402. continue;
  1403. }
  1404. /* Verify with device addr */
  1405. if (buf->smem->device_addr == dsp2cpu_cmd->sbuf.iova) {
  1406. dprintk(CVP_DSP, "%s find device addr 0x%x\n",
  1407. __func__, buf->smem->device_addr);
  1408. rc = eva_fastrpc_dev_unmap_dma(frpc_device, buf);
  1409. if (rc) {
  1410. dprintk(CVP_ERR,
  1411. "%s Failed to unmap buffer 0x%x\n",
  1412. __func__, rc);
  1413. cmd->ret = -1;
  1414. goto fail_fastrpc_dev_unmap_dma;
  1415. }
  1416. rc = cvp_release_dsp_buffers(inst, buf);
  1417. if (rc) {
  1418. dprintk(CVP_ERR,
  1419. "%s Failed to free buffer 0x%x\n",
  1420. __func__, rc);
  1421. cmd->ret = -1;
  1422. goto fail_release_buf;
  1423. }
  1424. list_del(&buf->list);
  1425. kfree(buf);
  1426. break;
  1427. }
  1428. }
  1429. fail_release_buf:
  1430. fail_fastrpc_dev_unmap_dma:
  1431. mutex_unlock(&buf_list->lock);
  1432. #endif
  1433. }
  1434. static int cvp_dsp_thread(void *data)
  1435. {
  1436. int rc = 0, old_state;
  1437. struct cvp_dsp_apps *me = &gfa_cv;
  1438. struct cvp_dsp_cmd_msg cmd;
  1439. struct cvp_hfi_device *hdev;
  1440. struct msm_cvp_core *core;
  1441. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  1442. if (!core) {
  1443. dprintk(CVP_ERR, "%s: Failed to find core\n", __func__);
  1444. rc = -EINVAL;
  1445. goto exit;
  1446. }
  1447. hdev = (struct cvp_hfi_device *)core->device;
  1448. if (!hdev) {
  1449. dprintk(CVP_ERR, "%s Invalid device handle\n", __func__);
  1450. rc = -EINVAL;
  1451. goto exit;
  1452. }
  1453. wait_dsp:
  1454. rc = wait_for_completion_interruptible(
  1455. &me->completions[CPU2DSP_MAX_CMD]);
  1456. if (me->state == DSP_INVALID)
  1457. goto exit;
  1458. if (me->state == DSP_UNINIT)
  1459. goto wait_dsp;
  1460. if (me->state == DSP_PROBED) {
  1461. cvp_dsp_send_hfi_queue();
  1462. goto wait_dsp;
  1463. }
  1464. cmd.type = me->pending_dsp2cpu_cmd.type;
  1465. if (rc == -ERESTARTSYS) {
  1466. dprintk(CVP_WARN, "%s received interrupt signal\n", __func__);
  1467. } else {
  1468. mutex_lock(&me->lock);
  1469. switch (me->pending_dsp2cpu_cmd.type) {
  1470. case DSP2CPU_POWERON:
  1471. {
  1472. if (me->state == DSP_READY) {
  1473. cmd.ret = 0;
  1474. break;
  1475. }
  1476. mutex_unlock(&me->lock);
  1477. old_state = me->state;
  1478. me->state = DSP_READY;
  1479. rc = call_hfi_op(hdev, resume, hdev->hfi_device_data);
  1480. if (rc) {
  1481. dprintk(CVP_WARN, "%s Failed to resume cvp\n",
  1482. __func__);
  1483. mutex_lock(&me->lock);
  1484. me->state = old_state;
  1485. cmd.ret = 1;
  1486. break;
  1487. }
  1488. mutex_lock(&me->lock);
  1489. cmd.ret = 0;
  1490. break;
  1491. }
  1492. case DSP2CPU_POWEROFF:
  1493. {
  1494. me->state = DSP_SUSPEND;
  1495. cmd.ret = 0;
  1496. break;
  1497. }
  1498. case DSP2CPU_CREATE_SESSION:
  1499. {
  1500. __dsp_cvp_sess_create(&cmd);
  1501. break;
  1502. }
  1503. case DSP2CPU_DETELE_SESSION:
  1504. {
  1505. __dsp_cvp_sess_delete(&cmd);
  1506. break;
  1507. }
  1508. case DSP2CPU_POWER_REQUEST:
  1509. {
  1510. __dsp_cvp_power_req(&cmd);
  1511. break;
  1512. }
  1513. case DSP2CPU_REGISTER_BUFFER:
  1514. {
  1515. __dsp_cvp_buf_register(&cmd);
  1516. break;
  1517. }
  1518. case DSP2CPU_DEREGISTER_BUFFER:
  1519. {
  1520. __dsp_cvp_buf_deregister(&cmd);
  1521. break;
  1522. }
  1523. case DSP2CPU_MEM_ALLOC:
  1524. {
  1525. __dsp_cvp_mem_alloc(&cmd);
  1526. break;
  1527. }
  1528. case DSP2CPU_MEM_FREE:
  1529. {
  1530. __dsp_cvp_mem_free(&cmd);
  1531. break;
  1532. }
  1533. default:
  1534. dprintk(CVP_ERR, "unrecognaized dsp cmds: %d\n",
  1535. me->pending_dsp2cpu_cmd.type);
  1536. break;
  1537. }
  1538. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1539. mutex_unlock(&me->lock);
  1540. }
  1541. /* Responds to DSP */
  1542. rc = cvp_dsp_send_cmd(&cmd, sizeof(struct cvp_dsp_cmd_msg));
  1543. if (rc)
  1544. dprintk(CVP_ERR,
  1545. "%s: cvp_dsp_send_cmd failed rc = %d cmd type=%d\n",
  1546. __func__, rc, cmd.type);
  1547. goto wait_dsp;
  1548. exit:
  1549. dprintk(CVP_DBG, "dsp thread exit\n");
  1550. do_exit(rc);
  1551. }
  1552. int cvp_dsp_device_init(void)
  1553. {
  1554. struct cvp_dsp_apps *me = &gfa_cv;
  1555. char tname[16];
  1556. int rc;
  1557. int i;
  1558. mutex_init(&me->lock);
  1559. me->state = DSP_INVALID;
  1560. me->hyp_assigned = false;
  1561. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1562. init_completion(&me->completions[i]);
  1563. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1564. me->pending_dsp2cpu_rsp.type = CVP_INVALID_RPMSG_TYPE;
  1565. INIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1566. rc = register_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1567. if (rc) {
  1568. dprintk(CVP_ERR,
  1569. "%s : register_rpmsg_driver failed rc = %d\n",
  1570. __func__, rc);
  1571. goto register_bail;
  1572. }
  1573. snprintf(tname, sizeof(tname), "cvp-dsp-thread");
  1574. me->state = DSP_UNINIT;
  1575. me->dsp_thread = kthread_run(cvp_dsp_thread, me, tname);
  1576. if (!me->dsp_thread) {
  1577. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  1578. rc = -ECHILD;
  1579. me->state = DSP_INVALID;
  1580. goto register_bail;
  1581. }
  1582. return 0;
  1583. register_bail:
  1584. return rc;
  1585. }
  1586. void cvp_dsp_device_exit(void)
  1587. {
  1588. struct cvp_dsp_apps *me = &gfa_cv;
  1589. int i;
  1590. mutex_lock(&me->lock);
  1591. me->state = DSP_INVALID;
  1592. mutex_unlock(&me->lock);
  1593. DEINIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1594. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1595. complete_all(&me->completions[i]);
  1596. mutex_destroy(&me->lock);
  1597. unregister_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1598. }