smp.c 6.5 KB

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