pSeries_smp.c 12 KB

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