msm_cvp_common.c 39 KB

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