hotplug-memory.c 6.6 KB

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