hotplug-cpu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. #define pr_fmt(fmt) "pseries-hotplug-cpu: " fmt
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/sched.h> /* for idle_task_exit */
  25. #include <linux/cpu.h>
  26. #include <linux/of.h>
  27. #include <linux/slab.h>
  28. #include <asm/prom.h>
  29. #include <asm/rtas.h>
  30. #include <asm/firmware.h>
  31. #include <asm/machdep.h>
  32. #include <asm/vdso_datapage.h>
  33. #include <asm/xics.h>
  34. #include <asm/plpar_wrappers.h>
  35. #include "pseries.h"
  36. #include "offline_states.h"
  37. /* This version can't take the spinlock, because it never returns */
  38. static int rtas_stop_self_token = RTAS_UNKNOWN_SERVICE;
  39. static DEFINE_PER_CPU(enum cpu_state_vals, preferred_offline_state) =
  40. CPU_STATE_OFFLINE;
  41. static DEFINE_PER_CPU(enum cpu_state_vals, current_state) = CPU_STATE_OFFLINE;
  42. static enum cpu_state_vals default_offline_state = CPU_STATE_OFFLINE;
  43. static bool cede_offline_enabled __read_mostly = true;
  44. /*
  45. * Enable/disable cede_offline when available.
  46. */
  47. static int __init setup_cede_offline(char *str)
  48. {
  49. return (kstrtobool(str, &cede_offline_enabled) == 0);
  50. }
  51. __setup("cede_offline=", setup_cede_offline);
  52. enum cpu_state_vals get_cpu_current_state(int cpu)
  53. {
  54. return per_cpu(current_state, cpu);
  55. }
  56. void set_cpu_current_state(int cpu, enum cpu_state_vals state)
  57. {
  58. per_cpu(current_state, cpu) = state;
  59. }
  60. enum cpu_state_vals get_preferred_offline_state(int cpu)
  61. {
  62. return per_cpu(preferred_offline_state, cpu);
  63. }
  64. void set_preferred_offline_state(int cpu, enum cpu_state_vals state)
  65. {
  66. per_cpu(preferred_offline_state, cpu) = state;
  67. }
  68. void set_default_offline_state(int cpu)
  69. {
  70. per_cpu(preferred_offline_state, cpu) = default_offline_state;
  71. }
  72. static void rtas_stop_self(void)
  73. {
  74. static struct rtas_args args;
  75. local_irq_disable();
  76. BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
  77. printk("cpu %u (hwid %u) Ready to die...\n",
  78. smp_processor_id(), hard_smp_processor_id());
  79. rtas_call_unlocked(&args, rtas_stop_self_token, 0, 1, NULL);
  80. panic("Alas, I survived.\n");
  81. }
  82. static void pseries_mach_cpu_die(void)
  83. {
  84. unsigned int cpu = smp_processor_id();
  85. unsigned int hwcpu = hard_smp_processor_id();
  86. u8 cede_latency_hint = 0;
  87. local_irq_disable();
  88. idle_task_exit();
  89. xics_teardown_cpu();
  90. if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  91. set_cpu_current_state(cpu, CPU_STATE_INACTIVE);
  92. if (ppc_md.suspend_disable_cpu)
  93. ppc_md.suspend_disable_cpu();
  94. cede_latency_hint = 2;
  95. get_lppaca()->idle = 1;
  96. if (!lppaca_shared_proc(get_lppaca()))
  97. get_lppaca()->donate_dedicated_cpu = 1;
  98. while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  99. while (!prep_irq_for_idle()) {
  100. local_irq_enable();
  101. local_irq_disable();
  102. }
  103. extended_cede_processor(cede_latency_hint);
  104. }
  105. local_irq_disable();
  106. if (!lppaca_shared_proc(get_lppaca()))
  107. get_lppaca()->donate_dedicated_cpu = 0;
  108. get_lppaca()->idle = 0;
  109. if (get_preferred_offline_state(cpu) == CPU_STATE_ONLINE) {
  110. unregister_slb_shadow(hwcpu);
  111. hard_irq_disable();
  112. /*
  113. * Call to start_secondary_resume() will not return.
  114. * Kernel stack will be reset and start_secondary()
  115. * will be called to continue the online operation.
  116. */
  117. start_secondary_resume();
  118. }
  119. }
  120. /* Requested state is CPU_STATE_OFFLINE at this point */
  121. WARN_ON(get_preferred_offline_state(cpu) != CPU_STATE_OFFLINE);
  122. set_cpu_current_state(cpu, CPU_STATE_OFFLINE);
  123. unregister_slb_shadow(hwcpu);
  124. rtas_stop_self();
  125. /* Should never get here... */
  126. BUG();
  127. for(;;);
  128. }
  129. static int pseries_cpu_disable(void)
  130. {
  131. int cpu = smp_processor_id();
  132. set_cpu_online(cpu, false);
  133. vdso_data->processorCount--;
  134. /*fix boot_cpuid here*/
  135. if (cpu == boot_cpuid)
  136. boot_cpuid = cpumask_any(cpu_online_mask);
  137. /* FIXME: abstract this to not be platform specific later on */
  138. xics_migrate_irqs_away();
  139. return 0;
  140. }
  141. /*
  142. * pseries_cpu_die: Wait for the cpu to die.
  143. * @cpu: logical processor id of the CPU whose death we're awaiting.
  144. *
  145. * This function is called from the context of the thread which is performing
  146. * the cpu-offline. Here we wait for long enough to allow the cpu in question
  147. * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
  148. * notifications.
  149. *
  150. * OTOH, pseries_mach_cpu_die() is called by the @cpu when it wants to
  151. * self-destruct.
  152. */
  153. static void pseries_cpu_die(unsigned int cpu)
  154. {
  155. int tries;
  156. int cpu_status = 1;
  157. unsigned int pcpu = get_hard_smp_processor_id(cpu);
  158. if (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
  159. cpu_status = 1;
  160. for (tries = 0; tries < 5000; tries++) {
  161. if (get_cpu_current_state(cpu) == CPU_STATE_INACTIVE) {
  162. cpu_status = 0;
  163. break;
  164. }
  165. msleep(1);
  166. }
  167. } else if (get_preferred_offline_state(cpu) == CPU_STATE_OFFLINE) {
  168. for (tries = 0; tries < 25; tries++) {
  169. cpu_status = smp_query_cpu_stopped(pcpu);
  170. if (cpu_status == QCSS_STOPPED ||
  171. cpu_status == QCSS_HARDWARE_ERROR)
  172. break;
  173. cpu_relax();
  174. }
  175. }
  176. if (cpu_status != 0) {
  177. printk("Querying DEAD? cpu %i (%i) shows %i\n",
  178. cpu, pcpu, cpu_status);
  179. }
  180. /* Isolation and deallocation are definitely done by
  181. * drslot_chrp_cpu. If they were not they would be
  182. * done here. Change isolate state to Isolate and
  183. * change allocation-state to Unusable.
  184. */
  185. paca[cpu].cpu_start = 0;
  186. }
  187. /*
  188. * Update cpu_present_mask and paca(s) for a new cpu node. The wrinkle
  189. * here is that a cpu device node may represent up to two logical cpus
  190. * in the SMT case. We must honor the assumption in other code that
  191. * the logical ids for sibling SMT threads x and y are adjacent, such
  192. * that x^1 == y and y^1 == x.
  193. */
  194. static int pseries_add_processor(struct device_node *np)
  195. {
  196. unsigned int cpu;
  197. cpumask_var_t candidate_mask, tmp;
  198. int err = -ENOSPC, len, nthreads, i;
  199. const __be32 *intserv;
  200. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  201. if (!intserv)
  202. return 0;
  203. zalloc_cpumask_var(&candidate_mask, GFP_KERNEL);
  204. zalloc_cpumask_var(&tmp, GFP_KERNEL);
  205. nthreads = len / sizeof(u32);
  206. for (i = 0; i < nthreads; i++)
  207. cpumask_set_cpu(i, tmp);
  208. cpu_maps_update_begin();
  209. BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
  210. /* Get a bitmap of unoccupied slots. */
  211. cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
  212. if (cpumask_empty(candidate_mask)) {
  213. /* If we get here, it most likely means that NR_CPUS is
  214. * less than the partition's max processors setting.
  215. */
  216. printk(KERN_ERR "Cannot add cpu %s; this system configuration"
  217. " supports %d logical cpus.\n", np->full_name,
  218. num_possible_cpus());
  219. goto out_unlock;
  220. }
  221. while (!cpumask_empty(tmp))
  222. if (cpumask_subset(tmp, candidate_mask))
  223. /* Found a range where we can insert the new cpu(s) */
  224. break;
  225. else
  226. cpumask_shift_left(tmp, tmp, nthreads);
  227. if (cpumask_empty(tmp)) {
  228. printk(KERN_ERR "Unable to find space in cpu_present_mask for"
  229. " processor %s with %d thread(s)\n", np->name,
  230. nthreads);
  231. goto out_unlock;
  232. }
  233. for_each_cpu(cpu, tmp) {
  234. BUG_ON(cpu_present(cpu));
  235. set_cpu_present(cpu, true);
  236. set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
  237. }
  238. err = 0;
  239. out_unlock:
  240. cpu_maps_update_done();
  241. free_cpumask_var(candidate_mask);
  242. free_cpumask_var(tmp);
  243. return err;
  244. }
  245. /*
  246. * Update the present map for a cpu node which is going away, and set
  247. * the hard id in the paca(s) to -1 to be consistent with boot time
  248. * convention for non-present cpus.
  249. */
  250. static void pseries_remove_processor(struct device_node *np)
  251. {
  252. unsigned int cpu;
  253. int len, nthreads, i;
  254. const __be32 *intserv;
  255. u32 thread;
  256. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  257. if (!intserv)
  258. return;
  259. nthreads = len / sizeof(u32);
  260. cpu_maps_update_begin();
  261. for (i = 0; i < nthreads; i++) {
  262. thread = be32_to_cpu(intserv[i]);
  263. for_each_present_cpu(cpu) {
  264. if (get_hard_smp_processor_id(cpu) != thread)
  265. continue;
  266. BUG_ON(cpu_online(cpu));
  267. set_cpu_present(cpu, false);
  268. set_hard_smp_processor_id(cpu, -1);
  269. break;
  270. }
  271. if (cpu >= nr_cpu_ids)
  272. printk(KERN_WARNING "Could not find cpu to remove "
  273. "with physical id 0x%x\n", thread);
  274. }
  275. cpu_maps_update_done();
  276. }
  277. static int dlpar_online_cpu(struct device_node *dn)
  278. {
  279. int rc = 0;
  280. unsigned int cpu;
  281. int len, nthreads, i;
  282. const __be32 *intserv;
  283. u32 thread;
  284. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  285. if (!intserv)
  286. return -EINVAL;
  287. nthreads = len / sizeof(u32);
  288. cpu_maps_update_begin();
  289. for (i = 0; i < nthreads; i++) {
  290. thread = be32_to_cpu(intserv[i]);
  291. for_each_present_cpu(cpu) {
  292. if (get_hard_smp_processor_id(cpu) != thread)
  293. continue;
  294. BUG_ON(get_cpu_current_state(cpu)
  295. != CPU_STATE_OFFLINE);
  296. cpu_maps_update_done();
  297. rc = device_online(get_cpu_device(cpu));
  298. if (rc)
  299. goto out;
  300. cpu_maps_update_begin();
  301. break;
  302. }
  303. if (cpu == num_possible_cpus())
  304. printk(KERN_WARNING "Could not find cpu to online "
  305. "with physical id 0x%x\n", thread);
  306. }
  307. cpu_maps_update_done();
  308. out:
  309. return rc;
  310. }
  311. static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
  312. {
  313. struct device_node *child = NULL;
  314. u32 my_drc_index;
  315. bool found;
  316. int rc;
  317. /* Assume cpu doesn't exist */
  318. found = false;
  319. for_each_child_of_node(parent, child) {
  320. rc = of_property_read_u32(child, "ibm,my-drc-index",
  321. &my_drc_index);
  322. if (rc)
  323. continue;
  324. if (my_drc_index == drc_index) {
  325. of_node_put(child);
  326. found = true;
  327. break;
  328. }
  329. }
  330. return found;
  331. }
  332. static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
  333. {
  334. bool found = false;
  335. int rc, index;
  336. index = 0;
  337. while (!found) {
  338. u32 drc;
  339. rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
  340. index++, &drc);
  341. if (rc)
  342. break;
  343. if (drc == drc_index)
  344. found = true;
  345. }
  346. return found;
  347. }
  348. static ssize_t dlpar_cpu_add(u32 drc_index)
  349. {
  350. struct device_node *dn, *parent;
  351. int rc, saved_rc;
  352. pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
  353. parent = of_find_node_by_path("/cpus");
  354. if (!parent) {
  355. pr_warn("Failed to find CPU root node \"/cpus\"\n");
  356. return -ENODEV;
  357. }
  358. if (dlpar_cpu_exists(parent, drc_index)) {
  359. of_node_put(parent);
  360. pr_warn("CPU with drc index %x already exists\n", drc_index);
  361. return -EINVAL;
  362. }
  363. if (!valid_cpu_drc_index(parent, drc_index)) {
  364. of_node_put(parent);
  365. pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
  366. return -EINVAL;
  367. }
  368. rc = dlpar_acquire_drc(drc_index);
  369. if (rc) {
  370. pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
  371. rc, drc_index);
  372. of_node_put(parent);
  373. return -EINVAL;
  374. }
  375. dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
  376. of_node_put(parent);
  377. if (!dn) {
  378. pr_warn("Failed call to configure-connector, drc index: %x\n",
  379. drc_index);
  380. dlpar_release_drc(drc_index);
  381. return -EINVAL;
  382. }
  383. rc = dlpar_attach_node(dn);
  384. if (rc) {
  385. saved_rc = rc;
  386. pr_warn("Failed to attach node %s, rc: %d, drc index: %x\n",
  387. dn->name, rc, drc_index);
  388. rc = dlpar_release_drc(drc_index);
  389. if (!rc)
  390. dlpar_free_cc_nodes(dn);
  391. return saved_rc;
  392. }
  393. rc = dlpar_online_cpu(dn);
  394. if (rc) {
  395. saved_rc = rc;
  396. pr_warn("Failed to online cpu %s, rc: %d, drc index: %x\n",
  397. dn->name, rc, drc_index);
  398. rc = dlpar_detach_node(dn);
  399. if (!rc)
  400. dlpar_release_drc(drc_index);
  401. return saved_rc;
  402. }
  403. pr_debug("Successfully added CPU %s, drc index: %x\n", dn->name,
  404. drc_index);
  405. return rc;
  406. }
  407. static int dlpar_offline_cpu(struct device_node *dn)
  408. {
  409. int rc = 0;
  410. unsigned int cpu;
  411. int len, nthreads, i;
  412. const __be32 *intserv;
  413. u32 thread;
  414. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  415. if (!intserv)
  416. return -EINVAL;
  417. nthreads = len / sizeof(u32);
  418. cpu_maps_update_begin();
  419. for (i = 0; i < nthreads; i++) {
  420. thread = be32_to_cpu(intserv[i]);
  421. for_each_present_cpu(cpu) {
  422. if (get_hard_smp_processor_id(cpu) != thread)
  423. continue;
  424. if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
  425. break;
  426. if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
  427. set_preferred_offline_state(cpu,
  428. CPU_STATE_OFFLINE);
  429. cpu_maps_update_done();
  430. rc = device_offline(get_cpu_device(cpu));
  431. if (rc)
  432. goto out;
  433. cpu_maps_update_begin();
  434. break;
  435. }
  436. /*
  437. * The cpu is in CPU_STATE_INACTIVE.
  438. * Upgrade it's state to CPU_STATE_OFFLINE.
  439. */
  440. set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
  441. BUG_ON(plpar_hcall_norets(H_PROD, thread)
  442. != H_SUCCESS);
  443. __cpu_die(cpu);
  444. break;
  445. }
  446. if (cpu == num_possible_cpus())
  447. printk(KERN_WARNING "Could not find cpu to offline with physical id 0x%x\n", thread);
  448. }
  449. cpu_maps_update_done();
  450. out:
  451. return rc;
  452. }
  453. static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
  454. {
  455. int rc;
  456. pr_debug("Attemping to remove CPU %s, drc index: %x\n",
  457. dn->name, drc_index);
  458. rc = dlpar_offline_cpu(dn);
  459. if (rc) {
  460. pr_warn("Failed to offline CPU %s, rc: %d\n", dn->name, rc);
  461. return -EINVAL;
  462. }
  463. rc = dlpar_release_drc(drc_index);
  464. if (rc) {
  465. pr_warn("Failed to release drc (%x) for CPU %s, rc: %d\n",
  466. drc_index, dn->name, rc);
  467. dlpar_online_cpu(dn);
  468. return rc;
  469. }
  470. rc = dlpar_detach_node(dn);
  471. if (rc) {
  472. int saved_rc = rc;
  473. pr_warn("Failed to detach CPU %s, rc: %d", dn->name, rc);
  474. rc = dlpar_acquire_drc(drc_index);
  475. if (!rc)
  476. dlpar_online_cpu(dn);
  477. return saved_rc;
  478. }
  479. pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
  480. return 0;
  481. }
  482. static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
  483. {
  484. struct device_node *dn;
  485. u32 my_index;
  486. int rc;
  487. for_each_node_by_type(dn, "cpu") {
  488. rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
  489. if (rc)
  490. continue;
  491. if (my_index == drc_index)
  492. break;
  493. }
  494. return dn;
  495. }
  496. static int dlpar_cpu_remove_by_index(u32 drc_index)
  497. {
  498. struct device_node *dn;
  499. int rc;
  500. dn = cpu_drc_index_to_dn(drc_index);
  501. if (!dn) {
  502. pr_warn("Cannot find CPU (drc index %x) to remove\n",
  503. drc_index);
  504. return -ENODEV;
  505. }
  506. rc = dlpar_cpu_remove(dn, drc_index);
  507. of_node_put(dn);
  508. return rc;
  509. }
  510. static int find_dlpar_cpus_to_remove(u32 *cpu_drcs, int cpus_to_remove)
  511. {
  512. struct device_node *dn;
  513. int cpus_found = 0;
  514. int rc;
  515. /* We want to find cpus_to_remove + 1 CPUs to ensure we do not
  516. * remove the last CPU.
  517. */
  518. for_each_node_by_type(dn, "cpu") {
  519. cpus_found++;
  520. if (cpus_found > cpus_to_remove) {
  521. of_node_put(dn);
  522. break;
  523. }
  524. /* Note that cpus_found is always 1 ahead of the index
  525. * into the cpu_drcs array, so we use cpus_found - 1
  526. */
  527. rc = of_property_read_u32(dn, "ibm,my-drc-index",
  528. &cpu_drcs[cpus_found - 1]);
  529. if (rc) {
  530. pr_warn("Error occurred getting drc-index for %s\n",
  531. dn->name);
  532. of_node_put(dn);
  533. return -1;
  534. }
  535. }
  536. if (cpus_found < cpus_to_remove) {
  537. pr_warn("Failed to find enough CPUs (%d of %d) to remove\n",
  538. cpus_found, cpus_to_remove);
  539. } else if (cpus_found == cpus_to_remove) {
  540. pr_warn("Cannot remove all CPUs\n");
  541. }
  542. return cpus_found;
  543. }
  544. static int dlpar_cpu_remove_by_count(u32 cpus_to_remove)
  545. {
  546. u32 *cpu_drcs;
  547. int cpus_found;
  548. int cpus_removed = 0;
  549. int i, rc;
  550. pr_debug("Attempting to hot-remove %d CPUs\n", cpus_to_remove);
  551. cpu_drcs = kcalloc(cpus_to_remove, sizeof(*cpu_drcs), GFP_KERNEL);
  552. if (!cpu_drcs)
  553. return -EINVAL;
  554. cpus_found = find_dlpar_cpus_to_remove(cpu_drcs, cpus_to_remove);
  555. if (cpus_found <= cpus_to_remove) {
  556. kfree(cpu_drcs);
  557. return -EINVAL;
  558. }
  559. for (i = 0; i < cpus_to_remove; i++) {
  560. rc = dlpar_cpu_remove_by_index(cpu_drcs[i]);
  561. if (rc)
  562. break;
  563. cpus_removed++;
  564. }
  565. if (cpus_removed != cpus_to_remove) {
  566. pr_warn("CPU hot-remove failed, adding back removed CPUs\n");
  567. for (i = 0; i < cpus_removed; i++)
  568. dlpar_cpu_add(cpu_drcs[i]);
  569. rc = -EINVAL;
  570. } else {
  571. rc = 0;
  572. }
  573. kfree(cpu_drcs);
  574. return rc;
  575. }
  576. static int find_dlpar_cpus_to_add(u32 *cpu_drcs, u32 cpus_to_add)
  577. {
  578. struct device_node *parent;
  579. int cpus_found = 0;
  580. int index, rc;
  581. parent = of_find_node_by_path("/cpus");
  582. if (!parent) {
  583. pr_warn("Could not find CPU root node in device tree\n");
  584. kfree(cpu_drcs);
  585. return -1;
  586. }
  587. /* Search the ibm,drc-indexes array for possible CPU drcs to
  588. * add. Note that the format of the ibm,drc-indexes array is
  589. * the number of entries in the array followed by the array
  590. * of drc values so we start looking at index = 1.
  591. */
  592. index = 1;
  593. while (cpus_found < cpus_to_add) {
  594. u32 drc;
  595. rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
  596. index++, &drc);
  597. if (rc)
  598. break;
  599. if (dlpar_cpu_exists(parent, drc))
  600. continue;
  601. cpu_drcs[cpus_found++] = drc;
  602. }
  603. of_node_put(parent);
  604. return cpus_found;
  605. }
  606. static int dlpar_cpu_add_by_count(u32 cpus_to_add)
  607. {
  608. u32 *cpu_drcs;
  609. int cpus_added = 0;
  610. int cpus_found;
  611. int i, rc;
  612. pr_debug("Attempting to hot-add %d CPUs\n", cpus_to_add);
  613. cpu_drcs = kcalloc(cpus_to_add, sizeof(*cpu_drcs), GFP_KERNEL);
  614. if (!cpu_drcs)
  615. return -EINVAL;
  616. cpus_found = find_dlpar_cpus_to_add(cpu_drcs, cpus_to_add);
  617. if (cpus_found < cpus_to_add) {
  618. pr_warn("Failed to find enough CPUs (%d of %d) to add\n",
  619. cpus_found, cpus_to_add);
  620. kfree(cpu_drcs);
  621. return -EINVAL;
  622. }
  623. for (i = 0; i < cpus_to_add; i++) {
  624. rc = dlpar_cpu_add(cpu_drcs[i]);
  625. if (rc)
  626. break;
  627. cpus_added++;
  628. }
  629. if (cpus_added < cpus_to_add) {
  630. pr_warn("CPU hot-add failed, removing any added CPUs\n");
  631. for (i = 0; i < cpus_added; i++)
  632. dlpar_cpu_remove_by_index(cpu_drcs[i]);
  633. rc = -EINVAL;
  634. } else {
  635. rc = 0;
  636. }
  637. kfree(cpu_drcs);
  638. return rc;
  639. }
  640. int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
  641. {
  642. u32 count, drc_index;
  643. int rc;
  644. count = hp_elog->_drc_u.drc_count;
  645. drc_index = hp_elog->_drc_u.drc_index;
  646. lock_device_hotplug();
  647. switch (hp_elog->action) {
  648. case PSERIES_HP_ELOG_ACTION_REMOVE:
  649. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
  650. rc = dlpar_cpu_remove_by_count(count);
  651. else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
  652. rc = dlpar_cpu_remove_by_index(drc_index);
  653. else
  654. rc = -EINVAL;
  655. break;
  656. case PSERIES_HP_ELOG_ACTION_ADD:
  657. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
  658. rc = dlpar_cpu_add_by_count(count);
  659. else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
  660. rc = dlpar_cpu_add(drc_index);
  661. else
  662. rc = -EINVAL;
  663. break;
  664. default:
  665. pr_err("Invalid action (%d) specified\n", hp_elog->action);
  666. rc = -EINVAL;
  667. break;
  668. }
  669. unlock_device_hotplug();
  670. return rc;
  671. }
  672. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  673. static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
  674. {
  675. u32 drc_index;
  676. int rc;
  677. rc = kstrtou32(buf, 0, &drc_index);
  678. if (rc)
  679. return -EINVAL;
  680. rc = dlpar_cpu_add(drc_index);
  681. return rc ? rc : count;
  682. }
  683. static ssize_t dlpar_cpu_release(const char *buf, size_t count)
  684. {
  685. struct device_node *dn;
  686. u32 drc_index;
  687. int rc;
  688. dn = of_find_node_by_path(buf);
  689. if (!dn)
  690. return -EINVAL;
  691. rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
  692. if (rc) {
  693. of_node_put(dn);
  694. return -EINVAL;
  695. }
  696. rc = dlpar_cpu_remove(dn, drc_index);
  697. of_node_put(dn);
  698. return rc ? rc : count;
  699. }
  700. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  701. static int pseries_smp_notifier(struct notifier_block *nb,
  702. unsigned long action, void *data)
  703. {
  704. struct of_reconfig_data *rd = data;
  705. int err = 0;
  706. switch (action) {
  707. case OF_RECONFIG_ATTACH_NODE:
  708. err = pseries_add_processor(rd->dn);
  709. break;
  710. case OF_RECONFIG_DETACH_NODE:
  711. pseries_remove_processor(rd->dn);
  712. break;
  713. }
  714. return notifier_from_errno(err);
  715. }
  716. static struct notifier_block pseries_smp_nb = {
  717. .notifier_call = pseries_smp_notifier,
  718. };
  719. #define MAX_CEDE_LATENCY_LEVELS 4
  720. #define CEDE_LATENCY_PARAM_LENGTH 10
  721. #define CEDE_LATENCY_PARAM_MAX_LENGTH \
  722. (MAX_CEDE_LATENCY_LEVELS * CEDE_LATENCY_PARAM_LENGTH * sizeof(char))
  723. #define CEDE_LATENCY_TOKEN 45
  724. static char cede_parameters[CEDE_LATENCY_PARAM_MAX_LENGTH];
  725. static int parse_cede_parameters(void)
  726. {
  727. memset(cede_parameters, 0, CEDE_LATENCY_PARAM_MAX_LENGTH);
  728. return rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  729. NULL,
  730. CEDE_LATENCY_TOKEN,
  731. __pa(cede_parameters),
  732. CEDE_LATENCY_PARAM_MAX_LENGTH);
  733. }
  734. static int __init pseries_cpu_hotplug_init(void)
  735. {
  736. int cpu;
  737. int qcss_tok;
  738. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  739. ppc_md.cpu_probe = dlpar_cpu_probe;
  740. ppc_md.cpu_release = dlpar_cpu_release;
  741. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  742. rtas_stop_self_token = rtas_token("stop-self");
  743. qcss_tok = rtas_token("query-cpu-stopped-state");
  744. if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
  745. qcss_tok == RTAS_UNKNOWN_SERVICE) {
  746. printk(KERN_INFO "CPU Hotplug not supported by firmware "
  747. "- disabling.\n");
  748. return 0;
  749. }
  750. ppc_md.cpu_die = pseries_mach_cpu_die;
  751. smp_ops->cpu_disable = pseries_cpu_disable;
  752. smp_ops->cpu_die = pseries_cpu_die;
  753. /* Processors can be added/removed only on LPAR */
  754. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  755. of_reconfig_notifier_register(&pseries_smp_nb);
  756. cpu_maps_update_begin();
  757. if (cede_offline_enabled && parse_cede_parameters() == 0) {
  758. default_offline_state = CPU_STATE_INACTIVE;
  759. for_each_online_cpu(cpu)
  760. set_default_offline_state(cpu);
  761. }
  762. cpu_maps_update_done();
  763. }
  764. return 0;
  765. }
  766. machine_arch_initcall(pseries, pseries_cpu_hotplug_init);