hotplug-memory.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/memblock.h>
  13. #include <linux/vmalloc.h>
  14. #include <asm/firmware.h>
  15. #include <asm/machdep.h>
  16. #include <asm/pSeries_reconfig.h>
  17. #include <asm/sparsemem.h>
  18. static unsigned long get_memblock_size(void)
  19. {
  20. struct device_node *np;
  21. unsigned int memblock_size = 0;
  22. np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  23. if (np) {
  24. const unsigned long *size;
  25. size = of_get_property(np, "ibm,lmb-size", NULL);
  26. memblock_size = size ? *size : 0;
  27. of_node_put(np);
  28. } else {
  29. unsigned int memzero_size = 0;
  30. const unsigned int *regs;
  31. np = of_find_node_by_path("/memory@0");
  32. if (np) {
  33. regs = of_get_property(np, "reg", NULL);
  34. memzero_size = regs ? regs[3] : 0;
  35. of_node_put(np);
  36. }
  37. if (memzero_size) {
  38. /* We now know the size of memory@0, use this to find
  39. * the first memoryblock and get its size.
  40. */
  41. char buf[64];
  42. sprintf(buf, "/memory@%x", memzero_size);
  43. np = of_find_node_by_path(buf);
  44. if (np) {
  45. regs = of_get_property(np, "reg", NULL);
  46. memblock_size = regs ? regs[3] : 0;
  47. of_node_put(np);
  48. }
  49. }
  50. }
  51. return memblock_size;
  52. }
  53. unsigned long memory_block_size_bytes(void)
  54. {
  55. return get_memblock_size();
  56. }
  57. static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
  58. {
  59. unsigned long start, start_pfn;
  60. struct zone *zone;
  61. int ret;
  62. start_pfn = base >> PAGE_SHIFT;
  63. if (!pfn_valid(start_pfn)) {
  64. memblock_remove(base, memblock_size);
  65. return 0;
  66. }
  67. zone = page_zone(pfn_to_page(start_pfn));
  68. /*
  69. * Remove section mappings and sysfs entries for the
  70. * section of the memory we are removing.
  71. *
  72. * NOTE: Ideally, this should be done in generic code like
  73. * remove_memory(). But remove_memory() gets called by writing
  74. * to sysfs "state" file and we can't remove sysfs entries
  75. * while writing to it. So we have to defer it to here.
  76. */
  77. ret = __remove_pages(zone, start_pfn, memblock_size >> PAGE_SHIFT);
  78. if (ret)
  79. return ret;
  80. /*
  81. * Update memory regions for memory remove
  82. */
  83. memblock_remove(base, memblock_size);
  84. /*
  85. * Remove htab bolted mappings for this section of memory
  86. */
  87. start = (unsigned long)__va(base);
  88. ret = remove_section_mapping(start, start + memblock_size);
  89. /* Ensure all vmalloc mappings are flushed in case they also
  90. * hit that section of memory
  91. */
  92. vm_unmap_aliases();
  93. return ret;
  94. }
  95. static int pseries_remove_memory(struct device_node *np)
  96. {
  97. const char *type;
  98. const unsigned int *regs;
  99. unsigned long base;
  100. unsigned int lmb_size;
  101. int ret = -EINVAL;
  102. /*
  103. * Check to see if we are actually removing memory
  104. */
  105. type = of_get_property(np, "device_type", NULL);
  106. if (type == NULL || strcmp(type, "memory") != 0)
  107. return 0;
  108. /*
  109. * Find the bae address and size of the memblock
  110. */
  111. regs = of_get_property(np, "reg", NULL);
  112. if (!regs)
  113. return ret;
  114. base = *(unsigned long *)regs;
  115. lmb_size = regs[3];
  116. ret = pseries_remove_memblock(base, lmb_size);
  117. return ret;
  118. }
  119. static int pseries_add_memory(struct device_node *np)
  120. {
  121. const char *type;
  122. const unsigned int *regs;
  123. unsigned long base;
  124. unsigned int lmb_size;
  125. int ret = -EINVAL;
  126. /*
  127. * Check to see if we are actually adding memory
  128. */
  129. type = of_get_property(np, "device_type", NULL);
  130. if (type == NULL || strcmp(type, "memory") != 0)
  131. return 0;
  132. /*
  133. * Find the base and size of the memblock
  134. */
  135. regs = of_get_property(np, "reg", NULL);
  136. if (!regs)
  137. return ret;
  138. base = *(unsigned long *)regs;
  139. lmb_size = regs[3];
  140. /*
  141. * Update memory region to represent the memory add
  142. */
  143. ret = memblock_add(base, lmb_size);
  144. return (ret < 0) ? -EINVAL : 0;
  145. }
  146. static int pseries_drconf_memory(unsigned long *base, unsigned int action)
  147. {
  148. unsigned long memblock_size;
  149. int rc;
  150. memblock_size = get_memblock_size();
  151. if (!memblock_size)
  152. return -EINVAL;
  153. if (action == PSERIES_DRCONF_MEM_ADD) {
  154. rc = memblock_add(*base, memblock_size);
  155. rc = (rc < 0) ? -EINVAL : 0;
  156. } else if (action == PSERIES_DRCONF_MEM_REMOVE) {
  157. rc = pseries_remove_memblock(*base, memblock_size);
  158. } else {
  159. rc = -EINVAL;
  160. }
  161. return rc;
  162. }
  163. static int pseries_memory_notifier(struct notifier_block *nb,
  164. unsigned long action, void *node)
  165. {
  166. int err = NOTIFY_OK;
  167. switch (action) {
  168. case PSERIES_RECONFIG_ADD:
  169. if (pseries_add_memory(node))
  170. err = NOTIFY_BAD;
  171. break;
  172. case PSERIES_RECONFIG_REMOVE:
  173. if (pseries_remove_memory(node))
  174. err = NOTIFY_BAD;
  175. break;
  176. case PSERIES_DRCONF_MEM_ADD:
  177. case PSERIES_DRCONF_MEM_REMOVE:
  178. if (pseries_drconf_memory(node, action))
  179. err = NOTIFY_BAD;
  180. break;
  181. default:
  182. err = NOTIFY_DONE;
  183. break;
  184. }
  185. return err;
  186. }
  187. static struct notifier_block pseries_mem_nb = {
  188. .notifier_call = pseries_memory_notifier,
  189. };
  190. static int __init pseries_memory_hotplug_init(void)
  191. {
  192. if (firmware_has_feature(FW_FEATURE_LPAR))
  193. pSeries_reconfig_notifier_register(&pseries_mem_nb);
  194. return 0;
  195. }
  196. machine_device_initcall(pseries, pseries_memory_hotplug_init);