pmem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Persistent Memory Driver
  3. *
  4. * Copyright (c) 2014-2015, Intel Corporation.
  5. * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
  6. * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #include <asm/cacheflush.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/hdreg.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/module.h>
  23. #include <linux/memory_hotplug.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/slab.h>
  27. #include <linux/pmem.h>
  28. #include <linux/nd.h>
  29. #include "pfn.h"
  30. #include "nd.h"
  31. struct pmem_device {
  32. struct request_queue *pmem_queue;
  33. struct gendisk *pmem_disk;
  34. struct nd_namespace_common *ndns;
  35. /* One contiguous memory region per device */
  36. phys_addr_t phys_addr;
  37. /* when non-zero this device is hosting a 'pfn' instance */
  38. phys_addr_t data_offset;
  39. void __pmem *virt_addr;
  40. size_t size;
  41. };
  42. static int pmem_major;
  43. static void pmem_do_bvec(struct pmem_device *pmem, struct page *page,
  44. unsigned int len, unsigned int off, int rw,
  45. sector_t sector)
  46. {
  47. void *mem = kmap_atomic(page);
  48. phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
  49. void __pmem *pmem_addr = pmem->virt_addr + pmem_off;
  50. if (rw == READ) {
  51. memcpy_from_pmem(mem + off, pmem_addr, len);
  52. flush_dcache_page(page);
  53. } else {
  54. flush_dcache_page(page);
  55. memcpy_to_pmem(pmem_addr, mem + off, len);
  56. }
  57. kunmap_atomic(mem);
  58. }
  59. static void pmem_make_request(struct request_queue *q, struct bio *bio)
  60. {
  61. bool do_acct;
  62. unsigned long start;
  63. struct bio_vec bvec;
  64. struct bvec_iter iter;
  65. struct block_device *bdev = bio->bi_bdev;
  66. struct pmem_device *pmem = bdev->bd_disk->private_data;
  67. do_acct = nd_iostat_start(bio, &start);
  68. bio_for_each_segment(bvec, bio, iter)
  69. pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset,
  70. bio_data_dir(bio), iter.bi_sector);
  71. if (do_acct)
  72. nd_iostat_end(bio, start);
  73. if (bio_data_dir(bio))
  74. wmb_pmem();
  75. bio_endio(bio);
  76. }
  77. static int pmem_rw_page(struct block_device *bdev, sector_t sector,
  78. struct page *page, int rw)
  79. {
  80. struct pmem_device *pmem = bdev->bd_disk->private_data;
  81. pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector);
  82. if (rw & WRITE)
  83. wmb_pmem();
  84. page_endio(page, rw & WRITE, 0);
  85. return 0;
  86. }
  87. static long pmem_direct_access(struct block_device *bdev, sector_t sector,
  88. void __pmem **kaddr, unsigned long *pfn)
  89. {
  90. struct pmem_device *pmem = bdev->bd_disk->private_data;
  91. resource_size_t offset = sector * 512 + pmem->data_offset;
  92. resource_size_t size;
  93. if (pmem->data_offset) {
  94. /*
  95. * Limit the direct_access() size to what is covered by
  96. * the memmap
  97. */
  98. size = (pmem->size - offset) & ~ND_PFN_MASK;
  99. } else
  100. size = pmem->size - offset;
  101. /* FIXME convert DAX to comprehend that this mapping has a lifetime */
  102. *kaddr = pmem->virt_addr + offset;
  103. *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT;
  104. return size;
  105. }
  106. static const struct block_device_operations pmem_fops = {
  107. .owner = THIS_MODULE,
  108. .rw_page = pmem_rw_page,
  109. .direct_access = pmem_direct_access,
  110. .revalidate_disk = nvdimm_revalidate_disk,
  111. };
  112. static struct pmem_device *pmem_alloc(struct device *dev,
  113. struct resource *res, int id)
  114. {
  115. struct pmem_device *pmem;
  116. pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
  117. if (!pmem)
  118. return ERR_PTR(-ENOMEM);
  119. pmem->phys_addr = res->start;
  120. pmem->size = resource_size(res);
  121. if (!arch_has_wmb_pmem())
  122. dev_warn(dev, "unable to guarantee persistence of writes\n");
  123. if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size,
  124. dev_name(dev))) {
  125. dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
  126. &pmem->phys_addr, pmem->size);
  127. return ERR_PTR(-EBUSY);
  128. }
  129. if (pmem_should_map_pages(dev)) {
  130. void *addr = devm_memremap_pages(dev, res);
  131. if (IS_ERR(addr))
  132. return addr;
  133. pmem->virt_addr = (void __pmem *) addr;
  134. } else {
  135. pmem->virt_addr = memremap_pmem(dev, pmem->phys_addr,
  136. pmem->size);
  137. if (!pmem->virt_addr)
  138. return ERR_PTR(-ENXIO);
  139. }
  140. return pmem;
  141. }
  142. static void pmem_detach_disk(struct pmem_device *pmem)
  143. {
  144. if (!pmem->pmem_disk)
  145. return;
  146. del_gendisk(pmem->pmem_disk);
  147. put_disk(pmem->pmem_disk);
  148. blk_cleanup_queue(pmem->pmem_queue);
  149. }
  150. static int pmem_attach_disk(struct device *dev,
  151. struct nd_namespace_common *ndns, struct pmem_device *pmem)
  152. {
  153. struct gendisk *disk;
  154. pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL);
  155. if (!pmem->pmem_queue)
  156. return -ENOMEM;
  157. blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
  158. blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE);
  159. blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
  160. blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
  161. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue);
  162. disk = alloc_disk(0);
  163. if (!disk) {
  164. blk_cleanup_queue(pmem->pmem_queue);
  165. return -ENOMEM;
  166. }
  167. disk->major = pmem_major;
  168. disk->first_minor = 0;
  169. disk->fops = &pmem_fops;
  170. disk->private_data = pmem;
  171. disk->queue = pmem->pmem_queue;
  172. disk->flags = GENHD_FL_EXT_DEVT;
  173. nvdimm_namespace_disk_name(ndns, disk->disk_name);
  174. disk->driverfs_dev = dev;
  175. set_capacity(disk, (pmem->size - pmem->data_offset) / 512);
  176. pmem->pmem_disk = disk;
  177. add_disk(disk);
  178. revalidate_disk(disk);
  179. return 0;
  180. }
  181. static int pmem_rw_bytes(struct nd_namespace_common *ndns,
  182. resource_size_t offset, void *buf, size_t size, int rw)
  183. {
  184. struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
  185. if (unlikely(offset + size > pmem->size)) {
  186. dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
  187. return -EFAULT;
  188. }
  189. if (rw == READ)
  190. memcpy_from_pmem(buf, pmem->virt_addr + offset, size);
  191. else {
  192. memcpy_to_pmem(pmem->virt_addr + offset, buf, size);
  193. wmb_pmem();
  194. }
  195. return 0;
  196. }
  197. static int nd_pfn_init(struct nd_pfn *nd_pfn)
  198. {
  199. struct nd_pfn_sb *pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
  200. struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev);
  201. struct nd_namespace_common *ndns = nd_pfn->ndns;
  202. struct nd_region *nd_region;
  203. unsigned long npfns;
  204. phys_addr_t offset;
  205. u64 checksum;
  206. int rc;
  207. if (!pfn_sb)
  208. return -ENOMEM;
  209. nd_pfn->pfn_sb = pfn_sb;
  210. rc = nd_pfn_validate(nd_pfn);
  211. if (rc == 0 || rc == -EBUSY)
  212. return rc;
  213. /* section alignment for simple hotplug */
  214. if (nvdimm_namespace_capacity(ndns) < ND_PFN_ALIGN
  215. || pmem->phys_addr & ND_PFN_MASK)
  216. return -ENODEV;
  217. nd_region = to_nd_region(nd_pfn->dev.parent);
  218. if (nd_region->ro) {
  219. dev_info(&nd_pfn->dev,
  220. "%s is read-only, unable to init metadata\n",
  221. dev_name(&nd_region->dev));
  222. goto err;
  223. }
  224. memset(pfn_sb, 0, sizeof(*pfn_sb));
  225. npfns = (pmem->size - SZ_8K) / SZ_4K;
  226. /*
  227. * Note, we use 64 here for the standard size of struct page,
  228. * debugging options may cause it to be larger in which case the
  229. * implementation will limit the pfns advertised through
  230. * ->direct_access() to those that are included in the memmap.
  231. */
  232. if (nd_pfn->mode == PFN_MODE_PMEM)
  233. offset = ALIGN(SZ_8K + 64 * npfns, PMD_SIZE);
  234. else if (nd_pfn->mode == PFN_MODE_RAM)
  235. offset = SZ_8K;
  236. else
  237. goto err;
  238. npfns = (pmem->size - offset) / SZ_4K;
  239. pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
  240. pfn_sb->dataoff = cpu_to_le64(offset);
  241. pfn_sb->npfns = cpu_to_le64(npfns);
  242. memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN);
  243. memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
  244. pfn_sb->version_major = cpu_to_le16(1);
  245. checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
  246. pfn_sb->checksum = cpu_to_le64(checksum);
  247. rc = nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
  248. if (rc)
  249. goto err;
  250. return 0;
  251. err:
  252. nd_pfn->pfn_sb = NULL;
  253. kfree(pfn_sb);
  254. return -ENXIO;
  255. }
  256. static int nvdimm_namespace_detach_pfn(struct nd_namespace_common *ndns)
  257. {
  258. struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
  259. struct pmem_device *pmem;
  260. /* free pmem disk */
  261. pmem = dev_get_drvdata(&nd_pfn->dev);
  262. pmem_detach_disk(pmem);
  263. /* release nd_pfn resources */
  264. kfree(nd_pfn->pfn_sb);
  265. nd_pfn->pfn_sb = NULL;
  266. return 0;
  267. }
  268. static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns)
  269. {
  270. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  271. struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
  272. struct device *dev = &nd_pfn->dev;
  273. struct vmem_altmap *altmap;
  274. struct nd_region *nd_region;
  275. struct nd_pfn_sb *pfn_sb;
  276. struct pmem_device *pmem;
  277. phys_addr_t offset;
  278. int rc;
  279. if (!nd_pfn->uuid || !nd_pfn->ndns)
  280. return -ENODEV;
  281. nd_region = to_nd_region(dev->parent);
  282. rc = nd_pfn_init(nd_pfn);
  283. if (rc)
  284. return rc;
  285. if (PAGE_SIZE != SZ_4K) {
  286. dev_err(dev, "only supported on systems with 4K PAGE_SIZE\n");
  287. return -ENXIO;
  288. }
  289. if (nsio->res.start & ND_PFN_MASK) {
  290. dev_err(dev, "%s not memory hotplug section aligned\n",
  291. dev_name(&ndns->dev));
  292. return -ENXIO;
  293. }
  294. pfn_sb = nd_pfn->pfn_sb;
  295. offset = le64_to_cpu(pfn_sb->dataoff);
  296. nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode);
  297. if (nd_pfn->mode == PFN_MODE_RAM) {
  298. if (offset != SZ_8K)
  299. return -EINVAL;
  300. nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
  301. altmap = NULL;
  302. } else {
  303. rc = -ENXIO;
  304. goto err;
  305. }
  306. /* establish pfn range for lookup, and switch to direct map */
  307. pmem = dev_get_drvdata(dev);
  308. memunmap_pmem(dev, pmem->virt_addr);
  309. pmem->virt_addr = (void __pmem *)devm_memremap_pages(dev, &nsio->res);
  310. if (IS_ERR(pmem->virt_addr)) {
  311. rc = PTR_ERR(pmem->virt_addr);
  312. goto err;
  313. }
  314. /* attach pmem disk in "pfn-mode" */
  315. pmem->data_offset = offset;
  316. rc = pmem_attach_disk(dev, ndns, pmem);
  317. if (rc)
  318. goto err;
  319. return rc;
  320. err:
  321. nvdimm_namespace_detach_pfn(ndns);
  322. return rc;
  323. }
  324. static int nd_pmem_probe(struct device *dev)
  325. {
  326. struct nd_region *nd_region = to_nd_region(dev->parent);
  327. struct nd_namespace_common *ndns;
  328. struct nd_namespace_io *nsio;
  329. struct pmem_device *pmem;
  330. ndns = nvdimm_namespace_common_probe(dev);
  331. if (IS_ERR(ndns))
  332. return PTR_ERR(ndns);
  333. nsio = to_nd_namespace_io(&ndns->dev);
  334. pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
  335. if (IS_ERR(pmem))
  336. return PTR_ERR(pmem);
  337. pmem->ndns = ndns;
  338. dev_set_drvdata(dev, pmem);
  339. ndns->rw_bytes = pmem_rw_bytes;
  340. if (is_nd_btt(dev))
  341. return nvdimm_namespace_attach_btt(ndns);
  342. if (is_nd_pfn(dev))
  343. return nvdimm_namespace_attach_pfn(ndns);
  344. if (nd_btt_probe(ndns, pmem) == 0) {
  345. /* we'll come back as btt-pmem */
  346. return -ENXIO;
  347. }
  348. if (nd_pfn_probe(ndns, pmem) == 0) {
  349. /* we'll come back as pfn-pmem */
  350. return -ENXIO;
  351. }
  352. return pmem_attach_disk(dev, ndns, pmem);
  353. }
  354. static int nd_pmem_remove(struct device *dev)
  355. {
  356. struct pmem_device *pmem = dev_get_drvdata(dev);
  357. if (is_nd_btt(dev))
  358. nvdimm_namespace_detach_btt(pmem->ndns);
  359. else if (is_nd_pfn(dev))
  360. nvdimm_namespace_detach_pfn(pmem->ndns);
  361. else
  362. pmem_detach_disk(pmem);
  363. return 0;
  364. }
  365. MODULE_ALIAS("pmem");
  366. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
  367. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
  368. static struct nd_device_driver nd_pmem_driver = {
  369. .probe = nd_pmem_probe,
  370. .remove = nd_pmem_remove,
  371. .drv = {
  372. .name = "nd_pmem",
  373. },
  374. .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
  375. };
  376. static int __init pmem_init(void)
  377. {
  378. int error;
  379. pmem_major = register_blkdev(0, "pmem");
  380. if (pmem_major < 0)
  381. return pmem_major;
  382. error = nd_driver_register(&nd_pmem_driver);
  383. if (error) {
  384. unregister_blkdev(pmem_major, "pmem");
  385. return error;
  386. }
  387. return 0;
  388. }
  389. module_init(pmem_init);
  390. static void pmem_exit(void)
  391. {
  392. driver_unregister(&nd_pmem_driver.drv);
  393. unregister_blkdev(pmem_major, "pmem");
  394. }
  395. module_exit(pmem_exit);
  396. MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
  397. MODULE_LICENSE("GPL v2");