xref: /openbmc/linux/drivers/dax/pmem.c (revision d0e22329)
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 "device-dax.h"
20 
21 struct dax_pmem {
22 	struct device *dev;
23 	struct percpu_ref ref;
24 	struct dev_pagemap pgmap;
25 	struct completion cmp;
26 };
27 
28 static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
29 {
30 	return container_of(ref, struct dax_pmem, ref);
31 }
32 
33 static void dax_pmem_percpu_release(struct percpu_ref *ref)
34 {
35 	struct dax_pmem *dax_pmem = to_dax_pmem(ref);
36 
37 	dev_dbg(dax_pmem->dev, "trace\n");
38 	complete(&dax_pmem->cmp);
39 }
40 
41 static void dax_pmem_percpu_exit(void *data)
42 {
43 	struct percpu_ref *ref = data;
44 	struct dax_pmem *dax_pmem = to_dax_pmem(ref);
45 
46 	dev_dbg(dax_pmem->dev, "trace\n");
47 	wait_for_completion(&dax_pmem->cmp);
48 	percpu_ref_exit(ref);
49 }
50 
51 static void dax_pmem_percpu_kill(struct percpu_ref *ref)
52 {
53 	struct dax_pmem *dax_pmem = to_dax_pmem(ref);
54 
55 	dev_dbg(dax_pmem->dev, "trace\n");
56 	percpu_ref_kill(ref);
57 }
58 
59 static int dax_pmem_probe(struct device *dev)
60 {
61 	void *addr;
62 	struct resource res;
63 	int rc, id, region_id;
64 	struct nd_pfn_sb *pfn_sb;
65 	struct dev_dax *dev_dax;
66 	struct dax_pmem *dax_pmem;
67 	struct nd_namespace_io *nsio;
68 	struct dax_region *dax_region;
69 	struct nd_namespace_common *ndns;
70 	struct nd_dax *nd_dax = to_nd_dax(dev);
71 	struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
72 
73 	ndns = nvdimm_namespace_common_probe(dev);
74 	if (IS_ERR(ndns))
75 		return PTR_ERR(ndns);
76 	nsio = to_nd_namespace_io(&ndns->dev);
77 
78 	dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
79 	if (!dax_pmem)
80 		return -ENOMEM;
81 
82 	/* parse the 'pfn' info block via ->rw_bytes */
83 	rc = devm_nsio_enable(dev, nsio);
84 	if (rc)
85 		return rc;
86 	rc = nvdimm_setup_pfn(nd_pfn, &dax_pmem->pgmap);
87 	if (rc)
88 		return rc;
89 	devm_nsio_disable(dev, nsio);
90 
91 	pfn_sb = nd_pfn->pfn_sb;
92 
93 	if (!devm_request_mem_region(dev, nsio->res.start,
94 				resource_size(&nsio->res),
95 				dev_name(&ndns->dev))) {
96 		dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
97 		return -EBUSY;
98 	}
99 
100 	dax_pmem->dev = dev;
101 	init_completion(&dax_pmem->cmp);
102 	rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
103 			GFP_KERNEL);
104 	if (rc)
105 		return rc;
106 
107 	rc = devm_add_action(dev, dax_pmem_percpu_exit, &dax_pmem->ref);
108 	if (rc) {
109 		percpu_ref_exit(&dax_pmem->ref);
110 		return rc;
111 	}
112 
113 	dax_pmem->pgmap.ref = &dax_pmem->ref;
114 	dax_pmem->pgmap.kill = dax_pmem_percpu_kill;
115 	addr = devm_memremap_pages(dev, &dax_pmem->pgmap);
116 	if (IS_ERR(addr))
117 		return PTR_ERR(addr);
118 
119 	/* adjust the dax_region resource to the start of data */
120 	memcpy(&res, &dax_pmem->pgmap.res, sizeof(res));
121 	res.start += le64_to_cpu(pfn_sb->dataoff);
122 
123 	rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
124 	if (rc != 2)
125 		return -EINVAL;
126 
127 	dax_region = alloc_dax_region(dev, region_id, &res,
128 			le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
129 	if (!dax_region)
130 		return -ENOMEM;
131 
132 	/* TODO: support for subdividing a dax region... */
133 	dev_dax = devm_create_dev_dax(dax_region, id, &res, 1);
134 
135 	/* child dev_dax instances now own the lifetime of the dax_region */
136 	dax_region_put(dax_region);
137 
138 	return PTR_ERR_OR_ZERO(dev_dax);
139 }
140 
141 static struct nd_device_driver dax_pmem_driver = {
142 	.probe = dax_pmem_probe,
143 	.drv = {
144 		.name = "dax_pmem",
145 	},
146 	.type = ND_DRIVER_DAX_PMEM,
147 };
148 
149 module_nd_driver(dax_pmem_driver);
150 
151 MODULE_LICENSE("GPL v2");
152 MODULE_AUTHOR("Intel Corporation");
153 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);
154