cam_cre_context.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/videodev2.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include "cam_trace.h"
  11. #include "cam_mem_mgr.h"
  12. #include "cam_cre_context.h"
  13. #include "cam_context_utils.h"
  14. #include "cam_debug_util.h"
  15. #include "cam_packet_util.h"
  16. #include "cam_context.h"
  17. static const char cre_dev_name[] = "cam-cre";
  18. static int __cam_cre_start_dev_in_acquired(struct cam_context *ctx,
  19. struct cam_start_stop_dev_cmd *cmd)
  20. {
  21. int rc;
  22. rc = cam_context_start_dev_to_hw(ctx, cmd);
  23. if (!rc) {
  24. ctx->state = CAM_CTX_READY;
  25. trace_cam_context_state("CRE", ctx);
  26. }
  27. return rc;
  28. }
  29. static int __cam_cre_ctx_flush_dev_in_ready(struct cam_context *ctx,
  30. struct cam_flush_dev_cmd *cmd)
  31. {
  32. int rc;
  33. struct cam_context_utils_flush_args flush_args;
  34. flush_args.cmd = cmd;
  35. flush_args.flush_active_req = false;
  36. rc = cam_context_flush_dev_to_hw(ctx, &flush_args);
  37. if (rc)
  38. CAM_ERR(CAM_CRE, "Failed to flush device");
  39. return rc;
  40. }
  41. static int __cam_cre_ctx_dump_dev_in_ready(struct cam_context *ctx,
  42. struct cam_dump_req_cmd *cmd)
  43. {
  44. int rc;
  45. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  46. if (rc)
  47. CAM_ERR(CAM_CRE, "Failed to dump device");
  48. return rc;
  49. }
  50. static int __cam_cre_ctx_config_dev_in_ready(struct cam_context *ctx,
  51. struct cam_config_dev_cmd *cmd)
  52. {
  53. int rc;
  54. size_t len;
  55. uintptr_t packet_addr;
  56. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  57. &packet_addr, &len);
  58. if (rc) {
  59. CAM_ERR(CAM_CRE, "[%s][%d] Can not get packet address",
  60. ctx->dev_name, ctx->ctx_id);
  61. rc = -EINVAL;
  62. return rc;
  63. }
  64. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  65. if (rc)
  66. CAM_ERR(CAM_CRE, "Failed to prepare device");
  67. cam_mem_put_cpu_buf((int32_t) cmd->packet_handle);
  68. return rc;
  69. }
  70. static int __cam_cre_ctx_stop_dev_in_ready(struct cam_context *ctx,
  71. struct cam_start_stop_dev_cmd *cmd)
  72. {
  73. int rc;
  74. rc = cam_context_stop_dev_to_hw(ctx);
  75. if (rc)
  76. CAM_ERR(CAM_CRE, "Failed to stop device");
  77. ctx->state = CAM_CTX_ACQUIRED;
  78. trace_cam_context_state("CRE", ctx);
  79. return rc;
  80. }
  81. static int __cam_cre_ctx_release_dev_in_acquired(struct cam_context *ctx,
  82. struct cam_release_dev_cmd *cmd)
  83. {
  84. int rc;
  85. rc = cam_context_release_dev_to_hw(ctx, cmd);
  86. if (rc)
  87. CAM_ERR(CAM_CRE, "Unable to release device %d", rc);
  88. ctx->state = CAM_CTX_AVAILABLE;
  89. return rc;
  90. }
  91. static int __cam_cre_ctx_release_dev_in_ready(struct cam_context *ctx,
  92. struct cam_release_dev_cmd *cmd)
  93. {
  94. int rc;
  95. rc = __cam_cre_ctx_stop_dev_in_ready(ctx, NULL);
  96. if (rc)
  97. CAM_ERR(CAM_CRE, "Failed to stop device");
  98. rc = __cam_cre_ctx_release_dev_in_acquired(ctx, cmd);
  99. if (rc)
  100. CAM_ERR(CAM_CRE, "Failed to release device");
  101. return rc;
  102. }
  103. static int __cam_cre_ctx_handle_buf_done_in_ready(void *ctx,
  104. uint32_t evt_id, void *done)
  105. {
  106. return cam_context_buf_done_from_hw(ctx, done, evt_id);
  107. }
  108. static int cam_cre_context_dump_active_request(void *data, void *args)
  109. {
  110. struct cam_context *ctx = (struct cam_context *)data;
  111. struct cam_ctx_request *req = NULL;
  112. struct cam_ctx_request *req_temp = NULL;
  113. struct cam_hw_dump_pf_args *pf_args = (struct cam_hw_dump_pf_args *)args;
  114. int rc = 0;
  115. if (!ctx || !pf_args) {
  116. CAM_ERR(CAM_CRE, "Invalid ctx %pK or pf arguments %pK",
  117. ctx, pf_args);
  118. return -EINVAL;
  119. }
  120. CAM_INFO(CAM_CRE, "iommu fault for cre ctx %d state %d",
  121. ctx->ctx_id, ctx->state);
  122. list_for_each_entry_safe(req, req_temp,
  123. &ctx->active_req_list, list) {
  124. CAM_INFO(CAM_CRE, "Active req_id: %llu ctx_id: %u",
  125. req->request_id, ctx->ctx_id);
  126. rc = cam_context_dump_pf_info_to_hw(ctx, pf_args, &req->pf_data);
  127. if (rc)
  128. CAM_ERR(CAM_CRE, "Failed to dump pf info ctx_id: %u state: %d",
  129. ctx->ctx_id, ctx->state);
  130. }
  131. if (pf_args->pf_context_info.ctx_found) {
  132. /* Send PF notification to UMD if PF found on current CTX */
  133. rc = cam_context_send_pf_evt(ctx, pf_args);
  134. if (rc)
  135. CAM_ERR(CAM_CRE,
  136. "Failed to notify PF event to userspace rc: %d", rc);
  137. }
  138. return rc;
  139. }
  140. static int __cam_cre_ctx_acquire_dev_in_available(struct cam_context *ctx,
  141. struct cam_acquire_dev_cmd *cmd)
  142. {
  143. int rc;
  144. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  145. if (rc)
  146. CAM_ERR(CAM_CRE, "Unable to Acquire device %d", rc);
  147. else
  148. ctx->state = CAM_CTX_ACQUIRED;
  149. return rc;
  150. }
  151. /* top state machine */
  152. static struct cam_ctx_ops
  153. cam_cre_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  154. /* Uninit */
  155. {
  156. .ioctl_ops = { },
  157. .crm_ops = { },
  158. .irq_ops = NULL,
  159. },
  160. /* Available */
  161. {
  162. .ioctl_ops = {
  163. .acquire_dev = __cam_cre_ctx_acquire_dev_in_available,
  164. },
  165. .crm_ops = { },
  166. .irq_ops = NULL,
  167. },
  168. /* Acquired */
  169. {
  170. .ioctl_ops = {
  171. .release_dev = __cam_cre_ctx_release_dev_in_acquired,
  172. .start_dev = __cam_cre_start_dev_in_acquired,
  173. .config_dev = __cam_cre_ctx_config_dev_in_ready,
  174. .flush_dev = __cam_cre_ctx_flush_dev_in_ready,
  175. .dump_dev = __cam_cre_ctx_dump_dev_in_ready,
  176. },
  177. .crm_ops = { },
  178. .irq_ops = __cam_cre_ctx_handle_buf_done_in_ready,
  179. .pagefault_ops = cam_cre_context_dump_active_request,
  180. },
  181. /* Ready */
  182. {
  183. .ioctl_ops = {
  184. .stop_dev = __cam_cre_ctx_stop_dev_in_ready,
  185. .release_dev = __cam_cre_ctx_release_dev_in_ready,
  186. .config_dev = __cam_cre_ctx_config_dev_in_ready,
  187. .flush_dev = __cam_cre_ctx_flush_dev_in_ready,
  188. .dump_dev = __cam_cre_ctx_dump_dev_in_ready,
  189. },
  190. .crm_ops = {},
  191. .irq_ops = __cam_cre_ctx_handle_buf_done_in_ready,
  192. .pagefault_ops = cam_cre_context_dump_active_request,
  193. },
  194. /* Activated */
  195. {
  196. .ioctl_ops = {},
  197. .crm_ops = {},
  198. .irq_ops = NULL,
  199. .pagefault_ops = cam_cre_context_dump_active_request,
  200. },
  201. };
  202. int cam_cre_context_init(struct cam_cre_context *ctx,
  203. struct cam_hw_mgr_intf *hw_intf,
  204. uint32_t ctx_id, int img_iommu_hdl)
  205. {
  206. int rc;
  207. int i;
  208. if (!ctx || !ctx->base) {
  209. CAM_ERR(CAM_CRE, "Invalid Context");
  210. rc = -EFAULT;
  211. goto err;
  212. }
  213. for (i = 0; i < CAM_CTX_REQ_MAX; i++)
  214. ctx->req_base[i].req_priv = ctx;
  215. rc = cam_context_init(ctx->base, cre_dev_name, CAM_CRE, ctx_id,
  216. NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX, img_iommu_hdl);
  217. if (rc) {
  218. CAM_ERR(CAM_CRE, "Camera Context Base init failed");
  219. goto err;
  220. }
  221. ctx->base->state_machine = cam_cre_ctx_state_machine;
  222. ctx->base->ctx_priv = ctx;
  223. ctx->base->max_hw_update_entries = CAM_CTX_CFG_MAX;
  224. ctx->base->max_in_map_entries = CAM_CTX_CFG_MAX;
  225. ctx->base->max_out_map_entries = CAM_CTX_CFG_MAX;
  226. err:
  227. return rc;
  228. }
  229. int cam_cre_context_deinit(struct cam_cre_context *ctx)
  230. {
  231. if (!ctx || !ctx->base) {
  232. CAM_ERR(CAM_CRE, "Invalid params: %pK", ctx);
  233. return -EINVAL;
  234. }
  235. cam_context_deinit(ctx->base);
  236. memset(ctx, 0, sizeof(*ctx));
  237. return 0;
  238. }