cam_compat.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include <linux/of_address.h>
  7. #include "cam_compat.h"
  8. #include "cam_debug_util.h"
  9. #if KERNEL_VERSION(5, 4, 0) >= LINUX_VERSION_CODE
  10. int cam_reserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  11. {
  12. int rc = 0;
  13. struct device_node *of_node;
  14. struct device_node *mem_node;
  15. struct resource res;
  16. of_node = (icp_fw->fw_dev)->of_node;
  17. mem_node = of_parse_phandle(of_node, "memory-region", 0);
  18. if (!mem_node) {
  19. rc = -ENOMEM;
  20. CAM_ERR(CAM_SMMU, "FW memory carveout not found");
  21. goto end;
  22. }
  23. rc = of_address_to_resource(mem_node, 0, &res);
  24. of_node_put(mem_node);
  25. if (rc < 0) {
  26. CAM_ERR(CAM_SMMU, "Unable to get start of FW mem carveout");
  27. goto end;
  28. }
  29. icp_fw->fw_hdl = res.start;
  30. icp_fw->fw_kva = ioremap_wc(icp_fw->fw_hdl, fw_length);
  31. if (!icp_fw->fw_kva) {
  32. CAM_ERR(CAM_SMMU, "Failed to map the FW.");
  33. rc = -ENOMEM;
  34. goto end;
  35. }
  36. memset_io(icp_fw->fw_kva, 0, fw_length);
  37. end:
  38. return rc;
  39. }
  40. void cam_unreserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  41. {
  42. iounmap(icp_fw->fw_kva);
  43. }
  44. #else
  45. int cam_reserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  46. {
  47. int rc = 0;
  48. icp_fw->fw_kva = dma_alloc_coherent(icp_fw->fw_dev, fw_length,
  49. &icp_fw->fw_hdl, GFP_KERNEL);
  50. if (!icp_fw->fw_kva) {
  51. CAM_ERR(CAM_SMMU, "FW memory alloc failed");
  52. rc = -ENOMEM;
  53. }
  54. return rc;
  55. }
  56. void cam_unreserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  57. {
  58. dma_free_coherent(icp_fw->fw_dev, fw_length, icp_fw->fw_kva,
  59. icp_fw->fw_hdl);
  60. }
  61. #endif