msm_cvp_common.c 38 KB

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