cam_ope_subdev.c 6.0 KB

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