smp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. BUG_ON(nr < 0 || nr >= NR_CPUS);
  58. /*
  59. * If we already started or OPALv2 is not supported, we just
  60. * kick the CPU via the PACA
  61. */
  62. if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPALv2))
  63. goto kick;
  64. /*
  65. * At this point, the CPU can either be spinning on the way in
  66. * from kexec or be inside OPAL waiting to be started for the
  67. * first time. OPAL v3 allows us to query OPAL to know if it
  68. * has the CPUs, so we do that
  69. */
  70. if (firmware_has_feature(FW_FEATURE_OPALv3)) {
  71. uint8_t status;
  72. rc = opal_query_cpu_status(pcpu, &status);
  73. if (rc != OPAL_SUCCESS) {
  74. pr_warn("OPAL Error %ld querying CPU %d state\n",
  75. rc, nr);
  76. return -ENODEV;
  77. }
  78. /*
  79. * Already started, just kick it, probably coming from
  80. * kexec and spinning
  81. */
  82. if (status == OPAL_THREAD_STARTED)
  83. goto kick;
  84. /*
  85. * Available/inactive, let's kick it
  86. */
  87. if (status == OPAL_THREAD_INACTIVE) {
  88. pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n",
  89. nr, pcpu);
  90. rc = opal_start_cpu(pcpu, start_here);
  91. if (rc != OPAL_SUCCESS) {
  92. pr_warn("OPAL Error %ld starting CPU %d\n",
  93. rc, nr);
  94. return -ENODEV;
  95. }
  96. } else {
  97. /*
  98. * An unavailable CPU (or any other unknown status)
  99. * shouldn't be started. It should also
  100. * not be in the possible map but currently it can
  101. * happen
  102. */
  103. pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
  104. " (status %d)...\n", nr, pcpu, status);
  105. return -ENODEV;
  106. }
  107. } else {
  108. /*
  109. * On OPAL v2, we just kick it and hope for the best,
  110. * we must not test the error from opal_start_cpu() or
  111. * we would fail to get CPUs from kexec.
  112. */
  113. opal_start_cpu(pcpu, start_here);
  114. }
  115. kick:
  116. return smp_generic_kick_cpu(nr);
  117. }
  118. #ifdef CONFIG_HOTPLUG_CPU
  119. static int pnv_smp_cpu_disable(void)
  120. {
  121. int cpu = smp_processor_id();
  122. /* This is identical to pSeries... might consolidate by
  123. * moving migrate_irqs_away to a ppc_md with default to
  124. * the generic fixup_irqs. --BenH.
  125. */
  126. set_cpu_online(cpu, false);
  127. vdso_data->processorCount--;
  128. if (cpu == boot_cpuid)
  129. boot_cpuid = cpumask_any(cpu_online_mask);
  130. xics_migrate_irqs_away();
  131. return 0;
  132. }
  133. static void pnv_smp_cpu_kill_self(void)
  134. {
  135. unsigned int cpu;
  136. unsigned long srr1, wmask;
  137. u32 idle_states;
  138. /* Standard hot unplug procedure */
  139. local_irq_disable();
  140. idle_task_exit();
  141. current->active_mm = NULL; /* for sanity */
  142. cpu = smp_processor_id();
  143. DBG("CPU%d offline\n", cpu);
  144. generic_set_cpu_dead(cpu);
  145. smp_wmb();
  146. wmask = SRR1_WAKEMASK;
  147. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  148. wmask = SRR1_WAKEMASK_P8;
  149. idle_states = pnv_get_supported_cpuidle_states();
  150. /* We don't want to take decrementer interrupts while we are offline,
  151. * so clear LPCR:PECE1. We keep PECE2 enabled.
  152. */
  153. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
  154. while (!generic_check_cpu_restart(cpu)) {
  155. ppc64_runlatch_off();
  156. if (idle_states & OPAL_PM_WINKLE_ENABLED)
  157. srr1 = power7_winkle();
  158. else if ((idle_states & OPAL_PM_SLEEP_ENABLED) ||
  159. (idle_states & OPAL_PM_SLEEP_ENABLED_ER1))
  160. srr1 = power7_sleep();
  161. else
  162. srr1 = power7_nap(1);
  163. ppc64_runlatch_on();
  164. /*
  165. * If the SRR1 value indicates that we woke up due to
  166. * an external interrupt, then clear the interrupt.
  167. * We clear the interrupt before checking for the
  168. * reason, so as to avoid a race where we wake up for
  169. * some other reason, find nothing and clear the interrupt
  170. * just as some other cpu is sending us an interrupt.
  171. * If we returned from power7_nap as a result of
  172. * having finished executing in a KVM guest, then srr1
  173. * contains 0.
  174. */
  175. if ((srr1 & wmask) == SRR1_WAKEEE) {
  176. icp_native_flush_interrupt();
  177. local_paca->irq_happened &= PACA_IRQ_HARD_DIS;
  178. smp_mb();
  179. } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
  180. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  181. asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
  182. kvmppc_set_host_ipi(cpu, 0);
  183. }
  184. if (cpu_core_split_required())
  185. continue;
  186. if (!generic_check_cpu_restart(cpu))
  187. DBG("CPU%d Unexpected exit while offline !\n", cpu);
  188. }
  189. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
  190. DBG("CPU%d coming online...\n", cpu);
  191. }
  192. #endif /* CONFIG_HOTPLUG_CPU */
  193. static int pnv_cpu_bootable(unsigned int nr)
  194. {
  195. /*
  196. * Starting with POWER8, the subcore logic relies on all threads of a
  197. * core being booted so that they can participate in split mode
  198. * switches. So on those machines we ignore the smt_enabled_at_boot
  199. * setting (smt-enabled on the kernel command line).
  200. */
  201. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  202. return 1;
  203. return smp_generic_cpu_bootable(nr);
  204. }
  205. static struct smp_ops_t pnv_smp_ops = {
  206. .message_pass = smp_muxed_ipi_message_pass,
  207. .cause_ipi = NULL, /* Filled at runtime by xics_smp_probe() */
  208. .probe = xics_smp_probe,
  209. .kick_cpu = pnv_smp_kick_cpu,
  210. .setup_cpu = pnv_smp_setup_cpu,
  211. .cpu_bootable = pnv_cpu_bootable,
  212. #ifdef CONFIG_HOTPLUG_CPU
  213. .cpu_disable = pnv_smp_cpu_disable,
  214. .cpu_die = generic_cpu_die,
  215. #endif /* CONFIG_HOTPLUG_CPU */
  216. };
  217. /* This is called very early during platform setup_arch */
  218. void __init pnv_smp_init(void)
  219. {
  220. smp_ops = &pnv_smp_ops;
  221. #ifdef CONFIG_HOTPLUG_CPU
  222. ppc_md.cpu_die = pnv_smp_cpu_kill_self;
  223. #endif
  224. }