0001
0002
0003 #define pr_fmt(fmt) "of_pmem: " fmt
0004
0005 #include <linux/of_platform.h>
0006 #include <linux/of_address.h>
0007 #include <linux/libnvdimm.h>
0008 #include <linux/module.h>
0009 #include <linux/ioport.h>
0010 #include <linux/slab.h>
0011
0012 struct of_pmem_private {
0013 struct nvdimm_bus_descriptor bus_desc;
0014 struct nvdimm_bus *bus;
0015 };
0016
0017 static int of_pmem_region_probe(struct platform_device *pdev)
0018 {
0019 struct of_pmem_private *priv;
0020 struct device_node *np;
0021 struct nvdimm_bus *bus;
0022 bool is_volatile;
0023 int i;
0024
0025 np = dev_of_node(&pdev->dev);
0026 if (!np)
0027 return -ENXIO;
0028
0029 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0030 if (!priv)
0031 return -ENOMEM;
0032
0033 priv->bus_desc.provider_name = kstrdup(pdev->name, GFP_KERNEL);
0034 priv->bus_desc.module = THIS_MODULE;
0035 priv->bus_desc.of_node = np;
0036
0037 priv->bus = bus = nvdimm_bus_register(&pdev->dev, &priv->bus_desc);
0038 if (!bus) {
0039 kfree(priv);
0040 return -ENODEV;
0041 }
0042 platform_set_drvdata(pdev, priv);
0043
0044 is_volatile = !!of_find_property(np, "volatile", NULL);
0045 dev_dbg(&pdev->dev, "Registering %s regions from %pOF\n",
0046 is_volatile ? "volatile" : "non-volatile", np);
0047
0048 for (i = 0; i < pdev->num_resources; i++) {
0049 struct nd_region_desc ndr_desc;
0050 struct nd_region *region;
0051
0052
0053
0054
0055
0056 memset(&ndr_desc, 0, sizeof(ndr_desc));
0057 ndr_desc.numa_node = dev_to_node(&pdev->dev);
0058 ndr_desc.target_node = ndr_desc.numa_node;
0059 ndr_desc.res = &pdev->resource[i];
0060 ndr_desc.of_node = np;
0061 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
0062
0063 if (is_volatile)
0064 region = nvdimm_volatile_region_create(bus, &ndr_desc);
0065 else {
0066 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);
0067 region = nvdimm_pmem_region_create(bus, &ndr_desc);
0068 }
0069
0070 if (!region)
0071 dev_warn(&pdev->dev, "Unable to register region %pR from %pOF\n",
0072 ndr_desc.res, np);
0073 else
0074 dev_dbg(&pdev->dev, "Registered region %pR from %pOF\n",
0075 ndr_desc.res, np);
0076 }
0077
0078 return 0;
0079 }
0080
0081 static int of_pmem_region_remove(struct platform_device *pdev)
0082 {
0083 struct of_pmem_private *priv = platform_get_drvdata(pdev);
0084
0085 nvdimm_bus_unregister(priv->bus);
0086 kfree(priv);
0087
0088 return 0;
0089 }
0090
0091 static const struct of_device_id of_pmem_region_match[] = {
0092 { .compatible = "pmem-region" },
0093 { .compatible = "pmem-region-v2" },
0094 { },
0095 };
0096
0097 static struct platform_driver of_pmem_region_driver = {
0098 .probe = of_pmem_region_probe,
0099 .remove = of_pmem_region_remove,
0100 .driver = {
0101 .name = "of_pmem",
0102 .of_match_table = of_pmem_region_match,
0103 },
0104 };
0105
0106 module_platform_driver(of_pmem_region_driver);
0107 MODULE_DEVICE_TABLE(of, of_pmem_region_match);
0108 MODULE_LICENSE("GPL");
0109 MODULE_AUTHOR("IBM Corporation");