msm_cvp_common.c 35 KB

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