msm_cvp_dsp.c 47 KB

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