blob: e02f60ad6c99fc02f91d75274eb32eccc2867548 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams7a678322015-08-19 00:34:34 -04002/*
3 * Copyright (c) 2015, Christoph Hellwig.
4 * Copyright (c) 2015, Intel Corporation.
5 */
6#include <linux/platform_device.h>
Dan Williamsf7256dc2015-11-11 16:46:33 -08007#include <linux/memory_hotplug.h>
Dan Williams7a678322015-08-19 00:34:34 -04008#include <linux/libnvdimm.h>
9#include <linux/module.h>
10
Dan Williams7a678322015-08-19 00:34:34 -040011static int e820_pmem_remove(struct platform_device *pdev)
12{
13 struct nvdimm_bus *nvdimm_bus = platform_get_drvdata(pdev);
14
15 nvdimm_bus_unregister(nvdimm_bus);
16 return 0;
17}
18
Dan Williamsf7256dc2015-11-11 16:46:33 -080019#ifdef CONFIG_MEMORY_HOTPLUG
20static int e820_range_to_nid(resource_size_t addr)
21{
22 return memory_add_physaddr_to_nid(addr);
23}
24#else
25static int e820_range_to_nid(resource_size_t addr)
26{
27 return NUMA_NO_NODE;
28}
29#endif
30
Dan Williamsd76401a2018-06-02 11:43:39 -070031static int e820_register_one(struct resource *res, void *data)
32{
33 struct nd_region_desc ndr_desc;
34 struct nvdimm_bus *nvdimm_bus = data;
35
36 memset(&ndr_desc, 0, sizeof(ndr_desc));
37 ndr_desc.res = res;
Dan Williamsd76401a2018-06-02 11:43:39 -070038 ndr_desc.numa_node = e820_range_to_nid(res->start);
Dan Williams8fc5c732018-11-09 12:43:07 -080039 ndr_desc.target_node = ndr_desc.numa_node;
Dan Williamsd76401a2018-06-02 11:43:39 -070040 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
41 if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
42 return -ENXIO;
43 return 0;
44}
45
Dan Williams7a678322015-08-19 00:34:34 -040046static int e820_pmem_probe(struct platform_device *pdev)
47{
48 static struct nvdimm_bus_descriptor nd_desc;
49 struct device *dev = &pdev->dev;
50 struct nvdimm_bus *nvdimm_bus;
Dan Williamsd76401a2018-06-02 11:43:39 -070051 int rc = -ENXIO;
Dan Williams7a678322015-08-19 00:34:34 -040052
Dan Williams7a678322015-08-19 00:34:34 -040053 nd_desc.provider_name = "e820";
Dan Williamsbc9775d2016-07-21 20:03:19 -070054 nd_desc.module = THIS_MODULE;
Dan Williams7a678322015-08-19 00:34:34 -040055 nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
56 if (!nvdimm_bus)
57 goto err;
58 platform_set_drvdata(pdev, nvdimm_bus);
59
Dan Williamsd76401a2018-06-02 11:43:39 -070060 rc = walk_iomem_res_desc(IORES_DESC_PERSISTENT_MEMORY_LEGACY,
61 IORESOURCE_MEM, 0, -1, nvdimm_bus, e820_register_one);
62 if (rc)
63 goto err;
Dan Williams7a678322015-08-19 00:34:34 -040064 return 0;
Dan Williamsd76401a2018-06-02 11:43:39 -070065err:
Dan Williams7a678322015-08-19 00:34:34 -040066 nvdimm_bus_unregister(nvdimm_bus);
67 dev_err(dev, "failed to register legacy persistent memory ranges\n");
Dan Williamsd76401a2018-06-02 11:43:39 -070068 return rc;
Dan Williams7a678322015-08-19 00:34:34 -040069}
70
71static 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
Johannes Thumshirn3a71b3c2016-12-05 09:23:20 +010079module_platform_driver(e820_pmem_driver);
Dan Williams7a678322015-08-19 00:34:34 -040080
81MODULE_ALIAS("platform:e820_pmem*");
82MODULE_LICENSE("GPL v2");
83MODULE_AUTHOR("Intel Corporation");