cam_icp_context.c 6.7 KB

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