1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/list_sort.h>
6 #include <linux/libnvdimm.h>
7 #include <linux/module.h>
8 #include <linux/nospec.h>
9 #include <linux/mutex.h>
10 #include <linux/ndctl.h>
11 #include <linux/sysfs.h>
12 #include <linux/delay.h>
13 #include <linux/list.h>
14 #include <linux/acpi.h>
15 #include <linux/sort.h>
18 #include <asm/cacheflush.h>
19 #include <acpi/nfit.h>
24 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
27 #include <linux/io-64-nonatomic-hi-lo.h>
29 static bool force_enable_dimms
;
30 module_param(force_enable_dimms
, bool, S_IRUGO
|S_IWUSR
);
31 MODULE_PARM_DESC(force_enable_dimms
, "Ignore _STA (ACPI DIMM device) status");
33 static bool disable_vendor_specific
;
34 module_param(disable_vendor_specific
, bool, S_IRUGO
);
35 MODULE_PARM_DESC(disable_vendor_specific
,
36 "Limit commands to the publicly specified set");
38 static unsigned long override_dsm_mask
;
39 module_param(override_dsm_mask
, ulong
, S_IRUGO
);
40 MODULE_PARM_DESC(override_dsm_mask
, "Bitmask of allowed NVDIMM DSM functions");
42 static int default_dsm_family
= -1;
43 module_param(default_dsm_family
, int, S_IRUGO
);
44 MODULE_PARM_DESC(default_dsm_family
,
45 "Try this DSM type first when identifying NVDIMM family");
47 static bool no_init_ars
;
48 module_param(no_init_ars
, bool, 0644);
49 MODULE_PARM_DESC(no_init_ars
, "Skip ARS run at nfit init time");
51 static bool force_labels
;
52 module_param(force_labels
, bool, 0444);
53 MODULE_PARM_DESC(force_labels
, "Opt-in to labels despite missing methods");
55 LIST_HEAD(acpi_descs
);
56 DEFINE_MUTEX(acpi_desc_lock
);
58 static struct workqueue_struct
*nfit_wq
;
60 struct nfit_table_prev
{
61 struct list_head spas
;
62 struct list_head memdevs
;
63 struct list_head dcrs
;
64 struct list_head bdws
;
65 struct list_head idts
;
66 struct list_head flushes
;
69 static guid_t nfit_uuid
[NFIT_UUID_MAX
];
71 const guid_t
*to_nfit_uuid(enum nfit_uuids id
)
73 return &nfit_uuid
[id
];
75 EXPORT_SYMBOL(to_nfit_uuid
);
77 static const guid_t
*to_nfit_bus_uuid(int family
)
79 if (WARN_ONCE(family
== NVDIMM_BUS_FAMILY_NFIT
,
80 "only secondary bus families can be translated\n"))
83 * The index of bus UUIDs starts immediately following the last
86 return to_nfit_uuid(family
+ NVDIMM_FAMILY_MAX
);
89 static struct acpi_device
*to_acpi_dev(struct acpi_nfit_desc
*acpi_desc
)
91 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
94 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
97 if (!nd_desc
->provider_name
98 || strcmp(nd_desc
->provider_name
, "ACPI.NFIT") != 0)
101 return to_acpi_device(acpi_desc
->dev
);
104 static int xlat_bus_status(void *buf
, unsigned int cmd
, u32 status
)
106 struct nd_cmd_clear_error
*clear_err
;
107 struct nd_cmd_ars_status
*ars_status
;
112 if ((status
& 0xffff) == NFIT_ARS_CAP_NONE
)
119 /* No supported scan types for this range */
120 flags
= ND_ARS_PERSISTENT
| ND_ARS_VOLATILE
;
121 if ((status
>> 16 & flags
) == 0)
124 case ND_CMD_ARS_START
:
125 /* ARS is in progress */
126 if ((status
& 0xffff) == NFIT_ARS_START_BUSY
)
133 case ND_CMD_ARS_STATUS
:
138 /* Check extended status (Upper two bytes) */
139 if (status
== NFIT_ARS_STATUS_DONE
)
142 /* ARS is in progress */
143 if (status
== NFIT_ARS_STATUS_BUSY
)
146 /* No ARS performed for the current boot */
147 if (status
== NFIT_ARS_STATUS_NONE
)
151 * ARS interrupted, either we overflowed or some other
152 * agent wants the scan to stop. If we didn't overflow
153 * then just continue with the returned results.
155 if (status
== NFIT_ARS_STATUS_INTR
) {
156 if (ars_status
->out_length
>= 40 && (ars_status
->flags
157 & NFIT_ARS_F_OVERFLOW
))
166 case ND_CMD_CLEAR_ERROR
:
170 if (!clear_err
->cleared
)
172 if (clear_err
->length
> clear_err
->cleared
)
173 return clear_err
->cleared
;
179 /* all other non-zero status results in an error */
185 #define ACPI_LABELS_LOCKED 3
187 static int xlat_nvdimm_status(struct nvdimm
*nvdimm
, void *buf
, unsigned int cmd
,
190 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
193 case ND_CMD_GET_CONFIG_SIZE
:
195 * In the _LSI, _LSR, _LSW case the locked status is
196 * communicated via the read/write commands
198 if (test_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
))
201 if (status
>> 16 & ND_CONFIG_LOCKED
)
204 case ND_CMD_GET_CONFIG_DATA
:
205 if (test_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
)
206 && status
== ACPI_LABELS_LOCKED
)
209 case ND_CMD_SET_CONFIG_DATA
:
210 if (test_bit(NFIT_MEM_LSW
, &nfit_mem
->flags
)
211 && status
== ACPI_LABELS_LOCKED
)
218 /* all other non-zero status results in an error */
224 static int xlat_status(struct nvdimm
*nvdimm
, void *buf
, unsigned int cmd
,
228 return xlat_bus_status(buf
, cmd
, status
);
229 return xlat_nvdimm_status(nvdimm
, buf
, cmd
, status
);
232 /* convert _LS{I,R} packages to the buffer object acpi_nfit_ctl expects */
233 static union acpi_object
*pkg_to_buf(union acpi_object
*pkg
)
238 union acpi_object
*buf
= NULL
;
240 if (pkg
->type
!= ACPI_TYPE_PACKAGE
) {
241 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
246 for (i
= 0; i
< pkg
->package
.count
; i
++) {
247 union acpi_object
*obj
= &pkg
->package
.elements
[i
];
249 if (obj
->type
== ACPI_TYPE_INTEGER
)
251 else if (obj
->type
== ACPI_TYPE_BUFFER
)
252 size
+= obj
->buffer
.length
;
254 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
260 buf
= ACPI_ALLOCATE(sizeof(*buf
) + size
);
265 buf
->type
= ACPI_TYPE_BUFFER
;
266 buf
->buffer
.length
= size
;
267 buf
->buffer
.pointer
= dst
;
268 for (i
= 0; i
< pkg
->package
.count
; i
++) {
269 union acpi_object
*obj
= &pkg
->package
.elements
[i
];
271 if (obj
->type
== ACPI_TYPE_INTEGER
) {
272 memcpy(dst
, &obj
->integer
.value
, 4);
274 } else if (obj
->type
== ACPI_TYPE_BUFFER
) {
275 memcpy(dst
, obj
->buffer
.pointer
, obj
->buffer
.length
);
276 dst
+= obj
->buffer
.length
;
284 static union acpi_object
*int_to_buf(union acpi_object
*integer
)
286 union acpi_object
*buf
= NULL
;
289 if (integer
->type
!= ACPI_TYPE_INTEGER
) {
290 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
295 buf
= ACPI_ALLOCATE(sizeof(*buf
) + 4);
300 buf
->type
= ACPI_TYPE_BUFFER
;
301 buf
->buffer
.length
= 4;
302 buf
->buffer
.pointer
= dst
;
303 memcpy(dst
, &integer
->integer
.value
, 4);
309 static union acpi_object
*acpi_label_write(acpi_handle handle
, u32 offset
,
313 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
314 struct acpi_object_list input
= {
316 .pointer
= (union acpi_object
[]) {
318 .integer
.type
= ACPI_TYPE_INTEGER
,
319 .integer
.value
= offset
,
322 .integer
.type
= ACPI_TYPE_INTEGER
,
323 .integer
.value
= len
,
326 .buffer
.type
= ACPI_TYPE_BUFFER
,
327 .buffer
.pointer
= data
,
328 .buffer
.length
= len
,
333 rc
= acpi_evaluate_object(handle
, "_LSW", &input
, &buf
);
334 if (ACPI_FAILURE(rc
))
336 return int_to_buf(buf
.pointer
);
339 static union acpi_object
*acpi_label_read(acpi_handle handle
, u32 offset
,
343 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
344 struct acpi_object_list input
= {
346 .pointer
= (union acpi_object
[]) {
348 .integer
.type
= ACPI_TYPE_INTEGER
,
349 .integer
.value
= offset
,
352 .integer
.type
= ACPI_TYPE_INTEGER
,
353 .integer
.value
= len
,
358 rc
= acpi_evaluate_object(handle
, "_LSR", &input
, &buf
);
359 if (ACPI_FAILURE(rc
))
361 return pkg_to_buf(buf
.pointer
);
364 static union acpi_object
*acpi_label_info(acpi_handle handle
)
367 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
369 rc
= acpi_evaluate_object(handle
, "_LSI", NULL
, &buf
);
370 if (ACPI_FAILURE(rc
))
372 return pkg_to_buf(buf
.pointer
);
375 static u8
nfit_dsm_revid(unsigned family
, unsigned func
)
377 static const u8 revid_table
[NVDIMM_FAMILY_MAX
+1][NVDIMM_CMD_MAX
+1] = {
378 [NVDIMM_FAMILY_INTEL
] = {
379 [NVDIMM_INTEL_GET_MODES
...
380 NVDIMM_INTEL_FW_ACTIVATE_ARM
] = 2,
385 if (family
> NVDIMM_FAMILY_MAX
)
387 if (func
> NVDIMM_CMD_MAX
)
389 id
= revid_table
[family
][func
];
391 return 1; /* default */
395 static bool payload_dumpable(struct nvdimm
*nvdimm
, unsigned int func
)
397 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
399 if (nfit_mem
&& nfit_mem
->family
== NVDIMM_FAMILY_INTEL
400 && func
>= NVDIMM_INTEL_GET_SECURITY_STATE
401 && func
<= NVDIMM_INTEL_MASTER_SECURE_ERASE
)
402 return IS_ENABLED(CONFIG_NFIT_SECURITY_DEBUG
);
406 static int cmd_to_func(struct nfit_mem
*nfit_mem
, unsigned int cmd
,
407 struct nd_cmd_pkg
*call_pkg
, int *family
)
412 if (nfit_mem
&& nfit_mem
->family
!= call_pkg
->nd_family
)
415 for (i
= 0; i
< ARRAY_SIZE(call_pkg
->nd_reserved2
); i
++)
416 if (call_pkg
->nd_reserved2
[i
])
418 *family
= call_pkg
->nd_family
;
419 return call_pkg
->nd_command
;
422 /* In the !call_pkg case, bus commands == bus functions */
426 /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
427 if (nfit_mem
->family
== NVDIMM_FAMILY_INTEL
)
431 * Force function number validation to fail since 0 is never
432 * published as a valid function in dsm_mask.
437 int acpi_nfit_ctl(struct nvdimm_bus_descriptor
*nd_desc
, struct nvdimm
*nvdimm
,
438 unsigned int cmd
, void *buf
, unsigned int buf_len
, int *cmd_rc
)
440 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
441 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
442 union acpi_object in_obj
, in_buf
, *out_obj
;
443 const struct nd_cmd_desc
*desc
= NULL
;
444 struct device
*dev
= acpi_desc
->dev
;
445 struct nd_cmd_pkg
*call_pkg
= NULL
;
446 const char *cmd_name
, *dimm_name
;
447 unsigned long cmd_mask
, dsm_mask
;
448 u32 offset
, fw_status
= 0;
457 if (cmd
== ND_CMD_CALL
)
459 func
= cmd_to_func(nfit_mem
, cmd
, call_pkg
, &family
);
464 struct acpi_device
*adev
= nfit_mem
->adev
;
469 dimm_name
= nvdimm_name(nvdimm
);
470 cmd_name
= nvdimm_cmd_name(cmd
);
471 cmd_mask
= nvdimm_cmd_mask(nvdimm
);
472 dsm_mask
= nfit_mem
->dsm_mask
;
473 desc
= nd_cmd_dimm_desc(cmd
);
474 guid
= to_nfit_uuid(nfit_mem
->family
);
475 handle
= adev
->handle
;
477 struct acpi_device
*adev
= to_acpi_dev(acpi_desc
);
479 cmd_name
= nvdimm_bus_cmd_name(cmd
);
480 cmd_mask
= nd_desc
->cmd_mask
;
481 if (cmd
== ND_CMD_CALL
&& call_pkg
->nd_family
) {
482 family
= call_pkg
->nd_family
;
483 if (family
> NVDIMM_BUS_FAMILY_MAX
||
484 !test_bit(family
, &nd_desc
->bus_family_mask
))
486 family
= array_index_nospec(family
,
487 NVDIMM_BUS_FAMILY_MAX
+ 1);
488 dsm_mask
= acpi_desc
->family_dsm_mask
[family
];
489 guid
= to_nfit_bus_uuid(family
);
491 dsm_mask
= acpi_desc
->bus_dsm_mask
;
492 guid
= to_nfit_uuid(NFIT_DEV_BUS
);
494 desc
= nd_cmd_bus_desc(cmd
);
495 handle
= adev
->handle
;
499 if (!desc
|| (cmd
&& (desc
->out_num
+ desc
->in_num
== 0)))
503 * Check for a valid command. For ND_CMD_CALL, we also have to
504 * make sure that the DSM function is supported.
506 if (cmd
== ND_CMD_CALL
&&
507 (func
> NVDIMM_CMD_MAX
|| !test_bit(func
, &dsm_mask
)))
509 else if (!test_bit(cmd
, &cmd_mask
))
512 in_obj
.type
= ACPI_TYPE_PACKAGE
;
513 in_obj
.package
.count
= 1;
514 in_obj
.package
.elements
= &in_buf
;
515 in_buf
.type
= ACPI_TYPE_BUFFER
;
516 in_buf
.buffer
.pointer
= buf
;
517 in_buf
.buffer
.length
= 0;
519 /* libnvdimm has already validated the input envelope */
520 for (i
= 0; i
< desc
->in_num
; i
++)
521 in_buf
.buffer
.length
+= nd_cmd_in_size(nvdimm
, cmd
, desc
,
525 /* skip over package wrapper */
526 in_buf
.buffer
.pointer
= (void *) &call_pkg
->nd_payload
;
527 in_buf
.buffer
.length
= call_pkg
->nd_size_in
;
530 dev_dbg(dev
, "%s cmd: %d: family: %d func: %d input length: %d\n",
531 dimm_name
, cmd
, family
, func
, in_buf
.buffer
.length
);
532 if (payload_dumpable(nvdimm
, func
))
533 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET
, 4, 4,
534 in_buf
.buffer
.pointer
,
535 min_t(u32
, 256, in_buf
.buffer
.length
), true);
537 /* call the BIOS, prefer the named methods over _DSM if available */
538 if (nvdimm
&& cmd
== ND_CMD_GET_CONFIG_SIZE
539 && test_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
))
540 out_obj
= acpi_label_info(handle
);
541 else if (nvdimm
&& cmd
== ND_CMD_GET_CONFIG_DATA
542 && test_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
)) {
543 struct nd_cmd_get_config_data_hdr
*p
= buf
;
545 out_obj
= acpi_label_read(handle
, p
->in_offset
, p
->in_length
);
546 } else if (nvdimm
&& cmd
== ND_CMD_SET_CONFIG_DATA
547 && test_bit(NFIT_MEM_LSW
, &nfit_mem
->flags
)) {
548 struct nd_cmd_set_config_hdr
*p
= buf
;
550 out_obj
= acpi_label_write(handle
, p
->in_offset
, p
->in_length
,
556 revid
= nfit_dsm_revid(nfit_mem
->family
, func
);
559 out_obj
= acpi_evaluate_dsm(handle
, guid
, revid
, func
, &in_obj
);
563 dev_dbg(dev
, "%s _DSM failed cmd: %s\n", dimm_name
, cmd_name
);
567 if (out_obj
->type
!= ACPI_TYPE_BUFFER
) {
568 dev_dbg(dev
, "%s unexpected output object type cmd: %s type: %d\n",
569 dimm_name
, cmd_name
, out_obj
->type
);
574 dev_dbg(dev
, "%s cmd: %s output length: %d\n", dimm_name
,
575 cmd_name
, out_obj
->buffer
.length
);
576 print_hex_dump_debug(cmd_name
, DUMP_PREFIX_OFFSET
, 4, 4,
577 out_obj
->buffer
.pointer
,
578 min_t(u32
, 128, out_obj
->buffer
.length
), true);
581 call_pkg
->nd_fw_size
= out_obj
->buffer
.length
;
582 memcpy(call_pkg
->nd_payload
+ call_pkg
->nd_size_in
,
583 out_obj
->buffer
.pointer
,
584 min(call_pkg
->nd_fw_size
, call_pkg
->nd_size_out
));
588 * Need to support FW function w/o known size in advance.
589 * Caller can determine required size based upon nd_fw_size.
590 * If we return an error (like elsewhere) then caller wouldn't
591 * be able to rely upon data returned to make calculation.
598 for (i
= 0, offset
= 0; i
< desc
->out_num
; i
++) {
599 u32 out_size
= nd_cmd_out_size(nvdimm
, cmd
, desc
, i
, buf
,
600 (u32
*) out_obj
->buffer
.pointer
,
601 out_obj
->buffer
.length
- offset
);
603 if (offset
+ out_size
> out_obj
->buffer
.length
) {
604 dev_dbg(dev
, "%s output object underflow cmd: %s field: %d\n",
605 dimm_name
, cmd_name
, i
);
609 if (in_buf
.buffer
.length
+ offset
+ out_size
> buf_len
) {
610 dev_dbg(dev
, "%s output overrun cmd: %s field: %d\n",
611 dimm_name
, cmd_name
, i
);
615 memcpy(buf
+ in_buf
.buffer
.length
+ offset
,
616 out_obj
->buffer
.pointer
+ offset
, out_size
);
621 * Set fw_status for all the commands with a known format to be
622 * later interpreted by xlat_status().
624 if (i
>= 1 && ((!nvdimm
&& cmd
>= ND_CMD_ARS_CAP
625 && cmd
<= ND_CMD_CLEAR_ERROR
)
626 || (nvdimm
&& cmd
>= ND_CMD_SMART
627 && cmd
<= ND_CMD_VENDOR
)))
628 fw_status
= *(u32
*) out_obj
->buffer
.pointer
;
630 if (offset
+ in_buf
.buffer
.length
< buf_len
) {
633 * status valid, return the number of bytes left
634 * unfilled in the output buffer
636 rc
= buf_len
- offset
- in_buf
.buffer
.length
;
638 *cmd_rc
= xlat_status(nvdimm
, buf
, cmd
,
641 dev_err(dev
, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
642 __func__
, dimm_name
, cmd_name
, buf_len
,
649 *cmd_rc
= xlat_status(nvdimm
, buf
, cmd
, fw_status
);
657 EXPORT_SYMBOL_GPL(acpi_nfit_ctl
);
659 static const char *spa_type_name(u16 type
)
661 static const char *to_name
[] = {
662 [NFIT_SPA_VOLATILE
] = "volatile",
663 [NFIT_SPA_PM
] = "pmem",
664 [NFIT_SPA_DCR
] = "dimm-control-region",
665 [NFIT_SPA_BDW
] = "block-data-window",
666 [NFIT_SPA_VDISK
] = "volatile-disk",
667 [NFIT_SPA_VCD
] = "volatile-cd",
668 [NFIT_SPA_PDISK
] = "persistent-disk",
669 [NFIT_SPA_PCD
] = "persistent-cd",
673 if (type
> NFIT_SPA_PCD
)
676 return to_name
[type
];
679 int nfit_spa_type(struct acpi_nfit_system_address
*spa
)
683 for (i
= 0; i
< NFIT_UUID_MAX
; i
++)
684 if (guid_equal(to_nfit_uuid(i
), (guid_t
*)&spa
->range_guid
))
689 static bool add_spa(struct acpi_nfit_desc
*acpi_desc
,
690 struct nfit_table_prev
*prev
,
691 struct acpi_nfit_system_address
*spa
)
693 struct device
*dev
= acpi_desc
->dev
;
694 struct nfit_spa
*nfit_spa
;
696 if (spa
->header
.length
!= sizeof(*spa
))
699 list_for_each_entry(nfit_spa
, &prev
->spas
, list
) {
700 if (memcmp(nfit_spa
->spa
, spa
, sizeof(*spa
)) == 0) {
701 list_move_tail(&nfit_spa
->list
, &acpi_desc
->spas
);
706 nfit_spa
= devm_kzalloc(dev
, sizeof(*nfit_spa
) + sizeof(*spa
),
710 INIT_LIST_HEAD(&nfit_spa
->list
);
711 memcpy(nfit_spa
->spa
, spa
, sizeof(*spa
));
712 list_add_tail(&nfit_spa
->list
, &acpi_desc
->spas
);
713 dev_dbg(dev
, "spa index: %d type: %s\n",
715 spa_type_name(nfit_spa_type(spa
)));
719 static bool add_memdev(struct acpi_nfit_desc
*acpi_desc
,
720 struct nfit_table_prev
*prev
,
721 struct acpi_nfit_memory_map
*memdev
)
723 struct device
*dev
= acpi_desc
->dev
;
724 struct nfit_memdev
*nfit_memdev
;
726 if (memdev
->header
.length
!= sizeof(*memdev
))
729 list_for_each_entry(nfit_memdev
, &prev
->memdevs
, list
)
730 if (memcmp(nfit_memdev
->memdev
, memdev
, sizeof(*memdev
)) == 0) {
731 list_move_tail(&nfit_memdev
->list
, &acpi_desc
->memdevs
);
735 nfit_memdev
= devm_kzalloc(dev
, sizeof(*nfit_memdev
) + sizeof(*memdev
),
739 INIT_LIST_HEAD(&nfit_memdev
->list
);
740 memcpy(nfit_memdev
->memdev
, memdev
, sizeof(*memdev
));
741 list_add_tail(&nfit_memdev
->list
, &acpi_desc
->memdevs
);
742 dev_dbg(dev
, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
743 memdev
->device_handle
, memdev
->range_index
,
744 memdev
->region_index
, memdev
->flags
);
748 int nfit_get_smbios_id(u32 device_handle
, u16
*flags
)
750 struct acpi_nfit_memory_map
*memdev
;
751 struct acpi_nfit_desc
*acpi_desc
;
752 struct nfit_mem
*nfit_mem
;
755 mutex_lock(&acpi_desc_lock
);
756 list_for_each_entry(acpi_desc
, &acpi_descs
, list
) {
757 mutex_lock(&acpi_desc
->init_mutex
);
758 list_for_each_entry(nfit_mem
, &acpi_desc
->dimms
, list
) {
759 memdev
= __to_nfit_memdev(nfit_mem
);
760 if (memdev
->device_handle
== device_handle
) {
761 *flags
= memdev
->flags
;
762 physical_id
= memdev
->physical_id
;
763 mutex_unlock(&acpi_desc
->init_mutex
);
764 mutex_unlock(&acpi_desc_lock
);
768 mutex_unlock(&acpi_desc
->init_mutex
);
770 mutex_unlock(&acpi_desc_lock
);
774 EXPORT_SYMBOL_GPL(nfit_get_smbios_id
);
777 * An implementation may provide a truncated control region if no block windows
780 static size_t sizeof_dcr(struct acpi_nfit_control_region
*dcr
)
782 if (dcr
->header
.length
< offsetof(struct acpi_nfit_control_region
,
787 return offsetof(struct acpi_nfit_control_region
, window_size
);
790 static bool add_dcr(struct acpi_nfit_desc
*acpi_desc
,
791 struct nfit_table_prev
*prev
,
792 struct acpi_nfit_control_region
*dcr
)
794 struct device
*dev
= acpi_desc
->dev
;
795 struct nfit_dcr
*nfit_dcr
;
797 if (!sizeof_dcr(dcr
))
800 list_for_each_entry(nfit_dcr
, &prev
->dcrs
, list
)
801 if (memcmp(nfit_dcr
->dcr
, dcr
, sizeof_dcr(dcr
)) == 0) {
802 list_move_tail(&nfit_dcr
->list
, &acpi_desc
->dcrs
);
806 nfit_dcr
= devm_kzalloc(dev
, sizeof(*nfit_dcr
) + sizeof(*dcr
),
810 INIT_LIST_HEAD(&nfit_dcr
->list
);
811 memcpy(nfit_dcr
->dcr
, dcr
, sizeof_dcr(dcr
));
812 list_add_tail(&nfit_dcr
->list
, &acpi_desc
->dcrs
);
813 dev_dbg(dev
, "dcr index: %d windows: %d\n",
814 dcr
->region_index
, dcr
->windows
);
818 static bool add_bdw(struct acpi_nfit_desc
*acpi_desc
,
819 struct nfit_table_prev
*prev
,
820 struct acpi_nfit_data_region
*bdw
)
822 struct device
*dev
= acpi_desc
->dev
;
823 struct nfit_bdw
*nfit_bdw
;
825 if (bdw
->header
.length
!= sizeof(*bdw
))
827 list_for_each_entry(nfit_bdw
, &prev
->bdws
, list
)
828 if (memcmp(nfit_bdw
->bdw
, bdw
, sizeof(*bdw
)) == 0) {
829 list_move_tail(&nfit_bdw
->list
, &acpi_desc
->bdws
);
833 nfit_bdw
= devm_kzalloc(dev
, sizeof(*nfit_bdw
) + sizeof(*bdw
),
837 INIT_LIST_HEAD(&nfit_bdw
->list
);
838 memcpy(nfit_bdw
->bdw
, bdw
, sizeof(*bdw
));
839 list_add_tail(&nfit_bdw
->list
, &acpi_desc
->bdws
);
840 dev_dbg(dev
, "bdw dcr: %d windows: %d\n",
841 bdw
->region_index
, bdw
->windows
);
845 static size_t sizeof_idt(struct acpi_nfit_interleave
*idt
)
847 if (idt
->header
.length
< sizeof(*idt
))
849 return sizeof(*idt
) + sizeof(u32
) * (idt
->line_count
- 1);
852 static bool add_idt(struct acpi_nfit_desc
*acpi_desc
,
853 struct nfit_table_prev
*prev
,
854 struct acpi_nfit_interleave
*idt
)
856 struct device
*dev
= acpi_desc
->dev
;
857 struct nfit_idt
*nfit_idt
;
859 if (!sizeof_idt(idt
))
862 list_for_each_entry(nfit_idt
, &prev
->idts
, list
) {
863 if (sizeof_idt(nfit_idt
->idt
) != sizeof_idt(idt
))
866 if (memcmp(nfit_idt
->idt
, idt
, sizeof_idt(idt
)) == 0) {
867 list_move_tail(&nfit_idt
->list
, &acpi_desc
->idts
);
872 nfit_idt
= devm_kzalloc(dev
, sizeof(*nfit_idt
) + sizeof_idt(idt
),
876 INIT_LIST_HEAD(&nfit_idt
->list
);
877 memcpy(nfit_idt
->idt
, idt
, sizeof_idt(idt
));
878 list_add_tail(&nfit_idt
->list
, &acpi_desc
->idts
);
879 dev_dbg(dev
, "idt index: %d num_lines: %d\n",
880 idt
->interleave_index
, idt
->line_count
);
884 static size_t sizeof_flush(struct acpi_nfit_flush_address
*flush
)
886 if (flush
->header
.length
< sizeof(*flush
))
888 return sizeof(*flush
) + sizeof(u64
) * (flush
->hint_count
- 1);
891 static bool add_flush(struct acpi_nfit_desc
*acpi_desc
,
892 struct nfit_table_prev
*prev
,
893 struct acpi_nfit_flush_address
*flush
)
895 struct device
*dev
= acpi_desc
->dev
;
896 struct nfit_flush
*nfit_flush
;
898 if (!sizeof_flush(flush
))
901 list_for_each_entry(nfit_flush
, &prev
->flushes
, list
) {
902 if (sizeof_flush(nfit_flush
->flush
) != sizeof_flush(flush
))
905 if (memcmp(nfit_flush
->flush
, flush
,
906 sizeof_flush(flush
)) == 0) {
907 list_move_tail(&nfit_flush
->list
, &acpi_desc
->flushes
);
912 nfit_flush
= devm_kzalloc(dev
, sizeof(*nfit_flush
)
913 + sizeof_flush(flush
), GFP_KERNEL
);
916 INIT_LIST_HEAD(&nfit_flush
->list
);
917 memcpy(nfit_flush
->flush
, flush
, sizeof_flush(flush
));
918 list_add_tail(&nfit_flush
->list
, &acpi_desc
->flushes
);
919 dev_dbg(dev
, "nfit_flush handle: %d hint_count: %d\n",
920 flush
->device_handle
, flush
->hint_count
);
924 static bool add_platform_cap(struct acpi_nfit_desc
*acpi_desc
,
925 struct acpi_nfit_capabilities
*pcap
)
927 struct device
*dev
= acpi_desc
->dev
;
930 mask
= (1 << (pcap
->highest_capability
+ 1)) - 1;
931 acpi_desc
->platform_cap
= pcap
->capabilities
& mask
;
932 dev_dbg(dev
, "cap: %#x\n", acpi_desc
->platform_cap
);
936 static void *add_table(struct acpi_nfit_desc
*acpi_desc
,
937 struct nfit_table_prev
*prev
, void *table
, const void *end
)
939 struct device
*dev
= acpi_desc
->dev
;
940 struct acpi_nfit_header
*hdr
;
941 void *err
= ERR_PTR(-ENOMEM
);
948 dev_warn(dev
, "found a zero length table '%d' parsing nfit\n",
954 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS
:
955 if (!add_spa(acpi_desc
, prev
, table
))
958 case ACPI_NFIT_TYPE_MEMORY_MAP
:
959 if (!add_memdev(acpi_desc
, prev
, table
))
962 case ACPI_NFIT_TYPE_CONTROL_REGION
:
963 if (!add_dcr(acpi_desc
, prev
, table
))
966 case ACPI_NFIT_TYPE_DATA_REGION
:
967 if (!add_bdw(acpi_desc
, prev
, table
))
970 case ACPI_NFIT_TYPE_INTERLEAVE
:
971 if (!add_idt(acpi_desc
, prev
, table
))
974 case ACPI_NFIT_TYPE_FLUSH_ADDRESS
:
975 if (!add_flush(acpi_desc
, prev
, table
))
978 case ACPI_NFIT_TYPE_SMBIOS
:
979 dev_dbg(dev
, "smbios\n");
981 case ACPI_NFIT_TYPE_CAPABILITIES
:
982 if (!add_platform_cap(acpi_desc
, table
))
986 dev_err(dev
, "unknown table '%d' parsing nfit\n", hdr
->type
);
990 return table
+ hdr
->length
;
993 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc
*acpi_desc
,
994 struct nfit_mem
*nfit_mem
)
996 u32 device_handle
= __to_nfit_memdev(nfit_mem
)->device_handle
;
997 u16 dcr
= nfit_mem
->dcr
->region_index
;
998 struct nfit_spa
*nfit_spa
;
1000 list_for_each_entry(nfit_spa
, &acpi_desc
->spas
, list
) {
1001 u16 range_index
= nfit_spa
->spa
->range_index
;
1002 int type
= nfit_spa_type(nfit_spa
->spa
);
1003 struct nfit_memdev
*nfit_memdev
;
1005 if (type
!= NFIT_SPA_BDW
)
1008 list_for_each_entry(nfit_memdev
, &acpi_desc
->memdevs
, list
) {
1009 if (nfit_memdev
->memdev
->range_index
!= range_index
)
1011 if (nfit_memdev
->memdev
->device_handle
!= device_handle
)
1013 if (nfit_memdev
->memdev
->region_index
!= dcr
)
1016 nfit_mem
->spa_bdw
= nfit_spa
->spa
;
1021 dev_dbg(acpi_desc
->dev
, "SPA-BDW not found for SPA-DCR %d\n",
1022 nfit_mem
->spa_dcr
->range_index
);
1023 nfit_mem
->bdw
= NULL
;
1026 static void nfit_mem_init_bdw(struct acpi_nfit_desc
*acpi_desc
,
1027 struct nfit_mem
*nfit_mem
, struct acpi_nfit_system_address
*spa
)
1029 u16 dcr
= __to_nfit_memdev(nfit_mem
)->region_index
;
1030 struct nfit_memdev
*nfit_memdev
;
1031 struct nfit_bdw
*nfit_bdw
;
1032 struct nfit_idt
*nfit_idt
;
1033 u16 idt_idx
, range_index
;
1035 list_for_each_entry(nfit_bdw
, &acpi_desc
->bdws
, list
) {
1036 if (nfit_bdw
->bdw
->region_index
!= dcr
)
1038 nfit_mem
->bdw
= nfit_bdw
->bdw
;
1045 nfit_mem_find_spa_bdw(acpi_desc
, nfit_mem
);
1047 if (!nfit_mem
->spa_bdw
)
1050 range_index
= nfit_mem
->spa_bdw
->range_index
;
1051 list_for_each_entry(nfit_memdev
, &acpi_desc
->memdevs
, list
) {
1052 if (nfit_memdev
->memdev
->range_index
!= range_index
||
1053 nfit_memdev
->memdev
->region_index
!= dcr
)
1055 nfit_mem
->memdev_bdw
= nfit_memdev
->memdev
;
1056 idt_idx
= nfit_memdev
->memdev
->interleave_index
;
1057 list_for_each_entry(nfit_idt
, &acpi_desc
->idts
, list
) {
1058 if (nfit_idt
->idt
->interleave_index
!= idt_idx
)
1060 nfit_mem
->idt_bdw
= nfit_idt
->idt
;
1067 static int __nfit_mem_init(struct acpi_nfit_desc
*acpi_desc
,
1068 struct acpi_nfit_system_address
*spa
)
1070 struct nfit_mem
*nfit_mem
, *found
;
1071 struct nfit_memdev
*nfit_memdev
;
1072 int type
= spa
? nfit_spa_type(spa
) : 0;
1084 * This loop runs in two modes, when a dimm is mapped the loop
1085 * adds memdev associations to an existing dimm, or creates a
1086 * dimm. In the unmapped dimm case this loop sweeps for memdev
1087 * instances with an invalid / zero range_index and adds those
1088 * dimms without spa associations.
1090 list_for_each_entry(nfit_memdev
, &acpi_desc
->memdevs
, list
) {
1091 struct nfit_flush
*nfit_flush
;
1092 struct nfit_dcr
*nfit_dcr
;
1096 if (spa
&& nfit_memdev
->memdev
->range_index
!= spa
->range_index
)
1098 if (!spa
&& nfit_memdev
->memdev
->range_index
)
1101 dcr
= nfit_memdev
->memdev
->region_index
;
1102 device_handle
= nfit_memdev
->memdev
->device_handle
;
1103 list_for_each_entry(nfit_mem
, &acpi_desc
->dimms
, list
)
1104 if (__to_nfit_memdev(nfit_mem
)->device_handle
1113 nfit_mem
= devm_kzalloc(acpi_desc
->dev
,
1114 sizeof(*nfit_mem
), GFP_KERNEL
);
1117 INIT_LIST_HEAD(&nfit_mem
->list
);
1118 nfit_mem
->acpi_desc
= acpi_desc
;
1119 list_add(&nfit_mem
->list
, &acpi_desc
->dimms
);
1122 list_for_each_entry(nfit_dcr
, &acpi_desc
->dcrs
, list
) {
1123 if (nfit_dcr
->dcr
->region_index
!= dcr
)
1126 * Record the control region for the dimm. For
1127 * the ACPI 6.1 case, where there are separate
1128 * control regions for the pmem vs blk
1129 * interfaces, be sure to record the extended
1133 nfit_mem
->dcr
= nfit_dcr
->dcr
;
1134 else if (nfit_mem
->dcr
->windows
== 0
1135 && nfit_dcr
->dcr
->windows
)
1136 nfit_mem
->dcr
= nfit_dcr
->dcr
;
1140 list_for_each_entry(nfit_flush
, &acpi_desc
->flushes
, list
) {
1141 struct acpi_nfit_flush_address
*flush
;
1144 if (nfit_flush
->flush
->device_handle
!= device_handle
)
1146 nfit_mem
->nfit_flush
= nfit_flush
;
1147 flush
= nfit_flush
->flush
;
1148 nfit_mem
->flush_wpq
= devm_kcalloc(acpi_desc
->dev
,
1150 sizeof(struct resource
),
1152 if (!nfit_mem
->flush_wpq
)
1154 for (i
= 0; i
< flush
->hint_count
; i
++) {
1155 struct resource
*res
= &nfit_mem
->flush_wpq
[i
];
1157 res
->start
= flush
->hint_address
[i
];
1158 res
->end
= res
->start
+ 8 - 1;
1163 if (dcr
&& !nfit_mem
->dcr
) {
1164 dev_err(acpi_desc
->dev
, "SPA %d missing DCR %d\n",
1165 spa
->range_index
, dcr
);
1169 if (type
== NFIT_SPA_DCR
) {
1170 struct nfit_idt
*nfit_idt
;
1173 /* multiple dimms may share a SPA when interleaved */
1174 nfit_mem
->spa_dcr
= spa
;
1175 nfit_mem
->memdev_dcr
= nfit_memdev
->memdev
;
1176 idt_idx
= nfit_memdev
->memdev
->interleave_index
;
1177 list_for_each_entry(nfit_idt
, &acpi_desc
->idts
, list
) {
1178 if (nfit_idt
->idt
->interleave_index
!= idt_idx
)
1180 nfit_mem
->idt_dcr
= nfit_idt
->idt
;
1183 nfit_mem_init_bdw(acpi_desc
, nfit_mem
, spa
);
1184 } else if (type
== NFIT_SPA_PM
) {
1186 * A single dimm may belong to multiple SPA-PM
1187 * ranges, record at least one in addition to
1188 * any SPA-DCR range.
1190 nfit_mem
->memdev_pmem
= nfit_memdev
->memdev
;
1192 nfit_mem
->memdev_dcr
= nfit_memdev
->memdev
;
1198 static int nfit_mem_cmp(void *priv
, struct list_head
*_a
, struct list_head
*_b
)
1200 struct nfit_mem
*a
= container_of(_a
, typeof(*a
), list
);
1201 struct nfit_mem
*b
= container_of(_b
, typeof(*b
), list
);
1202 u32 handleA
, handleB
;
1204 handleA
= __to_nfit_memdev(a
)->device_handle
;
1205 handleB
= __to_nfit_memdev(b
)->device_handle
;
1206 if (handleA
< handleB
)
1208 else if (handleA
> handleB
)
1213 static int nfit_mem_init(struct acpi_nfit_desc
*acpi_desc
)
1215 struct nfit_spa
*nfit_spa
;
1220 * For each SPA-DCR or SPA-PMEM address range find its
1221 * corresponding MEMDEV(s). From each MEMDEV find the
1222 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1223 * try to find a SPA-BDW and a corresponding BDW that references
1224 * the DCR. Throw it all into an nfit_mem object. Note, that
1225 * BDWs are optional.
1227 list_for_each_entry(nfit_spa
, &acpi_desc
->spas
, list
) {
1228 rc
= __nfit_mem_init(acpi_desc
, nfit_spa
->spa
);
1234 * If a DIMM has failed to be mapped into SPA there will be no
1235 * SPA entries above. Find and register all the unmapped DIMMs
1236 * for reporting and recovery purposes.
1238 rc
= __nfit_mem_init(acpi_desc
, NULL
);
1242 list_sort(NULL
, &acpi_desc
->dimms
, nfit_mem_cmp
);
1247 static ssize_t
bus_dsm_mask_show(struct device
*dev
,
1248 struct device_attribute
*attr
, char *buf
)
1250 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
1251 struct nvdimm_bus_descriptor
*nd_desc
= to_nd_desc(nvdimm_bus
);
1252 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
1254 return sprintf(buf
, "%#lx\n", acpi_desc
->bus_dsm_mask
);
1256 static struct device_attribute dev_attr_bus_dsm_mask
=
1257 __ATTR(dsm_mask
, 0444, bus_dsm_mask_show
, NULL
);
1259 static ssize_t
revision_show(struct device
*dev
,
1260 struct device_attribute
*attr
, char *buf
)
1262 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
1263 struct nvdimm_bus_descriptor
*nd_desc
= to_nd_desc(nvdimm_bus
);
1264 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
1266 return sprintf(buf
, "%d\n", acpi_desc
->acpi_header
.revision
);
1268 static DEVICE_ATTR_RO(revision
);
1270 static ssize_t
hw_error_scrub_show(struct device
*dev
,
1271 struct device_attribute
*attr
, char *buf
)
1273 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
1274 struct nvdimm_bus_descriptor
*nd_desc
= to_nd_desc(nvdimm_bus
);
1275 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
1277 return sprintf(buf
, "%d\n", acpi_desc
->scrub_mode
);
1281 * The 'hw_error_scrub' attribute can have the following values written to it:
1282 * '0': Switch to the default mode where an exception will only insert
1283 * the address of the memory error into the poison and badblocks lists.
1284 * '1': Enable a full scrub to happen if an exception for a memory error is
1287 static ssize_t
hw_error_scrub_store(struct device
*dev
,
1288 struct device_attribute
*attr
, const char *buf
, size_t size
)
1290 struct nvdimm_bus_descriptor
*nd_desc
;
1294 rc
= kstrtol(buf
, 0, &val
);
1298 nfit_device_lock(dev
);
1299 nd_desc
= dev_get_drvdata(dev
);
1301 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
1304 case HW_ERROR_SCRUB_ON
:
1305 acpi_desc
->scrub_mode
= HW_ERROR_SCRUB_ON
;
1307 case HW_ERROR_SCRUB_OFF
:
1308 acpi_desc
->scrub_mode
= HW_ERROR_SCRUB_OFF
;
1315 nfit_device_unlock(dev
);
1320 static DEVICE_ATTR_RW(hw_error_scrub
);
1323 * This shows the number of full Address Range Scrubs that have been
1324 * completed since driver load time. Userspace can wait on this using
1325 * select/poll etc. A '+' at the end indicates an ARS is in progress
1327 static ssize_t
scrub_show(struct device
*dev
,
1328 struct device_attribute
*attr
, char *buf
)
1330 struct nvdimm_bus_descriptor
*nd_desc
;
1331 struct acpi_nfit_desc
*acpi_desc
;
1332 ssize_t rc
= -ENXIO
;
1335 nfit_device_lock(dev
);
1336 nd_desc
= dev_get_drvdata(dev
);
1338 nfit_device_unlock(dev
);
1341 acpi_desc
= to_acpi_desc(nd_desc
);
1343 mutex_lock(&acpi_desc
->init_mutex
);
1344 busy
= test_bit(ARS_BUSY
, &acpi_desc
->scrub_flags
)
1345 && !test_bit(ARS_CANCEL
, &acpi_desc
->scrub_flags
);
1346 rc
= sprintf(buf
, "%d%s", acpi_desc
->scrub_count
, busy
? "+\n" : "\n");
1347 /* Allow an admin to poll the busy state at a higher rate */
1348 if (busy
&& capable(CAP_SYS_RAWIO
) && !test_and_set_bit(ARS_POLL
,
1349 &acpi_desc
->scrub_flags
)) {
1350 acpi_desc
->scrub_tmo
= 1;
1351 mod_delayed_work(nfit_wq
, &acpi_desc
->dwork
, HZ
);
1354 mutex_unlock(&acpi_desc
->init_mutex
);
1355 nfit_device_unlock(dev
);
1359 static ssize_t
scrub_store(struct device
*dev
,
1360 struct device_attribute
*attr
, const char *buf
, size_t size
)
1362 struct nvdimm_bus_descriptor
*nd_desc
;
1366 rc
= kstrtol(buf
, 0, &val
);
1372 nfit_device_lock(dev
);
1373 nd_desc
= dev_get_drvdata(dev
);
1375 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
1377 rc
= acpi_nfit_ars_rescan(acpi_desc
, ARS_REQ_LONG
);
1379 nfit_device_unlock(dev
);
1384 static DEVICE_ATTR_RW(scrub
);
1386 static bool ars_supported(struct nvdimm_bus
*nvdimm_bus
)
1388 struct nvdimm_bus_descriptor
*nd_desc
= to_nd_desc(nvdimm_bus
);
1389 const unsigned long mask
= 1 << ND_CMD_ARS_CAP
| 1 << ND_CMD_ARS_START
1390 | 1 << ND_CMD_ARS_STATUS
;
1392 return (nd_desc
->cmd_mask
& mask
) == mask
;
1395 static umode_t
nfit_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
1397 struct device
*dev
= kobj_to_dev(kobj
);
1398 struct nvdimm_bus
*nvdimm_bus
= to_nvdimm_bus(dev
);
1400 if (a
== &dev_attr_scrub
.attr
)
1401 return ars_supported(nvdimm_bus
) ? a
->mode
: 0;
1403 if (a
== &dev_attr_firmware_activate_noidle
.attr
)
1404 return intel_fwa_supported(nvdimm_bus
) ? a
->mode
: 0;
1409 static struct attribute
*acpi_nfit_attributes
[] = {
1410 &dev_attr_revision
.attr
,
1411 &dev_attr_scrub
.attr
,
1412 &dev_attr_hw_error_scrub
.attr
,
1413 &dev_attr_bus_dsm_mask
.attr
,
1414 &dev_attr_firmware_activate_noidle
.attr
,
1418 static const struct attribute_group acpi_nfit_attribute_group
= {
1420 .attrs
= acpi_nfit_attributes
,
1421 .is_visible
= nfit_visible
,
1424 static const struct attribute_group
*acpi_nfit_attribute_groups
[] = {
1425 &acpi_nfit_attribute_group
,
1429 static struct acpi_nfit_memory_map
*to_nfit_memdev(struct device
*dev
)
1431 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1432 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1434 return __to_nfit_memdev(nfit_mem
);
1437 static struct acpi_nfit_control_region
*to_nfit_dcr(struct device
*dev
)
1439 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1440 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1442 return nfit_mem
->dcr
;
1445 static ssize_t
handle_show(struct device
*dev
,
1446 struct device_attribute
*attr
, char *buf
)
1448 struct acpi_nfit_memory_map
*memdev
= to_nfit_memdev(dev
);
1450 return sprintf(buf
, "%#x\n", memdev
->device_handle
);
1452 static DEVICE_ATTR_RO(handle
);
1454 static ssize_t
phys_id_show(struct device
*dev
,
1455 struct device_attribute
*attr
, char *buf
)
1457 struct acpi_nfit_memory_map
*memdev
= to_nfit_memdev(dev
);
1459 return sprintf(buf
, "%#x\n", memdev
->physical_id
);
1461 static DEVICE_ATTR_RO(phys_id
);
1463 static ssize_t
vendor_show(struct device
*dev
,
1464 struct device_attribute
*attr
, char *buf
)
1466 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1468 return sprintf(buf
, "0x%04x\n", be16_to_cpu(dcr
->vendor_id
));
1470 static DEVICE_ATTR_RO(vendor
);
1472 static ssize_t
rev_id_show(struct device
*dev
,
1473 struct device_attribute
*attr
, char *buf
)
1475 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1477 return sprintf(buf
, "0x%04x\n", be16_to_cpu(dcr
->revision_id
));
1479 static DEVICE_ATTR_RO(rev_id
);
1481 static ssize_t
device_show(struct device
*dev
,
1482 struct device_attribute
*attr
, char *buf
)
1484 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1486 return sprintf(buf
, "0x%04x\n", be16_to_cpu(dcr
->device_id
));
1488 static DEVICE_ATTR_RO(device
);
1490 static ssize_t
subsystem_vendor_show(struct device
*dev
,
1491 struct device_attribute
*attr
, char *buf
)
1493 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1495 return sprintf(buf
, "0x%04x\n", be16_to_cpu(dcr
->subsystem_vendor_id
));
1497 static DEVICE_ATTR_RO(subsystem_vendor
);
1499 static ssize_t
subsystem_rev_id_show(struct device
*dev
,
1500 struct device_attribute
*attr
, char *buf
)
1502 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1504 return sprintf(buf
, "0x%04x\n",
1505 be16_to_cpu(dcr
->subsystem_revision_id
));
1507 static DEVICE_ATTR_RO(subsystem_rev_id
);
1509 static ssize_t
subsystem_device_show(struct device
*dev
,
1510 struct device_attribute
*attr
, char *buf
)
1512 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1514 return sprintf(buf
, "0x%04x\n", be16_to_cpu(dcr
->subsystem_device_id
));
1516 static DEVICE_ATTR_RO(subsystem_device
);
1518 static int num_nvdimm_formats(struct nvdimm
*nvdimm
)
1520 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1523 if (nfit_mem
->memdev_pmem
)
1525 if (nfit_mem
->memdev_bdw
)
1530 static ssize_t
format_show(struct device
*dev
,
1531 struct device_attribute
*attr
, char *buf
)
1533 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1535 return sprintf(buf
, "0x%04x\n", le16_to_cpu(dcr
->code
));
1537 static DEVICE_ATTR_RO(format
);
1539 static ssize_t
format1_show(struct device
*dev
,
1540 struct device_attribute
*attr
, char *buf
)
1543 ssize_t rc
= -ENXIO
;
1544 struct nfit_mem
*nfit_mem
;
1545 struct nfit_memdev
*nfit_memdev
;
1546 struct acpi_nfit_desc
*acpi_desc
;
1547 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1548 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1550 nfit_mem
= nvdimm_provider_data(nvdimm
);
1551 acpi_desc
= nfit_mem
->acpi_desc
;
1552 handle
= to_nfit_memdev(dev
)->device_handle
;
1554 /* assumes DIMMs have at most 2 published interface codes */
1555 mutex_lock(&acpi_desc
->init_mutex
);
1556 list_for_each_entry(nfit_memdev
, &acpi_desc
->memdevs
, list
) {
1557 struct acpi_nfit_memory_map
*memdev
= nfit_memdev
->memdev
;
1558 struct nfit_dcr
*nfit_dcr
;
1560 if (memdev
->device_handle
!= handle
)
1563 list_for_each_entry(nfit_dcr
, &acpi_desc
->dcrs
, list
) {
1564 if (nfit_dcr
->dcr
->region_index
!= memdev
->region_index
)
1566 if (nfit_dcr
->dcr
->code
== dcr
->code
)
1568 rc
= sprintf(buf
, "0x%04x\n",
1569 le16_to_cpu(nfit_dcr
->dcr
->code
));
1575 mutex_unlock(&acpi_desc
->init_mutex
);
1578 static DEVICE_ATTR_RO(format1
);
1580 static ssize_t
formats_show(struct device
*dev
,
1581 struct device_attribute
*attr
, char *buf
)
1583 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1585 return sprintf(buf
, "%d\n", num_nvdimm_formats(nvdimm
));
1587 static DEVICE_ATTR_RO(formats
);
1589 static ssize_t
serial_show(struct device
*dev
,
1590 struct device_attribute
*attr
, char *buf
)
1592 struct acpi_nfit_control_region
*dcr
= to_nfit_dcr(dev
);
1594 return sprintf(buf
, "0x%08x\n", be32_to_cpu(dcr
->serial_number
));
1596 static DEVICE_ATTR_RO(serial
);
1598 static ssize_t
family_show(struct device
*dev
,
1599 struct device_attribute
*attr
, char *buf
)
1601 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1602 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1604 if (nfit_mem
->family
< 0)
1606 return sprintf(buf
, "%d\n", nfit_mem
->family
);
1608 static DEVICE_ATTR_RO(family
);
1610 static ssize_t
dsm_mask_show(struct device
*dev
,
1611 struct device_attribute
*attr
, char *buf
)
1613 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1614 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1616 if (nfit_mem
->family
< 0)
1618 return sprintf(buf
, "%#lx\n", nfit_mem
->dsm_mask
);
1620 static DEVICE_ATTR_RO(dsm_mask
);
1622 static ssize_t
flags_show(struct device
*dev
,
1623 struct device_attribute
*attr
, char *buf
)
1625 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1626 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1627 u16 flags
= __to_nfit_memdev(nfit_mem
)->flags
;
1629 if (test_bit(NFIT_MEM_DIRTY
, &nfit_mem
->flags
))
1630 flags
|= ACPI_NFIT_MEM_FLUSH_FAILED
;
1632 return sprintf(buf
, "%s%s%s%s%s%s%s\n",
1633 flags
& ACPI_NFIT_MEM_SAVE_FAILED
? "save_fail " : "",
1634 flags
& ACPI_NFIT_MEM_RESTORE_FAILED
? "restore_fail " : "",
1635 flags
& ACPI_NFIT_MEM_FLUSH_FAILED
? "flush_fail " : "",
1636 flags
& ACPI_NFIT_MEM_NOT_ARMED
? "not_armed " : "",
1637 flags
& ACPI_NFIT_MEM_HEALTH_OBSERVED
? "smart_event " : "",
1638 flags
& ACPI_NFIT_MEM_MAP_FAILED
? "map_fail " : "",
1639 flags
& ACPI_NFIT_MEM_HEALTH_ENABLED
? "smart_notify " : "");
1641 static DEVICE_ATTR_RO(flags
);
1643 static ssize_t
id_show(struct device
*dev
,
1644 struct device_attribute
*attr
, char *buf
)
1646 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1647 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1649 return sprintf(buf
, "%s\n", nfit_mem
->id
);
1651 static DEVICE_ATTR_RO(id
);
1653 static ssize_t
dirty_shutdown_show(struct device
*dev
,
1654 struct device_attribute
*attr
, char *buf
)
1656 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1657 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1659 return sprintf(buf
, "%d\n", nfit_mem
->dirty_shutdown
);
1661 static DEVICE_ATTR_RO(dirty_shutdown
);
1663 static struct attribute
*acpi_nfit_dimm_attributes
[] = {
1664 &dev_attr_handle
.attr
,
1665 &dev_attr_phys_id
.attr
,
1666 &dev_attr_vendor
.attr
,
1667 &dev_attr_device
.attr
,
1668 &dev_attr_rev_id
.attr
,
1669 &dev_attr_subsystem_vendor
.attr
,
1670 &dev_attr_subsystem_device
.attr
,
1671 &dev_attr_subsystem_rev_id
.attr
,
1672 &dev_attr_format
.attr
,
1673 &dev_attr_formats
.attr
,
1674 &dev_attr_format1
.attr
,
1675 &dev_attr_serial
.attr
,
1676 &dev_attr_flags
.attr
,
1678 &dev_attr_family
.attr
,
1679 &dev_attr_dsm_mask
.attr
,
1680 &dev_attr_dirty_shutdown
.attr
,
1684 static umode_t
acpi_nfit_dimm_attr_visible(struct kobject
*kobj
,
1685 struct attribute
*a
, int n
)
1687 struct device
*dev
= kobj_to_dev(kobj
);
1688 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
1689 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
1691 if (!to_nfit_dcr(dev
)) {
1692 /* Without a dcr only the memdev attributes can be surfaced */
1693 if (a
== &dev_attr_handle
.attr
|| a
== &dev_attr_phys_id
.attr
1694 || a
== &dev_attr_flags
.attr
1695 || a
== &dev_attr_family
.attr
1696 || a
== &dev_attr_dsm_mask
.attr
)
1701 if (a
== &dev_attr_format1
.attr
&& num_nvdimm_formats(nvdimm
) <= 1)
1704 if (!test_bit(NFIT_MEM_DIRTY_COUNT
, &nfit_mem
->flags
)
1705 && a
== &dev_attr_dirty_shutdown
.attr
)
1711 static const struct attribute_group acpi_nfit_dimm_attribute_group
= {
1713 .attrs
= acpi_nfit_dimm_attributes
,
1714 .is_visible
= acpi_nfit_dimm_attr_visible
,
1717 static const struct attribute_group
*acpi_nfit_dimm_attribute_groups
[] = {
1718 &acpi_nfit_dimm_attribute_group
,
1722 static struct nvdimm
*acpi_nfit_dimm_by_handle(struct acpi_nfit_desc
*acpi_desc
,
1725 struct nfit_mem
*nfit_mem
;
1727 list_for_each_entry(nfit_mem
, &acpi_desc
->dimms
, list
)
1728 if (__to_nfit_memdev(nfit_mem
)->device_handle
== device_handle
)
1729 return nfit_mem
->nvdimm
;
1734 void __acpi_nvdimm_notify(struct device
*dev
, u32 event
)
1736 struct nfit_mem
*nfit_mem
;
1737 struct acpi_nfit_desc
*acpi_desc
;
1739 dev_dbg(dev
->parent
, "%s: event: %d\n", dev_name(dev
),
1742 if (event
!= NFIT_NOTIFY_DIMM_HEALTH
) {
1743 dev_dbg(dev
->parent
, "%s: unknown event: %d\n", dev_name(dev
),
1748 acpi_desc
= dev_get_drvdata(dev
->parent
);
1753 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1756 nfit_mem
= dev_get_drvdata(dev
);
1757 if (nfit_mem
&& nfit_mem
->flags_attr
)
1758 sysfs_notify_dirent(nfit_mem
->flags_attr
);
1760 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify
);
1762 static void acpi_nvdimm_notify(acpi_handle handle
, u32 event
, void *data
)
1764 struct acpi_device
*adev
= data
;
1765 struct device
*dev
= &adev
->dev
;
1767 nfit_device_lock(dev
->parent
);
1768 __acpi_nvdimm_notify(dev
, event
);
1769 nfit_device_unlock(dev
->parent
);
1772 static bool acpi_nvdimm_has_method(struct acpi_device
*adev
, char *method
)
1777 status
= acpi_get_handle(adev
->handle
, method
, &handle
);
1779 if (ACPI_SUCCESS(status
))
1784 __weak
void nfit_intel_shutdown_status(struct nfit_mem
*nfit_mem
)
1786 struct device
*dev
= &nfit_mem
->adev
->dev
;
1787 struct nd_intel_smart smart
= { 0 };
1788 union acpi_object in_buf
= {
1789 .buffer
.type
= ACPI_TYPE_BUFFER
,
1792 union acpi_object in_obj
= {
1793 .package
.type
= ACPI_TYPE_PACKAGE
,
1795 .package
.elements
= &in_buf
,
1797 const u8 func
= ND_INTEL_SMART
;
1798 const guid_t
*guid
= to_nfit_uuid(nfit_mem
->family
);
1799 u8 revid
= nfit_dsm_revid(nfit_mem
->family
, func
);
1800 struct acpi_device
*adev
= nfit_mem
->adev
;
1801 acpi_handle handle
= adev
->handle
;
1802 union acpi_object
*out_obj
;
1804 if ((nfit_mem
->dsm_mask
& (1 << func
)) == 0)
1807 out_obj
= acpi_evaluate_dsm(handle
, guid
, revid
, func
, &in_obj
);
1808 if (!out_obj
|| out_obj
->type
!= ACPI_TYPE_BUFFER
1809 || out_obj
->buffer
.length
< sizeof(smart
)) {
1810 dev_dbg(dev
->parent
, "%s: failed to retrieve initial health\n",
1815 memcpy(&smart
, out_obj
->buffer
.pointer
, sizeof(smart
));
1818 if (smart
.flags
& ND_INTEL_SMART_SHUTDOWN_VALID
) {
1819 if (smart
.shutdown_state
)
1820 set_bit(NFIT_MEM_DIRTY
, &nfit_mem
->flags
);
1823 if (smart
.flags
& ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
) {
1824 set_bit(NFIT_MEM_DIRTY_COUNT
, &nfit_mem
->flags
);
1825 nfit_mem
->dirty_shutdown
= smart
.shutdown_count
;
1829 static void populate_shutdown_status(struct nfit_mem
*nfit_mem
)
1832 * For DIMMs that provide a dynamic facility to retrieve a
1833 * dirty-shutdown status and/or a dirty-shutdown count, cache
1834 * these values in nfit_mem.
1836 if (nfit_mem
->family
== NVDIMM_FAMILY_INTEL
)
1837 nfit_intel_shutdown_status(nfit_mem
);
1840 static int acpi_nfit_add_dimm(struct acpi_nfit_desc
*acpi_desc
,
1841 struct nfit_mem
*nfit_mem
, u32 device_handle
)
1843 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
1844 struct acpi_device
*adev
, *adev_dimm
;
1845 struct device
*dev
= acpi_desc
->dev
;
1846 unsigned long dsm_mask
, label_mask
;
1850 struct acpi_nfit_control_region
*dcr
= nfit_mem
->dcr
;
1852 /* nfit test assumes 1:1 relationship between commands and dsms */
1853 nfit_mem
->dsm_mask
= acpi_desc
->dimm_cmd_force_en
;
1854 nfit_mem
->family
= NVDIMM_FAMILY_INTEL
;
1855 set_bit(NVDIMM_FAMILY_INTEL
, &nd_desc
->dimm_family_mask
);
1857 if (dcr
->valid_fields
& ACPI_NFIT_CONTROL_MFG_INFO_VALID
)
1858 sprintf(nfit_mem
->id
, "%04x-%02x-%04x-%08x",
1859 be16_to_cpu(dcr
->vendor_id
),
1860 dcr
->manufacturing_location
,
1861 be16_to_cpu(dcr
->manufacturing_date
),
1862 be32_to_cpu(dcr
->serial_number
));
1864 sprintf(nfit_mem
->id
, "%04x-%08x",
1865 be16_to_cpu(dcr
->vendor_id
),
1866 be32_to_cpu(dcr
->serial_number
));
1868 adev
= to_acpi_dev(acpi_desc
);
1870 /* unit test case */
1871 populate_shutdown_status(nfit_mem
);
1875 adev_dimm
= acpi_find_child_device(adev
, device_handle
, false);
1876 nfit_mem
->adev
= adev_dimm
;
1878 dev_err(dev
, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1880 return force_enable_dimms
? 0 : -ENODEV
;
1883 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm
->handle
,
1884 ACPI_DEVICE_NOTIFY
, acpi_nvdimm_notify
, adev_dimm
))) {
1885 dev_err(dev
, "%s: notification registration failed\n",
1886 dev_name(&adev_dimm
->dev
));
1890 * Record nfit_mem for the notification path to track back to
1891 * the nfit sysfs attributes for this dimm device object.
1893 dev_set_drvdata(&adev_dimm
->dev
, nfit_mem
);
1896 * There are 4 "legacy" NVDIMM command sets
1897 * (NVDIMM_FAMILY_{INTEL,MSFT,HPE1,HPE2}) that were created before
1898 * an EFI working group was established to constrain this
1899 * proliferation. The nfit driver probes for the supported command
1900 * set by GUID. Note, if you're a platform developer looking to add
1901 * a new command set to this probe, consider using an existing set,
1902 * or otherwise seek approval to publish the command set at
1903 * http://www.uefi.org/RFIC_LIST.
1905 * Note, that checking for function0 (bit0) tells us if any commands
1906 * are reachable through this GUID.
1908 clear_bit(NVDIMM_FAMILY_INTEL
, &nd_desc
->dimm_family_mask
);
1909 for (i
= 0; i
<= NVDIMM_FAMILY_MAX
; i
++)
1910 if (acpi_check_dsm(adev_dimm
->handle
, to_nfit_uuid(i
), 1, 1)) {
1911 set_bit(i
, &nd_desc
->dimm_family_mask
);
1912 if (family
< 0 || i
== default_dsm_family
)
1916 /* limit the supported commands to those that are publicly documented */
1917 nfit_mem
->family
= family
;
1918 if (override_dsm_mask
&& !disable_vendor_specific
)
1919 dsm_mask
= override_dsm_mask
;
1920 else if (nfit_mem
->family
== NVDIMM_FAMILY_INTEL
) {
1921 dsm_mask
= NVDIMM_INTEL_CMDMASK
;
1922 if (disable_vendor_specific
)
1923 dsm_mask
&= ~(1 << ND_CMD_VENDOR
);
1924 } else if (nfit_mem
->family
== NVDIMM_FAMILY_HPE1
) {
1925 dsm_mask
= 0x1c3c76;
1926 } else if (nfit_mem
->family
== NVDIMM_FAMILY_HPE2
) {
1928 if (disable_vendor_specific
)
1929 dsm_mask
&= ~(1 << 8);
1930 } else if (nfit_mem
->family
== NVDIMM_FAMILY_MSFT
) {
1931 dsm_mask
= 0xffffffff;
1932 } else if (nfit_mem
->family
== NVDIMM_FAMILY_HYPERV
) {
1935 dev_dbg(dev
, "unknown dimm command family\n");
1936 nfit_mem
->family
= -1;
1937 /* DSMs are optional, continue loading the driver... */
1942 * Function 0 is the command interrogation function, don't
1943 * export it to potential userspace use, and enable it to be
1944 * used as an error value in acpi_nfit_ctl().
1948 guid
= to_nfit_uuid(nfit_mem
->family
);
1949 for_each_set_bit(i
, &dsm_mask
, BITS_PER_LONG
)
1950 if (acpi_check_dsm(adev_dimm
->handle
, guid
,
1951 nfit_dsm_revid(nfit_mem
->family
, i
),
1953 set_bit(i
, &nfit_mem
->dsm_mask
);
1956 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1957 * due to their better semantics handling locked capacity.
1959 label_mask
= 1 << ND_CMD_GET_CONFIG_SIZE
| 1 << ND_CMD_GET_CONFIG_DATA
1960 | 1 << ND_CMD_SET_CONFIG_DATA
;
1961 if (family
== NVDIMM_FAMILY_INTEL
1962 && (dsm_mask
& label_mask
) == label_mask
)
1963 /* skip _LS{I,R,W} enabling */;
1965 if (acpi_nvdimm_has_method(adev_dimm
, "_LSI")
1966 && acpi_nvdimm_has_method(adev_dimm
, "_LSR")) {
1967 dev_dbg(dev
, "%s: has _LSR\n", dev_name(&adev_dimm
->dev
));
1968 set_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
);
1971 if (test_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
)
1972 && acpi_nvdimm_has_method(adev_dimm
, "_LSW")) {
1973 dev_dbg(dev
, "%s: has _LSW\n", dev_name(&adev_dimm
->dev
));
1974 set_bit(NFIT_MEM_LSW
, &nfit_mem
->flags
);
1978 * Quirk read-only label configurations to preserve
1979 * access to label-less namespaces by default.
1981 if (!test_bit(NFIT_MEM_LSW
, &nfit_mem
->flags
)
1983 dev_dbg(dev
, "%s: No _LSW, disable labels\n",
1984 dev_name(&adev_dimm
->dev
));
1985 clear_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
);
1987 dev_dbg(dev
, "%s: Force enable labels\n",
1988 dev_name(&adev_dimm
->dev
));
1991 populate_shutdown_status(nfit_mem
);
1996 static void shutdown_dimm_notify(void *data
)
1998 struct acpi_nfit_desc
*acpi_desc
= data
;
1999 struct nfit_mem
*nfit_mem
;
2001 mutex_lock(&acpi_desc
->init_mutex
);
2003 * Clear out the nfit_mem->flags_attr and shut down dimm event
2006 list_for_each_entry(nfit_mem
, &acpi_desc
->dimms
, list
) {
2007 struct acpi_device
*adev_dimm
= nfit_mem
->adev
;
2009 if (nfit_mem
->flags_attr
) {
2010 sysfs_put(nfit_mem
->flags_attr
);
2011 nfit_mem
->flags_attr
= NULL
;
2014 acpi_remove_notify_handler(adev_dimm
->handle
,
2015 ACPI_DEVICE_NOTIFY
, acpi_nvdimm_notify
);
2016 dev_set_drvdata(&adev_dimm
->dev
, NULL
);
2019 mutex_unlock(&acpi_desc
->init_mutex
);
2022 static const struct nvdimm_security_ops
*acpi_nfit_get_security_ops(int family
)
2025 case NVDIMM_FAMILY_INTEL
:
2026 return intel_security_ops
;
2032 static const struct nvdimm_fw_ops
*acpi_nfit_get_fw_ops(
2033 struct nfit_mem
*nfit_mem
)
2036 struct acpi_nfit_desc
*acpi_desc
= nfit_mem
->acpi_desc
;
2037 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
2039 if (!nd_desc
->fw_ops
)
2042 if (nfit_mem
->family
!= NVDIMM_FAMILY_INTEL
)
2045 mask
= nfit_mem
->dsm_mask
& NVDIMM_INTEL_FW_ACTIVATE_CMDMASK
;
2046 if (mask
!= NVDIMM_INTEL_FW_ACTIVATE_CMDMASK
)
2049 return intel_fw_ops
;
2052 static int acpi_nfit_register_dimms(struct acpi_nfit_desc
*acpi_desc
)
2054 struct nfit_mem
*nfit_mem
;
2055 int dimm_count
= 0, rc
;
2056 struct nvdimm
*nvdimm
;
2058 list_for_each_entry(nfit_mem
, &acpi_desc
->dimms
, list
) {
2059 struct acpi_nfit_flush_address
*flush
;
2060 unsigned long flags
= 0, cmd_mask
;
2061 struct nfit_memdev
*nfit_memdev
;
2065 device_handle
= __to_nfit_memdev(nfit_mem
)->device_handle
;
2066 nvdimm
= acpi_nfit_dimm_by_handle(acpi_desc
, device_handle
);
2072 if (nfit_mem
->bdw
&& nfit_mem
->memdev_pmem
) {
2073 set_bit(NDD_ALIASING
, &flags
);
2074 set_bit(NDD_LABELING
, &flags
);
2077 /* collate flags across all memdevs for this dimm */
2078 list_for_each_entry(nfit_memdev
, &acpi_desc
->memdevs
, list
) {
2079 struct acpi_nfit_memory_map
*dimm_memdev
;
2081 dimm_memdev
= __to_nfit_memdev(nfit_mem
);
2082 if (dimm_memdev
->device_handle
2083 != nfit_memdev
->memdev
->device_handle
)
2085 dimm_memdev
->flags
|= nfit_memdev
->memdev
->flags
;
2088 mem_flags
= __to_nfit_memdev(nfit_mem
)->flags
;
2089 if (mem_flags
& ACPI_NFIT_MEM_NOT_ARMED
)
2090 set_bit(NDD_UNARMED
, &flags
);
2092 rc
= acpi_nfit_add_dimm(acpi_desc
, nfit_mem
, device_handle
);
2097 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
2098 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
2099 * userspace interface.
2101 cmd_mask
= 1UL << ND_CMD_CALL
;
2102 if (nfit_mem
->family
== NVDIMM_FAMILY_INTEL
) {
2104 * These commands have a 1:1 correspondence
2105 * between DSM payload and libnvdimm ioctl
2108 cmd_mask
|= nfit_mem
->dsm_mask
& NVDIMM_STANDARD_CMDMASK
;
2111 /* Quirk to ignore LOCAL for labels on HYPERV DIMMs */
2112 if (nfit_mem
->family
== NVDIMM_FAMILY_HYPERV
)
2113 set_bit(NDD_NOBLK
, &flags
);
2115 if (test_bit(NFIT_MEM_LSR
, &nfit_mem
->flags
)) {
2116 set_bit(ND_CMD_GET_CONFIG_SIZE
, &cmd_mask
);
2117 set_bit(ND_CMD_GET_CONFIG_DATA
, &cmd_mask
);
2119 if (test_bit(NFIT_MEM_LSW
, &nfit_mem
->flags
))
2120 set_bit(ND_CMD_SET_CONFIG_DATA
, &cmd_mask
);
2122 flush
= nfit_mem
->nfit_flush
? nfit_mem
->nfit_flush
->flush
2124 nvdimm
= __nvdimm_create(acpi_desc
->nvdimm_bus
, nfit_mem
,
2125 acpi_nfit_dimm_attribute_groups
,
2126 flags
, cmd_mask
, flush
? flush
->hint_count
: 0,
2127 nfit_mem
->flush_wpq
, &nfit_mem
->id
[0],
2128 acpi_nfit_get_security_ops(nfit_mem
->family
),
2129 acpi_nfit_get_fw_ops(nfit_mem
));
2133 nfit_mem
->nvdimm
= nvdimm
;
2136 if ((mem_flags
& ACPI_NFIT_MEM_FAILED_MASK
) == 0)
2139 dev_err(acpi_desc
->dev
, "Error found in NVDIMM %s flags:%s%s%s%s%s\n",
2140 nvdimm_name(nvdimm
),
2141 mem_flags
& ACPI_NFIT_MEM_SAVE_FAILED
? " save_fail" : "",
2142 mem_flags
& ACPI_NFIT_MEM_RESTORE_FAILED
? " restore_fail":"",
2143 mem_flags
& ACPI_NFIT_MEM_FLUSH_FAILED
? " flush_fail" : "",
2144 mem_flags
& ACPI_NFIT_MEM_NOT_ARMED
? " not_armed" : "",
2145 mem_flags
& ACPI_NFIT_MEM_MAP_FAILED
? " map_fail" : "");
2149 rc
= nvdimm_bus_check_dimm_count(acpi_desc
->nvdimm_bus
, dimm_count
);
2154 * Now that dimms are successfully registered, and async registration
2155 * is flushed, attempt to enable event notification.
2157 list_for_each_entry(nfit_mem
, &acpi_desc
->dimms
, list
) {
2158 struct kernfs_node
*nfit_kernfs
;
2160 nvdimm
= nfit_mem
->nvdimm
;
2164 nfit_kernfs
= sysfs_get_dirent(nvdimm_kobj(nvdimm
)->sd
, "nfit");
2166 nfit_mem
->flags_attr
= sysfs_get_dirent(nfit_kernfs
,
2168 sysfs_put(nfit_kernfs
);
2169 if (!nfit_mem
->flags_attr
)
2170 dev_warn(acpi_desc
->dev
, "%s: notifications disabled\n",
2171 nvdimm_name(nvdimm
));
2174 return devm_add_action_or_reset(acpi_desc
->dev
, shutdown_dimm_notify
,
2179 * These constants are private because there are no kernel consumers of
2182 enum nfit_aux_cmds
{
2183 NFIT_CMD_TRANSLATE_SPA
= 5,
2184 NFIT_CMD_ARS_INJECT_SET
= 7,
2185 NFIT_CMD_ARS_INJECT_CLEAR
= 8,
2186 NFIT_CMD_ARS_INJECT_GET
= 9,
2189 static void acpi_nfit_init_dsms(struct acpi_nfit_desc
*acpi_desc
)
2191 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
2192 const guid_t
*guid
= to_nfit_uuid(NFIT_DEV_BUS
);
2193 unsigned long dsm_mask
, *mask
;
2194 struct acpi_device
*adev
;
2197 set_bit(ND_CMD_CALL
, &nd_desc
->cmd_mask
);
2198 set_bit(NVDIMM_BUS_FAMILY_NFIT
, &nd_desc
->bus_family_mask
);
2200 /* enable nfit_test to inject bus command emulation */
2201 if (acpi_desc
->bus_cmd_force_en
) {
2202 nd_desc
->cmd_mask
= acpi_desc
->bus_cmd_force_en
;
2203 mask
= &nd_desc
->bus_family_mask
;
2204 if (acpi_desc
->family_dsm_mask
[NVDIMM_BUS_FAMILY_INTEL
]) {
2205 set_bit(NVDIMM_BUS_FAMILY_INTEL
, mask
);
2206 nd_desc
->fw_ops
= intel_bus_fw_ops
;
2210 adev
= to_acpi_dev(acpi_desc
);
2214 for (i
= ND_CMD_ARS_CAP
; i
<= ND_CMD_CLEAR_ERROR
; i
++)
2215 if (acpi_check_dsm(adev
->handle
, guid
, 1, 1ULL << i
))
2216 set_bit(i
, &nd_desc
->cmd_mask
);
2219 (1 << ND_CMD_ARS_CAP
) |
2220 (1 << ND_CMD_ARS_START
) |
2221 (1 << ND_CMD_ARS_STATUS
) |
2222 (1 << ND_CMD_CLEAR_ERROR
) |
2223 (1 << NFIT_CMD_TRANSLATE_SPA
) |
2224 (1 << NFIT_CMD_ARS_INJECT_SET
) |
2225 (1 << NFIT_CMD_ARS_INJECT_CLEAR
) |
2226 (1 << NFIT_CMD_ARS_INJECT_GET
);
2227 for_each_set_bit(i
, &dsm_mask
, BITS_PER_LONG
)
2228 if (acpi_check_dsm(adev
->handle
, guid
, 1, 1ULL << i
))
2229 set_bit(i
, &acpi_desc
->bus_dsm_mask
);
2231 /* Enumerate allowed NVDIMM_BUS_FAMILY_INTEL commands */
2232 dsm_mask
= NVDIMM_BUS_INTEL_FW_ACTIVATE_CMDMASK
;
2233 guid
= to_nfit_bus_uuid(NVDIMM_BUS_FAMILY_INTEL
);
2234 mask
= &acpi_desc
->family_dsm_mask
[NVDIMM_BUS_FAMILY_INTEL
];
2235 for_each_set_bit(i
, &dsm_mask
, BITS_PER_LONG
)
2236 if (acpi_check_dsm(adev
->handle
, guid
, 1, 1ULL << i
))
2239 if (*mask
== dsm_mask
) {
2240 set_bit(NVDIMM_BUS_FAMILY_INTEL
, &nd_desc
->bus_family_mask
);
2241 nd_desc
->fw_ops
= intel_bus_fw_ops
;
2245 static ssize_t
range_index_show(struct device
*dev
,
2246 struct device_attribute
*attr
, char *buf
)
2248 struct nd_region
*nd_region
= to_nd_region(dev
);
2249 struct nfit_spa
*nfit_spa
= nd_region_provider_data(nd_region
);
2251 return sprintf(buf
, "%d\n", nfit_spa
->spa
->range_index
);
2253 static DEVICE_ATTR_RO(range_index
);
2255 static struct attribute
*acpi_nfit_region_attributes
[] = {
2256 &dev_attr_range_index
.attr
,
2260 static const struct attribute_group acpi_nfit_region_attribute_group
= {
2262 .attrs
= acpi_nfit_region_attributes
,
2265 static const struct attribute_group
*acpi_nfit_region_attribute_groups
[] = {
2266 &acpi_nfit_region_attribute_group
,
2270 /* enough info to uniquely specify an interleave set */
2271 struct nfit_set_info
{
2272 struct nfit_set_info_map
{
2279 struct nfit_set_info2
{
2280 struct nfit_set_info_map2
{
2284 u16 manufacturing_date
;
2285 u8 manufacturing_location
;
2290 static size_t sizeof_nfit_set_info(int num_mappings
)
2292 return sizeof(struct nfit_set_info
)
2293 + num_mappings
* sizeof(struct nfit_set_info_map
);
2296 static size_t sizeof_nfit_set_info2(int num_mappings
)
2298 return sizeof(struct nfit_set_info2
)
2299 + num_mappings
* sizeof(struct nfit_set_info_map2
);
2302 static int cmp_map_compat(const void *m0
, const void *m1
)
2304 const struct nfit_set_info_map
*map0
= m0
;
2305 const struct nfit_set_info_map
*map1
= m1
;
2307 return memcmp(&map0
->region_offset
, &map1
->region_offset
,
2311 static int cmp_map(const void *m0
, const void *m1
)
2313 const struct nfit_set_info_map
*map0
= m0
;
2314 const struct nfit_set_info_map
*map1
= m1
;
2316 if (map0
->region_offset
< map1
->region_offset
)
2318 else if (map0
->region_offset
> map1
->region_offset
)
2323 static int cmp_map2(const void *m0
, const void *m1
)
2325 const struct nfit_set_info_map2
*map0
= m0
;
2326 const struct nfit_set_info_map2
*map1
= m1
;
2328 if (map0
->region_offset
< map1
->region_offset
)
2330 else if (map0
->region_offset
> map1
->region_offset
)
2335 /* Retrieve the nth entry referencing this spa */
2336 static struct acpi_nfit_memory_map
*memdev_from_spa(
2337 struct acpi_nfit_desc
*acpi_desc
, u16 range_index
, int n
)
2339 struct nfit_memdev
*nfit_memdev
;
2341 list_for_each_entry(nfit_memdev
, &acpi_desc
->memdevs
, list
)
2342 if (nfit_memdev
->memdev
->range_index
== range_index
)
2344 return nfit_memdev
->memdev
;
2348 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc
*acpi_desc
,
2349 struct nd_region_desc
*ndr_desc
,
2350 struct acpi_nfit_system_address
*spa
)
2352 struct device
*dev
= acpi_desc
->dev
;
2353 struct nd_interleave_set
*nd_set
;
2354 u16 nr
= ndr_desc
->num_mappings
;
2355 struct nfit_set_info2
*info2
;
2356 struct nfit_set_info
*info
;
2359 nd_set
= devm_kzalloc(dev
, sizeof(*nd_set
), GFP_KERNEL
);
2362 import_guid(&nd_set
->type_guid
, spa
->range_guid
);
2364 info
= devm_kzalloc(dev
, sizeof_nfit_set_info(nr
), GFP_KERNEL
);
2368 info2
= devm_kzalloc(dev
, sizeof_nfit_set_info2(nr
), GFP_KERNEL
);
2372 for (i
= 0; i
< nr
; i
++) {
2373 struct nd_mapping_desc
*mapping
= &ndr_desc
->mapping
[i
];
2374 struct nfit_set_info_map
*map
= &info
->mapping
[i
];
2375 struct nfit_set_info_map2
*map2
= &info2
->mapping
[i
];
2376 struct nvdimm
*nvdimm
= mapping
->nvdimm
;
2377 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
2378 struct acpi_nfit_memory_map
*memdev
= memdev_from_spa(acpi_desc
,
2379 spa
->range_index
, i
);
2380 struct acpi_nfit_control_region
*dcr
= nfit_mem
->dcr
;
2382 if (!memdev
|| !nfit_mem
->dcr
) {
2383 dev_err(dev
, "%s: failed to find DCR\n", __func__
);
2387 map
->region_offset
= memdev
->region_offset
;
2388 map
->serial_number
= dcr
->serial_number
;
2390 map2
->region_offset
= memdev
->region_offset
;
2391 map2
->serial_number
= dcr
->serial_number
;
2392 map2
->vendor_id
= dcr
->vendor_id
;
2393 map2
->manufacturing_date
= dcr
->manufacturing_date
;
2394 map2
->manufacturing_location
= dcr
->manufacturing_location
;
2397 /* v1.1 namespaces */
2398 sort(&info
->mapping
[0], nr
, sizeof(struct nfit_set_info_map
),
2400 nd_set
->cookie1
= nd_fletcher64(info
, sizeof_nfit_set_info(nr
), 0);
2402 /* v1.2 namespaces */
2403 sort(&info2
->mapping
[0], nr
, sizeof(struct nfit_set_info_map2
),
2405 nd_set
->cookie2
= nd_fletcher64(info2
, sizeof_nfit_set_info2(nr
), 0);
2407 /* support v1.1 namespaces created with the wrong sort order */
2408 sort(&info
->mapping
[0], nr
, sizeof(struct nfit_set_info_map
),
2409 cmp_map_compat
, NULL
);
2410 nd_set
->altcookie
= nd_fletcher64(info
, sizeof_nfit_set_info(nr
), 0);
2412 /* record the result of the sort for the mapping position */
2413 for (i
= 0; i
< nr
; i
++) {
2414 struct nfit_set_info_map2
*map2
= &info2
->mapping
[i
];
2417 for (j
= 0; j
< nr
; j
++) {
2418 struct nd_mapping_desc
*mapping
= &ndr_desc
->mapping
[j
];
2419 struct nvdimm
*nvdimm
= mapping
->nvdimm
;
2420 struct nfit_mem
*nfit_mem
= nvdimm_provider_data(nvdimm
);
2421 struct acpi_nfit_control_region
*dcr
= nfit_mem
->dcr
;
2423 if (map2
->serial_number
== dcr
->serial_number
&&
2424 map2
->vendor_id
== dcr
->vendor_id
&&
2425 map2
->manufacturing_date
== dcr
->manufacturing_date
&&
2426 map2
->manufacturing_location
2427 == dcr
->manufacturing_location
) {
2428 mapping
->position
= i
;
2434 ndr_desc
->nd_set
= nd_set
;
2435 devm_kfree(dev
, info
);
2436 devm_kfree(dev
, info2
);
2441 static u64
to_interleave_offset(u64 offset
, struct nfit_blk_mmio
*mmio
)
2443 struct acpi_nfit_interleave
*idt
= mmio
->idt
;
2444 u32 sub_line_offset
, line_index
, line_offset
;
2445 u64 line_no
, table_skip_count
, table_offset
;
2447 line_no
= div_u64_rem(offset
, mmio
->line_size
, &sub_line_offset
);
2448 table_skip_count
= div_u64_rem(line_no
, mmio
->num_lines
, &line_index
);
2449 line_offset
= idt
->line_offset
[line_index
]
2451 table_offset
= table_skip_count
* mmio
->table_size
;
2453 return mmio
->base_offset
+ line_offset
+ table_offset
+ sub_line_offset
;
2456 static u32
read_blk_stat(struct nfit_blk
*nfit_blk
, unsigned int bw
)
2458 struct nfit_blk_mmio
*mmio
= &nfit_blk
->mmio
[DCR
];
2459 u64 offset
= nfit_blk
->stat_offset
+ mmio
->size
* bw
;
2460 const u32 STATUS_MASK
= 0x80000037;
2462 if (mmio
->num_lines
)
2463 offset
= to_interleave_offset(offset
, mmio
);
2465 return readl(mmio
->addr
.base
+ offset
) & STATUS_MASK
;
2468 static void write_blk_ctl(struct nfit_blk
*nfit_blk
, unsigned int bw
,
2469 resource_size_t dpa
, unsigned int len
, unsigned int write
)
2472 struct nfit_blk_mmio
*mmio
= &nfit_blk
->mmio
[DCR
];
2475 BCW_OFFSET_MASK
= (1ULL << 48)-1,
2477 BCW_LEN_MASK
= (1ULL << 8) - 1,
2481 cmd
= (dpa
>> L1_CACHE_SHIFT
) & BCW_OFFSET_MASK
;
2482 len
= len
>> L1_CACHE_SHIFT
;
2483 cmd
|= ((u64
) len
& BCW_LEN_MASK
) << BCW_LEN_SHIFT
;
2484 cmd
|= ((u64
) write
) << BCW_CMD_SHIFT
;
2486 offset
= nfit_blk
->cmd_offset
+ mmio
->size
* bw
;
2487 if (mmio
->num_lines
)
2488 offset
= to_interleave_offset(offset
, mmio
);
2490 writeq(cmd
, mmio
->addr
.base
+ offset
);
2491 nvdimm_flush(nfit_blk
->nd_region
, NULL
);
2493 if (nfit_blk
->dimm_flags
& NFIT_BLK_DCR_LATCH
)
2494 readq(mmio
->addr
.base
+ offset
);
2497 static int acpi_nfit_blk_single_io(struct nfit_blk
*nfit_blk
,
2498 resource_size_t dpa
, void *iobuf
, size_t len
, int rw
,
2501 struct nfit_blk_mmio
*mmio
= &nfit_blk
->mmio
[BDW
];
2502 unsigned int copied
= 0;
2506 base_offset
= nfit_blk
->bdw_offset
+ dpa
% L1_CACHE_BYTES
2507 + lane
* mmio
->size
;
2508 write_blk_ctl(nfit_blk
, lane
, dpa
, len
, rw
);
2513 if (mmio
->num_lines
) {
2516 offset
= to_interleave_offset(base_offset
+ copied
,
2518 div_u64_rem(offset
, mmio
->line_size
, &line_offset
);
2519 c
= min_t(size_t, len
, mmio
->line_size
- line_offset
);
2521 offset
= base_offset
+ nfit_blk
->bdw_offset
;
2526 memcpy_flushcache(mmio
->addr
.aperture
+ offset
, iobuf
+ copied
, c
);
2528 if (nfit_blk
->dimm_flags
& NFIT_BLK_READ_FLUSH
)
2529 arch_invalidate_pmem((void __force
*)
2530 mmio
->addr
.aperture
+ offset
, c
);
2532 memcpy(iobuf
+ copied
, mmio
->addr
.aperture
+ offset
, c
);
2540 nvdimm_flush(nfit_blk
->nd_region
, NULL
);
2542 rc
= read_blk_stat(nfit_blk
, lane
) ? -EIO
: 0;
2546 static int acpi_nfit_blk_region_do_io(struct nd_blk_region
*ndbr
,
2547 resource_size_t dpa
, void *iobuf
, u64 len
, int rw
)
2549 struct nfit_blk
*nfit_blk
= nd_blk_region_provider_data(ndbr
);
2550 struct nfit_blk_mmio
*mmio
= &nfit_blk
->mmio
[BDW
];
2551 struct nd_region
*nd_region
= nfit_blk
->nd_region
;
2552 unsigned int lane
, copied
= 0;
2555 lane
= nd_region_acquire_lane(nd_region
);
2557 u64 c
= min(len
, mmio
->size
);
2559 rc
= acpi_nfit_blk_single_io(nfit_blk
, dpa
+ copied
,
2560 iobuf
+ copied
, c
, rw
, lane
);
2567 nd_region_release_lane(nd_region
, lane
);
2572 static int nfit_blk_init_interleave(struct nfit_blk_mmio
*mmio
,
2573 struct acpi_nfit_interleave
*idt
, u16 interleave_ways
)
2576 mmio
->num_lines
= idt
->line_count
;
2577 mmio
->line_size
= idt
->line_size
;
2578 if (interleave_ways
== 0)
2580 mmio
->table_size
= mmio
->num_lines
* interleave_ways
2587 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor
*nd_desc
,
2588 struct nvdimm
*nvdimm
, struct nfit_blk
*nfit_blk
)
2590 struct nd_cmd_dimm_flags flags
;
2593 memset(&flags
, 0, sizeof(flags
));
2594 rc
= nd_desc
->ndctl(nd_desc
, nvdimm
, ND_CMD_DIMM_FLAGS
, &flags
,
2595 sizeof(flags
), NULL
);
2597 if (rc
>= 0 && flags
.status
== 0)
2598 nfit_blk
->dimm_flags
= flags
.flags
;
2599 else if (rc
== -ENOTTY
) {
2600 /* fall back to a conservative default */
2601 nfit_blk
->dimm_flags
= NFIT_BLK_DCR_LATCH
| NFIT_BLK_READ_FLUSH
;
2609 static int acpi_nfit_blk_region_enable(struct nvdimm_bus
*nvdimm_bus
,
2612 struct nvdimm_bus_descriptor
*nd_desc
= to_nd_desc(nvdimm_bus
);
2613 struct nd_blk_region
*ndbr
= to_nd_blk_region(dev
);
2614 struct nfit_blk_mmio
*mmio
;
2615 struct nfit_blk
*nfit_blk
;
2616 struct nfit_mem
*nfit_mem
;
2617 struct nvdimm
*nvdimm
;
2620 nvdimm
= nd_blk_region_to_dimm(ndbr
);
2621 nfit_mem
= nvdimm_provider_data(nvdimm
);
2622 if (!nfit_mem
|| !nfit_mem
->dcr
|| !nfit_mem
->bdw
) {
2623 dev_dbg(dev
, "missing%s%s%s\n",
2624 nfit_mem
? "" : " nfit_mem",
2625 (nfit_mem
&& nfit_mem
->dcr
) ? "" : " dcr",
2626 (nfit_mem
&& nfit_mem
->bdw
) ? "" : " bdw");
2630 nfit_blk
= devm_kzalloc(dev
, sizeof(*nfit_blk
), GFP_KERNEL
);
2633 nd_blk_region_set_provider_data(ndbr
, nfit_blk
);
2634 nfit_blk
->nd_region
= to_nd_region(dev
);
2636 /* map block aperture memory */
2637 nfit_blk
->bdw_offset
= nfit_mem
->bdw
->offset
;
2638 mmio
= &nfit_blk
->mmio
[BDW
];
2639 mmio
->addr
.base
= devm_nvdimm_memremap(dev
, nfit_mem
->spa_bdw
->address
,
2640 nfit_mem
->spa_bdw
->length
, nd_blk_memremap_flags(ndbr
));
2641 if (!mmio
->addr
.base
) {
2642 dev_dbg(dev
, "%s failed to map bdw\n",
2643 nvdimm_name(nvdimm
));
2646 mmio
->size
= nfit_mem
->bdw
->size
;
2647 mmio
->base_offset
= nfit_mem
->memdev_bdw
->region_offset
;
2648 mmio
->idt
= nfit_mem
->idt_bdw
;
2649 mmio
->spa
= nfit_mem
->spa_bdw
;
2650 rc
= nfit_blk_init_interleave(mmio
, nfit_mem
->idt_bdw
,
2651 nfit_mem
->memdev_bdw
->interleave_ways
);
2653 dev_dbg(dev
, "%s failed to init bdw interleave\n",
2654 nvdimm_name(nvdimm
));
2658 /* map block control memory */
2659 nfit_blk
->cmd_offset
= nfit_mem
->dcr
->command_offset
;
2660 nfit_blk
->stat_offset
= nfit_mem
->dcr
->status_offset
;
2661 mmio
= &nfit_blk
->mmio
[DCR
];
2662 mmio
->addr
.base
= devm_nvdimm_ioremap(dev
, nfit_mem
->spa_dcr
->address
,
2663 nfit_mem
->spa_dcr
->length
);
2664 if (!mmio
->addr
.base
) {
2665 dev_dbg(dev
, "%s failed to map dcr\n",
2666 nvdimm_name(nvdimm
));
2669 mmio
->size
= nfit_mem
->dcr
->window_size
;
2670 mmio
->base_offset
= nfit_mem
->memdev_dcr
->region_offset
;
2671 mmio
->idt
= nfit_mem
->idt_dcr
;
2672 mmio
->spa
= nfit_mem
->spa_dcr
;
2673 rc
= nfit_blk_init_interleave(mmio
, nfit_mem
->idt_dcr
,
2674 nfit_mem
->memdev_dcr
->interleave_ways
);
2676 dev_dbg(dev
, "%s failed to init dcr interleave\n",
2677 nvdimm_name(nvdimm
));
2681 rc
= acpi_nfit_blk_get_flags(nd_desc
, nvdimm
, nfit_blk
);
2683 dev_dbg(dev
, "%s failed get DIMM flags\n",
2684 nvdimm_name(nvdimm
));
2688 if (nvdimm_has_flush(nfit_blk
->nd_region
) < 0)
2689 dev_warn(dev
, "unable to guarantee persistence of writes\n");
2691 if (mmio
->line_size
== 0)
2694 if ((u32
) nfit_blk
->cmd_offset
% mmio
->line_size
2695 + 8 > mmio
->line_size
) {
2696 dev_dbg(dev
, "cmd_offset crosses interleave boundary\n");
2698 } else if ((u32
) nfit_blk
->stat_offset
% mmio
->line_size
2699 + 8 > mmio
->line_size
) {
2700 dev_dbg(dev
, "stat_offset crosses interleave boundary\n");
2707 static int ars_get_cap(struct acpi_nfit_desc
*acpi_desc
,
2708 struct nd_cmd_ars_cap
*cmd
, struct nfit_spa
*nfit_spa
)
2710 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
2711 struct acpi_nfit_system_address
*spa
= nfit_spa
->spa
;
2714 cmd
->address
= spa
->address
;
2715 cmd
->length
= spa
->length
;
2716 rc
= nd_desc
->ndctl(nd_desc
, NULL
, ND_CMD_ARS_CAP
, cmd
,
2717 sizeof(*cmd
), &cmd_rc
);
2723 static int ars_start(struct acpi_nfit_desc
*acpi_desc
,
2724 struct nfit_spa
*nfit_spa
, enum nfit_ars_state req_type
)
2728 struct nd_cmd_ars_start ars_start
;
2729 struct acpi_nfit_system_address
*spa
= nfit_spa
->spa
;
2730 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
2732 memset(&ars_start
, 0, sizeof(ars_start
));
2733 ars_start
.address
= spa
->address
;
2734 ars_start
.length
= spa
->length
;
2735 if (req_type
== ARS_REQ_SHORT
)
2736 ars_start
.flags
= ND_ARS_RETURN_PREV_DATA
;
2737 if (nfit_spa_type(spa
) == NFIT_SPA_PM
)
2738 ars_start
.type
= ND_ARS_PERSISTENT
;
2739 else if (nfit_spa_type(spa
) == NFIT_SPA_VOLATILE
)
2740 ars_start
.type
= ND_ARS_VOLATILE
;
2744 rc
= nd_desc
->ndctl(nd_desc
, NULL
, ND_CMD_ARS_START
, &ars_start
,
2745 sizeof(ars_start
), &cmd_rc
);
2751 set_bit(ARS_VALID
, &acpi_desc
->scrub_flags
);
2755 static int ars_continue(struct acpi_nfit_desc
*acpi_desc
)
2758 struct nd_cmd_ars_start ars_start
;
2759 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
2760 struct nd_cmd_ars_status
*ars_status
= acpi_desc
->ars_status
;
2762 ars_start
= (struct nd_cmd_ars_start
) {
2763 .address
= ars_status
->restart_address
,
2764 .length
= ars_status
->restart_length
,
2765 .type
= ars_status
->type
,
2767 rc
= nd_desc
->ndctl(nd_desc
, NULL
, ND_CMD_ARS_START
, &ars_start
,
2768 sizeof(ars_start
), &cmd_rc
);
2774 static int ars_get_status(struct acpi_nfit_desc
*acpi_desc
)
2776 struct nvdimm_bus_descriptor
*nd_desc
= &acpi_desc
->nd_desc
;
2777 struct nd_cmd_ars_status
*ars_status
= acpi_desc
->ars_status
;
2780 rc
= nd_desc
->ndctl(nd_desc
, NULL
, ND_CMD_ARS_STATUS
, ars_status
,
2781 acpi_desc
->max_ars
, &cmd_rc
);
2787 static void ars_complete(struct acpi_nfit_desc
*acpi_desc
,
2788 struct nfit_spa
*nfit_spa
)
2790 struct nd_cmd_ars_status
*ars_status
= acpi_desc
->ars_status
;
2791 struct acpi_nfit_system_address
*spa
= nfit_spa
->spa
;
2792 struct nd_region
*nd_region
= nfit_spa
->nd_region
;
2795 lockdep_assert_held(&acpi_desc
->init_mutex
);
2797 * Only advance the ARS state for ARS runs initiated by the
2798 * kernel, ignore ARS results from BIOS initiated runs for scrub
2799 * completion tracking.
2801 if (acpi_desc
->scrub_spa
!= nfit_spa
)
2804 if ((ars_status
->address
>= spa
->address
&& ars_status
->address
2805 < spa
->address
+ spa
->length
)
2806 || (ars_status
->address
< spa
->address
)) {
2808 * Assume that if a scrub starts at an offset from the
2809 * start of nfit_spa that we are in the continuation
2812 * Otherwise, if the scrub covers the spa range, mark
2813 * any pending request complete.
2815 if (ars_status
->address
+ ars_status
->length
2816 >= spa
->address
+ spa
->length
)
2823 acpi_desc
->scrub_spa
= NULL
;
2825 dev
= nd_region_dev(nd_region
);
2826 nvdimm_region_notify(nd_region
, NVDIMM_REVALIDATE_POISON
);
2828 dev
= acpi_desc
->dev
;
2829 dev_dbg(dev
, "ARS: range %d complete\n", spa
->range_index
);
2832 static int ars_status_process_records(struct acpi_nfit_desc
*acpi_desc
)
2834 struct nvdimm_bus
*nvdimm_bus
= acpi_desc
->nvdimm_bus
;
2835 struct nd_cmd_ars_status
*ars_status
= acpi_desc
->ars_status
;
2840 * First record starts at 44 byte offset from the start of the
2843 if (ars_status
->out_length
< 44)
2847 * Ignore potentially stale results that are only refreshed
2848 * after a start-ARS event.
2850 if (!test_and_clear_bit(ARS_VALID
, &acpi_desc
->scrub_flags
)) {
2851 dev_dbg(acpi_desc
->dev
, "skip %d stale records\n",
2852 ars_status
->num_records
);
2856 for (i
= 0; i
< ars_status
->num_records
; i
++) {
2857 /* only process full records */
2858 if (ars_status
->out_length
2859 < 44 + sizeof(struct nd_ars_record
) * (i
+ 1))
2861 rc
= nvdimm_bus_add_badrange(nvdimm_bus
,
2862 ars_status
->records
[i
].err_address
,
2863 ars_status
->records
[i
].length
);
2867 if (i
< ars_status
->num_records
)
2868 dev_warn(acpi_desc
->dev
, "detected truncated ars results\n");
2873 static void acpi_nfit_remove_resource(void *data
)
2875 struct resource
*res
= data
;
2877 remove_resource(res
);
2880 static int acpi_nfit_insert_resource(struct acpi_nfit_desc
*acpi_desc
,
2881 struct nd_region_desc
*ndr_desc
)
2883 struct resource
*res
, *nd_res
= ndr_desc
->res
;
2886 /* No operation if the region is already registered as PMEM */
2887 is_pmem
= region_intersects(nd_res
->start
, resource_size(nd_res
),
2888 IORESOURCE_MEM
, IORES_DESC_PERSISTENT_MEMORY
);
2889 if (is_pmem
== REGION_INTERSECTS
)
2892 res
= devm_kzalloc(acpi_desc
->dev
, sizeof(*res
), GFP_KERNEL
);
2896 res
->name
= "Persistent Memory";
2897 res
->start
= nd_res
->start
;
2898 res
->end
= nd_res
->end
;
2899 res
->flags
= IORESOURCE_MEM
;
2900 res
->desc
= IORES_DESC_PERSISTENT_MEMORY
;
2902 ret
= insert_resource(&iomem_resource
, res
);
2906 ret
= devm_add_action_or_reset(acpi_desc
->dev
,
2907 acpi_nfit_remove_resource
,
2915 static int acpi_nfit_init_mapping(struct acpi_nfit_desc
*acpi_desc
,
2916 struct nd_mapping_desc
*mapping
, struct nd_region_desc
*ndr_desc
,
2917 struct acpi_nfit_memory_map
*memdev
,
2918 struct nfit_spa
*nfit_spa
)
2920 struct nvdimm
*nvdimm
= acpi_nfit_dimm_by_handle(acpi_desc
,
2921 memdev
->device_handle
);
2922 struct acpi_nfit_system_address
*spa
= nfit_spa
->spa
;
2923 struct nd_blk_region_desc
*ndbr_desc
;
2924 struct nfit_mem
*nfit_mem
;
2928 dev_err(acpi_desc
->dev
, "spa%d dimm: %#x not found\n",
2929 spa
->range_index
, memdev
->device_handle
);
2933 mapping
->nvdimm
= nvdimm
;
2934 switch (nfit_spa_type(spa
)) {
2936 case NFIT_SPA_VOLATILE
:
2937 mapping
->start
= memdev
->address
;
2938 mapping
->size
= memdev
->region_size
;
2941 nfit_mem
= nvdimm_provider_data(nvdimm
);
2942 if (!nfit_mem
|| !nfit_mem
->bdw
) {
2943 dev_dbg(acpi_desc
->dev
, "spa%d %s missing bdw\n",
2944 spa
->range_index
, nvdimm_name(nvdimm
));
2948 mapping
->size
= nfit_mem
->bdw
->capacity
;
2949 mapping
->start
= nfit_mem
->bdw
->start_address
;
2950 ndr_desc
->num_lanes
= nfit_mem
->bdw
->windows
;
2951 ndr_desc
->mapping
= mapping
;
2952 ndr_desc
->num_mappings
= 1;
2953 ndbr_desc
= to_blk_region_desc(ndr_desc
);
2954 ndbr_desc
->enable
= acpi_nfit_blk_region_enable
;
2955 ndbr_desc
->do_io
= acpi_desc
->blk_do_io
;
2956 rc
= acpi_nfit_init_interleave_set(acpi_desc
, ndr_desc
, spa
);
2959 nfit_spa
->nd_region
= nvdimm_blk_region_create(acpi_desc
->nvdimm_bus
,
2961 if (!nfit_spa
->nd_region
)
2969 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address
*spa
)
2971 return (nfit_spa_type(spa
) == NFIT_SPA_VDISK
||
2972 nfit_spa_type(spa
) == NFIT_SPA_VCD
||
2973 nfit_spa_type(spa
) == NFIT_SPA_PDISK
||
2974 nfit_spa_type(spa
) == NFIT_SPA_PCD
);
2977 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address
*spa
)
2979 return (nfit_spa_type(spa
) == NFIT_SPA_VDISK
||
2980 nfit_spa_type(spa
) == NFIT_SPA_VCD
||
2981 nfit_spa_type(spa
) == NFIT_SPA_VOLATILE
);
2984 static int acpi_nfit_register_region(struct acpi_nfit_desc
*acpi_desc
,
2985 struct nfit_spa
*nfit_spa
)
2987 static struct nd_mapping_desc mappings
[ND_MAX_MAPPINGS
];
2988 struct acpi_nfit_system_address
*spa
= nfit_spa
->spa
;
2989 struct nd_blk_region_desc ndbr_desc
;
2990 struct nd_region_desc
*ndr_desc
;
2991 struct nfit_memdev
*nfit_memdev
;
2992 struct nvdimm_bus
*nvdimm_bus
;
2993 struct resource res
;
2996 if (nfit_spa
->nd_region
)
2999 if (spa
->range_index
== 0 && !nfit_spa_is_virtual(spa
)) {
3000 dev_dbg(acpi_desc
->dev
, "detected invalid spa index\n");
3004 memset(&res
, 0, sizeof(res
));
3005 memset(&mappings
, 0, sizeof(mappings
));
3006 memset(&ndbr_desc
, 0, sizeof(ndbr_desc
));
3007 res
.start
= spa
->address
;
3008 res
.end
= res
.start
+ spa
->length
- 1;
3009 ndr_desc
= &ndbr_desc
.ndr_desc
;
3010 ndr_desc
->res
= &res
;
3011 ndr_desc
->provider_data
= nfit_spa
;
3012 ndr_desc
->attr_groups
= acpi_nfit_region_attribute_groups
;
3013 if (spa
->flags
& ACPI_NFIT_PROXIMITY_VALID
) {
3014 ndr_desc
->numa_node
= pxm_to_online_node(spa
->proximity_domain
);
3015 ndr_desc
->target_node
= pxm_to_node(spa
->proximity_domain
);
3017 ndr_desc
->numa_node
= NUMA_NO_NODE
;
3018 ndr_desc
->target_node
= NUMA_NO_NODE
;
3022 * Persistence domain bits are hierarchical, if
3023 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
3024 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
3026 if (acpi_desc
->platform_cap
& ACPI_NFIT_CAPABILITY_CACHE_FLUSH
)
3027 set_bit(ND_REGION_PERSIST_CACHE
, &ndr_desc
->flags
);
3028 else if (acpi_desc
->platform_cap
& ACPI_NFIT_CAPABILITY_MEM_FLUSH
)
3029 set_bit(ND_REGION_PERSIST_MEMCTRL
, &ndr_desc
->flags
);
3031 list_for_each_entry(nfit_memdev
, &acpi_desc
->memdevs
, list
) {
3032 struct acpi_nfit_memory_map
*memdev
= nfit_memdev
->memdev
;
3033 struct nd_mapping_desc
*mapping
;
3035 if (memdev
->range_index
!= spa
->range_index
)
3037 if (count
>= ND_MAX_MAPPINGS
) {
3038 dev_err(acpi_desc
->dev
, "spa%d exceeds max mappings %d\n",
3039 spa
->range_index
, ND_MAX_MAPPINGS
);
3042 mapping
= &mappings
[count
++];
3043 rc
= acpi_nfit_init_mapping(acpi_desc
, mapping
, ndr_desc
,
3049 ndr_desc
->mapping
= mappings
;
3050 ndr_desc
->num_mappings
= count
;
3051 rc
= acpi_nfit_init_interleave_set(acpi_desc
, ndr_desc
, spa
);
3055 nvdimm_bus
= acpi_desc
->nvdimm_bus
;
3056 if (nfit_spa_type(spa
) == NFIT_SPA_PM
) {
3057 rc
= acpi_nfit_insert_resource(acpi_desc
, ndr_desc
);
3059 dev_warn(acpi_desc
->dev
,
3060 "failed to insert pmem resource to iomem: %d\n",
3065 nfit_spa
->nd_region
= nvdimm_pmem_region_create(nvdimm_bus
,
3067 if (!nfit_spa
->nd_region
)
3069 } else if (nfit_spa_is_volatile(spa
)) {
3070 nfit_spa
->nd_region
= nvdimm_volatile_region_create(nvdimm_bus
,
3072 if (!nfit_spa
->nd_region
)
3074 } else if (nfit_spa_is_virtual(spa
)) {
3075 nfit_spa
->nd_region
= nvdimm_pmem_region_create(nvdimm_bus
,
3077 if (!nfit_spa
->nd_region
)
3083 dev_err(acpi_desc
->dev
, "failed to register spa range %d\n",
3084 nfit_spa
->spa
->range_index
);
3088 static int ars_status_alloc(struct acpi_nfit_desc
*acpi_desc
)
3090 struct device
*dev
= acpi_desc
->dev
;
3091 struct nd_cmd_ars_status
*ars_status
;
3093 if (acpi_desc
->ars_status
) {
3094 memset(acpi_desc
->ars_status
, 0, acpi_desc
->max_ars
);
3098 ars_status
= devm_kzalloc(dev
, acpi_desc
->max_ars
, GFP_KERNEL
);
3101 acpi_desc
->ars_status
= ars_status
;
3105 static int acpi_nfit_query_poison(struct acpi_nfit_desc
*acpi_desc
)
3109 if (ars_status_alloc(acpi_desc
))
3112 rc
= ars_get_status(acpi_desc
);
3114 if (rc
< 0 && rc
!= -ENOSPC
)
3117 if (ars_status_process_records(acpi_desc
))
3118 dev_err(acpi_desc
->dev
, "Failed to process ARS records\n");
3123 static int ars_register(struct acpi_nfit_desc
*acpi_desc
,
3124 struct nfit_spa
*nfit_spa
)
3128 if (test_bit(ARS_FAILED
, &nfit_spa
->ars_state
))
3129 return acpi_nfit_register_region(acpi_desc
, nfit_spa
);
3131 set_bit(ARS_REQ_SHORT
, &nfit_spa
->ars_state
);
3133 set_bit(ARS_REQ_LONG
, &nfit_spa
->ars_state
);
3135 switch (acpi_nfit_query_poison(acpi_desc
)) {
3139 rc
= ars_start(acpi_desc
, nfit_spa
, ARS_REQ_SHORT
);
3140 /* shouldn't happen, try again later */
3144 set_bit(ARS_FAILED
, &nfit_spa
->ars_state
);
3147 clear_bit(ARS_REQ_SHORT
, &nfit_spa
->ars_state
);
3148 rc
= acpi_nfit_query_poison(acpi_desc
);
3151 acpi_desc
->scrub_spa
= nfit_spa
;
3152 ars_complete(acpi_desc
, nfit_spa
);
3154 * If ars_complete() says we didn't complete the
3155 * short scrub, we'll try again with a long
3158 acpi_desc
->scrub_spa
= NULL
;
3163 * BIOS was using ARS, wait for it to complete (or
3164 * resources to become available) and then perform our
3169 set_bit(ARS_FAILED
, &nfit_spa
->ars_state
);
3173 return acpi_nfit_register_region(acpi_desc
, nfit_spa
);
3176 static void ars_complete_all(struct acpi_nfit_desc
*acpi_desc
)
3178 struct nfit_spa
*nfit_spa
;
3180 list_for_each_entry(nfit_spa
, &acpi_desc
->spas
, list
) {
3181 if (test_bit(ARS_FAILED
, &nfit_spa
->ars_state
))
3183 ars_complete(acpi_desc
, nfit_spa
);
3187 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc
*acpi_desc
,
3190 unsigned int tmo
= acpi_desc
->scrub_tmo
;
3191 struct device
*dev
= acpi_desc
->dev
;
3192 struct nfit_spa
*nfit_spa
;
3194 lockdep_assert_held(&acpi_desc
->init_mutex
);
3196 if (test_bit(ARS_CANCEL
, &acpi_desc
->scrub_flags
))
3199 if (query_rc
== -EBUSY
) {
3200 dev_dbg(dev
, "ARS: ARS busy\n");
3201 return min(30U * 60U, tmo
* 2);
3203 if (query_rc
== -ENOSPC
) {
3204 dev_dbg(dev
, "ARS: ARS continue\n");
3205 ars_continue(acpi_desc
);
3208 if (query_rc
&& query_rc
!= -EAGAIN
) {
3209 unsigned long long addr
, end
;
3211 addr
= acpi_desc
->ars_status
->address
;
3212 end
= addr
+ acpi_desc
->ars_status
->length
;
3213 dev_dbg(dev
, "ARS: %llx-%llx failed (%d)\n", addr
, end
,
3217 ars_complete_all(acpi_desc
);
3218 list_for_each_entry(nfit_spa
, &acpi_desc
->spas
, list
) {
3219 enum nfit_ars_state req_type
;
3222 if (test_bit(ARS_FAILED
, &nfit_spa
->ars_state
))
3225 /* prefer short ARS requests first */
3226 if (test_bit(ARS_REQ_SHORT
, &nfit_spa
->ars_state
))
3227 req_type
= ARS_REQ_SHORT
;
3228 else if (test_bit(ARS_REQ_LONG
, &nfit_spa
->ars_state
))
3229 req_type
= ARS_REQ_LONG
;
3232 rc
= ars_start(acpi_desc
, nfit_spa
, req_type
);
3234 dev
= nd_region_dev(nfit_spa
->nd_region
);
3235 dev_dbg(dev
, "ARS: range %d ARS start %s (%d)\n",
3236 nfit_spa
->spa
->range_index
,
3237 req_type
== ARS_REQ_SHORT
? "short" : "long",
3240 * Hmm, we raced someone else starting ARS? Try again in
3246 dev_WARN_ONCE(dev
, acpi_desc
->scrub_spa
,
3247 "scrub start while range %d active\n",
3248 acpi_desc
->scrub_spa
->spa
->range_index
);
3249 clear_bit(req_type
, &nfit_spa
->ars_state
);
3250 acpi_desc
->scrub_spa
= nfit_spa
;
3252 * Consider this spa last for future scrub
3255 list_move_tail(&nfit_spa
->list
, &acpi_desc
->spas
);
3259 dev_err(dev
, "ARS: range %d ARS failed (%d)\n",
3260 nfit_spa
->spa
->range_index
, rc
);
3261 set_bit(ARS_FAILED
, &nfit_spa
->ars_state
);
3266 static void __sched_ars(struct acpi_nfit_desc
*acpi_desc
, unsigned int tmo
)
3268 lockdep_assert_held(&acpi_desc
->init_mutex
);
3270 set_bit(ARS_BUSY
, &acpi_desc
->scrub_flags
);
3271 /* note this should only be set from within the workqueue */
3273 acpi_desc
->scrub_tmo
= tmo
;
3274 queue_delayed_work(nfit_wq
, &acpi_desc
->dwork
, tmo
* HZ
);
3277 static void sched_ars(struct acpi_nfit_desc
*acpi_desc
)
3279 __sched_ars(acpi_desc
, 0);
3282 static void notify_ars_done(struct acpi_nfit_desc
*acpi_desc
)
3284 lockdep_assert_held(&acpi_desc
->init_mutex
);
3286 clear_bit(ARS_BUSY
, &acpi_desc
->scrub_flags
);
3287 acpi_desc
->scrub_count
++;
3288 if (acpi_desc
->scrub_count_state
)
3289 sysfs_notify_dirent(acpi_desc
->scrub_count_state
);
3292 static void acpi_nfit_scrub(struct work_struct
*work
)
3294 struct acpi_nfit_desc
*acpi_desc
;
3298 acpi_desc
= container_of(work
, typeof(*acpi_desc
), dwork
.work
);
3299 mutex_lock(&acpi_desc
->init_mutex
);
3300 query_rc
= acpi_nfit_query_poison(acpi_desc
);
3301 tmo
= __acpi_nfit_scrub(acpi_desc
, query_rc
);
3303 __sched_ars(acpi_desc
, tmo
);
3305 notify_ars_done(acpi_desc
);
3306 memset(acpi_desc
->ars_status
, 0, acpi_desc
->max_ars
);
3307 clear_bit(ARS_POLL
, &acpi_desc
->scrub_flags
);
3308 mutex_unlock(&acpi_desc
->init_mutex
);
3311 static void acpi_nfit_init_ars(struct acpi_nfit_desc
*acpi_desc
,
3312 struct nfit_spa
*nfit_spa
)
3314 int type
= nfit_spa_type(nfit_spa
->spa
);
3315 struct nd_cmd_ars_cap ars_cap
;
3318 set_bit(ARS_FAILED
, &nfit_spa
->ars_state
);
3319 memset(&ars_cap
, 0, sizeof(ars_cap
));
3320 rc
= ars_get_cap(acpi_desc
, &ars_cap
, nfit_spa
);
3323 /* check that the supported scrub types match the spa type */
3324 if (type
== NFIT_SPA_VOLATILE
&& ((ars_cap
.status
>> 16)
3325 & ND_ARS_VOLATILE
) == 0)
3327 if (type
== NFIT_SPA_PM
&& ((ars_cap
.status
>> 16)
3328 & ND_ARS_PERSISTENT
) == 0)
3331 nfit_spa
->max_ars
= ars_cap
.max_ars_out
;
3332 nfit_spa
->clear_err_unit
= ars_cap
.clear_err_unit
;
3333 acpi_desc
->max_ars
= max(nfit_spa
->max_ars
, acpi_desc
->max_ars
);
3334 clear_bit(ARS_FAILED
, &nfit_spa
->ars_state
);
3337 static int acpi_nfit_register_regions(struct acpi_nfit_desc
*acpi_desc
)
3339 struct nfit_spa
*nfit_spa
;
3340 int rc
, do_sched_ars
= 0;
3342 set_bit(ARS_VALID
, &acpi_desc
->scrub_flags
);
3343 list_for_each_entry(nfit_spa
, &acpi_desc
->spas
, list
) {
3344 switch (nfit_spa_type(nfit_spa
->spa
)) {
3345 case NFIT_SPA_VOLATILE
:
3347 acpi_nfit_init_ars(acpi_desc
, nfit_spa
);
3352 list_for_each_entry(nfit_spa
, &acpi_desc
->spas
, list
) {
3353 switch (nfit_spa_type(nfit_spa
->spa
)) {
3354 case NFIT_SPA_VOLATILE
:
3356 /* register regions and kick off initial ARS run */
3357 rc
= ars_register(acpi_desc
, nfit_spa
);
3362 * Kick off background ARS if at least one
3363 * region successfully registered ARS
3365 if (!test_bit(ARS_FAILED
, &nfit_spa
->ars_state
))
3369 /* nothing to register */
3372 case NFIT_SPA_VDISK
:
3374 case NFIT_SPA_PDISK
:
3376 /* register known regions that don't support ARS */
3377 rc
= acpi_nfit_register_region(acpi_desc
, nfit_spa
);
3382 /* don't register unknown regions */
3388 sched_ars(acpi_desc
);
3392 static int acpi_nfit_check_deletions(struct acpi_nfit_desc
*acpi_desc
,
3393 struct nfit_table_prev
*prev
)
3395 struct device
*dev
= acpi_desc
->dev
;
3397 if (!list_empty(&prev
->spas
) ||
3398 !list_empty(&prev
->memdevs
) ||
3399 !list_empty(&prev
->dcrs
) ||
3400 !list_empty(&prev
->bdws
) ||
3401 !list_empty(&prev
->idts
) ||
3402 !list_empty(&prev
->flushes
)) {
3403 dev_err(dev
, "new nfit deletes entries (unsupported)\n");
3409 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc
*acpi_desc
)
3411 struct device
*dev
= acpi_desc
->dev
;
3412 struct kernfs_node
*nfit
;
3413 struct device
*bus_dev
;
3415 if (!ars_supported(acpi_desc
->nvdimm_bus
))
3418 bus_dev
= to_nvdimm_bus_dev(acpi_desc
->nvdimm_bus
);
3419 nfit
= sysfs_get_dirent(bus_dev
->kobj
.sd
, "nfit");
3421 dev_err(dev
, "sysfs_get_dirent 'nfit' failed\n");
3424 acpi_desc
->scrub_count_state
= sysfs_get_dirent(nfit
, "scrub");
3426 if (!acpi_desc
->scrub_count_state
) {
3427 dev_err(dev
, "sysfs_get_dirent 'scrub' failed\n");
3434 static void acpi_nfit_unregister(void *data
)
3436 struct acpi_nfit_desc
*acpi_desc
= data
;
3438 nvdimm_bus_unregister(acpi_desc
->nvdimm_bus
);
3441 int acpi_nfit_init(struct acpi_nfit_desc
*acpi_desc
, void *data
, acpi_size sz
)
3443 struct device
*dev
= acpi_desc
->dev
;
3444 struct nfit_table_prev prev
;
3448 if (!acpi_desc
->nvdimm_bus
) {
3449 acpi_nfit_init_dsms(acpi_desc
);
3451 acpi_desc
->nvdimm_bus
= nvdimm_bus_register(dev
,
3452 &acpi_desc
->nd_desc
);
3453 if (!acpi_desc
->nvdimm_bus
)
3456 rc
= devm_add_action_or_reset(dev
, acpi_nfit_unregister
,
3461 rc
= acpi_nfit_desc_init_scrub_attr(acpi_desc
);
3465 /* register this acpi_desc for mce notifications */
3466 mutex_lock(&acpi_desc_lock
);
3467 list_add_tail(&acpi_desc
->list
, &acpi_descs
);
3468 mutex_unlock(&acpi_desc_lock
);
3471 mutex_lock(&acpi_desc
->init_mutex
);
3473 INIT_LIST_HEAD(&prev
.spas
);
3474 INIT_LIST_HEAD(&prev
.memdevs
);
3475 INIT_LIST_HEAD(&prev
.dcrs
);
3476 INIT_LIST_HEAD(&prev
.bdws
);
3477 INIT_LIST_HEAD(&prev
.idts
);
3478 INIT_LIST_HEAD(&prev
.flushes
);
3480 list_cut_position(&prev
.spas
, &acpi_desc
->spas
,
3481 acpi_desc
->spas
.prev
);
3482 list_cut_position(&prev
.memdevs
, &acpi_desc
->memdevs
,
3483 acpi_desc
->memdevs
.prev
);
3484 list_cut_position(&prev
.dcrs
, &acpi_desc
->dcrs
,
3485 acpi_desc
->dcrs
.prev
);
3486 list_cut_position(&prev
.bdws
, &acpi_desc
->bdws
,
3487 acpi_desc
->bdws
.prev
);
3488 list_cut_position(&prev
.idts
, &acpi_desc
->idts
,
3489 acpi_desc
->idts
.prev
);
3490 list_cut_position(&prev
.flushes
, &acpi_desc
->flushes
,
3491 acpi_desc
->flushes
.prev
);
3494 while (!IS_ERR_OR_NULL(data
))
3495 data
= add_table(acpi_desc
, &prev
, data
, end
);
3498 dev_dbg(dev
, "nfit table parsing error: %ld\n", PTR_ERR(data
));
3503 rc
= acpi_nfit_check_deletions(acpi_desc
, &prev
);
3507 rc
= nfit_mem_init(acpi_desc
);
3511 rc
= acpi_nfit_register_dimms(acpi_desc
);
3515 rc
= acpi_nfit_register_regions(acpi_desc
);
3518 mutex_unlock(&acpi_desc
->init_mutex
);
3521 EXPORT_SYMBOL_GPL(acpi_nfit_init
);
3523 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor
*nd_desc
)
3525 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
3526 struct device
*dev
= acpi_desc
->dev
;
3528 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3529 nfit_device_lock(dev
);
3530 nfit_device_unlock(dev
);
3532 /* Bounce the init_mutex to complete initial registration */
3533 mutex_lock(&acpi_desc
->init_mutex
);
3534 mutex_unlock(&acpi_desc
->init_mutex
);
3539 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor
*nd_desc
,
3540 struct nvdimm
*nvdimm
, unsigned int cmd
)
3542 struct acpi_nfit_desc
*acpi_desc
= to_acpi_desc(nd_desc
);
3546 if (cmd
!= ND_CMD_ARS_START
)
3550 * The kernel and userspace may race to initiate a scrub, but
3551 * the scrub thread is prepared to lose that initial race. It
3552 * just needs guarantees that any ARS it initiates are not
3553 * interrupted by any intervening start requests from userspace.
3555 if (work_busy(&acpi_desc
->dwork
.work
))
3562 * Prevent security and firmware activate commands from being issued via
3565 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor
*nd_desc
,
3566 struct nvdimm
*nvdimm
, unsigned int cmd
, void *buf
)
3568 struct nd_cmd_pkg
*call_pkg
= buf
;
3571 if (nvdimm
&& cmd
== ND_CMD_CALL
&&
3572 call_pkg
->nd_family
== NVDIMM_FAMILY_INTEL
) {
3573 func
= call_pkg
->nd_command
;
3574 if (func
> NVDIMM_CMD_MAX
||
3575 (1 << func
) & NVDIMM_INTEL_DENY_CMDMASK
)
3579 /* block all non-nfit bus commands */
3580 if (!nvdimm
&& cmd
== ND_CMD_CALL
&&
3581 call_pkg
->nd_family
!= NVDIMM_BUS_FAMILY_NFIT
)
3584 return __acpi_nfit_clear_to_send(nd_desc
, nvdimm
, cmd
);
3587 int acpi_nfit_ars_rescan(struct acpi_nfit_desc
*acpi_desc
,
3588 enum nfit_ars_state req_type
)
3590 struct device
*dev
= acpi_desc
->dev
;
3591 int scheduled
= 0, busy
= 0;
3592 struct nfit_spa
*nfit_spa
;
3594 mutex_lock(&acpi_desc
->init_mutex
);
3595 if (test_bit(ARS_CANCEL
, &acpi_desc
->scrub_flags
)) {
3596 mutex_unlock(&acpi_desc
->init_mutex
);
3600 list_for_each_entry(nfit_spa
, &acpi_desc
->spas
, list
) {
3601 int type
= nfit_spa_type(nfit_spa
->spa
);
3603 if (type
!= NFIT_SPA_PM
&& type
!= NFIT_SPA_VOLATILE
)
3605 if (test_bit(ARS_FAILED
, &nfit_spa
->ars_state
))
3608 if (test_and_set_bit(req_type
, &nfit_spa
->ars_state
))
3614 sched_ars(acpi_desc
);
3615 dev_dbg(dev
, "ars_scan triggered\n");
3617 mutex_unlock(&acpi_desc
->init_mutex
);
3626 void acpi_nfit_desc_init(struct acpi_nfit_desc
*acpi_desc
, struct device
*dev
)
3628 struct nvdimm_bus_descriptor
*nd_desc
;
3630 dev_set_drvdata(dev
, acpi_desc
);
3631 acpi_desc
->dev
= dev
;
3632 acpi_desc
->blk_do_io
= acpi_nfit_blk_region_do_io
;
3633 nd_desc
= &acpi_desc
->nd_desc
;
3634 nd_desc
->provider_name
= "ACPI.NFIT";
3635 nd_desc
->module
= THIS_MODULE
;
3636 nd_desc
->ndctl
= acpi_nfit_ctl
;
3637 nd_desc
->flush_probe
= acpi_nfit_flush_probe
;
3638 nd_desc
->clear_to_send
= acpi_nfit_clear_to_send
;
3639 nd_desc
->attr_groups
= acpi_nfit_attribute_groups
;
3641 INIT_LIST_HEAD(&acpi_desc
->spas
);
3642 INIT_LIST_HEAD(&acpi_desc
->dcrs
);
3643 INIT_LIST_HEAD(&acpi_desc
->bdws
);
3644 INIT_LIST_HEAD(&acpi_desc
->idts
);
3645 INIT_LIST_HEAD(&acpi_desc
->flushes
);
3646 INIT_LIST_HEAD(&acpi_desc
->memdevs
);
3647 INIT_LIST_HEAD(&acpi_desc
->dimms
);
3648 INIT_LIST_HEAD(&acpi_desc
->list
);
3649 mutex_init(&acpi_desc
->init_mutex
);
3650 acpi_desc
->scrub_tmo
= 1;
3651 INIT_DELAYED_WORK(&acpi_desc
->dwork
, acpi_nfit_scrub
);
3653 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init
);
3655 static void acpi_nfit_put_table(void *table
)
3657 acpi_put_table(table
);
3660 void acpi_nfit_shutdown(void *data
)
3662 struct acpi_nfit_desc
*acpi_desc
= data
;
3663 struct device
*bus_dev
= to_nvdimm_bus_dev(acpi_desc
->nvdimm_bus
);
3666 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3669 mutex_lock(&acpi_desc_lock
);
3670 list_del(&acpi_desc
->list
);
3671 mutex_unlock(&acpi_desc_lock
);
3673 mutex_lock(&acpi_desc
->init_mutex
);
3674 set_bit(ARS_CANCEL
, &acpi_desc
->scrub_flags
);
3675 cancel_delayed_work_sync(&acpi_desc
->dwork
);
3676 mutex_unlock(&acpi_desc
->init_mutex
);
3679 * Bounce the nvdimm bus lock to make sure any in-flight
3680 * acpi_nfit_ars_rescan() submissions have had a chance to
3681 * either submit or see ->cancel set.
3683 nfit_device_lock(bus_dev
);
3684 nfit_device_unlock(bus_dev
);
3686 flush_workqueue(nfit_wq
);
3688 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown
);
3690 static int acpi_nfit_add(struct acpi_device
*adev
)
3692 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
3693 struct acpi_nfit_desc
*acpi_desc
;
3694 struct device
*dev
= &adev
->dev
;
3695 struct acpi_table_header
*tbl
;
3696 acpi_status status
= AE_OK
;
3700 status
= acpi_get_table(ACPI_SIG_NFIT
, 0, &tbl
);
3701 if (ACPI_FAILURE(status
)) {
3702 /* The NVDIMM root device allows OS to trigger enumeration of
3703 * NVDIMMs through NFIT at boot time and re-enumeration at
3704 * root level via the _FIT method during runtime.
3705 * This is ok to return 0 here, we could have an nvdimm
3706 * hotplugged later and evaluate _FIT method which returns
3707 * data in the format of a series of NFIT Structures.
3709 dev_dbg(dev
, "failed to find NFIT at startup\n");
3713 rc
= devm_add_action_or_reset(dev
, acpi_nfit_put_table
, tbl
);
3718 acpi_desc
= devm_kzalloc(dev
, sizeof(*acpi_desc
), GFP_KERNEL
);
3721 acpi_nfit_desc_init(acpi_desc
, &adev
->dev
);
3723 /* Save the acpi header for exporting the revision via sysfs */
3724 acpi_desc
->acpi_header
= *tbl
;
3726 /* Evaluate _FIT and override with that if present */
3727 status
= acpi_evaluate_object(adev
->handle
, "_FIT", NULL
, &buf
);
3728 if (ACPI_SUCCESS(status
) && buf
.length
> 0) {
3729 union acpi_object
*obj
= buf
.pointer
;
3731 if (obj
->type
== ACPI_TYPE_BUFFER
)
3732 rc
= acpi_nfit_init(acpi_desc
, obj
->buffer
.pointer
,
3733 obj
->buffer
.length
);
3735 dev_dbg(dev
, "invalid type %d, ignoring _FIT\n",
3739 /* skip over the lead-in header table */
3740 rc
= acpi_nfit_init(acpi_desc
, (void *) tbl
3741 + sizeof(struct acpi_table_nfit
),
3742 sz
- sizeof(struct acpi_table_nfit
));
3746 return devm_add_action_or_reset(dev
, acpi_nfit_shutdown
, acpi_desc
);
3749 static int acpi_nfit_remove(struct acpi_device
*adev
)
3751 /* see acpi_nfit_unregister */
3755 static void acpi_nfit_update_notify(struct device
*dev
, acpi_handle handle
)
3757 struct acpi_nfit_desc
*acpi_desc
= dev_get_drvdata(dev
);
3758 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
3759 union acpi_object
*obj
;
3764 /* dev->driver may be null if we're being removed */
3765 dev_dbg(dev
, "no driver found for dev\n");
3770 acpi_desc
= devm_kzalloc(dev
, sizeof(*acpi_desc
), GFP_KERNEL
);
3773 acpi_nfit_desc_init(acpi_desc
, dev
);
3776 * Finish previous registration before considering new
3779 flush_workqueue(nfit_wq
);
3783 status
= acpi_evaluate_object(handle
, "_FIT", NULL
, &buf
);
3784 if (ACPI_FAILURE(status
)) {
3785 dev_err(dev
, "failed to evaluate _FIT\n");
3790 if (obj
->type
== ACPI_TYPE_BUFFER
) {
3791 ret
= acpi_nfit_init(acpi_desc
, obj
->buffer
.pointer
,
3792 obj
->buffer
.length
);
3794 dev_err(dev
, "failed to merge updated NFIT\n");
3796 dev_err(dev
, "Invalid _FIT\n");
3800 static void acpi_nfit_uc_error_notify(struct device
*dev
, acpi_handle handle
)
3802 struct acpi_nfit_desc
*acpi_desc
= dev_get_drvdata(dev
);
3804 if (acpi_desc
->scrub_mode
== HW_ERROR_SCRUB_ON
)
3805 acpi_nfit_ars_rescan(acpi_desc
, ARS_REQ_LONG
);
3807 acpi_nfit_ars_rescan(acpi_desc
, ARS_REQ_SHORT
);
3810 void __acpi_nfit_notify(struct device
*dev
, acpi_handle handle
, u32 event
)
3812 dev_dbg(dev
, "event: 0x%x\n", event
);
3815 case NFIT_NOTIFY_UPDATE
:
3816 return acpi_nfit_update_notify(dev
, handle
);
3817 case NFIT_NOTIFY_UC_MEMORY_ERROR
:
3818 return acpi_nfit_uc_error_notify(dev
, handle
);
3823 EXPORT_SYMBOL_GPL(__acpi_nfit_notify
);
3825 static void acpi_nfit_notify(struct acpi_device
*adev
, u32 event
)
3827 nfit_device_lock(&adev
->dev
);
3828 __acpi_nfit_notify(&adev
->dev
, adev
->handle
, event
);
3829 nfit_device_unlock(&adev
->dev
);
3832 static const struct acpi_device_id acpi_nfit_ids
[] = {
3836 MODULE_DEVICE_TABLE(acpi
, acpi_nfit_ids
);
3838 static struct acpi_driver acpi_nfit_driver
= {
3839 .name
= KBUILD_MODNAME
,
3840 .ids
= acpi_nfit_ids
,
3842 .add
= acpi_nfit_add
,
3843 .remove
= acpi_nfit_remove
,
3844 .notify
= acpi_nfit_notify
,
3848 static __init
int nfit_init(void)
3852 BUILD_BUG_ON(sizeof(struct acpi_table_nfit
) != 40);
3853 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address
) != 56);
3854 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map
) != 48);
3855 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave
) != 20);
3856 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios
) != 9);
3857 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region
) != 80);
3858 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region
) != 40);
3859 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities
) != 16);
3861 guid_parse(UUID_VOLATILE_MEMORY
, &nfit_uuid
[NFIT_SPA_VOLATILE
]);
3862 guid_parse(UUID_PERSISTENT_MEMORY
, &nfit_uuid
[NFIT_SPA_PM
]);
3863 guid_parse(UUID_CONTROL_REGION
, &nfit_uuid
[NFIT_SPA_DCR
]);
3864 guid_parse(UUID_DATA_REGION
, &nfit_uuid
[NFIT_SPA_BDW
]);
3865 guid_parse(UUID_VOLATILE_VIRTUAL_DISK
, &nfit_uuid
[NFIT_SPA_VDISK
]);
3866 guid_parse(UUID_VOLATILE_VIRTUAL_CD
, &nfit_uuid
[NFIT_SPA_VCD
]);
3867 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK
, &nfit_uuid
[NFIT_SPA_PDISK
]);
3868 guid_parse(UUID_PERSISTENT_VIRTUAL_CD
, &nfit_uuid
[NFIT_SPA_PCD
]);
3869 guid_parse(UUID_NFIT_BUS
, &nfit_uuid
[NFIT_DEV_BUS
]);
3870 guid_parse(UUID_NFIT_DIMM
, &nfit_uuid
[NFIT_DEV_DIMM
]);
3871 guid_parse(UUID_NFIT_DIMM_N_HPE1
, &nfit_uuid
[NFIT_DEV_DIMM_N_HPE1
]);
3872 guid_parse(UUID_NFIT_DIMM_N_HPE2
, &nfit_uuid
[NFIT_DEV_DIMM_N_HPE2
]);
3873 guid_parse(UUID_NFIT_DIMM_N_MSFT
, &nfit_uuid
[NFIT_DEV_DIMM_N_MSFT
]);
3874 guid_parse(UUID_NFIT_DIMM_N_HYPERV
, &nfit_uuid
[NFIT_DEV_DIMM_N_HYPERV
]);
3875 guid_parse(UUID_INTEL_BUS
, &nfit_uuid
[NFIT_BUS_INTEL
]);
3877 nfit_wq
= create_singlethread_workqueue("nfit");
3881 nfit_mce_register();
3882 ret
= acpi_bus_register_driver(&acpi_nfit_driver
);
3884 nfit_mce_unregister();
3885 destroy_workqueue(nfit_wq
);
3892 static __exit
void nfit_exit(void)
3894 nfit_mce_unregister();
3895 acpi_bus_unregister_driver(&acpi_nfit_driver
);
3896 destroy_workqueue(nfit_wq
);
3897 WARN_ON(!list_empty(&acpi_descs
));
3900 module_init(nfit_init
);
3901 module_exit(nfit_exit
);
3902 MODULE_LICENSE("GPL v2");
3903 MODULE_AUTHOR("Intel Corporation");