cam_icp_subdev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-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_icp.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_icp_context.h"
  28. #include "cam_hw_mgr_intf.h"
  29. #include "cam_icp_hw_mgr_intf.h"
  30. #include "cam_debug_util.h"
  31. #include "cam_smmu_api.h"
  32. #include "camera_main.h"
  33. #include "cam_common_util.h"
  34. #include "cam_context_utils.h"
  35. #define CAM_ICP_DEV_NAME "cam-icp"
  36. struct cam_icp_subdev {
  37. struct cam_subdev sd;
  38. struct cam_node *node;
  39. struct cam_context ctx[CAM_ICP_CTX_MAX];
  40. struct cam_icp_context ctx_icp[CAM_ICP_CTX_MAX];
  41. struct mutex icp_lock;
  42. int32_t open_cnt;
  43. int32_t reserved;
  44. };
  45. static struct cam_icp_subdev g_icp_dev;
  46. static const struct of_device_id cam_icp_dt_match[] = {
  47. {.compatible = "qcom,cam-icp"},
  48. {}
  49. };
  50. static int cam_icp_dev_evt_inject_cb(void *inject_args)
  51. {
  52. struct cam_common_inject_evt_param *inject_params = inject_args;
  53. int i;
  54. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  55. if (g_icp_dev.ctx[i].dev_hdl == inject_params->dev_hdl) {
  56. cam_context_add_evt_inject(&g_icp_dev.ctx[i],
  57. &inject_params->evt_params);
  58. return 0;
  59. }
  60. }
  61. CAM_ERR(CAM_ICP, "No dev hdl found %d", inject_params->dev_hdl);
  62. return -ENODEV;
  63. }
  64. static void cam_icp_dev_iommu_fault_handler(struct cam_smmu_pf_info *pf_smmu_info)
  65. {
  66. int i, rc;
  67. struct cam_node *node = NULL;
  68. struct cam_hw_dump_pf_args pf_args = {0};
  69. if (!pf_smmu_info || !pf_smmu_info->token) {
  70. CAM_ERR(CAM_ICP, "invalid token in page handler cb");
  71. return;
  72. }
  73. node = (struct cam_node *)pf_smmu_info->token;
  74. pf_args.pf_smmu_info = pf_smmu_info;
  75. for (i = 0; i < node->ctx_size; i++) {
  76. cam_context_dump_pf_info(&(node->ctx_list[i]), &pf_args);
  77. if (pf_args.pf_context_info.ctx_found)
  78. /* found ctx and packet of the faulted address */
  79. break;
  80. }
  81. if (i == node->ctx_size) {
  82. /* Faulted ctx not found. Report PF to userspace */
  83. rc = cam_context_send_pf_evt(NULL, &pf_args);
  84. if (rc)
  85. CAM_ERR(CAM_ICP,
  86. "Failed to notify PF event to userspace rc: %d", rc);
  87. }
  88. }
  89. static void cam_icp_dev_mini_dump_cb(void *priv, void *args)
  90. {
  91. struct cam_context *ctx = NULL;
  92. if (!priv || !args) {
  93. CAM_ERR(CAM_ICP, "Invalid param priv: %pK args %pK", priv, args);
  94. return;
  95. }
  96. ctx = (struct cam_context *)priv;
  97. cam_context_mini_dump_from_hw(ctx, args);
  98. }
  99. static int cam_icp_subdev_open(struct v4l2_subdev *sd,
  100. struct v4l2_subdev_fh *fh)
  101. {
  102. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  103. struct cam_node *node = v4l2_get_subdevdata(sd);
  104. int rc = 0;
  105. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_LOCK);
  106. mutex_lock(&g_icp_dev.icp_lock);
  107. if (g_icp_dev.open_cnt >= 1) {
  108. CAM_ERR(CAM_ICP, "ICP subdev is already opened");
  109. rc = -EALREADY;
  110. goto end;
  111. }
  112. if (!node) {
  113. CAM_ERR(CAM_ICP, "Invalid args");
  114. rc = -EINVAL;
  115. goto end;
  116. }
  117. hw_mgr_intf = &node->hw_mgr_intf;
  118. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  119. if (rc < 0) {
  120. CAM_ERR(CAM_ICP, "FW download failed");
  121. goto end;
  122. }
  123. g_icp_dev.open_cnt++;
  124. end:
  125. mutex_unlock(&g_icp_dev.icp_lock);
  126. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_UNLOCK);
  127. return rc;
  128. }
  129. static int cam_icp_subdev_close_internal(struct v4l2_subdev *sd,
  130. struct v4l2_subdev_fh *fh)
  131. {
  132. int rc = 0;
  133. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  134. struct cam_node *node = v4l2_get_subdevdata(sd);
  135. mutex_lock(&g_icp_dev.icp_lock);
  136. if (g_icp_dev.open_cnt <= 0) {
  137. CAM_DBG(CAM_ICP, "ICP subdev is already closed");
  138. goto end;
  139. }
  140. g_icp_dev.open_cnt--;
  141. if (!node) {
  142. CAM_ERR(CAM_ICP, "Invalid args");
  143. rc = -EINVAL;
  144. goto end;
  145. }
  146. hw_mgr_intf = &node->hw_mgr_intf;
  147. if (!hw_mgr_intf) {
  148. CAM_ERR(CAM_ICP, "hw_mgr_intf is not initialized");
  149. rc = -EINVAL;
  150. goto end;
  151. }
  152. rc = cam_node_shutdown(node);
  153. if (rc < 0) {
  154. CAM_ERR(CAM_ICP, "HW close failed");
  155. goto end;
  156. }
  157. end:
  158. mutex_unlock(&g_icp_dev.icp_lock);
  159. return rc;
  160. }
  161. static int cam_icp_subdev_close(struct v4l2_subdev *sd,
  162. struct v4l2_subdev_fh *fh)
  163. {
  164. bool crm_active = cam_req_mgr_is_open();
  165. if (crm_active) {
  166. CAM_DBG(CAM_ICP, "CRM is ACTIVE, close should be from CRM");
  167. return 0;
  168. }
  169. return cam_icp_subdev_close_internal(sd, fh);
  170. }
  171. const struct v4l2_subdev_internal_ops cam_icp_subdev_internal_ops = {
  172. .open = cam_icp_subdev_open,
  173. .close = cam_icp_subdev_close,
  174. };
  175. static int cam_icp_component_bind(struct device *dev,
  176. struct device *master_dev, void *data)
  177. {
  178. int rc = 0, i = 0;
  179. struct cam_node *node;
  180. struct cam_hw_mgr_intf *hw_mgr_intf;
  181. int iommu_hdl = -1;
  182. struct platform_device *pdev = to_platform_device(dev);
  183. if (!pdev) {
  184. CAM_ERR(CAM_ICP, "pdev is NULL");
  185. return -EINVAL;
  186. }
  187. g_icp_dev.sd.pdev = pdev;
  188. g_icp_dev.sd.internal_ops = &cam_icp_subdev_internal_ops;
  189. g_icp_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  190. rc = cam_subdev_probe(&g_icp_dev.sd, pdev, CAM_ICP_DEV_NAME,
  191. CAM_ICP_DEVICE_TYPE);
  192. if (rc) {
  193. CAM_ERR(CAM_ICP, "ICP cam_subdev_probe failed");
  194. goto probe_fail;
  195. }
  196. node = (struct cam_node *) g_icp_dev.sd.token;
  197. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  198. if (!hw_mgr_intf) {
  199. rc = -ENOMEM;
  200. CAM_ERR(CAM_ICP, "Memory allocation fail");
  201. goto hw_alloc_fail;
  202. }
  203. rc = cam_icp_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
  204. &iommu_hdl, cam_icp_dev_mini_dump_cb);
  205. if (rc) {
  206. CAM_ERR(CAM_ICP, "ICP HW manager init failed: %d", rc);
  207. goto hw_init_fail;
  208. }
  209. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  210. g_icp_dev.ctx_icp[i].base = &g_icp_dev.ctx[i];
  211. rc = cam_icp_context_init(&g_icp_dev.ctx_icp[i],
  212. hw_mgr_intf, i, iommu_hdl);
  213. if (rc) {
  214. CAM_ERR(CAM_ICP, "ICP context init failed");
  215. goto ctx_fail;
  216. }
  217. }
  218. rc = cam_node_init(node, hw_mgr_intf, g_icp_dev.ctx,
  219. CAM_ICP_CTX_MAX, CAM_ICP_DEV_NAME);
  220. if (rc) {
  221. CAM_ERR(CAM_ICP, "ICP node init failed");
  222. goto ctx_fail;
  223. }
  224. cam_common_register_evt_inject_cb(cam_icp_dev_evt_inject_cb,
  225. CAM_COMMON_EVT_INJECT_HW_ICP);
  226. node->sd_handler = cam_icp_subdev_close_internal;
  227. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  228. cam_icp_dev_iommu_fault_handler, node);
  229. g_icp_dev.open_cnt = 0;
  230. mutex_init(&g_icp_dev.icp_lock);
  231. CAM_DBG(CAM_ICP, "Component bound successfully");
  232. return rc;
  233. ctx_fail:
  234. for (--i; i >= 0; i--)
  235. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  236. hw_init_fail:
  237. kfree(hw_mgr_intf);
  238. hw_alloc_fail:
  239. cam_subdev_remove(&g_icp_dev.sd);
  240. probe_fail:
  241. return rc;
  242. }
  243. static void cam_icp_component_unbind(struct device *dev,
  244. struct device *master_dev, void *data)
  245. {
  246. int i;
  247. struct v4l2_subdev *sd;
  248. struct platform_device *pdev = to_platform_device(dev);
  249. if (!pdev) {
  250. CAM_ERR(CAM_ICP, "pdev is NULL");
  251. return;
  252. }
  253. sd = platform_get_drvdata(pdev);
  254. if (!sd) {
  255. CAM_ERR(CAM_ICP, "V4l2 subdev is NULL");
  256. return;
  257. }
  258. for (i = 0; i < CAM_ICP_CTX_MAX; i++)
  259. cam_icp_context_deinit(&g_icp_dev.ctx_icp[i]);
  260. cam_icp_hw_mgr_deinit();
  261. cam_node_deinit(g_icp_dev.node);
  262. cam_subdev_remove(&g_icp_dev.sd);
  263. mutex_destroy(&g_icp_dev.icp_lock);
  264. }
  265. const static struct component_ops cam_icp_component_ops = {
  266. .bind = cam_icp_component_bind,
  267. .unbind = cam_icp_component_unbind,
  268. };
  269. static int cam_icp_probe(struct platform_device *pdev)
  270. {
  271. int rc = 0;
  272. CAM_DBG(CAM_ICP, "Adding ICP component");
  273. rc = component_add(&pdev->dev, &cam_icp_component_ops);
  274. if (rc)
  275. CAM_ERR(CAM_ICP, "failed to add component rc: %d", rc);
  276. return rc;
  277. }
  278. static int cam_icp_remove(struct platform_device *pdev)
  279. {
  280. component_del(&pdev->dev, &cam_icp_component_ops);
  281. return 0;
  282. }
  283. struct platform_driver cam_icp_driver = {
  284. .probe = cam_icp_probe,
  285. .remove = cam_icp_remove,
  286. .driver = {
  287. .name = "cam_icp",
  288. .owner = THIS_MODULE,
  289. .of_match_table = cam_icp_dt_match,
  290. .suppress_bind_attrs = true,
  291. },
  292. };
  293. int cam_icp_init_module(void)
  294. {
  295. return platform_driver_register(&cam_icp_driver);
  296. }
  297. void cam_icp_exit_module(void)
  298. {
  299. platform_driver_unregister(&cam_icp_driver);
  300. }
  301. MODULE_DESCRIPTION("MSM ICP driver");
  302. MODULE_LICENSE("GPL v2");