cam_icp_context.c 6.7 KB

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