162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright(c) 2016 Intel Corporation. All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#ifndef __DAX_PRIVATE_H__
662306a36Sopenharmony_ci#define __DAX_PRIVATE_H__
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/device.h>
962306a36Sopenharmony_ci#include <linux/cdev.h>
1062306a36Sopenharmony_ci#include <linux/idr.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/* private routines between core files */
1362306a36Sopenharmony_cistruct dax_device;
1462306a36Sopenharmony_cistruct dax_device *inode_dax(struct inode *inode);
1562306a36Sopenharmony_cistruct inode *dax_inode(struct dax_device *dax_dev);
1662306a36Sopenharmony_ciint dax_bus_init(void);
1762306a36Sopenharmony_civoid dax_bus_exit(void);
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/**
2062306a36Sopenharmony_ci * struct dax_region - mapping infrastructure for dax devices
2162306a36Sopenharmony_ci * @id: kernel-wide unique region for a memory range
2262306a36Sopenharmony_ci * @target_node: effective numa node if this memory range is onlined
2362306a36Sopenharmony_ci * @kref: to pin while other agents have a need to do lookups
2462306a36Sopenharmony_ci * @dev: parent device backing this region
2562306a36Sopenharmony_ci * @align: allocation and mapping alignment for child dax devices
2662306a36Sopenharmony_ci * @ida: instance id allocator
2762306a36Sopenharmony_ci * @res: resource tree to track instance allocations
2862306a36Sopenharmony_ci * @seed: allow userspace to find the first unbound seed device
2962306a36Sopenharmony_ci * @youngest: allow userspace to find the most recently created device
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_cistruct dax_region {
3262306a36Sopenharmony_ci	int id;
3362306a36Sopenharmony_ci	int target_node;
3462306a36Sopenharmony_ci	struct kref kref;
3562306a36Sopenharmony_ci	struct device *dev;
3662306a36Sopenharmony_ci	unsigned int align;
3762306a36Sopenharmony_ci	struct ida ida;
3862306a36Sopenharmony_ci	struct resource res;
3962306a36Sopenharmony_ci	struct device *seed;
4062306a36Sopenharmony_ci	struct device *youngest;
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct dax_mapping {
4462306a36Sopenharmony_ci	struct device dev;
4562306a36Sopenharmony_ci	int range_id;
4662306a36Sopenharmony_ci	int id;
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/**
5062306a36Sopenharmony_ci * struct dev_dax - instance data for a subdivision of a dax region, and
5162306a36Sopenharmony_ci * data while the device is activated in the driver.
5262306a36Sopenharmony_ci * @region - parent region
5362306a36Sopenharmony_ci * @dax_dev - core dax functionality
5462306a36Sopenharmony_ci * @target_node: effective numa node if dev_dax memory range is onlined
5562306a36Sopenharmony_ci * @dyn_id: is this a dynamic or statically created instance
5662306a36Sopenharmony_ci * @id: ida allocated id when the dax_region is not static
5762306a36Sopenharmony_ci * @ida: mapping id allocator
5862306a36Sopenharmony_ci * @dev - device core
5962306a36Sopenharmony_ci * @pgmap - pgmap for memmap setup / lifetime (driver owned)
6062306a36Sopenharmony_ci * @nr_range: size of @ranges
6162306a36Sopenharmony_ci * @ranges: resource-span + pgoff tuples for the instance
6262306a36Sopenharmony_ci */
6362306a36Sopenharmony_cistruct dev_dax {
6462306a36Sopenharmony_ci	struct dax_region *region;
6562306a36Sopenharmony_ci	struct dax_device *dax_dev;
6662306a36Sopenharmony_ci	unsigned int align;
6762306a36Sopenharmony_ci	int target_node;
6862306a36Sopenharmony_ci	bool dyn_id;
6962306a36Sopenharmony_ci	int id;
7062306a36Sopenharmony_ci	struct ida ida;
7162306a36Sopenharmony_ci	struct device dev;
7262306a36Sopenharmony_ci	struct dev_pagemap *pgmap;
7362306a36Sopenharmony_ci	int nr_range;
7462306a36Sopenharmony_ci	struct dev_dax_range {
7562306a36Sopenharmony_ci		unsigned long pgoff;
7662306a36Sopenharmony_ci		struct range range;
7762306a36Sopenharmony_ci		struct dax_mapping *mapping;
7862306a36Sopenharmony_ci	} *ranges;
7962306a36Sopenharmony_ci};
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/*
8262306a36Sopenharmony_ci * While run_dax() is potentially a generic operation that could be
8362306a36Sopenharmony_ci * defined in include/linux/dax.h we don't want to grow any users
8462306a36Sopenharmony_ci * outside of drivers/dax/
8562306a36Sopenharmony_ci */
8662306a36Sopenharmony_civoid run_dax(struct dax_device *dax_dev);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic inline struct dev_dax *to_dev_dax(struct device *dev)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	return container_of(dev, struct dev_dax, dev);
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic inline struct dax_mapping *to_dax_mapping(struct device *dev)
9462306a36Sopenharmony_ci{
9562306a36Sopenharmony_ci	return container_of(dev, struct dax_mapping, dev);
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciphys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff, unsigned long size);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
10162306a36Sopenharmony_cistatic inline bool dax_align_valid(unsigned long align)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	if (align == PUD_SIZE && IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
10462306a36Sopenharmony_ci		return true;
10562306a36Sopenharmony_ci	if (align == PMD_SIZE && has_transparent_hugepage())
10662306a36Sopenharmony_ci		return true;
10762306a36Sopenharmony_ci	if (align == PAGE_SIZE)
10862306a36Sopenharmony_ci		return true;
10962306a36Sopenharmony_ci	return false;
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci#else
11262306a36Sopenharmony_cistatic inline bool dax_align_valid(unsigned long align)
11362306a36Sopenharmony_ci{
11462306a36Sopenharmony_ci	return align == PAGE_SIZE;
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
11762306a36Sopenharmony_ci#endif
118