msm_cvp_dsp.c 49 KB

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