cam_ope_subdev.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-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_ope.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_ope_context.h"
  28. #include "cam_ope_hw_mgr_intf.h"
  29. #include "cam_hw_mgr_intf.h"
  30. #include "cam_debug_util.h"
  31. #include "cam_smmu_api.h"
  32. #include "camera_main.h"
  33. #include "cam_context_utils.h"
  34. #define OPE_DEV_NAME "cam-ope"
  35. struct cam_ope_subdev {
  36. struct cam_subdev sd;
  37. struct cam_node *node;
  38. struct cam_context ctx[OPE_CTX_MAX];
  39. struct cam_ope_context ctx_ope[OPE_CTX_MAX];
  40. struct mutex ope_lock;
  41. int32_t open_cnt;
  42. int32_t reserved;
  43. };
  44. static struct cam_ope_subdev g_ope_dev;
  45. static void cam_ope_dev_iommu_fault_handler(
  46. struct cam_smmu_pf_info *pf_smmu_info)
  47. {
  48. int i, rc;
  49. struct cam_node *node = NULL;
  50. struct cam_hw_dump_pf_args pf_args = {0};
  51. if (!pf_smmu_info || !pf_smmu_info->token) {
  52. CAM_ERR(CAM_OPE, "invalid token in page handler cb");
  53. return;
  54. }
  55. node = (struct cam_node *)pf_smmu_info->token;
  56. pf_args.pf_smmu_info = pf_smmu_info;
  57. for (i = 0; i < node->ctx_size; i++) {
  58. cam_context_dump_pf_info(&(node->ctx_list[i]), &pf_args);
  59. if (pf_args.pf_context_info.ctx_found)
  60. /* found ctx and packet of the faulted address */
  61. break;
  62. }
  63. if (i == node->ctx_size) {
  64. /* Faulted ctx not found. But report PF to UMD anyway*/
  65. rc = cam_context_send_pf_evt(NULL, &pf_args);
  66. if (rc)
  67. CAM_ERR(CAM_OPE,
  68. "Failed to notify PF event to userspace rc: %d", rc);
  69. }
  70. }
  71. static int cam_ope_subdev_open(struct v4l2_subdev *sd,
  72. struct v4l2_subdev_fh *fh)
  73. {
  74. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  75. struct cam_node *node = v4l2_get_subdevdata(sd);
  76. int rc = 0;
  77. mutex_lock(&g_ope_dev.ope_lock);
  78. if (g_ope_dev.open_cnt >= 1) {
  79. CAM_ERR(CAM_OPE, "OPE subdev is already opened");
  80. rc = -EALREADY;
  81. goto end;
  82. }
  83. if (!node) {
  84. CAM_ERR(CAM_OPE, "Invalid args");
  85. rc = -EINVAL;
  86. goto end;
  87. }
  88. hw_mgr_intf = &node->hw_mgr_intf;
  89. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  90. if (rc < 0) {
  91. CAM_ERR(CAM_OPE, "OPE HW open failed: %d", rc);
  92. goto end;
  93. }
  94. g_ope_dev.open_cnt++;
  95. CAM_DBG(CAM_OPE, "OPE HW open success: %d", rc);
  96. end:
  97. mutex_unlock(&g_ope_dev.ope_lock);
  98. return rc;
  99. }
  100. static int cam_ope_subdev_close_internal(struct v4l2_subdev *sd,
  101. struct v4l2_subdev_fh *fh)
  102. {
  103. int rc = 0;
  104. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  105. struct cam_node *node = v4l2_get_subdevdata(sd);
  106. mutex_lock(&g_ope_dev.ope_lock);
  107. if (g_ope_dev.open_cnt <= 0) {
  108. CAM_DBG(CAM_OPE, "OPE subdev is already closed");
  109. rc = -EINVAL;
  110. goto end;
  111. }
  112. g_ope_dev.open_cnt--;
  113. if (!node) {
  114. CAM_ERR(CAM_OPE, "Invalid args");
  115. rc = -EINVAL;
  116. goto end;
  117. }
  118. hw_mgr_intf = &node->hw_mgr_intf;
  119. if (!hw_mgr_intf) {
  120. CAM_ERR(CAM_OPE, "hw_mgr_intf is not initialized");
  121. rc = -EINVAL;
  122. goto end;
  123. }
  124. rc = cam_node_shutdown(node);
  125. if (rc < 0) {
  126. CAM_ERR(CAM_OPE, "HW close failed");
  127. goto end;
  128. }
  129. CAM_DBG(CAM_OPE, "OPE HW close success: %d", rc);
  130. end:
  131. mutex_unlock(&g_ope_dev.ope_lock);
  132. return rc;
  133. }
  134. static int cam_ope_subdev_close(struct v4l2_subdev *sd,
  135. struct v4l2_subdev_fh *fh)
  136. {
  137. bool crm_active = cam_req_mgr_is_open();
  138. if (crm_active) {
  139. CAM_DBG(CAM_OPE, "CRM is ACTIVE, close should be from CRM");
  140. return 0;
  141. }
  142. return cam_ope_subdev_close_internal(sd, fh);
  143. }
  144. const struct v4l2_subdev_internal_ops cam_ope_subdev_internal_ops = {
  145. .open = cam_ope_subdev_open,
  146. .close = cam_ope_subdev_close,
  147. };
  148. static int cam_ope_subdev_component_bind(struct device *dev,
  149. struct device *master_dev, void *data)
  150. {
  151. int rc = 0, i = 0;
  152. struct cam_node *node;
  153. struct cam_hw_mgr_intf *hw_mgr_intf;
  154. int iommu_hdl = -1;
  155. struct platform_device *pdev = to_platform_device(dev);
  156. CAM_DBG(CAM_OPE, "Binding OPE subdev component");
  157. if (!pdev) {
  158. CAM_ERR(CAM_OPE, "pdev is NULL");
  159. return -EINVAL;
  160. }
  161. g_ope_dev.sd.pdev = pdev;
  162. g_ope_dev.sd.internal_ops = &cam_ope_subdev_internal_ops;
  163. g_ope_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  164. rc = cam_subdev_probe(&g_ope_dev.sd, pdev, OPE_DEV_NAME,
  165. CAM_OPE_DEVICE_TYPE);
  166. if (rc) {
  167. CAM_ERR(CAM_OPE, "OPE cam_subdev_probe failed:%d", rc);
  168. return rc;
  169. }
  170. node = (struct cam_node *) g_ope_dev.sd.token;
  171. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  172. if (!hw_mgr_intf) {
  173. rc = -EINVAL;
  174. goto hw_alloc_fail;
  175. }
  176. rc = cam_ope_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
  177. &iommu_hdl);
  178. if (rc) {
  179. CAM_ERR(CAM_OPE, "OPE HW manager init failed: %d", rc);
  180. goto hw_init_fail;
  181. }
  182. for (i = 0; i < OPE_CTX_MAX; i++) {
  183. g_ope_dev.ctx_ope[i].base = &g_ope_dev.ctx[i];
  184. rc = cam_ope_context_init(&g_ope_dev.ctx_ope[i],
  185. hw_mgr_intf, i, iommu_hdl);
  186. if (rc) {
  187. CAM_ERR(CAM_OPE, "OPE context init failed");
  188. goto ctx_fail;
  189. }
  190. }
  191. rc = cam_node_init(node, hw_mgr_intf, g_ope_dev.ctx,
  192. OPE_CTX_MAX, OPE_DEV_NAME);
  193. if (rc) {
  194. CAM_ERR(CAM_OPE, "OPE node init failed");
  195. goto ctx_fail;
  196. }
  197. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  198. cam_ope_dev_iommu_fault_handler, node);
  199. g_ope_dev.open_cnt = 0;
  200. mutex_init(&g_ope_dev.ope_lock);
  201. node->sd_handler = cam_ope_subdev_close_internal;
  202. CAM_DBG(CAM_OPE, "Subdev component bound successfully");
  203. return rc;
  204. ctx_fail:
  205. for (--i; i >= 0; i--)
  206. cam_ope_context_deinit(&g_ope_dev.ctx_ope[i]);
  207. hw_init_fail:
  208. kfree(hw_mgr_intf);
  209. hw_alloc_fail:
  210. cam_subdev_remove(&g_ope_dev.sd);
  211. return rc;
  212. }
  213. static void cam_ope_subdev_component_unbind(struct device *dev,
  214. struct device *master_dev, void *data)
  215. {
  216. int i;
  217. struct v4l2_subdev *sd;
  218. struct cam_subdev *subdev;
  219. struct platform_device *pdev = to_platform_device(dev);
  220. if (!pdev) {
  221. CAM_ERR(CAM_OPE, "pdev is NULL");
  222. return;
  223. }
  224. sd = platform_get_drvdata(pdev);
  225. if (!sd) {
  226. CAM_ERR(CAM_OPE, "V4l2 subdev is NULL");
  227. return;
  228. }
  229. subdev = v4l2_get_subdevdata(sd);
  230. if (!subdev) {
  231. CAM_ERR(CAM_OPE, "cam subdev is NULL");
  232. return;
  233. }
  234. for (i = 0; i < OPE_CTX_MAX; i++)
  235. cam_ope_context_deinit(&g_ope_dev.ctx_ope[i]);
  236. cam_node_deinit(g_ope_dev.node);
  237. cam_subdev_remove(&g_ope_dev.sd);
  238. mutex_destroy(&g_ope_dev.ope_lock);
  239. }
  240. const static struct component_ops cam_ope_subdev_component_ops = {
  241. .bind = cam_ope_subdev_component_bind,
  242. .unbind = cam_ope_subdev_component_unbind,
  243. };
  244. static int cam_ope_subdev_probe(struct platform_device *pdev)
  245. {
  246. int rc = 0;
  247. CAM_DBG(CAM_OPE, "Adding OPE subdev component");
  248. rc = component_add(&pdev->dev, &cam_ope_subdev_component_ops);
  249. if (rc)
  250. CAM_ERR(CAM_OPE, "failed to add component rc: %d", rc);
  251. return rc;
  252. }
  253. static int cam_ope_subdev_remove(struct platform_device *pdev)
  254. {
  255. component_del(&pdev->dev, &cam_ope_subdev_component_ops);
  256. return 0;
  257. }
  258. static const struct of_device_id cam_ope_dt_match[] = {
  259. {.compatible = "qcom,cam-ope"},
  260. {}
  261. };
  262. struct platform_driver cam_ope_subdev_driver = {
  263. .probe = cam_ope_subdev_probe,
  264. .remove = cam_ope_subdev_remove,
  265. .driver = {
  266. .name = "cam_ope",
  267. .of_match_table = cam_ope_dt_match,
  268. .suppress_bind_attrs = true,
  269. },
  270. };
  271. int cam_ope_subdev_init_module(void)
  272. {
  273. return platform_driver_register(&cam_ope_subdev_driver);
  274. }
  275. void cam_ope_subdev_exit_module(void)
  276. {
  277. platform_driver_unregister(&cam_ope_subdev_driver);
  278. }
  279. MODULE_DESCRIPTION("MSM OPE driver");
  280. MODULE_LICENSE("GPL v2");