blob: 87f72f725e4feef734fd21086785d3f3902e7415 [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
11static const struct attribute_group *e820_pmem_attribute_groups[] = {
12 &nvdimm_bus_attribute_group,
13 NULL,
14};
15
16static const struct attribute_group *e820_pmem_region_attribute_groups[] = {
17 &nd_region_attribute_group,
18 &nd_device_attribute_group,
19 NULL,
20};
21
22static int e820_pmem_remove(struct platform_device *pdev)
23{
24 struct nvdimm_bus *nvdimm_bus = platform_get_drvdata(pdev);
25
26 nvdimm_bus_unregister(nvdimm_bus);
27 return 0;
28}
29
Dan Williamsf7256dc2015-11-11 16:46:33 -080030#ifdef CONFIG_MEMORY_HOTPLUG
31static int e820_range_to_nid(resource_size_t addr)
32{
33 return memory_add_physaddr_to_nid(addr);
34}
35#else
36static int e820_range_to_nid(resource_size_t addr)
37{
38 return NUMA_NO_NODE;
39}
40#endif
41
Dan Williamsd76401a2018-06-02 11:43:39 -070042static int e820_register_one(struct resource *res, void *data)
43{
44 struct nd_region_desc ndr_desc;
45 struct nvdimm_bus *nvdimm_bus = data;
46
47 memset(&ndr_desc, 0, sizeof(ndr_desc));
48 ndr_desc.res = res;
49 ndr_desc.attr_groups = e820_pmem_region_attribute_groups;
50 ndr_desc.numa_node = e820_range_to_nid(res->start);
Dan Williams8fc5c732018-11-09 12:43:07 -080051 ndr_desc.target_node = ndr_desc.numa_node;
Dan Williamsd76401a2018-06-02 11:43:39 -070052 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
53 if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
54 return -ENXIO;
55 return 0;
56}
57
Dan Williams7a678322015-08-19 00:34:34 -040058static int e820_pmem_probe(struct platform_device *pdev)
59{
60 static struct nvdimm_bus_descriptor nd_desc;
61 struct device *dev = &pdev->dev;
62 struct nvdimm_bus *nvdimm_bus;
Dan Williamsd76401a2018-06-02 11:43:39 -070063 int rc = -ENXIO;
Dan Williams7a678322015-08-19 00:34:34 -040064
65 nd_desc.attr_groups = e820_pmem_attribute_groups;
66 nd_desc.provider_name = "e820";
Dan Williamsbc9775d2016-07-21 20:03:19 -070067 nd_desc.module = THIS_MODULE;
Dan Williams7a678322015-08-19 00:34:34 -040068 nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
69 if (!nvdimm_bus)
70 goto err;
71 platform_set_drvdata(pdev, nvdimm_bus);
72
Dan Williamsd76401a2018-06-02 11:43:39 -070073 rc = walk_iomem_res_desc(IORES_DESC_PERSISTENT_MEMORY_LEGACY,
74 IORESOURCE_MEM, 0, -1, nvdimm_bus, e820_register_one);
75 if (rc)
76 goto err;
Dan Williams7a678322015-08-19 00:34:34 -040077 return 0;
Dan Williamsd76401a2018-06-02 11:43:39 -070078err:
Dan Williams7a678322015-08-19 00:34:34 -040079 nvdimm_bus_unregister(nvdimm_bus);
80 dev_err(dev, "failed to register legacy persistent memory ranges\n");
Dan Williamsd76401a2018-06-02 11:43:39 -070081 return rc;
Dan Williams7a678322015-08-19 00:34:34 -040082}
83
84static struct platform_driver e820_pmem_driver = {
85 .probe = e820_pmem_probe,
86 .remove = e820_pmem_remove,
87 .driver = {
88 .name = "e820_pmem",
89 },
90};
91
Johannes Thumshirn3a71b3c2016-12-05 09:23:20 +010092module_platform_driver(e820_pmem_driver);
Dan Williams7a678322015-08-19 00:34:34 -040093
94MODULE_ALIAS("platform:e820_pmem*");
95MODULE_LICENSE("GPL v2");
96MODULE_AUTHOR("Intel Corporation");