blob: f3b629779266aacdbb221de683792e21940b1e42 [file] [log] [blame]
Ross Zwisler9e853f22015-04-01 09:12:19 +02001/*
2 * Persistent Memory Driver
3 *
Dan Williams9f53f9f2015-06-09 15:33:45 -04004 * Copyright (c) 2014-2015, Intel Corporation.
Ross Zwisler9e853f22015-04-01 09:12:19 +02005 * 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
18#include <asm/cacheflush.h>
19#include <linux/blkdev.h>
20#include <linux/hdreg.h>
21#include <linux/init.h>
22#include <linux/platform_device.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/slab.h>
Ross Zwisler61031952015-06-25 03:08:39 -040026#include <linux/pmem.h>
Dan Williams9f53f9f2015-06-09 15:33:45 -040027#include <linux/nd.h>
28#include "nd.h"
Ross Zwisler9e853f22015-04-01 09:12:19 +020029
30struct pmem_device {
31 struct request_queue *pmem_queue;
32 struct gendisk *pmem_disk;
33
34 /* One contiguous memory region per device */
35 phys_addr_t phys_addr;
Ross Zwisler61031952015-06-25 03:08:39 -040036 void __pmem *virt_addr;
Ross Zwisler9e853f22015-04-01 09:12:19 +020037 size_t size;
38};
39
40static int pmem_major;
Ross Zwisler9e853f22015-04-01 09:12:19 +020041
42static void pmem_do_bvec(struct pmem_device *pmem, struct page *page,
43 unsigned int len, unsigned int off, int rw,
44 sector_t sector)
45{
46 void *mem = kmap_atomic(page);
47 size_t pmem_off = sector << 9;
Ross Zwisler61031952015-06-25 03:08:39 -040048 void __pmem *pmem_addr = pmem->virt_addr + pmem_off;
Ross Zwisler9e853f22015-04-01 09:12:19 +020049
50 if (rw == READ) {
Ross Zwisler61031952015-06-25 03:08:39 -040051 memcpy_from_pmem(mem + off, pmem_addr, len);
Ross Zwisler9e853f22015-04-01 09:12:19 +020052 flush_dcache_page(page);
53 } else {
54 flush_dcache_page(page);
Ross Zwisler61031952015-06-25 03:08:39 -040055 memcpy_to_pmem(pmem_addr, mem + off, len);
Ross Zwisler9e853f22015-04-01 09:12:19 +020056 }
57
58 kunmap_atomic(mem);
59}
60
61static void pmem_make_request(struct request_queue *q, struct bio *bio)
62{
Dan Williamsf0dc0892015-05-16 12:28:53 -040063 bool do_acct;
64 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -040065 struct bio_vec bvec;
66 struct bvec_iter iter;
Ross Zwisler9e853f22015-04-01 09:12:19 +020067 struct block_device *bdev = bio->bi_bdev;
68 struct pmem_device *pmem = bdev->bd_disk->private_data;
Ross Zwisler9e853f22015-04-01 09:12:19 +020069
Dan Williamsf0dc0892015-05-16 12:28:53 -040070 do_acct = nd_iostat_start(bio, &start);
Dan Williamsedc870e2015-05-16 12:28:51 -040071 bio_for_each_segment(bvec, bio, iter)
Ross Zwisler9e853f22015-04-01 09:12:19 +020072 pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset,
Dan Williamsedc870e2015-05-16 12:28:51 -040073 bio_data_dir(bio), iter.bi_sector);
Dan Williamsf0dc0892015-05-16 12:28:53 -040074 if (do_acct)
75 nd_iostat_end(bio, start);
Ross Zwisler61031952015-06-25 03:08:39 -040076
77 if (bio_data_dir(bio))
78 wmb_pmem();
79
Dan Williamsedc870e2015-05-16 12:28:51 -040080 bio_endio(bio, 0);
Ross Zwisler9e853f22015-04-01 09:12:19 +020081}
82
83static int pmem_rw_page(struct block_device *bdev, sector_t sector,
84 struct page *page, int rw)
85{
86 struct pmem_device *pmem = bdev->bd_disk->private_data;
87
88 pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector);
89 page_endio(page, rw & WRITE, 0);
90
91 return 0;
92}
93
94static long pmem_direct_access(struct block_device *bdev, sector_t sector,
Ross Zwislere2e05392015-08-18 13:55:41 -060095 void __pmem **kaddr, unsigned long *pfn, long size)
Ross Zwisler9e853f22015-04-01 09:12:19 +020096{
97 struct pmem_device *pmem = bdev->bd_disk->private_data;
98 size_t offset = sector << 9;
99
100 if (!pmem)
101 return -ENODEV;
102
Ross Zwisler61031952015-06-25 03:08:39 -0400103 /* FIXME convert DAX to comprehend that this mapping has a lifetime */
Ross Zwislere2e05392015-08-18 13:55:41 -0600104 *kaddr = pmem->virt_addr + offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200105 *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT;
106
107 return pmem->size - offset;
108}
109
110static const struct block_device_operations pmem_fops = {
111 .owner = THIS_MODULE,
112 .rw_page = pmem_rw_page,
113 .direct_access = pmem_direct_access,
Dan Williams58138822015-06-23 20:08:34 -0400114 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200115};
116
Dan Williams9f53f9f2015-06-09 15:33:45 -0400117static struct pmem_device *pmem_alloc(struct device *dev,
118 struct resource *res, int id)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200119{
120 struct pmem_device *pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200121
Christoph Hellwig708ab622015-08-10 23:07:08 -0400122 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200123 if (!pmem)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400124 return ERR_PTR(-ENOMEM);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200125
126 pmem->phys_addr = res->start;
127 pmem->size = resource_size(res);
Ross Zwisler61031952015-06-25 03:08:39 -0400128 if (!arch_has_pmem_api())
129 dev_warn(dev, "unable to guarantee persistence of writes\n");
Ross Zwisler9e853f22015-04-01 09:12:19 +0200130
Christoph Hellwig708ab622015-08-10 23:07:08 -0400131 if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size,
132 dev_name(dev))) {
Dan Williams9f53f9f2015-06-09 15:33:45 -0400133 dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
134 &pmem->phys_addr, pmem->size);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400135 return ERR_PTR(-EBUSY);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200136 }
137
Christoph Hellwig708ab622015-08-10 23:07:08 -0400138 pmem->virt_addr = memremap_pmem(dev, pmem->phys_addr, pmem->size);
139 if (!pmem->virt_addr)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400140 return ERR_PTR(-ENXIO);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400141
142 return pmem;
143}
144
145static void pmem_detach_disk(struct pmem_device *pmem)
146{
147 del_gendisk(pmem->pmem_disk);
148 put_disk(pmem->pmem_disk);
149 blk_cleanup_queue(pmem->pmem_queue);
150}
151
152static int pmem_attach_disk(struct nd_namespace_common *ndns,
153 struct pmem_device *pmem)
154{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400155 struct gendisk *disk;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200156
157 pmem->pmem_queue = blk_alloc_queue(GFP_KERNEL);
158 if (!pmem->pmem_queue)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400159 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200160
161 blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
Vishal Verma6b474962015-07-23 11:58:48 -0600162 blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE);
Dan Williams43d3fa32015-05-16 12:28:50 -0400163 blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200164 blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
Dan Williams0f51c4f2015-05-16 12:28:54 -0400165 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200166
Dan Williams9f53f9f2015-06-09 15:33:45 -0400167 disk = alloc_disk(0);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400168 if (!disk) {
169 blk_cleanup_queue(pmem->pmem_queue);
170 return -ENOMEM;
171 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200172
Ross Zwisler9e853f22015-04-01 09:12:19 +0200173 disk->major = pmem_major;
Dan Williams9f53f9f2015-06-09 15:33:45 -0400174 disk->first_minor = 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200175 disk->fops = &pmem_fops;
176 disk->private_data = pmem;
177 disk->queue = pmem->pmem_queue;
178 disk->flags = GENHD_FL_EXT_DEVT;
Vishal Verma5212e112015-06-25 04:20:32 -0400179 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400180 disk->driverfs_dev = &ndns->dev;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200181 set_capacity(disk, pmem->size >> 9);
182 pmem->pmem_disk = disk;
183
184 add_disk(disk);
Dan Williams58138822015-06-23 20:08:34 -0400185 revalidate_disk(disk);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200186
Dan Williams8c2f7e82015-06-25 04:20:04 -0400187 return 0;
188}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200189
Dan Williams8c2f7e82015-06-25 04:20:04 -0400190static int pmem_rw_bytes(struct nd_namespace_common *ndns,
191 resource_size_t offset, void *buf, size_t size, int rw)
192{
193 struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
194
195 if (unlikely(offset + size > pmem->size)) {
196 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
197 return -EFAULT;
198 }
199
200 if (rw == READ)
Ross Zwisler61031952015-06-25 03:08:39 -0400201 memcpy_from_pmem(buf, pmem->virt_addr + offset, size);
202 else {
203 memcpy_to_pmem(pmem->virt_addr + offset, buf, size);
204 wmb_pmem();
205 }
Dan Williams8c2f7e82015-06-25 04:20:04 -0400206
207 return 0;
208}
209
Dan Williams9f53f9f2015-06-09 15:33:45 -0400210static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200211{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400212 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400213 struct nd_namespace_common *ndns;
214 struct nd_namespace_io *nsio;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200215 struct pmem_device *pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200216
Dan Williams8c2f7e82015-06-25 04:20:04 -0400217 ndns = nvdimm_namespace_common_probe(dev);
218 if (IS_ERR(ndns))
219 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400220
Dan Williams8c2f7e82015-06-25 04:20:04 -0400221 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400222 pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200223 if (IS_ERR(pmem))
224 return PTR_ERR(pmem);
225
Dan Williams9f53f9f2015-06-09 15:33:45 -0400226 dev_set_drvdata(dev, pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400227 ndns->rw_bytes = pmem_rw_bytes;
Christoph Hellwig708ab622015-08-10 23:07:08 -0400228
Dan Williams8c2f7e82015-06-25 04:20:04 -0400229 if (is_nd_btt(dev))
Christoph Hellwig708ab622015-08-10 23:07:08 -0400230 return nvdimm_namespace_attach_btt(ndns);
231
232 if (nd_btt_probe(ndns, pmem) == 0)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400233 /* we'll come back as btt-pmem */
Christoph Hellwig708ab622015-08-10 23:07:08 -0400234 return -ENXIO;
235 return pmem_attach_disk(ndns, pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200236}
237
Dan Williams9f53f9f2015-06-09 15:33:45 -0400238static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200239{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400240 struct pmem_device *pmem = dev_get_drvdata(dev);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200241
Dan Williams8c2f7e82015-06-25 04:20:04 -0400242 if (is_nd_btt(dev))
243 nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns);
244 else
245 pmem_detach_disk(pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400246
Ross Zwisler9e853f22015-04-01 09:12:19 +0200247 return 0;
248}
249
Dan Williams9f53f9f2015-06-09 15:33:45 -0400250MODULE_ALIAS("pmem");
251MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400252MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400253static struct nd_device_driver nd_pmem_driver = {
254 .probe = nd_pmem_probe,
255 .remove = nd_pmem_remove,
256 .drv = {
257 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200258 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400259 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200260};
261
262static int __init pmem_init(void)
263{
264 int error;
265
266 pmem_major = register_blkdev(0, "pmem");
267 if (pmem_major < 0)
268 return pmem_major;
269
Dan Williams9f53f9f2015-06-09 15:33:45 -0400270 error = nd_driver_register(&nd_pmem_driver);
271 if (error) {
Ross Zwisler9e853f22015-04-01 09:12:19 +0200272 unregister_blkdev(pmem_major, "pmem");
Dan Williams9f53f9f2015-06-09 15:33:45 -0400273 return error;
274 }
275
276 return 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200277}
278module_init(pmem_init);
279
280static void pmem_exit(void)
281{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400282 driver_unregister(&nd_pmem_driver.drv);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200283 unregister_blkdev(pmem_major, "pmem");
284}
285module_exit(pmem_exit);
286
287MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
288MODULE_LICENSE("GPL v2");