cam_custom_context.c 43 KB

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