hotplug-memory.c 6.8 KB

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