msm_cvp_common.c 39 KB

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