e820.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_register_one(struct resource *res, void *data)
  36. {
  37. struct nd_region_desc ndr_desc;
  38. struct nvdimm_bus *nvdimm_bus = data;
  39. memset(&ndr_desc, 0, sizeof(ndr_desc));
  40. ndr_desc.res = res;
  41. ndr_desc.attr_groups = e820_pmem_region_attribute_groups;
  42. ndr_desc.numa_node = e820_range_to_nid(res->start);
  43. set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
  44. if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
  45. return -ENXIO;
  46. return 0;
  47. }
  48. static int e820_pmem_probe(struct platform_device *pdev)
  49. {
  50. static struct nvdimm_bus_descriptor nd_desc;
  51. struct device *dev = &pdev->dev;
  52. struct nvdimm_bus *nvdimm_bus;
  53. int rc = -ENXIO;
  54. nd_desc.attr_groups = e820_pmem_attribute_groups;
  55. nd_desc.provider_name = "e820";
  56. nd_desc.module = THIS_MODULE;
  57. nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
  58. if (!nvdimm_bus)
  59. goto err;
  60. platform_set_drvdata(pdev, nvdimm_bus);
  61. rc = walk_iomem_res_desc(IORES_DESC_PERSISTENT_MEMORY_LEGACY,
  62. IORESOURCE_MEM, 0, -1, nvdimm_bus, e820_register_one);
  63. if (rc)
  64. goto err;
  65. return 0;
  66. err:
  67. nvdimm_bus_unregister(nvdimm_bus);
  68. dev_err(dev, "failed to register legacy persistent memory ranges\n");
  69. return rc;
  70. }
  71. static struct platform_driver e820_pmem_driver = {
  72. .probe = e820_pmem_probe,
  73. .remove = e820_pmem_remove,
  74. .driver = {
  75. .name = "e820_pmem",
  76. },
  77. };
  78. module_platform_driver(e820_pmem_driver);
  79. MODULE_ALIAS("platform:e820_pmem*");
  80. MODULE_LICENSE("GPL v2");
  81. MODULE_AUTHOR("Intel Corporation");