hotplug-cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * NOTE: Calling start_secondary() here for now to
  124. * start new context.
  125. * However, need to do it cleanly by resetting the
  126. * stack pointer.
  127. */
  128. start_secondary();
  129. } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
  130. set_cpu_current_state(cpu, CPU_STATE_OFFLINE);
  131. unregister_slb_shadow(hard_smp_processor_id(),
  132. __pa(get_slb_shadow()));
  133. rtas_stop_self();
  134. }
  135. /* Should never get here... */
  136. BUG();
  137. for(;;);
  138. }
  139. static int qcss_tok; /* query-cpu-stopped-state token */
  140. /* Get state of physical CPU.
  141. * Return codes:
  142. * 0 - The processor is in the RTAS stopped state
  143. * 1 - stop-self is in progress
  144. * 2 - The processor is not in the RTAS stopped state
  145. * -1 - Hardware Error
  146. * -2 - Hardware Busy, Try again later.
  147. */
  148. static int query_cpu_stopped(unsigned int pcpu)
  149. {
  150. int cpu_status, status;
  151. status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
  152. if (status != 0) {
  153. printk(KERN_ERR
  154. "RTAS query-cpu-stopped-state failed: %i\n", status);
  155. return status;
  156. }
  157. return cpu_status;
  158. }
  159. static int pseries_cpu_disable(void)
  160. {
  161. int cpu = smp_processor_id();
  162. set_cpu_online(cpu, false);
  163. vdso_data->processorCount--;
  164. /*fix boot_cpuid here*/
  165. if (cpu == boot_cpuid)
  166. boot_cpuid = any_online_cpu(cpu_online_map);
  167. /* FIXME: abstract this to not be platform specific later on */
  168. xics_migrate_irqs_away();
  169. return 0;
  170. }
  171. /*
  172. * pseries_cpu_die: Wait for the cpu to die.
  173. * @cpu: logical processor id of the CPU whose death we're awaiting.
  174. *
  175. * This function is called from the context of the thread which is performing
  176. * the cpu-offline. Here we wait for long enough to allow the cpu in question
  177. * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
  178. * notifications.
  179. *
  180. * OTOH, pseries_mach_cpu_die() is called by the @cpu when it wants to
  181. * self-destruct.
  182. */
  183. static void pseries_cpu_die(unsigned int cpu)
  184. {
  185. int tries;
  186. int cpu_status = 1;
  187. unsigned int pcpu = get_hard_smp_processor_id(cpu);
  188. if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  189. cpu_status = 1;
  190. for (tries = 0; tries < 1000; tries++) {
  191. if (get_cpu_current_state(cpu) == CPU_STATE_INACTIVE) {
  192. cpu_status = 0;
  193. break;
  194. }
  195. cpu_relax();
  196. }
  197. } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
  198. for (tries = 0; tries < 25; tries++) {
  199. cpu_status = query_cpu_stopped(pcpu);
  200. if (cpu_status == 0 || cpu_status == -1)
  201. break;
  202. cpu_relax();
  203. }
  204. }
  205. if (cpu_status != 0) {
  206. printk("Querying DEAD? cpu %i (%i) shows %i\n",
  207. cpu, pcpu, cpu_status);
  208. }
  209. /* Isolation and deallocation are definatly done by
  210. * drslot_chrp_cpu. If they were not they would be
  211. * done here. Change isolate state to Isolate and
  212. * change allocation-state to Unusable.
  213. */
  214. paca[cpu].cpu_start = 0;
  215. }
  216. /*
  217. * Update cpu_present_map and paca(s) for a new cpu node. The wrinkle
  218. * here is that a cpu device node may represent up to two logical cpus
  219. * in the SMT case. We must honor the assumption in other code that
  220. * the logical ids for sibling SMT threads x and y are adjacent, such
  221. * that x^1 == y and y^1 == x.
  222. */
  223. static int pseries_add_processor(struct device_node *np)
  224. {
  225. unsigned int cpu;
  226. cpumask_t candidate_map, tmp = CPU_MASK_NONE;
  227. int err = -ENOSPC, len, nthreads, i;
  228. const u32 *intserv;
  229. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  230. if (!intserv)
  231. return 0;
  232. nthreads = len / sizeof(u32);
  233. for (i = 0; i < nthreads; i++)
  234. cpu_set(i, tmp);
  235. cpu_maps_update_begin();
  236. BUG_ON(!cpus_subset(cpu_present_map, cpu_possible_map));
  237. /* Get a bitmap of unoccupied slots. */
  238. cpus_xor(candidate_map, cpu_possible_map, cpu_present_map);
  239. if (cpus_empty(candidate_map)) {
  240. /* If we get here, it most likely means that NR_CPUS is
  241. * less than the partition's max processors setting.
  242. */
  243. printk(KERN_ERR "Cannot add cpu %s; this system configuration"
  244. " supports %d logical cpus.\n", np->full_name,
  245. cpus_weight(cpu_possible_map));
  246. goto out_unlock;
  247. }
  248. while (!cpus_empty(tmp))
  249. if (cpus_subset(tmp, candidate_map))
  250. /* Found a range where we can insert the new cpu(s) */
  251. break;
  252. else
  253. cpus_shift_left(tmp, tmp, nthreads);
  254. if (cpus_empty(tmp)) {
  255. printk(KERN_ERR "Unable to find space in cpu_present_map for"
  256. " processor %s with %d thread(s)\n", np->name,
  257. nthreads);
  258. goto out_unlock;
  259. }
  260. for_each_cpu_mask(cpu, tmp) {
  261. BUG_ON(cpu_isset(cpu, cpu_present_map));
  262. set_cpu_present(cpu, true);
  263. set_hard_smp_processor_id(cpu, *intserv++);
  264. }
  265. err = 0;
  266. out_unlock:
  267. cpu_maps_update_done();
  268. return err;
  269. }
  270. /*
  271. * Update the present map for a cpu node which is going away, and set
  272. * the hard id in the paca(s) to -1 to be consistent with boot time
  273. * convention for non-present cpus.
  274. */
  275. static void pseries_remove_processor(struct device_node *np)
  276. {
  277. unsigned int cpu;
  278. int len, nthreads, i;
  279. const u32 *intserv;
  280. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  281. if (!intserv)
  282. return;
  283. nthreads = len / sizeof(u32);
  284. cpu_maps_update_begin();
  285. for (i = 0; i < nthreads; i++) {
  286. for_each_present_cpu(cpu) {
  287. if (get_hard_smp_processor_id(cpu) != intserv[i])
  288. continue;
  289. BUG_ON(cpu_online(cpu));
  290. set_cpu_present(cpu, false);
  291. set_hard_smp_processor_id(cpu, -1);
  292. break;
  293. }
  294. if (cpu == NR_CPUS)
  295. printk(KERN_WARNING "Could not find cpu to remove "
  296. "with physical id 0x%x\n", intserv[i]);
  297. }
  298. cpu_maps_update_done();
  299. }
  300. static int pseries_smp_notifier(struct notifier_block *nb,
  301. unsigned long action, void *node)
  302. {
  303. int err = NOTIFY_OK;
  304. switch (action) {
  305. case PSERIES_RECONFIG_ADD:
  306. if (pseries_add_processor(node))
  307. err = NOTIFY_BAD;
  308. break;
  309. case PSERIES_RECONFIG_REMOVE:
  310. pseries_remove_processor(node);
  311. break;
  312. default:
  313. err = NOTIFY_DONE;
  314. break;
  315. }
  316. return err;
  317. }
  318. static struct notifier_block pseries_smp_nb = {
  319. .notifier_call = pseries_smp_notifier,
  320. };
  321. #define MAX_CEDE_LATENCY_LEVELS 4
  322. #define CEDE_LATENCY_PARAM_LENGTH 10
  323. #define CEDE_LATENCY_PARAM_MAX_LENGTH \
  324. (MAX_CEDE_LATENCY_LEVELS * CEDE_LATENCY_PARAM_LENGTH * sizeof(char))
  325. #define CEDE_LATENCY_TOKEN 45
  326. static char cede_parameters[CEDE_LATENCY_PARAM_MAX_LENGTH];
  327. static int parse_cede_parameters(void)
  328. {
  329. int call_status;
  330. memset(cede_parameters, 0, CEDE_LATENCY_PARAM_MAX_LENGTH);
  331. call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  332. NULL,
  333. CEDE_LATENCY_TOKEN,
  334. __pa(cede_parameters),
  335. CEDE_LATENCY_PARAM_MAX_LENGTH);
  336. if (call_status != 0)
  337. printk(KERN_INFO "CEDE_LATENCY: \
  338. %s %s Error calling get-system-parameter(0x%x)\n",
  339. __FILE__, __func__, call_status);
  340. else
  341. printk(KERN_INFO "CEDE_LATENCY: \
  342. get-system-parameter successful.\n");
  343. return call_status;
  344. }
  345. static int __init pseries_cpu_hotplug_init(void)
  346. {
  347. struct device_node *np;
  348. const char *typep;
  349. int cpu;
  350. for_each_node_by_name(np, "interrupt-controller") {
  351. typep = of_get_property(np, "compatible", NULL);
  352. if (strstr(typep, "open-pic")) {
  353. of_node_put(np);
  354. printk(KERN_INFO "CPU Hotplug not supported on "
  355. "systems using MPIC\n");
  356. return 0;
  357. }
  358. }
  359. rtas_stop_self_args.token = rtas_token("stop-self");
  360. qcss_tok = rtas_token("query-cpu-stopped-state");
  361. if (rtas_stop_self_args.token == RTAS_UNKNOWN_SERVICE ||
  362. qcss_tok == RTAS_UNKNOWN_SERVICE) {
  363. printk(KERN_INFO "CPU Hotplug not supported by firmware "
  364. "- disabling.\n");
  365. return 0;
  366. }
  367. ppc_md.cpu_die = pseries_mach_cpu_die;
  368. smp_ops->cpu_disable = pseries_cpu_disable;
  369. smp_ops->cpu_die = pseries_cpu_die;
  370. /* Processors can be added/removed only on LPAR */
  371. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  372. pSeries_reconfig_notifier_register(&pseries_smp_nb);
  373. cpu_maps_update_begin();
  374. if (cede_offline_enabled && parse_cede_parameters() == 0) {
  375. default_offline_state = CPU_STATE_INACTIVE;
  376. for_each_online_cpu(cpu)
  377. set_default_offline_state(cpu);
  378. }
  379. cpu_maps_update_done();
  380. }
  381. return 0;
  382. }
  383. arch_initcall(pseries_cpu_hotplug_init);