msm_cvp_dsp.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, 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->tx_lock);
  146. me->chan = rpdev;
  147. me->state = DSP_PROBED;
  148. mutex_unlock(&me->tx_lock);
  149. complete(&me->completions[CPU2DSP_MAX_CMD]);
  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->tx_lock);
  157. cvp_hyp_assign_from_dsp();
  158. me->chan = NULL;
  159. me->state = DSP_UNINIT;
  160. mutex_unlock(&me->tx_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->tx_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->tx_lock);
  232. retried = true;
  233. rc = cvp_reinit_dsp();
  234. mutex_lock(&me->tx_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->tx_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->tx_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->tx_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->tx_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->tx_lock);
  334. retried = true;
  335. rc = cvp_reinit_dsp();
  336. mutex_lock(&me->tx_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->tx_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->tx_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->tx_lock);
  395. retried = true;
  396. rc = cvp_reinit_dsp();
  397. mutex_lock(&me->tx_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->tx_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->tx_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->tx_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->tx_lock);
  548. me->state = DSP_INVALID;
  549. cvp_hyp_assign_from_dsp();
  550. mutex_unlock(&me->tx_lock);
  551. }
  552. return rc;
  553. }
  554. static struct cvp_dsp_fastrpc_driver_entry *cvp_find_fastrpc_node_with_handle(
  555. uint32_t handle)
  556. {
  557. struct cvp_dsp_apps *me = &gfa_cv;
  558. struct list_head *ptr = NULL, *next = NULL;
  559. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  560. mutex_lock(&me->fastrpc_driver_list.lock);
  561. list_for_each_safe(ptr, next, &me->fastrpc_driver_list.list) {
  562. frpc_node = list_entry(ptr,
  563. struct cvp_dsp_fastrpc_driver_entry, list);
  564. if (handle == frpc_node->handle) {
  565. dprintk(CVP_DSP, "Find frpc_node with handle 0x%x\n",
  566. handle);
  567. break;
  568. }
  569. }
  570. mutex_unlock(&me->fastrpc_driver_list.lock);
  571. return frpc_node;
  572. }
  573. static void eva_fastrpc_driver_unregister(uint32_t handle, bool force_exit);
  574. static int cvp_fastrpc_probe(struct fastrpc_device *rpc_dev)
  575. {
  576. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  577. dprintk(CVP_DSP, "%s fastrpc probe handle 0x%x\n",
  578. __func__, rpc_dev->handle);
  579. frpc_node = cvp_find_fastrpc_node_with_handle(rpc_dev->handle);
  580. if (frpc_node) {
  581. frpc_node->cvp_fastrpc_device = rpc_dev;
  582. // static structure with signal and pid
  583. complete(&frpc_node->fastrpc_probe_completion);
  584. }
  585. return 0;
  586. }
  587. static int cvp_fastrpc_callback(struct fastrpc_device *rpc_dev,
  588. enum fastrpc_driver_status fastrpc_proc_num)
  589. {
  590. dprintk(CVP_DSP, "%s handle 0x%x, proc %d\n", __func__,
  591. rpc_dev->handle, fastrpc_proc_num);
  592. /* fastrpc drive down when process gone
  593. * any handling can happen here, such as
  594. * eva_fastrpc_driver_unregister(rpc_dev->handle, true);
  595. */
  596. return 0;
  597. }
  598. static struct fastrpc_driver cvp_fastrpc_client = {
  599. .probe = cvp_fastrpc_probe,
  600. .callback = cvp_fastrpc_callback,
  601. .driver = {
  602. .name = "qcom,fastcv",
  603. },
  604. };
  605. static int eva_fastrpc_dev_map_dma(struct fastrpc_device *frpc_device,
  606. struct cvp_internal_buf *buf,
  607. uint32_t dsp_remote_map,
  608. uint64_t *v_dsp_addr)
  609. {
  610. struct fastrpc_dev_map_dma frpc_map_buf = {0};
  611. int rc = 0;
  612. if (dsp_remote_map == 1) {
  613. frpc_map_buf.buf = buf->smem->dma_buf;
  614. frpc_map_buf.size = buf->smem->size;
  615. frpc_map_buf.attrs = 0;
  616. dprintk(CVP_DSP,
  617. "%s frpc_map_buf size %d, dma_buf %pK, map %pK, 0x%x\n",
  618. __func__, frpc_map_buf.size, frpc_map_buf.buf,
  619. &frpc_map_buf, (unsigned long)&frpc_map_buf);
  620. rc = fastrpc_driver_invoke(frpc_device, FASTRPC_DEV_MAP_DMA,
  621. (unsigned long)(&frpc_map_buf));
  622. if (rc) {
  623. dprintk(CVP_ERR,
  624. "%s Failed to map buffer 0x%x\n", __func__, rc);
  625. return rc;
  626. }
  627. buf->fd = (s32)frpc_map_buf.v_dsp_addr;
  628. *v_dsp_addr = frpc_map_buf.v_dsp_addr;
  629. } else {
  630. dprintk(CVP_DSP, "%s Buffer not mapped to dsp\n", __func__);
  631. buf->fd = 0;
  632. }
  633. return rc;
  634. }
  635. static int eva_fastrpc_dev_unmap_dma(struct fastrpc_device *frpc_device,
  636. struct cvp_internal_buf *buf)
  637. {
  638. struct fastrpc_dev_unmap_dma frpc_unmap_buf = {0};
  639. int rc = 0;
  640. /* Only if buffer is mapped to dsp */
  641. if (buf->fd != 0) {
  642. frpc_unmap_buf.buf = buf->smem->dma_buf;
  643. rc = fastrpc_driver_invoke(frpc_device, FASTRPC_DEV_UNMAP_DMA,
  644. (unsigned long)(&frpc_unmap_buf));
  645. if (rc) {
  646. dprintk(CVP_ERR, "%s Failed to unmap buffer 0x%x\n",
  647. __func__, rc);
  648. return rc;
  649. }
  650. } else {
  651. dprintk(CVP_DSP, "%s buffer not mapped to dsp\n", __func__);
  652. }
  653. return rc;
  654. }
  655. static void eva_fastrpc_driver_add_sess(
  656. struct cvp_dsp_fastrpc_driver_entry *frpc,
  657. struct msm_cvp_inst *inst)
  658. {
  659. mutex_lock(&frpc->dsp_sessions.lock);
  660. if (inst)
  661. list_add_tail(&inst->dsp_list, &frpc->dsp_sessions.list);
  662. else
  663. dprintk(CVP_ERR, "%s incorrect input %pK\n", __func__, inst);
  664. frpc->session_cnt++;
  665. mutex_unlock(&frpc->dsp_sessions.lock);
  666. dprintk(CVP_DSP, "add dsp sess %pK fastrpc_driver %pK\n", inst, frpc);
  667. }
  668. int cvp_dsp_fastrpc_unmap(uint32_t process_id, struct cvp_internal_buf *buf)
  669. {
  670. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  671. struct fastrpc_device *frpc_device = NULL;
  672. int rc = 0;
  673. frpc_node = cvp_find_fastrpc_node_with_handle(process_id);
  674. if (!frpc_node) {
  675. dprintk(CVP_ERR, "%s no frpc node for process id %d\n",
  676. __func__, process_id);
  677. return -EINVAL;
  678. }
  679. frpc_device = frpc_node->cvp_fastrpc_device;
  680. rc = eva_fastrpc_dev_unmap_dma(frpc_device, buf);
  681. if (rc) {
  682. dprintk(CVP_ERR,
  683. "%s Fail to unmap buffer 0x%x\n",
  684. __func__, rc);
  685. return rc;
  686. }
  687. return rc;
  688. }
  689. int cvp_dsp_del_sess(uint32_t process_id, struct msm_cvp_inst *inst)
  690. {
  691. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  692. struct list_head *ptr = NULL, *next = NULL;
  693. struct msm_cvp_inst *sess;
  694. bool found = false;
  695. frpc_node = cvp_find_fastrpc_node_with_handle(process_id);
  696. if (!frpc_node) {
  697. dprintk(CVP_ERR, "%s no frpc node for process id %d\n",
  698. __func__, process_id);
  699. return -EINVAL;
  700. }
  701. mutex_lock(&frpc_node->dsp_sessions.lock);
  702. list_for_each_safe(ptr, next, &frpc_node->dsp_sessions.list) {
  703. sess = list_entry(ptr, struct msm_cvp_inst, dsp_list);
  704. if (sess == inst) {
  705. dprintk(CVP_DSP, "%s Find sess %pK to be deleted\n",
  706. __func__, inst);
  707. found = true;
  708. break;
  709. }
  710. }
  711. if (found) {
  712. list_del(&inst->dsp_list);
  713. frpc_node->session_cnt--;
  714. }
  715. mutex_unlock(&frpc_node->dsp_sessions.lock);
  716. return 0;
  717. }
  718. static int eva_fastrpc_driver_register(uint32_t handle)
  719. {
  720. struct cvp_dsp_apps *me = &gfa_cv;
  721. int rc = 0;
  722. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  723. frpc_node = cvp_find_fastrpc_node_with_handle(handle);
  724. if (frpc_node == NULL) {
  725. frpc_node = kzalloc(sizeof(*frpc_node), GFP_KERNEL);
  726. if (!frpc_node) {
  727. dprintk(CVP_DSP, "%s allocate frpc node fail\n",
  728. __func__);
  729. return -EINVAL;
  730. }
  731. memset(frpc_node, 0, sizeof(*frpc_node));
  732. /* Init completion */
  733. init_completion(&frpc_node->fastrpc_probe_completion);
  734. mutex_lock(&me->fastrpc_driver_list.lock);
  735. dprintk(CVP_DSP, "Add frpc node 0x%x to list\n", frpc_node);
  736. list_add_tail(&frpc_node->list, &me->fastrpc_driver_list.list);
  737. mutex_unlock(&me->fastrpc_driver_list.lock);
  738. INIT_MSM_CVP_LIST(&frpc_node->dsp_sessions);
  739. /* register fastrpc device to this session */
  740. frpc_node->handle = handle;
  741. frpc_node->cvp_fastrpc_driver = cvp_fastrpc_client;
  742. frpc_node->cvp_fastrpc_driver.handle = handle;
  743. rc = fastrpc_driver_register(&frpc_node->cvp_fastrpc_driver);
  744. if (rc) {
  745. dprintk(CVP_ERR, "%s fastrpc driver reg fail err %d\n",
  746. __func__, rc);
  747. goto fail_fastrpc_driver_register;
  748. }
  749. /* signal wait reuse dsp timeout setup for now */
  750. if (!wait_for_completion_timeout(
  751. &frpc_node->fastrpc_probe_completion,
  752. msecs_to_jiffies(CVP_DSP_RESPONSE_TIMEOUT))) {
  753. dprintk(CVP_ERR, "%s fastrpc driver_register timeout\n",
  754. __func__);
  755. goto fail_fastrpc_driver_timeout;
  756. }
  757. }
  758. return rc;
  759. fail_fastrpc_driver_timeout:
  760. /* remove list if this is the last session */
  761. mutex_lock(&me->fastrpc_driver_list.lock);
  762. list_del(&frpc_node->list);
  763. mutex_unlock(&me->fastrpc_driver_list.lock);
  764. fastrpc_driver_unregister(&frpc_node->cvp_fastrpc_driver);
  765. fail_fastrpc_driver_register:
  766. kfree(frpc_node);
  767. return -EINVAL;
  768. }
  769. static void eva_fastrpc_driver_unregister(uint32_t handle, bool force_exit)
  770. {
  771. struct cvp_dsp_apps *me = &gfa_cv;
  772. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  773. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  774. dprintk(CVP_DSP, "%s Unregister fastrpc driver handle 0x%x, force %d\n",
  775. __func__, handle, (uint32_t)force_exit);
  776. /* Foundd fastrpc node */
  777. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  778. if (frpc_node == NULL)
  779. return;
  780. if ((frpc_node->session_cnt == 0) || force_exit) {
  781. dprintk(CVP_DSP, "%s session cnt %d, force %d\n",
  782. __func__, frpc_node->session_cnt, (uint32_t)force_exit);
  783. DEINIT_MSM_CVP_LIST(&frpc_node->dsp_sessions);
  784. /* remove list if this is the last session */
  785. mutex_lock(&me->fastrpc_driver_list.lock);
  786. list_del(&frpc_node->list);
  787. mutex_unlock(&me->fastrpc_driver_list.lock);
  788. fastrpc_driver_unregister(&frpc_node->cvp_fastrpc_driver);
  789. kfree(frpc_node);
  790. }
  791. }
  792. void cvp_dsp_send_debug_mask(void)
  793. {
  794. struct cvp_dsp_cmd_msg cmd;
  795. struct cvp_dsp_apps *me = &gfa_cv;
  796. struct cvp_dsp_rsp_msg rsp;
  797. int rc;
  798. cmd.type = CPU2DSP_SET_DEBUG_LEVEL;
  799. cmd.eva_dsp_debug_mask = me->debug_mask;
  800. dprintk(CVP_DSP,
  801. "%s: debug mask 0x%x\n",
  802. __func__, cmd.eva_dsp_debug_mask);
  803. rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg), &rsp);
  804. if (rc)
  805. dprintk(CVP_ERR,
  806. "%s: cvp_dsp_send_cmd failed rc = %d\n",
  807. __func__, rc);
  808. }
  809. void cvp_dsp_send_hfi_queue(void)
  810. {
  811. struct msm_cvp_core *core;
  812. struct iris_hfi_device *device;
  813. struct cvp_dsp_apps *me = &gfa_cv;
  814. struct cvp_dsp_rsp_msg rsp = {0};
  815. uint64_t addr;
  816. uint32_t size;
  817. int rc;
  818. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  819. if (core && core->device)
  820. device = core->device->hfi_device_data;
  821. else
  822. return;
  823. if (!device) {
  824. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  825. return;
  826. }
  827. dprintk(CVP_DSP, "Entering %s\n", __func__);
  828. mutex_lock(&device->lock);
  829. mutex_lock(&me->tx_lock);
  830. if (!device->dsp_iface_q_table.align_virtual_addr) {
  831. dprintk(CVP_ERR, "%s: DSP HFI queue released\n", __func__);
  832. goto exit;
  833. }
  834. addr = (uint64_t)device->dsp_iface_q_table.mem_data.dma_handle;
  835. size = device->dsp_iface_q_table.mem_data.size;
  836. if (!addr || !size) {
  837. dprintk(CVP_DSP, "%s: HFI queue is not ready\n", __func__);
  838. goto exit;
  839. }
  840. if (me->state != DSP_PROBED && me->state != DSP_INACTIVE)
  841. goto exit;
  842. rc = cvp_hyp_assign_to_dsp(addr, size);
  843. if (rc) {
  844. dprintk(CVP_ERR, "%s: cvp_hyp_assign_to_dsp. rc=%d\n",
  845. __func__, rc);
  846. goto exit;
  847. }
  848. if (me->state == DSP_PROBED) {
  849. cvp_dsp_init_hfi_queue_hdr(device);
  850. dprintk(CVP_WARN,
  851. "%s: Done init of HFI queue headers\n", __func__);
  852. }
  853. rc = cvp_dsp_send_cmd_hfi_queue((phys_addr_t *)addr, size, &rsp);
  854. if (rc) {
  855. dprintk(CVP_WARN, "%s: Send HFI Queue failed rc = %d\n",
  856. __func__, rc);
  857. goto exit;
  858. }
  859. if (rsp.ret == CPU2DSP_EUNSUPPORTED) {
  860. dprintk(CVP_WARN, "%s unsupported cmd %d\n",
  861. __func__, rsp.type);
  862. goto exit;
  863. }
  864. if (rsp.ret == CPU2DSP_EFATAL || rsp.ret == CPU2DSP_EUNAVAILABLE) {
  865. dprintk(CVP_ERR, "%s fatal error returned %d\n",
  866. __func__, rsp.dsp_state);
  867. me->state = DSP_INVALID;
  868. cvp_hyp_assign_from_dsp();
  869. goto exit;
  870. } else if (rsp.ret == CPU2DSP_EINVALSTATE) {
  871. dprintk(CVP_ERR, "%s dsp invalid state %d\n",
  872. __func__, rsp.dsp_state);
  873. mutex_unlock(&me->tx_lock);
  874. if (cvp_reinit_dsp()) {
  875. dprintk(CVP_ERR, "%s reinit dsp fail\n", __func__);
  876. mutex_unlock(&device->lock);
  877. return;
  878. }
  879. mutex_lock(&me->tx_lock);
  880. }
  881. dprintk(CVP_DSP, "%s: dsp initialized\n", __func__);
  882. me->state = DSP_READY;
  883. exit:
  884. mutex_unlock(&me->tx_lock);
  885. mutex_unlock(&device->lock);
  886. }
  887. /* 32 or 64 bit CPU Side Ptr <-> 2 32 bit DSP Pointers. Dirty Fix. */
  888. static void *ptr_dsp2cpu(uint32_t session_cpu_high, uint32_t session_cpu_low)
  889. {
  890. void *inst;
  891. if ((session_cpu_high == 0) && (sizeof(void *) == BITPTRSIZE32)) {
  892. inst = (void *)((uintptr_t)session_cpu_low);
  893. } else if ((session_cpu_high != 0) && (sizeof(void *) == BITPTRSIZE64)) {
  894. inst = (void *)((uintptr_t)(((uint64_t)session_cpu_high) << 32
  895. | session_cpu_low));
  896. } else {
  897. dprintk(CVP_ERR,
  898. "%s Invalid _cpu_high = 0x%x _cpu_low = 0x%x\n",
  899. __func__, session_cpu_high, session_cpu_low);
  900. inst = NULL;
  901. }
  902. return inst;
  903. }
  904. static void print_power(const struct eva_power_req *pwr_req)
  905. {
  906. if (pwr_req) {
  907. dprintk(CVP_DSP, "Clock: Fdu %d Ica %d Od %d Mpu %d Fw %d",
  908. pwr_req->clock_fdu, pwr_req->clock_ica,
  909. pwr_req->clock_od, pwr_req->clock_mpu,
  910. pwr_req->clock_fw);
  911. dprintk(CVP_DSP, "OpClock: Fdu %d Ica %d Od %d Mpu %d Fw %d",
  912. pwr_req->op_clock_fdu, pwr_req->op_clock_ica,
  913. pwr_req->op_clock_od, pwr_req->op_clock_mpu,
  914. pwr_req->op_clock_fw);
  915. dprintk(CVP_DSP, "Actual Bw: Ddr %d, SysCache %d",
  916. pwr_req->bw_ddr, pwr_req->bw_sys_cache);
  917. dprintk(CVP_DSP, "OpBw: Ddr %d, SysCache %d",
  918. pwr_req->op_bw_ddr, pwr_req->op_bw_sys_cache);
  919. }
  920. }
  921. static void __dsp_cvp_sess_create(struct cvp_dsp_cmd_msg *cmd)
  922. {
  923. struct cvp_dsp_apps *me = &gfa_cv;
  924. struct msm_cvp_inst *inst = NULL;
  925. uint64_t inst_handle = 0;
  926. int rc = 0;
  927. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  928. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  929. struct pid *pid_s = NULL;
  930. struct task_struct *task = NULL;
  931. cmd->ret = 0;
  932. dprintk(CVP_DSP,
  933. "%s sess Type %d Mask %d Prio %d Sec %d pid 0x%x\n",
  934. __func__, dsp2cpu_cmd->session_type,
  935. dsp2cpu_cmd->kernel_mask,
  936. dsp2cpu_cmd->session_prio,
  937. dsp2cpu_cmd->is_secure,
  938. dsp2cpu_cmd->pid);
  939. rc = eva_fastrpc_driver_register(dsp2cpu_cmd->pid);
  940. if (rc) {
  941. dprintk(CVP_ERR, "%s Register fastrpc driver fail\n", __func__);
  942. cmd->ret = -1;
  943. return;
  944. }
  945. inst = msm_cvp_open(MSM_CORE_CVP, MSM_CVP_DSP);
  946. if (!inst) {
  947. dprintk(CVP_ERR, "%s Failed create instance\n", __func__);
  948. goto fail_msm_cvp_open;
  949. }
  950. inst->process_id = dsp2cpu_cmd->pid;
  951. inst->prop.kernel_mask = dsp2cpu_cmd->kernel_mask;
  952. inst->prop.type = dsp2cpu_cmd->session_type;
  953. inst->prop.priority = dsp2cpu_cmd->session_prio;
  954. inst->prop.is_secure = dsp2cpu_cmd->is_secure;
  955. inst->prop.dsp_mask = dsp2cpu_cmd->dsp_access_mask;
  956. rc = msm_cvp_session_create(inst);
  957. if (rc) {
  958. dprintk(CVP_ERR, "Warning: send Session Create failed\n");
  959. goto fail_session_create;
  960. } else {
  961. dprintk(CVP_DSP, "%s DSP Session Create done\n", __func__);
  962. }
  963. /* Get session id */
  964. rc = msm_cvp_get_session_info(inst, &cmd->session_id);
  965. if (rc) {
  966. dprintk(CVP_ERR, "Warning: get session index failed %d\n", rc);
  967. goto fail_get_session_info;
  968. }
  969. inst_handle = (uint64_t)inst;
  970. cmd->session_cpu_high = (uint32_t)((inst_handle & HIGH32) >> 32);
  971. cmd->session_cpu_low = (uint32_t)(inst_handle & LOW32);
  972. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  973. if (frpc_node)
  974. eva_fastrpc_driver_add_sess(frpc_node, inst);
  975. pid_s = find_get_pid(inst->process_id);
  976. if (pid_s == NULL) {
  977. dprintk(CVP_WARN, "%s incorrect pid\n", __func__);
  978. goto fail_get_pid;
  979. }
  980. dprintk(CVP_DSP, "%s get pid_s 0x%x from pidA 0x%x\n", __func__,
  981. pid_s, inst->process_id);
  982. task = get_pid_task(pid_s, PIDTYPE_TGID);
  983. if (!task) {
  984. dprintk(CVP_WARN, "%s task doesn't exist\n", __func__);
  985. goto fail_get_task;
  986. }
  987. inst->task = task;
  988. dprintk(CVP_DSP,
  989. "%s CREATE_SESS id 0x%x, cpu_low 0x%x, cpu_high 0x%x\n",
  990. __func__, cmd->session_id, cmd->session_cpu_low,
  991. cmd->session_cpu_high);
  992. return;
  993. fail_get_pid:
  994. fail_get_task:
  995. fail_get_session_info:
  996. fail_session_create:
  997. msm_cvp_close(inst);
  998. fail_msm_cvp_open:
  999. /* unregister fastrpc driver */
  1000. eva_fastrpc_driver_unregister(dsp2cpu_cmd->pid, false);
  1001. cmd->ret = -1;
  1002. }
  1003. static void __dsp_cvp_sess_delete(struct cvp_dsp_cmd_msg *cmd)
  1004. {
  1005. struct cvp_dsp_apps *me = &gfa_cv;
  1006. struct msm_cvp_inst *inst;
  1007. int rc;
  1008. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1009. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1010. struct task_struct *task = NULL;
  1011. cmd->ret = 0;
  1012. dprintk(CVP_DSP,
  1013. "%s sess id 0x%x low 0x%x high 0x%x, pid 0x%x\n",
  1014. __func__, dsp2cpu_cmd->session_id,
  1015. dsp2cpu_cmd->session_cpu_low,
  1016. dsp2cpu_cmd->session_cpu_high,
  1017. dsp2cpu_cmd->pid);
  1018. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1019. if (!frpc_node) {
  1020. dprintk(CVP_ERR, "%s pid 0x%x not registered with fastrpc\n",
  1021. __func__, dsp2cpu_cmd->pid);
  1022. cmd->ret = -1;
  1023. return;
  1024. }
  1025. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1026. dsp2cpu_cmd->session_cpu_high,
  1027. dsp2cpu_cmd->session_cpu_low);
  1028. if (!inst) {
  1029. dprintk(CVP_ERR, "%s incorrect session ID\n", __func__);
  1030. cmd->ret = -1;
  1031. goto dsp_fail_delete;
  1032. }
  1033. rc = msm_cvp_session_delete(inst);
  1034. if (rc) {
  1035. dprintk(CVP_ERR, "Warning: send Delete Session failed\n");
  1036. cmd->ret = -1;
  1037. goto dsp_fail_delete;
  1038. }
  1039. task = inst->task;
  1040. rc = msm_cvp_close(inst);
  1041. if (rc) {
  1042. dprintk(CVP_ERR, "Warning: Failed to close cvp instance\n");
  1043. cmd->ret = -1;
  1044. goto dsp_fail_delete;
  1045. }
  1046. /* unregister fastrpc driver */
  1047. eva_fastrpc_driver_unregister(dsp2cpu_cmd->pid, false);
  1048. if (task)
  1049. put_task_struct(task);
  1050. dprintk(CVP_DSP, "%s DSP2CPU_DETELE_SESSION Done\n", __func__);
  1051. dsp_fail_delete:
  1052. return;
  1053. }
  1054. static void __dsp_cvp_power_req(struct cvp_dsp_cmd_msg *cmd)
  1055. {
  1056. struct cvp_dsp_apps *me = &gfa_cv;
  1057. struct msm_cvp_inst *inst;
  1058. int rc;
  1059. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1060. cmd->ret = 0;
  1061. dprintk(CVP_DSP,
  1062. "%s sess id 0x%x, low 0x%x, high 0x%x\n",
  1063. __func__, dsp2cpu_cmd->session_id,
  1064. dsp2cpu_cmd->session_cpu_low,
  1065. dsp2cpu_cmd->session_cpu_high);
  1066. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1067. dsp2cpu_cmd->session_cpu_high,
  1068. dsp2cpu_cmd->session_cpu_low);
  1069. if (!inst) {
  1070. cmd->ret = -1;
  1071. goto dsp_fail_power_req;
  1072. }
  1073. print_power(&dsp2cpu_cmd->power_req);
  1074. inst->prop.fdu_cycles = dsp2cpu_cmd->power_req.clock_fdu;
  1075. inst->prop.ica_cycles = dsp2cpu_cmd->power_req.clock_ica;
  1076. inst->prop.od_cycles = dsp2cpu_cmd->power_req.clock_od;
  1077. inst->prop.mpu_cycles = dsp2cpu_cmd->power_req.clock_mpu;
  1078. inst->prop.fw_cycles = dsp2cpu_cmd->power_req.clock_fw;
  1079. inst->prop.ddr_bw = dsp2cpu_cmd->power_req.bw_ddr;
  1080. inst->prop.ddr_cache = dsp2cpu_cmd->power_req.bw_sys_cache;
  1081. inst->prop.fdu_op_cycles = dsp2cpu_cmd->power_req.op_clock_fdu;
  1082. inst->prop.ica_op_cycles = dsp2cpu_cmd->power_req.op_clock_ica;
  1083. inst->prop.od_op_cycles = dsp2cpu_cmd->power_req.op_clock_od;
  1084. inst->prop.mpu_op_cycles = dsp2cpu_cmd->power_req.op_clock_mpu;
  1085. inst->prop.fw_op_cycles = dsp2cpu_cmd->power_req.op_clock_fw;
  1086. inst->prop.ddr_op_bw = dsp2cpu_cmd->power_req.op_bw_ddr;
  1087. inst->prop.ddr_op_cache = dsp2cpu_cmd->power_req.op_bw_sys_cache;
  1088. rc = msm_cvp_update_power(inst);
  1089. if (rc) {
  1090. /*
  1091. *May need to define more error types
  1092. * Check UMD implementation
  1093. */
  1094. dprintk(CVP_ERR, "%s Failed update power\n", __func__);
  1095. cmd->ret = -1;
  1096. goto dsp_fail_power_req;
  1097. }
  1098. dprintk(CVP_DSP, "%s DSP2CPU_POWER_REQUEST Done\n", __func__);
  1099. dsp_fail_power_req:
  1100. return;
  1101. }
  1102. static void __dsp_cvp_buf_register(struct cvp_dsp_cmd_msg *cmd)
  1103. {
  1104. struct cvp_dsp_apps *me = &gfa_cv;
  1105. struct msm_cvp_inst *inst;
  1106. struct eva_kmd_arg *kmd;
  1107. struct eva_kmd_buffer *kmd_buf;
  1108. int rc;
  1109. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1110. cmd->ret = 0;
  1111. dprintk(CVP_DSP,
  1112. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1113. __func__, dsp2cpu_cmd->session_id,
  1114. dsp2cpu_cmd->session_cpu_low,
  1115. dsp2cpu_cmd->session_cpu_high,
  1116. dsp2cpu_cmd->pid);
  1117. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1118. if (!kmd) {
  1119. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1120. cmd->ret = -1;
  1121. return;
  1122. }
  1123. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1124. dsp2cpu_cmd->session_cpu_high,
  1125. dsp2cpu_cmd->session_cpu_low);
  1126. kmd->type = EVA_KMD_REGISTER_BUFFER;
  1127. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1128. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1129. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1130. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1131. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1132. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1133. kmd_buf->pixelformat = 0;
  1134. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1135. rc = msm_cvp_register_buffer(inst, kmd_buf);
  1136. if (rc) {
  1137. dprintk(CVP_ERR, "%s Failed to register buffer\n", __func__);
  1138. cmd->ret = -1;
  1139. goto dsp_fail_buf_reg;
  1140. }
  1141. dprintk(CVP_DSP, "%s register buffer done\n", __func__);
  1142. cmd->sbuf.iova = kmd_buf->reserved[0];
  1143. cmd->sbuf.size = kmd_buf->size;
  1144. cmd->sbuf.fd = kmd_buf->fd;
  1145. cmd->sbuf.index = kmd_buf->index;
  1146. cmd->sbuf.offset = kmd_buf->offset;
  1147. dprintk(CVP_DSP, "%s: fd %d, iova 0x%x\n", __func__,
  1148. cmd->sbuf.fd, cmd->sbuf.iova);
  1149. dsp_fail_buf_reg:
  1150. kfree(kmd);
  1151. }
  1152. static void __dsp_cvp_buf_deregister(struct cvp_dsp_cmd_msg *cmd)
  1153. {
  1154. struct cvp_dsp_apps *me = &gfa_cv;
  1155. struct msm_cvp_inst *inst;
  1156. struct eva_kmd_arg *kmd;
  1157. struct eva_kmd_buffer *kmd_buf;
  1158. int rc;
  1159. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1160. cmd->ret = 0;
  1161. dprintk(CVP_DSP,
  1162. "%s : sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1163. __func__, dsp2cpu_cmd->session_id,
  1164. dsp2cpu_cmd->session_cpu_low,
  1165. dsp2cpu_cmd->session_cpu_high,
  1166. dsp2cpu_cmd->pid);
  1167. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1168. if (!kmd) {
  1169. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1170. cmd->ret = -1;
  1171. return;
  1172. }
  1173. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1174. dsp2cpu_cmd->session_cpu_high,
  1175. dsp2cpu_cmd->session_cpu_low);
  1176. kmd->type = EVA_KMD_UNREGISTER_BUFFER;
  1177. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1178. kmd_buf->type = EVA_KMD_UNREGISTER_BUFFER;
  1179. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1180. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1181. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1182. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1183. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1184. kmd_buf->pixelformat = 0;
  1185. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1186. rc = msm_cvp_unregister_buffer(inst, kmd_buf);
  1187. if (rc) {
  1188. dprintk(CVP_ERR, "%s Failed to deregister buffer\n", __func__);
  1189. cmd->ret = -1;
  1190. goto fail_dsp_buf_dereg;
  1191. }
  1192. dprintk(CVP_DSP, "%s deregister buffer done\n", __func__);
  1193. fail_dsp_buf_dereg:
  1194. kfree(kmd);
  1195. }
  1196. static void __dsp_cvp_mem_alloc(struct cvp_dsp_cmd_msg *cmd)
  1197. {
  1198. struct cvp_dsp_apps *me = &gfa_cv;
  1199. struct msm_cvp_inst *inst;
  1200. int rc;
  1201. struct cvp_internal_buf *buf = NULL;
  1202. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1203. uint64_t v_dsp_addr = 0;
  1204. struct fastrpc_device *frpc_device = NULL;
  1205. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1206. cmd->ret = 0;
  1207. dprintk(CVP_DSP,
  1208. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1209. __func__, dsp2cpu_cmd->session_id,
  1210. dsp2cpu_cmd->session_cpu_low,
  1211. dsp2cpu_cmd->session_cpu_high,
  1212. dsp2cpu_cmd->pid);
  1213. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1214. if (!frpc_node) {
  1215. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1216. __func__, dsp2cpu_cmd->pid);
  1217. goto fail_fastrpc_node;
  1218. }
  1219. frpc_device = frpc_node->cvp_fastrpc_device;
  1220. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1221. dsp2cpu_cmd->session_cpu_high,
  1222. dsp2cpu_cmd->session_cpu_low);
  1223. buf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  1224. if (!buf)
  1225. goto fail_kzalloc_buf;
  1226. rc = cvp_allocate_dsp_bufs(inst, buf,
  1227. dsp2cpu_cmd->sbuf.size,
  1228. dsp2cpu_cmd->sbuf.type);
  1229. if (rc)
  1230. goto fail_allocate_dsp_buf;
  1231. rc = eva_fastrpc_dev_map_dma(frpc_device, buf,
  1232. dsp2cpu_cmd->sbuf.dsp_remote_map,
  1233. &v_dsp_addr);
  1234. if (rc) {
  1235. dprintk(CVP_ERR, "%s Failed to map buffer 0x%x\n", __func__,
  1236. rc);
  1237. goto fail_fastrpc_dev_map_dma;
  1238. }
  1239. mutex_lock(&inst->cvpdspbufs.lock);
  1240. list_add_tail(&buf->list, &inst->cvpdspbufs.list);
  1241. mutex_unlock(&inst->cvpdspbufs.lock);
  1242. dprintk(CVP_DSP, "%s allocate buffer done, addr 0x%llx\n",
  1243. __func__, v_dsp_addr);
  1244. cmd->sbuf.size = buf->smem->size;
  1245. cmd->sbuf.fd = buf->fd;
  1246. cmd->sbuf.offset = 0;
  1247. cmd->sbuf.iova = buf->smem->device_addr;
  1248. cmd->sbuf.v_dsp_addr = v_dsp_addr;
  1249. dprintk(CVP_DSP, "%s: size %d, iova 0x%x, v_dsp_addr 0x%llx\n",
  1250. __func__, cmd->sbuf.size, cmd->sbuf.iova,
  1251. cmd->sbuf.v_dsp_addr);
  1252. return;
  1253. fail_fastrpc_dev_map_dma:
  1254. cvp_release_dsp_buffers(inst, buf);
  1255. fail_allocate_dsp_buf:
  1256. kmem_cache_free(cvp_driver->buf_cache, buf);
  1257. fail_kzalloc_buf:
  1258. fail_fastrpc_node:
  1259. cmd->ret = -1;
  1260. return;
  1261. }
  1262. static void __dsp_cvp_mem_free(struct cvp_dsp_cmd_msg *cmd)
  1263. {
  1264. struct cvp_dsp_apps *me = &gfa_cv;
  1265. struct msm_cvp_inst *inst;
  1266. int rc;
  1267. struct cvp_internal_buf *buf = NULL;
  1268. struct list_head *ptr = NULL, *next = NULL;
  1269. struct msm_cvp_list *buf_list = NULL;
  1270. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1271. struct fastrpc_device *frpc_device = NULL;
  1272. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1273. cmd->ret = 0;
  1274. dprintk(CVP_DSP,
  1275. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1276. __func__, dsp2cpu_cmd->session_id,
  1277. dsp2cpu_cmd->session_cpu_low,
  1278. dsp2cpu_cmd->session_cpu_high,
  1279. dsp2cpu_cmd->pid);
  1280. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1281. dsp2cpu_cmd->session_cpu_high,
  1282. dsp2cpu_cmd->session_cpu_low);
  1283. if (!inst) {
  1284. dprintk(CVP_ERR, "%s Failed to get inst\n",
  1285. __func__);
  1286. cmd->ret = -1;
  1287. return;
  1288. }
  1289. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1290. if (!frpc_node) {
  1291. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1292. __func__, dsp2cpu_cmd->pid);
  1293. cmd->ret = -1;
  1294. return;
  1295. }
  1296. frpc_device = frpc_node->cvp_fastrpc_device;
  1297. buf_list = &inst->cvpdspbufs;
  1298. mutex_lock(&buf_list->lock);
  1299. list_for_each_safe(ptr, next, &buf_list->list) {
  1300. buf = list_entry(ptr, struct cvp_internal_buf, list);
  1301. dprintk(CVP_DSP, "fd in list 0x%x, fd from dsp 0x%x\n",
  1302. buf->fd, dsp2cpu_cmd->sbuf.fd);
  1303. if (!buf->smem) {
  1304. dprintk(CVP_DSP, "Empyt smem\n");
  1305. continue;
  1306. }
  1307. /* Verify with device addr */
  1308. if (buf->smem->device_addr == dsp2cpu_cmd->sbuf.iova) {
  1309. dprintk(CVP_DSP, "%s find device addr 0x%x\n",
  1310. __func__, buf->smem->device_addr);
  1311. rc = eva_fastrpc_dev_unmap_dma(frpc_device, buf);
  1312. if (rc) {
  1313. dprintk(CVP_ERR,
  1314. "%s Failed to unmap buffer 0x%x\n",
  1315. __func__, rc);
  1316. cmd->ret = -1;
  1317. goto fail_fastrpc_dev_unmap_dma;
  1318. }
  1319. rc = cvp_release_dsp_buffers(inst, buf);
  1320. if (rc) {
  1321. dprintk(CVP_ERR,
  1322. "%s Failed to free buffer 0x%x\n",
  1323. __func__, rc);
  1324. cmd->ret = -1;
  1325. goto fail_release_buf;
  1326. }
  1327. list_del(&buf->list);
  1328. kmem_cache_free(cvp_driver->buf_cache, buf);
  1329. break;
  1330. }
  1331. }
  1332. fail_release_buf:
  1333. fail_fastrpc_dev_unmap_dma:
  1334. mutex_unlock(&buf_list->lock);
  1335. }
  1336. static int cvp_dsp_thread(void *data)
  1337. {
  1338. int rc = 0, old_state;
  1339. struct cvp_dsp_apps *me = &gfa_cv;
  1340. struct cvp_dsp_cmd_msg cmd;
  1341. struct cvp_hfi_device *hdev;
  1342. struct msm_cvp_core *core;
  1343. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  1344. if (!core) {
  1345. dprintk(CVP_ERR, "%s: Failed to find core\n", __func__);
  1346. rc = -EINVAL;
  1347. goto exit;
  1348. }
  1349. hdev = (struct cvp_hfi_device *)core->device;
  1350. if (!hdev) {
  1351. dprintk(CVP_ERR, "%s Invalid device handle\n", __func__);
  1352. rc = -EINVAL;
  1353. goto exit;
  1354. }
  1355. wait_dsp:
  1356. rc = wait_for_completion_interruptible(
  1357. &me->completions[CPU2DSP_MAX_CMD]);
  1358. if (me->state == DSP_INVALID)
  1359. goto exit;
  1360. if (me->state == DSP_UNINIT)
  1361. goto wait_dsp;
  1362. if (me->state == DSP_PROBED) {
  1363. cvp_dsp_send_hfi_queue();
  1364. goto wait_dsp;
  1365. }
  1366. cmd.type = me->pending_dsp2cpu_cmd.type;
  1367. if (rc == -ERESTARTSYS) {
  1368. dprintk(CVP_WARN, "%s received interrupt signal\n", __func__);
  1369. } else {
  1370. mutex_lock(&me->rx_lock);
  1371. switch (me->pending_dsp2cpu_cmd.type) {
  1372. case DSP2CPU_POWERON:
  1373. {
  1374. if (me->state == DSP_READY) {
  1375. cmd.ret = 0;
  1376. break;
  1377. }
  1378. mutex_lock(&me->tx_lock);
  1379. old_state = me->state;
  1380. me->state = DSP_READY;
  1381. rc = call_hfi_op(hdev, resume, hdev->hfi_device_data);
  1382. if (rc) {
  1383. dprintk(CVP_WARN, "%s Failed to resume cvp\n",
  1384. __func__);
  1385. me->state = old_state;
  1386. mutex_unlock(&me->tx_lock);
  1387. cmd.ret = 1;
  1388. break;
  1389. }
  1390. mutex_unlock(&me->tx_lock);
  1391. cmd.ret = 0;
  1392. break;
  1393. }
  1394. case DSP2CPU_POWEROFF:
  1395. {
  1396. me->state = DSP_SUSPEND;
  1397. cmd.ret = 0;
  1398. break;
  1399. }
  1400. case DSP2CPU_CREATE_SESSION:
  1401. {
  1402. __dsp_cvp_sess_create(&cmd);
  1403. break;
  1404. }
  1405. case DSP2CPU_DETELE_SESSION:
  1406. {
  1407. __dsp_cvp_sess_delete(&cmd);
  1408. break;
  1409. }
  1410. case DSP2CPU_POWER_REQUEST:
  1411. {
  1412. __dsp_cvp_power_req(&cmd);
  1413. break;
  1414. }
  1415. case DSP2CPU_REGISTER_BUFFER:
  1416. {
  1417. __dsp_cvp_buf_register(&cmd);
  1418. break;
  1419. }
  1420. case DSP2CPU_DEREGISTER_BUFFER:
  1421. {
  1422. __dsp_cvp_buf_deregister(&cmd);
  1423. break;
  1424. }
  1425. case DSP2CPU_MEM_ALLOC:
  1426. {
  1427. __dsp_cvp_mem_alloc(&cmd);
  1428. break;
  1429. }
  1430. case DSP2CPU_MEM_FREE:
  1431. {
  1432. __dsp_cvp_mem_free(&cmd);
  1433. break;
  1434. }
  1435. default:
  1436. dprintk(CVP_ERR, "unrecognaized dsp cmds: %d\n",
  1437. me->pending_dsp2cpu_cmd.type);
  1438. break;
  1439. }
  1440. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1441. mutex_unlock(&me->rx_lock);
  1442. }
  1443. /* Responds to DSP */
  1444. rc = cvp_dsp_send_cmd(&cmd, sizeof(struct cvp_dsp_cmd_msg));
  1445. if (rc)
  1446. dprintk(CVP_ERR,
  1447. "%s: cvp_dsp_send_cmd failed rc = %d cmd type=%d\n",
  1448. __func__, rc, cmd.type);
  1449. goto wait_dsp;
  1450. exit:
  1451. dprintk(CVP_DBG, "dsp thread exit\n");
  1452. do_exit(rc);
  1453. return rc;
  1454. }
  1455. int cvp_dsp_device_init(void)
  1456. {
  1457. struct cvp_dsp_apps *me = &gfa_cv;
  1458. char tname[16];
  1459. int rc;
  1460. int i;
  1461. mutex_init(&me->tx_lock);
  1462. mutex_init(&me->rx_lock);
  1463. me->state = DSP_INVALID;
  1464. me->hyp_assigned = false;
  1465. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1466. init_completion(&me->completions[i]);
  1467. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1468. me->pending_dsp2cpu_rsp.type = CVP_INVALID_RPMSG_TYPE;
  1469. INIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1470. rc = register_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1471. if (rc) {
  1472. dprintk(CVP_ERR,
  1473. "%s : register_rpmsg_driver failed rc = %d\n",
  1474. __func__, rc);
  1475. goto register_bail;
  1476. }
  1477. snprintf(tname, sizeof(tname), "cvp-dsp-thread");
  1478. me->state = DSP_UNINIT;
  1479. me->dsp_thread = kthread_run(cvp_dsp_thread, me, tname);
  1480. if (!me->dsp_thread) {
  1481. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  1482. rc = -ECHILD;
  1483. me->state = DSP_INVALID;
  1484. goto register_bail;
  1485. }
  1486. return 0;
  1487. register_bail:
  1488. return rc;
  1489. }
  1490. void cvp_dsp_device_exit(void)
  1491. {
  1492. struct cvp_dsp_apps *me = &gfa_cv;
  1493. int i;
  1494. mutex_lock(&me->tx_lock);
  1495. me->state = DSP_INVALID;
  1496. mutex_unlock(&me->tx_lock);
  1497. DEINIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1498. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1499. complete_all(&me->completions[i]);
  1500. mutex_destroy(&me->tx_lock);
  1501. mutex_destroy(&me->rx_lock);
  1502. unregister_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1503. }