psci.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2013 ARM Limited
  5. *
  6. * Author: Will Deacon <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "psci: " fmt
  9. #include <linux/init.h>
  10. #include <linux/of.h>
  11. #include <linux/smp.h>
  12. #include <linux/delay.h>
  13. #include <linux/psci.h>
  14. #include <linux/mm.h>
  15. #include <uapi/linux/psci.h>
  16. #include <asm/cpu_ops.h>
  17. #include <asm/errno.h>
  18. #include <asm/smp_plat.h>
  19. static int __init cpu_psci_cpu_init(unsigned int cpu)
  20. {
  21. return 0;
  22. }
  23. static int __init cpu_psci_cpu_prepare(unsigned int cpu)
  24. {
  25. if (!psci_ops.cpu_on) {
  26. pr_err("no cpu_on method, not booting CPU%d\n", cpu);
  27. return -ENODEV;
  28. }
  29. return 0;
  30. }
  31. static int cpu_psci_cpu_boot(unsigned int cpu)
  32. {
  33. phys_addr_t pa_secondary_entry = __pa_symbol(secondary_entry);
  34. int err = psci_ops.cpu_on(cpu_logical_map(cpu), pa_secondary_entry);
  35. if (err)
  36. pr_err("failed to boot CPU%d (%d)\n", cpu, err);
  37. return err;
  38. }
  39. #ifdef CONFIG_HOTPLUG_CPU
  40. static bool cpu_psci_cpu_can_disable(unsigned int cpu)
  41. {
  42. return !psci_tos_resident_on(cpu);
  43. }
  44. static int cpu_psci_cpu_disable(unsigned int cpu)
  45. {
  46. /* Fail early if we don't have CPU_OFF support */
  47. if (!psci_ops.cpu_off)
  48. return -EOPNOTSUPP;
  49. /* Trusted OS will deny CPU_OFF */
  50. if (psci_tos_resident_on(cpu))
  51. return -EPERM;
  52. return 0;
  53. }
  54. static void cpu_psci_cpu_die(unsigned int cpu)
  55. {
  56. /*
  57. * There are no known implementations of PSCI actually using the
  58. * power state field, pass a sensible default for now.
  59. */
  60. u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
  61. PSCI_0_2_POWER_STATE_TYPE_SHIFT;
  62. psci_ops.cpu_off(state);
  63. }
  64. static int cpu_psci_cpu_kill(unsigned int cpu)
  65. {
  66. int err;
  67. unsigned long start, end;
  68. if (!psci_ops.affinity_info)
  69. return 0;
  70. /*
  71. * cpu_kill could race with cpu_die and we can
  72. * potentially end up declaring this cpu undead
  73. * while it is dying. So, try again a few times.
  74. */
  75. start = jiffies;
  76. end = start + msecs_to_jiffies(100);
  77. do {
  78. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  79. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  80. pr_info("CPU%d killed (polled %d ms)\n", cpu,
  81. jiffies_to_msecs(jiffies - start));
  82. return 0;
  83. }
  84. usleep_range(100, 1000);
  85. } while (time_before(jiffies, end));
  86. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  87. cpu, err);
  88. return -ETIMEDOUT;
  89. }
  90. #endif
  91. const struct cpu_operations cpu_psci_ops = {
  92. .name = "psci",
  93. .cpu_init = cpu_psci_cpu_init,
  94. .cpu_prepare = cpu_psci_cpu_prepare,
  95. .cpu_boot = cpu_psci_cpu_boot,
  96. #ifdef CONFIG_HOTPLUG_CPU
  97. .cpu_can_disable = cpu_psci_cpu_can_disable,
  98. .cpu_disable = cpu_psci_cpu_disable,
  99. .cpu_die = cpu_psci_cpu_die,
  100. .cpu_kill = cpu_psci_cpu_kill,
  101. #endif
  102. };