msm_cvp_dsp.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  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->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, dsp_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 void __dsp_cvp_sess_create(struct cvp_dsp_cmd_msg *cmd)
  907. {
  908. struct cvp_dsp_apps *me = &gfa_cv;
  909. struct msm_cvp_inst *inst = NULL;
  910. uint64_t inst_handle = 0;
  911. int rc = 0;
  912. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  913. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  914. struct pid *pid_s = NULL;
  915. struct task_struct *task = NULL;
  916. cmd->ret = 0;
  917. dprintk(CVP_DSP,
  918. "%s sess Type %d Mask %d Prio %d Sec %d pid 0x%x\n",
  919. __func__, dsp2cpu_cmd->session_type,
  920. dsp2cpu_cmd->kernel_mask,
  921. dsp2cpu_cmd->session_prio,
  922. dsp2cpu_cmd->is_secure,
  923. dsp2cpu_cmd->pid);
  924. rc = eva_fastrpc_driver_register(dsp2cpu_cmd->pid);
  925. if (rc) {
  926. dprintk(CVP_ERR, "%s Register fastrpc driver fail\n", __func__);
  927. cmd->ret = -1;
  928. return;
  929. }
  930. inst = msm_cvp_open(MSM_CORE_CVP, MSM_CVP_DSP);
  931. if (!inst) {
  932. dprintk(CVP_ERR, "%s Failed create instance\n", __func__);
  933. goto fail_msm_cvp_open;
  934. }
  935. inst->process_id = dsp2cpu_cmd->pid;
  936. inst->prop.kernel_mask = dsp2cpu_cmd->kernel_mask;
  937. inst->prop.type = dsp2cpu_cmd->session_type;
  938. inst->prop.priority = dsp2cpu_cmd->session_prio;
  939. inst->prop.is_secure = dsp2cpu_cmd->is_secure;
  940. inst->prop.dsp_mask = dsp2cpu_cmd->dsp_access_mask;
  941. rc = msm_cvp_session_create(inst);
  942. if (rc) {
  943. dprintk(CVP_ERR, "Warning: send Session Create failed\n");
  944. goto fail_session_create;
  945. } else {
  946. dprintk(CVP_DSP, "%s DSP Session Create done\n", __func__);
  947. }
  948. /* Get session id */
  949. rc = msm_cvp_get_session_info(inst, &cmd->session_id);
  950. if (rc) {
  951. dprintk(CVP_ERR, "Warning: get session index failed %d\n", rc);
  952. goto fail_get_session_info;
  953. }
  954. inst_handle = (uint64_t)inst;
  955. cmd->session_cpu_high = (uint32_t)((inst_handle & HIGH32) >> 32);
  956. cmd->session_cpu_low = (uint32_t)(inst_handle & LOW32);
  957. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  958. if (frpc_node)
  959. eva_fastrpc_driver_add_sess(frpc_node, inst);
  960. pid_s = find_get_pid(inst->process_id);
  961. if (pid_s == NULL) {
  962. dprintk(CVP_WARN, "%s incorrect pid\n", __func__);
  963. goto fail_get_pid;
  964. }
  965. dprintk(CVP_DSP, "%s get pid_s 0x%x from pidA 0x%x\n", __func__,
  966. pid_s, inst->process_id);
  967. task = get_pid_task(pid_s, PIDTYPE_TGID);
  968. if (!task) {
  969. dprintk(CVP_WARN, "%s task doesn't exist\n", __func__);
  970. goto fail_get_task;
  971. }
  972. inst->task = task;
  973. dprintk(CVP_DSP,
  974. "%s CREATE_SESS id 0x%x, cpu_low 0x%x, cpu_high 0x%x\n",
  975. __func__, cmd->session_id, cmd->session_cpu_low,
  976. cmd->session_cpu_high);
  977. return;
  978. fail_get_pid:
  979. fail_get_task:
  980. fail_get_session_info:
  981. fail_session_create:
  982. msm_cvp_close(inst);
  983. fail_msm_cvp_open:
  984. /* unregister fastrpc driver */
  985. eva_fastrpc_driver_unregister(dsp2cpu_cmd->pid, false);
  986. cmd->ret = -1;
  987. }
  988. static void __dsp_cvp_sess_delete(struct cvp_dsp_cmd_msg *cmd)
  989. {
  990. struct cvp_dsp_apps *me = &gfa_cv;
  991. struct msm_cvp_inst *inst;
  992. int rc;
  993. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  994. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  995. cmd->ret = 0;
  996. dprintk(CVP_DSP,
  997. "%s sess id 0x%x low 0x%x high 0x%x, pid 0x%x\n",
  998. __func__, dsp2cpu_cmd->session_id,
  999. dsp2cpu_cmd->session_cpu_low,
  1000. dsp2cpu_cmd->session_cpu_high,
  1001. dsp2cpu_cmd->pid);
  1002. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1003. if (!frpc_node) {
  1004. dprintk(CVP_ERR, "%s pid 0x%x not registered with fastrpc\n",
  1005. __func__, dsp2cpu_cmd->pid);
  1006. cmd->ret = -1;
  1007. return;
  1008. }
  1009. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1010. dsp2cpu_cmd->session_cpu_high,
  1011. dsp2cpu_cmd->session_cpu_low);
  1012. rc = msm_cvp_session_delete(inst);
  1013. if (rc) {
  1014. dprintk(CVP_ERR, "Warning: send Delete Session failed\n");
  1015. cmd->ret = -1;
  1016. goto dsp_fail_delete;
  1017. }
  1018. rc = msm_cvp_close(inst);
  1019. if (rc) {
  1020. dprintk(CVP_ERR, "Warning: Failed to close cvp instance\n");
  1021. cmd->ret = -1;
  1022. goto dsp_fail_delete;
  1023. }
  1024. /* unregister fastrpc driver */
  1025. eva_fastrpc_driver_unregister(dsp2cpu_cmd->pid, false);
  1026. if (inst->task)
  1027. put_task_struct(inst->task);
  1028. dprintk(CVP_DSP, "%s DSP2CPU_DETELE_SESSION Done\n", __func__);
  1029. dsp_fail_delete:
  1030. return;
  1031. }
  1032. static void __dsp_cvp_power_req(struct cvp_dsp_cmd_msg *cmd)
  1033. {
  1034. struct cvp_dsp_apps *me = &gfa_cv;
  1035. struct msm_cvp_inst *inst;
  1036. int rc;
  1037. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1038. cmd->ret = 0;
  1039. dprintk(CVP_DSP,
  1040. "%s sess id 0x%x, low 0x%x, high 0x%x\n",
  1041. __func__, dsp2cpu_cmd->session_id,
  1042. dsp2cpu_cmd->session_cpu_low,
  1043. dsp2cpu_cmd->session_cpu_high);
  1044. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1045. dsp2cpu_cmd->session_cpu_high,
  1046. dsp2cpu_cmd->session_cpu_low);
  1047. if (!inst) {
  1048. cmd->ret = -1;
  1049. goto dsp_fail_power_req;
  1050. }
  1051. print_power(&dsp2cpu_cmd->power_req);
  1052. inst->prop.fdu_cycles = dsp2cpu_cmd->power_req.clock_fdu;
  1053. inst->prop.ica_cycles = dsp2cpu_cmd->power_req.clock_ica;
  1054. inst->prop.od_cycles = dsp2cpu_cmd->power_req.clock_od;
  1055. inst->prop.mpu_cycles = dsp2cpu_cmd->power_req.clock_mpu;
  1056. inst->prop.fw_cycles = dsp2cpu_cmd->power_req.clock_fw;
  1057. inst->prop.ddr_bw = dsp2cpu_cmd->power_req.bw_ddr;
  1058. inst->prop.ddr_cache = dsp2cpu_cmd->power_req.bw_sys_cache;
  1059. inst->prop.fdu_op_cycles = dsp2cpu_cmd->power_req.op_clock_fdu;
  1060. inst->prop.ica_op_cycles = dsp2cpu_cmd->power_req.op_clock_ica;
  1061. inst->prop.od_op_cycles = dsp2cpu_cmd->power_req.op_clock_od;
  1062. inst->prop.mpu_op_cycles = dsp2cpu_cmd->power_req.op_clock_mpu;
  1063. inst->prop.fw_op_cycles = dsp2cpu_cmd->power_req.op_clock_fw;
  1064. inst->prop.ddr_op_bw = dsp2cpu_cmd->power_req.op_bw_ddr;
  1065. inst->prop.ddr_op_cache = dsp2cpu_cmd->power_req.op_bw_sys_cache;
  1066. rc = msm_cvp_update_power(inst);
  1067. if (rc) {
  1068. /*
  1069. *May need to define more error types
  1070. * Check UMD implementation
  1071. */
  1072. dprintk(CVP_ERR, "%s Failed update power\n", __func__);
  1073. cmd->ret = -1;
  1074. goto dsp_fail_power_req;
  1075. }
  1076. dprintk(CVP_DSP, "%s DSP2CPU_POWER_REQUEST Done\n", __func__);
  1077. dsp_fail_power_req:
  1078. return;
  1079. }
  1080. static void __dsp_cvp_buf_register(struct cvp_dsp_cmd_msg *cmd)
  1081. {
  1082. struct cvp_dsp_apps *me = &gfa_cv;
  1083. struct msm_cvp_inst *inst;
  1084. struct eva_kmd_arg *kmd;
  1085. struct eva_kmd_buffer *kmd_buf;
  1086. int rc;
  1087. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1088. cmd->ret = 0;
  1089. dprintk(CVP_DSP,
  1090. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1091. __func__, dsp2cpu_cmd->session_id,
  1092. dsp2cpu_cmd->session_cpu_low,
  1093. dsp2cpu_cmd->session_cpu_high,
  1094. dsp2cpu_cmd->pid);
  1095. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1096. if (!kmd) {
  1097. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1098. cmd->ret = -1;
  1099. return;
  1100. }
  1101. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1102. dsp2cpu_cmd->session_cpu_high,
  1103. dsp2cpu_cmd->session_cpu_low);
  1104. kmd->type = EVA_KMD_REGISTER_BUFFER;
  1105. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1106. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1107. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1108. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1109. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1110. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1111. kmd_buf->pixelformat = 0;
  1112. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1113. rc = msm_cvp_register_buffer(inst, kmd_buf);
  1114. if (rc) {
  1115. dprintk(CVP_ERR, "%s Failed to register buffer\n", __func__);
  1116. cmd->ret = -1;
  1117. goto dsp_fail_buf_reg;
  1118. }
  1119. dprintk(CVP_DSP, "%s register buffer done\n", __func__);
  1120. cmd->sbuf.iova = kmd_buf->reserved[0];
  1121. cmd->sbuf.size = kmd_buf->size;
  1122. cmd->sbuf.fd = kmd_buf->fd;
  1123. cmd->sbuf.index = kmd_buf->index;
  1124. cmd->sbuf.offset = kmd_buf->offset;
  1125. dprintk(CVP_DSP, "%s: fd %d, iova 0x%x\n", __func__,
  1126. cmd->sbuf.fd, cmd->sbuf.iova);
  1127. dsp_fail_buf_reg:
  1128. kfree(kmd);
  1129. }
  1130. static void __dsp_cvp_buf_deregister(struct cvp_dsp_cmd_msg *cmd)
  1131. {
  1132. struct cvp_dsp_apps *me = &gfa_cv;
  1133. struct msm_cvp_inst *inst;
  1134. struct eva_kmd_arg *kmd;
  1135. struct eva_kmd_buffer *kmd_buf;
  1136. int rc;
  1137. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1138. cmd->ret = 0;
  1139. dprintk(CVP_DSP,
  1140. "%s : sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1141. __func__, dsp2cpu_cmd->session_id,
  1142. dsp2cpu_cmd->session_cpu_low,
  1143. dsp2cpu_cmd->session_cpu_high,
  1144. dsp2cpu_cmd->pid);
  1145. kmd = kzalloc(sizeof(*kmd), GFP_KERNEL);
  1146. if (!kmd) {
  1147. dprintk(CVP_ERR, "%s kzalloc failure\n", __func__);
  1148. cmd->ret = -1;
  1149. return;
  1150. }
  1151. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1152. dsp2cpu_cmd->session_cpu_high,
  1153. dsp2cpu_cmd->session_cpu_low);
  1154. kmd->type = EVA_KMD_UNREGISTER_BUFFER;
  1155. kmd_buf = (struct eva_kmd_buffer *)&(kmd->data.regbuf);
  1156. kmd_buf->type = EVA_KMD_UNREGISTER_BUFFER;
  1157. kmd_buf->type = EVA_KMD_BUFTYPE_INPUT;
  1158. kmd_buf->index = dsp2cpu_cmd->sbuf.index;
  1159. kmd_buf->fd = dsp2cpu_cmd->sbuf.fd;
  1160. kmd_buf->size = dsp2cpu_cmd->sbuf.size;
  1161. kmd_buf->offset = dsp2cpu_cmd->sbuf.offset;
  1162. kmd_buf->pixelformat = 0;
  1163. kmd_buf->flags = EVA_KMD_FLAG_UNSECURE;
  1164. rc = msm_cvp_unregister_buffer(inst, kmd_buf);
  1165. if (rc) {
  1166. dprintk(CVP_ERR, "%s Failed to deregister buffer\n", __func__);
  1167. cmd->ret = -1;
  1168. goto fail_dsp_buf_dereg;
  1169. }
  1170. dprintk(CVP_DSP, "%s deregister buffer done\n", __func__);
  1171. fail_dsp_buf_dereg:
  1172. kfree(kmd);
  1173. }
  1174. static void __dsp_cvp_mem_alloc(struct cvp_dsp_cmd_msg *cmd)
  1175. {
  1176. struct cvp_dsp_apps *me = &gfa_cv;
  1177. struct msm_cvp_inst *inst;
  1178. int rc;
  1179. struct cvp_internal_buf *buf = NULL;
  1180. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1181. uint64_t v_dsp_addr = 0;
  1182. struct fastrpc_device *frpc_device = NULL;
  1183. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1184. cmd->ret = 0;
  1185. dprintk(CVP_DSP,
  1186. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1187. __func__, dsp2cpu_cmd->session_id,
  1188. dsp2cpu_cmd->session_cpu_low,
  1189. dsp2cpu_cmd->session_cpu_high,
  1190. dsp2cpu_cmd->pid);
  1191. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1192. if (!frpc_node) {
  1193. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1194. __func__, dsp2cpu_cmd->pid);
  1195. goto fail_fastrpc_node;
  1196. }
  1197. frpc_device = frpc_node->cvp_fastrpc_device;
  1198. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1199. dsp2cpu_cmd->session_cpu_high,
  1200. dsp2cpu_cmd->session_cpu_low);
  1201. buf = kmem_cache_zalloc(cvp_driver->buf_cache, GFP_KERNEL);
  1202. if (!buf)
  1203. goto fail_kzalloc_buf;
  1204. rc = cvp_allocate_dsp_bufs(inst, buf,
  1205. dsp2cpu_cmd->sbuf.size,
  1206. dsp2cpu_cmd->sbuf.type);
  1207. if (rc)
  1208. goto fail_allocate_dsp_buf;
  1209. rc = eva_fastrpc_dev_map_dma(frpc_device, buf,
  1210. dsp2cpu_cmd->sbuf.dsp_remote_map,
  1211. &v_dsp_addr);
  1212. if (rc) {
  1213. dprintk(CVP_ERR, "%s Failed to map buffer 0x%x\n", __func__,
  1214. rc);
  1215. goto fail_fastrpc_dev_map_dma;
  1216. }
  1217. mutex_lock(&inst->cvpdspbufs.lock);
  1218. list_add_tail(&buf->list, &inst->cvpdspbufs.list);
  1219. mutex_unlock(&inst->cvpdspbufs.lock);
  1220. dprintk(CVP_DSP, "%s allocate buffer done, addr 0x%llx\n",
  1221. __func__, v_dsp_addr);
  1222. cmd->sbuf.size = buf->smem->size;
  1223. cmd->sbuf.fd = buf->fd;
  1224. cmd->sbuf.offset = 0;
  1225. cmd->sbuf.iova = buf->smem->device_addr;
  1226. cmd->sbuf.v_dsp_addr = v_dsp_addr;
  1227. dprintk(CVP_DSP, "%s: size %d, iova 0x%x, v_dsp_addr 0x%llx\n",
  1228. __func__, cmd->sbuf.size, cmd->sbuf.iova,
  1229. cmd->sbuf.v_dsp_addr);
  1230. return;
  1231. fail_fastrpc_dev_map_dma:
  1232. cvp_release_dsp_buffers(inst, buf);
  1233. fail_allocate_dsp_buf:
  1234. kmem_cache_free(cvp_driver->buf_cache, buf);
  1235. fail_kzalloc_buf:
  1236. fail_fastrpc_node:
  1237. cmd->ret = -1;
  1238. return;
  1239. }
  1240. static void __dsp_cvp_mem_free(struct cvp_dsp_cmd_msg *cmd)
  1241. {
  1242. struct cvp_dsp_apps *me = &gfa_cv;
  1243. struct msm_cvp_inst *inst;
  1244. int rc;
  1245. struct cvp_internal_buf *buf = NULL;
  1246. struct list_head *ptr = NULL, *next = NULL;
  1247. struct msm_cvp_list *buf_list = NULL;
  1248. struct cvp_dsp2cpu_cmd_msg *dsp2cpu_cmd = &me->pending_dsp2cpu_cmd;
  1249. struct fastrpc_device *frpc_device = NULL;
  1250. struct cvp_dsp_fastrpc_driver_entry *frpc_node = NULL;
  1251. cmd->ret = 0;
  1252. dprintk(CVP_DSP,
  1253. "%s sess id 0x%x, low 0x%x, high 0x%x, pid 0x%x\n",
  1254. __func__, dsp2cpu_cmd->session_id,
  1255. dsp2cpu_cmd->session_cpu_low,
  1256. dsp2cpu_cmd->session_cpu_high,
  1257. dsp2cpu_cmd->pid);
  1258. inst = (struct msm_cvp_inst *)ptr_dsp2cpu(
  1259. dsp2cpu_cmd->session_cpu_high,
  1260. dsp2cpu_cmd->session_cpu_low);
  1261. if (!inst) {
  1262. dprintk(CVP_ERR, "%s Failed to get inst\n",
  1263. __func__);
  1264. cmd->ret = -1;
  1265. return;
  1266. }
  1267. frpc_node = cvp_find_fastrpc_node_with_handle(dsp2cpu_cmd->pid);
  1268. if (!frpc_node) {
  1269. dprintk(CVP_ERR, "%s Failed to find fastrpc node 0x%x\n",
  1270. __func__, dsp2cpu_cmd->pid);
  1271. cmd->ret = -1;
  1272. return;
  1273. }
  1274. frpc_device = frpc_node->cvp_fastrpc_device;
  1275. buf_list = &inst->cvpdspbufs;
  1276. mutex_lock(&buf_list->lock);
  1277. list_for_each_safe(ptr, next, &buf_list->list) {
  1278. buf = list_entry(ptr, struct cvp_internal_buf, list);
  1279. dprintk(CVP_DSP, "fd in list 0x%x, fd from dsp 0x%x\n",
  1280. buf->fd, dsp2cpu_cmd->sbuf.fd);
  1281. if (!buf->smem) {
  1282. dprintk(CVP_DSP, "Empyt smem\n");
  1283. continue;
  1284. }
  1285. /* Verify with device addr */
  1286. if (buf->smem->device_addr == dsp2cpu_cmd->sbuf.iova) {
  1287. dprintk(CVP_DSP, "%s find device addr 0x%x\n",
  1288. __func__, buf->smem->device_addr);
  1289. rc = eva_fastrpc_dev_unmap_dma(frpc_device, buf);
  1290. if (rc) {
  1291. dprintk(CVP_ERR,
  1292. "%s Failed to unmap buffer 0x%x\n",
  1293. __func__, rc);
  1294. cmd->ret = -1;
  1295. goto fail_fastrpc_dev_unmap_dma;
  1296. }
  1297. rc = cvp_release_dsp_buffers(inst, buf);
  1298. if (rc) {
  1299. dprintk(CVP_ERR,
  1300. "%s Failed to free buffer 0x%x\n",
  1301. __func__, rc);
  1302. cmd->ret = -1;
  1303. goto fail_release_buf;
  1304. }
  1305. list_del(&buf->list);
  1306. kmem_cache_free(cvp_driver->buf_cache, buf);
  1307. break;
  1308. }
  1309. }
  1310. fail_release_buf:
  1311. fail_fastrpc_dev_unmap_dma:
  1312. mutex_unlock(&buf_list->lock);
  1313. }
  1314. static int cvp_dsp_thread(void *data)
  1315. {
  1316. int rc = 0, old_state;
  1317. struct cvp_dsp_apps *me = &gfa_cv;
  1318. struct cvp_dsp_cmd_msg cmd;
  1319. struct cvp_hfi_device *hdev;
  1320. struct msm_cvp_core *core;
  1321. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  1322. if (!core) {
  1323. dprintk(CVP_ERR, "%s: Failed to find core\n", __func__);
  1324. rc = -EINVAL;
  1325. goto exit;
  1326. }
  1327. hdev = (struct cvp_hfi_device *)core->device;
  1328. if (!hdev) {
  1329. dprintk(CVP_ERR, "%s Invalid device handle\n", __func__);
  1330. rc = -EINVAL;
  1331. goto exit;
  1332. }
  1333. wait_dsp:
  1334. rc = wait_for_completion_interruptible(
  1335. &me->completions[CPU2DSP_MAX_CMD]);
  1336. if (me->state == DSP_INVALID)
  1337. goto exit;
  1338. if (me->state == DSP_UNINIT)
  1339. goto wait_dsp;
  1340. if (me->state == DSP_PROBED) {
  1341. cvp_dsp_send_hfi_queue();
  1342. goto wait_dsp;
  1343. }
  1344. cmd.type = me->pending_dsp2cpu_cmd.type;
  1345. if (rc == -ERESTARTSYS) {
  1346. dprintk(CVP_WARN, "%s received interrupt signal\n", __func__);
  1347. } else {
  1348. mutex_lock(&me->lock);
  1349. switch (me->pending_dsp2cpu_cmd.type) {
  1350. case DSP2CPU_POWERON:
  1351. {
  1352. if (me->state == DSP_READY) {
  1353. cmd.ret = 0;
  1354. break;
  1355. }
  1356. mutex_unlock(&me->lock);
  1357. old_state = me->state;
  1358. me->state = DSP_READY;
  1359. rc = call_hfi_op(hdev, resume, hdev->hfi_device_data);
  1360. if (rc) {
  1361. dprintk(CVP_WARN, "%s Failed to resume cvp\n",
  1362. __func__);
  1363. mutex_lock(&me->lock);
  1364. me->state = old_state;
  1365. cmd.ret = 1;
  1366. break;
  1367. }
  1368. mutex_lock(&me->lock);
  1369. cmd.ret = 0;
  1370. break;
  1371. }
  1372. case DSP2CPU_POWEROFF:
  1373. {
  1374. me->state = DSP_SUSPEND;
  1375. cmd.ret = 0;
  1376. break;
  1377. }
  1378. case DSP2CPU_CREATE_SESSION:
  1379. {
  1380. __dsp_cvp_sess_create(&cmd);
  1381. break;
  1382. }
  1383. case DSP2CPU_DETELE_SESSION:
  1384. {
  1385. __dsp_cvp_sess_delete(&cmd);
  1386. break;
  1387. }
  1388. case DSP2CPU_POWER_REQUEST:
  1389. {
  1390. __dsp_cvp_power_req(&cmd);
  1391. break;
  1392. }
  1393. case DSP2CPU_REGISTER_BUFFER:
  1394. {
  1395. __dsp_cvp_buf_register(&cmd);
  1396. break;
  1397. }
  1398. case DSP2CPU_DEREGISTER_BUFFER:
  1399. {
  1400. __dsp_cvp_buf_deregister(&cmd);
  1401. break;
  1402. }
  1403. case DSP2CPU_MEM_ALLOC:
  1404. {
  1405. __dsp_cvp_mem_alloc(&cmd);
  1406. break;
  1407. }
  1408. case DSP2CPU_MEM_FREE:
  1409. {
  1410. __dsp_cvp_mem_free(&cmd);
  1411. break;
  1412. }
  1413. default:
  1414. dprintk(CVP_ERR, "unrecognaized dsp cmds: %d\n",
  1415. me->pending_dsp2cpu_cmd.type);
  1416. break;
  1417. }
  1418. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1419. mutex_unlock(&me->lock);
  1420. }
  1421. /* Responds to DSP */
  1422. rc = cvp_dsp_send_cmd(&cmd, sizeof(struct cvp_dsp_cmd_msg));
  1423. if (rc)
  1424. dprintk(CVP_ERR,
  1425. "%s: cvp_dsp_send_cmd failed rc = %d cmd type=%d\n",
  1426. __func__, rc, cmd.type);
  1427. goto wait_dsp;
  1428. exit:
  1429. dprintk(CVP_DBG, "dsp thread exit\n");
  1430. do_exit(rc);
  1431. return rc;
  1432. }
  1433. int cvp_dsp_device_init(void)
  1434. {
  1435. struct cvp_dsp_apps *me = &gfa_cv;
  1436. char tname[16];
  1437. int rc;
  1438. int i;
  1439. mutex_init(&me->lock);
  1440. me->state = DSP_INVALID;
  1441. me->hyp_assigned = false;
  1442. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1443. init_completion(&me->completions[i]);
  1444. me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
  1445. me->pending_dsp2cpu_rsp.type = CVP_INVALID_RPMSG_TYPE;
  1446. INIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1447. rc = register_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1448. if (rc) {
  1449. dprintk(CVP_ERR,
  1450. "%s : register_rpmsg_driver failed rc = %d\n",
  1451. __func__, rc);
  1452. goto register_bail;
  1453. }
  1454. snprintf(tname, sizeof(tname), "cvp-dsp-thread");
  1455. me->state = DSP_UNINIT;
  1456. me->dsp_thread = kthread_run(cvp_dsp_thread, me, tname);
  1457. if (!me->dsp_thread) {
  1458. dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
  1459. rc = -ECHILD;
  1460. me->state = DSP_INVALID;
  1461. goto register_bail;
  1462. }
  1463. return 0;
  1464. register_bail:
  1465. return rc;
  1466. }
  1467. void cvp_dsp_device_exit(void)
  1468. {
  1469. struct cvp_dsp_apps *me = &gfa_cv;
  1470. int i;
  1471. mutex_lock(&me->lock);
  1472. me->state = DSP_INVALID;
  1473. mutex_unlock(&me->lock);
  1474. DEINIT_MSM_CVP_LIST(&me->fastrpc_driver_list);
  1475. for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
  1476. complete_all(&me->completions[i]);
  1477. mutex_destroy(&me->lock);
  1478. unregister_rpmsg_driver(&cvp_dsp_rpmsg_client);
  1479. }