hotplug-memory.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <asm/firmware.h>
  13. #include <asm/machdep.h>
  14. #include <asm/pSeries_reconfig.h>
  15. static int pseries_remove_memory(struct device_node *np)
  16. {
  17. const char *type;
  18. const unsigned int *my_index;
  19. const unsigned int *regs;
  20. u64 start_pfn, start;
  21. struct zone *zone;
  22. int ret = -EINVAL;
  23. /*
  24. * Check to see if we are actually removing memory
  25. */
  26. type = of_get_property(np, "device_type", NULL);
  27. if (type == NULL || strcmp(type, "memory") != 0)
  28. return 0;
  29. /*
  30. * Find the memory index and size of the removing section
  31. */
  32. my_index = of_get_property(np, "ibm,my-drc-index", NULL);
  33. if (!my_index)
  34. return ret;
  35. regs = of_get_property(np, "reg", NULL);
  36. if (!regs)
  37. return ret;
  38. start_pfn = section_nr_to_pfn(*my_index & 0xffff);
  39. zone = page_zone(pfn_to_page(start_pfn));
  40. /*
  41. * Remove section mappings and sysfs entries for the
  42. * section of the memory we are removing.
  43. *
  44. * NOTE: Ideally, this should be done in generic code like
  45. * remove_memory(). But remove_memory() gets called by writing
  46. * to sysfs "state" file and we can't remove sysfs entries
  47. * while writing to it. So we have to defer it to here.
  48. */
  49. ret = __remove_pages(zone, start_pfn, regs[3] >> PAGE_SHIFT);
  50. if (ret)
  51. return ret;
  52. /*
  53. * Remove htab bolted mappings for this section of memory
  54. */
  55. start = (unsigned long)__va(start_pfn << PAGE_SHIFT);
  56. ret = remove_section_mapping(start, start + regs[3]);
  57. return ret;
  58. }
  59. static int pseries_memory_notifier(struct notifier_block *nb,
  60. unsigned long action, void *node)
  61. {
  62. int err = NOTIFY_OK;
  63. switch (action) {
  64. case PSERIES_RECONFIG_ADD:
  65. break;
  66. case PSERIES_RECONFIG_REMOVE:
  67. if (pseries_remove_memory(node))
  68. err = NOTIFY_BAD;
  69. break;
  70. default:
  71. err = NOTIFY_DONE;
  72. break;
  73. }
  74. return err;
  75. }
  76. static struct notifier_block pseries_mem_nb = {
  77. .notifier_call = pseries_memory_notifier,
  78. };
  79. static int __init pseries_memory_hotplug_init(void)
  80. {
  81. if (firmware_has_feature(FW_FEATURE_LPAR))
  82. pSeries_reconfig_notifier_register(&pseries_mem_nb);
  83. return 0;
  84. }
  85. machine_device_initcall(pseries, pseries_memory_hotplug_init);