cam_custom_context.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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_enqueue_request_in_order(
  25. struct cam_context *ctx, struct cam_ctx_request *req)
  26. {
  27. struct cam_ctx_request *req_current;
  28. struct cam_ctx_request *req_prev;
  29. struct list_head temp_list;
  30. INIT_LIST_HEAD(&temp_list);
  31. spin_lock_bh(&ctx->lock);
  32. if (list_empty(&ctx->pending_req_list)) {
  33. list_add_tail(&req->list, &ctx->pending_req_list);
  34. } else {
  35. list_for_each_entry_safe_reverse(
  36. req_current, req_prev, &ctx->pending_req_list, list) {
  37. if (req->request_id < req_current->request_id) {
  38. list_del_init(&req_current->list);
  39. list_add(&req_current->list, &temp_list);
  40. continue;
  41. } else if (req->request_id == req_current->request_id) {
  42. CAM_WARN(CAM_CUSTOM,
  43. "Received duplicated request %lld",
  44. req->request_id);
  45. }
  46. break;
  47. }
  48. list_add_tail(&req->list, &ctx->pending_req_list);
  49. if (!list_empty(&temp_list)) {
  50. list_for_each_entry_safe(
  51. req_current, req_prev, &temp_list, list) {
  52. list_del_init(&req_current->list);
  53. list_add_tail(&req_current->list,
  54. &ctx->pending_req_list);
  55. }
  56. }
  57. }
  58. spin_unlock_bh(&ctx->lock);
  59. return 0;
  60. }
  61. static int __cam_custom_ctx_flush_req(struct cam_context *ctx,
  62. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  63. {
  64. int i, rc;
  65. uint32_t cancel_req_id_found = 0;
  66. struct cam_ctx_request *req;
  67. struct cam_ctx_request *req_temp;
  68. struct cam_custom_dev_ctx_req *req_custom;
  69. struct list_head flush_list;
  70. INIT_LIST_HEAD(&flush_list);
  71. if (list_empty(req_list)) {
  72. CAM_DBG(CAM_CUSTOM, "request list is empty");
  73. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  74. CAM_ERR(CAM_CUSTOM, "no request to cancel");
  75. return -EINVAL;
  76. } else {
  77. return 0;
  78. }
  79. }
  80. CAM_DBG(CAM_CUSTOM, "Flush [%u] in progress for req_id %llu",
  81. flush_req->type, flush_req->req_id);
  82. list_for_each_entry_safe(req, req_temp, req_list, list) {
  83. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  84. if (req->request_id != flush_req->req_id) {
  85. continue;
  86. } else {
  87. list_del_init(&req->list);
  88. list_add_tail(&req->list, &flush_list);
  89. cancel_req_id_found = 1;
  90. break;
  91. }
  92. }
  93. list_del_init(&req->list);
  94. list_add_tail(&req->list, &flush_list);
  95. }
  96. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  97. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  98. for (i = 0; i < req_custom->num_fence_map_out; i++) {
  99. if (req_custom->fence_map_out[i].sync_id != -1) {
  100. CAM_DBG(CAM_CUSTOM,
  101. "Flush req 0x%llx, fence %d",
  102. req->request_id,
  103. req_custom->fence_map_out[i].sync_id);
  104. rc = cam_sync_signal(
  105. req_custom->fence_map_out[i].sync_id,
  106. CAM_SYNC_STATE_SIGNALED_CANCEL);
  107. if (rc)
  108. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  109. "signal fence failed\n");
  110. req_custom->fence_map_out[i].sync_id = -1;
  111. }
  112. }
  113. list_add_tail(&req->list, &ctx->free_req_list);
  114. }
  115. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
  116. !cancel_req_id_found)
  117. CAM_DBG(CAM_CUSTOM,
  118. "Flush request id:%lld is not found in the list",
  119. flush_req->req_id);
  120. return 0;
  121. }
  122. static int __cam_custom_ctx_unlink_in_acquired(struct cam_context *ctx,
  123. struct cam_req_mgr_core_dev_link_setup *unlink)
  124. {
  125. ctx->link_hdl = -1;
  126. ctx->ctx_crm_intf = NULL;
  127. return 0;
  128. }
  129. static int __cam_custom_ctx_unlink_in_ready(struct cam_context *ctx,
  130. struct cam_req_mgr_core_dev_link_setup *unlink)
  131. {
  132. ctx->link_hdl = -1;
  133. ctx->ctx_crm_intf = NULL;
  134. ctx->state = CAM_CTX_ACQUIRED;
  135. return 0;
  136. }
  137. static int __cam_custom_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  138. struct cam_req_mgr_device_info *dev_info)
  139. {
  140. dev_info->dev_hdl = ctx->dev_hdl;
  141. strlcpy(dev_info->name, CAM_CUSTOM_DEV_NAME, sizeof(dev_info->name));
  142. dev_info->dev_id = CAM_REQ_MGR_DEVICE_CUSTOM_HW;
  143. dev_info->p_delay = 1;
  144. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  145. dev_info->enable_apply_default = true;
  146. return 0;
  147. }
  148. static int __cam_custom_ctx_flush_req_in_top_state(
  149. struct cam_context *ctx,
  150. struct cam_req_mgr_flush_request *flush_req)
  151. {
  152. int rc = 0;
  153. struct cam_custom_context *custom_ctx;
  154. struct cam_hw_reset_args reset_args;
  155. struct cam_hw_stop_args stop_args;
  156. struct cam_custom_stop_args custom_stop;
  157. custom_ctx =
  158. (struct cam_custom_context *) ctx->ctx_priv;
  159. CAM_DBG(CAM_CUSTOM, "Flushing pending list");
  160. spin_lock_bh(&ctx->lock);
  161. __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  162. spin_unlock_bh(&ctx->lock);
  163. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  164. if (ctx->state <= CAM_CTX_READY) {
  165. ctx->state = CAM_CTX_ACQUIRED;
  166. goto end;
  167. }
  168. spin_lock_bh(&ctx->lock);
  169. ctx->state = CAM_CTX_FLUSHED;
  170. spin_unlock_bh(&ctx->lock);
  171. CAM_INFO(CAM_CUSTOM, "Last request id to flush is %lld",
  172. flush_req->req_id);
  173. ctx->last_flush_req = flush_req->req_id;
  174. /* stop hw first */
  175. if (ctx->hw_mgr_intf->hw_stop) {
  176. custom_stop.stop_only = true;
  177. stop_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  178. stop_args.args = (void *) &custom_stop;
  179. rc = ctx->hw_mgr_intf->hw_stop(
  180. ctx->hw_mgr_intf->hw_mgr_priv, &stop_args);
  181. if (rc)
  182. CAM_ERR(CAM_CUSTOM,
  183. "HW stop failed in flush rc %d", rc);
  184. }
  185. spin_lock_bh(&ctx->lock);
  186. if (!list_empty(&ctx->wait_req_list))
  187. __cam_custom_ctx_flush_req(ctx, &ctx->wait_req_list,
  188. flush_req);
  189. if (!list_empty(&ctx->active_req_list))
  190. __cam_custom_ctx_flush_req(ctx, &ctx->active_req_list,
  191. flush_req);
  192. custom_ctx->active_req_cnt = 0;
  193. spin_unlock_bh(&ctx->lock);
  194. reset_args.ctxt_to_hw_map = custom_ctx->hw_ctx;
  195. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  196. &reset_args);
  197. if (rc)
  198. CAM_ERR(CAM_CUSTOM,
  199. "Reset HW failed in flush rc %d", rc);
  200. custom_ctx->init_received = false;
  201. }
  202. end:
  203. return rc;
  204. }
  205. static int __cam_custom_ctx_flush_req_in_ready(
  206. struct cam_context *ctx,
  207. struct cam_req_mgr_flush_request *flush_req)
  208. {
  209. int rc = 0;
  210. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  211. spin_lock_bh(&ctx->lock);
  212. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  213. /* if nothing is in pending req list, change state to acquire */
  214. if (list_empty(&ctx->pending_req_list))
  215. ctx->state = CAM_CTX_ACQUIRED;
  216. spin_unlock_bh(&ctx->lock);
  217. CAM_DBG(CAM_CUSTOM, "Flush request in ready state. next state %d",
  218. ctx->state);
  219. return rc;
  220. }
  221. static int __cam_custom_stop_dev_core(
  222. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  223. {
  224. int rc = 0;
  225. uint32_t i;
  226. struct cam_custom_context *ctx_custom =
  227. (struct cam_custom_context *) ctx->ctx_priv;
  228. struct cam_ctx_request *req;
  229. struct cam_custom_dev_ctx_req *req_custom;
  230. struct cam_hw_stop_args stop;
  231. struct cam_custom_stop_args custom_stop;
  232. if ((ctx->state != CAM_CTX_FLUSHED) && (ctx_custom->hw_ctx) &&
  233. (ctx->hw_mgr_intf->hw_stop)) {
  234. custom_stop.stop_only = false;
  235. stop.ctxt_to_hw_map = ctx_custom->hw_ctx;
  236. stop.args = (void *) &custom_stop;
  237. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  238. &stop);
  239. if (rc)
  240. CAM_ERR(CAM_CUSTOM, "HW stop failed rc %d", rc);
  241. }
  242. while (!list_empty(&ctx->pending_req_list)) {
  243. req = list_first_entry(&ctx->pending_req_list,
  244. struct cam_ctx_request, list);
  245. list_del_init(&req->list);
  246. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  247. CAM_DBG(CAM_CUSTOM,
  248. "signal fence in pending list. fence num %d",
  249. req_custom->num_fence_map_out);
  250. for (i = 0; i < req_custom->num_fence_map_out; i++)
  251. if (req_custom->fence_map_out[i].sync_id != -1) {
  252. cam_sync_signal(
  253. req_custom->fence_map_out[i].sync_id,
  254. CAM_SYNC_STATE_SIGNALED_CANCEL);
  255. }
  256. list_add_tail(&req->list, &ctx->free_req_list);
  257. }
  258. while (!list_empty(&ctx->wait_req_list)) {
  259. req = list_first_entry(&ctx->wait_req_list,
  260. struct cam_ctx_request, list);
  261. list_del_init(&req->list);
  262. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  263. CAM_DBG(CAM_CUSTOM, "signal fence in wait list. fence num %d",
  264. req_custom->num_fence_map_out);
  265. for (i = 0; i < req_custom->num_fence_map_out; i++)
  266. if (req_custom->fence_map_out[i].sync_id != -1) {
  267. cam_sync_signal(
  268. req_custom->fence_map_out[i].sync_id,
  269. CAM_SYNC_STATE_SIGNALED_CANCEL);
  270. }
  271. list_add_tail(&req->list, &ctx->free_req_list);
  272. }
  273. while (!list_empty(&ctx->active_req_list)) {
  274. req = list_first_entry(&ctx->active_req_list,
  275. struct cam_ctx_request, list);
  276. list_del_init(&req->list);
  277. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  278. CAM_DBG(CAM_CUSTOM, "signal fence in active list. fence num %d",
  279. req_custom->num_fence_map_out);
  280. for (i = 0; i < req_custom->num_fence_map_out; i++)
  281. if (req_custom->fence_map_out[i].sync_id != -1) {
  282. cam_sync_signal(
  283. req_custom->fence_map_out[i].sync_id,
  284. CAM_SYNC_STATE_SIGNALED_CANCEL);
  285. }
  286. list_add_tail(&req->list, &ctx->free_req_list);
  287. }
  288. ctx_custom->frame_id = 0;
  289. ctx_custom->active_req_cnt = 0;
  290. CAM_DBG(CAM_CUSTOM, "Stop device success next state %d on ctx %u",
  291. ctx->state, ctx->ctx_id);
  292. if (!stop_cmd) {
  293. rc = __cam_custom_ctx_unlink_in_ready(ctx, NULL);
  294. if (rc)
  295. CAM_ERR(CAM_CUSTOM, "Unlink failed rc=%d", rc);
  296. }
  297. return rc;
  298. }
  299. static int __cam_custom_stop_dev_in_activated(struct cam_context *ctx,
  300. struct cam_start_stop_dev_cmd *cmd)
  301. {
  302. struct cam_custom_context *ctx_custom =
  303. (struct cam_custom_context *)ctx->ctx_priv;
  304. __cam_custom_stop_dev_core(ctx, cmd);
  305. ctx_custom->init_received = false;
  306. ctx->state = CAM_CTX_ACQUIRED;
  307. return 0;
  308. }
  309. static int __cam_custom_ctx_release_hw_in_top_state(
  310. struct cam_context *ctx, void *cmd)
  311. {
  312. int rc = 0;
  313. struct cam_hw_release_args rel_arg;
  314. struct cam_req_mgr_flush_request flush_req;
  315. struct cam_custom_context *custom_ctx =
  316. (struct cam_custom_context *) ctx->ctx_priv;
  317. if (custom_ctx->hw_ctx) {
  318. rel_arg.ctxt_to_hw_map = custom_ctx->hw_ctx;
  319. rc = ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  320. &rel_arg);
  321. custom_ctx->hw_ctx = NULL;
  322. if (rc)
  323. CAM_ERR(CAM_CUSTOM,
  324. "Failed to release HW for ctx:%u", ctx->ctx_id);
  325. } else {
  326. CAM_ERR(CAM_CUSTOM, "No HW resources acquired for this ctx");
  327. }
  328. ctx->last_flush_req = 0;
  329. custom_ctx->frame_id = 0;
  330. custom_ctx->active_req_cnt = 0;
  331. custom_ctx->hw_acquired = false;
  332. custom_ctx->init_received = false;
  333. /* check for active requests as well */
  334. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  335. flush_req.link_hdl = ctx->link_hdl;
  336. flush_req.dev_hdl = ctx->dev_hdl;
  337. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  338. spin_lock_bh(&ctx->lock);
  339. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list,
  340. &flush_req);
  341. spin_unlock_bh(&ctx->lock);
  342. ctx->state = CAM_CTX_ACQUIRED;
  343. CAM_DBG(CAM_CUSTOM, "Release HW success[%u] next state %d",
  344. ctx->ctx_id, ctx->state);
  345. return rc;
  346. }
  347. static int __cam_custom_ctx_release_hw_in_activated_state(
  348. struct cam_context *ctx, void *cmd)
  349. {
  350. int rc = 0;
  351. rc = __cam_custom_stop_dev_in_activated(ctx, NULL);
  352. if (rc)
  353. CAM_ERR(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  354. rc = __cam_custom_ctx_release_hw_in_top_state(ctx, cmd);
  355. if (rc)
  356. CAM_ERR(CAM_CUSTOM, "Release hw failed rc=%d", rc);
  357. return rc;
  358. }
  359. static int __cam_custom_release_dev_in_acquired(struct cam_context *ctx,
  360. struct cam_release_dev_cmd *cmd)
  361. {
  362. int rc;
  363. struct cam_custom_context *ctx_custom =
  364. (struct cam_custom_context *) ctx->ctx_priv;
  365. struct cam_req_mgr_flush_request flush_req;
  366. if (cmd && ctx_custom->hw_ctx) {
  367. CAM_ERR(CAM_CUSTOM, "releasing hw");
  368. __cam_custom_ctx_release_hw_in_top_state(ctx, NULL);
  369. }
  370. ctx->ctx_crm_intf = NULL;
  371. ctx->last_flush_req = 0;
  372. ctx_custom->frame_id = 0;
  373. ctx_custom->active_req_cnt = 0;
  374. ctx_custom->hw_acquired = false;
  375. ctx_custom->init_received = false;
  376. if (!list_empty(&ctx->active_req_list))
  377. CAM_ERR(CAM_CUSTOM, "Active list is not empty");
  378. /* Flush all the pending request list */
  379. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  380. flush_req.link_hdl = ctx->link_hdl;
  381. flush_req.dev_hdl = ctx->dev_hdl;
  382. CAM_DBG(CAM_CUSTOM, "try to flush pending list");
  383. spin_lock_bh(&ctx->lock);
  384. rc = __cam_custom_ctx_flush_req(ctx, &ctx->pending_req_list,
  385. &flush_req);
  386. spin_unlock_bh(&ctx->lock);
  387. ctx->state = CAM_CTX_AVAILABLE;
  388. CAM_DBG(CAM_CUSTOM, "Release device success[%u] next state %d",
  389. ctx->ctx_id, ctx->state);
  390. return rc;
  391. }
  392. static int __cam_custom_ctx_apply_default_settings(
  393. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  394. {
  395. int rc = 0;
  396. struct cam_custom_context *custom_ctx =
  397. (struct cam_custom_context *) ctx->ctx_priv;
  398. struct cam_hw_cmd_args hw_cmd_args;
  399. struct cam_custom_hw_cmd_args custom_hw_cmd_args;
  400. hw_cmd_args.ctxt_to_hw_map = custom_ctx->hw_ctx;
  401. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  402. custom_hw_cmd_args.cmd_type =
  403. CAM_CUSTOM_HW_MGR_PROG_DEFAULT_CONFIG;
  404. hw_cmd_args.u.internal_args = (void *)&custom_hw_cmd_args;
  405. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  406. &hw_cmd_args);
  407. if (rc)
  408. CAM_ERR(CAM_CUSTOM,
  409. "Failed to apply default settings rc %d", rc);
  410. else
  411. CAM_DBG(CAM_CUSTOM, "Applied default settings rc %d", rc);
  412. return rc;
  413. }
  414. static int __cam_custom_ctx_apply_req_in_activated_state(
  415. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  416. {
  417. int rc = 0;
  418. struct cam_ctx_request *req;
  419. struct cam_custom_dev_ctx_req *req_custom;
  420. struct cam_custom_context *custom_ctx = NULL;
  421. struct cam_hw_config_args cfg;
  422. if (list_empty(&ctx->pending_req_list)) {
  423. CAM_ERR(CAM_CUSTOM, "No available request for Apply id %lld",
  424. apply->request_id);
  425. rc = -EFAULT;
  426. goto end;
  427. }
  428. custom_ctx = (struct cam_custom_context *) ctx->ctx_priv;
  429. spin_lock_bh(&ctx->lock);
  430. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  431. list);
  432. spin_unlock_bh(&ctx->lock);
  433. /*
  434. * Check whether the request id is matching the tip
  435. */
  436. if (req->request_id != apply->request_id) {
  437. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  438. "Invalid Request Id asking %llu existing %llu",
  439. apply->request_id, req->request_id);
  440. rc = -EFAULT;
  441. goto end;
  442. }
  443. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  444. cfg.ctxt_to_hw_map = custom_ctx->hw_ctx;
  445. cfg.request_id = req->request_id;
  446. cfg.hw_update_entries = req_custom->cfg;
  447. cfg.num_hw_update_entries = req_custom->num_cfg;
  448. cfg.priv = &req_custom->hw_update_data;
  449. cfg.init_packet = 0;
  450. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  451. if (rc) {
  452. CAM_ERR_RATE_LIMIT(CAM_CUSTOM,
  453. "Can not apply the configuration");
  454. } else {
  455. spin_lock_bh(&ctx->lock);
  456. list_del_init(&req->list);
  457. if (!req->num_out_map_entries) {
  458. list_add_tail(&req->list, &ctx->free_req_list);
  459. spin_unlock_bh(&ctx->lock);
  460. } else {
  461. list_add_tail(&req->list, &ctx->active_req_list);
  462. spin_unlock_bh(&ctx->lock);
  463. /*
  464. * for test purposes only-this should be
  465. * triggered based on irq
  466. */
  467. __cam_custom_ctx_handle_irq_in_activated(ctx, 0, NULL);
  468. }
  469. }
  470. end:
  471. return rc;
  472. }
  473. static int __cam_custom_ctx_acquire_hw_v1(
  474. struct cam_context *ctx, void *args)
  475. {
  476. int rc = 0;
  477. struct cam_acquire_hw_cmd_v1 *cmd =
  478. (struct cam_acquire_hw_cmd_v1 *)args;
  479. struct cam_hw_acquire_args param;
  480. struct cam_custom_context *ctx_custom =
  481. (struct cam_custom_context *) ctx->ctx_priv;
  482. struct cam_custom_acquire_hw_info *acquire_hw_info = NULL;
  483. if (!ctx->hw_mgr_intf) {
  484. CAM_ERR(CAM_CUSTOM, "HW interface is not ready");
  485. rc = -EFAULT;
  486. goto end;
  487. }
  488. CAM_DBG(CAM_CUSTOM,
  489. "session_hdl 0x%x, hdl type %d, res %lld",
  490. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  491. if (cmd->handle_type != 1) {
  492. CAM_ERR(CAM_CUSTOM, "Only user pointer is supported");
  493. rc = -EINVAL;
  494. goto end;
  495. }
  496. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  497. CAM_ERR(CAM_CUSTOM, "data_size is not a valid value");
  498. goto end;
  499. }
  500. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  501. if (!acquire_hw_info) {
  502. rc = -ENOMEM;
  503. goto end;
  504. }
  505. CAM_DBG(CAM_CUSTOM, "start copy resources from user");
  506. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  507. cmd->data_size)) {
  508. rc = -EFAULT;
  509. goto free_res;
  510. }
  511. memset(&param, 0, sizeof(param));
  512. param.context_data = ctx;
  513. param.event_cb = ctx->irq_cb_intf;
  514. param.acquire_info_size = cmd->data_size;
  515. param.acquire_info = (uint64_t) acquire_hw_info;
  516. /* call HW manager to reserve the resource */
  517. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  518. &param);
  519. if (rc != 0) {
  520. CAM_ERR(CAM_CUSTOM, "Acquire HW failed");
  521. goto free_res;
  522. }
  523. ctx_custom->hw_ctx = param.ctxt_to_hw_map;
  524. ctx_custom->hw_acquired = true;
  525. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  526. CAM_DBG(CAM_CUSTOM,
  527. "Acquire HW success on session_hdl 0x%xs for ctx_id %u",
  528. ctx->session_hdl, ctx->ctx_id);
  529. kfree(acquire_hw_info);
  530. return rc;
  531. free_res:
  532. kfree(acquire_hw_info);
  533. end:
  534. return rc;
  535. }
  536. static int __cam_custom_ctx_acquire_dev_in_available(
  537. struct cam_context *ctx, struct cam_acquire_dev_cmd *cmd)
  538. {
  539. int rc = 0;
  540. struct cam_create_dev_hdl req_hdl_param;
  541. if (!ctx->hw_mgr_intf) {
  542. CAM_ERR(CAM_CUSTOM, "HW interface is not ready");
  543. rc = -EFAULT;
  544. return rc;
  545. }
  546. CAM_DBG(CAM_CUSTOM,
  547. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  548. cmd->session_handle, cmd->num_resources,
  549. cmd->handle_type, cmd->resource_hdl);
  550. if (cmd->num_resources != CAM_API_COMPAT_CONSTANT) {
  551. CAM_ERR(CAM_CUSTOM, "Invalid num_resources 0x%x",
  552. cmd->num_resources);
  553. return -EINVAL;
  554. }
  555. req_hdl_param.session_hdl = cmd->session_handle;
  556. req_hdl_param.v4l2_sub_dev_flag = 0;
  557. req_hdl_param.media_entity_flag = 0;
  558. req_hdl_param.ops = ctx->crm_ctx_intf;
  559. req_hdl_param.priv = ctx;
  560. CAM_DBG(CAM_CUSTOM, "get device handle from bridge");
  561. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  562. if (ctx->dev_hdl <= 0) {
  563. rc = -EFAULT;
  564. CAM_ERR(CAM_CUSTOM, "Can not create device handle");
  565. return rc;
  566. }
  567. cmd->dev_handle = ctx->dev_hdl;
  568. ctx->session_hdl = cmd->session_handle;
  569. ctx->state = CAM_CTX_ACQUIRED;
  570. CAM_DBG(CAM_CUSTOM,
  571. "Acquire dev success on session_hdl 0x%x for ctx %u",
  572. cmd->session_handle, ctx->ctx_id);
  573. return rc;
  574. }
  575. static int __cam_custom_ctx_enqueue_init_request(
  576. struct cam_context *ctx, struct cam_ctx_request *req)
  577. {
  578. int rc = 0;
  579. struct cam_ctx_request *req_old;
  580. struct cam_custom_dev_ctx_req *req_custom_old;
  581. struct cam_custom_dev_ctx_req *req_custom_new;
  582. spin_lock_bh(&ctx->lock);
  583. if (list_empty(&ctx->pending_req_list)) {
  584. list_add_tail(&req->list, &ctx->pending_req_list);
  585. goto end;
  586. }
  587. req_old = list_first_entry(&ctx->pending_req_list,
  588. struct cam_ctx_request, list);
  589. req_custom_old = (struct cam_custom_dev_ctx_req *) req_old->req_priv;
  590. req_custom_new = (struct cam_custom_dev_ctx_req *) req->req_priv;
  591. if (req_custom_old->hw_update_data.packet_opcode_type ==
  592. CAM_CUSTOM_PACKET_INIT_DEV) {
  593. if ((req_custom_old->num_cfg + req_custom_new->num_cfg) >=
  594. CAM_CUSTOM_CTX_CFG_MAX) {
  595. CAM_WARN(CAM_CUSTOM, "Can not merge INIT pkt");
  596. rc = -ENOMEM;
  597. }
  598. if (req_custom_old->num_fence_map_out != 0 ||
  599. req_custom_old->num_fence_map_in != 0) {
  600. CAM_WARN(CAM_CUSTOM, "Invalid INIT pkt sequence");
  601. rc = -EINVAL;
  602. }
  603. if (!rc) {
  604. memcpy(req_custom_old->fence_map_out,
  605. req_custom_new->fence_map_out,
  606. sizeof(req_custom_new->fence_map_out[0])*
  607. req_custom_new->num_fence_map_out);
  608. req_custom_old->num_fence_map_out =
  609. req_custom_new->num_fence_map_out;
  610. memcpy(req_custom_old->fence_map_in,
  611. req_custom_new->fence_map_in,
  612. sizeof(req_custom_new->fence_map_in[0])*
  613. req_custom_new->num_fence_map_in);
  614. req_custom_old->num_fence_map_in =
  615. req_custom_new->num_fence_map_in;
  616. memcpy(&req_custom_old->cfg[req_custom_old->num_cfg],
  617. req_custom_new->cfg,
  618. sizeof(req_custom_new->cfg[0])*
  619. req_custom_new->num_cfg);
  620. req_custom_old->num_cfg += req_custom_new->num_cfg;
  621. req_old->request_id = req->request_id;
  622. list_add_tail(&req->list, &ctx->free_req_list);
  623. }
  624. } else {
  625. CAM_WARN(CAM_CUSTOM,
  626. "Received Update pkt before INIT pkt. req_id= %lld",
  627. req->request_id);
  628. rc = -EINVAL;
  629. }
  630. end:
  631. spin_unlock_bh(&ctx->lock);
  632. return rc;
  633. }
  634. static int __cam_custom_ctx_config_dev(struct cam_context *ctx,
  635. struct cam_config_dev_cmd *cmd)
  636. {
  637. int rc = 0, i;
  638. struct cam_ctx_request *req = NULL;
  639. struct cam_custom_dev_ctx_req *req_custom;
  640. uintptr_t packet_addr;
  641. struct cam_packet *packet;
  642. size_t len = 0;
  643. struct cam_hw_prepare_update_args cfg;
  644. struct cam_req_mgr_add_request add_req;
  645. struct cam_custom_context *ctx_custom =
  646. (struct cam_custom_context *) ctx->ctx_priv;
  647. /* get free request */
  648. spin_lock_bh(&ctx->lock);
  649. if (!list_empty(&ctx->free_req_list)) {
  650. req = list_first_entry(&ctx->free_req_list,
  651. struct cam_ctx_request, list);
  652. list_del_init(&req->list);
  653. }
  654. spin_unlock_bh(&ctx->lock);
  655. if (!req) {
  656. CAM_ERR(CAM_CUSTOM, "No more request obj free");
  657. return -ENOMEM;
  658. }
  659. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  660. /* for config dev, only memory handle is supported */
  661. /* map packet from the memhandle */
  662. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  663. &packet_addr, &len);
  664. if (rc != 0) {
  665. CAM_ERR(CAM_CUSTOM, "Can not get packet address");
  666. rc = -EINVAL;
  667. goto free_req;
  668. }
  669. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  670. CAM_DBG(CAM_CUSTOM, "pack_handle %llx", cmd->packet_handle);
  671. CAM_DBG(CAM_CUSTOM, "packet address is 0x%zx", packet_addr);
  672. CAM_DBG(CAM_CUSTOM, "packet with length %zu, offset 0x%llx",
  673. len, cmd->offset);
  674. CAM_DBG(CAM_CUSTOM, "Packet request id %lld",
  675. packet->header.request_id);
  676. CAM_DBG(CAM_CUSTOM, "Packet size 0x%x", packet->header.size);
  677. CAM_DBG(CAM_CUSTOM, "packet op %d", packet->header.op_code);
  678. if ((((packet->header.op_code) & 0xF) ==
  679. CAM_CUSTOM_PACKET_UPDATE_DEV)
  680. && (packet->header.request_id <= ctx->last_flush_req)) {
  681. CAM_DBG(CAM_CUSTOM,
  682. "request %lld has been flushed, reject packet",
  683. packet->header.request_id);
  684. rc = -EINVAL;
  685. goto free_req;
  686. }
  687. /* preprocess the configuration */
  688. memset(&cfg, 0, sizeof(cfg));
  689. cfg.packet = packet;
  690. cfg.ctxt_to_hw_map = ctx_custom->hw_ctx;
  691. cfg.out_map_entries = req_custom->fence_map_out;
  692. cfg.in_map_entries = req_custom->fence_map_in;
  693. cfg.priv = &req_custom->hw_update_data;
  694. cfg.pf_data = &(req->pf_data);
  695. rc = ctx->hw_mgr_intf->hw_prepare_update(
  696. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  697. if (rc != 0) {
  698. CAM_ERR(CAM_CUSTOM, "Prepare config packet failed in HW layer");
  699. rc = -EFAULT;
  700. goto free_req;
  701. }
  702. req_custom->num_cfg = cfg.num_hw_update_entries;
  703. req_custom->num_fence_map_out = cfg.num_out_map_entries;
  704. req_custom->num_fence_map_in = cfg.num_in_map_entries;
  705. req_custom->num_acked = 0;
  706. for (i = 0; i < req_custom->num_fence_map_out; i++) {
  707. rc = cam_sync_get_obj_ref(req_custom->fence_map_out[i].sync_id);
  708. if (rc) {
  709. CAM_ERR(CAM_CUSTOM, "Can't get ref for fence %d",
  710. req_custom->fence_map_out[i].sync_id);
  711. goto put_ref;
  712. }
  713. }
  714. CAM_DBG(CAM_CUSTOM,
  715. "num_entry: %d, num fence out: %d, num fence in: %d",
  716. req_custom->num_cfg, req_custom->num_fence_map_out,
  717. req_custom->num_fence_map_in);
  718. req->request_id = packet->header.request_id;
  719. req->status = 1;
  720. CAM_DBG(CAM_CUSTOM, "Packet request id %lld packet opcode:%d",
  721. packet->header.request_id,
  722. req_custom->hw_update_data.packet_opcode_type);
  723. if (req_custom->hw_update_data.packet_opcode_type ==
  724. CAM_CUSTOM_PACKET_INIT_DEV) {
  725. if (ctx->state < CAM_CTX_ACTIVATED) {
  726. rc = __cam_custom_ctx_enqueue_init_request(ctx, req);
  727. if (rc)
  728. CAM_ERR(CAM_CUSTOM, "Enqueue INIT pkt failed");
  729. ctx_custom->init_received = true;
  730. } else {
  731. rc = -EINVAL;
  732. CAM_ERR(CAM_CUSTOM, "Recevied INIT pkt in wrong state");
  733. }
  734. } else {
  735. if ((ctx->state != CAM_CTX_FLUSHED) &&
  736. (ctx->state >= CAM_CTX_READY) &&
  737. (ctx->ctx_crm_intf->add_req)) {
  738. add_req.link_hdl = ctx->link_hdl;
  739. add_req.dev_hdl = ctx->dev_hdl;
  740. add_req.req_id = req->request_id;
  741. add_req.skip_before_applying = 0;
  742. rc = ctx->ctx_crm_intf->add_req(&add_req);
  743. if (rc) {
  744. CAM_ERR(CAM_CUSTOM,
  745. "Add req failed: req id=%llu",
  746. req->request_id);
  747. } else {
  748. __cam_custom_ctx_enqueue_request_in_order(
  749. ctx, req);
  750. }
  751. } else {
  752. rc = -EINVAL;
  753. CAM_ERR(CAM_CUSTOM, "Recevied Update in wrong state");
  754. }
  755. }
  756. if (rc)
  757. goto put_ref;
  758. CAM_DBG(CAM_CUSTOM,
  759. "Preprocessing Config req_id %lld successful on ctx %u",
  760. req->request_id, ctx->ctx_id);
  761. return rc;
  762. put_ref:
  763. for (--i; i >= 0; i--) {
  764. if (cam_sync_put_obj_ref(req_custom->fence_map_out[i].sync_id))
  765. CAM_ERR(CAM_CUSTOM, "Failed to put ref of fence %d",
  766. req_custom->fence_map_out[i].sync_id);
  767. }
  768. free_req:
  769. spin_lock_bh(&ctx->lock);
  770. list_add_tail(&req->list, &ctx->free_req_list);
  771. spin_unlock_bh(&ctx->lock);
  772. return rc;
  773. }
  774. static int __cam_custom_ctx_config_dev_in_flushed(struct cam_context *ctx,
  775. struct cam_config_dev_cmd *cmd)
  776. {
  777. int rc = 0;
  778. struct cam_start_stop_dev_cmd start_cmd;
  779. struct cam_custom_context *custom_ctx =
  780. (struct cam_custom_context *) ctx->ctx_priv;
  781. if (!custom_ctx->hw_acquired) {
  782. CAM_ERR(CAM_CUSTOM, "HW is not acquired, reject packet");
  783. rc = -EINVAL;
  784. goto end;
  785. }
  786. rc = __cam_custom_ctx_config_dev(ctx, cmd);
  787. if (rc)
  788. goto end;
  789. if (!custom_ctx->init_received) {
  790. CAM_WARN(CAM_CUSTOM,
  791. "Received update packet in flushed state, skip start");
  792. goto end;
  793. }
  794. start_cmd.dev_handle = cmd->dev_handle;
  795. start_cmd.session_handle = cmd->session_handle;
  796. rc = __cam_custom_ctx_start_dev_in_ready(ctx, &start_cmd);
  797. if (rc)
  798. CAM_ERR(CAM_CUSTOM,
  799. "Failed to re-start HW after flush rc: %d", rc);
  800. else
  801. CAM_INFO(CAM_CUSTOM,
  802. "Received init after flush. Re-start HW complete.");
  803. end:
  804. return rc;
  805. }
  806. static int __cam_custom_ctx_config_dev_in_acquired(struct cam_context *ctx,
  807. struct cam_config_dev_cmd *cmd)
  808. {
  809. int rc = 0;
  810. struct cam_custom_context *ctx_custom =
  811. (struct cam_custom_context *) ctx->ctx_priv;
  812. if (!ctx_custom->hw_acquired) {
  813. CAM_ERR(CAM_CUSTOM, "HW not acquired, reject config packet");
  814. return -EAGAIN;
  815. }
  816. rc = __cam_custom_ctx_config_dev(ctx, cmd);
  817. if (!rc && (ctx->link_hdl >= 0))
  818. ctx->state = CAM_CTX_READY;
  819. return rc;
  820. }
  821. static int __cam_custom_ctx_link_in_acquired(struct cam_context *ctx,
  822. struct cam_req_mgr_core_dev_link_setup *link)
  823. {
  824. struct cam_custom_context *ctx_custom =
  825. (struct cam_custom_context *) ctx->ctx_priv;
  826. ctx->link_hdl = link->link_hdl;
  827. ctx->ctx_crm_intf = link->crm_cb;
  828. ctx_custom->subscribe_event = link->subscribe_event;
  829. /* change state only if we had the init config */
  830. if (ctx_custom->init_received)
  831. ctx->state = CAM_CTX_READY;
  832. CAM_DBG(CAM_CUSTOM, "next state %d", ctx->state);
  833. return 0;
  834. }
  835. static int __cam_custom_ctx_start_dev_in_ready(struct cam_context *ctx,
  836. struct cam_start_stop_dev_cmd *cmd)
  837. {
  838. int rc = 0;
  839. struct cam_custom_start_args custom_start;
  840. struct cam_ctx_request *req;
  841. struct cam_custom_dev_ctx_req *req_custom;
  842. struct cam_custom_context *ctx_custom =
  843. (struct cam_custom_context *) ctx->ctx_priv;
  844. if (cmd->session_handle != ctx->session_hdl ||
  845. cmd->dev_handle != ctx->dev_hdl) {
  846. rc = -EPERM;
  847. goto end;
  848. }
  849. if (list_empty(&ctx->pending_req_list)) {
  850. /* should never happen */
  851. CAM_ERR(CAM_CUSTOM, "Start device with empty configuration");
  852. rc = -EFAULT;
  853. goto end;
  854. } else {
  855. req = list_first_entry(&ctx->pending_req_list,
  856. struct cam_ctx_request, list);
  857. }
  858. req_custom = (struct cam_custom_dev_ctx_req *) req->req_priv;
  859. if (!ctx_custom->hw_ctx) {
  860. CAM_ERR(CAM_CUSTOM, "Wrong hw context pointer.");
  861. rc = -EFAULT;
  862. goto end;
  863. }
  864. custom_start.hw_config.ctxt_to_hw_map = ctx_custom->hw_ctx;
  865. custom_start.hw_config.request_id = req->request_id;
  866. custom_start.hw_config.hw_update_entries = req_custom->cfg;
  867. custom_start.hw_config.num_hw_update_entries = req_custom->num_cfg;
  868. custom_start.hw_config.priv = &req_custom->hw_update_data;
  869. custom_start.hw_config.init_packet = 1;
  870. if (ctx->state == CAM_CTX_FLUSHED)
  871. custom_start.start_only = true;
  872. else
  873. custom_start.start_only = false;
  874. ctx->state = CAM_CTX_ACTIVATED;
  875. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  876. &custom_start);
  877. if (rc) {
  878. /* HW failure. User need to clean up the resource */
  879. CAM_ERR(CAM_CUSTOM, "Start HW failed");
  880. ctx->state = CAM_CTX_READY;
  881. goto end;
  882. }
  883. CAM_DBG(CAM_CUSTOM, "start device success ctx %u",
  884. ctx->ctx_id);
  885. spin_lock_bh(&ctx->lock);
  886. list_del_init(&req->list);
  887. if (req_custom->num_fence_map_out)
  888. list_add_tail(&req->list, &ctx->active_req_list);
  889. else
  890. list_add_tail(&req->list, &ctx->free_req_list);
  891. spin_unlock_bh(&ctx->lock);
  892. end:
  893. return rc;
  894. }
  895. static int __cam_custom_ctx_release_dev_in_activated(struct cam_context *ctx,
  896. struct cam_release_dev_cmd *cmd)
  897. {
  898. int rc = 0;
  899. rc = __cam_custom_stop_dev_core(ctx, NULL);
  900. if (rc)
  901. CAM_ERR(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  902. rc = __cam_custom_release_dev_in_acquired(ctx, cmd);
  903. if (rc)
  904. CAM_ERR(CAM_CUSTOM, "Release device failed rc=%d", rc);
  905. return rc;
  906. }
  907. static int __cam_custom_ctx_unlink_in_activated(struct cam_context *ctx,
  908. struct cam_req_mgr_core_dev_link_setup *unlink)
  909. {
  910. int rc = 0;
  911. CAM_WARN(CAM_CUSTOM,
  912. "Received unlink in activated state. It's unexpected");
  913. rc = __cam_custom_stop_dev_in_activated(ctx, NULL);
  914. if (rc)
  915. CAM_WARN(CAM_CUSTOM, "Stop device failed rc=%d", rc);
  916. rc = __cam_custom_ctx_unlink_in_ready(ctx, unlink);
  917. if (rc)
  918. CAM_ERR(CAM_CUSTOM, "Unlink failed rc=%d", rc);
  919. return rc;
  920. }
  921. static int __cam_custom_ctx_process_evt(struct cam_context *ctx,
  922. struct cam_req_mgr_link_evt_data *link_evt_data)
  923. {
  924. switch (link_evt_data->evt_type) {
  925. case CAM_REQ_MGR_LINK_EVT_ERR:
  926. /* Handle error/bubble related issues */
  927. break;
  928. default:
  929. CAM_WARN(CAM_CUSTOM, "Unknown event from CRM");
  930. break;
  931. }
  932. return 0;
  933. }
  934. static int __cam_custom_ctx_handle_irq_in_activated(void *context,
  935. uint32_t evt_id, void *evt_data)
  936. {
  937. int rc;
  938. struct cam_context *ctx =
  939. (struct cam_context *)context;
  940. CAM_DBG(CAM_CUSTOM, "Enter %d", ctx->ctx_id);
  941. /*
  942. * handle based on different irq's currently
  943. * triggering only buf done if there are fences
  944. */
  945. rc = cam_context_buf_done_from_hw(ctx, evt_data, 0);
  946. if (rc)
  947. CAM_ERR(CAM_CUSTOM, "Failed in buf done, rc=%d", rc);
  948. return rc;
  949. }
  950. static int __cam_custom_ctx_acquire_hw_in_acquired(
  951. struct cam_context *ctx, void *args)
  952. {
  953. int rc = -EINVAL;
  954. uint32_t api_version;
  955. if (!ctx || !args) {
  956. CAM_ERR(CAM_CUSTOM, "Invalid input pointer");
  957. return rc;
  958. }
  959. api_version = *((uint32_t *)args);
  960. if (api_version == 1)
  961. rc = __cam_custom_ctx_acquire_hw_v1(ctx, args);
  962. else
  963. CAM_ERR(CAM_CUSTOM, "Unsupported api version %d",
  964. api_version);
  965. return rc;
  966. }
  967. /* top state machine */
  968. static struct cam_ctx_ops
  969. cam_custom_dev_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  970. /* Uninit */
  971. {
  972. .ioctl_ops = {},
  973. .crm_ops = {},
  974. .irq_ops = NULL,
  975. },
  976. /* Available */
  977. {
  978. .ioctl_ops = {
  979. .acquire_dev =
  980. __cam_custom_ctx_acquire_dev_in_available,
  981. },
  982. .crm_ops = {},
  983. .irq_ops = NULL,
  984. },
  985. /* Acquired */
  986. {
  987. .ioctl_ops = {
  988. .acquire_hw = __cam_custom_ctx_acquire_hw_in_acquired,
  989. .release_dev = __cam_custom_release_dev_in_acquired,
  990. .config_dev = __cam_custom_ctx_config_dev_in_acquired,
  991. .release_hw = __cam_custom_ctx_release_hw_in_top_state,
  992. },
  993. .crm_ops = {
  994. .link = __cam_custom_ctx_link_in_acquired,
  995. .unlink = __cam_custom_ctx_unlink_in_acquired,
  996. .get_dev_info =
  997. __cam_custom_ctx_get_dev_info_in_acquired,
  998. .flush_req = __cam_custom_ctx_flush_req_in_top_state,
  999. },
  1000. .irq_ops = NULL,
  1001. .pagefault_ops = NULL,
  1002. },
  1003. /* Ready */
  1004. {
  1005. .ioctl_ops = {
  1006. .start_dev = __cam_custom_ctx_start_dev_in_ready,
  1007. .release_dev = __cam_custom_release_dev_in_acquired,
  1008. .config_dev = __cam_custom_ctx_config_dev,
  1009. .release_hw = __cam_custom_ctx_release_hw_in_top_state,
  1010. },
  1011. .crm_ops = {
  1012. .unlink = __cam_custom_ctx_unlink_in_ready,
  1013. .flush_req = __cam_custom_ctx_flush_req_in_ready,
  1014. },
  1015. .irq_ops = NULL,
  1016. .pagefault_ops = NULL,
  1017. },
  1018. /* Flushed */
  1019. {
  1020. .ioctl_ops = {
  1021. .stop_dev = __cam_custom_stop_dev_in_activated,
  1022. .release_dev =
  1023. __cam_custom_ctx_release_dev_in_activated,
  1024. .config_dev = __cam_custom_ctx_config_dev_in_flushed,
  1025. .release_hw =
  1026. __cam_custom_ctx_release_hw_in_activated_state,
  1027. },
  1028. .crm_ops = {
  1029. .unlink = __cam_custom_ctx_unlink_in_ready,
  1030. },
  1031. .irq_ops = NULL,
  1032. },
  1033. /* Activated */
  1034. {
  1035. .ioctl_ops = {
  1036. .stop_dev = __cam_custom_stop_dev_in_activated,
  1037. .release_dev =
  1038. __cam_custom_ctx_release_dev_in_activated,
  1039. .config_dev = __cam_custom_ctx_config_dev,
  1040. .release_hw =
  1041. __cam_custom_ctx_release_hw_in_activated_state,
  1042. },
  1043. .crm_ops = {
  1044. .unlink = __cam_custom_ctx_unlink_in_activated,
  1045. .apply_req =
  1046. __cam_custom_ctx_apply_req_in_activated_state,
  1047. .apply_default =
  1048. __cam_custom_ctx_apply_default_settings,
  1049. .flush_req = __cam_custom_ctx_flush_req_in_top_state,
  1050. .process_evt = __cam_custom_ctx_process_evt,
  1051. },
  1052. .irq_ops = __cam_custom_ctx_handle_irq_in_activated,
  1053. .pagefault_ops = NULL,
  1054. },
  1055. };
  1056. int cam_custom_dev_context_init(struct cam_custom_context *ctx,
  1057. struct cam_context *ctx_base,
  1058. struct cam_req_mgr_kmd_ops *crm_node_intf,
  1059. struct cam_hw_mgr_intf *hw_intf,
  1060. uint32_t ctx_id)
  1061. {
  1062. int rc = -1, i = 0;
  1063. if (!ctx || !ctx_base) {
  1064. CAM_ERR(CAM_CUSTOM, "Invalid Context");
  1065. return -EINVAL;
  1066. }
  1067. /* Custom HW context setup */
  1068. memset(ctx, 0, sizeof(*ctx));
  1069. ctx->base = ctx_base;
  1070. ctx->frame_id = 0;
  1071. ctx->active_req_cnt = 0;
  1072. ctx->hw_ctx = NULL;
  1073. for (i = 0; i < CAM_CTX_REQ_MAX; i++) {
  1074. ctx->req_base[i].req_priv = &ctx->req_custom[i];
  1075. ctx->req_custom[i].base = &ctx->req_base[i];
  1076. }
  1077. /* camera context setup */
  1078. rc = cam_context_init(ctx_base, custom_dev_name, CAM_CUSTOM, ctx_id,
  1079. crm_node_intf, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  1080. if (rc) {
  1081. CAM_ERR(CAM_CUSTOM, "Camera Context Base init failed");
  1082. return rc;
  1083. }
  1084. /* link camera context with custom HW context */
  1085. ctx_base->state_machine = cam_custom_dev_ctx_top_state_machine;
  1086. ctx_base->ctx_priv = ctx;
  1087. return rc;
  1088. }
  1089. int cam_custom_dev_context_deinit(struct cam_custom_context *ctx)
  1090. {
  1091. if (ctx->base)
  1092. cam_context_deinit(ctx->base);
  1093. memset(ctx, 0, sizeof(*ctx));
  1094. return 0;
  1095. }