hotplug-cpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/delay.h>
  22. #include <linux/cpu.h>
  23. #include <asm/system.h>
  24. #include <asm/prom.h>
  25. #include <asm/rtas.h>
  26. #include <asm/firmware.h>
  27. #include <asm/machdep.h>
  28. #include <asm/vdso_datapage.h>
  29. #include <asm/pSeries_reconfig.h>
  30. #include "xics.h"
  31. #include "plpar_wrappers.h"
  32. #include "offline_states.h"
  33. /* This version can't take the spinlock, because it never returns */
  34. static struct rtas_args rtas_stop_self_args = {
  35. .token = RTAS_UNKNOWN_SERVICE,
  36. .nargs = 0,
  37. .nret = 1,
  38. .rets = &rtas_stop_self_args.args[0],
  39. };
  40. static DEFINE_PER_CPU(enum cpu_state_vals, preferred_offline_state) =
  41. CPU_STATE_OFFLINE;
  42. static DEFINE_PER_CPU(enum cpu_state_vals, current_state) = CPU_STATE_OFFLINE;
  43. static enum cpu_state_vals default_offline_state = CPU_STATE_OFFLINE;
  44. static int cede_offline_enabled __read_mostly = 1;
  45. /*
  46. * Enable/disable cede_offline when available.
  47. */
  48. static int __init setup_cede_offline(char *str)
  49. {
  50. if (!strcmp(str, "off"))
  51. cede_offline_enabled = 0;
  52. else if (!strcmp(str, "on"))
  53. cede_offline_enabled = 1;
  54. else
  55. return 0;
  56. return 1;
  57. }
  58. __setup("cede_offline=", setup_cede_offline);
  59. enum cpu_state_vals get_cpu_current_state(int cpu)
  60. {
  61. return per_cpu(current_state, cpu);
  62. }
  63. void set_cpu_current_state(int cpu, enum cpu_state_vals state)
  64. {
  65. per_cpu(current_state, cpu) = state;
  66. }
  67. enum cpu_state_vals get_preferred_offline_state(int cpu)
  68. {
  69. return per_cpu(preferred_offline_state, cpu);
  70. }
  71. void set_preferred_offline_state(int cpu, enum cpu_state_vals state)
  72. {
  73. per_cpu(preferred_offline_state, cpu) = state;
  74. }
  75. void set_default_offline_state(int cpu)
  76. {
  77. per_cpu(preferred_offline_state, cpu) = default_offline_state;
  78. }
  79. static void rtas_stop_self(void)
  80. {
  81. struct rtas_args *args = &rtas_stop_self_args;
  82. local_irq_disable();
  83. BUG_ON(args->token == RTAS_UNKNOWN_SERVICE);
  84. printk("cpu %u (hwid %u) Ready to die...\n",
  85. smp_processor_id(), hard_smp_processor_id());
  86. enter_rtas(__pa(args));
  87. panic("Alas, I survived.\n");
  88. }
  89. static void pseries_mach_cpu_die(void)
  90. {
  91. unsigned int cpu = smp_processor_id();
  92. unsigned int hwcpu = hard_smp_processor_id();
  93. u8 cede_latency_hint = 0;
  94. local_irq_disable();
  95. idle_task_exit();
  96. xics_teardown_cpu();
  97. if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  98. set_cpu_current_state(cpu, CPU_STATE_INACTIVE);
  99. cede_latency_hint = 2;
  100. get_lppaca()->idle = 1;
  101. if (!get_lppaca()->shared_proc)
  102. get_lppaca()->donate_dedicated_cpu = 1;
  103. printk(KERN_INFO
  104. "cpu %u (hwid %u) ceding for offline with hint %d\n",
  105. cpu, hwcpu, cede_latency_hint);
  106. while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  107. extended_cede_processor(cede_latency_hint);
  108. printk(KERN_INFO "cpu %u (hwid %u) returned from cede.\n",
  109. cpu, hwcpu);
  110. printk(KERN_INFO
  111. "Decrementer value = %x Timebase value = %llx\n",
  112. get_dec(), get_tb());
  113. }
  114. printk(KERN_INFO "cpu %u (hwid %u) got prodded to go online\n",
  115. cpu, hwcpu);
  116. if (!get_lppaca()->shared_proc)
  117. get_lppaca()->donate_dedicated_cpu = 0;
  118. get_lppaca()->idle = 0;
  119. }
  120. if (get_preferred_offline_state(cpu) == CPU_STATE_ONLINE) {
  121. unregister_slb_shadow(hwcpu, __pa(get_slb_shadow()));
  122. /*
  123. * Call to start_secondary_resume() will not return.
  124. * Kernel stack will be reset and start_secondary()
  125. * will be called to continue the online operation.
  126. */
  127. start_secondary_resume();
  128. } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
  129. set_cpu_current_state(cpu, CPU_STATE_OFFLINE);
  130. unregister_slb_shadow(hard_smp_processor_id(),
  131. __pa(get_slb_shadow()));
  132. rtas_stop_self();
  133. }
  134. /* Should never get here... */
  135. BUG();
  136. for(;;);
  137. }
  138. static int qcss_tok; /* query-cpu-stopped-state token */
  139. /* Get state of physical CPU.
  140. * Return codes:
  141. * 0 - The processor is in the RTAS stopped state
  142. * 1 - stop-self is in progress
  143. * 2 - The processor is not in the RTAS stopped state
  144. * -1 - Hardware Error
  145. * -2 - Hardware Busy, Try again later.
  146. */
  147. static int query_cpu_stopped(unsigned int pcpu)
  148. {
  149. int cpu_status, status;
  150. status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
  151. if (status != 0) {
  152. printk(KERN_ERR
  153. "RTAS query-cpu-stopped-state failed: %i\n", status);
  154. return status;
  155. }
  156. return cpu_status;
  157. }
  158. static int pseries_cpu_disable(void)
  159. {
  160. int cpu = smp_processor_id();
  161. set_cpu_online(cpu, false);
  162. vdso_data->processorCount--;
  163. /*fix boot_cpuid here*/
  164. if (cpu == boot_cpuid)
  165. boot_cpuid = any_online_cpu(cpu_online_map);
  166. /* FIXME: abstract this to not be platform specific later on */
  167. xics_migrate_irqs_away();
  168. return 0;
  169. }
  170. /*
  171. * pseries_cpu_die: Wait for the cpu to die.
  172. * @cpu: logical processor id of the CPU whose death we're awaiting.
  173. *
  174. * This function is called from the context of the thread which is performing
  175. * the cpu-offline. Here we wait for long enough to allow the cpu in question
  176. * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
  177. * notifications.
  178. *
  179. * OTOH, pseries_mach_cpu_die() is called by the @cpu when it wants to
  180. * self-destruct.
  181. */
  182. static void pseries_cpu_die(unsigned int cpu)
  183. {
  184. int tries;
  185. int cpu_status = 1;
  186. unsigned int pcpu = get_hard_smp_processor_id(cpu);
  187. if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  188. cpu_status = 1;
  189. for (tries = 0; tries < 1000; tries++) {
  190. if (get_cpu_current_state(cpu) == CPU_STATE_INACTIVE) {
  191. cpu_status = 0;
  192. break;
  193. }
  194. cpu_relax();
  195. }
  196. } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
  197. for (tries = 0; tries < 25; tries++) {
  198. cpu_status = query_cpu_stopped(pcpu);
  199. if (cpu_status == 0 || cpu_status == -1)
  200. break;
  201. cpu_relax();
  202. }
  203. }
  204. if (cpu_status != 0) {
  205. printk("Querying DEAD? cpu %i (%i) shows %i\n",
  206. cpu, pcpu, cpu_status);
  207. }
  208. /* Isolation and deallocation are definatly done by
  209. * drslot_chrp_cpu. If they were not they would be
  210. * done here. Change isolate state to Isolate and
  211. * change allocation-state to Unusable.
  212. */
  213. paca[cpu].cpu_start = 0;
  214. }
  215. /*
  216. * Update cpu_present_map and paca(s) for a new cpu node. The wrinkle
  217. * here is that a cpu device node may represent up to two logical cpus
  218. * in the SMT case. We must honor the assumption in other code that
  219. * the logical ids for sibling SMT threads x and y are adjacent, such
  220. * that x^1 == y and y^1 == x.
  221. */
  222. static int pseries_add_processor(struct device_node *np)
  223. {
  224. unsigned int cpu;
  225. cpumask_t candidate_map, tmp = CPU_MASK_NONE;
  226. int err = -ENOSPC, len, nthreads, i;
  227. const u32 *intserv;
  228. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  229. if (!intserv)
  230. return 0;
  231. nthreads = len / sizeof(u32);
  232. for (i = 0; i < nthreads; i++)
  233. cpu_set(i, tmp);
  234. cpu_maps_update_begin();
  235. BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
  236. /* Get a bitmap of unoccupied slots. */
  237. cpus_xor(candidate_map, cpu_possible_map, cpu_present_map);
  238. if (cpus_empty(candidate_map)) {
  239. /* If we get here, it most likely means that NR_CPUS is
  240. * less than the partition's max processors setting.
  241. */
  242. printk(KERN_ERR "Cannot add cpu %s; this system configuration"
  243. " supports %d logical cpus.\n", np->full_name,
  244. cpus_weight(cpu_possible_map));
  245. goto out_unlock;
  246. }
  247. while (!cpus_empty(tmp))
  248. if (cpus_subset(tmp, candidate_map))
  249. /* Found a range where we can insert the new cpu(s) */
  250. break;
  251. else
  252. cpus_shift_left(tmp, tmp, nthreads);
  253. if (cpus_empty(tmp)) {
  254. printk(KERN_ERR "Unable to find space in cpu_present_map for"
  255. " processor %s with %d thread(s)\n", np->name,
  256. nthreads);
  257. goto out_unlock;
  258. }
  259. for_each_cpu_mask(cpu, tmp) {
  260. BUG_ON(cpu_isset(cpu, cpu_present_map));
  261. set_cpu_present(cpu, true);
  262. set_hard_smp_processor_id(cpu, *intserv++);
  263. }
  264. err = 0;
  265. out_unlock:
  266. cpu_maps_update_done();
  267. return err;
  268. }
  269. /*
  270. * Update the present map for a cpu node which is going away, and set
  271. * the hard id in the paca(s) to -1 to be consistent with boot time
  272. * convention for non-present cpus.
  273. */
  274. static void pseries_remove_processor(struct device_node *np)
  275. {
  276. unsigned int cpu;
  277. int len, nthreads, i;
  278. const u32 *intserv;
  279. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  280. if (!intserv)
  281. return;
  282. nthreads = len / sizeof(u32);
  283. cpu_maps_update_begin();
  284. for (i = 0; i < nthreads; i++) {
  285. for_each_present_cpu(cpu) {
  286. if (get_hard_smp_processor_id(cpu) != intserv[i])
  287. continue;
  288. BUG_ON(cpu_online(cpu));
  289. set_cpu_present(cpu, false);
  290. set_hard_smp_processor_id(cpu, -1);
  291. break;
  292. }
  293. if (cpu == NR_CPUS)
  294. printk(KERN_WARNING "Could not find cpu to remove "
  295. "with physical id 0x%x\n", intserv[i]);
  296. }
  297. cpu_maps_update_done();
  298. }
  299. static int pseries_smp_notifier(struct notifier_block *nb,
  300. unsigned long action, void *node)
  301. {
  302. int err = NOTIFY_OK;
  303. switch (action) {
  304. case PSERIES_RECONFIG_ADD:
  305. if (pseries_add_processor(node))
  306. err = NOTIFY_BAD;
  307. break;
  308. case PSERIES_RECONFIG_REMOVE:
  309. pseries_remove_processor(node);
  310. break;
  311. default:
  312. err = NOTIFY_DONE;
  313. break;
  314. }
  315. return err;
  316. }
  317. static struct notifier_block pseries_smp_nb = {
  318. .notifier_call = pseries_smp_notifier,
  319. };
  320. #define MAX_CEDE_LATENCY_LEVELS 4
  321. #define CEDE_LATENCY_PARAM_LENGTH 10
  322. #define CEDE_LATENCY_PARAM_MAX_LENGTH \
  323. (MAX_CEDE_LATENCY_LEVELS * CEDE_LATENCY_PARAM_LENGTH * sizeof(char))
  324. #define CEDE_LATENCY_TOKEN 45
  325. static char cede_parameters[CEDE_LATENCY_PARAM_MAX_LENGTH];
  326. static int parse_cede_parameters(void)
  327. {
  328. memset(cede_parameters, 0, CEDE_LATENCY_PARAM_MAX_LENGTH);
  329. return rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  330. NULL,
  331. CEDE_LATENCY_TOKEN,
  332. __pa(cede_parameters),
  333. CEDE_LATENCY_PARAM_MAX_LENGTH);
  334. }
  335. static int __init pseries_cpu_hotplug_init(void)
  336. {
  337. struct device_node *np;
  338. const char *typep;
  339. int cpu;
  340. for_each_node_by_name(np, "interrupt-controller") {
  341. typep = of_get_property(np, "compatible", NULL);
  342. if (strstr(typep, "open-pic")) {
  343. of_node_put(np);
  344. printk(KERN_INFO "CPU Hotplug not supported on "
  345. "systems using MPIC\n");
  346. return 0;
  347. }
  348. }
  349. rtas_stop_self_args.token = rtas_token("stop-self");
  350. qcss_tok = rtas_token("query-cpu-stopped-state");
  351. if (rtas_stop_self_args.token == RTAS_UNKNOWN_SERVICE ||
  352. qcss_tok == RTAS_UNKNOWN_SERVICE) {
  353. printk(KERN_INFO "CPU Hotplug not supported by firmware "
  354. "- disabling.\n");
  355. return 0;
  356. }
  357. ppc_md.cpu_die = pseries_mach_cpu_die;
  358. smp_ops->cpu_disable = pseries_cpu_disable;
  359. smp_ops->cpu_die = pseries_cpu_die;
  360. /* Processors can be added/removed only on LPAR */
  361. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  362. pSeries_reconfig_notifier_register(&pseries_smp_nb);
  363. cpu_maps_update_begin();
  364. if (cede_offline_enabled && parse_cede_parameters() == 0) {
  365. default_offline_state = CPU_STATE_INACTIVE;
  366. for_each_online_cpu(cpu)
  367. set_default_offline_state(cpu);
  368. }
  369. cpu_maps_update_done();
  370. }
  371. return 0;
  372. }
  373. arch_initcall(pseries_cpu_hotplug_init);