msm_cvp_common.c 36 KB

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