e820.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2015, Christoph Hellwig.
  3. * Copyright (c) 2015, Intel Corporation.
  4. */
  5. #include <linux/platform_device.h>
  6. #include <linux/memory_hotplug.h>
  7. #include <linux/libnvdimm.h>
  8. #include <linux/module.h>
  9. static const struct attribute_group *e820_pmem_attribute_groups[] = {
  10. &nvdimm_bus_attribute_group,
  11. NULL,
  12. };
  13. static const struct attribute_group *e820_pmem_region_attribute_groups[] = {
  14. &nd_region_attribute_group,
  15. &nd_device_attribute_group,
  16. NULL,
  17. };
  18. static int e820_pmem_remove(struct platform_device *pdev)
  19. {
  20. struct nvdimm_bus *nvdimm_bus = platform_get_drvdata(pdev);
  21. nvdimm_bus_unregister(nvdimm_bus);
  22. return 0;
  23. }
  24. #ifdef CONFIG_MEMORY_HOTPLUG
  25. static int e820_range_to_nid(resource_size_t addr)
  26. {
  27. return memory_add_physaddr_to_nid(addr);
  28. }
  29. #else
  30. static int e820_range_to_nid(resource_size_t addr)
  31. {
  32. return NUMA_NO_NODE;
  33. }
  34. #endif
  35. static int e820_pmem_probe(struct platform_device *pdev)
  36. {
  37. static struct nvdimm_bus_descriptor nd_desc;
  38. struct device *dev = &pdev->dev;
  39. struct nvdimm_bus *nvdimm_bus;
  40. struct resource *p;
  41. nd_desc.attr_groups = e820_pmem_attribute_groups;
  42. nd_desc.provider_name = "e820";
  43. nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
  44. if (!nvdimm_bus)
  45. goto err;
  46. platform_set_drvdata(pdev, nvdimm_bus);
  47. for (p = iomem_resource.child; p ; p = p->sibling) {
  48. struct nd_region_desc ndr_desc;
  49. if (strncmp(p->name, "Persistent Memory (legacy)", 26) != 0)
  50. continue;
  51. memset(&ndr_desc, 0, sizeof(ndr_desc));
  52. ndr_desc.res = p;
  53. ndr_desc.attr_groups = e820_pmem_region_attribute_groups;
  54. ndr_desc.numa_node = e820_range_to_nid(p->start);
  55. set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
  56. if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
  57. goto err;
  58. }
  59. return 0;
  60. err:
  61. nvdimm_bus_unregister(nvdimm_bus);
  62. dev_err(dev, "failed to register legacy persistent memory ranges\n");
  63. return -ENXIO;
  64. }
  65. static struct platform_driver e820_pmem_driver = {
  66. .probe = e820_pmem_probe,
  67. .remove = e820_pmem_remove,
  68. .driver = {
  69. .name = "e820_pmem",
  70. },
  71. };
  72. static __init int e820_pmem_init(void)
  73. {
  74. return platform_driver_register(&e820_pmem_driver);
  75. }
  76. static __exit void e820_pmem_exit(void)
  77. {
  78. platform_driver_unregister(&e820_pmem_driver);
  79. }
  80. MODULE_ALIAS("platform:e820_pmem*");
  81. MODULE_LICENSE("GPL v2");
  82. MODULE_AUTHOR("Intel Corporation");
  83. module_init(e820_pmem_init);
  84. module_exit(e820_pmem_exit);