cam_icp_context.c 7.8 KB

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