cpuidle-qcom-spm.c 4.5 KB

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