smp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * SMP support for PowerNV machines.
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/cpu.h>
  20. #include <asm/irq.h>
  21. #include <asm/smp.h>
  22. #include <asm/paca.h>
  23. #include <asm/machdep.h>
  24. #include <asm/cputable.h>
  25. #include <asm/firmware.h>
  26. #include <asm/vdso_datapage.h>
  27. #include <asm/cputhreads.h>
  28. #include <asm/xics.h>
  29. #include <asm/opal.h>
  30. #include <asm/runlatch.h>
  31. #include <asm/code-patching.h>
  32. #include <asm/dbell.h>
  33. #include <asm/kvm_ppc.h>
  34. #include <asm/ppc-opcode.h>
  35. #include "powernv.h"
  36. #ifdef DEBUG
  37. #include <asm/udbg.h>
  38. #define DBG(fmt...) udbg_printf(fmt)
  39. #else
  40. #define DBG(fmt...)
  41. #endif
  42. static void pnv_smp_setup_cpu(int cpu)
  43. {
  44. if (cpu != boot_cpuid)
  45. xics_setup_cpu();
  46. #ifdef CONFIG_PPC_DOORBELL
  47. if (cpu_has_feature(CPU_FTR_DBELL))
  48. doorbell_setup_this_cpu();
  49. #endif
  50. }
  51. static int pnv_smp_kick_cpu(int nr)
  52. {
  53. unsigned int pcpu = get_hard_smp_processor_id(nr);
  54. unsigned long start_here =
  55. __pa(ppc_function_entry(generic_secondary_smp_init));
  56. long rc;
  57. uint8_t status;
  58. BUG_ON(nr < 0 || nr >= NR_CPUS);
  59. /*
  60. * If we already started or OPAL is not supported, we just
  61. * kick the CPU via the PACA
  62. */
  63. if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPAL))
  64. goto kick;
  65. /*
  66. * At this point, the CPU can either be spinning on the way in
  67. * from kexec or be inside OPAL waiting to be started for the
  68. * first time. OPAL v3 allows us to query OPAL to know if it
  69. * has the CPUs, so we do that
  70. */
  71. rc = opal_query_cpu_status(pcpu, &status);
  72. if (rc != OPAL_SUCCESS) {
  73. pr_warn("OPAL Error %ld querying CPU %d state\n", rc, nr);
  74. return -ENODEV;
  75. }
  76. /*
  77. * Already started, just kick it, probably coming from
  78. * kexec and spinning
  79. */
  80. if (status == OPAL_THREAD_STARTED)
  81. goto kick;
  82. /*
  83. * Available/inactive, let's kick it
  84. */
  85. if (status == OPAL_THREAD_INACTIVE) {
  86. pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", nr, pcpu);
  87. rc = opal_start_cpu(pcpu, start_here);
  88. if (rc != OPAL_SUCCESS) {
  89. pr_warn("OPAL Error %ld starting CPU %d\n", rc, nr);
  90. return -ENODEV;
  91. }
  92. } else {
  93. /*
  94. * An unavailable CPU (or any other unknown status)
  95. * shouldn't be started. It should also
  96. * not be in the possible map but currently it can
  97. * happen
  98. */
  99. pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
  100. " (status %d)...\n", nr, pcpu, status);
  101. return -ENODEV;
  102. }
  103. kick:
  104. return smp_generic_kick_cpu(nr);
  105. }
  106. #ifdef CONFIG_HOTPLUG_CPU
  107. static int pnv_smp_cpu_disable(void)
  108. {
  109. int cpu = smp_processor_id();
  110. /* This is identical to pSeries... might consolidate by
  111. * moving migrate_irqs_away to a ppc_md with default to
  112. * the generic fixup_irqs. --BenH.
  113. */
  114. set_cpu_online(cpu, false);
  115. vdso_data->processorCount--;
  116. if (cpu == boot_cpuid)
  117. boot_cpuid = cpumask_any(cpu_online_mask);
  118. xics_migrate_irqs_away();
  119. return 0;
  120. }
  121. static void pnv_smp_cpu_kill_self(void)
  122. {
  123. unsigned int cpu;
  124. unsigned long srr1, wmask;
  125. u32 idle_states;
  126. /* Standard hot unplug procedure */
  127. local_irq_disable();
  128. idle_task_exit();
  129. current->active_mm = NULL; /* for sanity */
  130. cpu = smp_processor_id();
  131. DBG("CPU%d offline\n", cpu);
  132. generic_set_cpu_dead(cpu);
  133. smp_wmb();
  134. wmask = SRR1_WAKEMASK;
  135. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  136. wmask = SRR1_WAKEMASK_P8;
  137. idle_states = pnv_get_supported_cpuidle_states();
  138. /* We don't want to take decrementer interrupts while we are offline,
  139. * so clear LPCR:PECE1. We keep PECE2 (and LPCR_PECE_HVEE on P9)
  140. * enabled as to let IPIs in.
  141. */
  142. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
  143. /*
  144. * Hard-disable interrupts, and then clear irq_happened flags
  145. * that we can safely ignore while off-line, since they
  146. * are for things for which we do no processing when off-line
  147. * (or in the case of HMI, all the processing we need to do
  148. * is done in lower-level real-mode code).
  149. */
  150. hard_irq_disable();
  151. local_paca->irq_happened &= ~(PACA_IRQ_DEC | PACA_IRQ_HMI);
  152. while (!generic_check_cpu_restart(cpu)) {
  153. /*
  154. * Clear IPI flag, since we don't handle IPIs while
  155. * offline, except for those when changing micro-threading
  156. * mode, which are handled explicitly below, and those
  157. * for coming online, which are handled via
  158. * generic_check_cpu_restart() calls.
  159. */
  160. kvmppc_set_host_ipi(cpu, 0);
  161. ppc64_runlatch_off();
  162. if (cpu_has_feature(CPU_FTR_ARCH_300))
  163. srr1 = power9_idle_stop(pnv_deepest_stop_state);
  164. else if (idle_states & OPAL_PM_WINKLE_ENABLED)
  165. srr1 = power7_winkle();
  166. else if ((idle_states & OPAL_PM_SLEEP_ENABLED) ||
  167. (idle_states & OPAL_PM_SLEEP_ENABLED_ER1))
  168. srr1 = power7_sleep();
  169. else
  170. srr1 = power7_nap(1);
  171. ppc64_runlatch_on();
  172. /*
  173. * If the SRR1 value indicates that we woke up due to
  174. * an external interrupt, then clear the interrupt.
  175. * We clear the interrupt before checking for the
  176. * reason, so as to avoid a race where we wake up for
  177. * some other reason, find nothing and clear the interrupt
  178. * just as some other cpu is sending us an interrupt.
  179. * If we returned from power7_nap as a result of
  180. * having finished executing in a KVM guest, then srr1
  181. * contains 0.
  182. */
  183. if (((srr1 & wmask) == SRR1_WAKEEE) ||
  184. ((srr1 & wmask) == SRR1_WAKEHVI) ||
  185. (local_paca->irq_happened & PACA_IRQ_EE)) {
  186. if (cpu_has_feature(CPU_FTR_ARCH_300))
  187. icp_opal_flush_interrupt();
  188. else
  189. icp_native_flush_interrupt();
  190. } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
  191. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  192. asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
  193. }
  194. local_paca->irq_happened &= ~(PACA_IRQ_EE | PACA_IRQ_DBELL);
  195. smp_mb();
  196. if (cpu_core_split_required())
  197. continue;
  198. if (srr1 && !generic_check_cpu_restart(cpu))
  199. DBG("CPU%d Unexpected exit while offline !\n", cpu);
  200. }
  201. /* Re-enable decrementer interrupts */
  202. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
  203. DBG("CPU%d coming online...\n", cpu);
  204. }
  205. #endif /* CONFIG_HOTPLUG_CPU */
  206. static int pnv_cpu_bootable(unsigned int nr)
  207. {
  208. /*
  209. * Starting with POWER8, the subcore logic relies on all threads of a
  210. * core being booted so that they can participate in split mode
  211. * switches. So on those machines we ignore the smt_enabled_at_boot
  212. * setting (smt-enabled on the kernel command line).
  213. */
  214. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  215. return 1;
  216. return smp_generic_cpu_bootable(nr);
  217. }
  218. static struct smp_ops_t pnv_smp_ops = {
  219. .message_pass = smp_muxed_ipi_message_pass,
  220. .cause_ipi = NULL, /* Filled at runtime by xics_smp_probe() */
  221. .probe = xics_smp_probe,
  222. .kick_cpu = pnv_smp_kick_cpu,
  223. .setup_cpu = pnv_smp_setup_cpu,
  224. .cpu_bootable = pnv_cpu_bootable,
  225. #ifdef CONFIG_HOTPLUG_CPU
  226. .cpu_disable = pnv_smp_cpu_disable,
  227. .cpu_die = generic_cpu_die,
  228. #endif /* CONFIG_HOTPLUG_CPU */
  229. };
  230. /* This is called very early during platform setup_arch */
  231. void __init pnv_smp_init(void)
  232. {
  233. smp_ops = &pnv_smp_ops;
  234. #ifdef CONFIG_HOTPLUG_CPU
  235. ppc_md.cpu_die = pnv_smp_cpu_kill_self;
  236. #endif
  237. }