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 
e820_pmem_remove(struct platform_device * pdev)12 static void 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 }
18 
e820_register_one(struct resource * res,void * data)19 static int e820_register_one(struct resource *res, void *data)
20 {
21 	struct nd_region_desc ndr_desc;
22 	struct nvdimm_bus *nvdimm_bus = data;
23 	int nid = phys_to_target_node(res->start);
24 
25 	memset(&ndr_desc, 0, sizeof(ndr_desc));
26 	ndr_desc.res = res;
27 	ndr_desc.numa_node = numa_map_to_online_node(nid);
28 	ndr_desc.target_node = nid;
29 	set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
30 	if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
31 		return -ENXIO;
32 	return 0;
33 }
34 
e820_pmem_probe(struct platform_device * pdev)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 	int rc = -ENXIO;
41 
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 
49 	rc = walk_iomem_res_desc(IORES_DESC_PERSISTENT_MEMORY_LEGACY,
50 			IORESOURCE_MEM, 0, -1, nvdimm_bus, e820_register_one);
51 	if (rc)
52 		goto err;
53 	return 0;
54 err:
55 	nvdimm_bus_unregister(nvdimm_bus);
56 	dev_err(dev, "failed to register legacy persistent memory ranges\n");
57 	return rc;
58 }
59 
60 static struct platform_driver e820_pmem_driver = {
61 	.probe = e820_pmem_probe,
62 	.remove_new = e820_pmem_remove,
63 	.driver = {
64 		.name = "e820_pmem",
65 	},
66 };
67 
68 module_platform_driver(e820_pmem_driver);
69 
70 MODULE_ALIAS("platform:e820_pmem*");
71 MODULE_DESCRIPTION("NVDIMM support for e820 type-12 memory");
72 MODULE_LICENSE("GPL v2");
73 MODULE_AUTHOR("Intel Corporation");
74