cam_icp_context.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, 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_dump_dev_in_ready(
  91. struct cam_context *ctx,
  92. struct cam_dump_req_cmd *cmd)
  93. {
  94. int rc;
  95. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  96. if (rc)
  97. CAM_ERR(CAM_ICP, "Failed to dump device");
  98. return rc;
  99. }
  100. static int __cam_icp_flush_dev_in_ready(struct cam_context *ctx,
  101. struct cam_flush_dev_cmd *cmd)
  102. {
  103. int rc;
  104. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  105. if (rc)
  106. CAM_ERR(CAM_ICP, "Failed to flush device");
  107. return rc;
  108. }
  109. static int __cam_icp_config_dev_in_ready(struct cam_context *ctx,
  110. struct cam_config_dev_cmd *cmd)
  111. {
  112. int rc;
  113. size_t len;
  114. uintptr_t packet_addr;
  115. struct cam_packet *packet;
  116. size_t remain_len = 0;
  117. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  118. &packet_addr, &len);
  119. if (rc) {
  120. CAM_ERR(CAM_ICP, "[%s][%d] Can not get packet address",
  121. ctx->dev_name, ctx->ctx_id);
  122. rc = -EINVAL;
  123. return rc;
  124. }
  125. remain_len = len;
  126. if ((len < sizeof(struct cam_packet)) ||
  127. (cmd->offset >= (len - sizeof(struct cam_packet)))) {
  128. CAM_ERR(CAM_CTXT,
  129. "Invalid offset, len: %zu cmd offset: %llu sizeof packet: %zu",
  130. len, cmd->offset, sizeof(struct cam_packet));
  131. return -EINVAL;
  132. }
  133. remain_len -= (size_t)cmd->offset;
  134. packet = (struct cam_packet *) ((uint8_t *)packet_addr +
  135. (uint32_t)cmd->offset);
  136. rc = cam_packet_util_validate_packet(packet, remain_len);
  137. if (rc) {
  138. CAM_ERR(CAM_CTXT, "Invalid packet params, remain length: %zu",
  139. remain_len);
  140. return rc;
  141. }
  142. if (((packet->header.op_code & 0xff) ==
  143. CAM_ICP_OPCODE_IPE_SETTINGS) ||
  144. ((packet->header.op_code & 0xff) ==
  145. CAM_ICP_OPCODE_BPS_SETTINGS))
  146. rc = cam_context_config_dev_to_hw(ctx, cmd);
  147. else
  148. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  149. if (rc)
  150. CAM_ERR(CAM_ICP, "Failed to prepare device");
  151. return rc;
  152. }
  153. static int __cam_icp_stop_dev_in_ready(struct cam_context *ctx,
  154. struct cam_start_stop_dev_cmd *cmd)
  155. {
  156. int rc;
  157. rc = cam_context_stop_dev_to_hw(ctx);
  158. if (rc)
  159. CAM_ERR(CAM_ICP, "Failed to stop device");
  160. ctx->state = CAM_CTX_ACQUIRED;
  161. trace_cam_context_state("ICP", ctx);
  162. return rc;
  163. }
  164. static int __cam_icp_release_dev_in_ready(struct cam_context *ctx,
  165. struct cam_release_dev_cmd *cmd)
  166. {
  167. int rc;
  168. rc = __cam_icp_stop_dev_in_ready(ctx, NULL);
  169. if (rc)
  170. CAM_ERR(CAM_ICP, "Failed to stop device");
  171. rc = __cam_icp_release_dev_in_acquired(ctx, cmd);
  172. if (rc)
  173. CAM_ERR(CAM_ICP, "Failed to release device");
  174. return rc;
  175. }
  176. static int __cam_icp_handle_buf_done_in_ready(void *ctx,
  177. uint32_t evt_id, void *done)
  178. {
  179. return cam_context_buf_done_from_hw(ctx, done, evt_id);
  180. }
  181. static struct cam_ctx_ops
  182. cam_icp_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  183. /* Uninit */
  184. {
  185. .ioctl_ops = {},
  186. .crm_ops = {},
  187. .irq_ops = NULL,
  188. },
  189. /* Available */
  190. {
  191. .ioctl_ops = {
  192. .acquire_dev = __cam_icp_acquire_dev_in_available,
  193. },
  194. .crm_ops = {},
  195. .irq_ops = NULL,
  196. },
  197. /* Acquired */
  198. {
  199. .ioctl_ops = {
  200. .release_dev = __cam_icp_release_dev_in_acquired,
  201. .start_dev = __cam_icp_start_dev_in_acquired,
  202. .config_dev = __cam_icp_config_dev_in_ready,
  203. .flush_dev = __cam_icp_flush_dev_in_ready,
  204. .dump_dev = __cam_icp_dump_dev_in_ready,
  205. },
  206. .crm_ops = {},
  207. .irq_ops = __cam_icp_handle_buf_done_in_ready,
  208. .pagefault_ops = cam_icp_context_dump_active_request,
  209. },
  210. /* Ready */
  211. {
  212. .ioctl_ops = {
  213. .stop_dev = __cam_icp_stop_dev_in_ready,
  214. .release_dev = __cam_icp_release_dev_in_ready,
  215. .config_dev = __cam_icp_config_dev_in_ready,
  216. .flush_dev = __cam_icp_flush_dev_in_ready,
  217. .dump_dev = __cam_icp_dump_dev_in_ready,
  218. },
  219. .crm_ops = {},
  220. .irq_ops = __cam_icp_handle_buf_done_in_ready,
  221. .pagefault_ops = cam_icp_context_dump_active_request,
  222. },
  223. /* Flushed */
  224. {},
  225. /* Activated */
  226. {
  227. .ioctl_ops = {},
  228. .crm_ops = {},
  229. .irq_ops = NULL,
  230. .pagefault_ops = cam_icp_context_dump_active_request,
  231. },
  232. };
  233. int cam_icp_context_init(struct cam_icp_context *ctx,
  234. struct cam_hw_mgr_intf *hw_intf, uint32_t ctx_id)
  235. {
  236. int rc;
  237. if ((!ctx) || (!ctx->base) || (!hw_intf)) {
  238. CAM_ERR(CAM_ICP, "Invalid params: %pK %pK", ctx, hw_intf);
  239. rc = -EINVAL;
  240. goto err;
  241. }
  242. rc = cam_context_init(ctx->base, icp_dev_name, CAM_ICP, ctx_id,
  243. NULL, hw_intf, ctx->req_base, CAM_CTX_ICP_REQ_MAX);
  244. if (rc) {
  245. CAM_ERR(CAM_ICP, "Camera Context Base init failed");
  246. goto err;
  247. }
  248. ctx->base->state_machine = cam_icp_ctx_state_machine;
  249. ctx->base->ctx_priv = ctx;
  250. ctx->ctxt_to_hw_map = NULL;
  251. err:
  252. return rc;
  253. }
  254. int cam_icp_context_deinit(struct cam_icp_context *ctx)
  255. {
  256. if ((!ctx) || (!ctx->base)) {
  257. CAM_ERR(CAM_ICP, "Invalid params: %pK", ctx);
  258. return -EINVAL;
  259. }
  260. cam_context_deinit(ctx->base);
  261. memset(ctx, 0, sizeof(*ctx));
  262. return 0;
  263. }