msm_cvp_common.c 39 KB

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