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 && 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 /* In the !call_pkg case, bus commands == bus functions */
431 /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
432 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
436 * Force function number validation to fail since 0 is never
437 * published as a valid function in dsm_mask.
442 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
443 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
445 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
446 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
447 union acpi_object in_obj, in_buf, *out_obj;
448 const struct nd_cmd_desc *desc = NULL;
449 struct device *dev = acpi_desc->dev;
450 struct nd_cmd_pkg *call_pkg = NULL;
451 const char *cmd_name, *dimm_name;
452 unsigned long cmd_mask, dsm_mask;
453 u32 offset, fw_status = 0;
461 if (cmd == ND_CMD_CALL)
463 func = cmd_to_func(nfit_mem, cmd, call_pkg);
468 struct acpi_device *adev = nfit_mem->adev;
473 dimm_name = nvdimm_name(nvdimm);
474 cmd_name = nvdimm_cmd_name(cmd);
475 cmd_mask = nvdimm_cmd_mask(nvdimm);
476 dsm_mask = nfit_mem->dsm_mask;
477 desc = nd_cmd_dimm_desc(cmd);
478 guid = to_nfit_uuid(nfit_mem->family);
479 handle = adev->handle;
481 struct acpi_device *adev = to_acpi_dev(acpi_desc);
483 cmd_name = nvdimm_bus_cmd_name(cmd);
484 cmd_mask = nd_desc->cmd_mask;
485 dsm_mask = nd_desc->bus_dsm_mask;
486 desc = nd_cmd_bus_desc(cmd);
487 guid = to_nfit_uuid(NFIT_DEV_BUS);
488 handle = adev->handle;
492 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
496 * Check for a valid command. For ND_CMD_CALL, we also have to
497 * make sure that the DSM function is supported.
499 if (cmd == ND_CMD_CALL && !test_bit(func, &dsm_mask))
501 else if (!test_bit(cmd, &cmd_mask))
504 in_obj.type = ACPI_TYPE_PACKAGE;
505 in_obj.package.count = 1;
506 in_obj.package.elements = &in_buf;
507 in_buf.type = ACPI_TYPE_BUFFER;
508 in_buf.buffer.pointer = buf;
509 in_buf.buffer.length = 0;
511 /* libnvdimm has already validated the input envelope */
512 for (i = 0; i < desc->in_num; i++)
513 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
517 /* skip over package wrapper */
518 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
519 in_buf.buffer.length = call_pkg->nd_size_in;
522 dev_dbg(dev, "%s cmd: %d: func: %d input length: %d\n",
523 dimm_name, cmd, func, in_buf.buffer.length);
524 if (payload_dumpable(nvdimm, func))
525 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
526 in_buf.buffer.pointer,
527 min_t(u32, 256, in_buf.buffer.length), true);
529 /* call the BIOS, prefer the named methods over _DSM if available */
530 if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE
531 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
532 out_obj = acpi_label_info(handle);
533 else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA
534 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
535 struct nd_cmd_get_config_data_hdr *p = buf;
537 out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
538 } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
539 && test_bit(NFIT_MEM_LSW, &nfit_mem->flags)) {
540 struct nd_cmd_set_config_hdr *p = buf;
542 out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
548 revid = nfit_dsm_revid(nfit_mem->family, func);
551 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
555 dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
560 call_pkg->nd_fw_size = out_obj->buffer.length;
561 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
562 out_obj->buffer.pointer,
563 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
567 * Need to support FW function w/o known size in advance.
568 * Caller can determine required size based upon nd_fw_size.
569 * If we return an error (like elsewhere) then caller wouldn't
570 * be able to rely upon data returned to make calculation.
577 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
578 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
579 dimm_name, cmd_name, out_obj->type);
584 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
585 cmd_name, out_obj->buffer.length);
586 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
587 out_obj->buffer.pointer,
588 min_t(u32, 128, out_obj->buffer.length), true);
590 for (i = 0, offset = 0; i < desc->out_num; i++) {
591 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
592 (u32 *) out_obj->buffer.pointer,
593 out_obj->buffer.length - offset);
595 if (offset + out_size > out_obj->buffer.length) {
596 dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
597 dimm_name, cmd_name, i);
601 if (in_buf.buffer.length + offset + out_size > buf_len) {
602 dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
603 dimm_name, cmd_name, i);
607 memcpy(buf + in_buf.buffer.length + offset,
608 out_obj->buffer.pointer + offset, out_size);
613 * Set fw_status for all the commands with a known format to be
614 * later interpreted by xlat_status().
616 if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
617 && cmd <= ND_CMD_CLEAR_ERROR)
618 || (nvdimm && cmd >= ND_CMD_SMART
619 && cmd <= ND_CMD_VENDOR)))
620 fw_status = *(u32 *) out_obj->buffer.pointer;
622 if (offset + in_buf.buffer.length < buf_len) {
625 * status valid, return the number of bytes left
626 * unfilled in the output buffer
628 rc = buf_len - offset - in_buf.buffer.length;
630 *cmd_rc = xlat_status(nvdimm, buf, cmd,
633 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
634 __func__, dimm_name, cmd_name, buf_len,
641 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
649 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
651 static const char *spa_type_name(u16 type)
653 static const char *to_name[] = {
654 [NFIT_SPA_VOLATILE] = "volatile",
655 [NFIT_SPA_PM] = "pmem",
656 [NFIT_SPA_DCR] = "dimm-control-region",
657 [NFIT_SPA_BDW] = "block-data-window",
658 [NFIT_SPA_VDISK] = "volatile-disk",
659 [NFIT_SPA_VCD] = "volatile-cd",
660 [NFIT_SPA_PDISK] = "persistent-disk",
661 [NFIT_SPA_PCD] = "persistent-cd",
665 if (type > NFIT_SPA_PCD)
668 return to_name[type];
671 int nfit_spa_type(struct acpi_nfit_system_address *spa)
675 for (i = 0; i < NFIT_UUID_MAX; i++)
676 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
681 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
682 struct nfit_table_prev *prev,
683 struct acpi_nfit_system_address *spa)
685 struct device *dev = acpi_desc->dev;
686 struct nfit_spa *nfit_spa;
688 if (spa->header.length != sizeof(*spa))
691 list_for_each_entry(nfit_spa, &prev->spas, list) {
692 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
693 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
698 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
702 INIT_LIST_HEAD(&nfit_spa->list);
703 memcpy(nfit_spa->spa, spa, sizeof(*spa));
704 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
705 dev_dbg(dev, "spa index: %d type: %s\n",
707 spa_type_name(nfit_spa_type(spa)));
711 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
712 struct nfit_table_prev *prev,
713 struct acpi_nfit_memory_map *memdev)
715 struct device *dev = acpi_desc->dev;
716 struct nfit_memdev *nfit_memdev;
718 if (memdev->header.length != sizeof(*memdev))
721 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
722 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
723 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
727 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
731 INIT_LIST_HEAD(&nfit_memdev->list);
732 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
733 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
734 dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
735 memdev->device_handle, memdev->range_index,
736 memdev->region_index, memdev->flags);
740 int nfit_get_smbios_id(u32 device_handle, u16 *flags)
742 struct acpi_nfit_memory_map *memdev;
743 struct acpi_nfit_desc *acpi_desc;
744 struct nfit_mem *nfit_mem;
747 mutex_lock(&acpi_desc_lock);
748 list_for_each_entry(acpi_desc, &acpi_descs, list) {
749 mutex_lock(&acpi_desc->init_mutex);
750 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
751 memdev = __to_nfit_memdev(nfit_mem);
752 if (memdev->device_handle == device_handle) {
753 *flags = memdev->flags;
754 physical_id = memdev->physical_id;
755 mutex_unlock(&acpi_desc->init_mutex);
756 mutex_unlock(&acpi_desc_lock);
760 mutex_unlock(&acpi_desc->init_mutex);
762 mutex_unlock(&acpi_desc_lock);
766 EXPORT_SYMBOL_GPL(nfit_get_smbios_id);
769 * An implementation may provide a truncated control region if no block windows
772 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
774 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
779 return offsetof(struct acpi_nfit_control_region, window_size);
782 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
783 struct nfit_table_prev *prev,
784 struct acpi_nfit_control_region *dcr)
786 struct device *dev = acpi_desc->dev;
787 struct nfit_dcr *nfit_dcr;
789 if (!sizeof_dcr(dcr))
792 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
793 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
794 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
798 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
802 INIT_LIST_HEAD(&nfit_dcr->list);
803 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
804 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
805 dev_dbg(dev, "dcr index: %d windows: %d\n",
806 dcr->region_index, dcr->windows);
810 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
811 struct nfit_table_prev *prev,
812 struct acpi_nfit_data_region *bdw)
814 struct device *dev = acpi_desc->dev;
815 struct nfit_bdw *nfit_bdw;
817 if (bdw->header.length != sizeof(*bdw))
819 list_for_each_entry(nfit_bdw, &prev->bdws, list)
820 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
821 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
825 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
829 INIT_LIST_HEAD(&nfit_bdw->list);
830 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
831 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
832 dev_dbg(dev, "bdw dcr: %d windows: %d\n",
833 bdw->region_index, bdw->windows);
837 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
839 if (idt->header.length < sizeof(*idt))
841 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
844 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
845 struct nfit_table_prev *prev,
846 struct acpi_nfit_interleave *idt)
848 struct device *dev = acpi_desc->dev;
849 struct nfit_idt *nfit_idt;
851 if (!sizeof_idt(idt))
854 list_for_each_entry(nfit_idt, &prev->idts, list) {
855 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
858 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
859 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
864 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
868 INIT_LIST_HEAD(&nfit_idt->list);
869 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
870 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
871 dev_dbg(dev, "idt index: %d num_lines: %d\n",
872 idt->interleave_index, idt->line_count);
876 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
878 if (flush->header.length < sizeof(*flush))
880 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
883 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
884 struct nfit_table_prev *prev,
885 struct acpi_nfit_flush_address *flush)
887 struct device *dev = acpi_desc->dev;
888 struct nfit_flush *nfit_flush;
890 if (!sizeof_flush(flush))
893 list_for_each_entry(nfit_flush, &prev->flushes, list) {
894 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
897 if (memcmp(nfit_flush->flush, flush,
898 sizeof_flush(flush)) == 0) {
899 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
904 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
905 + sizeof_flush(flush), GFP_KERNEL);
908 INIT_LIST_HEAD(&nfit_flush->list);
909 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
910 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
911 dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
912 flush->device_handle, flush->hint_count);
916 static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
917 struct acpi_nfit_capabilities *pcap)
919 struct device *dev = acpi_desc->dev;
922 mask = (1 << (pcap->highest_capability + 1)) - 1;
923 acpi_desc->platform_cap = pcap->capabilities & mask;
924 dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
928 static void *add_table(struct acpi_nfit_desc *acpi_desc,
929 struct nfit_table_prev *prev, void *table, const void *end)
931 struct device *dev = acpi_desc->dev;
932 struct acpi_nfit_header *hdr;
933 void *err = ERR_PTR(-ENOMEM);
940 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
946 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
947 if (!add_spa(acpi_desc, prev, table))
950 case ACPI_NFIT_TYPE_MEMORY_MAP:
951 if (!add_memdev(acpi_desc, prev, table))
954 case ACPI_NFIT_TYPE_CONTROL_REGION:
955 if (!add_dcr(acpi_desc, prev, table))
958 case ACPI_NFIT_TYPE_DATA_REGION:
959 if (!add_bdw(acpi_desc, prev, table))
962 case ACPI_NFIT_TYPE_INTERLEAVE:
963 if (!add_idt(acpi_desc, prev, table))
966 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
967 if (!add_flush(acpi_desc, prev, table))
970 case ACPI_NFIT_TYPE_SMBIOS:
971 dev_dbg(dev, "smbios\n");
973 case ACPI_NFIT_TYPE_CAPABILITIES:
974 if (!add_platform_cap(acpi_desc, table))
978 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
982 return table + hdr->length;
985 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
986 struct nfit_mem *nfit_mem)
988 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
989 u16 dcr = nfit_mem->dcr->region_index;
990 struct nfit_spa *nfit_spa;
992 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
993 u16 range_index = nfit_spa->spa->range_index;
994 int type = nfit_spa_type(nfit_spa->spa);
995 struct nfit_memdev *nfit_memdev;
997 if (type != NFIT_SPA_BDW)
1000 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1001 if (nfit_memdev->memdev->range_index != range_index)
1003 if (nfit_memdev->memdev->device_handle != device_handle)
1005 if (nfit_memdev->memdev->region_index != dcr)
1008 nfit_mem->spa_bdw = nfit_spa->spa;
1013 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
1014 nfit_mem->spa_dcr->range_index);
1015 nfit_mem->bdw = NULL;
1018 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
1019 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
1021 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
1022 struct nfit_memdev *nfit_memdev;
1023 struct nfit_bdw *nfit_bdw;
1024 struct nfit_idt *nfit_idt;
1025 u16 idt_idx, range_index;
1027 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
1028 if (nfit_bdw->bdw->region_index != dcr)
1030 nfit_mem->bdw = nfit_bdw->bdw;
1037 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
1039 if (!nfit_mem->spa_bdw)
1042 range_index = nfit_mem->spa_bdw->range_index;
1043 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1044 if (nfit_memdev->memdev->range_index != range_index ||
1045 nfit_memdev->memdev->region_index != dcr)
1047 nfit_mem->memdev_bdw = nfit_memdev->memdev;
1048 idt_idx = nfit_memdev->memdev->interleave_index;
1049 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1050 if (nfit_idt->idt->interleave_index != idt_idx)
1052 nfit_mem->idt_bdw = nfit_idt->idt;
1059 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
1060 struct acpi_nfit_system_address *spa)
1062 struct nfit_mem *nfit_mem, *found;
1063 struct nfit_memdev *nfit_memdev;
1064 int type = spa ? nfit_spa_type(spa) : 0;
1076 * This loop runs in two modes, when a dimm is mapped the loop
1077 * adds memdev associations to an existing dimm, or creates a
1078 * dimm. In the unmapped dimm case this loop sweeps for memdev
1079 * instances with an invalid / zero range_index and adds those
1080 * dimms without spa associations.
1082 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1083 struct nfit_flush *nfit_flush;
1084 struct nfit_dcr *nfit_dcr;
1088 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
1090 if (!spa && nfit_memdev->memdev->range_index)
1093 dcr = nfit_memdev->memdev->region_index;
1094 device_handle = nfit_memdev->memdev->device_handle;
1095 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1096 if (__to_nfit_memdev(nfit_mem)->device_handle
1105 nfit_mem = devm_kzalloc(acpi_desc->dev,
1106 sizeof(*nfit_mem), GFP_KERNEL);
1109 INIT_LIST_HEAD(&nfit_mem->list);
1110 nfit_mem->acpi_desc = acpi_desc;
1111 list_add(&nfit_mem->list, &acpi_desc->dimms);
1114 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1115 if (nfit_dcr->dcr->region_index != dcr)
1118 * Record the control region for the dimm. For
1119 * the ACPI 6.1 case, where there are separate
1120 * control regions for the pmem vs blk
1121 * interfaces, be sure to record the extended
1125 nfit_mem->dcr = nfit_dcr->dcr;
1126 else if (nfit_mem->dcr->windows == 0
1127 && nfit_dcr->dcr->windows)
1128 nfit_mem->dcr = nfit_dcr->dcr;
1132 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
1133 struct acpi_nfit_flush_address *flush;
1136 if (nfit_flush->flush->device_handle != device_handle)
1138 nfit_mem->nfit_flush = nfit_flush;
1139 flush = nfit_flush->flush;
1140 nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
1142 sizeof(struct resource),
1144 if (!nfit_mem->flush_wpq)
1146 for (i = 0; i < flush->hint_count; i++) {
1147 struct resource *res = &nfit_mem->flush_wpq[i];
1149 res->start = flush->hint_address[i];
1150 res->end = res->start + 8 - 1;
1155 if (dcr && !nfit_mem->dcr) {
1156 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
1157 spa->range_index, dcr);
1161 if (type == NFIT_SPA_DCR) {
1162 struct nfit_idt *nfit_idt;
1165 /* multiple dimms may share a SPA when interleaved */
1166 nfit_mem->spa_dcr = spa;
1167 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1168 idt_idx = nfit_memdev->memdev->interleave_index;
1169 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1170 if (nfit_idt->idt->interleave_index != idt_idx)
1172 nfit_mem->idt_dcr = nfit_idt->idt;
1175 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
1176 } else if (type == NFIT_SPA_PM) {
1178 * A single dimm may belong to multiple SPA-PM
1179 * ranges, record at least one in addition to
1180 * any SPA-DCR range.
1182 nfit_mem->memdev_pmem = nfit_memdev->memdev;
1184 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1190 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
1192 struct nfit_mem *a = container_of(_a, typeof(*a), list);
1193 struct nfit_mem *b = container_of(_b, typeof(*b), list);
1194 u32 handleA, handleB;
1196 handleA = __to_nfit_memdev(a)->device_handle;
1197 handleB = __to_nfit_memdev(b)->device_handle;
1198 if (handleA < handleB)
1200 else if (handleA > handleB)
1205 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
1207 struct nfit_spa *nfit_spa;
1212 * For each SPA-DCR or SPA-PMEM address range find its
1213 * corresponding MEMDEV(s). From each MEMDEV find the
1214 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1215 * try to find a SPA-BDW and a corresponding BDW that references
1216 * the DCR. Throw it all into an nfit_mem object. Note, that
1217 * BDWs are optional.
1219 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1220 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
1226 * If a DIMM has failed to be mapped into SPA there will be no
1227 * SPA entries above. Find and register all the unmapped DIMMs
1228 * for reporting and recovery purposes.
1230 rc = __nfit_mem_init(acpi_desc, NULL);
1234 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
1239 static ssize_t bus_dsm_mask_show(struct device *dev,
1240 struct device_attribute *attr, char *buf)
1242 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1243 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1245 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
1247 static struct device_attribute dev_attr_bus_dsm_mask =
1248 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
1250 static ssize_t revision_show(struct device *dev,
1251 struct device_attribute *attr, char *buf)
1253 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1254 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1255 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1257 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
1259 static DEVICE_ATTR_RO(revision);
1261 static ssize_t hw_error_scrub_show(struct device *dev,
1262 struct device_attribute *attr, char *buf)
1264 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1265 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1266 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1268 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
1272 * The 'hw_error_scrub' attribute can have the following values written to it:
1273 * '0': Switch to the default mode where an exception will only insert
1274 * the address of the memory error into the poison and badblocks lists.
1275 * '1': Enable a full scrub to happen if an exception for a memory error is
1278 static ssize_t hw_error_scrub_store(struct device *dev,
1279 struct device_attribute *attr, const char *buf, size_t size)
1281 struct nvdimm_bus_descriptor *nd_desc;
1285 rc = kstrtol(buf, 0, &val);
1290 nd_desc = dev_get_drvdata(dev);
1292 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1295 case HW_ERROR_SCRUB_ON:
1296 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1298 case HW_ERROR_SCRUB_OFF:
1299 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1311 static DEVICE_ATTR_RW(hw_error_scrub);
1314 * This shows the number of full Address Range Scrubs that have been
1315 * completed since driver load time. Userspace can wait on this using
1316 * select/poll etc. A '+' at the end indicates an ARS is in progress
1318 static ssize_t scrub_show(struct device *dev,
1319 struct device_attribute *attr, char *buf)
1321 struct nvdimm_bus_descriptor *nd_desc;
1322 ssize_t rc = -ENXIO;
1325 nd_desc = dev_get_drvdata(dev);
1327 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1329 mutex_lock(&acpi_desc->init_mutex);
1330 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1331 acpi_desc->scrub_busy
1332 && !acpi_desc->cancel ? "+\n" : "\n");
1333 mutex_unlock(&acpi_desc->init_mutex);
1339 static ssize_t scrub_store(struct device *dev,
1340 struct device_attribute *attr, const char *buf, size_t size)
1342 struct nvdimm_bus_descriptor *nd_desc;
1346 rc = kstrtol(buf, 0, &val);
1353 nd_desc = dev_get_drvdata(dev);
1355 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1357 rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
1364 static DEVICE_ATTR_RW(scrub);
1366 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1368 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1369 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1370 | 1 << ND_CMD_ARS_STATUS;
1372 return (nd_desc->cmd_mask & mask) == mask;
1375 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1377 struct device *dev = container_of(kobj, struct device, kobj);
1378 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1380 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1385 static struct attribute *acpi_nfit_attributes[] = {
1386 &dev_attr_revision.attr,
1387 &dev_attr_scrub.attr,
1388 &dev_attr_hw_error_scrub.attr,
1389 &dev_attr_bus_dsm_mask.attr,
1393 static const struct attribute_group acpi_nfit_attribute_group = {
1395 .attrs = acpi_nfit_attributes,
1396 .is_visible = nfit_visible,
1399 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1400 &nvdimm_bus_attribute_group,
1401 &acpi_nfit_attribute_group,
1405 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1407 struct nvdimm *nvdimm = to_nvdimm(dev);
1408 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1410 return __to_nfit_memdev(nfit_mem);
1413 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1415 struct nvdimm *nvdimm = to_nvdimm(dev);
1416 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1418 return nfit_mem->dcr;
1421 static ssize_t handle_show(struct device *dev,
1422 struct device_attribute *attr, char *buf)
1424 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1426 return sprintf(buf, "%#x\n", memdev->device_handle);
1428 static DEVICE_ATTR_RO(handle);
1430 static ssize_t phys_id_show(struct device *dev,
1431 struct device_attribute *attr, char *buf)
1433 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1435 return sprintf(buf, "%#x\n", memdev->physical_id);
1437 static DEVICE_ATTR_RO(phys_id);
1439 static ssize_t vendor_show(struct device *dev,
1440 struct device_attribute *attr, char *buf)
1442 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1444 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1446 static DEVICE_ATTR_RO(vendor);
1448 static ssize_t rev_id_show(struct device *dev,
1449 struct device_attribute *attr, char *buf)
1451 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1453 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1455 static DEVICE_ATTR_RO(rev_id);
1457 static ssize_t device_show(struct device *dev,
1458 struct device_attribute *attr, char *buf)
1460 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1462 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1464 static DEVICE_ATTR_RO(device);
1466 static ssize_t subsystem_vendor_show(struct device *dev,
1467 struct device_attribute *attr, char *buf)
1469 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1471 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1473 static DEVICE_ATTR_RO(subsystem_vendor);
1475 static ssize_t subsystem_rev_id_show(struct device *dev,
1476 struct device_attribute *attr, char *buf)
1478 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1480 return sprintf(buf, "0x%04x\n",
1481 be16_to_cpu(dcr->subsystem_revision_id));
1483 static DEVICE_ATTR_RO(subsystem_rev_id);
1485 static ssize_t subsystem_device_show(struct device *dev,
1486 struct device_attribute *attr, char *buf)
1488 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1490 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1492 static DEVICE_ATTR_RO(subsystem_device);
1494 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1496 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1499 if (nfit_mem->memdev_pmem)
1501 if (nfit_mem->memdev_bdw)
1506 static ssize_t format_show(struct device *dev,
1507 struct device_attribute *attr, char *buf)
1509 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1511 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1513 static DEVICE_ATTR_RO(format);
1515 static ssize_t format1_show(struct device *dev,
1516 struct device_attribute *attr, char *buf)
1519 ssize_t rc = -ENXIO;
1520 struct nfit_mem *nfit_mem;
1521 struct nfit_memdev *nfit_memdev;
1522 struct acpi_nfit_desc *acpi_desc;
1523 struct nvdimm *nvdimm = to_nvdimm(dev);
1524 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1526 nfit_mem = nvdimm_provider_data(nvdimm);
1527 acpi_desc = nfit_mem->acpi_desc;
1528 handle = to_nfit_memdev(dev)->device_handle;
1530 /* assumes DIMMs have at most 2 published interface codes */
1531 mutex_lock(&acpi_desc->init_mutex);
1532 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1533 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1534 struct nfit_dcr *nfit_dcr;
1536 if (memdev->device_handle != handle)
1539 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1540 if (nfit_dcr->dcr->region_index != memdev->region_index)
1542 if (nfit_dcr->dcr->code == dcr->code)
1544 rc = sprintf(buf, "0x%04x\n",
1545 le16_to_cpu(nfit_dcr->dcr->code));
1551 mutex_unlock(&acpi_desc->init_mutex);
1554 static DEVICE_ATTR_RO(format1);
1556 static ssize_t formats_show(struct device *dev,
1557 struct device_attribute *attr, char *buf)
1559 struct nvdimm *nvdimm = to_nvdimm(dev);
1561 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1563 static DEVICE_ATTR_RO(formats);
1565 static ssize_t serial_show(struct device *dev,
1566 struct device_attribute *attr, char *buf)
1568 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1570 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1572 static DEVICE_ATTR_RO(serial);
1574 static ssize_t family_show(struct device *dev,
1575 struct device_attribute *attr, char *buf)
1577 struct nvdimm *nvdimm = to_nvdimm(dev);
1578 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1580 if (nfit_mem->family < 0)
1582 return sprintf(buf, "%d\n", nfit_mem->family);
1584 static DEVICE_ATTR_RO(family);
1586 static ssize_t dsm_mask_show(struct device *dev,
1587 struct device_attribute *attr, char *buf)
1589 struct nvdimm *nvdimm = to_nvdimm(dev);
1590 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1592 if (nfit_mem->family < 0)
1594 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1596 static DEVICE_ATTR_RO(dsm_mask);
1598 static ssize_t flags_show(struct device *dev,
1599 struct device_attribute *attr, char *buf)
1601 struct nvdimm *nvdimm = to_nvdimm(dev);
1602 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1603 u16 flags = __to_nfit_memdev(nfit_mem)->flags;
1605 if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
1606 flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
1608 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1609 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1610 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1611 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1612 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1613 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1614 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1615 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1617 static DEVICE_ATTR_RO(flags);
1619 static ssize_t id_show(struct device *dev,
1620 struct device_attribute *attr, char *buf)
1622 struct nvdimm *nvdimm = to_nvdimm(dev);
1623 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1625 return sprintf(buf, "%s\n", nfit_mem->id);
1627 static DEVICE_ATTR_RO(id);
1629 static ssize_t dirty_shutdown_show(struct device *dev,
1630 struct device_attribute *attr, char *buf)
1632 struct nvdimm *nvdimm = to_nvdimm(dev);
1633 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1635 return sprintf(buf, "%d\n", nfit_mem->dirty_shutdown);
1637 static DEVICE_ATTR_RO(dirty_shutdown);
1639 static struct attribute *acpi_nfit_dimm_attributes[] = {
1640 &dev_attr_handle.attr,
1641 &dev_attr_phys_id.attr,
1642 &dev_attr_vendor.attr,
1643 &dev_attr_device.attr,
1644 &dev_attr_rev_id.attr,
1645 &dev_attr_subsystem_vendor.attr,
1646 &dev_attr_subsystem_device.attr,
1647 &dev_attr_subsystem_rev_id.attr,
1648 &dev_attr_format.attr,
1649 &dev_attr_formats.attr,
1650 &dev_attr_format1.attr,
1651 &dev_attr_serial.attr,
1652 &dev_attr_flags.attr,
1654 &dev_attr_family.attr,
1655 &dev_attr_dsm_mask.attr,
1656 &dev_attr_dirty_shutdown.attr,
1660 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1661 struct attribute *a, int n)
1663 struct device *dev = container_of(kobj, struct device, kobj);
1664 struct nvdimm *nvdimm = to_nvdimm(dev);
1665 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1667 if (!to_nfit_dcr(dev)) {
1668 /* Without a dcr only the memdev attributes can be surfaced */
1669 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1670 || a == &dev_attr_flags.attr
1671 || a == &dev_attr_family.attr
1672 || a == &dev_attr_dsm_mask.attr)
1677 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1680 if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
1681 && a == &dev_attr_dirty_shutdown.attr)
1687 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1689 .attrs = acpi_nfit_dimm_attributes,
1690 .is_visible = acpi_nfit_dimm_attr_visible,
1693 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1694 &nvdimm_attribute_group,
1695 &nd_device_attribute_group,
1696 &acpi_nfit_dimm_attribute_group,
1700 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1703 struct nfit_mem *nfit_mem;
1705 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1706 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1707 return nfit_mem->nvdimm;
1712 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1714 struct nfit_mem *nfit_mem;
1715 struct acpi_nfit_desc *acpi_desc;
1717 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
1720 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1721 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1726 acpi_desc = dev_get_drvdata(dev->parent);
1731 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1734 nfit_mem = dev_get_drvdata(dev);
1735 if (nfit_mem && nfit_mem->flags_attr)
1736 sysfs_notify_dirent(nfit_mem->flags_attr);
1738 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1740 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1742 struct acpi_device *adev = data;
1743 struct device *dev = &adev->dev;
1745 device_lock(dev->parent);
1746 __acpi_nvdimm_notify(dev, event);
1747 device_unlock(dev->parent);
1750 static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1755 status = acpi_get_handle(adev->handle, method, &handle);
1757 if (ACPI_SUCCESS(status))
1762 __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
1764 struct nd_intel_smart smart = { 0 };
1765 union acpi_object in_buf = {
1766 .type = ACPI_TYPE_BUFFER,
1767 .buffer.pointer = (char *) &smart,
1768 .buffer.length = sizeof(smart),
1770 union acpi_object in_obj = {
1771 .type = ACPI_TYPE_PACKAGE,
1773 .package.elements = &in_buf,
1775 const u8 func = ND_INTEL_SMART;
1776 const guid_t *guid = to_nfit_uuid(nfit_mem->family);
1777 u8 revid = nfit_dsm_revid(nfit_mem->family, func);
1778 struct acpi_device *adev = nfit_mem->adev;
1779 acpi_handle handle = adev->handle;
1780 union acpi_object *out_obj;
1782 if ((nfit_mem->dsm_mask & (1 << func)) == 0)
1785 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
1789 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
1790 if (smart.shutdown_state)
1791 set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
1794 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
1795 set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
1796 nfit_mem->dirty_shutdown = smart.shutdown_count;
1801 static void populate_shutdown_status(struct nfit_mem *nfit_mem)
1804 * For DIMMs that provide a dynamic facility to retrieve a
1805 * dirty-shutdown status and/or a dirty-shutdown count, cache
1806 * these values in nfit_mem.
1808 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1809 nfit_intel_shutdown_status(nfit_mem);
1812 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1813 struct nfit_mem *nfit_mem, u32 device_handle)
1815 struct acpi_device *adev, *adev_dimm;
1816 struct device *dev = acpi_desc->dev;
1817 unsigned long dsm_mask, label_mask;
1821 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1823 /* nfit test assumes 1:1 relationship between commands and dsms */
1824 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1825 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1827 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1828 sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
1829 be16_to_cpu(dcr->vendor_id),
1830 dcr->manufacturing_location,
1831 be16_to_cpu(dcr->manufacturing_date),
1832 be32_to_cpu(dcr->serial_number));
1834 sprintf(nfit_mem->id, "%04x-%08x",
1835 be16_to_cpu(dcr->vendor_id),
1836 be32_to_cpu(dcr->serial_number));
1838 adev = to_acpi_dev(acpi_desc);
1840 /* unit test case */
1841 populate_shutdown_status(nfit_mem);
1845 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1846 nfit_mem->adev = adev_dimm;
1848 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1850 return force_enable_dimms ? 0 : -ENODEV;
1853 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1854 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1855 dev_err(dev, "%s: notification registration failed\n",
1856 dev_name(&adev_dimm->dev));
1860 * Record nfit_mem for the notification path to track back to
1861 * the nfit sysfs attributes for this dimm device object.
1863 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1866 * Until standardization materializes we need to consider 4
1867 * different command sets. Note, that checking for function0 (bit0)
1868 * tells us if any commands are reachable through this GUID.
1870 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
1871 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1872 if (family < 0 || i == default_dsm_family)
1875 /* limit the supported commands to those that are publicly documented */
1876 nfit_mem->family = family;
1877 if (override_dsm_mask && !disable_vendor_specific)
1878 dsm_mask = override_dsm_mask;
1879 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1880 dsm_mask = NVDIMM_INTEL_CMDMASK;
1881 if (disable_vendor_specific)
1882 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1883 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1884 dsm_mask = 0x1c3c76;
1885 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1887 if (disable_vendor_specific)
1888 dsm_mask &= ~(1 << 8);
1889 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1890 dsm_mask = 0xffffffff;
1892 dev_dbg(dev, "unknown dimm command family\n");
1893 nfit_mem->family = -1;
1894 /* DSMs are optional, continue loading the driver... */
1899 * Function 0 is the command interrogation function, don't
1900 * export it to potential userspace use, and enable it to be
1901 * used as an error value in acpi_nfit_ctl().
1905 guid = to_nfit_uuid(nfit_mem->family);
1906 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1907 if (acpi_check_dsm(adev_dimm->handle, guid,
1908 nfit_dsm_revid(nfit_mem->family, i),
1910 set_bit(i, &nfit_mem->dsm_mask);
1913 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1914 * due to their better semantics handling locked capacity.
1916 label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
1917 | 1 << ND_CMD_SET_CONFIG_DATA;
1918 if (family == NVDIMM_FAMILY_INTEL
1919 && (dsm_mask & label_mask) == label_mask)
1922 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1923 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
1924 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
1925 set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1928 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
1929 && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
1930 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
1931 set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
1934 populate_shutdown_status(nfit_mem);
1939 static void shutdown_dimm_notify(void *data)
1941 struct acpi_nfit_desc *acpi_desc = data;
1942 struct nfit_mem *nfit_mem;
1944 mutex_lock(&acpi_desc->init_mutex);
1946 * Clear out the nfit_mem->flags_attr and shut down dimm event
1949 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1950 struct acpi_device *adev_dimm = nfit_mem->adev;
1952 if (nfit_mem->flags_attr) {
1953 sysfs_put(nfit_mem->flags_attr);
1954 nfit_mem->flags_attr = NULL;
1957 acpi_remove_notify_handler(adev_dimm->handle,
1958 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1959 dev_set_drvdata(&adev_dimm->dev, NULL);
1962 mutex_unlock(&acpi_desc->init_mutex);
1965 static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
1968 case NVDIMM_FAMILY_INTEL:
1969 return intel_security_ops;
1975 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1977 struct nfit_mem *nfit_mem;
1978 int dimm_count = 0, rc;
1979 struct nvdimm *nvdimm;
1981 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1982 struct acpi_nfit_flush_address *flush;
1983 unsigned long flags = 0, cmd_mask;
1984 struct nfit_memdev *nfit_memdev;
1988 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1989 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1995 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1996 set_bit(NDD_ALIASING, &flags);
1998 /* collate flags across all memdevs for this dimm */
1999 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2000 struct acpi_nfit_memory_map *dimm_memdev;
2002 dimm_memdev = __to_nfit_memdev(nfit_mem);
2003 if (dimm_memdev->device_handle
2004 != nfit_memdev->memdev->device_handle)
2006 dimm_memdev->flags |= nfit_memdev->memdev->flags;
2009 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
2010 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
2011 set_bit(NDD_UNARMED, &flags);
2013 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
2018 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
2019 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
2020 * userspace interface.
2022 cmd_mask = 1UL << ND_CMD_CALL;
2023 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
2025 * These commands have a 1:1 correspondence
2026 * between DSM payload and libnvdimm ioctl
2029 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
2032 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
2033 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
2034 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
2036 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
2037 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
2039 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
2041 nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
2042 acpi_nfit_dimm_attribute_groups,
2043 flags, cmd_mask, flush ? flush->hint_count : 0,
2044 nfit_mem->flush_wpq, &nfit_mem->id[0],
2045 acpi_nfit_get_security_ops(nfit_mem->family));
2049 nfit_mem->nvdimm = nvdimm;
2052 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
2055 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
2056 nvdimm_name(nvdimm),
2057 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
2058 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
2059 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
2060 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
2061 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
2065 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
2070 * Now that dimms are successfully registered, and async registration
2071 * is flushed, attempt to enable event notification.
2073 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2074 struct kernfs_node *nfit_kernfs;
2076 nvdimm = nfit_mem->nvdimm;
2080 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
2082 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
2084 sysfs_put(nfit_kernfs);
2085 if (!nfit_mem->flags_attr)
2086 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
2087 nvdimm_name(nvdimm));
2090 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
2095 * These constants are private because there are no kernel consumers of
2098 enum nfit_aux_cmds {
2099 NFIT_CMD_TRANSLATE_SPA = 5,
2100 NFIT_CMD_ARS_INJECT_SET = 7,
2101 NFIT_CMD_ARS_INJECT_CLEAR = 8,
2102 NFIT_CMD_ARS_INJECT_GET = 9,
2105 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
2107 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2108 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
2109 struct acpi_device *adev;
2110 unsigned long dsm_mask;
2113 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
2114 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
2115 adev = to_acpi_dev(acpi_desc);
2119 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
2120 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2121 set_bit(i, &nd_desc->cmd_mask);
2122 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
2125 (1 << ND_CMD_ARS_CAP) |
2126 (1 << ND_CMD_ARS_START) |
2127 (1 << ND_CMD_ARS_STATUS) |
2128 (1 << ND_CMD_CLEAR_ERROR) |
2129 (1 << NFIT_CMD_TRANSLATE_SPA) |
2130 (1 << NFIT_CMD_ARS_INJECT_SET) |
2131 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
2132 (1 << NFIT_CMD_ARS_INJECT_GET);
2133 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
2134 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2135 set_bit(i, &nd_desc->bus_dsm_mask);
2138 static ssize_t range_index_show(struct device *dev,
2139 struct device_attribute *attr, char *buf)
2141 struct nd_region *nd_region = to_nd_region(dev);
2142 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
2144 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
2146 static DEVICE_ATTR_RO(range_index);
2148 static struct attribute *acpi_nfit_region_attributes[] = {
2149 &dev_attr_range_index.attr,
2153 static const struct attribute_group acpi_nfit_region_attribute_group = {
2155 .attrs = acpi_nfit_region_attributes,
2158 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
2159 &nd_region_attribute_group,
2160 &nd_mapping_attribute_group,
2161 &nd_device_attribute_group,
2162 &nd_numa_attribute_group,
2163 &acpi_nfit_region_attribute_group,
2167 /* enough info to uniquely specify an interleave set */
2168 struct nfit_set_info {
2169 struct nfit_set_info_map {
2176 struct nfit_set_info2 {
2177 struct nfit_set_info_map2 {
2181 u16 manufacturing_date;
2182 u8 manufacturing_location;
2187 static size_t sizeof_nfit_set_info(int num_mappings)
2189 return sizeof(struct nfit_set_info)
2190 + num_mappings * sizeof(struct nfit_set_info_map);
2193 static size_t sizeof_nfit_set_info2(int num_mappings)
2195 return sizeof(struct nfit_set_info2)
2196 + num_mappings * sizeof(struct nfit_set_info_map2);
2199 static int cmp_map_compat(const void *m0, const void *m1)
2201 const struct nfit_set_info_map *map0 = m0;
2202 const struct nfit_set_info_map *map1 = m1;
2204 return memcmp(&map0->region_offset, &map1->region_offset,
2208 static int cmp_map(const void *m0, const void *m1)
2210 const struct nfit_set_info_map *map0 = m0;
2211 const struct nfit_set_info_map *map1 = m1;
2213 if (map0->region_offset < map1->region_offset)
2215 else if (map0->region_offset > map1->region_offset)
2220 static int cmp_map2(const void *m0, const void *m1)
2222 const struct nfit_set_info_map2 *map0 = m0;
2223 const struct nfit_set_info_map2 *map1 = m1;
2225 if (map0->region_offset < map1->region_offset)
2227 else if (map0->region_offset > map1->region_offset)
2232 /* Retrieve the nth entry referencing this spa */
2233 static struct acpi_nfit_memory_map *memdev_from_spa(
2234 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2236 struct nfit_memdev *nfit_memdev;
2238 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2239 if (nfit_memdev->memdev->range_index == range_index)
2241 return nfit_memdev->memdev;
2245 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2246 struct nd_region_desc *ndr_desc,
2247 struct acpi_nfit_system_address *spa)
2249 struct device *dev = acpi_desc->dev;
2250 struct nd_interleave_set *nd_set;
2251 u16 nr = ndr_desc->num_mappings;
2252 struct nfit_set_info2 *info2;
2253 struct nfit_set_info *info;
2256 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2259 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2261 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2265 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2269 for (i = 0; i < nr; i++) {
2270 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
2271 struct nfit_set_info_map *map = &info->mapping[i];
2272 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2273 struct nvdimm *nvdimm = mapping->nvdimm;
2274 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2275 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2276 spa->range_index, i);
2277 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2279 if (!memdev || !nfit_mem->dcr) {
2280 dev_err(dev, "%s: failed to find DCR\n", __func__);
2284 map->region_offset = memdev->region_offset;
2285 map->serial_number = dcr->serial_number;
2287 map2->region_offset = memdev->region_offset;
2288 map2->serial_number = dcr->serial_number;
2289 map2->vendor_id = dcr->vendor_id;
2290 map2->manufacturing_date = dcr->manufacturing_date;
2291 map2->manufacturing_location = dcr->manufacturing_location;
2294 /* v1.1 namespaces */
2295 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2297 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2299 /* v1.2 namespaces */
2300 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2302 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2304 /* support v1.1 namespaces created with the wrong sort order */
2305 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2306 cmp_map_compat, NULL);
2307 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2309 /* record the result of the sort for the mapping position */
2310 for (i = 0; i < nr; i++) {
2311 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2314 for (j = 0; j < nr; j++) {
2315 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2316 struct nvdimm *nvdimm = mapping->nvdimm;
2317 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2318 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2320 if (map2->serial_number == dcr->serial_number &&
2321 map2->vendor_id == dcr->vendor_id &&
2322 map2->manufacturing_date == dcr->manufacturing_date &&
2323 map2->manufacturing_location
2324 == dcr->manufacturing_location) {
2325 mapping->position = i;
2331 ndr_desc->nd_set = nd_set;
2332 devm_kfree(dev, info);
2333 devm_kfree(dev, info2);
2338 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2340 struct acpi_nfit_interleave *idt = mmio->idt;
2341 u32 sub_line_offset, line_index, line_offset;
2342 u64 line_no, table_skip_count, table_offset;
2344 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2345 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2346 line_offset = idt->line_offset[line_index]
2348 table_offset = table_skip_count * mmio->table_size;
2350 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2353 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
2355 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2356 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
2357 const u32 STATUS_MASK = 0x80000037;
2359 if (mmio->num_lines)
2360 offset = to_interleave_offset(offset, mmio);
2362 return readl(mmio->addr.base + offset) & STATUS_MASK;
2365 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2366 resource_size_t dpa, unsigned int len, unsigned int write)
2369 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2372 BCW_OFFSET_MASK = (1ULL << 48)-1,
2374 BCW_LEN_MASK = (1ULL << 8) - 1,
2378 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2379 len = len >> L1_CACHE_SHIFT;
2380 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2381 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2383 offset = nfit_blk->cmd_offset + mmio->size * bw;
2384 if (mmio->num_lines)
2385 offset = to_interleave_offset(offset, mmio);
2387 writeq(cmd, mmio->addr.base + offset);
2388 nvdimm_flush(nfit_blk->nd_region);
2390 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
2391 readq(mmio->addr.base + offset);
2394 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2395 resource_size_t dpa, void *iobuf, size_t len, int rw,
2398 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2399 unsigned int copied = 0;
2403 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2404 + lane * mmio->size;
2405 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2410 if (mmio->num_lines) {
2413 offset = to_interleave_offset(base_offset + copied,
2415 div_u64_rem(offset, mmio->line_size, &line_offset);
2416 c = min_t(size_t, len, mmio->line_size - line_offset);
2418 offset = base_offset + nfit_blk->bdw_offset;
2423 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2425 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2426 arch_invalidate_pmem((void __force *)
2427 mmio->addr.aperture + offset, c);
2429 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2437 nvdimm_flush(nfit_blk->nd_region);
2439 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2443 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2444 resource_size_t dpa, void *iobuf, u64 len, int rw)
2446 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2447 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2448 struct nd_region *nd_region = nfit_blk->nd_region;
2449 unsigned int lane, copied = 0;
2452 lane = nd_region_acquire_lane(nd_region);
2454 u64 c = min(len, mmio->size);
2456 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2457 iobuf + copied, c, rw, lane);
2464 nd_region_release_lane(nd_region, lane);
2469 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2470 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2473 mmio->num_lines = idt->line_count;
2474 mmio->line_size = idt->line_size;
2475 if (interleave_ways == 0)
2477 mmio->table_size = mmio->num_lines * interleave_ways
2484 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2485 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2487 struct nd_cmd_dimm_flags flags;
2490 memset(&flags, 0, sizeof(flags));
2491 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2492 sizeof(flags), NULL);
2494 if (rc >= 0 && flags.status == 0)
2495 nfit_blk->dimm_flags = flags.flags;
2496 else if (rc == -ENOTTY) {
2497 /* fall back to a conservative default */
2498 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2506 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2509 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2510 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2511 struct nfit_blk_mmio *mmio;
2512 struct nfit_blk *nfit_blk;
2513 struct nfit_mem *nfit_mem;
2514 struct nvdimm *nvdimm;
2517 nvdimm = nd_blk_region_to_dimm(ndbr);
2518 nfit_mem = nvdimm_provider_data(nvdimm);
2519 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2520 dev_dbg(dev, "missing%s%s%s\n",
2521 nfit_mem ? "" : " nfit_mem",
2522 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2523 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2527 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2530 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2531 nfit_blk->nd_region = to_nd_region(dev);
2533 /* map block aperture memory */
2534 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2535 mmio = &nfit_blk->mmio[BDW];
2536 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2537 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2538 if (!mmio->addr.base) {
2539 dev_dbg(dev, "%s failed to map bdw\n",
2540 nvdimm_name(nvdimm));
2543 mmio->size = nfit_mem->bdw->size;
2544 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2545 mmio->idt = nfit_mem->idt_bdw;
2546 mmio->spa = nfit_mem->spa_bdw;
2547 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2548 nfit_mem->memdev_bdw->interleave_ways);
2550 dev_dbg(dev, "%s failed to init bdw interleave\n",
2551 nvdimm_name(nvdimm));
2555 /* map block control memory */
2556 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2557 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2558 mmio = &nfit_blk->mmio[DCR];
2559 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2560 nfit_mem->spa_dcr->length);
2561 if (!mmio->addr.base) {
2562 dev_dbg(dev, "%s failed to map dcr\n",
2563 nvdimm_name(nvdimm));
2566 mmio->size = nfit_mem->dcr->window_size;
2567 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2568 mmio->idt = nfit_mem->idt_dcr;
2569 mmio->spa = nfit_mem->spa_dcr;
2570 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2571 nfit_mem->memdev_dcr->interleave_ways);
2573 dev_dbg(dev, "%s failed to init dcr interleave\n",
2574 nvdimm_name(nvdimm));
2578 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2580 dev_dbg(dev, "%s failed get DIMM flags\n",
2581 nvdimm_name(nvdimm));
2585 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2586 dev_warn(dev, "unable to guarantee persistence of writes\n");
2588 if (mmio->line_size == 0)
2591 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2592 + 8 > mmio->line_size) {
2593 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2595 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2596 + 8 > mmio->line_size) {
2597 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2604 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2605 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2607 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2608 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2611 cmd->address = spa->address;
2612 cmd->length = spa->length;
2613 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2614 sizeof(*cmd), &cmd_rc);
2620 static int ars_start(struct acpi_nfit_desc *acpi_desc,
2621 struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
2625 struct nd_cmd_ars_start ars_start;
2626 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2627 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2629 memset(&ars_start, 0, sizeof(ars_start));
2630 ars_start.address = spa->address;
2631 ars_start.length = spa->length;
2632 if (req_type == ARS_REQ_SHORT)
2633 ars_start.flags = ND_ARS_RETURN_PREV_DATA;
2634 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2635 ars_start.type = ND_ARS_PERSISTENT;
2636 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2637 ars_start.type = ND_ARS_VOLATILE;
2641 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2642 sizeof(ars_start), &cmd_rc);
2649 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2652 struct nd_cmd_ars_start ars_start;
2653 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2654 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2656 memset(&ars_start, 0, sizeof(ars_start));
2657 ars_start.address = ars_status->restart_address;
2658 ars_start.length = ars_status->restart_length;
2659 ars_start.type = ars_status->type;
2660 ars_start.flags = acpi_desc->ars_start_flags;
2661 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2662 sizeof(ars_start), &cmd_rc);
2668 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2670 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2671 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2674 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2675 acpi_desc->max_ars, &cmd_rc);
2681 static void ars_complete(struct acpi_nfit_desc *acpi_desc,
2682 struct nfit_spa *nfit_spa)
2684 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2685 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2686 struct nd_region *nd_region = nfit_spa->nd_region;
2689 lockdep_assert_held(&acpi_desc->init_mutex);
2691 * Only advance the ARS state for ARS runs initiated by the
2692 * kernel, ignore ARS results from BIOS initiated runs for scrub
2693 * completion tracking.
2695 if (acpi_desc->scrub_spa != nfit_spa)
2698 if ((ars_status->address >= spa->address && ars_status->address
2699 < spa->address + spa->length)
2700 || (ars_status->address < spa->address)) {
2702 * Assume that if a scrub starts at an offset from the
2703 * start of nfit_spa that we are in the continuation
2706 * Otherwise, if the scrub covers the spa range, mark
2707 * any pending request complete.
2709 if (ars_status->address + ars_status->length
2710 >= spa->address + spa->length)
2717 acpi_desc->scrub_spa = NULL;
2719 dev = nd_region_dev(nd_region);
2720 nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
2722 dev = acpi_desc->dev;
2723 dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
2726 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
2728 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2729 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2734 * First record starts at 44 byte offset from the start of the
2737 if (ars_status->out_length < 44)
2739 for (i = 0; i < ars_status->num_records; i++) {
2740 /* only process full records */
2741 if (ars_status->out_length
2742 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2744 rc = nvdimm_bus_add_badrange(nvdimm_bus,
2745 ars_status->records[i].err_address,
2746 ars_status->records[i].length);
2750 if (i < ars_status->num_records)
2751 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2756 static void acpi_nfit_remove_resource(void *data)
2758 struct resource *res = data;
2760 remove_resource(res);
2763 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2764 struct nd_region_desc *ndr_desc)
2766 struct resource *res, *nd_res = ndr_desc->res;
2769 /* No operation if the region is already registered as PMEM */
2770 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2771 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2772 if (is_pmem == REGION_INTERSECTS)
2775 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2779 res->name = "Persistent Memory";
2780 res->start = nd_res->start;
2781 res->end = nd_res->end;
2782 res->flags = IORESOURCE_MEM;
2783 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2785 ret = insert_resource(&iomem_resource, res);
2789 ret = devm_add_action_or_reset(acpi_desc->dev,
2790 acpi_nfit_remove_resource,
2798 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2799 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2800 struct acpi_nfit_memory_map *memdev,
2801 struct nfit_spa *nfit_spa)
2803 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2804 memdev->device_handle);
2805 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2806 struct nd_blk_region_desc *ndbr_desc;
2807 struct nfit_mem *nfit_mem;
2811 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2812 spa->range_index, memdev->device_handle);
2816 mapping->nvdimm = nvdimm;
2817 switch (nfit_spa_type(spa)) {
2819 case NFIT_SPA_VOLATILE:
2820 mapping->start = memdev->address;
2821 mapping->size = memdev->region_size;
2824 nfit_mem = nvdimm_provider_data(nvdimm);
2825 if (!nfit_mem || !nfit_mem->bdw) {
2826 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2827 spa->range_index, nvdimm_name(nvdimm));
2831 mapping->size = nfit_mem->bdw->capacity;
2832 mapping->start = nfit_mem->bdw->start_address;
2833 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2834 ndr_desc->mapping = mapping;
2835 ndr_desc->num_mappings = 1;
2836 ndbr_desc = to_blk_region_desc(ndr_desc);
2837 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2838 ndbr_desc->do_io = acpi_desc->blk_do_io;
2839 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2842 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2844 if (!nfit_spa->nd_region)
2852 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2854 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2855 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2856 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2857 nfit_spa_type(spa) == NFIT_SPA_PCD);
2860 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2862 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2863 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2864 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2867 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2868 struct nfit_spa *nfit_spa)
2870 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2871 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2872 struct nd_blk_region_desc ndbr_desc;
2873 struct nd_region_desc *ndr_desc;
2874 struct nfit_memdev *nfit_memdev;
2875 struct nvdimm_bus *nvdimm_bus;
2876 struct resource res;
2879 if (nfit_spa->nd_region)
2882 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2883 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
2887 memset(&res, 0, sizeof(res));
2888 memset(&mappings, 0, sizeof(mappings));
2889 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2890 res.start = spa->address;
2891 res.end = res.start + spa->length - 1;
2892 ndr_desc = &ndbr_desc.ndr_desc;
2893 ndr_desc->res = &res;
2894 ndr_desc->provider_data = nfit_spa;
2895 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2896 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2897 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2898 spa->proximity_domain);
2900 ndr_desc->numa_node = NUMA_NO_NODE;
2903 * Persistence domain bits are hierarchical, if
2904 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
2905 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
2907 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2908 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2909 else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2910 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2912 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2913 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2914 struct nd_mapping_desc *mapping;
2916 if (memdev->range_index != spa->range_index)
2918 if (count >= ND_MAX_MAPPINGS) {
2919 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2920 spa->range_index, ND_MAX_MAPPINGS);
2923 mapping = &mappings[count++];
2924 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2930 ndr_desc->mapping = mappings;
2931 ndr_desc->num_mappings = count;
2932 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2936 nvdimm_bus = acpi_desc->nvdimm_bus;
2937 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2938 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2940 dev_warn(acpi_desc->dev,
2941 "failed to insert pmem resource to iomem: %d\n",
2946 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2948 if (!nfit_spa->nd_region)
2950 } else if (nfit_spa_is_volatile(spa)) {
2951 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2953 if (!nfit_spa->nd_region)
2955 } else if (nfit_spa_is_virtual(spa)) {
2956 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2958 if (!nfit_spa->nd_region)
2964 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2965 nfit_spa->spa->range_index);
2969 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
2971 struct device *dev = acpi_desc->dev;
2972 struct nd_cmd_ars_status *ars_status;
2974 if (acpi_desc->ars_status) {
2975 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
2979 ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
2982 acpi_desc->ars_status = ars_status;
2986 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
2990 if (ars_status_alloc(acpi_desc))
2993 rc = ars_get_status(acpi_desc);
2995 if (rc < 0 && rc != -ENOSPC)
2998 if (ars_status_process_records(acpi_desc))
2999 dev_err(acpi_desc->dev, "Failed to process ARS records\n");
3004 static int ars_register(struct acpi_nfit_desc *acpi_desc,
3005 struct nfit_spa *nfit_spa)
3009 if (no_init_ars || test_bit(ARS_FAILED, &nfit_spa->ars_state))
3010 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3012 set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3013 set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
3015 switch (acpi_nfit_query_poison(acpi_desc)) {
3018 rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
3019 /* shouldn't happen, try again later */
3023 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3026 clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3027 rc = acpi_nfit_query_poison(acpi_desc);
3030 acpi_desc->scrub_spa = nfit_spa;
3031 ars_complete(acpi_desc, nfit_spa);
3033 * If ars_complete() says we didn't complete the
3034 * short scrub, we'll try again with a long
3037 acpi_desc->scrub_spa = NULL;
3043 * BIOS was using ARS, wait for it to complete (or
3044 * resources to become available) and then perform our
3049 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3053 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3056 static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
3058 struct nfit_spa *nfit_spa;
3060 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3061 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3063 ars_complete(acpi_desc, nfit_spa);
3067 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
3070 unsigned int tmo = acpi_desc->scrub_tmo;
3071 struct device *dev = acpi_desc->dev;
3072 struct nfit_spa *nfit_spa;
3074 lockdep_assert_held(&acpi_desc->init_mutex);
3076 if (acpi_desc->cancel)
3079 if (query_rc == -EBUSY) {
3080 dev_dbg(dev, "ARS: ARS busy\n");
3081 return min(30U * 60U, tmo * 2);
3083 if (query_rc == -ENOSPC) {
3084 dev_dbg(dev, "ARS: ARS continue\n");
3085 ars_continue(acpi_desc);
3088 if (query_rc && query_rc != -EAGAIN) {
3089 unsigned long long addr, end;
3091 addr = acpi_desc->ars_status->address;
3092 end = addr + acpi_desc->ars_status->length;
3093 dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
3097 ars_complete_all(acpi_desc);
3098 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3099 enum nfit_ars_state req_type;
3102 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3105 /* prefer short ARS requests first */
3106 if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
3107 req_type = ARS_REQ_SHORT;
3108 else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
3109 req_type = ARS_REQ_LONG;
3112 rc = ars_start(acpi_desc, nfit_spa, req_type);
3114 dev = nd_region_dev(nfit_spa->nd_region);
3115 dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
3116 nfit_spa->spa->range_index,
3117 req_type == ARS_REQ_SHORT ? "short" : "long",
3120 * Hmm, we raced someone else starting ARS? Try again in
3126 dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
3127 "scrub start while range %d active\n",
3128 acpi_desc->scrub_spa->spa->range_index);
3129 clear_bit(req_type, &nfit_spa->ars_state);
3130 acpi_desc->scrub_spa = nfit_spa;
3132 * Consider this spa last for future scrub
3135 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
3139 dev_err(dev, "ARS: range %d ARS failed (%d)\n",
3140 nfit_spa->spa->range_index, rc);
3141 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3146 static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
3148 lockdep_assert_held(&acpi_desc->init_mutex);
3150 acpi_desc->scrub_busy = 1;
3151 /* note this should only be set from within the workqueue */
3153 acpi_desc->scrub_tmo = tmo;
3154 queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
3157 static void sched_ars(struct acpi_nfit_desc *acpi_desc)
3159 __sched_ars(acpi_desc, 0);
3162 static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
3164 lockdep_assert_held(&acpi_desc->init_mutex);
3166 acpi_desc->scrub_busy = 0;
3167 acpi_desc->scrub_count++;
3168 if (acpi_desc->scrub_count_state)
3169 sysfs_notify_dirent(acpi_desc->scrub_count_state);
3172 static void acpi_nfit_scrub(struct work_struct *work)
3174 struct acpi_nfit_desc *acpi_desc;
3178 acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
3179 mutex_lock(&acpi_desc->init_mutex);
3180 query_rc = acpi_nfit_query_poison(acpi_desc);
3181 tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
3183 __sched_ars(acpi_desc, tmo);
3185 notify_ars_done(acpi_desc);
3186 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3187 mutex_unlock(&acpi_desc->init_mutex);
3190 static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
3191 struct nfit_spa *nfit_spa)
3193 int type = nfit_spa_type(nfit_spa->spa);
3194 struct nd_cmd_ars_cap ars_cap;
3197 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3198 memset(&ars_cap, 0, sizeof(ars_cap));
3199 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
3202 /* check that the supported scrub types match the spa type */
3203 if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
3204 & ND_ARS_VOLATILE) == 0)
3206 if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
3207 & ND_ARS_PERSISTENT) == 0)
3210 nfit_spa->max_ars = ars_cap.max_ars_out;
3211 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
3212 acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
3213 clear_bit(ARS_FAILED, &nfit_spa->ars_state);
3216 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3218 struct nfit_spa *nfit_spa;
3221 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3222 switch (nfit_spa_type(nfit_spa->spa)) {
3223 case NFIT_SPA_VOLATILE:
3225 acpi_nfit_init_ars(acpi_desc, nfit_spa);
3230 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3231 switch (nfit_spa_type(nfit_spa->spa)) {
3232 case NFIT_SPA_VOLATILE:
3234 /* register regions and kick off initial ARS run */
3235 rc = ars_register(acpi_desc, nfit_spa);
3240 /* nothing to register */
3243 case NFIT_SPA_VDISK:
3245 case NFIT_SPA_PDISK:
3247 /* register known regions that don't support ARS */
3248 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3253 /* don't register unknown regions */
3257 sched_ars(acpi_desc);
3261 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3262 struct nfit_table_prev *prev)
3264 struct device *dev = acpi_desc->dev;
3266 if (!list_empty(&prev->spas) ||
3267 !list_empty(&prev->memdevs) ||
3268 !list_empty(&prev->dcrs) ||
3269 !list_empty(&prev->bdws) ||
3270 !list_empty(&prev->idts) ||
3271 !list_empty(&prev->flushes)) {
3272 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3278 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3280 struct device *dev = acpi_desc->dev;
3281 struct kernfs_node *nfit;
3282 struct device *bus_dev;
3284 if (!ars_supported(acpi_desc->nvdimm_bus))
3287 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3288 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3290 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3293 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3295 if (!acpi_desc->scrub_count_state) {
3296 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3303 static void acpi_nfit_unregister(void *data)
3305 struct acpi_nfit_desc *acpi_desc = data;
3307 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
3310 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
3312 struct device *dev = acpi_desc->dev;
3313 struct nfit_table_prev prev;
3317 if (!acpi_desc->nvdimm_bus) {
3318 acpi_nfit_init_dsms(acpi_desc);
3320 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3321 &acpi_desc->nd_desc);
3322 if (!acpi_desc->nvdimm_bus)
3325 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
3330 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3334 /* register this acpi_desc for mce notifications */
3335 mutex_lock(&acpi_desc_lock);
3336 list_add_tail(&acpi_desc->list, &acpi_descs);
3337 mutex_unlock(&acpi_desc_lock);
3340 mutex_lock(&acpi_desc->init_mutex);
3342 INIT_LIST_HEAD(&prev.spas);
3343 INIT_LIST_HEAD(&prev.memdevs);
3344 INIT_LIST_HEAD(&prev.dcrs);
3345 INIT_LIST_HEAD(&prev.bdws);
3346 INIT_LIST_HEAD(&prev.idts);
3347 INIT_LIST_HEAD(&prev.flushes);
3349 list_cut_position(&prev.spas, &acpi_desc->spas,
3350 acpi_desc->spas.prev);
3351 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3352 acpi_desc->memdevs.prev);
3353 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3354 acpi_desc->dcrs.prev);
3355 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3356 acpi_desc->bdws.prev);
3357 list_cut_position(&prev.idts, &acpi_desc->idts,
3358 acpi_desc->idts.prev);
3359 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3360 acpi_desc->flushes.prev);
3363 while (!IS_ERR_OR_NULL(data))
3364 data = add_table(acpi_desc, &prev, data, end);
3367 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
3372 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3376 rc = nfit_mem_init(acpi_desc);
3380 rc = acpi_nfit_register_dimms(acpi_desc);
3384 rc = acpi_nfit_register_regions(acpi_desc);
3387 mutex_unlock(&acpi_desc->init_mutex);
3390 EXPORT_SYMBOL_GPL(acpi_nfit_init);
3392 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3394 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3395 struct device *dev = acpi_desc->dev;
3397 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3401 /* Bounce the init_mutex to complete initial registration */
3402 mutex_lock(&acpi_desc->init_mutex);
3403 mutex_unlock(&acpi_desc->init_mutex);
3408 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3409 struct nvdimm *nvdimm, unsigned int cmd)
3411 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3415 if (cmd != ND_CMD_ARS_START)
3419 * The kernel and userspace may race to initiate a scrub, but
3420 * the scrub thread is prepared to lose that initial race. It
3421 * just needs guarantees that any ARS it initiates are not
3422 * interrupted by any intervening start requests from userspace.
3424 if (work_busy(&acpi_desc->dwork.work))
3430 /* prevent security commands from being issued via ioctl */
3431 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3432 struct nvdimm *nvdimm, unsigned int cmd, void *buf)
3434 struct nd_cmd_pkg *call_pkg = buf;
3437 if (nvdimm && cmd == ND_CMD_CALL &&
3438 call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
3439 func = call_pkg->nd_command;
3440 if ((1 << func) & NVDIMM_INTEL_SECURITY_CMDMASK)
3444 return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
3447 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
3448 enum nfit_ars_state req_type)
3450 struct device *dev = acpi_desc->dev;
3451 int scheduled = 0, busy = 0;
3452 struct nfit_spa *nfit_spa;
3454 mutex_lock(&acpi_desc->init_mutex);
3455 if (acpi_desc->cancel) {
3456 mutex_unlock(&acpi_desc->init_mutex);
3460 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3461 int type = nfit_spa_type(nfit_spa->spa);
3463 if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
3465 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3468 if (test_and_set_bit(req_type, &nfit_spa->ars_state))
3474 sched_ars(acpi_desc);
3475 dev_dbg(dev, "ars_scan triggered\n");
3477 mutex_unlock(&acpi_desc->init_mutex);
3486 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3488 struct nvdimm_bus_descriptor *nd_desc;
3490 dev_set_drvdata(dev, acpi_desc);
3491 acpi_desc->dev = dev;
3492 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3493 nd_desc = &acpi_desc->nd_desc;
3494 nd_desc->provider_name = "ACPI.NFIT";
3495 nd_desc->module = THIS_MODULE;
3496 nd_desc->ndctl = acpi_nfit_ctl;
3497 nd_desc->flush_probe = acpi_nfit_flush_probe;
3498 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3499 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3501 INIT_LIST_HEAD(&acpi_desc->spas);
3502 INIT_LIST_HEAD(&acpi_desc->dcrs);
3503 INIT_LIST_HEAD(&acpi_desc->bdws);
3504 INIT_LIST_HEAD(&acpi_desc->idts);
3505 INIT_LIST_HEAD(&acpi_desc->flushes);
3506 INIT_LIST_HEAD(&acpi_desc->memdevs);
3507 INIT_LIST_HEAD(&acpi_desc->dimms);
3508 INIT_LIST_HEAD(&acpi_desc->list);
3509 mutex_init(&acpi_desc->init_mutex);
3510 acpi_desc->scrub_tmo = 1;
3511 INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
3513 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3515 static void acpi_nfit_put_table(void *table)
3517 acpi_put_table(table);
3520 void acpi_nfit_shutdown(void *data)
3522 struct acpi_nfit_desc *acpi_desc = data;
3523 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3526 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3529 mutex_lock(&acpi_desc_lock);
3530 list_del(&acpi_desc->list);
3531 mutex_unlock(&acpi_desc_lock);
3533 mutex_lock(&acpi_desc->init_mutex);
3534 acpi_desc->cancel = 1;
3535 cancel_delayed_work_sync(&acpi_desc->dwork);
3536 mutex_unlock(&acpi_desc->init_mutex);
3539 * Bounce the nvdimm bus lock to make sure any in-flight
3540 * acpi_nfit_ars_rescan() submissions have had a chance to
3541 * either submit or see ->cancel set.
3543 device_lock(bus_dev);
3544 device_unlock(bus_dev);
3546 flush_workqueue(nfit_wq);
3548 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3550 static int acpi_nfit_add(struct acpi_device *adev)
3552 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3553 struct acpi_nfit_desc *acpi_desc;
3554 struct device *dev = &adev->dev;
3555 struct acpi_table_header *tbl;
3556 acpi_status status = AE_OK;
3560 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3561 if (ACPI_FAILURE(status)) {
3562 /* The NVDIMM root device allows OS to trigger enumeration of
3563 * NVDIMMs through NFIT at boot time and re-enumeration at
3564 * root level via the _FIT method during runtime.
3565 * This is ok to return 0 here, we could have an nvdimm
3566 * hotplugged later and evaluate _FIT method which returns
3567 * data in the format of a series of NFIT Structures.
3569 dev_dbg(dev, "failed to find NFIT at startup\n");
3573 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3578 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3581 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3583 /* Save the acpi header for exporting the revision via sysfs */
3584 acpi_desc->acpi_header = *tbl;
3586 /* Evaluate _FIT and override with that if present */
3587 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3588 if (ACPI_SUCCESS(status) && buf.length > 0) {
3589 union acpi_object *obj = buf.pointer;
3591 if (obj->type == ACPI_TYPE_BUFFER)
3592 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3593 obj->buffer.length);
3595 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3599 /* skip over the lead-in header table */
3600 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3601 + sizeof(struct acpi_table_nfit),
3602 sz - sizeof(struct acpi_table_nfit));
3606 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3609 static int acpi_nfit_remove(struct acpi_device *adev)
3611 /* see acpi_nfit_unregister */
3615 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3617 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3618 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3619 union acpi_object *obj;
3624 /* dev->driver may be null if we're being removed */
3625 dev_dbg(dev, "no driver found for dev\n");
3630 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3633 acpi_nfit_desc_init(acpi_desc, dev);
3636 * Finish previous registration before considering new
3639 flush_workqueue(nfit_wq);
3643 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3644 if (ACPI_FAILURE(status)) {
3645 dev_err(dev, "failed to evaluate _FIT\n");
3650 if (obj->type == ACPI_TYPE_BUFFER) {
3651 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3652 obj->buffer.length);
3654 dev_err(dev, "failed to merge updated NFIT\n");
3656 dev_err(dev, "Invalid _FIT\n");
3660 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3662 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3664 if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
3665 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
3667 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
3670 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3672 dev_dbg(dev, "event: 0x%x\n", event);
3675 case NFIT_NOTIFY_UPDATE:
3676 return acpi_nfit_update_notify(dev, handle);
3677 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3678 return acpi_nfit_uc_error_notify(dev, handle);
3683 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3685 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3687 device_lock(&adev->dev);
3688 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3689 device_unlock(&adev->dev);
3692 static const struct acpi_device_id acpi_nfit_ids[] = {
3696 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3698 static struct acpi_driver acpi_nfit_driver = {
3699 .name = KBUILD_MODNAME,
3700 .ids = acpi_nfit_ids,
3702 .add = acpi_nfit_add,
3703 .remove = acpi_nfit_remove,
3704 .notify = acpi_nfit_notify,
3708 static __init int nfit_init(void)
3712 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3713 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3714 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3715 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3716 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3717 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3718 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3719 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
3721 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3722 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3723 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3724 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3725 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3726 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3727 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3728 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3729 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3730 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3731 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3732 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3733 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3735 nfit_wq = create_singlethread_workqueue("nfit");
3739 nfit_mce_register();
3740 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3742 nfit_mce_unregister();
3743 destroy_workqueue(nfit_wq);
3750 static __exit void nfit_exit(void)
3752 nfit_mce_unregister();
3753 acpi_bus_unregister_driver(&acpi_nfit_driver);
3754 destroy_workqueue(nfit_wq);
3755 WARN_ON(!list_empty(&acpi_descs));
3758 module_init(nfit_init);
3759 module_exit(nfit_exit);
3760 MODULE_LICENSE("GPL v2");
3761 MODULE_AUTHOR("Intel Corporation");