msm_cvp_common.c 35 KB

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