cam_custom_context.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/debugfs.h>
  6. #include <linux/videodev2.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/ratelimit.h>
  10. #include "cam_mem_mgr.h"
  11. #include "cam_sync_api.h"
  12. #include "cam_req_mgr_dev.h"
  13. #include "cam_trace.h"
  14. #include "cam_debug_util.h"
  15. #include "cam_packet_util.h"
  16. #include "cam_context_utils.h"
  17. #include "cam_custom_context.h"
  18. #include "cam_common_util.h"
  19. static const char custom_dev_name[] = "cam-custom";
  20. static int __cam_custom_ctx_handle_irq_in_activated(
  21. void *context, uint32_t evt_id, void *evt_data);
  22. static int __cam_custom_ctx_start_dev_in_ready(
  23. struct cam_context *ctx, struct cam_start_stop_dev_cmd *cmd);
  24. static int __cam_custom_ctx_apply_req_in_activated_state(
  25. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  26. uint32_t next_state);
  27. static int __cam_custom_ctx_apply_default_settings(
  28. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply);
  29. static int __cam_custom_ctx_apply_req_in_activated(
  30. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  31. {
  32. int rc = 0;
  33. struct cam_custom_context *custom_ctx =
  34. (struct cam_custom_context *) ctx->ctx_priv;
  35. rc = __cam_custom_ctx_apply_req_in_activated_state(
  36. ctx, apply, CAM_CUSTOM_CTX_ACTIVATED_APPLIED);
  37. CAM_DBG(CAM_CUSTOM, "new substate %d", custom_ctx->substate_activated);
  38. if (rc)
  39. CAM_ERR(CAM_CUSTOM, "Apply failed in state %d rc %d",
  40. custom_ctx->substate_activated, rc);
  41. return rc;
  42. }
  43. static int __cam_custom_ctx_handle_error(
  44. struct cam_custom_context *custom_ctx, void *evt_data)
  45. {
  46. /*
  47. * Handle any HW error scenerios here, all the
  48. * requests in all the lists can be signaled error.
  49. * Notify UMD about this error if needed.
  50. */
  51. return 0;
  52. }
  53. static int __cam_custom_ctx_reg_upd_in_sof(
  54. struct cam_custom_context *custom_ctx, void *evt_data)
  55. {
  56. struct cam_ctx_request *req = NULL;
  57. struct cam_custom_dev_ctx_req *req_custom;
  58. struct cam_context *ctx = custom_ctx->base;
  59. custom_ctx->frame_id++;
  60. /*
  61. * This is for the first update before streamon.
  62. * The initial setting will cause the reg_upd in the
  63. * first frame.
  64. */
  65. if (!list_empty(&ctx->wait_req_list)) {
  66. req = list_first_entry(&ctx->wait_req_list,
  67. struct cam_ctx_request, list);
  68. list_del_init(&req->list);
  69. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  70. if (req_custom->num_fence_map_out == req_custom->num_acked) {
  71. list_add_tail(&req->list, &ctx->free_req_list);
  72. } else {
  73. list_add_tail(&req->list, &ctx->active_req_list);
  74. custom_ctx->active_req_cnt++;
  75. CAM_DBG(CAM_REQ,
  76. "move request %lld to active list(cnt = %d), ctx %u",
  77. req->request_id, custom_ctx->active_req_cnt,
  78. ctx->ctx_id);
  79. }
  80. }
  81. return 0;
  82. }
  83. static int __cam_custom_ctx_reg_upd_in_applied_state(
  84. struct cam_custom_context *custom_ctx, void *evt_data)
  85. {
  86. struct cam_ctx_request *req;
  87. struct cam_context *ctx = custom_ctx->base;
  88. struct cam_custom_dev_ctx_req *req_custom;
  89. custom_ctx->frame_id++;
  90. if (list_empty(&ctx->wait_req_list)) {
  91. CAM_ERR(CAM_CUSTOM,
  92. "Reg upd ack with no waiting request");
  93. goto end;
  94. }
  95. req = list_first_entry(&ctx->wait_req_list,
  96. struct cam_ctx_request, list);
  97. list_del_init(&req->list);
  98. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  99. if (req_custom->num_fence_map_out != 0) {
  100. list_add_tail(&req->list, &ctx->active_req_list);
  101. custom_ctx->active_req_cnt++;
  102. CAM_DBG(CAM_REQ,
  103. "move request %lld to active list(cnt = %d), ctx %u",
  104. req->request_id, custom_ctx->active_req_cnt,
  105. ctx->ctx_id);
  106. } else {
  107. /* no io config, so the request is completed. */
  108. list_add_tail(&req->list, &ctx->free_req_list);
  109. CAM_DBG(CAM_ISP,
  110. "move active request %lld to free list(cnt = %d), ctx %u",
  111. req->request_id, custom_ctx->active_req_cnt,
  112. ctx->ctx_id);
  113. }
  114. custom_ctx->substate_activated = CAM_CUSTOM_CTX_ACTIVATED_SOF;
  115. CAM_DBG(CAM_CUSTOM, "next substate %d", custom_ctx->substate_activated);
  116. end:
  117. return 0;
  118. }
  119. static int __cam_custom_ctx_frame_done(
  120. struct cam_custom_context *custom_ctx, void *evt_data)
  121. {
  122. int rc = 0, i, j;
  123. uint64_t frame_done_req_id;
  124. struct cam_ctx_request *req;
  125. struct cam_custom_dev_ctx_req *req_custom;
  126. struct cam_context *ctx = custom_ctx->base;
  127. struct cam_custom_hw_done_event_data *done_data =
  128. (struct cam_custom_hw_done_event_data *)evt_data;
  129. if (list_empty(&ctx->active_req_list)) {
  130. CAM_DBG(CAM_CUSTOM, "Frame done with no active request");
  131. return 0;
  132. }
  133. req = list_first_entry(&ctx->active_req_list,
  134. struct cam_ctx_request, list);
  135. req_custom = req->req_priv;
  136. for (i = 0; i < done_data->num_handles; i++) {
  137. for (j = 0; j < req_custom->num_fence_map_out; j++) {
  138. if (done_data->resource_handle[i] ==
  139. req_custom->fence_map_out[j].resource_handle)
  140. break;
  141. }
  142. if (j == req_custom->num_fence_map_out) {
  143. CAM_ERR(CAM_CUSTOM,
  144. "Can not find matching rsrc handle 0x%x!",
  145. done_data->resource_handle[i]);
  146. rc = -EINVAL;
  147. continue;
  148. }
  149. if (req_custom->fence_map_out[j].sync_id == -1) {
  150. CAM_WARN(CAM_CUSTOM,
  151. "Duplicate frame done for req %lld",
  152. req->request_id);
  153. continue;
  154. }
  155. rc = cam_sync_signal(req_custom->fence_map_out[j].sync_id,
  156. CAM_SYNC_STATE_SIGNALED_SUCCESS);
  157. if (rc)
  158. CAM_ERR(CAM_CUSTOM, "Sync failed with rc = %d", rc);
  159. req_custom->num_acked++;
  160. req_custom->fence_map_out[j].sync_id = -1;
  161. }
  162. if (req_custom->num_acked > req_custom->num_fence_map_out) {
  163. CAM_ERR(CAM_CUSTOM,
  164. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u",
  165. req->request_id, req_custom->num_acked,
  166. req_custom->num_fence_map_out, ctx->ctx_id);
  167. }
  168. if (req_custom->num_acked != req_custom->num_fence_map_out)
  169. return rc;
  170. custom_ctx->active_req_cnt--;
  171. frame_done_req_id = req->request_id;
  172. list_del_init(&req->list);
  173. list_add_tail(&req->list, &ctx->free_req_list);
  174. CAM_DBG(CAM_REQ,
  175. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u",
  176. frame_done_req_id, custom_ctx->active_req_cnt, ctx->ctx_id);
  177. return rc;
  178. }
  179. static struct cam_ctx_ops
  180. cam_custom_ctx_activated_state_machine
  181. [CAM_CUSTOM_CTX_ACTIVATED_MAX] = {
  182. /* SOF */
  183. {
  184. .ioctl_ops = {},
  185. .crm_ops = {
  186. .apply_req = __cam_custom_ctx_apply_req_in_activated,
  187. .notify_frame_skip =
  188. __cam_custom_ctx_apply_default_settings,
  189. },
  190. .irq_ops = NULL,
  191. },
  192. /* APPLIED */
  193. {
  194. .ioctl_ops = {},
  195. .crm_ops = {
  196. .apply_req = __cam_custom_ctx_apply_req_in_activated,
  197. .notify_frame_skip =
  198. __cam_custom_ctx_apply_default_settings,
  199. },
  200. .irq_ops = NULL,
  201. },
  202. /* HW ERROR */
  203. {
  204. .ioctl_ops = {},
  205. .crm_ops = {},
  206. .irq_ops = NULL,
  207. },
  208. /* HALT */
  209. {
  210. .ioctl_ops = {},
  211. .crm_ops = {},
  212. .irq_ops = NULL,
  213. },
  214. };
  215. static struct cam_custom_ctx_irq_ops
  216. cam_custom_ctx_activated_state_machine_irq
  217. [CAM_CUSTOM_CTX_ACTIVATED_MAX] = {
  218. /* SOF */
  219. {
  220. .irq_ops = {
  221. __cam_custom_ctx_handle_error,
  222. __cam_custom_ctx_reg_upd_in_sof,
  223. __cam_custom_ctx_frame_done,
  224. },
  225. },
  226. /* APPLIED */
  227. {
  228. .irq_ops = {
  229. __cam_custom_ctx_handle_error,
  230. __cam_custom_ctx_reg_upd_in_applied_state,
  231. __cam_custom_ctx_frame_done,
  232. },
  233. },
  234. /* HW ERROR */
  235. {
  236. .irq_ops = {
  237. NULL,
  238. NULL,
  239. NULL,
  240. },
  241. },
  242. /* HALT */
  243. {
  244. },
  245. };
  246. static int __cam_custom_ctx_enqueue_request_in_order(
  247. struct cam_context *ctx, struct cam_ctx_request *req)
  248. {
  249. struct cam_ctx_request *req_current;
  250. struct cam_ctx_request *req_prev;
  251. struct list_head temp_list;
  252. INIT_LIST_HEAD(&temp_list);
  253. spin_lock_bh(&ctx->lock);
  254. if (list_empty(&ctx->pending_req_list)) {
  255. list_add_tail(&req->list, &ctx->pending_req_list);
  256. } else {
  257. list_for_each_entry_safe_reverse(
  258. req_current, req_prev, &ctx->pending_req_list, list) {
  259. if (req->request_id < req_current->request_id) {
  260. list_del_init(&req_current->list);
  261. list_add(&req_current->list, &temp_list);
  262. continue;
  263. } else if (req->request_id == req_current->request_id) {
  264. CAM_WARN(CAM_CUSTOM,
  265. "Received duplicated request %lld",
  266. req->request_id);
  267. }
  268. break;
  269. }
  270. list_add_tail(&req->list, &ctx->pending_req_list);
  271. if (!list_empty(&temp_list)) {
  272. list_for_each_entry_safe(
  273. req_current, req_prev, &temp_list, list) {
  274. list_del_init(&req_current->list);
  275. list_add_tail(&req_current->list,
  276. &ctx->pending_req_list);
  277. }
  278. }
  279. }
  280. spin_unlock_bh(&ctx->lock);
  281. return 0;
  282. }
  283. static int __cam_custom_ctx_flush_req(struct cam_context *ctx,
  284. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  285. {
  286. int i, rc;
  287. uint32_t cancel_req_id_found = 0;
  288. struct cam_ctx_request *req;
  289. struct cam_ctx_request *req_temp;
  290. struct cam_custom_dev_ctx_req *req_custom;
  291. struct list_head flush_list;
  292. INIT_LIST_HEAD(&flush_list);
  293. if (list_empty(req_list)) {
  294. CAM_DBG(CAM_CUSTOM, "request list is empty");
  295. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  296. CAM_ERR(CAM_CUSTOM, "no request to cancel");
  297. return -EINVAL;
  298. } else {
  299. return 0;
  300. }
  301. }
  302. CAM_DBG(CAM_CUSTOM, "Flush [%u] in progress for req_id %llu",
  303. flush_req->type, flush_req->req_id);
  304. list_for_each_entry_safe(req, req_temp, req_list, list) {
  305. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  306. if (req->request_id != flush_req->req_id) {
  307. continue;
  308. } else {
  309. list_del_init(&req->list);
  310. list_add_tail(&req->list, &flush_list);
  311. cancel_req_id_found = 1;
  312. break;
  313. }
  314. }
  315. list_del_init(&req->list);
  316. list_add_tail(&req->list, &flush_list);
  317. }
  318. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  319. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  320. for (i = 0; i < req_custom->num_fence_map_out; i++) {
  321. if (req_custom->fence_map_out[i].sync_id != -1) {
  322. CAM_DBG(CAM_CUSTOM,
  323. "Flush req 0x%llx, fence %d",
  324. req->request_id,
  325. req_custom->fence_map_out[i].sync_id);
  326. rc = cam_sync_signal(
  327. req_custom->fence_map_out[i].sync_id,
  328. CAM_SYNC_STATE_SIGNALED_CANCEL);
  329. if (rc)
  330. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  331. "signal fence failed\n");
  332. req_custom->fence_map_out[i].sync_id = -1;
  333. }
  334. }
  335. list_add_tail(&req->list, &ctx->free_req_list);
  336. }
  337. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
  338. !cancel_req_id_found)
  339. CAM_DBG(CAM_CUSTOM,
  340. "Flush request id:%lld is not found in the list",
  341. flush_req->req_id);
  342. return 0;
  343. }
  344. static int __cam_custom_ctx_unlink_in_acquired(struct cam_context *ctx,
  345. struct cam_req_mgr_core_dev_link_setup *unlink)
  346. {
  347. ctx->link_hdl = -1;
  348. ctx->ctx_crm_intf = NULL;
  349. return 0;
  350. }
  351. static int __cam_custom_ctx_unlink_in_ready(struct cam_context *ctx,
  352. struct cam_req_mgr_core_dev_link_setup *unlink)
  353. {
  354. ctx->link_hdl = -1;
  355. ctx->ctx_crm_intf = NULL;
  356. ctx->state = CAM_CTX_ACQUIRED;
  357. return 0;
  358. }
  359. static int __cam_custom_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  360. struct cam_req_mgr_device_info *dev_info)
  361. {
  362. dev_info->dev_hdl = ctx->dev_hdl;
  363. strlcpy(dev_info->name, CAM_CUSTOM_DEV_NAME, sizeof(dev_info->name));
  364. dev_info->dev_id = CAM_REQ_MGR_DEVICE_CUSTOM_HW;
  365. dev_info->p_delay = 1;
  366. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  367. return 0;
  368. }
  369. static int __cam_custom_ctx_flush_req_in_top_state(
  370. struct cam_context *ctx,
  371. struct cam_req_mgr_flush_request *flush_req)
  372. {
  373. int rc = 0;
  374. struct cam_custom_context *custom_ctx;
  375. struct cam_hw_reset_args reset_args;
  376. struct cam_hw_stop_args stop_args;
  377. struct cam_custom_stop_args custom_stop;
  378. custom_ctx =
  379. (struct cam_custom_context *) ctx->ctx_priv;
  380. CAM_DBG(CAM_CUSTOM, "Flushing pending list");
  381. spin_lock_bh(&ctx->lock);
  382. __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  383. spin_unlock_bh(&ctx->lock);
  384. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  385. if (ctx->state <= CAM_CTX_READY) {
  386. ctx->state = CAM_CTX_ACQUIRED;
  387. goto end;
  388. }
  389. spin_lock_bh(&ctx->lock);
  390. ctx->state = CAM_CTX_FLUSHED;
  391. spin_unlock_bh(&ctx->lock);
  392. CAM_INFO(CAM_CUSTOM, "Last request id to flush is %lld",
  393. flush_req->req_id);
  394. ctx->last_flush_req = flush_req->req_id;
  395. /* stop hw first */
  396. if (ctx->hw_mgr_intf->hw_stop) {
  397. custom_stop.stop_only = true;
  398. stop_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  399. stop_args.args = (void *) &custom_stop;
  400. rc = ctx->hw_mgr_intf->hw_stop(
  401. ctx->hw_mgr_intf->hw_mgr_priv, &stop_args);
  402. if (rc)
  403. CAM_ERR(CAM_CUSTOM,
  404. "HW stop failed in flush rc %d", rc);
  405. }
  406. spin_lock_bh(&ctx->lock);
  407. if (!list_empty(&ctx->wait_req_list))
  408. __cam_custom_ctx_flush_req(ctx, &ctx->wait_req_list,
  409. flush_req);
  410. if (!list_empty(&ctx->active_req_list))
  411. __cam_custom_ctx_flush_req(ctx, &ctx->active_req_list,
  412. flush_req);
  413. custom_ctx->active_req_cnt = 0;
  414. spin_unlock_bh(&ctx->lock);
  415. reset_args.ctxt_to_hw_map = custom_ctx->hw_ctx;
  416. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  417. &reset_args);
  418. if (rc)
  419. CAM_ERR(CAM_CUSTOM,
  420. "Reset HW failed in flush rc %d", rc);
  421. custom_ctx->init_received = false;
  422. }
  423. end:
  424. return rc;
  425. }
  426. static int __cam_custom_ctx_flush_req_in_ready(
  427. struct cam_context *ctx,
  428. struct cam_req_mgr_flush_request *flush_req)
  429. {
  430. int rc = 0;
  431. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  432. spin_lock_bh(&ctx->lock);
  433. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  434. /* if nothing is in pending req list, change state to acquire */
  435. if (list_empty(&ctx->pending_req_list))
  436. ctx->state = CAM_CTX_ACQUIRED;
  437. spin_unlock_bh(&ctx->lock);
  438. CAM_DBG(CAM_CUSTOM, "Flush request in ready state. next state %d",
  439. ctx->state);
  440. return rc;
  441. }
  442. static int __cam_custom_stop_dev_core(
  443. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  444. {
  445. int rc = 0;
  446. uint32_t i;
  447. struct cam_custom_context *ctx_custom =
  448. (struct cam_custom_context *) ctx->ctx_priv;
  449. struct cam_ctx_request *req;
  450. struct cam_custom_dev_ctx_req *req_custom;
  451. struct cam_hw_stop_args stop;
  452. struct cam_custom_stop_args custom_stop;
  453. if ((ctx->state != CAM_CTX_FLUSHED) && (ctx_custom->hw_ctx) &&
  454. (ctx->hw_mgr_intf->hw_stop)) {
  455. custom_stop.stop_only = false;
  456. stop.ctxt_to_hw_map = ctx_custom->hw_ctx;
  457. stop.args = (void *) &custom_stop;
  458. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  459. &stop);
  460. if (rc)
  461. CAM_ERR(CAM_CUSTOM, "HW stop failed rc %d", rc);
  462. }
  463. while (!list_empty(&ctx->pending_req_list)) {
  464. req = list_first_entry(&ctx->pending_req_list,
  465. struct cam_ctx_request, list);
  466. list_del_init(&req->list);
  467. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  468. CAM_DBG(CAM_CUSTOM,
  469. "signal fence in pending list. fence num %d",
  470. req_custom->num_fence_map_out);
  471. for (i = 0; i < req_custom->num_fence_map_out; i++)
  472. if (req_custom->fence_map_out[i].sync_id != -1) {
  473. cam_sync_signal(
  474. req_custom->fence_map_out[i].sync_id,
  475. CAM_SYNC_STATE_SIGNALED_CANCEL);
  476. }
  477. list_add_tail(&req->list, &ctx->free_req_list);
  478. }
  479. while (!list_empty(&ctx->wait_req_list)) {
  480. req = list_first_entry(&ctx->wait_req_list,
  481. struct cam_ctx_request, list);
  482. list_del_init(&req->list);
  483. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  484. CAM_DBG(CAM_CUSTOM, "signal fence in wait list. fence num %d",
  485. req_custom->num_fence_map_out);
  486. for (i = 0; i < req_custom->num_fence_map_out; i++)
  487. if (req_custom->fence_map_out[i].sync_id != -1) {
  488. cam_sync_signal(
  489. req_custom->fence_map_out[i].sync_id,
  490. CAM_SYNC_STATE_SIGNALED_CANCEL);
  491. }
  492. list_add_tail(&req->list, &ctx->free_req_list);
  493. }
  494. while (!list_empty(&ctx->active_req_list)) {
  495. req = list_first_entry(&ctx->active_req_list,
  496. struct cam_ctx_request, list);
  497. list_del_init(&req->list);
  498. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  499. CAM_DBG(CAM_CUSTOM, "signal fence in active list. fence num %d",
  500. req_custom->num_fence_map_out);
  501. for (i = 0; i < req_custom->num_fence_map_out; i++)
  502. if (req_custom->fence_map_out[i].sync_id != -1) {
  503. cam_sync_signal(
  504. req_custom->fence_map_out[i].sync_id,
  505. CAM_SYNC_STATE_SIGNALED_CANCEL);
  506. }
  507. list_add_tail(&req->list, &ctx->free_req_list);
  508. }
  509. ctx_custom->frame_id = 0;
  510. ctx_custom->active_req_cnt = 0;
  511. CAM_DBG(CAM_CUSTOM, "Stop device success next state %d on ctx %u",
  512. ctx->state, ctx->ctx_id);
  513. if (!stop_cmd) {
  514. rc = __cam_custom_ctx_unlink_in_ready(ctx, NULL);
  515. if (rc)
  516. CAM_ERR(CAM_CUSTOM, "Unlink failed rc=%d", rc);
  517. }
  518. return rc;
  519. }
  520. static int __cam_custom_stop_dev_in_activated(struct cam_context *ctx,
  521. struct cam_start_stop_dev_cmd *cmd)
  522. {
  523. struct cam_custom_context *ctx_custom =
  524. (struct cam_custom_context *)ctx->ctx_priv;
  525. __cam_custom_stop_dev_core(ctx, cmd);
  526. ctx_custom->init_received = false;
  527. ctx->state = CAM_CTX_ACQUIRED;
  528. return 0;
  529. }
  530. static int __cam_custom_ctx_release_hw_in_top_state(
  531. struct cam_context *ctx, void *cmd)
  532. {
  533. int rc = 0;
  534. struct cam_hw_release_args rel_arg;
  535. struct cam_req_mgr_flush_request flush_req;
  536. struct cam_custom_context *custom_ctx =
  537. (struct cam_custom_context *) ctx->ctx_priv;
  538. if (custom_ctx->hw_ctx) {
  539. rel_arg.ctxt_to_hw_map = custom_ctx->hw_ctx;
  540. rc = ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  541. &rel_arg);
  542. custom_ctx->hw_ctx = NULL;
  543. if (rc)
  544. CAM_ERR(CAM_CUSTOM,
  545. "Failed to release HW for ctx:%u", ctx->ctx_id);
  546. } else {
  547. CAM_ERR(CAM_CUSTOM, "No HW resources acquired for this ctx");
  548. }
  549. ctx->last_flush_req = 0;
  550. custom_ctx->frame_id = 0;
  551. custom_ctx->active_req_cnt = 0;
  552. custom_ctx->hw_acquired = false;
  553. custom_ctx->init_received = false;
  554. /* check for active requests as well */
  555. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  556. flush_req.link_hdl = ctx->link_hdl;
  557. flush_req.dev_hdl = ctx->dev_hdl;
  558. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  559. spin_lock_bh(&ctx->lock);
  560. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list,
  561. &flush_req);
  562. spin_unlock_bh(&ctx->lock);
  563. ctx->state = CAM_CTX_ACQUIRED;
  564. CAM_DBG(CAM_CUSTOM, "Release HW success[%u] next state %d",
  565. ctx->ctx_id, ctx->state);
  566. return rc;
  567. }
  568. static int __cam_custom_ctx_release_hw_in_activated_state(
  569. struct cam_context *ctx, void *cmd)
  570. {
  571. int rc = 0;
  572. rc = __cam_custom_stop_dev_in_activated(ctx, NULL);
  573. if (rc)
  574. CAM_ERR(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  575. rc = __cam_custom_ctx_release_hw_in_top_state(ctx, cmd);
  576. if (rc)
  577. CAM_ERR(CAM_CUSTOM, "Release hw failed rc=%d", rc);
  578. return rc;
  579. }
  580. static int __cam_custom_release_dev_in_acquired(struct cam_context *ctx,
  581. struct cam_release_dev_cmd *cmd)
  582. {
  583. int rc;
  584. struct cam_custom_context *ctx_custom =
  585. (struct cam_custom_context *) ctx->ctx_priv;
  586. struct cam_req_mgr_flush_request flush_req;
  587. if (cmd && ctx_custom->hw_ctx) {
  588. CAM_ERR(CAM_CUSTOM, "releasing hw");
  589. __cam_custom_ctx_release_hw_in_top_state(ctx, NULL);
  590. }
  591. ctx->ctx_crm_intf = NULL;
  592. ctx->last_flush_req = 0;
  593. ctx_custom->frame_id = 0;
  594. ctx_custom->active_req_cnt = 0;
  595. ctx_custom->hw_acquired = false;
  596. ctx_custom->init_received = false;
  597. if (!list_empty(&ctx->active_req_list))
  598. CAM_ERR(CAM_CUSTOM, "Active list is not empty");
  599. /* Flush all the pending request list */
  600. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  601. flush_req.link_hdl = ctx->link_hdl;
  602. flush_req.dev_hdl = ctx->dev_hdl;
  603. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  604. spin_lock_bh(&ctx->lock);
  605. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list,
  606. &flush_req);
  607. spin_unlock_bh(&ctx->lock);
  608. ctx->state = CAM_CTX_AVAILABLE;
  609. CAM_DBG(CAM_CUSTOM, "Release device success[%u] next state %d",
  610. ctx->ctx_id, ctx->state);
  611. return rc;
  612. }
  613. static int __cam_custom_ctx_apply_default_settings(
  614. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  615. {
  616. int rc = 0;
  617. struct cam_custom_context *custom_ctx =
  618. (struct cam_custom_context *) ctx->ctx_priv;
  619. struct cam_hw_cmd_args hw_cmd_args;
  620. struct cam_custom_hw_cmd_args custom_hw_cmd_args;
  621. hw_cmd_args.ctxt_to_hw_map = custom_ctx->hw_ctx;
  622. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  623. custom_hw_cmd_args.cmd_type =
  624. CAM_CUSTOM_HW_MGR_PROG_DEFAULT_CONFIG;
  625. hw_cmd_args.u.internal_args = (void *)&custom_hw_cmd_args;
  626. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  627. &hw_cmd_args);
  628. if (rc)
  629. CAM_ERR(CAM_CUSTOM,
  630. "Failed to apply default settings rc %d", rc);
  631. else
  632. CAM_DBG(CAM_CUSTOM, "Applied default settings rc %d", rc);
  633. return rc;
  634. }
  635. static int __cam_custom_ctx_apply_req_in_activated_state(
  636. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  637. uint32_t next_state)
  638. {
  639. int rc = 0;
  640. struct cam_ctx_request *req;
  641. struct cam_custom_dev_ctx_req *req_custom;
  642. struct cam_custom_context *custom_ctx = NULL;
  643. struct cam_hw_config_args cfg;
  644. if (list_empty(&ctx->pending_req_list)) {
  645. CAM_ERR(CAM_CUSTOM, "No available request for Apply id %lld",
  646. apply->request_id);
  647. rc = -EFAULT;
  648. goto end;
  649. }
  650. if (!list_empty(&ctx->wait_req_list))
  651. CAM_WARN(CAM_CUSTOM, "Apply invoked with a req in wait list");
  652. custom_ctx = (struct cam_custom_context *) ctx->ctx_priv;
  653. spin_lock_bh(&ctx->lock);
  654. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  655. list);
  656. spin_unlock_bh(&ctx->lock);
  657. /*
  658. * Check whether the request id is matching the tip
  659. */
  660. if (req->request_id != apply->request_id) {
  661. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  662. "Invalid Request Id asking %llu existing %llu",
  663. apply->request_id, req->request_id);
  664. rc = -EFAULT;
  665. goto end;
  666. }
  667. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  668. cfg.ctxt_to_hw_map = custom_ctx->hw_ctx;
  669. cfg.request_id = req->request_id;
  670. cfg.hw_update_entries = req_custom->cfg;
  671. cfg.num_hw_update_entries = req_custom->num_cfg;
  672. cfg.priv = &req_custom->hw_update_data;
  673. cfg.init_packet = 0;
  674. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  675. if (rc) {
  676. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  677. "Can not apply the configuration");
  678. } else {
  679. spin_lock_bh(&ctx->lock);
  680. custom_ctx->substate_activated = next_state;
  681. list_del_init(&req->list);
  682. list_add_tail(&req->list, &ctx->wait_req_list);
  683. spin_unlock_bh(&ctx->lock);
  684. }
  685. end:
  686. return rc;
  687. }
  688. static int __cam_custom_ctx_acquire_hw_v1(
  689. struct cam_context *ctx, void *args)
  690. {
  691. int rc = 0;
  692. struct cam_acquire_hw_cmd_v1 *cmd =
  693. (struct cam_acquire_hw_cmd_v1 *)args;
  694. struct cam_hw_acquire_args param;
  695. struct cam_custom_context *ctx_custom =
  696. (struct cam_custom_context *) ctx->ctx_priv;
  697. struct cam_custom_acquire_hw_info *acquire_hw_info = NULL;
  698. if (!ctx->hw_mgr_intf) {
  699. CAM_ERR(CAM_CUSTOM, "HW interface is not ready");
  700. rc = -EFAULT;
  701. goto end;
  702. }
  703. CAM_DBG(CAM_CUSTOM,
  704. "session_hdl 0x%x, hdl type %d, res %lld",
  705. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  706. if (cmd->handle_type != 1) {
  707. CAM_ERR(CAM_CUSTOM, "Only user pointer is supported");
  708. rc = -EINVAL;
  709. goto end;
  710. }
  711. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  712. CAM_ERR(CAM_CUSTOM, "data_size is not a valid value");
  713. goto end;
  714. }
  715. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  716. if (!acquire_hw_info) {
  717. rc = -ENOMEM;
  718. goto end;
  719. }
  720. CAM_DBG(CAM_CUSTOM, "start copy resources from user");
  721. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  722. cmd->data_size)) {
  723. rc = -EFAULT;
  724. goto free_res;
  725. }
  726. memset(&param, 0, sizeof(param));
  727. param.context_data = ctx;
  728. param.event_cb = ctx->irq_cb_intf;
  729. param.acquire_info_size = cmd->data_size;
  730. param.acquire_info = (uint64_t) acquire_hw_info;
  731. /* call HW manager to reserve the resource */
  732. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  733. &param);
  734. if (rc != 0) {
  735. CAM_ERR(CAM_CUSTOM, "Acquire HW failed");
  736. goto free_res;
  737. }
  738. ctx_custom->substate_machine_irq =
  739. cam_custom_ctx_activated_state_machine_irq;
  740. ctx_custom->substate_machine =
  741. cam_custom_ctx_activated_state_machine;
  742. ctx_custom->hw_ctx = param.ctxt_to_hw_map;
  743. ctx_custom->hw_acquired = true;
  744. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  745. CAM_DBG(CAM_CUSTOM,
  746. "Acquire HW success on session_hdl 0x%xs for ctx_id %u",
  747. ctx->session_hdl, ctx->ctx_id);
  748. kfree(acquire_hw_info);
  749. return rc;
  750. free_res:
  751. kfree(acquire_hw_info);
  752. end:
  753. return rc;
  754. }
  755. static int __cam_custom_ctx_acquire_dev_in_available(
  756. struct cam_context *ctx, struct cam_acquire_dev_cmd *cmd)
  757. {
  758. int rc = 0;
  759. struct cam_create_dev_hdl req_hdl_param;
  760. if (!ctx->hw_mgr_intf) {
  761. CAM_ERR(CAM_CUSTOM, "HW interface is not ready");
  762. rc = -EFAULT;
  763. return rc;
  764. }
  765. CAM_DBG(CAM_CUSTOM,
  766. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  767. cmd->session_handle, cmd->num_resources,
  768. cmd->handle_type, cmd->resource_hdl);
  769. if (cmd->num_resources != CAM_API_COMPAT_CONSTANT) {
  770. CAM_ERR(CAM_CUSTOM, "Invalid num_resources 0x%x",
  771. cmd->num_resources);
  772. return -EINVAL;
  773. }
  774. req_hdl_param.session_hdl = cmd->session_handle;
  775. req_hdl_param.v4l2_sub_dev_flag = 0;
  776. req_hdl_param.media_entity_flag = 0;
  777. req_hdl_param.ops = ctx->crm_ctx_intf;
  778. req_hdl_param.priv = ctx;
  779. CAM_DBG(CAM_CUSTOM, "get device handle from bridge");
  780. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  781. if (ctx->dev_hdl <= 0) {
  782. rc = -EFAULT;
  783. CAM_ERR(CAM_CUSTOM, "Can not create device handle");
  784. return rc;
  785. }
  786. cmd->dev_handle = ctx->dev_hdl;
  787. ctx->session_hdl = cmd->session_handle;
  788. ctx->state = CAM_CTX_ACQUIRED;
  789. CAM_DBG(CAM_CUSTOM,
  790. "Acquire dev success on session_hdl 0x%x for ctx %u",
  791. cmd->session_handle, ctx->ctx_id);
  792. return rc;
  793. }
  794. static int __cam_custom_ctx_enqueue_init_request(
  795. struct cam_context *ctx, struct cam_ctx_request *req)
  796. {
  797. int rc = 0;
  798. struct cam_ctx_request *req_old;
  799. struct cam_custom_dev_ctx_req *req_custom_old;
  800. struct cam_custom_dev_ctx_req *req_custom_new;
  801. spin_lock_bh(&ctx->lock);
  802. if (list_empty(&ctx->pending_req_list)) {
  803. list_add_tail(&req->list, &ctx->pending_req_list);
  804. goto end;
  805. }
  806. req_old = list_first_entry(&ctx->pending_req_list,
  807. struct cam_ctx_request, list);
  808. req_custom_old = (struct cam_custom_dev_ctx_req *) req_old->req_priv;
  809. req_custom_new = (struct cam_custom_dev_ctx_req *) req->req_priv;
  810. if (req_custom_old->hw_update_data.packet_opcode_type ==
  811. CAM_CUSTOM_PACKET_INIT_DEV) {
  812. if ((req_custom_old->num_cfg + req_custom_new->num_cfg) >=
  813. CAM_CUSTOM_CTX_CFG_MAX) {
  814. CAM_WARN(CAM_CUSTOM, "Can not merge INIT pkt");
  815. rc = -ENOMEM;
  816. }
  817. if (req_custom_old->num_fence_map_out != 0 ||
  818. req_custom_old->num_fence_map_in != 0) {
  819. CAM_WARN(CAM_CUSTOM, "Invalid INIT pkt sequence");
  820. rc = -EINVAL;
  821. }
  822. if (!rc) {
  823. memcpy(req_custom_old->fence_map_out,
  824. req_custom_new->fence_map_out,
  825. sizeof(req_custom_new->fence_map_out[0])*
  826. req_custom_new->num_fence_map_out);
  827. req_custom_old->num_fence_map_out =
  828. req_custom_new->num_fence_map_out;
  829. memcpy(req_custom_old->fence_map_in,
  830. req_custom_new->fence_map_in,
  831. sizeof(req_custom_new->fence_map_in[0])*
  832. req_custom_new->num_fence_map_in);
  833. req_custom_old->num_fence_map_in =
  834. req_custom_new->num_fence_map_in;
  835. memcpy(&req_custom_old->cfg[req_custom_old->num_cfg],
  836. req_custom_new->cfg,
  837. sizeof(req_custom_new->cfg[0])*
  838. req_custom_new->num_cfg);
  839. req_custom_old->num_cfg += req_custom_new->num_cfg;
  840. req_old->request_id = req->request_id;
  841. list_add_tail(&req->list, &ctx->free_req_list);
  842. }
  843. } else {
  844. CAM_WARN(CAM_CUSTOM,
  845. "Received Update pkt before INIT pkt. req_id= %lld",
  846. req->request_id);
  847. rc = -EINVAL;
  848. }
  849. end:
  850. spin_unlock_bh(&ctx->lock);
  851. return rc;
  852. }
  853. static int __cam_custom_ctx_config_dev(struct cam_context *ctx,
  854. struct cam_config_dev_cmd *cmd)
  855. {
  856. int rc = 0, i;
  857. struct cam_ctx_request *req = NULL;
  858. struct cam_custom_dev_ctx_req *req_custom;
  859. uintptr_t packet_addr;
  860. struct cam_packet *packet;
  861. size_t len = 0;
  862. struct cam_hw_prepare_update_args cfg;
  863. struct cam_req_mgr_add_request add_req;
  864. struct cam_custom_context *ctx_custom =
  865. (struct cam_custom_context *) ctx->ctx_priv;
  866. /* get free request */
  867. spin_lock_bh(&ctx->lock);
  868. if (!list_empty(&ctx->free_req_list)) {
  869. req = list_first_entry(&ctx->free_req_list,
  870. struct cam_ctx_request, list);
  871. list_del_init(&req->list);
  872. }
  873. spin_unlock_bh(&ctx->lock);
  874. if (!req) {
  875. CAM_ERR(CAM_CUSTOM, "No more request obj free");
  876. return -ENOMEM;
  877. }
  878. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  879. /* for config dev, only memory handle is supported */
  880. /* map packet from the memhandle */
  881. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  882. &packet_addr, &len);
  883. if (rc != 0) {
  884. CAM_ERR(CAM_CUSTOM, "Can not get packet address");
  885. rc = -EINVAL;
  886. goto free_req;
  887. }
  888. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  889. CAM_DBG(CAM_CUSTOM, "pack_handle %llx", cmd->packet_handle);
  890. CAM_DBG(CAM_CUSTOM, "packet address is 0x%zx", packet_addr);
  891. CAM_DBG(CAM_CUSTOM, "packet with length %zu, offset 0x%llx",
  892. len, cmd->offset);
  893. CAM_DBG(CAM_CUSTOM, "Packet request id %lld",
  894. packet->header.request_id);
  895. CAM_DBG(CAM_CUSTOM, "Packet size 0x%x", packet->header.size);
  896. CAM_DBG(CAM_CUSTOM, "packet op %d", packet->header.op_code);
  897. if ((((packet->header.op_code) & 0xF) ==
  898. CAM_CUSTOM_PACKET_UPDATE_DEV)
  899. && (packet->header.request_id <= ctx->last_flush_req)) {
  900. CAM_DBG(CAM_CUSTOM,
  901. "request %lld has been flushed, reject packet",
  902. packet->header.request_id);
  903. rc = -EINVAL;
  904. goto free_req;
  905. }
  906. /* preprocess the configuration */
  907. memset(&cfg, 0, sizeof(cfg));
  908. cfg.packet = packet;
  909. cfg.ctxt_to_hw_map = ctx_custom->hw_ctx;
  910. cfg.out_map_entries = req_custom->fence_map_out;
  911. cfg.max_out_map_entries = CAM_CUSTOM_DEV_CTX_RES_MAX;
  912. cfg.in_map_entries = req_custom->fence_map_in;
  913. cfg.max_in_map_entries = CAM_CUSTOM_DEV_CTX_RES_MAX;
  914. cfg.priv = &req_custom->hw_update_data;
  915. cfg.pf_data = &(req->pf_data);
  916. rc = ctx->hw_mgr_intf->hw_prepare_update(
  917. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  918. if (rc != 0) {
  919. CAM_ERR(CAM_CUSTOM, "Prepare config packet failed in HW layer");
  920. rc = -EFAULT;
  921. goto free_req;
  922. }
  923. req_custom->num_cfg = cfg.num_hw_update_entries;
  924. req_custom->num_fence_map_out = cfg.num_out_map_entries;
  925. req_custom->num_fence_map_in = cfg.num_in_map_entries;
  926. req_custom->num_acked = 0;
  927. req_custom->hw_update_data.num_cfg = cfg.num_out_map_entries;
  928. for (i = 0; i < req_custom->num_fence_map_out; i++) {
  929. rc = cam_sync_get_obj_ref(req_custom->fence_map_out[i].sync_id);
  930. if (rc) {
  931. CAM_ERR(CAM_CUSTOM, "Can't get ref for fence %d",
  932. req_custom->fence_map_out[i].sync_id);
  933. goto put_ref;
  934. }
  935. }
  936. CAM_DBG(CAM_CUSTOM,
  937. "num_entry: %d, num fence out: %d, num fence in: %d",
  938. req_custom->num_cfg, req_custom->num_fence_map_out,
  939. req_custom->num_fence_map_in);
  940. req->request_id = packet->header.request_id;
  941. req->status = 1;
  942. CAM_DBG(CAM_CUSTOM, "Packet request id %lld packet opcode:%d",
  943. packet->header.request_id,
  944. req_custom->hw_update_data.packet_opcode_type);
  945. if (req_custom->hw_update_data.packet_opcode_type ==
  946. CAM_CUSTOM_PACKET_INIT_DEV) {
  947. if (ctx->state < CAM_CTX_ACTIVATED) {
  948. rc = __cam_custom_ctx_enqueue_init_request(ctx, req);
  949. if (rc)
  950. CAM_ERR(CAM_CUSTOM, "Enqueue INIT pkt failed");
  951. ctx_custom->init_received = true;
  952. } else {
  953. rc = -EINVAL;
  954. CAM_ERR(CAM_CUSTOM, "Recevied INIT pkt in wrong state");
  955. }
  956. } else {
  957. if ((ctx->state != CAM_CTX_FLUSHED) &&
  958. (ctx->state >= CAM_CTX_READY) &&
  959. (ctx->ctx_crm_intf->add_req)) {
  960. add_req.link_hdl = ctx->link_hdl;
  961. add_req.dev_hdl = ctx->dev_hdl;
  962. add_req.req_id = req->request_id;
  963. add_req.skip_before_applying = 0;
  964. rc = ctx->ctx_crm_intf->add_req(&add_req);
  965. if (rc) {
  966. CAM_ERR(CAM_CUSTOM,
  967. "Add req failed: req id=%llu",
  968. req->request_id);
  969. } else {
  970. __cam_custom_ctx_enqueue_request_in_order(
  971. ctx, req);
  972. }
  973. } else {
  974. rc = -EINVAL;
  975. CAM_ERR(CAM_CUSTOM, "Recevied Update in wrong state");
  976. }
  977. }
  978. if (rc)
  979. goto put_ref;
  980. CAM_DBG(CAM_CUSTOM,
  981. "Preprocessing Config req_id %lld successful on ctx %u",
  982. req->request_id, ctx->ctx_id);
  983. return rc;
  984. put_ref:
  985. for (--i; i >= 0; i--) {
  986. if (cam_sync_put_obj_ref(req_custom->fence_map_out[i].sync_id))
  987. CAM_ERR(CAM_CUSTOM, "Failed to put ref of fence %d",
  988. req_custom->fence_map_out[i].sync_id);
  989. }
  990. free_req:
  991. spin_lock_bh(&ctx->lock);
  992. list_add_tail(&req->list, &ctx->free_req_list);
  993. spin_unlock_bh(&ctx->lock);
  994. return rc;
  995. }
  996. static int __cam_custom_ctx_config_dev_in_flushed(struct cam_context *ctx,
  997. struct cam_config_dev_cmd *cmd)
  998. {
  999. int rc = 0;
  1000. struct cam_start_stop_dev_cmd start_cmd;
  1001. struct cam_custom_context *custom_ctx =
  1002. (struct cam_custom_context *) ctx->ctx_priv;
  1003. if (!custom_ctx->hw_acquired) {
  1004. CAM_ERR(CAM_CUSTOM, "HW is not acquired, reject packet");
  1005. rc = -EINVAL;
  1006. goto end;
  1007. }
  1008. rc = __cam_custom_ctx_config_dev(ctx, cmd);
  1009. if (rc)
  1010. goto end;
  1011. if (!custom_ctx->init_received) {
  1012. CAM_WARN(CAM_CUSTOM,
  1013. "Received update packet in flushed state, skip start");
  1014. goto end;
  1015. }
  1016. start_cmd.dev_handle = cmd->dev_handle;
  1017. start_cmd.session_handle = cmd->session_handle;
  1018. rc = __cam_custom_ctx_start_dev_in_ready(ctx, &start_cmd);
  1019. if (rc)
  1020. CAM_ERR(CAM_CUSTOM,
  1021. "Failed to re-start HW after flush rc: %d", rc);
  1022. else
  1023. CAM_INFO(CAM_CUSTOM,
  1024. "Received init after flush. Re-start HW complete.");
  1025. end:
  1026. return rc;
  1027. }
  1028. static int __cam_custom_ctx_config_dev_in_acquired(struct cam_context *ctx,
  1029. struct cam_config_dev_cmd *cmd)
  1030. {
  1031. int rc = 0;
  1032. struct cam_custom_context *ctx_custom =
  1033. (struct cam_custom_context *) ctx->ctx_priv;
  1034. if (!ctx_custom->hw_acquired) {
  1035. CAM_ERR(CAM_CUSTOM, "HW not acquired, reject config packet");
  1036. return -EAGAIN;
  1037. }
  1038. rc = __cam_custom_ctx_config_dev(ctx, cmd);
  1039. if (!rc && (ctx->link_hdl >= 0))
  1040. ctx->state = CAM_CTX_READY;
  1041. return rc;
  1042. }
  1043. static int __cam_custom_ctx_link_in_acquired(struct cam_context *ctx,
  1044. struct cam_req_mgr_core_dev_link_setup *link)
  1045. {
  1046. struct cam_custom_context *ctx_custom =
  1047. (struct cam_custom_context *) ctx->ctx_priv;
  1048. ctx->link_hdl = link->link_hdl;
  1049. ctx->ctx_crm_intf = link->crm_cb;
  1050. ctx_custom->subscribe_event = link->subscribe_event;
  1051. /* change state only if we had the init config */
  1052. if (ctx_custom->init_received)
  1053. ctx->state = CAM_CTX_READY;
  1054. CAM_DBG(CAM_CUSTOM, "next state %d", ctx->state);
  1055. return 0;
  1056. }
  1057. static int __cam_custom_ctx_start_dev_in_ready(struct cam_context *ctx,
  1058. struct cam_start_stop_dev_cmd *cmd)
  1059. {
  1060. int rc = 0;
  1061. struct cam_custom_start_args custom_start;
  1062. struct cam_ctx_request *req;
  1063. struct cam_custom_dev_ctx_req *req_custom;
  1064. struct cam_custom_context *ctx_custom =
  1065. (struct cam_custom_context *) ctx->ctx_priv;
  1066. if (cmd->session_handle != ctx->session_hdl ||
  1067. cmd->dev_handle != ctx->dev_hdl) {
  1068. rc = -EPERM;
  1069. goto end;
  1070. }
  1071. if (list_empty(&ctx->pending_req_list)) {
  1072. /* should never happen */
  1073. CAM_ERR(CAM_CUSTOM, "Start device with empty configuration");
  1074. rc = -EFAULT;
  1075. goto end;
  1076. } else {
  1077. req = list_first_entry(&ctx->pending_req_list,
  1078. struct cam_ctx_request, list);
  1079. }
  1080. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  1081. if (!ctx_custom->hw_ctx) {
  1082. CAM_ERR(CAM_CUSTOM, "Wrong hw context pointer.");
  1083. rc = -EFAULT;
  1084. goto end;
  1085. }
  1086. custom_start.hw_config.ctxt_to_hw_map = ctx_custom->hw_ctx;
  1087. custom_start.hw_config.request_id = req->request_id;
  1088. custom_start.hw_config.hw_update_entries = req_custom->cfg;
  1089. custom_start.hw_config.num_hw_update_entries = req_custom->num_cfg;
  1090. custom_start.hw_config.priv = &req_custom->hw_update_data;
  1091. custom_start.hw_config.init_packet = 1;
  1092. if (ctx->state == CAM_CTX_FLUSHED)
  1093. custom_start.start_only = true;
  1094. else
  1095. custom_start.start_only = false;
  1096. ctx_custom->frame_id = 0;
  1097. ctx_custom->active_req_cnt = 0;
  1098. ctx_custom->substate_activated =
  1099. (req_custom->num_fence_map_out) ?
  1100. CAM_CUSTOM_CTX_ACTIVATED_APPLIED :
  1101. CAM_CUSTOM_CTX_ACTIVATED_SOF;
  1102. ctx->state = CAM_CTX_ACTIVATED;
  1103. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  1104. &custom_start);
  1105. if (rc) {
  1106. /* HW failure. User need to clean up the resource */
  1107. CAM_ERR(CAM_CUSTOM, "Start HW failed");
  1108. ctx->state = CAM_CTX_READY;
  1109. goto end;
  1110. }
  1111. CAM_DBG(CAM_CUSTOM, "start device success ctx %u",
  1112. ctx->ctx_id);
  1113. spin_lock_bh(&ctx->lock);
  1114. list_del_init(&req->list);
  1115. list_add_tail(&req->list, &ctx->wait_req_list);
  1116. spin_unlock_bh(&ctx->lock);
  1117. end:
  1118. return rc;
  1119. }
  1120. static int __cam_custom_ctx_release_dev_in_activated(struct cam_context *ctx,
  1121. struct cam_release_dev_cmd *cmd)
  1122. {
  1123. int rc = 0;
  1124. rc = __cam_custom_stop_dev_core(ctx, NULL);
  1125. if (rc)
  1126. CAM_ERR(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  1127. rc = __cam_custom_release_dev_in_acquired(ctx, cmd);
  1128. if (rc)
  1129. CAM_ERR(CAM_CUSTOM, "Release device failed rc=%d", rc);
  1130. return rc;
  1131. }
  1132. static int __cam_custom_ctx_unlink_in_activated(struct cam_context *ctx,
  1133. struct cam_req_mgr_core_dev_link_setup *unlink)
  1134. {
  1135. int rc = 0;
  1136. CAM_WARN(CAM_CUSTOM,
  1137. "Received unlink in activated state. It's unexpected");
  1138. rc = __cam_custom_stop_dev_in_activated(ctx, NULL);
  1139. if (rc)
  1140. CAM_WARN(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  1141. rc = __cam_custom_ctx_unlink_in_ready(ctx, unlink);
  1142. if (rc)
  1143. CAM_ERR(CAM_CUSTOM, "Unlink failed rc=%d", rc);
  1144. return rc;
  1145. }
  1146. static int __cam_custom_ctx_process_evt(struct cam_context *ctx,
  1147. struct cam_req_mgr_link_evt_data *link_evt_data)
  1148. {
  1149. switch (link_evt_data->evt_type) {
  1150. case CAM_REQ_MGR_LINK_EVT_ERR:
  1151. /* Handle error/bubble related issues */
  1152. break;
  1153. default:
  1154. CAM_WARN(CAM_CUSTOM, "Unknown event from CRM");
  1155. break;
  1156. }
  1157. return 0;
  1158. }
  1159. static int __cam_custom_ctx_handle_irq_in_activated(void *context,
  1160. uint32_t evt_id, void *evt_data)
  1161. {
  1162. int rc = 0;
  1163. struct cam_custom_ctx_irq_ops *custom_irq_ops = NULL;
  1164. struct cam_context *ctx = (struct cam_context *)context;
  1165. struct cam_custom_context *ctx_custom =
  1166. (struct cam_custom_context *)ctx->ctx_priv;
  1167. spin_lock(&ctx->lock);
  1168. CAM_DBG(CAM_CUSTOM, "Enter: State %d, Substate %d, evt id %d",
  1169. ctx->state, ctx_custom->substate_activated, evt_id);
  1170. custom_irq_ops = &ctx_custom->substate_machine_irq[
  1171. ctx_custom->substate_activated];
  1172. if (custom_irq_ops->irq_ops[evt_id])
  1173. rc = custom_irq_ops->irq_ops[evt_id](ctx_custom,
  1174. evt_data);
  1175. else
  1176. CAM_DBG(CAM_CUSTOM, "No handle function for substate %d",
  1177. ctx_custom->substate_activated);
  1178. CAM_DBG(CAM_CUSTOM, "Exit: State %d Substate %d",
  1179. ctx->state, ctx_custom->substate_activated);
  1180. spin_unlock(&ctx->lock);
  1181. return rc;
  1182. }
  1183. static int __cam_custom_ctx_acquire_hw_in_acquired(
  1184. struct cam_context *ctx, void *args)
  1185. {
  1186. int rc = -EINVAL;
  1187. uint32_t api_version;
  1188. if (!ctx || !args) {
  1189. CAM_ERR(CAM_CUSTOM, "Invalid input pointer");
  1190. return rc;
  1191. }
  1192. api_version = *((uint32_t *)args);
  1193. if (api_version == 1)
  1194. rc = __cam_custom_ctx_acquire_hw_v1(ctx, args);
  1195. else
  1196. CAM_ERR(CAM_CUSTOM, "Unsupported api version %d",
  1197. api_version);
  1198. return rc;
  1199. }
  1200. static int __cam_custom_ctx_apply_req(struct cam_context *ctx,
  1201. struct cam_req_mgr_apply_request *apply)
  1202. {
  1203. int rc = 0;
  1204. struct cam_ctx_ops *ctx_ops = NULL;
  1205. struct cam_custom_context *custom_ctx =
  1206. (struct cam_custom_context *) ctx->ctx_priv;
  1207. CAM_DBG(CAM_CUSTOM,
  1208. "Enter: apply req in Substate %d request _id:%lld",
  1209. custom_ctx->substate_activated, apply->request_id);
  1210. ctx_ops = &custom_ctx->substate_machine[
  1211. custom_ctx->substate_activated];
  1212. if (ctx_ops->crm_ops.apply_req) {
  1213. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  1214. } else {
  1215. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1216. "No handle function in activated substate %d",
  1217. custom_ctx->substate_activated);
  1218. rc = -EFAULT;
  1219. }
  1220. if (rc)
  1221. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1222. "Apply failed in active substate %d rc %d",
  1223. custom_ctx->substate_activated, rc);
  1224. return rc;
  1225. }
  1226. static int __cam_custom_ctx_apply_default_req(
  1227. struct cam_context *ctx,
  1228. struct cam_req_mgr_apply_request *apply)
  1229. {
  1230. int rc = 0;
  1231. struct cam_ctx_ops *ctx_ops = NULL;
  1232. struct cam_custom_context *custom_ctx =
  1233. (struct cam_custom_context *) ctx->ctx_priv;
  1234. CAM_DBG(CAM_CUSTOM,
  1235. "Enter: apply req in Substate %d request _id:%lld",
  1236. custom_ctx->substate_activated, apply->request_id);
  1237. ctx_ops = &custom_ctx->substate_machine[
  1238. custom_ctx->substate_activated];
  1239. if (ctx_ops->crm_ops.notify_frame_skip) {
  1240. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  1241. } else {
  1242. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1243. "No handle function in activated substate %d",
  1244. custom_ctx->substate_activated);
  1245. rc = -EFAULT;
  1246. }
  1247. if (rc)
  1248. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1249. "Apply default failed in active substate %d rc %d",
  1250. custom_ctx->substate_activated, rc);
  1251. return rc;
  1252. }
  1253. /* top state machine */
  1254. static struct cam_ctx_ops
  1255. cam_custom_dev_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  1256. /* Uninit */
  1257. {
  1258. .ioctl_ops = {},
  1259. .crm_ops = {},
  1260. .irq_ops = NULL,
  1261. },
  1262. /* Available */
  1263. {
  1264. .ioctl_ops = {
  1265. .acquire_dev =
  1266. __cam_custom_ctx_acquire_dev_in_available,
  1267. },
  1268. .crm_ops = {},
  1269. .irq_ops = NULL,
  1270. },
  1271. /* Acquired */
  1272. {
  1273. .ioctl_ops = {
  1274. .acquire_hw = __cam_custom_ctx_acquire_hw_in_acquired,
  1275. .release_dev = __cam_custom_release_dev_in_acquired,
  1276. .config_dev = __cam_custom_ctx_config_dev_in_acquired,
  1277. .release_hw = __cam_custom_ctx_release_hw_in_top_state,
  1278. },
  1279. .crm_ops = {
  1280. .link = __cam_custom_ctx_link_in_acquired,
  1281. .unlink = __cam_custom_ctx_unlink_in_acquired,
  1282. .get_dev_info =
  1283. __cam_custom_ctx_get_dev_info_in_acquired,
  1284. .flush_req = __cam_custom_ctx_flush_req_in_top_state,
  1285. },
  1286. .irq_ops = NULL,
  1287. .pagefault_ops = NULL,
  1288. },
  1289. /* Ready */
  1290. {
  1291. .ioctl_ops = {
  1292. .start_dev = __cam_custom_ctx_start_dev_in_ready,
  1293. .release_dev = __cam_custom_release_dev_in_acquired,
  1294. .config_dev = __cam_custom_ctx_config_dev,
  1295. .release_hw = __cam_custom_ctx_release_hw_in_top_state,
  1296. },
  1297. .crm_ops = {
  1298. .unlink = __cam_custom_ctx_unlink_in_ready,
  1299. .flush_req = __cam_custom_ctx_flush_req_in_ready,
  1300. },
  1301. .irq_ops = NULL,
  1302. .pagefault_ops = NULL,
  1303. },
  1304. /* Flushed */
  1305. {
  1306. .ioctl_ops = {
  1307. .stop_dev = __cam_custom_stop_dev_in_activated,
  1308. .release_dev =
  1309. __cam_custom_ctx_release_dev_in_activated,
  1310. .config_dev = __cam_custom_ctx_config_dev_in_flushed,
  1311. .release_hw =
  1312. __cam_custom_ctx_release_hw_in_activated_state,
  1313. },
  1314. .crm_ops = {
  1315. .unlink = __cam_custom_ctx_unlink_in_ready,
  1316. },
  1317. .irq_ops = NULL,
  1318. },
  1319. /* Activated */
  1320. {
  1321. .ioctl_ops = {
  1322. .stop_dev = __cam_custom_stop_dev_in_activated,
  1323. .release_dev =
  1324. __cam_custom_ctx_release_dev_in_activated,
  1325. .config_dev = __cam_custom_ctx_config_dev,
  1326. .release_hw =
  1327. __cam_custom_ctx_release_hw_in_activated_state,
  1328. },
  1329. .crm_ops = {
  1330. .unlink = __cam_custom_ctx_unlink_in_activated,
  1331. .apply_req = __cam_custom_ctx_apply_req,
  1332. .notify_frame_skip =
  1333. __cam_custom_ctx_apply_default_req,
  1334. .flush_req = __cam_custom_ctx_flush_req_in_top_state,
  1335. .process_evt = __cam_custom_ctx_process_evt,
  1336. },
  1337. .irq_ops = __cam_custom_ctx_handle_irq_in_activated,
  1338. .pagefault_ops = NULL,
  1339. },
  1340. };
  1341. int cam_custom_dev_context_init(struct cam_custom_context *ctx,
  1342. struct cam_context *ctx_base,
  1343. struct cam_req_mgr_kmd_ops *crm_node_intf,
  1344. struct cam_hw_mgr_intf *hw_intf,
  1345. uint32_t ctx_id)
  1346. {
  1347. int rc = -1, i = 0;
  1348. if (!ctx || !ctx_base) {
  1349. CAM_ERR(CAM_CUSTOM, "Invalid Context");
  1350. return -EINVAL;
  1351. }
  1352. /* Custom HW context setup */
  1353. memset(ctx, 0, sizeof(*ctx));
  1354. ctx->base = ctx_base;
  1355. ctx->frame_id = 0;
  1356. ctx->active_req_cnt = 0;
  1357. ctx->hw_ctx = NULL;
  1358. for (i = 0; i < CAM_CTX_REQ_MAX; i++) {
  1359. ctx->req_base[i].req_priv = &ctx->req_custom[i];
  1360. ctx->req_custom[i].base = &ctx->req_base[i];
  1361. }
  1362. /* camera context setup */
  1363. rc = cam_context_init(ctx_base, custom_dev_name, CAM_CUSTOM, ctx_id,
  1364. crm_node_intf, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  1365. if (rc) {
  1366. CAM_ERR(CAM_CUSTOM, "Camera Context Base init failed");
  1367. return rc;
  1368. }
  1369. /* link camera context with custom HW context */
  1370. ctx_base->state_machine = cam_custom_dev_ctx_top_state_machine;
  1371. ctx_base->ctx_priv = ctx;
  1372. return rc;
  1373. }
  1374. int cam_custom_dev_context_deinit(struct cam_custom_context *ctx)
  1375. {
  1376. if (ctx->base)
  1377. cam_context_deinit(ctx->base);
  1378. memset(ctx, 0, sizeof(*ctx));
  1379. return 0;
  1380. }