cam_isp_dev.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/iommu.h>
  11. #include <linux/timer.h>
  12. #include <linux/kernel.h>
  13. #include <media/cam_req_mgr.h>
  14. #include "cam_isp_dev.h"
  15. #include "cam_hw_mgr_intf.h"
  16. #include "cam_isp_hw_mgr_intf.h"
  17. #include "cam_node.h"
  18. #include "cam_debug_util.h"
  19. #include "cam_smmu_api.h"
  20. #include "camera_main.h"
  21. #include "cam_common_util.h"
  22. #include "cam_context_utils.h"
  23. static struct cam_isp_dev g_isp_dev;
  24. static int cam_isp_dev_err_inject_cb(void *err_param)
  25. {
  26. int i = 0;
  27. for (i = 0; i < g_isp_dev.max_context; i++) {
  28. if ((g_isp_dev.ctx[i].dev_hdl) ==
  29. ((struct cam_err_inject_param *)err_param)->dev_hdl) {
  30. cam_context_add_err_inject(&g_isp_dev.ctx[i], err_param);
  31. return 0;
  32. }
  33. }
  34. CAM_ERR(CAM_ISP, "no dev hdl found for IFE");
  35. return -ENODEV;
  36. }
  37. static void cam_isp_dev_iommu_fault_handler(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_ISP, "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. /* Faulted ctx found */
  52. break;
  53. }
  54. if (i == node->ctx_size) {
  55. /* Faulted ctx not found. Report PF to userspace */
  56. rc = cam_context_send_pf_evt(NULL, &pf_args);
  57. if (rc)
  58. CAM_ERR(CAM_ISP,
  59. "Failed to notify PF event to userspace rc: %d", rc);
  60. }
  61. }
  62. static const struct of_device_id cam_isp_dt_match[] = {
  63. {
  64. .compatible = "qcom,cam-isp"
  65. },
  66. {}
  67. };
  68. static int cam_isp_subdev_open(struct v4l2_subdev *sd,
  69. struct v4l2_subdev_fh *fh)
  70. {
  71. mutex_lock(&g_isp_dev.isp_mutex);
  72. g_isp_dev.open_cnt++;
  73. mutex_unlock(&g_isp_dev.isp_mutex);
  74. return 0;
  75. }
  76. static int cam_isp_subdev_close_internal(struct v4l2_subdev *sd,
  77. struct v4l2_subdev_fh *fh)
  78. {
  79. int rc = 0;
  80. struct cam_node *node = v4l2_get_subdevdata(sd);
  81. mutex_lock(&g_isp_dev.isp_mutex);
  82. if (g_isp_dev.open_cnt <= 0) {
  83. CAM_DBG(CAM_ISP, "ISP subdev is already closed");
  84. rc = -EINVAL;
  85. goto end;
  86. }
  87. g_isp_dev.open_cnt--;
  88. if (!node) {
  89. CAM_ERR(CAM_ISP, "Node ptr is NULL");
  90. rc = -EINVAL;
  91. goto end;
  92. }
  93. if (g_isp_dev.open_cnt == 0)
  94. cam_node_shutdown(node);
  95. end:
  96. mutex_unlock(&g_isp_dev.isp_mutex);
  97. return rc;
  98. }
  99. static int cam_isp_subdev_close(struct v4l2_subdev *sd,
  100. struct v4l2_subdev_fh *fh)
  101. {
  102. bool crm_active = cam_req_mgr_is_open();
  103. if (crm_active) {
  104. CAM_DBG(CAM_ISP, "CRM is ACTIVE, close should be from CRM");
  105. return 0;
  106. }
  107. return cam_isp_subdev_close_internal(sd, fh);
  108. }
  109. static const struct v4l2_subdev_internal_ops cam_isp_subdev_internal_ops = {
  110. .close = cam_isp_subdev_close,
  111. .open = cam_isp_subdev_open,
  112. };
  113. static int cam_isp_dev_component_bind(struct device *dev,
  114. struct device *master_dev, void *data)
  115. {
  116. int rc = -1;
  117. int i;
  118. struct cam_hw_mgr_intf hw_mgr_intf;
  119. struct cam_node *node;
  120. const char *compat_str = NULL;
  121. struct platform_device *pdev = to_platform_device(dev);
  122. int iommu_hdl = -1;
  123. rc = of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  124. (const char **)&compat_str);
  125. g_isp_dev.sd.internal_ops = &cam_isp_subdev_internal_ops;
  126. g_isp_dev.sd.close_seq_prior = CAM_SD_CLOSE_HIGH_PRIORITY;
  127. /* Initialize the v4l2 subdevice first. (create cam_node) */
  128. if (strnstr(compat_str, "ife", strlen(compat_str))) {
  129. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  130. CAM_IFE_DEVICE_TYPE);
  131. g_isp_dev.isp_device_type = CAM_IFE_DEVICE_TYPE;
  132. g_isp_dev.max_context = CAM_IFE_CTX_MAX;
  133. } else if (strnstr(compat_str, "tfe", strlen(compat_str))) {
  134. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  135. CAM_TFE_DEVICE_TYPE);
  136. g_isp_dev.isp_device_type = CAM_TFE_DEVICE_TYPE;
  137. g_isp_dev.max_context = CAM_TFE_CTX_MAX;
  138. } else {
  139. CAM_ERR(CAM_ISP, "Invalid ISP hw type %s", compat_str);
  140. rc = -EINVAL;
  141. goto err;
  142. }
  143. if (rc) {
  144. CAM_ERR(CAM_ISP, "ISP cam_subdev_probe failed!");
  145. goto err;
  146. }
  147. node = (struct cam_node *) g_isp_dev.sd.token;
  148. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  149. g_isp_dev.ctx = kcalloc(g_isp_dev.max_context,
  150. sizeof(struct cam_context),
  151. GFP_KERNEL);
  152. if (!g_isp_dev.ctx) {
  153. CAM_ERR(CAM_ISP,
  154. "Mem Allocation failed for ISP base context");
  155. goto unregister;
  156. }
  157. g_isp_dev.ctx_isp = kcalloc(g_isp_dev.max_context,
  158. sizeof(struct cam_isp_context),
  159. GFP_KERNEL);
  160. if (!g_isp_dev.ctx_isp) {
  161. CAM_ERR(CAM_ISP,
  162. "Mem Allocation failed for Isp private context");
  163. kfree(g_isp_dev.ctx);
  164. g_isp_dev.ctx = NULL;
  165. goto unregister;
  166. }
  167. rc = cam_isp_hw_mgr_init(compat_str, &hw_mgr_intf, &iommu_hdl);
  168. if (rc != 0) {
  169. CAM_ERR(CAM_ISP, "Can not initialized ISP HW manager!");
  170. goto kfree;
  171. }
  172. for (i = 0; i < g_isp_dev.max_context; i++) {
  173. rc = cam_isp_context_init(&g_isp_dev.ctx_isp[i],
  174. &g_isp_dev.ctx[i],
  175. &node->crm_node_intf,
  176. &node->hw_mgr_intf,
  177. i,
  178. g_isp_dev.isp_device_type, iommu_hdl);
  179. if (rc) {
  180. CAM_ERR(CAM_ISP, "ISP context init failed!");
  181. goto kfree;
  182. }
  183. }
  184. cam_common_register_err_inject_cb(cam_isp_dev_err_inject_cb,
  185. CAM_COMMON_ERR_INJECT_HW_ISP);
  186. rc = cam_node_init(node, &hw_mgr_intf, g_isp_dev.ctx,
  187. g_isp_dev.max_context, CAM_ISP_DEV_NAME);
  188. if (rc) {
  189. CAM_ERR(CAM_ISP, "ISP node init failed!");
  190. goto kfree;
  191. }
  192. node->sd_handler = cam_isp_subdev_close_internal;
  193. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  194. cam_isp_dev_iommu_fault_handler, node);
  195. mutex_init(&g_isp_dev.isp_mutex);
  196. CAM_DBG(CAM_ISP, "Component bound successfully");
  197. return 0;
  198. kfree:
  199. kfree(g_isp_dev.ctx);
  200. g_isp_dev.ctx = NULL;
  201. kfree(g_isp_dev.ctx_isp);
  202. g_isp_dev.ctx_isp = NULL;
  203. unregister:
  204. rc = cam_subdev_remove(&g_isp_dev.sd);
  205. err:
  206. return rc;
  207. }
  208. static void cam_isp_dev_component_unbind(struct device *dev,
  209. struct device *master_dev, void *data)
  210. {
  211. int rc = 0;
  212. int i;
  213. const char *compat_str = NULL;
  214. struct platform_device *pdev = to_platform_device(dev);
  215. rc = of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  216. (const char **)&compat_str);
  217. cam_isp_hw_mgr_deinit(compat_str);
  218. /* clean up resources */
  219. for (i = 0; i < g_isp_dev.max_context; i++) {
  220. rc = cam_isp_context_deinit(&g_isp_dev.ctx_isp[i]);
  221. if (rc)
  222. CAM_ERR(CAM_ISP, "ISP context %d deinit failed",
  223. i);
  224. }
  225. kfree(g_isp_dev.ctx);
  226. g_isp_dev.ctx = NULL;
  227. kfree(g_isp_dev.ctx_isp);
  228. g_isp_dev.ctx_isp = NULL;
  229. rc = cam_subdev_remove(&g_isp_dev.sd);
  230. if (rc)
  231. CAM_ERR(CAM_ISP, "Unregister failed rc: %d", rc);
  232. memset(&g_isp_dev, 0, sizeof(g_isp_dev));
  233. }
  234. const static struct component_ops cam_isp_dev_component_ops = {
  235. .bind = cam_isp_dev_component_bind,
  236. .unbind = cam_isp_dev_component_unbind,
  237. };
  238. static int cam_isp_dev_remove(struct platform_device *pdev)
  239. {
  240. component_del(&pdev->dev, &cam_isp_dev_component_ops);
  241. return 0;
  242. }
  243. static int cam_isp_dev_probe(struct platform_device *pdev)
  244. {
  245. int rc = 0;
  246. CAM_DBG(CAM_ISP, "Adding ISP dev component");
  247. rc = component_add(&pdev->dev, &cam_isp_dev_component_ops);
  248. if (rc)
  249. CAM_ERR(CAM_ISP, "failed to add component rc: %d", rc);
  250. return rc;
  251. }
  252. struct platform_driver isp_driver = {
  253. .probe = cam_isp_dev_probe,
  254. .remove = cam_isp_dev_remove,
  255. .driver = {
  256. .name = "cam_isp",
  257. .owner = THIS_MODULE,
  258. .of_match_table = cam_isp_dt_match,
  259. .suppress_bind_attrs = true,
  260. },
  261. };
  262. int cam_isp_dev_init_module(void)
  263. {
  264. return platform_driver_register(&isp_driver);
  265. }
  266. void cam_isp_dev_exit_module(void)
  267. {
  268. platform_driver_unregister(&isp_driver);
  269. }
  270. MODULE_DESCRIPTION("MSM ISP driver");
  271. MODULE_LICENSE("GPL v2");