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