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/mutex.h>
9 #include <linux/ndctl.h>
10 #include <linux/sysfs.h>
11 #include <linux/delay.h>
12 #include <linux/list.h>
13 #include <linux/acpi.h>
14 #include <linux/sort.h>
17 #include <asm/cacheflush.h>
18 #include <acpi/nfit.h>
23 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
26 #include <linux/io-64-nonatomic-hi-lo.h>
28 static bool force_enable_dimms;
29 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
30 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
32 static bool disable_vendor_specific;
33 module_param(disable_vendor_specific, bool, S_IRUGO);
34 MODULE_PARM_DESC(disable_vendor_specific,
35 "Limit commands to the publicly specified set");
37 static unsigned long override_dsm_mask;
38 module_param(override_dsm_mask, ulong, S_IRUGO);
39 MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
41 static int default_dsm_family = -1;
42 module_param(default_dsm_family, int, S_IRUGO);
43 MODULE_PARM_DESC(default_dsm_family,
44 "Try this DSM type first when identifying NVDIMM family");
46 static bool no_init_ars;
47 module_param(no_init_ars, bool, 0644);
48 MODULE_PARM_DESC(no_init_ars, "Skip ARS run at nfit init time");
50 static bool force_labels;
51 module_param(force_labels, bool, 0444);
52 MODULE_PARM_DESC(force_labels, "Opt-in to labels despite missing methods");
54 LIST_HEAD(acpi_descs);
55 DEFINE_MUTEX(acpi_desc_lock);
57 static struct workqueue_struct *nfit_wq;
59 struct nfit_table_prev {
60 struct list_head spas;
61 struct list_head memdevs;
62 struct list_head dcrs;
63 struct list_head bdws;
64 struct list_head idts;
65 struct list_head flushes;
68 static guid_t nfit_uuid[NFIT_UUID_MAX];
70 const guid_t *to_nfit_uuid(enum nfit_uuids id)
72 return &nfit_uuid[id];
74 EXPORT_SYMBOL(to_nfit_uuid);
76 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
78 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
81 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
84 if (!nd_desc->provider_name
85 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
88 return to_acpi_device(acpi_desc->dev);
91 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
93 struct nd_cmd_clear_error *clear_err;
94 struct nd_cmd_ars_status *ars_status;
99 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
106 /* No supported scan types for this range */
107 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
108 if ((status >> 16 & flags) == 0)
111 case ND_CMD_ARS_START:
112 /* ARS is in progress */
113 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
120 case ND_CMD_ARS_STATUS:
125 /* Check extended status (Upper two bytes) */
126 if (status == NFIT_ARS_STATUS_DONE)
129 /* ARS is in progress */
130 if (status == NFIT_ARS_STATUS_BUSY)
133 /* No ARS performed for the current boot */
134 if (status == NFIT_ARS_STATUS_NONE)
138 * ARS interrupted, either we overflowed or some other
139 * agent wants the scan to stop. If we didn't overflow
140 * then just continue with the returned results.
142 if (status == NFIT_ARS_STATUS_INTR) {
143 if (ars_status->out_length >= 40 && (ars_status->flags
144 & NFIT_ARS_F_OVERFLOW))
153 case ND_CMD_CLEAR_ERROR:
157 if (!clear_err->cleared)
159 if (clear_err->length > clear_err->cleared)
160 return clear_err->cleared;
166 /* all other non-zero status results in an error */
172 #define ACPI_LABELS_LOCKED 3
174 static int xlat_nvdimm_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
177 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
180 case ND_CMD_GET_CONFIG_SIZE:
182 * In the _LSI, _LSR, _LSW case the locked status is
183 * communicated via the read/write commands
185 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
188 if (status >> 16 & ND_CONFIG_LOCKED)
191 case ND_CMD_GET_CONFIG_DATA:
192 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
193 && status == ACPI_LABELS_LOCKED)
196 case ND_CMD_SET_CONFIG_DATA:
197 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
198 && status == ACPI_LABELS_LOCKED)
205 /* all other non-zero status results in an error */
211 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
215 return xlat_bus_status(buf, cmd, status);
216 return xlat_nvdimm_status(nvdimm, buf, cmd, status);
219 /* convert _LS{I,R} packages to the buffer object acpi_nfit_ctl expects */
220 static union acpi_object *pkg_to_buf(union acpi_object *pkg)
225 union acpi_object *buf = NULL;
227 if (pkg->type != ACPI_TYPE_PACKAGE) {
228 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
233 for (i = 0; i < pkg->package.count; i++) {
234 union acpi_object *obj = &pkg->package.elements[i];
236 if (obj->type == ACPI_TYPE_INTEGER)
238 else if (obj->type == ACPI_TYPE_BUFFER)
239 size += obj->buffer.length;
241 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
247 buf = ACPI_ALLOCATE(sizeof(*buf) + size);
252 buf->type = ACPI_TYPE_BUFFER;
253 buf->buffer.length = size;
254 buf->buffer.pointer = dst;
255 for (i = 0; i < pkg->package.count; i++) {
256 union acpi_object *obj = &pkg->package.elements[i];
258 if (obj->type == ACPI_TYPE_INTEGER) {
259 memcpy(dst, &obj->integer.value, 4);
261 } else if (obj->type == ACPI_TYPE_BUFFER) {
262 memcpy(dst, obj->buffer.pointer, obj->buffer.length);
263 dst += obj->buffer.length;
271 static union acpi_object *int_to_buf(union acpi_object *integer)
273 union acpi_object *buf = ACPI_ALLOCATE(sizeof(*buf) + 4);
279 if (integer->type != ACPI_TYPE_INTEGER) {
280 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
286 buf->type = ACPI_TYPE_BUFFER;
287 buf->buffer.length = 4;
288 buf->buffer.pointer = dst;
289 memcpy(dst, &integer->integer.value, 4);
295 static union acpi_object *acpi_label_write(acpi_handle handle, u32 offset,
299 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
300 struct acpi_object_list input = {
302 .pointer = (union acpi_object []) {
304 .integer.type = ACPI_TYPE_INTEGER,
305 .integer.value = offset,
308 .integer.type = ACPI_TYPE_INTEGER,
309 .integer.value = len,
312 .buffer.type = ACPI_TYPE_BUFFER,
313 .buffer.pointer = data,
314 .buffer.length = len,
319 rc = acpi_evaluate_object(handle, "_LSW", &input, &buf);
320 if (ACPI_FAILURE(rc))
322 return int_to_buf(buf.pointer);
325 static union acpi_object *acpi_label_read(acpi_handle handle, u32 offset,
329 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
330 struct acpi_object_list input = {
332 .pointer = (union acpi_object []) {
334 .integer.type = ACPI_TYPE_INTEGER,
335 .integer.value = offset,
338 .integer.type = ACPI_TYPE_INTEGER,
339 .integer.value = len,
344 rc = acpi_evaluate_object(handle, "_LSR", &input, &buf);
345 if (ACPI_FAILURE(rc))
347 return pkg_to_buf(buf.pointer);
350 static union acpi_object *acpi_label_info(acpi_handle handle)
353 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
355 rc = acpi_evaluate_object(handle, "_LSI", NULL, &buf);
356 if (ACPI_FAILURE(rc))
358 return pkg_to_buf(buf.pointer);
361 static u8 nfit_dsm_revid(unsigned family, unsigned func)
363 static const u8 revid_table[NVDIMM_FAMILY_MAX+1][32] = {
364 [NVDIMM_FAMILY_INTEL] = {
365 [NVDIMM_INTEL_GET_MODES] = 2,
366 [NVDIMM_INTEL_GET_FWINFO] = 2,
367 [NVDIMM_INTEL_START_FWUPDATE] = 2,
368 [NVDIMM_INTEL_SEND_FWUPDATE] = 2,
369 [NVDIMM_INTEL_FINISH_FWUPDATE] = 2,
370 [NVDIMM_INTEL_QUERY_FWUPDATE] = 2,
371 [NVDIMM_INTEL_SET_THRESHOLD] = 2,
372 [NVDIMM_INTEL_INJECT_ERROR] = 2,
373 [NVDIMM_INTEL_GET_SECURITY_STATE] = 2,
374 [NVDIMM_INTEL_SET_PASSPHRASE] = 2,
375 [NVDIMM_INTEL_DISABLE_PASSPHRASE] = 2,
376 [NVDIMM_INTEL_UNLOCK_UNIT] = 2,
377 [NVDIMM_INTEL_FREEZE_LOCK] = 2,
378 [NVDIMM_INTEL_SECURE_ERASE] = 2,
379 [NVDIMM_INTEL_OVERWRITE] = 2,
380 [NVDIMM_INTEL_QUERY_OVERWRITE] = 2,
381 [NVDIMM_INTEL_SET_MASTER_PASSPHRASE] = 2,
382 [NVDIMM_INTEL_MASTER_SECURE_ERASE] = 2,
387 if (family > NVDIMM_FAMILY_MAX)
391 id = revid_table[family][func];
393 return 1; /* default */
397 static bool payload_dumpable(struct nvdimm *nvdimm, unsigned int func)
399 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
401 if (nfit_mem && nfit_mem->family == NVDIMM_FAMILY_INTEL
402 && func >= NVDIMM_INTEL_GET_SECURITY_STATE
403 && func <= NVDIMM_INTEL_MASTER_SECURE_ERASE)
404 return IS_ENABLED(CONFIG_NFIT_SECURITY_DEBUG);
408 static int cmd_to_func(struct nfit_mem *nfit_mem, unsigned int cmd,
409 struct nd_cmd_pkg *call_pkg)
414 if (nfit_mem && nfit_mem->family != call_pkg->nd_family)
417 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
418 if (call_pkg->nd_reserved2[i])
420 return call_pkg->nd_command;
423 /* In the !call_pkg case, bus commands == bus functions */
427 /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
428 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
432 * Force function number validation to fail since 0 is never
433 * published as a valid function in dsm_mask.
438 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
439 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
441 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
442 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
443 union acpi_object in_obj, in_buf, *out_obj;
444 const struct nd_cmd_desc *desc = NULL;
445 struct device *dev = acpi_desc->dev;
446 struct nd_cmd_pkg *call_pkg = NULL;
447 const char *cmd_name, *dimm_name;
448 unsigned long cmd_mask, dsm_mask;
449 u32 offset, fw_status = 0;
457 if (cmd == ND_CMD_CALL)
459 func = cmd_to_func(nfit_mem, cmd, call_pkg);
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 dsm_mask = nd_desc->bus_dsm_mask;
482 desc = nd_cmd_bus_desc(cmd);
483 guid = to_nfit_uuid(NFIT_DEV_BUS);
484 handle = adev->handle;
488 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
492 * Check for a valid command. For ND_CMD_CALL, we also have to
493 * make sure that the DSM function is supported.
495 if (cmd == ND_CMD_CALL && !test_bit(func, &dsm_mask))
497 else if (!test_bit(cmd, &cmd_mask))
500 in_obj.type = ACPI_TYPE_PACKAGE;
501 in_obj.package.count = 1;
502 in_obj.package.elements = &in_buf;
503 in_buf.type = ACPI_TYPE_BUFFER;
504 in_buf.buffer.pointer = buf;
505 in_buf.buffer.length = 0;
507 /* libnvdimm has already validated the input envelope */
508 for (i = 0; i < desc->in_num; i++)
509 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
513 /* skip over package wrapper */
514 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
515 in_buf.buffer.length = call_pkg->nd_size_in;
518 dev_dbg(dev, "%s cmd: %d: func: %d input length: %d\n",
519 dimm_name, cmd, func, in_buf.buffer.length);
520 if (payload_dumpable(nvdimm, func))
521 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
522 in_buf.buffer.pointer,
523 min_t(u32, 256, in_buf.buffer.length), true);
525 /* call the BIOS, prefer the named methods over _DSM if available */
526 if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE
527 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
528 out_obj = acpi_label_info(handle);
529 else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA
530 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
531 struct nd_cmd_get_config_data_hdr *p = buf;
533 out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
534 } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
535 && test_bit(NFIT_MEM_LSW, &nfit_mem->flags)) {
536 struct nd_cmd_set_config_hdr *p = buf;
538 out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
544 revid = nfit_dsm_revid(nfit_mem->family, func);
547 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
551 dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
555 if (out_obj->type != ACPI_TYPE_BUFFER) {
556 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
557 dimm_name, cmd_name, out_obj->type);
562 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
563 cmd_name, out_obj->buffer.length);
564 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
565 out_obj->buffer.pointer,
566 min_t(u32, 128, out_obj->buffer.length), true);
569 call_pkg->nd_fw_size = out_obj->buffer.length;
570 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
571 out_obj->buffer.pointer,
572 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
576 * Need to support FW function w/o known size in advance.
577 * Caller can determine required size based upon nd_fw_size.
578 * If we return an error (like elsewhere) then caller wouldn't
579 * be able to rely upon data returned to make calculation.
586 for (i = 0, offset = 0; i < desc->out_num; i++) {
587 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
588 (u32 *) out_obj->buffer.pointer,
589 out_obj->buffer.length - offset);
591 if (offset + out_size > out_obj->buffer.length) {
592 dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
593 dimm_name, cmd_name, i);
597 if (in_buf.buffer.length + offset + out_size > buf_len) {
598 dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
599 dimm_name, cmd_name, i);
603 memcpy(buf + in_buf.buffer.length + offset,
604 out_obj->buffer.pointer + offset, out_size);
609 * Set fw_status for all the commands with a known format to be
610 * later interpreted by xlat_status().
612 if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
613 && cmd <= ND_CMD_CLEAR_ERROR)
614 || (nvdimm && cmd >= ND_CMD_SMART
615 && cmd <= ND_CMD_VENDOR)))
616 fw_status = *(u32 *) out_obj->buffer.pointer;
618 if (offset + in_buf.buffer.length < buf_len) {
621 * status valid, return the number of bytes left
622 * unfilled in the output buffer
624 rc = buf_len - offset - in_buf.buffer.length;
626 *cmd_rc = xlat_status(nvdimm, buf, cmd,
629 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
630 __func__, dimm_name, cmd_name, buf_len,
637 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
645 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
647 static const char *spa_type_name(u16 type)
649 static const char *to_name[] = {
650 [NFIT_SPA_VOLATILE] = "volatile",
651 [NFIT_SPA_PM] = "pmem",
652 [NFIT_SPA_DCR] = "dimm-control-region",
653 [NFIT_SPA_BDW] = "block-data-window",
654 [NFIT_SPA_VDISK] = "volatile-disk",
655 [NFIT_SPA_VCD] = "volatile-cd",
656 [NFIT_SPA_PDISK] = "persistent-disk",
657 [NFIT_SPA_PCD] = "persistent-cd",
661 if (type > NFIT_SPA_PCD)
664 return to_name[type];
667 int nfit_spa_type(struct acpi_nfit_system_address *spa)
671 for (i = 0; i < NFIT_UUID_MAX; i++)
672 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
677 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
678 struct nfit_table_prev *prev,
679 struct acpi_nfit_system_address *spa)
681 struct device *dev = acpi_desc->dev;
682 struct nfit_spa *nfit_spa;
684 if (spa->header.length != sizeof(*spa))
687 list_for_each_entry(nfit_spa, &prev->spas, list) {
688 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
689 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
694 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
698 INIT_LIST_HEAD(&nfit_spa->list);
699 memcpy(nfit_spa->spa, spa, sizeof(*spa));
700 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
701 dev_dbg(dev, "spa index: %d type: %s\n",
703 spa_type_name(nfit_spa_type(spa)));
707 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
708 struct nfit_table_prev *prev,
709 struct acpi_nfit_memory_map *memdev)
711 struct device *dev = acpi_desc->dev;
712 struct nfit_memdev *nfit_memdev;
714 if (memdev->header.length != sizeof(*memdev))
717 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
718 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
719 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
723 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
727 INIT_LIST_HEAD(&nfit_memdev->list);
728 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
729 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
730 dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
731 memdev->device_handle, memdev->range_index,
732 memdev->region_index, memdev->flags);
736 int nfit_get_smbios_id(u32 device_handle, u16 *flags)
738 struct acpi_nfit_memory_map *memdev;
739 struct acpi_nfit_desc *acpi_desc;
740 struct nfit_mem *nfit_mem;
743 mutex_lock(&acpi_desc_lock);
744 list_for_each_entry(acpi_desc, &acpi_descs, list) {
745 mutex_lock(&acpi_desc->init_mutex);
746 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
747 memdev = __to_nfit_memdev(nfit_mem);
748 if (memdev->device_handle == device_handle) {
749 *flags = memdev->flags;
750 physical_id = memdev->physical_id;
751 mutex_unlock(&acpi_desc->init_mutex);
752 mutex_unlock(&acpi_desc_lock);
756 mutex_unlock(&acpi_desc->init_mutex);
758 mutex_unlock(&acpi_desc_lock);
762 EXPORT_SYMBOL_GPL(nfit_get_smbios_id);
765 * An implementation may provide a truncated control region if no block windows
768 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
770 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
775 return offsetof(struct acpi_nfit_control_region, window_size);
778 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
779 struct nfit_table_prev *prev,
780 struct acpi_nfit_control_region *dcr)
782 struct device *dev = acpi_desc->dev;
783 struct nfit_dcr *nfit_dcr;
785 if (!sizeof_dcr(dcr))
788 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
789 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
790 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
794 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
798 INIT_LIST_HEAD(&nfit_dcr->list);
799 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
800 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
801 dev_dbg(dev, "dcr index: %d windows: %d\n",
802 dcr->region_index, dcr->windows);
806 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
807 struct nfit_table_prev *prev,
808 struct acpi_nfit_data_region *bdw)
810 struct device *dev = acpi_desc->dev;
811 struct nfit_bdw *nfit_bdw;
813 if (bdw->header.length != sizeof(*bdw))
815 list_for_each_entry(nfit_bdw, &prev->bdws, list)
816 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
817 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
821 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
825 INIT_LIST_HEAD(&nfit_bdw->list);
826 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
827 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
828 dev_dbg(dev, "bdw dcr: %d windows: %d\n",
829 bdw->region_index, bdw->windows);
833 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
835 if (idt->header.length < sizeof(*idt))
837 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
840 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
841 struct nfit_table_prev *prev,
842 struct acpi_nfit_interleave *idt)
844 struct device *dev = acpi_desc->dev;
845 struct nfit_idt *nfit_idt;
847 if (!sizeof_idt(idt))
850 list_for_each_entry(nfit_idt, &prev->idts, list) {
851 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
854 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
855 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
860 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
864 INIT_LIST_HEAD(&nfit_idt->list);
865 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
866 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
867 dev_dbg(dev, "idt index: %d num_lines: %d\n",
868 idt->interleave_index, idt->line_count);
872 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
874 if (flush->header.length < sizeof(*flush))
876 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
879 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
880 struct nfit_table_prev *prev,
881 struct acpi_nfit_flush_address *flush)
883 struct device *dev = acpi_desc->dev;
884 struct nfit_flush *nfit_flush;
886 if (!sizeof_flush(flush))
889 list_for_each_entry(nfit_flush, &prev->flushes, list) {
890 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
893 if (memcmp(nfit_flush->flush, flush,
894 sizeof_flush(flush)) == 0) {
895 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
900 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
901 + sizeof_flush(flush), GFP_KERNEL);
904 INIT_LIST_HEAD(&nfit_flush->list);
905 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
906 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
907 dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
908 flush->device_handle, flush->hint_count);
912 static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
913 struct acpi_nfit_capabilities *pcap)
915 struct device *dev = acpi_desc->dev;
918 mask = (1 << (pcap->highest_capability + 1)) - 1;
919 acpi_desc->platform_cap = pcap->capabilities & mask;
920 dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
924 static void *add_table(struct acpi_nfit_desc *acpi_desc,
925 struct nfit_table_prev *prev, void *table, const void *end)
927 struct device *dev = acpi_desc->dev;
928 struct acpi_nfit_header *hdr;
929 void *err = ERR_PTR(-ENOMEM);
936 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
942 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
943 if (!add_spa(acpi_desc, prev, table))
946 case ACPI_NFIT_TYPE_MEMORY_MAP:
947 if (!add_memdev(acpi_desc, prev, table))
950 case ACPI_NFIT_TYPE_CONTROL_REGION:
951 if (!add_dcr(acpi_desc, prev, table))
954 case ACPI_NFIT_TYPE_DATA_REGION:
955 if (!add_bdw(acpi_desc, prev, table))
958 case ACPI_NFIT_TYPE_INTERLEAVE:
959 if (!add_idt(acpi_desc, prev, table))
962 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
963 if (!add_flush(acpi_desc, prev, table))
966 case ACPI_NFIT_TYPE_SMBIOS:
967 dev_dbg(dev, "smbios\n");
969 case ACPI_NFIT_TYPE_CAPABILITIES:
970 if (!add_platform_cap(acpi_desc, table))
974 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
978 return table + hdr->length;
981 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
982 struct nfit_mem *nfit_mem)
984 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
985 u16 dcr = nfit_mem->dcr->region_index;
986 struct nfit_spa *nfit_spa;
988 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
989 u16 range_index = nfit_spa->spa->range_index;
990 int type = nfit_spa_type(nfit_spa->spa);
991 struct nfit_memdev *nfit_memdev;
993 if (type != NFIT_SPA_BDW)
996 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
997 if (nfit_memdev->memdev->range_index != range_index)
999 if (nfit_memdev->memdev->device_handle != device_handle)
1001 if (nfit_memdev->memdev->region_index != dcr)
1004 nfit_mem->spa_bdw = nfit_spa->spa;
1009 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
1010 nfit_mem->spa_dcr->range_index);
1011 nfit_mem->bdw = NULL;
1014 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
1015 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
1017 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
1018 struct nfit_memdev *nfit_memdev;
1019 struct nfit_bdw *nfit_bdw;
1020 struct nfit_idt *nfit_idt;
1021 u16 idt_idx, range_index;
1023 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
1024 if (nfit_bdw->bdw->region_index != dcr)
1026 nfit_mem->bdw = nfit_bdw->bdw;
1033 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
1035 if (!nfit_mem->spa_bdw)
1038 range_index = nfit_mem->spa_bdw->range_index;
1039 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1040 if (nfit_memdev->memdev->range_index != range_index ||
1041 nfit_memdev->memdev->region_index != dcr)
1043 nfit_mem->memdev_bdw = nfit_memdev->memdev;
1044 idt_idx = nfit_memdev->memdev->interleave_index;
1045 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1046 if (nfit_idt->idt->interleave_index != idt_idx)
1048 nfit_mem->idt_bdw = nfit_idt->idt;
1055 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
1056 struct acpi_nfit_system_address *spa)
1058 struct nfit_mem *nfit_mem, *found;
1059 struct nfit_memdev *nfit_memdev;
1060 int type = spa ? nfit_spa_type(spa) : 0;
1072 * This loop runs in two modes, when a dimm is mapped the loop
1073 * adds memdev associations to an existing dimm, or creates a
1074 * dimm. In the unmapped dimm case this loop sweeps for memdev
1075 * instances with an invalid / zero range_index and adds those
1076 * dimms without spa associations.
1078 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1079 struct nfit_flush *nfit_flush;
1080 struct nfit_dcr *nfit_dcr;
1084 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
1086 if (!spa && nfit_memdev->memdev->range_index)
1089 dcr = nfit_memdev->memdev->region_index;
1090 device_handle = nfit_memdev->memdev->device_handle;
1091 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1092 if (__to_nfit_memdev(nfit_mem)->device_handle
1101 nfit_mem = devm_kzalloc(acpi_desc->dev,
1102 sizeof(*nfit_mem), GFP_KERNEL);
1105 INIT_LIST_HEAD(&nfit_mem->list);
1106 nfit_mem->acpi_desc = acpi_desc;
1107 list_add(&nfit_mem->list, &acpi_desc->dimms);
1110 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1111 if (nfit_dcr->dcr->region_index != dcr)
1114 * Record the control region for the dimm. For
1115 * the ACPI 6.1 case, where there are separate
1116 * control regions for the pmem vs blk
1117 * interfaces, be sure to record the extended
1121 nfit_mem->dcr = nfit_dcr->dcr;
1122 else if (nfit_mem->dcr->windows == 0
1123 && nfit_dcr->dcr->windows)
1124 nfit_mem->dcr = nfit_dcr->dcr;
1128 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
1129 struct acpi_nfit_flush_address *flush;
1132 if (nfit_flush->flush->device_handle != device_handle)
1134 nfit_mem->nfit_flush = nfit_flush;
1135 flush = nfit_flush->flush;
1136 nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
1138 sizeof(struct resource),
1140 if (!nfit_mem->flush_wpq)
1142 for (i = 0; i < flush->hint_count; i++) {
1143 struct resource *res = &nfit_mem->flush_wpq[i];
1145 res->start = flush->hint_address[i];
1146 res->end = res->start + 8 - 1;
1151 if (dcr && !nfit_mem->dcr) {
1152 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
1153 spa->range_index, dcr);
1157 if (type == NFIT_SPA_DCR) {
1158 struct nfit_idt *nfit_idt;
1161 /* multiple dimms may share a SPA when interleaved */
1162 nfit_mem->spa_dcr = spa;
1163 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1164 idt_idx = nfit_memdev->memdev->interleave_index;
1165 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1166 if (nfit_idt->idt->interleave_index != idt_idx)
1168 nfit_mem->idt_dcr = nfit_idt->idt;
1171 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
1172 } else if (type == NFIT_SPA_PM) {
1174 * A single dimm may belong to multiple SPA-PM
1175 * ranges, record at least one in addition to
1176 * any SPA-DCR range.
1178 nfit_mem->memdev_pmem = nfit_memdev->memdev;
1180 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1186 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
1188 struct nfit_mem *a = container_of(_a, typeof(*a), list);
1189 struct nfit_mem *b = container_of(_b, typeof(*b), list);
1190 u32 handleA, handleB;
1192 handleA = __to_nfit_memdev(a)->device_handle;
1193 handleB = __to_nfit_memdev(b)->device_handle;
1194 if (handleA < handleB)
1196 else if (handleA > handleB)
1201 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
1203 struct nfit_spa *nfit_spa;
1208 * For each SPA-DCR or SPA-PMEM address range find its
1209 * corresponding MEMDEV(s). From each MEMDEV find the
1210 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1211 * try to find a SPA-BDW and a corresponding BDW that references
1212 * the DCR. Throw it all into an nfit_mem object. Note, that
1213 * BDWs are optional.
1215 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1216 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
1222 * If a DIMM has failed to be mapped into SPA there will be no
1223 * SPA entries above. Find and register all the unmapped DIMMs
1224 * for reporting and recovery purposes.
1226 rc = __nfit_mem_init(acpi_desc, NULL);
1230 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
1235 static ssize_t bus_dsm_mask_show(struct device *dev,
1236 struct device_attribute *attr, char *buf)
1238 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1239 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1241 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
1243 static struct device_attribute dev_attr_bus_dsm_mask =
1244 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
1246 static ssize_t revision_show(struct device *dev,
1247 struct device_attribute *attr, char *buf)
1249 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1250 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1251 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1253 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
1255 static DEVICE_ATTR_RO(revision);
1257 static ssize_t hw_error_scrub_show(struct device *dev,
1258 struct device_attribute *attr, char *buf)
1260 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1261 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1262 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1264 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
1268 * The 'hw_error_scrub' attribute can have the following values written to it:
1269 * '0': Switch to the default mode where an exception will only insert
1270 * the address of the memory error into the poison and badblocks lists.
1271 * '1': Enable a full scrub to happen if an exception for a memory error is
1274 static ssize_t hw_error_scrub_store(struct device *dev,
1275 struct device_attribute *attr, const char *buf, size_t size)
1277 struct nvdimm_bus_descriptor *nd_desc;
1281 rc = kstrtol(buf, 0, &val);
1285 nfit_device_lock(dev);
1286 nd_desc = dev_get_drvdata(dev);
1288 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1291 case HW_ERROR_SCRUB_ON:
1292 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1294 case HW_ERROR_SCRUB_OFF:
1295 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1302 nfit_device_unlock(dev);
1307 static DEVICE_ATTR_RW(hw_error_scrub);
1310 * This shows the number of full Address Range Scrubs that have been
1311 * completed since driver load time. Userspace can wait on this using
1312 * select/poll etc. A '+' at the end indicates an ARS is in progress
1314 static ssize_t scrub_show(struct device *dev,
1315 struct device_attribute *attr, char *buf)
1317 struct nvdimm_bus_descriptor *nd_desc;
1318 struct acpi_nfit_desc *acpi_desc;
1319 ssize_t rc = -ENXIO;
1322 nfit_device_lock(dev);
1323 nd_desc = dev_get_drvdata(dev);
1328 acpi_desc = to_acpi_desc(nd_desc);
1330 mutex_lock(&acpi_desc->init_mutex);
1331 busy = test_bit(ARS_BUSY, &acpi_desc->scrub_flags)
1332 && !test_bit(ARS_CANCEL, &acpi_desc->scrub_flags);
1333 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count, busy ? "+\n" : "\n");
1334 /* Allow an admin to poll the busy state at a higher rate */
1335 if (busy && capable(CAP_SYS_RAWIO) && !test_and_set_bit(ARS_POLL,
1336 &acpi_desc->scrub_flags)) {
1337 acpi_desc->scrub_tmo = 1;
1338 mod_delayed_work(nfit_wq, &acpi_desc->dwork, HZ);
1341 mutex_unlock(&acpi_desc->init_mutex);
1342 nfit_device_unlock(dev);
1346 static ssize_t scrub_store(struct device *dev,
1347 struct device_attribute *attr, const char *buf, size_t size)
1349 struct nvdimm_bus_descriptor *nd_desc;
1353 rc = kstrtol(buf, 0, &val);
1359 nfit_device_lock(dev);
1360 nd_desc = dev_get_drvdata(dev);
1362 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1364 rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
1366 nfit_device_unlock(dev);
1371 static DEVICE_ATTR_RW(scrub);
1373 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1375 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1376 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1377 | 1 << ND_CMD_ARS_STATUS;
1379 return (nd_desc->cmd_mask & mask) == mask;
1382 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1384 struct device *dev = container_of(kobj, struct device, kobj);
1385 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1387 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1392 static struct attribute *acpi_nfit_attributes[] = {
1393 &dev_attr_revision.attr,
1394 &dev_attr_scrub.attr,
1395 &dev_attr_hw_error_scrub.attr,
1396 &dev_attr_bus_dsm_mask.attr,
1400 static const struct attribute_group acpi_nfit_attribute_group = {
1402 .attrs = acpi_nfit_attributes,
1403 .is_visible = nfit_visible,
1406 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1407 &nvdimm_bus_attribute_group,
1408 &acpi_nfit_attribute_group,
1412 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1414 struct nvdimm *nvdimm = to_nvdimm(dev);
1415 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1417 return __to_nfit_memdev(nfit_mem);
1420 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1422 struct nvdimm *nvdimm = to_nvdimm(dev);
1423 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1425 return nfit_mem->dcr;
1428 static ssize_t handle_show(struct device *dev,
1429 struct device_attribute *attr, char *buf)
1431 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1433 return sprintf(buf, "%#x\n", memdev->device_handle);
1435 static DEVICE_ATTR_RO(handle);
1437 static ssize_t phys_id_show(struct device *dev,
1438 struct device_attribute *attr, char *buf)
1440 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1442 return sprintf(buf, "%#x\n", memdev->physical_id);
1444 static DEVICE_ATTR_RO(phys_id);
1446 static ssize_t vendor_show(struct device *dev,
1447 struct device_attribute *attr, char *buf)
1449 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1451 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1453 static DEVICE_ATTR_RO(vendor);
1455 static ssize_t rev_id_show(struct device *dev,
1456 struct device_attribute *attr, char *buf)
1458 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1460 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1462 static DEVICE_ATTR_RO(rev_id);
1464 static ssize_t device_show(struct device *dev,
1465 struct device_attribute *attr, char *buf)
1467 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1469 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1471 static DEVICE_ATTR_RO(device);
1473 static ssize_t subsystem_vendor_show(struct device *dev,
1474 struct device_attribute *attr, char *buf)
1476 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1478 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1480 static DEVICE_ATTR_RO(subsystem_vendor);
1482 static ssize_t subsystem_rev_id_show(struct device *dev,
1483 struct device_attribute *attr, char *buf)
1485 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1487 return sprintf(buf, "0x%04x\n",
1488 be16_to_cpu(dcr->subsystem_revision_id));
1490 static DEVICE_ATTR_RO(subsystem_rev_id);
1492 static ssize_t subsystem_device_show(struct device *dev,
1493 struct device_attribute *attr, char *buf)
1495 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1497 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1499 static DEVICE_ATTR_RO(subsystem_device);
1501 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1503 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1506 if (nfit_mem->memdev_pmem)
1508 if (nfit_mem->memdev_bdw)
1513 static ssize_t format_show(struct device *dev,
1514 struct device_attribute *attr, char *buf)
1516 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1518 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1520 static DEVICE_ATTR_RO(format);
1522 static ssize_t format1_show(struct device *dev,
1523 struct device_attribute *attr, char *buf)
1526 ssize_t rc = -ENXIO;
1527 struct nfit_mem *nfit_mem;
1528 struct nfit_memdev *nfit_memdev;
1529 struct acpi_nfit_desc *acpi_desc;
1530 struct nvdimm *nvdimm = to_nvdimm(dev);
1531 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1533 nfit_mem = nvdimm_provider_data(nvdimm);
1534 acpi_desc = nfit_mem->acpi_desc;
1535 handle = to_nfit_memdev(dev)->device_handle;
1537 /* assumes DIMMs have at most 2 published interface codes */
1538 mutex_lock(&acpi_desc->init_mutex);
1539 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1540 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1541 struct nfit_dcr *nfit_dcr;
1543 if (memdev->device_handle != handle)
1546 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1547 if (nfit_dcr->dcr->region_index != memdev->region_index)
1549 if (nfit_dcr->dcr->code == dcr->code)
1551 rc = sprintf(buf, "0x%04x\n",
1552 le16_to_cpu(nfit_dcr->dcr->code));
1558 mutex_unlock(&acpi_desc->init_mutex);
1561 static DEVICE_ATTR_RO(format1);
1563 static ssize_t formats_show(struct device *dev,
1564 struct device_attribute *attr, char *buf)
1566 struct nvdimm *nvdimm = to_nvdimm(dev);
1568 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1570 static DEVICE_ATTR_RO(formats);
1572 static ssize_t serial_show(struct device *dev,
1573 struct device_attribute *attr, char *buf)
1575 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1577 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1579 static DEVICE_ATTR_RO(serial);
1581 static ssize_t family_show(struct device *dev,
1582 struct device_attribute *attr, char *buf)
1584 struct nvdimm *nvdimm = to_nvdimm(dev);
1585 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1587 if (nfit_mem->family < 0)
1589 return sprintf(buf, "%d\n", nfit_mem->family);
1591 static DEVICE_ATTR_RO(family);
1593 static ssize_t dsm_mask_show(struct device *dev,
1594 struct device_attribute *attr, char *buf)
1596 struct nvdimm *nvdimm = to_nvdimm(dev);
1597 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1599 if (nfit_mem->family < 0)
1601 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1603 static DEVICE_ATTR_RO(dsm_mask);
1605 static ssize_t flags_show(struct device *dev,
1606 struct device_attribute *attr, char *buf)
1608 struct nvdimm *nvdimm = to_nvdimm(dev);
1609 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1610 u16 flags = __to_nfit_memdev(nfit_mem)->flags;
1612 if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
1613 flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
1615 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1616 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1617 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1618 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1619 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1620 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1621 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1622 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1624 static DEVICE_ATTR_RO(flags);
1626 static ssize_t id_show(struct device *dev,
1627 struct device_attribute *attr, char *buf)
1629 struct nvdimm *nvdimm = to_nvdimm(dev);
1630 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1632 return sprintf(buf, "%s\n", nfit_mem->id);
1634 static DEVICE_ATTR_RO(id);
1636 static ssize_t dirty_shutdown_show(struct device *dev,
1637 struct device_attribute *attr, char *buf)
1639 struct nvdimm *nvdimm = to_nvdimm(dev);
1640 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1642 return sprintf(buf, "%d\n", nfit_mem->dirty_shutdown);
1644 static DEVICE_ATTR_RO(dirty_shutdown);
1646 static struct attribute *acpi_nfit_dimm_attributes[] = {
1647 &dev_attr_handle.attr,
1648 &dev_attr_phys_id.attr,
1649 &dev_attr_vendor.attr,
1650 &dev_attr_device.attr,
1651 &dev_attr_rev_id.attr,
1652 &dev_attr_subsystem_vendor.attr,
1653 &dev_attr_subsystem_device.attr,
1654 &dev_attr_subsystem_rev_id.attr,
1655 &dev_attr_format.attr,
1656 &dev_attr_formats.attr,
1657 &dev_attr_format1.attr,
1658 &dev_attr_serial.attr,
1659 &dev_attr_flags.attr,
1661 &dev_attr_family.attr,
1662 &dev_attr_dsm_mask.attr,
1663 &dev_attr_dirty_shutdown.attr,
1667 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1668 struct attribute *a, int n)
1670 struct device *dev = container_of(kobj, struct device, kobj);
1671 struct nvdimm *nvdimm = to_nvdimm(dev);
1672 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1674 if (!to_nfit_dcr(dev)) {
1675 /* Without a dcr only the memdev attributes can be surfaced */
1676 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1677 || a == &dev_attr_flags.attr
1678 || a == &dev_attr_family.attr
1679 || a == &dev_attr_dsm_mask.attr)
1684 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1687 if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
1688 && a == &dev_attr_dirty_shutdown.attr)
1694 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1696 .attrs = acpi_nfit_dimm_attributes,
1697 .is_visible = acpi_nfit_dimm_attr_visible,
1700 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1701 &nvdimm_attribute_group,
1702 &nd_device_attribute_group,
1703 &acpi_nfit_dimm_attribute_group,
1707 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1710 struct nfit_mem *nfit_mem;
1712 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1713 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1714 return nfit_mem->nvdimm;
1719 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1721 struct nfit_mem *nfit_mem;
1722 struct acpi_nfit_desc *acpi_desc;
1724 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
1727 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1728 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1733 acpi_desc = dev_get_drvdata(dev->parent);
1738 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1741 nfit_mem = dev_get_drvdata(dev);
1742 if (nfit_mem && nfit_mem->flags_attr)
1743 sysfs_notify_dirent(nfit_mem->flags_attr);
1745 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1747 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1749 struct acpi_device *adev = data;
1750 struct device *dev = &adev->dev;
1752 nfit_device_lock(dev->parent);
1753 __acpi_nvdimm_notify(dev, event);
1754 nfit_device_unlock(dev->parent);
1757 static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1762 status = acpi_get_handle(adev->handle, method, &handle);
1764 if (ACPI_SUCCESS(status))
1769 __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
1771 struct device *dev = &nfit_mem->adev->dev;
1772 struct nd_intel_smart smart = { 0 };
1773 union acpi_object in_buf = {
1774 .buffer.type = ACPI_TYPE_BUFFER,
1777 union acpi_object in_obj = {
1778 .package.type = ACPI_TYPE_PACKAGE,
1780 .package.elements = &in_buf,
1782 const u8 func = ND_INTEL_SMART;
1783 const guid_t *guid = to_nfit_uuid(nfit_mem->family);
1784 u8 revid = nfit_dsm_revid(nfit_mem->family, func);
1785 struct acpi_device *adev = nfit_mem->adev;
1786 acpi_handle handle = adev->handle;
1787 union acpi_object *out_obj;
1789 if ((nfit_mem->dsm_mask & (1 << func)) == 0)
1792 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
1793 if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER
1794 || out_obj->buffer.length < sizeof(smart)) {
1795 dev_dbg(dev->parent, "%s: failed to retrieve initial health\n",
1800 memcpy(&smart, out_obj->buffer.pointer, sizeof(smart));
1803 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
1804 if (smart.shutdown_state)
1805 set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
1808 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
1809 set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
1810 nfit_mem->dirty_shutdown = smart.shutdown_count;
1814 static void populate_shutdown_status(struct nfit_mem *nfit_mem)
1817 * For DIMMs that provide a dynamic facility to retrieve a
1818 * dirty-shutdown status and/or a dirty-shutdown count, cache
1819 * these values in nfit_mem.
1821 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1822 nfit_intel_shutdown_status(nfit_mem);
1825 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1826 struct nfit_mem *nfit_mem, u32 device_handle)
1828 struct acpi_device *adev, *adev_dimm;
1829 struct device *dev = acpi_desc->dev;
1830 unsigned long dsm_mask, label_mask;
1834 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1836 /* nfit test assumes 1:1 relationship between commands and dsms */
1837 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1838 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1840 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1841 sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
1842 be16_to_cpu(dcr->vendor_id),
1843 dcr->manufacturing_location,
1844 be16_to_cpu(dcr->manufacturing_date),
1845 be32_to_cpu(dcr->serial_number));
1847 sprintf(nfit_mem->id, "%04x-%08x",
1848 be16_to_cpu(dcr->vendor_id),
1849 be32_to_cpu(dcr->serial_number));
1851 adev = to_acpi_dev(acpi_desc);
1853 /* unit test case */
1854 populate_shutdown_status(nfit_mem);
1858 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1859 nfit_mem->adev = adev_dimm;
1861 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1863 return force_enable_dimms ? 0 : -ENODEV;
1866 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1867 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1868 dev_err(dev, "%s: notification registration failed\n",
1869 dev_name(&adev_dimm->dev));
1873 * Record nfit_mem for the notification path to track back to
1874 * the nfit sysfs attributes for this dimm device object.
1876 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1879 * There are 4 "legacy" NVDIMM command sets
1880 * (NVDIMM_FAMILY_{INTEL,MSFT,HPE1,HPE2}) that were created before
1881 * an EFI working group was established to constrain this
1882 * proliferation. The nfit driver probes for the supported command
1883 * set by GUID. Note, if you're a platform developer looking to add
1884 * a new command set to this probe, consider using an existing set,
1885 * or otherwise seek approval to publish the command set at
1886 * http://www.uefi.org/RFIC_LIST.
1888 * Note, that checking for function0 (bit0) tells us if any commands
1889 * are reachable through this GUID.
1891 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
1892 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1893 if (family < 0 || i == default_dsm_family)
1896 /* limit the supported commands to those that are publicly documented */
1897 nfit_mem->family = family;
1898 if (override_dsm_mask && !disable_vendor_specific)
1899 dsm_mask = override_dsm_mask;
1900 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1901 dsm_mask = NVDIMM_INTEL_CMDMASK;
1902 if (disable_vendor_specific)
1903 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1904 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1905 dsm_mask = 0x1c3c76;
1906 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1908 if (disable_vendor_specific)
1909 dsm_mask &= ~(1 << 8);
1910 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1911 dsm_mask = 0xffffffff;
1912 } else if (nfit_mem->family == NVDIMM_FAMILY_HYPERV) {
1915 dev_dbg(dev, "unknown dimm command family\n");
1916 nfit_mem->family = -1;
1917 /* DSMs are optional, continue loading the driver... */
1922 * Function 0 is the command interrogation function, don't
1923 * export it to potential userspace use, and enable it to be
1924 * used as an error value in acpi_nfit_ctl().
1928 guid = to_nfit_uuid(nfit_mem->family);
1929 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1930 if (acpi_check_dsm(adev_dimm->handle, guid,
1931 nfit_dsm_revid(nfit_mem->family, i),
1933 set_bit(i, &nfit_mem->dsm_mask);
1936 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1937 * due to their better semantics handling locked capacity.
1939 label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
1940 | 1 << ND_CMD_SET_CONFIG_DATA;
1941 if (family == NVDIMM_FAMILY_INTEL
1942 && (dsm_mask & label_mask) == label_mask)
1943 /* skip _LS{I,R,W} enabling */;
1945 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1946 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
1947 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
1948 set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1951 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
1952 && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
1953 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
1954 set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
1958 * Quirk read-only label configurations to preserve
1959 * access to label-less namespaces by default.
1961 if (!test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
1963 dev_dbg(dev, "%s: No _LSW, disable labels\n",
1964 dev_name(&adev_dimm->dev));
1965 clear_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1967 dev_dbg(dev, "%s: Force enable labels\n",
1968 dev_name(&adev_dimm->dev));
1971 populate_shutdown_status(nfit_mem);
1976 static void shutdown_dimm_notify(void *data)
1978 struct acpi_nfit_desc *acpi_desc = data;
1979 struct nfit_mem *nfit_mem;
1981 mutex_lock(&acpi_desc->init_mutex);
1983 * Clear out the nfit_mem->flags_attr and shut down dimm event
1986 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1987 struct acpi_device *adev_dimm = nfit_mem->adev;
1989 if (nfit_mem->flags_attr) {
1990 sysfs_put(nfit_mem->flags_attr);
1991 nfit_mem->flags_attr = NULL;
1994 acpi_remove_notify_handler(adev_dimm->handle,
1995 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1996 dev_set_drvdata(&adev_dimm->dev, NULL);
1999 mutex_unlock(&acpi_desc->init_mutex);
2002 static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
2005 case NVDIMM_FAMILY_INTEL:
2006 return intel_security_ops;
2012 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
2014 struct nfit_mem *nfit_mem;
2015 int dimm_count = 0, rc;
2016 struct nvdimm *nvdimm;
2018 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2019 struct acpi_nfit_flush_address *flush;
2020 unsigned long flags = 0, cmd_mask;
2021 struct nfit_memdev *nfit_memdev;
2025 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2026 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
2032 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
2033 set_bit(NDD_ALIASING, &flags);
2035 /* collate flags across all memdevs for this dimm */
2036 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2037 struct acpi_nfit_memory_map *dimm_memdev;
2039 dimm_memdev = __to_nfit_memdev(nfit_mem);
2040 if (dimm_memdev->device_handle
2041 != nfit_memdev->memdev->device_handle)
2043 dimm_memdev->flags |= nfit_memdev->memdev->flags;
2046 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
2047 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
2048 set_bit(NDD_UNARMED, &flags);
2050 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
2055 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
2056 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
2057 * userspace interface.
2059 cmd_mask = 1UL << ND_CMD_CALL;
2060 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
2062 * These commands have a 1:1 correspondence
2063 * between DSM payload and libnvdimm ioctl
2066 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
2069 /* Quirk to ignore LOCAL for labels on HYPERV DIMMs */
2070 if (nfit_mem->family == NVDIMM_FAMILY_HYPERV)
2071 set_bit(NDD_NOBLK, &flags);
2073 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
2074 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
2075 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
2077 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
2078 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
2080 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
2082 nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
2083 acpi_nfit_dimm_attribute_groups,
2084 flags, cmd_mask, flush ? flush->hint_count : 0,
2085 nfit_mem->flush_wpq, &nfit_mem->id[0],
2086 acpi_nfit_get_security_ops(nfit_mem->family));
2090 nfit_mem->nvdimm = nvdimm;
2093 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
2096 dev_err(acpi_desc->dev, "Error found in NVDIMM %s flags:%s%s%s%s%s\n",
2097 nvdimm_name(nvdimm),
2098 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
2099 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
2100 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
2101 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
2102 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
2106 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
2111 * Now that dimms are successfully registered, and async registration
2112 * is flushed, attempt to enable event notification.
2114 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2115 struct kernfs_node *nfit_kernfs;
2117 nvdimm = nfit_mem->nvdimm;
2121 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
2123 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
2125 sysfs_put(nfit_kernfs);
2126 if (!nfit_mem->flags_attr)
2127 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
2128 nvdimm_name(nvdimm));
2131 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
2136 * These constants are private because there are no kernel consumers of
2139 enum nfit_aux_cmds {
2140 NFIT_CMD_TRANSLATE_SPA = 5,
2141 NFIT_CMD_ARS_INJECT_SET = 7,
2142 NFIT_CMD_ARS_INJECT_CLEAR = 8,
2143 NFIT_CMD_ARS_INJECT_GET = 9,
2146 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
2148 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2149 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
2150 struct acpi_device *adev;
2151 unsigned long dsm_mask;
2154 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
2155 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
2156 adev = to_acpi_dev(acpi_desc);
2160 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
2161 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2162 set_bit(i, &nd_desc->cmd_mask);
2163 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
2166 (1 << ND_CMD_ARS_CAP) |
2167 (1 << ND_CMD_ARS_START) |
2168 (1 << ND_CMD_ARS_STATUS) |
2169 (1 << ND_CMD_CLEAR_ERROR) |
2170 (1 << NFIT_CMD_TRANSLATE_SPA) |
2171 (1 << NFIT_CMD_ARS_INJECT_SET) |
2172 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
2173 (1 << NFIT_CMD_ARS_INJECT_GET);
2174 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
2175 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2176 set_bit(i, &nd_desc->bus_dsm_mask);
2179 static ssize_t range_index_show(struct device *dev,
2180 struct device_attribute *attr, char *buf)
2182 struct nd_region *nd_region = to_nd_region(dev);
2183 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
2185 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
2187 static DEVICE_ATTR_RO(range_index);
2189 static struct attribute *acpi_nfit_region_attributes[] = {
2190 &dev_attr_range_index.attr,
2194 static const struct attribute_group acpi_nfit_region_attribute_group = {
2196 .attrs = acpi_nfit_region_attributes,
2199 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
2200 &nd_region_attribute_group,
2201 &nd_mapping_attribute_group,
2202 &nd_device_attribute_group,
2203 &nd_numa_attribute_group,
2204 &acpi_nfit_region_attribute_group,
2208 /* enough info to uniquely specify an interleave set */
2209 struct nfit_set_info {
2210 struct nfit_set_info_map {
2217 struct nfit_set_info2 {
2218 struct nfit_set_info_map2 {
2222 u16 manufacturing_date;
2223 u8 manufacturing_location;
2228 static size_t sizeof_nfit_set_info(int num_mappings)
2230 return sizeof(struct nfit_set_info)
2231 + num_mappings * sizeof(struct nfit_set_info_map);
2234 static size_t sizeof_nfit_set_info2(int num_mappings)
2236 return sizeof(struct nfit_set_info2)
2237 + num_mappings * sizeof(struct nfit_set_info_map2);
2240 static int cmp_map_compat(const void *m0, const void *m1)
2242 const struct nfit_set_info_map *map0 = m0;
2243 const struct nfit_set_info_map *map1 = m1;
2245 return memcmp(&map0->region_offset, &map1->region_offset,
2249 static int cmp_map(const void *m0, const void *m1)
2251 const struct nfit_set_info_map *map0 = m0;
2252 const struct nfit_set_info_map *map1 = m1;
2254 if (map0->region_offset < map1->region_offset)
2256 else if (map0->region_offset > map1->region_offset)
2261 static int cmp_map2(const void *m0, const void *m1)
2263 const struct nfit_set_info_map2 *map0 = m0;
2264 const struct nfit_set_info_map2 *map1 = m1;
2266 if (map0->region_offset < map1->region_offset)
2268 else if (map0->region_offset > map1->region_offset)
2273 /* Retrieve the nth entry referencing this spa */
2274 static struct acpi_nfit_memory_map *memdev_from_spa(
2275 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2277 struct nfit_memdev *nfit_memdev;
2279 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2280 if (nfit_memdev->memdev->range_index == range_index)
2282 return nfit_memdev->memdev;
2286 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2287 struct nd_region_desc *ndr_desc,
2288 struct acpi_nfit_system_address *spa)
2290 struct device *dev = acpi_desc->dev;
2291 struct nd_interleave_set *nd_set;
2292 u16 nr = ndr_desc->num_mappings;
2293 struct nfit_set_info2 *info2;
2294 struct nfit_set_info *info;
2297 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2300 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2302 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2306 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2310 for (i = 0; i < nr; i++) {
2311 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
2312 struct nfit_set_info_map *map = &info->mapping[i];
2313 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2314 struct nvdimm *nvdimm = mapping->nvdimm;
2315 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2316 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2317 spa->range_index, i);
2318 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2320 if (!memdev || !nfit_mem->dcr) {
2321 dev_err(dev, "%s: failed to find DCR\n", __func__);
2325 map->region_offset = memdev->region_offset;
2326 map->serial_number = dcr->serial_number;
2328 map2->region_offset = memdev->region_offset;
2329 map2->serial_number = dcr->serial_number;
2330 map2->vendor_id = dcr->vendor_id;
2331 map2->manufacturing_date = dcr->manufacturing_date;
2332 map2->manufacturing_location = dcr->manufacturing_location;
2335 /* v1.1 namespaces */
2336 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2338 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2340 /* v1.2 namespaces */
2341 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2343 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2345 /* support v1.1 namespaces created with the wrong sort order */
2346 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2347 cmp_map_compat, NULL);
2348 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2350 /* record the result of the sort for the mapping position */
2351 for (i = 0; i < nr; i++) {
2352 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2355 for (j = 0; j < nr; j++) {
2356 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2357 struct nvdimm *nvdimm = mapping->nvdimm;
2358 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2359 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2361 if (map2->serial_number == dcr->serial_number &&
2362 map2->vendor_id == dcr->vendor_id &&
2363 map2->manufacturing_date == dcr->manufacturing_date &&
2364 map2->manufacturing_location
2365 == dcr->manufacturing_location) {
2366 mapping->position = i;
2372 ndr_desc->nd_set = nd_set;
2373 devm_kfree(dev, info);
2374 devm_kfree(dev, info2);
2379 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2381 struct acpi_nfit_interleave *idt = mmio->idt;
2382 u32 sub_line_offset, line_index, line_offset;
2383 u64 line_no, table_skip_count, table_offset;
2385 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2386 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2387 line_offset = idt->line_offset[line_index]
2389 table_offset = table_skip_count * mmio->table_size;
2391 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2394 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
2396 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2397 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
2398 const u32 STATUS_MASK = 0x80000037;
2400 if (mmio->num_lines)
2401 offset = to_interleave_offset(offset, mmio);
2403 return readl(mmio->addr.base + offset) & STATUS_MASK;
2406 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2407 resource_size_t dpa, unsigned int len, unsigned int write)
2410 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2413 BCW_OFFSET_MASK = (1ULL << 48)-1,
2415 BCW_LEN_MASK = (1ULL << 8) - 1,
2419 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2420 len = len >> L1_CACHE_SHIFT;
2421 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2422 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2424 offset = nfit_blk->cmd_offset + mmio->size * bw;
2425 if (mmio->num_lines)
2426 offset = to_interleave_offset(offset, mmio);
2428 writeq(cmd, mmio->addr.base + offset);
2429 nvdimm_flush(nfit_blk->nd_region, NULL);
2431 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
2432 readq(mmio->addr.base + offset);
2435 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2436 resource_size_t dpa, void *iobuf, size_t len, int rw,
2439 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2440 unsigned int copied = 0;
2444 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2445 + lane * mmio->size;
2446 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2451 if (mmio->num_lines) {
2454 offset = to_interleave_offset(base_offset + copied,
2456 div_u64_rem(offset, mmio->line_size, &line_offset);
2457 c = min_t(size_t, len, mmio->line_size - line_offset);
2459 offset = base_offset + nfit_blk->bdw_offset;
2464 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2466 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2467 arch_invalidate_pmem((void __force *)
2468 mmio->addr.aperture + offset, c);
2470 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2478 nvdimm_flush(nfit_blk->nd_region, NULL);
2480 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2484 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2485 resource_size_t dpa, void *iobuf, u64 len, int rw)
2487 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2488 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2489 struct nd_region *nd_region = nfit_blk->nd_region;
2490 unsigned int lane, copied = 0;
2493 lane = nd_region_acquire_lane(nd_region);
2495 u64 c = min(len, mmio->size);
2497 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2498 iobuf + copied, c, rw, lane);
2505 nd_region_release_lane(nd_region, lane);
2510 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2511 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2514 mmio->num_lines = idt->line_count;
2515 mmio->line_size = idt->line_size;
2516 if (interleave_ways == 0)
2518 mmio->table_size = mmio->num_lines * interleave_ways
2525 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2526 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2528 struct nd_cmd_dimm_flags flags;
2531 memset(&flags, 0, sizeof(flags));
2532 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2533 sizeof(flags), NULL);
2535 if (rc >= 0 && flags.status == 0)
2536 nfit_blk->dimm_flags = flags.flags;
2537 else if (rc == -ENOTTY) {
2538 /* fall back to a conservative default */
2539 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2547 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2550 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2551 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2552 struct nfit_blk_mmio *mmio;
2553 struct nfit_blk *nfit_blk;
2554 struct nfit_mem *nfit_mem;
2555 struct nvdimm *nvdimm;
2558 nvdimm = nd_blk_region_to_dimm(ndbr);
2559 nfit_mem = nvdimm_provider_data(nvdimm);
2560 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2561 dev_dbg(dev, "missing%s%s%s\n",
2562 nfit_mem ? "" : " nfit_mem",
2563 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2564 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2568 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2571 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2572 nfit_blk->nd_region = to_nd_region(dev);
2574 /* map block aperture memory */
2575 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2576 mmio = &nfit_blk->mmio[BDW];
2577 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2578 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2579 if (!mmio->addr.base) {
2580 dev_dbg(dev, "%s failed to map bdw\n",
2581 nvdimm_name(nvdimm));
2584 mmio->size = nfit_mem->bdw->size;
2585 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2586 mmio->idt = nfit_mem->idt_bdw;
2587 mmio->spa = nfit_mem->spa_bdw;
2588 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2589 nfit_mem->memdev_bdw->interleave_ways);
2591 dev_dbg(dev, "%s failed to init bdw interleave\n",
2592 nvdimm_name(nvdimm));
2596 /* map block control memory */
2597 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2598 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2599 mmio = &nfit_blk->mmio[DCR];
2600 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2601 nfit_mem->spa_dcr->length);
2602 if (!mmio->addr.base) {
2603 dev_dbg(dev, "%s failed to map dcr\n",
2604 nvdimm_name(nvdimm));
2607 mmio->size = nfit_mem->dcr->window_size;
2608 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2609 mmio->idt = nfit_mem->idt_dcr;
2610 mmio->spa = nfit_mem->spa_dcr;
2611 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2612 nfit_mem->memdev_dcr->interleave_ways);
2614 dev_dbg(dev, "%s failed to init dcr interleave\n",
2615 nvdimm_name(nvdimm));
2619 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2621 dev_dbg(dev, "%s failed get DIMM flags\n",
2622 nvdimm_name(nvdimm));
2626 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2627 dev_warn(dev, "unable to guarantee persistence of writes\n");
2629 if (mmio->line_size == 0)
2632 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2633 + 8 > mmio->line_size) {
2634 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2636 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2637 + 8 > mmio->line_size) {
2638 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2645 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2646 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2648 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2649 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2652 cmd->address = spa->address;
2653 cmd->length = spa->length;
2654 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2655 sizeof(*cmd), &cmd_rc);
2661 static int ars_start(struct acpi_nfit_desc *acpi_desc,
2662 struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
2666 struct nd_cmd_ars_start ars_start;
2667 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2668 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2670 memset(&ars_start, 0, sizeof(ars_start));
2671 ars_start.address = spa->address;
2672 ars_start.length = spa->length;
2673 if (req_type == ARS_REQ_SHORT)
2674 ars_start.flags = ND_ARS_RETURN_PREV_DATA;
2675 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2676 ars_start.type = ND_ARS_PERSISTENT;
2677 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2678 ars_start.type = ND_ARS_VOLATILE;
2682 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2683 sizeof(ars_start), &cmd_rc);
2689 set_bit(ARS_VALID, &acpi_desc->scrub_flags);
2693 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2696 struct nd_cmd_ars_start ars_start;
2697 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2698 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2700 ars_start = (struct nd_cmd_ars_start) {
2701 .address = ars_status->restart_address,
2702 .length = ars_status->restart_length,
2703 .type = ars_status->type,
2705 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2706 sizeof(ars_start), &cmd_rc);
2712 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2714 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2715 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2718 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2719 acpi_desc->max_ars, &cmd_rc);
2725 static void ars_complete(struct acpi_nfit_desc *acpi_desc,
2726 struct nfit_spa *nfit_spa)
2728 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2729 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2730 struct nd_region *nd_region = nfit_spa->nd_region;
2733 lockdep_assert_held(&acpi_desc->init_mutex);
2735 * Only advance the ARS state for ARS runs initiated by the
2736 * kernel, ignore ARS results from BIOS initiated runs for scrub
2737 * completion tracking.
2739 if (acpi_desc->scrub_spa != nfit_spa)
2742 if ((ars_status->address >= spa->address && ars_status->address
2743 < spa->address + spa->length)
2744 || (ars_status->address < spa->address)) {
2746 * Assume that if a scrub starts at an offset from the
2747 * start of nfit_spa that we are in the continuation
2750 * Otherwise, if the scrub covers the spa range, mark
2751 * any pending request complete.
2753 if (ars_status->address + ars_status->length
2754 >= spa->address + spa->length)
2761 acpi_desc->scrub_spa = NULL;
2763 dev = nd_region_dev(nd_region);
2764 nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
2766 dev = acpi_desc->dev;
2767 dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
2770 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
2772 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2773 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2778 * First record starts at 44 byte offset from the start of the
2781 if (ars_status->out_length < 44)
2785 * Ignore potentially stale results that are only refreshed
2786 * after a start-ARS event.
2788 if (!test_and_clear_bit(ARS_VALID, &acpi_desc->scrub_flags)) {
2789 dev_dbg(acpi_desc->dev, "skip %d stale records\n",
2790 ars_status->num_records);
2794 for (i = 0; i < ars_status->num_records; i++) {
2795 /* only process full records */
2796 if (ars_status->out_length
2797 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2799 rc = nvdimm_bus_add_badrange(nvdimm_bus,
2800 ars_status->records[i].err_address,
2801 ars_status->records[i].length);
2805 if (i < ars_status->num_records)
2806 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2811 static void acpi_nfit_remove_resource(void *data)
2813 struct resource *res = data;
2815 remove_resource(res);
2818 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2819 struct nd_region_desc *ndr_desc)
2821 struct resource *res, *nd_res = ndr_desc->res;
2824 /* No operation if the region is already registered as PMEM */
2825 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2826 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2827 if (is_pmem == REGION_INTERSECTS)
2830 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2834 res->name = "Persistent Memory";
2835 res->start = nd_res->start;
2836 res->end = nd_res->end;
2837 res->flags = IORESOURCE_MEM;
2838 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2840 ret = insert_resource(&iomem_resource, res);
2844 ret = devm_add_action_or_reset(acpi_desc->dev,
2845 acpi_nfit_remove_resource,
2853 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2854 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2855 struct acpi_nfit_memory_map *memdev,
2856 struct nfit_spa *nfit_spa)
2858 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2859 memdev->device_handle);
2860 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2861 struct nd_blk_region_desc *ndbr_desc;
2862 struct nfit_mem *nfit_mem;
2866 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2867 spa->range_index, memdev->device_handle);
2871 mapping->nvdimm = nvdimm;
2872 switch (nfit_spa_type(spa)) {
2874 case NFIT_SPA_VOLATILE:
2875 mapping->start = memdev->address;
2876 mapping->size = memdev->region_size;
2879 nfit_mem = nvdimm_provider_data(nvdimm);
2880 if (!nfit_mem || !nfit_mem->bdw) {
2881 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2882 spa->range_index, nvdimm_name(nvdimm));
2886 mapping->size = nfit_mem->bdw->capacity;
2887 mapping->start = nfit_mem->bdw->start_address;
2888 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2889 ndr_desc->mapping = mapping;
2890 ndr_desc->num_mappings = 1;
2891 ndbr_desc = to_blk_region_desc(ndr_desc);
2892 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2893 ndbr_desc->do_io = acpi_desc->blk_do_io;
2894 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2897 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2899 if (!nfit_spa->nd_region)
2907 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2909 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2910 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2911 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2912 nfit_spa_type(spa) == NFIT_SPA_PCD);
2915 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2917 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2918 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2919 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2922 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2923 struct nfit_spa *nfit_spa)
2925 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2926 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2927 struct nd_blk_region_desc ndbr_desc;
2928 struct nd_region_desc *ndr_desc;
2929 struct nfit_memdev *nfit_memdev;
2930 struct nvdimm_bus *nvdimm_bus;
2931 struct resource res;
2934 if (nfit_spa->nd_region)
2937 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2938 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
2942 memset(&res, 0, sizeof(res));
2943 memset(&mappings, 0, sizeof(mappings));
2944 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2945 res.start = spa->address;
2946 res.end = res.start + spa->length - 1;
2947 ndr_desc = &ndbr_desc.ndr_desc;
2948 ndr_desc->res = &res;
2949 ndr_desc->provider_data = nfit_spa;
2950 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2951 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID) {
2952 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2953 spa->proximity_domain);
2954 ndr_desc->target_node = acpi_map_pxm_to_node(
2955 spa->proximity_domain);
2957 ndr_desc->numa_node = NUMA_NO_NODE;
2958 ndr_desc->target_node = NUMA_NO_NODE;
2962 * Persistence domain bits are hierarchical, if
2963 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
2964 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
2966 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2967 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2968 else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2969 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2971 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2972 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2973 struct nd_mapping_desc *mapping;
2975 if (memdev->range_index != spa->range_index)
2977 if (count >= ND_MAX_MAPPINGS) {
2978 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2979 spa->range_index, ND_MAX_MAPPINGS);
2982 mapping = &mappings[count++];
2983 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2989 ndr_desc->mapping = mappings;
2990 ndr_desc->num_mappings = count;
2991 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2995 nvdimm_bus = acpi_desc->nvdimm_bus;
2996 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2997 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2999 dev_warn(acpi_desc->dev,
3000 "failed to insert pmem resource to iomem: %d\n",
3005 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
3007 if (!nfit_spa->nd_region)
3009 } else if (nfit_spa_is_volatile(spa)) {
3010 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
3012 if (!nfit_spa->nd_region)
3014 } else if (nfit_spa_is_virtual(spa)) {
3015 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
3017 if (!nfit_spa->nd_region)
3023 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
3024 nfit_spa->spa->range_index);
3028 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
3030 struct device *dev = acpi_desc->dev;
3031 struct nd_cmd_ars_status *ars_status;
3033 if (acpi_desc->ars_status) {
3034 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3038 ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
3041 acpi_desc->ars_status = ars_status;
3045 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
3049 if (ars_status_alloc(acpi_desc))
3052 rc = ars_get_status(acpi_desc);
3054 if (rc < 0 && rc != -ENOSPC)
3057 if (ars_status_process_records(acpi_desc))
3058 dev_err(acpi_desc->dev, "Failed to process ARS records\n");
3063 static int ars_register(struct acpi_nfit_desc *acpi_desc,
3064 struct nfit_spa *nfit_spa)
3068 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3069 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3071 set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3073 set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
3075 switch (acpi_nfit_query_poison(acpi_desc)) {
3079 rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
3080 /* shouldn't happen, try again later */
3084 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3087 clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3088 rc = acpi_nfit_query_poison(acpi_desc);
3091 acpi_desc->scrub_spa = nfit_spa;
3092 ars_complete(acpi_desc, nfit_spa);
3094 * If ars_complete() says we didn't complete the
3095 * short scrub, we'll try again with a long
3098 acpi_desc->scrub_spa = NULL;
3103 * BIOS was using ARS, wait for it to complete (or
3104 * resources to become available) and then perform our
3109 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3113 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3116 static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
3118 struct nfit_spa *nfit_spa;
3120 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3121 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3123 ars_complete(acpi_desc, nfit_spa);
3127 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
3130 unsigned int tmo = acpi_desc->scrub_tmo;
3131 struct device *dev = acpi_desc->dev;
3132 struct nfit_spa *nfit_spa;
3134 lockdep_assert_held(&acpi_desc->init_mutex);
3136 if (test_bit(ARS_CANCEL, &acpi_desc->scrub_flags))
3139 if (query_rc == -EBUSY) {
3140 dev_dbg(dev, "ARS: ARS busy\n");
3141 return min(30U * 60U, tmo * 2);
3143 if (query_rc == -ENOSPC) {
3144 dev_dbg(dev, "ARS: ARS continue\n");
3145 ars_continue(acpi_desc);
3148 if (query_rc && query_rc != -EAGAIN) {
3149 unsigned long long addr, end;
3151 addr = acpi_desc->ars_status->address;
3152 end = addr + acpi_desc->ars_status->length;
3153 dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
3157 ars_complete_all(acpi_desc);
3158 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3159 enum nfit_ars_state req_type;
3162 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3165 /* prefer short ARS requests first */
3166 if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
3167 req_type = ARS_REQ_SHORT;
3168 else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
3169 req_type = ARS_REQ_LONG;
3172 rc = ars_start(acpi_desc, nfit_spa, req_type);
3174 dev = nd_region_dev(nfit_spa->nd_region);
3175 dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
3176 nfit_spa->spa->range_index,
3177 req_type == ARS_REQ_SHORT ? "short" : "long",
3180 * Hmm, we raced someone else starting ARS? Try again in
3186 dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
3187 "scrub start while range %d active\n",
3188 acpi_desc->scrub_spa->spa->range_index);
3189 clear_bit(req_type, &nfit_spa->ars_state);
3190 acpi_desc->scrub_spa = nfit_spa;
3192 * Consider this spa last for future scrub
3195 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
3199 dev_err(dev, "ARS: range %d ARS failed (%d)\n",
3200 nfit_spa->spa->range_index, rc);
3201 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3206 static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
3208 lockdep_assert_held(&acpi_desc->init_mutex);
3210 set_bit(ARS_BUSY, &acpi_desc->scrub_flags);
3211 /* note this should only be set from within the workqueue */
3213 acpi_desc->scrub_tmo = tmo;
3214 queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
3217 static void sched_ars(struct acpi_nfit_desc *acpi_desc)
3219 __sched_ars(acpi_desc, 0);
3222 static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
3224 lockdep_assert_held(&acpi_desc->init_mutex);
3226 clear_bit(ARS_BUSY, &acpi_desc->scrub_flags);
3227 acpi_desc->scrub_count++;
3228 if (acpi_desc->scrub_count_state)
3229 sysfs_notify_dirent(acpi_desc->scrub_count_state);
3232 static void acpi_nfit_scrub(struct work_struct *work)
3234 struct acpi_nfit_desc *acpi_desc;
3238 acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
3239 mutex_lock(&acpi_desc->init_mutex);
3240 query_rc = acpi_nfit_query_poison(acpi_desc);
3241 tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
3243 __sched_ars(acpi_desc, tmo);
3245 notify_ars_done(acpi_desc);
3246 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3247 clear_bit(ARS_POLL, &acpi_desc->scrub_flags);
3248 mutex_unlock(&acpi_desc->init_mutex);
3251 static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
3252 struct nfit_spa *nfit_spa)
3254 int type = nfit_spa_type(nfit_spa->spa);
3255 struct nd_cmd_ars_cap ars_cap;
3258 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3259 memset(&ars_cap, 0, sizeof(ars_cap));
3260 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
3263 /* check that the supported scrub types match the spa type */
3264 if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
3265 & ND_ARS_VOLATILE) == 0)
3267 if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
3268 & ND_ARS_PERSISTENT) == 0)
3271 nfit_spa->max_ars = ars_cap.max_ars_out;
3272 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
3273 acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
3274 clear_bit(ARS_FAILED, &nfit_spa->ars_state);
3277 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3279 struct nfit_spa *nfit_spa;
3282 set_bit(ARS_VALID, &acpi_desc->scrub_flags);
3283 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3284 switch (nfit_spa_type(nfit_spa->spa)) {
3285 case NFIT_SPA_VOLATILE:
3287 acpi_nfit_init_ars(acpi_desc, nfit_spa);
3292 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3293 switch (nfit_spa_type(nfit_spa->spa)) {
3294 case NFIT_SPA_VOLATILE:
3296 /* register regions and kick off initial ARS run */
3297 rc = ars_register(acpi_desc, nfit_spa);
3302 /* nothing to register */
3305 case NFIT_SPA_VDISK:
3307 case NFIT_SPA_PDISK:
3309 /* register known regions that don't support ARS */
3310 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3315 /* don't register unknown regions */
3319 sched_ars(acpi_desc);
3323 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3324 struct nfit_table_prev *prev)
3326 struct device *dev = acpi_desc->dev;
3328 if (!list_empty(&prev->spas) ||
3329 !list_empty(&prev->memdevs) ||
3330 !list_empty(&prev->dcrs) ||
3331 !list_empty(&prev->bdws) ||
3332 !list_empty(&prev->idts) ||
3333 !list_empty(&prev->flushes)) {
3334 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3340 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3342 struct device *dev = acpi_desc->dev;
3343 struct kernfs_node *nfit;
3344 struct device *bus_dev;
3346 if (!ars_supported(acpi_desc->nvdimm_bus))
3349 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3350 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3352 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3355 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3357 if (!acpi_desc->scrub_count_state) {
3358 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3365 static void acpi_nfit_unregister(void *data)
3367 struct acpi_nfit_desc *acpi_desc = data;
3369 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
3372 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
3374 struct device *dev = acpi_desc->dev;
3375 struct nfit_table_prev prev;
3379 if (!acpi_desc->nvdimm_bus) {
3380 acpi_nfit_init_dsms(acpi_desc);
3382 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3383 &acpi_desc->nd_desc);
3384 if (!acpi_desc->nvdimm_bus)
3387 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
3392 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3396 /* register this acpi_desc for mce notifications */
3397 mutex_lock(&acpi_desc_lock);
3398 list_add_tail(&acpi_desc->list, &acpi_descs);
3399 mutex_unlock(&acpi_desc_lock);
3402 mutex_lock(&acpi_desc->init_mutex);
3404 INIT_LIST_HEAD(&prev.spas);
3405 INIT_LIST_HEAD(&prev.memdevs);
3406 INIT_LIST_HEAD(&prev.dcrs);
3407 INIT_LIST_HEAD(&prev.bdws);
3408 INIT_LIST_HEAD(&prev.idts);
3409 INIT_LIST_HEAD(&prev.flushes);
3411 list_cut_position(&prev.spas, &acpi_desc->spas,
3412 acpi_desc->spas.prev);
3413 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3414 acpi_desc->memdevs.prev);
3415 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3416 acpi_desc->dcrs.prev);
3417 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3418 acpi_desc->bdws.prev);
3419 list_cut_position(&prev.idts, &acpi_desc->idts,
3420 acpi_desc->idts.prev);
3421 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3422 acpi_desc->flushes.prev);
3425 while (!IS_ERR_OR_NULL(data))
3426 data = add_table(acpi_desc, &prev, data, end);
3429 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
3434 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3438 rc = nfit_mem_init(acpi_desc);
3442 rc = acpi_nfit_register_dimms(acpi_desc);
3446 rc = acpi_nfit_register_regions(acpi_desc);
3449 mutex_unlock(&acpi_desc->init_mutex);
3452 EXPORT_SYMBOL_GPL(acpi_nfit_init);
3454 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3456 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3457 struct device *dev = acpi_desc->dev;
3459 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3460 nfit_device_lock(dev);
3461 nfit_device_unlock(dev);
3463 /* Bounce the init_mutex to complete initial registration */
3464 mutex_lock(&acpi_desc->init_mutex);
3465 mutex_unlock(&acpi_desc->init_mutex);
3470 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3471 struct nvdimm *nvdimm, unsigned int cmd)
3473 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3477 if (cmd != ND_CMD_ARS_START)
3481 * The kernel and userspace may race to initiate a scrub, but
3482 * the scrub thread is prepared to lose that initial race. It
3483 * just needs guarantees that any ARS it initiates are not
3484 * interrupted by any intervening start requests from userspace.
3486 if (work_busy(&acpi_desc->dwork.work))
3492 /* prevent security commands from being issued via ioctl */
3493 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3494 struct nvdimm *nvdimm, unsigned int cmd, void *buf)
3496 struct nd_cmd_pkg *call_pkg = buf;
3499 if (nvdimm && cmd == ND_CMD_CALL &&
3500 call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
3501 func = call_pkg->nd_command;
3502 if ((1 << func) & NVDIMM_INTEL_SECURITY_CMDMASK)
3506 return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
3509 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
3510 enum nfit_ars_state req_type)
3512 struct device *dev = acpi_desc->dev;
3513 int scheduled = 0, busy = 0;
3514 struct nfit_spa *nfit_spa;
3516 mutex_lock(&acpi_desc->init_mutex);
3517 if (test_bit(ARS_CANCEL, &acpi_desc->scrub_flags)) {
3518 mutex_unlock(&acpi_desc->init_mutex);
3522 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3523 int type = nfit_spa_type(nfit_spa->spa);
3525 if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
3527 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3530 if (test_and_set_bit(req_type, &nfit_spa->ars_state))
3536 sched_ars(acpi_desc);
3537 dev_dbg(dev, "ars_scan triggered\n");
3539 mutex_unlock(&acpi_desc->init_mutex);
3548 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3550 struct nvdimm_bus_descriptor *nd_desc;
3552 dev_set_drvdata(dev, acpi_desc);
3553 acpi_desc->dev = dev;
3554 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3555 nd_desc = &acpi_desc->nd_desc;
3556 nd_desc->provider_name = "ACPI.NFIT";
3557 nd_desc->module = THIS_MODULE;
3558 nd_desc->ndctl = acpi_nfit_ctl;
3559 nd_desc->flush_probe = acpi_nfit_flush_probe;
3560 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3561 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3563 INIT_LIST_HEAD(&acpi_desc->spas);
3564 INIT_LIST_HEAD(&acpi_desc->dcrs);
3565 INIT_LIST_HEAD(&acpi_desc->bdws);
3566 INIT_LIST_HEAD(&acpi_desc->idts);
3567 INIT_LIST_HEAD(&acpi_desc->flushes);
3568 INIT_LIST_HEAD(&acpi_desc->memdevs);
3569 INIT_LIST_HEAD(&acpi_desc->dimms);
3570 INIT_LIST_HEAD(&acpi_desc->list);
3571 mutex_init(&acpi_desc->init_mutex);
3572 acpi_desc->scrub_tmo = 1;
3573 INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
3575 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3577 static void acpi_nfit_put_table(void *table)
3579 acpi_put_table(table);
3582 void acpi_nfit_shutdown(void *data)
3584 struct acpi_nfit_desc *acpi_desc = data;
3585 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3588 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3591 mutex_lock(&acpi_desc_lock);
3592 list_del(&acpi_desc->list);
3593 mutex_unlock(&acpi_desc_lock);
3595 mutex_lock(&acpi_desc->init_mutex);
3596 set_bit(ARS_CANCEL, &acpi_desc->scrub_flags);
3597 cancel_delayed_work_sync(&acpi_desc->dwork);
3598 mutex_unlock(&acpi_desc->init_mutex);
3601 * Bounce the nvdimm bus lock to make sure any in-flight
3602 * acpi_nfit_ars_rescan() submissions have had a chance to
3603 * either submit or see ->cancel set.
3605 nfit_device_lock(bus_dev);
3606 nfit_device_unlock(bus_dev);
3608 flush_workqueue(nfit_wq);
3610 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3612 static int acpi_nfit_add(struct acpi_device *adev)
3614 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3615 struct acpi_nfit_desc *acpi_desc;
3616 struct device *dev = &adev->dev;
3617 struct acpi_table_header *tbl;
3618 acpi_status status = AE_OK;
3622 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3623 if (ACPI_FAILURE(status)) {
3624 /* The NVDIMM root device allows OS to trigger enumeration of
3625 * NVDIMMs through NFIT at boot time and re-enumeration at
3626 * root level via the _FIT method during runtime.
3627 * This is ok to return 0 here, we could have an nvdimm
3628 * hotplugged later and evaluate _FIT method which returns
3629 * data in the format of a series of NFIT Structures.
3631 dev_dbg(dev, "failed to find NFIT at startup\n");
3635 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3640 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3643 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3645 /* Save the acpi header for exporting the revision via sysfs */
3646 acpi_desc->acpi_header = *tbl;
3648 /* Evaluate _FIT and override with that if present */
3649 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3650 if (ACPI_SUCCESS(status) && buf.length > 0) {
3651 union acpi_object *obj = buf.pointer;
3653 if (obj->type == ACPI_TYPE_BUFFER)
3654 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3655 obj->buffer.length);
3657 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3661 /* skip over the lead-in header table */
3662 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3663 + sizeof(struct acpi_table_nfit),
3664 sz - sizeof(struct acpi_table_nfit));
3668 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3671 static int acpi_nfit_remove(struct acpi_device *adev)
3673 /* see acpi_nfit_unregister */
3677 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3679 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3680 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3681 union acpi_object *obj;
3686 /* dev->driver may be null if we're being removed */
3687 dev_dbg(dev, "no driver found for dev\n");
3692 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3695 acpi_nfit_desc_init(acpi_desc, dev);
3698 * Finish previous registration before considering new
3701 flush_workqueue(nfit_wq);
3705 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3706 if (ACPI_FAILURE(status)) {
3707 dev_err(dev, "failed to evaluate _FIT\n");
3712 if (obj->type == ACPI_TYPE_BUFFER) {
3713 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3714 obj->buffer.length);
3716 dev_err(dev, "failed to merge updated NFIT\n");
3718 dev_err(dev, "Invalid _FIT\n");
3722 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3724 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3726 if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
3727 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
3729 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
3732 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3734 dev_dbg(dev, "event: 0x%x\n", event);
3737 case NFIT_NOTIFY_UPDATE:
3738 return acpi_nfit_update_notify(dev, handle);
3739 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3740 return acpi_nfit_uc_error_notify(dev, handle);
3745 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3747 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3749 nfit_device_lock(&adev->dev);
3750 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3751 nfit_device_unlock(&adev->dev);
3754 static const struct acpi_device_id acpi_nfit_ids[] = {
3758 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3760 static struct acpi_driver acpi_nfit_driver = {
3761 .name = KBUILD_MODNAME,
3762 .ids = acpi_nfit_ids,
3764 .add = acpi_nfit_add,
3765 .remove = acpi_nfit_remove,
3766 .notify = acpi_nfit_notify,
3770 static __init int nfit_init(void)
3774 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3775 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3776 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3777 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3778 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3779 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3780 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3781 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
3783 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3784 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3785 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3786 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3787 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3788 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3789 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3790 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3791 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3792 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3793 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3794 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3795 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3796 guid_parse(UUID_NFIT_DIMM_N_HYPERV, &nfit_uuid[NFIT_DEV_DIMM_N_HYPERV]);
3798 nfit_wq = create_singlethread_workqueue("nfit");
3802 nfit_mce_register();
3803 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3805 nfit_mce_unregister();
3806 destroy_workqueue(nfit_wq);
3813 static __exit void nfit_exit(void)
3815 nfit_mce_unregister();
3816 acpi_bus_unregister_driver(&acpi_nfit_driver);
3817 destroy_workqueue(nfit_wq);
3818 WARN_ON(!list_empty(&acpi_descs));
3821 module_init(nfit_init);
3822 module_exit(nfit_exit);
3823 MODULE_LICENSE("GPL v2");
3824 MODULE_AUTHOR("Intel Corporation");