msm_cvp_common.c 38 KB

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