msm_cvp_dsp.c 57 KB

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