cam_isp_dev.c 7.9 KB

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