hotplug-memory.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/pSeries_reconfig.h>
  18. #include <asm/sparsemem.h>
  19. static unsigned long get_memblock_size(void)
  20. {
  21. struct device_node *np;
  22. unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
  23. struct resource r;
  24. np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  25. if (np) {
  26. const __be64 *size;
  27. size = of_get_property(np, "ibm,lmb-size", NULL);
  28. if (size)
  29. memblock_size = be64_to_cpup(size);
  30. of_node_put(np);
  31. } else if (machine_is(pseries)) {
  32. /* This fallback really only applies to pseries */
  33. unsigned int memzero_size = 0;
  34. np = of_find_node_by_path("/memory@0");
  35. if (np) {
  36. if (!of_address_to_resource(np, 0, &r))
  37. memzero_size = resource_size(&r);
  38. of_node_put(np);
  39. }
  40. if (memzero_size) {
  41. /* We now know the size of memory@0, use this to find
  42. * the first memoryblock and get its size.
  43. */
  44. char buf[64];
  45. sprintf(buf, "/memory@%x", memzero_size);
  46. np = of_find_node_by_path(buf);
  47. if (np) {
  48. if (!of_address_to_resource(np, 0, &r))
  49. memblock_size = resource_size(&r);
  50. of_node_put(np);
  51. }
  52. }
  53. }
  54. return memblock_size;
  55. }
  56. /* WARNING: This is going to override the generic definition whenever
  57. * pseries is built-in regardless of what platform is active at boot
  58. * time. This is fine for now as this is the only "option" and it
  59. * should work everywhere. If not, we'll have to turn this into a
  60. * ppc_md. callback
  61. */
  62. unsigned long memory_block_size_bytes(void)
  63. {
  64. return get_memblock_size();
  65. }
  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. start_pfn = base >> PAGE_SHIFT;
  72. if (!pfn_valid(start_pfn)) {
  73. memblock_remove(base, memblock_size);
  74. return 0;
  75. }
  76. zone = page_zone(pfn_to_page(start_pfn));
  77. /*
  78. * Remove section mappings and sysfs entries for the
  79. * section of the memory we are removing.
  80. *
  81. * NOTE: Ideally, this should be done in generic code like
  82. * remove_memory(). But remove_memory() gets called by writing
  83. * to sysfs "state" file and we can't remove sysfs entries
  84. * while writing to it. So we have to defer it to here.
  85. */
  86. ret = __remove_pages(zone, start_pfn, memblock_size >> PAGE_SHIFT);
  87. if (ret)
  88. return ret;
  89. /*
  90. * Update memory regions for memory remove
  91. */
  92. memblock_remove(base, memblock_size);
  93. /*
  94. * Remove htab bolted mappings for this section of memory
  95. */
  96. start = (unsigned long)__va(base);
  97. ret = remove_section_mapping(start, start + memblock_size);
  98. /* Ensure all vmalloc mappings are flushed in case they also
  99. * hit that section of memory
  100. */
  101. vm_unmap_aliases();
  102. return ret;
  103. }
  104. static int pseries_remove_memory(struct device_node *np)
  105. {
  106. const char *type;
  107. const unsigned int *regs;
  108. unsigned long base;
  109. unsigned int lmb_size;
  110. int ret = -EINVAL;
  111. /*
  112. * Check to see if we are actually removing memory
  113. */
  114. type = of_get_property(np, "device_type", NULL);
  115. if (type == NULL || strcmp(type, "memory") != 0)
  116. return 0;
  117. /*
  118. * Find the bae address and size of the memblock
  119. */
  120. regs = of_get_property(np, "reg", NULL);
  121. if (!regs)
  122. return ret;
  123. base = *(unsigned long *)regs;
  124. lmb_size = regs[3];
  125. ret = pseries_remove_memblock(base, lmb_size);
  126. return ret;
  127. }
  128. static int pseries_add_memory(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_drconf_memory(unsigned long *base, unsigned int action)
  156. {
  157. unsigned long memblock_size;
  158. int rc;
  159. memblock_size = get_memblock_size();
  160. if (!memblock_size)
  161. return -EINVAL;
  162. if (action == PSERIES_DRCONF_MEM_ADD) {
  163. rc = memblock_add(*base, memblock_size);
  164. rc = (rc < 0) ? -EINVAL : 0;
  165. } else if (action == PSERIES_DRCONF_MEM_REMOVE) {
  166. rc = pseries_remove_memblock(*base, memblock_size);
  167. } else {
  168. rc = -EINVAL;
  169. }
  170. return rc;
  171. }
  172. static int pseries_memory_notifier(struct notifier_block *nb,
  173. unsigned long action, void *node)
  174. {
  175. int err = 0;
  176. switch (action) {
  177. case PSERIES_RECONFIG_ADD:
  178. err = pseries_add_memory(node);
  179. break;
  180. case PSERIES_RECONFIG_REMOVE:
  181. err = pseries_remove_memory(node);
  182. break;
  183. case PSERIES_DRCONF_MEM_ADD:
  184. case PSERIES_DRCONF_MEM_REMOVE:
  185. err = pseries_drconf_memory(node, action);
  186. break;
  187. }
  188. return notifier_from_errno(err);
  189. }
  190. static struct notifier_block pseries_mem_nb = {
  191. .notifier_call = pseries_memory_notifier,
  192. };
  193. static int __init pseries_memory_hotplug_init(void)
  194. {
  195. if (firmware_has_feature(FW_FEATURE_LPAR))
  196. pSeries_reconfig_notifier_register(&pseries_mem_nb);
  197. return 0;
  198. }
  199. machine_device_initcall(pseries, pseries_memory_hotplug_init);