cam_icp_subdev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2018, 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. #define CAM_ICP_DEV_NAME "cam-icp"
  32. struct cam_icp_subdev {
  33. struct cam_subdev sd;
  34. struct cam_node *node;
  35. struct cam_context ctx[CAM_ICP_CTX_MAX];
  36. struct cam_icp_context ctx_icp[CAM_ICP_CTX_MAX];
  37. struct mutex icp_lock;
  38. int32_t open_cnt;
  39. int32_t reserved;
  40. };
  41. static struct cam_icp_subdev g_icp_dev;
  42. static const struct of_device_id cam_icp_dt_match[] = {
  43. {.compatible = "qcom,cam-icp"},
  44. {}
  45. };
  46. static void cam_icp_dev_iommu_fault_handler(
  47. struct iommu_domain *domain, struct device *dev, unsigned long iova,
  48. int flags, void *token, uint32_t buf_info)
  49. {
  50. int i = 0;
  51. struct cam_node *node = NULL;
  52. if (!token) {
  53. CAM_ERR(CAM_ICP, "invalid token in page handler cb");
  54. return;
  55. }
  56. node = (struct cam_node *)token;
  57. for (i = 0; i < node->ctx_size; i++)
  58. cam_context_dump_pf_info(&(node->ctx_list[i]), iova,
  59. buf_info);
  60. }
  61. static int cam_icp_subdev_open(struct v4l2_subdev *sd,
  62. struct v4l2_subdev_fh *fh)
  63. {
  64. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  65. struct cam_node *node = v4l2_get_subdevdata(sd);
  66. int rc = 0;
  67. mutex_lock(&g_icp_dev.icp_lock);
  68. if (g_icp_dev.open_cnt >= 1) {
  69. CAM_ERR(CAM_ICP, "ICP subdev is already opened");
  70. rc = -EALREADY;
  71. goto end;
  72. }
  73. if (!node) {
  74. CAM_ERR(CAM_ICP, "Invalid args");
  75. rc = -EINVAL;
  76. goto end;
  77. }
  78. hw_mgr_intf = &node->hw_mgr_intf;
  79. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  80. if (rc < 0) {
  81. CAM_ERR(CAM_ICP, "FW download failed");
  82. goto end;
  83. }
  84. g_icp_dev.open_cnt++;
  85. end:
  86. mutex_unlock(&g_icp_dev.icp_lock);
  87. return rc;
  88. }
  89. static int cam_icp_subdev_close(struct v4l2_subdev *sd,
  90. struct v4l2_subdev_fh *fh)
  91. {
  92. int rc = 0;
  93. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  94. struct cam_node *node = v4l2_get_subdevdata(sd);
  95. mutex_lock(&g_icp_dev.icp_lock);
  96. if (g_icp_dev.open_cnt <= 0) {
  97. CAM_DBG(CAM_ICP, "ICP subdev is already closed");
  98. rc = -EINVAL;
  99. goto end;
  100. }
  101. g_icp_dev.open_cnt--;
  102. if (!node) {
  103. CAM_ERR(CAM_ICP, "Invalid args");
  104. rc = -EINVAL;
  105. goto end;
  106. }
  107. hw_mgr_intf = &node->hw_mgr_intf;
  108. if (!hw_mgr_intf) {
  109. CAM_ERR(CAM_ICP, "hw_mgr_intf is not initialized");
  110. rc = -EINVAL;
  111. goto end;
  112. }
  113. rc = cam_node_shutdown(node);
  114. if (rc < 0) {
  115. CAM_ERR(CAM_ICP, "HW close failed");
  116. goto end;
  117. }
  118. end:
  119. mutex_unlock(&g_icp_dev.icp_lock);
  120. return 0;
  121. }
  122. const struct v4l2_subdev_internal_ops cam_icp_subdev_internal_ops = {
  123. .open = cam_icp_subdev_open,
  124. .close = cam_icp_subdev_close,
  125. };
  126. static int cam_icp_probe(struct platform_device *pdev)
  127. {
  128. int rc = 0, i = 0;
  129. struct cam_node *node;
  130. struct cam_hw_mgr_intf *hw_mgr_intf;
  131. int iommu_hdl = -1;
  132. if (!pdev) {
  133. CAM_ERR(CAM_ICP, "pdev is NULL");
  134. return -EINVAL;
  135. }
  136. g_icp_dev.sd.pdev = pdev;
  137. g_icp_dev.sd.internal_ops = &cam_icp_subdev_internal_ops;
  138. rc = cam_subdev_probe(&g_icp_dev.sd, pdev, CAM_ICP_DEV_NAME,
  139. CAM_ICP_DEVICE_TYPE);
  140. if (rc) {
  141. CAM_ERR(CAM_ICP, "ICP cam_subdev_probe failed");
  142. goto probe_fail;
  143. }
  144. node = (struct cam_node *) g_icp_dev.sd.token;
  145. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  146. if (!hw_mgr_intf) {
  147. rc = -EINVAL;
  148. goto hw_alloc_fail;
  149. }
  150. rc = cam_icp_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
  151. &iommu_hdl);
  152. if (rc) {
  153. CAM_ERR(CAM_ICP, "ICP HW manager init failed: %d", rc);
  154. goto hw_init_fail;
  155. }
  156. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  157. g_icp_dev.ctx_icp[i].base = &g_icp_dev.ctx[i];
  158. rc = cam_icp_context_init(&g_icp_dev.ctx_icp[i],
  159. hw_mgr_intf, i);
  160. if (rc) {
  161. CAM_ERR(CAM_ICP, "ICP context init failed");
  162. goto ctx_fail;
  163. }
  164. }
  165. rc = cam_node_init(node, hw_mgr_intf, g_icp_dev.ctx,
  166. CAM_ICP_CTX_MAX, CAM_ICP_DEV_NAME);
  167. if (rc) {
  168. CAM_ERR(CAM_ICP, "ICP node init failed");
  169. goto ctx_fail;
  170. }
  171. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  172. cam_icp_dev_iommu_fault_handler, node);
  173. g_icp_dev.open_cnt = 0;
  174. mutex_init(&g_icp_dev.icp_lock);
  175. CAM_DBG(CAM_ICP, "ICP probe complete");
  176. return rc;
  177. ctx_fail:
  178. for (--i; i >= 0; i--)
  179. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  180. hw_init_fail:
  181. kfree(hw_mgr_intf);
  182. hw_alloc_fail:
  183. cam_subdev_remove(&g_icp_dev.sd);
  184. probe_fail:
  185. return rc;
  186. }
  187. static int cam_icp_remove(struct platform_device *pdev)
  188. {
  189. int i;
  190. struct v4l2_subdev *sd;
  191. struct cam_subdev *subdev;
  192. if (!pdev) {
  193. CAM_ERR(CAM_ICP, "pdev is NULL");
  194. return -ENODEV;
  195. }
  196. sd = platform_get_drvdata(pdev);
  197. if (!sd) {
  198. CAM_ERR(CAM_ICP, "V4l2 subdev is NULL");
  199. return -ENODEV;
  200. }
  201. subdev = v4l2_get_subdevdata(sd);
  202. if (!subdev) {
  203. CAM_ERR(CAM_ICP, "cam subdev is NULL");
  204. return -ENODEV;
  205. }
  206. for (i = 0; i < CAM_ICP_CTX_MAX; i++)
  207. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  208. cam_node_deinit(g_icp_dev.node);
  209. cam_subdev_remove(&g_icp_dev.sd);
  210. mutex_destroy(&g_icp_dev.icp_lock);
  211. return 0;
  212. }
  213. static struct platform_driver cam_icp_driver = {
  214. .probe = cam_icp_probe,
  215. .remove = cam_icp_remove,
  216. .driver = {
  217. .name = "cam_icp",
  218. .owner = THIS_MODULE,
  219. .of_match_table = cam_icp_dt_match,
  220. .suppress_bind_attrs = true,
  221. },
  222. };
  223. static int __init cam_icp_init_module(void)
  224. {
  225. return platform_driver_register(&cam_icp_driver);
  226. }
  227. static void __exit cam_icp_exit_module(void)
  228. {
  229. platform_driver_unregister(&cam_icp_driver);
  230. }
  231. module_init(cam_icp_init_module);
  232. module_exit(cam_icp_exit_module);
  233. MODULE_DESCRIPTION("MSM ICP driver");
  234. MODULE_LICENSE("GPL v2");