cam_compat.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include <linux/of_address.h>
  7. #include <linux/slab.h>
  8. #include "cam_compat.h"
  9. #include "cam_debug_util.h"
  10. #include "cam_cpas_api.h"
  11. #include "camera_main.h"
  12. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
  13. int cam_reserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  14. {
  15. int rc = 0;
  16. struct device_node *of_node;
  17. struct device_node *mem_node;
  18. struct resource res;
  19. of_node = (icp_fw->fw_dev)->of_node;
  20. mem_node = of_parse_phandle(of_node, "memory-region", 0);
  21. if (!mem_node) {
  22. rc = -ENOMEM;
  23. CAM_ERR(CAM_SMMU, "FW memory carveout not found");
  24. goto end;
  25. }
  26. rc = of_address_to_resource(mem_node, 0, &res);
  27. of_node_put(mem_node);
  28. if (rc < 0) {
  29. CAM_ERR(CAM_SMMU, "Unable to get start of FW mem carveout");
  30. goto end;
  31. }
  32. icp_fw->fw_hdl = res.start;
  33. icp_fw->fw_kva = ioremap_wc(icp_fw->fw_hdl, fw_length);
  34. if (!icp_fw->fw_kva) {
  35. CAM_ERR(CAM_SMMU, "Failed to map the FW.");
  36. rc = -ENOMEM;
  37. goto end;
  38. }
  39. memset_io(icp_fw->fw_kva, 0, fw_length);
  40. end:
  41. return rc;
  42. }
  43. void cam_unreserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  44. {
  45. iounmap(icp_fw->fw_kva);
  46. }
  47. int cam_ife_notify_safe_lut_scm(bool safe_trigger)
  48. {
  49. const uint32_t smmu_se_ife = 0;
  50. uint32_t camera_hw_version, rc = 0;
  51. rc = cam_cpas_get_cpas_hw_version(&camera_hw_version);
  52. if (!rc && qcom_scm_smmu_notify_secure_lut(smmu_se_ife, safe_trigger)) {
  53. switch (camera_hw_version) {
  54. case CAM_CPAS_TITAN_170_V100:
  55. case CAM_CPAS_TITAN_170_V110:
  56. case CAM_CPAS_TITAN_175_V100:
  57. CAM_ERR(CAM_ISP, "scm call to enable safe failed");
  58. rc = -EINVAL;
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. return rc;
  65. }
  66. int cam_csiphy_notify_secure_mode(struct csiphy_device *csiphy_dev,
  67. bool protect, int32_t offset)
  68. {
  69. int rc = 0;
  70. if (offset >= CSIPHY_MAX_INSTANCES_PER_PHY) {
  71. CAM_ERR(CAM_CSIPHY, "Invalid CSIPHY offset");
  72. rc = -EINVAL;
  73. } else if (qcom_scm_camera_protect_phy_lanes(protect,
  74. csiphy_dev->csiphy_info[offset]
  75. .csiphy_cpas_cp_reg_mask)) {
  76. CAM_ERR(CAM_CSIPHY, "SCM call to hypervisor failed");
  77. rc = -EINVAL;
  78. }
  79. return rc;
  80. }
  81. void cam_cpastop_scm_write(struct cam_cpas_hw_errata_wa *errata_wa)
  82. {
  83. int reg_val;
  84. qcom_scm_io_readl(errata_wa->data.reg_info.offset, &reg_val);
  85. reg_val |= errata_wa->data.reg_info.value;
  86. qcom_scm_io_writel(errata_wa->data.reg_info.offset, reg_val);
  87. }
  88. static int camera_platform_compare_dev(struct device *dev, const void *data)
  89. {
  90. return platform_bus_type.match(dev, (struct device_driver *) data);
  91. }
  92. #else
  93. int cam_reserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  94. {
  95. int rc = 0;
  96. icp_fw->fw_kva = dma_alloc_coherent(icp_fw->fw_dev, fw_length,
  97. &icp_fw->fw_hdl, GFP_KERNEL);
  98. if (!icp_fw->fw_kva) {
  99. CAM_ERR(CAM_SMMU, "FW memory alloc failed");
  100. rc = -ENOMEM;
  101. }
  102. return rc;
  103. }
  104. void cam_unreserve_icp_fw(struct cam_fw_alloc_info *icp_fw, size_t fw_length)
  105. {
  106. dma_free_coherent(icp_fw->fw_dev, fw_length, icp_fw->fw_kva,
  107. icp_fw->fw_hdl);
  108. }
  109. int cam_ife_notify_safe_lut_scm(bool safe_trigger)
  110. {
  111. const uint32_t smmu_se_ife = 0;
  112. uint32_t camera_hw_version, rc = 0;
  113. struct scm_desc description = {
  114. .arginfo = SCM_ARGS(2, SCM_VAL, SCM_VAL),
  115. .args[0] = smmu_se_ife,
  116. .args[1] = safe_trigger,
  117. };
  118. rc = cam_cpas_get_cpas_hw_version(&camera_hw_version);
  119. if (!rc && scm_call2(SCM_SIP_FNID(0x15, 0x3), &description)) {
  120. switch (camera_hw_version) {
  121. case CAM_CPAS_TITAN_170_V100:
  122. case CAM_CPAS_TITAN_170_V110:
  123. case CAM_CPAS_TITAN_175_V100:
  124. CAM_ERR(CAM_ISP, "scm call to enable safe failed");
  125. rc = -EINVAL;
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. return rc;
  132. }
  133. int cam_csiphy_notify_secure_mode(struct csiphy_device *csiphy_dev,
  134. bool protect, int32_t offset)
  135. {
  136. int rc = 0;
  137. struct scm_desc description = {
  138. .arginfo = SCM_ARGS(2, SCM_VAL, SCM_VAL),
  139. .args[0] = protect,
  140. .args[1] = csiphy_dev->csiphy_info[offset]
  141. .csiphy_cpas_cp_reg_mask,
  142. };
  143. if (offset >= CSIPHY_MAX_INSTANCES_PER_PHY) {
  144. CAM_ERR(CAM_CSIPHY, "Invalid CSIPHY offset");
  145. rc = -EINVAL;
  146. } else if (scm_call2(SCM_SIP_FNID(0x18, 0x7), &description)) {
  147. CAM_ERR(CAM_CSIPHY, "SCM call to hypervisor failed");
  148. rc = -EINVAL;
  149. }
  150. return rc;
  151. }
  152. void cam_cpastop_scm_write(struct cam_cpas_hw_errata_wa *errata_wa)
  153. {
  154. int reg_val;
  155. reg_val = scm_io_read(errata_wa->data.reg_info.offset);
  156. reg_val |= errata_wa->data.reg_info.value;
  157. scm_io_write(errata_wa->data.reg_info.offset, reg_val);
  158. }
  159. static int camera_platform_compare_dev(struct device *dev, void *data)
  160. {
  161. return platform_bus_type.match(dev, (struct device_driver *) data);
  162. }
  163. #endif
  164. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
  165. void cam_free_clear(const void * ptr)
  166. {
  167. kfree_sensitive(ptr);
  168. }
  169. #else
  170. void cam_free_clear(const void * ptr)
  171. {
  172. kzfree(ptr);
  173. }
  174. #endif
  175. /* Callback to compare device from match list before adding as component */
  176. static inline int camera_component_compare_dev(struct device *dev, void *data)
  177. {
  178. return dev == data;
  179. }
  180. /* Add component matches to list for master of aggregate driver */
  181. int camera_component_match_add_drivers(struct device *master_dev,
  182. struct component_match **match_list)
  183. {
  184. int i, rc = 0;
  185. struct platform_device *pdev = NULL;
  186. struct device *start_dev = NULL, *match_dev = NULL;
  187. if (!master_dev || !match_list) {
  188. CAM_ERR(CAM_UTIL, "Invalid parameters for component match add");
  189. rc = -EINVAL;
  190. goto end;
  191. }
  192. for (i = 0; i < ARRAY_SIZE(cam_component_drivers); i++) {
  193. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
  194. struct device_driver const *drv =
  195. &cam_component_drivers[i]->driver;
  196. const void *drv_ptr = (const void *)drv;
  197. #else
  198. struct device_driver *drv = &cam_component_drivers[i]->driver;
  199. void *drv_ptr = (void *)drv;
  200. #endif
  201. start_dev = NULL;
  202. while ((match_dev = bus_find_device(&platform_bus_type,
  203. start_dev, drv_ptr, &camera_platform_compare_dev))) {
  204. put_device(start_dev);
  205. pdev = to_platform_device(match_dev);
  206. CAM_DBG(CAM_UTIL, "Adding matched component:%s", pdev->name);
  207. component_match_add(master_dev, match_list,
  208. camera_component_compare_dev, match_dev);
  209. start_dev = match_dev;
  210. }
  211. put_device(start_dev);
  212. }
  213. end:
  214. return rc;
  215. }
  216. #if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE
  217. #include <linux/qcom-iommu-util.h>
  218. void cam_check_iommu_faults(struct iommu_domain *domain,
  219. struct cam_smmu_pf_info *pf_info)
  220. {
  221. struct qcom_iommu_fault_ids fault_ids = {0, 0, 0};
  222. if (qcom_iommu_get_fault_ids(domain, &fault_ids))
  223. CAM_ERR(CAM_SMMU, "Cannot get smmu fault ids");
  224. else
  225. CAM_ERR(CAM_SMMU, "smmu fault ids bid:%d pid:%d mid:%d",
  226. fault_ids.bid, fault_ids.pid, fault_ids.mid);
  227. pf_info->bid = fault_ids.bid;
  228. pf_info->pid = fault_ids.pid;
  229. pf_info->mid = fault_ids.mid;
  230. }
  231. #else
  232. void cam_check_iommu_faults(struct iommu_domain *domain,
  233. struct cam_smmu_pf_info *pf_info)
  234. {
  235. struct iommu_fault_ids fault_ids = {0, 0, 0};
  236. if (iommu_get_fault_ids(domain, &fault_ids))
  237. CAM_ERR(CAM_SMMU, "Error: Can not get smmu fault ids");
  238. CAM_ERR(CAM_SMMU, "smmu fault ids bid:%d pid:%d mid:%d",
  239. fault_ids.bid, fault_ids.pid, fault_ids.mid);
  240. pf_info->bid = fault_ids.bid;
  241. pf_info->pid = fault_ids.pid;
  242. pf_info->mid = fault_ids.mid;
  243. }
  244. #endif