msm_cvp_common.c 38 KB

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