cam_jpeg_context.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #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. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  101. if (rc)
  102. CAM_ERR(CAM_ICP, "Failed to flush device");
  103. return rc;
  104. }
  105. static int __cam_jpeg_ctx_config_dev_in_acquired(struct cam_context *ctx,
  106. struct cam_config_dev_cmd *cmd)
  107. {
  108. return cam_context_prepare_dev_to_hw(ctx, cmd);
  109. }
  110. static int __cam_jpeg_ctx_handle_buf_done_in_acquired(void *ctx, void *done_evt_data)
  111. {
  112. struct cam_jpeg_hw_buf_done_evt_data *buf_done = done_evt_data;
  113. return cam_context_buf_done_from_hw(ctx, buf_done->buf_done_data,
  114. buf_done->evt_id);
  115. }
  116. static int __cam_jpeg_ctx_handle_evt_inducement(void *ctx, void *inject_evt_arg)
  117. {
  118. return cam_context_apply_evt_injection(ctx, inject_evt_arg);
  119. }
  120. static int __cam_jpeg_ctx_handle_hw_event(void *ctx,
  121. uint32_t evt_id, void *evt_data)
  122. {
  123. int rc;
  124. if (!ctx || !evt_data) {
  125. CAM_ERR(CAM_JPEG, "Invalid parameters ctx %s evt_data: %s",
  126. CAM_IS_NULL_TO_STR(ctx), CAM_IS_NULL_TO_STR(evt_data));
  127. return -EINVAL;
  128. }
  129. switch (evt_id) {
  130. case CAM_JPEG_EVT_ID_BUF_DONE:
  131. rc = __cam_jpeg_ctx_handle_buf_done_in_acquired(ctx, evt_data);
  132. break;
  133. case CAM_JPEG_EVT_ID_INDUCE_ERR:
  134. rc = __cam_jpeg_ctx_handle_evt_inducement(ctx, evt_data);
  135. break;
  136. default:
  137. CAM_ERR(CAM_JPEG, "Invalid event id: %u", evt_id);
  138. rc = -EINVAL;
  139. }
  140. return rc;
  141. }
  142. static int __cam_jpeg_ctx_stop_dev_in_acquired(struct cam_context *ctx,
  143. struct cam_start_stop_dev_cmd *cmd)
  144. {
  145. int rc;
  146. rc = cam_context_stop_dev_to_hw(ctx);
  147. if (rc) {
  148. CAM_ERR(CAM_JPEG, "Failed in Stop dev, rc=%d", rc);
  149. return rc;
  150. }
  151. return rc;
  152. }
  153. static int cam_jpeg_context_validate_event_notify_injection(struct cam_context *ctx,
  154. struct cam_hw_inject_evt_param *evt_params)
  155. {
  156. int rc = 0;
  157. uint32_t evt_type;
  158. uint64_t req_id;
  159. req_id = evt_params->req_id;
  160. evt_type = evt_params->u.evt_notify.evt_notify_type;
  161. switch (evt_type) {
  162. case V4L_EVENT_CAM_REQ_MGR_NODE_EVENT: {
  163. struct cam_hw_inject_node_evt_param *node_evt_params =
  164. &evt_params->u.evt_notify.u.node_evt_params;
  165. switch (node_evt_params->event_type) {
  166. case CAM_REQ_MGR_RETRY_EVENT:
  167. break;
  168. default:
  169. CAM_ERR(CAM_JPEG,
  170. "Invalid event type %u for node event injection event cause: %u req id: %llu ctx id: %u dev hdl: %d",
  171. node_evt_params->event_type, node_evt_params->event_cause,
  172. req_id, ctx->ctx_id, ctx->dev_hdl);
  173. return -EINVAL;
  174. }
  175. CAM_INFO(CAM_JPEG,
  176. "Inject Node evt: event type: %u event cause: %u req id: %llu ctx id: %u dev hdl: %d",
  177. node_evt_params->event_type, node_evt_params->event_cause,
  178. req_id, ctx->ctx_id, ctx->dev_hdl);
  179. break;
  180. }
  181. case V4L_EVENT_CAM_REQ_MGR_PF_ERROR: {
  182. struct cam_hw_inject_pf_evt_param *pf_evt_params =
  183. &evt_params->u.evt_notify.u.pf_evt_params;
  184. bool non_fatal_en;
  185. rc = cam_smmu_is_cb_non_fatal_fault_en(ctx->img_iommu_hdl, &non_fatal_en);
  186. if (rc) {
  187. CAM_ERR(CAM_JPEG,
  188. "Fail to query whether device's cb has non-fatal enabled rc: %d",
  189. rc);
  190. return rc;
  191. }
  192. if (!non_fatal_en) {
  193. CAM_ERR(CAM_JPEG,
  194. "Fail to inject page fault event notification. Page fault is fatal for JPEG");
  195. return -EINVAL;
  196. }
  197. CAM_INFO(CAM_JPEG,
  198. "Inject PF evt: req_id: %llu ctx id: %u dev hdl: %d ctx found: %hhu",
  199. req_id, ctx->ctx_id, ctx->dev_hdl, pf_evt_params->ctx_found);
  200. break;
  201. }
  202. default:
  203. CAM_ERR(CAM_JPEG, "Event notification type not supported: %u", evt_type);
  204. rc = -EINVAL;
  205. }
  206. return rc;
  207. }
  208. static int cam_jpeg_context_inject_evt(void *context, void *evt_args)
  209. {
  210. struct cam_context *ctx = context;
  211. struct cam_hw_inject_evt_param *evt_params = NULL;
  212. struct cam_hw_inject_buffer_error_param *buf_err_params = NULL;
  213. int rc = 0;
  214. if (!ctx || !evt_args) {
  215. CAM_ERR(CAM_JPEG,
  216. "invalid params ctx %s event args %s",
  217. CAM_IS_NULL_TO_STR(ctx), CAM_IS_NULL_TO_STR(evt_args));
  218. return -EINVAL;
  219. }
  220. evt_params = (struct cam_hw_inject_evt_param *)evt_args;
  221. if (evt_params->inject_id == CAM_COMMON_EVT_INJECT_BUFFER_ERROR_TYPE) {
  222. buf_err_params = &evt_params->u.buf_err_evt;
  223. if (buf_err_params->sync_error > CAM_SYNC_JPEG_EVENT_START ||
  224. buf_err_params->sync_error < CAM_SYNC_JPEG_EVENT_END) {
  225. CAM_INFO(CAM_JPEG, "Inject buffer sync error %u ctx id: %u req id %llu",
  226. buf_err_params->sync_error, ctx->ctx_id, evt_params->req_id);
  227. } else {
  228. CAM_ERR(CAM_JPEG, "Invalid buffer sync error %u",
  229. buf_err_params->sync_error);
  230. return -EINVAL;
  231. }
  232. } else {
  233. rc = cam_jpeg_context_validate_event_notify_injection(ctx, evt_params);
  234. if (rc) {
  235. CAM_ERR(CAM_JPEG,
  236. "Event notification injection failed validation rc: %d", rc);
  237. return rc;
  238. }
  239. }
  240. if (ctx->hw_mgr_intf->hw_inject_evt)
  241. ctx->hw_mgr_intf->hw_inject_evt(ctx->ctxt_to_hw_map, evt_args);
  242. return rc;
  243. }
  244. /* top state machine */
  245. static struct cam_ctx_ops
  246. cam_jpeg_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  247. /* Uninit */
  248. {
  249. .ioctl_ops = { },
  250. .crm_ops = { },
  251. .irq_ops = NULL,
  252. },
  253. /* Available */
  254. {
  255. .ioctl_ops = {
  256. .acquire_dev = __cam_jpeg_ctx_acquire_dev_in_available,
  257. },
  258. .crm_ops = { },
  259. .irq_ops = NULL,
  260. .mini_dump_ops = cam_jpeg_context_mini_dump,
  261. },
  262. /* Acquired */
  263. {
  264. .ioctl_ops = {
  265. .release_dev = __cam_jpeg_ctx_release_dev_in_acquired,
  266. .config_dev = __cam_jpeg_ctx_config_dev_in_acquired,
  267. .stop_dev = __cam_jpeg_ctx_stop_dev_in_acquired,
  268. .flush_dev = __cam_jpeg_ctx_flush_dev_in_acquired,
  269. .dump_dev = __cam_jpeg_ctx_dump_dev_in_acquired,
  270. },
  271. .crm_ops = { },
  272. .irq_ops = __cam_jpeg_ctx_handle_hw_event,
  273. .pagefault_ops = cam_jpeg_context_dump_active_request,
  274. .mini_dump_ops = cam_jpeg_context_mini_dump,
  275. .evt_inject_ops = cam_jpeg_context_inject_evt,
  276. },
  277. /* Ready */
  278. {
  279. .ioctl_ops = {},
  280. },
  281. /* Flushed */
  282. {
  283. .ioctl_ops = {},
  284. },
  285. /* Activated */
  286. {
  287. .ioctl_ops = {},
  288. },
  289. };
  290. int cam_jpeg_context_init(struct cam_jpeg_context *ctx,
  291. struct cam_context *ctx_base,
  292. struct cam_hw_mgr_intf *hw_intf,
  293. uint32_t ctx_id,
  294. int img_iommu_hdl)
  295. {
  296. int rc;
  297. int i;
  298. if (!ctx || !ctx_base) {
  299. CAM_ERR(CAM_JPEG, "Invalid Context");
  300. rc = -EFAULT;
  301. goto err;
  302. }
  303. memset(ctx, 0, sizeof(*ctx));
  304. ctx->base = ctx_base;
  305. for (i = 0; i < CAM_CTX_REQ_MAX; i++)
  306. ctx->req_base[i].req_priv = &ctx->jpeg_req[i];
  307. rc = cam_context_init(ctx_base, jpeg_dev_name, CAM_JPEG, ctx_id,
  308. NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX, img_iommu_hdl);
  309. if (rc) {
  310. CAM_ERR(CAM_JPEG, "Camera Context Base init failed");
  311. goto err;
  312. }
  313. ctx_base->state_machine = cam_jpeg_ctx_state_machine;
  314. ctx_base->ctx_priv = ctx;
  315. ctx_base->max_hw_update_entries = CAM_CTX_CFG_MAX;
  316. ctx_base->max_in_map_entries = CAM_CTX_CFG_MAX;
  317. ctx_base->max_out_map_entries = CAM_CTX_CFG_MAX;
  318. err:
  319. return rc;
  320. }
  321. int cam_jpeg_context_deinit(struct cam_jpeg_context *ctx)
  322. {
  323. if (!ctx || !ctx->base) {
  324. CAM_ERR(CAM_JPEG, "Invalid params: %pK", ctx);
  325. return -EINVAL;
  326. }
  327. cam_context_deinit(ctx->base);
  328. memset(ctx, 0, sizeof(*ctx));
  329. return 0;
  330. }