hotplug-cpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * pseries CPU Hotplug infrastructure.
  3. *
  4. * Split out from arch/powerpc/platforms/pseries/setup.c
  5. * arch/powerpc/kernel/rtas.c, and arch/powerpc/platforms/pseries/smp.c
  6. *
  7. * Peter Bergner, IBM March 2001.
  8. * Copyright (C) 2001 IBM.
  9. * Dave Engebretsen, Peter Bergner, and
  10. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  11. * Plus various changes from other IBM teams...
  12. *
  13. * Copyright (C) 2006 Michael Ellerman, IBM Corporation
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/sched.h> /* for idle_task_exit */
  24. #include <linux/cpu.h>
  25. #include <linux/of.h>
  26. #include <asm/prom.h>
  27. #include <asm/rtas.h>
  28. #include <asm/firmware.h>
  29. #include <asm/machdep.h>
  30. #include <asm/vdso_datapage.h>
  31. #include <asm/xics.h>
  32. #include <asm/plpar_wrappers.h>
  33. #include "offline_states.h"
  34. /* This version can't take the spinlock, because it never returns */
  35. static int rtas_stop_self_token = RTAS_UNKNOWN_SERVICE;
  36. static DEFINE_PER_CPU(enum cpu_state_vals, preferred_offline_state) =
  37. CPU_STATE_OFFLINE;
  38. static DEFINE_PER_CPU(enum cpu_state_vals, current_state) = CPU_STATE_OFFLINE;
  39. static enum cpu_state_vals default_offline_state = CPU_STATE_OFFLINE;
  40. static int cede_offline_enabled __read_mostly = 1;
  41. /*
  42. * Enable/disable cede_offline when available.
  43. */
  44. static int __init setup_cede_offline(char *str)
  45. {
  46. if (!strcmp(str, "off"))
  47. cede_offline_enabled = 0;
  48. else if (!strcmp(str, "on"))
  49. cede_offline_enabled = 1;
  50. else
  51. return 0;
  52. return 1;
  53. }
  54. __setup("cede_offline=", setup_cede_offline);
  55. enum cpu_state_vals get_cpu_current_state(int cpu)
  56. {
  57. return per_cpu(current_state, cpu);
  58. }
  59. void set_cpu_current_state(int cpu, enum cpu_state_vals state)
  60. {
  61. per_cpu(current_state, cpu) = state;
  62. }
  63. enum cpu_state_vals get_preferred_offline_state(int cpu)
  64. {
  65. return per_cpu(preferred_offline_state, cpu);
  66. }
  67. void set_preferred_offline_state(int cpu, enum cpu_state_vals state)
  68. {
  69. per_cpu(preferred_offline_state, cpu) = state;
  70. }
  71. void set_default_offline_state(int cpu)
  72. {
  73. per_cpu(preferred_offline_state, cpu) = default_offline_state;
  74. }
  75. static void rtas_stop_self(void)
  76. {
  77. static struct rtas_args args = {
  78. .nargs = 0,
  79. .nret = cpu_to_be32(1),
  80. .rets = &args.args[0],
  81. };
  82. args.token = cpu_to_be32(rtas_stop_self_token);
  83. local_irq_disable();
  84. BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
  85. printk("cpu %u (hwid %u) Ready to die...\n",
  86. smp_processor_id(), hard_smp_processor_id());
  87. enter_rtas(__pa(&args));
  88. panic("Alas, I survived.\n");
  89. }
  90. static void pseries_mach_cpu_die(void)
  91. {
  92. unsigned int cpu = smp_processor_id();
  93. unsigned int hwcpu = hard_smp_processor_id();
  94. u8 cede_latency_hint = 0;
  95. local_irq_disable();
  96. idle_task_exit();
  97. xics_teardown_cpu();
  98. if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  99. set_cpu_current_state(cpu, CPU_STATE_INACTIVE);
  100. if (ppc_md.suspend_disable_cpu)
  101. ppc_md.suspend_disable_cpu();
  102. cede_latency_hint = 2;
  103. get_lppaca()->idle = 1;
  104. if (!lppaca_shared_proc(get_lppaca()))
  105. get_lppaca()->donate_dedicated_cpu = 1;
  106. while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  107. while (!prep_irq_for_idle()) {
  108. local_irq_enable();
  109. local_irq_disable();
  110. }
  111. extended_cede_processor(cede_latency_hint);
  112. }
  113. local_irq_disable();
  114. if (!lppaca_shared_proc(get_lppaca()))
  115. get_lppaca()->donate_dedicated_cpu = 0;
  116. get_lppaca()->idle = 0;
  117. if (get_preferred_offline_state(cpu) == CPU_STATE_ONLINE) {
  118. unregister_slb_shadow(hwcpu);
  119. hard_irq_disable();
  120. /*
  121. * Call to start_secondary_resume() will not return.
  122. * Kernel stack will be reset and start_secondary()
  123. * will be called to continue the online operation.
  124. */
  125. start_secondary_resume();
  126. }
  127. }
  128. /* Requested state is CPU_STATE_OFFLINE at this point */
  129. WARN_ON(get_preferred_offline_state(cpu) != CPU_STATE_OFFLINE);
  130. set_cpu_current_state(cpu, CPU_STATE_OFFLINE);
  131. unregister_slb_shadow(hwcpu);
  132. rtas_stop_self();
  133. /* Should never get here... */
  134. BUG();
  135. for(;;);
  136. }
  137. static int pseries_cpu_disable(void)
  138. {
  139. int cpu = smp_processor_id();
  140. set_cpu_online(cpu, false);
  141. vdso_data->processorCount--;
  142. /*fix boot_cpuid here*/
  143. if (cpu == boot_cpuid)
  144. boot_cpuid = cpumask_any(cpu_online_mask);
  145. /* FIXME: abstract this to not be platform specific later on */
  146. xics_migrate_irqs_away();
  147. return 0;
  148. }
  149. /*
  150. * pseries_cpu_die: Wait for the cpu to die.
  151. * @cpu: logical processor id of the CPU whose death we're awaiting.
  152. *
  153. * This function is called from the context of the thread which is performing
  154. * the cpu-offline. Here we wait for long enough to allow the cpu in question
  155. * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
  156. * notifications.
  157. *
  158. * OTOH, pseries_mach_cpu_die() is called by the @cpu when it wants to
  159. * self-destruct.
  160. */
  161. static void pseries_cpu_die(unsigned int cpu)
  162. {
  163. int tries;
  164. int cpu_status = 1;
  165. unsigned int pcpu = get_hard_smp_processor_id(cpu);
  166. if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  167. cpu_status = 1;
  168. for (tries = 0; tries < 5000; tries++) {
  169. if (get_cpu_current_state(cpu) == CPU_STATE_INACTIVE) {
  170. cpu_status = 0;
  171. break;
  172. }
  173. msleep(1);
  174. }
  175. } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
  176. for (tries = 0; tries < 25; tries++) {
  177. cpu_status = smp_query_cpu_stopped(pcpu);
  178. if (cpu_status == QCSS_STOPPED ||
  179. cpu_status == QCSS_HARDWARE_ERROR)
  180. break;
  181. cpu_relax();
  182. }
  183. }
  184. if (cpu_status != 0) {
  185. printk("Querying DEAD? cpu %i (%i) shows %i\n",
  186. cpu, pcpu, cpu_status);
  187. }
  188. /* Isolation and deallocation are definitely done by
  189. * drslot_chrp_cpu. If they were not they would be
  190. * done here. Change isolate state to Isolate and
  191. * change allocation-state to Unusable.
  192. */
  193. paca[cpu].cpu_start = 0;
  194. }
  195. /*
  196. * Update cpu_present_mask and paca(s) for a new cpu node. The wrinkle
  197. * here is that a cpu device node may represent up to two logical cpus
  198. * in the SMT case. We must honor the assumption in other code that
  199. * the logical ids for sibling SMT threads x and y are adjacent, such
  200. * that x^1 == y and y^1 == x.
  201. */
  202. static int pseries_add_processor(struct device_node *np)
  203. {
  204. unsigned int cpu;
  205. cpumask_var_t candidate_mask, tmp;
  206. int err = -ENOSPC, len, nthreads, i;
  207. const __be32 *intserv;
  208. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  209. if (!intserv)
  210. return 0;
  211. zalloc_cpumask_var(&candidate_mask, GFP_KERNEL);
  212. zalloc_cpumask_var(&tmp, GFP_KERNEL);
  213. nthreads = len / sizeof(u32);
  214. for (i = 0; i < nthreads; i++)
  215. cpumask_set_cpu(i, tmp);
  216. cpu_maps_update_begin();
  217. BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
  218. /* Get a bitmap of unoccupied slots. */
  219. cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
  220. if (cpumask_empty(candidate_mask)) {
  221. /* If we get here, it most likely means that NR_CPUS is
  222. * less than the partition's max processors setting.
  223. */
  224. printk(KERN_ERR "Cannot add cpu %s; this system configuration"
  225. " supports %d logical cpus.\n", np->full_name,
  226. num_possible_cpus());
  227. goto out_unlock;
  228. }
  229. while (!cpumask_empty(tmp))
  230. if (cpumask_subset(tmp, candidate_mask))
  231. /* Found a range where we can insert the new cpu(s) */
  232. break;
  233. else
  234. cpumask_shift_left(tmp, tmp, nthreads);
  235. if (cpumask_empty(tmp)) {
  236. printk(KERN_ERR "Unable to find space in cpu_present_mask for"
  237. " processor %s with %d thread(s)\n", np->name,
  238. nthreads);
  239. goto out_unlock;
  240. }
  241. for_each_cpu(cpu, tmp) {
  242. BUG_ON(cpu_present(cpu));
  243. set_cpu_present(cpu, true);
  244. set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
  245. }
  246. err = 0;
  247. out_unlock:
  248. cpu_maps_update_done();
  249. free_cpumask_var(candidate_mask);
  250. free_cpumask_var(tmp);
  251. return err;
  252. }
  253. /*
  254. * Update the present map for a cpu node which is going away, and set
  255. * the hard id in the paca(s) to -1 to be consistent with boot time
  256. * convention for non-present cpus.
  257. */
  258. static void pseries_remove_processor(struct device_node *np)
  259. {
  260. unsigned int cpu;
  261. int len, nthreads, i;
  262. const __be32 *intserv;
  263. u32 thread;
  264. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  265. if (!intserv)
  266. return;
  267. nthreads = len / sizeof(u32);
  268. cpu_maps_update_begin();
  269. for (i = 0; i < nthreads; i++) {
  270. thread = be32_to_cpu(intserv[i]);
  271. for_each_present_cpu(cpu) {
  272. if (get_hard_smp_processor_id(cpu) != thread)
  273. continue;
  274. BUG_ON(cpu_online(cpu));
  275. set_cpu_present(cpu, false);
  276. set_hard_smp_processor_id(cpu, -1);
  277. break;
  278. }
  279. if (cpu >= nr_cpu_ids)
  280. printk(KERN_WARNING "Could not find cpu to remove "
  281. "with physical id 0x%x\n", thread);
  282. }
  283. cpu_maps_update_done();
  284. }
  285. static int pseries_smp_notifier(struct notifier_block *nb,
  286. unsigned long action, void *data)
  287. {
  288. struct of_reconfig_data *rd = data;
  289. int err = 0;
  290. switch (action) {
  291. case OF_RECONFIG_ATTACH_NODE:
  292. err = pseries_add_processor(rd->dn);
  293. break;
  294. case OF_RECONFIG_DETACH_NODE:
  295. pseries_remove_processor(rd->dn);
  296. break;
  297. }
  298. return notifier_from_errno(err);
  299. }
  300. static struct notifier_block pseries_smp_nb = {
  301. .notifier_call = pseries_smp_notifier,
  302. };
  303. #define MAX_CEDE_LATENCY_LEVELS 4
  304. #define CEDE_LATENCY_PARAM_LENGTH 10
  305. #define CEDE_LATENCY_PARAM_MAX_LENGTH \
  306. (MAX_CEDE_LATENCY_LEVELS * CEDE_LATENCY_PARAM_LENGTH * sizeof(char))
  307. #define CEDE_LATENCY_TOKEN 45
  308. static char cede_parameters[CEDE_LATENCY_PARAM_MAX_LENGTH];
  309. static int parse_cede_parameters(void)
  310. {
  311. memset(cede_parameters, 0, CEDE_LATENCY_PARAM_MAX_LENGTH);
  312. return rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  313. NULL,
  314. CEDE_LATENCY_TOKEN,
  315. __pa(cede_parameters),
  316. CEDE_LATENCY_PARAM_MAX_LENGTH);
  317. }
  318. static int __init pseries_cpu_hotplug_init(void)
  319. {
  320. struct device_node *np;
  321. const char *typep;
  322. int cpu;
  323. int qcss_tok;
  324. for_each_node_by_name(np, "interrupt-controller") {
  325. typep = of_get_property(np, "compatible", NULL);
  326. if (strstr(typep, "open-pic")) {
  327. of_node_put(np);
  328. printk(KERN_INFO "CPU Hotplug not supported on "
  329. "systems using MPIC\n");
  330. return 0;
  331. }
  332. }
  333. rtas_stop_self_token = rtas_token("stop-self");
  334. qcss_tok = rtas_token("query-cpu-stopped-state");
  335. if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
  336. qcss_tok == RTAS_UNKNOWN_SERVICE) {
  337. printk(KERN_INFO "CPU Hotplug not supported by firmware "
  338. "- disabling.\n");
  339. return 0;
  340. }
  341. ppc_md.cpu_die = pseries_mach_cpu_die;
  342. smp_ops->cpu_disable = pseries_cpu_disable;
  343. smp_ops->cpu_die = pseries_cpu_die;
  344. /* Processors can be added/removed only on LPAR */
  345. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  346. of_reconfig_notifier_register(&pseries_smp_nb);
  347. cpu_maps_update_begin();
  348. if (cede_offline_enabled && parse_cede_parameters() == 0) {
  349. default_offline_state = CPU_STATE_INACTIVE;
  350. for_each_online_cpu(cpu)
  351. set_default_offline_state(cpu);
  352. }
  353. cpu_maps_update_done();
  354. }
  355. return 0;
  356. }
  357. machine_arch_initcall(pseries, pseries_cpu_hotplug_init);