ACPI: HMAT: refactor hmat_register_target_device to hmem_register_device
[linux-2.6-microblaze.git] / drivers / dax / hmem / device.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/platform_device.h>
3 #include <linux/memregion.h>
4 #include <linux/module.h>
5 #include <linux/dax.h>
6 #include <linux/mm.h>
7
8 void hmem_register_device(int target_nid, struct resource *r)
9 {
10         /* define a clean / non-busy resource for the platform device */
11         struct resource res = {
12                 .start = r->start,
13                 .end = r->end,
14                 .flags = IORESOURCE_MEM,
15         };
16         struct platform_device *pdev;
17         struct memregion_info info;
18         int rc, id;
19
20         rc = region_intersects(res.start, resource_size(&res), IORESOURCE_MEM,
21                         IORES_DESC_SOFT_RESERVED);
22         if (rc != REGION_INTERSECTS)
23                 return;
24
25         id = memregion_alloc(GFP_KERNEL);
26         if (id < 0) {
27                 pr_err("memregion allocation failure for %pr\n", &res);
28                 return;
29         }
30
31         pdev = platform_device_alloc("hmem", id);
32         if (!pdev) {
33                 pr_err("hmem device allocation failure for %pr\n", &res);
34                 goto out_pdev;
35         }
36
37         pdev->dev.numa_node = numa_map_to_online_node(target_nid);
38         info = (struct memregion_info) {
39                 .target_node = target_nid,
40         };
41         rc = platform_device_add_data(pdev, &info, sizeof(info));
42         if (rc < 0) {
43                 pr_err("hmem memregion_info allocation failure for %pr\n", &res);
44                 goto out_pdev;
45         }
46
47         rc = platform_device_add_resources(pdev, &res, 1);
48         if (rc < 0) {
49                 pr_err("hmem resource allocation failure for %pr\n", &res);
50                 goto out_resource;
51         }
52
53         rc = platform_device_add(pdev);
54         if (rc < 0) {
55                 dev_err(&pdev->dev, "device add failed for %pr\n", &res);
56                 goto out_resource;
57         }
58
59         return;
60
61 out_resource:
62         put_device(&pdev->dev);
63 out_pdev:
64         memregion_free(id);
65 }