hotplug-memory.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_memory(struct device_node *np)
  17. {
  18. const char *type;
  19. const unsigned int *my_index;
  20. const unsigned int *regs;
  21. u64 start_pfn, start;
  22. struct zone *zone;
  23. int ret = -EINVAL;
  24. /*
  25. * Check to see if we are actually removing memory
  26. */
  27. type = of_get_property(np, "device_type", NULL);
  28. if (type == NULL || strcmp(type, "memory") != 0)
  29. return 0;
  30. /*
  31. * Find the memory index and size of the removing section
  32. */
  33. my_index = of_get_property(np, "ibm,my-drc-index", NULL);
  34. if (!my_index)
  35. return ret;
  36. regs = of_get_property(np, "reg", NULL);
  37. if (!regs)
  38. return ret;
  39. start_pfn = section_nr_to_pfn(*my_index & 0xffff);
  40. zone = page_zone(pfn_to_page(start_pfn));
  41. /*
  42. * Remove section mappings and sysfs entries for the
  43. * section of the memory we are removing.
  44. *
  45. * NOTE: Ideally, this should be done in generic code like
  46. * remove_memory(). But remove_memory() gets called by writing
  47. * to sysfs "state" file and we can't remove sysfs entries
  48. * while writing to it. So we have to defer it to here.
  49. */
  50. ret = __remove_pages(zone, start_pfn, regs[3] >> PAGE_SHIFT);
  51. if (ret)
  52. return ret;
  53. /*
  54. * Update memory regions for memory remove
  55. */
  56. lmb_remove(start_pfn << PAGE_SHIFT, regs[3]);
  57. /*
  58. * Remove htab bolted mappings for this section of memory
  59. */
  60. start = (unsigned long)__va(start_pfn << PAGE_SHIFT);
  61. ret = remove_section_mapping(start, start + regs[3]);
  62. return ret;
  63. }
  64. static int pseries_add_memory(struct device_node *np)
  65. {
  66. const char *type;
  67. const unsigned int *my_index;
  68. const unsigned int *regs;
  69. u64 start_pfn;
  70. int ret = -EINVAL;
  71. /*
  72. * Check to see if we are actually adding memory
  73. */
  74. type = of_get_property(np, "device_type", NULL);
  75. if (type == NULL || strcmp(type, "memory") != 0)
  76. return 0;
  77. /*
  78. * Find the memory index and size of the added section
  79. */
  80. my_index = of_get_property(np, "ibm,my-drc-index", NULL);
  81. if (!my_index)
  82. return ret;
  83. regs = of_get_property(np, "reg", NULL);
  84. if (!regs)
  85. return ret;
  86. start_pfn = section_nr_to_pfn(*my_index & 0xffff);
  87. /*
  88. * Update memory region to represent the memory add
  89. */
  90. lmb_add(start_pfn << PAGE_SHIFT, regs[3]);
  91. return 0;
  92. }
  93. static int pseries_memory_notifier(struct notifier_block *nb,
  94. unsigned long action, void *node)
  95. {
  96. int err = NOTIFY_OK;
  97. switch (action) {
  98. case PSERIES_RECONFIG_ADD:
  99. if (pseries_add_memory(node))
  100. err = NOTIFY_BAD;
  101. break;
  102. case PSERIES_RECONFIG_REMOVE:
  103. if (pseries_remove_memory(node))
  104. err = NOTIFY_BAD;
  105. break;
  106. default:
  107. err = NOTIFY_DONE;
  108. break;
  109. }
  110. return err;
  111. }
  112. static struct notifier_block pseries_mem_nb = {
  113. .notifier_call = pseries_memory_notifier,
  114. };
  115. static int __init pseries_memory_hotplug_init(void)
  116. {
  117. if (firmware_has_feature(FW_FEATURE_LPAR))
  118. pSeries_reconfig_notifier_register(&pseries_mem_nb);
  119. return 0;
  120. }
  121. machine_device_initcall(pseries, pseries_memory_hotplug_init);