cam_fd_context.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include "cam_debug_util.h"
  9. #include "cam_fd_context.h"
  10. #include "cam_trace.h"
  11. static const char fd_dev_name[] = "cam-fd";
  12. /* Functions in Available state */
  13. static int __cam_fd_ctx_acquire_dev_in_available(struct cam_context *ctx,
  14. struct cam_acquire_dev_cmd *cmd)
  15. {
  16. int rc;
  17. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  18. if (rc) {
  19. CAM_ERR(CAM_FD, "Failed in Acquire dev, rc=%d", rc);
  20. return rc;
  21. }
  22. ctx->state = CAM_CTX_ACQUIRED;
  23. trace_cam_context_state("FD", ctx);
  24. return rc;
  25. }
  26. /* Functions in Acquired state */
  27. static int __cam_fd_ctx_release_dev_in_acquired(struct cam_context *ctx,
  28. struct cam_release_dev_cmd *cmd)
  29. {
  30. int rc;
  31. rc = cam_context_release_dev_to_hw(ctx, cmd);
  32. if (rc) {
  33. CAM_ERR(CAM_FD, "Failed in Release dev, rc=%d", rc);
  34. return rc;
  35. }
  36. ctx->state = CAM_CTX_AVAILABLE;
  37. trace_cam_context_state("FD", ctx);
  38. return rc;
  39. }
  40. static int __cam_fd_ctx_config_dev_in_acquired(struct cam_context *ctx,
  41. struct cam_config_dev_cmd *cmd)
  42. {
  43. int rc;
  44. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  45. if (rc) {
  46. CAM_ERR(CAM_FD, "Failed in Prepare dev, rc=%d", rc);
  47. return rc;
  48. }
  49. return rc;
  50. }
  51. static int __cam_fd_ctx_start_dev_in_acquired(struct cam_context *ctx,
  52. struct cam_start_stop_dev_cmd *cmd)
  53. {
  54. int rc;
  55. rc = cam_context_start_dev_to_hw(ctx, cmd);
  56. if (rc) {
  57. CAM_ERR(CAM_FD, "Failed in Start dev, rc=%d", rc);
  58. return rc;
  59. }
  60. ctx->state = CAM_CTX_ACTIVATED;
  61. trace_cam_context_state("FD", ctx);
  62. return rc;
  63. }
  64. /* Functions in Activated state */
  65. static int __cam_fd_ctx_stop_dev_in_activated(struct cam_context *ctx,
  66. struct cam_start_stop_dev_cmd *cmd)
  67. {
  68. int rc;
  69. rc = cam_context_stop_dev_to_hw(ctx);
  70. if (rc) {
  71. CAM_ERR(CAM_FD, "Failed in Stop dev, rc=%d", rc);
  72. return rc;
  73. }
  74. ctx->state = CAM_CTX_ACQUIRED;
  75. trace_cam_context_state("FD", ctx);
  76. return rc;
  77. }
  78. static int __cam_fd_ctx_release_dev_in_activated(struct cam_context *ctx,
  79. struct cam_release_dev_cmd *cmd)
  80. {
  81. int rc;
  82. rc = __cam_fd_ctx_stop_dev_in_activated(ctx, NULL);
  83. if (rc) {
  84. CAM_ERR(CAM_FD, "Failed in Stop dev, rc=%d", rc);
  85. return rc;
  86. }
  87. rc = __cam_fd_ctx_release_dev_in_acquired(ctx, cmd);
  88. if (rc) {
  89. CAM_ERR(CAM_FD, "Failed in Release dev, rc=%d", rc);
  90. return rc;
  91. }
  92. return rc;
  93. }
  94. static int __cam_fd_ctx_dump_dev_in_activated(
  95. struct cam_context *ctx,
  96. struct cam_dump_req_cmd *cmd)
  97. {
  98. int rc;
  99. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  100. if (rc)
  101. CAM_ERR(CAM_FD, "Failed to dump device, rc=%d", rc);
  102. return rc;
  103. }
  104. static int __cam_fd_ctx_flush_dev_in_activated(struct cam_context *ctx,
  105. struct cam_flush_dev_cmd *cmd)
  106. {
  107. int rc;
  108. struct cam_context_utils_flush_args flush_args;
  109. flush_args.cmd = cmd;
  110. flush_args.flush_active_req = true;
  111. rc = cam_context_flush_dev_to_hw(ctx, &flush_args);
  112. if (rc)
  113. CAM_ERR(CAM_ICP, "Failed to flush device, rc=%d", rc);
  114. return rc;
  115. }
  116. static int __cam_fd_ctx_config_dev_in_activated(
  117. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  118. {
  119. int rc;
  120. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  121. if (rc) {
  122. CAM_ERR(CAM_FD, "Failed in Prepare dev, rc=%d", rc);
  123. return rc;
  124. }
  125. return rc;
  126. }
  127. static int __cam_fd_ctx_handle_irq_in_activated(void *context,
  128. uint32_t evt_id, void *evt_data)
  129. {
  130. int rc;
  131. rc = cam_context_buf_done_from_hw(context, evt_data, evt_id);
  132. if (rc) {
  133. CAM_ERR(CAM_FD, "Failed in buf done, rc=%d", rc);
  134. return rc;
  135. }
  136. return rc;
  137. }
  138. /* top state machine */
  139. static struct cam_ctx_ops
  140. cam_fd_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  141. /* Uninit */
  142. {
  143. .ioctl_ops = {},
  144. .crm_ops = {},
  145. .irq_ops = NULL,
  146. },
  147. /* Available */
  148. {
  149. .ioctl_ops = {
  150. .acquire_dev = __cam_fd_ctx_acquire_dev_in_available,
  151. },
  152. .crm_ops = {},
  153. .irq_ops = NULL,
  154. },
  155. /* Acquired */
  156. {
  157. .ioctl_ops = {
  158. .release_dev = __cam_fd_ctx_release_dev_in_acquired,
  159. .config_dev = __cam_fd_ctx_config_dev_in_acquired,
  160. .start_dev = __cam_fd_ctx_start_dev_in_acquired,
  161. },
  162. .crm_ops = {},
  163. .irq_ops = NULL,
  164. },
  165. /* Ready */
  166. {
  167. .ioctl_ops = {},
  168. .crm_ops = {},
  169. .irq_ops = NULL,
  170. },
  171. /* Flushed */
  172. {
  173. .ioctl_ops = {},
  174. },
  175. /* Activated */
  176. {
  177. .ioctl_ops = {
  178. .stop_dev = __cam_fd_ctx_stop_dev_in_activated,
  179. .release_dev = __cam_fd_ctx_release_dev_in_activated,
  180. .config_dev = __cam_fd_ctx_config_dev_in_activated,
  181. .flush_dev = __cam_fd_ctx_flush_dev_in_activated,
  182. .dump_dev = __cam_fd_ctx_dump_dev_in_activated,
  183. },
  184. .crm_ops = {},
  185. .irq_ops = __cam_fd_ctx_handle_irq_in_activated,
  186. },
  187. };
  188. int cam_fd_context_init(struct cam_fd_context *fd_ctx,
  189. struct cam_context *base_ctx, struct cam_hw_mgr_intf *hw_intf,
  190. uint32_t ctx_id, int img_iommu_hdl)
  191. {
  192. int rc;
  193. if (!base_ctx || !fd_ctx) {
  194. CAM_ERR(CAM_FD, "Invalid Context %pK %pK", base_ctx, fd_ctx);
  195. return -EINVAL;
  196. }
  197. memset(fd_ctx, 0, sizeof(*fd_ctx));
  198. rc = cam_context_init(base_ctx, fd_dev_name, CAM_FD, ctx_id,
  199. NULL, hw_intf, fd_ctx->req_base, CAM_CTX_REQ_MAX, img_iommu_hdl);
  200. if (rc) {
  201. CAM_ERR(CAM_FD, "Camera Context Base init failed, rc=%d", rc);
  202. return rc;
  203. }
  204. fd_ctx->base = base_ctx;
  205. base_ctx->ctx_priv = fd_ctx;
  206. base_ctx->state_machine = cam_fd_ctx_state_machine;
  207. base_ctx->max_hw_update_entries = CAM_CTX_CFG_MAX;
  208. base_ctx->max_in_map_entries = CAM_CTX_CFG_MAX;
  209. base_ctx->max_out_map_entries = CAM_CTX_CFG_MAX;
  210. return rc;
  211. }
  212. int cam_fd_context_deinit(struct cam_fd_context *fd_ctx)
  213. {
  214. int rc = 0;
  215. if (!fd_ctx || !fd_ctx->base) {
  216. CAM_ERR(CAM_FD, "Invalid inputs %pK", fd_ctx);
  217. return -EINVAL;
  218. }
  219. rc = cam_context_deinit(fd_ctx->base);
  220. if (rc)
  221. CAM_ERR(CAM_FD, "Error in base deinit, rc=%d", rc);
  222. memset(fd_ctx, 0, sizeof(*fd_ctx));
  223. return rc;
  224. }