pmem.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. 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 nd_pfn_sb *pfn_sb;
  56. struct dax_pmem *dax_pmem;
  57. struct nd_region *nd_region;
  58. struct nd_namespace_io *nsio;
  59. struct dax_region *dax_region;
  60. struct nd_namespace_common *ndns;
  61. struct nd_dax *nd_dax = to_nd_dax(dev);
  62. struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
  63. struct vmem_altmap __altmap, *altmap = NULL;
  64. ndns = nvdimm_namespace_common_probe(dev);
  65. if (IS_ERR(ndns))
  66. return PTR_ERR(ndns);
  67. nsio = to_nd_namespace_io(&ndns->dev);
  68. /* parse the 'pfn' info block via ->rw_bytes */
  69. devm_nsio_enable(dev, nsio);
  70. altmap = nvdimm_setup_pfn(nd_pfn, &res, &__altmap);
  71. if (IS_ERR(altmap))
  72. return PTR_ERR(altmap);
  73. devm_nsio_disable(dev, nsio);
  74. pfn_sb = nd_pfn->pfn_sb;
  75. if (!devm_request_mem_region(dev, nsio->res.start,
  76. resource_size(&nsio->res), dev_name(dev))) {
  77. dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
  78. return -EBUSY;
  79. }
  80. dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
  81. if (!dax_pmem)
  82. return -ENOMEM;
  83. dax_pmem->dev = dev;
  84. init_completion(&dax_pmem->cmp);
  85. rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
  86. GFP_KERNEL);
  87. if (rc)
  88. return rc;
  89. rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
  90. &dax_pmem->ref);
  91. if (rc)
  92. return rc;
  93. addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
  94. if (IS_ERR(addr))
  95. return PTR_ERR(addr);
  96. rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
  97. &dax_pmem->ref);
  98. if (rc)
  99. return rc;
  100. /* adjust the dax_region resource to the start of data */
  101. res.start += le64_to_cpu(pfn_sb->dataoff);
  102. nd_region = to_nd_region(dev->parent);
  103. dax_region = alloc_dax_region(dev, nd_region->id, &res,
  104. le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
  105. if (!dax_region)
  106. return -ENOMEM;
  107. /* TODO: support for subdividing a dax region... */
  108. rc = devm_create_dax_dev(dax_region, &res, 1);
  109. /* child dax_dev instances now own the lifetime of the dax_region */
  110. dax_region_put(dax_region);
  111. return rc;
  112. }
  113. static struct nd_device_driver dax_pmem_driver = {
  114. .probe = dax_pmem_probe,
  115. .drv = {
  116. .name = "dax_pmem",
  117. },
  118. .type = ND_DRIVER_DAX_PMEM,
  119. };
  120. static int __init dax_pmem_init(void)
  121. {
  122. return nd_driver_register(&dax_pmem_driver);
  123. }
  124. module_init(dax_pmem_init);
  125. static void __exit dax_pmem_exit(void)
  126. {
  127. driver_unregister(&dax_pmem_driver.drv);
  128. }
  129. module_exit(dax_pmem_exit);
  130. MODULE_LICENSE("GPL v2");
  131. MODULE_AUTHOR("Intel Corporation");
  132. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);