msm_cvp_common.c 35 KB

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