hotplug-memory.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/of_address.h>
  13. #include <linux/memblock.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/memory.h>
  16. #include <linux/memory_hotplug.h>
  17. #include <asm/firmware.h>
  18. #include <asm/machdep.h>
  19. #include <asm/prom.h>
  20. #include <asm/sparsemem.h>
  21. #include "pseries.h"
  22. unsigned long pseries_memory_block_size(void)
  23. {
  24. struct device_node *np;
  25. unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
  26. struct resource r;
  27. np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  28. if (np) {
  29. const __be64 *size;
  30. size = of_get_property(np, "ibm,lmb-size", NULL);
  31. if (size)
  32. memblock_size = be64_to_cpup(size);
  33. of_node_put(np);
  34. } else if (machine_is(pseries)) {
  35. /* This fallback really only applies to pseries */
  36. unsigned int memzero_size = 0;
  37. np = of_find_node_by_path("/memory@0");
  38. if (np) {
  39. if (!of_address_to_resource(np, 0, &r))
  40. memzero_size = resource_size(&r);
  41. of_node_put(np);
  42. }
  43. if (memzero_size) {
  44. /* We now know the size of memory@0, use this to find
  45. * the first memoryblock and get its size.
  46. */
  47. char buf[64];
  48. sprintf(buf, "/memory@%x", memzero_size);
  49. np = of_find_node_by_path(buf);
  50. if (np) {
  51. if (!of_address_to_resource(np, 0, &r))
  52. memblock_size = resource_size(&r);
  53. of_node_put(np);
  54. }
  55. }
  56. }
  57. return memblock_size;
  58. }
  59. #ifdef CONFIG_MEMORY_HOTREMOVE
  60. static int pseries_remove_memory(u64 start, u64 size)
  61. {
  62. int ret;
  63. /* Remove htab bolted mappings for this section of memory */
  64. start = (unsigned long)__va(start);
  65. ret = remove_section_mapping(start, start + size);
  66. /* Ensure all vmalloc mappings are flushed in case they also
  67. * hit that section of memory
  68. */
  69. vm_unmap_aliases();
  70. return ret;
  71. }
  72. static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
  73. {
  74. unsigned long block_sz, start_pfn;
  75. int sections_per_block;
  76. int i, nid;
  77. start_pfn = base >> PAGE_SHIFT;
  78. lock_device_hotplug();
  79. if (!pfn_valid(start_pfn))
  80. goto out;
  81. block_sz = pseries_memory_block_size();
  82. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  83. nid = memory_add_physaddr_to_nid(base);
  84. for (i = 0; i < sections_per_block; i++) {
  85. remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
  86. base += MIN_MEMORY_BLOCK_SIZE;
  87. }
  88. out:
  89. /* Update memory regions for memory remove */
  90. memblock_remove(base, memblock_size);
  91. unlock_device_hotplug();
  92. return 0;
  93. }
  94. static int pseries_remove_mem_node(struct device_node *np)
  95. {
  96. const char *type;
  97. const __be32 *regs;
  98. unsigned long base;
  99. unsigned int lmb_size;
  100. int ret = -EINVAL;
  101. /*
  102. * Check to see if we are actually removing memory
  103. */
  104. type = of_get_property(np, "device_type", NULL);
  105. if (type == NULL || strcmp(type, "memory") != 0)
  106. return 0;
  107. /*
  108. * Find the base address and size of the memblock
  109. */
  110. regs = of_get_property(np, "reg", NULL);
  111. if (!regs)
  112. return ret;
  113. base = be64_to_cpu(*(unsigned long *)regs);
  114. lmb_size = be32_to_cpu(regs[3]);
  115. pseries_remove_memblock(base, lmb_size);
  116. return 0;
  117. }
  118. #else
  119. static inline int pseries_remove_memblock(unsigned long base,
  120. unsigned int memblock_size)
  121. {
  122. return -EOPNOTSUPP;
  123. }
  124. static inline int pseries_remove_mem_node(struct device_node *np)
  125. {
  126. return 0;
  127. }
  128. #endif /* CONFIG_MEMORY_HOTREMOVE */
  129. static int pseries_add_mem_node(struct device_node *np)
  130. {
  131. const char *type;
  132. const __be32 *regs;
  133. unsigned long base;
  134. unsigned int lmb_size;
  135. int ret = -EINVAL;
  136. /*
  137. * Check to see if we are actually adding memory
  138. */
  139. type = of_get_property(np, "device_type", NULL);
  140. if (type == NULL || strcmp(type, "memory") != 0)
  141. return 0;
  142. /*
  143. * Find the base and size of the memblock
  144. */
  145. regs = of_get_property(np, "reg", NULL);
  146. if (!regs)
  147. return ret;
  148. base = be64_to_cpu(*(unsigned long *)regs);
  149. lmb_size = be32_to_cpu(regs[3]);
  150. /*
  151. * Update memory region to represent the memory add
  152. */
  153. ret = memblock_add(base, lmb_size);
  154. return (ret < 0) ? -EINVAL : 0;
  155. }
  156. static int pseries_update_drconf_memory(struct of_prop_reconfig *pr)
  157. {
  158. struct of_drconf_cell *new_drmem, *old_drmem;
  159. unsigned long memblock_size;
  160. u32 entries;
  161. __be32 *p;
  162. int i, rc = -EINVAL;
  163. memblock_size = pseries_memory_block_size();
  164. if (!memblock_size)
  165. return -EINVAL;
  166. p = (__be32 *) pr->old_prop->value;
  167. if (!p)
  168. return -EINVAL;
  169. /* The first int of the property is the number of lmb's described
  170. * by the property. This is followed by an array of of_drconf_cell
  171. * entries. Get the number of entries and skip to the array of
  172. * of_drconf_cell's.
  173. */
  174. entries = be32_to_cpu(*p++);
  175. old_drmem = (struct of_drconf_cell *)p;
  176. p = (__be32 *)pr->prop->value;
  177. p++;
  178. new_drmem = (struct of_drconf_cell *)p;
  179. for (i = 0; i < entries; i++) {
  180. if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
  181. (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
  182. rc = pseries_remove_memblock(
  183. be64_to_cpu(old_drmem[i].base_addr),
  184. memblock_size);
  185. break;
  186. } else if ((!(be32_to_cpu(old_drmem[i].flags) &
  187. DRCONF_MEM_ASSIGNED)) &&
  188. (be32_to_cpu(new_drmem[i].flags) &
  189. DRCONF_MEM_ASSIGNED)) {
  190. rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
  191. memblock_size);
  192. rc = (rc < 0) ? -EINVAL : 0;
  193. break;
  194. }
  195. }
  196. return rc;
  197. }
  198. static int pseries_memory_notifier(struct notifier_block *nb,
  199. unsigned long action, void *node)
  200. {
  201. struct of_prop_reconfig *pr;
  202. int err = 0;
  203. switch (action) {
  204. case OF_RECONFIG_ATTACH_NODE:
  205. err = pseries_add_mem_node(node);
  206. break;
  207. case OF_RECONFIG_DETACH_NODE:
  208. err = pseries_remove_mem_node(node);
  209. break;
  210. case OF_RECONFIG_UPDATE_PROPERTY:
  211. pr = (struct of_prop_reconfig *)node;
  212. if (!strcmp(pr->prop->name, "ibm,dynamic-memory"))
  213. err = pseries_update_drconf_memory(pr);
  214. break;
  215. }
  216. return notifier_from_errno(err);
  217. }
  218. static struct notifier_block pseries_mem_nb = {
  219. .notifier_call = pseries_memory_notifier,
  220. };
  221. static int __init pseries_memory_hotplug_init(void)
  222. {
  223. if (firmware_has_feature(FW_FEATURE_LPAR))
  224. of_reconfig_notifier_register(&pseries_mem_nb);
  225. #ifdef CONFIG_MEMORY_HOTREMOVE
  226. ppc_md.remove_memory = pseries_remove_memory;
  227. #endif
  228. return 0;
  229. }
  230. machine_device_initcall(pseries, pseries_memory_hotplug_init);