qcom_scm.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* Copyright (c) 2010,2015, The Linux Foundation. All rights reserved.
  2. * Copyright (C) 2015 Linaro Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/module.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/export.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/types.h>
  20. #include <linux/qcom_scm.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/clk.h>
  24. #include <linux/reset-controller.h>
  25. #include "qcom_scm.h"
  26. struct qcom_scm {
  27. struct device *dev;
  28. struct clk *core_clk;
  29. struct clk *iface_clk;
  30. struct clk *bus_clk;
  31. struct reset_controller_dev reset;
  32. };
  33. static struct qcom_scm *__scm;
  34. static int qcom_scm_clk_enable(void)
  35. {
  36. int ret;
  37. ret = clk_prepare_enable(__scm->core_clk);
  38. if (ret)
  39. goto bail;
  40. ret = clk_prepare_enable(__scm->iface_clk);
  41. if (ret)
  42. goto disable_core;
  43. ret = clk_prepare_enable(__scm->bus_clk);
  44. if (ret)
  45. goto disable_iface;
  46. return 0;
  47. disable_iface:
  48. clk_disable_unprepare(__scm->iface_clk);
  49. disable_core:
  50. clk_disable_unprepare(__scm->core_clk);
  51. bail:
  52. return ret;
  53. }
  54. static void qcom_scm_clk_disable(void)
  55. {
  56. clk_disable_unprepare(__scm->core_clk);
  57. clk_disable_unprepare(__scm->iface_clk);
  58. clk_disable_unprepare(__scm->bus_clk);
  59. }
  60. /**
  61. * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
  62. * @entry: Entry point function for the cpus
  63. * @cpus: The cpumask of cpus that will use the entry point
  64. *
  65. * Set the cold boot address of the cpus. Any cpu outside the supported
  66. * range would be removed from the cpu present mask.
  67. */
  68. int qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
  69. {
  70. return __qcom_scm_set_cold_boot_addr(entry, cpus);
  71. }
  72. EXPORT_SYMBOL(qcom_scm_set_cold_boot_addr);
  73. /**
  74. * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
  75. * @entry: Entry point function for the cpus
  76. * @cpus: The cpumask of cpus that will use the entry point
  77. *
  78. * Set the Linux entry point for the SCM to transfer control to when coming
  79. * out of a power down. CPU power down may be executed on cpuidle or hotplug.
  80. */
  81. int qcom_scm_set_warm_boot_addr(void *entry, const cpumask_t *cpus)
  82. {
  83. return __qcom_scm_set_warm_boot_addr(__scm->dev, entry, cpus);
  84. }
  85. EXPORT_SYMBOL(qcom_scm_set_warm_boot_addr);
  86. /**
  87. * qcom_scm_cpu_power_down() - Power down the cpu
  88. * @flags - Flags to flush cache
  89. *
  90. * This is an end point to power down cpu. If there was a pending interrupt,
  91. * the control would return from this function, otherwise, the cpu jumps to the
  92. * warm boot entry point set for this cpu upon reset.
  93. */
  94. void qcom_scm_cpu_power_down(u32 flags)
  95. {
  96. __qcom_scm_cpu_power_down(flags);
  97. }
  98. EXPORT_SYMBOL(qcom_scm_cpu_power_down);
  99. /**
  100. * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
  101. *
  102. * Return true if HDCP is supported, false if not.
  103. */
  104. bool qcom_scm_hdcp_available(void)
  105. {
  106. int ret = qcom_scm_clk_enable();
  107. if (ret)
  108. return ret;
  109. ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
  110. QCOM_SCM_CMD_HDCP);
  111. qcom_scm_clk_disable();
  112. return ret > 0 ? true : false;
  113. }
  114. EXPORT_SYMBOL(qcom_scm_hdcp_available);
  115. /**
  116. * qcom_scm_hdcp_req() - Send HDCP request.
  117. * @req: HDCP request array
  118. * @req_cnt: HDCP request array count
  119. * @resp: response buffer passed to SCM
  120. *
  121. * Write HDCP register(s) through SCM.
  122. */
  123. int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
  124. {
  125. int ret = qcom_scm_clk_enable();
  126. if (ret)
  127. return ret;
  128. ret = __qcom_scm_hdcp_req(__scm->dev, req, req_cnt, resp);
  129. qcom_scm_clk_disable();
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(qcom_scm_hdcp_req);
  133. /**
  134. * qcom_scm_pas_supported() - Check if the peripheral authentication service is
  135. * available for the given peripherial
  136. * @peripheral: peripheral id
  137. *
  138. * Returns true if PAS is supported for this peripheral, otherwise false.
  139. */
  140. bool qcom_scm_pas_supported(u32 peripheral)
  141. {
  142. int ret;
  143. ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
  144. QCOM_SCM_PAS_IS_SUPPORTED_CMD);
  145. if (ret <= 0)
  146. return false;
  147. return __qcom_scm_pas_supported(__scm->dev, peripheral);
  148. }
  149. EXPORT_SYMBOL(qcom_scm_pas_supported);
  150. /**
  151. * qcom_scm_pas_init_image() - Initialize peripheral authentication service
  152. * state machine for a given peripheral, using the
  153. * metadata
  154. * @peripheral: peripheral id
  155. * @metadata: pointer to memory containing ELF header, program header table
  156. * and optional blob of data used for authenticating the metadata
  157. * and the rest of the firmware
  158. * @size: size of the metadata
  159. *
  160. * Returns 0 on success.
  161. */
  162. int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size)
  163. {
  164. dma_addr_t mdata_phys;
  165. void *mdata_buf;
  166. int ret;
  167. /*
  168. * During the scm call memory protection will be enabled for the meta
  169. * data blob, so make sure it's physically contiguous, 4K aligned and
  170. * non-cachable to avoid XPU violations.
  171. */
  172. mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
  173. GFP_KERNEL);
  174. if (!mdata_buf) {
  175. dev_err(__scm->dev, "Allocation of metadata buffer failed.\n");
  176. return -ENOMEM;
  177. }
  178. memcpy(mdata_buf, metadata, size);
  179. ret = qcom_scm_clk_enable();
  180. if (ret)
  181. goto free_metadata;
  182. ret = __qcom_scm_pas_init_image(__scm->dev, peripheral, mdata_phys);
  183. qcom_scm_clk_disable();
  184. free_metadata:
  185. dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(qcom_scm_pas_init_image);
  189. /**
  190. * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
  191. * for firmware loading
  192. * @peripheral: peripheral id
  193. * @addr: start address of memory area to prepare
  194. * @size: size of the memory area to prepare
  195. *
  196. * Returns 0 on success.
  197. */
  198. int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr, phys_addr_t size)
  199. {
  200. int ret;
  201. ret = qcom_scm_clk_enable();
  202. if (ret)
  203. return ret;
  204. ret = __qcom_scm_pas_mem_setup(__scm->dev, peripheral, addr, size);
  205. qcom_scm_clk_disable();
  206. return ret;
  207. }
  208. EXPORT_SYMBOL(qcom_scm_pas_mem_setup);
  209. /**
  210. * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
  211. * and reset the remote processor
  212. * @peripheral: peripheral id
  213. *
  214. * Return 0 on success.
  215. */
  216. int qcom_scm_pas_auth_and_reset(u32 peripheral)
  217. {
  218. int ret;
  219. ret = qcom_scm_clk_enable();
  220. if (ret)
  221. return ret;
  222. ret = __qcom_scm_pas_auth_and_reset(__scm->dev, peripheral);
  223. qcom_scm_clk_disable();
  224. return ret;
  225. }
  226. EXPORT_SYMBOL(qcom_scm_pas_auth_and_reset);
  227. /**
  228. * qcom_scm_pas_shutdown() - Shut down the remote processor
  229. * @peripheral: peripheral id
  230. *
  231. * Returns 0 on success.
  232. */
  233. int qcom_scm_pas_shutdown(u32 peripheral)
  234. {
  235. int ret;
  236. ret = qcom_scm_clk_enable();
  237. if (ret)
  238. return ret;
  239. ret = __qcom_scm_pas_shutdown(__scm->dev, peripheral);
  240. qcom_scm_clk_disable();
  241. return ret;
  242. }
  243. EXPORT_SYMBOL(qcom_scm_pas_shutdown);
  244. static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
  245. unsigned long idx)
  246. {
  247. if (idx != 0)
  248. return -EINVAL;
  249. return __qcom_scm_pas_mss_reset(__scm->dev, 1);
  250. }
  251. static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
  252. unsigned long idx)
  253. {
  254. if (idx != 0)
  255. return -EINVAL;
  256. return __qcom_scm_pas_mss_reset(__scm->dev, 0);
  257. }
  258. static const struct reset_control_ops qcom_scm_pas_reset_ops = {
  259. .assert = qcom_scm_pas_reset_assert,
  260. .deassert = qcom_scm_pas_reset_deassert,
  261. };
  262. /**
  263. * qcom_scm_is_available() - Checks if SCM is available
  264. */
  265. bool qcom_scm_is_available(void)
  266. {
  267. return !!__scm;
  268. }
  269. EXPORT_SYMBOL(qcom_scm_is_available);
  270. static int qcom_scm_probe(struct platform_device *pdev)
  271. {
  272. struct qcom_scm *scm;
  273. int ret;
  274. scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
  275. if (!scm)
  276. return -ENOMEM;
  277. scm->core_clk = devm_clk_get(&pdev->dev, "core");
  278. if (IS_ERR(scm->core_clk)) {
  279. if (PTR_ERR(scm->core_clk) == -EPROBE_DEFER)
  280. return PTR_ERR(scm->core_clk);
  281. scm->core_clk = NULL;
  282. }
  283. if (of_device_is_compatible(pdev->dev.of_node, "qcom,scm")) {
  284. scm->iface_clk = devm_clk_get(&pdev->dev, "iface");
  285. if (IS_ERR(scm->iface_clk)) {
  286. if (PTR_ERR(scm->iface_clk) != -EPROBE_DEFER)
  287. dev_err(&pdev->dev, "failed to acquire iface clk\n");
  288. return PTR_ERR(scm->iface_clk);
  289. }
  290. scm->bus_clk = devm_clk_get(&pdev->dev, "bus");
  291. if (IS_ERR(scm->bus_clk)) {
  292. if (PTR_ERR(scm->bus_clk) != -EPROBE_DEFER)
  293. dev_err(&pdev->dev, "failed to acquire bus clk\n");
  294. return PTR_ERR(scm->bus_clk);
  295. }
  296. }
  297. scm->reset.ops = &qcom_scm_pas_reset_ops;
  298. scm->reset.nr_resets = 1;
  299. scm->reset.of_node = pdev->dev.of_node;
  300. reset_controller_register(&scm->reset);
  301. /* vote for max clk rate for highest performance */
  302. ret = clk_set_rate(scm->core_clk, INT_MAX);
  303. if (ret)
  304. return ret;
  305. __scm = scm;
  306. __scm->dev = &pdev->dev;
  307. __qcom_scm_init();
  308. return 0;
  309. }
  310. static const struct of_device_id qcom_scm_dt_match[] = {
  311. { .compatible = "qcom,scm-apq8064",},
  312. { .compatible = "qcom,scm-msm8660",},
  313. { .compatible = "qcom,scm-msm8960",},
  314. { .compatible = "qcom,scm",},
  315. {}
  316. };
  317. MODULE_DEVICE_TABLE(of, qcom_scm_dt_match);
  318. static struct platform_driver qcom_scm_driver = {
  319. .driver = {
  320. .name = "qcom_scm",
  321. .of_match_table = qcom_scm_dt_match,
  322. },
  323. .probe = qcom_scm_probe,
  324. };
  325. static int __init qcom_scm_init(void)
  326. {
  327. struct device_node *np, *fw_np;
  328. int ret;
  329. fw_np = of_find_node_by_name(NULL, "firmware");
  330. if (!fw_np)
  331. return -ENODEV;
  332. np = of_find_matching_node(fw_np, qcom_scm_dt_match);
  333. if (!np) {
  334. of_node_put(fw_np);
  335. return -ENODEV;
  336. }
  337. of_node_put(np);
  338. ret = of_platform_populate(fw_np, qcom_scm_dt_match, NULL, NULL);
  339. of_node_put(fw_np);
  340. if (ret)
  341. return ret;
  342. return platform_driver_register(&qcom_scm_driver);
  343. }
  344. subsys_initcall(qcom_scm_init);
  345. static void __exit qcom_scm_exit(void)
  346. {
  347. platform_driver_unregister(&qcom_scm_driver);
  348. }
  349. module_exit(qcom_scm_exit);
  350. MODULE_DESCRIPTION("Qualcomm SCM driver");
  351. MODULE_LICENSE("GPL v2");