97c1226289755b1c8a1ea01bf0fdef7281725e84
[linux-2.6-microblaze.git] / drivers / acpi / nfit / nfit.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * NVDIMM Firmware Interface Table - NFIT
4  *
5  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
6  */
7 #ifndef __NFIT_H__
8 #define __NFIT_H__
9 #include <linux/workqueue.h>
10 #include <linux/libnvdimm.h>
11 #include <linux/ndctl.h>
12 #include <linux/types.h>
13 #include <linux/acpi.h>
14 #include <acpi/acuuid.h>
15
16 /* ACPI 6.1 */
17 #define UUID_NFIT_BUS "2f10e7a4-9e91-11e4-89d3-123b93f75cba"
18
19 /* http://pmem.io/documents/NVDIMM_DSM_Interface-V1.6.pdf */
20 #define UUID_NFIT_DIMM "4309ac30-0d11-11e4-9191-0800200c9a66"
21 #define UUID_INTEL_BUS "c7d8acd4-2df8-4b82-9f65-a325335af149"
22
23 /* https://github.com/HewlettPackard/hpe-nvm/blob/master/Documentation/ */
24 #define UUID_NFIT_DIMM_N_HPE1 "9002c334-acf3-4c0e-9642-a235f0d53bc6"
25 #define UUID_NFIT_DIMM_N_HPE2 "5008664b-b758-41a0-a03c-27c2f2d04f7e"
26
27 /* https://msdn.microsoft.com/library/windows/hardware/mt604741 */
28 #define UUID_NFIT_DIMM_N_MSFT "1ee68b36-d4bd-4a1a-9a16-4f8e53d46e05"
29
30 /* http://www.uefi.org/RFIC_LIST (see "Virtual NVDIMM 0x1901") */
31 #define UUID_NFIT_DIMM_N_HYPERV "5746c5f2-a9a2-4264-ad0e-e4ddc9e09e80"
32
33 #define ACPI_NFIT_MEM_FAILED_MASK (ACPI_NFIT_MEM_SAVE_FAILED \
34                 | ACPI_NFIT_MEM_RESTORE_FAILED | ACPI_NFIT_MEM_FLUSH_FAILED \
35                 | ACPI_NFIT_MEM_NOT_ARMED | ACPI_NFIT_MEM_MAP_FAILED)
36
37 #define NVDIMM_CMD_MAX 31
38
39 #define NVDIMM_STANDARD_CMDMASK \
40 (1 << ND_CMD_SMART | 1 << ND_CMD_SMART_THRESHOLD | 1 << ND_CMD_DIMM_FLAGS \
41  | 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA \
42  | 1 << ND_CMD_SET_CONFIG_DATA | 1 << ND_CMD_VENDOR_EFFECT_LOG_SIZE \
43  | 1 << ND_CMD_VENDOR_EFFECT_LOG | 1 << ND_CMD_VENDOR)
44
45 /*
46  * Command numbers that the kernel needs to know about to handle
47  * non-default DSM revision ids
48  */
49 enum nvdimm_family_cmds {
50         NVDIMM_INTEL_LATCH_SHUTDOWN = 10,
51         NVDIMM_INTEL_GET_MODES = 11,
52         NVDIMM_INTEL_GET_FWINFO = 12,
53         NVDIMM_INTEL_START_FWUPDATE = 13,
54         NVDIMM_INTEL_SEND_FWUPDATE = 14,
55         NVDIMM_INTEL_FINISH_FWUPDATE = 15,
56         NVDIMM_INTEL_QUERY_FWUPDATE = 16,
57         NVDIMM_INTEL_SET_THRESHOLD = 17,
58         NVDIMM_INTEL_INJECT_ERROR = 18,
59         NVDIMM_INTEL_GET_SECURITY_STATE = 19,
60         NVDIMM_INTEL_SET_PASSPHRASE = 20,
61         NVDIMM_INTEL_DISABLE_PASSPHRASE = 21,
62         NVDIMM_INTEL_UNLOCK_UNIT = 22,
63         NVDIMM_INTEL_FREEZE_LOCK = 23,
64         NVDIMM_INTEL_SECURE_ERASE = 24,
65         NVDIMM_INTEL_OVERWRITE = 25,
66         NVDIMM_INTEL_QUERY_OVERWRITE = 26,
67         NVDIMM_INTEL_SET_MASTER_PASSPHRASE = 27,
68         NVDIMM_INTEL_MASTER_SECURE_ERASE = 28,
69         NVDIMM_INTEL_FW_ACTIVATE_DIMMINFO = 29,
70         NVDIMM_INTEL_FW_ACTIVATE_ARM = 30,
71 };
72
73 enum nvdimm_bus_family_cmds {
74         NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO = 1,
75         NVDIMM_BUS_INTEL_FW_ACTIVATE = 2,
76 };
77
78 #define NVDIMM_INTEL_SECURITY_CMDMASK \
79 (1 << NVDIMM_INTEL_GET_SECURITY_STATE | 1 << NVDIMM_INTEL_SET_PASSPHRASE \
80 | 1 << NVDIMM_INTEL_DISABLE_PASSPHRASE | 1 << NVDIMM_INTEL_UNLOCK_UNIT \
81 | 1 << NVDIMM_INTEL_FREEZE_LOCK | 1 << NVDIMM_INTEL_SECURE_ERASE \
82 | 1 << NVDIMM_INTEL_OVERWRITE | 1 << NVDIMM_INTEL_QUERY_OVERWRITE \
83 | 1 << NVDIMM_INTEL_SET_MASTER_PASSPHRASE \
84 | 1 << NVDIMM_INTEL_MASTER_SECURE_ERASE)
85
86 #define NVDIMM_INTEL_FW_ACTIVATE_CMDMASK \
87 (1 << NVDIMM_INTEL_FW_ACTIVATE_DIMMINFO | 1 << NVDIMM_INTEL_FW_ACTIVATE_ARM)
88
89 #define NVDIMM_BUS_INTEL_FW_ACTIVATE_CMDMASK \
90 (1 << NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO | 1 << NVDIMM_BUS_INTEL_FW_ACTIVATE)
91
92 #define NVDIMM_INTEL_CMDMASK \
93 (NVDIMM_STANDARD_CMDMASK | 1 << NVDIMM_INTEL_GET_MODES \
94  | 1 << NVDIMM_INTEL_GET_FWINFO | 1 << NVDIMM_INTEL_START_FWUPDATE \
95  | 1 << NVDIMM_INTEL_SEND_FWUPDATE | 1 << NVDIMM_INTEL_FINISH_FWUPDATE \
96  | 1 << NVDIMM_INTEL_QUERY_FWUPDATE | 1 << NVDIMM_INTEL_SET_THRESHOLD \
97  | 1 << NVDIMM_INTEL_INJECT_ERROR | 1 << NVDIMM_INTEL_LATCH_SHUTDOWN \
98  | NVDIMM_INTEL_SECURITY_CMDMASK | NVDIMM_INTEL_FW_ACTIVATE_CMDMASK)
99
100 #define NVDIMM_INTEL_DENY_CMDMASK \
101 (NVDIMM_INTEL_SECURITY_CMDMASK | NVDIMM_INTEL_FW_ACTIVATE_CMDMASK)
102
103 enum nfit_uuids {
104         /* for simplicity alias the uuid index with the family id */
105         NFIT_DEV_DIMM = NVDIMM_FAMILY_INTEL,
106         NFIT_DEV_DIMM_N_HPE1 = NVDIMM_FAMILY_HPE1,
107         NFIT_DEV_DIMM_N_HPE2 = NVDIMM_FAMILY_HPE2,
108         NFIT_DEV_DIMM_N_MSFT = NVDIMM_FAMILY_MSFT,
109         NFIT_DEV_DIMM_N_HYPERV = NVDIMM_FAMILY_HYPERV,
110         /*
111          * to_nfit_bus_uuid() expects to translate bus uuid family ids
112          * to a UUID index using NVDIMM_FAMILY_MAX as an offset
113          */
114         NFIT_BUS_INTEL = NVDIMM_FAMILY_MAX + NVDIMM_BUS_FAMILY_INTEL,
115         NFIT_SPA_VOLATILE,
116         NFIT_SPA_PM,
117         NFIT_SPA_DCR,
118         NFIT_SPA_BDW,
119         NFIT_SPA_VDISK,
120         NFIT_SPA_VCD,
121         NFIT_SPA_PDISK,
122         NFIT_SPA_PCD,
123         NFIT_DEV_BUS,
124         NFIT_UUID_MAX,
125 };
126
127 /*
128  * Region format interface codes are stored with the interface as the
129  * LSB and the function as the MSB.
130  */
131 #define NFIT_FIC_BYTE cpu_to_le16(0x101) /* byte-addressable energy backed */
132 #define NFIT_FIC_BLK cpu_to_le16(0x201) /* block-addressable non-energy backed */
133 #define NFIT_FIC_BYTEN cpu_to_le16(0x301) /* byte-addressable non-energy backed */
134
135 enum {
136         NFIT_BLK_READ_FLUSH = 1,
137         NFIT_BLK_DCR_LATCH = 2,
138         NFIT_ARS_STATUS_DONE = 0,
139         NFIT_ARS_STATUS_BUSY = 1 << 16,
140         NFIT_ARS_STATUS_NONE = 2 << 16,
141         NFIT_ARS_STATUS_INTR = 3 << 16,
142         NFIT_ARS_START_BUSY = 6,
143         NFIT_ARS_CAP_NONE = 1,
144         NFIT_ARS_F_OVERFLOW = 1,
145         NFIT_ARS_TIMEOUT = 90,
146 };
147
148 enum nfit_root_notifiers {
149         NFIT_NOTIFY_UPDATE = 0x80,
150         NFIT_NOTIFY_UC_MEMORY_ERROR = 0x81,
151 };
152
153 enum nfit_dimm_notifiers {
154         NFIT_NOTIFY_DIMM_HEALTH = 0x81,
155 };
156
157 enum nfit_ars_state {
158         ARS_REQ_SHORT,
159         ARS_REQ_LONG,
160         ARS_FAILED,
161 };
162
163 struct nfit_spa {
164         struct list_head list;
165         struct nd_region *nd_region;
166         unsigned long ars_state;
167         u32 clear_err_unit;
168         u32 max_ars;
169         struct acpi_nfit_system_address spa[];
170 };
171
172 struct nfit_dcr {
173         struct list_head list;
174         struct acpi_nfit_control_region dcr[];
175 };
176
177 struct nfit_bdw {
178         struct list_head list;
179         struct acpi_nfit_data_region bdw[];
180 };
181
182 struct nfit_idt {
183         struct list_head list;
184         struct acpi_nfit_interleave idt[];
185 };
186
187 struct nfit_flush {
188         struct list_head list;
189         struct acpi_nfit_flush_address flush[];
190 };
191
192 struct nfit_memdev {
193         struct list_head list;
194         struct acpi_nfit_memory_map memdev[];
195 };
196
197 enum nfit_mem_flags {
198         NFIT_MEM_LSR,
199         NFIT_MEM_LSW,
200         NFIT_MEM_DIRTY,
201         NFIT_MEM_DIRTY_COUNT,
202 };
203
204 #define NFIT_DIMM_ID_LEN        22
205
206 /* assembled tables for a given dimm/memory-device */
207 struct nfit_mem {
208         struct nvdimm *nvdimm;
209         struct acpi_nfit_memory_map *memdev_dcr;
210         struct acpi_nfit_memory_map *memdev_pmem;
211         struct acpi_nfit_memory_map *memdev_bdw;
212         struct acpi_nfit_control_region *dcr;
213         struct acpi_nfit_data_region *bdw;
214         struct acpi_nfit_system_address *spa_dcr;
215         struct acpi_nfit_system_address *spa_bdw;
216         struct acpi_nfit_interleave *idt_dcr;
217         struct acpi_nfit_interleave *idt_bdw;
218         struct kernfs_node *flags_attr;
219         struct nfit_flush *nfit_flush;
220         struct list_head list;
221         struct acpi_device *adev;
222         struct acpi_nfit_desc *acpi_desc;
223         char id[NFIT_DIMM_ID_LEN+1];
224         struct resource *flush_wpq;
225         unsigned long dsm_mask;
226         unsigned long flags;
227         u32 dirty_shutdown;
228         int family;
229 };
230
231 enum scrub_flags {
232         ARS_BUSY,
233         ARS_CANCEL,
234         ARS_VALID,
235         ARS_POLL,
236 };
237
238 struct acpi_nfit_desc {
239         struct nvdimm_bus_descriptor nd_desc;
240         struct acpi_table_header acpi_header;
241         struct mutex init_mutex;
242         struct list_head memdevs;
243         struct list_head flushes;
244         struct list_head dimms;
245         struct list_head spas;
246         struct list_head dcrs;
247         struct list_head bdws;
248         struct list_head idts;
249         struct nvdimm_bus *nvdimm_bus;
250         struct device *dev;
251         struct nd_cmd_ars_status *ars_status;
252         struct nfit_spa *scrub_spa;
253         struct delayed_work dwork;
254         struct list_head list;
255         struct kernfs_node *scrub_count_state;
256         unsigned int max_ars;
257         unsigned int scrub_count;
258         unsigned int scrub_mode;
259         unsigned long scrub_flags;
260         unsigned long dimm_cmd_force_en;
261         unsigned long bus_cmd_force_en;
262         unsigned long bus_dsm_mask;
263         unsigned long family_dsm_mask[NVDIMM_BUS_FAMILY_MAX + 1];
264         unsigned int platform_cap;
265         unsigned int scrub_tmo;
266         int (*blk_do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
267                         void *iobuf, u64 len, int rw);
268 };
269
270 enum scrub_mode {
271         HW_ERROR_SCRUB_OFF,
272         HW_ERROR_SCRUB_ON,
273 };
274
275 enum nd_blk_mmio_selector {
276         BDW,
277         DCR,
278 };
279
280 struct nd_blk_addr {
281         union {
282                 void __iomem *base;
283                 void *aperture;
284         };
285 };
286
287 struct nfit_blk {
288         struct nfit_blk_mmio {
289                 struct nd_blk_addr addr;
290                 u64 size;
291                 u64 base_offset;
292                 u32 line_size;
293                 u32 num_lines;
294                 u32 table_size;
295                 struct acpi_nfit_interleave *idt;
296                 struct acpi_nfit_system_address *spa;
297         } mmio[2];
298         struct nd_region *nd_region;
299         u64 bdw_offset; /* post interleave offset */
300         u64 stat_offset;
301         u64 cmd_offset;
302         u32 dimm_flags;
303 };
304
305 extern struct list_head acpi_descs;
306 extern struct mutex acpi_desc_lock;
307 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
308                 enum nfit_ars_state req_type);
309
310 #ifdef CONFIG_X86_MCE
311 void nfit_mce_register(void);
312 void nfit_mce_unregister(void);
313 #else
314 static inline void nfit_mce_register(void)
315 {
316 }
317 static inline void nfit_mce_unregister(void)
318 {
319 }
320 #endif
321
322 int nfit_spa_type(struct acpi_nfit_system_address *spa);
323
324 static inline struct acpi_nfit_memory_map *__to_nfit_memdev(
325                 struct nfit_mem *nfit_mem)
326 {
327         if (nfit_mem->memdev_dcr)
328                 return nfit_mem->memdev_dcr;
329         return nfit_mem->memdev_pmem;
330 }
331
332 static inline struct acpi_nfit_desc *to_acpi_desc(
333                 struct nvdimm_bus_descriptor *nd_desc)
334 {
335         return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
336 }
337
338 #ifdef CONFIG_PROVE_LOCKING
339 static inline void nfit_device_lock(struct device *dev)
340 {
341         device_lock(dev);
342         mutex_lock(&dev->lockdep_mutex);
343 }
344
345 static inline void nfit_device_unlock(struct device *dev)
346 {
347         mutex_unlock(&dev->lockdep_mutex);
348         device_unlock(dev);
349 }
350 #else
351 static inline void nfit_device_lock(struct device *dev)
352 {
353         device_lock(dev);
354 }
355
356 static inline void nfit_device_unlock(struct device *dev)
357 {
358         device_unlock(dev);
359 }
360 #endif
361
362 const guid_t *to_nfit_uuid(enum nfit_uuids id);
363 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *nfit, acpi_size sz);
364 void acpi_nfit_shutdown(void *data);
365 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event);
366 void __acpi_nvdimm_notify(struct device *dev, u32 event);
367 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
368                 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc);
369 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev);
370 #endif /* __NFIT_H__ */