msm_cvp_dsp.c 58 KB

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