msm_cvp_common.c 37 KB

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