cam_icp_subdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022,2024 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_IS_DEV_IDX_INVALID(dev_idx) \
  36. ({ \
  37. ((dev_idx) < 0) || ((dev_idx) >= CAM_ICP_SUBDEV_MAX); \
  38. })
  39. struct cam_icp_subdev {
  40. struct cam_subdev sd;
  41. struct cam_node *node;
  42. struct cam_context ctx[CAM_ICP_CTX_MAX];
  43. struct cam_icp_context ctx_icp[CAM_ICP_CTX_MAX];
  44. struct mutex icp_lock;
  45. int32_t open_cnt;
  46. int32_t reserved;
  47. };
  48. static DEFINE_MUTEX(g_dev_lock);
  49. static struct cam_icp_subdev *g_icp_dev[CAM_ICP_SUBDEV_MAX];
  50. static char cam_icp_subdev_name_arr[CAM_ICP_SUBDEV_MAX][CAM_ICP_SUBDEV_NAME_LEN] = {
  51. "cam-icp0",
  52. "cam-icp1",
  53. };
  54. static const struct of_device_id cam_icp_dt_match[] = {
  55. {.compatible = "qcom,cam-icp"},
  56. {.compatible = "qcom,cam-icp0"},
  57. {.compatible = "qcom,cam-icp1"},
  58. {}
  59. };
  60. static int cam_icp_dev_evt_inject_cb(void *inject_args)
  61. {
  62. struct cam_common_inject_evt_param *inject_params = inject_args;
  63. struct cam_icp_subdev *icp_dev;
  64. int i;
  65. /* Event Injection currently supported for only a single instance of ICP */
  66. icp_dev = g_icp_dev[0];
  67. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  68. if (icp_dev->ctx[i].dev_hdl == inject_params->dev_hdl) {
  69. cam_context_add_evt_inject(&icp_dev->ctx[i],
  70. &inject_params->evt_params);
  71. return 0;
  72. }
  73. }
  74. CAM_ERR(CAM_ICP, "No dev hdl found %d", inject_params->dev_hdl);
  75. return -ENODEV;
  76. }
  77. static void cam_icp_dev_iommu_fault_handler(struct cam_smmu_pf_info *pf_smmu_info)
  78. {
  79. int i, rc;
  80. struct cam_node *node = NULL;
  81. struct cam_hw_dump_pf_args pf_args = {0};
  82. if (!pf_smmu_info || !pf_smmu_info->token) {
  83. CAM_ERR(CAM_ICP, "invalid token in page handler cb");
  84. return;
  85. }
  86. node = (struct cam_node *)pf_smmu_info->token;
  87. pf_args.pf_smmu_info = pf_smmu_info;
  88. for (i = 0; i < node->ctx_size; i++) {
  89. cam_context_dump_pf_info(&(node->ctx_list[i]), &pf_args);
  90. if (pf_args.pf_context_info.ctx_found)
  91. /* found ctx and packet of the faulted address */
  92. break;
  93. }
  94. if (i == node->ctx_size) {
  95. /* Faulted ctx not found. Report PF to userspace */
  96. rc = cam_context_send_pf_evt(NULL, &pf_args);
  97. if (rc)
  98. CAM_ERR(CAM_ICP,
  99. "Failed to notify PF event to userspace rc: %d", rc);
  100. }
  101. }
  102. static void cam_icp_dev_mini_dump_cb(void *priv, void *args)
  103. {
  104. struct cam_context *ctx = NULL;
  105. if (!priv || !args) {
  106. CAM_ERR(CAM_ICP, "Invalid params: priv: %pK args %pK", priv, args);
  107. return;
  108. }
  109. ctx = (struct cam_context *)priv;
  110. cam_context_mini_dump_from_hw(ctx, args);
  111. }
  112. static int cam_icp_subdev_open(struct v4l2_subdev *sd,
  113. struct v4l2_subdev_fh *fh)
  114. {
  115. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  116. struct cam_node *node = v4l2_get_subdevdata(sd);
  117. struct cam_icp_subdev *icp_dev = NULL;
  118. int rc = 0;
  119. if (!node) {
  120. CAM_ERR(CAM_ICP, "Invalid params: Node is NULL");
  121. return -EINVAL;
  122. }
  123. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_LOCK);
  124. CAM_DBG(CAM_ICP, "Enter device open for %s[%u]",
  125. sd->name, node->device_idx);
  126. if (CAM_ICP_IS_DEV_IDX_INVALID(node->device_idx)) {
  127. CAM_ERR(CAM_ICP, "Invalid device idx: %u for device: %s",
  128. node->device_idx, sd->name);
  129. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_UNLOCK);
  130. return -ENODEV;
  131. }
  132. icp_dev = g_icp_dev[node->device_idx];
  133. mutex_lock(&icp_dev->icp_lock);
  134. if (icp_dev->open_cnt >= 1) {
  135. CAM_ERR(CAM_ICP, "device[%s] is already opened, open count: %u",
  136. sd->name, icp_dev->open_cnt);
  137. rc = -EALREADY;
  138. goto end;
  139. }
  140. hw_mgr_intf = &node->hw_mgr_intf;
  141. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  142. if (rc < 0) {
  143. CAM_ERR(CAM_ICP, "FW download failed for device [%s]", sd->name);
  144. goto end;
  145. }
  146. icp_dev->open_cnt++;
  147. end:
  148. mutex_unlock(&icp_dev->icp_lock);
  149. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_UNLOCK);
  150. return rc;
  151. }
  152. static int cam_icp_subdev_close_internal(struct v4l2_subdev *sd,
  153. struct v4l2_subdev_fh *fh)
  154. {
  155. int rc = 0;
  156. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  157. struct cam_node *node = v4l2_get_subdevdata(sd);
  158. struct cam_icp_subdev *icp_dev = NULL;
  159. if (!node) {
  160. CAM_ERR(CAM_ICP, "device[%s] Invalid params: node is NULL",
  161. sd->name);
  162. rc = -EINVAL;
  163. goto end;
  164. }
  165. if (CAM_ICP_IS_DEV_IDX_INVALID(node->device_idx)) {
  166. CAM_ERR(CAM_ICP, "Invalid device idx: %u for device: %s",
  167. node->device_idx, node->name);
  168. return -ENODEV;
  169. }
  170. icp_dev = g_icp_dev[node->device_idx];
  171. mutex_lock(&icp_dev->icp_lock);
  172. if (icp_dev->open_cnt <= 0) {
  173. CAM_DBG(CAM_ICP, "device[%s] is already closed",
  174. sd->name);
  175. goto end;
  176. }
  177. icp_dev->open_cnt--;
  178. hw_mgr_intf = &node->hw_mgr_intf;
  179. if (!hw_mgr_intf) {
  180. CAM_ERR(CAM_ICP, "device[%s] hw_mgr_intf is not initialized",
  181. sd->name);
  182. rc = -EINVAL;
  183. goto end;
  184. }
  185. rc = cam_node_shutdown(node);
  186. if (rc < 0) {
  187. CAM_ERR(CAM_ICP, "device[%s] HW close failed",
  188. sd->name);
  189. goto end;
  190. }
  191. end:
  192. mutex_unlock(&icp_dev->icp_lock);
  193. return rc;
  194. }
  195. static int cam_icp_subdev_close(struct v4l2_subdev *sd,
  196. struct v4l2_subdev_fh *fh)
  197. {
  198. bool crm_active = cam_req_mgr_is_open();
  199. if (crm_active) {
  200. CAM_DBG(CAM_ICP,
  201. "CRM is ACTIVE, close should be from CRM for device[%s]",
  202. sd->name);
  203. return 0;
  204. }
  205. return cam_icp_subdev_close_internal(sd, fh);
  206. }
  207. const struct v4l2_subdev_internal_ops cam_icp_subdev_internal_ops = {
  208. .open = cam_icp_subdev_open,
  209. .close = cam_icp_subdev_close,
  210. };
  211. static inline int cam_icp_subdev_clean_up(uint32_t device_idx)
  212. {
  213. kfree(g_icp_dev[device_idx]);
  214. g_icp_dev[device_idx] = NULL;
  215. return 0;
  216. }
  217. static int cam_icp_component_bind(struct device *dev,
  218. struct device *master_dev, void *data)
  219. {
  220. int rc = 0, i = 0;
  221. struct cam_node *node;
  222. struct cam_hw_mgr_intf hw_mgr_intf;
  223. int iommu_hdl = -1;
  224. struct platform_device *pdev = to_platform_device(dev);
  225. struct cam_icp_subdev *icp_dev;
  226. char *subdev_name;
  227. uint32_t device_idx;
  228. if (!pdev) {
  229. CAM_ERR(CAM_ICP, "Invalid params: pdev is %s",
  230. CAM_IS_NULL_TO_STR(pdev));
  231. return -EINVAL;
  232. }
  233. rc = of_property_read_u32(dev->of_node, "cell-index", &device_idx);
  234. if (rc)
  235. device_idx = 0;
  236. if (CAM_ICP_IS_DEV_IDX_INVALID(device_idx)) {
  237. CAM_ERR(CAM_ICP, "Invalid device idx: %u exceeds subdev max: %u",
  238. device_idx, CAM_ICP_SUBDEV_MAX);
  239. return -EINVAL;
  240. }
  241. /*
  242. * For targets where only one subdevice exists, cell-index property is not listed
  243. * in the DT node, so the default name and device index are "cam-icp" and 0 respectively
  244. */
  245. if (rc)
  246. subdev_name = "cam-icp";
  247. else
  248. subdev_name = cam_icp_subdev_name_arr[device_idx];
  249. mutex_lock(&g_dev_lock);
  250. if (g_icp_dev[device_idx]) {
  251. CAM_ERR(CAM_ICP,
  252. "Invalid device index: %u for pdev: %s, ICP device for this idx is already bound",
  253. device_idx, pdev->name);
  254. rc = -EBADSLT;
  255. mutex_unlock(&g_dev_lock);
  256. goto probe_fail;
  257. }
  258. mutex_unlock(&g_dev_lock);
  259. icp_dev = kzalloc(sizeof(struct cam_icp_subdev), GFP_KERNEL);
  260. if (!icp_dev) {
  261. CAM_ERR(CAM_ICP,
  262. "Unable to allocate memory for icp device:%s size:%llu",
  263. pdev->name, sizeof(struct cam_icp_subdev));
  264. return -ENOMEM;
  265. }
  266. mutex_lock(&g_dev_lock);
  267. g_icp_dev[device_idx] = icp_dev;
  268. mutex_unlock(&g_dev_lock);
  269. icp_dev->sd.internal_ops = &cam_icp_subdev_internal_ops;
  270. icp_dev->sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  271. rc = cam_subdev_probe(&icp_dev->sd, pdev, subdev_name,
  272. CAM_ICP_DEVICE_TYPE);
  273. if (rc) {
  274. CAM_ERR(CAM_ICP, "device[%s] probe failed", subdev_name);
  275. goto probe_fail;
  276. }
  277. node = (struct cam_node *) icp_dev->sd.token;
  278. node->sd_handler = cam_icp_subdev_close_internal;
  279. node->device_idx = device_idx;
  280. mutex_init(&icp_dev->icp_lock);
  281. rc = cam_icp_hw_mgr_init(pdev->dev.of_node, (uint64_t *)(&hw_mgr_intf),
  282. &iommu_hdl, cam_icp_dev_mini_dump_cb, device_idx);
  283. if (rc) {
  284. CAM_ERR(CAM_ICP, "device[%s] HW manager init failed: %d", subdev_name, rc);
  285. goto hw_init_fail;
  286. }
  287. for (i = 0; i < CAM_ICP_CTX_MAX; i++) {
  288. icp_dev->ctx_icp[i].base = &icp_dev->ctx[i];
  289. rc = cam_icp_context_init(&icp_dev->ctx_icp[i],
  290. &node->hw_mgr_intf, i, iommu_hdl, subdev_name);
  291. if (rc) {
  292. CAM_ERR(CAM_ICP, "device[%s] context init failed", subdev_name);
  293. goto ctx_fail;
  294. }
  295. }
  296. rc = cam_node_init(node, &hw_mgr_intf, icp_dev->ctx,
  297. CAM_ICP_CTX_MAX, subdev_name);
  298. if (rc) {
  299. CAM_ERR(CAM_ICP, "device[%s] node init failed", subdev_name);
  300. goto ctx_fail;
  301. }
  302. cam_common_register_evt_inject_cb(cam_icp_dev_evt_inject_cb,
  303. CAM_COMMON_EVT_INJECT_HW_ICP);
  304. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  305. cam_icp_dev_iommu_fault_handler, node);
  306. icp_dev->open_cnt = 0;
  307. CAM_DBG(CAM_ICP, "device[%s] id: %u component bound successfully",
  308. subdev_name, device_idx);
  309. return rc;
  310. ctx_fail:
  311. for (--i; i >= 0; i--)
  312. cam_icp_context_deinit(&icp_dev->ctx_icp[i]);
  313. cam_icp_hw_mgr_deinit(device_idx);
  314. hw_init_fail:
  315. cam_node_deinit(icp_dev->node);
  316. cam_subdev_remove(&icp_dev->sd);
  317. probe_fail:
  318. cam_icp_subdev_clean_up(device_idx);
  319. return rc;
  320. }
  321. static void cam_icp_component_unbind(struct device *dev,
  322. struct device *master_dev, void *data)
  323. {
  324. int i, rc;
  325. struct platform_device *pdev = to_platform_device(dev);
  326. struct v4l2_subdev *sd;
  327. struct cam_icp_subdev *icp_dev;
  328. uint32_t device_idx;
  329. if (!pdev) {
  330. CAM_ERR(CAM_ICP, "pdev is NULL");
  331. return;
  332. }
  333. sd = platform_get_drvdata(pdev);
  334. if (!sd) {
  335. CAM_ERR(CAM_ICP,
  336. "V4l2 subdev is NULL for pdev: %s", pdev->name);
  337. return;
  338. }
  339. rc = of_property_read_u32(dev->of_node, "cell-index", &device_idx);
  340. if (rc)
  341. device_idx = 0;
  342. if (CAM_ICP_IS_DEV_IDX_INVALID(device_idx)) {
  343. CAM_ERR(CAM_ICP, "Invalid device idx: %u exceeds subdev max: %u",
  344. device_idx, CAM_ICP_SUBDEV_MAX);
  345. return;
  346. }
  347. icp_dev = g_icp_dev[device_idx];
  348. for (i = 0; i < CAM_ICP_CTX_MAX; i++)
  349. cam_icp_context_deinit(&icp_dev->ctx_icp[i]);
  350. cam_icp_hw_mgr_deinit(device_idx);
  351. cam_node_deinit(icp_dev->node);
  352. cam_subdev_remove(&icp_dev->sd);
  353. mutex_destroy(&icp_dev->icp_lock);
  354. cam_icp_subdev_clean_up(device_idx);
  355. CAM_DBG(CAM_ICP, "device[%s] component unbinded successfully", pdev->name);
  356. }
  357. const static struct component_ops cam_icp_component_ops = {
  358. .bind = cam_icp_component_bind,
  359. .unbind = cam_icp_component_unbind,
  360. };
  361. static int cam_icp_probe(struct platform_device *pdev)
  362. {
  363. int rc = 0;
  364. CAM_DBG(CAM_ICP, "%s Adding ICP component", pdev->name);
  365. rc = component_add(&pdev->dev, &cam_icp_component_ops);
  366. if (rc)
  367. CAM_ERR(CAM_ICP, "%s failed to add component rc: %d", pdev->name, rc);
  368. return rc;
  369. }
  370. static int cam_icp_remove(struct platform_device *pdev)
  371. {
  372. component_del(&pdev->dev, &cam_icp_component_ops);
  373. return 0;
  374. }
  375. struct platform_driver cam_icp_driver = {
  376. .probe = cam_icp_probe,
  377. .remove = cam_icp_remove,
  378. .driver = {
  379. .name = "cam_icp",
  380. .owner = THIS_MODULE,
  381. .of_match_table = cam_icp_dt_match,
  382. .suppress_bind_attrs = true,
  383. },
  384. };
  385. int cam_icp_init_module(void)
  386. {
  387. return platform_driver_register(&cam_icp_driver);
  388. }
  389. void cam_icp_exit_module(void)
  390. {
  391. platform_driver_unregister(&cam_icp_driver);
  392. }
  393. MODULE_DESCRIPTION("MSM ICP driver");
  394. MODULE_LICENSE("GPL v2");