hotplug-cpu.c 22 KB

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