cam_isp_dev.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 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 void cam_isp_subdev_handle_message(
  64. struct v4l2_subdev *sd,
  65. enum cam_subdev_message_type_t message_type,
  66. void *data)
  67. {
  68. int i, rc = 0;
  69. struct cam_node *node = v4l2_get_subdevdata(sd);
  70. CAM_DBG(CAM_ISP, "node name %s", node->name);
  71. for (i = 0; i < node->ctx_size; i++) {
  72. rc = cam_context_handle_message(&(node->ctx_list[i]), message_type, data);
  73. if (rc)
  74. CAM_ERR(CAM_ISP, "Failed to handle message for %s", node->name);
  75. }
  76. }
  77. static const struct of_device_id cam_isp_dt_match[] = {
  78. {
  79. .compatible = "qcom,cam-isp"
  80. },
  81. {}
  82. };
  83. static int cam_isp_subdev_open(struct v4l2_subdev *sd,
  84. struct v4l2_subdev_fh *fh)
  85. {
  86. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_LOCK);
  87. mutex_lock(&g_isp_dev.isp_mutex);
  88. g_isp_dev.open_cnt++;
  89. mutex_unlock(&g_isp_dev.isp_mutex);
  90. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_UNLOCK);
  91. return 0;
  92. }
  93. static int cam_isp_subdev_close_internal(struct v4l2_subdev *sd,
  94. struct v4l2_subdev_fh *fh)
  95. {
  96. int rc = 0;
  97. struct cam_node *node = v4l2_get_subdevdata(sd);
  98. mutex_lock(&g_isp_dev.isp_mutex);
  99. if (g_isp_dev.open_cnt <= 0) {
  100. CAM_DBG(CAM_ISP, "ISP subdev is already closed");
  101. rc = -EINVAL;
  102. goto end;
  103. }
  104. g_isp_dev.open_cnt--;
  105. if (!node) {
  106. CAM_ERR(CAM_ISP, "Node ptr is NULL");
  107. rc = -EINVAL;
  108. goto end;
  109. }
  110. if (g_isp_dev.open_cnt == 0)
  111. cam_node_shutdown(node);
  112. end:
  113. mutex_unlock(&g_isp_dev.isp_mutex);
  114. return rc;
  115. }
  116. static int cam_isp_subdev_close(struct v4l2_subdev *sd,
  117. struct v4l2_subdev_fh *fh)
  118. {
  119. bool crm_active = cam_req_mgr_is_open();
  120. if (crm_active) {
  121. CAM_DBG(CAM_ISP, "CRM is ACTIVE, close should be from CRM");
  122. return 0;
  123. }
  124. return cam_isp_subdev_close_internal(sd, fh);
  125. }
  126. static const struct v4l2_subdev_internal_ops cam_isp_subdev_internal_ops = {
  127. .close = cam_isp_subdev_close,
  128. .open = cam_isp_subdev_open,
  129. };
  130. static int cam_isp_dev_component_bind(struct device *dev,
  131. struct device *master_dev, void *data)
  132. {
  133. int rc = -1;
  134. int i;
  135. struct cam_hw_mgr_intf hw_mgr_intf;
  136. struct cam_node *node;
  137. const char *compat_str = NULL;
  138. struct platform_device *pdev = to_platform_device(dev);
  139. int iommu_hdl = -1;
  140. of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  141. (const char **)&compat_str);
  142. g_isp_dev.sd.internal_ops = &cam_isp_subdev_internal_ops;
  143. g_isp_dev.sd.close_seq_prior = CAM_SD_CLOSE_HIGH_PRIORITY;
  144. /* Initialize the v4l2 subdevice first. (create cam_node) */
  145. if (strnstr(compat_str, "ife", strlen(compat_str))) {
  146. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  147. CAM_IFE_DEVICE_TYPE);
  148. g_isp_dev.isp_device_type = CAM_IFE_DEVICE_TYPE;
  149. g_isp_dev.max_context = CAM_IFE_CTX_MAX;
  150. } else if (strnstr(compat_str, "mc_tfe", strlen(compat_str))) {
  151. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  152. CAM_TFE_MC_DEVICE_TYPE);
  153. g_isp_dev.isp_device_type = CAM_TFE_MC_DEVICE_TYPE;
  154. g_isp_dev.max_context = CAM_IFE_CTX_MAX;
  155. } else if (strnstr(compat_str, "tfe", strlen(compat_str))) {
  156. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  157. CAM_TFE_DEVICE_TYPE);
  158. g_isp_dev.sd.msg_cb = cam_isp_subdev_handle_message;
  159. g_isp_dev.isp_device_type = CAM_TFE_DEVICE_TYPE;
  160. g_isp_dev.max_context = CAM_TFE_CTX_MAX;
  161. } else {
  162. CAM_ERR(CAM_ISP, "Invalid ISP hw type %s", compat_str);
  163. rc = -EINVAL;
  164. goto err;
  165. }
  166. if (rc) {
  167. CAM_ERR(CAM_ISP, "ISP cam_subdev_probe failed!");
  168. goto err;
  169. }
  170. node = (struct cam_node *) g_isp_dev.sd.token;
  171. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  172. g_isp_dev.ctx = kcalloc(g_isp_dev.max_context,
  173. sizeof(struct cam_context),
  174. GFP_KERNEL);
  175. if (!g_isp_dev.ctx) {
  176. CAM_ERR(CAM_ISP,
  177. "Mem Allocation failed for ISP base context");
  178. goto unregister;
  179. }
  180. g_isp_dev.ctx_isp = kcalloc(g_isp_dev.max_context,
  181. sizeof(struct cam_isp_context),
  182. GFP_KERNEL);
  183. if (!g_isp_dev.ctx_isp) {
  184. CAM_ERR(CAM_ISP,
  185. "Mem Allocation failed for Isp private context");
  186. kfree(g_isp_dev.ctx);
  187. g_isp_dev.ctx = NULL;
  188. goto unregister;
  189. }
  190. rc = cam_isp_hw_mgr_init(compat_str, &hw_mgr_intf, &iommu_hdl,
  191. g_isp_dev.isp_device_type);
  192. if (rc != 0) {
  193. CAM_ERR(CAM_ISP, "Can not initialized ISP HW manager!");
  194. goto free_mem;
  195. }
  196. for (i = 0; i < g_isp_dev.max_context; i++) {
  197. rc = cam_isp_context_init(&g_isp_dev.ctx_isp[i],
  198. &g_isp_dev.ctx[i],
  199. &node->crm_node_intf,
  200. &node->hw_mgr_intf,
  201. i,
  202. g_isp_dev.isp_device_type, iommu_hdl);
  203. if (rc) {
  204. CAM_ERR(CAM_ISP, "ISP context init failed!");
  205. goto free_mem;
  206. }
  207. }
  208. if (g_isp_dev.isp_device_type == CAM_IFE_DEVICE_TYPE)
  209. cam_common_register_evt_inject_cb(cam_isp_dev_evt_inject_cb,
  210. CAM_COMMON_EVT_INJECT_HW_IFE);
  211. else
  212. cam_common_register_evt_inject_cb(cam_isp_dev_evt_inject_cb,
  213. CAM_COMMON_EVT_INJECT_HW_TFE);
  214. rc = cam_node_init(node, &hw_mgr_intf, g_isp_dev.ctx,
  215. g_isp_dev.max_context, CAM_ISP_DEV_NAME);
  216. if (rc) {
  217. CAM_ERR(CAM_ISP, "ISP node init failed!");
  218. goto free_mem;
  219. }
  220. node->sd_handler = cam_isp_subdev_close_internal;
  221. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  222. cam_isp_dev_iommu_fault_handler, node);
  223. mutex_init(&g_isp_dev.isp_mutex);
  224. CAM_DBG(CAM_ISP, "Component bound successfully");
  225. return 0;
  226. free_mem:
  227. kfree(g_isp_dev.ctx);
  228. g_isp_dev.ctx = NULL;
  229. kfree(g_isp_dev.ctx_isp);
  230. g_isp_dev.ctx_isp = NULL;
  231. unregister:
  232. rc = cam_subdev_remove(&g_isp_dev.sd);
  233. err:
  234. return rc;
  235. }
  236. static void cam_isp_dev_component_unbind(struct device *dev,
  237. struct device *master_dev, void *data)
  238. {
  239. int rc = 0;
  240. int i;
  241. const char *compat_str = NULL;
  242. struct platform_device *pdev = to_platform_device(dev);
  243. of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  244. (const char **)&compat_str);
  245. cam_isp_hw_mgr_deinit(compat_str);
  246. /* clean up resources */
  247. for (i = 0; i < g_isp_dev.max_context; i++) {
  248. rc = cam_isp_context_deinit(&g_isp_dev.ctx_isp[i]);
  249. if (rc)
  250. CAM_ERR(CAM_ISP, "ISP context %d deinit failed",
  251. i);
  252. }
  253. kfree(g_isp_dev.ctx);
  254. g_isp_dev.ctx = NULL;
  255. kfree(g_isp_dev.ctx_isp);
  256. g_isp_dev.ctx_isp = NULL;
  257. rc = cam_subdev_remove(&g_isp_dev.sd);
  258. if (rc)
  259. CAM_ERR(CAM_ISP, "Unregister failed rc: %d", rc);
  260. memset(&g_isp_dev, 0, sizeof(g_isp_dev));
  261. }
  262. const static struct component_ops cam_isp_dev_component_ops = {
  263. .bind = cam_isp_dev_component_bind,
  264. .unbind = cam_isp_dev_component_unbind,
  265. };
  266. static int cam_isp_dev_remove(struct platform_device *pdev)
  267. {
  268. component_del(&pdev->dev, &cam_isp_dev_component_ops);
  269. return 0;
  270. }
  271. static int cam_isp_dev_probe(struct platform_device *pdev)
  272. {
  273. int rc = 0;
  274. CAM_DBG(CAM_ISP, "Adding ISP dev component");
  275. rc = component_add(&pdev->dev, &cam_isp_dev_component_ops);
  276. if (rc)
  277. CAM_ERR(CAM_ISP, "failed to add component rc: %d", rc);
  278. return rc;
  279. }
  280. struct platform_driver isp_driver = {
  281. .probe = cam_isp_dev_probe,
  282. .remove = cam_isp_dev_remove,
  283. .driver = {
  284. .name = "cam_isp",
  285. .owner = THIS_MODULE,
  286. .of_match_table = cam_isp_dt_match,
  287. .suppress_bind_attrs = true,
  288. },
  289. };
  290. int cam_isp_dev_init_module(void)
  291. {
  292. return platform_driver_register(&isp_driver);
  293. }
  294. void cam_isp_dev_exit_module(void)
  295. {
  296. platform_driver_unregister(&isp_driver);
  297. }
  298. MODULE_DESCRIPTION("MSM ISP driver");
  299. MODULE_LICENSE("GPL v2");