msm_cvp_common.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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/jiffies.h>
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include <linux/kernel.h>
  10. #include <linux/bitops.h>
  11. #include <asm/div64.h>
  12. #include "msm_cvp_common.h"
  13. #include "cvp_hfi_api.h"
  14. #include "msm_cvp_debug.h"
  15. #include "msm_cvp_clocks.h"
  16. #include "msm_cvp.h"
  17. #include "cvp_core_hfi.h"
  18. #define IS_ALREADY_IN_STATE(__p, __d) (\
  19. (__p >= __d)\
  20. )
  21. static void handle_session_error(enum hal_command_response cmd, void *data);
  22. static void dump_hfi_queue(struct iris_hfi_device *device)
  23. {
  24. struct cvp_hfi_queue_header *queue;
  25. struct cvp_iface_q_info *qinfo;
  26. int i;
  27. u32 *read_ptr, read_idx;
  28. dprintk(CVP_ERR, "HFI queues in order of cmd(rd, wr), msg and dbg:\n");
  29. /*
  30. * mb() to ensure driver reads the updated header values from
  31. * main memory.
  32. */
  33. mb();
  34. mutex_lock(&device->lock);
  35. for (i = 0; i <= CVP_IFACEQ_DBGQ_IDX; i++) {
  36. qinfo = &device->iface_queues[i];
  37. queue = (struct cvp_hfi_queue_header *)qinfo->q_hdr;
  38. if (!queue) {
  39. mutex_unlock(&device->lock);
  40. dprintk(CVP_ERR, "HFI queue not init, fail to dump\n");
  41. return;
  42. }
  43. dprintk(CVP_ERR, "queue details: r:w %d:%d r:t %d %d\n",
  44. queue->qhdr_read_idx, queue->qhdr_write_idx,
  45. queue->qhdr_rx_req, queue->qhdr_tx_req);
  46. if (queue->qhdr_read_idx != queue->qhdr_write_idx) {
  47. read_idx = queue->qhdr_read_idx;
  48. read_ptr = (u32 *)((qinfo->q_array.align_virtual_addr) +
  49. (read_idx << 2));
  50. dprintk(CVP_ERR,
  51. "queue payload: %x %x %x %x %x %x %x %x %x\n",
  52. read_ptr[0], read_ptr[1], read_ptr[2],
  53. read_ptr[3], read_ptr[4], read_ptr[5],
  54. read_ptr[6], read_ptr[7], read_ptr[8]);
  55. }
  56. }
  57. mutex_unlock(&device->lock);
  58. }
  59. void print_hfi_queue_info(struct cvp_hfi_device *hdev)
  60. {
  61. if(hdev && hdev->hfi_device_data){
  62. call_hfi_op(hdev, flush_debug_queue, hdev->hfi_device_data);
  63. dump_hfi_queue(hdev->hfi_device_data);
  64. }
  65. }
  66. static void handle_sys_init_done(enum hal_command_response cmd, void *data)
  67. {
  68. struct msm_cvp_cb_cmd_done *response = data;
  69. struct msm_cvp_core *core;
  70. struct cvp_hal_sys_init_done *sys_init_msg;
  71. u32 index;
  72. if (!IS_HAL_SYS_CMD(cmd)) {
  73. dprintk(CVP_ERR, "%s - invalid cmd\n", __func__);
  74. return;
  75. }
  76. index = SYS_MSG_INDEX(cmd);
  77. if (!response) {
  78. dprintk(CVP_ERR,
  79. "Failed to get valid response for sys init\n");
  80. return;
  81. }
  82. core = cvp_driver->cvp_core;
  83. if (!core) {
  84. dprintk(CVP_ERR, "Wrong device_id received\n");
  85. return;
  86. }
  87. sys_init_msg = &response->data.sys_init_done;
  88. if (!sys_init_msg) {
  89. dprintk(CVP_ERR, "sys_init_done message not proper\n");
  90. return;
  91. }
  92. /* This should come from sys_init_done */
  93. core->resources.max_inst_count =
  94. sys_init_msg->max_sessions_supported ?
  95. min_t(u32, sys_init_msg->max_sessions_supported,
  96. MAX_SUPPORTED_INSTANCES) : MAX_SUPPORTED_INSTANCES;
  97. core->resources.max_secure_inst_count =
  98. core->resources.max_secure_inst_count ?
  99. core->resources.max_secure_inst_count :
  100. core->resources.max_inst_count;
  101. memcpy(core->capabilities, sys_init_msg->capabilities,
  102. sys_init_msg->codec_count * sizeof(struct msm_cvp_capability));
  103. dprintk(CVP_CORE,
  104. "%s: max_inst_count %d, max_secure_inst_count %d\n",
  105. __func__, core->resources.max_inst_count,
  106. core->resources.max_secure_inst_count);
  107. complete(&(core->completions[index]));
  108. }
  109. static void put_inst_helper(struct kref *kref)
  110. {
  111. struct msm_cvp_inst *inst = container_of(kref,
  112. struct msm_cvp_inst, kref);
  113. msm_cvp_destroy(inst);
  114. }
  115. void cvp_put_inst(struct msm_cvp_inst *inst)
  116. {
  117. if (!inst)
  118. return;
  119. kref_put(&inst->kref, put_inst_helper);
  120. }
  121. struct msm_cvp_inst *cvp_get_inst(struct msm_cvp_core *core,
  122. void *session_id)
  123. {
  124. struct msm_cvp_inst *inst = NULL;
  125. bool matches = false;
  126. if (!core || !session_id)
  127. return NULL;
  128. mutex_lock(&core->lock);
  129. /*
  130. * This is as good as !list_empty(!inst->list), but at this point
  131. * we don't really know if inst was kfree'd via close syscall before
  132. * hardware could respond. So manually walk thru the list of active
  133. * sessions
  134. */
  135. list_for_each_entry(inst, &core->instances, list) {
  136. if (inst == session_id) {
  137. /*
  138. * Even if the instance is valid, we really shouldn't
  139. * be receiving or handling callbacks when we've deleted
  140. * our session with HFI
  141. */
  142. matches = !!inst->session;
  143. break;
  144. }
  145. }
  146. /*
  147. * kref_* is atomic_int backed, so no need for inst->lock. But we can
  148. * always acquire inst->lock and release it in cvp_put_inst
  149. * for a stronger locking system.
  150. */
  151. inst = (matches && kref_get_unless_zero(&inst->kref)) ? inst : NULL;
  152. mutex_unlock(&core->lock);
  153. return inst;
  154. }
  155. struct msm_cvp_inst *cvp_get_inst_validate(struct msm_cvp_core *core,
  156. void *session_id)
  157. {
  158. int rc = 0;
  159. struct cvp_hfi_device *hdev;
  160. struct msm_cvp_inst *s;
  161. s = cvp_get_inst(core, session_id);
  162. if (!s) {
  163. dprintk(CVP_ERR, "%s session doesn't exit\n",
  164. __builtin_return_address(0));
  165. return NULL;
  166. }
  167. hdev = s->core->device;
  168. rc = call_hfi_op(hdev, validate_session, s->session, __func__);
  169. if (rc) {
  170. cvp_put_inst(s);
  171. s = NULL;
  172. }
  173. return s;
  174. }
  175. static void handle_session_set_buf_done(enum hal_command_response cmd,
  176. void *data)
  177. {
  178. struct msm_cvp_cb_cmd_done *response = data;
  179. struct msm_cvp_inst *inst;
  180. if (!response) {
  181. dprintk(CVP_ERR, "Invalid set_buf_done response\n");
  182. return;
  183. }
  184. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  185. if (!inst) {
  186. dprintk(CVP_WARN, "set_buf_done has an inactive session\n");
  187. return;
  188. }
  189. if (response->status) {
  190. dprintk(CVP_ERR,
  191. "set ARP buffer error from FW : %#x\n",
  192. response->status);
  193. }
  194. if (IS_HAL_SESSION_CMD(cmd))
  195. complete(&inst->completions[SESSION_MSG_INDEX(cmd)]);
  196. else
  197. dprintk(CVP_ERR, "set_buf_done: invalid cmd: %d\n", cmd);
  198. cvp_put_inst(inst);
  199. }
  200. static void handle_session_release_buf_done(enum hal_command_response cmd,
  201. void *data)
  202. {
  203. struct msm_cvp_cb_cmd_done *response = data;
  204. struct msm_cvp_inst *inst;
  205. struct cvp_internal_buf *buf;
  206. struct list_head *ptr, *next;
  207. u32 buf_found = false;
  208. u32 address;
  209. if (!response) {
  210. dprintk(CVP_ERR, "Invalid release_buf_done response\n");
  211. return;
  212. }
  213. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  214. if (!inst) {
  215. dprintk(CVP_WARN,
  216. "%s: Got a response for an inactive session\n",
  217. __func__);
  218. return;
  219. }
  220. address = response->data.buffer_addr;
  221. mutex_lock(&inst->persistbufs.lock);
  222. list_for_each_safe(ptr, next, &inst->persistbufs.list) {
  223. buf = list_entry(ptr, struct cvp_internal_buf, list);
  224. if (address == buf->smem->device_addr + buf->offset) {
  225. dprintk(CVP_SESS, "releasing persist: %#x\n",
  226. buf->smem->device_addr);
  227. buf_found = true;
  228. }
  229. }
  230. mutex_unlock(&inst->persistbufs.lock);
  231. if (response->status)
  232. dprintk(CVP_ERR, "HFI release persist buf err 0x%x\n",
  233. response->status);
  234. inst->error_code = response->status;
  235. if (IS_HAL_SESSION_CMD(cmd))
  236. complete(&inst->completions[SESSION_MSG_INDEX(cmd)]);
  237. else
  238. dprintk(CVP_ERR, "Invalid inst cmd response: %d\n", cmd);
  239. cvp_put_inst(inst);
  240. }
  241. static void handle_sys_release_res_done(
  242. enum hal_command_response cmd, void *data)
  243. {
  244. struct msm_cvp_cb_cmd_done *response = data;
  245. struct msm_cvp_core *core;
  246. if (!response) {
  247. dprintk(CVP_ERR,
  248. "Failed to get valid response for sys init\n");
  249. return;
  250. }
  251. core = cvp_driver->cvp_core;
  252. if (!core) {
  253. dprintk(CVP_ERR, "Wrong device_id received\n");
  254. return;
  255. }
  256. complete(&core->completions[
  257. SYS_MSG_INDEX(HAL_SYS_RELEASE_RESOURCE_DONE)]);
  258. }
  259. void change_cvp_inst_state(struct msm_cvp_inst *inst, enum instance_state state)
  260. {
  261. if (!inst) {
  262. dprintk(CVP_ERR, "Invalid parameter %s\n", __func__);
  263. return;
  264. }
  265. mutex_lock(&inst->lock);
  266. if (inst->state == MSM_CVP_CORE_INVALID) {
  267. dprintk(CVP_SESS,
  268. "Inst: %pK is in bad state can't change state to %d\n",
  269. inst, state);
  270. goto exit;
  271. }
  272. dprintk(CVP_SESS, "Moved inst: %pK from state: %d to state: %d\n",
  273. inst, inst->state, state);
  274. inst->state = state;
  275. exit:
  276. mutex_unlock(&inst->lock);
  277. }
  278. static int signal_session_msg_receipt(enum hal_command_response cmd,
  279. struct msm_cvp_inst *inst)
  280. {
  281. if (!inst) {
  282. dprintk(CVP_ERR, "Invalid(%pK) instance id\n", inst);
  283. return -EINVAL;
  284. }
  285. if (IS_HAL_SESSION_CMD(cmd)) {
  286. complete(&inst->completions[SESSION_MSG_INDEX(cmd)]);
  287. } else {
  288. dprintk(CVP_ERR, "Invalid inst cmd response: %d\n", cmd);
  289. return -EINVAL;
  290. }
  291. return 0;
  292. }
  293. int wait_for_sess_signal_receipt(struct msm_cvp_inst *inst,
  294. enum hal_command_response cmd)
  295. {
  296. int rc = 0;
  297. struct cvp_hfi_device *hdev;
  298. if (!IS_HAL_SESSION_CMD(cmd)) {
  299. dprintk(CVP_ERR, "Invalid inst cmd response: %d\n", cmd);
  300. return -EINVAL;
  301. }
  302. hdev = (struct cvp_hfi_device *)(inst->core->device);
  303. rc = wait_for_completion_timeout(
  304. &inst->completions[SESSION_MSG_INDEX(cmd)],
  305. msecs_to_jiffies(
  306. inst->core->resources.msm_cvp_hw_rsp_timeout));
  307. if (!rc) {
  308. dprintk(CVP_WARN, "Wait interrupted or timed out: %d\n",
  309. SESSION_MSG_INDEX(cmd));
  310. if (inst->state != MSM_CVP_CORE_INVALID)
  311. print_hfi_queue_info(hdev);
  312. rc = -ETIMEDOUT;
  313. } else if (inst->state == MSM_CVP_CORE_INVALID) {
  314. rc = -ECONNRESET;
  315. } else {
  316. rc = inst->error_code;
  317. inst->prev_error_code = inst->error_code;
  318. inst->error_code = CVP_ERR_NONE;
  319. }
  320. return rc;
  321. }
  322. static int wait_for_state(struct msm_cvp_inst *inst,
  323. enum instance_state flipped_state,
  324. enum instance_state desired_state,
  325. enum hal_command_response hal_cmd)
  326. {
  327. int rc = 0;
  328. if (IS_ALREADY_IN_STATE(flipped_state, desired_state)) {
  329. dprintk(CVP_INFO, "inst: %pK is already in state: %d\n",
  330. inst, inst->state);
  331. goto err_same_state;
  332. }
  333. dprintk(CVP_SESS, "Waiting for hal_cmd: %d\n", hal_cmd);
  334. rc = wait_for_sess_signal_receipt(inst, hal_cmd);
  335. if (!rc)
  336. change_cvp_inst_state(inst, desired_state);
  337. err_same_state:
  338. return rc;
  339. }
  340. static void handle_session_init_done(enum hal_command_response cmd, void *data)
  341. {
  342. struct msm_cvp_cb_cmd_done *response = data;
  343. struct msm_cvp_inst *inst = NULL;
  344. if (!response) {
  345. dprintk(CVP_ERR,
  346. "Failed to get valid response for session init\n");
  347. return;
  348. }
  349. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  350. if (!inst) {
  351. dprintk(CVP_WARN, "%s:Got a response for an inactive session\n",
  352. __func__);
  353. return;
  354. }
  355. if (response->status)
  356. dprintk(CVP_ERR,
  357. "Session %#x init err response from FW : 0x%x\n",
  358. hash32_ptr(inst->session), response->status);
  359. else
  360. dprintk(CVP_SESS, "%s: cvp session %#x\n", __func__,
  361. hash32_ptr(inst->session));
  362. inst->error_code = response->status;
  363. signal_session_msg_receipt(cmd, inst);
  364. cvp_put_inst(inst);
  365. return;
  366. }
  367. static void handle_event_change(enum hal_command_response cmd, void *data)
  368. {
  369. dprintk(CVP_WARN, "%s is not supported on CVP!\n", __func__);
  370. }
  371. static void handle_session_dump_notify(enum hal_command_response cmd,
  372. void *data)
  373. {
  374. struct msm_cvp_cb_cmd_done *response = data;
  375. struct msm_cvp_inst *inst;
  376. unsigned long flags = 0;
  377. if (!response) {
  378. dprintk(CVP_ERR,
  379. "Failed to get valid response during dump notify\n");
  380. return;
  381. }
  382. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  383. if (!inst) {
  384. dprintk(CVP_WARN, "%s:Got a response for an inactive session\n",
  385. __func__);
  386. return;
  387. }
  388. spin_lock_irqsave(&inst->event_handler.lock, flags);
  389. inst->event_handler.event = CVP_DUMP_EVENT;
  390. spin_unlock_irqrestore(&inst->event_handler.lock, flags);
  391. wake_up_all(&inst->event_handler.wq);
  392. dprintk(CVP_ERR,"Event_handler woken up\n");
  393. cvp_put_inst(inst);
  394. }
  395. static void handle_release_res_done(enum hal_command_response cmd, void *data)
  396. {
  397. struct msm_cvp_cb_cmd_done *response = data;
  398. struct msm_cvp_inst *inst;
  399. if (!response) {
  400. dprintk(CVP_ERR,
  401. "Failed to get valid response for release resource\n");
  402. return;
  403. }
  404. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  405. if (!inst) {
  406. dprintk(CVP_WARN, "%s:Got a response for an inactive session\n",
  407. __func__);
  408. return;
  409. }
  410. signal_session_msg_receipt(cmd, inst);
  411. cvp_put_inst(inst);
  412. }
  413. static void handle_session_ctrl(enum hal_command_response cmd, void *data)
  414. {
  415. struct msm_cvp_cb_cmd_done *response = data;
  416. struct msm_cvp_inst *inst;
  417. if (!response) {
  418. dprintk(CVP_ERR,
  419. "Failed to get valid response for release resource\n");
  420. return;
  421. }
  422. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  423. if (!inst) {
  424. dprintk(CVP_WARN, "%s:Got a response for an inactive session\n",
  425. __func__);
  426. return;
  427. }
  428. if (response->status)
  429. dprintk(CVP_ERR, "HFI sess ctrl err 0x%x HAL cmd %d\n",
  430. response->status, cmd);
  431. inst->error_code = response->status;
  432. signal_session_msg_receipt(cmd, inst);
  433. cvp_put_inst(inst);
  434. }
  435. static void handle_session_error(enum hal_command_response cmd, void *data)
  436. {
  437. struct msm_cvp_cb_cmd_done *response = data;
  438. struct cvp_hfi_device *hdev = NULL;
  439. struct msm_cvp_inst *inst = NULL;
  440. unsigned long flags = 0;
  441. int i;
  442. if (!response) {
  443. dprintk(CVP_ERR,
  444. "Failed to get valid response for session error\n");
  445. return;
  446. }
  447. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  448. if (!inst) {
  449. dprintk(CVP_WARN, "%s: response for an inactive session\n",
  450. __func__);
  451. return;
  452. }
  453. hdev = inst->core->device;
  454. dprintk(CVP_ERR, "Sess error 0x%x received for inst %pK sess %x\n",
  455. response->status, inst, hash32_ptr(inst->session));
  456. cvp_print_inst(CVP_WARN, inst);
  457. if (inst->state != MSM_CVP_CORE_INVALID) {
  458. change_cvp_inst_state(inst, MSM_CVP_CORE_INVALID);
  459. if (cvp_clean_session_queues(inst))
  460. dprintk(CVP_WARN, "Failed to clean sess queues\n");
  461. for (i = 0; i < ARRAY_SIZE(inst->completions); i++)
  462. complete(&inst->completions[i]);
  463. spin_lock_irqsave(&inst->event_handler.lock, flags);
  464. inst->event_handler.event = CVP_SSR_EVENT;
  465. spin_unlock_irqrestore(
  466. &inst->event_handler.lock, flags);
  467. wake_up_all(&inst->event_handler.wq);
  468. }
  469. cvp_put_inst(inst);
  470. }
  471. void handle_sys_error(enum hal_command_response cmd, void *data)
  472. {
  473. struct msm_cvp_cb_cmd_done *response = data;
  474. struct msm_cvp_core *core = NULL;
  475. struct cvp_hfi_device *hdev = NULL;
  476. struct iris_hfi_device *hfi_device;
  477. struct msm_cvp_inst *inst = NULL;
  478. int i, rc = 0;
  479. unsigned long flags = 0;
  480. enum cvp_core_state cur_state;
  481. if (!response) {
  482. dprintk(CVP_ERR,
  483. "Failed to get valid response for sys error\n");
  484. return;
  485. }
  486. core = cvp_driver->cvp_core;
  487. if (!core) {
  488. dprintk(CVP_ERR,
  489. "Got SYS_ERR but unable to identify core\n");
  490. return;
  491. }
  492. hdev = core->device;
  493. mutex_lock(&core->lock);
  494. if (core->state == CVP_CORE_UNINIT) {
  495. dprintk(CVP_ERR,
  496. "%s: Core %pK already moved to state %d\n",
  497. __func__, core, core->state);
  498. mutex_unlock(&core->lock);
  499. return;
  500. }
  501. cur_state = core->state;
  502. core->state = CVP_CORE_UNINIT;
  503. dprintk(CVP_WARN, "SYS_ERROR received for core %pK cmd %x\n",
  504. core, cmd);
  505. mutex_lock(&core->clk_lock);
  506. hfi_device = hdev->hfi_device_data;
  507. if (hfi_device->error == CVP_ERR_NOC_ERROR) {
  508. dprintk(CVP_WARN, "Got NOC error");
  509. msm_cvp_noc_error_info(core);
  510. }
  511. call_hfi_op(hdev, flush_debug_queue, hdev->hfi_device_data);
  512. list_for_each_entry(inst, &core->instances, list) {
  513. cvp_print_inst(CVP_WARN, inst);
  514. if (inst->state != MSM_CVP_CORE_INVALID) {
  515. change_cvp_inst_state(inst, MSM_CVP_CORE_INVALID);
  516. if (cvp_clean_session_queues(inst))
  517. dprintk(CVP_ERR, "Failed to clean fences\n");
  518. for (i = 0; i < ARRAY_SIZE(inst->completions); i++)
  519. complete(&inst->completions[i]);
  520. spin_lock_irqsave(&inst->event_handler.lock, flags);
  521. inst->event_handler.event = CVP_SSR_EVENT;
  522. spin_unlock_irqrestore(
  523. &inst->event_handler.lock, flags);
  524. wake_up_all(&inst->event_handler.wq);
  525. }
  526. if (!core->trigger_ssr)
  527. if (hfi_device->error != CVP_ERR_NOC_ERROR)
  528. msm_cvp_print_inst_bufs(inst, false);
  529. }
  530. /* handle the hw error before core released to get full debug info */
  531. msm_cvp_handle_hw_error(core);
  532. dprintk(CVP_CORE, "Calling core_release\n");
  533. rc = call_hfi_op(hdev, core_release, hdev->hfi_device_data);
  534. if (rc) {
  535. dprintk(CVP_ERR, "core_release failed\n");
  536. core->state = cur_state;
  537. mutex_unlock(&core->clk_lock);
  538. mutex_unlock(&core->lock);
  539. return;
  540. }
  541. mutex_unlock(&core->clk_lock);
  542. mutex_unlock(&core->lock);
  543. dprintk(CVP_WARN, "SYS_ERROR handled.\n");
  544. BUG_ON(core->resources.fatal_ssr);
  545. }
  546. void msm_cvp_comm_session_clean(struct msm_cvp_inst *inst)
  547. {
  548. int rc = 0;
  549. struct cvp_hfi_device *hdev = NULL;
  550. if (!inst || !inst->core || !inst->core->device) {
  551. dprintk(CVP_ERR, "%s invalid params\n", __func__);
  552. return;
  553. }
  554. if (!inst->session) {
  555. dprintk(CVP_SESS, "%s: inst %pK session already cleaned\n",
  556. __func__, inst);
  557. return;
  558. }
  559. hdev = inst->core->device;
  560. mutex_lock(&inst->lock);
  561. dprintk(CVP_SESS, "%s: inst %pK\n", __func__, inst);
  562. rc = call_hfi_op(hdev, session_clean,
  563. (void *)inst->session);
  564. if (rc) {
  565. dprintk(CVP_ERR,
  566. "Session clean failed :%pK\n", inst);
  567. }
  568. inst->session = NULL;
  569. mutex_unlock(&inst->lock);
  570. }
  571. static void handle_session_close(enum hal_command_response cmd, void *data)
  572. {
  573. struct msm_cvp_cb_cmd_done *response = data;
  574. struct msm_cvp_inst *inst;
  575. if (!response) {
  576. dprintk(CVP_ERR,
  577. "Failed to get valid response for session close\n");
  578. return;
  579. }
  580. inst = cvp_get_inst(cvp_driver->cvp_core, response->session_id);
  581. if (!inst) {
  582. dprintk(CVP_WARN, "%s: response for an inactive session\n",
  583. __func__);
  584. return;
  585. }
  586. if (response->status)
  587. dprintk(CVP_ERR, "HFI sess close fail 0x%x\n",
  588. response->status);
  589. inst->error_code = response->status;
  590. signal_session_msg_receipt(cmd, inst);
  591. show_stats(inst);
  592. cvp_put_inst(inst);
  593. }
  594. void cvp_handle_cmd_response(enum hal_command_response cmd, void *data)
  595. {
  596. dprintk(CVP_HFI, "Command response = %d\n", cmd);
  597. switch (cmd) {
  598. case HAL_SYS_INIT_DONE:
  599. handle_sys_init_done(cmd, data);
  600. break;
  601. case HAL_SYS_RELEASE_RESOURCE_DONE:
  602. handle_sys_release_res_done(cmd, data);
  603. break;
  604. case HAL_SESSION_INIT_DONE:
  605. handle_session_init_done(cmd, data);
  606. break;
  607. case HAL_SESSION_RELEASE_RESOURCE_DONE:
  608. handle_release_res_done(cmd, data);
  609. break;
  610. case HAL_SESSION_END_DONE:
  611. case HAL_SESSION_ABORT_DONE:
  612. handle_session_close(cmd, data);
  613. break;
  614. case HAL_SESSION_EVENT_CHANGE:
  615. handle_event_change(cmd, data);
  616. break;
  617. case HAL_SESSION_FLUSH_DONE:
  618. case HAL_SESSION_START_DONE:
  619. case HAL_SESSION_STOP_DONE:
  620. handle_session_ctrl(cmd, data);
  621. break;
  622. case HAL_SYS_WATCHDOG_TIMEOUT:
  623. case HAL_SYS_ERROR:
  624. handle_sys_error(cmd, data);
  625. break;
  626. case HAL_SESSION_ERROR:
  627. handle_session_error(cmd, data);
  628. break;
  629. case HAL_SESSION_SET_BUFFER_DONE:
  630. handle_session_set_buf_done(cmd, data);
  631. break;
  632. case HAL_SESSION_RELEASE_BUFFER_DONE:
  633. handle_session_release_buf_done(cmd, data);
  634. break;
  635. case HAL_SESSION_DUMP_NOTIFY:
  636. handle_session_dump_notify(cmd, data);
  637. break;
  638. default:
  639. dprintk(CVP_HFI, "response unhandled: %d\n", cmd);
  640. break;
  641. }
  642. }
  643. static inline enum msm_cvp_thermal_level msm_comm_cvp_thermal_level(int level)
  644. {
  645. switch (level) {
  646. case 0:
  647. return CVP_THERMAL_NORMAL;
  648. case 1:
  649. return CVP_THERMAL_LOW;
  650. case 2:
  651. return CVP_THERMAL_HIGH;
  652. default:
  653. return CVP_THERMAL_CRITICAL;
  654. }
  655. }
  656. static int msm_comm_session_abort(struct msm_cvp_inst *inst)
  657. {
  658. int rc = 0, abort_completion = 0;
  659. struct cvp_hfi_device *hdev;
  660. if (!inst || !inst->core || !inst->core->device) {
  661. dprintk(CVP_ERR, "%s invalid params\n", __func__);
  662. return -EINVAL;
  663. }
  664. hdev = inst->core->device;
  665. print_hfi_queue_info(hdev);
  666. if (1)
  667. return 0;
  668. /* Activate code below for Watchdog timeout testing */
  669. abort_completion = SESSION_MSG_INDEX(HAL_SESSION_ABORT_DONE);
  670. dprintk(CVP_WARN, "%s: inst %pK session %x\n", __func__,
  671. inst, hash32_ptr(inst->session));
  672. rc = call_hfi_op(hdev, session_abort, (void *)inst->session);
  673. if (rc) {
  674. dprintk(CVP_ERR,
  675. "%s session_abort failed rc: %d\n", __func__, rc);
  676. goto exit;
  677. }
  678. rc = wait_for_completion_timeout(
  679. &inst->completions[abort_completion],
  680. msecs_to_jiffies(
  681. inst->core->resources.msm_cvp_hw_rsp_timeout));
  682. if (!rc) {
  683. dprintk(CVP_ERR, "%s: inst %pK session %x abort timed out\n",
  684. __func__, inst, hash32_ptr(inst->session));
  685. print_hfi_queue_info(hdev);
  686. msm_cvp_comm_generate_sys_error(inst);
  687. rc = -EBUSY;
  688. } else {
  689. rc = 0;
  690. }
  691. exit:
  692. return rc;
  693. }
  694. void msm_cvp_comm_handle_thermal_event(void)
  695. {
  696. dprintk(CVP_WARN, "deprecated %s called\n", __func__);
  697. }
  698. int msm_cvp_comm_check_core_init(struct msm_cvp_core *core)
  699. {
  700. int rc = 0;
  701. struct cvp_hfi_device *hdev;
  702. mutex_lock(&core->lock);
  703. if (core->state >= CVP_CORE_INIT_DONE) {
  704. dprintk(CVP_INFO, "CVP core: is already in state: %d\n",
  705. core->state);
  706. goto exit;
  707. }
  708. dprintk(CVP_CORE, "Waiting for SYS_INIT_DONE\n");
  709. rc = wait_for_completion_timeout(
  710. &core->completions[SYS_MSG_INDEX(HAL_SYS_INIT_DONE)],
  711. msecs_to_jiffies(core->resources.msm_cvp_hw_rsp_timeout));
  712. if (!rc) {
  713. dprintk(CVP_ERR, "%s: Wait interrupted or timed out: %d\n",
  714. __func__, SYS_MSG_INDEX(HAL_SYS_INIT_DONE));
  715. hdev = core->device;
  716. print_hfi_queue_info(hdev);
  717. rc = -EIO;
  718. goto exit;
  719. } else {
  720. core->state = CVP_CORE_INIT_DONE;
  721. rc = 0;
  722. }
  723. dprintk(CVP_CORE, "SYS_INIT_DONE!!!\n");
  724. exit:
  725. mutex_unlock(&core->lock);
  726. return rc;
  727. }
  728. static int msm_comm_init_core_done(struct msm_cvp_inst *inst)
  729. {
  730. int rc = 0;
  731. rc = msm_cvp_comm_check_core_init(inst->core);
  732. if (rc) {
  733. dprintk(CVP_ERR, "%s - failed to initialize core\n", __func__);
  734. msm_cvp_comm_generate_sys_error(inst);
  735. return rc;
  736. }
  737. change_cvp_inst_state(inst, MSM_CVP_CORE_INIT_DONE);
  738. return rc;
  739. }
  740. static int msm_comm_init_core(struct msm_cvp_inst *inst)
  741. {
  742. int rc = 0;
  743. struct cvp_hfi_device *hdev;
  744. struct msm_cvp_core *core;
  745. if (!inst || !inst->core || !inst->core->device)
  746. return -EINVAL;
  747. core = inst->core;
  748. hdev = core->device;
  749. mutex_lock(&core->lock);
  750. if (core->state >= CVP_CORE_INIT) {
  751. dprintk(CVP_CORE, "CVP core: is already in state: %d\n",
  752. core->state);
  753. goto core_already_inited;
  754. }
  755. if (!core->capabilities) {
  756. core->capabilities = kcalloc(CVP_MAX_SESSIONS,
  757. sizeof(struct msm_cvp_capability), GFP_KERNEL);
  758. if (!core->capabilities) {
  759. dprintk(CVP_ERR,
  760. "%s: failed to allocate capabilities\n",
  761. __func__);
  762. rc = -ENOMEM;
  763. goto fail_cap_alloc;
  764. }
  765. } else {
  766. dprintk(CVP_WARN,
  767. "%s: capabilities memory is expected to be freed\n",
  768. __func__);
  769. }
  770. dprintk(CVP_CORE, "%s: core %pK\n", __func__, core);
  771. rc = call_hfi_op(hdev, core_init, hdev->hfi_device_data);
  772. if (rc) {
  773. dprintk(CVP_ERR, "Failed to init core\n");
  774. goto fail_core_init;
  775. }
  776. core->state = CVP_CORE_INIT;
  777. core->trigger_ssr = false;
  778. core_already_inited:
  779. change_cvp_inst_state(inst, MSM_CVP_CORE_INIT);
  780. mutex_unlock(&core->lock);
  781. return rc;
  782. fail_core_init:
  783. kfree(core->capabilities);
  784. fail_cap_alloc:
  785. core->capabilities = NULL;
  786. core->state = CVP_CORE_UNINIT;
  787. mutex_unlock(&core->lock);
  788. return rc;
  789. }
  790. int msm_cvp_deinit_core(struct msm_cvp_inst *inst)
  791. {
  792. struct msm_cvp_core *core;
  793. struct cvp_hfi_device *hdev;
  794. if (!inst || !inst->core || !inst->core->device) {
  795. dprintk(CVP_ERR, "%s invalid parameters\n", __func__);
  796. return -EINVAL;
  797. }
  798. core = inst->core;
  799. hdev = core->device;
  800. mutex_lock(&core->lock);
  801. change_cvp_inst_state(inst, MSM_CVP_CORE_UNINIT);
  802. mutex_unlock(&core->lock);
  803. return 0;
  804. }
  805. static int msm_comm_session_init_done(int flipped_state,
  806. struct msm_cvp_inst *inst)
  807. {
  808. int rc;
  809. dprintk(CVP_SESS, "inst %pK: waiting for session init done\n", inst);
  810. rc = wait_for_state(inst, flipped_state, MSM_CVP_OPEN_DONE,
  811. HAL_SESSION_INIT_DONE);
  812. if (rc) {
  813. dprintk(CVP_ERR, "Session init failed for inst %pK\n", inst);
  814. return rc;
  815. }
  816. return rc;
  817. }
  818. static int msm_comm_session_init(int flipped_state,
  819. struct msm_cvp_inst *inst)
  820. {
  821. int rc = 0;
  822. struct cvp_hfi_device *hdev;
  823. if (!inst || !inst->core || !inst->core->device) {
  824. dprintk(CVP_ERR, "%s invalid parameters\n", __func__);
  825. return -EINVAL;
  826. }
  827. hdev = inst->core->device;
  828. if (IS_ALREADY_IN_STATE(flipped_state, MSM_CVP_OPEN)) {
  829. dprintk(CVP_INFO, "inst: %pK is already in state: %d\n",
  830. inst, inst->state);
  831. goto exit;
  832. }
  833. dprintk(CVP_SESS, "%s: inst %pK\n", __func__, inst);
  834. rc = call_hfi_op(hdev, session_init, hdev->hfi_device_data,
  835. inst, &inst->session);
  836. if (rc || !inst->session) {
  837. dprintk(CVP_ERR,
  838. "Failed to call session init for: %pK, %pK, %d\n",
  839. inst->core->device, inst, inst->session_type);
  840. rc = -EINVAL;
  841. goto exit;
  842. }
  843. change_cvp_inst_state(inst, MSM_CVP_OPEN);
  844. exit:
  845. return rc;
  846. }
  847. static int msm_comm_session_close(int flipped_state,
  848. struct msm_cvp_inst *inst)
  849. {
  850. int rc = 0;
  851. struct cvp_hfi_device *hdev;
  852. if (!inst || !inst->core || !inst->core->device) {
  853. dprintk(CVP_ERR, "%s invalid params\n", __func__);
  854. return -EINVAL;
  855. }
  856. if (IS_ALREADY_IN_STATE(flipped_state, MSM_CVP_CLOSE)) {
  857. dprintk(CVP_INFO,
  858. "inst: %pK is already in state: %d\n",
  859. inst, inst->state);
  860. goto exit;
  861. }
  862. hdev = inst->core->device;
  863. dprintk(CVP_SESS, "%s: inst %pK\n", __func__, inst);
  864. rc = call_hfi_op(hdev, session_end, (void *) inst->session);
  865. if (rc) {
  866. dprintk(CVP_ERR,
  867. "Failed to send close\n");
  868. goto exit;
  869. }
  870. change_cvp_inst_state(inst, MSM_CVP_CLOSE);
  871. exit:
  872. return rc;
  873. }
  874. int msm_cvp_comm_suspend(void)
  875. {
  876. struct cvp_hfi_device *hdev;
  877. struct msm_cvp_core *core;
  878. int rc = 0;
  879. core = cvp_driver->cvp_core;
  880. if (!core) {
  881. dprintk(CVP_ERR,
  882. "%s: Failed to find cvp core\n", __func__);
  883. return -EINVAL;
  884. }
  885. hdev = (struct cvp_hfi_device *)core->device;
  886. if (!hdev) {
  887. dprintk(CVP_ERR, "%s Invalid device handle\n", __func__);
  888. return -EINVAL;
  889. }
  890. rc = call_hfi_op(hdev, suspend, hdev->hfi_device_data);
  891. return rc;
  892. }
  893. static int get_flipped_state(int present_state, int desired_state)
  894. {
  895. int flipped_state;
  896. if (present_state == MSM_CVP_CORE_INIT_DONE && desired_state > MSM_CVP_CLOSE)
  897. flipped_state = MSM_CVP_CORE_UNINIT;
  898. else if (present_state == MSM_CVP_CORE_INVALID)
  899. flipped_state = MSM_CVP_CLOSE;
  900. else
  901. flipped_state = present_state;
  902. return flipped_state;
  903. }
  904. static char state_names[MSM_CVP_CORE_INVALID + 1][32] = {
  905. "Invlid entry",
  906. "CORE_UNINIT_DONE",
  907. "CORE_INIT",
  908. "CORE_INIT_DONE",
  909. "OPEN",
  910. "OPEN_DONE",
  911. "CLOSE",
  912. "CLOSE_DONE",
  913. "CORE_UNINIT",
  914. "CORE_INVALID"
  915. };
  916. int msm_cvp_comm_try_state(struct msm_cvp_inst *inst, int state)
  917. {
  918. int rc = 0;
  919. int flipped_state;
  920. struct msm_cvp_core *core;
  921. core = cvp_driver->cvp_core;
  922. if (!inst) {
  923. dprintk(CVP_ERR, "%s: invalid params %pK", __func__, inst);
  924. return -EINVAL;
  925. }
  926. mutex_lock(&inst->sync_lock);
  927. if (inst->state == MSM_CVP_CORE_INVALID &&
  928. core->state == CVP_CORE_UNINIT) {
  929. dprintk(CVP_ERR, "%s: inst %pK & core are in invalid\n",
  930. __func__, inst);
  931. mutex_unlock(&inst->sync_lock);
  932. return -EINVAL;
  933. }
  934. flipped_state = get_flipped_state(inst->state, state);
  935. dprintk(CVP_SESS,
  936. "inst: %pK (%#x) cur_state %s dest_state %s flipped_state = %s\n",
  937. inst, hash32_ptr(inst->session), state_names[inst->state],
  938. state_names[state], state_names[flipped_state]);
  939. switch (flipped_state) {
  940. case MSM_CVP_CORE_UNINIT_DONE:
  941. case MSM_CVP_CORE_INIT:
  942. rc = msm_comm_init_core(inst);
  943. if (rc || state <= get_flipped_state(inst->state, state))
  944. break;
  945. /* defined in linux/compiler_attributes.h */
  946. fallthrough;
  947. case MSM_CVP_CORE_INIT_DONE:
  948. rc = msm_comm_init_core_done(inst);
  949. if (rc || state <= get_flipped_state(inst->state, state))
  950. break;
  951. fallthrough;
  952. case MSM_CVP_OPEN:
  953. rc = msm_comm_session_init(flipped_state, inst);
  954. if (rc || state <= get_flipped_state(inst->state, state))
  955. break;
  956. fallthrough;
  957. case MSM_CVP_OPEN_DONE:
  958. rc = msm_comm_session_init_done(flipped_state, inst);
  959. if (rc || state <= get_flipped_state(inst->state, state))
  960. break;
  961. fallthrough;
  962. case MSM_CVP_CLOSE:
  963. dprintk(CVP_INFO, "to CVP_CLOSE state\n");
  964. rc = msm_comm_session_close(flipped_state, inst);
  965. if (rc || state <= get_flipped_state(inst->state, state))
  966. break;
  967. fallthrough;
  968. case MSM_CVP_CLOSE_DONE:
  969. dprintk(CVP_INFO, "to CVP_CLOSE_DONE state\n");
  970. rc = wait_for_state(inst, flipped_state, MSM_CVP_CLOSE_DONE,
  971. HAL_SESSION_END_DONE);
  972. if (rc || state <= get_flipped_state(inst->state, state))
  973. break;
  974. msm_cvp_comm_session_clean(inst);
  975. fallthrough;
  976. case MSM_CVP_CORE_UNINIT:
  977. case MSM_CVP_CORE_INVALID:
  978. dprintk(CVP_INFO, "Sending core uninit\n");
  979. rc = msm_cvp_deinit_core(inst);
  980. if (rc || state <= get_flipped_state(inst->state, state))
  981. break;
  982. fallthrough;
  983. default:
  984. dprintk(CVP_ERR, "State not recognized\n");
  985. rc = -EINVAL;
  986. break;
  987. }
  988. mutex_unlock(&inst->sync_lock);
  989. if (rc == -ETIMEDOUT) {
  990. dprintk(CVP_ERR,
  991. "Timedout move from state: %s to %s\n",
  992. state_names[inst->state],
  993. state_names[state]);
  994. if (inst->state != MSM_CVP_CORE_INVALID)
  995. msm_cvp_comm_kill_session(inst);
  996. }
  997. return rc;
  998. }
  999. int msm_cvp_noc_error_info(struct msm_cvp_core *core)
  1000. {
  1001. struct cvp_hfi_device *hdev;
  1002. static u32 last_fault_count = 0;
  1003. if (!core || !core->device) {
  1004. dprintk(CVP_WARN, "%s: Invalid parameters: %pK\n",
  1005. __func__, core);
  1006. return -EINVAL;
  1007. }
  1008. if (!core->smmu_fault_count ||
  1009. core->smmu_fault_count == last_fault_count)
  1010. return 0;
  1011. last_fault_count = core->smmu_fault_count;
  1012. core->ssr_count++;
  1013. dprintk(CVP_ERR, "cvp ssr count %d %d %d\n", core->ssr_count,
  1014. core->resources.max_ssr_allowed,
  1015. core->smmu_fault_count);
  1016. hdev = core->device;
  1017. call_hfi_op(hdev, noc_error_info, hdev->hfi_device_data);
  1018. if (core->ssr_count >= core->resources.max_ssr_allowed)
  1019. BUG_ON(!core->resources.non_fatal_pagefaults);
  1020. return 0;
  1021. }
  1022. int msm_cvp_trigger_ssr(struct msm_cvp_core *core,
  1023. enum hal_ssr_trigger_type type)
  1024. {
  1025. if (!core) {
  1026. dprintk(CVP_WARN, "%s: Invalid parameters\n", __func__);
  1027. return -EINVAL;
  1028. }
  1029. core->ssr_type = type;
  1030. schedule_work(&core->ssr_work);
  1031. return 0;
  1032. }
  1033. void msm_cvp_ssr_handler(struct work_struct *work)
  1034. {
  1035. int rc;
  1036. struct msm_cvp_core *core;
  1037. struct cvp_hfi_device *hdev;
  1038. core = container_of(work, struct msm_cvp_core, ssr_work);
  1039. if (!core || !core->device) {
  1040. dprintk(CVP_ERR, "%s: Invalid params\n", __func__);
  1041. return;
  1042. }
  1043. hdev = core->device;
  1044. if (core->ssr_type == SSR_SESSION_ABORT) {
  1045. struct msm_cvp_inst *inst = NULL, *s;
  1046. dprintk(CVP_ERR, "Session abort triggered\n");
  1047. list_for_each_entry(inst, &core->instances, list) {
  1048. dprintk(CVP_WARN,
  1049. "Session to abort: inst %#x ref %x\n",
  1050. inst, kref_read(&inst->kref));
  1051. break;
  1052. }
  1053. if (inst != NULL) {
  1054. s = cvp_get_inst_validate(inst->core, inst);
  1055. if (!s)
  1056. return;
  1057. print_hfi_queue_info(hdev);
  1058. msm_cvp_comm_kill_session(inst);
  1059. cvp_put_inst(s);
  1060. } else {
  1061. dprintk(CVP_WARN, "No active CVP session to abort\n");
  1062. }
  1063. return;
  1064. }
  1065. send_again:
  1066. mutex_lock(&core->lock);
  1067. if (core->state == CVP_CORE_INIT_DONE) {
  1068. dprintk(CVP_WARN, "%s: ssr type %d at %llu\n", __func__,
  1069. core->ssr_type, get_aon_time());
  1070. /*
  1071. * In current implementation user-initiated SSR triggers
  1072. * a fatal error from hardware. However, there is no way
  1073. * to know if fatal error is due to SSR or not. Handle
  1074. * user SSR as non-fatal.
  1075. */
  1076. core->trigger_ssr = true;
  1077. rc = call_hfi_op(hdev, core_trigger_ssr,
  1078. hdev->hfi_device_data, core->ssr_type);
  1079. if (rc) {
  1080. if (rc == -EAGAIN) {
  1081. core->trigger_ssr = false;
  1082. mutex_unlock(&core->lock);
  1083. usleep_range(500, 1000);
  1084. dprintk(CVP_WARN, "Retry ssr\n");
  1085. goto send_again;
  1086. }
  1087. dprintk(CVP_ERR, "%s: trigger_ssr failed\n",
  1088. __func__);
  1089. core->trigger_ssr = false;
  1090. }
  1091. } else {
  1092. dprintk(CVP_WARN, "%s: cvp core %pK not initialized\n",
  1093. __func__, core);
  1094. }
  1095. mutex_unlock(&core->lock);
  1096. }
  1097. void msm_cvp_comm_generate_sys_error(struct msm_cvp_inst *inst)
  1098. {
  1099. struct msm_cvp_core *core;
  1100. enum hal_command_response cmd = HAL_SYS_ERROR;
  1101. struct msm_cvp_cb_cmd_done response = {0};
  1102. if (!inst || !inst->core) {
  1103. dprintk(CVP_ERR, "%s: invalid input parameters\n", __func__);
  1104. return;
  1105. }
  1106. dprintk(CVP_WARN, "%s: inst %pK\n", __func__, inst);
  1107. core = inst->core;
  1108. handle_sys_error(cmd, (void *) &response);
  1109. }
  1110. int msm_cvp_comm_kill_session(struct msm_cvp_inst *inst)
  1111. {
  1112. int rc = 0;
  1113. unsigned long flags = 0;
  1114. if (!inst || !inst->core || !inst->core->device) {
  1115. dprintk(CVP_ERR, "%s: invalid input parameters\n", __func__);
  1116. return -EINVAL;
  1117. } else if (!inst->session) {
  1118. dprintk(CVP_ERR, "%s: no session to kill for inst %pK\n",
  1119. __func__, inst);
  1120. return 0;
  1121. }
  1122. dprintk(CVP_WARN, "%s: inst %pK, session %x state %d\n", __func__,
  1123. inst, hash32_ptr(inst->session), inst->state);
  1124. /*
  1125. * We're internally forcibly killing the session, if fw is aware of
  1126. * the session send session_abort to firmware to clean up and release
  1127. * the session, else just kill the session inside the driver.
  1128. */
  1129. if (inst->state >= MSM_CVP_OPEN_DONE &&
  1130. inst->state < MSM_CVP_CLOSE_DONE) {
  1131. rc = msm_comm_session_abort(inst);
  1132. if (rc) {
  1133. dprintk(CVP_ERR,
  1134. "%s: inst %pK session %x abort failed\n",
  1135. __func__, inst, hash32_ptr(inst->session));
  1136. change_cvp_inst_state(inst, MSM_CVP_CORE_INVALID);
  1137. } else {
  1138. change_cvp_inst_state(inst, MSM_CVP_CORE_UNINIT);
  1139. }
  1140. }
  1141. if (inst->state >= MSM_CVP_CORE_UNINIT) {
  1142. spin_lock_irqsave(&inst->event_handler.lock, flags);
  1143. inst->event_handler.event = CVP_SSR_EVENT;
  1144. spin_unlock_irqrestore(&inst->event_handler.lock, flags);
  1145. wake_up_all(&inst->event_handler.wq);
  1146. }
  1147. return rc;
  1148. }
  1149. static int set_internal_buf_on_fw(struct msm_cvp_inst *inst,
  1150. struct msm_cvp_smem *handle)
  1151. {
  1152. struct cvp_hfi_device *hdev;
  1153. int rc = 0;
  1154. u32 iova;
  1155. u32 size;
  1156. if (!inst || !inst->core || !inst->core->device || !handle) {
  1157. dprintk(CVP_ERR, "%s - invalid params\n", __func__);
  1158. return -EINVAL;
  1159. }
  1160. hdev = inst->core->device;
  1161. iova = handle->device_addr;
  1162. size = handle->size;
  1163. dprintk(CVP_SESS, "%s: allocated ARP buffer : %x\n", __func__, iova);
  1164. rc = call_hfi_op(hdev, session_set_buffers,
  1165. (void *) inst->session, iova, size);
  1166. if (rc) {
  1167. dprintk(CVP_ERR, "cvp_session_set_buffers failed\n");
  1168. return rc;
  1169. }
  1170. return 0;
  1171. }
  1172. /* Set ARP buffer for CVP firmware to handle concurrency */
  1173. int cvp_comm_set_arp_buffers(struct msm_cvp_inst *inst)
  1174. {
  1175. int rc = 0;
  1176. struct cvp_internal_buf *buf;
  1177. if (!inst || !inst->core || !inst->core->device) {
  1178. dprintk(CVP_ERR, "%s invalid parameters\n", __func__);
  1179. return -EINVAL;
  1180. }
  1181. buf = cvp_allocate_arp_bufs(inst, ARP_BUF_SIZE);
  1182. if (!buf) {
  1183. rc = -ENOMEM;
  1184. goto error;
  1185. }
  1186. rc = set_internal_buf_on_fw(inst, buf->smem);
  1187. if (rc)
  1188. goto error;
  1189. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_SET_BUFFER_DONE);
  1190. if (rc) {
  1191. dprintk(CVP_WARN, "wait for set_buffer_done timeout %d\n", rc);
  1192. goto error;
  1193. }
  1194. return rc;
  1195. error:
  1196. if (rc != -ENOMEM)
  1197. cvp_release_arp_buffers(inst);
  1198. return rc;
  1199. }
  1200. bool is_cvp_inst_valid(struct msm_cvp_inst *inst)
  1201. {
  1202. struct msm_cvp_core *core;
  1203. struct msm_cvp_inst *sess;
  1204. core = cvp_driver->cvp_core;
  1205. if (!core)
  1206. return false;
  1207. mutex_lock(&core->lock);
  1208. list_for_each_entry(sess, &core->instances, list) {
  1209. if (inst == sess) {
  1210. if (kref_read(&inst->kref)) {
  1211. mutex_unlock(&core->lock);
  1212. return true;
  1213. }
  1214. }
  1215. }
  1216. mutex_unlock(&core->lock);
  1217. return false;
  1218. }
  1219. int cvp_print_inst(u32 tag, struct msm_cvp_inst *inst)
  1220. {
  1221. if (!inst) {
  1222. dprintk(CVP_ERR, "%s invalid inst %pK\n", __func__, inst);
  1223. return -EINVAL;
  1224. }
  1225. dprintk(tag, "%s inst stype %d %pK id = %#x ptype %#x prio %#x secure %#x kmask %#x dmask %#x, kref %#x state %#x\n",
  1226. inst->proc_name, inst->session_type, inst, hash32_ptr(inst->session),
  1227. inst->prop.type, inst->prop.priority, inst->prop.is_secure,
  1228. inst->prop.kernel_mask, inst->prop.dsp_mask,
  1229. kref_read(&inst->kref), inst->state);
  1230. return 0;
  1231. }