cam_jpeg_context.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/videodev2.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <media/cam_sync.h>
  11. #include "cam_mem_mgr.h"
  12. #include "cam_jpeg_context.h"
  13. #include "cam_context_utils.h"
  14. #include "cam_debug_util.h"
  15. #include "cam_packet_util.h"
  16. static const char jpeg_dev_name[] = "cam-jpeg";
  17. static int cam_jpeg_context_dump_active_request(void *data, void *args)
  18. {
  19. struct cam_context *ctx = (struct cam_context *)data;
  20. struct cam_ctx_request *req = NULL;
  21. struct cam_ctx_request *req_temp = NULL;
  22. struct cam_hw_dump_pf_args *pf_args = (struct cam_hw_dump_pf_args *)args;
  23. int rc = 0;
  24. if (!ctx || !pf_args) {
  25. CAM_ERR(CAM_JPEG, "Invalid ctx %pK or pf args %pK",
  26. ctx, pf_args);
  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. CAM_INFO(CAM_JPEG, "Active req_id: %lld, ctx_id: %u",
  34. req->request_id, ctx->ctx_id);
  35. rc = cam_context_dump_pf_info_to_hw(ctx, pf_args, &req->pf_data);
  36. if (rc)
  37. CAM_ERR(CAM_JPEG, "Failed to dump pf info ctx_id: %u state: %d",
  38. ctx->ctx_id, ctx->state);
  39. }
  40. if (pf_args->pf_context_info.ctx_found) {
  41. /* Send PF notification to UMD if PF found on current CTX */
  42. rc = cam_context_send_pf_evt(ctx, pf_args);
  43. if (rc)
  44. CAM_ERR(CAM_JPEG,
  45. "Failed to notify PF event to userspace rc: %d", rc);
  46. }
  47. return rc;
  48. }
  49. static int cam_jpeg_context_mini_dump(void *priv, void *args)
  50. {
  51. int rc;
  52. struct cam_context *ctx;
  53. if (!priv || args) {
  54. CAM_ERR(CAM_ICP, "Invalid param priv %pK args %pK", priv, args);
  55. return -EINVAL;
  56. }
  57. ctx = (struct cam_context *)priv;
  58. rc = cam_context_mini_dump(ctx, args);
  59. if (rc)
  60. CAM_ERR(CAM_JPEG, "Mini Dump failed %d", rc);
  61. return rc;
  62. }
  63. static int __cam_jpeg_ctx_acquire_dev_in_available(struct cam_context *ctx,
  64. struct cam_acquire_dev_cmd *cmd)
  65. {
  66. int rc;
  67. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  68. if (rc)
  69. CAM_ERR(CAM_JPEG, "Unable to Acquire device %d", rc);
  70. else
  71. ctx->state = CAM_CTX_ACQUIRED;
  72. return rc;
  73. }
  74. static int __cam_jpeg_ctx_release_dev_in_acquired(struct cam_context *ctx,
  75. struct cam_release_dev_cmd *cmd)
  76. {
  77. int rc;
  78. rc = cam_context_release_dev_to_hw(ctx, cmd);
  79. if (rc)
  80. CAM_ERR(CAM_JPEG, "Unable to release device %d", rc);
  81. cam_common_release_err_params(ctx->dev_hdl);
  82. ctx->state = CAM_CTX_AVAILABLE;
  83. return rc;
  84. }
  85. static int __cam_jpeg_ctx_dump_dev_in_acquired(
  86. struct cam_context *ctx,
  87. struct cam_dump_req_cmd *cmd)
  88. {
  89. int rc;
  90. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  91. if (rc)
  92. CAM_ERR(CAM_JPEG, "Failed to dump device, rc=%d", rc);
  93. return rc;
  94. }
  95. static int __cam_jpeg_ctx_flush_dev_in_acquired(struct cam_context *ctx,
  96. struct cam_flush_dev_cmd *cmd)
  97. {
  98. int rc;
  99. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  100. if (rc)
  101. CAM_ERR(CAM_ICP, "Failed to flush device");
  102. return rc;
  103. }
  104. static int __cam_jpeg_ctx_config_dev_in_acquired(struct cam_context *ctx,
  105. struct cam_config_dev_cmd *cmd)
  106. {
  107. return cam_context_prepare_dev_to_hw(ctx, cmd);
  108. }
  109. static int __cam_jpeg_ctx_handle_buf_done_in_acquired(void *ctx,
  110. uint32_t evt_id, void *done)
  111. {
  112. return cam_context_buf_done_from_hw(ctx, done, evt_id);
  113. }
  114. static int __cam_jpeg_ctx_stop_dev_in_acquired(struct cam_context *ctx,
  115. struct cam_start_stop_dev_cmd *cmd)
  116. {
  117. int rc;
  118. rc = cam_context_stop_dev_to_hw(ctx);
  119. if (rc) {
  120. CAM_ERR(CAM_JPEG, "Failed in Stop dev, rc=%d", rc);
  121. return rc;
  122. }
  123. return rc;
  124. }
  125. static int cam_jpeg_context_inject_error(void *context, void *err_param)
  126. {
  127. int rc = 0;
  128. struct cam_context *ctx = (struct cam_context *)context;
  129. uint64_t req_id;
  130. uint32_t err_code;
  131. uint32_t err_type;
  132. if (!err_param) {
  133. CAM_ERR(CAM_ISP, "err_param not valid");
  134. return -EINVAL;
  135. }
  136. req_id = ((struct cam_err_inject_param *)err_param)->req_id;
  137. err_code = ((struct cam_err_inject_param *)err_param)->err_code;
  138. err_type = ((struct cam_err_inject_param *)err_param)->err_type;
  139. switch (err_type) {
  140. case CAM_REQ_MGR_RETRY_EVENT:
  141. switch (err_code) {
  142. case CAM_REQ_MGR_JPEG_THUBNAIL_SIZE_ERROR:
  143. break;
  144. default:
  145. CAM_ERR(CAM_ISP, "err code not supported %d", err_code);
  146. return -EINVAL;
  147. }
  148. break;
  149. case CAM_SYNC_STATE_SIGNALED_ERROR:
  150. switch (err_code) {
  151. case CAM_SYNC_JPEG_EVENT_INVLD_CMD:
  152. case CAM_SYNC_JPEG_EVENT_SET_IRQ_CB:
  153. case CAM_SYNC_JPEG_EVENT_HW_RESET_FAILED:
  154. case CAM_SYNC_JPEG_EVENT_CDM_CHANGE_BASE_ERR:
  155. case CAM_SYNC_JPEG_EVENT_CDM_CONFIG_ERR:
  156. case CAM_SYNC_JPEG_EVENT_START_HW_ERR:
  157. break;
  158. default:
  159. CAM_ERR(CAM_ISP, "err code not supported %d", err_code);
  160. return -EINVAL;
  161. }
  162. break;
  163. default:
  164. CAM_ERR(CAM_ISP, "err type not supported %d", err_type);
  165. return -EINVAL;
  166. }
  167. rc = cam_context_err_to_hw(ctx, err_param);
  168. return rc;
  169. }
  170. /* top state machine */
  171. static struct cam_ctx_ops
  172. cam_jpeg_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  173. /* Uninit */
  174. {
  175. .ioctl_ops = { },
  176. .crm_ops = { },
  177. .irq_ops = NULL,
  178. },
  179. /* Available */
  180. {
  181. .ioctl_ops = {
  182. .acquire_dev = __cam_jpeg_ctx_acquire_dev_in_available,
  183. },
  184. .crm_ops = { },
  185. .irq_ops = NULL,
  186. .mini_dump_ops = cam_jpeg_context_mini_dump,
  187. },
  188. /* Acquired */
  189. {
  190. .ioctl_ops = {
  191. .release_dev = __cam_jpeg_ctx_release_dev_in_acquired,
  192. .config_dev = __cam_jpeg_ctx_config_dev_in_acquired,
  193. .stop_dev = __cam_jpeg_ctx_stop_dev_in_acquired,
  194. .flush_dev = __cam_jpeg_ctx_flush_dev_in_acquired,
  195. .dump_dev = __cam_jpeg_ctx_dump_dev_in_acquired,
  196. },
  197. .crm_ops = { },
  198. .irq_ops = __cam_jpeg_ctx_handle_buf_done_in_acquired,
  199. .pagefault_ops = cam_jpeg_context_dump_active_request,
  200. .mini_dump_ops = cam_jpeg_context_mini_dump,
  201. .err_inject_ops = cam_jpeg_context_inject_error,
  202. },
  203. /* Ready */
  204. {
  205. .ioctl_ops = {},
  206. },
  207. /* Flushed */
  208. {
  209. .ioctl_ops = {},
  210. },
  211. /* Activated */
  212. {
  213. .ioctl_ops = {},
  214. },
  215. };
  216. int cam_jpeg_context_init(struct cam_jpeg_context *ctx,
  217. struct cam_context *ctx_base,
  218. struct cam_hw_mgr_intf *hw_intf,
  219. uint32_t ctx_id,
  220. int img_iommu_hdl)
  221. {
  222. int rc;
  223. int i;
  224. if (!ctx || !ctx_base) {
  225. CAM_ERR(CAM_JPEG, "Invalid Context");
  226. rc = -EFAULT;
  227. goto err;
  228. }
  229. memset(ctx, 0, sizeof(*ctx));
  230. ctx->base = ctx_base;
  231. for (i = 0; i < CAM_CTX_REQ_MAX; i++)
  232. ctx->req_base[i].req_priv = &ctx->jpeg_req[i];
  233. rc = cam_context_init(ctx_base, jpeg_dev_name, CAM_JPEG, ctx_id,
  234. NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX, img_iommu_hdl);
  235. if (rc) {
  236. CAM_ERR(CAM_JPEG, "Camera Context Base init failed");
  237. goto err;
  238. }
  239. ctx_base->state_machine = cam_jpeg_ctx_state_machine;
  240. ctx_base->ctx_priv = ctx;
  241. ctx_base->max_hw_update_entries = CAM_CTX_CFG_MAX;
  242. ctx_base->max_in_map_entries = CAM_CTX_CFG_MAX;
  243. ctx_base->max_out_map_entries = CAM_CTX_CFG_MAX;
  244. err:
  245. return rc;
  246. }
  247. int cam_jpeg_context_deinit(struct cam_jpeg_context *ctx)
  248. {
  249. if (!ctx || !ctx->base) {
  250. CAM_ERR(CAM_JPEG, "Invalid params: %pK", ctx);
  251. return -EINVAL;
  252. }
  253. cam_context_deinit(ctx->base);
  254. memset(ctx, 0, sizeof(*ctx));
  255. return 0;
  256. }