pSeries_smp.c 12 KB

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