hotplug-cpu.c 22 KB

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