pmem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Handles hot and cold plug of persistent memory regions on pseries.
  4. */
  5. #define pr_fmt(fmt) "pseries-pmem: " fmt
  6. #include <linux/kernel.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/delay.h>
  9. #include <linux/sched.h> /* for idle_task_exit */
  10. #include <linux/sched/hotplug.h>
  11. #include <linux/cpu.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/slab.h>
  15. #include <asm/prom.h>
  16. #include <asm/rtas.h>
  17. #include <asm/firmware.h>
  18. #include <asm/machdep.h>
  19. #include <asm/vdso_datapage.h>
  20. #include <asm/plpar_wrappers.h>
  21. #include <asm/topology.h>
  22. #include "pseries.h"
  23. #include "offline_states.h"
  24. static struct device_node *pmem_node;
  25. static ssize_t pmem_drc_add_node(u32 drc_index)
  26. {
  27. struct device_node *dn;
  28. int rc;
  29. pr_debug("Attempting to add pmem node, drc index: %x\n", drc_index);
  30. rc = dlpar_acquire_drc(drc_index);
  31. if (rc) {
  32. pr_err("Failed to acquire DRC, rc: %d, drc index: %x\n",
  33. rc, drc_index);
  34. return -EINVAL;
  35. }
  36. dn = dlpar_configure_connector(cpu_to_be32(drc_index), pmem_node);
  37. if (!dn) {
  38. pr_err("configure-connector failed for drc %x\n", drc_index);
  39. dlpar_release_drc(drc_index);
  40. return -EINVAL;
  41. }
  42. /* NB: The of reconfig notifier creates platform device from the node */
  43. rc = dlpar_attach_node(dn, pmem_node);
  44. if (rc) {
  45. pr_err("Failed to attach node %s, rc: %d, drc index: %x\n",
  46. dn->name, rc, drc_index);
  47. if (dlpar_release_drc(drc_index))
  48. dlpar_free_cc_nodes(dn);
  49. return rc;
  50. }
  51. pr_info("Successfully added %pOF, drc index: %x\n", dn, drc_index);
  52. return 0;
  53. }
  54. static ssize_t pmem_drc_remove_node(u32 drc_index)
  55. {
  56. struct device_node *dn;
  57. uint32_t index;
  58. int rc;
  59. for_each_child_of_node(pmem_node, dn) {
  60. if (of_property_read_u32(dn, "ibm,my-drc-index", &index))
  61. continue;
  62. if (index == drc_index)
  63. break;
  64. }
  65. if (!dn) {
  66. pr_err("Attempting to remove unused DRC index %x\n", drc_index);
  67. return -ENODEV;
  68. }
  69. pr_debug("Attempting to remove %pOF, drc index: %x\n", dn, drc_index);
  70. /* * NB: tears down the ibm,pmemory device as a side-effect */
  71. rc = dlpar_detach_node(dn);
  72. if (rc)
  73. return rc;
  74. rc = dlpar_release_drc(drc_index);
  75. if (rc) {
  76. pr_err("Failed to release drc (%x) for CPU %s, rc: %d\n",
  77. drc_index, dn->name, rc);
  78. dlpar_attach_node(dn, pmem_node);
  79. return rc;
  80. }
  81. pr_info("Successfully removed PMEM with drc index: %x\n", drc_index);
  82. return 0;
  83. }
  84. int dlpar_hp_pmem(struct pseries_hp_errorlog *hp_elog)
  85. {
  86. u32 count, drc_index;
  87. int rc;
  88. /* slim chance, but we might get a hotplug event while booting */
  89. if (!pmem_node)
  90. pmem_node = of_find_node_by_type(NULL, "ibm,persistent-memory");
  91. if (!pmem_node) {
  92. pr_err("Hotplug event for a pmem device, but none exists\n");
  93. return -ENODEV;
  94. }
  95. if (hp_elog->id_type != PSERIES_HP_ELOG_ID_DRC_INDEX) {
  96. pr_err("Unsupported hotplug event type %d\n",
  97. hp_elog->id_type);
  98. return -EINVAL;
  99. }
  100. count = hp_elog->_drc_u.drc_count;
  101. drc_index = hp_elog->_drc_u.drc_index;
  102. lock_device_hotplug();
  103. if (hp_elog->action == PSERIES_HP_ELOG_ACTION_ADD) {
  104. rc = pmem_drc_add_node(drc_index);
  105. } else if (hp_elog->action == PSERIES_HP_ELOG_ACTION_REMOVE) {
  106. rc = pmem_drc_remove_node(drc_index);
  107. } else {
  108. pr_err("Unsupported hotplug action (%d)\n", hp_elog->action);
  109. rc = -EINVAL;
  110. }
  111. unlock_device_hotplug();
  112. return rc;
  113. }
  114. const struct of_device_id drc_pmem_match[] = {
  115. { .type = "ibm,persistent-memory", },
  116. {}
  117. };
  118. static int pseries_pmem_init(void)
  119. {
  120. pmem_node = of_find_node_by_type(NULL, "ibm,persistent-memory");
  121. if (!pmem_node)
  122. return 0;
  123. /*
  124. * The generic OF bus probe/populate handles creating platform devices
  125. * from the child (ibm,pmemory) nodes. The generic code registers an of
  126. * reconfig notifier to handle the hot-add/remove cases too.
  127. */
  128. of_platform_bus_probe(pmem_node, drc_pmem_match, NULL);
  129. return 0;
  130. }
  131. machine_arch_initcall(pseries, pseries_pmem_init);