msm_cvp_common.c 39 KB

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