cpuidle-qcom-spm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2014,2015, Linaro Ltd.
  5. *
  6. * SAW power controller driver
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/slab.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/err.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/cpuidle.h>
  17. #include <linux/cpu_pm.h>
  18. #include <linux/firmware/qcom/qcom_scm.h>
  19. #include <soc/qcom/spm.h>
  20. #include <asm/proc-fns.h>
  21. #include <asm/suspend.h>
  22. #include "dt_idle_states.h"
  23. struct cpuidle_qcom_spm_data {
  24. struct cpuidle_driver cpuidle_driver;
  25. struct spm_driver_data *spm;
  26. };
  27. static int qcom_pm_collapse(unsigned long int unused)
  28. {
  29. qcom_scm_cpu_power_down(QCOM_SCM_CPU_PWR_DOWN_L2_ON);
  30. /*
  31. * Returns here only if there was a pending interrupt and we did not
  32. * power down as a result.
  33. */
  34. return -1;
  35. }
  36. static int qcom_cpu_spc(struct spm_driver_data *drv)
  37. {
  38. int ret;
  39. spm_set_low_power_mode(drv, PM_SLEEP_MODE_SPC);
  40. ret = cpu_suspend(0, qcom_pm_collapse);
  41. /*
  42. * ARM common code executes WFI without calling into our driver and
  43. * if the SPM mode is not reset, then we may accidentally power down the
  44. * cpu when we intended only to gate the cpu clock.
  45. * Ensure the state is set to standby before returning.
  46. */
  47. spm_set_low_power_mode(drv, PM_SLEEP_MODE_STBY);
  48. return ret;
  49. }
  50. static __cpuidle int spm_enter_idle_state(struct cpuidle_device *dev,
  51. struct cpuidle_driver *drv, int idx)
  52. {
  53. struct cpuidle_qcom_spm_data *data = container_of(drv, struct cpuidle_qcom_spm_data,
  54. cpuidle_driver);
  55. return CPU_PM_CPU_IDLE_ENTER_PARAM(qcom_cpu_spc, idx, data->spm);
  56. }
  57. static struct cpuidle_driver qcom_spm_idle_driver = {
  58. .name = "qcom_spm",
  59. .owner = THIS_MODULE,
  60. .states[0] = {
  61. .enter = spm_enter_idle_state,
  62. .exit_latency = 1,
  63. .target_residency = 1,
  64. .power_usage = UINT_MAX,
  65. .name = "WFI",
  66. .desc = "ARM WFI",
  67. }
  68. };
  69. static const struct of_device_id qcom_idle_state_match[] = {
  70. { .compatible = "qcom,idle-state-spc", .data = spm_enter_idle_state },
  71. { },
  72. };
  73. static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
  74. {
  75. struct platform_device *pdev;
  76. struct device_node *cpu_node, *saw_node;
  77. struct cpuidle_qcom_spm_data *data;
  78. int ret;
  79. cpu_node = of_cpu_device_node_get(cpu);
  80. if (!cpu_node)
  81. return -ENODEV;
  82. saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
  83. of_node_put(cpu_node);
  84. if (!saw_node)
  85. return -ENODEV;
  86. pdev = of_find_device_by_node(saw_node);
  87. of_node_put(saw_node);
  88. if (!pdev)
  89. return -ENODEV;
  90. data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
  91. if (!data) {
  92. put_device(&pdev->dev);
  93. return -ENOMEM;
  94. }
  95. data->spm = dev_get_drvdata(&pdev->dev);
  96. put_device(&pdev->dev);
  97. if (!data->spm)
  98. return -EINVAL;
  99. data->cpuidle_driver = qcom_spm_idle_driver;
  100. data->cpuidle_driver.cpumask = (struct cpumask *)cpumask_of(cpu);
  101. ret = dt_init_idle_driver(&data->cpuidle_driver,
  102. qcom_idle_state_match, 1);
  103. if (ret <= 0)
  104. return ret ? : -ENODEV;
  105. return cpuidle_register(&data->cpuidle_driver, NULL);
  106. }
  107. static int spm_cpuidle_drv_probe(struct platform_device *pdev)
  108. {
  109. int cpu, ret;
  110. if (!qcom_scm_is_available())
  111. return -EPROBE_DEFER;
  112. ret = qcom_scm_set_warm_boot_addr(cpu_resume_arm);
  113. if (ret)
  114. return dev_err_probe(&pdev->dev, ret, "set warm boot addr failed");
  115. for_each_present_cpu(cpu) {
  116. ret = spm_cpuidle_register(&pdev->dev, cpu);
  117. if (ret && ret != -ENODEV) {
  118. dev_err(&pdev->dev,
  119. "Cannot register for CPU%d: %d\n", cpu, ret);
  120. }
  121. }
  122. return 0;
  123. }
  124. static struct platform_driver spm_cpuidle_driver = {
  125. .probe = spm_cpuidle_drv_probe,
  126. .driver = {
  127. .name = "qcom-spm-cpuidle",
  128. .suppress_bind_attrs = true,
  129. },
  130. };
  131. static bool __init qcom_spm_find_any_cpu(void)
  132. {
  133. struct device_node *cpu_node, *saw_node;
  134. for_each_of_cpu_node(cpu_node) {
  135. saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
  136. if (of_device_is_available(saw_node)) {
  137. of_node_put(saw_node);
  138. of_node_put(cpu_node);
  139. return true;
  140. }
  141. of_node_put(saw_node);
  142. }
  143. return false;
  144. }
  145. static int __init qcom_spm_cpuidle_init(void)
  146. {
  147. struct platform_device *pdev;
  148. int ret;
  149. ret = platform_driver_register(&spm_cpuidle_driver);
  150. if (ret)
  151. return ret;
  152. /* Make sure there is actually any CPU managed by the SPM */
  153. if (!qcom_spm_find_any_cpu())
  154. return 0;
  155. pdev = platform_device_register_simple("qcom-spm-cpuidle",
  156. -1, NULL, 0);
  157. if (IS_ERR(pdev)) {
  158. platform_driver_unregister(&spm_cpuidle_driver);
  159. return PTR_ERR(pdev);
  160. }
  161. return 0;
  162. }
  163. device_initcall(qcom_spm_cpuidle_init);