smp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "powernv.h"
  32. #ifdef DEBUG
  33. #include <asm/udbg.h>
  34. #define DBG(fmt...) udbg_printf(fmt)
  35. #else
  36. #define DBG(fmt...)
  37. #endif
  38. static void pnv_smp_setup_cpu(int cpu)
  39. {
  40. if (cpu != boot_cpuid)
  41. xics_setup_cpu();
  42. }
  43. int pnv_smp_kick_cpu(int nr)
  44. {
  45. unsigned int pcpu = get_hard_smp_processor_id(nr);
  46. unsigned long start_here = __pa(*((unsigned long *)
  47. generic_secondary_smp_init));
  48. long rc;
  49. BUG_ON(nr < 0 || nr >= NR_CPUS);
  50. /*
  51. * If we already started or OPALv2 is not supported, we just
  52. * kick the CPU via the PACA
  53. */
  54. if (paca[nr].cpu_start || !firmware_has_feature(FW_FEATURE_OPALv2))
  55. goto kick;
  56. /*
  57. * At this point, the CPU can either be spinning on the way in
  58. * from kexec or be inside OPAL waiting to be started for the
  59. * first time. OPAL v3 allows us to query OPAL to know if it
  60. * has the CPUs, so we do that
  61. */
  62. if (firmware_has_feature(FW_FEATURE_OPALv3)) {
  63. uint8_t status;
  64. rc = opal_query_cpu_status(pcpu, &status);
  65. if (rc != OPAL_SUCCESS) {
  66. pr_warn("OPAL Error %ld querying CPU %d state\n",
  67. rc, nr);
  68. return -ENODEV;
  69. }
  70. /*
  71. * Already started, just kick it, probably coming from
  72. * kexec and spinning
  73. */
  74. if (status == OPAL_THREAD_STARTED)
  75. goto kick;
  76. /*
  77. * Available/inactive, let's kick it
  78. */
  79. if (status == OPAL_THREAD_INACTIVE) {
  80. pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n",
  81. nr, pcpu);
  82. rc = opal_start_cpu(pcpu, start_here);
  83. if (rc != OPAL_SUCCESS) {
  84. pr_warn("OPAL Error %ld starting CPU %d\n",
  85. rc, nr);
  86. return -ENODEV;
  87. }
  88. } else {
  89. /*
  90. * An unavailable CPU (or any other unknown status)
  91. * shouldn't be started. It should also
  92. * not be in the possible map but currently it can
  93. * happen
  94. */
  95. pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
  96. " (status %d)...\n", nr, pcpu, status);
  97. return -ENODEV;
  98. }
  99. } else {
  100. /*
  101. * On OPAL v2, we just kick it and hope for the best,
  102. * we must not test the error from opal_start_cpu() or
  103. * we would fail to get CPUs from kexec.
  104. */
  105. opal_start_cpu(pcpu, start_here);
  106. }
  107. kick:
  108. return smp_generic_kick_cpu(nr);
  109. }
  110. #ifdef CONFIG_HOTPLUG_CPU
  111. static int pnv_smp_cpu_disable(void)
  112. {
  113. int cpu = smp_processor_id();
  114. /* This is identical to pSeries... might consolidate by
  115. * moving migrate_irqs_away to a ppc_md with default to
  116. * the generic fixup_irqs. --BenH.
  117. */
  118. set_cpu_online(cpu, false);
  119. vdso_data->processorCount--;
  120. if (cpu == boot_cpuid)
  121. boot_cpuid = cpumask_any(cpu_online_mask);
  122. xics_migrate_irqs_away();
  123. return 0;
  124. }
  125. static void pnv_smp_cpu_kill_self(void)
  126. {
  127. unsigned int cpu;
  128. /* Standard hot unplug procedure */
  129. local_irq_disable();
  130. idle_task_exit();
  131. current->active_mm = NULL; /* for sanity */
  132. cpu = smp_processor_id();
  133. DBG("CPU%d offline\n", cpu);
  134. generic_set_cpu_dead(cpu);
  135. smp_wmb();
  136. /* We don't want to take decrementer interrupts while we are offline,
  137. * so clear LPCR:PECE1. We keep PECE2 enabled.
  138. */
  139. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1);
  140. while (!generic_check_cpu_restart(cpu)) {
  141. power7_nap();
  142. if (!generic_check_cpu_restart(cpu)) {
  143. DBG("CPU%d Unexpected exit while offline !\n", cpu);
  144. /* We may be getting an IPI, so we re-enable
  145. * interrupts to process it, it will be ignored
  146. * since we aren't online (hopefully)
  147. */
  148. local_irq_enable();
  149. local_irq_disable();
  150. }
  151. }
  152. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_PECE1);
  153. DBG("CPU%d coming online...\n", cpu);
  154. }
  155. #endif /* CONFIG_HOTPLUG_CPU */
  156. static struct smp_ops_t pnv_smp_ops = {
  157. .message_pass = smp_muxed_ipi_message_pass,
  158. .cause_ipi = NULL, /* Filled at runtime by xics_smp_probe() */
  159. .probe = xics_smp_probe,
  160. .kick_cpu = pnv_smp_kick_cpu,
  161. .setup_cpu = pnv_smp_setup_cpu,
  162. .cpu_bootable = smp_generic_cpu_bootable,
  163. #ifdef CONFIG_HOTPLUG_CPU
  164. .cpu_disable = pnv_smp_cpu_disable,
  165. .cpu_die = generic_cpu_die,
  166. #endif /* CONFIG_HOTPLUG_CPU */
  167. };
  168. /* This is called very early during platform setup_arch */
  169. void __init pnv_smp_init(void)
  170. {
  171. smp_ops = &pnv_smp_ops;
  172. /* XXX We don't yet have a proper entry point from HAL, for
  173. * now we rely on kexec-style entry from BML
  174. */
  175. #ifdef CONFIG_PPC_RTAS
  176. /* Non-lpar has additional take/give timebase */
  177. if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
  178. smp_ops->give_timebase = rtas_give_timebase;
  179. smp_ops->take_timebase = rtas_take_timebase;
  180. }
  181. #endif /* CONFIG_PPC_RTAS */
  182. #ifdef CONFIG_HOTPLUG_CPU
  183. ppc_md.cpu_die = pnv_smp_cpu_kill_self;
  184. #endif
  185. }