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 LIST_HEAD(acpi_descs);
59 DEFINE_MUTEX(acpi_desc_lock);
61 static struct workqueue_struct *nfit_wq;
63 struct nfit_table_prev {
64 struct list_head spas;
65 struct list_head memdevs;
66 struct list_head dcrs;
67 struct list_head bdws;
68 struct list_head idts;
69 struct list_head flushes;
72 static guid_t nfit_uuid[NFIT_UUID_MAX];
74 const guid_t *to_nfit_uuid(enum nfit_uuids id)
76 return &nfit_uuid[id];
78 EXPORT_SYMBOL(to_nfit_uuid);
80 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
82 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
85 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
88 if (!nd_desc->provider_name
89 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
92 return to_acpi_device(acpi_desc->dev);
95 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
97 struct nd_cmd_clear_error *clear_err;
98 struct nd_cmd_ars_status *ars_status;
103 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
110 /* No supported scan types for this range */
111 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
112 if ((status >> 16 & flags) == 0)
115 case ND_CMD_ARS_START:
116 /* ARS is in progress */
117 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
124 case ND_CMD_ARS_STATUS:
129 /* Check extended status (Upper two bytes) */
130 if (status == NFIT_ARS_STATUS_DONE)
133 /* ARS is in progress */
134 if (status == NFIT_ARS_STATUS_BUSY)
137 /* No ARS performed for the current boot */
138 if (status == NFIT_ARS_STATUS_NONE)
142 * ARS interrupted, either we overflowed or some other
143 * agent wants the scan to stop. If we didn't overflow
144 * then just continue with the returned results.
146 if (status == NFIT_ARS_STATUS_INTR) {
147 if (ars_status->out_length >= 40 && (ars_status->flags
148 & NFIT_ARS_F_OVERFLOW))
157 case ND_CMD_CLEAR_ERROR:
161 if (!clear_err->cleared)
163 if (clear_err->length > clear_err->cleared)
164 return clear_err->cleared;
170 /* all other non-zero status results in an error */
176 #define ACPI_LABELS_LOCKED 3
178 static int xlat_nvdimm_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
181 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
184 case ND_CMD_GET_CONFIG_SIZE:
186 * In the _LSI, _LSR, _LSW case the locked status is
187 * communicated via the read/write commands
189 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
192 if (status >> 16 & ND_CONFIG_LOCKED)
195 case ND_CMD_GET_CONFIG_DATA:
196 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
197 && status == ACPI_LABELS_LOCKED)
200 case ND_CMD_SET_CONFIG_DATA:
201 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
202 && status == ACPI_LABELS_LOCKED)
209 /* all other non-zero status results in an error */
215 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
219 return xlat_bus_status(buf, cmd, status);
220 return xlat_nvdimm_status(nvdimm, buf, cmd, status);
223 /* convert _LS{I,R} packages to the buffer object acpi_nfit_ctl expects */
224 static union acpi_object *pkg_to_buf(union acpi_object *pkg)
229 union acpi_object *buf = NULL;
231 if (pkg->type != ACPI_TYPE_PACKAGE) {
232 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
237 for (i = 0; i < pkg->package.count; i++) {
238 union acpi_object *obj = &pkg->package.elements[i];
240 if (obj->type == ACPI_TYPE_INTEGER)
242 else if (obj->type == ACPI_TYPE_BUFFER)
243 size += obj->buffer.length;
245 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
251 buf = ACPI_ALLOCATE(sizeof(*buf) + size);
256 buf->type = ACPI_TYPE_BUFFER;
257 buf->buffer.length = size;
258 buf->buffer.pointer = dst;
259 for (i = 0; i < pkg->package.count; i++) {
260 union acpi_object *obj = &pkg->package.elements[i];
262 if (obj->type == ACPI_TYPE_INTEGER) {
263 memcpy(dst, &obj->integer.value, 4);
265 } else if (obj->type == ACPI_TYPE_BUFFER) {
266 memcpy(dst, obj->buffer.pointer, obj->buffer.length);
267 dst += obj->buffer.length;
275 static union acpi_object *int_to_buf(union acpi_object *integer)
277 union acpi_object *buf = ACPI_ALLOCATE(sizeof(*buf) + 4);
283 if (integer->type != ACPI_TYPE_INTEGER) {
284 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
290 buf->type = ACPI_TYPE_BUFFER;
291 buf->buffer.length = 4;
292 buf->buffer.pointer = dst;
293 memcpy(dst, &integer->integer.value, 4);
299 static union acpi_object *acpi_label_write(acpi_handle handle, u32 offset,
303 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
304 struct acpi_object_list input = {
306 .pointer = (union acpi_object []) {
308 .integer.type = ACPI_TYPE_INTEGER,
309 .integer.value = offset,
312 .integer.type = ACPI_TYPE_INTEGER,
313 .integer.value = len,
316 .buffer.type = ACPI_TYPE_BUFFER,
317 .buffer.pointer = data,
318 .buffer.length = len,
323 rc = acpi_evaluate_object(handle, "_LSW", &input, &buf);
324 if (ACPI_FAILURE(rc))
326 return int_to_buf(buf.pointer);
329 static union acpi_object *acpi_label_read(acpi_handle handle, u32 offset,
333 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
334 struct acpi_object_list input = {
336 .pointer = (union acpi_object []) {
338 .integer.type = ACPI_TYPE_INTEGER,
339 .integer.value = offset,
342 .integer.type = ACPI_TYPE_INTEGER,
343 .integer.value = len,
348 rc = acpi_evaluate_object(handle, "_LSR", &input, &buf);
349 if (ACPI_FAILURE(rc))
351 return pkg_to_buf(buf.pointer);
354 static union acpi_object *acpi_label_info(acpi_handle handle)
357 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
359 rc = acpi_evaluate_object(handle, "_LSI", NULL, &buf);
360 if (ACPI_FAILURE(rc))
362 return pkg_to_buf(buf.pointer);
365 static u8 nfit_dsm_revid(unsigned family, unsigned func)
367 static const u8 revid_table[NVDIMM_FAMILY_MAX+1][32] = {
368 [NVDIMM_FAMILY_INTEL] = {
369 [NVDIMM_INTEL_GET_MODES] = 2,
370 [NVDIMM_INTEL_GET_FWINFO] = 2,
371 [NVDIMM_INTEL_START_FWUPDATE] = 2,
372 [NVDIMM_INTEL_SEND_FWUPDATE] = 2,
373 [NVDIMM_INTEL_FINISH_FWUPDATE] = 2,
374 [NVDIMM_INTEL_QUERY_FWUPDATE] = 2,
375 [NVDIMM_INTEL_SET_THRESHOLD] = 2,
376 [NVDIMM_INTEL_INJECT_ERROR] = 2,
377 [NVDIMM_INTEL_GET_SECURITY_STATE] = 2,
378 [NVDIMM_INTEL_SET_PASSPHRASE] = 2,
379 [NVDIMM_INTEL_DISABLE_PASSPHRASE] = 2,
380 [NVDIMM_INTEL_UNLOCK_UNIT] = 2,
381 [NVDIMM_INTEL_FREEZE_LOCK] = 2,
382 [NVDIMM_INTEL_SECURE_ERASE] = 2,
383 [NVDIMM_INTEL_OVERWRITE] = 2,
384 [NVDIMM_INTEL_QUERY_OVERWRITE] = 2,
385 [NVDIMM_INTEL_SET_MASTER_PASSPHRASE] = 2,
386 [NVDIMM_INTEL_MASTER_SECURE_ERASE] = 2,
391 if (family > NVDIMM_FAMILY_MAX)
395 id = revid_table[family][func];
397 return 1; /* default */
401 static bool payload_dumpable(struct nvdimm *nvdimm, unsigned int func)
403 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
405 if (nfit_mem && nfit_mem->family == NVDIMM_FAMILY_INTEL
406 && func >= NVDIMM_INTEL_GET_SECURITY_STATE
407 && func <= NVDIMM_INTEL_MASTER_SECURE_ERASE)
408 return IS_ENABLED(CONFIG_NFIT_SECURITY_DEBUG);
412 static int cmd_to_func(struct nfit_mem *nfit_mem, unsigned int cmd,
413 struct nd_cmd_pkg *call_pkg)
418 if (nfit_mem->family != call_pkg->nd_family)
421 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
422 if (call_pkg->nd_reserved2[i])
424 return call_pkg->nd_command;
427 /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
428 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
432 * Force function number validation to fail since 0 is never
433 * published as a valid function in dsm_mask.
438 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
439 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
441 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
442 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
443 union acpi_object in_obj, in_buf, *out_obj;
444 const struct nd_cmd_desc *desc = NULL;
445 struct device *dev = acpi_desc->dev;
446 struct nd_cmd_pkg *call_pkg = NULL;
447 const char *cmd_name, *dimm_name;
448 unsigned long cmd_mask, dsm_mask;
449 u32 offset, fw_status = 0;
458 struct acpi_device *adev = nfit_mem->adev;
463 if (cmd == ND_CMD_CALL)
465 func = cmd_to_func(nfit_mem, cmd, call_pkg);
468 dimm_name = nvdimm_name(nvdimm);
469 cmd_name = nvdimm_cmd_name(cmd);
470 cmd_mask = nvdimm_cmd_mask(nvdimm);
471 dsm_mask = nfit_mem->dsm_mask;
472 desc = nd_cmd_dimm_desc(cmd);
473 guid = to_nfit_uuid(nfit_mem->family);
474 handle = adev->handle;
476 struct acpi_device *adev = to_acpi_dev(acpi_desc);
479 cmd_name = nvdimm_bus_cmd_name(cmd);
480 cmd_mask = nd_desc->cmd_mask;
482 if (cmd == ND_CMD_CALL)
483 dsm_mask = nd_desc->bus_dsm_mask;
484 desc = nd_cmd_bus_desc(cmd);
485 guid = to_nfit_uuid(NFIT_DEV_BUS);
486 handle = adev->handle;
490 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
494 * Check for a valid command. For ND_CMD_CALL, we also have to
495 * make sure that the DSM function is supported.
497 if (cmd == ND_CMD_CALL && !test_bit(func, &dsm_mask))
499 else if (!test_bit(cmd, &cmd_mask))
502 in_obj.type = ACPI_TYPE_PACKAGE;
503 in_obj.package.count = 1;
504 in_obj.package.elements = &in_buf;
505 in_buf.type = ACPI_TYPE_BUFFER;
506 in_buf.buffer.pointer = buf;
507 in_buf.buffer.length = 0;
509 /* libnvdimm has already validated the input envelope */
510 for (i = 0; i < desc->in_num; i++)
511 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
515 /* skip over package wrapper */
516 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
517 in_buf.buffer.length = call_pkg->nd_size_in;
520 dev_dbg(dev, "%s cmd: %d: func: %d input length: %d\n",
521 dimm_name, cmd, func, in_buf.buffer.length);
522 if (payload_dumpable(nvdimm, func))
523 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
524 in_buf.buffer.pointer,
525 min_t(u32, 256, in_buf.buffer.length), true);
527 /* call the BIOS, prefer the named methods over _DSM if available */
528 if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE
529 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
530 out_obj = acpi_label_info(handle);
531 else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA
532 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
533 struct nd_cmd_get_config_data_hdr *p = buf;
535 out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
536 } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
537 && test_bit(NFIT_MEM_LSW, &nfit_mem->flags)) {
538 struct nd_cmd_set_config_hdr *p = buf;
540 out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
546 revid = nfit_dsm_revid(nfit_mem->family, func);
549 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
553 dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
558 call_pkg->nd_fw_size = out_obj->buffer.length;
559 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
560 out_obj->buffer.pointer,
561 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
565 * Need to support FW function w/o known size in advance.
566 * Caller can determine required size based upon nd_fw_size.
567 * If we return an error (like elsewhere) then caller wouldn't
568 * be able to rely upon data returned to make calculation.
575 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
576 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
577 dimm_name, cmd_name, out_obj->type);
582 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
583 cmd_name, out_obj->buffer.length);
584 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
585 out_obj->buffer.pointer,
586 min_t(u32, 128, out_obj->buffer.length), true);
588 for (i = 0, offset = 0; i < desc->out_num; i++) {
589 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
590 (u32 *) out_obj->buffer.pointer,
591 out_obj->buffer.length - offset);
593 if (offset + out_size > out_obj->buffer.length) {
594 dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
595 dimm_name, cmd_name, i);
599 if (in_buf.buffer.length + offset + out_size > buf_len) {
600 dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
601 dimm_name, cmd_name, i);
605 memcpy(buf + in_buf.buffer.length + offset,
606 out_obj->buffer.pointer + offset, out_size);
611 * Set fw_status for all the commands with a known format to be
612 * later interpreted by xlat_status().
614 if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
615 && cmd <= ND_CMD_CLEAR_ERROR)
616 || (nvdimm && cmd >= ND_CMD_SMART
617 && cmd <= ND_CMD_VENDOR)))
618 fw_status = *(u32 *) out_obj->buffer.pointer;
620 if (offset + in_buf.buffer.length < buf_len) {
623 * status valid, return the number of bytes left
624 * unfilled in the output buffer
626 rc = buf_len - offset - in_buf.buffer.length;
628 *cmd_rc = xlat_status(nvdimm, buf, cmd,
631 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
632 __func__, dimm_name, cmd_name, buf_len,
639 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
647 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
649 static const char *spa_type_name(u16 type)
651 static const char *to_name[] = {
652 [NFIT_SPA_VOLATILE] = "volatile",
653 [NFIT_SPA_PM] = "pmem",
654 [NFIT_SPA_DCR] = "dimm-control-region",
655 [NFIT_SPA_BDW] = "block-data-window",
656 [NFIT_SPA_VDISK] = "volatile-disk",
657 [NFIT_SPA_VCD] = "volatile-cd",
658 [NFIT_SPA_PDISK] = "persistent-disk",
659 [NFIT_SPA_PCD] = "persistent-cd",
663 if (type > NFIT_SPA_PCD)
666 return to_name[type];
669 int nfit_spa_type(struct acpi_nfit_system_address *spa)
673 for (i = 0; i < NFIT_UUID_MAX; i++)
674 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
679 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
680 struct nfit_table_prev *prev,
681 struct acpi_nfit_system_address *spa)
683 struct device *dev = acpi_desc->dev;
684 struct nfit_spa *nfit_spa;
686 if (spa->header.length != sizeof(*spa))
689 list_for_each_entry(nfit_spa, &prev->spas, list) {
690 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
691 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
696 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
700 INIT_LIST_HEAD(&nfit_spa->list);
701 memcpy(nfit_spa->spa, spa, sizeof(*spa));
702 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
703 dev_dbg(dev, "spa index: %d type: %s\n",
705 spa_type_name(nfit_spa_type(spa)));
709 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
710 struct nfit_table_prev *prev,
711 struct acpi_nfit_memory_map *memdev)
713 struct device *dev = acpi_desc->dev;
714 struct nfit_memdev *nfit_memdev;
716 if (memdev->header.length != sizeof(*memdev))
719 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
720 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
721 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
725 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
729 INIT_LIST_HEAD(&nfit_memdev->list);
730 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
731 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
732 dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
733 memdev->device_handle, memdev->range_index,
734 memdev->region_index, memdev->flags);
738 int nfit_get_smbios_id(u32 device_handle, u16 *flags)
740 struct acpi_nfit_memory_map *memdev;
741 struct acpi_nfit_desc *acpi_desc;
742 struct nfit_mem *nfit_mem;
745 mutex_lock(&acpi_desc_lock);
746 list_for_each_entry(acpi_desc, &acpi_descs, list) {
747 mutex_lock(&acpi_desc->init_mutex);
748 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
749 memdev = __to_nfit_memdev(nfit_mem);
750 if (memdev->device_handle == device_handle) {
751 *flags = memdev->flags;
752 physical_id = memdev->physical_id;
753 mutex_unlock(&acpi_desc->init_mutex);
754 mutex_unlock(&acpi_desc_lock);
758 mutex_unlock(&acpi_desc->init_mutex);
760 mutex_unlock(&acpi_desc_lock);
764 EXPORT_SYMBOL_GPL(nfit_get_smbios_id);
767 * An implementation may provide a truncated control region if no block windows
770 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
772 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
777 return offsetof(struct acpi_nfit_control_region, window_size);
780 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
781 struct nfit_table_prev *prev,
782 struct acpi_nfit_control_region *dcr)
784 struct device *dev = acpi_desc->dev;
785 struct nfit_dcr *nfit_dcr;
787 if (!sizeof_dcr(dcr))
790 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
791 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
792 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
796 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
800 INIT_LIST_HEAD(&nfit_dcr->list);
801 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
802 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
803 dev_dbg(dev, "dcr index: %d windows: %d\n",
804 dcr->region_index, dcr->windows);
808 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
809 struct nfit_table_prev *prev,
810 struct acpi_nfit_data_region *bdw)
812 struct device *dev = acpi_desc->dev;
813 struct nfit_bdw *nfit_bdw;
815 if (bdw->header.length != sizeof(*bdw))
817 list_for_each_entry(nfit_bdw, &prev->bdws, list)
818 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
819 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
823 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
827 INIT_LIST_HEAD(&nfit_bdw->list);
828 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
829 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
830 dev_dbg(dev, "bdw dcr: %d windows: %d\n",
831 bdw->region_index, bdw->windows);
835 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
837 if (idt->header.length < sizeof(*idt))
839 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
842 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
843 struct nfit_table_prev *prev,
844 struct acpi_nfit_interleave *idt)
846 struct device *dev = acpi_desc->dev;
847 struct nfit_idt *nfit_idt;
849 if (!sizeof_idt(idt))
852 list_for_each_entry(nfit_idt, &prev->idts, list) {
853 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
856 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
857 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
862 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
866 INIT_LIST_HEAD(&nfit_idt->list);
867 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
868 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
869 dev_dbg(dev, "idt index: %d num_lines: %d\n",
870 idt->interleave_index, idt->line_count);
874 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
876 if (flush->header.length < sizeof(*flush))
878 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
881 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
882 struct nfit_table_prev *prev,
883 struct acpi_nfit_flush_address *flush)
885 struct device *dev = acpi_desc->dev;
886 struct nfit_flush *nfit_flush;
888 if (!sizeof_flush(flush))
891 list_for_each_entry(nfit_flush, &prev->flushes, list) {
892 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
895 if (memcmp(nfit_flush->flush, flush,
896 sizeof_flush(flush)) == 0) {
897 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
902 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
903 + sizeof_flush(flush), GFP_KERNEL);
906 INIT_LIST_HEAD(&nfit_flush->list);
907 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
908 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
909 dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
910 flush->device_handle, flush->hint_count);
914 static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
915 struct acpi_nfit_capabilities *pcap)
917 struct device *dev = acpi_desc->dev;
920 mask = (1 << (pcap->highest_capability + 1)) - 1;
921 acpi_desc->platform_cap = pcap->capabilities & mask;
922 dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
926 static void *add_table(struct acpi_nfit_desc *acpi_desc,
927 struct nfit_table_prev *prev, void *table, const void *end)
929 struct device *dev = acpi_desc->dev;
930 struct acpi_nfit_header *hdr;
931 void *err = ERR_PTR(-ENOMEM);
938 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
944 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
945 if (!add_spa(acpi_desc, prev, table))
948 case ACPI_NFIT_TYPE_MEMORY_MAP:
949 if (!add_memdev(acpi_desc, prev, table))
952 case ACPI_NFIT_TYPE_CONTROL_REGION:
953 if (!add_dcr(acpi_desc, prev, table))
956 case ACPI_NFIT_TYPE_DATA_REGION:
957 if (!add_bdw(acpi_desc, prev, table))
960 case ACPI_NFIT_TYPE_INTERLEAVE:
961 if (!add_idt(acpi_desc, prev, table))
964 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
965 if (!add_flush(acpi_desc, prev, table))
968 case ACPI_NFIT_TYPE_SMBIOS:
969 dev_dbg(dev, "smbios\n");
971 case ACPI_NFIT_TYPE_CAPABILITIES:
972 if (!add_platform_cap(acpi_desc, table))
976 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
980 return table + hdr->length;
983 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
984 struct nfit_mem *nfit_mem)
986 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
987 u16 dcr = nfit_mem->dcr->region_index;
988 struct nfit_spa *nfit_spa;
990 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
991 u16 range_index = nfit_spa->spa->range_index;
992 int type = nfit_spa_type(nfit_spa->spa);
993 struct nfit_memdev *nfit_memdev;
995 if (type != NFIT_SPA_BDW)
998 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
999 if (nfit_memdev->memdev->range_index != range_index)
1001 if (nfit_memdev->memdev->device_handle != device_handle)
1003 if (nfit_memdev->memdev->region_index != dcr)
1006 nfit_mem->spa_bdw = nfit_spa->spa;
1011 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
1012 nfit_mem->spa_dcr->range_index);
1013 nfit_mem->bdw = NULL;
1016 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
1017 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
1019 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
1020 struct nfit_memdev *nfit_memdev;
1021 struct nfit_bdw *nfit_bdw;
1022 struct nfit_idt *nfit_idt;
1023 u16 idt_idx, range_index;
1025 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
1026 if (nfit_bdw->bdw->region_index != dcr)
1028 nfit_mem->bdw = nfit_bdw->bdw;
1035 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
1037 if (!nfit_mem->spa_bdw)
1040 range_index = nfit_mem->spa_bdw->range_index;
1041 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1042 if (nfit_memdev->memdev->range_index != range_index ||
1043 nfit_memdev->memdev->region_index != dcr)
1045 nfit_mem->memdev_bdw = nfit_memdev->memdev;
1046 idt_idx = nfit_memdev->memdev->interleave_index;
1047 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1048 if (nfit_idt->idt->interleave_index != idt_idx)
1050 nfit_mem->idt_bdw = nfit_idt->idt;
1057 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
1058 struct acpi_nfit_system_address *spa)
1060 struct nfit_mem *nfit_mem, *found;
1061 struct nfit_memdev *nfit_memdev;
1062 int type = spa ? nfit_spa_type(spa) : 0;
1074 * This loop runs in two modes, when a dimm is mapped the loop
1075 * adds memdev associations to an existing dimm, or creates a
1076 * dimm. In the unmapped dimm case this loop sweeps for memdev
1077 * instances with an invalid / zero range_index and adds those
1078 * dimms without spa associations.
1080 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1081 struct nfit_flush *nfit_flush;
1082 struct nfit_dcr *nfit_dcr;
1086 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
1088 if (!spa && nfit_memdev->memdev->range_index)
1091 dcr = nfit_memdev->memdev->region_index;
1092 device_handle = nfit_memdev->memdev->device_handle;
1093 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1094 if (__to_nfit_memdev(nfit_mem)->device_handle
1103 nfit_mem = devm_kzalloc(acpi_desc->dev,
1104 sizeof(*nfit_mem), GFP_KERNEL);
1107 INIT_LIST_HEAD(&nfit_mem->list);
1108 nfit_mem->acpi_desc = acpi_desc;
1109 list_add(&nfit_mem->list, &acpi_desc->dimms);
1112 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1113 if (nfit_dcr->dcr->region_index != dcr)
1116 * Record the control region for the dimm. For
1117 * the ACPI 6.1 case, where there are separate
1118 * control regions for the pmem vs blk
1119 * interfaces, be sure to record the extended
1123 nfit_mem->dcr = nfit_dcr->dcr;
1124 else if (nfit_mem->dcr->windows == 0
1125 && nfit_dcr->dcr->windows)
1126 nfit_mem->dcr = nfit_dcr->dcr;
1130 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
1131 struct acpi_nfit_flush_address *flush;
1134 if (nfit_flush->flush->device_handle != device_handle)
1136 nfit_mem->nfit_flush = nfit_flush;
1137 flush = nfit_flush->flush;
1138 nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
1140 sizeof(struct resource),
1142 if (!nfit_mem->flush_wpq)
1144 for (i = 0; i < flush->hint_count; i++) {
1145 struct resource *res = &nfit_mem->flush_wpq[i];
1147 res->start = flush->hint_address[i];
1148 res->end = res->start + 8 - 1;
1153 if (dcr && !nfit_mem->dcr) {
1154 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
1155 spa->range_index, dcr);
1159 if (type == NFIT_SPA_DCR) {
1160 struct nfit_idt *nfit_idt;
1163 /* multiple dimms may share a SPA when interleaved */
1164 nfit_mem->spa_dcr = spa;
1165 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1166 idt_idx = nfit_memdev->memdev->interleave_index;
1167 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1168 if (nfit_idt->idt->interleave_index != idt_idx)
1170 nfit_mem->idt_dcr = nfit_idt->idt;
1173 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
1174 } else if (type == NFIT_SPA_PM) {
1176 * A single dimm may belong to multiple SPA-PM
1177 * ranges, record at least one in addition to
1178 * any SPA-DCR range.
1180 nfit_mem->memdev_pmem = nfit_memdev->memdev;
1182 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1188 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
1190 struct nfit_mem *a = container_of(_a, typeof(*a), list);
1191 struct nfit_mem *b = container_of(_b, typeof(*b), list);
1192 u32 handleA, handleB;
1194 handleA = __to_nfit_memdev(a)->device_handle;
1195 handleB = __to_nfit_memdev(b)->device_handle;
1196 if (handleA < handleB)
1198 else if (handleA > handleB)
1203 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
1205 struct nfit_spa *nfit_spa;
1210 * For each SPA-DCR or SPA-PMEM address range find its
1211 * corresponding MEMDEV(s). From each MEMDEV find the
1212 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1213 * try to find a SPA-BDW and a corresponding BDW that references
1214 * the DCR. Throw it all into an nfit_mem object. Note, that
1215 * BDWs are optional.
1217 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1218 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
1224 * If a DIMM has failed to be mapped into SPA there will be no
1225 * SPA entries above. Find and register all the unmapped DIMMs
1226 * for reporting and recovery purposes.
1228 rc = __nfit_mem_init(acpi_desc, NULL);
1232 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
1237 static ssize_t bus_dsm_mask_show(struct device *dev,
1238 struct device_attribute *attr, char *buf)
1240 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1241 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1243 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
1245 static struct device_attribute dev_attr_bus_dsm_mask =
1246 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
1248 static ssize_t revision_show(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1251 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1252 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1253 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1255 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
1257 static DEVICE_ATTR_RO(revision);
1259 static ssize_t hw_error_scrub_show(struct device *dev,
1260 struct device_attribute *attr, char *buf)
1262 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1263 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1264 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1266 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
1270 * The 'hw_error_scrub' attribute can have the following values written to it:
1271 * '0': Switch to the default mode where an exception will only insert
1272 * the address of the memory error into the poison and badblocks lists.
1273 * '1': Enable a full scrub to happen if an exception for a memory error is
1276 static ssize_t hw_error_scrub_store(struct device *dev,
1277 struct device_attribute *attr, const char *buf, size_t size)
1279 struct nvdimm_bus_descriptor *nd_desc;
1283 rc = kstrtol(buf, 0, &val);
1288 nd_desc = dev_get_drvdata(dev);
1290 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1293 case HW_ERROR_SCRUB_ON:
1294 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1296 case HW_ERROR_SCRUB_OFF:
1297 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1309 static DEVICE_ATTR_RW(hw_error_scrub);
1312 * This shows the number of full Address Range Scrubs that have been
1313 * completed since driver load time. Userspace can wait on this using
1314 * select/poll etc. A '+' at the end indicates an ARS is in progress
1316 static ssize_t scrub_show(struct device *dev,
1317 struct device_attribute *attr, char *buf)
1319 struct nvdimm_bus_descriptor *nd_desc;
1320 ssize_t rc = -ENXIO;
1323 nd_desc = dev_get_drvdata(dev);
1325 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1327 mutex_lock(&acpi_desc->init_mutex);
1328 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1329 acpi_desc->scrub_busy
1330 && !acpi_desc->cancel ? "+\n" : "\n");
1331 mutex_unlock(&acpi_desc->init_mutex);
1337 static ssize_t scrub_store(struct device *dev,
1338 struct device_attribute *attr, const char *buf, size_t size)
1340 struct nvdimm_bus_descriptor *nd_desc;
1344 rc = kstrtol(buf, 0, &val);
1351 nd_desc = dev_get_drvdata(dev);
1353 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1355 rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
1362 static DEVICE_ATTR_RW(scrub);
1364 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1366 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1367 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1368 | 1 << ND_CMD_ARS_STATUS;
1370 return (nd_desc->cmd_mask & mask) == mask;
1373 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1375 struct device *dev = container_of(kobj, struct device, kobj);
1376 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1378 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1383 static struct attribute *acpi_nfit_attributes[] = {
1384 &dev_attr_revision.attr,
1385 &dev_attr_scrub.attr,
1386 &dev_attr_hw_error_scrub.attr,
1387 &dev_attr_bus_dsm_mask.attr,
1391 static const struct attribute_group acpi_nfit_attribute_group = {
1393 .attrs = acpi_nfit_attributes,
1394 .is_visible = nfit_visible,
1397 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1398 &nvdimm_bus_attribute_group,
1399 &acpi_nfit_attribute_group,
1403 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1405 struct nvdimm *nvdimm = to_nvdimm(dev);
1406 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1408 return __to_nfit_memdev(nfit_mem);
1411 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1413 struct nvdimm *nvdimm = to_nvdimm(dev);
1414 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1416 return nfit_mem->dcr;
1419 static ssize_t handle_show(struct device *dev,
1420 struct device_attribute *attr, char *buf)
1422 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1424 return sprintf(buf, "%#x\n", memdev->device_handle);
1426 static DEVICE_ATTR_RO(handle);
1428 static ssize_t phys_id_show(struct device *dev,
1429 struct device_attribute *attr, char *buf)
1431 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1433 return sprintf(buf, "%#x\n", memdev->physical_id);
1435 static DEVICE_ATTR_RO(phys_id);
1437 static ssize_t vendor_show(struct device *dev,
1438 struct device_attribute *attr, char *buf)
1440 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1442 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1444 static DEVICE_ATTR_RO(vendor);
1446 static ssize_t rev_id_show(struct device *dev,
1447 struct device_attribute *attr, char *buf)
1449 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1451 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1453 static DEVICE_ATTR_RO(rev_id);
1455 static ssize_t device_show(struct device *dev,
1456 struct device_attribute *attr, char *buf)
1458 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1460 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1462 static DEVICE_ATTR_RO(device);
1464 static ssize_t subsystem_vendor_show(struct device *dev,
1465 struct device_attribute *attr, char *buf)
1467 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1469 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1471 static DEVICE_ATTR_RO(subsystem_vendor);
1473 static ssize_t subsystem_rev_id_show(struct device *dev,
1474 struct device_attribute *attr, char *buf)
1476 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1478 return sprintf(buf, "0x%04x\n",
1479 be16_to_cpu(dcr->subsystem_revision_id));
1481 static DEVICE_ATTR_RO(subsystem_rev_id);
1483 static ssize_t subsystem_device_show(struct device *dev,
1484 struct device_attribute *attr, char *buf)
1486 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1488 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1490 static DEVICE_ATTR_RO(subsystem_device);
1492 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1494 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1497 if (nfit_mem->memdev_pmem)
1499 if (nfit_mem->memdev_bdw)
1504 static ssize_t format_show(struct device *dev,
1505 struct device_attribute *attr, char *buf)
1507 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1509 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1511 static DEVICE_ATTR_RO(format);
1513 static ssize_t format1_show(struct device *dev,
1514 struct device_attribute *attr, char *buf)
1517 ssize_t rc = -ENXIO;
1518 struct nfit_mem *nfit_mem;
1519 struct nfit_memdev *nfit_memdev;
1520 struct acpi_nfit_desc *acpi_desc;
1521 struct nvdimm *nvdimm = to_nvdimm(dev);
1522 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1524 nfit_mem = nvdimm_provider_data(nvdimm);
1525 acpi_desc = nfit_mem->acpi_desc;
1526 handle = to_nfit_memdev(dev)->device_handle;
1528 /* assumes DIMMs have at most 2 published interface codes */
1529 mutex_lock(&acpi_desc->init_mutex);
1530 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1531 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1532 struct nfit_dcr *nfit_dcr;
1534 if (memdev->device_handle != handle)
1537 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1538 if (nfit_dcr->dcr->region_index != memdev->region_index)
1540 if (nfit_dcr->dcr->code == dcr->code)
1542 rc = sprintf(buf, "0x%04x\n",
1543 le16_to_cpu(nfit_dcr->dcr->code));
1549 mutex_unlock(&acpi_desc->init_mutex);
1552 static DEVICE_ATTR_RO(format1);
1554 static ssize_t formats_show(struct device *dev,
1555 struct device_attribute *attr, char *buf)
1557 struct nvdimm *nvdimm = to_nvdimm(dev);
1559 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1561 static DEVICE_ATTR_RO(formats);
1563 static ssize_t serial_show(struct device *dev,
1564 struct device_attribute *attr, char *buf)
1566 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1568 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1570 static DEVICE_ATTR_RO(serial);
1572 static ssize_t family_show(struct device *dev,
1573 struct device_attribute *attr, char *buf)
1575 struct nvdimm *nvdimm = to_nvdimm(dev);
1576 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1578 if (nfit_mem->family < 0)
1580 return sprintf(buf, "%d\n", nfit_mem->family);
1582 static DEVICE_ATTR_RO(family);
1584 static ssize_t dsm_mask_show(struct device *dev,
1585 struct device_attribute *attr, char *buf)
1587 struct nvdimm *nvdimm = to_nvdimm(dev);
1588 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1590 if (nfit_mem->family < 0)
1592 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1594 static DEVICE_ATTR_RO(dsm_mask);
1596 static ssize_t flags_show(struct device *dev,
1597 struct device_attribute *attr, char *buf)
1599 struct nvdimm *nvdimm = to_nvdimm(dev);
1600 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1601 u16 flags = __to_nfit_memdev(nfit_mem)->flags;
1603 if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
1604 flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
1606 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1607 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1608 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1609 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1610 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1611 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1612 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1613 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1615 static DEVICE_ATTR_RO(flags);
1617 static ssize_t id_show(struct device *dev,
1618 struct device_attribute *attr, char *buf)
1620 struct nvdimm *nvdimm = to_nvdimm(dev);
1621 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1623 return sprintf(buf, "%s\n", nfit_mem->id);
1625 static DEVICE_ATTR_RO(id);
1627 static ssize_t dirty_shutdown_show(struct device *dev,
1628 struct device_attribute *attr, char *buf)
1630 struct nvdimm *nvdimm = to_nvdimm(dev);
1631 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1633 return sprintf(buf, "%d\n", nfit_mem->dirty_shutdown);
1635 static DEVICE_ATTR_RO(dirty_shutdown);
1637 static struct attribute *acpi_nfit_dimm_attributes[] = {
1638 &dev_attr_handle.attr,
1639 &dev_attr_phys_id.attr,
1640 &dev_attr_vendor.attr,
1641 &dev_attr_device.attr,
1642 &dev_attr_rev_id.attr,
1643 &dev_attr_subsystem_vendor.attr,
1644 &dev_attr_subsystem_device.attr,
1645 &dev_attr_subsystem_rev_id.attr,
1646 &dev_attr_format.attr,
1647 &dev_attr_formats.attr,
1648 &dev_attr_format1.attr,
1649 &dev_attr_serial.attr,
1650 &dev_attr_flags.attr,
1652 &dev_attr_family.attr,
1653 &dev_attr_dsm_mask.attr,
1654 &dev_attr_dirty_shutdown.attr,
1658 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1659 struct attribute *a, int n)
1661 struct device *dev = container_of(kobj, struct device, kobj);
1662 struct nvdimm *nvdimm = to_nvdimm(dev);
1663 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1665 if (!to_nfit_dcr(dev)) {
1666 /* Without a dcr only the memdev attributes can be surfaced */
1667 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1668 || a == &dev_attr_flags.attr
1669 || a == &dev_attr_family.attr
1670 || a == &dev_attr_dsm_mask.attr)
1675 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1678 if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
1679 && a == &dev_attr_dirty_shutdown.attr)
1685 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1687 .attrs = acpi_nfit_dimm_attributes,
1688 .is_visible = acpi_nfit_dimm_attr_visible,
1691 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1692 &nvdimm_attribute_group,
1693 &nd_device_attribute_group,
1694 &acpi_nfit_dimm_attribute_group,
1698 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1701 struct nfit_mem *nfit_mem;
1703 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1704 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1705 return nfit_mem->nvdimm;
1710 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1712 struct nfit_mem *nfit_mem;
1713 struct acpi_nfit_desc *acpi_desc;
1715 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
1718 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1719 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1724 acpi_desc = dev_get_drvdata(dev->parent);
1729 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1732 nfit_mem = dev_get_drvdata(dev);
1733 if (nfit_mem && nfit_mem->flags_attr)
1734 sysfs_notify_dirent(nfit_mem->flags_attr);
1736 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1738 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1740 struct acpi_device *adev = data;
1741 struct device *dev = &adev->dev;
1743 device_lock(dev->parent);
1744 __acpi_nvdimm_notify(dev, event);
1745 device_unlock(dev->parent);
1748 static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1753 status = acpi_get_handle(adev->handle, method, &handle);
1755 if (ACPI_SUCCESS(status))
1760 __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
1762 struct nd_intel_smart smart = { 0 };
1763 union acpi_object in_buf = {
1764 .type = ACPI_TYPE_BUFFER,
1765 .buffer.pointer = (char *) &smart,
1766 .buffer.length = sizeof(smart),
1768 union acpi_object in_obj = {
1769 .type = ACPI_TYPE_PACKAGE,
1771 .package.elements = &in_buf,
1773 const u8 func = ND_INTEL_SMART;
1774 const guid_t *guid = to_nfit_uuid(nfit_mem->family);
1775 u8 revid = nfit_dsm_revid(nfit_mem->family, func);
1776 struct acpi_device *adev = nfit_mem->adev;
1777 acpi_handle handle = adev->handle;
1778 union acpi_object *out_obj;
1780 if ((nfit_mem->dsm_mask & (1 << func)) == 0)
1783 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
1787 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
1788 if (smart.shutdown_state)
1789 set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
1792 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
1793 set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
1794 nfit_mem->dirty_shutdown = smart.shutdown_count;
1799 static void populate_shutdown_status(struct nfit_mem *nfit_mem)
1802 * For DIMMs that provide a dynamic facility to retrieve a
1803 * dirty-shutdown status and/or a dirty-shutdown count, cache
1804 * these values in nfit_mem.
1806 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1807 nfit_intel_shutdown_status(nfit_mem);
1810 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1811 struct nfit_mem *nfit_mem, u32 device_handle)
1813 struct acpi_device *adev, *adev_dimm;
1814 struct device *dev = acpi_desc->dev;
1815 unsigned long dsm_mask, label_mask;
1819 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1821 /* nfit test assumes 1:1 relationship between commands and dsms */
1822 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1823 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1825 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1826 sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
1827 be16_to_cpu(dcr->vendor_id),
1828 dcr->manufacturing_location,
1829 be16_to_cpu(dcr->manufacturing_date),
1830 be32_to_cpu(dcr->serial_number));
1832 sprintf(nfit_mem->id, "%04x-%08x",
1833 be16_to_cpu(dcr->vendor_id),
1834 be32_to_cpu(dcr->serial_number));
1836 adev = to_acpi_dev(acpi_desc);
1838 /* unit test case */
1839 populate_shutdown_status(nfit_mem);
1843 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1844 nfit_mem->adev = adev_dimm;
1846 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1848 return force_enable_dimms ? 0 : -ENODEV;
1851 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1852 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1853 dev_err(dev, "%s: notification registration failed\n",
1854 dev_name(&adev_dimm->dev));
1858 * Record nfit_mem for the notification path to track back to
1859 * the nfit sysfs attributes for this dimm device object.
1861 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1864 * Until standardization materializes we need to consider 4
1865 * different command sets. Note, that checking for function0 (bit0)
1866 * tells us if any commands are reachable through this GUID.
1868 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
1869 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1870 if (family < 0 || i == default_dsm_family)
1873 /* limit the supported commands to those that are publicly documented */
1874 nfit_mem->family = family;
1875 if (override_dsm_mask && !disable_vendor_specific)
1876 dsm_mask = override_dsm_mask;
1877 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1878 dsm_mask = NVDIMM_INTEL_CMDMASK;
1879 if (disable_vendor_specific)
1880 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1881 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1882 dsm_mask = 0x1c3c76;
1883 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1885 if (disable_vendor_specific)
1886 dsm_mask &= ~(1 << 8);
1887 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1888 dsm_mask = 0xffffffff;
1890 dev_dbg(dev, "unknown dimm command family\n");
1891 nfit_mem->family = -1;
1892 /* DSMs are optional, continue loading the driver... */
1897 * Function 0 is the command interrogation function, don't
1898 * export it to potential userspace use, and enable it to be
1899 * used as an error value in acpi_nfit_ctl().
1903 guid = to_nfit_uuid(nfit_mem->family);
1904 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1905 if (acpi_check_dsm(adev_dimm->handle, guid,
1906 nfit_dsm_revid(nfit_mem->family, i),
1908 set_bit(i, &nfit_mem->dsm_mask);
1911 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1912 * due to their better semantics handling locked capacity.
1914 label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
1915 | 1 << ND_CMD_SET_CONFIG_DATA;
1916 if (family == NVDIMM_FAMILY_INTEL
1917 && (dsm_mask & label_mask) == label_mask)
1920 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1921 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
1922 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
1923 set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1926 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
1927 && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
1928 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
1929 set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
1932 populate_shutdown_status(nfit_mem);
1937 static void shutdown_dimm_notify(void *data)
1939 struct acpi_nfit_desc *acpi_desc = data;
1940 struct nfit_mem *nfit_mem;
1942 mutex_lock(&acpi_desc->init_mutex);
1944 * Clear out the nfit_mem->flags_attr and shut down dimm event
1947 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1948 struct acpi_device *adev_dimm = nfit_mem->adev;
1950 if (nfit_mem->flags_attr) {
1951 sysfs_put(nfit_mem->flags_attr);
1952 nfit_mem->flags_attr = NULL;
1955 acpi_remove_notify_handler(adev_dimm->handle,
1956 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1957 dev_set_drvdata(&adev_dimm->dev, NULL);
1960 mutex_unlock(&acpi_desc->init_mutex);
1963 static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
1966 case NVDIMM_FAMILY_INTEL:
1967 return intel_security_ops;
1973 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1975 struct nfit_mem *nfit_mem;
1976 int dimm_count = 0, rc;
1977 struct nvdimm *nvdimm;
1979 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1980 struct acpi_nfit_flush_address *flush;
1981 unsigned long flags = 0, cmd_mask;
1982 struct nfit_memdev *nfit_memdev;
1986 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1987 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1993 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1994 set_bit(NDD_ALIASING, &flags);
1996 /* collate flags across all memdevs for this dimm */
1997 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1998 struct acpi_nfit_memory_map *dimm_memdev;
2000 dimm_memdev = __to_nfit_memdev(nfit_mem);
2001 if (dimm_memdev->device_handle
2002 != nfit_memdev->memdev->device_handle)
2004 dimm_memdev->flags |= nfit_memdev->memdev->flags;
2007 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
2008 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
2009 set_bit(NDD_UNARMED, &flags);
2011 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
2016 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
2017 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
2018 * userspace interface.
2020 cmd_mask = 1UL << ND_CMD_CALL;
2021 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
2023 * These commands have a 1:1 correspondence
2024 * between DSM payload and libnvdimm ioctl
2027 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
2030 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
2031 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
2032 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
2034 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
2035 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
2037 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
2039 nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
2040 acpi_nfit_dimm_attribute_groups,
2041 flags, cmd_mask, flush ? flush->hint_count : 0,
2042 nfit_mem->flush_wpq, &nfit_mem->id[0],
2043 acpi_nfit_get_security_ops(nfit_mem->family));
2047 nfit_mem->nvdimm = nvdimm;
2050 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
2053 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
2054 nvdimm_name(nvdimm),
2055 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
2056 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
2057 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
2058 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
2059 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
2063 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
2068 * Now that dimms are successfully registered, and async registration
2069 * is flushed, attempt to enable event notification.
2071 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2072 struct kernfs_node *nfit_kernfs;
2074 nvdimm = nfit_mem->nvdimm;
2078 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
2080 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
2082 sysfs_put(nfit_kernfs);
2083 if (!nfit_mem->flags_attr)
2084 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
2085 nvdimm_name(nvdimm));
2088 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
2093 * These constants are private because there are no kernel consumers of
2096 enum nfit_aux_cmds {
2097 NFIT_CMD_TRANSLATE_SPA = 5,
2098 NFIT_CMD_ARS_INJECT_SET = 7,
2099 NFIT_CMD_ARS_INJECT_CLEAR = 8,
2100 NFIT_CMD_ARS_INJECT_GET = 9,
2103 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
2105 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2106 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
2107 struct acpi_device *adev;
2108 unsigned long dsm_mask;
2111 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
2112 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
2113 adev = to_acpi_dev(acpi_desc);
2117 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
2118 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2119 set_bit(i, &nd_desc->cmd_mask);
2120 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
2123 (1 << ND_CMD_ARS_CAP) |
2124 (1 << ND_CMD_ARS_START) |
2125 (1 << ND_CMD_ARS_STATUS) |
2126 (1 << ND_CMD_CLEAR_ERROR) |
2127 (1 << NFIT_CMD_TRANSLATE_SPA) |
2128 (1 << NFIT_CMD_ARS_INJECT_SET) |
2129 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
2130 (1 << NFIT_CMD_ARS_INJECT_GET);
2131 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
2132 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2133 set_bit(i, &nd_desc->bus_dsm_mask);
2136 static ssize_t range_index_show(struct device *dev,
2137 struct device_attribute *attr, char *buf)
2139 struct nd_region *nd_region = to_nd_region(dev);
2140 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
2142 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
2144 static DEVICE_ATTR_RO(range_index);
2146 static struct attribute *acpi_nfit_region_attributes[] = {
2147 &dev_attr_range_index.attr,
2151 static const struct attribute_group acpi_nfit_region_attribute_group = {
2153 .attrs = acpi_nfit_region_attributes,
2156 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
2157 &nd_region_attribute_group,
2158 &nd_mapping_attribute_group,
2159 &nd_device_attribute_group,
2160 &nd_numa_attribute_group,
2161 &acpi_nfit_region_attribute_group,
2165 /* enough info to uniquely specify an interleave set */
2166 struct nfit_set_info {
2167 struct nfit_set_info_map {
2174 struct nfit_set_info2 {
2175 struct nfit_set_info_map2 {
2179 u16 manufacturing_date;
2180 u8 manufacturing_location;
2185 static size_t sizeof_nfit_set_info(int num_mappings)
2187 return sizeof(struct nfit_set_info)
2188 + num_mappings * sizeof(struct nfit_set_info_map);
2191 static size_t sizeof_nfit_set_info2(int num_mappings)
2193 return sizeof(struct nfit_set_info2)
2194 + num_mappings * sizeof(struct nfit_set_info_map2);
2197 static int cmp_map_compat(const void *m0, const void *m1)
2199 const struct nfit_set_info_map *map0 = m0;
2200 const struct nfit_set_info_map *map1 = m1;
2202 return memcmp(&map0->region_offset, &map1->region_offset,
2206 static int cmp_map(const void *m0, const void *m1)
2208 const struct nfit_set_info_map *map0 = m0;
2209 const struct nfit_set_info_map *map1 = m1;
2211 if (map0->region_offset < map1->region_offset)
2213 else if (map0->region_offset > map1->region_offset)
2218 static int cmp_map2(const void *m0, const void *m1)
2220 const struct nfit_set_info_map2 *map0 = m0;
2221 const struct nfit_set_info_map2 *map1 = m1;
2223 if (map0->region_offset < map1->region_offset)
2225 else if (map0->region_offset > map1->region_offset)
2230 /* Retrieve the nth entry referencing this spa */
2231 static struct acpi_nfit_memory_map *memdev_from_spa(
2232 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2234 struct nfit_memdev *nfit_memdev;
2236 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2237 if (nfit_memdev->memdev->range_index == range_index)
2239 return nfit_memdev->memdev;
2243 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2244 struct nd_region_desc *ndr_desc,
2245 struct acpi_nfit_system_address *spa)
2247 struct device *dev = acpi_desc->dev;
2248 struct nd_interleave_set *nd_set;
2249 u16 nr = ndr_desc->num_mappings;
2250 struct nfit_set_info2 *info2;
2251 struct nfit_set_info *info;
2254 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2257 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2259 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2263 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2267 for (i = 0; i < nr; i++) {
2268 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
2269 struct nfit_set_info_map *map = &info->mapping[i];
2270 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2271 struct nvdimm *nvdimm = mapping->nvdimm;
2272 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2273 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2274 spa->range_index, i);
2275 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2277 if (!memdev || !nfit_mem->dcr) {
2278 dev_err(dev, "%s: failed to find DCR\n", __func__);
2282 map->region_offset = memdev->region_offset;
2283 map->serial_number = dcr->serial_number;
2285 map2->region_offset = memdev->region_offset;
2286 map2->serial_number = dcr->serial_number;
2287 map2->vendor_id = dcr->vendor_id;
2288 map2->manufacturing_date = dcr->manufacturing_date;
2289 map2->manufacturing_location = dcr->manufacturing_location;
2292 /* v1.1 namespaces */
2293 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2295 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2297 /* v1.2 namespaces */
2298 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2300 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2302 /* support v1.1 namespaces created with the wrong sort order */
2303 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2304 cmp_map_compat, NULL);
2305 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2307 /* record the result of the sort for the mapping position */
2308 for (i = 0; i < nr; i++) {
2309 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2312 for (j = 0; j < nr; j++) {
2313 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2314 struct nvdimm *nvdimm = mapping->nvdimm;
2315 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2316 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2318 if (map2->serial_number == dcr->serial_number &&
2319 map2->vendor_id == dcr->vendor_id &&
2320 map2->manufacturing_date == dcr->manufacturing_date &&
2321 map2->manufacturing_location
2322 == dcr->manufacturing_location) {
2323 mapping->position = i;
2329 ndr_desc->nd_set = nd_set;
2330 devm_kfree(dev, info);
2331 devm_kfree(dev, info2);
2336 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2338 struct acpi_nfit_interleave *idt = mmio->idt;
2339 u32 sub_line_offset, line_index, line_offset;
2340 u64 line_no, table_skip_count, table_offset;
2342 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2343 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2344 line_offset = idt->line_offset[line_index]
2346 table_offset = table_skip_count * mmio->table_size;
2348 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2351 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
2353 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2354 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
2355 const u32 STATUS_MASK = 0x80000037;
2357 if (mmio->num_lines)
2358 offset = to_interleave_offset(offset, mmio);
2360 return readl(mmio->addr.base + offset) & STATUS_MASK;
2363 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2364 resource_size_t dpa, unsigned int len, unsigned int write)
2367 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2370 BCW_OFFSET_MASK = (1ULL << 48)-1,
2372 BCW_LEN_MASK = (1ULL << 8) - 1,
2376 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2377 len = len >> L1_CACHE_SHIFT;
2378 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2379 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2381 offset = nfit_blk->cmd_offset + mmio->size * bw;
2382 if (mmio->num_lines)
2383 offset = to_interleave_offset(offset, mmio);
2385 writeq(cmd, mmio->addr.base + offset);
2386 nvdimm_flush(nfit_blk->nd_region);
2388 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
2389 readq(mmio->addr.base + offset);
2392 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2393 resource_size_t dpa, void *iobuf, size_t len, int rw,
2396 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2397 unsigned int copied = 0;
2401 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2402 + lane * mmio->size;
2403 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2408 if (mmio->num_lines) {
2411 offset = to_interleave_offset(base_offset + copied,
2413 div_u64_rem(offset, mmio->line_size, &line_offset);
2414 c = min_t(size_t, len, mmio->line_size - line_offset);
2416 offset = base_offset + nfit_blk->bdw_offset;
2421 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2423 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2424 arch_invalidate_pmem((void __force *)
2425 mmio->addr.aperture + offset, c);
2427 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2435 nvdimm_flush(nfit_blk->nd_region);
2437 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2441 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2442 resource_size_t dpa, void *iobuf, u64 len, int rw)
2444 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2445 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2446 struct nd_region *nd_region = nfit_blk->nd_region;
2447 unsigned int lane, copied = 0;
2450 lane = nd_region_acquire_lane(nd_region);
2452 u64 c = min(len, mmio->size);
2454 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2455 iobuf + copied, c, rw, lane);
2462 nd_region_release_lane(nd_region, lane);
2467 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2468 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2471 mmio->num_lines = idt->line_count;
2472 mmio->line_size = idt->line_size;
2473 if (interleave_ways == 0)
2475 mmio->table_size = mmio->num_lines * interleave_ways
2482 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2483 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2485 struct nd_cmd_dimm_flags flags;
2488 memset(&flags, 0, sizeof(flags));
2489 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2490 sizeof(flags), NULL);
2492 if (rc >= 0 && flags.status == 0)
2493 nfit_blk->dimm_flags = flags.flags;
2494 else if (rc == -ENOTTY) {
2495 /* fall back to a conservative default */
2496 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2504 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2507 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2508 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2509 struct nfit_blk_mmio *mmio;
2510 struct nfit_blk *nfit_blk;
2511 struct nfit_mem *nfit_mem;
2512 struct nvdimm *nvdimm;
2515 nvdimm = nd_blk_region_to_dimm(ndbr);
2516 nfit_mem = nvdimm_provider_data(nvdimm);
2517 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2518 dev_dbg(dev, "missing%s%s%s\n",
2519 nfit_mem ? "" : " nfit_mem",
2520 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2521 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2525 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2528 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2529 nfit_blk->nd_region = to_nd_region(dev);
2531 /* map block aperture memory */
2532 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2533 mmio = &nfit_blk->mmio[BDW];
2534 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2535 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2536 if (!mmio->addr.base) {
2537 dev_dbg(dev, "%s failed to map bdw\n",
2538 nvdimm_name(nvdimm));
2541 mmio->size = nfit_mem->bdw->size;
2542 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2543 mmio->idt = nfit_mem->idt_bdw;
2544 mmio->spa = nfit_mem->spa_bdw;
2545 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2546 nfit_mem->memdev_bdw->interleave_ways);
2548 dev_dbg(dev, "%s failed to init bdw interleave\n",
2549 nvdimm_name(nvdimm));
2553 /* map block control memory */
2554 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2555 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2556 mmio = &nfit_blk->mmio[DCR];
2557 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2558 nfit_mem->spa_dcr->length);
2559 if (!mmio->addr.base) {
2560 dev_dbg(dev, "%s failed to map dcr\n",
2561 nvdimm_name(nvdimm));
2564 mmio->size = nfit_mem->dcr->window_size;
2565 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2566 mmio->idt = nfit_mem->idt_dcr;
2567 mmio->spa = nfit_mem->spa_dcr;
2568 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2569 nfit_mem->memdev_dcr->interleave_ways);
2571 dev_dbg(dev, "%s failed to init dcr interleave\n",
2572 nvdimm_name(nvdimm));
2576 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2578 dev_dbg(dev, "%s failed get DIMM flags\n",
2579 nvdimm_name(nvdimm));
2583 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2584 dev_warn(dev, "unable to guarantee persistence of writes\n");
2586 if (mmio->line_size == 0)
2589 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2590 + 8 > mmio->line_size) {
2591 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2593 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2594 + 8 > mmio->line_size) {
2595 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2602 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2603 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2605 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2606 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2609 cmd->address = spa->address;
2610 cmd->length = spa->length;
2611 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2612 sizeof(*cmd), &cmd_rc);
2618 static int ars_start(struct acpi_nfit_desc *acpi_desc,
2619 struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
2623 struct nd_cmd_ars_start ars_start;
2624 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2625 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2627 memset(&ars_start, 0, sizeof(ars_start));
2628 ars_start.address = spa->address;
2629 ars_start.length = spa->length;
2630 if (req_type == ARS_REQ_SHORT)
2631 ars_start.flags = ND_ARS_RETURN_PREV_DATA;
2632 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2633 ars_start.type = ND_ARS_PERSISTENT;
2634 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2635 ars_start.type = ND_ARS_VOLATILE;
2639 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2640 sizeof(ars_start), &cmd_rc);
2647 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2650 struct nd_cmd_ars_start ars_start;
2651 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2652 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2654 memset(&ars_start, 0, sizeof(ars_start));
2655 ars_start.address = ars_status->restart_address;
2656 ars_start.length = ars_status->restart_length;
2657 ars_start.type = ars_status->type;
2658 ars_start.flags = acpi_desc->ars_start_flags;
2659 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2660 sizeof(ars_start), &cmd_rc);
2666 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2668 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2669 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2672 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2673 acpi_desc->max_ars, &cmd_rc);
2679 static void ars_complete(struct acpi_nfit_desc *acpi_desc,
2680 struct nfit_spa *nfit_spa)
2682 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2683 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2684 struct nd_region *nd_region = nfit_spa->nd_region;
2687 lockdep_assert_held(&acpi_desc->init_mutex);
2689 * Only advance the ARS state for ARS runs initiated by the
2690 * kernel, ignore ARS results from BIOS initiated runs for scrub
2691 * completion tracking.
2693 if (acpi_desc->scrub_spa != nfit_spa)
2696 if ((ars_status->address >= spa->address && ars_status->address
2697 < spa->address + spa->length)
2698 || (ars_status->address < spa->address)) {
2700 * Assume that if a scrub starts at an offset from the
2701 * start of nfit_spa that we are in the continuation
2704 * Otherwise, if the scrub covers the spa range, mark
2705 * any pending request complete.
2707 if (ars_status->address + ars_status->length
2708 >= spa->address + spa->length)
2715 acpi_desc->scrub_spa = NULL;
2717 dev = nd_region_dev(nd_region);
2718 nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
2720 dev = acpi_desc->dev;
2721 dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
2724 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
2726 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2727 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2732 * First record starts at 44 byte offset from the start of the
2735 if (ars_status->out_length < 44)
2737 for (i = 0; i < ars_status->num_records; i++) {
2738 /* only process full records */
2739 if (ars_status->out_length
2740 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2742 rc = nvdimm_bus_add_badrange(nvdimm_bus,
2743 ars_status->records[i].err_address,
2744 ars_status->records[i].length);
2748 if (i < ars_status->num_records)
2749 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2754 static void acpi_nfit_remove_resource(void *data)
2756 struct resource *res = data;
2758 remove_resource(res);
2761 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2762 struct nd_region_desc *ndr_desc)
2764 struct resource *res, *nd_res = ndr_desc->res;
2767 /* No operation if the region is already registered as PMEM */
2768 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2769 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2770 if (is_pmem == REGION_INTERSECTS)
2773 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2777 res->name = "Persistent Memory";
2778 res->start = nd_res->start;
2779 res->end = nd_res->end;
2780 res->flags = IORESOURCE_MEM;
2781 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2783 ret = insert_resource(&iomem_resource, res);
2787 ret = devm_add_action_or_reset(acpi_desc->dev,
2788 acpi_nfit_remove_resource,
2796 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2797 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2798 struct acpi_nfit_memory_map *memdev,
2799 struct nfit_spa *nfit_spa)
2801 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2802 memdev->device_handle);
2803 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2804 struct nd_blk_region_desc *ndbr_desc;
2805 struct nfit_mem *nfit_mem;
2809 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2810 spa->range_index, memdev->device_handle);
2814 mapping->nvdimm = nvdimm;
2815 switch (nfit_spa_type(spa)) {
2817 case NFIT_SPA_VOLATILE:
2818 mapping->start = memdev->address;
2819 mapping->size = memdev->region_size;
2822 nfit_mem = nvdimm_provider_data(nvdimm);
2823 if (!nfit_mem || !nfit_mem->bdw) {
2824 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2825 spa->range_index, nvdimm_name(nvdimm));
2829 mapping->size = nfit_mem->bdw->capacity;
2830 mapping->start = nfit_mem->bdw->start_address;
2831 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2832 ndr_desc->mapping = mapping;
2833 ndr_desc->num_mappings = 1;
2834 ndbr_desc = to_blk_region_desc(ndr_desc);
2835 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2836 ndbr_desc->do_io = acpi_desc->blk_do_io;
2837 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2840 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2842 if (!nfit_spa->nd_region)
2850 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2852 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2853 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2854 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2855 nfit_spa_type(spa) == NFIT_SPA_PCD);
2858 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2860 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2861 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2862 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2865 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2866 struct nfit_spa *nfit_spa)
2868 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2869 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2870 struct nd_blk_region_desc ndbr_desc;
2871 struct nd_region_desc *ndr_desc;
2872 struct nfit_memdev *nfit_memdev;
2873 struct nvdimm_bus *nvdimm_bus;
2874 struct resource res;
2877 if (nfit_spa->nd_region)
2880 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2881 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
2885 memset(&res, 0, sizeof(res));
2886 memset(&mappings, 0, sizeof(mappings));
2887 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2888 res.start = spa->address;
2889 res.end = res.start + spa->length - 1;
2890 ndr_desc = &ndbr_desc.ndr_desc;
2891 ndr_desc->res = &res;
2892 ndr_desc->provider_data = nfit_spa;
2893 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2894 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2895 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2896 spa->proximity_domain);
2898 ndr_desc->numa_node = NUMA_NO_NODE;
2901 * Persistence domain bits are hierarchical, if
2902 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
2903 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
2905 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2906 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2907 else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2908 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2910 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2911 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2912 struct nd_mapping_desc *mapping;
2914 if (memdev->range_index != spa->range_index)
2916 if (count >= ND_MAX_MAPPINGS) {
2917 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2918 spa->range_index, ND_MAX_MAPPINGS);
2921 mapping = &mappings[count++];
2922 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2928 ndr_desc->mapping = mappings;
2929 ndr_desc->num_mappings = count;
2930 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2934 nvdimm_bus = acpi_desc->nvdimm_bus;
2935 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2936 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2938 dev_warn(acpi_desc->dev,
2939 "failed to insert pmem resource to iomem: %d\n",
2944 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2946 if (!nfit_spa->nd_region)
2948 } else if (nfit_spa_is_volatile(spa)) {
2949 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2951 if (!nfit_spa->nd_region)
2953 } else if (nfit_spa_is_virtual(spa)) {
2954 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2956 if (!nfit_spa->nd_region)
2962 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2963 nfit_spa->spa->range_index);
2967 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
2969 struct device *dev = acpi_desc->dev;
2970 struct nd_cmd_ars_status *ars_status;
2972 if (acpi_desc->ars_status) {
2973 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
2977 ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
2980 acpi_desc->ars_status = ars_status;
2984 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
2988 if (ars_status_alloc(acpi_desc))
2991 rc = ars_get_status(acpi_desc);
2993 if (rc < 0 && rc != -ENOSPC)
2996 if (ars_status_process_records(acpi_desc))
2997 dev_err(acpi_desc->dev, "Failed to process ARS records\n");
3002 static int ars_register(struct acpi_nfit_desc *acpi_desc,
3003 struct nfit_spa *nfit_spa)
3007 if (no_init_ars || test_bit(ARS_FAILED, &nfit_spa->ars_state))
3008 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3010 set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3011 set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
3013 switch (acpi_nfit_query_poison(acpi_desc)) {
3016 rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
3017 /* shouldn't happen, try again later */
3021 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3024 clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3025 rc = acpi_nfit_query_poison(acpi_desc);
3028 acpi_desc->scrub_spa = nfit_spa;
3029 ars_complete(acpi_desc, nfit_spa);
3031 * If ars_complete() says we didn't complete the
3032 * short scrub, we'll try again with a long
3035 acpi_desc->scrub_spa = NULL;
3041 * BIOS was using ARS, wait for it to complete (or
3042 * resources to become available) and then perform our
3047 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3051 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3054 static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
3056 struct nfit_spa *nfit_spa;
3058 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3059 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3061 ars_complete(acpi_desc, nfit_spa);
3065 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
3068 unsigned int tmo = acpi_desc->scrub_tmo;
3069 struct device *dev = acpi_desc->dev;
3070 struct nfit_spa *nfit_spa;
3072 lockdep_assert_held(&acpi_desc->init_mutex);
3074 if (acpi_desc->cancel)
3077 if (query_rc == -EBUSY) {
3078 dev_dbg(dev, "ARS: ARS busy\n");
3079 return min(30U * 60U, tmo * 2);
3081 if (query_rc == -ENOSPC) {
3082 dev_dbg(dev, "ARS: ARS continue\n");
3083 ars_continue(acpi_desc);
3086 if (query_rc && query_rc != -EAGAIN) {
3087 unsigned long long addr, end;
3089 addr = acpi_desc->ars_status->address;
3090 end = addr + acpi_desc->ars_status->length;
3091 dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
3095 ars_complete_all(acpi_desc);
3096 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3097 enum nfit_ars_state req_type;
3100 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3103 /* prefer short ARS requests first */
3104 if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
3105 req_type = ARS_REQ_SHORT;
3106 else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
3107 req_type = ARS_REQ_LONG;
3110 rc = ars_start(acpi_desc, nfit_spa, req_type);
3112 dev = nd_region_dev(nfit_spa->nd_region);
3113 dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
3114 nfit_spa->spa->range_index,
3115 req_type == ARS_REQ_SHORT ? "short" : "long",
3118 * Hmm, we raced someone else starting ARS? Try again in
3124 dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
3125 "scrub start while range %d active\n",
3126 acpi_desc->scrub_spa->spa->range_index);
3127 clear_bit(req_type, &nfit_spa->ars_state);
3128 acpi_desc->scrub_spa = nfit_spa;
3130 * Consider this spa last for future scrub
3133 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
3137 dev_err(dev, "ARS: range %d ARS failed (%d)\n",
3138 nfit_spa->spa->range_index, rc);
3139 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3144 static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
3146 lockdep_assert_held(&acpi_desc->init_mutex);
3148 acpi_desc->scrub_busy = 1;
3149 /* note this should only be set from within the workqueue */
3151 acpi_desc->scrub_tmo = tmo;
3152 queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
3155 static void sched_ars(struct acpi_nfit_desc *acpi_desc)
3157 __sched_ars(acpi_desc, 0);
3160 static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
3162 lockdep_assert_held(&acpi_desc->init_mutex);
3164 acpi_desc->scrub_busy = 0;
3165 acpi_desc->scrub_count++;
3166 if (acpi_desc->scrub_count_state)
3167 sysfs_notify_dirent(acpi_desc->scrub_count_state);
3170 static void acpi_nfit_scrub(struct work_struct *work)
3172 struct acpi_nfit_desc *acpi_desc;
3176 acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
3177 mutex_lock(&acpi_desc->init_mutex);
3178 query_rc = acpi_nfit_query_poison(acpi_desc);
3179 tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
3181 __sched_ars(acpi_desc, tmo);
3183 notify_ars_done(acpi_desc);
3184 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3185 mutex_unlock(&acpi_desc->init_mutex);
3188 static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
3189 struct nfit_spa *nfit_spa)
3191 int type = nfit_spa_type(nfit_spa->spa);
3192 struct nd_cmd_ars_cap ars_cap;
3195 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3196 memset(&ars_cap, 0, sizeof(ars_cap));
3197 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
3200 /* check that the supported scrub types match the spa type */
3201 if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
3202 & ND_ARS_VOLATILE) == 0)
3204 if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
3205 & ND_ARS_PERSISTENT) == 0)
3208 nfit_spa->max_ars = ars_cap.max_ars_out;
3209 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
3210 acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
3211 clear_bit(ARS_FAILED, &nfit_spa->ars_state);
3214 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3216 struct nfit_spa *nfit_spa;
3219 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3220 switch (nfit_spa_type(nfit_spa->spa)) {
3221 case NFIT_SPA_VOLATILE:
3223 acpi_nfit_init_ars(acpi_desc, nfit_spa);
3228 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3229 switch (nfit_spa_type(nfit_spa->spa)) {
3230 case NFIT_SPA_VOLATILE:
3232 /* register regions and kick off initial ARS run */
3233 rc = ars_register(acpi_desc, nfit_spa);
3238 /* nothing to register */
3241 case NFIT_SPA_VDISK:
3243 case NFIT_SPA_PDISK:
3245 /* register known regions that don't support ARS */
3246 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3251 /* don't register unknown regions */
3255 sched_ars(acpi_desc);
3259 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3260 struct nfit_table_prev *prev)
3262 struct device *dev = acpi_desc->dev;
3264 if (!list_empty(&prev->spas) ||
3265 !list_empty(&prev->memdevs) ||
3266 !list_empty(&prev->dcrs) ||
3267 !list_empty(&prev->bdws) ||
3268 !list_empty(&prev->idts) ||
3269 !list_empty(&prev->flushes)) {
3270 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3276 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3278 struct device *dev = acpi_desc->dev;
3279 struct kernfs_node *nfit;
3280 struct device *bus_dev;
3282 if (!ars_supported(acpi_desc->nvdimm_bus))
3285 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3286 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3288 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3291 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3293 if (!acpi_desc->scrub_count_state) {
3294 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3301 static void acpi_nfit_unregister(void *data)
3303 struct acpi_nfit_desc *acpi_desc = data;
3305 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
3308 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
3310 struct device *dev = acpi_desc->dev;
3311 struct nfit_table_prev prev;
3315 if (!acpi_desc->nvdimm_bus) {
3316 acpi_nfit_init_dsms(acpi_desc);
3318 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3319 &acpi_desc->nd_desc);
3320 if (!acpi_desc->nvdimm_bus)
3323 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
3328 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3332 /* register this acpi_desc for mce notifications */
3333 mutex_lock(&acpi_desc_lock);
3334 list_add_tail(&acpi_desc->list, &acpi_descs);
3335 mutex_unlock(&acpi_desc_lock);
3338 mutex_lock(&acpi_desc->init_mutex);
3340 INIT_LIST_HEAD(&prev.spas);
3341 INIT_LIST_HEAD(&prev.memdevs);
3342 INIT_LIST_HEAD(&prev.dcrs);
3343 INIT_LIST_HEAD(&prev.bdws);
3344 INIT_LIST_HEAD(&prev.idts);
3345 INIT_LIST_HEAD(&prev.flushes);
3347 list_cut_position(&prev.spas, &acpi_desc->spas,
3348 acpi_desc->spas.prev);
3349 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3350 acpi_desc->memdevs.prev);
3351 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3352 acpi_desc->dcrs.prev);
3353 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3354 acpi_desc->bdws.prev);
3355 list_cut_position(&prev.idts, &acpi_desc->idts,
3356 acpi_desc->idts.prev);
3357 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3358 acpi_desc->flushes.prev);
3361 while (!IS_ERR_OR_NULL(data))
3362 data = add_table(acpi_desc, &prev, data, end);
3365 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
3370 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3374 rc = nfit_mem_init(acpi_desc);
3378 rc = acpi_nfit_register_dimms(acpi_desc);
3382 rc = acpi_nfit_register_regions(acpi_desc);
3385 mutex_unlock(&acpi_desc->init_mutex);
3388 EXPORT_SYMBOL_GPL(acpi_nfit_init);
3390 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3392 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3393 struct device *dev = acpi_desc->dev;
3395 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3399 /* Bounce the init_mutex to complete initial registration */
3400 mutex_lock(&acpi_desc->init_mutex);
3401 mutex_unlock(&acpi_desc->init_mutex);
3406 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3407 struct nvdimm *nvdimm, unsigned int cmd)
3409 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3413 if (cmd != ND_CMD_ARS_START)
3417 * The kernel and userspace may race to initiate a scrub, but
3418 * the scrub thread is prepared to lose that initial race. It
3419 * just needs guarantees that any ARS it initiates are not
3420 * interrupted by any intervening start requests from userspace.
3422 if (work_busy(&acpi_desc->dwork.work))
3428 /* prevent security commands from being issued via ioctl */
3429 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3430 struct nvdimm *nvdimm, unsigned int cmd, void *buf)
3432 struct nd_cmd_pkg *call_pkg = buf;
3435 if (nvdimm && cmd == ND_CMD_CALL &&
3436 call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
3437 func = call_pkg->nd_command;
3438 if ((1 << func) & NVDIMM_INTEL_SECURITY_CMDMASK)
3442 return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
3445 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
3446 enum nfit_ars_state req_type)
3448 struct device *dev = acpi_desc->dev;
3449 int scheduled = 0, busy = 0;
3450 struct nfit_spa *nfit_spa;
3452 mutex_lock(&acpi_desc->init_mutex);
3453 if (acpi_desc->cancel) {
3454 mutex_unlock(&acpi_desc->init_mutex);
3458 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3459 int type = nfit_spa_type(nfit_spa->spa);
3461 if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
3463 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3466 if (test_and_set_bit(req_type, &nfit_spa->ars_state))
3472 sched_ars(acpi_desc);
3473 dev_dbg(dev, "ars_scan triggered\n");
3475 mutex_unlock(&acpi_desc->init_mutex);
3484 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3486 struct nvdimm_bus_descriptor *nd_desc;
3488 dev_set_drvdata(dev, acpi_desc);
3489 acpi_desc->dev = dev;
3490 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3491 nd_desc = &acpi_desc->nd_desc;
3492 nd_desc->provider_name = "ACPI.NFIT";
3493 nd_desc->module = THIS_MODULE;
3494 nd_desc->ndctl = acpi_nfit_ctl;
3495 nd_desc->flush_probe = acpi_nfit_flush_probe;
3496 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3497 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3499 INIT_LIST_HEAD(&acpi_desc->spas);
3500 INIT_LIST_HEAD(&acpi_desc->dcrs);
3501 INIT_LIST_HEAD(&acpi_desc->bdws);
3502 INIT_LIST_HEAD(&acpi_desc->idts);
3503 INIT_LIST_HEAD(&acpi_desc->flushes);
3504 INIT_LIST_HEAD(&acpi_desc->memdevs);
3505 INIT_LIST_HEAD(&acpi_desc->dimms);
3506 INIT_LIST_HEAD(&acpi_desc->list);
3507 mutex_init(&acpi_desc->init_mutex);
3508 acpi_desc->scrub_tmo = 1;
3509 INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
3511 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3513 static void acpi_nfit_put_table(void *table)
3515 acpi_put_table(table);
3518 void acpi_nfit_shutdown(void *data)
3520 struct acpi_nfit_desc *acpi_desc = data;
3521 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3524 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3527 mutex_lock(&acpi_desc_lock);
3528 list_del(&acpi_desc->list);
3529 mutex_unlock(&acpi_desc_lock);
3531 mutex_lock(&acpi_desc->init_mutex);
3532 acpi_desc->cancel = 1;
3533 cancel_delayed_work_sync(&acpi_desc->dwork);
3534 mutex_unlock(&acpi_desc->init_mutex);
3537 * Bounce the nvdimm bus lock to make sure any in-flight
3538 * acpi_nfit_ars_rescan() submissions have had a chance to
3539 * either submit or see ->cancel set.
3541 device_lock(bus_dev);
3542 device_unlock(bus_dev);
3544 flush_workqueue(nfit_wq);
3546 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3548 static int acpi_nfit_add(struct acpi_device *adev)
3550 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3551 struct acpi_nfit_desc *acpi_desc;
3552 struct device *dev = &adev->dev;
3553 struct acpi_table_header *tbl;
3554 acpi_status status = AE_OK;
3558 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3559 if (ACPI_FAILURE(status)) {
3560 /* The NVDIMM root device allows OS to trigger enumeration of
3561 * NVDIMMs through NFIT at boot time and re-enumeration at
3562 * root level via the _FIT method during runtime.
3563 * This is ok to return 0 here, we could have an nvdimm
3564 * hotplugged later and evaluate _FIT method which returns
3565 * data in the format of a series of NFIT Structures.
3567 dev_dbg(dev, "failed to find NFIT at startup\n");
3571 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3576 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3579 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3581 /* Save the acpi header for exporting the revision via sysfs */
3582 acpi_desc->acpi_header = *tbl;
3584 /* Evaluate _FIT and override with that if present */
3585 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3586 if (ACPI_SUCCESS(status) && buf.length > 0) {
3587 union acpi_object *obj = buf.pointer;
3589 if (obj->type == ACPI_TYPE_BUFFER)
3590 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3591 obj->buffer.length);
3593 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3597 /* skip over the lead-in header table */
3598 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3599 + sizeof(struct acpi_table_nfit),
3600 sz - sizeof(struct acpi_table_nfit));
3604 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3607 static int acpi_nfit_remove(struct acpi_device *adev)
3609 /* see acpi_nfit_unregister */
3613 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3615 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3616 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3617 union acpi_object *obj;
3622 /* dev->driver may be null if we're being removed */
3623 dev_dbg(dev, "no driver found for dev\n");
3628 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3631 acpi_nfit_desc_init(acpi_desc, dev);
3634 * Finish previous registration before considering new
3637 flush_workqueue(nfit_wq);
3641 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3642 if (ACPI_FAILURE(status)) {
3643 dev_err(dev, "failed to evaluate _FIT\n");
3648 if (obj->type == ACPI_TYPE_BUFFER) {
3649 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3650 obj->buffer.length);
3652 dev_err(dev, "failed to merge updated NFIT\n");
3654 dev_err(dev, "Invalid _FIT\n");
3658 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3660 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3662 if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
3663 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
3665 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
3668 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3670 dev_dbg(dev, "event: 0x%x\n", event);
3673 case NFIT_NOTIFY_UPDATE:
3674 return acpi_nfit_update_notify(dev, handle);
3675 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3676 return acpi_nfit_uc_error_notify(dev, handle);
3681 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3683 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3685 device_lock(&adev->dev);
3686 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3687 device_unlock(&adev->dev);
3690 static const struct acpi_device_id acpi_nfit_ids[] = {
3694 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3696 static struct acpi_driver acpi_nfit_driver = {
3697 .name = KBUILD_MODNAME,
3698 .ids = acpi_nfit_ids,
3700 .add = acpi_nfit_add,
3701 .remove = acpi_nfit_remove,
3702 .notify = acpi_nfit_notify,
3706 static __init int nfit_init(void)
3710 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3711 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3712 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3713 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3714 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3715 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3716 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3717 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
3719 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3720 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3721 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3722 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3723 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3724 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3725 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3726 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3727 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3728 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3729 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3730 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3731 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3733 nfit_wq = create_singlethread_workqueue("nfit");
3737 nfit_mce_register();
3738 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3740 nfit_mce_unregister();
3741 destroy_workqueue(nfit_wq);
3748 static __exit void nfit_exit(void)
3750 nfit_mce_unregister();
3751 acpi_bus_unregister_driver(&acpi_nfit_driver);
3752 destroy_workqueue(nfit_wq);
3753 WARN_ON(!list_empty(&acpi_descs));
3756 module_init(nfit_init);
3757 module_exit(nfit_exit);
3758 MODULE_LICENSE("GPL v2");
3759 MODULE_AUTHOR("Intel Corporation");