xref: /linux-6.15/include/linux/libnvdimm.h (revision 8d7253dc)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * libnvdimm - Non-volatile-memory Devices Subsystem
4  *
5  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
6  */
7 #ifndef __LIBNVDIMM_H__
8 #define __LIBNVDIMM_H__
9 #include <linux/kernel.h>
10 #include <linux/sizes.h>
11 #include <linux/types.h>
12 #include <linux/uuid.h>
13 #include <linux/spinlock.h>
14 #include <linux/bio.h>
15 
16 struct badrange_entry {
17 	u64 start;
18 	u64 length;
19 	struct list_head list;
20 };
21 
22 struct badrange {
23 	struct list_head list;
24 	spinlock_t lock;
25 };
26 
27 enum {
28 	/* unarmed memory devices may not persist writes */
29 	NDD_UNARMED = 1,
30 	/* locked memory devices should not be accessed */
31 	NDD_LOCKED = 2,
32 	/* memory under security wipes should not be accessed */
33 	NDD_SECURITY_OVERWRITE = 3,
34 	/*  tracking whether or not there is a pending device reference */
35 	NDD_WORK_PENDING = 4,
36 	/* dimm supports namespace labels */
37 	NDD_LABELING = 6,
38 	/*
39 	 * dimm contents have changed requiring invalidation of CPU caches prior
40 	 * to activation of a region that includes this device
41 	 */
42 	NDD_INCOHERENT = 7,
43 
44 	/* need to set a limit somewhere, but yes, this is likely overkill */
45 	ND_IOCTL_MAX_BUFLEN = SZ_4M,
46 	ND_CMD_MAX_ELEM = 5,
47 	ND_CMD_MAX_ENVELOPE = 256,
48 	ND_MAX_MAPPINGS = 32,
49 
50 	/* region flag indicating to direct-map persistent memory by default */
51 	ND_REGION_PAGEMAP = 0,
52 	/*
53 	 * Platform ensures entire CPU store data path is flushed to pmem on
54 	 * system power loss.
55 	 */
56 	ND_REGION_PERSIST_CACHE = 1,
57 	/*
58 	 * Platform provides mechanisms to automatically flush outstanding
59 	 * write data from memory controler to pmem on system power loss.
60 	 * (ADR)
61 	 */
62 	ND_REGION_PERSIST_MEMCTRL = 2,
63 
64 	/* Platform provides asynchronous flush mechanism */
65 	ND_REGION_ASYNC = 3,
66 
67 	/* Region was created by CXL subsystem */
68 	ND_REGION_CXL = 4,
69 
70 	/* mark newly adjusted resources as requiring a label update */
71 	DPA_RESOURCE_ADJUSTED = 1 << 0,
72 };
73 
74 struct nvdimm;
75 struct nvdimm_bus_descriptor;
76 typedef int (*ndctl_fn)(struct nvdimm_bus_descriptor *nd_desc,
77 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
78 		unsigned int buf_len, int *cmd_rc);
79 
80 struct device_node;
81 struct nvdimm_bus_descriptor {
82 	const struct attribute_group **attr_groups;
83 	unsigned long cmd_mask;
84 	unsigned long dimm_family_mask;
85 	unsigned long bus_family_mask;
86 	struct module *module;
87 	char *provider_name;
88 	struct device_node *of_node;
89 	ndctl_fn ndctl;
90 	int (*flush_probe)(struct nvdimm_bus_descriptor *nd_desc);
91 	int (*clear_to_send)(struct nvdimm_bus_descriptor *nd_desc,
92 			struct nvdimm *nvdimm, unsigned int cmd, void *data);
93 	const struct nvdimm_bus_fw_ops *fw_ops;
94 };
95 
96 struct nd_cmd_desc {
97 	int in_num;
98 	int out_num;
99 	u32 in_sizes[ND_CMD_MAX_ELEM];
100 	int out_sizes[ND_CMD_MAX_ELEM];
101 };
102 
103 struct nd_interleave_set {
104 	/* v1.1 definition of the interleave-set-cookie algorithm */
105 	u64 cookie1;
106 	/* v1.2 definition of the interleave-set-cookie algorithm */
107 	u64 cookie2;
108 	/* compatibility with initial buggy Linux implementation */
109 	u64 altcookie;
110 
111 	guid_t type_guid;
112 };
113 
114 struct nd_mapping_desc {
115 	struct nvdimm *nvdimm;
116 	u64 start;
117 	u64 size;
118 	int position;
119 };
120 
121 struct nd_region;
122 struct nd_region_desc {
123 	struct resource *res;
124 	struct nd_mapping_desc *mapping;
125 	u16 num_mappings;
126 	const struct attribute_group **attr_groups;
127 	struct nd_interleave_set *nd_set;
128 	void *provider_data;
129 	int num_lanes;
130 	int numa_node;
131 	int target_node;
132 	unsigned long flags;
133 	int memregion;
134 	struct device_node *of_node;
135 	int (*flush)(struct nd_region *nd_region, struct bio *bio);
136 };
137 
138 struct device;
139 void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset,
140 		size_t size, unsigned long flags);
141 static inline void __iomem *devm_nvdimm_ioremap(struct device *dev,
142 		resource_size_t offset, size_t size)
143 {
144 	return (void __iomem *) devm_nvdimm_memremap(dev, offset, size, 0);
145 }
146 
147 struct nvdimm_bus;
148 
149 /*
150  * Note that separate bits for locked + unlocked are defined so that
151  * 'flags == 0' corresponds to an error / not-supported state.
152  */
153 enum nvdimm_security_bits {
154 	NVDIMM_SECURITY_DISABLED,
155 	NVDIMM_SECURITY_UNLOCKED,
156 	NVDIMM_SECURITY_LOCKED,
157 	NVDIMM_SECURITY_FROZEN,
158 	NVDIMM_SECURITY_OVERWRITE,
159 };
160 
161 #define NVDIMM_PASSPHRASE_LEN		32
162 #define NVDIMM_KEY_DESC_LEN		22
163 
164 struct nvdimm_key_data {
165 	u8 data[NVDIMM_PASSPHRASE_LEN];
166 };
167 
168 enum nvdimm_passphrase_type {
169 	NVDIMM_USER,
170 	NVDIMM_MASTER,
171 };
172 
173 struct nvdimm_security_ops {
174 	unsigned long (*get_flags)(struct nvdimm *nvdimm,
175 			enum nvdimm_passphrase_type pass_type);
176 	int (*freeze)(struct nvdimm *nvdimm);
177 	int (*change_key)(struct nvdimm *nvdimm,
178 			const struct nvdimm_key_data *old_data,
179 			const struct nvdimm_key_data *new_data,
180 			enum nvdimm_passphrase_type pass_type);
181 	int (*unlock)(struct nvdimm *nvdimm,
182 			const struct nvdimm_key_data *key_data);
183 	int (*disable)(struct nvdimm *nvdimm,
184 			const struct nvdimm_key_data *key_data);
185 	int (*erase)(struct nvdimm *nvdimm,
186 			const struct nvdimm_key_data *key_data,
187 			enum nvdimm_passphrase_type pass_type);
188 	int (*overwrite)(struct nvdimm *nvdimm,
189 			const struct nvdimm_key_data *key_data);
190 	int (*query_overwrite)(struct nvdimm *nvdimm);
191 	int (*disable_master)(struct nvdimm *nvdimm,
192 			      const struct nvdimm_key_data *key_data);
193 };
194 
195 enum nvdimm_fwa_state {
196 	NVDIMM_FWA_INVALID,
197 	NVDIMM_FWA_IDLE,
198 	NVDIMM_FWA_ARMED,
199 	NVDIMM_FWA_BUSY,
200 	NVDIMM_FWA_ARM_OVERFLOW,
201 };
202 
203 enum nvdimm_fwa_trigger {
204 	NVDIMM_FWA_ARM,
205 	NVDIMM_FWA_DISARM,
206 };
207 
208 enum nvdimm_fwa_capability {
209 	NVDIMM_FWA_CAP_INVALID,
210 	NVDIMM_FWA_CAP_NONE,
211 	NVDIMM_FWA_CAP_QUIESCE,
212 	NVDIMM_FWA_CAP_LIVE,
213 };
214 
215 enum nvdimm_fwa_result {
216 	NVDIMM_FWA_RESULT_INVALID,
217 	NVDIMM_FWA_RESULT_NONE,
218 	NVDIMM_FWA_RESULT_SUCCESS,
219 	NVDIMM_FWA_RESULT_NOTSTAGED,
220 	NVDIMM_FWA_RESULT_NEEDRESET,
221 	NVDIMM_FWA_RESULT_FAIL,
222 };
223 
224 struct nvdimm_bus_fw_ops {
225 	enum nvdimm_fwa_state (*activate_state)
226 		(struct nvdimm_bus_descriptor *nd_desc);
227 	enum nvdimm_fwa_capability (*capability)
228 		(struct nvdimm_bus_descriptor *nd_desc);
229 	int (*activate)(struct nvdimm_bus_descriptor *nd_desc);
230 };
231 
232 struct nvdimm_fw_ops {
233 	enum nvdimm_fwa_state (*activate_state)(struct nvdimm *nvdimm);
234 	enum nvdimm_fwa_result (*activate_result)(struct nvdimm *nvdimm);
235 	int (*arm)(struct nvdimm *nvdimm, enum nvdimm_fwa_trigger arg);
236 };
237 
238 void badrange_init(struct badrange *badrange);
239 int badrange_add(struct badrange *badrange, u64 addr, u64 length);
240 void badrange_forget(struct badrange *badrange, phys_addr_t start,
241 		unsigned int len);
242 int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr,
243 		u64 length);
244 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
245 		struct nvdimm_bus_descriptor *nfit_desc);
246 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus);
247 struct nvdimm_bus *to_nvdimm_bus(struct device *dev);
248 struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm);
249 struct nvdimm *to_nvdimm(struct device *dev);
250 struct nd_region *to_nd_region(struct device *dev);
251 struct device *nd_region_dev(struct nd_region *nd_region);
252 struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus);
253 struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus);
254 const char *nvdimm_name(struct nvdimm *nvdimm);
255 struct kobject *nvdimm_kobj(struct nvdimm *nvdimm);
256 unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm);
257 void *nvdimm_provider_data(struct nvdimm *nvdimm);
258 struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
259 		void *provider_data, const struct attribute_group **groups,
260 		unsigned long flags, unsigned long cmd_mask, int num_flush,
261 		struct resource *flush_wpq, const char *dimm_id,
262 		const struct nvdimm_security_ops *sec_ops,
263 		const struct nvdimm_fw_ops *fw_ops);
264 static inline struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus,
265 		void *provider_data, const struct attribute_group **groups,
266 		unsigned long flags, unsigned long cmd_mask, int num_flush,
267 		struct resource *flush_wpq)
268 {
269 	return __nvdimm_create(nvdimm_bus, provider_data, groups, flags,
270 			cmd_mask, num_flush, flush_wpq, NULL, NULL, NULL);
271 }
272 void nvdimm_delete(struct nvdimm *nvdimm);
273 void nvdimm_region_delete(struct nd_region *nd_region);
274 
275 const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd);
276 const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd);
277 u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
278 		const struct nd_cmd_desc *desc, int idx, void *buf);
279 u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
280 		const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
281 		const u32 *out_field, unsigned long remainder);
282 int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count);
283 struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
284 		struct nd_region_desc *ndr_desc);
285 struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
286 		struct nd_region_desc *ndr_desc);
287 struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
288 		struct nd_region_desc *ndr_desc);
289 void *nd_region_provider_data(struct nd_region *nd_region);
290 unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
291 void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
292 u64 nd_fletcher64(void *addr, size_t len, bool le);
293 int nvdimm_flush(struct nd_region *nd_region, struct bio *bio);
294 int generic_nvdimm_flush(struct nd_region *nd_region);
295 int nvdimm_has_flush(struct nd_region *nd_region);
296 int nvdimm_has_cache(struct nd_region *nd_region);
297 int nvdimm_in_overwrite(struct nvdimm *nvdimm);
298 bool is_nvdimm_sync(struct nd_region *nd_region);
299 
300 static inline int nvdimm_ctl(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
301 		unsigned int buf_len, int *cmd_rc)
302 {
303 	struct nvdimm_bus *nvdimm_bus = nvdimm_to_bus(nvdimm);
304 	struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
305 
306 	return nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, cmd_rc);
307 }
308 
309 #ifdef CONFIG_ARCH_HAS_PMEM_API
310 #define ARCH_MEMREMAP_PMEM MEMREMAP_WB
311 void arch_wb_cache_pmem(void *addr, size_t size);
312 void arch_invalidate_pmem(void *addr, size_t size);
313 #else
314 #define ARCH_MEMREMAP_PMEM MEMREMAP_WT
315 static inline void arch_wb_cache_pmem(void *addr, size_t size)
316 {
317 }
318 static inline void arch_invalidate_pmem(void *addr, size_t size)
319 {
320 }
321 #endif
322 
323 #endif /* __LIBNVDIMM_H__ */
324