msm_cvp_common.c 39 KB

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