cam_cre_context.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 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 "cam_trace.h"
  10. #include "cam_mem_mgr.h"
  11. #include "cam_cre_context.h"
  12. #include "cam_context_utils.h"
  13. #include "cam_debug_util.h"
  14. #include "cam_packet_util.h"
  15. #include "cam_context.h"
  16. static const char cre_dev_name[] = "cam-cre";
  17. static int __cam_cre_start_dev_in_acquired(struct cam_context *ctx,
  18. struct cam_start_stop_dev_cmd *cmd)
  19. {
  20. int rc;
  21. rc = cam_context_start_dev_to_hw(ctx, cmd);
  22. if (!rc) {
  23. ctx->state = CAM_CTX_READY;
  24. trace_cam_context_state("CRE", ctx);
  25. }
  26. return rc;
  27. }
  28. static int __cam_cre_ctx_flush_dev_in_ready(struct cam_context *ctx,
  29. struct cam_flush_dev_cmd *cmd)
  30. {
  31. int rc;
  32. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  33. if (rc)
  34. CAM_ERR(CAM_CRE, "Failed to flush device");
  35. return rc;
  36. }
  37. static int __cam_cre_ctx_dump_dev_in_ready(struct cam_context *ctx,
  38. struct cam_dump_req_cmd *cmd)
  39. {
  40. int rc;
  41. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  42. if (rc)
  43. CAM_ERR(CAM_CRE, "Failed to dump device");
  44. return rc;
  45. }
  46. static int __cam_cre_ctx_config_dev_in_ready(struct cam_context *ctx,
  47. struct cam_config_dev_cmd *cmd)
  48. {
  49. int rc;
  50. size_t len;
  51. uintptr_t packet_addr;
  52. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  53. &packet_addr, &len);
  54. if (rc) {
  55. CAM_ERR(CAM_CRE, "[%s][%d] Can not get packet address",
  56. ctx->dev_name, ctx->ctx_id);
  57. rc = -EINVAL;
  58. return rc;
  59. }
  60. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  61. if (rc)
  62. CAM_ERR(CAM_CRE, "Failed to prepare device");
  63. return rc;
  64. }
  65. static int __cam_cre_ctx_stop_dev_in_ready(struct cam_context *ctx,
  66. struct cam_start_stop_dev_cmd *cmd)
  67. {
  68. int rc;
  69. rc = cam_context_stop_dev_to_hw(ctx);
  70. if (rc)
  71. CAM_ERR(CAM_CRE, "Failed to stop device");
  72. ctx->state = CAM_CTX_ACQUIRED;
  73. trace_cam_context_state("CRE", ctx);
  74. return rc;
  75. }
  76. static int __cam_cre_ctx_release_dev_in_acquired(struct cam_context *ctx,
  77. struct cam_release_dev_cmd *cmd)
  78. {
  79. int rc;
  80. rc = cam_context_release_dev_to_hw(ctx, cmd);
  81. if (rc)
  82. CAM_ERR(CAM_CRE, "Unable to release device %d", rc);
  83. ctx->state = CAM_CTX_AVAILABLE;
  84. return rc;
  85. }
  86. static int __cam_cre_ctx_release_dev_in_ready(struct cam_context *ctx,
  87. struct cam_release_dev_cmd *cmd)
  88. {
  89. int rc;
  90. rc = __cam_cre_ctx_stop_dev_in_ready(ctx, NULL);
  91. if (rc)
  92. CAM_ERR(CAM_CRE, "Failed to stop device");
  93. rc = __cam_cre_ctx_release_dev_in_acquired(ctx, cmd);
  94. if (rc)
  95. CAM_ERR(CAM_CRE, "Failed to release device");
  96. return rc;
  97. }
  98. static int __cam_cre_ctx_handle_buf_done_in_ready(void *ctx,
  99. uint32_t evt_id, void *done)
  100. {
  101. return cam_context_buf_done_from_hw(ctx, done, evt_id);
  102. }
  103. static int cam_cre_context_dump_active_request(void *data,
  104. struct cam_smmu_pf_info *pf_info)
  105. {
  106. struct cam_context *ctx = (struct cam_context *)data;
  107. struct cam_ctx_request *req = NULL;
  108. struct cam_ctx_request *req_temp = NULL;
  109. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  110. uint32_t resource_type = 0;
  111. int rc = 0;
  112. int closest_port;
  113. bool b_mem_found = false, b_ctx_found = false;
  114. if (!ctx) {
  115. CAM_ERR(CAM_CRE, "Invalid ctx");
  116. return -EINVAL;
  117. }
  118. CAM_INFO(CAM_CRE, "iommu fault for cre ctx %d state %d",
  119. ctx->ctx_id, ctx->state);
  120. list_for_each_entry_safe(req, req_temp,
  121. &ctx->active_req_list, list) {
  122. pf_dbg_entry = &(req->pf_data);
  123. closest_port = -1;
  124. CAM_INFO(CAM_CRE, "req_id : %lld ", req->request_id);
  125. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  126. &b_mem_found, &b_ctx_found, &resource_type, pf_info);
  127. if (rc)
  128. CAM_ERR(CAM_CRE, "Failed to dump pf info");
  129. if (b_mem_found)
  130. CAM_ERR(CAM_CRE, "Found page fault in req %lld %d",
  131. req->request_id, rc);
  132. }
  133. return rc;
  134. }
  135. static int __cam_cre_ctx_acquire_dev_in_available(struct cam_context *ctx,
  136. struct cam_acquire_dev_cmd *cmd)
  137. {
  138. int rc;
  139. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  140. if (rc)
  141. CAM_ERR(CAM_CRE, "Unable to Acquire device %d", rc);
  142. else
  143. ctx->state = CAM_CTX_ACQUIRED;
  144. return rc;
  145. }
  146. /* top state machine */
  147. static struct cam_ctx_ops
  148. cam_cre_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  149. /* Uninit */
  150. {
  151. .ioctl_ops = { },
  152. .crm_ops = { },
  153. .irq_ops = NULL,
  154. },
  155. /* Available */
  156. {
  157. .ioctl_ops = {
  158. .acquire_dev = __cam_cre_ctx_acquire_dev_in_available,
  159. },
  160. .crm_ops = { },
  161. .irq_ops = NULL,
  162. },
  163. /* Acquired */
  164. {
  165. .ioctl_ops = {
  166. .release_dev = __cam_cre_ctx_release_dev_in_acquired,
  167. .start_dev = __cam_cre_start_dev_in_acquired,
  168. .config_dev = __cam_cre_ctx_config_dev_in_ready,
  169. .flush_dev = __cam_cre_ctx_flush_dev_in_ready,
  170. .dump_dev = __cam_cre_ctx_dump_dev_in_ready,
  171. },
  172. .crm_ops = { },
  173. .irq_ops = __cam_cre_ctx_handle_buf_done_in_ready,
  174. .pagefault_ops = cam_cre_context_dump_active_request,
  175. },
  176. /* Ready */
  177. {
  178. .ioctl_ops = {
  179. .stop_dev = __cam_cre_ctx_stop_dev_in_ready,
  180. .release_dev = __cam_cre_ctx_release_dev_in_ready,
  181. .config_dev = __cam_cre_ctx_config_dev_in_ready,
  182. .flush_dev = __cam_cre_ctx_flush_dev_in_ready,
  183. .dump_dev = __cam_cre_ctx_dump_dev_in_ready,
  184. },
  185. .crm_ops = {},
  186. .irq_ops = __cam_cre_ctx_handle_buf_done_in_ready,
  187. .pagefault_ops = cam_cre_context_dump_active_request,
  188. },
  189. /* Activated */
  190. {
  191. .ioctl_ops = {},
  192. .crm_ops = {},
  193. .irq_ops = NULL,
  194. .pagefault_ops = cam_cre_context_dump_active_request,
  195. },
  196. };
  197. int cam_cre_context_init(struct cam_cre_context *ctx,
  198. struct cam_context *ctx_base,
  199. struct cam_hw_mgr_intf *hw_intf,
  200. uint32_t ctx_id)
  201. {
  202. int rc;
  203. int i;
  204. if (!ctx || !ctx_base) {
  205. CAM_ERR(CAM_CRE, "Invalid Context");
  206. rc = -EFAULT;
  207. goto err;
  208. }
  209. memset(ctx, 0, sizeof(*ctx));
  210. ctx->base = ctx_base;
  211. for (i = 0; i < CAM_CTX_REQ_MAX; i++)
  212. ctx->req_base[i].req_priv = ctx;
  213. rc = cam_context_init(ctx_base, cre_dev_name, CAM_CRE, ctx_id,
  214. NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  215. if (rc) {
  216. CAM_ERR(CAM_CRE, "Camera Context Base init failed");
  217. goto err;
  218. }
  219. ctx_base->state_machine = cam_cre_ctx_state_machine;
  220. ctx_base->ctx_priv = ctx;
  221. err:
  222. return rc;
  223. }
  224. int cam_cre_context_deinit(struct cam_cre_context *ctx)
  225. {
  226. if (!ctx || !ctx->base) {
  227. CAM_ERR(CAM_CRE, "Invalid params: %pK", ctx);
  228. return -EINVAL;
  229. }
  230. cam_context_deinit(ctx->base);
  231. memset(ctx, 0, sizeof(*ctx));
  232. return 0;
  233. }