e820.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. nd_desc.module = THIS_MODULE;
  44. nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
  45. if (!nvdimm_bus)
  46. goto err;
  47. platform_set_drvdata(pdev, nvdimm_bus);
  48. for (p = iomem_resource.child; p ; p = p->sibling) {
  49. struct nd_region_desc ndr_desc;
  50. if (p->desc != IORES_DESC_PERSISTENT_MEMORY_LEGACY)
  51. continue;
  52. memset(&ndr_desc, 0, sizeof(ndr_desc));
  53. ndr_desc.res = p;
  54. ndr_desc.attr_groups = e820_pmem_region_attribute_groups;
  55. ndr_desc.numa_node = e820_range_to_nid(p->start);
  56. set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
  57. if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
  58. goto err;
  59. }
  60. return 0;
  61. err:
  62. nvdimm_bus_unregister(nvdimm_bus);
  63. dev_err(dev, "failed to register legacy persistent memory ranges\n");
  64. return -ENXIO;
  65. }
  66. static struct platform_driver e820_pmem_driver = {
  67. .probe = e820_pmem_probe,
  68. .remove = e820_pmem_remove,
  69. .driver = {
  70. .name = "e820_pmem",
  71. },
  72. };
  73. static __init int e820_pmem_init(void)
  74. {
  75. return platform_driver_register(&e820_pmem_driver);
  76. }
  77. static __exit void e820_pmem_exit(void)
  78. {
  79. platform_driver_unregister(&e820_pmem_driver);
  80. }
  81. MODULE_ALIAS("platform:e820_pmem*");
  82. MODULE_LICENSE("GPL v2");
  83. MODULE_AUTHOR("Intel Corporation");
  84. module_init(e820_pmem_init);
  85. module_exit(e820_pmem_exit);