hotplug-memory.c 4.1 KB

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