cam_custom_context.c 47 KB

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