hotplug-memory.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 *regs;
  20. unsigned long base;
  21. unsigned int lmb_size;
  22. u64 start_pfn, start;
  23. struct zone *zone;
  24. int ret = -EINVAL;
  25. /*
  26. * Check to see if we are actually removing memory
  27. */
  28. type = of_get_property(np, "device_type", NULL);
  29. if (type == NULL || strcmp(type, "memory") != 0)
  30. return 0;
  31. /*
  32. * Find the bae address and size of the lmb
  33. */
  34. regs = of_get_property(np, "reg", NULL);
  35. if (!regs)
  36. return ret;
  37. base = *(unsigned long *)regs;
  38. lmb_size = regs[3];
  39. start_pfn = base >> PFN_SECTION_SHIFT;
  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, lmb_size >> PAGE_SHIFT);
  51. if (ret)
  52. return ret;
  53. /*
  54. * Update memory regions for memory remove
  55. */
  56. lmb_remove(base, lmb_size);
  57. /*
  58. * Remove htab bolted mappings for this section of memory
  59. */
  60. start = (unsigned long)__va(base);
  61. ret = remove_section_mapping(start, start + lmb_size);
  62. return ret;
  63. }
  64. static int pseries_add_memory(struct device_node *np)
  65. {
  66. const char *type;
  67. const unsigned int *regs;
  68. unsigned long base;
  69. unsigned int lmb_size;
  70. u64 start_pfn;
  71. int ret = -EINVAL;
  72. /*
  73. * Check to see if we are actually adding memory
  74. */
  75. type = of_get_property(np, "device_type", NULL);
  76. if (type == NULL || strcmp(type, "memory") != 0)
  77. return 0;
  78. /*
  79. * Find the base and size of the lmb
  80. */
  81. regs = of_get_property(np, "reg", NULL);
  82. if (!regs)
  83. return ret;
  84. base = *(unsigned long *)regs;
  85. lmb_size = regs[3];
  86. /*
  87. * Update memory region to represent the memory add
  88. */
  89. lmb_add(base, lmb_size);
  90. return 0;
  91. }
  92. static int pseries_memory_notifier(struct notifier_block *nb,
  93. unsigned long action, void *node)
  94. {
  95. int err = NOTIFY_OK;
  96. switch (action) {
  97. case PSERIES_RECONFIG_ADD:
  98. if (pseries_add_memory(node))
  99. err = NOTIFY_BAD;
  100. break;
  101. case PSERIES_RECONFIG_REMOVE:
  102. if (pseries_remove_memory(node))
  103. err = NOTIFY_BAD;
  104. break;
  105. default:
  106. err = NOTIFY_DONE;
  107. break;
  108. }
  109. return err;
  110. }
  111. static struct notifier_block pseries_mem_nb = {
  112. .notifier_call = pseries_memory_notifier,
  113. };
  114. static int __init pseries_memory_hotplug_init(void)
  115. {
  116. if (firmware_has_feature(FW_FEATURE_LPAR))
  117. pSeries_reconfig_notifier_register(&pseries_mem_nb);
  118. return 0;
  119. }
  120. machine_device_initcall(pseries, pseries_memory_hotplug_init);