msm_cvp_dsp.c 48 KB

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