cam_jpeg_dev.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/delay.h>
  7. #include <linux/io.h>
  8. #include <linux/of.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include "cam_node.h"
  12. #include "cam_hw_mgr_intf.h"
  13. #include "cam_jpeg_hw_mgr_intf.h"
  14. #include "cam_jpeg_dev.h"
  15. #include "cam_debug_util.h"
  16. #include "cam_smmu_api.h"
  17. #include "camera_main.h"
  18. #include "cam_common_util.h"
  19. #include "cam_context_utils.h"
  20. #define CAM_JPEG_DEV_NAME "cam-jpeg"
  21. static struct cam_jpeg_dev g_jpeg_dev;
  22. static int cam_jpeg_dev_err_inject_cb(void *err_param)
  23. {
  24. int i = 0;
  25. for (i = 0; i < CAM_JPEG_CTX_MAX; i++) {
  26. if (g_jpeg_dev.ctx[i].dev_hdl ==
  27. ((struct cam_err_inject_param *)err_param)->dev_hdl) {
  28. CAM_INFO(CAM_ISP, "dev_hdl found:%d", g_jpeg_dev.ctx[i].dev_hdl);
  29. cam_context_add_err_inject(&g_jpeg_dev.ctx[i], err_param);
  30. return 0;
  31. }
  32. }
  33. CAM_ERR(CAM_ISP, "no dev hdl found jpeg");
  34. return -EINVAL;
  35. }
  36. static void cam_jpeg_dev_iommu_fault_handler(
  37. struct cam_smmu_pf_info *pf_smmu_info)
  38. {
  39. int i, rc;
  40. struct cam_node *node = NULL;
  41. struct cam_hw_dump_pf_args pf_args = {0};
  42. if (!pf_smmu_info || !pf_smmu_info->token) {
  43. CAM_ERR(CAM_JPEG, "invalid token in page handler cb");
  44. return;
  45. }
  46. node = (struct cam_node *)pf_smmu_info->token;
  47. pf_args.pf_smmu_info = pf_smmu_info;
  48. for (i = 0; i < node->ctx_size; i++) {
  49. cam_context_dump_pf_info(&(node->ctx_list[i]), &pf_args);
  50. if (pf_args.pf_context_info.ctx_found)
  51. /* found ctx and packet of the faulted address */
  52. break;
  53. }
  54. if (i == node->ctx_size) {
  55. /* Faulted ctx not found. But report PF to UMD anyway*/
  56. rc = cam_context_send_pf_evt(NULL, &pf_args);
  57. if (rc)
  58. CAM_ERR(CAM_JPEG,
  59. "Failed to notify PF event to userspace rc: %d", rc);
  60. }
  61. }
  62. static void cam_jpeg_dev_mini_dump_cb(void *priv, void *args)
  63. {
  64. struct cam_context *ctx = NULL;
  65. if (!priv || !args) {
  66. CAM_ERR(CAM_JPEG, "Invalid param priv %pK %pK args", priv, args);
  67. return;
  68. }
  69. ctx = (struct cam_context *)priv;
  70. cam_context_mini_dump_from_hw(ctx, args);
  71. }
  72. static const struct of_device_id cam_jpeg_dt_match[] = {
  73. {
  74. .compatible = "qcom,cam-jpeg"
  75. },
  76. { }
  77. };
  78. static int cam_jpeg_subdev_open(struct v4l2_subdev *sd,
  79. struct v4l2_subdev_fh *fh)
  80. {
  81. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  82. g_jpeg_dev.open_cnt++;
  83. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  84. return 0;
  85. }
  86. static int cam_jpeg_subdev_close_internal(struct v4l2_subdev *sd,
  87. struct v4l2_subdev_fh *fh)
  88. {
  89. int rc = 0;
  90. struct cam_node *node = v4l2_get_subdevdata(sd);
  91. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  92. if (g_jpeg_dev.open_cnt <= 0) {
  93. CAM_DBG(CAM_JPEG, "JPEG subdev is already closed");
  94. rc = -EINVAL;
  95. goto end;
  96. }
  97. g_jpeg_dev.open_cnt--;
  98. if (!node) {
  99. CAM_ERR(CAM_JPEG, "Node ptr is NULL");
  100. rc = -EINVAL;
  101. goto end;
  102. }
  103. if (g_jpeg_dev.open_cnt == 0)
  104. cam_node_shutdown(node);
  105. end:
  106. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  107. return rc;
  108. }
  109. static int cam_jpeg_subdev_close(struct v4l2_subdev *sd,
  110. struct v4l2_subdev_fh *fh)
  111. {
  112. bool crm_active = cam_req_mgr_is_open();
  113. if (crm_active) {
  114. CAM_DBG(CAM_JPEG, "CRM is ACTIVE, close should be from CRM");
  115. return 0;
  116. }
  117. return cam_jpeg_subdev_close_internal(sd, fh);
  118. }
  119. static const struct v4l2_subdev_internal_ops cam_jpeg_subdev_internal_ops = {
  120. .close = cam_jpeg_subdev_close,
  121. .open = cam_jpeg_subdev_open,
  122. };
  123. static int cam_jpeg_dev_component_bind(struct device *dev,
  124. struct device *master_dev, void *data)
  125. {
  126. int rc;
  127. int i;
  128. struct cam_hw_mgr_intf hw_mgr_intf;
  129. struct cam_node *node;
  130. int iommu_hdl = -1;
  131. struct platform_device *pdev = to_platform_device(dev);
  132. g_jpeg_dev.sd.internal_ops = &cam_jpeg_subdev_internal_ops;
  133. g_jpeg_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  134. rc = cam_subdev_probe(&g_jpeg_dev.sd, pdev, CAM_JPEG_DEV_NAME,
  135. CAM_JPEG_DEVICE_TYPE);
  136. if (rc) {
  137. CAM_ERR(CAM_JPEG, "JPEG cam_subdev_probe failed %d", rc);
  138. goto err;
  139. }
  140. node = (struct cam_node *)g_jpeg_dev.sd.token;
  141. rc = cam_jpeg_hw_mgr_init(pdev->dev.of_node,
  142. (uint64_t *)&hw_mgr_intf, &iommu_hdl,
  143. cam_jpeg_dev_mini_dump_cb);
  144. if (rc) {
  145. CAM_ERR(CAM_JPEG, "Can not initialize JPEG HWmanager %d", rc);
  146. goto unregister;
  147. }
  148. for (i = 0; i < CAM_JPEG_CTX_MAX; i++) {
  149. rc = cam_jpeg_context_init(&g_jpeg_dev.ctx_jpeg[i],
  150. &g_jpeg_dev.ctx[i],
  151. &node->hw_mgr_intf,
  152. i, iommu_hdl);
  153. if (rc) {
  154. CAM_ERR(CAM_JPEG, "JPEG context init failed %d %d",
  155. i, rc);
  156. goto ctx_init_fail;
  157. }
  158. }
  159. cam_common_register_err_inject_cb(cam_jpeg_dev_err_inject_cb,
  160. CAM_COMMON_ERR_INJECT_HW_JPEG);
  161. rc = cam_node_init(node, &hw_mgr_intf, g_jpeg_dev.ctx, CAM_JPEG_CTX_MAX,
  162. CAM_JPEG_DEV_NAME);
  163. if (rc) {
  164. CAM_ERR(CAM_JPEG, "JPEG node init failed %d", rc);
  165. goto ctx_init_fail;
  166. }
  167. node->sd_handler = cam_jpeg_subdev_close_internal;
  168. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  169. cam_jpeg_dev_iommu_fault_handler, node);
  170. mutex_init(&g_jpeg_dev.jpeg_mutex);
  171. CAM_DBG(CAM_JPEG, "Component bound successfully");
  172. return rc;
  173. ctx_init_fail:
  174. for (--i; i >= 0; i--)
  175. if (cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]))
  176. CAM_ERR(CAM_JPEG, "deinit fail %d %d", i, rc);
  177. unregister:
  178. if (cam_subdev_remove(&g_jpeg_dev.sd))
  179. CAM_ERR(CAM_JPEG, "remove fail %d", rc);
  180. err:
  181. return rc;
  182. }
  183. static void cam_jpeg_dev_component_unbind(struct device *dev,
  184. struct device *master_dev, void *data)
  185. {
  186. int rc;
  187. int i;
  188. for (i = 0; i < CAM_CTX_MAX; i++) {
  189. rc = cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]);
  190. if (rc)
  191. CAM_ERR(CAM_JPEG, "JPEG context %d deinit failed %d",
  192. i, rc);
  193. }
  194. rc = cam_subdev_remove(&g_jpeg_dev.sd);
  195. if (rc)
  196. CAM_ERR(CAM_JPEG, "Unregister failed %d", rc);
  197. }
  198. const static struct component_ops cam_jpeg_dev_component_ops = {
  199. .bind = cam_jpeg_dev_component_bind,
  200. .unbind = cam_jpeg_dev_component_unbind,
  201. };
  202. static int cam_jpeg_dev_remove(struct platform_device *pdev)
  203. {
  204. component_del(&pdev->dev, &cam_jpeg_dev_component_ops);
  205. return 0;
  206. }
  207. static int cam_jpeg_dev_probe(struct platform_device *pdev)
  208. {
  209. int rc = 0;
  210. CAM_DBG(CAM_JPEG, "Adding JPEG component");
  211. rc = component_add(&pdev->dev, &cam_jpeg_dev_component_ops);
  212. if (rc)
  213. CAM_ERR(CAM_JPEG, "failed to add component rc: %d", rc);
  214. return rc;
  215. }
  216. struct platform_driver jpeg_driver = {
  217. .probe = cam_jpeg_dev_probe,
  218. .remove = cam_jpeg_dev_remove,
  219. .driver = {
  220. .name = "cam_jpeg",
  221. .owner = THIS_MODULE,
  222. .of_match_table = cam_jpeg_dt_match,
  223. .suppress_bind_attrs = true,
  224. },
  225. };
  226. int cam_jpeg_dev_init_module(void)
  227. {
  228. return platform_driver_register(&jpeg_driver);
  229. }
  230. void cam_jpeg_dev_exit_module(void)
  231. {
  232. platform_driver_unregister(&jpeg_driver);
  233. }
  234. MODULE_DESCRIPTION("MSM JPEG driver");
  235. MODULE_LICENSE("GPL v2");