msm_cvp_dsp.c 56 KB

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