mobility.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support for Partition Mobility/Migration
  4. *
  5. * Copyright (C) 2010 Nathan Fontenot
  6. * Copyright (C) 2010 IBM Corporation
  7. */
  8. #define pr_fmt(fmt) "mobility: " fmt
  9. #include <linux/cpu.h>
  10. #include <linux/kernel.h>
  11. #include <linux/kobject.h>
  12. #include <linux/nmi.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/stat.h>
  16. #include <linux/stop_machine.h>
  17. #include <linux/completion.h>
  18. #include <linux/device.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/stringify.h>
  22. #include <asm/machdep.h>
  23. #include <asm/rtas.h>
  24. #include "pseries.h"
  25. #include "vas.h" /* vas_migration_handler() */
  26. #include "../../kernel/cacheinfo.h"
  27. static struct kobject *mobility_kobj;
  28. struct update_props_workarea {
  29. __be32 phandle;
  30. __be32 state;
  31. __be64 reserved;
  32. __be32 nprops;
  33. } __packed;
  34. #define NODE_ACTION_MASK 0xff000000
  35. #define NODE_COUNT_MASK 0x00ffffff
  36. #define DELETE_DT_NODE 0x01000000
  37. #define UPDATE_DT_NODE 0x02000000
  38. #define ADD_DT_NODE 0x03000000
  39. #define MIGRATION_SCOPE (1)
  40. #define PRRN_SCOPE -2
  41. #ifdef CONFIG_PPC_WATCHDOG
  42. static unsigned int nmi_wd_lpm_factor = 200;
  43. #ifdef CONFIG_SYSCTL
  44. static struct ctl_table nmi_wd_lpm_factor_ctl_table[] = {
  45. {
  46. .procname = "nmi_wd_lpm_factor",
  47. .data = &nmi_wd_lpm_factor,
  48. .maxlen = sizeof(int),
  49. .mode = 0644,
  50. .proc_handler = proc_douintvec_minmax,
  51. },
  52. {}
  53. };
  54. static struct ctl_table nmi_wd_lpm_factor_sysctl_root[] = {
  55. {
  56. .procname = "kernel",
  57. .mode = 0555,
  58. .child = nmi_wd_lpm_factor_ctl_table,
  59. },
  60. {}
  61. };
  62. static int __init register_nmi_wd_lpm_factor_sysctl(void)
  63. {
  64. register_sysctl_table(nmi_wd_lpm_factor_sysctl_root);
  65. return 0;
  66. }
  67. device_initcall(register_nmi_wd_lpm_factor_sysctl);
  68. #endif /* CONFIG_SYSCTL */
  69. #endif /* CONFIG_PPC_WATCHDOG */
  70. static int mobility_rtas_call(int token, char *buf, s32 scope)
  71. {
  72. int rc;
  73. spin_lock(&rtas_data_buf_lock);
  74. memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
  75. rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
  76. memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  77. spin_unlock(&rtas_data_buf_lock);
  78. return rc;
  79. }
  80. static int delete_dt_node(struct device_node *dn)
  81. {
  82. struct device_node *pdn;
  83. bool is_platfac;
  84. pdn = of_get_parent(dn);
  85. is_platfac = of_node_is_type(dn, "ibm,platform-facilities") ||
  86. of_node_is_type(pdn, "ibm,platform-facilities");
  87. of_node_put(pdn);
  88. /*
  89. * The drivers that bind to nodes in the platform-facilities
  90. * hierarchy don't support node removal, and the removal directive
  91. * from firmware is always followed by an add of an equivalent
  92. * node. The capability (e.g. RNG, encryption, compression)
  93. * represented by the node is never interrupted by the migration.
  94. * So ignore changes to this part of the tree.
  95. */
  96. if (is_platfac) {
  97. pr_notice("ignoring remove operation for %pOFfp\n", dn);
  98. return 0;
  99. }
  100. pr_debug("removing node %pOFfp\n", dn);
  101. dlpar_detach_node(dn);
  102. return 0;
  103. }
  104. static int update_dt_property(struct device_node *dn, struct property **prop,
  105. const char *name, u32 vd, char *value)
  106. {
  107. struct property *new_prop = *prop;
  108. int more = 0;
  109. /* A negative 'vd' value indicates that only part of the new property
  110. * value is contained in the buffer and we need to call
  111. * ibm,update-properties again to get the rest of the value.
  112. *
  113. * A negative value is also the two's compliment of the actual value.
  114. */
  115. if (vd & 0x80000000) {
  116. vd = ~vd + 1;
  117. more = 1;
  118. }
  119. if (new_prop) {
  120. /* partial property fixup */
  121. char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
  122. if (!new_data)
  123. return -ENOMEM;
  124. memcpy(new_data, new_prop->value, new_prop->length);
  125. memcpy(new_data + new_prop->length, value, vd);
  126. kfree(new_prop->value);
  127. new_prop->value = new_data;
  128. new_prop->length += vd;
  129. } else {
  130. new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
  131. if (!new_prop)
  132. return -ENOMEM;
  133. new_prop->name = kstrdup(name, GFP_KERNEL);
  134. if (!new_prop->name) {
  135. kfree(new_prop);
  136. return -ENOMEM;
  137. }
  138. new_prop->length = vd;
  139. new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
  140. if (!new_prop->value) {
  141. kfree(new_prop->name);
  142. kfree(new_prop);
  143. return -ENOMEM;
  144. }
  145. memcpy(new_prop->value, value, vd);
  146. *prop = new_prop;
  147. }
  148. if (!more) {
  149. pr_debug("updating node %pOF property %s\n", dn, name);
  150. of_update_property(dn, new_prop);
  151. *prop = NULL;
  152. }
  153. return 0;
  154. }
  155. static int update_dt_node(struct device_node *dn, s32 scope)
  156. {
  157. struct update_props_workarea *upwa;
  158. struct property *prop = NULL;
  159. int i, rc, rtas_rc;
  160. char *prop_data;
  161. char *rtas_buf;
  162. int update_properties_token;
  163. u32 nprops;
  164. u32 vd;
  165. update_properties_token = rtas_token("ibm,update-properties");
  166. if (update_properties_token == RTAS_UNKNOWN_SERVICE)
  167. return -EINVAL;
  168. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  169. if (!rtas_buf)
  170. return -ENOMEM;
  171. upwa = (struct update_props_workarea *)&rtas_buf[0];
  172. upwa->phandle = cpu_to_be32(dn->phandle);
  173. do {
  174. rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
  175. scope);
  176. if (rtas_rc < 0)
  177. break;
  178. prop_data = rtas_buf + sizeof(*upwa);
  179. nprops = be32_to_cpu(upwa->nprops);
  180. /* On the first call to ibm,update-properties for a node the
  181. * first property value descriptor contains an empty
  182. * property name, the property value length encoded as u32,
  183. * and the property value is the node path being updated.
  184. */
  185. if (*prop_data == 0) {
  186. prop_data++;
  187. vd = be32_to_cpu(*(__be32 *)prop_data);
  188. prop_data += vd + sizeof(vd);
  189. nprops--;
  190. }
  191. for (i = 0; i < nprops; i++) {
  192. char *prop_name;
  193. prop_name = prop_data;
  194. prop_data += strlen(prop_name) + 1;
  195. vd = be32_to_cpu(*(__be32 *)prop_data);
  196. prop_data += sizeof(vd);
  197. switch (vd) {
  198. case 0x00000000:
  199. /* name only property, nothing to do */
  200. break;
  201. case 0x80000000:
  202. of_remove_property(dn, of_find_property(dn,
  203. prop_name, NULL));
  204. prop = NULL;
  205. break;
  206. default:
  207. rc = update_dt_property(dn, &prop, prop_name,
  208. vd, prop_data);
  209. if (rc) {
  210. pr_err("updating %s property failed: %d\n",
  211. prop_name, rc);
  212. }
  213. prop_data += vd;
  214. break;
  215. }
  216. cond_resched();
  217. }
  218. cond_resched();
  219. } while (rtas_rc == 1);
  220. kfree(rtas_buf);
  221. return 0;
  222. }
  223. static int add_dt_node(struct device_node *parent_dn, __be32 drc_index)
  224. {
  225. struct device_node *dn;
  226. int rc;
  227. dn = dlpar_configure_connector(drc_index, parent_dn);
  228. if (!dn)
  229. return -ENOENT;
  230. /*
  231. * Since delete_dt_node() ignores this node type, this is the
  232. * necessary counterpart. We also know that a platform-facilities
  233. * node returned from dlpar_configure_connector() has children
  234. * attached, and dlpar_attach_node() only adds the parent, leaking
  235. * the children. So ignore these on the add side for now.
  236. */
  237. if (of_node_is_type(dn, "ibm,platform-facilities")) {
  238. pr_notice("ignoring add operation for %pOF\n", dn);
  239. dlpar_free_cc_nodes(dn);
  240. return 0;
  241. }
  242. rc = dlpar_attach_node(dn, parent_dn);
  243. if (rc)
  244. dlpar_free_cc_nodes(dn);
  245. pr_debug("added node %pOFfp\n", dn);
  246. return rc;
  247. }
  248. static int pseries_devicetree_update(s32 scope)
  249. {
  250. char *rtas_buf;
  251. __be32 *data;
  252. int update_nodes_token;
  253. int rc;
  254. update_nodes_token = rtas_token("ibm,update-nodes");
  255. if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
  256. return 0;
  257. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  258. if (!rtas_buf)
  259. return -ENOMEM;
  260. do {
  261. rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
  262. if (rc && rc != 1)
  263. break;
  264. data = (__be32 *)rtas_buf + 4;
  265. while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
  266. int i;
  267. u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
  268. u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
  269. data++;
  270. for (i = 0; i < node_count; i++) {
  271. struct device_node *np;
  272. __be32 phandle = *data++;
  273. __be32 drc_index;
  274. np = of_find_node_by_phandle(be32_to_cpu(phandle));
  275. if (!np) {
  276. pr_warn("Failed lookup: phandle 0x%x for action 0x%x\n",
  277. be32_to_cpu(phandle), action);
  278. continue;
  279. }
  280. switch (action) {
  281. case DELETE_DT_NODE:
  282. delete_dt_node(np);
  283. break;
  284. case UPDATE_DT_NODE:
  285. update_dt_node(np, scope);
  286. break;
  287. case ADD_DT_NODE:
  288. drc_index = *data++;
  289. add_dt_node(np, drc_index);
  290. break;
  291. }
  292. of_node_put(np);
  293. cond_resched();
  294. }
  295. }
  296. cond_resched();
  297. } while (rc == 1);
  298. kfree(rtas_buf);
  299. return rc;
  300. }
  301. void post_mobility_fixup(void)
  302. {
  303. int rc;
  304. rtas_activate_firmware();
  305. /*
  306. * We don't want CPUs to go online/offline while the device
  307. * tree is being updated.
  308. */
  309. cpus_read_lock();
  310. /*
  311. * It's common for the destination firmware to replace cache
  312. * nodes. Release all of the cacheinfo hierarchy's references
  313. * before updating the device tree.
  314. */
  315. cacheinfo_teardown();
  316. rc = pseries_devicetree_update(MIGRATION_SCOPE);
  317. if (rc)
  318. pr_err("device tree update failed: %d\n", rc);
  319. cacheinfo_rebuild();
  320. cpus_read_unlock();
  321. /* Possibly switch to a new L1 flush type */
  322. pseries_setup_security_mitigations();
  323. /* Reinitialise system information for hv-24x7 */
  324. read_24x7_sys_info();
  325. return;
  326. }
  327. static int poll_vasi_state(u64 handle, unsigned long *res)
  328. {
  329. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  330. long hvrc;
  331. int ret;
  332. hvrc = plpar_hcall(H_VASI_STATE, retbuf, handle);
  333. switch (hvrc) {
  334. case H_SUCCESS:
  335. ret = 0;
  336. *res = retbuf[0];
  337. break;
  338. case H_PARAMETER:
  339. ret = -EINVAL;
  340. break;
  341. case H_FUNCTION:
  342. ret = -EOPNOTSUPP;
  343. break;
  344. case H_HARDWARE:
  345. default:
  346. pr_err("unexpected H_VASI_STATE result %ld\n", hvrc);
  347. ret = -EIO;
  348. break;
  349. }
  350. return ret;
  351. }
  352. static int wait_for_vasi_session_suspending(u64 handle)
  353. {
  354. unsigned long state;
  355. int ret;
  356. /*
  357. * Wait for transition from H_VASI_ENABLED to
  358. * H_VASI_SUSPENDING. Treat anything else as an error.
  359. */
  360. while (true) {
  361. ret = poll_vasi_state(handle, &state);
  362. if (ret != 0 || state == H_VASI_SUSPENDING) {
  363. break;
  364. } else if (state == H_VASI_ENABLED) {
  365. ssleep(1);
  366. } else {
  367. pr_err("unexpected H_VASI_STATE result %lu\n", state);
  368. ret = -EIO;
  369. break;
  370. }
  371. }
  372. /*
  373. * Proceed even if H_VASI_STATE is unavailable. If H_JOIN or
  374. * ibm,suspend-me are also unimplemented, we'll recover then.
  375. */
  376. if (ret == -EOPNOTSUPP)
  377. ret = 0;
  378. return ret;
  379. }
  380. static void wait_for_vasi_session_completed(u64 handle)
  381. {
  382. unsigned long state = 0;
  383. int ret;
  384. pr_info("waiting for memory transfer to complete...\n");
  385. /*
  386. * Wait for transition from H_VASI_RESUMED to H_VASI_COMPLETED.
  387. */
  388. while (true) {
  389. ret = poll_vasi_state(handle, &state);
  390. /*
  391. * If the memory transfer is already complete and the migration
  392. * has been cleaned up by the hypervisor, H_PARAMETER is return,
  393. * which is translate in EINVAL by poll_vasi_state().
  394. */
  395. if (ret == -EINVAL || (!ret && state == H_VASI_COMPLETED)) {
  396. pr_info("memory transfer completed.\n");
  397. break;
  398. }
  399. if (ret) {
  400. pr_err("H_VASI_STATE return error (%d)\n", ret);
  401. break;
  402. }
  403. if (state != H_VASI_RESUMED) {
  404. pr_err("unexpected H_VASI_STATE result %lu\n", state);
  405. break;
  406. }
  407. msleep(500);
  408. }
  409. }
  410. static void prod_single(unsigned int target_cpu)
  411. {
  412. long hvrc;
  413. int hwid;
  414. hwid = get_hard_smp_processor_id(target_cpu);
  415. hvrc = plpar_hcall_norets(H_PROD, hwid);
  416. if (hvrc == H_SUCCESS)
  417. return;
  418. pr_err_ratelimited("H_PROD of CPU %u (hwid %d) error: %ld\n",
  419. target_cpu, hwid, hvrc);
  420. }
  421. static void prod_others(void)
  422. {
  423. unsigned int cpu;
  424. for_each_online_cpu(cpu) {
  425. if (cpu != smp_processor_id())
  426. prod_single(cpu);
  427. }
  428. }
  429. static u16 clamp_slb_size(void)
  430. {
  431. #ifdef CONFIG_PPC_64S_HASH_MMU
  432. u16 prev = mmu_slb_size;
  433. slb_set_size(SLB_MIN_SIZE);
  434. return prev;
  435. #else
  436. return 0;
  437. #endif
  438. }
  439. static int do_suspend(void)
  440. {
  441. u16 saved_slb_size;
  442. int status;
  443. int ret;
  444. pr_info("calling ibm,suspend-me on CPU %i\n", smp_processor_id());
  445. /*
  446. * The destination processor model may have fewer SLB entries
  447. * than the source. We reduce mmu_slb_size to a safe minimum
  448. * before suspending in order to minimize the possibility of
  449. * programming non-existent entries on the destination. If
  450. * suspend fails, we restore it before returning. On success
  451. * the OF reconfig path will update it from the new device
  452. * tree after resuming on the destination.
  453. */
  454. saved_slb_size = clamp_slb_size();
  455. ret = rtas_ibm_suspend_me(&status);
  456. if (ret != 0) {
  457. pr_err("ibm,suspend-me error: %d\n", status);
  458. slb_set_size(saved_slb_size);
  459. }
  460. return ret;
  461. }
  462. /**
  463. * struct pseries_suspend_info - State shared between CPUs for join/suspend.
  464. * @counter: Threads are to increment this upon resuming from suspend
  465. * or if an error is received from H_JOIN. The thread which performs
  466. * the first increment (i.e. sets it to 1) is responsible for
  467. * waking the other threads.
  468. * @done: False if join/suspend is in progress. True if the operation is
  469. * complete (successful or not).
  470. */
  471. struct pseries_suspend_info {
  472. atomic_t counter;
  473. bool done;
  474. };
  475. static int do_join(void *arg)
  476. {
  477. struct pseries_suspend_info *info = arg;
  478. atomic_t *counter = &info->counter;
  479. long hvrc;
  480. int ret;
  481. retry:
  482. /* Must ensure MSR.EE off for H_JOIN. */
  483. hard_irq_disable();
  484. hvrc = plpar_hcall_norets(H_JOIN);
  485. switch (hvrc) {
  486. case H_CONTINUE:
  487. /*
  488. * All other CPUs are offline or in H_JOIN. This CPU
  489. * attempts the suspend.
  490. */
  491. ret = do_suspend();
  492. break;
  493. case H_SUCCESS:
  494. /*
  495. * The suspend is complete and this cpu has received a
  496. * prod, or we've received a stray prod from unrelated
  497. * code (e.g. paravirt spinlocks) and we need to join
  498. * again.
  499. *
  500. * This barrier orders the return from H_JOIN above vs
  501. * the load of info->done. It pairs with the barrier
  502. * in the wakeup/prod path below.
  503. */
  504. smp_mb();
  505. if (READ_ONCE(info->done) == false) {
  506. pr_info_ratelimited("premature return from H_JOIN on CPU %i, retrying",
  507. smp_processor_id());
  508. goto retry;
  509. }
  510. ret = 0;
  511. break;
  512. case H_BAD_MODE:
  513. case H_HARDWARE:
  514. default:
  515. ret = -EIO;
  516. pr_err_ratelimited("H_JOIN error %ld on CPU %i\n",
  517. hvrc, smp_processor_id());
  518. break;
  519. }
  520. if (atomic_inc_return(counter) == 1) {
  521. pr_info("CPU %u waking all threads\n", smp_processor_id());
  522. WRITE_ONCE(info->done, true);
  523. /*
  524. * This barrier orders the store to info->done vs subsequent
  525. * H_PRODs to wake the other CPUs. It pairs with the barrier
  526. * in the H_SUCCESS case above.
  527. */
  528. smp_mb();
  529. prod_others();
  530. }
  531. /*
  532. * Execution may have been suspended for several seconds, so
  533. * reset the watchdog.
  534. */
  535. touch_nmi_watchdog();
  536. return ret;
  537. }
  538. /*
  539. * Abort reason code byte 0. We use only the 'Migrating partition' value.
  540. */
  541. enum vasi_aborting_entity {
  542. ORCHESTRATOR = 1,
  543. VSP_SOURCE = 2,
  544. PARTITION_FIRMWARE = 3,
  545. PLATFORM_FIRMWARE = 4,
  546. VSP_TARGET = 5,
  547. MIGRATING_PARTITION = 6,
  548. };
  549. static void pseries_cancel_migration(u64 handle, int err)
  550. {
  551. u32 reason_code;
  552. u32 detail;
  553. u8 entity;
  554. long hvrc;
  555. entity = MIGRATING_PARTITION;
  556. detail = abs(err) & 0xffffff;
  557. reason_code = (entity << 24) | detail;
  558. hvrc = plpar_hcall_norets(H_VASI_SIGNAL, handle,
  559. H_VASI_SIGNAL_CANCEL, reason_code);
  560. if (hvrc)
  561. pr_err("H_VASI_SIGNAL error: %ld\n", hvrc);
  562. }
  563. static int pseries_suspend(u64 handle)
  564. {
  565. const unsigned int max_attempts = 5;
  566. unsigned int retry_interval_ms = 1;
  567. unsigned int attempt = 1;
  568. int ret;
  569. while (true) {
  570. struct pseries_suspend_info info;
  571. unsigned long vasi_state;
  572. int vasi_err;
  573. info = (struct pseries_suspend_info) {
  574. .counter = ATOMIC_INIT(0),
  575. .done = false,
  576. };
  577. ret = stop_machine(do_join, &info, cpu_online_mask);
  578. if (ret == 0)
  579. break;
  580. /*
  581. * Encountered an error. If the VASI stream is still
  582. * in Suspending state, it's likely a transient
  583. * condition related to some device in the partition
  584. * and we can retry in the hope that the cause has
  585. * cleared after some delay.
  586. *
  587. * A better design would allow drivers etc to prepare
  588. * for the suspend and avoid conditions which prevent
  589. * the suspend from succeeding. For now, we have this
  590. * mitigation.
  591. */
  592. pr_notice("Partition suspend attempt %u of %u error: %d\n",
  593. attempt, max_attempts, ret);
  594. if (attempt == max_attempts)
  595. break;
  596. vasi_err = poll_vasi_state(handle, &vasi_state);
  597. if (vasi_err == 0) {
  598. if (vasi_state != H_VASI_SUSPENDING) {
  599. pr_notice("VASI state %lu after failed suspend\n",
  600. vasi_state);
  601. break;
  602. }
  603. } else if (vasi_err != -EOPNOTSUPP) {
  604. pr_err("VASI state poll error: %d", vasi_err);
  605. break;
  606. }
  607. pr_notice("Will retry partition suspend after %u ms\n",
  608. retry_interval_ms);
  609. msleep(retry_interval_ms);
  610. retry_interval_ms *= 10;
  611. attempt++;
  612. }
  613. return ret;
  614. }
  615. static int pseries_migrate_partition(u64 handle)
  616. {
  617. int ret;
  618. unsigned int factor = 0;
  619. #ifdef CONFIG_PPC_WATCHDOG
  620. factor = nmi_wd_lpm_factor;
  621. #endif
  622. /*
  623. * When the migration is initiated, the hypervisor changes VAS
  624. * mappings to prepare before OS gets the notification and
  625. * closes all VAS windows. NX generates continuous faults during
  626. * this time and the user space can not differentiate these
  627. * faults from the migration event. So reduce this time window
  628. * by closing VAS windows at the beginning of this function.
  629. */
  630. vas_migration_handler(VAS_SUSPEND);
  631. ret = wait_for_vasi_session_suspending(handle);
  632. if (ret)
  633. goto out;
  634. if (factor)
  635. watchdog_nmi_set_timeout_pct(factor);
  636. ret = pseries_suspend(handle);
  637. if (ret == 0) {
  638. post_mobility_fixup();
  639. /*
  640. * Wait until the memory transfer is complete, so that the user
  641. * space process returns from the syscall after the transfer is
  642. * complete. This allows the user hooks to be executed at the
  643. * right time.
  644. */
  645. wait_for_vasi_session_completed(handle);
  646. } else
  647. pseries_cancel_migration(handle, ret);
  648. if (factor)
  649. watchdog_nmi_set_timeout_pct(0);
  650. out:
  651. vas_migration_handler(VAS_RESUME);
  652. return ret;
  653. }
  654. int rtas_syscall_dispatch_ibm_suspend_me(u64 handle)
  655. {
  656. return pseries_migrate_partition(handle);
  657. }
  658. static ssize_t migration_store(struct class *class,
  659. struct class_attribute *attr, const char *buf,
  660. size_t count)
  661. {
  662. u64 streamid;
  663. int rc;
  664. rc = kstrtou64(buf, 0, &streamid);
  665. if (rc)
  666. return rc;
  667. rc = pseries_migrate_partition(streamid);
  668. if (rc)
  669. return rc;
  670. return count;
  671. }
  672. /*
  673. * Used by drmgr to determine the kernel behavior of the migration interface.
  674. *
  675. * Version 1: Performs all PAPR requirements for migration including
  676. * firmware activation and device tree update.
  677. */
  678. #define MIGRATION_API_VERSION 1
  679. static CLASS_ATTR_WO(migration);
  680. static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
  681. static int __init mobility_sysfs_init(void)
  682. {
  683. int rc;
  684. mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
  685. if (!mobility_kobj)
  686. return -ENOMEM;
  687. rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
  688. if (rc)
  689. pr_err("unable to create migration sysfs file (%d)\n", rc);
  690. rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
  691. if (rc)
  692. pr_err("unable to create api_version sysfs file (%d)\n", rc);
  693. return 0;
  694. }
  695. machine_device_initcall(pseries, mobility_sysfs_init);