hotplug-memory.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * pseries Memory Hotplug infrastructure.
  3. *
  4. * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/of.h>
  12. #include <linux/lmb.h>
  13. #include <asm/firmware.h>
  14. #include <asm/machdep.h>
  15. #include <asm/pSeries_reconfig.h>
  16. static int pseries_remove_lmb(unsigned long base, unsigned int lmb_size)
  17. {
  18. unsigned long start, start_pfn;
  19. struct zone *zone;
  20. int ret;
  21. start_pfn = base >> PFN_SECTION_SHIFT;
  22. zone = page_zone(pfn_to_page(start_pfn));
  23. /*
  24. * Remove section mappings and sysfs entries for the
  25. * section of the memory we are removing.
  26. *
  27. * NOTE: Ideally, this should be done in generic code like
  28. * remove_memory(). But remove_memory() gets called by writing
  29. * to sysfs "state" file and we can't remove sysfs entries
  30. * while writing to it. So we have to defer it to here.
  31. */
  32. ret = __remove_pages(zone, start_pfn, lmb_size >> PAGE_SHIFT);
  33. if (ret)
  34. return ret;
  35. /*
  36. * Update memory regions for memory remove
  37. */
  38. lmb_remove(base, lmb_size);
  39. /*
  40. * Remove htab bolted mappings for this section of memory
  41. */
  42. start = (unsigned long)__va(base);
  43. ret = remove_section_mapping(start, start + lmb_size);
  44. return ret;
  45. }
  46. static int pseries_remove_memory(struct device_node *np)
  47. {
  48. const char *type;
  49. const unsigned int *regs;
  50. unsigned long base;
  51. unsigned int lmb_size;
  52. int ret = -EINVAL;
  53. /*
  54. * Check to see if we are actually removing memory
  55. */
  56. type = of_get_property(np, "device_type", NULL);
  57. if (type == NULL || strcmp(type, "memory") != 0)
  58. return 0;
  59. /*
  60. * Find the bae address and size of the lmb
  61. */
  62. regs = of_get_property(np, "reg", NULL);
  63. if (!regs)
  64. return ret;
  65. base = *(unsigned long *)regs;
  66. lmb_size = regs[3];
  67. ret = pseries_remove_lmb(base, lmb_size);
  68. return ret;
  69. }
  70. static int pseries_add_memory(struct device_node *np)
  71. {
  72. const char *type;
  73. const unsigned int *regs;
  74. unsigned long base;
  75. unsigned int lmb_size;
  76. int ret = -EINVAL;
  77. /*
  78. * Check to see if we are actually adding memory
  79. */
  80. type = of_get_property(np, "device_type", NULL);
  81. if (type == NULL || strcmp(type, "memory") != 0)
  82. return 0;
  83. /*
  84. * Find the base and size of the lmb
  85. */
  86. regs = of_get_property(np, "reg", NULL);
  87. if (!regs)
  88. return ret;
  89. base = *(unsigned long *)regs;
  90. lmb_size = regs[3];
  91. /*
  92. * Update memory region to represent the memory add
  93. */
  94. ret = lmb_add(base, lmb_size);
  95. return (ret < 0) ? -EINVAL : 0;
  96. }
  97. static int pseries_drconf_memory(unsigned long *base, unsigned int action)
  98. {
  99. struct device_node *np;
  100. const unsigned long *lmb_size;
  101. int rc;
  102. np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  103. if (!np)
  104. return -EINVAL;
  105. lmb_size = of_get_property(np, "ibm,lmb-size", NULL);
  106. if (!lmb_size) {
  107. of_node_put(np);
  108. return -EINVAL;
  109. }
  110. if (action == PSERIES_DRCONF_MEM_ADD) {
  111. rc = lmb_add(*base, *lmb_size);
  112. rc = (rc < 0) ? -EINVAL : 0;
  113. } else if (action == PSERIES_DRCONF_MEM_REMOVE) {
  114. rc = pseries_remove_lmb(*base, *lmb_size);
  115. } else {
  116. rc = -EINVAL;
  117. }
  118. of_node_put(np);
  119. return rc;
  120. }
  121. static int pseries_memory_notifier(struct notifier_block *nb,
  122. unsigned long action, void *node)
  123. {
  124. int err = NOTIFY_OK;
  125. switch (action) {
  126. case PSERIES_RECONFIG_ADD:
  127. if (pseries_add_memory(node))
  128. err = NOTIFY_BAD;
  129. break;
  130. case PSERIES_RECONFIG_REMOVE:
  131. if (pseries_remove_memory(node))
  132. err = NOTIFY_BAD;
  133. break;
  134. case PSERIES_DRCONF_MEM_ADD:
  135. case PSERIES_DRCONF_MEM_REMOVE:
  136. if (pseries_drconf_memory(node, action))
  137. err = NOTIFY_BAD;
  138. break;
  139. default:
  140. err = NOTIFY_DONE;
  141. break;
  142. }
  143. return err;
  144. }
  145. static struct notifier_block pseries_mem_nb = {
  146. .notifier_call = pseries_memory_notifier,
  147. };
  148. static int __init pseries_memory_hotplug_init(void)
  149. {
  150. if (firmware_has_feature(FW_FEATURE_LPAR))
  151. pSeries_reconfig_notifier_register(&pseries_mem_nb);
  152. return 0;
  153. }
  154. machine_device_initcall(pseries, pseries_memory_hotplug_init);