2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/list_sort.h>
14 #include <linux/libnvdimm.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/ndctl.h>
18 #include <linux/sysfs.h>
19 #include <linux/delay.h>
20 #include <linux/list.h>
21 #include <linux/acpi.h>
22 #include <linux/sort.h>
25 #include <asm/cacheflush.h>
26 #include <acpi/nfit.h>
31 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
34 #include <linux/io-64-nonatomic-hi-lo.h>
36 static bool force_enable_dimms;
37 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
38 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
40 static bool disable_vendor_specific;
41 module_param(disable_vendor_specific, bool, S_IRUGO);
42 MODULE_PARM_DESC(disable_vendor_specific,
43 "Limit commands to the publicly specified set");
45 static unsigned long override_dsm_mask;
46 module_param(override_dsm_mask, ulong, S_IRUGO);
47 MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
49 static int default_dsm_family = -1;
50 module_param(default_dsm_family, int, S_IRUGO);
51 MODULE_PARM_DESC(default_dsm_family,
52 "Try this DSM type first when identifying NVDIMM family");
54 static bool no_init_ars;
55 module_param(no_init_ars, bool, 0644);
56 MODULE_PARM_DESC(no_init_ars, "Skip ARS run at nfit init time");
58 static bool force_labels;
59 module_param(force_labels, bool, 0444);
60 MODULE_PARM_DESC(force_labels, "Opt-in to labels despite missing methods");
62 LIST_HEAD(acpi_descs);
63 DEFINE_MUTEX(acpi_desc_lock);
65 static struct workqueue_struct *nfit_wq;
67 struct nfit_table_prev {
68 struct list_head spas;
69 struct list_head memdevs;
70 struct list_head dcrs;
71 struct list_head bdws;
72 struct list_head idts;
73 struct list_head flushes;
76 static guid_t nfit_uuid[NFIT_UUID_MAX];
78 const guid_t *to_nfit_uuid(enum nfit_uuids id)
80 return &nfit_uuid[id];
82 EXPORT_SYMBOL(to_nfit_uuid);
84 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
86 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
89 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
92 if (!nd_desc->provider_name
93 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
96 return to_acpi_device(acpi_desc->dev);
99 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
101 struct nd_cmd_clear_error *clear_err;
102 struct nd_cmd_ars_status *ars_status;
107 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
114 /* No supported scan types for this range */
115 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
116 if ((status >> 16 & flags) == 0)
119 case ND_CMD_ARS_START:
120 /* ARS is in progress */
121 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
128 case ND_CMD_ARS_STATUS:
133 /* Check extended status (Upper two bytes) */
134 if (status == NFIT_ARS_STATUS_DONE)
137 /* ARS is in progress */
138 if (status == NFIT_ARS_STATUS_BUSY)
141 /* No ARS performed for the current boot */
142 if (status == NFIT_ARS_STATUS_NONE)
146 * ARS interrupted, either we overflowed or some other
147 * agent wants the scan to stop. If we didn't overflow
148 * then just continue with the returned results.
150 if (status == NFIT_ARS_STATUS_INTR) {
151 if (ars_status->out_length >= 40 && (ars_status->flags
152 & NFIT_ARS_F_OVERFLOW))
161 case ND_CMD_CLEAR_ERROR:
165 if (!clear_err->cleared)
167 if (clear_err->length > clear_err->cleared)
168 return clear_err->cleared;
174 /* all other non-zero status results in an error */
180 #define ACPI_LABELS_LOCKED 3
182 static int xlat_nvdimm_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
185 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
188 case ND_CMD_GET_CONFIG_SIZE:
190 * In the _LSI, _LSR, _LSW case the locked status is
191 * communicated via the read/write commands
193 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
196 if (status >> 16 & ND_CONFIG_LOCKED)
199 case ND_CMD_GET_CONFIG_DATA:
200 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
201 && status == ACPI_LABELS_LOCKED)
204 case ND_CMD_SET_CONFIG_DATA:
205 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
206 && status == ACPI_LABELS_LOCKED)
213 /* all other non-zero status results in an error */
219 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
223 return xlat_bus_status(buf, cmd, status);
224 return xlat_nvdimm_status(nvdimm, buf, cmd, status);
227 /* convert _LS{I,R} packages to the buffer object acpi_nfit_ctl expects */
228 static union acpi_object *pkg_to_buf(union acpi_object *pkg)
233 union acpi_object *buf = NULL;
235 if (pkg->type != ACPI_TYPE_PACKAGE) {
236 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
241 for (i = 0; i < pkg->package.count; i++) {
242 union acpi_object *obj = &pkg->package.elements[i];
244 if (obj->type == ACPI_TYPE_INTEGER)
246 else if (obj->type == ACPI_TYPE_BUFFER)
247 size += obj->buffer.length;
249 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
255 buf = ACPI_ALLOCATE(sizeof(*buf) + size);
260 buf->type = ACPI_TYPE_BUFFER;
261 buf->buffer.length = size;
262 buf->buffer.pointer = dst;
263 for (i = 0; i < pkg->package.count; i++) {
264 union acpi_object *obj = &pkg->package.elements[i];
266 if (obj->type == ACPI_TYPE_INTEGER) {
267 memcpy(dst, &obj->integer.value, 4);
269 } else if (obj->type == ACPI_TYPE_BUFFER) {
270 memcpy(dst, obj->buffer.pointer, obj->buffer.length);
271 dst += obj->buffer.length;
279 static union acpi_object *int_to_buf(union acpi_object *integer)
281 union acpi_object *buf = ACPI_ALLOCATE(sizeof(*buf) + 4);
287 if (integer->type != ACPI_TYPE_INTEGER) {
288 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
294 buf->type = ACPI_TYPE_BUFFER;
295 buf->buffer.length = 4;
296 buf->buffer.pointer = dst;
297 memcpy(dst, &integer->integer.value, 4);
303 static union acpi_object *acpi_label_write(acpi_handle handle, u32 offset,
307 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
308 struct acpi_object_list input = {
310 .pointer = (union acpi_object []) {
312 .integer.type = ACPI_TYPE_INTEGER,
313 .integer.value = offset,
316 .integer.type = ACPI_TYPE_INTEGER,
317 .integer.value = len,
320 .buffer.type = ACPI_TYPE_BUFFER,
321 .buffer.pointer = data,
322 .buffer.length = len,
327 rc = acpi_evaluate_object(handle, "_LSW", &input, &buf);
328 if (ACPI_FAILURE(rc))
330 return int_to_buf(buf.pointer);
333 static union acpi_object *acpi_label_read(acpi_handle handle, u32 offset,
337 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
338 struct acpi_object_list input = {
340 .pointer = (union acpi_object []) {
342 .integer.type = ACPI_TYPE_INTEGER,
343 .integer.value = offset,
346 .integer.type = ACPI_TYPE_INTEGER,
347 .integer.value = len,
352 rc = acpi_evaluate_object(handle, "_LSR", &input, &buf);
353 if (ACPI_FAILURE(rc))
355 return pkg_to_buf(buf.pointer);
358 static union acpi_object *acpi_label_info(acpi_handle handle)
361 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
363 rc = acpi_evaluate_object(handle, "_LSI", NULL, &buf);
364 if (ACPI_FAILURE(rc))
366 return pkg_to_buf(buf.pointer);
369 static u8 nfit_dsm_revid(unsigned family, unsigned func)
371 static const u8 revid_table[NVDIMM_FAMILY_MAX+1][32] = {
372 [NVDIMM_FAMILY_INTEL] = {
373 [NVDIMM_INTEL_GET_MODES] = 2,
374 [NVDIMM_INTEL_GET_FWINFO] = 2,
375 [NVDIMM_INTEL_START_FWUPDATE] = 2,
376 [NVDIMM_INTEL_SEND_FWUPDATE] = 2,
377 [NVDIMM_INTEL_FINISH_FWUPDATE] = 2,
378 [NVDIMM_INTEL_QUERY_FWUPDATE] = 2,
379 [NVDIMM_INTEL_SET_THRESHOLD] = 2,
380 [NVDIMM_INTEL_INJECT_ERROR] = 2,
381 [NVDIMM_INTEL_GET_SECURITY_STATE] = 2,
382 [NVDIMM_INTEL_SET_PASSPHRASE] = 2,
383 [NVDIMM_INTEL_DISABLE_PASSPHRASE] = 2,
384 [NVDIMM_INTEL_UNLOCK_UNIT] = 2,
385 [NVDIMM_INTEL_FREEZE_LOCK] = 2,
386 [NVDIMM_INTEL_SECURE_ERASE] = 2,
387 [NVDIMM_INTEL_OVERWRITE] = 2,
388 [NVDIMM_INTEL_QUERY_OVERWRITE] = 2,
389 [NVDIMM_INTEL_SET_MASTER_PASSPHRASE] = 2,
390 [NVDIMM_INTEL_MASTER_SECURE_ERASE] = 2,
395 if (family > NVDIMM_FAMILY_MAX)
399 id = revid_table[family][func];
401 return 1; /* default */
405 static bool payload_dumpable(struct nvdimm *nvdimm, unsigned int func)
407 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
409 if (nfit_mem && nfit_mem->family == NVDIMM_FAMILY_INTEL
410 && func >= NVDIMM_INTEL_GET_SECURITY_STATE
411 && func <= NVDIMM_INTEL_MASTER_SECURE_ERASE)
412 return IS_ENABLED(CONFIG_NFIT_SECURITY_DEBUG);
416 static int cmd_to_func(struct nfit_mem *nfit_mem, unsigned int cmd,
417 struct nd_cmd_pkg *call_pkg)
422 if (nfit_mem && nfit_mem->family != call_pkg->nd_family)
425 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
426 if (call_pkg->nd_reserved2[i])
428 return call_pkg->nd_command;
431 /* In the !call_pkg case, bus commands == bus functions */
435 /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
436 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
440 * Force function number validation to fail since 0 is never
441 * published as a valid function in dsm_mask.
446 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
447 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
449 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
450 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
451 union acpi_object in_obj, in_buf, *out_obj;
452 const struct nd_cmd_desc *desc = NULL;
453 struct device *dev = acpi_desc->dev;
454 struct nd_cmd_pkg *call_pkg = NULL;
455 const char *cmd_name, *dimm_name;
456 unsigned long cmd_mask, dsm_mask;
457 u32 offset, fw_status = 0;
465 if (cmd == ND_CMD_CALL)
467 func = cmd_to_func(nfit_mem, cmd, call_pkg);
472 struct acpi_device *adev = nfit_mem->adev;
477 dimm_name = nvdimm_name(nvdimm);
478 cmd_name = nvdimm_cmd_name(cmd);
479 cmd_mask = nvdimm_cmd_mask(nvdimm);
480 dsm_mask = nfit_mem->dsm_mask;
481 desc = nd_cmd_dimm_desc(cmd);
482 guid = to_nfit_uuid(nfit_mem->family);
483 handle = adev->handle;
485 struct acpi_device *adev = to_acpi_dev(acpi_desc);
487 cmd_name = nvdimm_bus_cmd_name(cmd);
488 cmd_mask = nd_desc->cmd_mask;
489 dsm_mask = nd_desc->bus_dsm_mask;
490 desc = nd_cmd_bus_desc(cmd);
491 guid = to_nfit_uuid(NFIT_DEV_BUS);
492 handle = adev->handle;
496 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
500 * Check for a valid command. For ND_CMD_CALL, we also have to
501 * make sure that the DSM function is supported.
503 if (cmd == ND_CMD_CALL && !test_bit(func, &dsm_mask))
505 else if (!test_bit(cmd, &cmd_mask))
508 in_obj.type = ACPI_TYPE_PACKAGE;
509 in_obj.package.count = 1;
510 in_obj.package.elements = &in_buf;
511 in_buf.type = ACPI_TYPE_BUFFER;
512 in_buf.buffer.pointer = buf;
513 in_buf.buffer.length = 0;
515 /* libnvdimm has already validated the input envelope */
516 for (i = 0; i < desc->in_num; i++)
517 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
521 /* skip over package wrapper */
522 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
523 in_buf.buffer.length = call_pkg->nd_size_in;
526 dev_dbg(dev, "%s cmd: %d: func: %d input length: %d\n",
527 dimm_name, cmd, func, in_buf.buffer.length);
528 if (payload_dumpable(nvdimm, func))
529 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
530 in_buf.buffer.pointer,
531 min_t(u32, 256, in_buf.buffer.length), true);
533 /* call the BIOS, prefer the named methods over _DSM if available */
534 if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE
535 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
536 out_obj = acpi_label_info(handle);
537 else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA
538 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
539 struct nd_cmd_get_config_data_hdr *p = buf;
541 out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
542 } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
543 && test_bit(NFIT_MEM_LSW, &nfit_mem->flags)) {
544 struct nd_cmd_set_config_hdr *p = buf;
546 out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
552 revid = nfit_dsm_revid(nfit_mem->family, func);
555 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
559 dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
563 if (out_obj->type != ACPI_TYPE_BUFFER) {
564 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
565 dimm_name, cmd_name, out_obj->type);
570 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
571 cmd_name, out_obj->buffer.length);
572 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
573 out_obj->buffer.pointer,
574 min_t(u32, 128, out_obj->buffer.length), true);
577 call_pkg->nd_fw_size = out_obj->buffer.length;
578 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
579 out_obj->buffer.pointer,
580 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
584 * Need to support FW function w/o known size in advance.
585 * Caller can determine required size based upon nd_fw_size.
586 * If we return an error (like elsewhere) then caller wouldn't
587 * be able to rely upon data returned to make calculation.
594 for (i = 0, offset = 0; i < desc->out_num; i++) {
595 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
596 (u32 *) out_obj->buffer.pointer,
597 out_obj->buffer.length - offset);
599 if (offset + out_size > out_obj->buffer.length) {
600 dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
601 dimm_name, cmd_name, i);
605 if (in_buf.buffer.length + offset + out_size > buf_len) {
606 dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
607 dimm_name, cmd_name, i);
611 memcpy(buf + in_buf.buffer.length + offset,
612 out_obj->buffer.pointer + offset, out_size);
617 * Set fw_status for all the commands with a known format to be
618 * later interpreted by xlat_status().
620 if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
621 && cmd <= ND_CMD_CLEAR_ERROR)
622 || (nvdimm && cmd >= ND_CMD_SMART
623 && cmd <= ND_CMD_VENDOR)))
624 fw_status = *(u32 *) out_obj->buffer.pointer;
626 if (offset + in_buf.buffer.length < buf_len) {
629 * status valid, return the number of bytes left
630 * unfilled in the output buffer
632 rc = buf_len - offset - in_buf.buffer.length;
634 *cmd_rc = xlat_status(nvdimm, buf, cmd,
637 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
638 __func__, dimm_name, cmd_name, buf_len,
645 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
653 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
655 static const char *spa_type_name(u16 type)
657 static const char *to_name[] = {
658 [NFIT_SPA_VOLATILE] = "volatile",
659 [NFIT_SPA_PM] = "pmem",
660 [NFIT_SPA_DCR] = "dimm-control-region",
661 [NFIT_SPA_BDW] = "block-data-window",
662 [NFIT_SPA_VDISK] = "volatile-disk",
663 [NFIT_SPA_VCD] = "volatile-cd",
664 [NFIT_SPA_PDISK] = "persistent-disk",
665 [NFIT_SPA_PCD] = "persistent-cd",
669 if (type > NFIT_SPA_PCD)
672 return to_name[type];
675 int nfit_spa_type(struct acpi_nfit_system_address *spa)
679 for (i = 0; i < NFIT_UUID_MAX; i++)
680 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
685 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
686 struct nfit_table_prev *prev,
687 struct acpi_nfit_system_address *spa)
689 struct device *dev = acpi_desc->dev;
690 struct nfit_spa *nfit_spa;
692 if (spa->header.length != sizeof(*spa))
695 list_for_each_entry(nfit_spa, &prev->spas, list) {
696 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
697 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
702 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
706 INIT_LIST_HEAD(&nfit_spa->list);
707 memcpy(nfit_spa->spa, spa, sizeof(*spa));
708 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
709 dev_dbg(dev, "spa index: %d type: %s\n",
711 spa_type_name(nfit_spa_type(spa)));
715 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
716 struct nfit_table_prev *prev,
717 struct acpi_nfit_memory_map *memdev)
719 struct device *dev = acpi_desc->dev;
720 struct nfit_memdev *nfit_memdev;
722 if (memdev->header.length != sizeof(*memdev))
725 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
726 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
727 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
731 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
735 INIT_LIST_HEAD(&nfit_memdev->list);
736 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
737 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
738 dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
739 memdev->device_handle, memdev->range_index,
740 memdev->region_index, memdev->flags);
744 int nfit_get_smbios_id(u32 device_handle, u16 *flags)
746 struct acpi_nfit_memory_map *memdev;
747 struct acpi_nfit_desc *acpi_desc;
748 struct nfit_mem *nfit_mem;
751 mutex_lock(&acpi_desc_lock);
752 list_for_each_entry(acpi_desc, &acpi_descs, list) {
753 mutex_lock(&acpi_desc->init_mutex);
754 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
755 memdev = __to_nfit_memdev(nfit_mem);
756 if (memdev->device_handle == device_handle) {
757 *flags = memdev->flags;
758 physical_id = memdev->physical_id;
759 mutex_unlock(&acpi_desc->init_mutex);
760 mutex_unlock(&acpi_desc_lock);
764 mutex_unlock(&acpi_desc->init_mutex);
766 mutex_unlock(&acpi_desc_lock);
770 EXPORT_SYMBOL_GPL(nfit_get_smbios_id);
773 * An implementation may provide a truncated control region if no block windows
776 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
778 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
783 return offsetof(struct acpi_nfit_control_region, window_size);
786 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
787 struct nfit_table_prev *prev,
788 struct acpi_nfit_control_region *dcr)
790 struct device *dev = acpi_desc->dev;
791 struct nfit_dcr *nfit_dcr;
793 if (!sizeof_dcr(dcr))
796 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
797 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
798 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
802 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
806 INIT_LIST_HEAD(&nfit_dcr->list);
807 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
808 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
809 dev_dbg(dev, "dcr index: %d windows: %d\n",
810 dcr->region_index, dcr->windows);
814 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
815 struct nfit_table_prev *prev,
816 struct acpi_nfit_data_region *bdw)
818 struct device *dev = acpi_desc->dev;
819 struct nfit_bdw *nfit_bdw;
821 if (bdw->header.length != sizeof(*bdw))
823 list_for_each_entry(nfit_bdw, &prev->bdws, list)
824 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
825 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
829 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
833 INIT_LIST_HEAD(&nfit_bdw->list);
834 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
835 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
836 dev_dbg(dev, "bdw dcr: %d windows: %d\n",
837 bdw->region_index, bdw->windows);
841 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
843 if (idt->header.length < sizeof(*idt))
845 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
848 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
849 struct nfit_table_prev *prev,
850 struct acpi_nfit_interleave *idt)
852 struct device *dev = acpi_desc->dev;
853 struct nfit_idt *nfit_idt;
855 if (!sizeof_idt(idt))
858 list_for_each_entry(nfit_idt, &prev->idts, list) {
859 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
862 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
863 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
868 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
872 INIT_LIST_HEAD(&nfit_idt->list);
873 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
874 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
875 dev_dbg(dev, "idt index: %d num_lines: %d\n",
876 idt->interleave_index, idt->line_count);
880 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
882 if (flush->header.length < sizeof(*flush))
884 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
887 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
888 struct nfit_table_prev *prev,
889 struct acpi_nfit_flush_address *flush)
891 struct device *dev = acpi_desc->dev;
892 struct nfit_flush *nfit_flush;
894 if (!sizeof_flush(flush))
897 list_for_each_entry(nfit_flush, &prev->flushes, list) {
898 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
901 if (memcmp(nfit_flush->flush, flush,
902 sizeof_flush(flush)) == 0) {
903 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
908 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
909 + sizeof_flush(flush), GFP_KERNEL);
912 INIT_LIST_HEAD(&nfit_flush->list);
913 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
914 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
915 dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
916 flush->device_handle, flush->hint_count);
920 static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
921 struct acpi_nfit_capabilities *pcap)
923 struct device *dev = acpi_desc->dev;
926 mask = (1 << (pcap->highest_capability + 1)) - 1;
927 acpi_desc->platform_cap = pcap->capabilities & mask;
928 dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
932 static void *add_table(struct acpi_nfit_desc *acpi_desc,
933 struct nfit_table_prev *prev, void *table, const void *end)
935 struct device *dev = acpi_desc->dev;
936 struct acpi_nfit_header *hdr;
937 void *err = ERR_PTR(-ENOMEM);
944 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
950 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
951 if (!add_spa(acpi_desc, prev, table))
954 case ACPI_NFIT_TYPE_MEMORY_MAP:
955 if (!add_memdev(acpi_desc, prev, table))
958 case ACPI_NFIT_TYPE_CONTROL_REGION:
959 if (!add_dcr(acpi_desc, prev, table))
962 case ACPI_NFIT_TYPE_DATA_REGION:
963 if (!add_bdw(acpi_desc, prev, table))
966 case ACPI_NFIT_TYPE_INTERLEAVE:
967 if (!add_idt(acpi_desc, prev, table))
970 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
971 if (!add_flush(acpi_desc, prev, table))
974 case ACPI_NFIT_TYPE_SMBIOS:
975 dev_dbg(dev, "smbios\n");
977 case ACPI_NFIT_TYPE_CAPABILITIES:
978 if (!add_platform_cap(acpi_desc, table))
982 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
986 return table + hdr->length;
989 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
990 struct nfit_mem *nfit_mem)
992 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
993 u16 dcr = nfit_mem->dcr->region_index;
994 struct nfit_spa *nfit_spa;
996 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
997 u16 range_index = nfit_spa->spa->range_index;
998 int type = nfit_spa_type(nfit_spa->spa);
999 struct nfit_memdev *nfit_memdev;
1001 if (type != NFIT_SPA_BDW)
1004 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1005 if (nfit_memdev->memdev->range_index != range_index)
1007 if (nfit_memdev->memdev->device_handle != device_handle)
1009 if (nfit_memdev->memdev->region_index != dcr)
1012 nfit_mem->spa_bdw = nfit_spa->spa;
1017 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
1018 nfit_mem->spa_dcr->range_index);
1019 nfit_mem->bdw = NULL;
1022 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
1023 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
1025 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
1026 struct nfit_memdev *nfit_memdev;
1027 struct nfit_bdw *nfit_bdw;
1028 struct nfit_idt *nfit_idt;
1029 u16 idt_idx, range_index;
1031 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
1032 if (nfit_bdw->bdw->region_index != dcr)
1034 nfit_mem->bdw = nfit_bdw->bdw;
1041 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
1043 if (!nfit_mem->spa_bdw)
1046 range_index = nfit_mem->spa_bdw->range_index;
1047 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1048 if (nfit_memdev->memdev->range_index != range_index ||
1049 nfit_memdev->memdev->region_index != dcr)
1051 nfit_mem->memdev_bdw = nfit_memdev->memdev;
1052 idt_idx = nfit_memdev->memdev->interleave_index;
1053 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1054 if (nfit_idt->idt->interleave_index != idt_idx)
1056 nfit_mem->idt_bdw = nfit_idt->idt;
1063 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
1064 struct acpi_nfit_system_address *spa)
1066 struct nfit_mem *nfit_mem, *found;
1067 struct nfit_memdev *nfit_memdev;
1068 int type = spa ? nfit_spa_type(spa) : 0;
1080 * This loop runs in two modes, when a dimm is mapped the loop
1081 * adds memdev associations to an existing dimm, or creates a
1082 * dimm. In the unmapped dimm case this loop sweeps for memdev
1083 * instances with an invalid / zero range_index and adds those
1084 * dimms without spa associations.
1086 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1087 struct nfit_flush *nfit_flush;
1088 struct nfit_dcr *nfit_dcr;
1092 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
1094 if (!spa && nfit_memdev->memdev->range_index)
1097 dcr = nfit_memdev->memdev->region_index;
1098 device_handle = nfit_memdev->memdev->device_handle;
1099 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1100 if (__to_nfit_memdev(nfit_mem)->device_handle
1109 nfit_mem = devm_kzalloc(acpi_desc->dev,
1110 sizeof(*nfit_mem), GFP_KERNEL);
1113 INIT_LIST_HEAD(&nfit_mem->list);
1114 nfit_mem->acpi_desc = acpi_desc;
1115 list_add(&nfit_mem->list, &acpi_desc->dimms);
1118 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1119 if (nfit_dcr->dcr->region_index != dcr)
1122 * Record the control region for the dimm. For
1123 * the ACPI 6.1 case, where there are separate
1124 * control regions for the pmem vs blk
1125 * interfaces, be sure to record the extended
1129 nfit_mem->dcr = nfit_dcr->dcr;
1130 else if (nfit_mem->dcr->windows == 0
1131 && nfit_dcr->dcr->windows)
1132 nfit_mem->dcr = nfit_dcr->dcr;
1136 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
1137 struct acpi_nfit_flush_address *flush;
1140 if (nfit_flush->flush->device_handle != device_handle)
1142 nfit_mem->nfit_flush = nfit_flush;
1143 flush = nfit_flush->flush;
1144 nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
1146 sizeof(struct resource),
1148 if (!nfit_mem->flush_wpq)
1150 for (i = 0; i < flush->hint_count; i++) {
1151 struct resource *res = &nfit_mem->flush_wpq[i];
1153 res->start = flush->hint_address[i];
1154 res->end = res->start + 8 - 1;
1159 if (dcr && !nfit_mem->dcr) {
1160 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
1161 spa->range_index, dcr);
1165 if (type == NFIT_SPA_DCR) {
1166 struct nfit_idt *nfit_idt;
1169 /* multiple dimms may share a SPA when interleaved */
1170 nfit_mem->spa_dcr = spa;
1171 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1172 idt_idx = nfit_memdev->memdev->interleave_index;
1173 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1174 if (nfit_idt->idt->interleave_index != idt_idx)
1176 nfit_mem->idt_dcr = nfit_idt->idt;
1179 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
1180 } else if (type == NFIT_SPA_PM) {
1182 * A single dimm may belong to multiple SPA-PM
1183 * ranges, record at least one in addition to
1184 * any SPA-DCR range.
1186 nfit_mem->memdev_pmem = nfit_memdev->memdev;
1188 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1194 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
1196 struct nfit_mem *a = container_of(_a, typeof(*a), list);
1197 struct nfit_mem *b = container_of(_b, typeof(*b), list);
1198 u32 handleA, handleB;
1200 handleA = __to_nfit_memdev(a)->device_handle;
1201 handleB = __to_nfit_memdev(b)->device_handle;
1202 if (handleA < handleB)
1204 else if (handleA > handleB)
1209 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
1211 struct nfit_spa *nfit_spa;
1216 * For each SPA-DCR or SPA-PMEM address range find its
1217 * corresponding MEMDEV(s). From each MEMDEV find the
1218 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1219 * try to find a SPA-BDW and a corresponding BDW that references
1220 * the DCR. Throw it all into an nfit_mem object. Note, that
1221 * BDWs are optional.
1223 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1224 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
1230 * If a DIMM has failed to be mapped into SPA there will be no
1231 * SPA entries above. Find and register all the unmapped DIMMs
1232 * for reporting and recovery purposes.
1234 rc = __nfit_mem_init(acpi_desc, NULL);
1238 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
1243 static ssize_t bus_dsm_mask_show(struct device *dev,
1244 struct device_attribute *attr, char *buf)
1246 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1247 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1249 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
1251 static struct device_attribute dev_attr_bus_dsm_mask =
1252 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
1254 static ssize_t revision_show(struct device *dev,
1255 struct device_attribute *attr, char *buf)
1257 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1258 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1259 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1261 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
1263 static DEVICE_ATTR_RO(revision);
1265 static ssize_t hw_error_scrub_show(struct device *dev,
1266 struct device_attribute *attr, char *buf)
1268 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1269 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1270 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1272 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
1276 * The 'hw_error_scrub' attribute can have the following values written to it:
1277 * '0': Switch to the default mode where an exception will only insert
1278 * the address of the memory error into the poison and badblocks lists.
1279 * '1': Enable a full scrub to happen if an exception for a memory error is
1282 static ssize_t hw_error_scrub_store(struct device *dev,
1283 struct device_attribute *attr, const char *buf, size_t size)
1285 struct nvdimm_bus_descriptor *nd_desc;
1289 rc = kstrtol(buf, 0, &val);
1294 nd_desc = dev_get_drvdata(dev);
1296 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1299 case HW_ERROR_SCRUB_ON:
1300 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1302 case HW_ERROR_SCRUB_OFF:
1303 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1315 static DEVICE_ATTR_RW(hw_error_scrub);
1318 * This shows the number of full Address Range Scrubs that have been
1319 * completed since driver load time. Userspace can wait on this using
1320 * select/poll etc. A '+' at the end indicates an ARS is in progress
1322 static ssize_t scrub_show(struct device *dev,
1323 struct device_attribute *attr, char *buf)
1325 struct nvdimm_bus_descriptor *nd_desc;
1326 struct acpi_nfit_desc *acpi_desc;
1327 ssize_t rc = -ENXIO;
1331 nd_desc = dev_get_drvdata(dev);
1336 acpi_desc = to_acpi_desc(nd_desc);
1338 mutex_lock(&acpi_desc->init_mutex);
1339 busy = test_bit(ARS_BUSY, &acpi_desc->scrub_flags)
1340 && !test_bit(ARS_CANCEL, &acpi_desc->scrub_flags);
1341 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count, busy ? "+\n" : "\n");
1342 /* Allow an admin to poll the busy state at a higher rate */
1343 if (busy && capable(CAP_SYS_RAWIO) && !test_and_set_bit(ARS_POLL,
1344 &acpi_desc->scrub_flags)) {
1345 acpi_desc->scrub_tmo = 1;
1346 mod_delayed_work(nfit_wq, &acpi_desc->dwork, HZ);
1349 mutex_unlock(&acpi_desc->init_mutex);
1354 static ssize_t scrub_store(struct device *dev,
1355 struct device_attribute *attr, const char *buf, size_t size)
1357 struct nvdimm_bus_descriptor *nd_desc;
1361 rc = kstrtol(buf, 0, &val);
1368 nd_desc = dev_get_drvdata(dev);
1370 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1372 rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
1379 static DEVICE_ATTR_RW(scrub);
1381 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1383 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1384 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1385 | 1 << ND_CMD_ARS_STATUS;
1387 return (nd_desc->cmd_mask & mask) == mask;
1390 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1392 struct device *dev = container_of(kobj, struct device, kobj);
1393 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1395 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1400 static struct attribute *acpi_nfit_attributes[] = {
1401 &dev_attr_revision.attr,
1402 &dev_attr_scrub.attr,
1403 &dev_attr_hw_error_scrub.attr,
1404 &dev_attr_bus_dsm_mask.attr,
1408 static const struct attribute_group acpi_nfit_attribute_group = {
1410 .attrs = acpi_nfit_attributes,
1411 .is_visible = nfit_visible,
1414 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1415 &nvdimm_bus_attribute_group,
1416 &acpi_nfit_attribute_group,
1420 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1422 struct nvdimm *nvdimm = to_nvdimm(dev);
1423 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1425 return __to_nfit_memdev(nfit_mem);
1428 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1430 struct nvdimm *nvdimm = to_nvdimm(dev);
1431 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1433 return nfit_mem->dcr;
1436 static ssize_t handle_show(struct device *dev,
1437 struct device_attribute *attr, char *buf)
1439 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1441 return sprintf(buf, "%#x\n", memdev->device_handle);
1443 static DEVICE_ATTR_RO(handle);
1445 static ssize_t phys_id_show(struct device *dev,
1446 struct device_attribute *attr, char *buf)
1448 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1450 return sprintf(buf, "%#x\n", memdev->physical_id);
1452 static DEVICE_ATTR_RO(phys_id);
1454 static ssize_t vendor_show(struct device *dev,
1455 struct device_attribute *attr, char *buf)
1457 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1459 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1461 static DEVICE_ATTR_RO(vendor);
1463 static ssize_t rev_id_show(struct device *dev,
1464 struct device_attribute *attr, char *buf)
1466 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1468 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1470 static DEVICE_ATTR_RO(rev_id);
1472 static ssize_t device_show(struct device *dev,
1473 struct device_attribute *attr, char *buf)
1475 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1477 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1479 static DEVICE_ATTR_RO(device);
1481 static ssize_t subsystem_vendor_show(struct device *dev,
1482 struct device_attribute *attr, char *buf)
1484 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1486 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1488 static DEVICE_ATTR_RO(subsystem_vendor);
1490 static ssize_t subsystem_rev_id_show(struct device *dev,
1491 struct device_attribute *attr, char *buf)
1493 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1495 return sprintf(buf, "0x%04x\n",
1496 be16_to_cpu(dcr->subsystem_revision_id));
1498 static DEVICE_ATTR_RO(subsystem_rev_id);
1500 static ssize_t subsystem_device_show(struct device *dev,
1501 struct device_attribute *attr, char *buf)
1503 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1505 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1507 static DEVICE_ATTR_RO(subsystem_device);
1509 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1511 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1514 if (nfit_mem->memdev_pmem)
1516 if (nfit_mem->memdev_bdw)
1521 static ssize_t format_show(struct device *dev,
1522 struct device_attribute *attr, char *buf)
1524 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1526 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1528 static DEVICE_ATTR_RO(format);
1530 static ssize_t format1_show(struct device *dev,
1531 struct device_attribute *attr, char *buf)
1534 ssize_t rc = -ENXIO;
1535 struct nfit_mem *nfit_mem;
1536 struct nfit_memdev *nfit_memdev;
1537 struct acpi_nfit_desc *acpi_desc;
1538 struct nvdimm *nvdimm = to_nvdimm(dev);
1539 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1541 nfit_mem = nvdimm_provider_data(nvdimm);
1542 acpi_desc = nfit_mem->acpi_desc;
1543 handle = to_nfit_memdev(dev)->device_handle;
1545 /* assumes DIMMs have at most 2 published interface codes */
1546 mutex_lock(&acpi_desc->init_mutex);
1547 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1548 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1549 struct nfit_dcr *nfit_dcr;
1551 if (memdev->device_handle != handle)
1554 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1555 if (nfit_dcr->dcr->region_index != memdev->region_index)
1557 if (nfit_dcr->dcr->code == dcr->code)
1559 rc = sprintf(buf, "0x%04x\n",
1560 le16_to_cpu(nfit_dcr->dcr->code));
1566 mutex_unlock(&acpi_desc->init_mutex);
1569 static DEVICE_ATTR_RO(format1);
1571 static ssize_t formats_show(struct device *dev,
1572 struct device_attribute *attr, char *buf)
1574 struct nvdimm *nvdimm = to_nvdimm(dev);
1576 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1578 static DEVICE_ATTR_RO(formats);
1580 static ssize_t serial_show(struct device *dev,
1581 struct device_attribute *attr, char *buf)
1583 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1585 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1587 static DEVICE_ATTR_RO(serial);
1589 static ssize_t family_show(struct device *dev,
1590 struct device_attribute *attr, char *buf)
1592 struct nvdimm *nvdimm = to_nvdimm(dev);
1593 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1595 if (nfit_mem->family < 0)
1597 return sprintf(buf, "%d\n", nfit_mem->family);
1599 static DEVICE_ATTR_RO(family);
1601 static ssize_t dsm_mask_show(struct device *dev,
1602 struct device_attribute *attr, char *buf)
1604 struct nvdimm *nvdimm = to_nvdimm(dev);
1605 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1607 if (nfit_mem->family < 0)
1609 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1611 static DEVICE_ATTR_RO(dsm_mask);
1613 static ssize_t flags_show(struct device *dev,
1614 struct device_attribute *attr, char *buf)
1616 struct nvdimm *nvdimm = to_nvdimm(dev);
1617 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1618 u16 flags = __to_nfit_memdev(nfit_mem)->flags;
1620 if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
1621 flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
1623 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1624 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1625 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1626 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1627 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1628 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1629 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1630 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1632 static DEVICE_ATTR_RO(flags);
1634 static ssize_t id_show(struct device *dev,
1635 struct device_attribute *attr, char *buf)
1637 struct nvdimm *nvdimm = to_nvdimm(dev);
1638 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1640 return sprintf(buf, "%s\n", nfit_mem->id);
1642 static DEVICE_ATTR_RO(id);
1644 static ssize_t dirty_shutdown_show(struct device *dev,
1645 struct device_attribute *attr, char *buf)
1647 struct nvdimm *nvdimm = to_nvdimm(dev);
1648 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1650 return sprintf(buf, "%d\n", nfit_mem->dirty_shutdown);
1652 static DEVICE_ATTR_RO(dirty_shutdown);
1654 static struct attribute *acpi_nfit_dimm_attributes[] = {
1655 &dev_attr_handle.attr,
1656 &dev_attr_phys_id.attr,
1657 &dev_attr_vendor.attr,
1658 &dev_attr_device.attr,
1659 &dev_attr_rev_id.attr,
1660 &dev_attr_subsystem_vendor.attr,
1661 &dev_attr_subsystem_device.attr,
1662 &dev_attr_subsystem_rev_id.attr,
1663 &dev_attr_format.attr,
1664 &dev_attr_formats.attr,
1665 &dev_attr_format1.attr,
1666 &dev_attr_serial.attr,
1667 &dev_attr_flags.attr,
1669 &dev_attr_family.attr,
1670 &dev_attr_dsm_mask.attr,
1671 &dev_attr_dirty_shutdown.attr,
1675 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1676 struct attribute *a, int n)
1678 struct device *dev = container_of(kobj, struct device, kobj);
1679 struct nvdimm *nvdimm = to_nvdimm(dev);
1680 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1682 if (!to_nfit_dcr(dev)) {
1683 /* Without a dcr only the memdev attributes can be surfaced */
1684 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1685 || a == &dev_attr_flags.attr
1686 || a == &dev_attr_family.attr
1687 || a == &dev_attr_dsm_mask.attr)
1692 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1695 if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
1696 && a == &dev_attr_dirty_shutdown.attr)
1702 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1704 .attrs = acpi_nfit_dimm_attributes,
1705 .is_visible = acpi_nfit_dimm_attr_visible,
1708 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1709 &nvdimm_attribute_group,
1710 &nd_device_attribute_group,
1711 &acpi_nfit_dimm_attribute_group,
1715 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1718 struct nfit_mem *nfit_mem;
1720 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1721 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1722 return nfit_mem->nvdimm;
1727 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1729 struct nfit_mem *nfit_mem;
1730 struct acpi_nfit_desc *acpi_desc;
1732 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
1735 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1736 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1741 acpi_desc = dev_get_drvdata(dev->parent);
1746 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1749 nfit_mem = dev_get_drvdata(dev);
1750 if (nfit_mem && nfit_mem->flags_attr)
1751 sysfs_notify_dirent(nfit_mem->flags_attr);
1753 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1755 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1757 struct acpi_device *adev = data;
1758 struct device *dev = &adev->dev;
1760 device_lock(dev->parent);
1761 __acpi_nvdimm_notify(dev, event);
1762 device_unlock(dev->parent);
1765 static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1770 status = acpi_get_handle(adev->handle, method, &handle);
1772 if (ACPI_SUCCESS(status))
1777 __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
1779 struct device *dev = &nfit_mem->adev->dev;
1780 struct nd_intel_smart smart = { 0 };
1781 union acpi_object in_buf = {
1782 .buffer.type = ACPI_TYPE_BUFFER,
1785 union acpi_object in_obj = {
1786 .package.type = ACPI_TYPE_PACKAGE,
1788 .package.elements = &in_buf,
1790 const u8 func = ND_INTEL_SMART;
1791 const guid_t *guid = to_nfit_uuid(nfit_mem->family);
1792 u8 revid = nfit_dsm_revid(nfit_mem->family, func);
1793 struct acpi_device *adev = nfit_mem->adev;
1794 acpi_handle handle = adev->handle;
1795 union acpi_object *out_obj;
1797 if ((nfit_mem->dsm_mask & (1 << func)) == 0)
1800 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
1801 if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER
1802 || out_obj->buffer.length < sizeof(smart)) {
1803 dev_dbg(dev->parent, "%s: failed to retrieve initial health\n",
1808 memcpy(&smart, out_obj->buffer.pointer, sizeof(smart));
1811 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
1812 if (smart.shutdown_state)
1813 set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
1816 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
1817 set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
1818 nfit_mem->dirty_shutdown = smart.shutdown_count;
1822 static void populate_shutdown_status(struct nfit_mem *nfit_mem)
1825 * For DIMMs that provide a dynamic facility to retrieve a
1826 * dirty-shutdown status and/or a dirty-shutdown count, cache
1827 * these values in nfit_mem.
1829 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1830 nfit_intel_shutdown_status(nfit_mem);
1833 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1834 struct nfit_mem *nfit_mem, u32 device_handle)
1836 struct acpi_device *adev, *adev_dimm;
1837 struct device *dev = acpi_desc->dev;
1838 unsigned long dsm_mask, label_mask;
1842 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1844 /* nfit test assumes 1:1 relationship between commands and dsms */
1845 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1846 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1848 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1849 sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
1850 be16_to_cpu(dcr->vendor_id),
1851 dcr->manufacturing_location,
1852 be16_to_cpu(dcr->manufacturing_date),
1853 be32_to_cpu(dcr->serial_number));
1855 sprintf(nfit_mem->id, "%04x-%08x",
1856 be16_to_cpu(dcr->vendor_id),
1857 be32_to_cpu(dcr->serial_number));
1859 adev = to_acpi_dev(acpi_desc);
1861 /* unit test case */
1862 populate_shutdown_status(nfit_mem);
1866 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1867 nfit_mem->adev = adev_dimm;
1869 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1871 return force_enable_dimms ? 0 : -ENODEV;
1874 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1875 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1876 dev_err(dev, "%s: notification registration failed\n",
1877 dev_name(&adev_dimm->dev));
1881 * Record nfit_mem for the notification path to track back to
1882 * the nfit sysfs attributes for this dimm device object.
1884 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1887 * There are 4 "legacy" NVDIMM command sets
1888 * (NVDIMM_FAMILY_{INTEL,MSFT,HPE1,HPE2}) that were created before
1889 * an EFI working group was established to constrain this
1890 * proliferation. The nfit driver probes for the supported command
1891 * set by GUID. Note, if you're a platform developer looking to add
1892 * a new command set to this probe, consider using an existing set,
1893 * or otherwise seek approval to publish the command set at
1894 * http://www.uefi.org/RFIC_LIST.
1896 * Note, that checking for function0 (bit0) tells us if any commands
1897 * are reachable through this GUID.
1899 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
1900 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1901 if (family < 0 || i == default_dsm_family)
1904 /* limit the supported commands to those that are publicly documented */
1905 nfit_mem->family = family;
1906 if (override_dsm_mask && !disable_vendor_specific)
1907 dsm_mask = override_dsm_mask;
1908 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1909 dsm_mask = NVDIMM_INTEL_CMDMASK;
1910 if (disable_vendor_specific)
1911 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1912 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1913 dsm_mask = 0x1c3c76;
1914 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1916 if (disable_vendor_specific)
1917 dsm_mask &= ~(1 << 8);
1918 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1919 dsm_mask = 0xffffffff;
1920 } else if (nfit_mem->family == NVDIMM_FAMILY_HYPERV) {
1923 dev_dbg(dev, "unknown dimm command family\n");
1924 nfit_mem->family = -1;
1925 /* DSMs are optional, continue loading the driver... */
1930 * Function 0 is the command interrogation function, don't
1931 * export it to potential userspace use, and enable it to be
1932 * used as an error value in acpi_nfit_ctl().
1936 guid = to_nfit_uuid(nfit_mem->family);
1937 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1938 if (acpi_check_dsm(adev_dimm->handle, guid,
1939 nfit_dsm_revid(nfit_mem->family, i),
1941 set_bit(i, &nfit_mem->dsm_mask);
1944 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1945 * due to their better semantics handling locked capacity.
1947 label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
1948 | 1 << ND_CMD_SET_CONFIG_DATA;
1949 if (family == NVDIMM_FAMILY_INTEL
1950 && (dsm_mask & label_mask) == label_mask)
1951 /* skip _LS{I,R,W} enabling */;
1953 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1954 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
1955 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
1956 set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1959 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
1960 && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
1961 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
1962 set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
1966 * Quirk read-only label configurations to preserve
1967 * access to label-less namespaces by default.
1969 if (!test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
1971 dev_dbg(dev, "%s: No _LSW, disable labels\n",
1972 dev_name(&adev_dimm->dev));
1973 clear_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1975 dev_dbg(dev, "%s: Force enable labels\n",
1976 dev_name(&adev_dimm->dev));
1979 populate_shutdown_status(nfit_mem);
1984 static void shutdown_dimm_notify(void *data)
1986 struct acpi_nfit_desc *acpi_desc = data;
1987 struct nfit_mem *nfit_mem;
1989 mutex_lock(&acpi_desc->init_mutex);
1991 * Clear out the nfit_mem->flags_attr and shut down dimm event
1994 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1995 struct acpi_device *adev_dimm = nfit_mem->adev;
1997 if (nfit_mem->flags_attr) {
1998 sysfs_put(nfit_mem->flags_attr);
1999 nfit_mem->flags_attr = NULL;
2002 acpi_remove_notify_handler(adev_dimm->handle,
2003 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
2004 dev_set_drvdata(&adev_dimm->dev, NULL);
2007 mutex_unlock(&acpi_desc->init_mutex);
2010 static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
2013 case NVDIMM_FAMILY_INTEL:
2014 return intel_security_ops;
2020 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
2022 struct nfit_mem *nfit_mem;
2023 int dimm_count = 0, rc;
2024 struct nvdimm *nvdimm;
2026 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2027 struct acpi_nfit_flush_address *flush;
2028 unsigned long flags = 0, cmd_mask;
2029 struct nfit_memdev *nfit_memdev;
2033 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2034 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
2040 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
2041 set_bit(NDD_ALIASING, &flags);
2043 /* collate flags across all memdevs for this dimm */
2044 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2045 struct acpi_nfit_memory_map *dimm_memdev;
2047 dimm_memdev = __to_nfit_memdev(nfit_mem);
2048 if (dimm_memdev->device_handle
2049 != nfit_memdev->memdev->device_handle)
2051 dimm_memdev->flags |= nfit_memdev->memdev->flags;
2054 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
2055 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
2056 set_bit(NDD_UNARMED, &flags);
2058 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
2063 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
2064 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
2065 * userspace interface.
2067 cmd_mask = 1UL << ND_CMD_CALL;
2068 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
2070 * These commands have a 1:1 correspondence
2071 * between DSM payload and libnvdimm ioctl
2074 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
2077 /* Quirk to ignore LOCAL for labels on HYPERV DIMMs */
2078 if (nfit_mem->family == NVDIMM_FAMILY_HYPERV)
2079 set_bit(NDD_NOBLK, &flags);
2081 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
2082 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
2083 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
2085 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
2086 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
2088 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
2090 nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
2091 acpi_nfit_dimm_attribute_groups,
2092 flags, cmd_mask, flush ? flush->hint_count : 0,
2093 nfit_mem->flush_wpq, &nfit_mem->id[0],
2094 acpi_nfit_get_security_ops(nfit_mem->family));
2098 nfit_mem->nvdimm = nvdimm;
2101 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
2104 dev_err(acpi_desc->dev, "Error found in NVDIMM %s flags:%s%s%s%s%s\n",
2105 nvdimm_name(nvdimm),
2106 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
2107 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
2108 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
2109 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
2110 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
2114 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
2119 * Now that dimms are successfully registered, and async registration
2120 * is flushed, attempt to enable event notification.
2122 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2123 struct kernfs_node *nfit_kernfs;
2125 nvdimm = nfit_mem->nvdimm;
2129 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
2131 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
2133 sysfs_put(nfit_kernfs);
2134 if (!nfit_mem->flags_attr)
2135 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
2136 nvdimm_name(nvdimm));
2139 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
2144 * These constants are private because there are no kernel consumers of
2147 enum nfit_aux_cmds {
2148 NFIT_CMD_TRANSLATE_SPA = 5,
2149 NFIT_CMD_ARS_INJECT_SET = 7,
2150 NFIT_CMD_ARS_INJECT_CLEAR = 8,
2151 NFIT_CMD_ARS_INJECT_GET = 9,
2154 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
2156 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2157 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
2158 struct acpi_device *adev;
2159 unsigned long dsm_mask;
2162 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
2163 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
2164 adev = to_acpi_dev(acpi_desc);
2168 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
2169 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2170 set_bit(i, &nd_desc->cmd_mask);
2171 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
2174 (1 << ND_CMD_ARS_CAP) |
2175 (1 << ND_CMD_ARS_START) |
2176 (1 << ND_CMD_ARS_STATUS) |
2177 (1 << ND_CMD_CLEAR_ERROR) |
2178 (1 << NFIT_CMD_TRANSLATE_SPA) |
2179 (1 << NFIT_CMD_ARS_INJECT_SET) |
2180 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
2181 (1 << NFIT_CMD_ARS_INJECT_GET);
2182 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
2183 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2184 set_bit(i, &nd_desc->bus_dsm_mask);
2187 static ssize_t range_index_show(struct device *dev,
2188 struct device_attribute *attr, char *buf)
2190 struct nd_region *nd_region = to_nd_region(dev);
2191 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
2193 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
2195 static DEVICE_ATTR_RO(range_index);
2197 static struct attribute *acpi_nfit_region_attributes[] = {
2198 &dev_attr_range_index.attr,
2202 static const struct attribute_group acpi_nfit_region_attribute_group = {
2204 .attrs = acpi_nfit_region_attributes,
2207 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
2208 &nd_region_attribute_group,
2209 &nd_mapping_attribute_group,
2210 &nd_device_attribute_group,
2211 &nd_numa_attribute_group,
2212 &acpi_nfit_region_attribute_group,
2216 /* enough info to uniquely specify an interleave set */
2217 struct nfit_set_info {
2218 struct nfit_set_info_map {
2225 struct nfit_set_info2 {
2226 struct nfit_set_info_map2 {
2230 u16 manufacturing_date;
2231 u8 manufacturing_location;
2236 static size_t sizeof_nfit_set_info(int num_mappings)
2238 return sizeof(struct nfit_set_info)
2239 + num_mappings * sizeof(struct nfit_set_info_map);
2242 static size_t sizeof_nfit_set_info2(int num_mappings)
2244 return sizeof(struct nfit_set_info2)
2245 + num_mappings * sizeof(struct nfit_set_info_map2);
2248 static int cmp_map_compat(const void *m0, const void *m1)
2250 const struct nfit_set_info_map *map0 = m0;
2251 const struct nfit_set_info_map *map1 = m1;
2253 return memcmp(&map0->region_offset, &map1->region_offset,
2257 static int cmp_map(const void *m0, const void *m1)
2259 const struct nfit_set_info_map *map0 = m0;
2260 const struct nfit_set_info_map *map1 = m1;
2262 if (map0->region_offset < map1->region_offset)
2264 else if (map0->region_offset > map1->region_offset)
2269 static int cmp_map2(const void *m0, const void *m1)
2271 const struct nfit_set_info_map2 *map0 = m0;
2272 const struct nfit_set_info_map2 *map1 = m1;
2274 if (map0->region_offset < map1->region_offset)
2276 else if (map0->region_offset > map1->region_offset)
2281 /* Retrieve the nth entry referencing this spa */
2282 static struct acpi_nfit_memory_map *memdev_from_spa(
2283 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2285 struct nfit_memdev *nfit_memdev;
2287 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2288 if (nfit_memdev->memdev->range_index == range_index)
2290 return nfit_memdev->memdev;
2294 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2295 struct nd_region_desc *ndr_desc,
2296 struct acpi_nfit_system_address *spa)
2298 struct device *dev = acpi_desc->dev;
2299 struct nd_interleave_set *nd_set;
2300 u16 nr = ndr_desc->num_mappings;
2301 struct nfit_set_info2 *info2;
2302 struct nfit_set_info *info;
2305 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2308 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2310 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2314 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2318 for (i = 0; i < nr; i++) {
2319 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
2320 struct nfit_set_info_map *map = &info->mapping[i];
2321 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2322 struct nvdimm *nvdimm = mapping->nvdimm;
2323 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2324 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2325 spa->range_index, i);
2326 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2328 if (!memdev || !nfit_mem->dcr) {
2329 dev_err(dev, "%s: failed to find DCR\n", __func__);
2333 map->region_offset = memdev->region_offset;
2334 map->serial_number = dcr->serial_number;
2336 map2->region_offset = memdev->region_offset;
2337 map2->serial_number = dcr->serial_number;
2338 map2->vendor_id = dcr->vendor_id;
2339 map2->manufacturing_date = dcr->manufacturing_date;
2340 map2->manufacturing_location = dcr->manufacturing_location;
2343 /* v1.1 namespaces */
2344 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2346 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2348 /* v1.2 namespaces */
2349 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2351 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2353 /* support v1.1 namespaces created with the wrong sort order */
2354 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2355 cmp_map_compat, NULL);
2356 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2358 /* record the result of the sort for the mapping position */
2359 for (i = 0; i < nr; i++) {
2360 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2363 for (j = 0; j < nr; j++) {
2364 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2365 struct nvdimm *nvdimm = mapping->nvdimm;
2366 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2367 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2369 if (map2->serial_number == dcr->serial_number &&
2370 map2->vendor_id == dcr->vendor_id &&
2371 map2->manufacturing_date == dcr->manufacturing_date &&
2372 map2->manufacturing_location
2373 == dcr->manufacturing_location) {
2374 mapping->position = i;
2380 ndr_desc->nd_set = nd_set;
2381 devm_kfree(dev, info);
2382 devm_kfree(dev, info2);
2387 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2389 struct acpi_nfit_interleave *idt = mmio->idt;
2390 u32 sub_line_offset, line_index, line_offset;
2391 u64 line_no, table_skip_count, table_offset;
2393 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2394 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2395 line_offset = idt->line_offset[line_index]
2397 table_offset = table_skip_count * mmio->table_size;
2399 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2402 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
2404 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2405 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
2406 const u32 STATUS_MASK = 0x80000037;
2408 if (mmio->num_lines)
2409 offset = to_interleave_offset(offset, mmio);
2411 return readl(mmio->addr.base + offset) & STATUS_MASK;
2414 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2415 resource_size_t dpa, unsigned int len, unsigned int write)
2418 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2421 BCW_OFFSET_MASK = (1ULL << 48)-1,
2423 BCW_LEN_MASK = (1ULL << 8) - 1,
2427 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2428 len = len >> L1_CACHE_SHIFT;
2429 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2430 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2432 offset = nfit_blk->cmd_offset + mmio->size * bw;
2433 if (mmio->num_lines)
2434 offset = to_interleave_offset(offset, mmio);
2436 writeq(cmd, mmio->addr.base + offset);
2437 nvdimm_flush(nfit_blk->nd_region);
2439 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
2440 readq(mmio->addr.base + offset);
2443 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2444 resource_size_t dpa, void *iobuf, size_t len, int rw,
2447 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2448 unsigned int copied = 0;
2452 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2453 + lane * mmio->size;
2454 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2459 if (mmio->num_lines) {
2462 offset = to_interleave_offset(base_offset + copied,
2464 div_u64_rem(offset, mmio->line_size, &line_offset);
2465 c = min_t(size_t, len, mmio->line_size - line_offset);
2467 offset = base_offset + nfit_blk->bdw_offset;
2472 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2474 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2475 arch_invalidate_pmem((void __force *)
2476 mmio->addr.aperture + offset, c);
2478 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2486 nvdimm_flush(nfit_blk->nd_region);
2488 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2492 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2493 resource_size_t dpa, void *iobuf, u64 len, int rw)
2495 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2496 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2497 struct nd_region *nd_region = nfit_blk->nd_region;
2498 unsigned int lane, copied = 0;
2501 lane = nd_region_acquire_lane(nd_region);
2503 u64 c = min(len, mmio->size);
2505 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2506 iobuf + copied, c, rw, lane);
2513 nd_region_release_lane(nd_region, lane);
2518 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2519 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2522 mmio->num_lines = idt->line_count;
2523 mmio->line_size = idt->line_size;
2524 if (interleave_ways == 0)
2526 mmio->table_size = mmio->num_lines * interleave_ways
2533 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2534 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2536 struct nd_cmd_dimm_flags flags;
2539 memset(&flags, 0, sizeof(flags));
2540 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2541 sizeof(flags), NULL);
2543 if (rc >= 0 && flags.status == 0)
2544 nfit_blk->dimm_flags = flags.flags;
2545 else if (rc == -ENOTTY) {
2546 /* fall back to a conservative default */
2547 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2555 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2558 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2559 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2560 struct nfit_blk_mmio *mmio;
2561 struct nfit_blk *nfit_blk;
2562 struct nfit_mem *nfit_mem;
2563 struct nvdimm *nvdimm;
2566 nvdimm = nd_blk_region_to_dimm(ndbr);
2567 nfit_mem = nvdimm_provider_data(nvdimm);
2568 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2569 dev_dbg(dev, "missing%s%s%s\n",
2570 nfit_mem ? "" : " nfit_mem",
2571 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2572 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2576 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2579 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2580 nfit_blk->nd_region = to_nd_region(dev);
2582 /* map block aperture memory */
2583 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2584 mmio = &nfit_blk->mmio[BDW];
2585 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2586 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2587 if (!mmio->addr.base) {
2588 dev_dbg(dev, "%s failed to map bdw\n",
2589 nvdimm_name(nvdimm));
2592 mmio->size = nfit_mem->bdw->size;
2593 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2594 mmio->idt = nfit_mem->idt_bdw;
2595 mmio->spa = nfit_mem->spa_bdw;
2596 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2597 nfit_mem->memdev_bdw->interleave_ways);
2599 dev_dbg(dev, "%s failed to init bdw interleave\n",
2600 nvdimm_name(nvdimm));
2604 /* map block control memory */
2605 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2606 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2607 mmio = &nfit_blk->mmio[DCR];
2608 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2609 nfit_mem->spa_dcr->length);
2610 if (!mmio->addr.base) {
2611 dev_dbg(dev, "%s failed to map dcr\n",
2612 nvdimm_name(nvdimm));
2615 mmio->size = nfit_mem->dcr->window_size;
2616 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2617 mmio->idt = nfit_mem->idt_dcr;
2618 mmio->spa = nfit_mem->spa_dcr;
2619 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2620 nfit_mem->memdev_dcr->interleave_ways);
2622 dev_dbg(dev, "%s failed to init dcr interleave\n",
2623 nvdimm_name(nvdimm));
2627 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2629 dev_dbg(dev, "%s failed get DIMM flags\n",
2630 nvdimm_name(nvdimm));
2634 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2635 dev_warn(dev, "unable to guarantee persistence of writes\n");
2637 if (mmio->line_size == 0)
2640 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2641 + 8 > mmio->line_size) {
2642 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2644 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2645 + 8 > mmio->line_size) {
2646 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2653 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2654 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2656 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2657 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2660 cmd->address = spa->address;
2661 cmd->length = spa->length;
2662 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2663 sizeof(*cmd), &cmd_rc);
2669 static int ars_start(struct acpi_nfit_desc *acpi_desc,
2670 struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
2674 struct nd_cmd_ars_start ars_start;
2675 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2676 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2678 memset(&ars_start, 0, sizeof(ars_start));
2679 ars_start.address = spa->address;
2680 ars_start.length = spa->length;
2681 if (req_type == ARS_REQ_SHORT)
2682 ars_start.flags = ND_ARS_RETURN_PREV_DATA;
2683 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2684 ars_start.type = ND_ARS_PERSISTENT;
2685 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2686 ars_start.type = ND_ARS_VOLATILE;
2690 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2691 sizeof(ars_start), &cmd_rc);
2697 set_bit(ARS_VALID, &acpi_desc->scrub_flags);
2701 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2704 struct nd_cmd_ars_start ars_start;
2705 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2706 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2708 ars_start = (struct nd_cmd_ars_start) {
2709 .address = ars_status->restart_address,
2710 .length = ars_status->restart_length,
2711 .type = ars_status->type,
2713 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2714 sizeof(ars_start), &cmd_rc);
2720 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2722 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2723 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2726 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2727 acpi_desc->max_ars, &cmd_rc);
2733 static void ars_complete(struct acpi_nfit_desc *acpi_desc,
2734 struct nfit_spa *nfit_spa)
2736 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2737 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2738 struct nd_region *nd_region = nfit_spa->nd_region;
2741 lockdep_assert_held(&acpi_desc->init_mutex);
2743 * Only advance the ARS state for ARS runs initiated by the
2744 * kernel, ignore ARS results from BIOS initiated runs for scrub
2745 * completion tracking.
2747 if (acpi_desc->scrub_spa != nfit_spa)
2750 if ((ars_status->address >= spa->address && ars_status->address
2751 < spa->address + spa->length)
2752 || (ars_status->address < spa->address)) {
2754 * Assume that if a scrub starts at an offset from the
2755 * start of nfit_spa that we are in the continuation
2758 * Otherwise, if the scrub covers the spa range, mark
2759 * any pending request complete.
2761 if (ars_status->address + ars_status->length
2762 >= spa->address + spa->length)
2769 acpi_desc->scrub_spa = NULL;
2771 dev = nd_region_dev(nd_region);
2772 nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
2774 dev = acpi_desc->dev;
2775 dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
2778 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
2780 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2781 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2786 * First record starts at 44 byte offset from the start of the
2789 if (ars_status->out_length < 44)
2793 * Ignore potentially stale results that are only refreshed
2794 * after a start-ARS event.
2796 if (!test_and_clear_bit(ARS_VALID, &acpi_desc->scrub_flags)) {
2797 dev_dbg(acpi_desc->dev, "skip %d stale records\n",
2798 ars_status->num_records);
2802 for (i = 0; i < ars_status->num_records; i++) {
2803 /* only process full records */
2804 if (ars_status->out_length
2805 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2807 rc = nvdimm_bus_add_badrange(nvdimm_bus,
2808 ars_status->records[i].err_address,
2809 ars_status->records[i].length);
2813 if (i < ars_status->num_records)
2814 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2819 static void acpi_nfit_remove_resource(void *data)
2821 struct resource *res = data;
2823 remove_resource(res);
2826 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2827 struct nd_region_desc *ndr_desc)
2829 struct resource *res, *nd_res = ndr_desc->res;
2832 /* No operation if the region is already registered as PMEM */
2833 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2834 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2835 if (is_pmem == REGION_INTERSECTS)
2838 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2842 res->name = "Persistent Memory";
2843 res->start = nd_res->start;
2844 res->end = nd_res->end;
2845 res->flags = IORESOURCE_MEM;
2846 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2848 ret = insert_resource(&iomem_resource, res);
2852 ret = devm_add_action_or_reset(acpi_desc->dev,
2853 acpi_nfit_remove_resource,
2861 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2862 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2863 struct acpi_nfit_memory_map *memdev,
2864 struct nfit_spa *nfit_spa)
2866 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2867 memdev->device_handle);
2868 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2869 struct nd_blk_region_desc *ndbr_desc;
2870 struct nfit_mem *nfit_mem;
2874 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2875 spa->range_index, memdev->device_handle);
2879 mapping->nvdimm = nvdimm;
2880 switch (nfit_spa_type(spa)) {
2882 case NFIT_SPA_VOLATILE:
2883 mapping->start = memdev->address;
2884 mapping->size = memdev->region_size;
2887 nfit_mem = nvdimm_provider_data(nvdimm);
2888 if (!nfit_mem || !nfit_mem->bdw) {
2889 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2890 spa->range_index, nvdimm_name(nvdimm));
2894 mapping->size = nfit_mem->bdw->capacity;
2895 mapping->start = nfit_mem->bdw->start_address;
2896 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2897 ndr_desc->mapping = mapping;
2898 ndr_desc->num_mappings = 1;
2899 ndbr_desc = to_blk_region_desc(ndr_desc);
2900 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2901 ndbr_desc->do_io = acpi_desc->blk_do_io;
2902 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2905 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2907 if (!nfit_spa->nd_region)
2915 static bool nfit_spa_is_virtual(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_PDISK ||
2920 nfit_spa_type(spa) == NFIT_SPA_PCD);
2923 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2925 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2926 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2927 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2930 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2931 struct nfit_spa *nfit_spa)
2933 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2934 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2935 struct nd_blk_region_desc ndbr_desc;
2936 struct nd_region_desc *ndr_desc;
2937 struct nfit_memdev *nfit_memdev;
2938 struct nvdimm_bus *nvdimm_bus;
2939 struct resource res;
2942 if (nfit_spa->nd_region)
2945 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2946 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
2950 memset(&res, 0, sizeof(res));
2951 memset(&mappings, 0, sizeof(mappings));
2952 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2953 res.start = spa->address;
2954 res.end = res.start + spa->length - 1;
2955 ndr_desc = &ndbr_desc.ndr_desc;
2956 ndr_desc->res = &res;
2957 ndr_desc->provider_data = nfit_spa;
2958 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2959 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID) {
2960 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2961 spa->proximity_domain);
2962 ndr_desc->target_node = acpi_map_pxm_to_node(
2963 spa->proximity_domain);
2965 ndr_desc->numa_node = NUMA_NO_NODE;
2966 ndr_desc->target_node = NUMA_NO_NODE;
2970 * Persistence domain bits are hierarchical, if
2971 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
2972 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
2974 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2975 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2976 else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2977 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2979 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2980 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2981 struct nd_mapping_desc *mapping;
2983 if (memdev->range_index != spa->range_index)
2985 if (count >= ND_MAX_MAPPINGS) {
2986 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2987 spa->range_index, ND_MAX_MAPPINGS);
2990 mapping = &mappings[count++];
2991 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2997 ndr_desc->mapping = mappings;
2998 ndr_desc->num_mappings = count;
2999 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
3003 nvdimm_bus = acpi_desc->nvdimm_bus;
3004 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
3005 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
3007 dev_warn(acpi_desc->dev,
3008 "failed to insert pmem resource to iomem: %d\n",
3013 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
3015 if (!nfit_spa->nd_region)
3017 } else if (nfit_spa_is_volatile(spa)) {
3018 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
3020 if (!nfit_spa->nd_region)
3022 } else if (nfit_spa_is_virtual(spa)) {
3023 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
3025 if (!nfit_spa->nd_region)
3031 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
3032 nfit_spa->spa->range_index);
3036 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
3038 struct device *dev = acpi_desc->dev;
3039 struct nd_cmd_ars_status *ars_status;
3041 if (acpi_desc->ars_status) {
3042 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3046 ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
3049 acpi_desc->ars_status = ars_status;
3053 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
3057 if (ars_status_alloc(acpi_desc))
3060 rc = ars_get_status(acpi_desc);
3062 if (rc < 0 && rc != -ENOSPC)
3065 if (ars_status_process_records(acpi_desc))
3066 dev_err(acpi_desc->dev, "Failed to process ARS records\n");
3071 static int ars_register(struct acpi_nfit_desc *acpi_desc,
3072 struct nfit_spa *nfit_spa)
3076 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3077 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3079 set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3081 set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
3083 switch (acpi_nfit_query_poison(acpi_desc)) {
3087 rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
3088 /* shouldn't happen, try again later */
3092 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3095 clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3096 rc = acpi_nfit_query_poison(acpi_desc);
3099 acpi_desc->scrub_spa = nfit_spa;
3100 ars_complete(acpi_desc, nfit_spa);
3102 * If ars_complete() says we didn't complete the
3103 * short scrub, we'll try again with a long
3106 acpi_desc->scrub_spa = NULL;
3111 * BIOS was using ARS, wait for it to complete (or
3112 * resources to become available) and then perform our
3117 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3121 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3124 static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
3126 struct nfit_spa *nfit_spa;
3128 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3129 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3131 ars_complete(acpi_desc, nfit_spa);
3135 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
3138 unsigned int tmo = acpi_desc->scrub_tmo;
3139 struct device *dev = acpi_desc->dev;
3140 struct nfit_spa *nfit_spa;
3142 lockdep_assert_held(&acpi_desc->init_mutex);
3144 if (test_bit(ARS_CANCEL, &acpi_desc->scrub_flags))
3147 if (query_rc == -EBUSY) {
3148 dev_dbg(dev, "ARS: ARS busy\n");
3149 return min(30U * 60U, tmo * 2);
3151 if (query_rc == -ENOSPC) {
3152 dev_dbg(dev, "ARS: ARS continue\n");
3153 ars_continue(acpi_desc);
3156 if (query_rc && query_rc != -EAGAIN) {
3157 unsigned long long addr, end;
3159 addr = acpi_desc->ars_status->address;
3160 end = addr + acpi_desc->ars_status->length;
3161 dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
3165 ars_complete_all(acpi_desc);
3166 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3167 enum nfit_ars_state req_type;
3170 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3173 /* prefer short ARS requests first */
3174 if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
3175 req_type = ARS_REQ_SHORT;
3176 else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
3177 req_type = ARS_REQ_LONG;
3180 rc = ars_start(acpi_desc, nfit_spa, req_type);
3182 dev = nd_region_dev(nfit_spa->nd_region);
3183 dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
3184 nfit_spa->spa->range_index,
3185 req_type == ARS_REQ_SHORT ? "short" : "long",
3188 * Hmm, we raced someone else starting ARS? Try again in
3194 dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
3195 "scrub start while range %d active\n",
3196 acpi_desc->scrub_spa->spa->range_index);
3197 clear_bit(req_type, &nfit_spa->ars_state);
3198 acpi_desc->scrub_spa = nfit_spa;
3200 * Consider this spa last for future scrub
3203 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
3207 dev_err(dev, "ARS: range %d ARS failed (%d)\n",
3208 nfit_spa->spa->range_index, rc);
3209 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3214 static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
3216 lockdep_assert_held(&acpi_desc->init_mutex);
3218 set_bit(ARS_BUSY, &acpi_desc->scrub_flags);
3219 /* note this should only be set from within the workqueue */
3221 acpi_desc->scrub_tmo = tmo;
3222 queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
3225 static void sched_ars(struct acpi_nfit_desc *acpi_desc)
3227 __sched_ars(acpi_desc, 0);
3230 static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
3232 lockdep_assert_held(&acpi_desc->init_mutex);
3234 clear_bit(ARS_BUSY, &acpi_desc->scrub_flags);
3235 acpi_desc->scrub_count++;
3236 if (acpi_desc->scrub_count_state)
3237 sysfs_notify_dirent(acpi_desc->scrub_count_state);
3240 static void acpi_nfit_scrub(struct work_struct *work)
3242 struct acpi_nfit_desc *acpi_desc;
3246 acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
3247 mutex_lock(&acpi_desc->init_mutex);
3248 query_rc = acpi_nfit_query_poison(acpi_desc);
3249 tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
3251 __sched_ars(acpi_desc, tmo);
3253 notify_ars_done(acpi_desc);
3254 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3255 clear_bit(ARS_POLL, &acpi_desc->scrub_flags);
3256 mutex_unlock(&acpi_desc->init_mutex);
3259 static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
3260 struct nfit_spa *nfit_spa)
3262 int type = nfit_spa_type(nfit_spa->spa);
3263 struct nd_cmd_ars_cap ars_cap;
3266 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3267 memset(&ars_cap, 0, sizeof(ars_cap));
3268 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
3271 /* check that the supported scrub types match the spa type */
3272 if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
3273 & ND_ARS_VOLATILE) == 0)
3275 if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
3276 & ND_ARS_PERSISTENT) == 0)
3279 nfit_spa->max_ars = ars_cap.max_ars_out;
3280 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
3281 acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
3282 clear_bit(ARS_FAILED, &nfit_spa->ars_state);
3285 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3287 struct nfit_spa *nfit_spa;
3290 set_bit(ARS_VALID, &acpi_desc->scrub_flags);
3291 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3292 switch (nfit_spa_type(nfit_spa->spa)) {
3293 case NFIT_SPA_VOLATILE:
3295 acpi_nfit_init_ars(acpi_desc, nfit_spa);
3300 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3301 switch (nfit_spa_type(nfit_spa->spa)) {
3302 case NFIT_SPA_VOLATILE:
3304 /* register regions and kick off initial ARS run */
3305 rc = ars_register(acpi_desc, nfit_spa);
3310 /* nothing to register */
3313 case NFIT_SPA_VDISK:
3315 case NFIT_SPA_PDISK:
3317 /* register known regions that don't support ARS */
3318 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3323 /* don't register unknown regions */
3327 sched_ars(acpi_desc);
3331 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3332 struct nfit_table_prev *prev)
3334 struct device *dev = acpi_desc->dev;
3336 if (!list_empty(&prev->spas) ||
3337 !list_empty(&prev->memdevs) ||
3338 !list_empty(&prev->dcrs) ||
3339 !list_empty(&prev->bdws) ||
3340 !list_empty(&prev->idts) ||
3341 !list_empty(&prev->flushes)) {
3342 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3348 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3350 struct device *dev = acpi_desc->dev;
3351 struct kernfs_node *nfit;
3352 struct device *bus_dev;
3354 if (!ars_supported(acpi_desc->nvdimm_bus))
3357 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3358 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3360 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3363 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3365 if (!acpi_desc->scrub_count_state) {
3366 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3373 static void acpi_nfit_unregister(void *data)
3375 struct acpi_nfit_desc *acpi_desc = data;
3377 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
3380 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
3382 struct device *dev = acpi_desc->dev;
3383 struct nfit_table_prev prev;
3387 if (!acpi_desc->nvdimm_bus) {
3388 acpi_nfit_init_dsms(acpi_desc);
3390 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3391 &acpi_desc->nd_desc);
3392 if (!acpi_desc->nvdimm_bus)
3395 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
3400 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3404 /* register this acpi_desc for mce notifications */
3405 mutex_lock(&acpi_desc_lock);
3406 list_add_tail(&acpi_desc->list, &acpi_descs);
3407 mutex_unlock(&acpi_desc_lock);
3410 mutex_lock(&acpi_desc->init_mutex);
3412 INIT_LIST_HEAD(&prev.spas);
3413 INIT_LIST_HEAD(&prev.memdevs);
3414 INIT_LIST_HEAD(&prev.dcrs);
3415 INIT_LIST_HEAD(&prev.bdws);
3416 INIT_LIST_HEAD(&prev.idts);
3417 INIT_LIST_HEAD(&prev.flushes);
3419 list_cut_position(&prev.spas, &acpi_desc->spas,
3420 acpi_desc->spas.prev);
3421 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3422 acpi_desc->memdevs.prev);
3423 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3424 acpi_desc->dcrs.prev);
3425 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3426 acpi_desc->bdws.prev);
3427 list_cut_position(&prev.idts, &acpi_desc->idts,
3428 acpi_desc->idts.prev);
3429 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3430 acpi_desc->flushes.prev);
3433 while (!IS_ERR_OR_NULL(data))
3434 data = add_table(acpi_desc, &prev, data, end);
3437 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
3442 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3446 rc = nfit_mem_init(acpi_desc);
3450 rc = acpi_nfit_register_dimms(acpi_desc);
3454 rc = acpi_nfit_register_regions(acpi_desc);
3457 mutex_unlock(&acpi_desc->init_mutex);
3460 EXPORT_SYMBOL_GPL(acpi_nfit_init);
3462 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3464 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3465 struct device *dev = acpi_desc->dev;
3467 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3471 /* Bounce the init_mutex to complete initial registration */
3472 mutex_lock(&acpi_desc->init_mutex);
3473 mutex_unlock(&acpi_desc->init_mutex);
3478 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3479 struct nvdimm *nvdimm, unsigned int cmd)
3481 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3485 if (cmd != ND_CMD_ARS_START)
3489 * The kernel and userspace may race to initiate a scrub, but
3490 * the scrub thread is prepared to lose that initial race. It
3491 * just needs guarantees that any ARS it initiates are not
3492 * interrupted by any intervening start requests from userspace.
3494 if (work_busy(&acpi_desc->dwork.work))
3500 /* prevent security commands from being issued via ioctl */
3501 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3502 struct nvdimm *nvdimm, unsigned int cmd, void *buf)
3504 struct nd_cmd_pkg *call_pkg = buf;
3507 if (nvdimm && cmd == ND_CMD_CALL &&
3508 call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
3509 func = call_pkg->nd_command;
3510 if ((1 << func) & NVDIMM_INTEL_SECURITY_CMDMASK)
3514 return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
3517 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
3518 enum nfit_ars_state req_type)
3520 struct device *dev = acpi_desc->dev;
3521 int scheduled = 0, busy = 0;
3522 struct nfit_spa *nfit_spa;
3524 mutex_lock(&acpi_desc->init_mutex);
3525 if (test_bit(ARS_CANCEL, &acpi_desc->scrub_flags)) {
3526 mutex_unlock(&acpi_desc->init_mutex);
3530 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3531 int type = nfit_spa_type(nfit_spa->spa);
3533 if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
3535 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3538 if (test_and_set_bit(req_type, &nfit_spa->ars_state))
3544 sched_ars(acpi_desc);
3545 dev_dbg(dev, "ars_scan triggered\n");
3547 mutex_unlock(&acpi_desc->init_mutex);
3556 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3558 struct nvdimm_bus_descriptor *nd_desc;
3560 dev_set_drvdata(dev, acpi_desc);
3561 acpi_desc->dev = dev;
3562 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3563 nd_desc = &acpi_desc->nd_desc;
3564 nd_desc->provider_name = "ACPI.NFIT";
3565 nd_desc->module = THIS_MODULE;
3566 nd_desc->ndctl = acpi_nfit_ctl;
3567 nd_desc->flush_probe = acpi_nfit_flush_probe;
3568 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3569 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3571 INIT_LIST_HEAD(&acpi_desc->spas);
3572 INIT_LIST_HEAD(&acpi_desc->dcrs);
3573 INIT_LIST_HEAD(&acpi_desc->bdws);
3574 INIT_LIST_HEAD(&acpi_desc->idts);
3575 INIT_LIST_HEAD(&acpi_desc->flushes);
3576 INIT_LIST_HEAD(&acpi_desc->memdevs);
3577 INIT_LIST_HEAD(&acpi_desc->dimms);
3578 INIT_LIST_HEAD(&acpi_desc->list);
3579 mutex_init(&acpi_desc->init_mutex);
3580 acpi_desc->scrub_tmo = 1;
3581 INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
3583 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3585 static void acpi_nfit_put_table(void *table)
3587 acpi_put_table(table);
3590 void acpi_nfit_shutdown(void *data)
3592 struct acpi_nfit_desc *acpi_desc = data;
3593 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3596 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3599 mutex_lock(&acpi_desc_lock);
3600 list_del(&acpi_desc->list);
3601 mutex_unlock(&acpi_desc_lock);
3603 mutex_lock(&acpi_desc->init_mutex);
3604 set_bit(ARS_CANCEL, &acpi_desc->scrub_flags);
3605 cancel_delayed_work_sync(&acpi_desc->dwork);
3606 mutex_unlock(&acpi_desc->init_mutex);
3609 * Bounce the nvdimm bus lock to make sure any in-flight
3610 * acpi_nfit_ars_rescan() submissions have had a chance to
3611 * either submit or see ->cancel set.
3613 device_lock(bus_dev);
3614 device_unlock(bus_dev);
3616 flush_workqueue(nfit_wq);
3618 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3620 static int acpi_nfit_add(struct acpi_device *adev)
3622 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3623 struct acpi_nfit_desc *acpi_desc;
3624 struct device *dev = &adev->dev;
3625 struct acpi_table_header *tbl;
3626 acpi_status status = AE_OK;
3630 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3631 if (ACPI_FAILURE(status)) {
3632 /* The NVDIMM root device allows OS to trigger enumeration of
3633 * NVDIMMs through NFIT at boot time and re-enumeration at
3634 * root level via the _FIT method during runtime.
3635 * This is ok to return 0 here, we could have an nvdimm
3636 * hotplugged later and evaluate _FIT method which returns
3637 * data in the format of a series of NFIT Structures.
3639 dev_dbg(dev, "failed to find NFIT at startup\n");
3643 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3648 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3651 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3653 /* Save the acpi header for exporting the revision via sysfs */
3654 acpi_desc->acpi_header = *tbl;
3656 /* Evaluate _FIT and override with that if present */
3657 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3658 if (ACPI_SUCCESS(status) && buf.length > 0) {
3659 union acpi_object *obj = buf.pointer;
3661 if (obj->type == ACPI_TYPE_BUFFER)
3662 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3663 obj->buffer.length);
3665 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3669 /* skip over the lead-in header table */
3670 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3671 + sizeof(struct acpi_table_nfit),
3672 sz - sizeof(struct acpi_table_nfit));
3676 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3679 static int acpi_nfit_remove(struct acpi_device *adev)
3681 /* see acpi_nfit_unregister */
3685 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3687 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3688 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3689 union acpi_object *obj;
3694 /* dev->driver may be null if we're being removed */
3695 dev_dbg(dev, "no driver found for dev\n");
3700 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3703 acpi_nfit_desc_init(acpi_desc, dev);
3706 * Finish previous registration before considering new
3709 flush_workqueue(nfit_wq);
3713 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3714 if (ACPI_FAILURE(status)) {
3715 dev_err(dev, "failed to evaluate _FIT\n");
3720 if (obj->type == ACPI_TYPE_BUFFER) {
3721 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3722 obj->buffer.length);
3724 dev_err(dev, "failed to merge updated NFIT\n");
3726 dev_err(dev, "Invalid _FIT\n");
3730 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3732 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3734 if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
3735 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
3737 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
3740 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3742 dev_dbg(dev, "event: 0x%x\n", event);
3745 case NFIT_NOTIFY_UPDATE:
3746 return acpi_nfit_update_notify(dev, handle);
3747 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3748 return acpi_nfit_uc_error_notify(dev, handle);
3753 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3755 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3757 device_lock(&adev->dev);
3758 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3759 device_unlock(&adev->dev);
3762 static const struct acpi_device_id acpi_nfit_ids[] = {
3766 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3768 static struct acpi_driver acpi_nfit_driver = {
3769 .name = KBUILD_MODNAME,
3770 .ids = acpi_nfit_ids,
3772 .add = acpi_nfit_add,
3773 .remove = acpi_nfit_remove,
3774 .notify = acpi_nfit_notify,
3778 static __init int nfit_init(void)
3782 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3783 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3784 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3785 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3786 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3787 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3788 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3789 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
3791 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3792 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3793 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3794 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3795 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3796 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3797 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3798 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3799 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3800 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3801 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3802 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3803 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3804 guid_parse(UUID_NFIT_DIMM_N_HYPERV, &nfit_uuid[NFIT_DEV_DIMM_N_HYPERV]);
3806 nfit_wq = create_singlethread_workqueue("nfit");
3810 nfit_mce_register();
3811 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3813 nfit_mce_unregister();
3814 destroy_workqueue(nfit_wq);
3821 static __exit void nfit_exit(void)
3823 nfit_mce_unregister();
3824 acpi_bus_unregister_driver(&acpi_nfit_driver);
3825 destroy_workqueue(nfit_wq);
3826 WARN_ON(!list_empty(&acpi_descs));
3829 module_init(nfit_init);
3830 module_exit(nfit_exit);
3831 MODULE_LICENSE("GPL v2");
3832 MODULE_AUTHOR("Intel Corporation");