hotplug-memory.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. unsigned long pseries_memory_block_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. #ifdef CONFIG_MEMORY_HOTREMOVE
  59. static int pseries_remove_memory(u64 start, u64 size)
  60. {
  61. int ret;
  62. /* Remove htab bolted mappings for this section of memory */
  63. start = (unsigned long)__va(start);
  64. ret = remove_section_mapping(start, start + size);
  65. /* Ensure all vmalloc mappings are flushed in case they also
  66. * hit that section of memory
  67. */
  68. vm_unmap_aliases();
  69. return ret;
  70. }
  71. static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
  72. {
  73. unsigned long block_sz, start_pfn;
  74. int sections_per_block;
  75. int i, nid;
  76. start_pfn = base >> PAGE_SHIFT;
  77. lock_device_hotplug();
  78. if (!pfn_valid(start_pfn))
  79. goto out;
  80. block_sz = pseries_memory_block_size();
  81. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  82. nid = memory_add_physaddr_to_nid(base);
  83. for (i = 0; i < sections_per_block; i++) {
  84. remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
  85. base += MIN_MEMORY_BLOCK_SIZE;
  86. }
  87. out:
  88. /* Update memory regions for memory remove */
  89. memblock_remove(base, memblock_size);
  90. unlock_device_hotplug();
  91. return 0;
  92. }
  93. static int pseries_remove_mem_node(struct device_node *np)
  94. {
  95. const char *type;
  96. const unsigned int *regs;
  97. unsigned long base;
  98. unsigned int lmb_size;
  99. int ret = -EINVAL;
  100. /*
  101. * Check to see if we are actually removing memory
  102. */
  103. type = of_get_property(np, "device_type", NULL);
  104. if (type == NULL || strcmp(type, "memory") != 0)
  105. return 0;
  106. /*
  107. * Find the bae address and size of the memblock
  108. */
  109. regs = of_get_property(np, "reg", NULL);
  110. if (!regs)
  111. return ret;
  112. base = *(unsigned long *)regs;
  113. lmb_size = regs[3];
  114. pseries_remove_memblock(base, lmb_size);
  115. return 0;
  116. }
  117. #else
  118. static inline int pseries_remove_memblock(unsigned long base,
  119. unsigned int memblock_size)
  120. {
  121. return -EOPNOTSUPP;
  122. }
  123. static inline int pseries_remove_mem_node(struct device_node *np)
  124. {
  125. return -EOPNOTSUPP;
  126. }
  127. #endif /* CONFIG_MEMORY_HOTREMOVE */
  128. static int pseries_add_mem_node(struct device_node *np)
  129. {
  130. const char *type;
  131. const unsigned int *regs;
  132. unsigned long base;
  133. unsigned int lmb_size;
  134. int ret = -EINVAL;
  135. /*
  136. * Check to see if we are actually adding memory
  137. */
  138. type = of_get_property(np, "device_type", NULL);
  139. if (type == NULL || strcmp(type, "memory") != 0)
  140. return 0;
  141. /*
  142. * Find the base and size of the memblock
  143. */
  144. regs = of_get_property(np, "reg", NULL);
  145. if (!regs)
  146. return ret;
  147. base = *(unsigned long *)regs;
  148. lmb_size = regs[3];
  149. /*
  150. * Update memory region to represent the memory add
  151. */
  152. ret = memblock_add(base, lmb_size);
  153. return (ret < 0) ? -EINVAL : 0;
  154. }
  155. static int pseries_update_drconf_memory(struct of_prop_reconfig *pr)
  156. {
  157. struct of_drconf_cell *new_drmem, *old_drmem;
  158. unsigned long memblock_size;
  159. u32 entries;
  160. u32 *p;
  161. int i, rc = -EINVAL;
  162. memblock_size = pseries_memory_block_size();
  163. if (!memblock_size)
  164. return -EINVAL;
  165. p = (u32 *)of_get_property(pr->dn, "ibm,dynamic-memory", NULL);
  166. if (!p)
  167. return -EINVAL;
  168. /* The first int of the property is the number of lmb's described
  169. * by the property. This is followed by an array of of_drconf_cell
  170. * entries. Get the niumber of entries and skip to the array of
  171. * of_drconf_cell's.
  172. */
  173. entries = *p++;
  174. old_drmem = (struct of_drconf_cell *)p;
  175. p = (u32 *)pr->prop->value;
  176. p++;
  177. new_drmem = (struct of_drconf_cell *)p;
  178. for (i = 0; i < entries; i++) {
  179. if ((old_drmem[i].flags & DRCONF_MEM_ASSIGNED) &&
  180. (!(new_drmem[i].flags & DRCONF_MEM_ASSIGNED))) {
  181. rc = pseries_remove_memblock(old_drmem[i].base_addr,
  182. memblock_size);
  183. break;
  184. } else if ((!(old_drmem[i].flags & DRCONF_MEM_ASSIGNED)) &&
  185. (new_drmem[i].flags & DRCONF_MEM_ASSIGNED)) {
  186. rc = memblock_add(old_drmem[i].base_addr,
  187. memblock_size);
  188. rc = (rc < 0) ? -EINVAL : 0;
  189. break;
  190. }
  191. }
  192. return rc;
  193. }
  194. static int pseries_memory_notifier(struct notifier_block *nb,
  195. unsigned long action, void *node)
  196. {
  197. struct of_prop_reconfig *pr;
  198. int err = 0;
  199. switch (action) {
  200. case OF_RECONFIG_ATTACH_NODE:
  201. err = pseries_add_mem_node(node);
  202. break;
  203. case OF_RECONFIG_DETACH_NODE:
  204. err = pseries_remove_mem_node(node);
  205. break;
  206. case OF_RECONFIG_UPDATE_PROPERTY:
  207. pr = (struct of_prop_reconfig *)node;
  208. if (!strcmp(pr->prop->name, "ibm,dynamic-memory"))
  209. err = pseries_update_drconf_memory(pr);
  210. break;
  211. }
  212. return notifier_from_errno(err);
  213. }
  214. static struct notifier_block pseries_mem_nb = {
  215. .notifier_call = pseries_memory_notifier,
  216. };
  217. static int __init pseries_memory_hotplug_init(void)
  218. {
  219. if (firmware_has_feature(FW_FEATURE_LPAR))
  220. of_reconfig_notifier_register(&pseries_mem_nb);
  221. #ifdef CONFIG_MEMORY_HOTREMOVE
  222. ppc_md.remove_memory = pseries_remove_memory;
  223. #endif
  224. return 0;
  225. }
  226. machine_device_initcall(pseries, pseries_memory_hotplug_init);