cam_icp_subdev.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, 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(struct cam_smmu_pf_info *pf_info)
  48. {
  49. int i = 0;
  50. struct cam_node *node = NULL;
  51. if (!pf_info || !pf_info->token) {
  52. CAM_ERR(CAM_ISP, "invalid token in page handler cb");
  53. return;
  54. }
  55. node = (struct cam_node *)pf_info->token;
  56. for (i = 0; i < node->ctx_size; i++)
  57. cam_context_dump_pf_info(&(node->ctx_list[i]), pf_info);
  58. }
  59. static void cam_icp_dev_mini_dump_cb(void *priv, void *args)
  60. {
  61. struct cam_context *ctx = NULL;
  62. if (!priv || !args) {
  63. CAM_ERR(CAM_ICP, "Invalid param priv: %pK args %pK", priv, args);
  64. return;
  65. }
  66. ctx = (struct cam_context *)priv;
  67. cam_context_mini_dump_from_hw(ctx, args);
  68. }
  69. static int cam_icp_subdev_open(struct v4l2_subdev *sd,
  70. struct v4l2_subdev_fh *fh)
  71. {
  72. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  73. struct cam_node *node = v4l2_get_subdevdata(sd);
  74. int rc = 0;
  75. mutex_lock(&g_icp_dev.icp_lock);
  76. if (g_icp_dev.open_cnt >= 1) {
  77. CAM_ERR(CAM_ICP, "ICP subdev is already opened");
  78. rc = -EALREADY;
  79. goto end;
  80. }
  81. if (!node) {
  82. CAM_ERR(CAM_ICP, "Invalid args");
  83. rc = -EINVAL;
  84. goto end;
  85. }
  86. hw_mgr_intf = &node->hw_mgr_intf;
  87. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  88. if (rc < 0) {
  89. CAM_ERR(CAM_ICP, "FW download failed");
  90. goto end;
  91. }
  92. g_icp_dev.open_cnt++;
  93. end:
  94. mutex_unlock(&g_icp_dev.icp_lock);
  95. return rc;
  96. }
  97. static int cam_icp_subdev_close_internal(struct v4l2_subdev *sd,
  98. struct v4l2_subdev_fh *fh)
  99. {
  100. int rc = 0;
  101. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  102. struct cam_node *node = v4l2_get_subdevdata(sd);
  103. mutex_lock(&g_icp_dev.icp_lock);
  104. if (g_icp_dev.open_cnt <= 0) {
  105. CAM_DBG(CAM_ICP, "ICP subdev is already closed");
  106. goto end;
  107. }
  108. g_icp_dev.open_cnt--;
  109. if (!node) {
  110. CAM_ERR(CAM_ICP, "Invalid args");
  111. rc = -EINVAL;
  112. goto end;
  113. }
  114. hw_mgr_intf = &node->hw_mgr_intf;
  115. if (!hw_mgr_intf) {
  116. CAM_ERR(CAM_ICP, "hw_mgr_intf is not initialized");
  117. rc = -EINVAL;
  118. goto end;
  119. }
  120. rc = cam_node_shutdown(node);
  121. if (rc < 0) {
  122. CAM_ERR(CAM_ICP, "HW close failed");
  123. goto end;
  124. }
  125. end:
  126. mutex_unlock(&g_icp_dev.icp_lock);
  127. return rc;
  128. }
  129. static int cam_icp_subdev_close(struct v4l2_subdev *sd,
  130. struct v4l2_subdev_fh *fh)
  131. {
  132. bool crm_active = cam_req_mgr_is_open();
  133. if (crm_active) {
  134. CAM_DBG(CAM_ICP, "CRM is ACTIVE, close should be from CRM");
  135. return 0;
  136. }
  137. return cam_icp_subdev_close_internal(sd, fh);
  138. }
  139. const struct v4l2_subdev_internal_ops cam_icp_subdev_internal_ops = {
  140. .open = cam_icp_subdev_open,
  141. .close = cam_icp_subdev_close,
  142. };
  143. static int cam_icp_component_bind(struct device *dev,
  144. struct device *master_dev, void *data)
  145. {
  146. int rc = 0, i = 0;
  147. struct cam_node *node;
  148. struct cam_hw_mgr_intf *hw_mgr_intf;
  149. int iommu_hdl = -1;
  150. struct platform_device *pdev = to_platform_device(dev);
  151. if (!pdev) {
  152. CAM_ERR(CAM_ICP, "pdev is NULL");
  153. return -EINVAL;
  154. }
  155. g_icp_dev.sd.pdev = pdev;
  156. g_icp_dev.sd.internal_ops = &cam_icp_subdev_internal_ops;
  157. g_icp_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  158. rc = cam_subdev_probe(&g_icp_dev.sd, pdev, CAM_ICP_DEV_NAME,
  159. CAM_ICP_DEVICE_TYPE);
  160. if (rc) {
  161. CAM_ERR(CAM_ICP, "ICP cam_subdev_probe failed");
  162. goto probe_fail;
  163. }
  164. node = (struct cam_node *) g_icp_dev.sd.token;
  165. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  166. if (!hw_mgr_intf) {
  167. rc = -ENOMEM;
  168. CAM_ERR(CAM_ICP, "Memory allocation fail");
  169. goto hw_alloc_fail;
  170. }
  171. rc = cam_icp_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
  172. &iommu_hdl, cam_icp_dev_mini_dump_cb);
  173. if (rc) {
  174. CAM_ERR(CAM_ICP, "ICP HW manager init failed: %d", rc);
  175. goto hw_init_fail;
  176. }
  177. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  178. g_icp_dev.ctx_icp[i].base = &g_icp_dev.ctx[i];
  179. rc = cam_icp_context_init(&g_icp_dev.ctx_icp[i],
  180. hw_mgr_intf, i, iommu_hdl);
  181. if (rc) {
  182. CAM_ERR(CAM_ICP, "ICP context init failed");
  183. goto ctx_fail;
  184. }
  185. }
  186. rc = cam_node_init(node, hw_mgr_intf, g_icp_dev.ctx,
  187. CAM_ICP_CTX_MAX, CAM_ICP_DEV_NAME);
  188. if (rc) {
  189. CAM_ERR(CAM_ICP, "ICP node init failed");
  190. goto ctx_fail;
  191. }
  192. node->sd_handler = cam_icp_subdev_close_internal;
  193. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  194. cam_icp_dev_iommu_fault_handler, node);
  195. g_icp_dev.open_cnt = 0;
  196. mutex_init(&g_icp_dev.icp_lock);
  197. CAM_DBG(CAM_ICP, "Component bound successfully");
  198. return rc;
  199. ctx_fail:
  200. for (--i; i >= 0; i--)
  201. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  202. hw_init_fail:
  203. kfree(hw_mgr_intf);
  204. hw_alloc_fail:
  205. cam_subdev_remove(&g_icp_dev.sd);
  206. probe_fail:
  207. return rc;
  208. }
  209. static void cam_icp_component_unbind(struct device *dev,
  210. struct device *master_dev, void *data)
  211. {
  212. int i;
  213. struct v4l2_subdev *sd;
  214. struct platform_device *pdev = to_platform_device(dev);
  215. if (!pdev) {
  216. CAM_ERR(CAM_ICP, "pdev is NULL");
  217. return;
  218. }
  219. sd = platform_get_drvdata(pdev);
  220. if (!sd) {
  221. CAM_ERR(CAM_ICP, "V4l2 subdev is NULL");
  222. return;
  223. }
  224. for (i = 0; i < CAM_ICP_CTX_MAX; i++)
  225. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  226. cam_icp_hw_mgr_deinit();
  227. cam_node_deinit(g_icp_dev.node);
  228. cam_subdev_remove(&g_icp_dev.sd);
  229. mutex_destroy(&g_icp_dev.icp_lock);
  230. }
  231. const static struct component_ops cam_icp_component_ops = {
  232. .bind = cam_icp_component_bind,
  233. .unbind = cam_icp_component_unbind,
  234. };
  235. static int cam_icp_probe(struct platform_device *pdev)
  236. {
  237. int rc = 0;
  238. CAM_DBG(CAM_ICP, "Adding ICP component");
  239. rc = component_add(&pdev->dev, &cam_icp_component_ops);
  240. if (rc)
  241. CAM_ERR(CAM_ICP, "failed to add component rc: %d", rc);
  242. return rc;
  243. }
  244. static int cam_icp_remove(struct platform_device *pdev)
  245. {
  246. component_del(&pdev->dev, &cam_icp_component_ops);
  247. return 0;
  248. }
  249. struct platform_driver cam_icp_driver = {
  250. .probe = cam_icp_probe,
  251. .remove = cam_icp_remove,
  252. .driver = {
  253. .name = "cam_icp",
  254. .owner = THIS_MODULE,
  255. .of_match_table = cam_icp_dt_match,
  256. .suppress_bind_attrs = true,
  257. },
  258. };
  259. int cam_icp_init_module(void)
  260. {
  261. return platform_driver_register(&cam_icp_driver);
  262. }
  263. void cam_icp_exit_module(void)
  264. {
  265. platform_driver_unregister(&cam_icp_driver);
  266. }
  267. MODULE_DESCRIPTION("MSM ICP driver");
  268. MODULE_LICENSE("GPL v2");