msm_cvp_dsp.c 58 KB

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