1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/platform_device.h>
3 #include <linux/memregion.h>
4 #include <linux/module.h>
9 module_param_named(disable, nohmem, bool, 0444);
11 void hmem_register_device(int target_nid, struct resource *r)
13 /* define a clean / non-busy resource for the platform device */
14 struct resource res = {
17 .flags = IORESOURCE_MEM,
18 .desc = IORES_DESC_SOFT_RESERVED,
20 struct platform_device *pdev;
21 struct memregion_info info;
27 rc = region_intersects(res.start, resource_size(&res), IORESOURCE_MEM,
28 IORES_DESC_SOFT_RESERVED);
29 if (rc != REGION_INTERSECTS)
32 id = memregion_alloc(GFP_KERNEL);
34 pr_err("memregion allocation failure for %pr\n", &res);
38 pdev = platform_device_alloc("hmem", id);
40 pr_err("hmem device allocation failure for %pr\n", &res);
44 pdev->dev.numa_node = numa_map_to_online_node(target_nid);
45 info = (struct memregion_info) {
46 .target_node = target_nid,
48 rc = platform_device_add_data(pdev, &info, sizeof(info));
50 pr_err("hmem memregion_info allocation failure for %pr\n", &res);
54 rc = platform_device_add_resources(pdev, &res, 1);
56 pr_err("hmem resource allocation failure for %pr\n", &res);
60 rc = platform_device_add(pdev);
62 dev_err(&pdev->dev, "device add failed for %pr\n", &res);
69 put_device(&pdev->dev);
74 static __init int hmem_register_one(struct resource *res, void *data)
77 * If the resource is not a top-level resource it was already
78 * assigned to a device by the HMAT parsing.
80 if (res->parent != &iomem_resource) {
81 pr_info("HMEM: skip %pr, already claimed\n", res);
85 hmem_register_device(phys_to_target_node(res->start), res);
90 static __init int hmem_init(void)
92 walk_iomem_res_desc(IORES_DESC_SOFT_RESERVED,
93 IORESOURCE_MEM, 0, -1, NULL, hmem_register_one);
98 * As this is a fallback for address ranges unclaimed by the ACPI HMAT
99 * parsing it must be at an initcall level greater than hmat_init().
101 late_initcall(hmem_init);