cam_jpeg_context.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, 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. #include "cam_jpeg_hw_intf.h"
  17. static const char jpeg_dev_name[] = "cam-jpeg";
  18. static int cam_jpeg_context_dump_active_request(void *data, void *args)
  19. {
  20. struct cam_context *ctx = (struct cam_context *)data;
  21. struct cam_ctx_request *req = NULL;
  22. struct cam_ctx_request *req_temp = NULL;
  23. struct cam_hw_dump_pf_args *pf_args = (struct cam_hw_dump_pf_args *)args;
  24. int rc = 0;
  25. if (!ctx || !pf_args) {
  26. CAM_ERR(CAM_JPEG, "Invalid ctx %pK or pf args %pK",
  27. ctx, pf_args);
  28. return -EINVAL;
  29. }
  30. CAM_INFO(CAM_JPEG, "iommu fault for jpeg ctx %d state %d",
  31. ctx->ctx_id, ctx->state);
  32. list_for_each_entry_safe(req, req_temp,
  33. &ctx->active_req_list, list) {
  34. CAM_INFO(CAM_JPEG, "Active req_id: %lld, ctx_id: %u",
  35. req->request_id, ctx->ctx_id);
  36. rc = cam_context_dump_pf_info_to_hw(ctx, pf_args, &req->pf_data);
  37. if (rc)
  38. CAM_ERR(CAM_JPEG, "Failed to dump pf info ctx_id: %u state: %d",
  39. ctx->ctx_id, ctx->state);
  40. }
  41. if (pf_args->pf_context_info.ctx_found) {
  42. /* Send PF notification to UMD if PF found on current CTX */
  43. rc = cam_context_send_pf_evt(ctx, pf_args);
  44. if (rc)
  45. CAM_ERR(CAM_JPEG,
  46. "Failed to notify PF event to userspace rc: %d", rc);
  47. }
  48. return rc;
  49. }
  50. static int cam_jpeg_context_mini_dump(void *priv, void *args)
  51. {
  52. int rc;
  53. struct cam_context *ctx;
  54. if (!priv || args) {
  55. CAM_ERR(CAM_ICP, "Invalid param priv %pK args %pK", priv, args);
  56. return -EINVAL;
  57. }
  58. ctx = (struct cam_context *)priv;
  59. rc = cam_context_mini_dump(ctx, args);
  60. if (rc)
  61. CAM_ERR(CAM_JPEG, "Mini Dump failed %d", rc);
  62. return rc;
  63. }
  64. static int __cam_jpeg_ctx_acquire_dev_in_available(struct cam_context *ctx,
  65. struct cam_acquire_dev_cmd *cmd)
  66. {
  67. int rc;
  68. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  69. if (rc)
  70. CAM_ERR(CAM_JPEG, "Unable to Acquire device %d", rc);
  71. else
  72. ctx->state = CAM_CTX_ACQUIRED;
  73. return rc;
  74. }
  75. static int __cam_jpeg_ctx_release_dev_in_acquired(struct cam_context *ctx,
  76. struct cam_release_dev_cmd *cmd)
  77. {
  78. int rc;
  79. cam_common_release_evt_params(ctx->dev_hdl);
  80. rc = cam_context_release_dev_to_hw(ctx, cmd);
  81. if (rc)
  82. CAM_ERR(CAM_JPEG, "Unable to release device %d", rc);
  83. ctx->state = CAM_CTX_AVAILABLE;
  84. return rc;
  85. }
  86. static int __cam_jpeg_ctx_dump_dev_in_acquired(
  87. struct cam_context *ctx,
  88. struct cam_dump_req_cmd *cmd)
  89. {
  90. int rc;
  91. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  92. if (rc)
  93. CAM_ERR(CAM_JPEG, "Failed to dump device, rc=%d", rc);
  94. return rc;
  95. }
  96. static int __cam_jpeg_ctx_flush_dev_in_acquired(struct cam_context *ctx,
  97. struct cam_flush_dev_cmd *cmd)
  98. {
  99. int rc;
  100. struct cam_context_utils_flush_args flush_args;
  101. flush_args.cmd = cmd;
  102. flush_args.flush_active_req = true;
  103. rc = cam_context_flush_dev_to_hw(ctx, &flush_args);
  104. if (rc)
  105. CAM_ERR(CAM_ICP, "Failed to flush device");
  106. return rc;
  107. }
  108. static int __cam_jpeg_ctx_config_dev_in_acquired(struct cam_context *ctx,
  109. struct cam_config_dev_cmd *cmd)
  110. {
  111. return cam_context_prepare_dev_to_hw(ctx, cmd);
  112. }
  113. static int __cam_jpeg_ctx_handle_buf_done_in_acquired(void *ctx, void *done_evt_data)
  114. {
  115. struct cam_jpeg_hw_buf_done_evt_data *buf_done = done_evt_data;
  116. return cam_context_buf_done_from_hw(ctx, buf_done->buf_done_data,
  117. buf_done->evt_id);
  118. }
  119. static int __cam_jpeg_ctx_handle_evt_inducement(void *ctx, void *inject_evt_arg)
  120. {
  121. return cam_context_apply_evt_injection(ctx, inject_evt_arg);
  122. }
  123. static int __cam_jpeg_ctx_handle_hw_event(void *ctx,
  124. uint32_t evt_id, void *evt_data)
  125. {
  126. int rc;
  127. if (!ctx || !evt_data) {
  128. CAM_ERR(CAM_JPEG, "Invalid parameters ctx %s evt_data: %s",
  129. CAM_IS_NULL_TO_STR(ctx), CAM_IS_NULL_TO_STR(evt_data));
  130. return -EINVAL;
  131. }
  132. switch (evt_id) {
  133. case CAM_JPEG_EVT_ID_BUF_DONE:
  134. rc = __cam_jpeg_ctx_handle_buf_done_in_acquired(ctx, evt_data);
  135. break;
  136. case CAM_JPEG_EVT_ID_INDUCE_ERR:
  137. rc = __cam_jpeg_ctx_handle_evt_inducement(ctx, evt_data);
  138. break;
  139. default:
  140. CAM_ERR(CAM_JPEG, "Invalid event id: %u", evt_id);
  141. rc = -EINVAL;
  142. }
  143. return rc;
  144. }
  145. static int __cam_jpeg_ctx_stop_dev_in_acquired(struct cam_context *ctx,
  146. struct cam_start_stop_dev_cmd *cmd)
  147. {
  148. int rc;
  149. rc = cam_context_stop_dev_to_hw(ctx);
  150. if (rc) {
  151. CAM_ERR(CAM_JPEG, "Failed in Stop dev, rc=%d", rc);
  152. return rc;
  153. }
  154. return rc;
  155. }
  156. static int cam_jpeg_context_validate_event_notify_injection(struct cam_context *ctx,
  157. struct cam_hw_inject_evt_param *evt_params)
  158. {
  159. int rc = 0;
  160. uint32_t evt_type;
  161. uint64_t req_id;
  162. req_id = evt_params->req_id;
  163. evt_type = evt_params->u.evt_notify.evt_notify_type;
  164. switch (evt_type) {
  165. case V4L_EVENT_CAM_REQ_MGR_NODE_EVENT: {
  166. struct cam_hw_inject_node_evt_param *node_evt_params =
  167. &evt_params->u.evt_notify.u.node_evt_params;
  168. switch (node_evt_params->event_type) {
  169. case CAM_REQ_MGR_RETRY_EVENT:
  170. break;
  171. default:
  172. CAM_ERR(CAM_JPEG,
  173. "Invalid event type %u for node event injection event cause: %u req id: %llu ctx id: %u dev hdl: %d",
  174. node_evt_params->event_type, node_evt_params->event_cause,
  175. req_id, ctx->ctx_id, ctx->dev_hdl);
  176. return -EINVAL;
  177. }
  178. CAM_INFO(CAM_JPEG,
  179. "Inject Node evt: event type: %u event cause: %u req id: %llu ctx id: %u dev hdl: %d",
  180. node_evt_params->event_type, node_evt_params->event_cause,
  181. req_id, ctx->ctx_id, ctx->dev_hdl);
  182. break;
  183. }
  184. case V4L_EVENT_CAM_REQ_MGR_PF_ERROR: {
  185. struct cam_hw_inject_pf_evt_param *pf_evt_params =
  186. &evt_params->u.evt_notify.u.pf_evt_params;
  187. bool non_fatal_en;
  188. rc = cam_smmu_is_cb_non_fatal_fault_en(ctx->img_iommu_hdl, &non_fatal_en);
  189. if (rc) {
  190. CAM_ERR(CAM_JPEG,
  191. "Fail to query whether device's cb has non-fatal enabled rc: %d",
  192. rc);
  193. return rc;
  194. }
  195. if (!non_fatal_en) {
  196. CAM_ERR(CAM_JPEG,
  197. "Fail to inject page fault event notification. Page fault is fatal for JPEG");
  198. return -EINVAL;
  199. }
  200. CAM_INFO(CAM_JPEG,
  201. "Inject PF evt: req_id: %llu ctx id: %u dev hdl: %d ctx found: %hhu",
  202. req_id, ctx->ctx_id, ctx->dev_hdl, pf_evt_params->ctx_found);
  203. break;
  204. }
  205. default:
  206. CAM_ERR(CAM_JPEG, "Event notification type not supported: %u", evt_type);
  207. rc = -EINVAL;
  208. }
  209. return rc;
  210. }
  211. static int cam_jpeg_context_inject_evt(void *context, void *evt_args)
  212. {
  213. struct cam_context *ctx = context;
  214. struct cam_hw_inject_evt_param *evt_params = NULL;
  215. struct cam_hw_inject_buffer_error_param *buf_err_params = NULL;
  216. int rc = 0;
  217. if (!ctx || !evt_args) {
  218. CAM_ERR(CAM_JPEG,
  219. "invalid params ctx %s event args %s",
  220. CAM_IS_NULL_TO_STR(ctx), CAM_IS_NULL_TO_STR(evt_args));
  221. return -EINVAL;
  222. }
  223. evt_params = (struct cam_hw_inject_evt_param *)evt_args;
  224. if (evt_params->inject_id == CAM_COMMON_EVT_INJECT_BUFFER_ERROR_TYPE) {
  225. buf_err_params = &evt_params->u.buf_err_evt;
  226. if (buf_err_params->sync_error > CAM_SYNC_JPEG_EVENT_START ||
  227. buf_err_params->sync_error < CAM_SYNC_JPEG_EVENT_END) {
  228. CAM_INFO(CAM_JPEG, "Inject buffer sync error %u ctx id: %u req id %llu",
  229. buf_err_params->sync_error, ctx->ctx_id, evt_params->req_id);
  230. } else {
  231. CAM_ERR(CAM_JPEG, "Invalid buffer sync error %u",
  232. buf_err_params->sync_error);
  233. return -EINVAL;
  234. }
  235. } else {
  236. rc = cam_jpeg_context_validate_event_notify_injection(ctx, evt_params);
  237. if (rc) {
  238. CAM_ERR(CAM_JPEG,
  239. "Event notification injection failed validation rc: %d", rc);
  240. return rc;
  241. }
  242. }
  243. if (ctx->hw_mgr_intf->hw_inject_evt)
  244. ctx->hw_mgr_intf->hw_inject_evt(ctx->ctxt_to_hw_map, evt_args);
  245. return rc;
  246. }
  247. /* top state machine */
  248. static struct cam_ctx_ops
  249. cam_jpeg_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  250. /* Uninit */
  251. {
  252. .ioctl_ops = { },
  253. .crm_ops = { },
  254. .irq_ops = NULL,
  255. },
  256. /* Available */
  257. {
  258. .ioctl_ops = {
  259. .acquire_dev = __cam_jpeg_ctx_acquire_dev_in_available,
  260. },
  261. .crm_ops = { },
  262. .irq_ops = NULL,
  263. .mini_dump_ops = cam_jpeg_context_mini_dump,
  264. },
  265. /* Acquired */
  266. {
  267. .ioctl_ops = {
  268. .release_dev = __cam_jpeg_ctx_release_dev_in_acquired,
  269. .config_dev = __cam_jpeg_ctx_config_dev_in_acquired,
  270. .stop_dev = __cam_jpeg_ctx_stop_dev_in_acquired,
  271. .flush_dev = __cam_jpeg_ctx_flush_dev_in_acquired,
  272. .dump_dev = __cam_jpeg_ctx_dump_dev_in_acquired,
  273. },
  274. .crm_ops = { },
  275. .irq_ops = __cam_jpeg_ctx_handle_hw_event,
  276. .pagefault_ops = cam_jpeg_context_dump_active_request,
  277. .mini_dump_ops = cam_jpeg_context_mini_dump,
  278. .evt_inject_ops = cam_jpeg_context_inject_evt,
  279. },
  280. /* Ready */
  281. {
  282. .ioctl_ops = {},
  283. },
  284. /* Flushed */
  285. {
  286. .ioctl_ops = {},
  287. },
  288. /* Activated */
  289. {
  290. .ioctl_ops = {},
  291. },
  292. };
  293. int cam_jpeg_context_init(struct cam_jpeg_context *ctx,
  294. struct cam_context *ctx_base,
  295. struct cam_hw_mgr_intf *hw_intf,
  296. uint32_t ctx_id,
  297. int img_iommu_hdl)
  298. {
  299. int rc;
  300. int i;
  301. if (!ctx || !ctx_base) {
  302. CAM_ERR(CAM_JPEG, "Invalid Context");
  303. rc = -EFAULT;
  304. goto err;
  305. }
  306. memset(ctx, 0, sizeof(*ctx));
  307. ctx->base = ctx_base;
  308. for (i = 0; i < CAM_CTX_REQ_MAX; i++)
  309. ctx->req_base[i].req_priv = &ctx->jpeg_req[i];
  310. rc = cam_context_init(ctx_base, jpeg_dev_name, CAM_JPEG, ctx_id,
  311. NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX, img_iommu_hdl);
  312. if (rc) {
  313. CAM_ERR(CAM_JPEG, "Camera Context Base init failed");
  314. goto err;
  315. }
  316. ctx_base->state_machine = cam_jpeg_ctx_state_machine;
  317. ctx_base->ctx_priv = ctx;
  318. ctx_base->max_hw_update_entries = CAM_CTX_CFG_MAX;
  319. ctx_base->max_in_map_entries = CAM_CTX_CFG_MAX;
  320. ctx_base->max_out_map_entries = CAM_CTX_CFG_MAX;
  321. err:
  322. return rc;
  323. }
  324. int cam_jpeg_context_deinit(struct cam_jpeg_context *ctx)
  325. {
  326. if (!ctx || !ctx->base) {
  327. CAM_ERR(CAM_JPEG, "Invalid params: %pK", ctx);
  328. return -EINVAL;
  329. }
  330. cam_context_deinit(ctx->base);
  331. memset(ctx, 0, sizeof(*ctx));
  332. return 0;
  333. }