msm_cvp_common.c 35 KB

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