cam_cre_context.c 6.4 KB

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