hotplug-memory.c 4.1 KB

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