msm_cvp_dsp.c 58 KB

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