msm_cvp_dsp.c 49 KB

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