cam_custom_context.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  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. flush_req.req_id = 0;
  559. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  560. spin_lock_bh(&ctx->lock);
  561. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list,
  562. &flush_req);
  563. spin_unlock_bh(&ctx->lock);
  564. ctx->state = CAM_CTX_ACQUIRED;
  565. CAM_DBG(CAM_CUSTOM, "Release HW success[%u] next state %d",
  566. ctx->ctx_id, ctx->state);
  567. return rc;
  568. }
  569. static int __cam_custom_ctx_release_hw_in_activated_state(
  570. struct cam_context *ctx, void *cmd)
  571. {
  572. int rc = 0;
  573. rc = __cam_custom_stop_dev_in_activated(ctx, NULL);
  574. if (rc)
  575. CAM_ERR(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  576. rc = __cam_custom_ctx_release_hw_in_top_state(ctx, cmd);
  577. if (rc)
  578. CAM_ERR(CAM_CUSTOM, "Release hw failed rc=%d", rc);
  579. return rc;
  580. }
  581. static int __cam_custom_release_dev_in_acquired(struct cam_context *ctx,
  582. struct cam_release_dev_cmd *cmd)
  583. {
  584. int rc;
  585. struct cam_custom_context *ctx_custom =
  586. (struct cam_custom_context *) ctx->ctx_priv;
  587. struct cam_req_mgr_flush_request flush_req;
  588. if (cmd && ctx_custom->hw_ctx) {
  589. CAM_ERR(CAM_CUSTOM, "releasing hw");
  590. __cam_custom_ctx_release_hw_in_top_state(ctx, NULL);
  591. }
  592. ctx->ctx_crm_intf = NULL;
  593. ctx->last_flush_req = 0;
  594. ctx_custom->frame_id = 0;
  595. ctx_custom->active_req_cnt = 0;
  596. ctx_custom->hw_acquired = false;
  597. ctx_custom->init_received = false;
  598. if (!list_empty(&ctx->active_req_list))
  599. CAM_ERR(CAM_CUSTOM, "Active list is not empty");
  600. /* Flush all the pending request list */
  601. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  602. flush_req.link_hdl = ctx->link_hdl;
  603. flush_req.dev_hdl = ctx->dev_hdl;
  604. flush_req.req_id = 0;
  605. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  606. spin_lock_bh(&ctx->lock);
  607. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list,
  608. &flush_req);
  609. spin_unlock_bh(&ctx->lock);
  610. ctx->state = CAM_CTX_AVAILABLE;
  611. CAM_DBG(CAM_CUSTOM, "Release device success[%u] next state %d",
  612. ctx->ctx_id, ctx->state);
  613. return rc;
  614. }
  615. static int __cam_custom_ctx_apply_default_settings(
  616. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  617. {
  618. int rc = 0;
  619. struct cam_custom_context *custom_ctx =
  620. (struct cam_custom_context *) ctx->ctx_priv;
  621. struct cam_hw_cmd_args hw_cmd_args;
  622. struct cam_custom_hw_cmd_args custom_hw_cmd_args;
  623. hw_cmd_args.ctxt_to_hw_map = custom_ctx->hw_ctx;
  624. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  625. custom_hw_cmd_args.cmd_type =
  626. CAM_CUSTOM_HW_MGR_PROG_DEFAULT_CONFIG;
  627. hw_cmd_args.u.internal_args = (void *)&custom_hw_cmd_args;
  628. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  629. &hw_cmd_args);
  630. if (rc)
  631. CAM_ERR(CAM_CUSTOM,
  632. "Failed to apply default settings rc %d", rc);
  633. else
  634. CAM_DBG(CAM_CUSTOM, "Applied default settings rc %d", rc);
  635. return rc;
  636. }
  637. static int __cam_custom_ctx_apply_req_in_activated_state(
  638. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  639. uint32_t next_state)
  640. {
  641. int rc = 0;
  642. struct cam_ctx_request *req;
  643. struct cam_custom_dev_ctx_req *req_custom;
  644. struct cam_custom_context *custom_ctx = NULL;
  645. struct cam_hw_config_args cfg;
  646. if (list_empty(&ctx->pending_req_list)) {
  647. CAM_ERR(CAM_CUSTOM, "No available request for Apply id %lld",
  648. apply->request_id);
  649. rc = -EFAULT;
  650. goto end;
  651. }
  652. if (!list_empty(&ctx->wait_req_list))
  653. CAM_WARN(CAM_CUSTOM, "Apply invoked with a req in wait list");
  654. custom_ctx = (struct cam_custom_context *) ctx->ctx_priv;
  655. spin_lock_bh(&ctx->lock);
  656. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  657. list);
  658. spin_unlock_bh(&ctx->lock);
  659. /*
  660. * Check whether the request id is matching the tip
  661. */
  662. if (req->request_id != apply->request_id) {
  663. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  664. "Invalid Request Id asking %llu existing %llu",
  665. apply->request_id, req->request_id);
  666. rc = -EFAULT;
  667. goto end;
  668. }
  669. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  670. cfg.ctxt_to_hw_map = custom_ctx->hw_ctx;
  671. cfg.request_id = req->request_id;
  672. cfg.hw_update_entries = req_custom->cfg;
  673. cfg.num_hw_update_entries = req_custom->num_cfg;
  674. cfg.priv = &req_custom->hw_update_data;
  675. cfg.init_packet = 0;
  676. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  677. if (rc) {
  678. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  679. "Can not apply the configuration");
  680. } else {
  681. spin_lock_bh(&ctx->lock);
  682. custom_ctx->substate_activated = next_state;
  683. list_del_init(&req->list);
  684. list_add_tail(&req->list, &ctx->wait_req_list);
  685. spin_unlock_bh(&ctx->lock);
  686. }
  687. end:
  688. return rc;
  689. }
  690. static int __cam_custom_ctx_acquire_hw_v1(
  691. struct cam_context *ctx, void *args)
  692. {
  693. int rc = 0;
  694. struct cam_acquire_hw_cmd_v1 *cmd =
  695. (struct cam_acquire_hw_cmd_v1 *)args;
  696. struct cam_hw_acquire_args param;
  697. struct cam_custom_context *ctx_custom =
  698. (struct cam_custom_context *) ctx->ctx_priv;
  699. struct cam_custom_acquire_hw_info *acquire_hw_info = NULL;
  700. if (!ctx->hw_mgr_intf) {
  701. CAM_ERR(CAM_CUSTOM, "HW interface is not ready");
  702. rc = -EFAULT;
  703. goto end;
  704. }
  705. CAM_DBG(CAM_CUSTOM,
  706. "session_hdl 0x%x, hdl type %d, res %lld",
  707. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  708. if (cmd->handle_type != 1) {
  709. CAM_ERR(CAM_CUSTOM, "Only user pointer is supported");
  710. rc = -EINVAL;
  711. goto end;
  712. }
  713. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  714. CAM_ERR(CAM_CUSTOM, "data_size is not a valid value");
  715. goto end;
  716. }
  717. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  718. if (!acquire_hw_info) {
  719. rc = -ENOMEM;
  720. goto end;
  721. }
  722. CAM_DBG(CAM_CUSTOM, "start copy resources from user");
  723. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  724. cmd->data_size)) {
  725. rc = -EFAULT;
  726. goto free_res;
  727. }
  728. memset(&param, 0, sizeof(param));
  729. param.context_data = ctx;
  730. param.event_cb = ctx->irq_cb_intf;
  731. param.acquire_info_size = cmd->data_size;
  732. param.acquire_info = (uint64_t) acquire_hw_info;
  733. /* call HW manager to reserve the resource */
  734. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  735. &param);
  736. if (rc != 0) {
  737. CAM_ERR(CAM_CUSTOM, "Acquire HW failed");
  738. goto free_res;
  739. }
  740. ctx_custom->substate_machine_irq =
  741. cam_custom_ctx_activated_state_machine_irq;
  742. ctx_custom->substate_machine =
  743. cam_custom_ctx_activated_state_machine;
  744. ctx_custom->hw_ctx = param.ctxt_to_hw_map;
  745. ctx_custom->hw_acquired = true;
  746. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  747. CAM_DBG(CAM_CUSTOM,
  748. "Acquire HW success on session_hdl 0x%xs for ctx_id %u",
  749. ctx->session_hdl, ctx->ctx_id);
  750. kfree(acquire_hw_info);
  751. return rc;
  752. free_res:
  753. kfree(acquire_hw_info);
  754. end:
  755. return rc;
  756. }
  757. static int __cam_custom_ctx_acquire_dev_in_available(
  758. struct cam_context *ctx, struct cam_acquire_dev_cmd *cmd)
  759. {
  760. int rc = 0;
  761. struct cam_create_dev_hdl req_hdl_param;
  762. if (!ctx->hw_mgr_intf) {
  763. CAM_ERR(CAM_CUSTOM, "HW interface is not ready");
  764. rc = -EFAULT;
  765. return rc;
  766. }
  767. CAM_DBG(CAM_CUSTOM,
  768. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  769. cmd->session_handle, cmd->num_resources,
  770. cmd->handle_type, cmd->resource_hdl);
  771. if (cmd->num_resources != CAM_API_COMPAT_CONSTANT) {
  772. CAM_ERR(CAM_CUSTOM, "Invalid num_resources 0x%x",
  773. cmd->num_resources);
  774. return -EINVAL;
  775. }
  776. req_hdl_param.session_hdl = cmd->session_handle;
  777. req_hdl_param.v4l2_sub_dev_flag = 0;
  778. req_hdl_param.media_entity_flag = 0;
  779. req_hdl_param.ops = ctx->crm_ctx_intf;
  780. req_hdl_param.priv = ctx;
  781. CAM_DBG(CAM_CUSTOM, "get device handle from bridge");
  782. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  783. if (ctx->dev_hdl <= 0) {
  784. rc = -EFAULT;
  785. CAM_ERR(CAM_CUSTOM, "Can not create device handle");
  786. return rc;
  787. }
  788. cmd->dev_handle = ctx->dev_hdl;
  789. ctx->session_hdl = cmd->session_handle;
  790. ctx->state = CAM_CTX_ACQUIRED;
  791. CAM_DBG(CAM_CUSTOM,
  792. "Acquire dev success on session_hdl 0x%x for ctx %u",
  793. cmd->session_handle, ctx->ctx_id);
  794. return rc;
  795. }
  796. static int __cam_custom_ctx_enqueue_init_request(
  797. struct cam_context *ctx, struct cam_ctx_request *req)
  798. {
  799. int rc = 0;
  800. struct cam_ctx_request *req_old;
  801. struct cam_custom_dev_ctx_req *req_custom_old;
  802. struct cam_custom_dev_ctx_req *req_custom_new;
  803. spin_lock_bh(&ctx->lock);
  804. if (list_empty(&ctx->pending_req_list)) {
  805. list_add_tail(&req->list, &ctx->pending_req_list);
  806. goto end;
  807. }
  808. req_old = list_first_entry(&ctx->pending_req_list,
  809. struct cam_ctx_request, list);
  810. req_custom_old = (struct cam_custom_dev_ctx_req *) req_old->req_priv;
  811. req_custom_new = (struct cam_custom_dev_ctx_req *) req->req_priv;
  812. if (req_custom_old->hw_update_data.packet_opcode_type ==
  813. CAM_CUSTOM_PACKET_INIT_DEV) {
  814. if ((req_custom_old->num_cfg + req_custom_new->num_cfg) >=
  815. CAM_CUSTOM_CTX_CFG_MAX) {
  816. CAM_WARN(CAM_CUSTOM, "Can not merge INIT pkt");
  817. rc = -ENOMEM;
  818. }
  819. if (req_custom_old->num_fence_map_out != 0 ||
  820. req_custom_old->num_fence_map_in != 0) {
  821. CAM_WARN(CAM_CUSTOM, "Invalid INIT pkt sequence");
  822. rc = -EINVAL;
  823. }
  824. if (!rc) {
  825. memcpy(req_custom_old->fence_map_out,
  826. req_custom_new->fence_map_out,
  827. sizeof(req_custom_new->fence_map_out[0])*
  828. req_custom_new->num_fence_map_out);
  829. req_custom_old->num_fence_map_out =
  830. req_custom_new->num_fence_map_out;
  831. memcpy(req_custom_old->fence_map_in,
  832. req_custom_new->fence_map_in,
  833. sizeof(req_custom_new->fence_map_in[0])*
  834. req_custom_new->num_fence_map_in);
  835. req_custom_old->num_fence_map_in =
  836. req_custom_new->num_fence_map_in;
  837. memcpy(&req_custom_old->cfg[req_custom_old->num_cfg],
  838. req_custom_new->cfg,
  839. sizeof(req_custom_new->cfg[0])*
  840. req_custom_new->num_cfg);
  841. req_custom_old->num_cfg += req_custom_new->num_cfg;
  842. req_old->request_id = req->request_id;
  843. list_add_tail(&req->list, &ctx->free_req_list);
  844. }
  845. } else {
  846. CAM_WARN(CAM_CUSTOM,
  847. "Received Update pkt before INIT pkt. req_id= %lld",
  848. req->request_id);
  849. rc = -EINVAL;
  850. }
  851. end:
  852. spin_unlock_bh(&ctx->lock);
  853. return rc;
  854. }
  855. static int __cam_custom_ctx_config_dev(struct cam_context *ctx,
  856. struct cam_config_dev_cmd *cmd)
  857. {
  858. int rc = 0, i;
  859. struct cam_ctx_request *req = NULL;
  860. struct cam_custom_dev_ctx_req *req_custom;
  861. uintptr_t packet_addr;
  862. struct cam_packet *packet;
  863. size_t len = 0;
  864. struct cam_hw_prepare_update_args cfg;
  865. struct cam_req_mgr_add_request add_req;
  866. struct cam_custom_context *ctx_custom =
  867. (struct cam_custom_context *) ctx->ctx_priv;
  868. /* get free request */
  869. spin_lock_bh(&ctx->lock);
  870. if (!list_empty(&ctx->free_req_list)) {
  871. req = list_first_entry(&ctx->free_req_list,
  872. struct cam_ctx_request, list);
  873. list_del_init(&req->list);
  874. }
  875. spin_unlock_bh(&ctx->lock);
  876. if (!req) {
  877. CAM_ERR(CAM_CUSTOM, "No more request obj free");
  878. return -ENOMEM;
  879. }
  880. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  881. /* for config dev, only memory handle is supported */
  882. /* map packet from the memhandle */
  883. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  884. &packet_addr, &len);
  885. if (rc != 0) {
  886. CAM_ERR(CAM_CUSTOM, "Can not get packet address");
  887. rc = -EINVAL;
  888. goto free_req;
  889. }
  890. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  891. CAM_DBG(CAM_CUSTOM, "pack_handle %llx", cmd->packet_handle);
  892. CAM_DBG(CAM_CUSTOM, "packet address is 0x%zx", packet_addr);
  893. CAM_DBG(CAM_CUSTOM, "packet with length %zu, offset 0x%llx",
  894. len, cmd->offset);
  895. CAM_DBG(CAM_CUSTOM, "Packet request id %lld",
  896. packet->header.request_id);
  897. CAM_DBG(CAM_CUSTOM, "Packet size 0x%x", packet->header.size);
  898. CAM_DBG(CAM_CUSTOM, "packet op %d", packet->header.op_code);
  899. if ((((packet->header.op_code) & 0xF) ==
  900. CAM_CUSTOM_PACKET_UPDATE_DEV)
  901. && (packet->header.request_id <= ctx->last_flush_req)) {
  902. CAM_DBG(CAM_CUSTOM,
  903. "request %lld has been flushed, reject packet",
  904. packet->header.request_id);
  905. rc = -EINVAL;
  906. goto free_req;
  907. }
  908. /* preprocess the configuration */
  909. memset(&cfg, 0, sizeof(cfg));
  910. cfg.packet = packet;
  911. cfg.ctxt_to_hw_map = ctx_custom->hw_ctx;
  912. cfg.out_map_entries = req_custom->fence_map_out;
  913. cfg.max_out_map_entries = CAM_CUSTOM_DEV_CTX_RES_MAX;
  914. cfg.in_map_entries = req_custom->fence_map_in;
  915. cfg.max_in_map_entries = CAM_CUSTOM_DEV_CTX_RES_MAX;
  916. cfg.priv = &req_custom->hw_update_data;
  917. cfg.pf_data = &(req->pf_data);
  918. rc = ctx->hw_mgr_intf->hw_prepare_update(
  919. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  920. if (rc != 0) {
  921. CAM_ERR(CAM_CUSTOM, "Prepare config packet failed in HW layer");
  922. rc = -EFAULT;
  923. goto free_req;
  924. }
  925. req_custom->num_cfg = cfg.num_hw_update_entries;
  926. req_custom->num_fence_map_out = cfg.num_out_map_entries;
  927. req_custom->num_fence_map_in = cfg.num_in_map_entries;
  928. req_custom->num_acked = 0;
  929. req_custom->hw_update_data.num_cfg = cfg.num_out_map_entries;
  930. for (i = 0; i < req_custom->num_fence_map_out; i++) {
  931. rc = cam_sync_get_obj_ref(req_custom->fence_map_out[i].sync_id);
  932. if (rc) {
  933. CAM_ERR(CAM_CUSTOM, "Can't get ref for fence %d",
  934. req_custom->fence_map_out[i].sync_id);
  935. goto put_ref;
  936. }
  937. }
  938. CAM_DBG(CAM_CUSTOM,
  939. "num_entry: %d, num fence out: %d, num fence in: %d",
  940. req_custom->num_cfg, req_custom->num_fence_map_out,
  941. req_custom->num_fence_map_in);
  942. req->request_id = packet->header.request_id;
  943. req->status = 1;
  944. CAM_DBG(CAM_CUSTOM, "Packet request id %lld packet opcode:%d",
  945. packet->header.request_id,
  946. req_custom->hw_update_data.packet_opcode_type);
  947. if (req_custom->hw_update_data.packet_opcode_type ==
  948. CAM_CUSTOM_PACKET_INIT_DEV) {
  949. if (ctx->state < CAM_CTX_ACTIVATED) {
  950. rc = __cam_custom_ctx_enqueue_init_request(ctx, req);
  951. if (rc)
  952. CAM_ERR(CAM_CUSTOM, "Enqueue INIT pkt failed");
  953. ctx_custom->init_received = true;
  954. } else {
  955. rc = -EINVAL;
  956. CAM_ERR(CAM_CUSTOM, "Recevied INIT pkt in wrong state");
  957. }
  958. } else {
  959. if ((ctx->state != CAM_CTX_FLUSHED) &&
  960. (ctx->state >= CAM_CTX_READY) &&
  961. (ctx->ctx_crm_intf->add_req)) {
  962. add_req.link_hdl = ctx->link_hdl;
  963. add_req.dev_hdl = ctx->dev_hdl;
  964. add_req.req_id = req->request_id;
  965. add_req.skip_before_applying = 0;
  966. rc = ctx->ctx_crm_intf->add_req(&add_req);
  967. if (rc) {
  968. CAM_ERR(CAM_CUSTOM,
  969. "Add req failed: req id=%llu",
  970. req->request_id);
  971. } else {
  972. __cam_custom_ctx_enqueue_request_in_order(
  973. ctx, req);
  974. }
  975. } else {
  976. rc = -EINVAL;
  977. CAM_ERR(CAM_CUSTOM, "Recevied Update in wrong state");
  978. }
  979. }
  980. if (rc)
  981. goto put_ref;
  982. CAM_DBG(CAM_CUSTOM,
  983. "Preprocessing Config req_id %lld successful on ctx %u",
  984. req->request_id, ctx->ctx_id);
  985. return rc;
  986. put_ref:
  987. for (--i; i >= 0; i--) {
  988. if (cam_sync_put_obj_ref(req_custom->fence_map_out[i].sync_id))
  989. CAM_ERR(CAM_CUSTOM, "Failed to put ref of fence %d",
  990. req_custom->fence_map_out[i].sync_id);
  991. }
  992. free_req:
  993. spin_lock_bh(&ctx->lock);
  994. list_add_tail(&req->list, &ctx->free_req_list);
  995. spin_unlock_bh(&ctx->lock);
  996. return rc;
  997. }
  998. static int __cam_custom_ctx_config_dev_in_flushed(struct cam_context *ctx,
  999. struct cam_config_dev_cmd *cmd)
  1000. {
  1001. int rc = 0;
  1002. struct cam_start_stop_dev_cmd start_cmd;
  1003. struct cam_custom_context *custom_ctx =
  1004. (struct cam_custom_context *) ctx->ctx_priv;
  1005. if (!custom_ctx->hw_acquired) {
  1006. CAM_ERR(CAM_CUSTOM, "HW is not acquired, reject packet");
  1007. rc = -EINVAL;
  1008. goto end;
  1009. }
  1010. rc = __cam_custom_ctx_config_dev(ctx, cmd);
  1011. if (rc)
  1012. goto end;
  1013. if (!custom_ctx->init_received) {
  1014. CAM_WARN(CAM_CUSTOM,
  1015. "Received update packet in flushed state, skip start");
  1016. goto end;
  1017. }
  1018. start_cmd.dev_handle = cmd->dev_handle;
  1019. start_cmd.session_handle = cmd->session_handle;
  1020. rc = __cam_custom_ctx_start_dev_in_ready(ctx, &start_cmd);
  1021. if (rc)
  1022. CAM_ERR(CAM_CUSTOM,
  1023. "Failed to re-start HW after flush rc: %d", rc);
  1024. else
  1025. CAM_INFO(CAM_CUSTOM,
  1026. "Received init after flush. Re-start HW complete.");
  1027. end:
  1028. return rc;
  1029. }
  1030. static int __cam_custom_ctx_config_dev_in_acquired(struct cam_context *ctx,
  1031. struct cam_config_dev_cmd *cmd)
  1032. {
  1033. int rc = 0;
  1034. struct cam_custom_context *ctx_custom =
  1035. (struct cam_custom_context *) ctx->ctx_priv;
  1036. if (!ctx_custom->hw_acquired) {
  1037. CAM_ERR(CAM_CUSTOM, "HW not acquired, reject config packet");
  1038. return -EAGAIN;
  1039. }
  1040. rc = __cam_custom_ctx_config_dev(ctx, cmd);
  1041. if (!rc && (ctx->link_hdl >= 0))
  1042. ctx->state = CAM_CTX_READY;
  1043. return rc;
  1044. }
  1045. static int __cam_custom_ctx_link_in_acquired(struct cam_context *ctx,
  1046. struct cam_req_mgr_core_dev_link_setup *link)
  1047. {
  1048. struct cam_custom_context *ctx_custom =
  1049. (struct cam_custom_context *) ctx->ctx_priv;
  1050. ctx->link_hdl = link->link_hdl;
  1051. ctx->ctx_crm_intf = link->crm_cb;
  1052. ctx_custom->subscribe_event = link->subscribe_event;
  1053. /* change state only if we had the init config */
  1054. if (ctx_custom->init_received)
  1055. ctx->state = CAM_CTX_READY;
  1056. CAM_DBG(CAM_CUSTOM, "next state %d", ctx->state);
  1057. return 0;
  1058. }
  1059. static int __cam_custom_ctx_start_dev_in_ready(struct cam_context *ctx,
  1060. struct cam_start_stop_dev_cmd *cmd)
  1061. {
  1062. int rc = 0;
  1063. struct cam_custom_start_args custom_start;
  1064. struct cam_ctx_request *req;
  1065. struct cam_custom_dev_ctx_req *req_custom;
  1066. struct cam_custom_context *ctx_custom =
  1067. (struct cam_custom_context *) ctx->ctx_priv;
  1068. if (cmd->session_handle != ctx->session_hdl ||
  1069. cmd->dev_handle != ctx->dev_hdl) {
  1070. rc = -EPERM;
  1071. goto end;
  1072. }
  1073. if (list_empty(&ctx->pending_req_list)) {
  1074. /* should never happen */
  1075. CAM_ERR(CAM_CUSTOM, "Start device with empty configuration");
  1076. rc = -EFAULT;
  1077. goto end;
  1078. } else {
  1079. req = list_first_entry(&ctx->pending_req_list,
  1080. struct cam_ctx_request, list);
  1081. }
  1082. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  1083. if (!ctx_custom->hw_ctx) {
  1084. CAM_ERR(CAM_CUSTOM, "Wrong hw context pointer.");
  1085. rc = -EFAULT;
  1086. goto end;
  1087. }
  1088. custom_start.hw_config.ctxt_to_hw_map = ctx_custom->hw_ctx;
  1089. custom_start.hw_config.request_id = req->request_id;
  1090. custom_start.hw_config.hw_update_entries = req_custom->cfg;
  1091. custom_start.hw_config.num_hw_update_entries = req_custom->num_cfg;
  1092. custom_start.hw_config.priv = &req_custom->hw_update_data;
  1093. custom_start.hw_config.init_packet = 1;
  1094. if (ctx->state == CAM_CTX_FLUSHED)
  1095. custom_start.start_only = true;
  1096. else
  1097. custom_start.start_only = false;
  1098. ctx_custom->frame_id = 0;
  1099. ctx_custom->active_req_cnt = 0;
  1100. ctx_custom->substate_activated =
  1101. (req_custom->num_fence_map_out) ?
  1102. CAM_CUSTOM_CTX_ACTIVATED_APPLIED :
  1103. CAM_CUSTOM_CTX_ACTIVATED_SOF;
  1104. ctx->state = CAM_CTX_ACTIVATED;
  1105. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  1106. &custom_start);
  1107. if (rc) {
  1108. /* HW failure. User need to clean up the resource */
  1109. CAM_ERR(CAM_CUSTOM, "Start HW failed");
  1110. ctx->state = CAM_CTX_READY;
  1111. goto end;
  1112. }
  1113. CAM_DBG(CAM_CUSTOM, "start device success ctx %u",
  1114. ctx->ctx_id);
  1115. spin_lock_bh(&ctx->lock);
  1116. list_del_init(&req->list);
  1117. list_add_tail(&req->list, &ctx->wait_req_list);
  1118. spin_unlock_bh(&ctx->lock);
  1119. end:
  1120. return rc;
  1121. }
  1122. static int __cam_custom_ctx_release_dev_in_activated(struct cam_context *ctx,
  1123. struct cam_release_dev_cmd *cmd)
  1124. {
  1125. int rc = 0;
  1126. rc = __cam_custom_stop_dev_core(ctx, NULL);
  1127. if (rc)
  1128. CAM_ERR(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  1129. rc = __cam_custom_release_dev_in_acquired(ctx, cmd);
  1130. if (rc)
  1131. CAM_ERR(CAM_CUSTOM, "Release device failed rc=%d", rc);
  1132. return rc;
  1133. }
  1134. static int __cam_custom_ctx_unlink_in_activated(struct cam_context *ctx,
  1135. struct cam_req_mgr_core_dev_link_setup *unlink)
  1136. {
  1137. int rc = 0;
  1138. CAM_WARN(CAM_CUSTOM,
  1139. "Received unlink in activated state. It's unexpected");
  1140. rc = __cam_custom_stop_dev_in_activated(ctx, NULL);
  1141. if (rc)
  1142. CAM_WARN(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  1143. rc = __cam_custom_ctx_unlink_in_ready(ctx, unlink);
  1144. if (rc)
  1145. CAM_ERR(CAM_CUSTOM, "Unlink failed rc=%d", rc);
  1146. return rc;
  1147. }
  1148. static int __cam_custom_ctx_process_evt(struct cam_context *ctx,
  1149. struct cam_req_mgr_link_evt_data *link_evt_data)
  1150. {
  1151. switch (link_evt_data->evt_type) {
  1152. case CAM_REQ_MGR_LINK_EVT_ERR:
  1153. /* Handle error/bubble related issues */
  1154. break;
  1155. default:
  1156. CAM_WARN(CAM_CUSTOM, "Unknown event from CRM");
  1157. break;
  1158. }
  1159. return 0;
  1160. }
  1161. static int __cam_custom_ctx_handle_irq_in_activated(void *context,
  1162. uint32_t evt_id, void *evt_data)
  1163. {
  1164. int rc = 0;
  1165. struct cam_custom_ctx_irq_ops *custom_irq_ops = NULL;
  1166. struct cam_context *ctx = (struct cam_context *)context;
  1167. struct cam_custom_context *ctx_custom =
  1168. (struct cam_custom_context *)ctx->ctx_priv;
  1169. spin_lock(&ctx->lock);
  1170. CAM_DBG(CAM_CUSTOM, "Enter: State %d, Substate %d, evt id %d",
  1171. ctx->state, ctx_custom->substate_activated, evt_id);
  1172. custom_irq_ops = &ctx_custom->substate_machine_irq[
  1173. ctx_custom->substate_activated];
  1174. if (custom_irq_ops->irq_ops[evt_id])
  1175. rc = custom_irq_ops->irq_ops[evt_id](ctx_custom,
  1176. evt_data);
  1177. else
  1178. CAM_DBG(CAM_CUSTOM, "No handle function for substate %d",
  1179. ctx_custom->substate_activated);
  1180. CAM_DBG(CAM_CUSTOM, "Exit: State %d Substate %d",
  1181. ctx->state, ctx_custom->substate_activated);
  1182. spin_unlock(&ctx->lock);
  1183. return rc;
  1184. }
  1185. static int __cam_custom_ctx_acquire_hw_in_acquired(
  1186. struct cam_context *ctx, void *args)
  1187. {
  1188. int rc = -EINVAL;
  1189. uint32_t api_version;
  1190. if (!ctx || !args) {
  1191. CAM_ERR(CAM_CUSTOM, "Invalid input pointer");
  1192. return rc;
  1193. }
  1194. api_version = *((uint32_t *)args);
  1195. if (api_version == 1)
  1196. rc = __cam_custom_ctx_acquire_hw_v1(ctx, args);
  1197. else
  1198. CAM_ERR(CAM_CUSTOM, "Unsupported api version %d",
  1199. api_version);
  1200. return rc;
  1201. }
  1202. static int __cam_custom_ctx_apply_req(struct cam_context *ctx,
  1203. struct cam_req_mgr_apply_request *apply)
  1204. {
  1205. int rc = 0;
  1206. struct cam_ctx_ops *ctx_ops = NULL;
  1207. struct cam_custom_context *custom_ctx =
  1208. (struct cam_custom_context *) ctx->ctx_priv;
  1209. CAM_DBG(CAM_CUSTOM,
  1210. "Enter: apply req in Substate %d request _id:%lld",
  1211. custom_ctx->substate_activated, apply->request_id);
  1212. ctx_ops = &custom_ctx->substate_machine[
  1213. custom_ctx->substate_activated];
  1214. if (ctx_ops->crm_ops.apply_req) {
  1215. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  1216. } else {
  1217. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1218. "No handle function in activated substate %d",
  1219. custom_ctx->substate_activated);
  1220. rc = -EFAULT;
  1221. }
  1222. if (rc)
  1223. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1224. "Apply failed in active substate %d rc %d",
  1225. custom_ctx->substate_activated, rc);
  1226. return rc;
  1227. }
  1228. static int __cam_custom_ctx_apply_default_req(
  1229. struct cam_context *ctx,
  1230. struct cam_req_mgr_apply_request *apply)
  1231. {
  1232. int rc = 0;
  1233. struct cam_ctx_ops *ctx_ops = NULL;
  1234. struct cam_custom_context *custom_ctx =
  1235. (struct cam_custom_context *) ctx->ctx_priv;
  1236. CAM_DBG(CAM_CUSTOM,
  1237. "Enter: apply req in Substate %d request _id:%lld",
  1238. custom_ctx->substate_activated, apply->request_id);
  1239. ctx_ops = &custom_ctx->substate_machine[
  1240. custom_ctx->substate_activated];
  1241. if (ctx_ops->crm_ops.notify_frame_skip) {
  1242. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  1243. } else {
  1244. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1245. "No handle function in activated substate %d",
  1246. custom_ctx->substate_activated);
  1247. rc = -EFAULT;
  1248. }
  1249. if (rc)
  1250. CAM_WARN_RATE_LIMIT(CAM_CUSTOM,
  1251. "Apply default failed in active substate %d rc %d",
  1252. custom_ctx->substate_activated, rc);
  1253. return rc;
  1254. }
  1255. /* top state machine */
  1256. static struct cam_ctx_ops
  1257. cam_custom_dev_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  1258. /* Uninit */
  1259. {
  1260. .ioctl_ops = {},
  1261. .crm_ops = {},
  1262. .irq_ops = NULL,
  1263. },
  1264. /* Available */
  1265. {
  1266. .ioctl_ops = {
  1267. .acquire_dev =
  1268. __cam_custom_ctx_acquire_dev_in_available,
  1269. },
  1270. .crm_ops = {},
  1271. .irq_ops = NULL,
  1272. },
  1273. /* Acquired */
  1274. {
  1275. .ioctl_ops = {
  1276. .acquire_hw = __cam_custom_ctx_acquire_hw_in_acquired,
  1277. .release_dev = __cam_custom_release_dev_in_acquired,
  1278. .config_dev = __cam_custom_ctx_config_dev_in_acquired,
  1279. .release_hw = __cam_custom_ctx_release_hw_in_top_state,
  1280. },
  1281. .crm_ops = {
  1282. .link = __cam_custom_ctx_link_in_acquired,
  1283. .unlink = __cam_custom_ctx_unlink_in_acquired,
  1284. .get_dev_info =
  1285. __cam_custom_ctx_get_dev_info_in_acquired,
  1286. .flush_req = __cam_custom_ctx_flush_req_in_top_state,
  1287. },
  1288. .irq_ops = NULL,
  1289. .pagefault_ops = NULL,
  1290. },
  1291. /* Ready */
  1292. {
  1293. .ioctl_ops = {
  1294. .start_dev = __cam_custom_ctx_start_dev_in_ready,
  1295. .release_dev = __cam_custom_release_dev_in_acquired,
  1296. .config_dev = __cam_custom_ctx_config_dev,
  1297. .release_hw = __cam_custom_ctx_release_hw_in_top_state,
  1298. },
  1299. .crm_ops = {
  1300. .unlink = __cam_custom_ctx_unlink_in_ready,
  1301. .flush_req = __cam_custom_ctx_flush_req_in_ready,
  1302. },
  1303. .irq_ops = NULL,
  1304. .pagefault_ops = NULL,
  1305. },
  1306. /* Flushed */
  1307. {
  1308. .ioctl_ops = {
  1309. .stop_dev = __cam_custom_stop_dev_in_activated,
  1310. .release_dev =
  1311. __cam_custom_ctx_release_dev_in_activated,
  1312. .config_dev = __cam_custom_ctx_config_dev_in_flushed,
  1313. .release_hw =
  1314. __cam_custom_ctx_release_hw_in_activated_state,
  1315. },
  1316. .crm_ops = {
  1317. .unlink = __cam_custom_ctx_unlink_in_ready,
  1318. },
  1319. .irq_ops = NULL,
  1320. },
  1321. /* Activated */
  1322. {
  1323. .ioctl_ops = {
  1324. .stop_dev = __cam_custom_stop_dev_in_activated,
  1325. .release_dev =
  1326. __cam_custom_ctx_release_dev_in_activated,
  1327. .config_dev = __cam_custom_ctx_config_dev,
  1328. .release_hw =
  1329. __cam_custom_ctx_release_hw_in_activated_state,
  1330. },
  1331. .crm_ops = {
  1332. .unlink = __cam_custom_ctx_unlink_in_activated,
  1333. .apply_req = __cam_custom_ctx_apply_req,
  1334. .notify_frame_skip =
  1335. __cam_custom_ctx_apply_default_req,
  1336. .flush_req = __cam_custom_ctx_flush_req_in_top_state,
  1337. .process_evt = __cam_custom_ctx_process_evt,
  1338. },
  1339. .irq_ops = __cam_custom_ctx_handle_irq_in_activated,
  1340. .pagefault_ops = NULL,
  1341. },
  1342. };
  1343. int cam_custom_dev_context_init(struct cam_custom_context *ctx,
  1344. struct cam_context *ctx_base,
  1345. struct cam_req_mgr_kmd_ops *crm_node_intf,
  1346. struct cam_hw_mgr_intf *hw_intf,
  1347. uint32_t ctx_id)
  1348. {
  1349. int rc = -1, i = 0;
  1350. if (!ctx || !ctx_base) {
  1351. CAM_ERR(CAM_CUSTOM, "Invalid Context");
  1352. return -EINVAL;
  1353. }
  1354. /* Custom HW context setup */
  1355. memset(ctx, 0, sizeof(*ctx));
  1356. ctx->base = ctx_base;
  1357. ctx->frame_id = 0;
  1358. ctx->active_req_cnt = 0;
  1359. ctx->hw_ctx = NULL;
  1360. for (i = 0; i < CAM_CTX_REQ_MAX; i++) {
  1361. ctx->req_base[i].req_priv = &ctx->req_custom[i];
  1362. ctx->req_custom[i].base = &ctx->req_base[i];
  1363. }
  1364. /* camera context setup */
  1365. rc = cam_context_init(ctx_base, custom_dev_name, CAM_CUSTOM, ctx_id,
  1366. crm_node_intf, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  1367. if (rc) {
  1368. CAM_ERR(CAM_CUSTOM, "Camera Context Base init failed");
  1369. return rc;
  1370. }
  1371. /* link camera context with custom HW context */
  1372. ctx_base->state_machine = cam_custom_dev_ctx_top_state_machine;
  1373. ctx_base->ctx_priv = ctx;
  1374. return rc;
  1375. }
  1376. int cam_custom_dev_context_deinit(struct cam_custom_context *ctx)
  1377. {
  1378. if (ctx->base)
  1379. cam_context_deinit(ctx->base);
  1380. memset(ctx, 0, sizeof(*ctx));
  1381. return 0;
  1382. }