cam_icp_subdev.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/of.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/iommu.h>
  11. #include <linux/timer.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/videodev2.h>
  14. #include <media/v4l2-fh.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/v4l2-ioctl.h>
  18. #include <media/v4l2-subdev.h>
  19. #include <media/cam_req_mgr.h>
  20. #include <media/cam_defs.h>
  21. #include <media/cam_icp.h>
  22. #include "cam_req_mgr_dev.h"
  23. #include "cam_subdev.h"
  24. #include "cam_node.h"
  25. #include "cam_context.h"
  26. #include "cam_icp_context.h"
  27. #include "cam_hw_mgr_intf.h"
  28. #include "cam_icp_hw_mgr_intf.h"
  29. #include "cam_debug_util.h"
  30. #include "cam_smmu_api.h"
  31. #include "camera_main.h"
  32. #define CAM_ICP_DEV_NAME "cam-icp"
  33. struct cam_icp_subdev {
  34. struct cam_subdev sd;
  35. struct cam_node *node;
  36. struct cam_context ctx[CAM_ICP_CTX_MAX];
  37. struct cam_icp_context ctx_icp[CAM_ICP_CTX_MAX];
  38. struct mutex icp_lock;
  39. int32_t open_cnt;
  40. int32_t reserved;
  41. };
  42. static struct cam_icp_subdev g_icp_dev;
  43. static const struct of_device_id cam_icp_dt_match[] = {
  44. {.compatible = "qcom,cam-icp"},
  45. {}
  46. };
  47. static void cam_icp_dev_iommu_fault_handler(
  48. struct iommu_domain *domain, struct device *dev, unsigned long iova,
  49. int flags, void *token, uint32_t buf_info)
  50. {
  51. int i = 0;
  52. struct cam_node *node = NULL;
  53. if (!token) {
  54. CAM_ERR(CAM_ICP, "invalid token in page handler cb");
  55. return;
  56. }
  57. node = (struct cam_node *)token;
  58. for (i = 0; i < node->ctx_size; i++)
  59. cam_context_dump_pf_info(&(node->ctx_list[i]), iova,
  60. buf_info);
  61. }
  62. static int cam_icp_subdev_open(struct v4l2_subdev *sd,
  63. struct v4l2_subdev_fh *fh)
  64. {
  65. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  66. struct cam_node *node = v4l2_get_subdevdata(sd);
  67. int rc = 0;
  68. mutex_lock(&g_icp_dev.icp_lock);
  69. if (g_icp_dev.open_cnt >= 1) {
  70. CAM_ERR(CAM_ICP, "ICP subdev is already opened");
  71. rc = -EALREADY;
  72. goto end;
  73. }
  74. if (!node) {
  75. CAM_ERR(CAM_ICP, "Invalid args");
  76. rc = -EINVAL;
  77. goto end;
  78. }
  79. hw_mgr_intf = &node->hw_mgr_intf;
  80. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  81. if (rc < 0) {
  82. CAM_ERR(CAM_ICP, "FW download failed");
  83. goto end;
  84. }
  85. g_icp_dev.open_cnt++;
  86. end:
  87. mutex_unlock(&g_icp_dev.icp_lock);
  88. return rc;
  89. }
  90. static int cam_icp_subdev_close(struct v4l2_subdev *sd,
  91. struct v4l2_subdev_fh *fh)
  92. {
  93. int rc = 0;
  94. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  95. struct cam_node *node = v4l2_get_subdevdata(sd);
  96. mutex_lock(&g_icp_dev.icp_lock);
  97. if (g_icp_dev.open_cnt <= 0) {
  98. CAM_DBG(CAM_ICP, "ICP subdev is already closed");
  99. rc = -EINVAL;
  100. goto end;
  101. }
  102. g_icp_dev.open_cnt--;
  103. if (!node) {
  104. CAM_ERR(CAM_ICP, "Invalid args");
  105. rc = -EINVAL;
  106. goto end;
  107. }
  108. hw_mgr_intf = &node->hw_mgr_intf;
  109. if (!hw_mgr_intf) {
  110. CAM_ERR(CAM_ICP, "hw_mgr_intf is not initialized");
  111. rc = -EINVAL;
  112. goto end;
  113. }
  114. rc = cam_node_shutdown(node);
  115. if (rc < 0) {
  116. CAM_ERR(CAM_ICP, "HW close failed");
  117. goto end;
  118. }
  119. end:
  120. mutex_unlock(&g_icp_dev.icp_lock);
  121. return 0;
  122. }
  123. const struct v4l2_subdev_internal_ops cam_icp_subdev_internal_ops = {
  124. .open = cam_icp_subdev_open,
  125. .close = cam_icp_subdev_close,
  126. };
  127. static int cam_icp_component_bind(struct device *dev,
  128. struct device *master_dev, void *data)
  129. {
  130. int rc = 0, i = 0;
  131. struct cam_node *node;
  132. struct cam_hw_mgr_intf *hw_mgr_intf;
  133. int iommu_hdl = -1;
  134. struct platform_device *pdev = to_platform_device(dev);
  135. if (!pdev) {
  136. CAM_ERR(CAM_ICP, "pdev is NULL");
  137. return -EINVAL;
  138. }
  139. g_icp_dev.sd.pdev = pdev;
  140. g_icp_dev.sd.internal_ops = &cam_icp_subdev_internal_ops;
  141. rc = cam_subdev_probe(&g_icp_dev.sd, pdev, CAM_ICP_DEV_NAME,
  142. CAM_ICP_DEVICE_TYPE);
  143. if (rc) {
  144. CAM_ERR(CAM_ICP, "ICP cam_subdev_probe failed");
  145. goto probe_fail;
  146. }
  147. node = (struct cam_node *) g_icp_dev.sd.token;
  148. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  149. if (!hw_mgr_intf) {
  150. rc = -ENOMEM;
  151. CAM_ERR(CAM_ICP, "Memory allocation fail");
  152. goto hw_alloc_fail;
  153. }
  154. rc = cam_icp_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
  155. &iommu_hdl);
  156. if (rc) {
  157. CAM_ERR(CAM_ICP, "ICP HW manager init failed: %d", rc);
  158. goto hw_init_fail;
  159. }
  160. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  161. g_icp_dev.ctx_icp[i].base = &g_icp_dev.ctx[i];
  162. rc = cam_icp_context_init(&g_icp_dev.ctx_icp[i],
  163. hw_mgr_intf, i);
  164. if (rc) {
  165. CAM_ERR(CAM_ICP, "ICP context init failed");
  166. goto ctx_fail;
  167. }
  168. }
  169. rc = cam_node_init(node, hw_mgr_intf, g_icp_dev.ctx,
  170. CAM_ICP_CTX_MAX, CAM_ICP_DEV_NAME);
  171. if (rc) {
  172. CAM_ERR(CAM_ICP, "ICP node init failed");
  173. goto ctx_fail;
  174. }
  175. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  176. cam_icp_dev_iommu_fault_handler, node);
  177. g_icp_dev.open_cnt = 0;
  178. mutex_init(&g_icp_dev.icp_lock);
  179. CAM_DBG(CAM_ICP, "Component bound successfully");
  180. return rc;
  181. ctx_fail:
  182. for (--i; i >= 0; i--)
  183. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  184. hw_init_fail:
  185. kfree(hw_mgr_intf);
  186. hw_alloc_fail:
  187. cam_subdev_remove(&g_icp_dev.sd);
  188. probe_fail:
  189. return rc;
  190. }
  191. static void cam_icp_component_unbind(struct device *dev,
  192. struct device *master_dev, void *data)
  193. {
  194. int i;
  195. struct v4l2_subdev *sd;
  196. struct platform_device *pdev = to_platform_device(dev);
  197. if (!pdev) {
  198. CAM_ERR(CAM_ICP, "pdev is NULL");
  199. return;
  200. }
  201. sd = platform_get_drvdata(pdev);
  202. if (!sd) {
  203. CAM_ERR(CAM_ICP, "V4l2 subdev is NULL");
  204. return;
  205. }
  206. for (i = 0; i < CAM_ICP_CTX_MAX; i++)
  207. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  208. cam_icp_hw_mgr_deinit();
  209. cam_node_deinit(g_icp_dev.node);
  210. cam_subdev_remove(&g_icp_dev.sd);
  211. mutex_destroy(&g_icp_dev.icp_lock);
  212. }
  213. const static struct component_ops cam_icp_component_ops = {
  214. .bind = cam_icp_component_bind,
  215. .unbind = cam_icp_component_unbind,
  216. };
  217. static int cam_icp_probe(struct platform_device *pdev)
  218. {
  219. int rc = 0;
  220. CAM_DBG(CAM_ICP, "Adding ICP component");
  221. rc = component_add(&pdev->dev, &cam_icp_component_ops);
  222. if (rc)
  223. CAM_ERR(CAM_ICP, "failed to add component rc: %d", rc);
  224. return rc;
  225. }
  226. static int cam_icp_remove(struct platform_device *pdev)
  227. {
  228. component_del(&pdev->dev, &cam_icp_component_ops);
  229. return 0;
  230. }
  231. struct platform_driver cam_icp_driver = {
  232. .probe = cam_icp_probe,
  233. .remove = cam_icp_remove,
  234. .driver = {
  235. .name = "cam_icp",
  236. .owner = THIS_MODULE,
  237. .of_match_table = cam_icp_dt_match,
  238. .suppress_bind_attrs = true,
  239. },
  240. };
  241. int cam_icp_init_module(void)
  242. {
  243. return platform_driver_register(&cam_icp_driver);
  244. }
  245. void cam_icp_exit_module(void)
  246. {
  247. platform_driver_unregister(&cam_icp_driver);
  248. }
  249. MODULE_DESCRIPTION("MSM ICP driver");
  250. MODULE_LICENSE("GPL v2");