xref: /openbmc/linux/drivers/nvdimm/e820.c (revision 0545810f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, Christoph Hellwig.
4  * Copyright (c) 2015, Intel Corporation.
5  */
6 #include <linux/platform_device.h>
7 #include <linux/memory_hotplug.h>
8 #include <linux/libnvdimm.h>
9 #include <linux/module.h>
10 #include <linux/numa.h>
11 
12 static int e820_pmem_remove(struct platform_device *pdev)
13 {
14 	struct nvdimm_bus *nvdimm_bus = platform_get_drvdata(pdev);
15 
16 	nvdimm_bus_unregister(nvdimm_bus);
17 	return 0;
18 }
19 
20 static int e820_register_one(struct resource *res, void *data)
21 {
22 	struct nd_region_desc ndr_desc;
23 	struct nvdimm_bus *nvdimm_bus = data;
24 	int nid = phys_to_target_node(res->start);
25 
26 	memset(&ndr_desc, 0, sizeof(ndr_desc));
27 	ndr_desc.res = res;
28 	ndr_desc.numa_node = numa_map_to_online_node(nid);
29 	ndr_desc.target_node = nid;
30 	set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
31 	if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
32 		return -ENXIO;
33 	return 0;
34 }
35 
36 static int e820_pmem_probe(struct platform_device *pdev)
37 {
38 	static struct nvdimm_bus_descriptor nd_desc;
39 	struct device *dev = &pdev->dev;
40 	struct nvdimm_bus *nvdimm_bus;
41 	int rc = -ENXIO;
42 
43 	nd_desc.provider_name = "e820";
44 	nd_desc.module = THIS_MODULE;
45 	nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
46 	if (!nvdimm_bus)
47 		goto err;
48 	platform_set_drvdata(pdev, nvdimm_bus);
49 
50 	rc = walk_iomem_res_desc(IORES_DESC_PERSISTENT_MEMORY_LEGACY,
51 			IORESOURCE_MEM, 0, -1, nvdimm_bus, e820_register_one);
52 	if (rc)
53 		goto err;
54 	return 0;
55 err:
56 	nvdimm_bus_unregister(nvdimm_bus);
57 	dev_err(dev, "failed to register legacy persistent memory ranges\n");
58 	return rc;
59 }
60 
61 static struct platform_driver e820_pmem_driver = {
62 	.probe = e820_pmem_probe,
63 	.remove = e820_pmem_remove,
64 	.driver = {
65 		.name = "e820_pmem",
66 	},
67 };
68 
69 module_platform_driver(e820_pmem_driver);
70 
71 MODULE_ALIAS("platform:e820_pmem*");
72 MODULE_LICENSE("GPL v2");
73 MODULE_AUTHOR("Intel Corporation");
74