Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/platform_device.h>
0003 #include <linux/memregion.h>
0004 #include <linux/module.h>
0005 #include <linux/dax.h>
0006 #include <linux/mm.h>
0007 
0008 static bool nohmem;
0009 module_param_named(disable, nohmem, bool, 0444);
0010 
0011 void hmem_register_device(int target_nid, struct resource *r)
0012 {
0013     /* define a clean / non-busy resource for the platform device */
0014     struct resource res = {
0015         .start = r->start,
0016         .end = r->end,
0017         .flags = IORESOURCE_MEM,
0018         .desc = IORES_DESC_SOFT_RESERVED,
0019     };
0020     struct platform_device *pdev;
0021     struct memregion_info info;
0022     int rc, id;
0023 
0024     if (nohmem)
0025         return;
0026 
0027     rc = region_intersects(res.start, resource_size(&res), IORESOURCE_MEM,
0028             IORES_DESC_SOFT_RESERVED);
0029     if (rc != REGION_INTERSECTS)
0030         return;
0031 
0032     id = memregion_alloc(GFP_KERNEL);
0033     if (id < 0) {
0034         pr_err("memregion allocation failure for %pr\n", &res);
0035         return;
0036     }
0037 
0038     pdev = platform_device_alloc("hmem", id);
0039     if (!pdev) {
0040         pr_err("hmem device allocation failure for %pr\n", &res);
0041         goto out_pdev;
0042     }
0043 
0044     pdev->dev.numa_node = numa_map_to_online_node(target_nid);
0045     info = (struct memregion_info) {
0046         .target_node = target_nid,
0047     };
0048     rc = platform_device_add_data(pdev, &info, sizeof(info));
0049     if (rc < 0) {
0050         pr_err("hmem memregion_info allocation failure for %pr\n", &res);
0051         goto out_pdev;
0052     }
0053 
0054     rc = platform_device_add_resources(pdev, &res, 1);
0055     if (rc < 0) {
0056         pr_err("hmem resource allocation failure for %pr\n", &res);
0057         goto out_resource;
0058     }
0059 
0060     rc = platform_device_add(pdev);
0061     if (rc < 0) {
0062         dev_err(&pdev->dev, "device add failed for %pr\n", &res);
0063         goto out_resource;
0064     }
0065 
0066     return;
0067 
0068 out_resource:
0069     put_device(&pdev->dev);
0070 out_pdev:
0071     memregion_free(id);
0072 }
0073 
0074 static __init int hmem_register_one(struct resource *res, void *data)
0075 {
0076     /*
0077      * If the resource is not a top-level resource it was already
0078      * assigned to a device by the HMAT parsing.
0079      */
0080     if (res->parent != &iomem_resource) {
0081         pr_info("HMEM: skip %pr, already claimed\n", res);
0082         return 0;
0083     }
0084 
0085     hmem_register_device(phys_to_target_node(res->start), res);
0086 
0087     return 0;
0088 }
0089 
0090 static __init int hmem_init(void)
0091 {
0092     walk_iomem_res_desc(IORES_DESC_SOFT_RESERVED,
0093             IORESOURCE_MEM, 0, -1, NULL, hmem_register_one);
0094     return 0;
0095 }
0096 
0097 /*
0098  * As this is a fallback for address ranges unclaimed by the ACPI HMAT
0099  * parsing it must be at an initcall level greater than hmat_init().
0100  */
0101 late_initcall(hmem_init);