hotplug-memory.c 5.9 KB

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