/arch/ppc64/kernel/pSeries_smp.c

https://bitbucket.org/evzijst/gittest · C · 451 lines · 298 code · 69 blank · 84 comment · 55 complexity · 8caee5f65bdef5d6863e2dda7d9dcb8b MD5 · raw file

  1. /*
  2. * SMP support for pSeries machines.
  3. *
  4. * Dave Engebretsen, Peter Bergner, and
  5. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  6. *
  7. * Plus various changes from other IBM teams...
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #undef DEBUG
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/smp.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/cache.h>
  25. #include <linux/err.h>
  26. #include <linux/sysdev.h>
  27. #include <linux/cpu.h>
  28. #include <asm/ptrace.h>
  29. #include <asm/atomic.h>
  30. #include <asm/irq.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/io.h>
  34. #include <asm/prom.h>
  35. #include <asm/smp.h>
  36. #include <asm/paca.h>
  37. #include <asm/time.h>
  38. #include <asm/machdep.h>
  39. #include <asm/xics.h>
  40. #include <asm/cputable.h>
  41. #include <asm/system.h>
  42. #include <asm/rtas.h>
  43. #include <asm/plpar_wrappers.h>
  44. #include <asm/pSeries_reconfig.h>
  45. #include "mpic.h"
  46. #ifdef DEBUG
  47. #define DBG(fmt...) udbg_printf(fmt)
  48. #else
  49. #define DBG(fmt...)
  50. #endif
  51. /*
  52. * The primary thread of each non-boot processor is recorded here before
  53. * smp init.
  54. */
  55. static cpumask_t of_spin_map;
  56. extern void pSeries_secondary_smp_init(unsigned long);
  57. #ifdef CONFIG_HOTPLUG_CPU
  58. /* Get state of physical CPU.
  59. * Return codes:
  60. * 0 - The processor is in the RTAS stopped state
  61. * 1 - stop-self is in progress
  62. * 2 - The processor is not in the RTAS stopped state
  63. * -1 - Hardware Error
  64. * -2 - Hardware Busy, Try again later.
  65. */
  66. static int query_cpu_stopped(unsigned int pcpu)
  67. {
  68. int cpu_status;
  69. int status, qcss_tok;
  70. qcss_tok = rtas_token("query-cpu-stopped-state");
  71. if (qcss_tok == RTAS_UNKNOWN_SERVICE)
  72. return -1;
  73. status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
  74. if (status != 0) {
  75. printk(KERN_ERR
  76. "RTAS query-cpu-stopped-state failed: %i\n", status);
  77. return status;
  78. }
  79. return cpu_status;
  80. }
  81. int pSeries_cpu_disable(void)
  82. {
  83. systemcfg->processorCount--;
  84. /*fix boot_cpuid here*/
  85. if (smp_processor_id() == boot_cpuid)
  86. boot_cpuid = any_online_cpu(cpu_online_map);
  87. /* FIXME: abstract this to not be platform specific later on */
  88. xics_migrate_irqs_away();
  89. return 0;
  90. }
  91. void pSeries_cpu_die(unsigned int cpu)
  92. {
  93. int tries;
  94. int cpu_status;
  95. unsigned int pcpu = get_hard_smp_processor_id(cpu);
  96. for (tries = 0; tries < 25; tries++) {
  97. cpu_status = query_cpu_stopped(pcpu);
  98. if (cpu_status == 0 || cpu_status == -1)
  99. break;
  100. msleep(200);
  101. }
  102. if (cpu_status != 0) {
  103. printk("Querying DEAD? cpu %i (%i) shows %i\n",
  104. cpu, pcpu, cpu_status);
  105. }
  106. /* Isolation and deallocation are definatly done by
  107. * drslot_chrp_cpu. If they were not they would be
  108. * done here. Change isolate state to Isolate and
  109. * change allocation-state to Unusable.
  110. */
  111. paca[cpu].cpu_start = 0;
  112. }
  113. /*
  114. * Update cpu_present_map and paca(s) for a new cpu node. The wrinkle
  115. * here is that a cpu device node may represent up to two logical cpus
  116. * in the SMT case. We must honor the assumption in other code that
  117. * the logical ids for sibling SMT threads x and y are adjacent, such
  118. * that x^1 == y and y^1 == x.
  119. */
  120. static int pSeries_add_processor(struct device_node *np)
  121. {
  122. unsigned int cpu;
  123. cpumask_t candidate_map, tmp = CPU_MASK_NONE;
  124. int err = -ENOSPC, len, nthreads, i;
  125. u32 *intserv;
  126. intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
  127. if (!intserv)
  128. return 0;
  129. nthreads = len / sizeof(u32);
  130. for (i = 0; i < nthreads; i++)
  131. cpu_set(i, tmp);
  132. lock_cpu_hotplug();
  133. BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
  134. /* Get a bitmap of unoccupied slots. */
  135. cpus_xor(candidate_map, cpu_possible_map, cpu_present_map);
  136. if (cpus_empty(candidate_map)) {
  137. /* If we get here, it most likely means that NR_CPUS is
  138. * less than the partition's max processors setting.
  139. */
  140. printk(KERN_ERR "Cannot add cpu %s; this system configuration"
  141. " supports %d logical cpus.\n", np->full_name,
  142. cpus_weight(cpu_possible_map));
  143. goto out_unlock;
  144. }
  145. while (!cpus_empty(tmp))
  146. if (cpus_subset(tmp, candidate_map))
  147. /* Found a range where we can insert the new cpu(s) */
  148. break;
  149. else
  150. cpus_shift_left(tmp, tmp, nthreads);
  151. if (cpus_empty(tmp)) {
  152. printk(KERN_ERR "Unable to find space in cpu_present_map for"
  153. " processor %s with %d thread(s)\n", np->name,
  154. nthreads);
  155. goto out_unlock;
  156. }
  157. for_each_cpu_mask(cpu, tmp) {
  158. BUG_ON(cpu_isset(cpu, cpu_present_map));
  159. cpu_set(cpu, cpu_present_map);
  160. set_hard_smp_processor_id(cpu, *intserv++);
  161. }
  162. err = 0;
  163. out_unlock:
  164. unlock_cpu_hotplug();
  165. return err;
  166. }
  167. /*
  168. * Update the present map for a cpu node which is going away, and set
  169. * the hard id in the paca(s) to -1 to be consistent with boot time
  170. * convention for non-present cpus.
  171. */
  172. static void pSeries_remove_processor(struct device_node *np)
  173. {
  174. unsigned int cpu;
  175. int len, nthreads, i;
  176. u32 *intserv;
  177. intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s", &len);
  178. if (!intserv)
  179. return;
  180. nthreads = len / sizeof(u32);
  181. lock_cpu_hotplug();
  182. for (i = 0; i < nthreads; i++) {
  183. for_each_present_cpu(cpu) {
  184. if (get_hard_smp_processor_id(cpu) != intserv[i])
  185. continue;
  186. BUG_ON(cpu_online(cpu));
  187. cpu_clear(cpu, cpu_present_map);
  188. set_hard_smp_processor_id(cpu, -1);
  189. break;
  190. }
  191. if (cpu == NR_CPUS)
  192. printk(KERN_WARNING "Could not find cpu to remove "
  193. "with physical id 0x%x\n", intserv[i]);
  194. }
  195. unlock_cpu_hotplug();
  196. }
  197. static int pSeries_smp_notifier(struct notifier_block *nb, unsigned long action, void *node)
  198. {
  199. int err = NOTIFY_OK;
  200. switch (action) {
  201. case PSERIES_RECONFIG_ADD:
  202. if (pSeries_add_processor(node))
  203. err = NOTIFY_BAD;
  204. break;
  205. case PSERIES_RECONFIG_REMOVE:
  206. pSeries_remove_processor(node);
  207. break;
  208. default:
  209. err = NOTIFY_DONE;
  210. break;
  211. }
  212. return err;
  213. }
  214. static struct notifier_block pSeries_smp_nb = {
  215. .notifier_call = pSeries_smp_notifier,
  216. };
  217. #endif /* CONFIG_HOTPLUG_CPU */
  218. /**
  219. * smp_startup_cpu() - start the given cpu
  220. *
  221. * At boot time, there is nothing to do for primary threads which were
  222. * started from Open Firmware. For anything else, call RTAS with the
  223. * appropriate start location.
  224. *
  225. * Returns:
  226. * 0 - failure
  227. * 1 - success
  228. */
  229. static inline int __devinit smp_startup_cpu(unsigned int lcpu)
  230. {
  231. int status;
  232. unsigned long start_here = __pa((u32)*((unsigned long *)
  233. pSeries_secondary_smp_init));
  234. unsigned int pcpu;
  235. if (cpu_isset(lcpu, of_spin_map))
  236. /* Already started by OF and sitting in spin loop */
  237. return 1;
  238. pcpu = get_hard_smp_processor_id(lcpu);
  239. /* Fixup atomic count: it exited inside IRQ handler. */
  240. paca[lcpu].__current->thread_info->preempt_count = 0;
  241. status = rtas_call(rtas_token("start-cpu"), 3, 1, NULL,
  242. pcpu, start_here, lcpu);
  243. if (status != 0) {
  244. printk(KERN_ERR "start-cpu failed: %i\n", status);
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. static inline void smp_xics_do_message(int cpu, int msg)
  250. {
  251. set_bit(msg, &xics_ipi_message[cpu].value);
  252. mb();
  253. xics_cause_IPI(cpu);
  254. }
  255. static void smp_xics_message_pass(int target, int msg)
  256. {
  257. unsigned int i;
  258. if (target < NR_CPUS) {
  259. smp_xics_do_message(target, msg);
  260. } else {
  261. for_each_online_cpu(i) {
  262. if (target == MSG_ALL_BUT_SELF
  263. && i == smp_processor_id())
  264. continue;
  265. smp_xics_do_message(i, msg);
  266. }
  267. }
  268. }
  269. static int __init smp_xics_probe(void)
  270. {
  271. xics_request_IPIs();
  272. return cpus_weight(cpu_possible_map);
  273. }
  274. static void __devinit smp_xics_setup_cpu(int cpu)
  275. {
  276. if (cpu != boot_cpuid)
  277. xics_setup_cpu();
  278. if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
  279. vpa_init(cpu);
  280. cpu_clear(cpu, of_spin_map);
  281. /*
  282. * Put the calling processor into the GIQ. This is really only
  283. * necessary from a secondary thread as the OF start-cpu interface
  284. * performs this function for us on primary threads.
  285. */
  286. rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
  287. (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
  288. }
  289. static DEFINE_SPINLOCK(timebase_lock);
  290. static unsigned long timebase = 0;
  291. static void __devinit pSeries_give_timebase(void)
  292. {
  293. spin_lock(&timebase_lock);
  294. rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
  295. timebase = get_tb();
  296. spin_unlock(&timebase_lock);
  297. while (timebase)
  298. barrier();
  299. rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
  300. }
  301. static void __devinit pSeries_take_timebase(void)
  302. {
  303. while (!timebase)
  304. barrier();
  305. spin_lock(&timebase_lock);
  306. set_tb(timebase >> 32, timebase & 0xffffffff);
  307. timebase = 0;
  308. spin_unlock(&timebase_lock);
  309. }
  310. static void __devinit smp_pSeries_kick_cpu(int nr)
  311. {
  312. BUG_ON(nr < 0 || nr >= NR_CPUS);
  313. if (!smp_startup_cpu(nr))
  314. return;
  315. /*
  316. * The processor is currently spinning, waiting for the
  317. * cpu_start field to become non-zero After we set cpu_start,
  318. * the processor will continue on to secondary_start
  319. */
  320. paca[nr].cpu_start = 1;
  321. }
  322. static int smp_pSeries_cpu_bootable(unsigned int nr)
  323. {
  324. /* Special case - we inhibit secondary thread startup
  325. * during boot if the user requests it. Odd-numbered
  326. * cpus are assumed to be secondary threads.
  327. */
  328. if (system_state < SYSTEM_RUNNING &&
  329. cur_cpu_spec->cpu_features & CPU_FTR_SMT &&
  330. !smt_enabled_at_boot && nr % 2 != 0)
  331. return 0;
  332. return 1;
  333. }
  334. static struct smp_ops_t pSeries_mpic_smp_ops = {
  335. .message_pass = smp_mpic_message_pass,
  336. .probe = smp_mpic_probe,
  337. .kick_cpu = smp_pSeries_kick_cpu,
  338. .setup_cpu = smp_mpic_setup_cpu,
  339. };
  340. static struct smp_ops_t pSeries_xics_smp_ops = {
  341. .message_pass = smp_xics_message_pass,
  342. .probe = smp_xics_probe,
  343. .kick_cpu = smp_pSeries_kick_cpu,
  344. .setup_cpu = smp_xics_setup_cpu,
  345. .cpu_bootable = smp_pSeries_cpu_bootable,
  346. };
  347. /* This is called very early */
  348. void __init smp_init_pSeries(void)
  349. {
  350. int i;
  351. DBG(" -> smp_init_pSeries()\n");
  352. if (ppc64_interrupt_controller == IC_OPEN_PIC)
  353. smp_ops = &pSeries_mpic_smp_ops;
  354. else
  355. smp_ops = &pSeries_xics_smp_ops;
  356. #ifdef CONFIG_HOTPLUG_CPU
  357. smp_ops->cpu_disable = pSeries_cpu_disable;
  358. smp_ops->cpu_die = pSeries_cpu_die;
  359. /* Processors can be added/removed only on LPAR */
  360. if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
  361. pSeries_reconfig_notifier_register(&pSeries_smp_nb);
  362. #endif
  363. /* Mark threads which are still spinning in hold loops. */
  364. if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
  365. for_each_present_cpu(i) {
  366. if (i % 2 == 0)
  367. /*
  368. * Even-numbered logical cpus correspond to
  369. * primary threads.
  370. */
  371. cpu_set(i, of_spin_map);
  372. }
  373. else
  374. of_spin_map = cpu_present_map;
  375. cpu_clear(boot_cpuid, of_spin_map);
  376. /* Non-lpar has additional take/give timebase */
  377. if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
  378. smp_ops->give_timebase = pSeries_give_timebase;
  379. smp_ops->take_timebase = pSeries_take_timebase;
  380. }
  381. DBG(" <- smp_init_pSeries()\n");
  382. }