msm_cvp_common.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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 cvp_handle_session_cmd_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, "%s: Invalid release_buf_done response\n",
  192. __func__);
  193. return;
  194. }
  195. inst = cvp_get_inst(get_cvp_core(response->device_id),
  196. response->session_id);
  197. if (!inst) {
  198. dprintk(CVP_ERR, "%s: Got response for an inactive session\n",
  199. __func__);
  200. return;
  201. }
  202. if (response->status)
  203. dprintk(CVP_ERR, "HFI MSG error %d cmd response %d\n",
  204. response->status, cmd);
  205. dprintk(CVP_SESS, "%s: inst=%pK\n", __func__, inst);
  206. if (IS_HAL_SESSION_CMD(cmd)) {
  207. dprintk(CVP_INFO, "%s: calling completion for index = %d",
  208. __func__, SESSION_MSG_INDEX(cmd));
  209. complete(&inst->completions[SESSION_MSG_INDEX(cmd)]);
  210. } else
  211. dprintk(CVP_ERR,
  212. "%s: Invalid inst cmd response: %d\n", __func__, cmd);
  213. cvp_put_inst(inst);
  214. }
  215. static void handle_session_set_buf_done(enum hal_command_response cmd,
  216. void *data)
  217. {
  218. struct msm_cvp_cb_cmd_done *response = data;
  219. struct msm_cvp_inst *inst;
  220. if (!response) {
  221. dprintk(CVP_ERR, "Invalid set_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, "set_buf_done has an inactive session\n");
  228. return;
  229. }
  230. if (response->status) {
  231. dprintk(CVP_ERR,
  232. "set ARP buffer error from FW : %#x\n",
  233. response->status);
  234. }
  235. if (IS_HAL_SESSION_CMD(cmd))
  236. complete(&inst->completions[SESSION_MSG_INDEX(cmd)]);
  237. else
  238. dprintk(CVP_ERR, "set_buf_done: invalid cmd: %d\n", cmd);
  239. cvp_put_inst(inst);
  240. }
  241. static void handle_session_release_buf_done(enum hal_command_response cmd,
  242. void *data)
  243. {
  244. struct msm_cvp_cb_cmd_done *response = data;
  245. struct msm_cvp_inst *inst;
  246. struct cvp_internal_buf *buf;
  247. struct list_head *ptr, *next;
  248. u32 buf_found = false;
  249. u32 address;
  250. if (!response) {
  251. dprintk(CVP_ERR, "Invalid release_buf_done response\n");
  252. return;
  253. }
  254. inst = cvp_get_inst(get_cvp_core(response->device_id),
  255. response->session_id);
  256. if (!inst) {
  257. dprintk(CVP_WARN,
  258. "%s: Got a response for an inactive session\n",
  259. __func__);
  260. return;
  261. }
  262. address = response->data.buffer_addr;
  263. mutex_lock(&inst->persistbufs.lock);
  264. list_for_each_safe(ptr, next, &inst->persistbufs.list) {
  265. buf = list_entry(ptr, struct cvp_internal_buf, list);
  266. if (address == buf->smem->device_addr + buf->offset) {
  267. dprintk(CVP_SESS, "releasing persist: %#x\n",
  268. buf->smem->device_addr);
  269. buf_found = true;
  270. }
  271. }
  272. mutex_unlock(&inst->persistbufs.lock);
  273. if (IS_HAL_SESSION_CMD(cmd))
  274. complete(&inst->completions[SESSION_MSG_INDEX(cmd)]);
  275. else
  276. dprintk(CVP_ERR, "Invalid inst cmd response: %d\n", cmd);
  277. cvp_put_inst(inst);
  278. }
  279. static void handle_sys_release_res_done(
  280. enum hal_command_response cmd, void *data)
  281. {
  282. struct msm_cvp_cb_cmd_done *response = data;
  283. struct msm_cvp_core *core;
  284. if (!response) {
  285. dprintk(CVP_ERR,
  286. "Failed to get valid response for sys init\n");
  287. return;
  288. }
  289. core = get_cvp_core(response->device_id);
  290. if (!core) {
  291. dprintk(CVP_ERR, "Wrong device_id received\n");
  292. return;
  293. }
  294. complete(&core->completions[
  295. SYS_MSG_INDEX(HAL_SYS_RELEASE_RESOURCE_DONE)]);
  296. }
  297. void change_cvp_inst_state(struct msm_cvp_inst *inst, enum instance_state state)
  298. {
  299. if (!inst) {
  300. dprintk(CVP_ERR, "Invalid parameter %s\n", __func__);
  301. return;
  302. }
  303. mutex_lock(&inst->lock);
  304. if (inst->state == MSM_CVP_CORE_INVALID) {
  305. dprintk(CVP_SESS,
  306. "Inst: %pK is in bad state can't change state to %d\n",
  307. inst, state);
  308. goto exit;
  309. }
  310. dprintk(CVP_SESS, "Moved inst: %pK from state: %d to state: %d\n",
  311. inst, inst->state, state);
  312. inst->state = state;
  313. exit:
  314. mutex_unlock(&inst->lock);
  315. }
  316. static int signal_session_msg_receipt(enum hal_command_response cmd,
  317. struct msm_cvp_inst *inst)
  318. {
  319. if (!inst) {
  320. dprintk(CVP_ERR, "Invalid(%pK) instance id\n", inst);
  321. return -EINVAL;
  322. }
  323. if (IS_HAL_SESSION_CMD(cmd)) {
  324. complete(&inst->completions[SESSION_MSG_INDEX(cmd)]);
  325. } else {
  326. dprintk(CVP_ERR, "Invalid inst cmd response: %d\n", cmd);
  327. return -EINVAL;
  328. }
  329. return 0;
  330. }
  331. int wait_for_sess_signal_receipt(struct msm_cvp_inst *inst,
  332. enum hal_command_response cmd)
  333. {
  334. int rc = 0;
  335. struct cvp_hfi_device *hdev;
  336. if (!IS_HAL_SESSION_CMD(cmd)) {
  337. dprintk(CVP_ERR, "Invalid inst cmd response: %d\n", cmd);
  338. return -EINVAL;
  339. }
  340. hdev = (struct cvp_hfi_device *)(inst->core->device);
  341. rc = wait_for_completion_timeout(
  342. &inst->completions[SESSION_MSG_INDEX(cmd)],
  343. msecs_to_jiffies(
  344. inst->core->resources.msm_cvp_hw_rsp_timeout));
  345. if (!rc) {
  346. dprintk(CVP_WARN, "Wait interrupted or timed out: %d\n",
  347. SESSION_MSG_INDEX(cmd));
  348. call_hfi_op(hdev, flush_debug_queue, hdev->hfi_device_data);
  349. dump_hfi_queue(hdev->hfi_device_data);
  350. rc = -EIO;
  351. } else if (inst->state == MSM_CVP_CORE_INVALID) {
  352. rc = -ECONNRESET;
  353. } else {
  354. rc = 0;
  355. }
  356. return rc;
  357. }
  358. static int wait_for_state(struct msm_cvp_inst *inst,
  359. enum instance_state flipped_state,
  360. enum instance_state desired_state,
  361. enum hal_command_response hal_cmd)
  362. {
  363. int rc = 0;
  364. if (IS_ALREADY_IN_STATE(flipped_state, desired_state)) {
  365. dprintk(CVP_INFO, "inst: %pK is already in state: %d\n",
  366. inst, inst->state);
  367. goto err_same_state;
  368. }
  369. dprintk(CVP_SESS, "Waiting for hal_cmd: %d\n", hal_cmd);
  370. rc = wait_for_sess_signal_receipt(inst, hal_cmd);
  371. if (!rc)
  372. change_cvp_inst_state(inst, desired_state);
  373. err_same_state:
  374. return rc;
  375. }
  376. void msm_cvp_notify_event(struct msm_cvp_inst *inst, int event_type)
  377. {
  378. }
  379. static void msm_cvp_comm_generate_max_clients_error(struct msm_cvp_inst *inst)
  380. {
  381. enum hal_command_response cmd = HAL_SESSION_ERROR;
  382. struct msm_cvp_cb_cmd_done response = {0};
  383. if (!inst) {
  384. dprintk(CVP_ERR, "%s: invalid input parameters\n", __func__);
  385. return;
  386. }
  387. dprintk(CVP_ERR, "%s: Too many clients\n", __func__);
  388. response.session_id = inst;
  389. response.status = CVP_ERR_MAX_CLIENTS;
  390. handle_session_error(cmd, (void *)&response);
  391. }
  392. static void handle_session_init_done(enum hal_command_response cmd, void *data)
  393. {
  394. struct msm_cvp_cb_cmd_done *response = data;
  395. struct msm_cvp_inst *inst = NULL;
  396. if (!response) {
  397. dprintk(CVP_ERR,
  398. "Failed to get valid response for session init\n");
  399. return;
  400. }
  401. inst = cvp_get_inst(get_cvp_core(response->device_id),
  402. response->session_id);
  403. if (!inst) {
  404. dprintk(CVP_WARN, "%s:Got a response for an inactive session\n",
  405. __func__);
  406. return;
  407. }
  408. if (response->status) {
  409. dprintk(CVP_ERR,
  410. "Session init response from FW : %#x\n",
  411. response->status);
  412. if (response->status == CVP_ERR_MAX_CLIENTS)
  413. msm_cvp_comm_generate_max_clients_error(inst);
  414. else
  415. msm_cvp_comm_generate_session_error(inst);
  416. signal_session_msg_receipt(cmd, inst);
  417. cvp_put_inst(inst);
  418. return;
  419. }
  420. dprintk(CVP_SESS, "%s: cvp session %#x\n", __func__,
  421. hash32_ptr(inst->session));
  422. signal_session_msg_receipt(cmd, inst);
  423. cvp_put_inst(inst);
  424. return;
  425. }
  426. static void handle_event_change(enum hal_command_response cmd, void *data)
  427. {
  428. dprintk(CVP_WARN, "%s is not supported on CVP!\n", __func__);
  429. }
  430. static void handle_release_res_done(enum hal_command_response cmd, void *data)
  431. {
  432. struct msm_cvp_cb_cmd_done *response = data;
  433. struct msm_cvp_inst *inst;
  434. if (!response) {
  435. dprintk(CVP_ERR,
  436. "Failed to get valid response for release resource\n");
  437. return;
  438. }
  439. inst = cvp_get_inst(get_cvp_core(response->device_id),
  440. response->session_id);
  441. if (!inst) {
  442. dprintk(CVP_WARN, "%s:Got a response for an inactive session\n",
  443. __func__);
  444. return;
  445. }
  446. signal_session_msg_receipt(cmd, inst);
  447. cvp_put_inst(inst);
  448. }
  449. static void handle_session_flush(enum hal_command_response cmd, void *data)
  450. {
  451. struct msm_cvp_cb_cmd_done *response = data;
  452. struct msm_cvp_inst *inst;
  453. if (!response) {
  454. dprintk(CVP_ERR,
  455. "Failed to get valid response for release resource\n");
  456. return;
  457. }
  458. inst = cvp_get_inst(get_cvp_core(response->device_id),
  459. response->session_id);
  460. if (!inst) {
  461. dprintk(CVP_WARN, "%s:Got a response for an inactive session\n",
  462. __func__);
  463. return;
  464. }
  465. signal_session_msg_receipt(cmd, inst);
  466. cvp_put_inst(inst);
  467. }
  468. static void handle_session_error(enum hal_command_response cmd, void *data)
  469. {
  470. struct msm_cvp_cb_cmd_done *response = data;
  471. struct cvp_hfi_device *hdev = NULL;
  472. struct msm_cvp_inst *inst = NULL;
  473. int event = CVP_SYS_ERROR_EVENT;
  474. if (!response) {
  475. dprintk(CVP_ERR,
  476. "Failed to get valid response for session error\n");
  477. return;
  478. }
  479. inst = cvp_get_inst(get_cvp_core(response->device_id),
  480. response->session_id);
  481. if (!inst) {
  482. dprintk(CVP_WARN, "%s: response for an inactive session\n",
  483. __func__);
  484. return;
  485. }
  486. hdev = inst->core->device;
  487. dprintk(CVP_ERR, "Session error received for inst %pK session %x\n",
  488. inst, hash32_ptr(inst->session));
  489. if (response->status == CVP_ERR_MAX_CLIENTS) {
  490. dprintk(CVP_WARN, "Too many clients, rejecting %pK", inst);
  491. event = CVP_MAX_CLIENTS_EVENT;
  492. /*
  493. * Clean the HFI session now. Since inst->state is moved to
  494. * INVALID, forward thread doesn't know FW has valid session
  495. * or not. This is the last place driver knows that there is
  496. * no session in FW. Hence clean HFI session now.
  497. */
  498. msm_cvp_comm_session_clean(inst);
  499. } else if (response->status == CVP_ERR_NOT_SUPPORTED) {
  500. dprintk(CVP_WARN, "Unsupported bitstream in %pK", inst);
  501. event = CVP_HW_UNSUPPORTED_EVENT;
  502. } else {
  503. dprintk(CVP_WARN, "Unknown session error (%d) for %pK\n",
  504. response->status, inst);
  505. event = CVP_SYS_ERROR_EVENT;
  506. }
  507. /* change state before sending error to client */
  508. change_cvp_inst_state(inst, MSM_CVP_CORE_INVALID);
  509. msm_cvp_notify_event(inst, event);
  510. cvp_put_inst(inst);
  511. }
  512. static void msm_comm_clean_notify_client(struct msm_cvp_core *core)
  513. {
  514. struct msm_cvp_inst *inst = NULL;
  515. if (!core) {
  516. dprintk(CVP_ERR, "%s: Invalid params\n", __func__);
  517. return;
  518. }
  519. dprintk(CVP_WARN, "%s: Core %pK\n", __func__, core);
  520. mutex_lock(&core->lock);
  521. list_for_each_entry(inst, &core->instances, list) {
  522. mutex_lock(&inst->lock);
  523. inst->state = MSM_CVP_CORE_INVALID;
  524. mutex_unlock(&inst->lock);
  525. dprintk(CVP_WARN,
  526. "%s Send sys error for inst %pK\n", __func__, inst);
  527. msm_cvp_notify_event(inst,
  528. CVP_SYS_ERROR_EVENT);
  529. }
  530. mutex_unlock(&core->lock);
  531. }
  532. static void handle_sys_error(enum hal_command_response cmd, void *data)
  533. {
  534. struct msm_cvp_cb_cmd_done *response = data;
  535. struct msm_cvp_core *core = NULL;
  536. struct cvp_hfi_device *hdev = NULL;
  537. struct msm_cvp_inst *inst = NULL;
  538. int i, rc = 0;
  539. unsigned long flags = 0;
  540. enum cvp_core_state cur_state;
  541. if (!response) {
  542. dprintk(CVP_ERR,
  543. "Failed to get valid response for sys error\n");
  544. return;
  545. }
  546. core = get_cvp_core(response->device_id);
  547. if (!core) {
  548. dprintk(CVP_ERR,
  549. "Got SYS_ERR but unable to identify core\n");
  550. return;
  551. }
  552. hdev = core->device;
  553. mutex_lock(&core->lock);
  554. if (core->state == CVP_CORE_UNINIT) {
  555. dprintk(CVP_ERR,
  556. "%s: Core %pK already moved to state %d\n",
  557. __func__, core, core->state);
  558. mutex_unlock(&core->lock);
  559. return;
  560. }
  561. cur_state = core->state;
  562. core->state = CVP_CORE_UNINIT;
  563. dprintk(CVP_WARN, "SYS_ERROR received for core %pK\n", core);
  564. msm_cvp_noc_error_info(core);
  565. call_hfi_op(hdev, flush_debug_queue, hdev->hfi_device_data);
  566. list_for_each_entry(inst, &core->instances, list) {
  567. dprintk(CVP_WARN,
  568. "%s: sys error inst %#x kref %x, cmd %x state %x\n",
  569. __func__, inst, kref_read(&inst->kref),
  570. inst->cur_cmd_type, inst->state);
  571. if (inst->state != MSM_CVP_CORE_INVALID) {
  572. change_cvp_inst_state(inst, MSM_CVP_CORE_INVALID);
  573. if (cvp_stop_clean_fence_queue(inst))
  574. dprintk(CVP_ERR, "Failed to clean fences\n");
  575. for (i = 0; i < ARRAY_SIZE(inst->completions); i++)
  576. complete(&inst->completions[i]);
  577. spin_lock_irqsave(&inst->event_handler.lock, flags);
  578. inst->event_handler.event = CVP_SSR_EVENT;
  579. spin_unlock_irqrestore(
  580. &inst->event_handler.lock, flags);
  581. wake_up_all(&inst->event_handler.wq);
  582. }
  583. if (!core->trigger_ssr)
  584. msm_cvp_print_inst_bufs(inst);
  585. }
  586. /* handle the hw error before core released to get full debug info */
  587. msm_cvp_handle_hw_error(core);
  588. if (response->status == CVP_ERR_NOC_ERROR) {
  589. dprintk(CVP_WARN, "Got NOC error");
  590. MSM_CVP_ERROR(true);
  591. }
  592. dprintk(CVP_CORE, "Calling core_release\n");
  593. rc = call_hfi_op(hdev, core_release, hdev->hfi_device_data);
  594. if (rc) {
  595. dprintk(CVP_ERR, "core_release failed\n");
  596. core->state = cur_state;
  597. mutex_unlock(&core->lock);
  598. return;
  599. }
  600. mutex_unlock(&core->lock);
  601. dprintk(CVP_WARN, "SYS_ERROR handled.\n");
  602. }
  603. void msm_cvp_comm_session_clean(struct msm_cvp_inst *inst)
  604. {
  605. int rc = 0;
  606. struct cvp_hfi_device *hdev = NULL;
  607. if (!inst || !inst->core || !inst->core->device) {
  608. dprintk(CVP_ERR, "%s invalid params\n", __func__);
  609. return;
  610. }
  611. if (!inst->session) {
  612. dprintk(CVP_SESS, "%s: inst %pK session already cleaned\n",
  613. __func__, inst);
  614. return;
  615. }
  616. hdev = inst->core->device;
  617. mutex_lock(&inst->lock);
  618. dprintk(CVP_SESS, "%s: inst %pK\n", __func__, inst);
  619. rc = call_hfi_op(hdev, session_clean,
  620. (void *)inst->session);
  621. if (rc) {
  622. dprintk(CVP_ERR,
  623. "Session clean failed :%pK\n", inst);
  624. }
  625. inst->session = NULL;
  626. mutex_unlock(&inst->lock);
  627. }
  628. static void handle_session_close(enum hal_command_response cmd, void *data)
  629. {
  630. struct msm_cvp_cb_cmd_done *response = data;
  631. struct msm_cvp_inst *inst;
  632. if (!response) {
  633. dprintk(CVP_ERR,
  634. "Failed to get valid response for session close\n");
  635. return;
  636. }
  637. inst = cvp_get_inst(get_cvp_core(response->device_id),
  638. response->session_id);
  639. if (!inst) {
  640. dprintk(CVP_WARN, "%s: response for an inactive session\n",
  641. __func__);
  642. return;
  643. }
  644. signal_session_msg_receipt(cmd, inst);
  645. show_stats(inst);
  646. cvp_put_inst(inst);
  647. }
  648. void cvp_handle_cmd_response(enum hal_command_response cmd, void *data)
  649. {
  650. dprintk(CVP_HFI, "Command response = %d\n", cmd);
  651. switch (cmd) {
  652. case HAL_SYS_INIT_DONE:
  653. handle_sys_init_done(cmd, data);
  654. break;
  655. case HAL_SYS_RELEASE_RESOURCE_DONE:
  656. handle_sys_release_res_done(cmd, data);
  657. break;
  658. case HAL_SESSION_INIT_DONE:
  659. handle_session_init_done(cmd, data);
  660. break;
  661. case HAL_SESSION_RELEASE_RESOURCE_DONE:
  662. handle_release_res_done(cmd, data);
  663. break;
  664. case HAL_SESSION_END_DONE:
  665. case HAL_SESSION_ABORT_DONE:
  666. handle_session_close(cmd, data);
  667. break;
  668. case HAL_SESSION_EVENT_CHANGE:
  669. handle_event_change(cmd, data);
  670. break;
  671. case HAL_SESSION_FLUSH_DONE:
  672. handle_session_flush(cmd, data);
  673. break;
  674. case HAL_SYS_WATCHDOG_TIMEOUT:
  675. case HAL_SYS_ERROR:
  676. handle_sys_error(cmd, data);
  677. break;
  678. case HAL_SESSION_ERROR:
  679. handle_session_error(cmd, data);
  680. break;
  681. case HAL_SESSION_SET_BUFFER_DONE:
  682. handle_session_set_buf_done(cmd, data);
  683. break;
  684. case HAL_SESSION_RELEASE_BUFFER_DONE:
  685. handle_session_release_buf_done(cmd, data);
  686. break;
  687. case HAL_SESSION_SGM_OF_CONFIG_CMD_DONE:
  688. case HAL_SESSION_DFS_CONFIG_CMD_DONE:
  689. case HAL_SESSION_DMM_CONFIG_CMD_DONE:
  690. case HAL_SESSION_DMM_PARAMS_CMD_DONE:
  691. case HAL_SESSION_WARP_CONFIG_CMD_DONE:
  692. case HAL_SESSION_WARP_DS_PARAMS_CMD_DONE:
  693. case HAL_SESSION_WARP_NCC_CONFIG_CMD_DONE:
  694. case HAL_SESSION_PERSIST_SET_DONE:
  695. case HAL_SESSION_PERSIST_REL_DONE:
  696. case HAL_SESSION_TME_CONFIG_CMD_DONE:
  697. case HAL_SESSION_ODT_CONFIG_CMD_DONE:
  698. case HAL_SESSION_OD_CONFIG_CMD_DONE:
  699. case HAL_SESSION_NCC_CONFIG_CMD_DONE:
  700. case HAL_SESSION_ICA_CONFIG_CMD_DONE:
  701. case HAL_SESSION_HCD_CONFIG_CMD_DONE:
  702. case HAL_SESSION_DCM_CONFIG_CMD_DONE:
  703. case HAL_SESSION_DC_CONFIG_CMD_DONE:
  704. case HAL_SESSION_PYS_HCD_CONFIG_CMD_DONE:
  705. case HAL_SESSION_FD_CONFIG_CMD_DONE:
  706. case HAL_SESSION_MODEL_BUF_CMD_DONE:
  707. cvp_handle_session_cmd_done(cmd, data);
  708. break;
  709. default:
  710. dprintk(CVP_HFI, "response unhandled: %d\n", cmd);
  711. break;
  712. }
  713. }
  714. static inline enum msm_cvp_thermal_level msm_comm_cvp_thermal_level(int level)
  715. {
  716. switch (level) {
  717. case 0:
  718. return CVP_THERMAL_NORMAL;
  719. case 1:
  720. return CVP_THERMAL_LOW;
  721. case 2:
  722. return CVP_THERMAL_HIGH;
  723. default:
  724. return CVP_THERMAL_CRITICAL;
  725. }
  726. }
  727. static bool is_core_turbo(struct msm_cvp_core *core, unsigned long freq)
  728. {
  729. int i = 0;
  730. struct allowed_clock_rates_table *allowed_clks_tbl = NULL;
  731. u32 max_freq = 0;
  732. allowed_clks_tbl = core->resources.allowed_clks_tbl;
  733. for (i = 0; i < core->resources.allowed_clks_tbl_size; i++) {
  734. if (max_freq < allowed_clks_tbl[i].clock_rate)
  735. max_freq = allowed_clks_tbl[i].clock_rate;
  736. }
  737. return freq >= max_freq;
  738. }
  739. static bool is_thermal_permissible(struct msm_cvp_core *core)
  740. {
  741. enum msm_cvp_thermal_level tl;
  742. unsigned long freq = 0;
  743. bool is_turbo = false;
  744. if (!core->resources.thermal_mitigable)
  745. return true;
  746. if (msm_cvp_thermal_mitigation_disabled) {
  747. dprintk(CVP_CORE,
  748. "Thermal mitigation not enabled. debugfs %d\n",
  749. msm_cvp_thermal_mitigation_disabled);
  750. return true;
  751. }
  752. tl = msm_comm_cvp_thermal_level(cvp_driver->thermal_level);
  753. freq = core->curr_freq;
  754. is_turbo = is_core_turbo(core, freq);
  755. dprintk(CVP_CORE,
  756. "Core freq %ld Thermal level %d Turbo mode %d\n",
  757. freq, tl, is_turbo);
  758. if (is_turbo && tl >= CVP_THERMAL_LOW) {
  759. dprintk(CVP_ERR,
  760. "CVP session not allowed. Turbo mode %d Thermal level %d\n",
  761. is_turbo, tl);
  762. return false;
  763. }
  764. return true;
  765. }
  766. static int msm_comm_session_abort(struct msm_cvp_inst *inst)
  767. {
  768. int rc = 0, abort_completion = 0;
  769. struct cvp_hfi_device *hdev;
  770. if (!inst || !inst->core || !inst->core->device) {
  771. dprintk(CVP_ERR, "%s invalid params\n", __func__);
  772. return -EINVAL;
  773. }
  774. hdev = inst->core->device;
  775. abort_completion = SESSION_MSG_INDEX(HAL_SESSION_ABORT_DONE);
  776. dprintk(CVP_WARN, "%s: inst %pK session %x\n", __func__,
  777. inst, hash32_ptr(inst->session));
  778. rc = call_hfi_op(hdev, session_abort, (void *)inst->session);
  779. if (rc) {
  780. dprintk(CVP_ERR,
  781. "%s session_abort failed rc: %d\n", __func__, rc);
  782. goto exit;
  783. }
  784. rc = wait_for_completion_timeout(
  785. &inst->completions[abort_completion],
  786. msecs_to_jiffies(
  787. inst->core->resources.msm_cvp_hw_rsp_timeout));
  788. if (!rc) {
  789. dprintk(CVP_ERR, "%s: inst %pK session %x abort timed out\n",
  790. __func__, inst, hash32_ptr(inst->session));
  791. call_hfi_op(hdev, flush_debug_queue, hdev->hfi_device_data);
  792. dump_hfi_queue(hdev->hfi_device_data);
  793. msm_cvp_comm_generate_sys_error(inst);
  794. rc = -EBUSY;
  795. } else {
  796. rc = 0;
  797. }
  798. exit:
  799. return rc;
  800. }
  801. static void handle_thermal_event(struct msm_cvp_core *core)
  802. {
  803. int rc = 0;
  804. struct msm_cvp_inst *inst;
  805. if (!core || !core->device) {
  806. dprintk(CVP_ERR, "%s Invalid params\n", __func__);
  807. return;
  808. }
  809. mutex_lock(&core->lock);
  810. list_for_each_entry(inst, &core->instances, list) {
  811. if (!inst->session)
  812. continue;
  813. mutex_unlock(&core->lock);
  814. if (inst->state >= MSM_CVP_OPEN_DONE &&
  815. inst->state < MSM_CVP_CLOSE_DONE) {
  816. dprintk(CVP_WARN, "%s: abort inst %pK\n",
  817. __func__, inst);
  818. rc = msm_comm_session_abort(inst);
  819. if (rc) {
  820. dprintk(CVP_ERR,
  821. "%s session_abort failed rc: %d\n",
  822. __func__, rc);
  823. goto err_sess_abort;
  824. }
  825. change_cvp_inst_state(inst, MSM_CVP_CORE_INVALID);
  826. dprintk(CVP_WARN,
  827. "%s Send sys error for inst %pK\n",
  828. __func__, inst);
  829. msm_cvp_notify_event(inst,
  830. CVP_SYS_ERROR_EVENT);
  831. } else {
  832. msm_cvp_comm_generate_session_error(inst);
  833. }
  834. mutex_lock(&core->lock);
  835. }
  836. mutex_unlock(&core->lock);
  837. return;
  838. err_sess_abort:
  839. msm_comm_clean_notify_client(core);
  840. }
  841. void msm_cvp_comm_handle_thermal_event(void)
  842. {
  843. struct msm_cvp_core *core;
  844. list_for_each_entry(core, &cvp_driver->cores, list) {
  845. if (!is_thermal_permissible(core)) {
  846. dprintk(CVP_WARN,
  847. "Thermal level critical, stop all active sessions!\n");
  848. handle_thermal_event(core);
  849. }
  850. }
  851. }
  852. int msm_cvp_comm_check_core_init(struct msm_cvp_core *core)
  853. {
  854. int rc = 0;
  855. struct cvp_hfi_device *hdev;
  856. mutex_lock(&core->lock);
  857. if (core->state >= CVP_CORE_INIT_DONE) {
  858. dprintk(CVP_INFO, "CVP core: %d is already in state: %d\n",
  859. core->id, core->state);
  860. goto exit;
  861. }
  862. dprintk(CVP_CORE, "Waiting for SYS_INIT_DONE\n");
  863. rc = wait_for_completion_timeout(
  864. &core->completions[SYS_MSG_INDEX(HAL_SYS_INIT_DONE)],
  865. msecs_to_jiffies(core->resources.msm_cvp_hw_rsp_timeout));
  866. if (!rc) {
  867. dprintk(CVP_ERR, "%s: Wait interrupted or timed out: %d\n",
  868. __func__, SYS_MSG_INDEX(HAL_SYS_INIT_DONE));
  869. hdev = core->device;
  870. call_hfi_op(hdev, flush_debug_queue, hdev->hfi_device_data);
  871. dump_hfi_queue(hdev->hfi_device_data);
  872. rc = -EIO;
  873. goto exit;
  874. } else {
  875. core->state = CVP_CORE_INIT_DONE;
  876. rc = 0;
  877. }
  878. dprintk(CVP_CORE, "SYS_INIT_DONE!!!\n");
  879. exit:
  880. mutex_unlock(&core->lock);
  881. return rc;
  882. }
  883. static int msm_comm_init_core_done(struct msm_cvp_inst *inst)
  884. {
  885. int rc = 0;
  886. rc = msm_cvp_comm_check_core_init(inst->core);
  887. if (rc) {
  888. dprintk(CVP_ERR, "%s - failed to initialize core\n", __func__);
  889. msm_cvp_comm_generate_sys_error(inst);
  890. return rc;
  891. }
  892. change_cvp_inst_state(inst, MSM_CVP_CORE_INIT_DONE);
  893. return rc;
  894. }
  895. static int msm_comm_init_core(struct msm_cvp_inst *inst)
  896. {
  897. int rc = 0;
  898. struct cvp_hfi_device *hdev;
  899. struct msm_cvp_core *core;
  900. if (!inst || !inst->core || !inst->core->device)
  901. return -EINVAL;
  902. core = inst->core;
  903. hdev = core->device;
  904. mutex_lock(&core->lock);
  905. if (core->state >= CVP_CORE_INIT) {
  906. dprintk(CVP_CORE, "CVP core: %d is already in state: %d\n",
  907. core->id, core->state);
  908. goto core_already_inited;
  909. }
  910. if (!core->capabilities) {
  911. core->capabilities = kcalloc(CVP_MAX_SESSIONS,
  912. sizeof(struct msm_cvp_capability), GFP_KERNEL);
  913. if (!core->capabilities) {
  914. dprintk(CVP_ERR,
  915. "%s: failed to allocate capabilities\n",
  916. __func__);
  917. rc = -ENOMEM;
  918. goto fail_cap_alloc;
  919. }
  920. } else {
  921. dprintk(CVP_WARN,
  922. "%s: capabilities memory is expected to be freed\n",
  923. __func__);
  924. }
  925. dprintk(CVP_CORE, "%s: core %pK\n", __func__, core);
  926. rc = call_hfi_op(hdev, core_init, hdev->hfi_device_data);
  927. if (rc) {
  928. dprintk(CVP_ERR, "Failed to init core, id = %d\n",
  929. core->id);
  930. goto fail_core_init;
  931. }
  932. core->state = CVP_CORE_INIT;
  933. core->smmu_fault_handled = false;
  934. core->trigger_ssr = false;
  935. core_already_inited:
  936. change_cvp_inst_state(inst, MSM_CVP_CORE_INIT);
  937. mutex_unlock(&core->lock);
  938. return rc;
  939. fail_core_init:
  940. kfree(core->capabilities);
  941. fail_cap_alloc:
  942. core->capabilities = NULL;
  943. core->state = CVP_CORE_UNINIT;
  944. mutex_unlock(&core->lock);
  945. return rc;
  946. }
  947. int msm_cvp_deinit_core(struct msm_cvp_inst *inst)
  948. {
  949. struct msm_cvp_core *core;
  950. struct cvp_hfi_device *hdev;
  951. if (!inst || !inst->core || !inst->core->device) {
  952. dprintk(CVP_ERR, "%s invalid parameters\n", __func__);
  953. return -EINVAL;
  954. }
  955. core = inst->core;
  956. hdev = core->device;
  957. mutex_lock(&core->lock);
  958. if (core->state == CVP_CORE_UNINIT) {
  959. dprintk(CVP_INFO, "CVP core: %d is already in state: %d\n",
  960. core->id, core->state);
  961. goto core_already_uninited;
  962. }
  963. if (!core->resources.never_unload_fw) {
  964. cancel_delayed_work(&core->fw_unload_work);
  965. /*
  966. * Delay unloading of firmware. This is useful
  967. * in avoiding firmware download delays in cases where we
  968. * will have a burst of back to back cvp sessions
  969. */
  970. schedule_delayed_work(&core->fw_unload_work,
  971. msecs_to_jiffies(core->state == CVP_CORE_INIT_DONE ?
  972. core->resources.msm_cvp_firmware_unload_delay : 0));
  973. dprintk(CVP_CORE, "firmware unload delayed by %u ms\n",
  974. core->state == CVP_CORE_INIT_DONE ?
  975. core->resources.msm_cvp_firmware_unload_delay : 0);
  976. }
  977. core_already_uninited:
  978. change_cvp_inst_state(inst, MSM_CVP_CORE_UNINIT);
  979. mutex_unlock(&core->lock);
  980. return 0;
  981. }
  982. static int msm_comm_session_init_done(int flipped_state,
  983. struct msm_cvp_inst *inst)
  984. {
  985. int rc;
  986. dprintk(CVP_SESS, "inst %pK: waiting for session init done\n", inst);
  987. rc = wait_for_state(inst, flipped_state, MSM_CVP_OPEN_DONE,
  988. HAL_SESSION_INIT_DONE);
  989. if (rc) {
  990. dprintk(CVP_ERR, "Session init failed for inst %pK\n", inst);
  991. msm_cvp_comm_generate_sys_error(inst);
  992. return rc;
  993. }
  994. return rc;
  995. }
  996. static int msm_comm_session_init(int flipped_state,
  997. struct msm_cvp_inst *inst)
  998. {
  999. int rc = 0;
  1000. struct cvp_hfi_device *hdev;
  1001. if (!inst || !inst->core || !inst->core->device) {
  1002. dprintk(CVP_ERR, "%s invalid parameters\n", __func__);
  1003. return -EINVAL;
  1004. }
  1005. hdev = inst->core->device;
  1006. if (IS_ALREADY_IN_STATE(flipped_state, MSM_CVP_OPEN)) {
  1007. dprintk(CVP_INFO, "inst: %pK is already in state: %d\n",
  1008. inst, inst->state);
  1009. goto exit;
  1010. }
  1011. dprintk(CVP_SESS, "%s: inst %pK\n", __func__, inst);
  1012. rc = call_hfi_op(hdev, session_init, hdev->hfi_device_data,
  1013. inst, &inst->session);
  1014. if (rc || !inst->session) {
  1015. dprintk(CVP_ERR,
  1016. "Failed to call session init for: %pK, %pK, %d\n",
  1017. inst->core->device, inst, inst->session_type);
  1018. rc = -EINVAL;
  1019. goto exit;
  1020. }
  1021. change_cvp_inst_state(inst, MSM_CVP_OPEN);
  1022. exit:
  1023. return rc;
  1024. }
  1025. static int msm_comm_session_close(int flipped_state,
  1026. struct msm_cvp_inst *inst)
  1027. {
  1028. int rc = 0;
  1029. struct cvp_hfi_device *hdev;
  1030. if (!inst || !inst->core || !inst->core->device) {
  1031. dprintk(CVP_ERR, "%s invalid params\n", __func__);
  1032. return -EINVAL;
  1033. }
  1034. if (IS_ALREADY_IN_STATE(flipped_state, MSM_CVP_CLOSE)) {
  1035. dprintk(CVP_INFO,
  1036. "inst: %pK is already in state: %d\n",
  1037. inst, inst->state);
  1038. goto exit;
  1039. }
  1040. hdev = inst->core->device;
  1041. dprintk(CVP_SESS, "%s: inst %pK\n", __func__, inst);
  1042. rc = call_hfi_op(hdev, session_end, (void *) inst->session);
  1043. if (rc) {
  1044. dprintk(CVP_ERR,
  1045. "Failed to send close\n");
  1046. goto exit;
  1047. }
  1048. change_cvp_inst_state(inst, MSM_CVP_CLOSE);
  1049. exit:
  1050. return rc;
  1051. }
  1052. int msm_cvp_comm_suspend(int core_id)
  1053. {
  1054. struct cvp_hfi_device *hdev;
  1055. struct msm_cvp_core *core;
  1056. int rc = 0;
  1057. core = get_cvp_core(core_id);
  1058. if (!core) {
  1059. dprintk(CVP_ERR,
  1060. "%s: Failed to find core for core_id = %d\n",
  1061. __func__, core_id);
  1062. return -EINVAL;
  1063. }
  1064. hdev = (struct cvp_hfi_device *)core->device;
  1065. if (!hdev) {
  1066. dprintk(CVP_ERR, "%s Invalid device handle\n", __func__);
  1067. return -EINVAL;
  1068. }
  1069. rc = call_hfi_op(hdev, suspend, hdev->hfi_device_data);
  1070. if (rc)
  1071. dprintk(CVP_WARN, "Failed to suspend\n");
  1072. return rc;
  1073. }
  1074. static int get_flipped_state(int present_state, int desired_state)
  1075. {
  1076. int flipped_state = present_state;
  1077. if (flipped_state < MSM_CVP_CLOSE && desired_state > MSM_CVP_CLOSE) {
  1078. flipped_state = MSM_CVP_CLOSE + (MSM_CVP_CLOSE - flipped_state);
  1079. flipped_state &= 0xFFFE;
  1080. flipped_state = flipped_state - 1;
  1081. } else if (flipped_state > MSM_CVP_CLOSE
  1082. && desired_state < MSM_CVP_CLOSE) {
  1083. flipped_state = MSM_CVP_CLOSE -
  1084. (flipped_state - MSM_CVP_CLOSE + 1);
  1085. flipped_state &= 0xFFFE;
  1086. flipped_state = flipped_state - 1;
  1087. }
  1088. return flipped_state;
  1089. }
  1090. int msm_cvp_comm_try_state(struct msm_cvp_inst *inst, int state)
  1091. {
  1092. int rc = 0;
  1093. int flipped_state;
  1094. if (!inst) {
  1095. dprintk(CVP_ERR, "%s: invalid params %pK", __func__, inst);
  1096. return -EINVAL;
  1097. }
  1098. dprintk(CVP_SESS,
  1099. "Trying to move inst: %pK (%#x) from: %#x to %#x\n",
  1100. inst, hash32_ptr(inst->session), inst->state, state);
  1101. mutex_lock(&inst->sync_lock);
  1102. if (inst->state == MSM_CVP_CORE_INVALID) {
  1103. dprintk(CVP_ERR, "%s: inst %pK is in invalid\n",
  1104. __func__, inst);
  1105. mutex_unlock(&inst->sync_lock);
  1106. return -EINVAL;
  1107. }
  1108. flipped_state = get_flipped_state(inst->state, state);
  1109. dprintk(CVP_SESS,
  1110. "inst: %pK (%#x) flipped_state = %#x %x\n",
  1111. inst, hash32_ptr(inst->session), flipped_state, state);
  1112. switch (flipped_state) {
  1113. case MSM_CVP_CORE_UNINIT_DONE:
  1114. case MSM_CVP_CORE_INIT:
  1115. rc = msm_comm_init_core(inst);
  1116. if (rc || state <= get_flipped_state(inst->state, state))
  1117. break;
  1118. case MSM_CVP_CORE_INIT_DONE:
  1119. rc = msm_comm_init_core_done(inst);
  1120. if (rc || state <= get_flipped_state(inst->state, state))
  1121. break;
  1122. case MSM_CVP_OPEN:
  1123. rc = msm_comm_session_init(flipped_state, inst);
  1124. if (rc || state <= get_flipped_state(inst->state, state))
  1125. break;
  1126. case MSM_CVP_OPEN_DONE:
  1127. rc = msm_comm_session_init_done(flipped_state, inst);
  1128. if (rc || state <= get_flipped_state(inst->state, state))
  1129. break;
  1130. case MSM_CVP_CLOSE:
  1131. dprintk(CVP_INFO, "to CVP_CLOSE state\n");
  1132. rc = msm_comm_session_close(flipped_state, inst);
  1133. if (rc || state <= get_flipped_state(inst->state, state))
  1134. break;
  1135. case MSM_CVP_CLOSE_DONE:
  1136. dprintk(CVP_INFO, "to CVP_CLOSE_DONE state\n");
  1137. rc = wait_for_state(inst, flipped_state, MSM_CVP_CLOSE_DONE,
  1138. HAL_SESSION_END_DONE);
  1139. if (rc || state <= get_flipped_state(inst->state, state))
  1140. break;
  1141. msm_cvp_comm_session_clean(inst);
  1142. case MSM_CVP_CORE_UNINIT:
  1143. case MSM_CVP_CORE_INVALID:
  1144. dprintk(CVP_INFO, "Sending core uninit\n");
  1145. rc = msm_cvp_deinit_core(inst);
  1146. if (rc || state <= get_flipped_state(inst->state, state))
  1147. break;
  1148. default:
  1149. dprintk(CVP_ERR, "State not recognized\n");
  1150. rc = -EINVAL;
  1151. break;
  1152. }
  1153. mutex_unlock(&inst->sync_lock);
  1154. if (rc) {
  1155. dprintk(CVP_ERR,
  1156. "Failed to move from state: %d to %d\n",
  1157. inst->state, state);
  1158. msm_cvp_comm_kill_session(inst);
  1159. }
  1160. return rc;
  1161. }
  1162. int msm_cvp_noc_error_info(struct msm_cvp_core *core)
  1163. {
  1164. struct cvp_hfi_device *hdev;
  1165. if (!core || !core->device) {
  1166. dprintk(CVP_WARN, "%s: Invalid parameters: %pK\n",
  1167. __func__, core);
  1168. return -EINVAL;
  1169. }
  1170. if (!core->resources.non_fatal_pagefaults)
  1171. return 0;
  1172. if (!core->smmu_fault_handled)
  1173. return 0;
  1174. hdev = core->device;
  1175. call_hfi_op(hdev, noc_error_info, hdev->hfi_device_data);
  1176. return 0;
  1177. }
  1178. int msm_cvp_trigger_ssr(struct msm_cvp_core *core,
  1179. enum hal_ssr_trigger_type type)
  1180. {
  1181. if (!core) {
  1182. dprintk(CVP_WARN, "%s: Invalid parameters\n", __func__);
  1183. return -EINVAL;
  1184. }
  1185. core->ssr_type = type;
  1186. schedule_work(&core->ssr_work);
  1187. return 0;
  1188. }
  1189. void msm_cvp_ssr_handler(struct work_struct *work)
  1190. {
  1191. int rc;
  1192. struct msm_cvp_core *core;
  1193. struct cvp_hfi_device *hdev;
  1194. core = container_of(work, struct msm_cvp_core, ssr_work);
  1195. if (!core || !core->device) {
  1196. dprintk(CVP_ERR, "%s: Invalid params\n", __func__);
  1197. return;
  1198. }
  1199. hdev = core->device;
  1200. if (core->ssr_type == SSR_SESSION_ABORT) {
  1201. struct msm_cvp_inst *inst = NULL, *s;
  1202. dprintk(CVP_ERR, "Session abort triggered\n");
  1203. list_for_each_entry(inst, &core->instances, list) {
  1204. dprintk(CVP_WARN,
  1205. "Session to abort: inst %#x cmd %x ref %x\n",
  1206. inst, inst->cur_cmd_type,
  1207. kref_read(&inst->kref));
  1208. break;
  1209. }
  1210. if (inst != NULL) {
  1211. s = cvp_get_inst_validate(inst->core, inst);
  1212. if (!s)
  1213. return;
  1214. call_hfi_op(hdev, flush_debug_queue,
  1215. hdev->hfi_device_data);
  1216. dump_hfi_queue(hdev->hfi_device_data);
  1217. msm_cvp_comm_kill_session(inst);
  1218. cvp_put_inst(s);
  1219. } else {
  1220. dprintk(CVP_WARN, "No active CVP session to abort\n");
  1221. }
  1222. return;
  1223. }
  1224. send_again:
  1225. mutex_lock(&core->lock);
  1226. if (core->state == CVP_CORE_INIT_DONE) {
  1227. dprintk(CVP_WARN, "%s: ssr type %d\n", __func__,
  1228. core->ssr_type);
  1229. /*
  1230. * In current implementation user-initiated SSR triggers
  1231. * a fatal error from hardware. However, there is no way
  1232. * to know if fatal error is due to SSR or not. Handle
  1233. * user SSR as non-fatal.
  1234. */
  1235. core->trigger_ssr = true;
  1236. rc = call_hfi_op(hdev, core_trigger_ssr,
  1237. hdev->hfi_device_data, core->ssr_type);
  1238. if (rc) {
  1239. if (rc == -EAGAIN) {
  1240. core->trigger_ssr = false;
  1241. mutex_unlock(&core->lock);
  1242. usleep_range(500, 1000);
  1243. dprintk(CVP_WARN, "Retry ssr\n");
  1244. goto send_again;
  1245. }
  1246. dprintk(CVP_ERR, "%s: trigger_ssr failed\n",
  1247. __func__);
  1248. core->trigger_ssr = false;
  1249. }
  1250. } else {
  1251. dprintk(CVP_WARN, "%s: cvp core %pK not initialized\n",
  1252. __func__, core);
  1253. }
  1254. mutex_unlock(&core->lock);
  1255. }
  1256. void msm_cvp_comm_generate_session_error(struct msm_cvp_inst *inst)
  1257. {
  1258. enum hal_command_response cmd = HAL_SESSION_ERROR;
  1259. struct msm_cvp_cb_cmd_done response = {0};
  1260. if (!inst || !inst->core) {
  1261. dprintk(CVP_ERR, "%s: invalid input parameters\n", __func__);
  1262. return;
  1263. }
  1264. dprintk(CVP_WARN, "%s: inst %pK\n", __func__, inst);
  1265. response.session_id = inst;
  1266. response.status = CVP_ERR_FAIL;
  1267. handle_session_error(cmd, (void *)&response);
  1268. }
  1269. void msm_cvp_comm_generate_sys_error(struct msm_cvp_inst *inst)
  1270. {
  1271. struct msm_cvp_core *core;
  1272. enum hal_command_response cmd = HAL_SYS_ERROR;
  1273. struct msm_cvp_cb_cmd_done response = {0};
  1274. if (!inst || !inst->core) {
  1275. dprintk(CVP_ERR, "%s: invalid input parameters\n", __func__);
  1276. return;
  1277. }
  1278. dprintk(CVP_WARN, "%s: inst %pK\n", __func__, inst);
  1279. core = inst->core;
  1280. response.device_id = (u32) core->id;
  1281. handle_sys_error(cmd, (void *) &response);
  1282. }
  1283. int msm_cvp_comm_kill_session(struct msm_cvp_inst *inst)
  1284. {
  1285. int rc = 0;
  1286. unsigned long flags = 0;
  1287. if (!inst || !inst->core || !inst->core->device) {
  1288. dprintk(CVP_ERR, "%s: invalid input parameters\n", __func__);
  1289. return -EINVAL;
  1290. } else if (!inst->session) {
  1291. dprintk(CVP_ERR, "%s: no session to kill for inst %pK\n",
  1292. __func__, inst);
  1293. return 0;
  1294. }
  1295. dprintk(CVP_WARN, "%s: inst %pK, session %x state %d\n", __func__,
  1296. inst, hash32_ptr(inst->session), inst->state);
  1297. /*
  1298. * We're internally forcibly killing the session, if fw is aware of
  1299. * the session send session_abort to firmware to clean up and release
  1300. * the session, else just kill the session inside the driver.
  1301. */
  1302. if (inst->state >= MSM_CVP_OPEN_DONE &&
  1303. inst->state < MSM_CVP_CLOSE_DONE) {
  1304. rc = msm_comm_session_abort(inst);
  1305. if (rc) {
  1306. dprintk(CVP_ERR,
  1307. "%s: inst %pK session %x abort failed\n",
  1308. __func__, inst, hash32_ptr(inst->session));
  1309. change_cvp_inst_state(inst, MSM_CVP_CORE_INVALID);
  1310. } else {
  1311. change_cvp_inst_state(inst, MSM_CVP_CORE_UNINIT);
  1312. }
  1313. }
  1314. if (inst->state >= MSM_CVP_CORE_UNINIT) {
  1315. spin_lock_irqsave(&inst->event_handler.lock, flags);
  1316. inst->event_handler.event = CVP_SSR_EVENT;
  1317. spin_unlock_irqrestore(&inst->event_handler.lock, flags);
  1318. wake_up_all(&inst->event_handler.wq);
  1319. }
  1320. return rc;
  1321. }
  1322. void msm_cvp_fw_unload_handler(struct work_struct *work)
  1323. {
  1324. struct msm_cvp_core *core = NULL;
  1325. struct cvp_hfi_device *hdev = NULL;
  1326. int rc = 0;
  1327. core = container_of(work, struct msm_cvp_core, fw_unload_work.work);
  1328. if (!core || !core->device) {
  1329. dprintk(CVP_ERR, "%s - invalid work or core handle\n",
  1330. __func__);
  1331. return;
  1332. }
  1333. hdev = core->device;
  1334. mutex_lock(&core->lock);
  1335. if (list_empty(&core->instances) &&
  1336. core->state != CVP_CORE_UNINIT) {
  1337. if (core->state > CVP_CORE_INIT) {
  1338. dprintk(CVP_CORE, "Calling cvp_hal_core_release\n");
  1339. rc = call_hfi_op(hdev, core_release,
  1340. hdev->hfi_device_data);
  1341. if (rc) {
  1342. dprintk(CVP_ERR,
  1343. "Failed to release core, id = %d\n",
  1344. core->id);
  1345. mutex_unlock(&core->lock);
  1346. return;
  1347. }
  1348. }
  1349. core->state = CVP_CORE_UNINIT;
  1350. kfree(core->capabilities);
  1351. core->capabilities = NULL;
  1352. }
  1353. mutex_unlock(&core->lock);
  1354. }
  1355. static int set_internal_buf_on_fw(struct msm_cvp_inst *inst,
  1356. struct msm_cvp_smem *handle)
  1357. {
  1358. struct cvp_hfi_device *hdev;
  1359. int rc = 0;
  1360. u32 iova;
  1361. u32 size;
  1362. if (!inst || !inst->core || !inst->core->device || !handle) {
  1363. dprintk(CVP_ERR, "%s - invalid params\n", __func__);
  1364. return -EINVAL;
  1365. }
  1366. hdev = inst->core->device;
  1367. iova = handle->device_addr;
  1368. size = handle->size;
  1369. dprintk(CVP_SESS, "%s: allocated ARP buffer : %x\n", __func__, iova);
  1370. rc = call_hfi_op(hdev, session_set_buffers,
  1371. (void *) inst->session, iova, size);
  1372. if (rc) {
  1373. dprintk(CVP_ERR, "cvp_session_set_buffers failed\n");
  1374. return rc;
  1375. }
  1376. return 0;
  1377. }
  1378. /* Set ARP buffer for CVP firmware to handle concurrency */
  1379. int cvp_comm_set_arp_buffers(struct msm_cvp_inst *inst)
  1380. {
  1381. int rc = 0;
  1382. struct cvp_internal_buf *buf;
  1383. if (!inst || !inst->core || !inst->core->device) {
  1384. dprintk(CVP_ERR, "%s invalid parameters\n", __func__);
  1385. return -EINVAL;
  1386. }
  1387. buf = cvp_allocate_arp_bufs(inst, ARP_BUF_SIZE);
  1388. if (!buf) {
  1389. rc = -ENOMEM;
  1390. goto error;
  1391. }
  1392. rc = set_internal_buf_on_fw(inst, buf->smem);
  1393. if (rc)
  1394. goto error;
  1395. rc = wait_for_sess_signal_receipt(inst, HAL_SESSION_SET_BUFFER_DONE);
  1396. if (rc) {
  1397. dprintk(CVP_WARN, "wait for set_buffer_done timeout %d\n", rc);
  1398. goto error;
  1399. }
  1400. return rc;
  1401. error:
  1402. if (rc != -ENOMEM)
  1403. cvp_release_arp_buffers(inst);
  1404. return rc;
  1405. }