qcom_scm.c 10 KB

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