msm_cvp_common.c 39 KB

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