Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015, Christoph Hellwig. |
| 4 | * Copyright (c) 2015, Intel Corporation. |
| 5 | */ |
| 6 | #include <linux/platform_device.h> |
Dan Williams | f7256dc | 2015-11-11 16:46:33 -0800 | [diff] [blame] | 7 | #include <linux/memory_hotplug.h> |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 8 | #include <linux/libnvdimm.h> |
| 9 | #include <linux/module.h> |
| 10 | |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 11 | static 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 Williams | f7256dc | 2015-11-11 16:46:33 -0800 | [diff] [blame] | 19 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 20 | static int e820_range_to_nid(resource_size_t addr) |
| 21 | { |
| 22 | return memory_add_physaddr_to_nid(addr); |
| 23 | } |
| 24 | #else |
| 25 | static int e820_range_to_nid(resource_size_t addr) |
| 26 | { |
| 27 | return NUMA_NO_NODE; |
| 28 | } |
| 29 | #endif |
| 30 | |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 31 | static 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 Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 38 | ndr_desc.numa_node = e820_range_to_nid(res->start); |
Dan Williams | 8fc5c73 | 2018-11-09 12:43:07 -0800 | [diff] [blame] | 39 | ndr_desc.target_node = ndr_desc.numa_node; |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 40 | 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 Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 46 | static 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 Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 51 | int rc = -ENXIO; |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 52 | |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 53 | nd_desc.provider_name = "e820"; |
Dan Williams | bc9775d | 2016-07-21 20:03:19 -0700 | [diff] [blame] | 54 | nd_desc.module = THIS_MODULE; |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 55 | nvdimm_bus = nvdimm_bus_register(dev, &nd_desc); |
| 56 | if (!nvdimm_bus) |
| 57 | goto err; |
| 58 | platform_set_drvdata(pdev, nvdimm_bus); |
| 59 | |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 60 | 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 Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 64 | return 0; |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 65 | err: |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 66 | nvdimm_bus_unregister(nvdimm_bus); |
| 67 | dev_err(dev, "failed to register legacy persistent memory ranges\n"); |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 68 | return rc; |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 69 | } |
| 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 | |
Johannes Thumshirn | 3a71b3c | 2016-12-05 09:23:20 +0100 | [diff] [blame] | 79 | module_platform_driver(e820_pmem_driver); |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 80 | |
| 81 | MODULE_ALIAS("platform:e820_pmem*"); |
| 82 | MODULE_LICENSE("GPL v2"); |
| 83 | MODULE_AUTHOR("Intel Corporation"); |