pmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 blk_qc_t 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. return BLK_QC_T_NONE;
  77. }
  78. static int pmem_rw_page(struct block_device *bdev, sector_t sector,
  79. struct page *page, int rw)
  80. {
  81. struct pmem_device *pmem = bdev->bd_disk->private_data;
  82. pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector);
  83. if (rw & WRITE)
  84. wmb_pmem();
  85. page_endio(page, rw & WRITE, 0);
  86. return 0;
  87. }
  88. static long pmem_direct_access(struct block_device *bdev, sector_t sector,
  89. void __pmem **kaddr, unsigned long *pfn)
  90. {
  91. struct pmem_device *pmem = bdev->bd_disk->private_data;
  92. resource_size_t offset = sector * 512 + pmem->data_offset;
  93. *kaddr = pmem->virt_addr + offset;
  94. *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT;
  95. return pmem->size - offset;
  96. }
  97. static const struct block_device_operations pmem_fops = {
  98. .owner = THIS_MODULE,
  99. .rw_page = pmem_rw_page,
  100. .direct_access = pmem_direct_access,
  101. .revalidate_disk = nvdimm_revalidate_disk,
  102. };
  103. static struct pmem_device *pmem_alloc(struct device *dev,
  104. struct resource *res, int id)
  105. {
  106. struct pmem_device *pmem;
  107. pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
  108. if (!pmem)
  109. return ERR_PTR(-ENOMEM);
  110. pmem->phys_addr = res->start;
  111. pmem->size = resource_size(res);
  112. if (!arch_has_wmb_pmem())
  113. dev_warn(dev, "unable to guarantee persistence of writes\n");
  114. if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size,
  115. dev_name(dev))) {
  116. dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
  117. &pmem->phys_addr, pmem->size);
  118. return ERR_PTR(-EBUSY);
  119. }
  120. if (pmem_should_map_pages(dev))
  121. pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, res);
  122. else
  123. pmem->virt_addr = (void __pmem *) devm_memremap(dev,
  124. pmem->phys_addr, pmem->size,
  125. ARCH_MEMREMAP_PMEM);
  126. if (IS_ERR(pmem->virt_addr))
  127. return (void __force *) pmem->virt_addr;
  128. return pmem;
  129. }
  130. static void pmem_detach_disk(struct pmem_device *pmem)
  131. {
  132. if (!pmem->pmem_disk)
  133. return;
  134. del_gendisk(pmem->pmem_disk);
  135. put_disk(pmem->pmem_disk);
  136. blk_cleanup_queue(pmem->pmem_queue);
  137. }
  138. static int pmem_attach_disk(struct device *dev,
  139. struct nd_namespace_common *ndns, struct pmem_device *pmem)
  140. {
  141. int nid = dev_to_node(dev);
  142. struct gendisk *disk;
  143. pmem->pmem_queue = blk_alloc_queue_node(GFP_KERNEL, nid);
  144. if (!pmem->pmem_queue)
  145. return -ENOMEM;
  146. blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
  147. blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE);
  148. blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
  149. blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
  150. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue);
  151. disk = alloc_disk_node(0, nid);
  152. if (!disk) {
  153. blk_cleanup_queue(pmem->pmem_queue);
  154. return -ENOMEM;
  155. }
  156. disk->major = pmem_major;
  157. disk->first_minor = 0;
  158. disk->fops = &pmem_fops;
  159. disk->private_data = pmem;
  160. disk->queue = pmem->pmem_queue;
  161. disk->flags = GENHD_FL_EXT_DEVT;
  162. nvdimm_namespace_disk_name(ndns, disk->disk_name);
  163. disk->driverfs_dev = dev;
  164. set_capacity(disk, (pmem->size - pmem->data_offset) / 512);
  165. pmem->pmem_disk = disk;
  166. add_disk(disk);
  167. revalidate_disk(disk);
  168. return 0;
  169. }
  170. static int pmem_rw_bytes(struct nd_namespace_common *ndns,
  171. resource_size_t offset, void *buf, size_t size, int rw)
  172. {
  173. struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
  174. if (unlikely(offset + size > pmem->size)) {
  175. dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
  176. return -EFAULT;
  177. }
  178. if (rw == READ)
  179. memcpy_from_pmem(buf, pmem->virt_addr + offset, size);
  180. else {
  181. memcpy_to_pmem(pmem->virt_addr + offset, buf, size);
  182. wmb_pmem();
  183. }
  184. return 0;
  185. }
  186. static int nd_pfn_init(struct nd_pfn *nd_pfn)
  187. {
  188. struct nd_pfn_sb *pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
  189. struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev);
  190. struct nd_namespace_common *ndns = nd_pfn->ndns;
  191. struct nd_region *nd_region;
  192. unsigned long npfns;
  193. phys_addr_t offset;
  194. u64 checksum;
  195. int rc;
  196. if (!pfn_sb)
  197. return -ENOMEM;
  198. nd_pfn->pfn_sb = pfn_sb;
  199. rc = nd_pfn_validate(nd_pfn);
  200. if (rc == 0 || rc == -EBUSY)
  201. return rc;
  202. /* section alignment for simple hotplug */
  203. if (nvdimm_namespace_capacity(ndns) < ND_PFN_ALIGN
  204. || pmem->phys_addr & ND_PFN_MASK)
  205. return -ENODEV;
  206. nd_region = to_nd_region(nd_pfn->dev.parent);
  207. if (nd_region->ro) {
  208. dev_info(&nd_pfn->dev,
  209. "%s is read-only, unable to init metadata\n",
  210. dev_name(&nd_region->dev));
  211. goto err;
  212. }
  213. memset(pfn_sb, 0, sizeof(*pfn_sb));
  214. npfns = (pmem->size - SZ_8K) / SZ_4K;
  215. /*
  216. * Note, we use 64 here for the standard size of struct page,
  217. * debugging options may cause it to be larger in which case the
  218. * implementation will limit the pfns advertised through
  219. * ->direct_access() to those that are included in the memmap.
  220. */
  221. if (nd_pfn->mode == PFN_MODE_PMEM)
  222. offset = ALIGN(SZ_8K + 64 * npfns, PMD_SIZE);
  223. else if (nd_pfn->mode == PFN_MODE_RAM)
  224. offset = SZ_8K;
  225. else
  226. goto err;
  227. npfns = (pmem->size - offset) / SZ_4K;
  228. pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
  229. pfn_sb->dataoff = cpu_to_le64(offset);
  230. pfn_sb->npfns = cpu_to_le64(npfns);
  231. memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN);
  232. memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
  233. pfn_sb->version_major = cpu_to_le16(1);
  234. checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
  235. pfn_sb->checksum = cpu_to_le64(checksum);
  236. rc = nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
  237. if (rc)
  238. goto err;
  239. return 0;
  240. err:
  241. nd_pfn->pfn_sb = NULL;
  242. kfree(pfn_sb);
  243. return -ENXIO;
  244. }
  245. static int nvdimm_namespace_detach_pfn(struct nd_namespace_common *ndns)
  246. {
  247. struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
  248. struct pmem_device *pmem;
  249. /* free pmem disk */
  250. pmem = dev_get_drvdata(&nd_pfn->dev);
  251. pmem_detach_disk(pmem);
  252. /* release nd_pfn resources */
  253. kfree(nd_pfn->pfn_sb);
  254. nd_pfn->pfn_sb = NULL;
  255. return 0;
  256. }
  257. static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns)
  258. {
  259. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  260. struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
  261. struct device *dev = &nd_pfn->dev;
  262. struct vmem_altmap *altmap;
  263. struct nd_region *nd_region;
  264. struct nd_pfn_sb *pfn_sb;
  265. struct pmem_device *pmem;
  266. phys_addr_t offset;
  267. int rc;
  268. if (!nd_pfn->uuid || !nd_pfn->ndns)
  269. return -ENODEV;
  270. nd_region = to_nd_region(dev->parent);
  271. rc = nd_pfn_init(nd_pfn);
  272. if (rc)
  273. return rc;
  274. if (PAGE_SIZE != SZ_4K) {
  275. dev_err(dev, "only supported on systems with 4K PAGE_SIZE\n");
  276. return -ENXIO;
  277. }
  278. if (nsio->res.start & ND_PFN_MASK) {
  279. dev_err(dev, "%s not memory hotplug section aligned\n",
  280. dev_name(&ndns->dev));
  281. return -ENXIO;
  282. }
  283. pfn_sb = nd_pfn->pfn_sb;
  284. offset = le64_to_cpu(pfn_sb->dataoff);
  285. nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode);
  286. if (nd_pfn->mode == PFN_MODE_RAM) {
  287. if (offset != SZ_8K)
  288. return -EINVAL;
  289. nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
  290. altmap = NULL;
  291. } else {
  292. rc = -ENXIO;
  293. goto err;
  294. }
  295. /* establish pfn range for lookup, and switch to direct map */
  296. pmem = dev_get_drvdata(dev);
  297. devm_memunmap(dev, (void __force *) pmem->virt_addr);
  298. pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, &nsio->res);
  299. if (IS_ERR(pmem->virt_addr)) {
  300. rc = PTR_ERR(pmem->virt_addr);
  301. goto err;
  302. }
  303. /* attach pmem disk in "pfn-mode" */
  304. pmem->data_offset = offset;
  305. rc = pmem_attach_disk(dev, ndns, pmem);
  306. if (rc)
  307. goto err;
  308. return rc;
  309. err:
  310. nvdimm_namespace_detach_pfn(ndns);
  311. return rc;
  312. }
  313. static int nd_pmem_probe(struct device *dev)
  314. {
  315. struct nd_region *nd_region = to_nd_region(dev->parent);
  316. struct nd_namespace_common *ndns;
  317. struct nd_namespace_io *nsio;
  318. struct pmem_device *pmem;
  319. ndns = nvdimm_namespace_common_probe(dev);
  320. if (IS_ERR(ndns))
  321. return PTR_ERR(ndns);
  322. nsio = to_nd_namespace_io(&ndns->dev);
  323. pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
  324. if (IS_ERR(pmem))
  325. return PTR_ERR(pmem);
  326. pmem->ndns = ndns;
  327. dev_set_drvdata(dev, pmem);
  328. ndns->rw_bytes = pmem_rw_bytes;
  329. if (is_nd_btt(dev))
  330. return nvdimm_namespace_attach_btt(ndns);
  331. if (is_nd_pfn(dev))
  332. return nvdimm_namespace_attach_pfn(ndns);
  333. if (nd_btt_probe(ndns, pmem) == 0) {
  334. /* we'll come back as btt-pmem */
  335. return -ENXIO;
  336. }
  337. if (nd_pfn_probe(ndns, pmem) == 0) {
  338. /* we'll come back as pfn-pmem */
  339. return -ENXIO;
  340. }
  341. return pmem_attach_disk(dev, ndns, pmem);
  342. }
  343. static int nd_pmem_remove(struct device *dev)
  344. {
  345. struct pmem_device *pmem = dev_get_drvdata(dev);
  346. if (is_nd_btt(dev))
  347. nvdimm_namespace_detach_btt(pmem->ndns);
  348. else if (is_nd_pfn(dev))
  349. nvdimm_namespace_detach_pfn(pmem->ndns);
  350. else
  351. pmem_detach_disk(pmem);
  352. return 0;
  353. }
  354. MODULE_ALIAS("pmem");
  355. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
  356. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
  357. static struct nd_device_driver nd_pmem_driver = {
  358. .probe = nd_pmem_probe,
  359. .remove = nd_pmem_remove,
  360. .drv = {
  361. .name = "nd_pmem",
  362. },
  363. .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
  364. };
  365. static int __init pmem_init(void)
  366. {
  367. int error;
  368. pmem_major = register_blkdev(0, "pmem");
  369. if (pmem_major < 0)
  370. return pmem_major;
  371. error = nd_driver_register(&nd_pmem_driver);
  372. if (error) {
  373. unregister_blkdev(pmem_major, "pmem");
  374. return error;
  375. }
  376. return 0;
  377. }
  378. module_init(pmem_init);
  379. static void pmem_exit(void)
  380. {
  381. driver_unregister(&nd_pmem_driver.drv);
  382. unregister_blkdev(pmem_major, "pmem");
  383. }
  384. module_exit(pmem_exit);
  385. MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
  386. MODULE_LICENSE("GPL v2");