hotplug-memory.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <asm/firmware.h>
  15. #include <asm/machdep.h>
  16. #include <asm/pSeries_reconfig.h>
  17. #include <asm/sparsemem.h>
  18. static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
  19. {
  20. unsigned long start, start_pfn;
  21. struct zone *zone;
  22. int ret;
  23. start_pfn = base >> PAGE_SHIFT;
  24. if (!pfn_valid(start_pfn)) {
  25. memblock_remove(base, memblock_size);
  26. return 0;
  27. }
  28. zone = page_zone(pfn_to_page(start_pfn));
  29. /*
  30. * Remove section mappings and sysfs entries for the
  31. * section of the memory we are removing.
  32. *
  33. * NOTE: Ideally, this should be done in generic code like
  34. * remove_memory(). But remove_memory() gets called by writing
  35. * to sysfs "state" file and we can't remove sysfs entries
  36. * while writing to it. So we have to defer it to here.
  37. */
  38. ret = __remove_pages(zone, start_pfn, memblock_size >> PAGE_SHIFT);
  39. if (ret)
  40. return ret;
  41. /*
  42. * Update memory regions for memory remove
  43. */
  44. memblock_remove(base, memblock_size);
  45. /*
  46. * Remove htab bolted mappings for this section of memory
  47. */
  48. start = (unsigned long)__va(base);
  49. ret = remove_section_mapping(start, start + memblock_size);
  50. /* Ensure all vmalloc mappings are flushed in case they also
  51. * hit that section of memory
  52. */
  53. vm_unmap_aliases();
  54. return ret;
  55. }
  56. static int pseries_remove_memory(struct device_node *np)
  57. {
  58. const char *type;
  59. const unsigned int *regs;
  60. unsigned long base;
  61. unsigned int lmb_size;
  62. int ret = -EINVAL;
  63. /*
  64. * Check to see if we are actually removing memory
  65. */
  66. type = of_get_property(np, "device_type", NULL);
  67. if (type == NULL || strcmp(type, "memory") != 0)
  68. return 0;
  69. /*
  70. * Find the bae address and size of the memblock
  71. */
  72. regs = of_get_property(np, "reg", NULL);
  73. if (!regs)
  74. return ret;
  75. base = *(unsigned long *)regs;
  76. lmb_size = regs[3];
  77. ret = pseries_remove_memblock(base, lmb_size);
  78. return ret;
  79. }
  80. static int pseries_add_memory(struct device_node *np)
  81. {
  82. const char *type;
  83. const unsigned int *regs;
  84. unsigned long base;
  85. unsigned int lmb_size;
  86. int ret = -EINVAL;
  87. /*
  88. * Check to see if we are actually adding memory
  89. */
  90. type = of_get_property(np, "device_type", NULL);
  91. if (type == NULL || strcmp(type, "memory") != 0)
  92. return 0;
  93. /*
  94. * Find the base and size of the memblock
  95. */
  96. regs = of_get_property(np, "reg", NULL);
  97. if (!regs)
  98. return ret;
  99. base = *(unsigned long *)regs;
  100. lmb_size = regs[3];
  101. /*
  102. * Update memory region to represent the memory add
  103. */
  104. ret = memblock_add(base, lmb_size);
  105. return (ret < 0) ? -EINVAL : 0;
  106. }
  107. static int pseries_drconf_memory(unsigned long *base, unsigned int action)
  108. {
  109. struct device_node *np;
  110. const unsigned long *lmb_size;
  111. int rc;
  112. np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  113. if (!np)
  114. return -EINVAL;
  115. lmb_size = of_get_property(np, "ibm,lmb-size", NULL);
  116. if (!lmb_size) {
  117. of_node_put(np);
  118. return -EINVAL;
  119. }
  120. if (action == PSERIES_DRCONF_MEM_ADD) {
  121. rc = memblock_add(*base, *lmb_size);
  122. rc = (rc < 0) ? -EINVAL : 0;
  123. } else if (action == PSERIES_DRCONF_MEM_REMOVE) {
  124. rc = pseries_remove_memblock(*base, *lmb_size);
  125. } else {
  126. rc = -EINVAL;
  127. }
  128. of_node_put(np);
  129. return rc;
  130. }
  131. static int pseries_memory_notifier(struct notifier_block *nb,
  132. unsigned long action, void *node)
  133. {
  134. int err = NOTIFY_OK;
  135. switch (action) {
  136. case PSERIES_RECONFIG_ADD:
  137. if (pseries_add_memory(node))
  138. err = NOTIFY_BAD;
  139. break;
  140. case PSERIES_RECONFIG_REMOVE:
  141. if (pseries_remove_memory(node))
  142. err = NOTIFY_BAD;
  143. break;
  144. case PSERIES_DRCONF_MEM_ADD:
  145. case PSERIES_DRCONF_MEM_REMOVE:
  146. if (pseries_drconf_memory(node, action))
  147. err = NOTIFY_BAD;
  148. break;
  149. default:
  150. err = NOTIFY_DONE;
  151. break;
  152. }
  153. return err;
  154. }
  155. static struct notifier_block pseries_mem_nb = {
  156. .notifier_call = pseries_memory_notifier,
  157. };
  158. static int __init pseries_memory_hotplug_init(void)
  159. {
  160. if (firmware_has_feature(FW_FEATURE_LPAR))
  161. pSeries_reconfig_notifier_register(&pseries_mem_nb);
  162. return 0;
  163. }
  164. machine_device_initcall(pseries, pseries_memory_hotplug_init);