psci.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2013 ARM Limited
  12. *
  13. * Author: Will Deacon <will.deacon@arm.com>
  14. */
  15. #define pr_fmt(fmt) "psci: " fmt
  16. #include <linux/init.h>
  17. #include <linux/of.h>
  18. #include <linux/smp.h>
  19. #include <linux/delay.h>
  20. #include <linux/psci.h>
  21. #include <linux/mm.h>
  22. #include <uapi/linux/psci.h>
  23. #include <asm/compiler.h>
  24. #include <asm/cpu_ops.h>
  25. #include <asm/errno.h>
  26. #include <asm/smp_plat.h>
  27. static int __init cpu_psci_cpu_init(unsigned int cpu)
  28. {
  29. return 0;
  30. }
  31. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  32. {
  33. if (!psci_ops.cpu_on) {
  34. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  35. return -ENODEV;
  36. }
  37. return 0;
  38. }
  39. static int cpu_psci_cpu_boot(unsigned int cpu)
  40. {
  41. int err = psci_ops.cpu_on(cpu_logical_map(cpu),
  42. __pa_function(secondary_entry));
  43. if (err)
  44. pr_err("failed to boot CPU%d (%d)\n", cpu, err);
  45. return err;
  46. }
  47. #ifdef CONFIG_HOTPLUG_CPU
  48. static int cpu_psci_cpu_disable(unsigned int cpu)
  49. {
  50. /* Fail early if we don't have CPU_OFF support */
  51. if (!psci_ops.cpu_off)
  52. return -EOPNOTSUPP;
  53. /* Trusted OS will deny CPU_OFF */
  54. if (psci_tos_resident_on(cpu))
  55. return -EPERM;
  56. return 0;
  57. }
  58. static void cpu_psci_cpu_die(unsigned int cpu)
  59. {
  60. /*
  61. * There are no known implementations of PSCI actually using the
  62. * power state field, pass a sensible default for now.
  63. */
  64. u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
  65. PSCI_0_2_POWER_STATE_TYPE_SHIFT;
  66. psci_ops.cpu_off(state);
  67. }
  68. static int cpu_psci_cpu_kill(unsigned int cpu)
  69. {
  70. int err;
  71. unsigned long start, end;
  72. if (!psci_ops.affinity_info)
  73. return 0;
  74. /*
  75. * cpu_kill could race with cpu_die and we can
  76. * potentially end up declaring this cpu undead
  77. * while it is dying. So, try again a few times.
  78. */
  79. start = jiffies;
  80. end = start + msecs_to_jiffies(100);
  81. do {
  82. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  83. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  84. pr_info("CPU%d killed (polled %d ms)\n", cpu,
  85. jiffies_to_msecs(jiffies - start));
  86. return 0;
  87. }
  88. usleep_range(100, 1000);
  89. } while (time_before(jiffies, end));
  90. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  91. cpu, err);
  92. return -ETIMEDOUT;
  93. }
  94. #endif
  95. const struct cpu_operations cpu_psci_ops = {
  96. .name = "psci",
  97. #ifdef CONFIG_CPU_IDLE
  98. .cpu_init_idle = psci_cpu_init_idle,
  99. .cpu_suspend = psci_cpu_suspend_enter,
  100. #endif
  101. .cpu_init = cpu_psci_cpu_init,
  102. .cpu_prepare = cpu_psci_cpu_prepare,
  103. .cpu_boot = cpu_psci_cpu_boot,
  104. #ifdef CONFIG_HOTPLUG_CPU
  105. .cpu_disable = cpu_psci_cpu_disable,
  106. .cpu_die = cpu_psci_cpu_die,
  107. .cpu_kill = cpu_psci_cpu_kill,
  108. #endif
  109. };