18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright(c) 2016 Intel Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#ifndef __DAX_PRIVATE_H__
68c2ecf20Sopenharmony_ci#define __DAX_PRIVATE_H__
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/device.h>
98c2ecf20Sopenharmony_ci#include <linux/cdev.h>
108c2ecf20Sopenharmony_ci#include <linux/idr.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* private routines between core files */
138c2ecf20Sopenharmony_cistruct dax_device;
148c2ecf20Sopenharmony_cistruct dax_device *inode_dax(struct inode *inode);
158c2ecf20Sopenharmony_cistruct inode *dax_inode(struct dax_device *dax_dev);
168c2ecf20Sopenharmony_ciint dax_bus_init(void);
178c2ecf20Sopenharmony_civoid dax_bus_exit(void);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/**
208c2ecf20Sopenharmony_ci * struct dax_region - mapping infrastructure for dax devices
218c2ecf20Sopenharmony_ci * @id: kernel-wide unique region for a memory range
228c2ecf20Sopenharmony_ci * @target_node: effective numa node if this memory range is onlined
238c2ecf20Sopenharmony_ci * @kref: to pin while other agents have a need to do lookups
248c2ecf20Sopenharmony_ci * @dev: parent device backing this region
258c2ecf20Sopenharmony_ci * @align: allocation and mapping alignment for child dax devices
268c2ecf20Sopenharmony_ci * @ida: instance id allocator
278c2ecf20Sopenharmony_ci * @res: resource tree to track instance allocations
288c2ecf20Sopenharmony_ci * @seed: allow userspace to find the first unbound seed device
298c2ecf20Sopenharmony_ci * @youngest: allow userspace to find the most recently created device
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_cistruct dax_region {
328c2ecf20Sopenharmony_ci	int id;
338c2ecf20Sopenharmony_ci	int target_node;
348c2ecf20Sopenharmony_ci	struct kref kref;
358c2ecf20Sopenharmony_ci	struct device *dev;
368c2ecf20Sopenharmony_ci	unsigned int align;
378c2ecf20Sopenharmony_ci	struct ida ida;
388c2ecf20Sopenharmony_ci	struct resource res;
398c2ecf20Sopenharmony_ci	struct device *seed;
408c2ecf20Sopenharmony_ci	struct device *youngest;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistruct dax_mapping {
448c2ecf20Sopenharmony_ci	struct device dev;
458c2ecf20Sopenharmony_ci	int range_id;
468c2ecf20Sopenharmony_ci	int id;
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/**
508c2ecf20Sopenharmony_ci * struct dev_dax - instance data for a subdivision of a dax region, and
518c2ecf20Sopenharmony_ci * data while the device is activated in the driver.
528c2ecf20Sopenharmony_ci * @region - parent region
538c2ecf20Sopenharmony_ci * @dax_dev - core dax functionality
548c2ecf20Sopenharmony_ci * @target_node: effective numa node if dev_dax memory range is onlined
558c2ecf20Sopenharmony_ci * @dyn_id: is this a dynamic or statically created instance
568c2ecf20Sopenharmony_ci * @id: ida allocated id when the dax_region is not static
578c2ecf20Sopenharmony_ci * @ida: mapping id allocator
588c2ecf20Sopenharmony_ci * @dev - device core
598c2ecf20Sopenharmony_ci * @pgmap - pgmap for memmap setup / lifetime (driver owned)
608c2ecf20Sopenharmony_ci * @nr_range: size of @ranges
618c2ecf20Sopenharmony_ci * @ranges: resource-span + pgoff tuples for the instance
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_cistruct dev_dax {
648c2ecf20Sopenharmony_ci	struct dax_region *region;
658c2ecf20Sopenharmony_ci	struct dax_device *dax_dev;
668c2ecf20Sopenharmony_ci	unsigned int align;
678c2ecf20Sopenharmony_ci	int target_node;
688c2ecf20Sopenharmony_ci	bool dyn_id;
698c2ecf20Sopenharmony_ci	int id;
708c2ecf20Sopenharmony_ci	struct ida ida;
718c2ecf20Sopenharmony_ci	struct device dev;
728c2ecf20Sopenharmony_ci	struct dev_pagemap *pgmap;
738c2ecf20Sopenharmony_ci	int nr_range;
748c2ecf20Sopenharmony_ci	struct dev_dax_range {
758c2ecf20Sopenharmony_ci		unsigned long pgoff;
768c2ecf20Sopenharmony_ci		struct range range;
778c2ecf20Sopenharmony_ci		struct dax_mapping *mapping;
788c2ecf20Sopenharmony_ci	} *ranges;
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic inline struct dev_dax *to_dev_dax(struct device *dev)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	return container_of(dev, struct dev_dax, dev);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic inline struct dax_mapping *to_dax_mapping(struct device *dev)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return container_of(dev, struct dax_mapping, dev);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciphys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff, unsigned long size);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
948c2ecf20Sopenharmony_cistatic inline bool dax_align_valid(unsigned long align)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	if (align == PUD_SIZE && IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
978c2ecf20Sopenharmony_ci		return true;
988c2ecf20Sopenharmony_ci	if (align == PMD_SIZE && has_transparent_hugepage())
998c2ecf20Sopenharmony_ci		return true;
1008c2ecf20Sopenharmony_ci	if (align == PAGE_SIZE)
1018c2ecf20Sopenharmony_ci		return true;
1028c2ecf20Sopenharmony_ci	return false;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci#else
1058c2ecf20Sopenharmony_cistatic inline bool dax_align_valid(unsigned long align)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	return align == PAGE_SIZE;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1108c2ecf20Sopenharmony_ci#endif
111