xref: /OK3568_Linux_fs/kernel/drivers/dax/hmem/device.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/platform_device.h>
3*4882a593Smuzhiyun #include <linux/memregion.h>
4*4882a593Smuzhiyun #include <linux/module.h>
5*4882a593Smuzhiyun #include <linux/dax.h>
6*4882a593Smuzhiyun #include <linux/mm.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun static bool nohmem;
9*4882a593Smuzhiyun module_param_named(disable, nohmem, bool, 0444);
10*4882a593Smuzhiyun 
hmem_register_device(int target_nid,struct resource * r)11*4882a593Smuzhiyun void hmem_register_device(int target_nid, struct resource *r)
12*4882a593Smuzhiyun {
13*4882a593Smuzhiyun 	/* define a clean / non-busy resource for the platform device */
14*4882a593Smuzhiyun 	struct resource res = {
15*4882a593Smuzhiyun 		.start = r->start,
16*4882a593Smuzhiyun 		.end = r->end,
17*4882a593Smuzhiyun 		.flags = IORESOURCE_MEM,
18*4882a593Smuzhiyun 		.desc = IORES_DESC_SOFT_RESERVED,
19*4882a593Smuzhiyun 	};
20*4882a593Smuzhiyun 	struct platform_device *pdev;
21*4882a593Smuzhiyun 	struct memregion_info info;
22*4882a593Smuzhiyun 	int rc, id;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	if (nohmem)
25*4882a593Smuzhiyun 		return;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	rc = region_intersects(res.start, resource_size(&res), IORESOURCE_MEM,
28*4882a593Smuzhiyun 			IORES_DESC_SOFT_RESERVED);
29*4882a593Smuzhiyun 	if (rc != REGION_INTERSECTS)
30*4882a593Smuzhiyun 		return;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	id = memregion_alloc(GFP_KERNEL);
33*4882a593Smuzhiyun 	if (id < 0) {
34*4882a593Smuzhiyun 		pr_err("memregion allocation failure for %pr\n", &res);
35*4882a593Smuzhiyun 		return;
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	pdev = platform_device_alloc("hmem", id);
39*4882a593Smuzhiyun 	if (!pdev) {
40*4882a593Smuzhiyun 		pr_err("hmem device allocation failure for %pr\n", &res);
41*4882a593Smuzhiyun 		goto out_pdev;
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	pdev->dev.numa_node = numa_map_to_online_node(target_nid);
45*4882a593Smuzhiyun 	info = (struct memregion_info) {
46*4882a593Smuzhiyun 		.target_node = target_nid,
47*4882a593Smuzhiyun 	};
48*4882a593Smuzhiyun 	rc = platform_device_add_data(pdev, &info, sizeof(info));
49*4882a593Smuzhiyun 	if (rc < 0) {
50*4882a593Smuzhiyun 		pr_err("hmem memregion_info allocation failure for %pr\n", &res);
51*4882a593Smuzhiyun 		goto out_pdev;
52*4882a593Smuzhiyun 	}
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	rc = platform_device_add_resources(pdev, &res, 1);
55*4882a593Smuzhiyun 	if (rc < 0) {
56*4882a593Smuzhiyun 		pr_err("hmem resource allocation failure for %pr\n", &res);
57*4882a593Smuzhiyun 		goto out_resource;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	rc = platform_device_add(pdev);
61*4882a593Smuzhiyun 	if (rc < 0) {
62*4882a593Smuzhiyun 		dev_err(&pdev->dev, "device add failed for %pr\n", &res);
63*4882a593Smuzhiyun 		goto out_resource;
64*4882a593Smuzhiyun 	}
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun out_resource:
69*4882a593Smuzhiyun 	put_device(&pdev->dev);
70*4882a593Smuzhiyun out_pdev:
71*4882a593Smuzhiyun 	memregion_free(id);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
hmem_register_one(struct resource * res,void * data)74*4882a593Smuzhiyun static __init int hmem_register_one(struct resource *res, void *data)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	/*
77*4882a593Smuzhiyun 	 * If the resource is not a top-level resource it was already
78*4882a593Smuzhiyun 	 * assigned to a device by the HMAT parsing.
79*4882a593Smuzhiyun 	 */
80*4882a593Smuzhiyun 	if (res->parent != &iomem_resource) {
81*4882a593Smuzhiyun 		pr_info("HMEM: skip %pr, already claimed\n", res);
82*4882a593Smuzhiyun 		return 0;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	hmem_register_device(phys_to_target_node(res->start), res);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	return 0;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
hmem_init(void)90*4882a593Smuzhiyun static __init int hmem_init(void)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	walk_iomem_res_desc(IORES_DESC_SOFT_RESERVED,
93*4882a593Smuzhiyun 			IORESOURCE_MEM, 0, -1, NULL, hmem_register_one);
94*4882a593Smuzhiyun 	return 0;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * As this is a fallback for address ranges unclaimed by the ACPI HMAT
99*4882a593Smuzhiyun  * parsing it must be at an initcall level greater than hmat_init().
100*4882a593Smuzhiyun  */
101*4882a593Smuzhiyun late_initcall(hmem_init);
102