psci_smp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. *
  6. * Author: Will Deacon <will.deacon@arm.com>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/smp.h>
  10. #include <linux/of.h>
  11. #include <linux/delay.h>
  12. #include <linux/psci.h>
  13. #include <uapi/linux/psci.h>
  14. #include <asm/psci.h>
  15. #include <asm/smp_plat.h>
  16. /*
  17. * psci_smp assumes that the following is true about PSCI:
  18. *
  19. * cpu_suspend Suspend the execution on a CPU
  20. * @state we don't currently describe affinity levels, so just pass 0.
  21. * @entry_point the first instruction to be executed on return
  22. * returns 0 success, < 0 on failure
  23. *
  24. * cpu_off Power down a CPU
  25. * @state we don't currently describe affinity levels, so just pass 0.
  26. * no return on successful call
  27. *
  28. * cpu_on Power up a CPU
  29. * @cpuid cpuid of target CPU, as from MPIDR
  30. * @entry_point the first instruction to be executed on return
  31. * returns 0 success, < 0 on failure
  32. *
  33. * migrate Migrate the context to a different CPU
  34. * @cpuid cpuid of target CPU, as from MPIDR
  35. * returns 0 success, < 0 on failure
  36. *
  37. */
  38. extern void secondary_startup(void);
  39. static int psci_boot_secondary(unsigned int cpu, struct task_struct *idle)
  40. {
  41. if (psci_ops.cpu_on)
  42. return psci_ops.cpu_on(cpu_logical_map(cpu),
  43. virt_to_idmap(&secondary_startup));
  44. return -ENODEV;
  45. }
  46. #ifdef CONFIG_HOTPLUG_CPU
  47. static int psci_cpu_disable(unsigned int cpu)
  48. {
  49. /* Fail early if we don't have CPU_OFF support */
  50. if (!psci_ops.cpu_off)
  51. return -EOPNOTSUPP;
  52. /* Trusted OS will deny CPU_OFF */
  53. if (psci_tos_resident_on(cpu))
  54. return -EPERM;
  55. return 0;
  56. }
  57. static void psci_cpu_die(unsigned int cpu)
  58. {
  59. u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
  60. PSCI_0_2_POWER_STATE_TYPE_SHIFT;
  61. if (psci_ops.cpu_off)
  62. psci_ops.cpu_off(state);
  63. /* We should never return */
  64. panic("psci: cpu %d failed to shutdown\n", cpu);
  65. }
  66. static int psci_cpu_kill(unsigned int cpu)
  67. {
  68. int err, i;
  69. if (!psci_ops.affinity_info)
  70. return 1;
  71. /*
  72. * cpu_kill could race with cpu_die and we can
  73. * potentially end up declaring this cpu undead
  74. * while it is dying. So, try again a few times.
  75. */
  76. for (i = 0; i < 10; i++) {
  77. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  78. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  79. pr_info("CPU%d killed.\n", cpu);
  80. return 1;
  81. }
  82. msleep(10);
  83. pr_info("Retrying again to check for CPU kill\n");
  84. }
  85. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  86. cpu, err);
  87. /* Make platform_cpu_kill() fail. */
  88. return 0;
  89. }
  90. #endif
  91. bool __init psci_smp_available(void)
  92. {
  93. /* is cpu_on available at least? */
  94. return (psci_ops.cpu_on != NULL);
  95. }
  96. const struct smp_operations psci_smp_ops __initconst = {
  97. .smp_boot_secondary = psci_boot_secondary,
  98. #ifdef CONFIG_HOTPLUG_CPU
  99. .cpu_disable = psci_cpu_disable,
  100. .cpu_die = psci_cpu_die,
  101. .cpu_kill = psci_cpu_kill,
  102. #endif
  103. };