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);
571 call_pkg->nd_fw_size = out_obj->buffer.length;
572 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
573 out_obj->buffer.pointer,
574 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
578 * Need to support FW function w/o known size in advance.
579 * Caller can determine required size based upon nd_fw_size.
580 * If we return an error (like elsewhere) then caller wouldn't
581 * be able to rely upon data returned to make calculation.
588 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
589 cmd_name, out_obj->buffer.length);
590 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
591 out_obj->buffer.pointer,
592 min_t(u32, 128, out_obj->buffer.length), true);
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 ssize_t rc = -ENXIO;
1329 nd_desc = dev_get_drvdata(dev);
1331 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1333 mutex_lock(&acpi_desc->init_mutex);
1334 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1335 acpi_desc->scrub_busy
1336 && !acpi_desc->cancel ? "+\n" : "\n");
1337 mutex_unlock(&acpi_desc->init_mutex);
1343 static ssize_t scrub_store(struct device *dev,
1344 struct device_attribute *attr, const char *buf, size_t size)
1346 struct nvdimm_bus_descriptor *nd_desc;
1350 rc = kstrtol(buf, 0, &val);
1357 nd_desc = dev_get_drvdata(dev);
1359 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1361 rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
1368 static DEVICE_ATTR_RW(scrub);
1370 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1372 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1373 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1374 | 1 << ND_CMD_ARS_STATUS;
1376 return (nd_desc->cmd_mask & mask) == mask;
1379 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1381 struct device *dev = container_of(kobj, struct device, kobj);
1382 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1384 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1389 static struct attribute *acpi_nfit_attributes[] = {
1390 &dev_attr_revision.attr,
1391 &dev_attr_scrub.attr,
1392 &dev_attr_hw_error_scrub.attr,
1393 &dev_attr_bus_dsm_mask.attr,
1397 static const struct attribute_group acpi_nfit_attribute_group = {
1399 .attrs = acpi_nfit_attributes,
1400 .is_visible = nfit_visible,
1403 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1404 &nvdimm_bus_attribute_group,
1405 &acpi_nfit_attribute_group,
1409 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1411 struct nvdimm *nvdimm = to_nvdimm(dev);
1412 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1414 return __to_nfit_memdev(nfit_mem);
1417 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1419 struct nvdimm *nvdimm = to_nvdimm(dev);
1420 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1422 return nfit_mem->dcr;
1425 static ssize_t handle_show(struct device *dev,
1426 struct device_attribute *attr, char *buf)
1428 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1430 return sprintf(buf, "%#x\n", memdev->device_handle);
1432 static DEVICE_ATTR_RO(handle);
1434 static ssize_t phys_id_show(struct device *dev,
1435 struct device_attribute *attr, char *buf)
1437 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1439 return sprintf(buf, "%#x\n", memdev->physical_id);
1441 static DEVICE_ATTR_RO(phys_id);
1443 static ssize_t vendor_show(struct device *dev,
1444 struct device_attribute *attr, char *buf)
1446 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1448 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1450 static DEVICE_ATTR_RO(vendor);
1452 static ssize_t rev_id_show(struct device *dev,
1453 struct device_attribute *attr, char *buf)
1455 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1457 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1459 static DEVICE_ATTR_RO(rev_id);
1461 static ssize_t device_show(struct device *dev,
1462 struct device_attribute *attr, char *buf)
1464 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1466 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1468 static DEVICE_ATTR_RO(device);
1470 static ssize_t subsystem_vendor_show(struct device *dev,
1471 struct device_attribute *attr, char *buf)
1473 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1475 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1477 static DEVICE_ATTR_RO(subsystem_vendor);
1479 static ssize_t subsystem_rev_id_show(struct device *dev,
1480 struct device_attribute *attr, char *buf)
1482 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1484 return sprintf(buf, "0x%04x\n",
1485 be16_to_cpu(dcr->subsystem_revision_id));
1487 static DEVICE_ATTR_RO(subsystem_rev_id);
1489 static ssize_t subsystem_device_show(struct device *dev,
1490 struct device_attribute *attr, char *buf)
1492 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1494 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1496 static DEVICE_ATTR_RO(subsystem_device);
1498 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1500 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1503 if (nfit_mem->memdev_pmem)
1505 if (nfit_mem->memdev_bdw)
1510 static ssize_t format_show(struct device *dev,
1511 struct device_attribute *attr, char *buf)
1513 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1515 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1517 static DEVICE_ATTR_RO(format);
1519 static ssize_t format1_show(struct device *dev,
1520 struct device_attribute *attr, char *buf)
1523 ssize_t rc = -ENXIO;
1524 struct nfit_mem *nfit_mem;
1525 struct nfit_memdev *nfit_memdev;
1526 struct acpi_nfit_desc *acpi_desc;
1527 struct nvdimm *nvdimm = to_nvdimm(dev);
1528 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1530 nfit_mem = nvdimm_provider_data(nvdimm);
1531 acpi_desc = nfit_mem->acpi_desc;
1532 handle = to_nfit_memdev(dev)->device_handle;
1534 /* assumes DIMMs have at most 2 published interface codes */
1535 mutex_lock(&acpi_desc->init_mutex);
1536 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1537 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1538 struct nfit_dcr *nfit_dcr;
1540 if (memdev->device_handle != handle)
1543 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1544 if (nfit_dcr->dcr->region_index != memdev->region_index)
1546 if (nfit_dcr->dcr->code == dcr->code)
1548 rc = sprintf(buf, "0x%04x\n",
1549 le16_to_cpu(nfit_dcr->dcr->code));
1555 mutex_unlock(&acpi_desc->init_mutex);
1558 static DEVICE_ATTR_RO(format1);
1560 static ssize_t formats_show(struct device *dev,
1561 struct device_attribute *attr, char *buf)
1563 struct nvdimm *nvdimm = to_nvdimm(dev);
1565 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1567 static DEVICE_ATTR_RO(formats);
1569 static ssize_t serial_show(struct device *dev,
1570 struct device_attribute *attr, char *buf)
1572 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1574 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1576 static DEVICE_ATTR_RO(serial);
1578 static ssize_t family_show(struct device *dev,
1579 struct device_attribute *attr, char *buf)
1581 struct nvdimm *nvdimm = to_nvdimm(dev);
1582 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1584 if (nfit_mem->family < 0)
1586 return sprintf(buf, "%d\n", nfit_mem->family);
1588 static DEVICE_ATTR_RO(family);
1590 static ssize_t dsm_mask_show(struct device *dev,
1591 struct device_attribute *attr, char *buf)
1593 struct nvdimm *nvdimm = to_nvdimm(dev);
1594 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1596 if (nfit_mem->family < 0)
1598 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1600 static DEVICE_ATTR_RO(dsm_mask);
1602 static ssize_t flags_show(struct device *dev,
1603 struct device_attribute *attr, char *buf)
1605 struct nvdimm *nvdimm = to_nvdimm(dev);
1606 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1607 u16 flags = __to_nfit_memdev(nfit_mem)->flags;
1609 if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
1610 flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
1612 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1613 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1614 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1615 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1616 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1617 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1618 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1619 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1621 static DEVICE_ATTR_RO(flags);
1623 static ssize_t id_show(struct device *dev,
1624 struct device_attribute *attr, char *buf)
1626 struct nvdimm *nvdimm = to_nvdimm(dev);
1627 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1629 return sprintf(buf, "%s\n", nfit_mem->id);
1631 static DEVICE_ATTR_RO(id);
1633 static ssize_t dirty_shutdown_show(struct device *dev,
1634 struct device_attribute *attr, char *buf)
1636 struct nvdimm *nvdimm = to_nvdimm(dev);
1637 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1639 return sprintf(buf, "%d\n", nfit_mem->dirty_shutdown);
1641 static DEVICE_ATTR_RO(dirty_shutdown);
1643 static struct attribute *acpi_nfit_dimm_attributes[] = {
1644 &dev_attr_handle.attr,
1645 &dev_attr_phys_id.attr,
1646 &dev_attr_vendor.attr,
1647 &dev_attr_device.attr,
1648 &dev_attr_rev_id.attr,
1649 &dev_attr_subsystem_vendor.attr,
1650 &dev_attr_subsystem_device.attr,
1651 &dev_attr_subsystem_rev_id.attr,
1652 &dev_attr_format.attr,
1653 &dev_attr_formats.attr,
1654 &dev_attr_format1.attr,
1655 &dev_attr_serial.attr,
1656 &dev_attr_flags.attr,
1658 &dev_attr_family.attr,
1659 &dev_attr_dsm_mask.attr,
1660 &dev_attr_dirty_shutdown.attr,
1664 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1665 struct attribute *a, int n)
1667 struct device *dev = container_of(kobj, struct device, kobj);
1668 struct nvdimm *nvdimm = to_nvdimm(dev);
1669 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1671 if (!to_nfit_dcr(dev)) {
1672 /* Without a dcr only the memdev attributes can be surfaced */
1673 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1674 || a == &dev_attr_flags.attr
1675 || a == &dev_attr_family.attr
1676 || a == &dev_attr_dsm_mask.attr)
1681 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1684 if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
1685 && a == &dev_attr_dirty_shutdown.attr)
1691 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1693 .attrs = acpi_nfit_dimm_attributes,
1694 .is_visible = acpi_nfit_dimm_attr_visible,
1697 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1698 &nvdimm_attribute_group,
1699 &nd_device_attribute_group,
1700 &acpi_nfit_dimm_attribute_group,
1704 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1707 struct nfit_mem *nfit_mem;
1709 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1710 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1711 return nfit_mem->nvdimm;
1716 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1718 struct nfit_mem *nfit_mem;
1719 struct acpi_nfit_desc *acpi_desc;
1721 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
1724 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1725 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1730 acpi_desc = dev_get_drvdata(dev->parent);
1735 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1738 nfit_mem = dev_get_drvdata(dev);
1739 if (nfit_mem && nfit_mem->flags_attr)
1740 sysfs_notify_dirent(nfit_mem->flags_attr);
1742 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1744 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1746 struct acpi_device *adev = data;
1747 struct device *dev = &adev->dev;
1749 device_lock(dev->parent);
1750 __acpi_nvdimm_notify(dev, event);
1751 device_unlock(dev->parent);
1754 static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1759 status = acpi_get_handle(adev->handle, method, &handle);
1761 if (ACPI_SUCCESS(status))
1766 __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
1768 struct device *dev = &nfit_mem->adev->dev;
1769 struct nd_intel_smart smart = { 0 };
1770 union acpi_object in_buf = {
1771 .buffer.type = ACPI_TYPE_BUFFER,
1774 union acpi_object in_obj = {
1775 .package.type = ACPI_TYPE_PACKAGE,
1777 .package.elements = &in_buf,
1779 const u8 func = ND_INTEL_SMART;
1780 const guid_t *guid = to_nfit_uuid(nfit_mem->family);
1781 u8 revid = nfit_dsm_revid(nfit_mem->family, func);
1782 struct acpi_device *adev = nfit_mem->adev;
1783 acpi_handle handle = adev->handle;
1784 union acpi_object *out_obj;
1786 if ((nfit_mem->dsm_mask & (1 << func)) == 0)
1789 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
1790 if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER
1791 || out_obj->buffer.length < sizeof(smart)) {
1792 dev_dbg(dev->parent, "%s: failed to retrieve initial health\n",
1797 memcpy(&smart, out_obj->buffer.pointer, sizeof(smart));
1800 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
1801 if (smart.shutdown_state)
1802 set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
1805 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
1806 set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
1807 nfit_mem->dirty_shutdown = smart.shutdown_count;
1811 static void populate_shutdown_status(struct nfit_mem *nfit_mem)
1814 * For DIMMs that provide a dynamic facility to retrieve a
1815 * dirty-shutdown status and/or a dirty-shutdown count, cache
1816 * these values in nfit_mem.
1818 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1819 nfit_intel_shutdown_status(nfit_mem);
1822 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1823 struct nfit_mem *nfit_mem, u32 device_handle)
1825 struct acpi_device *adev, *adev_dimm;
1826 struct device *dev = acpi_desc->dev;
1827 unsigned long dsm_mask, label_mask;
1831 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1833 /* nfit test assumes 1:1 relationship between commands and dsms */
1834 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1835 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1837 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1838 sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
1839 be16_to_cpu(dcr->vendor_id),
1840 dcr->manufacturing_location,
1841 be16_to_cpu(dcr->manufacturing_date),
1842 be32_to_cpu(dcr->serial_number));
1844 sprintf(nfit_mem->id, "%04x-%08x",
1845 be16_to_cpu(dcr->vendor_id),
1846 be32_to_cpu(dcr->serial_number));
1848 adev = to_acpi_dev(acpi_desc);
1850 /* unit test case */
1851 populate_shutdown_status(nfit_mem);
1855 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1856 nfit_mem->adev = adev_dimm;
1858 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1860 return force_enable_dimms ? 0 : -ENODEV;
1863 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1864 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1865 dev_err(dev, "%s: notification registration failed\n",
1866 dev_name(&adev_dimm->dev));
1870 * Record nfit_mem for the notification path to track back to
1871 * the nfit sysfs attributes for this dimm device object.
1873 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1876 * There are 4 "legacy" NVDIMM command sets
1877 * (NVDIMM_FAMILY_{INTEL,MSFT,HPE1,HPE2}) that were created before
1878 * an EFI working group was established to constrain this
1879 * proliferation. The nfit driver probes for the supported command
1880 * set by GUID. Note, if you're a platform developer looking to add
1881 * a new command set to this probe, consider using an existing set,
1882 * or otherwise seek approval to publish the command set at
1883 * http://www.uefi.org/RFIC_LIST.
1885 * Note, that checking for function0 (bit0) tells us if any commands
1886 * are reachable through this GUID.
1888 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
1889 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1890 if (family < 0 || i == default_dsm_family)
1893 /* limit the supported commands to those that are publicly documented */
1894 nfit_mem->family = family;
1895 if (override_dsm_mask && !disable_vendor_specific)
1896 dsm_mask = override_dsm_mask;
1897 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1898 dsm_mask = NVDIMM_INTEL_CMDMASK;
1899 if (disable_vendor_specific)
1900 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1901 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1902 dsm_mask = 0x1c3c76;
1903 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1905 if (disable_vendor_specific)
1906 dsm_mask &= ~(1 << 8);
1907 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1908 dsm_mask = 0xffffffff;
1909 } else if (nfit_mem->family == NVDIMM_FAMILY_HYPERV) {
1912 dev_dbg(dev, "unknown dimm command family\n");
1913 nfit_mem->family = -1;
1914 /* DSMs are optional, continue loading the driver... */
1919 * Function 0 is the command interrogation function, don't
1920 * export it to potential userspace use, and enable it to be
1921 * used as an error value in acpi_nfit_ctl().
1925 guid = to_nfit_uuid(nfit_mem->family);
1926 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1927 if (acpi_check_dsm(adev_dimm->handle, guid,
1928 nfit_dsm_revid(nfit_mem->family, i),
1930 set_bit(i, &nfit_mem->dsm_mask);
1933 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1934 * due to their better semantics handling locked capacity.
1936 label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
1937 | 1 << ND_CMD_SET_CONFIG_DATA;
1938 if (family == NVDIMM_FAMILY_INTEL
1939 && (dsm_mask & label_mask) == label_mask)
1940 /* skip _LS{I,R,W} enabling */;
1942 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1943 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
1944 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
1945 set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1948 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
1949 && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
1950 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
1951 set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
1955 * Quirk read-only label configurations to preserve
1956 * access to label-less namespaces by default.
1958 if (!test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
1960 dev_dbg(dev, "%s: No _LSW, disable labels\n",
1961 dev_name(&adev_dimm->dev));
1962 clear_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1964 dev_dbg(dev, "%s: Force enable labels\n",
1965 dev_name(&adev_dimm->dev));
1968 populate_shutdown_status(nfit_mem);
1973 static void shutdown_dimm_notify(void *data)
1975 struct acpi_nfit_desc *acpi_desc = data;
1976 struct nfit_mem *nfit_mem;
1978 mutex_lock(&acpi_desc->init_mutex);
1980 * Clear out the nfit_mem->flags_attr and shut down dimm event
1983 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1984 struct acpi_device *adev_dimm = nfit_mem->adev;
1986 if (nfit_mem->flags_attr) {
1987 sysfs_put(nfit_mem->flags_attr);
1988 nfit_mem->flags_attr = NULL;
1991 acpi_remove_notify_handler(adev_dimm->handle,
1992 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1993 dev_set_drvdata(&adev_dimm->dev, NULL);
1996 mutex_unlock(&acpi_desc->init_mutex);
1999 static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
2002 case NVDIMM_FAMILY_INTEL:
2003 return intel_security_ops;
2009 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
2011 struct nfit_mem *nfit_mem;
2012 int dimm_count = 0, rc;
2013 struct nvdimm *nvdimm;
2015 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2016 struct acpi_nfit_flush_address *flush;
2017 unsigned long flags = 0, cmd_mask;
2018 struct nfit_memdev *nfit_memdev;
2022 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2023 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
2029 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
2030 set_bit(NDD_ALIASING, &flags);
2032 /* collate flags across all memdevs for this dimm */
2033 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2034 struct acpi_nfit_memory_map *dimm_memdev;
2036 dimm_memdev = __to_nfit_memdev(nfit_mem);
2037 if (dimm_memdev->device_handle
2038 != nfit_memdev->memdev->device_handle)
2040 dimm_memdev->flags |= nfit_memdev->memdev->flags;
2043 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
2044 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
2045 set_bit(NDD_UNARMED, &flags);
2047 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
2052 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
2053 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
2054 * userspace interface.
2056 cmd_mask = 1UL << ND_CMD_CALL;
2057 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
2059 * These commands have a 1:1 correspondence
2060 * between DSM payload and libnvdimm ioctl
2063 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
2066 /* Quirk to ignore LOCAL for labels on HYPERV DIMMs */
2067 if (nfit_mem->family == NVDIMM_FAMILY_HYPERV)
2068 set_bit(NDD_NOBLK, &flags);
2070 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
2071 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
2072 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
2074 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
2075 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
2077 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
2079 nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
2080 acpi_nfit_dimm_attribute_groups,
2081 flags, cmd_mask, flush ? flush->hint_count : 0,
2082 nfit_mem->flush_wpq, &nfit_mem->id[0],
2083 acpi_nfit_get_security_ops(nfit_mem->family));
2087 nfit_mem->nvdimm = nvdimm;
2090 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
2093 dev_err(acpi_desc->dev, "Error found in NVDIMM %s flags:%s%s%s%s%s\n",
2094 nvdimm_name(nvdimm),
2095 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
2096 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
2097 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
2098 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
2099 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
2103 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
2108 * Now that dimms are successfully registered, and async registration
2109 * is flushed, attempt to enable event notification.
2111 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2112 struct kernfs_node *nfit_kernfs;
2114 nvdimm = nfit_mem->nvdimm;
2118 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
2120 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
2122 sysfs_put(nfit_kernfs);
2123 if (!nfit_mem->flags_attr)
2124 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
2125 nvdimm_name(nvdimm));
2128 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
2133 * These constants are private because there are no kernel consumers of
2136 enum nfit_aux_cmds {
2137 NFIT_CMD_TRANSLATE_SPA = 5,
2138 NFIT_CMD_ARS_INJECT_SET = 7,
2139 NFIT_CMD_ARS_INJECT_CLEAR = 8,
2140 NFIT_CMD_ARS_INJECT_GET = 9,
2143 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
2145 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2146 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
2147 struct acpi_device *adev;
2148 unsigned long dsm_mask;
2151 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
2152 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
2153 adev = to_acpi_dev(acpi_desc);
2157 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
2158 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2159 set_bit(i, &nd_desc->cmd_mask);
2160 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
2163 (1 << ND_CMD_ARS_CAP) |
2164 (1 << ND_CMD_ARS_START) |
2165 (1 << ND_CMD_ARS_STATUS) |
2166 (1 << ND_CMD_CLEAR_ERROR) |
2167 (1 << NFIT_CMD_TRANSLATE_SPA) |
2168 (1 << NFIT_CMD_ARS_INJECT_SET) |
2169 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
2170 (1 << NFIT_CMD_ARS_INJECT_GET);
2171 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
2172 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2173 set_bit(i, &nd_desc->bus_dsm_mask);
2176 static ssize_t range_index_show(struct device *dev,
2177 struct device_attribute *attr, char *buf)
2179 struct nd_region *nd_region = to_nd_region(dev);
2180 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
2182 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
2184 static DEVICE_ATTR_RO(range_index);
2186 static struct attribute *acpi_nfit_region_attributes[] = {
2187 &dev_attr_range_index.attr,
2191 static const struct attribute_group acpi_nfit_region_attribute_group = {
2193 .attrs = acpi_nfit_region_attributes,
2196 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
2197 &nd_region_attribute_group,
2198 &nd_mapping_attribute_group,
2199 &nd_device_attribute_group,
2200 &nd_numa_attribute_group,
2201 &acpi_nfit_region_attribute_group,
2205 /* enough info to uniquely specify an interleave set */
2206 struct nfit_set_info {
2207 struct nfit_set_info_map {
2214 struct nfit_set_info2 {
2215 struct nfit_set_info_map2 {
2219 u16 manufacturing_date;
2220 u8 manufacturing_location;
2225 static size_t sizeof_nfit_set_info(int num_mappings)
2227 return sizeof(struct nfit_set_info)
2228 + num_mappings * sizeof(struct nfit_set_info_map);
2231 static size_t sizeof_nfit_set_info2(int num_mappings)
2233 return sizeof(struct nfit_set_info2)
2234 + num_mappings * sizeof(struct nfit_set_info_map2);
2237 static int cmp_map_compat(const void *m0, const void *m1)
2239 const struct nfit_set_info_map *map0 = m0;
2240 const struct nfit_set_info_map *map1 = m1;
2242 return memcmp(&map0->region_offset, &map1->region_offset,
2246 static int cmp_map(const void *m0, const void *m1)
2248 const struct nfit_set_info_map *map0 = m0;
2249 const struct nfit_set_info_map *map1 = m1;
2251 if (map0->region_offset < map1->region_offset)
2253 else if (map0->region_offset > map1->region_offset)
2258 static int cmp_map2(const void *m0, const void *m1)
2260 const struct nfit_set_info_map2 *map0 = m0;
2261 const struct nfit_set_info_map2 *map1 = m1;
2263 if (map0->region_offset < map1->region_offset)
2265 else if (map0->region_offset > map1->region_offset)
2270 /* Retrieve the nth entry referencing this spa */
2271 static struct acpi_nfit_memory_map *memdev_from_spa(
2272 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2274 struct nfit_memdev *nfit_memdev;
2276 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2277 if (nfit_memdev->memdev->range_index == range_index)
2279 return nfit_memdev->memdev;
2283 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2284 struct nd_region_desc *ndr_desc,
2285 struct acpi_nfit_system_address *spa)
2287 struct device *dev = acpi_desc->dev;
2288 struct nd_interleave_set *nd_set;
2289 u16 nr = ndr_desc->num_mappings;
2290 struct nfit_set_info2 *info2;
2291 struct nfit_set_info *info;
2294 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2297 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2299 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2303 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2307 for (i = 0; i < nr; i++) {
2308 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
2309 struct nfit_set_info_map *map = &info->mapping[i];
2310 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2311 struct nvdimm *nvdimm = mapping->nvdimm;
2312 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2313 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2314 spa->range_index, i);
2315 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2317 if (!memdev || !nfit_mem->dcr) {
2318 dev_err(dev, "%s: failed to find DCR\n", __func__);
2322 map->region_offset = memdev->region_offset;
2323 map->serial_number = dcr->serial_number;
2325 map2->region_offset = memdev->region_offset;
2326 map2->serial_number = dcr->serial_number;
2327 map2->vendor_id = dcr->vendor_id;
2328 map2->manufacturing_date = dcr->manufacturing_date;
2329 map2->manufacturing_location = dcr->manufacturing_location;
2332 /* v1.1 namespaces */
2333 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2335 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2337 /* v1.2 namespaces */
2338 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2340 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2342 /* support v1.1 namespaces created with the wrong sort order */
2343 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2344 cmp_map_compat, NULL);
2345 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2347 /* record the result of the sort for the mapping position */
2348 for (i = 0; i < nr; i++) {
2349 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2352 for (j = 0; j < nr; j++) {
2353 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2354 struct nvdimm *nvdimm = mapping->nvdimm;
2355 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2356 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2358 if (map2->serial_number == dcr->serial_number &&
2359 map2->vendor_id == dcr->vendor_id &&
2360 map2->manufacturing_date == dcr->manufacturing_date &&
2361 map2->manufacturing_location
2362 == dcr->manufacturing_location) {
2363 mapping->position = i;
2369 ndr_desc->nd_set = nd_set;
2370 devm_kfree(dev, info);
2371 devm_kfree(dev, info2);
2376 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2378 struct acpi_nfit_interleave *idt = mmio->idt;
2379 u32 sub_line_offset, line_index, line_offset;
2380 u64 line_no, table_skip_count, table_offset;
2382 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2383 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2384 line_offset = idt->line_offset[line_index]
2386 table_offset = table_skip_count * mmio->table_size;
2388 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2391 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
2393 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2394 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
2395 const u32 STATUS_MASK = 0x80000037;
2397 if (mmio->num_lines)
2398 offset = to_interleave_offset(offset, mmio);
2400 return readl(mmio->addr.base + offset) & STATUS_MASK;
2403 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2404 resource_size_t dpa, unsigned int len, unsigned int write)
2407 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2410 BCW_OFFSET_MASK = (1ULL << 48)-1,
2412 BCW_LEN_MASK = (1ULL << 8) - 1,
2416 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2417 len = len >> L1_CACHE_SHIFT;
2418 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2419 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2421 offset = nfit_blk->cmd_offset + mmio->size * bw;
2422 if (mmio->num_lines)
2423 offset = to_interleave_offset(offset, mmio);
2425 writeq(cmd, mmio->addr.base + offset);
2426 nvdimm_flush(nfit_blk->nd_region);
2428 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
2429 readq(mmio->addr.base + offset);
2432 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2433 resource_size_t dpa, void *iobuf, size_t len, int rw,
2436 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2437 unsigned int copied = 0;
2441 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2442 + lane * mmio->size;
2443 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2448 if (mmio->num_lines) {
2451 offset = to_interleave_offset(base_offset + copied,
2453 div_u64_rem(offset, mmio->line_size, &line_offset);
2454 c = min_t(size_t, len, mmio->line_size - line_offset);
2456 offset = base_offset + nfit_blk->bdw_offset;
2461 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2463 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2464 arch_invalidate_pmem((void __force *)
2465 mmio->addr.aperture + offset, c);
2467 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2475 nvdimm_flush(nfit_blk->nd_region);
2477 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2481 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2482 resource_size_t dpa, void *iobuf, u64 len, int rw)
2484 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2485 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2486 struct nd_region *nd_region = nfit_blk->nd_region;
2487 unsigned int lane, copied = 0;
2490 lane = nd_region_acquire_lane(nd_region);
2492 u64 c = min(len, mmio->size);
2494 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2495 iobuf + copied, c, rw, lane);
2502 nd_region_release_lane(nd_region, lane);
2507 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2508 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2511 mmio->num_lines = idt->line_count;
2512 mmio->line_size = idt->line_size;
2513 if (interleave_ways == 0)
2515 mmio->table_size = mmio->num_lines * interleave_ways
2522 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2523 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2525 struct nd_cmd_dimm_flags flags;
2528 memset(&flags, 0, sizeof(flags));
2529 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2530 sizeof(flags), NULL);
2532 if (rc >= 0 && flags.status == 0)
2533 nfit_blk->dimm_flags = flags.flags;
2534 else if (rc == -ENOTTY) {
2535 /* fall back to a conservative default */
2536 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2544 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2547 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2548 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2549 struct nfit_blk_mmio *mmio;
2550 struct nfit_blk *nfit_blk;
2551 struct nfit_mem *nfit_mem;
2552 struct nvdimm *nvdimm;
2555 nvdimm = nd_blk_region_to_dimm(ndbr);
2556 nfit_mem = nvdimm_provider_data(nvdimm);
2557 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2558 dev_dbg(dev, "missing%s%s%s\n",
2559 nfit_mem ? "" : " nfit_mem",
2560 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2561 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2565 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2568 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2569 nfit_blk->nd_region = to_nd_region(dev);
2571 /* map block aperture memory */
2572 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2573 mmio = &nfit_blk->mmio[BDW];
2574 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2575 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2576 if (!mmio->addr.base) {
2577 dev_dbg(dev, "%s failed to map bdw\n",
2578 nvdimm_name(nvdimm));
2581 mmio->size = nfit_mem->bdw->size;
2582 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2583 mmio->idt = nfit_mem->idt_bdw;
2584 mmio->spa = nfit_mem->spa_bdw;
2585 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2586 nfit_mem->memdev_bdw->interleave_ways);
2588 dev_dbg(dev, "%s failed to init bdw interleave\n",
2589 nvdimm_name(nvdimm));
2593 /* map block control memory */
2594 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2595 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2596 mmio = &nfit_blk->mmio[DCR];
2597 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2598 nfit_mem->spa_dcr->length);
2599 if (!mmio->addr.base) {
2600 dev_dbg(dev, "%s failed to map dcr\n",
2601 nvdimm_name(nvdimm));
2604 mmio->size = nfit_mem->dcr->window_size;
2605 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2606 mmio->idt = nfit_mem->idt_dcr;
2607 mmio->spa = nfit_mem->spa_dcr;
2608 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2609 nfit_mem->memdev_dcr->interleave_ways);
2611 dev_dbg(dev, "%s failed to init dcr interleave\n",
2612 nvdimm_name(nvdimm));
2616 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2618 dev_dbg(dev, "%s failed get DIMM flags\n",
2619 nvdimm_name(nvdimm));
2623 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2624 dev_warn(dev, "unable to guarantee persistence of writes\n");
2626 if (mmio->line_size == 0)
2629 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2630 + 8 > mmio->line_size) {
2631 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2633 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2634 + 8 > mmio->line_size) {
2635 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2642 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2643 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2645 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2646 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2649 cmd->address = spa->address;
2650 cmd->length = spa->length;
2651 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2652 sizeof(*cmd), &cmd_rc);
2658 static int ars_start(struct acpi_nfit_desc *acpi_desc,
2659 struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
2663 struct nd_cmd_ars_start ars_start;
2664 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2665 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2667 memset(&ars_start, 0, sizeof(ars_start));
2668 ars_start.address = spa->address;
2669 ars_start.length = spa->length;
2670 if (req_type == ARS_REQ_SHORT)
2671 ars_start.flags = ND_ARS_RETURN_PREV_DATA;
2672 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2673 ars_start.type = ND_ARS_PERSISTENT;
2674 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2675 ars_start.type = ND_ARS_VOLATILE;
2679 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2680 sizeof(ars_start), &cmd_rc);
2687 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2690 struct nd_cmd_ars_start ars_start;
2691 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2692 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2694 memset(&ars_start, 0, sizeof(ars_start));
2695 ars_start.address = ars_status->restart_address;
2696 ars_start.length = ars_status->restart_length;
2697 ars_start.type = ars_status->type;
2698 ars_start.flags = acpi_desc->ars_start_flags;
2699 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2700 sizeof(ars_start), &cmd_rc);
2706 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2708 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2709 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2712 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2713 acpi_desc->max_ars, &cmd_rc);
2719 static void ars_complete(struct acpi_nfit_desc *acpi_desc,
2720 struct nfit_spa *nfit_spa)
2722 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2723 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2724 struct nd_region *nd_region = nfit_spa->nd_region;
2727 lockdep_assert_held(&acpi_desc->init_mutex);
2729 * Only advance the ARS state for ARS runs initiated by the
2730 * kernel, ignore ARS results from BIOS initiated runs for scrub
2731 * completion tracking.
2733 if (acpi_desc->scrub_spa != nfit_spa)
2736 if ((ars_status->address >= spa->address && ars_status->address
2737 < spa->address + spa->length)
2738 || (ars_status->address < spa->address)) {
2740 * Assume that if a scrub starts at an offset from the
2741 * start of nfit_spa that we are in the continuation
2744 * Otherwise, if the scrub covers the spa range, mark
2745 * any pending request complete.
2747 if (ars_status->address + ars_status->length
2748 >= spa->address + spa->length)
2755 acpi_desc->scrub_spa = NULL;
2757 dev = nd_region_dev(nd_region);
2758 nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
2760 dev = acpi_desc->dev;
2761 dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
2764 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
2766 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2767 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2772 * First record starts at 44 byte offset from the start of the
2775 if (ars_status->out_length < 44)
2777 for (i = 0; i < ars_status->num_records; i++) {
2778 /* only process full records */
2779 if (ars_status->out_length
2780 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2782 rc = nvdimm_bus_add_badrange(nvdimm_bus,
2783 ars_status->records[i].err_address,
2784 ars_status->records[i].length);
2788 if (i < ars_status->num_records)
2789 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2794 static void acpi_nfit_remove_resource(void *data)
2796 struct resource *res = data;
2798 remove_resource(res);
2801 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2802 struct nd_region_desc *ndr_desc)
2804 struct resource *res, *nd_res = ndr_desc->res;
2807 /* No operation if the region is already registered as PMEM */
2808 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2809 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2810 if (is_pmem == REGION_INTERSECTS)
2813 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2817 res->name = "Persistent Memory";
2818 res->start = nd_res->start;
2819 res->end = nd_res->end;
2820 res->flags = IORESOURCE_MEM;
2821 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2823 ret = insert_resource(&iomem_resource, res);
2827 ret = devm_add_action_or_reset(acpi_desc->dev,
2828 acpi_nfit_remove_resource,
2836 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2837 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2838 struct acpi_nfit_memory_map *memdev,
2839 struct nfit_spa *nfit_spa)
2841 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2842 memdev->device_handle);
2843 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2844 struct nd_blk_region_desc *ndbr_desc;
2845 struct nfit_mem *nfit_mem;
2849 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2850 spa->range_index, memdev->device_handle);
2854 mapping->nvdimm = nvdimm;
2855 switch (nfit_spa_type(spa)) {
2857 case NFIT_SPA_VOLATILE:
2858 mapping->start = memdev->address;
2859 mapping->size = memdev->region_size;
2862 nfit_mem = nvdimm_provider_data(nvdimm);
2863 if (!nfit_mem || !nfit_mem->bdw) {
2864 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2865 spa->range_index, nvdimm_name(nvdimm));
2869 mapping->size = nfit_mem->bdw->capacity;
2870 mapping->start = nfit_mem->bdw->start_address;
2871 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2872 ndr_desc->mapping = mapping;
2873 ndr_desc->num_mappings = 1;
2874 ndbr_desc = to_blk_region_desc(ndr_desc);
2875 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2876 ndbr_desc->do_io = acpi_desc->blk_do_io;
2877 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2880 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2882 if (!nfit_spa->nd_region)
2890 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2892 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2893 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2894 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2895 nfit_spa_type(spa) == NFIT_SPA_PCD);
2898 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2900 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2901 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2902 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2905 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2906 struct nfit_spa *nfit_spa)
2908 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2909 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2910 struct nd_blk_region_desc ndbr_desc;
2911 struct nd_region_desc *ndr_desc;
2912 struct nfit_memdev *nfit_memdev;
2913 struct nvdimm_bus *nvdimm_bus;
2914 struct resource res;
2917 if (nfit_spa->nd_region)
2920 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2921 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
2925 memset(&res, 0, sizeof(res));
2926 memset(&mappings, 0, sizeof(mappings));
2927 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2928 res.start = spa->address;
2929 res.end = res.start + spa->length - 1;
2930 ndr_desc = &ndbr_desc.ndr_desc;
2931 ndr_desc->res = &res;
2932 ndr_desc->provider_data = nfit_spa;
2933 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2934 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2935 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2936 spa->proximity_domain);
2938 ndr_desc->numa_node = NUMA_NO_NODE;
2941 * Persistence domain bits are hierarchical, if
2942 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
2943 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
2945 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2946 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2947 else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2948 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2950 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2951 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2952 struct nd_mapping_desc *mapping;
2954 if (memdev->range_index != spa->range_index)
2956 if (count >= ND_MAX_MAPPINGS) {
2957 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2958 spa->range_index, ND_MAX_MAPPINGS);
2961 mapping = &mappings[count++];
2962 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2968 ndr_desc->mapping = mappings;
2969 ndr_desc->num_mappings = count;
2970 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2974 nvdimm_bus = acpi_desc->nvdimm_bus;
2975 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2976 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2978 dev_warn(acpi_desc->dev,
2979 "failed to insert pmem resource to iomem: %d\n",
2984 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2986 if (!nfit_spa->nd_region)
2988 } else if (nfit_spa_is_volatile(spa)) {
2989 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2991 if (!nfit_spa->nd_region)
2993 } else if (nfit_spa_is_virtual(spa)) {
2994 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2996 if (!nfit_spa->nd_region)
3002 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
3003 nfit_spa->spa->range_index);
3007 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
3009 struct device *dev = acpi_desc->dev;
3010 struct nd_cmd_ars_status *ars_status;
3012 if (acpi_desc->ars_status) {
3013 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3017 ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
3020 acpi_desc->ars_status = ars_status;
3024 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
3028 if (ars_status_alloc(acpi_desc))
3031 rc = ars_get_status(acpi_desc);
3033 if (rc < 0 && rc != -ENOSPC)
3036 if (ars_status_process_records(acpi_desc))
3037 dev_err(acpi_desc->dev, "Failed to process ARS records\n");
3042 static int ars_register(struct acpi_nfit_desc *acpi_desc,
3043 struct nfit_spa *nfit_spa)
3047 if (no_init_ars || test_bit(ARS_FAILED, &nfit_spa->ars_state))
3048 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3050 set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3051 set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
3053 switch (acpi_nfit_query_poison(acpi_desc)) {
3056 rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
3057 /* shouldn't happen, try again later */
3061 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3064 clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3065 rc = acpi_nfit_query_poison(acpi_desc);
3068 acpi_desc->scrub_spa = nfit_spa;
3069 ars_complete(acpi_desc, nfit_spa);
3071 * If ars_complete() says we didn't complete the
3072 * short scrub, we'll try again with a long
3075 acpi_desc->scrub_spa = NULL;
3081 * BIOS was using ARS, wait for it to complete (or
3082 * resources to become available) and then perform our
3087 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3091 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3094 static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
3096 struct nfit_spa *nfit_spa;
3098 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3099 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3101 ars_complete(acpi_desc, nfit_spa);
3105 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
3108 unsigned int tmo = acpi_desc->scrub_tmo;
3109 struct device *dev = acpi_desc->dev;
3110 struct nfit_spa *nfit_spa;
3112 lockdep_assert_held(&acpi_desc->init_mutex);
3114 if (acpi_desc->cancel)
3117 if (query_rc == -EBUSY) {
3118 dev_dbg(dev, "ARS: ARS busy\n");
3119 return min(30U * 60U, tmo * 2);
3121 if (query_rc == -ENOSPC) {
3122 dev_dbg(dev, "ARS: ARS continue\n");
3123 ars_continue(acpi_desc);
3126 if (query_rc && query_rc != -EAGAIN) {
3127 unsigned long long addr, end;
3129 addr = acpi_desc->ars_status->address;
3130 end = addr + acpi_desc->ars_status->length;
3131 dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
3135 ars_complete_all(acpi_desc);
3136 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3137 enum nfit_ars_state req_type;
3140 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3143 /* prefer short ARS requests first */
3144 if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
3145 req_type = ARS_REQ_SHORT;
3146 else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
3147 req_type = ARS_REQ_LONG;
3150 rc = ars_start(acpi_desc, nfit_spa, req_type);
3152 dev = nd_region_dev(nfit_spa->nd_region);
3153 dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
3154 nfit_spa->spa->range_index,
3155 req_type == ARS_REQ_SHORT ? "short" : "long",
3158 * Hmm, we raced someone else starting ARS? Try again in
3164 dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
3165 "scrub start while range %d active\n",
3166 acpi_desc->scrub_spa->spa->range_index);
3167 clear_bit(req_type, &nfit_spa->ars_state);
3168 acpi_desc->scrub_spa = nfit_spa;
3170 * Consider this spa last for future scrub
3173 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
3177 dev_err(dev, "ARS: range %d ARS failed (%d)\n",
3178 nfit_spa->spa->range_index, rc);
3179 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3184 static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
3186 lockdep_assert_held(&acpi_desc->init_mutex);
3188 acpi_desc->scrub_busy = 1;
3189 /* note this should only be set from within the workqueue */
3191 acpi_desc->scrub_tmo = tmo;
3192 queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
3195 static void sched_ars(struct acpi_nfit_desc *acpi_desc)
3197 __sched_ars(acpi_desc, 0);
3200 static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
3202 lockdep_assert_held(&acpi_desc->init_mutex);
3204 acpi_desc->scrub_busy = 0;
3205 acpi_desc->scrub_count++;
3206 if (acpi_desc->scrub_count_state)
3207 sysfs_notify_dirent(acpi_desc->scrub_count_state);
3210 static void acpi_nfit_scrub(struct work_struct *work)
3212 struct acpi_nfit_desc *acpi_desc;
3216 acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
3217 mutex_lock(&acpi_desc->init_mutex);
3218 query_rc = acpi_nfit_query_poison(acpi_desc);
3219 tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
3221 __sched_ars(acpi_desc, tmo);
3223 notify_ars_done(acpi_desc);
3224 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3225 mutex_unlock(&acpi_desc->init_mutex);
3228 static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
3229 struct nfit_spa *nfit_spa)
3231 int type = nfit_spa_type(nfit_spa->spa);
3232 struct nd_cmd_ars_cap ars_cap;
3235 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3236 memset(&ars_cap, 0, sizeof(ars_cap));
3237 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
3240 /* check that the supported scrub types match the spa type */
3241 if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
3242 & ND_ARS_VOLATILE) == 0)
3244 if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
3245 & ND_ARS_PERSISTENT) == 0)
3248 nfit_spa->max_ars = ars_cap.max_ars_out;
3249 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
3250 acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
3251 clear_bit(ARS_FAILED, &nfit_spa->ars_state);
3254 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3256 struct nfit_spa *nfit_spa;
3259 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3260 switch (nfit_spa_type(nfit_spa->spa)) {
3261 case NFIT_SPA_VOLATILE:
3263 acpi_nfit_init_ars(acpi_desc, nfit_spa);
3268 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3269 switch (nfit_spa_type(nfit_spa->spa)) {
3270 case NFIT_SPA_VOLATILE:
3272 /* register regions and kick off initial ARS run */
3273 rc = ars_register(acpi_desc, nfit_spa);
3278 /* nothing to register */
3281 case NFIT_SPA_VDISK:
3283 case NFIT_SPA_PDISK:
3285 /* register known regions that don't support ARS */
3286 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3291 /* don't register unknown regions */
3295 sched_ars(acpi_desc);
3299 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3300 struct nfit_table_prev *prev)
3302 struct device *dev = acpi_desc->dev;
3304 if (!list_empty(&prev->spas) ||
3305 !list_empty(&prev->memdevs) ||
3306 !list_empty(&prev->dcrs) ||
3307 !list_empty(&prev->bdws) ||
3308 !list_empty(&prev->idts) ||
3309 !list_empty(&prev->flushes)) {
3310 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3316 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3318 struct device *dev = acpi_desc->dev;
3319 struct kernfs_node *nfit;
3320 struct device *bus_dev;
3322 if (!ars_supported(acpi_desc->nvdimm_bus))
3325 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3326 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3328 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3331 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3333 if (!acpi_desc->scrub_count_state) {
3334 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3341 static void acpi_nfit_unregister(void *data)
3343 struct acpi_nfit_desc *acpi_desc = data;
3345 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
3348 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
3350 struct device *dev = acpi_desc->dev;
3351 struct nfit_table_prev prev;
3355 if (!acpi_desc->nvdimm_bus) {
3356 acpi_nfit_init_dsms(acpi_desc);
3358 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3359 &acpi_desc->nd_desc);
3360 if (!acpi_desc->nvdimm_bus)
3363 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
3368 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3372 /* register this acpi_desc for mce notifications */
3373 mutex_lock(&acpi_desc_lock);
3374 list_add_tail(&acpi_desc->list, &acpi_descs);
3375 mutex_unlock(&acpi_desc_lock);
3378 mutex_lock(&acpi_desc->init_mutex);
3380 INIT_LIST_HEAD(&prev.spas);
3381 INIT_LIST_HEAD(&prev.memdevs);
3382 INIT_LIST_HEAD(&prev.dcrs);
3383 INIT_LIST_HEAD(&prev.bdws);
3384 INIT_LIST_HEAD(&prev.idts);
3385 INIT_LIST_HEAD(&prev.flushes);
3387 list_cut_position(&prev.spas, &acpi_desc->spas,
3388 acpi_desc->spas.prev);
3389 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3390 acpi_desc->memdevs.prev);
3391 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3392 acpi_desc->dcrs.prev);
3393 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3394 acpi_desc->bdws.prev);
3395 list_cut_position(&prev.idts, &acpi_desc->idts,
3396 acpi_desc->idts.prev);
3397 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3398 acpi_desc->flushes.prev);
3401 while (!IS_ERR_OR_NULL(data))
3402 data = add_table(acpi_desc, &prev, data, end);
3405 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
3410 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3414 rc = nfit_mem_init(acpi_desc);
3418 rc = acpi_nfit_register_dimms(acpi_desc);
3422 rc = acpi_nfit_register_regions(acpi_desc);
3425 mutex_unlock(&acpi_desc->init_mutex);
3428 EXPORT_SYMBOL_GPL(acpi_nfit_init);
3430 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3432 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3433 struct device *dev = acpi_desc->dev;
3435 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3439 /* Bounce the init_mutex to complete initial registration */
3440 mutex_lock(&acpi_desc->init_mutex);
3441 mutex_unlock(&acpi_desc->init_mutex);
3446 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3447 struct nvdimm *nvdimm, unsigned int cmd)
3449 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3453 if (cmd != ND_CMD_ARS_START)
3457 * The kernel and userspace may race to initiate a scrub, but
3458 * the scrub thread is prepared to lose that initial race. It
3459 * just needs guarantees that any ARS it initiates are not
3460 * interrupted by any intervening start requests from userspace.
3462 if (work_busy(&acpi_desc->dwork.work))
3468 /* prevent security commands from being issued via ioctl */
3469 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3470 struct nvdimm *nvdimm, unsigned int cmd, void *buf)
3472 struct nd_cmd_pkg *call_pkg = buf;
3475 if (nvdimm && cmd == ND_CMD_CALL &&
3476 call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
3477 func = call_pkg->nd_command;
3478 if ((1 << func) & NVDIMM_INTEL_SECURITY_CMDMASK)
3482 return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
3485 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
3486 enum nfit_ars_state req_type)
3488 struct device *dev = acpi_desc->dev;
3489 int scheduled = 0, busy = 0;
3490 struct nfit_spa *nfit_spa;
3492 mutex_lock(&acpi_desc->init_mutex);
3493 if (acpi_desc->cancel) {
3494 mutex_unlock(&acpi_desc->init_mutex);
3498 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3499 int type = nfit_spa_type(nfit_spa->spa);
3501 if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
3503 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3506 if (test_and_set_bit(req_type, &nfit_spa->ars_state))
3512 sched_ars(acpi_desc);
3513 dev_dbg(dev, "ars_scan triggered\n");
3515 mutex_unlock(&acpi_desc->init_mutex);
3524 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3526 struct nvdimm_bus_descriptor *nd_desc;
3528 dev_set_drvdata(dev, acpi_desc);
3529 acpi_desc->dev = dev;
3530 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3531 nd_desc = &acpi_desc->nd_desc;
3532 nd_desc->provider_name = "ACPI.NFIT";
3533 nd_desc->module = THIS_MODULE;
3534 nd_desc->ndctl = acpi_nfit_ctl;
3535 nd_desc->flush_probe = acpi_nfit_flush_probe;
3536 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3537 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3539 INIT_LIST_HEAD(&acpi_desc->spas);
3540 INIT_LIST_HEAD(&acpi_desc->dcrs);
3541 INIT_LIST_HEAD(&acpi_desc->bdws);
3542 INIT_LIST_HEAD(&acpi_desc->idts);
3543 INIT_LIST_HEAD(&acpi_desc->flushes);
3544 INIT_LIST_HEAD(&acpi_desc->memdevs);
3545 INIT_LIST_HEAD(&acpi_desc->dimms);
3546 INIT_LIST_HEAD(&acpi_desc->list);
3547 mutex_init(&acpi_desc->init_mutex);
3548 acpi_desc->scrub_tmo = 1;
3549 INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
3551 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3553 static void acpi_nfit_put_table(void *table)
3555 acpi_put_table(table);
3558 void acpi_nfit_shutdown(void *data)
3560 struct acpi_nfit_desc *acpi_desc = data;
3561 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3564 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3567 mutex_lock(&acpi_desc_lock);
3568 list_del(&acpi_desc->list);
3569 mutex_unlock(&acpi_desc_lock);
3571 mutex_lock(&acpi_desc->init_mutex);
3572 acpi_desc->cancel = 1;
3573 cancel_delayed_work_sync(&acpi_desc->dwork);
3574 mutex_unlock(&acpi_desc->init_mutex);
3577 * Bounce the nvdimm bus lock to make sure any in-flight
3578 * acpi_nfit_ars_rescan() submissions have had a chance to
3579 * either submit or see ->cancel set.
3581 device_lock(bus_dev);
3582 device_unlock(bus_dev);
3584 flush_workqueue(nfit_wq);
3586 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3588 static int acpi_nfit_add(struct acpi_device *adev)
3590 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3591 struct acpi_nfit_desc *acpi_desc;
3592 struct device *dev = &adev->dev;
3593 struct acpi_table_header *tbl;
3594 acpi_status status = AE_OK;
3598 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3599 if (ACPI_FAILURE(status)) {
3600 /* The NVDIMM root device allows OS to trigger enumeration of
3601 * NVDIMMs through NFIT at boot time and re-enumeration at
3602 * root level via the _FIT method during runtime.
3603 * This is ok to return 0 here, we could have an nvdimm
3604 * hotplugged later and evaluate _FIT method which returns
3605 * data in the format of a series of NFIT Structures.
3607 dev_dbg(dev, "failed to find NFIT at startup\n");
3611 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3616 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3619 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3621 /* Save the acpi header for exporting the revision via sysfs */
3622 acpi_desc->acpi_header = *tbl;
3624 /* Evaluate _FIT and override with that if present */
3625 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3626 if (ACPI_SUCCESS(status) && buf.length > 0) {
3627 union acpi_object *obj = buf.pointer;
3629 if (obj->type == ACPI_TYPE_BUFFER)
3630 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3631 obj->buffer.length);
3633 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3637 /* skip over the lead-in header table */
3638 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3639 + sizeof(struct acpi_table_nfit),
3640 sz - sizeof(struct acpi_table_nfit));
3644 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3647 static int acpi_nfit_remove(struct acpi_device *adev)
3649 /* see acpi_nfit_unregister */
3653 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3655 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3656 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3657 union acpi_object *obj;
3662 /* dev->driver may be null if we're being removed */
3663 dev_dbg(dev, "no driver found for dev\n");
3668 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3671 acpi_nfit_desc_init(acpi_desc, dev);
3674 * Finish previous registration before considering new
3677 flush_workqueue(nfit_wq);
3681 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3682 if (ACPI_FAILURE(status)) {
3683 dev_err(dev, "failed to evaluate _FIT\n");
3688 if (obj->type == ACPI_TYPE_BUFFER) {
3689 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3690 obj->buffer.length);
3692 dev_err(dev, "failed to merge updated NFIT\n");
3694 dev_err(dev, "Invalid _FIT\n");
3698 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3700 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3702 if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
3703 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
3705 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
3708 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3710 dev_dbg(dev, "event: 0x%x\n", event);
3713 case NFIT_NOTIFY_UPDATE:
3714 return acpi_nfit_update_notify(dev, handle);
3715 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3716 return acpi_nfit_uc_error_notify(dev, handle);
3721 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3723 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3725 device_lock(&adev->dev);
3726 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3727 device_unlock(&adev->dev);
3730 static const struct acpi_device_id acpi_nfit_ids[] = {
3734 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3736 static struct acpi_driver acpi_nfit_driver = {
3737 .name = KBUILD_MODNAME,
3738 .ids = acpi_nfit_ids,
3740 .add = acpi_nfit_add,
3741 .remove = acpi_nfit_remove,
3742 .notify = acpi_nfit_notify,
3746 static __init int nfit_init(void)
3750 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3751 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3752 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3753 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3754 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3755 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3756 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3757 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
3759 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3760 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3761 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3762 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3763 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3764 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3765 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3766 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3767 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3768 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3769 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3770 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3771 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3772 guid_parse(UUID_NFIT_DIMM_N_HYPERV, &nfit_uuid[NFIT_DEV_DIMM_N_HYPERV]);
3774 nfit_wq = create_singlethread_workqueue("nfit");
3778 nfit_mce_register();
3779 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3781 nfit_mce_unregister();
3782 destroy_workqueue(nfit_wq);
3789 static __exit void nfit_exit(void)
3791 nfit_mce_unregister();
3792 acpi_bus_unregister_driver(&acpi_nfit_driver);
3793 destroy_workqueue(nfit_wq);
3794 WARN_ON(!list_empty(&acpi_descs));
3797 module_init(nfit_init);
3798 module_exit(nfit_exit);
3799 MODULE_LICENSE("GPL v2");
3800 MODULE_AUTHOR("Intel Corporation");