hotplug-memory.c 6.9 KB

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