hotplug-cpu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pseries CPU Hotplug infrastructure.
  4. *
  5. * Split out from arch/powerpc/platforms/pseries/setup.c
  6. * arch/powerpc/kernel/rtas.c, and arch/powerpc/platforms/pseries/smp.c
  7. *
  8. * Peter Bergner, IBM March 2001.
  9. * Copyright (C) 2001 IBM.
  10. * Dave Engebretsen, Peter Bergner, and
  11. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  12. * Plus various changes from other IBM teams...
  13. *
  14. * Copyright (C) 2006 Michael Ellerman, IBM Corporation
  15. */
  16. #define pr_fmt(fmt) "pseries-hotplug-cpu: " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/sched.h> /* for idle_task_exit */
  21. #include <linux/sched/hotplug.h>
  22. #include <linux/cpu.h>
  23. #include <linux/of.h>
  24. #include <linux/slab.h>
  25. #include <asm/prom.h>
  26. #include <asm/rtas.h>
  27. #include <asm/firmware.h>
  28. #include <asm/machdep.h>
  29. #include <asm/vdso_datapage.h>
  30. #include <asm/xics.h>
  31. #include <asm/xive.h>
  32. #include <asm/plpar_wrappers.h>
  33. #include <asm/topology.h>
  34. #include "pseries.h"
  35. /* This version can't take the spinlock, because it never returns */
  36. static int rtas_stop_self_token = RTAS_UNKNOWN_SERVICE;
  37. /*
  38. * Record the CPU ids used on each nodes.
  39. * Protected by cpu_add_remove_lock.
  40. */
  41. static cpumask_var_t node_recorded_ids_map[MAX_NUMNODES];
  42. static void rtas_stop_self(void)
  43. {
  44. static struct rtas_args args;
  45. local_irq_disable();
  46. BUG_ON(rtas_stop_self_token == RTAS_UNKNOWN_SERVICE);
  47. rtas_call_unlocked(&args, rtas_stop_self_token, 0, 1, NULL);
  48. panic("Alas, I survived.\n");
  49. }
  50. static void pseries_cpu_offline_self(void)
  51. {
  52. unsigned int hwcpu = hard_smp_processor_id();
  53. local_irq_disable();
  54. idle_task_exit();
  55. if (xive_enabled())
  56. xive_teardown_cpu();
  57. else
  58. xics_teardown_cpu();
  59. unregister_slb_shadow(hwcpu);
  60. rtas_stop_self();
  61. /* Should never get here... */
  62. BUG();
  63. for(;;);
  64. }
  65. static int pseries_cpu_disable(void)
  66. {
  67. int cpu = smp_processor_id();
  68. set_cpu_online(cpu, false);
  69. vdso_data->processorCount--;
  70. /*fix boot_cpuid here*/
  71. if (cpu == boot_cpuid)
  72. boot_cpuid = cpumask_any(cpu_online_mask);
  73. /* FIXME: abstract this to not be platform specific later on */
  74. if (xive_enabled())
  75. xive_smp_disable_cpu();
  76. else
  77. xics_migrate_irqs_away();
  78. cleanup_cpu_mmu_context();
  79. return 0;
  80. }
  81. /*
  82. * pseries_cpu_die: Wait for the cpu to die.
  83. * @cpu: logical processor id of the CPU whose death we're awaiting.
  84. *
  85. * This function is called from the context of the thread which is performing
  86. * the cpu-offline. Here we wait for long enough to allow the cpu in question
  87. * to self-destroy so that the cpu-offline thread can send the CPU_DEAD
  88. * notifications.
  89. *
  90. * OTOH, pseries_cpu_offline_self() is called by the @cpu when it wants to
  91. * self-destruct.
  92. */
  93. static void pseries_cpu_die(unsigned int cpu)
  94. {
  95. int cpu_status = 1;
  96. unsigned int pcpu = get_hard_smp_processor_id(cpu);
  97. unsigned long timeout = jiffies + msecs_to_jiffies(120000);
  98. while (true) {
  99. cpu_status = smp_query_cpu_stopped(pcpu);
  100. if (cpu_status == QCSS_STOPPED ||
  101. cpu_status == QCSS_HARDWARE_ERROR)
  102. break;
  103. if (time_after(jiffies, timeout)) {
  104. pr_warn("CPU %i (hwid %i) didn't die after 120 seconds\n",
  105. cpu, pcpu);
  106. timeout = jiffies + msecs_to_jiffies(120000);
  107. }
  108. cond_resched();
  109. }
  110. if (cpu_status == QCSS_HARDWARE_ERROR) {
  111. pr_warn("CPU %i (hwid %i) reported error while dying\n",
  112. cpu, pcpu);
  113. }
  114. paca_ptrs[cpu]->cpu_start = 0;
  115. }
  116. /**
  117. * find_cpu_id_range - found a linear ranger of @nthreads free CPU ids.
  118. * @nthreads : the number of threads (cpu ids)
  119. * @assigned_node : the node it belongs to or NUMA_NO_NODE if free ids from any
  120. * node can be peek.
  121. * @cpu_mask: the returned CPU mask.
  122. *
  123. * Returns 0 on success.
  124. */
  125. static int find_cpu_id_range(unsigned int nthreads, int assigned_node,
  126. cpumask_var_t *cpu_mask)
  127. {
  128. cpumask_var_t candidate_mask;
  129. unsigned int cpu, node;
  130. int rc = -ENOSPC;
  131. if (!zalloc_cpumask_var(&candidate_mask, GFP_KERNEL))
  132. return -ENOMEM;
  133. cpumask_clear(*cpu_mask);
  134. for (cpu = 0; cpu < nthreads; cpu++)
  135. cpumask_set_cpu(cpu, *cpu_mask);
  136. BUG_ON(!cpumask_subset(cpu_present_mask, cpu_possible_mask));
  137. /* Get a bitmap of unoccupied slots. */
  138. cpumask_xor(candidate_mask, cpu_possible_mask, cpu_present_mask);
  139. if (assigned_node != NUMA_NO_NODE) {
  140. /*
  141. * Remove free ids previously assigned on the other nodes. We
  142. * can walk only online nodes because once a node became online
  143. * it is not turned offlined back.
  144. */
  145. for_each_online_node(node) {
  146. if (node == assigned_node)
  147. continue;
  148. cpumask_andnot(candidate_mask, candidate_mask,
  149. node_recorded_ids_map[node]);
  150. }
  151. }
  152. if (cpumask_empty(candidate_mask))
  153. goto out;
  154. while (!cpumask_empty(*cpu_mask)) {
  155. if (cpumask_subset(*cpu_mask, candidate_mask))
  156. /* Found a range where we can insert the new cpu(s) */
  157. break;
  158. cpumask_shift_left(*cpu_mask, *cpu_mask, nthreads);
  159. }
  160. if (!cpumask_empty(*cpu_mask))
  161. rc = 0;
  162. out:
  163. free_cpumask_var(candidate_mask);
  164. return rc;
  165. }
  166. /*
  167. * Update cpu_present_mask and paca(s) for a new cpu node. The wrinkle
  168. * here is that a cpu device node may represent multiple logical cpus
  169. * in the SMT case. We must honor the assumption in other code that
  170. * the logical ids for sibling SMT threads x and y are adjacent, such
  171. * that x^1 == y and y^1 == x.
  172. */
  173. static int pseries_add_processor(struct device_node *np)
  174. {
  175. int len, nthreads, node, cpu, assigned_node;
  176. int rc = 0;
  177. cpumask_var_t cpu_mask;
  178. const __be32 *intserv;
  179. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  180. if (!intserv)
  181. return 0;
  182. nthreads = len / sizeof(u32);
  183. if (!alloc_cpumask_var(&cpu_mask, GFP_KERNEL))
  184. return -ENOMEM;
  185. /*
  186. * Fetch from the DT nodes read by dlpar_configure_connector() the NUMA
  187. * node id the added CPU belongs to.
  188. */
  189. node = of_node_to_nid(np);
  190. if (node < 0 || !node_possible(node))
  191. node = first_online_node;
  192. BUG_ON(node == NUMA_NO_NODE);
  193. assigned_node = node;
  194. cpu_maps_update_begin();
  195. rc = find_cpu_id_range(nthreads, node, &cpu_mask);
  196. if (rc && nr_node_ids > 1) {
  197. /*
  198. * Try again, considering the free CPU ids from the other node.
  199. */
  200. node = NUMA_NO_NODE;
  201. rc = find_cpu_id_range(nthreads, NUMA_NO_NODE, &cpu_mask);
  202. }
  203. if (rc) {
  204. pr_err("Cannot add cpu %pOF; this system configuration"
  205. " supports %d logical cpus.\n", np, num_possible_cpus());
  206. goto out;
  207. }
  208. for_each_cpu(cpu, cpu_mask) {
  209. BUG_ON(cpu_present(cpu));
  210. set_cpu_present(cpu, true);
  211. set_hard_smp_processor_id(cpu, be32_to_cpu(*intserv++));
  212. }
  213. /* Record the newly used CPU ids for the associate node. */
  214. cpumask_or(node_recorded_ids_map[assigned_node],
  215. node_recorded_ids_map[assigned_node], cpu_mask);
  216. /*
  217. * If node is set to NUMA_NO_NODE, CPU ids have be reused from
  218. * another node, remove them from its mask.
  219. */
  220. if (node == NUMA_NO_NODE) {
  221. cpu = cpumask_first(cpu_mask);
  222. pr_warn("Reusing free CPU ids %d-%d from another node\n",
  223. cpu, cpu + nthreads - 1);
  224. for_each_online_node(node) {
  225. if (node == assigned_node)
  226. continue;
  227. cpumask_andnot(node_recorded_ids_map[node],
  228. node_recorded_ids_map[node],
  229. cpu_mask);
  230. }
  231. }
  232. out:
  233. cpu_maps_update_done();
  234. free_cpumask_var(cpu_mask);
  235. return rc;
  236. }
  237. /*
  238. * Update the present map for a cpu node which is going away, and set
  239. * the hard id in the paca(s) to -1 to be consistent with boot time
  240. * convention for non-present cpus.
  241. */
  242. static void pseries_remove_processor(struct device_node *np)
  243. {
  244. unsigned int cpu;
  245. int len, nthreads, i;
  246. const __be32 *intserv;
  247. u32 thread;
  248. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s", &len);
  249. if (!intserv)
  250. return;
  251. nthreads = len / sizeof(u32);
  252. cpu_maps_update_begin();
  253. for (i = 0; i < nthreads; i++) {
  254. thread = be32_to_cpu(intserv[i]);
  255. for_each_present_cpu(cpu) {
  256. if (get_hard_smp_processor_id(cpu) != thread)
  257. continue;
  258. BUG_ON(cpu_online(cpu));
  259. set_cpu_present(cpu, false);
  260. set_hard_smp_processor_id(cpu, -1);
  261. update_numa_cpu_lookup_table(cpu, -1);
  262. break;
  263. }
  264. if (cpu >= nr_cpu_ids)
  265. printk(KERN_WARNING "Could not find cpu to remove "
  266. "with physical id 0x%x\n", thread);
  267. }
  268. cpu_maps_update_done();
  269. }
  270. static int dlpar_offline_cpu(struct device_node *dn)
  271. {
  272. int rc = 0;
  273. unsigned int cpu;
  274. int len, nthreads, i;
  275. const __be32 *intserv;
  276. u32 thread;
  277. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  278. if (!intserv)
  279. return -EINVAL;
  280. nthreads = len / sizeof(u32);
  281. cpu_maps_update_begin();
  282. for (i = 0; i < nthreads; i++) {
  283. thread = be32_to_cpu(intserv[i]);
  284. for_each_present_cpu(cpu) {
  285. if (get_hard_smp_processor_id(cpu) != thread)
  286. continue;
  287. if (!cpu_online(cpu))
  288. break;
  289. /*
  290. * device_offline() will return -EBUSY (via cpu_down()) if there
  291. * is only one CPU left. Check it here to fail earlier and with a
  292. * more informative error message, while also retaining the
  293. * cpu_add_remove_lock to be sure that no CPUs are being
  294. * online/offlined during this check.
  295. */
  296. if (num_online_cpus() == 1) {
  297. pr_warn("Unable to remove last online CPU %pOFn\n", dn);
  298. rc = -EBUSY;
  299. goto out_unlock;
  300. }
  301. cpu_maps_update_done();
  302. rc = device_offline(get_cpu_device(cpu));
  303. if (rc)
  304. goto out;
  305. cpu_maps_update_begin();
  306. break;
  307. }
  308. if (cpu == num_possible_cpus()) {
  309. pr_warn("Could not find cpu to offline with physical id 0x%x\n",
  310. thread);
  311. }
  312. }
  313. out_unlock:
  314. cpu_maps_update_done();
  315. out:
  316. return rc;
  317. }
  318. static int dlpar_online_cpu(struct device_node *dn)
  319. {
  320. int rc = 0;
  321. unsigned int cpu;
  322. int len, nthreads, i;
  323. const __be32 *intserv;
  324. u32 thread;
  325. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  326. if (!intserv)
  327. return -EINVAL;
  328. nthreads = len / sizeof(u32);
  329. cpu_maps_update_begin();
  330. for (i = 0; i < nthreads; i++) {
  331. thread = be32_to_cpu(intserv[i]);
  332. for_each_present_cpu(cpu) {
  333. if (get_hard_smp_processor_id(cpu) != thread)
  334. continue;
  335. cpu_maps_update_done();
  336. find_and_update_cpu_nid(cpu);
  337. rc = device_online(get_cpu_device(cpu));
  338. if (rc) {
  339. dlpar_offline_cpu(dn);
  340. goto out;
  341. }
  342. cpu_maps_update_begin();
  343. break;
  344. }
  345. if (cpu == num_possible_cpus())
  346. printk(KERN_WARNING "Could not find cpu to online "
  347. "with physical id 0x%x\n", thread);
  348. }
  349. cpu_maps_update_done();
  350. out:
  351. return rc;
  352. }
  353. static bool dlpar_cpu_exists(struct device_node *parent, u32 drc_index)
  354. {
  355. struct device_node *child = NULL;
  356. u32 my_drc_index;
  357. bool found;
  358. int rc;
  359. /* Assume cpu doesn't exist */
  360. found = false;
  361. for_each_child_of_node(parent, child) {
  362. rc = of_property_read_u32(child, "ibm,my-drc-index",
  363. &my_drc_index);
  364. if (rc)
  365. continue;
  366. if (my_drc_index == drc_index) {
  367. of_node_put(child);
  368. found = true;
  369. break;
  370. }
  371. }
  372. return found;
  373. }
  374. static bool drc_info_valid_index(struct device_node *parent, u32 drc_index)
  375. {
  376. struct property *info;
  377. struct of_drc_info drc;
  378. const __be32 *value;
  379. u32 index;
  380. int count, i, j;
  381. info = of_find_property(parent, "ibm,drc-info", NULL);
  382. if (!info)
  383. return false;
  384. value = of_prop_next_u32(info, NULL, &count);
  385. /* First value of ibm,drc-info is number of drc-info records */
  386. if (value)
  387. value++;
  388. else
  389. return false;
  390. for (i = 0; i < count; i++) {
  391. if (of_read_drc_info_cell(&info, &value, &drc))
  392. return false;
  393. if (strncmp(drc.drc_type, "CPU", 3))
  394. break;
  395. if (drc_index > drc.last_drc_index)
  396. continue;
  397. index = drc.drc_index_start;
  398. for (j = 0; j < drc.num_sequential_elems; j++) {
  399. if (drc_index == index)
  400. return true;
  401. index += drc.sequential_inc;
  402. }
  403. }
  404. return false;
  405. }
  406. static bool valid_cpu_drc_index(struct device_node *parent, u32 drc_index)
  407. {
  408. bool found = false;
  409. int rc, index;
  410. if (of_find_property(parent, "ibm,drc-info", NULL))
  411. return drc_info_valid_index(parent, drc_index);
  412. /* Note that the format of the ibm,drc-indexes array is
  413. * the number of entries in the array followed by the array
  414. * of drc values so we start looking at index = 1.
  415. */
  416. index = 1;
  417. while (!found) {
  418. u32 drc;
  419. rc = of_property_read_u32_index(parent, "ibm,drc-indexes",
  420. index++, &drc);
  421. if (rc)
  422. break;
  423. if (drc == drc_index)
  424. found = true;
  425. }
  426. return found;
  427. }
  428. static int pseries_cpuhp_attach_nodes(struct device_node *dn)
  429. {
  430. struct of_changeset cs;
  431. int ret;
  432. /*
  433. * This device node is unattached but may have siblings; open-code the
  434. * traversal.
  435. */
  436. for (of_changeset_init(&cs); dn != NULL; dn = dn->sibling) {
  437. ret = of_changeset_attach_node(&cs, dn);
  438. if (ret)
  439. goto out;
  440. }
  441. ret = of_changeset_apply(&cs);
  442. out:
  443. of_changeset_destroy(&cs);
  444. return ret;
  445. }
  446. static ssize_t dlpar_cpu_add(u32 drc_index)
  447. {
  448. struct device_node *dn, *parent;
  449. int rc, saved_rc;
  450. pr_debug("Attempting to add CPU, drc index: %x\n", drc_index);
  451. parent = of_find_node_by_path("/cpus");
  452. if (!parent) {
  453. pr_warn("Failed to find CPU root node \"/cpus\"\n");
  454. return -ENODEV;
  455. }
  456. if (dlpar_cpu_exists(parent, drc_index)) {
  457. of_node_put(parent);
  458. pr_warn("CPU with drc index %x already exists\n", drc_index);
  459. return -EINVAL;
  460. }
  461. if (!valid_cpu_drc_index(parent, drc_index)) {
  462. of_node_put(parent);
  463. pr_warn("Cannot find CPU (drc index %x) to add.\n", drc_index);
  464. return -EINVAL;
  465. }
  466. rc = dlpar_acquire_drc(drc_index);
  467. if (rc) {
  468. pr_warn("Failed to acquire DRC, rc: %d, drc index: %x\n",
  469. rc, drc_index);
  470. of_node_put(parent);
  471. return -EINVAL;
  472. }
  473. dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
  474. if (!dn) {
  475. pr_warn("Failed call to configure-connector, drc index: %x\n",
  476. drc_index);
  477. dlpar_release_drc(drc_index);
  478. of_node_put(parent);
  479. return -EINVAL;
  480. }
  481. rc = pseries_cpuhp_attach_nodes(dn);
  482. /* Regardless we are done with parent now */
  483. of_node_put(parent);
  484. if (rc) {
  485. saved_rc = rc;
  486. pr_warn("Failed to attach node %pOFn, rc: %d, drc index: %x\n",
  487. dn, rc, drc_index);
  488. rc = dlpar_release_drc(drc_index);
  489. if (!rc)
  490. dlpar_free_cc_nodes(dn);
  491. return saved_rc;
  492. }
  493. update_numa_distance(dn);
  494. rc = dlpar_online_cpu(dn);
  495. if (rc) {
  496. saved_rc = rc;
  497. pr_warn("Failed to online cpu %pOFn, rc: %d, drc index: %x\n",
  498. dn, rc, drc_index);
  499. rc = dlpar_detach_node(dn);
  500. if (!rc)
  501. dlpar_release_drc(drc_index);
  502. return saved_rc;
  503. }
  504. pr_debug("Successfully added CPU %pOFn, drc index: %x\n", dn,
  505. drc_index);
  506. return rc;
  507. }
  508. static unsigned int pseries_cpuhp_cache_use_count(const struct device_node *cachedn)
  509. {
  510. unsigned int use_count = 0;
  511. struct device_node *dn, *tn;
  512. WARN_ON(!of_node_is_type(cachedn, "cache"));
  513. for_each_of_cpu_node(dn) {
  514. tn = of_find_next_cache_node(dn);
  515. of_node_put(tn);
  516. if (tn == cachedn)
  517. use_count++;
  518. }
  519. for_each_node_by_type(dn, "cache") {
  520. tn = of_find_next_cache_node(dn);
  521. of_node_put(tn);
  522. if (tn == cachedn)
  523. use_count++;
  524. }
  525. return use_count;
  526. }
  527. static int pseries_cpuhp_detach_nodes(struct device_node *cpudn)
  528. {
  529. struct device_node *dn;
  530. struct of_changeset cs;
  531. int ret = 0;
  532. of_changeset_init(&cs);
  533. ret = of_changeset_detach_node(&cs, cpudn);
  534. if (ret)
  535. goto out;
  536. dn = cpudn;
  537. while ((dn = of_find_next_cache_node(dn))) {
  538. if (pseries_cpuhp_cache_use_count(dn) > 1) {
  539. of_node_put(dn);
  540. break;
  541. }
  542. ret = of_changeset_detach_node(&cs, dn);
  543. of_node_put(dn);
  544. if (ret)
  545. goto out;
  546. }
  547. ret = of_changeset_apply(&cs);
  548. out:
  549. of_changeset_destroy(&cs);
  550. return ret;
  551. }
  552. static ssize_t dlpar_cpu_remove(struct device_node *dn, u32 drc_index)
  553. {
  554. int rc;
  555. pr_debug("Attempting to remove CPU %pOFn, drc index: %x\n",
  556. dn, drc_index);
  557. rc = dlpar_offline_cpu(dn);
  558. if (rc) {
  559. pr_warn("Failed to offline CPU %pOFn, rc: %d\n", dn, rc);
  560. return -EINVAL;
  561. }
  562. rc = dlpar_release_drc(drc_index);
  563. if (rc) {
  564. pr_warn("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",
  565. drc_index, dn, rc);
  566. dlpar_online_cpu(dn);
  567. return rc;
  568. }
  569. rc = pseries_cpuhp_detach_nodes(dn);
  570. if (rc) {
  571. int saved_rc = rc;
  572. pr_warn("Failed to detach CPU %pOFn, rc: %d", dn, rc);
  573. rc = dlpar_acquire_drc(drc_index);
  574. if (!rc)
  575. dlpar_online_cpu(dn);
  576. return saved_rc;
  577. }
  578. pr_debug("Successfully removed CPU, drc index: %x\n", drc_index);
  579. return 0;
  580. }
  581. static struct device_node *cpu_drc_index_to_dn(u32 drc_index)
  582. {
  583. struct device_node *dn;
  584. u32 my_index;
  585. int rc;
  586. for_each_node_by_type(dn, "cpu") {
  587. rc = of_property_read_u32(dn, "ibm,my-drc-index", &my_index);
  588. if (rc)
  589. continue;
  590. if (my_index == drc_index)
  591. break;
  592. }
  593. return dn;
  594. }
  595. static int dlpar_cpu_remove_by_index(u32 drc_index)
  596. {
  597. struct device_node *dn;
  598. int rc;
  599. dn = cpu_drc_index_to_dn(drc_index);
  600. if (!dn) {
  601. pr_warn("Cannot find CPU (drc index %x) to remove\n",
  602. drc_index);
  603. return -ENODEV;
  604. }
  605. rc = dlpar_cpu_remove(dn, drc_index);
  606. of_node_put(dn);
  607. return rc;
  608. }
  609. int dlpar_cpu(struct pseries_hp_errorlog *hp_elog)
  610. {
  611. u32 drc_index;
  612. int rc;
  613. drc_index = hp_elog->_drc_u.drc_index;
  614. lock_device_hotplug();
  615. switch (hp_elog->action) {
  616. case PSERIES_HP_ELOG_ACTION_REMOVE:
  617. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
  618. rc = dlpar_cpu_remove_by_index(drc_index);
  619. /*
  620. * Setting the isolation state of an UNISOLATED/CONFIGURED
  621. * device to UNISOLATE is a no-op, but the hypervisor can
  622. * use it as a hint that the CPU removal failed.
  623. */
  624. if (rc)
  625. dlpar_unisolate_drc(drc_index);
  626. }
  627. else
  628. rc = -EINVAL;
  629. break;
  630. case PSERIES_HP_ELOG_ACTION_ADD:
  631. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
  632. rc = dlpar_cpu_add(drc_index);
  633. else
  634. rc = -EINVAL;
  635. break;
  636. default:
  637. pr_err("Invalid action (%d) specified\n", hp_elog->action);
  638. rc = -EINVAL;
  639. break;
  640. }
  641. unlock_device_hotplug();
  642. return rc;
  643. }
  644. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  645. static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
  646. {
  647. u32 drc_index;
  648. int rc;
  649. rc = kstrtou32(buf, 0, &drc_index);
  650. if (rc)
  651. return -EINVAL;
  652. rc = dlpar_cpu_add(drc_index);
  653. return rc ? rc : count;
  654. }
  655. static ssize_t dlpar_cpu_release(const char *buf, size_t count)
  656. {
  657. struct device_node *dn;
  658. u32 drc_index;
  659. int rc;
  660. dn = of_find_node_by_path(buf);
  661. if (!dn)
  662. return -EINVAL;
  663. rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
  664. if (rc) {
  665. of_node_put(dn);
  666. return -EINVAL;
  667. }
  668. rc = dlpar_cpu_remove(dn, drc_index);
  669. of_node_put(dn);
  670. return rc ? rc : count;
  671. }
  672. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  673. static int pseries_smp_notifier(struct notifier_block *nb,
  674. unsigned long action, void *data)
  675. {
  676. struct of_reconfig_data *rd = data;
  677. int err = 0;
  678. switch (action) {
  679. case OF_RECONFIG_ATTACH_NODE:
  680. err = pseries_add_processor(rd->dn);
  681. break;
  682. case OF_RECONFIG_DETACH_NODE:
  683. pseries_remove_processor(rd->dn);
  684. break;
  685. }
  686. return notifier_from_errno(err);
  687. }
  688. static struct notifier_block pseries_smp_nb = {
  689. .notifier_call = pseries_smp_notifier,
  690. };
  691. static int __init pseries_cpu_hotplug_init(void)
  692. {
  693. int qcss_tok;
  694. unsigned int node;
  695. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  696. ppc_md.cpu_probe = dlpar_cpu_probe;
  697. ppc_md.cpu_release = dlpar_cpu_release;
  698. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  699. rtas_stop_self_token = rtas_token("stop-self");
  700. qcss_tok = rtas_token("query-cpu-stopped-state");
  701. if (rtas_stop_self_token == RTAS_UNKNOWN_SERVICE ||
  702. qcss_tok == RTAS_UNKNOWN_SERVICE) {
  703. printk(KERN_INFO "CPU Hotplug not supported by firmware "
  704. "- disabling.\n");
  705. return 0;
  706. }
  707. smp_ops->cpu_offline_self = pseries_cpu_offline_self;
  708. smp_ops->cpu_disable = pseries_cpu_disable;
  709. smp_ops->cpu_die = pseries_cpu_die;
  710. /* Processors can be added/removed only on LPAR */
  711. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  712. for_each_node(node) {
  713. if (!alloc_cpumask_var_node(&node_recorded_ids_map[node],
  714. GFP_KERNEL, node))
  715. return -ENOMEM;
  716. /* Record ids of CPU added at boot time */
  717. cpumask_copy(node_recorded_ids_map[node],
  718. cpumask_of_node(node));
  719. }
  720. of_reconfig_notifier_register(&pseries_smp_nb);
  721. }
  722. return 0;
  723. }
  724. machine_arch_initcall(pseries, pseries_cpu_hotplug_init);