pmem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright(c) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/percpu-refcount.h>
  14. #include <linux/memremap.h>
  15. #include <linux/module.h>
  16. #include <linux/pfn_t.h>
  17. #include "../nvdimm/pfn.h"
  18. #include "../nvdimm/nd.h"
  19. #include "dax.h"
  20. struct dax_pmem {
  21. struct device *dev;
  22. struct percpu_ref ref;
  23. struct completion cmp;
  24. };
  25. static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
  26. {
  27. return container_of(ref, struct dax_pmem, ref);
  28. }
  29. static void dax_pmem_percpu_release(struct percpu_ref *ref)
  30. {
  31. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  32. dev_dbg(dax_pmem->dev, "%s\n", __func__);
  33. complete(&dax_pmem->cmp);
  34. }
  35. static void dax_pmem_percpu_exit(void *data)
  36. {
  37. struct percpu_ref *ref = data;
  38. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  39. dev_dbg(dax_pmem->dev, "%s\n", __func__);
  40. percpu_ref_exit(ref);
  41. wait_for_completion(&dax_pmem->cmp);
  42. }
  43. static void dax_pmem_percpu_kill(void *data)
  44. {
  45. struct percpu_ref *ref = data;
  46. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  47. dev_dbg(dax_pmem->dev, "%s\n", __func__);
  48. percpu_ref_kill(ref);
  49. }
  50. static int dax_pmem_probe(struct device *dev)
  51. {
  52. int rc;
  53. void *addr;
  54. struct resource res;
  55. struct dax_dev *dax_dev;
  56. struct nd_pfn_sb *pfn_sb;
  57. struct dax_pmem *dax_pmem;
  58. struct nd_region *nd_region;
  59. struct nd_namespace_io *nsio;
  60. struct dax_region *dax_region;
  61. struct nd_namespace_common *ndns;
  62. struct nd_dax *nd_dax = to_nd_dax(dev);
  63. struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
  64. struct vmem_altmap __altmap, *altmap = NULL;
  65. ndns = nvdimm_namespace_common_probe(dev);
  66. if (IS_ERR(ndns))
  67. return PTR_ERR(ndns);
  68. nsio = to_nd_namespace_io(&ndns->dev);
  69. /* parse the 'pfn' info block via ->rw_bytes */
  70. devm_nsio_enable(dev, nsio);
  71. altmap = nvdimm_setup_pfn(nd_pfn, &res, &__altmap);
  72. if (IS_ERR(altmap))
  73. return PTR_ERR(altmap);
  74. devm_nsio_disable(dev, nsio);
  75. pfn_sb = nd_pfn->pfn_sb;
  76. if (!devm_request_mem_region(dev, nsio->res.start,
  77. resource_size(&nsio->res), dev_name(dev))) {
  78. dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
  79. return -EBUSY;
  80. }
  81. dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
  82. if (!dax_pmem)
  83. return -ENOMEM;
  84. dax_pmem->dev = dev;
  85. init_completion(&dax_pmem->cmp);
  86. rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
  87. GFP_KERNEL);
  88. if (rc)
  89. return rc;
  90. rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
  91. &dax_pmem->ref);
  92. if (rc)
  93. return rc;
  94. addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
  95. if (IS_ERR(addr))
  96. return PTR_ERR(addr);
  97. rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
  98. &dax_pmem->ref);
  99. if (rc)
  100. return rc;
  101. /* adjust the dax_region resource to the start of data */
  102. res.start += le64_to_cpu(pfn_sb->dataoff);
  103. nd_region = to_nd_region(dev->parent);
  104. dax_region = alloc_dax_region(dev, nd_region->id, &res,
  105. le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
  106. if (!dax_region)
  107. return -ENOMEM;
  108. /* TODO: support for subdividing a dax region... */
  109. dax_dev = devm_create_dax_dev(dax_region, &res, 1);
  110. /* child dax_dev instances now own the lifetime of the dax_region */
  111. dax_region_put(dax_region);
  112. return PTR_ERR_OR_ZERO(dax_dev);
  113. }
  114. static struct nd_device_driver dax_pmem_driver = {
  115. .probe = dax_pmem_probe,
  116. .drv = {
  117. .name = "dax_pmem",
  118. },
  119. .type = ND_DRIVER_DAX_PMEM,
  120. };
  121. static int __init dax_pmem_init(void)
  122. {
  123. return nd_driver_register(&dax_pmem_driver);
  124. }
  125. module_init(dax_pmem_init);
  126. static void __exit dax_pmem_exit(void)
  127. {
  128. driver_unregister(&dax_pmem_driver.drv);
  129. }
  130. module_exit(dax_pmem_exit);
  131. MODULE_LICENSE("GPL v2");
  132. MODULE_AUTHOR("Intel Corporation");
  133. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);