cam_jpeg_dev.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_evt_inject_cb(void *inject_args)
  23. {
  24. struct cam_common_inject_evt_param *inject_params = inject_args;
  25. int i;
  26. for (i = 0; i < CAM_JPEG_CTX_MAX; i++) {
  27. if (g_jpeg_dev.ctx[i].dev_hdl == inject_params->dev_hdl) {
  28. cam_context_add_evt_inject(&g_jpeg_dev.ctx[i],
  29. &inject_params->evt_params);
  30. return 0;
  31. }
  32. }
  33. CAM_ERR(CAM_JPEG, "No dev hdl found %d", inject_params->dev_hdl);
  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. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_LOCK);
  82. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  83. g_jpeg_dev.open_cnt++;
  84. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  85. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_UNLOCK);
  86. return 0;
  87. }
  88. static int cam_jpeg_subdev_close_internal(struct v4l2_subdev *sd,
  89. struct v4l2_subdev_fh *fh)
  90. {
  91. int rc = 0;
  92. struct cam_node *node = v4l2_get_subdevdata(sd);
  93. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  94. if (g_jpeg_dev.open_cnt <= 0) {
  95. CAM_DBG(CAM_JPEG, "JPEG subdev is already closed");
  96. rc = -EINVAL;
  97. goto end;
  98. }
  99. g_jpeg_dev.open_cnt--;
  100. if (!node) {
  101. CAM_ERR(CAM_JPEG, "Node ptr is NULL");
  102. rc = -EINVAL;
  103. goto end;
  104. }
  105. if (g_jpeg_dev.open_cnt == 0)
  106. cam_node_shutdown(node);
  107. end:
  108. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  109. return rc;
  110. }
  111. static int cam_jpeg_subdev_close(struct v4l2_subdev *sd,
  112. struct v4l2_subdev_fh *fh)
  113. {
  114. bool crm_active = cam_req_mgr_is_open();
  115. if (crm_active) {
  116. CAM_DBG(CAM_JPEG, "CRM is ACTIVE, close should be from CRM");
  117. return 0;
  118. }
  119. return cam_jpeg_subdev_close_internal(sd, fh);
  120. }
  121. static const struct v4l2_subdev_internal_ops cam_jpeg_subdev_internal_ops = {
  122. .close = cam_jpeg_subdev_close,
  123. .open = cam_jpeg_subdev_open,
  124. };
  125. static int cam_jpeg_dev_component_bind(struct device *dev,
  126. struct device *master_dev, void *data)
  127. {
  128. int rc;
  129. int i;
  130. struct cam_hw_mgr_intf hw_mgr_intf;
  131. struct cam_node *node;
  132. int iommu_hdl = -1;
  133. struct platform_device *pdev = to_platform_device(dev);
  134. g_jpeg_dev.sd.internal_ops = &cam_jpeg_subdev_internal_ops;
  135. g_jpeg_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  136. rc = cam_subdev_probe(&g_jpeg_dev.sd, pdev, CAM_JPEG_DEV_NAME,
  137. CAM_JPEG_DEVICE_TYPE);
  138. if (rc) {
  139. CAM_ERR(CAM_JPEG, "JPEG cam_subdev_probe failed %d", rc);
  140. goto err;
  141. }
  142. node = (struct cam_node *)g_jpeg_dev.sd.token;
  143. rc = cam_jpeg_hw_mgr_init(pdev->dev.of_node,
  144. (uint64_t *)&hw_mgr_intf, &iommu_hdl,
  145. cam_jpeg_dev_mini_dump_cb);
  146. if (rc) {
  147. CAM_ERR(CAM_JPEG, "Can not initialize JPEG HWmanager %d", rc);
  148. goto unregister;
  149. }
  150. for (i = 0; i < CAM_JPEG_CTX_MAX; i++) {
  151. rc = cam_jpeg_context_init(&g_jpeg_dev.ctx_jpeg[i],
  152. &g_jpeg_dev.ctx[i],
  153. &node->hw_mgr_intf,
  154. i, iommu_hdl);
  155. if (rc) {
  156. CAM_ERR(CAM_JPEG, "JPEG context init failed %d %d",
  157. i, rc);
  158. goto ctx_init_fail;
  159. }
  160. }
  161. cam_common_register_evt_inject_cb(cam_jpeg_dev_evt_inject_cb,
  162. CAM_COMMON_EVT_INJECT_HW_JPEG);
  163. rc = cam_node_init(node, &hw_mgr_intf, g_jpeg_dev.ctx, CAM_JPEG_CTX_MAX,
  164. CAM_JPEG_DEV_NAME);
  165. if (rc) {
  166. CAM_ERR(CAM_JPEG, "JPEG node init failed %d", rc);
  167. goto ctx_init_fail;
  168. }
  169. node->sd_handler = cam_jpeg_subdev_close_internal;
  170. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  171. cam_jpeg_dev_iommu_fault_handler, node);
  172. mutex_init(&g_jpeg_dev.jpeg_mutex);
  173. CAM_DBG(CAM_JPEG, "Component bound successfully");
  174. return rc;
  175. ctx_init_fail:
  176. for (--i; i >= 0; i--)
  177. if (cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]))
  178. CAM_ERR(CAM_JPEG, "deinit fail %d %d", i, rc);
  179. unregister:
  180. if (cam_subdev_remove(&g_jpeg_dev.sd))
  181. CAM_ERR(CAM_JPEG, "remove fail %d", rc);
  182. err:
  183. return rc;
  184. }
  185. static void cam_jpeg_dev_component_unbind(struct device *dev,
  186. struct device *master_dev, void *data)
  187. {
  188. int rc;
  189. int i;
  190. for (i = 0; i < CAM_CTX_MAX; i++) {
  191. rc = cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]);
  192. if (rc)
  193. CAM_ERR(CAM_JPEG, "JPEG context %d deinit failed %d",
  194. i, rc);
  195. }
  196. rc = cam_subdev_remove(&g_jpeg_dev.sd);
  197. if (rc)
  198. CAM_ERR(CAM_JPEG, "Unregister failed %d", rc);
  199. }
  200. const static struct component_ops cam_jpeg_dev_component_ops = {
  201. .bind = cam_jpeg_dev_component_bind,
  202. .unbind = cam_jpeg_dev_component_unbind,
  203. };
  204. static int cam_jpeg_dev_remove(struct platform_device *pdev)
  205. {
  206. component_del(&pdev->dev, &cam_jpeg_dev_component_ops);
  207. return 0;
  208. }
  209. static int cam_jpeg_dev_probe(struct platform_device *pdev)
  210. {
  211. int rc = 0;
  212. CAM_DBG(CAM_JPEG, "Adding JPEG component");
  213. rc = component_add(&pdev->dev, &cam_jpeg_dev_component_ops);
  214. if (rc)
  215. CAM_ERR(CAM_JPEG, "failed to add component rc: %d", rc);
  216. return rc;
  217. }
  218. struct platform_driver jpeg_driver = {
  219. .probe = cam_jpeg_dev_probe,
  220. .remove = cam_jpeg_dev_remove,
  221. .driver = {
  222. .name = "cam_jpeg",
  223. .owner = THIS_MODULE,
  224. .of_match_table = cam_jpeg_dt_match,
  225. .suppress_bind_attrs = true,
  226. },
  227. };
  228. int cam_jpeg_dev_init_module(void)
  229. {
  230. return platform_driver_register(&jpeg_driver);
  231. }
  232. void cam_jpeg_dev_exit_module(void)
  233. {
  234. platform_driver_unregister(&jpeg_driver);
  235. }
  236. MODULE_DESCRIPTION("MSM JPEG driver");
  237. MODULE_LICENSE("GPL v2");