msm_cvp_common.c 36 KB

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