psci_smp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #if defined(CONFIG_MACH_MT8163)
  24. #include <mt-smp.h>
  25. #include <hotplug.h>
  26. #endif
  27. /*
  28. * psci_smp assumes that the following is true about PSCI:
  29. *
  30. * cpu_suspend Suspend the execution on a CPU
  31. * @state we don't currently describe affinity levels, so just pass 0.
  32. * @entry_point the first instruction to be executed on return
  33. * returns 0 success, < 0 on failure
  34. *
  35. * cpu_off Power down a CPU
  36. * @state we don't currently describe affinity levels, so just pass 0.
  37. * no return on successful call
  38. *
  39. * cpu_on Power up a CPU
  40. * @cpuid cpuid of target CPU, as from MPIDR
  41. * @entry_point the first instruction to be executed on return
  42. * returns 0 success, < 0 on failure
  43. *
  44. * migrate Migrate the context to a different CPU
  45. * @cpuid cpuid of target CPU, as from MPIDR
  46. * returns 0 success, < 0 on failure
  47. *
  48. */
  49. extern void secondary_startup(void);
  50. static int psci_boot_secondary(unsigned int cpu, struct task_struct *idle)
  51. {
  52. #if defined(CONFIG_MACH_MT8163)
  53. int ret = -1;
  54. if (psci_ops.cpu_on)
  55. ret = psci_ops.cpu_on(cpu_logical_map(cpu),
  56. virt_to_idmap(&secondary_startup));
  57. if (ret < 0) {
  58. pr_err("psci cpu_on failed\n");
  59. return -ENODEV;
  60. }
  61. ret = mt_smp_boot_secondary(cpu, idle);
  62. if (ret < 0) {
  63. pr_err("mt_smp_boot_secondary failed\n");
  64. return -ENODEV;
  65. }
  66. return 0;
  67. #else
  68. if (psci_ops.cpu_on)
  69. return psci_ops.cpu_on(cpu_logical_map(cpu),
  70. virt_to_idmap(&secondary_startup));
  71. return -ENODEV;
  72. #endif
  73. }
  74. #ifdef CONFIG_HOTPLUG_CPU
  75. int psci_cpu_disable(unsigned int cpu)
  76. {
  77. /* Fail early if we don't have CPU_OFF support */
  78. if (!psci_ops.cpu_off)
  79. return -EOPNOTSUPP;
  80. /* Trusted OS will deny CPU_OFF */
  81. if (psci_tos_resident_on(cpu))
  82. return -EPERM;
  83. return 0;
  84. }
  85. void psci_cpu_die(unsigned int cpu)
  86. {
  87. u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
  88. PSCI_0_2_POWER_STATE_TYPE_SHIFT;
  89. if (psci_ops.cpu_off)
  90. psci_ops.cpu_off(state);
  91. /* We should never return */
  92. panic("psci: cpu %d failed to shutdown\n", cpu);
  93. }
  94. #if defined(CONFIG_MACH_MT8163)
  95. int psci_cpu_kill(unsigned int cpu)
  96. {
  97. return mt_cpu_kill(cpu);
  98. }
  99. #else
  100. int psci_cpu_kill(unsigned int cpu)
  101. {
  102. int err, i;
  103. if (!psci_ops.affinity_info)
  104. return 1;
  105. /*
  106. * cpu_kill could race with cpu_die and we can
  107. * potentially end up declaring this cpu undead
  108. * while it is dying. So, try again a few times.
  109. */
  110. for (i = 0; i < 10; i++) {
  111. err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
  112. if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
  113. pr_info("CPU%d killed.\n", cpu);
  114. return 1;
  115. }
  116. msleep(10);
  117. pr_info("Retrying again to check for CPU kill\n");
  118. }
  119. pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
  120. cpu, err);
  121. /* Make platform_cpu_kill() fail. */
  122. return 0;
  123. }
  124. #endif
  125. #endif
  126. bool __init psci_smp_available(void)
  127. {
  128. /* is cpu_on available at least? */
  129. return (psci_ops.cpu_on != NULL);
  130. }
  131. const struct smp_operations psci_smp_ops __initconst = {
  132. .smp_boot_secondary = psci_boot_secondary,
  133. #if defined(CONFIG_MACH_MT8163)
  134. .smp_prepare_cpus = mt_smp_prepare_cpus,
  135. #endif
  136. #if defined(CONFIG_MACH_MT8163)
  137. .smp_secondary_init = mt_smp_secondary_init,
  138. #endif
  139. #ifdef CONFIG_HOTPLUG_CPU
  140. .cpu_disable = psci_cpu_disable,
  141. .cpu_die = psci_cpu_die,
  142. .cpu_kill = psci_cpu_kill,
  143. #endif
  144. };