hotplug-cpu.c 22 KB

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