msm_cvp_dsp.c 49 KB

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