cam_icp_context.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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,
  23. struct cam_smmu_pf_info *pf_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. uint32_t resource_type = 0;
  30. int rc = 0;
  31. bool b_mem_found = false, b_ctx_found = false;
  32. if (!ctx) {
  33. CAM_ERR(CAM_ICP, "Invalid ctx");
  34. return -EINVAL;
  35. }
  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,
  48. &b_mem_found, &b_ctx_found, &resource_type, pf_info);
  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. return rc;
  57. }
  58. static int __cam_icp_acquire_dev_in_available(struct cam_context *ctx,
  59. struct cam_acquire_dev_cmd *cmd)
  60. {
  61. int rc;
  62. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  63. if (!rc) {
  64. ctx->state = CAM_CTX_ACQUIRED;
  65. trace_cam_context_state("ICP", ctx);
  66. }
  67. return rc;
  68. }
  69. static int __cam_icp_release_dev_in_acquired(struct cam_context *ctx,
  70. struct cam_release_dev_cmd *cmd)
  71. {
  72. int rc;
  73. rc = cam_context_release_dev_to_hw(ctx, cmd);
  74. if (rc)
  75. CAM_ERR(CAM_ICP, "Unable to release device");
  76. ctx->state = CAM_CTX_AVAILABLE;
  77. trace_cam_context_state("ICP", ctx);
  78. return rc;
  79. }
  80. static int __cam_icp_start_dev_in_acquired(struct cam_context *ctx,
  81. struct cam_start_stop_dev_cmd *cmd)
  82. {
  83. int rc;
  84. rc = cam_context_start_dev_to_hw(ctx, cmd);
  85. if (!rc) {
  86. ctx->state = CAM_CTX_READY;
  87. trace_cam_context_state("ICP", ctx);
  88. }
  89. return rc;
  90. }
  91. static int __cam_icp_dump_dev_in_ready(
  92. struct cam_context *ctx,
  93. struct cam_dump_req_cmd *cmd)
  94. {
  95. int rc;
  96. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  97. if (rc)
  98. CAM_ERR(CAM_ICP, "Failed to dump device");
  99. return rc;
  100. }
  101. static int __cam_icp_flush_dev_in_ready(struct cam_context *ctx,
  102. struct cam_flush_dev_cmd *cmd)
  103. {
  104. int rc;
  105. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  106. if (rc)
  107. CAM_ERR(CAM_ICP, "Failed to flush device");
  108. return rc;
  109. }
  110. static int __cam_icp_config_dev_in_ready(struct cam_context *ctx,
  111. struct cam_config_dev_cmd *cmd)
  112. {
  113. int rc;
  114. size_t len;
  115. uintptr_t packet_addr;
  116. struct cam_packet *packet;
  117. size_t remain_len = 0;
  118. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  119. &packet_addr, &len);
  120. if (rc) {
  121. CAM_ERR(CAM_ICP, "[%s][%d] Can not get packet address",
  122. ctx->dev_name, ctx->ctx_id);
  123. rc = -EINVAL;
  124. return rc;
  125. }
  126. remain_len = len;
  127. if ((len < sizeof(struct cam_packet)) ||
  128. (cmd->offset >= (len - sizeof(struct cam_packet)))) {
  129. CAM_ERR(CAM_CTXT,
  130. "Invalid offset, len: %zu cmd offset: %llu sizeof packet: %zu",
  131. len, cmd->offset, sizeof(struct cam_packet));
  132. return -EINVAL;
  133. }
  134. remain_len -= (size_t)cmd->offset;
  135. packet = (struct cam_packet *) ((uint8_t *)packet_addr +
  136. (uint32_t)cmd->offset);
  137. rc = cam_packet_util_validate_packet(packet, remain_len);
  138. if (rc) {
  139. CAM_ERR(CAM_CTXT, "Invalid packet params, remain length: %zu",
  140. remain_len);
  141. return rc;
  142. }
  143. if (((packet->header.op_code & 0xff) ==
  144. CAM_ICP_OPCODE_IPE_SETTINGS) ||
  145. ((packet->header.op_code & 0xff) ==
  146. CAM_ICP_OPCODE_BPS_SETTINGS))
  147. rc = cam_context_config_dev_to_hw(ctx, cmd);
  148. else
  149. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  150. if (rc)
  151. CAM_ERR(CAM_ICP, "Failed to prepare device");
  152. return rc;
  153. }
  154. static int __cam_icp_stop_dev_in_ready(struct cam_context *ctx,
  155. struct cam_start_stop_dev_cmd *cmd)
  156. {
  157. int rc;
  158. rc = cam_context_stop_dev_to_hw(ctx);
  159. if (rc)
  160. CAM_ERR(CAM_ICP, "Failed to stop device");
  161. ctx->state = CAM_CTX_ACQUIRED;
  162. trace_cam_context_state("ICP", ctx);
  163. return rc;
  164. }
  165. static int __cam_icp_release_dev_in_ready(struct cam_context *ctx,
  166. struct cam_release_dev_cmd *cmd)
  167. {
  168. int rc;
  169. rc = __cam_icp_stop_dev_in_ready(ctx, NULL);
  170. if (rc)
  171. CAM_ERR(CAM_ICP, "Failed to stop device");
  172. rc = __cam_icp_release_dev_in_acquired(ctx, cmd);
  173. if (rc)
  174. CAM_ERR(CAM_ICP, "Failed to release device");
  175. return rc;
  176. }
  177. static int __cam_icp_handle_buf_done_in_ready(void *ctx,
  178. uint32_t evt_id, void *done)
  179. {
  180. return cam_context_buf_done_from_hw(ctx, done, evt_id);
  181. }
  182. static struct cam_ctx_ops
  183. cam_icp_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  184. /* Uninit */
  185. {
  186. .ioctl_ops = {},
  187. .crm_ops = {},
  188. .irq_ops = NULL,
  189. },
  190. /* Available */
  191. {
  192. .ioctl_ops = {
  193. .acquire_dev = __cam_icp_acquire_dev_in_available,
  194. },
  195. .crm_ops = {},
  196. .irq_ops = NULL,
  197. },
  198. /* Acquired */
  199. {
  200. .ioctl_ops = {
  201. .release_dev = __cam_icp_release_dev_in_acquired,
  202. .start_dev = __cam_icp_start_dev_in_acquired,
  203. .config_dev = __cam_icp_config_dev_in_ready,
  204. .flush_dev = __cam_icp_flush_dev_in_ready,
  205. .dump_dev = __cam_icp_dump_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. /* Ready */
  212. {
  213. .ioctl_ops = {
  214. .stop_dev = __cam_icp_stop_dev_in_ready,
  215. .release_dev = __cam_icp_release_dev_in_ready,
  216. .config_dev = __cam_icp_config_dev_in_ready,
  217. .flush_dev = __cam_icp_flush_dev_in_ready,
  218. .dump_dev = __cam_icp_dump_dev_in_ready,
  219. },
  220. .crm_ops = {},
  221. .irq_ops = __cam_icp_handle_buf_done_in_ready,
  222. .pagefault_ops = cam_icp_context_dump_active_request,
  223. },
  224. /* Flushed */
  225. {},
  226. /* Activated */
  227. {
  228. .ioctl_ops = {},
  229. .crm_ops = {},
  230. .irq_ops = NULL,
  231. .pagefault_ops = cam_icp_context_dump_active_request,
  232. },
  233. };
  234. int cam_icp_context_init(struct cam_icp_context *ctx,
  235. struct cam_hw_mgr_intf *hw_intf, uint32_t ctx_id)
  236. {
  237. int rc;
  238. if ((!ctx) || (!ctx->base) || (!hw_intf)) {
  239. CAM_ERR(CAM_ICP, "Invalid params: %pK %pK", ctx, hw_intf);
  240. rc = -EINVAL;
  241. goto err;
  242. }
  243. rc = cam_context_init(ctx->base, icp_dev_name, CAM_ICP, ctx_id,
  244. NULL, hw_intf, ctx->req_base, CAM_CTX_ICP_REQ_MAX);
  245. if (rc) {
  246. CAM_ERR(CAM_ICP, "Camera Context Base init failed");
  247. goto err;
  248. }
  249. ctx->base->state_machine = cam_icp_ctx_state_machine;
  250. ctx->base->ctx_priv = ctx;
  251. ctx->base->max_hw_update_entries = CAM_CTX_CFG_MAX;
  252. ctx->base->max_in_map_entries = CAM_CTX_CFG_MAX;
  253. ctx->base->max_out_map_entries = CAM_CTX_CFG_MAX;
  254. ctx->ctxt_to_hw_map = NULL;
  255. err:
  256. return rc;
  257. }
  258. int cam_icp_context_deinit(struct cam_icp_context *ctx)
  259. {
  260. if ((!ctx) || (!ctx->base)) {
  261. CAM_ERR(CAM_ICP, "Invalid params: %pK", ctx);
  262. return -EINVAL;
  263. }
  264. cam_context_deinit(ctx->base);
  265. memset(ctx, 0, sizeof(*ctx));
  266. return 0;
  267. }