cam_icp_context.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, 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 <cam_sync.h>
  10. #include <cam_defs.h>
  11. #include <cam_icp.h>
  12. #include "cam_node.h"
  13. #include "cam_context.h"
  14. #include "cam_context_utils.h"
  15. #include "cam_icp_context.h"
  16. #include "cam_req_mgr_util.h"
  17. #include "cam_mem_mgr.h"
  18. #include "cam_trace.h"
  19. #include "cam_debug_util.h"
  20. #include "cam_packet_util.h"
  21. static const char icp_dev_name[] = "cam-icp";
  22. static int cam_icp_context_dump_active_request(void *data, unsigned long iova,
  23. uint32_t buf_info)
  24. {
  25. struct cam_context *ctx = (struct cam_context *)data;
  26. struct cam_ctx_request *req = NULL;
  27. struct cam_ctx_request *req_temp = NULL;
  28. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  29. int rc = 0;
  30. bool b_mem_found = false;
  31. if (!ctx) {
  32. CAM_ERR(CAM_ICP, "Invalid ctx");
  33. return -EINVAL;
  34. }
  35. CAM_INFO(CAM_ICP, "iommu fault for icp ctx %d state %d",
  36. ctx->ctx_id, ctx->state);
  37. list_for_each_entry_safe(req, req_temp,
  38. &ctx->active_req_list, list) {
  39. pf_dbg_entry = &(req->pf_data);
  40. CAM_INFO(CAM_ICP, "req_id : %lld", req->request_id);
  41. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  42. iova, buf_info, &b_mem_found);
  43. if (rc)
  44. CAM_ERR(CAM_ICP, "Failed to dump pf info");
  45. if (b_mem_found)
  46. CAM_ERR(CAM_ICP, "Found page fault in req %lld %d",
  47. req->request_id, rc);
  48. }
  49. return rc;
  50. }
  51. static int __cam_icp_acquire_dev_in_available(struct cam_context *ctx,
  52. struct cam_acquire_dev_cmd *cmd)
  53. {
  54. int rc;
  55. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  56. if (!rc) {
  57. ctx->state = CAM_CTX_ACQUIRED;
  58. trace_cam_context_state("ICP", ctx);
  59. }
  60. return rc;
  61. }
  62. static int __cam_icp_release_dev_in_acquired(struct cam_context *ctx,
  63. struct cam_release_dev_cmd *cmd)
  64. {
  65. int rc;
  66. rc = cam_context_release_dev_to_hw(ctx, cmd);
  67. if (rc)
  68. CAM_ERR(CAM_ICP, "Unable to release device");
  69. ctx->state = CAM_CTX_AVAILABLE;
  70. trace_cam_context_state("ICP", ctx);
  71. return rc;
  72. }
  73. static int __cam_icp_start_dev_in_acquired(struct cam_context *ctx,
  74. struct cam_start_stop_dev_cmd *cmd)
  75. {
  76. int rc;
  77. rc = cam_context_start_dev_to_hw(ctx, cmd);
  78. if (!rc) {
  79. ctx->state = CAM_CTX_READY;
  80. trace_cam_context_state("ICP", ctx);
  81. }
  82. return rc;
  83. }
  84. static int __cam_icp_flush_dev_in_ready(struct cam_context *ctx,
  85. struct cam_flush_dev_cmd *cmd)
  86. {
  87. int rc;
  88. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  89. if (rc)
  90. CAM_ERR(CAM_ICP, "Failed to flush device");
  91. return rc;
  92. }
  93. static int __cam_icp_config_dev_in_ready(struct cam_context *ctx,
  94. struct cam_config_dev_cmd *cmd)
  95. {
  96. int rc;
  97. size_t len;
  98. uintptr_t packet_addr;
  99. struct cam_packet *packet;
  100. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  101. &packet_addr, &len);
  102. if (rc) {
  103. CAM_ERR(CAM_ICP, "[%s][%d] Can not get packet address",
  104. ctx->dev_name, ctx->ctx_id);
  105. rc = -EINVAL;
  106. return rc;
  107. }
  108. packet = (struct cam_packet *) ((uint8_t *)packet_addr +
  109. (uint32_t)cmd->offset);
  110. if (((packet->header.op_code & 0xff) ==
  111. CAM_ICP_OPCODE_IPE_SETTINGS) ||
  112. ((packet->header.op_code & 0xff) ==
  113. CAM_ICP_OPCODE_BPS_SETTINGS))
  114. rc = cam_context_config_dev_to_hw(ctx, cmd);
  115. else
  116. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  117. if (rc)
  118. CAM_ERR(CAM_ICP, "Failed to prepare device");
  119. return rc;
  120. }
  121. static int __cam_icp_stop_dev_in_ready(struct cam_context *ctx,
  122. struct cam_start_stop_dev_cmd *cmd)
  123. {
  124. int rc;
  125. rc = cam_context_stop_dev_to_hw(ctx);
  126. if (rc)
  127. CAM_ERR(CAM_ICP, "Failed to stop device");
  128. ctx->state = CAM_CTX_ACQUIRED;
  129. trace_cam_context_state("ICP", ctx);
  130. return rc;
  131. }
  132. static int __cam_icp_release_dev_in_ready(struct cam_context *ctx,
  133. struct cam_release_dev_cmd *cmd)
  134. {
  135. int rc;
  136. rc = __cam_icp_stop_dev_in_ready(ctx, NULL);
  137. if (rc)
  138. CAM_ERR(CAM_ICP, "Failed to stop device");
  139. rc = __cam_icp_release_dev_in_acquired(ctx, cmd);
  140. if (rc)
  141. CAM_ERR(CAM_ICP, "Failed to release device");
  142. return rc;
  143. }
  144. static int __cam_icp_handle_buf_done_in_ready(void *ctx,
  145. uint32_t evt_id, void *done)
  146. {
  147. return cam_context_buf_done_from_hw(ctx, done, evt_id);
  148. }
  149. static struct cam_ctx_ops
  150. cam_icp_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  151. /* Uninit */
  152. {
  153. .ioctl_ops = {},
  154. .crm_ops = {},
  155. .irq_ops = NULL,
  156. },
  157. /* Available */
  158. {
  159. .ioctl_ops = {
  160. .acquire_dev = __cam_icp_acquire_dev_in_available,
  161. },
  162. .crm_ops = {},
  163. .irq_ops = NULL,
  164. },
  165. /* Acquired */
  166. {
  167. .ioctl_ops = {
  168. .release_dev = __cam_icp_release_dev_in_acquired,
  169. .start_dev = __cam_icp_start_dev_in_acquired,
  170. .config_dev = __cam_icp_config_dev_in_ready,
  171. .flush_dev = __cam_icp_flush_dev_in_ready,
  172. },
  173. .crm_ops = {},
  174. .irq_ops = __cam_icp_handle_buf_done_in_ready,
  175. .pagefault_ops = cam_icp_context_dump_active_request,
  176. },
  177. /* Ready */
  178. {
  179. .ioctl_ops = {
  180. .stop_dev = __cam_icp_stop_dev_in_ready,
  181. .release_dev = __cam_icp_release_dev_in_ready,
  182. .config_dev = __cam_icp_config_dev_in_ready,
  183. .flush_dev = __cam_icp_flush_dev_in_ready,
  184. },
  185. .crm_ops = {},
  186. .irq_ops = __cam_icp_handle_buf_done_in_ready,
  187. .pagefault_ops = cam_icp_context_dump_active_request,
  188. },
  189. /* Activated */
  190. {
  191. .ioctl_ops = {},
  192. .crm_ops = {},
  193. .irq_ops = NULL,
  194. .pagefault_ops = cam_icp_context_dump_active_request,
  195. },
  196. };
  197. int cam_icp_context_init(struct cam_icp_context *ctx,
  198. struct cam_hw_mgr_intf *hw_intf, uint32_t ctx_id)
  199. {
  200. int rc;
  201. if ((!ctx) || (!ctx->base) || (!hw_intf)) {
  202. CAM_ERR(CAM_ICP, "Invalid params: %pK %pK", ctx, hw_intf);
  203. rc = -EINVAL;
  204. goto err;
  205. }
  206. rc = cam_context_init(ctx->base, icp_dev_name, CAM_ICP, ctx_id,
  207. NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  208. if (rc) {
  209. CAM_ERR(CAM_ICP, "Camera Context Base init failed");
  210. goto err;
  211. }
  212. ctx->base->state_machine = cam_icp_ctx_state_machine;
  213. ctx->base->ctx_priv = ctx;
  214. ctx->ctxt_to_hw_map = NULL;
  215. err:
  216. return rc;
  217. }
  218. int cam_icp_context_deinit(struct cam_icp_context *ctx)
  219. {
  220. if ((!ctx) || (!ctx->base)) {
  221. CAM_ERR(CAM_ICP, "Invalid params: %pK", ctx);
  222. return -EINVAL;
  223. }
  224. cam_context_deinit(ctx->base);
  225. memset(ctx, 0, sizeof(*ctx));
  226. return 0;
  227. }