of_pmem.c 2.8 KB

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