msm_cvp_common.c 39 KB

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