msm_cvp_dsp.c 52 KB

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