cam_custom_context.c 46 KB

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