msm_cvp_dsp.c 56 KB

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