cam_jpeg_context.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "cam_mem_mgr.h"
  10. #include "cam_jpeg_context.h"
  11. #include "cam_context_utils.h"
  12. #include "cam_debug_util.h"
  13. #include "cam_packet_util.h"
  14. static const char jpeg_dev_name[] = "cam-jpeg";
  15. static int cam_jpeg_context_dump_active_request(void *data, unsigned long iova,
  16. uint32_t buf_info)
  17. {
  18. struct cam_context *ctx = (struct cam_context *)data;
  19. struct cam_ctx_request *req = NULL;
  20. struct cam_ctx_request *req_temp = NULL;
  21. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  22. int rc = 0;
  23. int closest_port;
  24. bool b_mem_found = false;
  25. if (!ctx) {
  26. CAM_ERR(CAM_JPEG, "Invalid ctx");
  27. return -EINVAL;
  28. }
  29. CAM_INFO(CAM_JPEG, "iommu fault for jpeg ctx %d state %d",
  30. ctx->ctx_id, ctx->state);
  31. list_for_each_entry_safe(req, req_temp,
  32. &ctx->active_req_list, list) {
  33. pf_dbg_entry = &(req->pf_data);
  34. closest_port = -1;
  35. CAM_INFO(CAM_JPEG, "req_id : %lld ", req->request_id);
  36. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  37. iova, buf_info, &b_mem_found);
  38. if (rc)
  39. CAM_ERR(CAM_JPEG, "Failed to dump pf info");
  40. if (b_mem_found)
  41. CAM_ERR(CAM_JPEG, "Found page fault in req %lld %d",
  42. req->request_id, rc);
  43. }
  44. return rc;
  45. }
  46. static int __cam_jpeg_ctx_acquire_dev_in_available(struct cam_context *ctx,
  47. struct cam_acquire_dev_cmd *cmd)
  48. {
  49. int rc;
  50. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  51. if (rc)
  52. CAM_ERR(CAM_JPEG, "Unable to Acquire device %d", rc);
  53. else
  54. ctx->state = CAM_CTX_ACQUIRED;
  55. return rc;
  56. }
  57. static int __cam_jpeg_ctx_release_dev_in_acquired(struct cam_context *ctx,
  58. struct cam_release_dev_cmd *cmd)
  59. {
  60. int rc;
  61. rc = cam_context_release_dev_to_hw(ctx, cmd);
  62. if (rc)
  63. CAM_ERR(CAM_JPEG, "Unable to release device %d", rc);
  64. ctx->state = CAM_CTX_AVAILABLE;
  65. return rc;
  66. }
  67. static int __cam_jpeg_ctx_dump_dev_in_acquired(
  68. struct cam_context *ctx,
  69. struct cam_dump_req_cmd *cmd)
  70. {
  71. int rc;
  72. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  73. if (rc)
  74. CAM_ERR(CAM_JPEG, "Failed to dump device, rc=%d", rc);
  75. return rc;
  76. }
  77. static int __cam_jpeg_ctx_flush_dev_in_acquired(struct cam_context *ctx,
  78. struct cam_flush_dev_cmd *cmd)
  79. {
  80. int rc;
  81. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  82. if (rc)
  83. CAM_ERR(CAM_ICP, "Failed to flush device");
  84. return rc;
  85. }
  86. static int __cam_jpeg_ctx_config_dev_in_acquired(struct cam_context *ctx,
  87. struct cam_config_dev_cmd *cmd)
  88. {
  89. return cam_context_prepare_dev_to_hw(ctx, cmd);
  90. }
  91. static int __cam_jpeg_ctx_handle_buf_done_in_acquired(void *ctx,
  92. uint32_t evt_id, void *done)
  93. {
  94. return cam_context_buf_done_from_hw(ctx, done, evt_id);
  95. }
  96. static int __cam_jpeg_ctx_stop_dev_in_acquired(struct cam_context *ctx,
  97. struct cam_start_stop_dev_cmd *cmd)
  98. {
  99. int rc;
  100. rc = cam_context_stop_dev_to_hw(ctx);
  101. if (rc) {
  102. CAM_ERR(CAM_JPEG, "Failed in Stop dev, rc=%d", rc);
  103. return rc;
  104. }
  105. return rc;
  106. }
  107. /* top state machine */
  108. static struct cam_ctx_ops
  109. cam_jpeg_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  110. /* Uninit */
  111. {
  112. .ioctl_ops = { },
  113. .crm_ops = { },
  114. .irq_ops = NULL,
  115. },
  116. /* Available */
  117. {
  118. .ioctl_ops = {
  119. .acquire_dev = __cam_jpeg_ctx_acquire_dev_in_available,
  120. },
  121. .crm_ops = { },
  122. .irq_ops = NULL,
  123. },
  124. /* Acquired */
  125. {
  126. .ioctl_ops = {
  127. .release_dev = __cam_jpeg_ctx_release_dev_in_acquired,
  128. .config_dev = __cam_jpeg_ctx_config_dev_in_acquired,
  129. .stop_dev = __cam_jpeg_ctx_stop_dev_in_acquired,
  130. .flush_dev = __cam_jpeg_ctx_flush_dev_in_acquired,
  131. .dump_dev = __cam_jpeg_ctx_dump_dev_in_acquired,
  132. },
  133. .crm_ops = { },
  134. .irq_ops = __cam_jpeg_ctx_handle_buf_done_in_acquired,
  135. .pagefault_ops = cam_jpeg_context_dump_active_request,
  136. },
  137. };
  138. int cam_jpeg_context_init(struct cam_jpeg_context *ctx,
  139. struct cam_context *ctx_base,
  140. struct cam_hw_mgr_intf *hw_intf,
  141. uint32_t ctx_id)
  142. {
  143. int rc;
  144. int i;
  145. if (!ctx || !ctx_base) {
  146. CAM_ERR(CAM_JPEG, "Invalid Context");
  147. rc = -EFAULT;
  148. goto err;
  149. }
  150. memset(ctx, 0, sizeof(*ctx));
  151. ctx->base = ctx_base;
  152. for (i = 0; i < CAM_CTX_REQ_MAX; i++)
  153. ctx->req_base[i].req_priv = ctx;
  154. rc = cam_context_init(ctx_base, jpeg_dev_name, CAM_JPEG, ctx_id,
  155. NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  156. if (rc) {
  157. CAM_ERR(CAM_JPEG, "Camera Context Base init failed");
  158. goto err;
  159. }
  160. ctx_base->state_machine = cam_jpeg_ctx_state_machine;
  161. ctx_base->ctx_priv = ctx;
  162. err:
  163. return rc;
  164. }
  165. int cam_jpeg_context_deinit(struct cam_jpeg_context *ctx)
  166. {
  167. if (!ctx || !ctx->base) {
  168. CAM_ERR(CAM_JPEG, "Invalid params: %pK", ctx);
  169. return -EINVAL;
  170. }
  171. cam_context_deinit(ctx->base);
  172. memset(ctx, 0, sizeof(*ctx));
  173. return 0;
  174. }