msm_cvp_common.c 39 KB

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