msm_cvp_dsp.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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. 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, 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_hfi_queue(void)
  793. {
  794. struct msm_cvp_core *core;
  795. struct iris_hfi_device *device;
  796. struct cvp_dsp_apps *me = &gfa_cv;
  797. struct cvp_dsp_rsp_msg rsp = {0};
  798. uint64_t addr;
  799. uint32_t size;
  800. int rc;
  801. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  802. if (core && core->device)
  803. device = core->device->hfi_device_data;
  804. else
  805. return;
  806. if (!device) {
  807. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  808. return;
  809. }
  810. dprintk(CVP_DSP, "Entering %s\n", __func__);
  811. mutex_lock(&device->lock);
  812. mutex_lock(&me->lock);
  813. if (!device->dsp_iface_q_table.align_virtual_addr) {
  814. dprintk(CVP_ERR, "%s: DSP HFI queue released\n", __func__);
  815. mutex_unlock(&me->lock);
  816. mutex_unlock(&device->lock);
  817. return;
  818. }
  819. addr = (uint64_t)device->dsp_iface_q_table.mem_data.dma_handle;
  820. size = device->dsp_iface_q_table.mem_data.size;
  821. if (!addr || !size) {
  822. dprintk(CVP_DSP, "%s: HFI queue is not ready\n", __func__);
  823. goto exit;
  824. }
  825. if (me->state != DSP_PROBED && me->state != DSP_INACTIVE)
  826. goto exit;
  827. rc = cvp_hyp_assign_to_dsp(addr, size);
  828. if (rc) {
  829. dprintk(CVP_ERR, "%s: cvp_hyp_assign_to_dsp. rc=%d\n",
  830. __func__, rc);
  831. goto exit;
  832. }
  833. if (me->state == DSP_PROBED) {
  834. cvp_dsp_init_hfi_queue_hdr(device);
  835. dprintk(CVP_WARN,
  836. "%s: Done init of HFI queue headers\n", __func__);
  837. }
  838. rc = cvp_dsp_send_cmd_hfi_queue((phys_addr_t *)addr, size, &rsp);
  839. if (rc) {
  840. dprintk(CVP_WARN, "%s: Send HFI Queue failed rc = %d\n",
  841. __func__, rc);
  842. goto exit;
  843. }
  844. if (rsp.ret == CPU2DSP_EUNSUPPORTED) {
  845. dprintk(CVP_WARN, "%s unsupported cmd %d\n",
  846. __func__, rsp.type);
  847. goto exit;
  848. }
  849. if (rsp.ret == CPU2DSP_EFATAL || rsp.ret == CPU2DSP_EUNAVAILABLE) {
  850. dprintk(CVP_ERR, "%s fatal error returned %d\n",
  851. __func__, rsp.dsp_state);
  852. me->state = DSP_INVALID;
  853. cvp_hyp_assign_from_dsp();
  854. goto exit;
  855. } else if (rsp.ret == CPU2DSP_EINVALSTATE) {
  856. dprintk(CVP_ERR, "%s dsp invalid state %d\n",
  857. __func__, rsp.dsp_state);
  858. mutex_unlock(&me->lock);
  859. if (cvp_reinit_dsp()) {
  860. dprintk(CVP_ERR, "%s reinit dsp fail\n", __func__);
  861. mutex_unlock(&device->lock);
  862. return;
  863. }
  864. mutex_lock(&me->lock);
  865. }
  866. dprintk(CVP_DSP, "%s: dsp initialized\n", __func__);
  867. me->state = DSP_READY;
  868. exit:
  869. mutex_unlock(&me->lock);
  870. mutex_unlock(&device->lock);
  871. }
  872. /* 32 or 64 bit CPU Side Ptr <-> 2 32 bit DSP Pointers. Dirty Fix. */
  873. static void *ptr_dsp2cpu(uint32_t session_cpu_high, uint32_t session_cpu_low)
  874. {
  875. void *inst;
  876. if ((session_cpu_high == 0) && (sizeof(void *) == BITPTRSIZE32)) {
  877. inst = (void *)((uintptr_t)session_cpu_low);
  878. } else if ((session_cpu_high != 0) && (sizeof(void *) == BITPTRSIZE64)) {
  879. inst = (void *)((uintptr_t)(((uint64_t)session_cpu_high) << 32
  880. | session_cpu_low));
  881. } else {
  882. dprintk(CVP_ERR,
  883. "%s Invalid _cpu_high = 0x%x _cpu_low = 0x%x\n",
  884. __func__, session_cpu_high, session_cpu_low);
  885. inst = NULL;
  886. }
  887. return inst;
  888. }
  889. static void print_power(const struct eva_power_req *pwr_req)
  890. {
  891. if (pwr_req) {
  892. dprintk(CVP_DSP, "Clock: Fdu %d Ica %d Od %d Mpu %d Fw %d",
  893. pwr_req->clock_fdu, pwr_req->clock_ica,
  894. pwr_req->clock_od, pwr_req->clock_mpu,
  895. pwr_req->clock_fw);
  896. dprintk(CVP_DSP, "OpClock: Fdu %d Ica %d Od %d Mpu %d Fw %d",
  897. pwr_req->op_clock_fdu, pwr_req->op_clock_ica,
  898. pwr_req->op_clock_od, pwr_req->op_clock_mpu,
  899. pwr_req->op_clock_fw);
  900. dprintk(CVP_DSP, "Actual Bw: Ddr %d, SysCache %d",
  901. pwr_req->bw_ddr, pwr_req->bw_sys_cache);
  902. dprintk(CVP_DSP, "OpBw: Ddr %d, SysCache %d",
  903. pwr_req->op_bw_ddr, pwr_req->op_bw_sys_cache);
  904. }
  905. }
  906. static int msm_cvp_register_buffer_dsp(struct msm_cvp_inst *inst,
  907. struct eva_kmd_buffer *buf,
  908. int32_t pid,
  909. uint32_t *iova)
  910. {
  911. struct cvp_hfi_device *hdev;
  912. struct cvp_hal_session *session;
  913. struct msm_cvp_inst *s;
  914. int rc = 0;
  915. if (!inst || !inst->core || !buf) {
  916. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  917. return -EINVAL;
  918. }
  919. if (!buf->index)
  920. return 0;
  921. s = cvp_get_inst_validate(inst->core, inst);
  922. if (!s)
  923. return -ECONNRESET;
  924. inst->cur_cmd_type = EVA_KMD_REGISTER_BUFFER;
  925. session = (struct cvp_hal_session *)inst->session;
  926. if (!session) {
  927. dprintk(CVP_ERR, "%s: invalid session\n", __func__);
  928. rc = -EINVAL;
  929. goto exit;
  930. }
  931. hdev = inst->core->device;
  932. print_client_buffer(CVP_HFI, "register", inst, buf);
  933. rc = msm_cvp_map_buf_dsp_new(inst, buf, pid, iova);
  934. dprintk(CVP_DSP, "%s: fd %d, iova 0x%x\n", __func__, buf->fd, *iova);
  935. exit:
  936. inst->cur_cmd_type = 0;
  937. cvp_put_inst(s);
  938. return rc;
  939. }
  940. static int msm_cvp_unregister_buffer_dsp(struct msm_cvp_inst *inst,
  941. struct eva_kmd_buffer *buf)
  942. {
  943. struct msm_cvp_inst *s;
  944. int rc = 0;
  945. if (!inst || !inst->core || !buf) {
  946. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  947. return -EINVAL;
  948. }
  949. if (!buf->index)
  950. return 0;
  951. s = cvp_get_inst_validate(inst->core, inst);
  952. if (!s)
  953. return -ECONNRESET;
  954. inst->cur_cmd_type = EVA_KMD_UNREGISTER_BUFFER;
  955. print_client_buffer(CVP_HFI, "unregister", inst, buf);
  956. rc = msm_cvp_unmap_buf_dsp_new(inst, buf);
  957. inst->cur_cmd_type = 0;
  958. cvp_put_inst(s);
  959. return rc;
  960. }
  961. static void __dsp_cvp_sess_create(struct cvp_dsp_cmd_msg *cmd)
  962. {
  963. struct cvp_dsp_apps *me = &gfa_cv;
  964. struct msm_cvp_inst *inst = NULL;
  965. uint64_t inst_handle = 0;
  966. struct eva_kmd_arg *kmd;
  967. struct eva_kmd_sys_properties *sys_prop = NULL;
  968. struct eva_kmd_session_control *sys_ctrl = NULL;
  969. int rc = 0;
  970. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  971. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  972. cmd->ret = 0;
  973. dprintk(CVP_DSP,
  974. "%s sess Type %d Mask %d Prio %d Sec %d pid 0x%x\n",
  975. __func__, dsp2cpu_cmd->session_type,
  976. dsp2cpu_cmd->kernel_mask,
  977. dsp2cpu_cmd->session_prio,
  978. dsp2cpu_cmd->is_secure,
  979. dsp2cpu_cmd->pid);
  980. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  981. if (!kmd) {
  982. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  983. goto fail_frpc_driver_reg;
  984. }
  985. rc = eva_fastrpc_driver_register(dsp2cpu_cmd->pid);
  986. if (rc) {
  987. dprintk(CVP_ERR, "%s Register fastrpc driver fail\n", __func__);
  988. goto fail_frpc_driver_reg;
  989. }
  990. inst = msm_cvp_open(MSM_CORE_CVP, MSM_CVP_DSP);
  991. if (!inst) {
  992. dprintk(CVP_ERR, "%s Failed create instance\n", __func__);
  993. goto fail_msm_cvp_open;
  994. }
  995. inst->process_id = dsp2cpu_cmd->pid;
  996. kmd->type = EVA_KMD_SET_SYS_PROPERTY;
  997. sys_prop = (struct eva_kmd_sys_properties *)&kmd->data.sys_properties;
  998. sys_prop->prop_num = 5;
  999. sys_prop->prop_data[0].prop_type = EVA_KMD_PROP_SESSION_KERNELMASK;
  1000. sys_prop->prop_data[0].data = dsp2cpu_cmd->kernel_mask;
  1001. sys_prop->prop_data[1].prop_type = EVA_KMD_PROP_SESSION_TYPE;
  1002. sys_prop->prop_data[1].data = dsp2cpu_cmd->session_type;
  1003. sys_prop->prop_data[2].prop_type = EVA_KMD_PROP_SESSION_PRIORITY;
  1004. sys_prop->prop_data[2].data = dsp2cpu_cmd->session_prio;
  1005. sys_prop->prop_data[3].prop_type = EVA_KMD_PROP_SESSION_SECURITY;
  1006. sys_prop->prop_data[3].data = dsp2cpu_cmd->is_secure;
  1007. sys_prop->prop_data[4].prop_type = EVA_KMD_PROP_SESSION_DSPMASK;
  1008. sys_prop->prop_data[4].data = dsp2cpu_cmd->dsp_access_mask;
  1009. rc = msm_cvp_handle_syscall(inst, kmd);
  1010. if (rc) {
  1011. dprintk(CVP_ERR, "%s Failed to set sys property\n", __func__);
  1012. goto fail_set_sys_property;
  1013. }
  1014. dprintk(CVP_DSP, "%s set sys property done\n", __func__);
  1015. /* EVA_KMD_SESSION_CONTROL from DSP */
  1016. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1017. kmd->type = EVA_KMD_SESSION_CONTROL;
  1018. sys_ctrl = (struct eva_kmd_session_control *)&kmd->data.session_ctrl;
  1019. sys_ctrl->ctrl_type = SESSION_CREATE;
  1020. rc = msm_cvp_handle_syscall(inst, kmd);
  1021. if (rc) {
  1022. dprintk(CVP_ERR, "Warning: send Session Create failed\n");
  1023. goto fail_session_create;
  1024. }
  1025. dprintk(CVP_DSP, "%s send Session Create done\n", __func__);
  1026. /* Get session id */
  1027. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1028. kmd->type = EVA_KMD_GET_SESSION_INFO;
  1029. rc = msm_cvp_handle_syscall(inst, kmd);
  1030. if (rc) {
  1031. dprintk(CVP_ERR, "Warning: get session index failed\n");
  1032. goto fail_get_session_info;
  1033. }
  1034. cmd->session_id = kmd->data.session.session_id;
  1035. inst_handle = (uint64_t)inst;
  1036. cmd->session_cpu_high = (uint32_t)((inst_handle & HIGH32) >> 32);
  1037. cmd->session_cpu_low = (uint32_t)(inst_handle & LOW32);
  1038. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1039. if (frpc_node)
  1040. eva_fastrpc_driver_add_sess(frpc_node, inst);
  1041. dprintk(CVP_DSP,
  1042. "%s CREATE_SESS id 0x%x, cpu_low 0x%x, cpu_high 0x%x\n",
  1043. __func__, cmd->session_id, cmd->session_cpu_low,
  1044. cmd->session_cpu_high);
  1045. kfree(kmd);
  1046. return;
  1047. fail_get_session_info:
  1048. fail_session_create:
  1049. fail_set_sys_property:
  1050. fail_msm_cvp_open:
  1051. /* unregister fastrpc driver */
  1052. fail_frpc_driver_reg:
  1053. cmd->ret = -1;
  1054. kfree(kmd);
  1055. }
  1056. static void __dsp_cvp_sess_delete(struct cvp_dsp_cmd_msg *cmd)
  1057. {
  1058. struct cvp_dsp_apps *me = &gfa_cv;
  1059. struct msm_cvp_inst *inst;
  1060. struct eva_kmd_arg *kmd;
  1061. struct eva_kmd_session_control *sys_ctrl;
  1062. int rc;
  1063. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1064. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1065. cmd->ret = 0;
  1066. dprintk(CVP_DSP,
  1067. "%s sess id 0x%x low 0x%x high 0x%x, pid 0x%x\n",
  1068. __func__, dsp2cpu_cmd->session_id,
  1069. dsp2cpu_cmd->session_cpu_low,
  1070. dsp2cpu_cmd->session_cpu_high,
  1071. dsp2cpu_cmd->pid);
  1072. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1073. if (!frpc_node) {
  1074. dprintk(CVP_ERR, "%s pid 0x%x not registered with fastrpc\n",
  1075. __func__, dsp2cpu_cmd->pid);
  1076. cmd->ret = -1;
  1077. return;
  1078. }
  1079. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1080. if (!kmd) {
  1081. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1082. cmd->ret = -1;
  1083. return;
  1084. }
  1085. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1086. dsp2cpu_cmd->session_cpu_high,
  1087. dsp2cpu_cmd->session_cpu_low);
  1088. kmd->type = EVA_KMD_SESSION_CONTROL;
  1089. sys_ctrl = (struct eva_kmd_session_control *)&kmd->data.session_ctrl;
  1090. /* Session delete does nothing here */
  1091. sys_ctrl->ctrl_type = SESSION_DELETE;
  1092. rc = msm_cvp_handle_syscall(inst, kmd);
  1093. if (rc) {
  1094. dprintk(CVP_ERR, "Warning: send Delete Session failed\n");
  1095. cmd->ret = -1;
  1096. goto dsp_fail_delete;
  1097. }
  1098. rc = msm_cvp_close(inst);
  1099. if (rc) {
  1100. dprintk(CVP_ERR, "Warning: Failed to close cvp instance\n");
  1101. cmd->ret = -1;
  1102. goto dsp_fail_delete;
  1103. }
  1104. /* unregister fastrpc driver */
  1105. eva_fastrpc_driver_unregister(dsp2cpu_cmd->pid, false);
  1106. dprintk(CVP_DSP, "%s DSP2CPU_DETELE_SESSION Done\n", __func__);
  1107. dsp_fail_delete:
  1108. kfree(kmd);
  1109. }
  1110. static void __dsp_cvp_power_req(struct cvp_dsp_cmd_msg *cmd)
  1111. {
  1112. struct cvp_dsp_apps *me = &gfa_cv;
  1113. struct msm_cvp_inst *inst;
  1114. struct eva_kmd_arg *kmd;
  1115. struct eva_kmd_sys_properties *sys_prop;
  1116. int rc;
  1117. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1118. cmd->ret = 0;
  1119. dprintk(CVP_DSP,
  1120. "%s sess id 0x%x, low 0x%x, high 0x%x\n",
  1121. __func__, dsp2cpu_cmd->session_id,
  1122. dsp2cpu_cmd->session_cpu_low,
  1123. dsp2cpu_cmd->session_cpu_high);
  1124. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1125. if (!kmd) {
  1126. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1127. cmd->ret = -1;
  1128. return;
  1129. }
  1130. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1131. dsp2cpu_cmd->session_cpu_high,
  1132. dsp2cpu_cmd->session_cpu_low);
  1133. print_power(&dsp2cpu_cmd->power_req);
  1134. /* EVA_KMD_SET_SYS_PROPERTY
  1135. * Total 14 properties, 8 max once
  1136. * Need to do 2 rounds
  1137. */
  1138. kmd->type = EVA_KMD_SET_SYS_PROPERTY;
  1139. sys_prop = (struct eva_kmd_sys_properties *)&kmd->data.sys_properties;
  1140. sys_prop->prop_num = 7;
  1141. sys_prop->prop_data[0].prop_type = EVA_KMD_PROP_PWR_FDU;
  1142. sys_prop->prop_data[0].data =
  1143. dsp2cpu_cmd->power_req.clock_fdu;
  1144. sys_prop->prop_data[1].prop_type = EVA_KMD_PROP_PWR_ICA;
  1145. sys_prop->prop_data[1].data =
  1146. dsp2cpu_cmd->power_req.clock_ica;
  1147. sys_prop->prop_data[2].prop_type = EVA_KMD_PROP_PWR_OD;
  1148. sys_prop->prop_data[2].data =
  1149. dsp2cpu_cmd->power_req.clock_od;
  1150. sys_prop->prop_data[3].prop_type = EVA_KMD_PROP_PWR_MPU;
  1151. sys_prop->prop_data[3].data =
  1152. dsp2cpu_cmd->power_req.clock_mpu;
  1153. sys_prop->prop_data[4].prop_type = EVA_KMD_PROP_PWR_FW;
  1154. sys_prop->prop_data[4].data =
  1155. dsp2cpu_cmd->power_req.clock_fw;
  1156. sys_prop->prop_data[5].prop_type = EVA_KMD_PROP_PWR_DDR;
  1157. sys_prop->prop_data[5].data =
  1158. dsp2cpu_cmd->power_req.bw_ddr;
  1159. sys_prop->prop_data[6].prop_type = EVA_KMD_PROP_PWR_SYSCACHE;
  1160. sys_prop->prop_data[6].data =
  1161. dsp2cpu_cmd->power_req.bw_sys_cache;
  1162. rc = msm_cvp_handle_syscall(inst, kmd);
  1163. if (rc) {
  1164. dprintk(CVP_ERR, "%s Failed to set sys property\n", __func__);
  1165. cmd->ret = -1;
  1166. goto dsp_fail_power_req;
  1167. }
  1168. dprintk(CVP_DSP, "%s set sys property done part 1\n", __func__);
  1169. /* EVA_KMD_SET_SYS_PROPERTY Round 2 */
  1170. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1171. kmd->type = EVA_KMD_SET_SYS_PROPERTY;
  1172. sys_prop = (struct eva_kmd_sys_properties *)&kmd->data.sys_properties;
  1173. sys_prop->prop_num = 7;
  1174. sys_prop->prop_data[0].prop_type = EVA_KMD_PROP_PWR_FDU_OP;
  1175. sys_prop->prop_data[0].data =
  1176. dsp2cpu_cmd->power_req.op_clock_fdu;
  1177. sys_prop->prop_data[1].prop_type = EVA_KMD_PROP_PWR_ICA_OP;
  1178. sys_prop->prop_data[1].data =
  1179. dsp2cpu_cmd->power_req.op_clock_ica;
  1180. sys_prop->prop_data[2].prop_type = EVA_KMD_PROP_PWR_OD_OP;
  1181. sys_prop->prop_data[2].data =
  1182. dsp2cpu_cmd->power_req.op_clock_od;
  1183. sys_prop->prop_data[3].prop_type = EVA_KMD_PROP_PWR_MPU_OP;
  1184. sys_prop->prop_data[3].data =
  1185. dsp2cpu_cmd->power_req.op_clock_mpu;
  1186. sys_prop->prop_data[4].prop_type = EVA_KMD_PROP_PWR_FW_OP;
  1187. sys_prop->prop_data[4].data =
  1188. dsp2cpu_cmd->power_req.op_clock_fw;
  1189. sys_prop->prop_data[5].prop_type = EVA_KMD_PROP_PWR_DDR_OP;
  1190. sys_prop->prop_data[5].data =
  1191. dsp2cpu_cmd->power_req.op_bw_ddr;
  1192. sys_prop->prop_data[6].prop_type = EVA_KMD_PROP_PWR_SYSCACHE_OP;
  1193. sys_prop->prop_data[6].data =
  1194. dsp2cpu_cmd->power_req.op_bw_sys_cache;
  1195. rc = msm_cvp_handle_syscall(inst, kmd);
  1196. if (rc) {
  1197. dprintk(CVP_ERR, "%s Failed to set sys property\n", __func__);
  1198. cmd->ret = -1;
  1199. goto dsp_fail_power_req;
  1200. }
  1201. dprintk(CVP_DSP, "%s set sys property done part 2\n", __func__);
  1202. memset(kmd, 0, sizeof(struct eva_kmd_arg));
  1203. kmd->type = EVA_KMD_UPDATE_POWER;
  1204. rc = msm_cvp_handle_syscall(inst, kmd);
  1205. if (rc) {
  1206. /* May need to define more error types
  1207. * Check UMD implementation here:
  1208. * https://opengrok.qualcomm.com/source/xref/LA.UM.9.14/vendor/qcom/proprietary/cv-noship/cvp/cpurev/src/cvpcpuRev_skel_imp_cvp2.cpp#380
  1209. */
  1210. dprintk(CVP_ERR, "%s Failed to send update power numbers\n", __func__);
  1211. cmd->ret = -1;
  1212. goto dsp_fail_power_req;
  1213. }
  1214. dprintk(CVP_DSP, "%s DSP2CPU_POWER_REQUEST Done\n", __func__);
  1215. dsp_fail_power_req:
  1216. kfree(kmd);
  1217. }
  1218. static void __dsp_cvp_buf_register(struct cvp_dsp_cmd_msg *cmd)
  1219. {
  1220. struct cvp_dsp_apps *me = &gfa_cv;
  1221. struct msm_cvp_inst *inst;
  1222. struct eva_kmd_arg *kmd;
  1223. struct eva_kmd_buffer *kmd_buf;
  1224. int rc;
  1225. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1226. cmd->ret = 0;
  1227. dprintk(CVP_DSP,
  1228. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1229. __func__, dsp2cpu_cmd->session_id,
  1230. dsp2cpu_cmd->session_cpu_low,
  1231. dsp2cpu_cmd->session_cpu_high,
  1232. dsp2cpu_cmd->pid);
  1233. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1234. if (!kmd) {
  1235. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1236. cmd->ret = -1;
  1237. return;
  1238. }
  1239. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1240. dsp2cpu_cmd->session_cpu_high,
  1241. dsp2cpu_cmd->session_cpu_low);
  1242. kmd->type = EVA_KMD_REGISTER_BUFFER;
  1243. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1244. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1245. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1246. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1247. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1248. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1249. kmd_buf->pixelformat = 0;
  1250. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1251. rc = msm_cvp_register_buffer_dsp(inst, kmd_buf,
  1252. dsp2cpu_cmd->pid, &cmd->sbuf.iova);
  1253. if (rc) {
  1254. dprintk(CVP_ERR, "%s Failed to register buffer\n", __func__);
  1255. cmd->ret = -1;
  1256. goto dsp_fail_buf_reg;
  1257. }
  1258. dprintk(CVP_DSP, "%s register buffer done\n", __func__);
  1259. cmd->sbuf.size = kmd_buf->size;
  1260. cmd->sbuf.fd = kmd_buf->fd;
  1261. cmd->sbuf.index = kmd_buf->index;
  1262. cmd->sbuf.offset = kmd_buf->offset;
  1263. dprintk(CVP_DSP, "%s: fd %d, iova 0x%x\n", __func__,
  1264. cmd->sbuf.fd, cmd->sbuf.iova);
  1265. dsp_fail_buf_reg:
  1266. kfree(kmd);
  1267. }
  1268. static void __dsp_cvp_buf_deregister(struct cvp_dsp_cmd_msg *cmd)
  1269. {
  1270. struct cvp_dsp_apps *me = &gfa_cv;
  1271. struct msm_cvp_inst *inst;
  1272. struct eva_kmd_arg *kmd;
  1273. struct eva_kmd_buffer *kmd_buf;
  1274. int rc;
  1275. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1276. cmd->ret = 0;
  1277. dprintk(CVP_DSP,
  1278. "%s : sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1279. __func__, dsp2cpu_cmd->session_id,
  1280. dsp2cpu_cmd->session_cpu_low,
  1281. dsp2cpu_cmd->session_cpu_high,
  1282. dsp2cpu_cmd->pid);
  1283. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1284. if (!kmd) {
  1285. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1286. cmd->ret = -1;
  1287. return;
  1288. }
  1289. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1290. dsp2cpu_cmd->session_cpu_high,
  1291. dsp2cpu_cmd->session_cpu_low);
  1292. kmd->type = EVA_KMD_UNREGISTER_BUFFER;
  1293. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1294. kmd_buf->type = EVA_KMD_UNREGISTER_BUFFER;
  1295. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1296. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1297. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1298. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1299. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1300. kmd_buf->pixelformat = 0;
  1301. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1302. rc = msm_cvp_unregister_buffer_dsp(inst, kmd_buf);
  1303. if (rc) {
  1304. dprintk(CVP_ERR, "%s Failed to deregister buffer\n", __func__);
  1305. cmd->ret = -1;
  1306. goto fail_dsp_buf_dereg;
  1307. }
  1308. dprintk(CVP_DSP, "%s deregister buffer done\n", __func__);
  1309. fail_dsp_buf_dereg:
  1310. kfree(kmd);
  1311. }
  1312. static void __dsp_cvp_mem_alloc(struct cvp_dsp_cmd_msg *cmd)
  1313. {
  1314. struct cvp_dsp_apps *me = &gfa_cv;
  1315. struct msm_cvp_inst *inst;
  1316. int rc;
  1317. struct cvp_internal_buf *buf = NULL;
  1318. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1319. uint64_t v_dsp_addr = 0;
  1320. struct fastrpc_device *frpc_device = NULL;
  1321. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1322. cmd->ret = 0;
  1323. dprintk(CVP_DSP,
  1324. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1325. __func__, dsp2cpu_cmd->session_id,
  1326. dsp2cpu_cmd->session_cpu_low,
  1327. dsp2cpu_cmd->session_cpu_high,
  1328. dsp2cpu_cmd->pid);
  1329. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1330. if (!frpc_node) {
  1331. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1332. __func__, dsp2cpu_cmd->pid);
  1333. goto fail_fastrpc_node;
  1334. }
  1335. frpc_device = frpc_node->cvp_fastrpc_device;
  1336. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1337. dsp2cpu_cmd->session_cpu_high,
  1338. dsp2cpu_cmd->session_cpu_low);
  1339. buf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  1340. if (!buf)
  1341. goto fail_kzalloc_buf;
  1342. rc = cvp_allocate_dsp_bufs(inst, buf,
  1343. dsp2cpu_cmd->sbuf.size,
  1344. dsp2cpu_cmd->sbuf.type);
  1345. if (rc)
  1346. goto fail_allocate_dsp_buf;
  1347. rc = eva_fastrpc_dev_map_dma(frpc_device, buf,
  1348. dsp2cpu_cmd->sbuf.dsp_remote_map,
  1349. &v_dsp_addr);
  1350. if (rc) {
  1351. dprintk(CVP_ERR, "%s Failed to map buffer 0x%x\n", __func__,
  1352. rc);
  1353. goto fail_fastrpc_dev_map_dma;
  1354. }
  1355. mutex_lock(&inst->cvpdspbufs.lock);
  1356. list_add_tail(&buf->list, &inst->cvpdspbufs.list);
  1357. mutex_unlock(&inst->cvpdspbufs.lock);
  1358. dprintk(CVP_DSP, "%s allocate buffer done, addr 0x%llx\n",
  1359. __func__, v_dsp_addr);
  1360. cmd->sbuf.size = buf->smem->size;
  1361. cmd->sbuf.fd = buf->fd;
  1362. cmd->sbuf.offset = 0;
  1363. cmd->sbuf.iova = buf->smem->device_addr;
  1364. cmd->sbuf.v_dsp_addr = v_dsp_addr;
  1365. dprintk(CVP_DSP, "%s: size %d, iova 0x%x, v_dsp_addr 0x%llx\n",
  1366. __func__, cmd->sbuf.size, cmd->sbuf.iova,
  1367. cmd->sbuf.v_dsp_addr);
  1368. return;
  1369. fail_fastrpc_dev_map_dma:
  1370. cvp_release_dsp_buffers(inst, buf);
  1371. fail_allocate_dsp_buf:
  1372. kmem_cache_free(cvp_driver->buf_cache, buf);
  1373. fail_kzalloc_buf:
  1374. fail_fastrpc_node:
  1375. cmd->ret = -1;
  1376. return;
  1377. }
  1378. static void __dsp_cvp_mem_free(struct cvp_dsp_cmd_msg *cmd)
  1379. {
  1380. struct cvp_dsp_apps *me = &gfa_cv;
  1381. struct msm_cvp_inst *inst;
  1382. int rc;
  1383. struct cvp_internal_buf *buf = NULL;
  1384. struct list_head *ptr = NULL, *next = NULL;
  1385. struct msm_cvp_list *buf_list = NULL;
  1386. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1387. struct fastrpc_device *frpc_device = NULL;
  1388. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1389. cmd->ret = 0;
  1390. dprintk(CVP_DSP,
  1391. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1392. __func__, dsp2cpu_cmd->session_id,
  1393. dsp2cpu_cmd->session_cpu_low,
  1394. dsp2cpu_cmd->session_cpu_high,
  1395. dsp2cpu_cmd->pid);
  1396. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1397. dsp2cpu_cmd->session_cpu_high,
  1398. dsp2cpu_cmd->session_cpu_low);
  1399. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1400. if (!frpc_node) {
  1401. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1402. __func__, dsp2cpu_cmd->pid);
  1403. cmd->ret = -1;
  1404. return;
  1405. }
  1406. frpc_device = frpc_node->cvp_fastrpc_device;
  1407. buf_list = &inst->cvpdspbufs;
  1408. mutex_lock(&buf_list->lock);
  1409. list_for_each_safe(ptr, next, &buf_list->list) {
  1410. buf = list_entry(ptr, struct cvp_internal_buf, list);
  1411. dprintk(CVP_DSP, "fd in list 0x%x, fd from dsp 0x%x\n",
  1412. buf->fd, dsp2cpu_cmd->sbuf.fd);
  1413. if (!buf->smem) {
  1414. dprintk(CVP_DSP, "Empyt smem\n");
  1415. continue;
  1416. }
  1417. /* Verify with device addr */
  1418. if (buf->smem->device_addr == dsp2cpu_cmd->sbuf.iova) {
  1419. dprintk(CVP_DSP, "%s find device addr 0x%x\n",
  1420. __func__, buf->smem->device_addr);
  1421. rc = eva_fastrpc_dev_unmap_dma(frpc_device, buf);
  1422. if (rc) {
  1423. dprintk(CVP_ERR,
  1424. "%s Failed to unmap buffer 0x%x\n",
  1425. __func__, rc);
  1426. cmd->ret = -1;
  1427. goto fail_fastrpc_dev_unmap_dma;
  1428. }
  1429. rc = cvp_release_dsp_buffers(inst, buf);
  1430. if (rc) {
  1431. dprintk(CVP_ERR,
  1432. "%s Failed to free buffer 0x%x\n",
  1433. __func__, rc);
  1434. cmd->ret = -1;
  1435. goto fail_release_buf;
  1436. }
  1437. list_del(&buf->list);
  1438. kmem_cache_free(cvp_driver->buf_cache, buf);
  1439. break;
  1440. }
  1441. }
  1442. fail_release_buf:
  1443. fail_fastrpc_dev_unmap_dma:
  1444. mutex_unlock(&buf_list->lock);
  1445. }
  1446. static int cvp_dsp_thread(void *data)
  1447. {
  1448. int rc = 0, old_state;
  1449. struct cvp_dsp_apps *me = &gfa_cv;
  1450. struct cvp_dsp_cmd_msg cmd;
  1451. struct cvp_hfi_device *hdev;
  1452. struct msm_cvp_core *core;
  1453. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  1454. if (!core) {
  1455. dprintk(CVP_ERR, "%s: Failed to find core\n", __func__);
  1456. rc = -EINVAL;
  1457. goto exit;
  1458. }
  1459. hdev = (struct cvp_hfi_device *)core->device;
  1460. if (!hdev) {
  1461. dprintk(CVP_ERR, "%s Invalid device handle\n", __func__);
  1462. rc = -EINVAL;
  1463. goto exit;
  1464. }
  1465. wait_dsp:
  1466. rc = wait_for_completion_interruptible(
  1467. &me->completions[CPU2DSP_MAX_CMD]);
  1468. if (me->state == DSP_INVALID)
  1469. goto exit;
  1470. if (me->state == DSP_UNINIT)
  1471. goto wait_dsp;
  1472. if (me->state == DSP_PROBED) {
  1473. cvp_dsp_send_hfi_queue();
  1474. goto wait_dsp;
  1475. }
  1476. cmd.type = me->pending_dsp2cpu_cmd.type;
  1477. if (rc == -ERESTARTSYS) {
  1478. dprintk(CVP_WARN, "%s received interrupt signal\n", __func__);
  1479. } else {
  1480. mutex_lock(&me->lock);
  1481. switch (me->pending_dsp2cpu_cmd.type) {
  1482. case DSP2CPU_POWERON:
  1483. {
  1484. if (me->state == DSP_READY) {
  1485. cmd.ret = 0;
  1486. break;
  1487. }
  1488. mutex_unlock(&me->lock);
  1489. old_state = me->state;
  1490. me->state = DSP_READY;
  1491. rc = call_hfi_op(hdev, resume, hdev->hfi_device_data);
  1492. if (rc) {
  1493. dprintk(CVP_WARN, "%s Failed to resume cvp\n",
  1494. __func__);
  1495. mutex_lock(&me->lock);
  1496. me->state = old_state;
  1497. cmd.ret = 1;
  1498. break;
  1499. }
  1500. mutex_lock(&me->lock);
  1501. cmd.ret = 0;
  1502. break;
  1503. }
  1504. case DSP2CPU_POWEROFF:
  1505. {
  1506. me->state = DSP_SUSPEND;
  1507. cmd.ret = 0;
  1508. break;
  1509. }
  1510. case DSP2CPU_CREATE_SESSION:
  1511. {
  1512. __dsp_cvp_sess_create(&cmd);
  1513. break;
  1514. }
  1515. case DSP2CPU_DETELE_SESSION:
  1516. {
  1517. __dsp_cvp_sess_delete(&cmd);
  1518. break;
  1519. }
  1520. case DSP2CPU_POWER_REQUEST:
  1521. {
  1522. __dsp_cvp_power_req(&cmd);
  1523. break;
  1524. }
  1525. case DSP2CPU_REGISTER_BUFFER:
  1526. {
  1527. __dsp_cvp_buf_register(&cmd);
  1528. break;
  1529. }
  1530. case DSP2CPU_DEREGISTER_BUFFER:
  1531. {
  1532. __dsp_cvp_buf_deregister(&cmd);
  1533. break;
  1534. }
  1535. case DSP2CPU_MEM_ALLOC:
  1536. {
  1537. __dsp_cvp_mem_alloc(&cmd);
  1538. break;
  1539. }
  1540. case DSP2CPU_MEM_FREE:
  1541. {
  1542. __dsp_cvp_mem_free(&cmd);
  1543. break;
  1544. }
  1545. default:
  1546. dprintk(CVP_ERR, "unrecognaized dsp cmds: %d\n",
  1547. me->pending_dsp2cpu_cmd.type);
  1548. break;
  1549. }
  1550. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1551. mutex_unlock(&me->lock);
  1552. }
  1553. /* Responds to DSP */
  1554. rc = cvp_dsp_send_cmd(&cmd, sizeof(struct cvp_dsp_cmd_msg));
  1555. if (rc)
  1556. dprintk(CVP_ERR,
  1557. "%s: cvp_dsp_send_cmd failed rc = %d cmd type=%d\n",
  1558. __func__, rc, cmd.type);
  1559. goto wait_dsp;
  1560. exit:
  1561. dprintk(CVP_DBG, "dsp thread exit\n");
  1562. do_exit(rc);
  1563. }
  1564. int cvp_dsp_device_init(void)
  1565. {
  1566. struct cvp_dsp_apps *me = &gfa_cv;
  1567. char tname[16];
  1568. int rc;
  1569. int i;
  1570. mutex_init(&me->lock);
  1571. me->state = DSP_INVALID;
  1572. me->hyp_assigned = false;
  1573. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1574. init_completion(&me->completions[i]);
  1575. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1576. me->pending_dsp2cpu_rsp.type = CVP_INVALID_RPMSG_TYPE;
  1577. INIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1578. rc = register_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1579. if (rc) {
  1580. dprintk(CVP_ERR,
  1581. "%s : register_rpmsg_driver failed rc = %d\n",
  1582. __func__, rc);
  1583. goto register_bail;
  1584. }
  1585. snprintf(tname, sizeof(tname), "cvp-dsp-thread");
  1586. me->state = DSP_UNINIT;
  1587. me->dsp_thread = kthread_run(cvp_dsp_thread, me, tname);
  1588. if (!me->dsp_thread) {
  1589. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  1590. rc = -ECHILD;
  1591. me->state = DSP_INVALID;
  1592. goto register_bail;
  1593. }
  1594. return 0;
  1595. register_bail:
  1596. return rc;
  1597. }
  1598. void cvp_dsp_device_exit(void)
  1599. {
  1600. struct cvp_dsp_apps *me = &gfa_cv;
  1601. int i;
  1602. mutex_lock(&me->lock);
  1603. me->state = DSP_INVALID;
  1604. mutex_unlock(&me->lock);
  1605. DEINIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1606. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1607. complete_all(&me->completions[i]);
  1608. mutex_destroy(&me->lock);
  1609. unregister_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1610. }