xref: /OK3568_Linux_fs/kernel/drivers/dax/pmem/core.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
3*4882a593Smuzhiyun #include <linux/memremap.h>
4*4882a593Smuzhiyun #include <linux/module.h>
5*4882a593Smuzhiyun #include <linux/pfn_t.h>
6*4882a593Smuzhiyun #include "../../nvdimm/pfn.h"
7*4882a593Smuzhiyun #include "../../nvdimm/nd.h"
8*4882a593Smuzhiyun #include "../bus.h"
9*4882a593Smuzhiyun 
__dax_pmem_probe(struct device * dev,enum dev_dax_subsys subsys)10*4882a593Smuzhiyun struct dev_dax *__dax_pmem_probe(struct device *dev, enum dev_dax_subsys subsys)
11*4882a593Smuzhiyun {
12*4882a593Smuzhiyun 	struct range range;
13*4882a593Smuzhiyun 	int rc, id, region_id;
14*4882a593Smuzhiyun 	resource_size_t offset;
15*4882a593Smuzhiyun 	struct nd_pfn_sb *pfn_sb;
16*4882a593Smuzhiyun 	struct dev_dax *dev_dax;
17*4882a593Smuzhiyun 	struct dev_dax_data data;
18*4882a593Smuzhiyun 	struct nd_namespace_io *nsio;
19*4882a593Smuzhiyun 	struct dax_region *dax_region;
20*4882a593Smuzhiyun 	struct dev_pagemap pgmap = { };
21*4882a593Smuzhiyun 	struct nd_namespace_common *ndns;
22*4882a593Smuzhiyun 	struct nd_dax *nd_dax = to_nd_dax(dev);
23*4882a593Smuzhiyun 	struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
24*4882a593Smuzhiyun 	struct nd_region *nd_region = to_nd_region(dev->parent);
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	ndns = nvdimm_namespace_common_probe(dev);
27*4882a593Smuzhiyun 	if (IS_ERR(ndns))
28*4882a593Smuzhiyun 		return ERR_CAST(ndns);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	/* parse the 'pfn' info block via ->rw_bytes */
31*4882a593Smuzhiyun 	rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
32*4882a593Smuzhiyun 	if (rc)
33*4882a593Smuzhiyun 		return ERR_PTR(rc);
34*4882a593Smuzhiyun 	rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
35*4882a593Smuzhiyun 	if (rc)
36*4882a593Smuzhiyun 		return ERR_PTR(rc);
37*4882a593Smuzhiyun 	devm_namespace_disable(dev, ndns);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	/* reserve the metadata area, device-dax will reserve the data */
40*4882a593Smuzhiyun 	pfn_sb = nd_pfn->pfn_sb;
41*4882a593Smuzhiyun 	offset = le64_to_cpu(pfn_sb->dataoff);
42*4882a593Smuzhiyun 	nsio = to_nd_namespace_io(&ndns->dev);
43*4882a593Smuzhiyun 	if (!devm_request_mem_region(dev, nsio->res.start, offset,
44*4882a593Smuzhiyun 				dev_name(&ndns->dev))) {
45*4882a593Smuzhiyun 		dev_warn(dev, "could not reserve metadata\n");
46*4882a593Smuzhiyun 		return ERR_PTR(-EBUSY);
47*4882a593Smuzhiyun 	}
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
50*4882a593Smuzhiyun 	if (rc != 2)
51*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* adjust the dax_region range to the start of data */
54*4882a593Smuzhiyun 	range = pgmap.range;
55*4882a593Smuzhiyun 	range.start += offset,
56*4882a593Smuzhiyun 	dax_region = alloc_dax_region(dev, region_id, &range,
57*4882a593Smuzhiyun 			nd_region->target_node, le32_to_cpu(pfn_sb->align),
58*4882a593Smuzhiyun 			IORESOURCE_DAX_STATIC);
59*4882a593Smuzhiyun 	if (!dax_region)
60*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	data = (struct dev_dax_data) {
63*4882a593Smuzhiyun 		.dax_region = dax_region,
64*4882a593Smuzhiyun 		.id = id,
65*4882a593Smuzhiyun 		.pgmap = &pgmap,
66*4882a593Smuzhiyun 		.subsys = subsys,
67*4882a593Smuzhiyun 		.size = range_len(&range),
68*4882a593Smuzhiyun 	};
69*4882a593Smuzhiyun 	dev_dax = devm_create_dev_dax(&data);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	/* child dev_dax instances now own the lifetime of the dax_region */
72*4882a593Smuzhiyun 	dax_region_put(dax_region);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	return dev_dax;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__dax_pmem_probe);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
79*4882a593Smuzhiyun MODULE_AUTHOR("Intel Corporation");
80