cam_icp_subdev.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <linux/iommu.h>
  12. #include <linux/timer.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/videodev2.h>
  15. #include <media/v4l2-fh.h>
  16. #include <media/v4l2-device.h>
  17. #include <media/v4l2-event.h>
  18. #include <media/v4l2-ioctl.h>
  19. #include <media/v4l2-subdev.h>
  20. #include <media/cam_req_mgr.h>
  21. #include <media/cam_defs.h>
  22. #include <media/cam_icp.h>
  23. #include "cam_req_mgr_dev.h"
  24. #include "cam_subdev.h"
  25. #include "cam_node.h"
  26. #include "cam_context.h"
  27. #include "cam_icp_context.h"
  28. #include "cam_hw_mgr_intf.h"
  29. #include "cam_icp_hw_mgr_intf.h"
  30. #include "cam_debug_util.h"
  31. #include "cam_smmu_api.h"
  32. #include "camera_main.h"
  33. #include "cam_common_util.h"
  34. #include "cam_context_utils.h"
  35. #define CAM_ICP_DEV_NAME "cam-icp"
  36. struct cam_icp_subdev {
  37. struct cam_subdev sd;
  38. struct cam_node *node;
  39. struct cam_context ctx[CAM_ICP_CTX_MAX];
  40. struct cam_icp_context ctx_icp[CAM_ICP_CTX_MAX];
  41. struct mutex icp_lock;
  42. int32_t open_cnt;
  43. int32_t reserved;
  44. };
  45. static struct cam_icp_subdev g_icp_dev;
  46. static const struct of_device_id cam_icp_dt_match[] = {
  47. {.compatible = "qcom,cam-icp"},
  48. {}
  49. };
  50. static int cam_icp_dev_err_inject_cb(void *err_param)
  51. {
  52. int i = 0;
  53. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  54. if (g_icp_dev.ctx[i].dev_hdl ==
  55. ((struct cam_err_inject_param *)err_param)->dev_hdl) {
  56. CAM_INFO(CAM_ICP, "ICP err inject dev_hdl found:%d",
  57. g_icp_dev.ctx[i].dev_hdl);
  58. cam_context_add_err_inject(&g_icp_dev.ctx[i], err_param);
  59. return 0;
  60. }
  61. }
  62. CAM_ERR(CAM_ICP, "No dev hdl found");
  63. return -ENODEV;
  64. }
  65. static void cam_icp_dev_iommu_fault_handler(struct cam_smmu_pf_info *pf_smmu_info)
  66. {
  67. int i, rc;
  68. struct cam_node *node = NULL;
  69. struct cam_hw_dump_pf_args pf_args = {0};
  70. if (!pf_smmu_info || !pf_smmu_info->token) {
  71. CAM_ERR(CAM_ICP, "invalid token in page handler cb");
  72. return;
  73. }
  74. node = (struct cam_node *)pf_smmu_info->token;
  75. pf_args.pf_smmu_info = pf_smmu_info;
  76. for (i = 0; i < node->ctx_size; i++) {
  77. cam_context_dump_pf_info(&(node->ctx_list[i]), &pf_args);
  78. if (pf_args.pf_context_info.ctx_found)
  79. /* found ctx and packet of the faulted address */
  80. break;
  81. }
  82. if (i == node->ctx_size) {
  83. /* Faulted ctx not found. Report PF to userspace */
  84. rc = cam_context_send_pf_evt(NULL, &pf_args);
  85. if (rc)
  86. CAM_ERR(CAM_ICP,
  87. "Failed to notify PF event to userspace rc: %d", rc);
  88. }
  89. }
  90. static void cam_icp_dev_mini_dump_cb(void *priv, void *args)
  91. {
  92. struct cam_context *ctx = NULL;
  93. if (!priv || !args) {
  94. CAM_ERR(CAM_ICP, "Invalid param priv: %pK args %pK", priv, args);
  95. return;
  96. }
  97. ctx = (struct cam_context *)priv;
  98. cam_context_mini_dump_from_hw(ctx, args);
  99. }
  100. static int cam_icp_subdev_open(struct v4l2_subdev *sd,
  101. struct v4l2_subdev_fh *fh)
  102. {
  103. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  104. struct cam_node *node = v4l2_get_subdevdata(sd);
  105. int rc = 0;
  106. mutex_lock(&g_icp_dev.icp_lock);
  107. if (g_icp_dev.open_cnt >= 1) {
  108. CAM_ERR(CAM_ICP, "ICP subdev is already opened");
  109. rc = -EALREADY;
  110. goto end;
  111. }
  112. if (!node) {
  113. CAM_ERR(CAM_ICP, "Invalid args");
  114. rc = -EINVAL;
  115. goto end;
  116. }
  117. hw_mgr_intf = &node->hw_mgr_intf;
  118. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  119. if (rc < 0) {
  120. CAM_ERR(CAM_ICP, "FW download failed");
  121. goto end;
  122. }
  123. g_icp_dev.open_cnt++;
  124. end:
  125. mutex_unlock(&g_icp_dev.icp_lock);
  126. return rc;
  127. }
  128. static int cam_icp_subdev_close_internal(struct v4l2_subdev *sd,
  129. struct v4l2_subdev_fh *fh)
  130. {
  131. int rc = 0;
  132. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  133. struct cam_node *node = v4l2_get_subdevdata(sd);
  134. mutex_lock(&g_icp_dev.icp_lock);
  135. if (g_icp_dev.open_cnt <= 0) {
  136. CAM_DBG(CAM_ICP, "ICP subdev is already closed");
  137. goto end;
  138. }
  139. g_icp_dev.open_cnt--;
  140. if (!node) {
  141. CAM_ERR(CAM_ICP, "Invalid args");
  142. rc = -EINVAL;
  143. goto end;
  144. }
  145. hw_mgr_intf = &node->hw_mgr_intf;
  146. if (!hw_mgr_intf) {
  147. CAM_ERR(CAM_ICP, "hw_mgr_intf is not initialized");
  148. rc = -EINVAL;
  149. goto end;
  150. }
  151. rc = cam_node_shutdown(node);
  152. if (rc < 0) {
  153. CAM_ERR(CAM_ICP, "HW close failed");
  154. goto end;
  155. }
  156. end:
  157. mutex_unlock(&g_icp_dev.icp_lock);
  158. return rc;
  159. }
  160. static int cam_icp_subdev_close(struct v4l2_subdev *sd,
  161. struct v4l2_subdev_fh *fh)
  162. {
  163. bool crm_active = cam_req_mgr_is_open();
  164. if (crm_active) {
  165. CAM_DBG(CAM_ICP, "CRM is ACTIVE, close should be from CRM");
  166. return 0;
  167. }
  168. return cam_icp_subdev_close_internal(sd, fh);
  169. }
  170. const struct v4l2_subdev_internal_ops cam_icp_subdev_internal_ops = {
  171. .open = cam_icp_subdev_open,
  172. .close = cam_icp_subdev_close,
  173. };
  174. static int cam_icp_component_bind(struct device *dev,
  175. struct device *master_dev, void *data)
  176. {
  177. int rc = 0, i = 0;
  178. struct cam_node *node;
  179. struct cam_hw_mgr_intf *hw_mgr_intf;
  180. int iommu_hdl = -1;
  181. struct platform_device *pdev = to_platform_device(dev);
  182. if (!pdev) {
  183. CAM_ERR(CAM_ICP, "pdev is NULL");
  184. return -EINVAL;
  185. }
  186. g_icp_dev.sd.pdev = pdev;
  187. g_icp_dev.sd.internal_ops = &cam_icp_subdev_internal_ops;
  188. g_icp_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  189. rc = cam_subdev_probe(&g_icp_dev.sd, pdev, CAM_ICP_DEV_NAME,
  190. CAM_ICP_DEVICE_TYPE);
  191. if (rc) {
  192. CAM_ERR(CAM_ICP, "ICP cam_subdev_probe failed");
  193. goto probe_fail;
  194. }
  195. node = (struct cam_node *) g_icp_dev.sd.token;
  196. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  197. if (!hw_mgr_intf) {
  198. rc = -ENOMEM;
  199. CAM_ERR(CAM_ICP, "Memory allocation fail");
  200. goto hw_alloc_fail;
  201. }
  202. rc = cam_icp_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
  203. &iommu_hdl, cam_icp_dev_mini_dump_cb);
  204. if (rc) {
  205. CAM_ERR(CAM_ICP, "ICP HW manager init failed: %d", rc);
  206. goto hw_init_fail;
  207. }
  208. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  209. g_icp_dev.ctx_icp[i].base = &g_icp_dev.ctx[i];
  210. rc = cam_icp_context_init(&g_icp_dev.ctx_icp[i],
  211. hw_mgr_intf, i, iommu_hdl);
  212. if (rc) {
  213. CAM_ERR(CAM_ICP, "ICP context init failed");
  214. goto ctx_fail;
  215. }
  216. }
  217. rc = cam_node_init(node, hw_mgr_intf, g_icp_dev.ctx,
  218. CAM_ICP_CTX_MAX, CAM_ICP_DEV_NAME);
  219. if (rc) {
  220. CAM_ERR(CAM_ICP, "ICP node init failed");
  221. goto ctx_fail;
  222. }
  223. cam_common_register_err_inject_cb(cam_icp_dev_err_inject_cb,
  224. CAM_COMMON_ERR_INJECT_HW_ICP);
  225. node->sd_handler = cam_icp_subdev_close_internal;
  226. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  227. cam_icp_dev_iommu_fault_handler, node);
  228. g_icp_dev.open_cnt = 0;
  229. mutex_init(&g_icp_dev.icp_lock);
  230. CAM_DBG(CAM_ICP, "Component bound successfully");
  231. return rc;
  232. ctx_fail:
  233. for (--i; i >= 0; i--)
  234. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  235. hw_init_fail:
  236. kfree(hw_mgr_intf);
  237. hw_alloc_fail:
  238. cam_subdev_remove(&g_icp_dev.sd);
  239. probe_fail:
  240. return rc;
  241. }
  242. static void cam_icp_component_unbind(struct device *dev,
  243. struct device *master_dev, void *data)
  244. {
  245. int i;
  246. struct v4l2_subdev *sd;
  247. struct platform_device *pdev = to_platform_device(dev);
  248. if (!pdev) {
  249. CAM_ERR(CAM_ICP, "pdev is NULL");
  250. return;
  251. }
  252. sd = platform_get_drvdata(pdev);
  253. if (!sd) {
  254. CAM_ERR(CAM_ICP, "V4l2 subdev is NULL");
  255. return;
  256. }
  257. for (i = 0; i < CAM_ICP_CTX_MAX; i++)
  258. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  259. cam_icp_hw_mgr_deinit();
  260. cam_node_deinit(g_icp_dev.node);
  261. cam_subdev_remove(&g_icp_dev.sd);
  262. mutex_destroy(&g_icp_dev.icp_lock);
  263. }
  264. const static struct component_ops cam_icp_component_ops = {
  265. .bind = cam_icp_component_bind,
  266. .unbind = cam_icp_component_unbind,
  267. };
  268. static int cam_icp_probe(struct platform_device *pdev)
  269. {
  270. int rc = 0;
  271. CAM_DBG(CAM_ICP, "Adding ICP component");
  272. rc = component_add(&pdev->dev, &cam_icp_component_ops);
  273. if (rc)
  274. CAM_ERR(CAM_ICP, "failed to add component rc: %d", rc);
  275. return rc;
  276. }
  277. static int cam_icp_remove(struct platform_device *pdev)
  278. {
  279. component_del(&pdev->dev, &cam_icp_component_ops);
  280. return 0;
  281. }
  282. struct platform_driver cam_icp_driver = {
  283. .probe = cam_icp_probe,
  284. .remove = cam_icp_remove,
  285. .driver = {
  286. .name = "cam_icp",
  287. .owner = THIS_MODULE,
  288. .of_match_table = cam_icp_dt_match,
  289. .suppress_bind_attrs = true,
  290. },
  291. };
  292. int cam_icp_init_module(void)
  293. {
  294. return platform_driver_register(&cam_icp_driver);
  295. }
  296. void cam_icp_exit_module(void)
  297. {
  298. platform_driver_unregister(&cam_icp_driver);
  299. }
  300. MODULE_DESCRIPTION("MSM ICP driver");
  301. MODULE_LICENSE("GPL v2");