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 | |
| 11 | static const struct attribute_group *e820_pmem_attribute_groups[] = { |
| 12 | &nvdimm_bus_attribute_group, |
| 13 | NULL, |
| 14 | }; |
| 15 | |
| 16 | static const struct attribute_group *e820_pmem_region_attribute_groups[] = { |
| 17 | &nd_region_attribute_group, |
| 18 | &nd_device_attribute_group, |
| 19 | NULL, |
| 20 | }; |
| 21 | |
| 22 | static 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 Williams | f7256dc | 2015-11-11 16:46:33 -0800 | [diff] [blame] | 30 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 31 | static int e820_range_to_nid(resource_size_t addr) |
| 32 | { |
| 33 | return memory_add_physaddr_to_nid(addr); |
| 34 | } |
| 35 | #else |
| 36 | static int e820_range_to_nid(resource_size_t addr) |
| 37 | { |
| 38 | return NUMA_NO_NODE; |
| 39 | } |
| 40 | #endif |
| 41 | |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 42 | static 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 Williams | 8fc5c73 | 2018-11-09 12:43:07 -0800 | [diff] [blame] | 51 | ndr_desc.target_node = ndr_desc.numa_node; |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 52 | 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 Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 58 | static 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 Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 63 | int rc = -ENXIO; |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 64 | |
| 65 | nd_desc.attr_groups = e820_pmem_attribute_groups; |
| 66 | nd_desc.provider_name = "e820"; |
Dan Williams | bc9775d | 2016-07-21 20:03:19 -0700 | [diff] [blame] | 67 | nd_desc.module = THIS_MODULE; |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 68 | nvdimm_bus = nvdimm_bus_register(dev, &nd_desc); |
| 69 | if (!nvdimm_bus) |
| 70 | goto err; |
| 71 | platform_set_drvdata(pdev, nvdimm_bus); |
| 72 | |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 73 | 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 Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 77 | return 0; |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 78 | err: |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 79 | nvdimm_bus_unregister(nvdimm_bus); |
| 80 | dev_err(dev, "failed to register legacy persistent memory ranges\n"); |
Dan Williams | d76401a | 2018-06-02 11:43:39 -0700 | [diff] [blame] | 81 | return rc; |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static 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 Thumshirn | 3a71b3c | 2016-12-05 09:23:20 +0100 | [diff] [blame] | 92 | module_platform_driver(e820_pmem_driver); |
Dan Williams | 7a67832 | 2015-08-19 00:34:34 -0400 | [diff] [blame] | 93 | |
| 94 | MODULE_ALIAS("platform:e820_pmem*"); |
| 95 | MODULE_LICENSE("GPL v2"); |
| 96 | MODULE_AUTHOR("Intel Corporation"); |