Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
 
 
 
 
 
 
 
 
 
 
 
  3#include <linux/memremap.h>
  4#include <linux/module.h>
  5#include <linux/pfn_t.h>
  6#include "../nvdimm/pfn.h"
  7#include "../nvdimm/nd.h"
  8#include "bus.h"
  9
 10static struct dev_dax *__dax_pmem_probe(struct device *dev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 11{
 12	struct range range;
 13	int rc, id, region_id;
 14	resource_size_t offset;
 
 15	struct nd_pfn_sb *pfn_sb;
 16	struct dev_dax *dev_dax;
 17	struct dev_dax_data data;
 18	struct nd_namespace_io *nsio;
 19	struct dax_region *dax_region;
 20	struct dev_pagemap pgmap = { };
 21	struct nd_namespace_common *ndns;
 22	struct nd_dax *nd_dax = to_nd_dax(dev);
 23	struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
 24	struct nd_region *nd_region = to_nd_region(dev->parent);
 25
 26	ndns = nvdimm_namespace_common_probe(dev);
 27	if (IS_ERR(ndns))
 28		return ERR_CAST(ndns);
 
 29
 30	/* parse the 'pfn' info block via ->rw_bytes */
 31	rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
 32	if (rc)
 33		return ERR_PTR(rc);
 34	rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
 35	if (rc)
 36		return ERR_PTR(rc);
 37	devm_namespace_disable(dev, ndns);
 
 
 
 38
 39	/* reserve the metadata area, device-dax will reserve the data */
 40	pfn_sb = nd_pfn->pfn_sb;
 41	offset = le64_to_cpu(pfn_sb->dataoff);
 42	nsio = to_nd_namespace_io(&ndns->dev);
 43	if (!devm_request_mem_region(dev, nsio->res.start, offset,
 44				dev_name(&ndns->dev))) {
 45		dev_warn(dev, "could not reserve metadata\n");
 46		return ERR_PTR(-EBUSY);
 47	}
 48
 49	rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
 50	if (rc != 2)
 51		return ERR_PTR(-EINVAL);
 52
 53	/* adjust the dax_region range to the start of data */
 54	range = pgmap.range;
 55	range.start += offset;
 56	dax_region = alloc_dax_region(dev, region_id, &range,
 57			nd_region->target_node, le32_to_cpu(pfn_sb->align),
 58			IORESOURCE_DAX_STATIC);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 59	if (!dax_region)
 60		return ERR_PTR(-ENOMEM);
 61
 62	data = (struct dev_dax_data) {
 63		.dax_region = dax_region,
 64		.id = id,
 65		.pgmap = &pgmap,
 66		.size = range_len(&range),
 67	};
 68	dev_dax = devm_create_dev_dax(&data);
 69
 70	/* child dev_dax instances now own the lifetime of the dax_region */
 71	dax_region_put(dax_region);
 72
 73	return dev_dax;
 74}
 75
 76static int dax_pmem_probe(struct device *dev)
 77{
 78	return PTR_ERR_OR_ZERO(__dax_pmem_probe(dev));
 79}
 80
 81static struct nd_device_driver dax_pmem_driver = {
 82	.probe = dax_pmem_probe,
 83	.drv = {
 84		.name = "dax_pmem",
 85	},
 86	.type = ND_DRIVER_DAX_PMEM,
 87};
 88
 89static int __init dax_pmem_init(void)
 90{
 91	return nd_driver_register(&dax_pmem_driver);
 92}
 93module_init(dax_pmem_init);
 94
 95static void __exit dax_pmem_exit(void)
 96{
 97	driver_unregister(&dax_pmem_driver.drv);
 98}
 99module_exit(dax_pmem_exit);
100
101MODULE_LICENSE("GPL v2");
102MODULE_AUTHOR("Intel Corporation");
103MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);
v4.10.11
  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 "dax.h"
 20
 21struct dax_pmem {
 22	struct device *dev;
 23	struct percpu_ref ref;
 24	struct completion cmp;
 25};
 26
 27static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
 28{
 29	return container_of(ref, struct dax_pmem, ref);
 30}
 31
 32static void dax_pmem_percpu_release(struct percpu_ref *ref)
 33{
 34	struct dax_pmem *dax_pmem = to_dax_pmem(ref);
 35
 36	dev_dbg(dax_pmem->dev, "%s\n", __func__);
 37	complete(&dax_pmem->cmp);
 38}
 39
 40static void dax_pmem_percpu_exit(void *data)
 41{
 42	struct percpu_ref *ref = data;
 43	struct dax_pmem *dax_pmem = to_dax_pmem(ref);
 44
 45	dev_dbg(dax_pmem->dev, "%s\n", __func__);
 46	percpu_ref_exit(ref);
 47}
 48
 49static void dax_pmem_percpu_kill(void *data)
 50{
 51	struct percpu_ref *ref = data;
 52	struct dax_pmem *dax_pmem = to_dax_pmem(ref);
 53
 54	dev_dbg(dax_pmem->dev, "%s\n", __func__);
 55	percpu_ref_kill(ref);
 56	wait_for_completion(&dax_pmem->cmp);
 57}
 58
 59static int dax_pmem_probe(struct device *dev)
 60{
 61	int rc;
 62	void *addr;
 63	struct resource res;
 64	struct dax_dev *dax_dev;
 65	struct nd_pfn_sb *pfn_sb;
 66	struct dax_pmem *dax_pmem;
 67	struct nd_region *nd_region;
 68	struct nd_namespace_io *nsio;
 69	struct dax_region *dax_region;
 
 70	struct nd_namespace_common *ndns;
 71	struct nd_dax *nd_dax = to_nd_dax(dev);
 72	struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
 73	struct vmem_altmap __altmap, *altmap = NULL;
 74
 75	ndns = nvdimm_namespace_common_probe(dev);
 76	if (IS_ERR(ndns))
 77		return PTR_ERR(ndns);
 78	nsio = to_nd_namespace_io(&ndns->dev);
 79
 80	/* parse the 'pfn' info block via ->rw_bytes */
 81	rc = devm_nsio_enable(dev, nsio);
 
 
 
 82	if (rc)
 83		return rc;
 84	altmap = nvdimm_setup_pfn(nd_pfn, &res, &__altmap);
 85	if (IS_ERR(altmap))
 86		return PTR_ERR(altmap);
 87	devm_nsio_disable(dev, nsio);
 88
 
 89	pfn_sb = nd_pfn->pfn_sb;
 90
 91	if (!devm_request_mem_region(dev, nsio->res.start,
 92				resource_size(&nsio->res),
 93				dev_name(&ndns->dev))) {
 94		dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
 95		return -EBUSY;
 96	}
 97
 98	dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
 99	if (!dax_pmem)
100		return -ENOMEM;
101
102	dax_pmem->dev = dev;
103	init_completion(&dax_pmem->cmp);
104	rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
105			GFP_KERNEL);
106	if (rc)
107		return rc;
108
109	rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
110							&dax_pmem->ref);
111	if (rc)
112		return rc;
113
114	addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
115	if (IS_ERR(addr))
116		return PTR_ERR(addr);
117
118	rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
119							&dax_pmem->ref);
120	if (rc)
121		return rc;
122
123	/* adjust the dax_region resource to the start of data */
124	res.start += le64_to_cpu(pfn_sb->dataoff);
125
126	nd_region = to_nd_region(dev->parent);
127	dax_region = alloc_dax_region(dev, nd_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	dax_dev = devm_create_dax_dev(dax_region, &res, 1);
 
 
 
 
 
134
135	/* child dax_dev instances now own the lifetime of the dax_region */
136	dax_region_put(dax_region);
137
138	return PTR_ERR_OR_ZERO(dax_dev);
 
 
 
 
 
139}
140
141static 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
149static int __init dax_pmem_init(void)
150{
151	return nd_driver_register(&dax_pmem_driver);
152}
153module_init(dax_pmem_init);
154
155static void __exit dax_pmem_exit(void)
156{
157	driver_unregister(&dax_pmem_driver.drv);
158}
159module_exit(dax_pmem_exit);
160
161MODULE_LICENSE("GPL v2");
162MODULE_AUTHOR("Intel Corporation");
163MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);