• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #define pr_fmt(fmt) "of_pmem: " fmt
4 
5 #include <linux/of_platform.h>
6 #include <linux/of_address.h>
7 #include <linux/libnvdimm.h>
8 #include <linux/module.h>
9 #include <linux/ioport.h>
10 #include <linux/slab.h>
11 
12 static const struct attribute_group *region_attr_groups[] = {
13 	&nd_region_attribute_group,
14 	&nd_device_attribute_group,
15 	NULL,
16 };
17 
18 static const struct attribute_group *bus_attr_groups[] = {
19 	&nvdimm_bus_attribute_group,
20 	NULL,
21 };
22 
23 struct of_pmem_private {
24 	struct nvdimm_bus_descriptor bus_desc;
25 	struct nvdimm_bus *bus;
26 };
27 
of_pmem_region_probe(struct platform_device * pdev)28 static int of_pmem_region_probe(struct platform_device *pdev)
29 {
30 	struct of_pmem_private *priv;
31 	struct device_node *np;
32 	struct nvdimm_bus *bus;
33 	bool is_volatile;
34 	int i;
35 
36 	np = dev_of_node(&pdev->dev);
37 	if (!np)
38 		return -ENXIO;
39 
40 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
41 	if (!priv)
42 		return -ENOMEM;
43 
44 	priv->bus_desc.attr_groups = bus_attr_groups;
45 	priv->bus_desc.provider_name = devm_kstrdup(&pdev->dev, pdev->name,
46 							GFP_KERNEL);
47 	if (!priv->bus_desc.provider_name) {
48 		kfree(priv);
49 		return -ENOMEM;
50 	}
51 
52 	priv->bus_desc.module = THIS_MODULE;
53 	priv->bus_desc.of_node = np;
54 
55 	priv->bus = bus = nvdimm_bus_register(&pdev->dev, &priv->bus_desc);
56 	if (!bus) {
57 		kfree(priv);
58 		return -ENODEV;
59 	}
60 	platform_set_drvdata(pdev, priv);
61 
62 	is_volatile = !!of_find_property(np, "volatile", NULL);
63 	dev_dbg(&pdev->dev, "Registering %s regions from %pOF\n",
64 			is_volatile ? "volatile" : "non-volatile",  np);
65 
66 	for (i = 0; i < pdev->num_resources; i++) {
67 		struct nd_region_desc ndr_desc;
68 		struct nd_region *region;
69 
70 		/*
71 		 * NB: libnvdimm copies the data from ndr_desc into it's own
72 		 * structures so passing a stack pointer is fine.
73 		 */
74 		memset(&ndr_desc, 0, sizeof(ndr_desc));
75 		ndr_desc.attr_groups = region_attr_groups;
76 		ndr_desc.numa_node = dev_to_node(&pdev->dev);
77 		ndr_desc.target_node = ndr_desc.numa_node;
78 		ndr_desc.res = &pdev->resource[i];
79 		ndr_desc.of_node = np;
80 		set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
81 
82 		if (is_volatile)
83 			region = nvdimm_volatile_region_create(bus, &ndr_desc);
84 		else
85 			region = nvdimm_pmem_region_create(bus, &ndr_desc);
86 
87 		if (!region)
88 			dev_warn(&pdev->dev, "Unable to register region %pR from %pOF\n",
89 					ndr_desc.res, np);
90 		else
91 			dev_dbg(&pdev->dev, "Registered region %pR from %pOF\n",
92 					ndr_desc.res, np);
93 	}
94 
95 	return 0;
96 }
97 
of_pmem_region_remove(struct platform_device * pdev)98 static int of_pmem_region_remove(struct platform_device *pdev)
99 {
100 	struct of_pmem_private *priv = platform_get_drvdata(pdev);
101 
102 	nvdimm_bus_unregister(priv->bus);
103 	kfree(priv);
104 
105 	return 0;
106 }
107 
108 static const struct of_device_id of_pmem_region_match[] = {
109 	{ .compatible = "pmem-region" },
110 	{ },
111 };
112 
113 static struct platform_driver of_pmem_region_driver = {
114 	.probe = of_pmem_region_probe,
115 	.remove = of_pmem_region_remove,
116 	.driver = {
117 		.name = "of_pmem",
118 		.of_match_table = of_pmem_region_match,
119 	},
120 };
121 
122 module_platform_driver(of_pmem_region_driver);
123 MODULE_DEVICE_TABLE(of, of_pmem_region_match);
124 MODULE_LICENSE("GPL");
125 MODULE_AUTHOR("IBM Corporation");
126