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 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
413 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
415 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
416 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
417 union acpi_object in_obj, in_buf, *out_obj;
418 const struct nd_cmd_desc *desc = NULL;
419 struct device *dev = acpi_desc->dev;
420 struct nd_cmd_pkg *call_pkg = NULL;
421 const char *cmd_name, *dimm_name;
422 unsigned long cmd_mask, dsm_mask;
423 u32 offset, fw_status = 0;
432 if (cmd == ND_CMD_CALL) {
434 func = call_pkg->nd_command;
436 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
437 if (call_pkg->nd_reserved2[i])
442 struct acpi_device *adev = nfit_mem->adev;
446 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
449 dimm_name = nvdimm_name(nvdimm);
450 cmd_name = nvdimm_cmd_name(cmd);
451 cmd_mask = nvdimm_cmd_mask(nvdimm);
452 dsm_mask = nfit_mem->dsm_mask;
453 desc = nd_cmd_dimm_desc(cmd);
454 guid = to_nfit_uuid(nfit_mem->family);
455 handle = adev->handle;
457 struct acpi_device *adev = to_acpi_dev(acpi_desc);
459 cmd_name = nvdimm_bus_cmd_name(cmd);
460 cmd_mask = nd_desc->cmd_mask;
462 if (cmd == ND_CMD_CALL)
463 dsm_mask = nd_desc->bus_dsm_mask;
464 desc = nd_cmd_bus_desc(cmd);
465 guid = to_nfit_uuid(NFIT_DEV_BUS);
466 handle = adev->handle;
470 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
473 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
476 in_obj.type = ACPI_TYPE_PACKAGE;
477 in_obj.package.count = 1;
478 in_obj.package.elements = &in_buf;
479 in_buf.type = ACPI_TYPE_BUFFER;
480 in_buf.buffer.pointer = buf;
481 in_buf.buffer.length = 0;
483 /* libnvdimm has already validated the input envelope */
484 for (i = 0; i < desc->in_num; i++)
485 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
489 /* skip over package wrapper */
490 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
491 in_buf.buffer.length = call_pkg->nd_size_in;
494 dev_dbg(dev, "%s cmd: %d: func: %d input length: %d\n",
495 dimm_name, cmd, func, in_buf.buffer.length);
496 if (payload_dumpable(nvdimm, func))
497 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
498 in_buf.buffer.pointer,
499 min_t(u32, 256, in_buf.buffer.length), true);
501 /* call the BIOS, prefer the named methods over _DSM if available */
502 if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE
503 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
504 out_obj = acpi_label_info(handle);
505 else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA
506 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
507 struct nd_cmd_get_config_data_hdr *p = buf;
509 out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
510 } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
511 && test_bit(NFIT_MEM_LSW, &nfit_mem->flags)) {
512 struct nd_cmd_set_config_hdr *p = buf;
514 out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
520 revid = nfit_dsm_revid(nfit_mem->family, func);
523 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
527 dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
532 call_pkg->nd_fw_size = out_obj->buffer.length;
533 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
534 out_obj->buffer.pointer,
535 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
539 * Need to support FW function w/o known size in advance.
540 * Caller can determine required size based upon nd_fw_size.
541 * If we return an error (like elsewhere) then caller wouldn't
542 * be able to rely upon data returned to make calculation.
549 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
550 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
551 dimm_name, cmd_name, out_obj->type);
556 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
557 cmd_name, out_obj->buffer.length);
558 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
559 out_obj->buffer.pointer,
560 min_t(u32, 128, out_obj->buffer.length), true);
562 for (i = 0, offset = 0; i < desc->out_num; i++) {
563 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
564 (u32 *) out_obj->buffer.pointer,
565 out_obj->buffer.length - offset);
567 if (offset + out_size > out_obj->buffer.length) {
568 dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
569 dimm_name, cmd_name, i);
573 if (in_buf.buffer.length + offset + out_size > buf_len) {
574 dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
575 dimm_name, cmd_name, i);
579 memcpy(buf + in_buf.buffer.length + offset,
580 out_obj->buffer.pointer + offset, out_size);
585 * Set fw_status for all the commands with a known format to be
586 * later interpreted by xlat_status().
588 if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
589 && cmd <= ND_CMD_CLEAR_ERROR)
590 || (nvdimm && cmd >= ND_CMD_SMART
591 && cmd <= ND_CMD_VENDOR)))
592 fw_status = *(u32 *) out_obj->buffer.pointer;
594 if (offset + in_buf.buffer.length < buf_len) {
597 * status valid, return the number of bytes left
598 * unfilled in the output buffer
600 rc = buf_len - offset - in_buf.buffer.length;
602 *cmd_rc = xlat_status(nvdimm, buf, cmd,
605 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
606 __func__, dimm_name, cmd_name, buf_len,
613 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
621 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
623 static const char *spa_type_name(u16 type)
625 static const char *to_name[] = {
626 [NFIT_SPA_VOLATILE] = "volatile",
627 [NFIT_SPA_PM] = "pmem",
628 [NFIT_SPA_DCR] = "dimm-control-region",
629 [NFIT_SPA_BDW] = "block-data-window",
630 [NFIT_SPA_VDISK] = "volatile-disk",
631 [NFIT_SPA_VCD] = "volatile-cd",
632 [NFIT_SPA_PDISK] = "persistent-disk",
633 [NFIT_SPA_PCD] = "persistent-cd",
637 if (type > NFIT_SPA_PCD)
640 return to_name[type];
643 int nfit_spa_type(struct acpi_nfit_system_address *spa)
647 for (i = 0; i < NFIT_UUID_MAX; i++)
648 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
653 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
654 struct nfit_table_prev *prev,
655 struct acpi_nfit_system_address *spa)
657 struct device *dev = acpi_desc->dev;
658 struct nfit_spa *nfit_spa;
660 if (spa->header.length != sizeof(*spa))
663 list_for_each_entry(nfit_spa, &prev->spas, list) {
664 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
665 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
670 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
674 INIT_LIST_HEAD(&nfit_spa->list);
675 memcpy(nfit_spa->spa, spa, sizeof(*spa));
676 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
677 dev_dbg(dev, "spa index: %d type: %s\n",
679 spa_type_name(nfit_spa_type(spa)));
683 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
684 struct nfit_table_prev *prev,
685 struct acpi_nfit_memory_map *memdev)
687 struct device *dev = acpi_desc->dev;
688 struct nfit_memdev *nfit_memdev;
690 if (memdev->header.length != sizeof(*memdev))
693 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
694 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
695 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
699 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
703 INIT_LIST_HEAD(&nfit_memdev->list);
704 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
705 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
706 dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
707 memdev->device_handle, memdev->range_index,
708 memdev->region_index, memdev->flags);
712 int nfit_get_smbios_id(u32 device_handle, u16 *flags)
714 struct acpi_nfit_memory_map *memdev;
715 struct acpi_nfit_desc *acpi_desc;
716 struct nfit_mem *nfit_mem;
719 mutex_lock(&acpi_desc_lock);
720 list_for_each_entry(acpi_desc, &acpi_descs, list) {
721 mutex_lock(&acpi_desc->init_mutex);
722 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
723 memdev = __to_nfit_memdev(nfit_mem);
724 if (memdev->device_handle == device_handle) {
725 *flags = memdev->flags;
726 physical_id = memdev->physical_id;
727 mutex_unlock(&acpi_desc->init_mutex);
728 mutex_unlock(&acpi_desc_lock);
732 mutex_unlock(&acpi_desc->init_mutex);
734 mutex_unlock(&acpi_desc_lock);
738 EXPORT_SYMBOL_GPL(nfit_get_smbios_id);
741 * An implementation may provide a truncated control region if no block windows
744 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
746 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
751 return offsetof(struct acpi_nfit_control_region, window_size);
754 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
755 struct nfit_table_prev *prev,
756 struct acpi_nfit_control_region *dcr)
758 struct device *dev = acpi_desc->dev;
759 struct nfit_dcr *nfit_dcr;
761 if (!sizeof_dcr(dcr))
764 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
765 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
766 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
770 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
774 INIT_LIST_HEAD(&nfit_dcr->list);
775 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
776 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
777 dev_dbg(dev, "dcr index: %d windows: %d\n",
778 dcr->region_index, dcr->windows);
782 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
783 struct nfit_table_prev *prev,
784 struct acpi_nfit_data_region *bdw)
786 struct device *dev = acpi_desc->dev;
787 struct nfit_bdw *nfit_bdw;
789 if (bdw->header.length != sizeof(*bdw))
791 list_for_each_entry(nfit_bdw, &prev->bdws, list)
792 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
793 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
797 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
801 INIT_LIST_HEAD(&nfit_bdw->list);
802 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
803 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
804 dev_dbg(dev, "bdw dcr: %d windows: %d\n",
805 bdw->region_index, bdw->windows);
809 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
811 if (idt->header.length < sizeof(*idt))
813 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
816 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
817 struct nfit_table_prev *prev,
818 struct acpi_nfit_interleave *idt)
820 struct device *dev = acpi_desc->dev;
821 struct nfit_idt *nfit_idt;
823 if (!sizeof_idt(idt))
826 list_for_each_entry(nfit_idt, &prev->idts, list) {
827 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
830 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
831 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
836 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
840 INIT_LIST_HEAD(&nfit_idt->list);
841 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
842 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
843 dev_dbg(dev, "idt index: %d num_lines: %d\n",
844 idt->interleave_index, idt->line_count);
848 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
850 if (flush->header.length < sizeof(*flush))
852 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
855 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
856 struct nfit_table_prev *prev,
857 struct acpi_nfit_flush_address *flush)
859 struct device *dev = acpi_desc->dev;
860 struct nfit_flush *nfit_flush;
862 if (!sizeof_flush(flush))
865 list_for_each_entry(nfit_flush, &prev->flushes, list) {
866 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
869 if (memcmp(nfit_flush->flush, flush,
870 sizeof_flush(flush)) == 0) {
871 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
876 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
877 + sizeof_flush(flush), GFP_KERNEL);
880 INIT_LIST_HEAD(&nfit_flush->list);
881 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
882 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
883 dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
884 flush->device_handle, flush->hint_count);
888 static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
889 struct acpi_nfit_capabilities *pcap)
891 struct device *dev = acpi_desc->dev;
894 mask = (1 << (pcap->highest_capability + 1)) - 1;
895 acpi_desc->platform_cap = pcap->capabilities & mask;
896 dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
900 static void *add_table(struct acpi_nfit_desc *acpi_desc,
901 struct nfit_table_prev *prev, void *table, const void *end)
903 struct device *dev = acpi_desc->dev;
904 struct acpi_nfit_header *hdr;
905 void *err = ERR_PTR(-ENOMEM);
912 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
918 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
919 if (!add_spa(acpi_desc, prev, table))
922 case ACPI_NFIT_TYPE_MEMORY_MAP:
923 if (!add_memdev(acpi_desc, prev, table))
926 case ACPI_NFIT_TYPE_CONTROL_REGION:
927 if (!add_dcr(acpi_desc, prev, table))
930 case ACPI_NFIT_TYPE_DATA_REGION:
931 if (!add_bdw(acpi_desc, prev, table))
934 case ACPI_NFIT_TYPE_INTERLEAVE:
935 if (!add_idt(acpi_desc, prev, table))
938 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
939 if (!add_flush(acpi_desc, prev, table))
942 case ACPI_NFIT_TYPE_SMBIOS:
943 dev_dbg(dev, "smbios\n");
945 case ACPI_NFIT_TYPE_CAPABILITIES:
946 if (!add_platform_cap(acpi_desc, table))
950 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
954 return table + hdr->length;
957 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
958 struct nfit_mem *nfit_mem)
960 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
961 u16 dcr = nfit_mem->dcr->region_index;
962 struct nfit_spa *nfit_spa;
964 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
965 u16 range_index = nfit_spa->spa->range_index;
966 int type = nfit_spa_type(nfit_spa->spa);
967 struct nfit_memdev *nfit_memdev;
969 if (type != NFIT_SPA_BDW)
972 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
973 if (nfit_memdev->memdev->range_index != range_index)
975 if (nfit_memdev->memdev->device_handle != device_handle)
977 if (nfit_memdev->memdev->region_index != dcr)
980 nfit_mem->spa_bdw = nfit_spa->spa;
985 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
986 nfit_mem->spa_dcr->range_index);
987 nfit_mem->bdw = NULL;
990 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
991 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
993 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
994 struct nfit_memdev *nfit_memdev;
995 struct nfit_bdw *nfit_bdw;
996 struct nfit_idt *nfit_idt;
997 u16 idt_idx, range_index;
999 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
1000 if (nfit_bdw->bdw->region_index != dcr)
1002 nfit_mem->bdw = nfit_bdw->bdw;
1009 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
1011 if (!nfit_mem->spa_bdw)
1014 range_index = nfit_mem->spa_bdw->range_index;
1015 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1016 if (nfit_memdev->memdev->range_index != range_index ||
1017 nfit_memdev->memdev->region_index != dcr)
1019 nfit_mem->memdev_bdw = nfit_memdev->memdev;
1020 idt_idx = nfit_memdev->memdev->interleave_index;
1021 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1022 if (nfit_idt->idt->interleave_index != idt_idx)
1024 nfit_mem->idt_bdw = nfit_idt->idt;
1031 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
1032 struct acpi_nfit_system_address *spa)
1034 struct nfit_mem *nfit_mem, *found;
1035 struct nfit_memdev *nfit_memdev;
1036 int type = spa ? nfit_spa_type(spa) : 0;
1048 * This loop runs in two modes, when a dimm is mapped the loop
1049 * adds memdev associations to an existing dimm, or creates a
1050 * dimm. In the unmapped dimm case this loop sweeps for memdev
1051 * instances with an invalid / zero range_index and adds those
1052 * dimms without spa associations.
1054 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1055 struct nfit_flush *nfit_flush;
1056 struct nfit_dcr *nfit_dcr;
1060 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
1062 if (!spa && nfit_memdev->memdev->range_index)
1065 dcr = nfit_memdev->memdev->region_index;
1066 device_handle = nfit_memdev->memdev->device_handle;
1067 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1068 if (__to_nfit_memdev(nfit_mem)->device_handle
1077 nfit_mem = devm_kzalloc(acpi_desc->dev,
1078 sizeof(*nfit_mem), GFP_KERNEL);
1081 INIT_LIST_HEAD(&nfit_mem->list);
1082 nfit_mem->acpi_desc = acpi_desc;
1083 list_add(&nfit_mem->list, &acpi_desc->dimms);
1086 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1087 if (nfit_dcr->dcr->region_index != dcr)
1090 * Record the control region for the dimm. For
1091 * the ACPI 6.1 case, where there are separate
1092 * control regions for the pmem vs blk
1093 * interfaces, be sure to record the extended
1097 nfit_mem->dcr = nfit_dcr->dcr;
1098 else if (nfit_mem->dcr->windows == 0
1099 && nfit_dcr->dcr->windows)
1100 nfit_mem->dcr = nfit_dcr->dcr;
1104 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
1105 struct acpi_nfit_flush_address *flush;
1108 if (nfit_flush->flush->device_handle != device_handle)
1110 nfit_mem->nfit_flush = nfit_flush;
1111 flush = nfit_flush->flush;
1112 nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
1114 sizeof(struct resource),
1116 if (!nfit_mem->flush_wpq)
1118 for (i = 0; i < flush->hint_count; i++) {
1119 struct resource *res = &nfit_mem->flush_wpq[i];
1121 res->start = flush->hint_address[i];
1122 res->end = res->start + 8 - 1;
1127 if (dcr && !nfit_mem->dcr) {
1128 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
1129 spa->range_index, dcr);
1133 if (type == NFIT_SPA_DCR) {
1134 struct nfit_idt *nfit_idt;
1137 /* multiple dimms may share a SPA when interleaved */
1138 nfit_mem->spa_dcr = spa;
1139 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1140 idt_idx = nfit_memdev->memdev->interleave_index;
1141 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1142 if (nfit_idt->idt->interleave_index != idt_idx)
1144 nfit_mem->idt_dcr = nfit_idt->idt;
1147 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
1148 } else if (type == NFIT_SPA_PM) {
1150 * A single dimm may belong to multiple SPA-PM
1151 * ranges, record at least one in addition to
1152 * any SPA-DCR range.
1154 nfit_mem->memdev_pmem = nfit_memdev->memdev;
1156 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1162 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
1164 struct nfit_mem *a = container_of(_a, typeof(*a), list);
1165 struct nfit_mem *b = container_of(_b, typeof(*b), list);
1166 u32 handleA, handleB;
1168 handleA = __to_nfit_memdev(a)->device_handle;
1169 handleB = __to_nfit_memdev(b)->device_handle;
1170 if (handleA < handleB)
1172 else if (handleA > handleB)
1177 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
1179 struct nfit_spa *nfit_spa;
1184 * For each SPA-DCR or SPA-PMEM address range find its
1185 * corresponding MEMDEV(s). From each MEMDEV find the
1186 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1187 * try to find a SPA-BDW and a corresponding BDW that references
1188 * the DCR. Throw it all into an nfit_mem object. Note, that
1189 * BDWs are optional.
1191 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1192 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
1198 * If a DIMM has failed to be mapped into SPA there will be no
1199 * SPA entries above. Find and register all the unmapped DIMMs
1200 * for reporting and recovery purposes.
1202 rc = __nfit_mem_init(acpi_desc, NULL);
1206 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
1211 static ssize_t bus_dsm_mask_show(struct device *dev,
1212 struct device_attribute *attr, char *buf)
1214 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1215 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1217 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
1219 static struct device_attribute dev_attr_bus_dsm_mask =
1220 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
1222 static ssize_t revision_show(struct device *dev,
1223 struct device_attribute *attr, char *buf)
1225 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1226 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1227 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1229 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
1231 static DEVICE_ATTR_RO(revision);
1233 static ssize_t hw_error_scrub_show(struct device *dev,
1234 struct device_attribute *attr, char *buf)
1236 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1237 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1238 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1240 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
1244 * The 'hw_error_scrub' attribute can have the following values written to it:
1245 * '0': Switch to the default mode where an exception will only insert
1246 * the address of the memory error into the poison and badblocks lists.
1247 * '1': Enable a full scrub to happen if an exception for a memory error is
1250 static ssize_t hw_error_scrub_store(struct device *dev,
1251 struct device_attribute *attr, const char *buf, size_t size)
1253 struct nvdimm_bus_descriptor *nd_desc;
1257 rc = kstrtol(buf, 0, &val);
1262 nd_desc = dev_get_drvdata(dev);
1264 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1267 case HW_ERROR_SCRUB_ON:
1268 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1270 case HW_ERROR_SCRUB_OFF:
1271 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1283 static DEVICE_ATTR_RW(hw_error_scrub);
1286 * This shows the number of full Address Range Scrubs that have been
1287 * completed since driver load time. Userspace can wait on this using
1288 * select/poll etc. A '+' at the end indicates an ARS is in progress
1290 static ssize_t scrub_show(struct device *dev,
1291 struct device_attribute *attr, char *buf)
1293 struct nvdimm_bus_descriptor *nd_desc;
1294 ssize_t rc = -ENXIO;
1297 nd_desc = dev_get_drvdata(dev);
1299 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1301 mutex_lock(&acpi_desc->init_mutex);
1302 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1303 acpi_desc->scrub_busy
1304 && !acpi_desc->cancel ? "+\n" : "\n");
1305 mutex_unlock(&acpi_desc->init_mutex);
1311 static ssize_t scrub_store(struct device *dev,
1312 struct device_attribute *attr, const char *buf, size_t size)
1314 struct nvdimm_bus_descriptor *nd_desc;
1318 rc = kstrtol(buf, 0, &val);
1325 nd_desc = dev_get_drvdata(dev);
1327 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1329 rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
1336 static DEVICE_ATTR_RW(scrub);
1338 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1340 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1341 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1342 | 1 << ND_CMD_ARS_STATUS;
1344 return (nd_desc->cmd_mask & mask) == mask;
1347 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1349 struct device *dev = container_of(kobj, struct device, kobj);
1350 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1352 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1357 static struct attribute *acpi_nfit_attributes[] = {
1358 &dev_attr_revision.attr,
1359 &dev_attr_scrub.attr,
1360 &dev_attr_hw_error_scrub.attr,
1361 &dev_attr_bus_dsm_mask.attr,
1365 static const struct attribute_group acpi_nfit_attribute_group = {
1367 .attrs = acpi_nfit_attributes,
1368 .is_visible = nfit_visible,
1371 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1372 &nvdimm_bus_attribute_group,
1373 &acpi_nfit_attribute_group,
1377 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1379 struct nvdimm *nvdimm = to_nvdimm(dev);
1380 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1382 return __to_nfit_memdev(nfit_mem);
1385 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1387 struct nvdimm *nvdimm = to_nvdimm(dev);
1388 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1390 return nfit_mem->dcr;
1393 static ssize_t handle_show(struct device *dev,
1394 struct device_attribute *attr, char *buf)
1396 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1398 return sprintf(buf, "%#x\n", memdev->device_handle);
1400 static DEVICE_ATTR_RO(handle);
1402 static ssize_t phys_id_show(struct device *dev,
1403 struct device_attribute *attr, char *buf)
1405 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1407 return sprintf(buf, "%#x\n", memdev->physical_id);
1409 static DEVICE_ATTR_RO(phys_id);
1411 static ssize_t vendor_show(struct device *dev,
1412 struct device_attribute *attr, char *buf)
1414 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1416 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1418 static DEVICE_ATTR_RO(vendor);
1420 static ssize_t rev_id_show(struct device *dev,
1421 struct device_attribute *attr, char *buf)
1423 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1425 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1427 static DEVICE_ATTR_RO(rev_id);
1429 static ssize_t device_show(struct device *dev,
1430 struct device_attribute *attr, char *buf)
1432 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1434 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1436 static DEVICE_ATTR_RO(device);
1438 static ssize_t subsystem_vendor_show(struct device *dev,
1439 struct device_attribute *attr, char *buf)
1441 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1443 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1445 static DEVICE_ATTR_RO(subsystem_vendor);
1447 static ssize_t subsystem_rev_id_show(struct device *dev,
1448 struct device_attribute *attr, char *buf)
1450 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1452 return sprintf(buf, "0x%04x\n",
1453 be16_to_cpu(dcr->subsystem_revision_id));
1455 static DEVICE_ATTR_RO(subsystem_rev_id);
1457 static ssize_t subsystem_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->subsystem_device_id));
1464 static DEVICE_ATTR_RO(subsystem_device);
1466 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1468 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1471 if (nfit_mem->memdev_pmem)
1473 if (nfit_mem->memdev_bdw)
1478 static ssize_t format_show(struct device *dev,
1479 struct device_attribute *attr, char *buf)
1481 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1483 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1485 static DEVICE_ATTR_RO(format);
1487 static ssize_t format1_show(struct device *dev,
1488 struct device_attribute *attr, char *buf)
1491 ssize_t rc = -ENXIO;
1492 struct nfit_mem *nfit_mem;
1493 struct nfit_memdev *nfit_memdev;
1494 struct acpi_nfit_desc *acpi_desc;
1495 struct nvdimm *nvdimm = to_nvdimm(dev);
1496 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1498 nfit_mem = nvdimm_provider_data(nvdimm);
1499 acpi_desc = nfit_mem->acpi_desc;
1500 handle = to_nfit_memdev(dev)->device_handle;
1502 /* assumes DIMMs have at most 2 published interface codes */
1503 mutex_lock(&acpi_desc->init_mutex);
1504 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1505 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1506 struct nfit_dcr *nfit_dcr;
1508 if (memdev->device_handle != handle)
1511 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1512 if (nfit_dcr->dcr->region_index != memdev->region_index)
1514 if (nfit_dcr->dcr->code == dcr->code)
1516 rc = sprintf(buf, "0x%04x\n",
1517 le16_to_cpu(nfit_dcr->dcr->code));
1523 mutex_unlock(&acpi_desc->init_mutex);
1526 static DEVICE_ATTR_RO(format1);
1528 static ssize_t formats_show(struct device *dev,
1529 struct device_attribute *attr, char *buf)
1531 struct nvdimm *nvdimm = to_nvdimm(dev);
1533 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1535 static DEVICE_ATTR_RO(formats);
1537 static ssize_t serial_show(struct device *dev,
1538 struct device_attribute *attr, char *buf)
1540 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1542 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1544 static DEVICE_ATTR_RO(serial);
1546 static ssize_t family_show(struct device *dev,
1547 struct device_attribute *attr, char *buf)
1549 struct nvdimm *nvdimm = to_nvdimm(dev);
1550 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1552 if (nfit_mem->family < 0)
1554 return sprintf(buf, "%d\n", nfit_mem->family);
1556 static DEVICE_ATTR_RO(family);
1558 static ssize_t dsm_mask_show(struct device *dev,
1559 struct device_attribute *attr, char *buf)
1561 struct nvdimm *nvdimm = to_nvdimm(dev);
1562 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1564 if (nfit_mem->family < 0)
1566 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1568 static DEVICE_ATTR_RO(dsm_mask);
1570 static ssize_t flags_show(struct device *dev,
1571 struct device_attribute *attr, char *buf)
1573 struct nvdimm *nvdimm = to_nvdimm(dev);
1574 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1575 u16 flags = __to_nfit_memdev(nfit_mem)->flags;
1577 if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
1578 flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
1580 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1581 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1582 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1583 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1584 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1585 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1586 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1587 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1589 static DEVICE_ATTR_RO(flags);
1591 static ssize_t id_show(struct device *dev,
1592 struct device_attribute *attr, char *buf)
1594 struct nvdimm *nvdimm = to_nvdimm(dev);
1595 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1597 return sprintf(buf, "%s\n", nfit_mem->id);
1599 static DEVICE_ATTR_RO(id);
1601 static ssize_t dirty_shutdown_show(struct device *dev,
1602 struct device_attribute *attr, char *buf)
1604 struct nvdimm *nvdimm = to_nvdimm(dev);
1605 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1607 return sprintf(buf, "%d\n", nfit_mem->dirty_shutdown);
1609 static DEVICE_ATTR_RO(dirty_shutdown);
1611 static struct attribute *acpi_nfit_dimm_attributes[] = {
1612 &dev_attr_handle.attr,
1613 &dev_attr_phys_id.attr,
1614 &dev_attr_vendor.attr,
1615 &dev_attr_device.attr,
1616 &dev_attr_rev_id.attr,
1617 &dev_attr_subsystem_vendor.attr,
1618 &dev_attr_subsystem_device.attr,
1619 &dev_attr_subsystem_rev_id.attr,
1620 &dev_attr_format.attr,
1621 &dev_attr_formats.attr,
1622 &dev_attr_format1.attr,
1623 &dev_attr_serial.attr,
1624 &dev_attr_flags.attr,
1626 &dev_attr_family.attr,
1627 &dev_attr_dsm_mask.attr,
1628 &dev_attr_dirty_shutdown.attr,
1632 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1633 struct attribute *a, int n)
1635 struct device *dev = container_of(kobj, struct device, kobj);
1636 struct nvdimm *nvdimm = to_nvdimm(dev);
1637 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1639 if (!to_nfit_dcr(dev)) {
1640 /* Without a dcr only the memdev attributes can be surfaced */
1641 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1642 || a == &dev_attr_flags.attr
1643 || a == &dev_attr_family.attr
1644 || a == &dev_attr_dsm_mask.attr)
1649 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1652 if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
1653 && a == &dev_attr_dirty_shutdown.attr)
1659 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1661 .attrs = acpi_nfit_dimm_attributes,
1662 .is_visible = acpi_nfit_dimm_attr_visible,
1665 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1666 &nvdimm_attribute_group,
1667 &nd_device_attribute_group,
1668 &acpi_nfit_dimm_attribute_group,
1672 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1675 struct nfit_mem *nfit_mem;
1677 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1678 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1679 return nfit_mem->nvdimm;
1684 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1686 struct nfit_mem *nfit_mem;
1687 struct acpi_nfit_desc *acpi_desc;
1689 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
1692 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1693 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1698 acpi_desc = dev_get_drvdata(dev->parent);
1703 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1706 nfit_mem = dev_get_drvdata(dev);
1707 if (nfit_mem && nfit_mem->flags_attr)
1708 sysfs_notify_dirent(nfit_mem->flags_attr);
1710 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1712 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1714 struct acpi_device *adev = data;
1715 struct device *dev = &adev->dev;
1717 device_lock(dev->parent);
1718 __acpi_nvdimm_notify(dev, event);
1719 device_unlock(dev->parent);
1722 static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1727 status = acpi_get_handle(adev->handle, method, &handle);
1729 if (ACPI_SUCCESS(status))
1734 __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
1736 struct nd_intel_smart smart = { 0 };
1737 union acpi_object in_buf = {
1738 .type = ACPI_TYPE_BUFFER,
1739 .buffer.pointer = (char *) &smart,
1740 .buffer.length = sizeof(smart),
1742 union acpi_object in_obj = {
1743 .type = ACPI_TYPE_PACKAGE,
1745 .package.elements = &in_buf,
1747 const u8 func = ND_INTEL_SMART;
1748 const guid_t *guid = to_nfit_uuid(nfit_mem->family);
1749 u8 revid = nfit_dsm_revid(nfit_mem->family, func);
1750 struct acpi_device *adev = nfit_mem->adev;
1751 acpi_handle handle = adev->handle;
1752 union acpi_object *out_obj;
1754 if ((nfit_mem->dsm_mask & (1 << func)) == 0)
1757 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
1761 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
1762 if (smart.shutdown_state)
1763 set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
1766 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
1767 set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
1768 nfit_mem->dirty_shutdown = smart.shutdown_count;
1773 static void populate_shutdown_status(struct nfit_mem *nfit_mem)
1776 * For DIMMs that provide a dynamic facility to retrieve a
1777 * dirty-shutdown status and/or a dirty-shutdown count, cache
1778 * these values in nfit_mem.
1780 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1781 nfit_intel_shutdown_status(nfit_mem);
1784 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1785 struct nfit_mem *nfit_mem, u32 device_handle)
1787 struct acpi_device *adev, *adev_dimm;
1788 struct device *dev = acpi_desc->dev;
1789 unsigned long dsm_mask, label_mask;
1793 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1795 /* nfit test assumes 1:1 relationship between commands and dsms */
1796 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1797 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1799 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1800 sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
1801 be16_to_cpu(dcr->vendor_id),
1802 dcr->manufacturing_location,
1803 be16_to_cpu(dcr->manufacturing_date),
1804 be32_to_cpu(dcr->serial_number));
1806 sprintf(nfit_mem->id, "%04x-%08x",
1807 be16_to_cpu(dcr->vendor_id),
1808 be32_to_cpu(dcr->serial_number));
1810 adev = to_acpi_dev(acpi_desc);
1812 /* unit test case */
1813 populate_shutdown_status(nfit_mem);
1817 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1818 nfit_mem->adev = adev_dimm;
1820 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1822 return force_enable_dimms ? 0 : -ENODEV;
1825 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1826 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1827 dev_err(dev, "%s: notification registration failed\n",
1828 dev_name(&adev_dimm->dev));
1832 * Record nfit_mem for the notification path to track back to
1833 * the nfit sysfs attributes for this dimm device object.
1835 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1838 * Until standardization materializes we need to consider 4
1839 * different command sets. Note, that checking for function0 (bit0)
1840 * tells us if any commands are reachable through this GUID.
1842 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
1843 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1844 if (family < 0 || i == default_dsm_family)
1847 /* limit the supported commands to those that are publicly documented */
1848 nfit_mem->family = family;
1849 if (override_dsm_mask && !disable_vendor_specific)
1850 dsm_mask = override_dsm_mask;
1851 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1852 dsm_mask = NVDIMM_INTEL_CMDMASK;
1853 if (disable_vendor_specific)
1854 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1855 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1856 dsm_mask = 0x1c3c76;
1857 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1859 if (disable_vendor_specific)
1860 dsm_mask &= ~(1 << 8);
1861 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1862 dsm_mask = 0xffffffff;
1864 dev_dbg(dev, "unknown dimm command family\n");
1865 nfit_mem->family = -1;
1866 /* DSMs are optional, continue loading the driver... */
1870 guid = to_nfit_uuid(nfit_mem->family);
1871 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1872 if (acpi_check_dsm(adev_dimm->handle, guid,
1873 nfit_dsm_revid(nfit_mem->family, i),
1875 set_bit(i, &nfit_mem->dsm_mask);
1878 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1879 * due to their better semantics handling locked capacity.
1881 label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
1882 | 1 << ND_CMD_SET_CONFIG_DATA;
1883 if (family == NVDIMM_FAMILY_INTEL
1884 && (dsm_mask & label_mask) == label_mask)
1887 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1888 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
1889 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
1890 set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1893 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
1894 && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
1895 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
1896 set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
1899 populate_shutdown_status(nfit_mem);
1904 static void shutdown_dimm_notify(void *data)
1906 struct acpi_nfit_desc *acpi_desc = data;
1907 struct nfit_mem *nfit_mem;
1909 mutex_lock(&acpi_desc->init_mutex);
1911 * Clear out the nfit_mem->flags_attr and shut down dimm event
1914 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1915 struct acpi_device *adev_dimm = nfit_mem->adev;
1917 if (nfit_mem->flags_attr) {
1918 sysfs_put(nfit_mem->flags_attr);
1919 nfit_mem->flags_attr = NULL;
1922 acpi_remove_notify_handler(adev_dimm->handle,
1923 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1924 dev_set_drvdata(&adev_dimm->dev, NULL);
1927 mutex_unlock(&acpi_desc->init_mutex);
1930 static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
1933 case NVDIMM_FAMILY_INTEL:
1934 return intel_security_ops;
1940 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1942 struct nfit_mem *nfit_mem;
1943 int dimm_count = 0, rc;
1944 struct nvdimm *nvdimm;
1946 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1947 struct acpi_nfit_flush_address *flush;
1948 unsigned long flags = 0, cmd_mask;
1949 struct nfit_memdev *nfit_memdev;
1953 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1954 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1960 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1961 set_bit(NDD_ALIASING, &flags);
1963 /* collate flags across all memdevs for this dimm */
1964 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1965 struct acpi_nfit_memory_map *dimm_memdev;
1967 dimm_memdev = __to_nfit_memdev(nfit_mem);
1968 if (dimm_memdev->device_handle
1969 != nfit_memdev->memdev->device_handle)
1971 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1974 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
1975 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
1976 set_bit(NDD_UNARMED, &flags);
1978 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1983 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1984 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1985 * userspace interface.
1987 cmd_mask = 1UL << ND_CMD_CALL;
1988 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1990 * These commands have a 1:1 correspondence
1991 * between DSM payload and libnvdimm ioctl
1994 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
1997 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
1998 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
1999 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
2001 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
2002 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
2004 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
2006 nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
2007 acpi_nfit_dimm_attribute_groups,
2008 flags, cmd_mask, flush ? flush->hint_count : 0,
2009 nfit_mem->flush_wpq, &nfit_mem->id[0],
2010 acpi_nfit_get_security_ops(nfit_mem->family));
2014 nfit_mem->nvdimm = nvdimm;
2017 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
2020 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
2021 nvdimm_name(nvdimm),
2022 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
2023 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
2024 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
2025 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
2026 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
2030 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
2035 * Now that dimms are successfully registered, and async registration
2036 * is flushed, attempt to enable event notification.
2038 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2039 struct kernfs_node *nfit_kernfs;
2041 nvdimm = nfit_mem->nvdimm;
2045 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
2047 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
2049 sysfs_put(nfit_kernfs);
2050 if (!nfit_mem->flags_attr)
2051 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
2052 nvdimm_name(nvdimm));
2055 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
2060 * These constants are private because there are no kernel consumers of
2063 enum nfit_aux_cmds {
2064 NFIT_CMD_TRANSLATE_SPA = 5,
2065 NFIT_CMD_ARS_INJECT_SET = 7,
2066 NFIT_CMD_ARS_INJECT_CLEAR = 8,
2067 NFIT_CMD_ARS_INJECT_GET = 9,
2070 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
2072 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2073 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
2074 struct acpi_device *adev;
2075 unsigned long dsm_mask;
2078 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
2079 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
2080 adev = to_acpi_dev(acpi_desc);
2084 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
2085 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2086 set_bit(i, &nd_desc->cmd_mask);
2087 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
2090 (1 << ND_CMD_ARS_CAP) |
2091 (1 << ND_CMD_ARS_START) |
2092 (1 << ND_CMD_ARS_STATUS) |
2093 (1 << ND_CMD_CLEAR_ERROR) |
2094 (1 << NFIT_CMD_TRANSLATE_SPA) |
2095 (1 << NFIT_CMD_ARS_INJECT_SET) |
2096 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
2097 (1 << NFIT_CMD_ARS_INJECT_GET);
2098 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
2099 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2100 set_bit(i, &nd_desc->bus_dsm_mask);
2103 static ssize_t range_index_show(struct device *dev,
2104 struct device_attribute *attr, char *buf)
2106 struct nd_region *nd_region = to_nd_region(dev);
2107 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
2109 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
2111 static DEVICE_ATTR_RO(range_index);
2113 static struct attribute *acpi_nfit_region_attributes[] = {
2114 &dev_attr_range_index.attr,
2118 static const struct attribute_group acpi_nfit_region_attribute_group = {
2120 .attrs = acpi_nfit_region_attributes,
2123 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
2124 &nd_region_attribute_group,
2125 &nd_mapping_attribute_group,
2126 &nd_device_attribute_group,
2127 &nd_numa_attribute_group,
2128 &acpi_nfit_region_attribute_group,
2132 /* enough info to uniquely specify an interleave set */
2133 struct nfit_set_info {
2134 struct nfit_set_info_map {
2141 struct nfit_set_info2 {
2142 struct nfit_set_info_map2 {
2146 u16 manufacturing_date;
2147 u8 manufacturing_location;
2152 static size_t sizeof_nfit_set_info(int num_mappings)
2154 return sizeof(struct nfit_set_info)
2155 + num_mappings * sizeof(struct nfit_set_info_map);
2158 static size_t sizeof_nfit_set_info2(int num_mappings)
2160 return sizeof(struct nfit_set_info2)
2161 + num_mappings * sizeof(struct nfit_set_info_map2);
2164 static int cmp_map_compat(const void *m0, const void *m1)
2166 const struct nfit_set_info_map *map0 = m0;
2167 const struct nfit_set_info_map *map1 = m1;
2169 return memcmp(&map0->region_offset, &map1->region_offset,
2173 static int cmp_map(const void *m0, const void *m1)
2175 const struct nfit_set_info_map *map0 = m0;
2176 const struct nfit_set_info_map *map1 = m1;
2178 if (map0->region_offset < map1->region_offset)
2180 else if (map0->region_offset > map1->region_offset)
2185 static int cmp_map2(const void *m0, const void *m1)
2187 const struct nfit_set_info_map2 *map0 = m0;
2188 const struct nfit_set_info_map2 *map1 = m1;
2190 if (map0->region_offset < map1->region_offset)
2192 else if (map0->region_offset > map1->region_offset)
2197 /* Retrieve the nth entry referencing this spa */
2198 static struct acpi_nfit_memory_map *memdev_from_spa(
2199 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2201 struct nfit_memdev *nfit_memdev;
2203 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2204 if (nfit_memdev->memdev->range_index == range_index)
2206 return nfit_memdev->memdev;
2210 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2211 struct nd_region_desc *ndr_desc,
2212 struct acpi_nfit_system_address *spa)
2214 struct device *dev = acpi_desc->dev;
2215 struct nd_interleave_set *nd_set;
2216 u16 nr = ndr_desc->num_mappings;
2217 struct nfit_set_info2 *info2;
2218 struct nfit_set_info *info;
2221 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2224 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2226 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2230 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2234 for (i = 0; i < nr; i++) {
2235 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
2236 struct nfit_set_info_map *map = &info->mapping[i];
2237 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2238 struct nvdimm *nvdimm = mapping->nvdimm;
2239 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2240 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2241 spa->range_index, i);
2242 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2244 if (!memdev || !nfit_mem->dcr) {
2245 dev_err(dev, "%s: failed to find DCR\n", __func__);
2249 map->region_offset = memdev->region_offset;
2250 map->serial_number = dcr->serial_number;
2252 map2->region_offset = memdev->region_offset;
2253 map2->serial_number = dcr->serial_number;
2254 map2->vendor_id = dcr->vendor_id;
2255 map2->manufacturing_date = dcr->manufacturing_date;
2256 map2->manufacturing_location = dcr->manufacturing_location;
2259 /* v1.1 namespaces */
2260 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2262 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2264 /* v1.2 namespaces */
2265 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2267 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2269 /* support v1.1 namespaces created with the wrong sort order */
2270 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2271 cmp_map_compat, NULL);
2272 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2274 /* record the result of the sort for the mapping position */
2275 for (i = 0; i < nr; i++) {
2276 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2279 for (j = 0; j < nr; j++) {
2280 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2281 struct nvdimm *nvdimm = mapping->nvdimm;
2282 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2283 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2285 if (map2->serial_number == dcr->serial_number &&
2286 map2->vendor_id == dcr->vendor_id &&
2287 map2->manufacturing_date == dcr->manufacturing_date &&
2288 map2->manufacturing_location
2289 == dcr->manufacturing_location) {
2290 mapping->position = i;
2296 ndr_desc->nd_set = nd_set;
2297 devm_kfree(dev, info);
2298 devm_kfree(dev, info2);
2303 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2305 struct acpi_nfit_interleave *idt = mmio->idt;
2306 u32 sub_line_offset, line_index, line_offset;
2307 u64 line_no, table_skip_count, table_offset;
2309 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2310 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2311 line_offset = idt->line_offset[line_index]
2313 table_offset = table_skip_count * mmio->table_size;
2315 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2318 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
2320 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2321 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
2322 const u32 STATUS_MASK = 0x80000037;
2324 if (mmio->num_lines)
2325 offset = to_interleave_offset(offset, mmio);
2327 return readl(mmio->addr.base + offset) & STATUS_MASK;
2330 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2331 resource_size_t dpa, unsigned int len, unsigned int write)
2334 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2337 BCW_OFFSET_MASK = (1ULL << 48)-1,
2339 BCW_LEN_MASK = (1ULL << 8) - 1,
2343 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2344 len = len >> L1_CACHE_SHIFT;
2345 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2346 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2348 offset = nfit_blk->cmd_offset + mmio->size * bw;
2349 if (mmio->num_lines)
2350 offset = to_interleave_offset(offset, mmio);
2352 writeq(cmd, mmio->addr.base + offset);
2353 nvdimm_flush(nfit_blk->nd_region);
2355 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
2356 readq(mmio->addr.base + offset);
2359 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2360 resource_size_t dpa, void *iobuf, size_t len, int rw,
2363 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2364 unsigned int copied = 0;
2368 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2369 + lane * mmio->size;
2370 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2375 if (mmio->num_lines) {
2378 offset = to_interleave_offset(base_offset + copied,
2380 div_u64_rem(offset, mmio->line_size, &line_offset);
2381 c = min_t(size_t, len, mmio->line_size - line_offset);
2383 offset = base_offset + nfit_blk->bdw_offset;
2388 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2390 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2391 arch_invalidate_pmem((void __force *)
2392 mmio->addr.aperture + offset, c);
2394 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2402 nvdimm_flush(nfit_blk->nd_region);
2404 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2408 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2409 resource_size_t dpa, void *iobuf, u64 len, int rw)
2411 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2412 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2413 struct nd_region *nd_region = nfit_blk->nd_region;
2414 unsigned int lane, copied = 0;
2417 lane = nd_region_acquire_lane(nd_region);
2419 u64 c = min(len, mmio->size);
2421 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2422 iobuf + copied, c, rw, lane);
2429 nd_region_release_lane(nd_region, lane);
2434 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2435 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2438 mmio->num_lines = idt->line_count;
2439 mmio->line_size = idt->line_size;
2440 if (interleave_ways == 0)
2442 mmio->table_size = mmio->num_lines * interleave_ways
2449 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2450 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2452 struct nd_cmd_dimm_flags flags;
2455 memset(&flags, 0, sizeof(flags));
2456 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2457 sizeof(flags), NULL);
2459 if (rc >= 0 && flags.status == 0)
2460 nfit_blk->dimm_flags = flags.flags;
2461 else if (rc == -ENOTTY) {
2462 /* fall back to a conservative default */
2463 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2471 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2474 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2475 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2476 struct nfit_blk_mmio *mmio;
2477 struct nfit_blk *nfit_blk;
2478 struct nfit_mem *nfit_mem;
2479 struct nvdimm *nvdimm;
2482 nvdimm = nd_blk_region_to_dimm(ndbr);
2483 nfit_mem = nvdimm_provider_data(nvdimm);
2484 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2485 dev_dbg(dev, "missing%s%s%s\n",
2486 nfit_mem ? "" : " nfit_mem",
2487 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2488 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2492 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2495 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2496 nfit_blk->nd_region = to_nd_region(dev);
2498 /* map block aperture memory */
2499 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2500 mmio = &nfit_blk->mmio[BDW];
2501 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2502 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2503 if (!mmio->addr.base) {
2504 dev_dbg(dev, "%s failed to map bdw\n",
2505 nvdimm_name(nvdimm));
2508 mmio->size = nfit_mem->bdw->size;
2509 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2510 mmio->idt = nfit_mem->idt_bdw;
2511 mmio->spa = nfit_mem->spa_bdw;
2512 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2513 nfit_mem->memdev_bdw->interleave_ways);
2515 dev_dbg(dev, "%s failed to init bdw interleave\n",
2516 nvdimm_name(nvdimm));
2520 /* map block control memory */
2521 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2522 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2523 mmio = &nfit_blk->mmio[DCR];
2524 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2525 nfit_mem->spa_dcr->length);
2526 if (!mmio->addr.base) {
2527 dev_dbg(dev, "%s failed to map dcr\n",
2528 nvdimm_name(nvdimm));
2531 mmio->size = nfit_mem->dcr->window_size;
2532 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2533 mmio->idt = nfit_mem->idt_dcr;
2534 mmio->spa = nfit_mem->spa_dcr;
2535 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2536 nfit_mem->memdev_dcr->interleave_ways);
2538 dev_dbg(dev, "%s failed to init dcr interleave\n",
2539 nvdimm_name(nvdimm));
2543 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2545 dev_dbg(dev, "%s failed get DIMM flags\n",
2546 nvdimm_name(nvdimm));
2550 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2551 dev_warn(dev, "unable to guarantee persistence of writes\n");
2553 if (mmio->line_size == 0)
2556 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2557 + 8 > mmio->line_size) {
2558 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2560 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2561 + 8 > mmio->line_size) {
2562 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2569 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2570 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2572 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2573 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2576 cmd->address = spa->address;
2577 cmd->length = spa->length;
2578 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2579 sizeof(*cmd), &cmd_rc);
2585 static int ars_start(struct acpi_nfit_desc *acpi_desc,
2586 struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
2590 struct nd_cmd_ars_start ars_start;
2591 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2592 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2594 memset(&ars_start, 0, sizeof(ars_start));
2595 ars_start.address = spa->address;
2596 ars_start.length = spa->length;
2597 if (req_type == ARS_REQ_SHORT)
2598 ars_start.flags = ND_ARS_RETURN_PREV_DATA;
2599 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2600 ars_start.type = ND_ARS_PERSISTENT;
2601 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2602 ars_start.type = ND_ARS_VOLATILE;
2606 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2607 sizeof(ars_start), &cmd_rc);
2614 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2617 struct nd_cmd_ars_start ars_start;
2618 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2619 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2621 memset(&ars_start, 0, sizeof(ars_start));
2622 ars_start.address = ars_status->restart_address;
2623 ars_start.length = ars_status->restart_length;
2624 ars_start.type = ars_status->type;
2625 ars_start.flags = acpi_desc->ars_start_flags;
2626 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2627 sizeof(ars_start), &cmd_rc);
2633 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2635 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2636 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2639 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2640 acpi_desc->max_ars, &cmd_rc);
2646 static void ars_complete(struct acpi_nfit_desc *acpi_desc,
2647 struct nfit_spa *nfit_spa)
2649 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2650 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2651 struct nd_region *nd_region = nfit_spa->nd_region;
2654 lockdep_assert_held(&acpi_desc->init_mutex);
2656 * Only advance the ARS state for ARS runs initiated by the
2657 * kernel, ignore ARS results from BIOS initiated runs for scrub
2658 * completion tracking.
2660 if (acpi_desc->scrub_spa != nfit_spa)
2663 if ((ars_status->address >= spa->address && ars_status->address
2664 < spa->address + spa->length)
2665 || (ars_status->address < spa->address)) {
2667 * Assume that if a scrub starts at an offset from the
2668 * start of nfit_spa that we are in the continuation
2671 * Otherwise, if the scrub covers the spa range, mark
2672 * any pending request complete.
2674 if (ars_status->address + ars_status->length
2675 >= spa->address + spa->length)
2682 acpi_desc->scrub_spa = NULL;
2684 dev = nd_region_dev(nd_region);
2685 nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
2687 dev = acpi_desc->dev;
2688 dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
2691 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
2693 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2694 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2699 * First record starts at 44 byte offset from the start of the
2702 if (ars_status->out_length < 44)
2704 for (i = 0; i < ars_status->num_records; i++) {
2705 /* only process full records */
2706 if (ars_status->out_length
2707 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2709 rc = nvdimm_bus_add_badrange(nvdimm_bus,
2710 ars_status->records[i].err_address,
2711 ars_status->records[i].length);
2715 if (i < ars_status->num_records)
2716 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2721 static void acpi_nfit_remove_resource(void *data)
2723 struct resource *res = data;
2725 remove_resource(res);
2728 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2729 struct nd_region_desc *ndr_desc)
2731 struct resource *res, *nd_res = ndr_desc->res;
2734 /* No operation if the region is already registered as PMEM */
2735 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2736 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2737 if (is_pmem == REGION_INTERSECTS)
2740 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2744 res->name = "Persistent Memory";
2745 res->start = nd_res->start;
2746 res->end = nd_res->end;
2747 res->flags = IORESOURCE_MEM;
2748 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2750 ret = insert_resource(&iomem_resource, res);
2754 ret = devm_add_action_or_reset(acpi_desc->dev,
2755 acpi_nfit_remove_resource,
2763 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2764 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2765 struct acpi_nfit_memory_map *memdev,
2766 struct nfit_spa *nfit_spa)
2768 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2769 memdev->device_handle);
2770 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2771 struct nd_blk_region_desc *ndbr_desc;
2772 struct nfit_mem *nfit_mem;
2776 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2777 spa->range_index, memdev->device_handle);
2781 mapping->nvdimm = nvdimm;
2782 switch (nfit_spa_type(spa)) {
2784 case NFIT_SPA_VOLATILE:
2785 mapping->start = memdev->address;
2786 mapping->size = memdev->region_size;
2789 nfit_mem = nvdimm_provider_data(nvdimm);
2790 if (!nfit_mem || !nfit_mem->bdw) {
2791 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2792 spa->range_index, nvdimm_name(nvdimm));
2796 mapping->size = nfit_mem->bdw->capacity;
2797 mapping->start = nfit_mem->bdw->start_address;
2798 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2799 ndr_desc->mapping = mapping;
2800 ndr_desc->num_mappings = 1;
2801 ndbr_desc = to_blk_region_desc(ndr_desc);
2802 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2803 ndbr_desc->do_io = acpi_desc->blk_do_io;
2804 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2807 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2809 if (!nfit_spa->nd_region)
2817 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2819 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2820 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2821 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2822 nfit_spa_type(spa) == NFIT_SPA_PCD);
2825 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2827 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2828 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2829 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2832 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2833 struct nfit_spa *nfit_spa)
2835 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2836 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2837 struct nd_blk_region_desc ndbr_desc;
2838 struct nd_region_desc *ndr_desc;
2839 struct nfit_memdev *nfit_memdev;
2840 struct nvdimm_bus *nvdimm_bus;
2841 struct resource res;
2844 if (nfit_spa->nd_region)
2847 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2848 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
2852 memset(&res, 0, sizeof(res));
2853 memset(&mappings, 0, sizeof(mappings));
2854 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2855 res.start = spa->address;
2856 res.end = res.start + spa->length - 1;
2857 ndr_desc = &ndbr_desc.ndr_desc;
2858 ndr_desc->res = &res;
2859 ndr_desc->provider_data = nfit_spa;
2860 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2861 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2862 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2863 spa->proximity_domain);
2865 ndr_desc->numa_node = NUMA_NO_NODE;
2868 * Persistence domain bits are hierarchical, if
2869 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
2870 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
2872 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2873 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2874 else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2875 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2877 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2878 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2879 struct nd_mapping_desc *mapping;
2881 if (memdev->range_index != spa->range_index)
2883 if (count >= ND_MAX_MAPPINGS) {
2884 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2885 spa->range_index, ND_MAX_MAPPINGS);
2888 mapping = &mappings[count++];
2889 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2895 ndr_desc->mapping = mappings;
2896 ndr_desc->num_mappings = count;
2897 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2901 nvdimm_bus = acpi_desc->nvdimm_bus;
2902 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2903 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2905 dev_warn(acpi_desc->dev,
2906 "failed to insert pmem resource to iomem: %d\n",
2911 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2913 if (!nfit_spa->nd_region)
2915 } else if (nfit_spa_is_volatile(spa)) {
2916 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2918 if (!nfit_spa->nd_region)
2920 } else if (nfit_spa_is_virtual(spa)) {
2921 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2923 if (!nfit_spa->nd_region)
2929 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2930 nfit_spa->spa->range_index);
2934 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
2936 struct device *dev = acpi_desc->dev;
2937 struct nd_cmd_ars_status *ars_status;
2939 if (acpi_desc->ars_status) {
2940 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
2944 ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
2947 acpi_desc->ars_status = ars_status;
2951 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
2955 if (ars_status_alloc(acpi_desc))
2958 rc = ars_get_status(acpi_desc);
2960 if (rc < 0 && rc != -ENOSPC)
2963 if (ars_status_process_records(acpi_desc))
2964 dev_err(acpi_desc->dev, "Failed to process ARS records\n");
2969 static int ars_register(struct acpi_nfit_desc *acpi_desc,
2970 struct nfit_spa *nfit_spa)
2974 if (no_init_ars || test_bit(ARS_FAILED, &nfit_spa->ars_state))
2975 return acpi_nfit_register_region(acpi_desc, nfit_spa);
2977 set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
2978 set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
2980 switch (acpi_nfit_query_poison(acpi_desc)) {
2983 rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
2984 /* shouldn't happen, try again later */
2988 set_bit(ARS_FAILED, &nfit_spa->ars_state);
2991 clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
2992 rc = acpi_nfit_query_poison(acpi_desc);
2995 acpi_desc->scrub_spa = nfit_spa;
2996 ars_complete(acpi_desc, nfit_spa);
2998 * If ars_complete() says we didn't complete the
2999 * short scrub, we'll try again with a long
3002 acpi_desc->scrub_spa = NULL;
3008 * BIOS was using ARS, wait for it to complete (or
3009 * resources to become available) and then perform our
3014 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3018 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3021 static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
3023 struct nfit_spa *nfit_spa;
3025 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3026 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3028 ars_complete(acpi_desc, nfit_spa);
3032 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
3035 unsigned int tmo = acpi_desc->scrub_tmo;
3036 struct device *dev = acpi_desc->dev;
3037 struct nfit_spa *nfit_spa;
3039 lockdep_assert_held(&acpi_desc->init_mutex);
3041 if (acpi_desc->cancel)
3044 if (query_rc == -EBUSY) {
3045 dev_dbg(dev, "ARS: ARS busy\n");
3046 return min(30U * 60U, tmo * 2);
3048 if (query_rc == -ENOSPC) {
3049 dev_dbg(dev, "ARS: ARS continue\n");
3050 ars_continue(acpi_desc);
3053 if (query_rc && query_rc != -EAGAIN) {
3054 unsigned long long addr, end;
3056 addr = acpi_desc->ars_status->address;
3057 end = addr + acpi_desc->ars_status->length;
3058 dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
3062 ars_complete_all(acpi_desc);
3063 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3064 enum nfit_ars_state req_type;
3067 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3070 /* prefer short ARS requests first */
3071 if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
3072 req_type = ARS_REQ_SHORT;
3073 else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
3074 req_type = ARS_REQ_LONG;
3077 rc = ars_start(acpi_desc, nfit_spa, req_type);
3079 dev = nd_region_dev(nfit_spa->nd_region);
3080 dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
3081 nfit_spa->spa->range_index,
3082 req_type == ARS_REQ_SHORT ? "short" : "long",
3085 * Hmm, we raced someone else starting ARS? Try again in
3091 dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
3092 "scrub start while range %d active\n",
3093 acpi_desc->scrub_spa->spa->range_index);
3094 clear_bit(req_type, &nfit_spa->ars_state);
3095 acpi_desc->scrub_spa = nfit_spa;
3097 * Consider this spa last for future scrub
3100 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
3104 dev_err(dev, "ARS: range %d ARS failed (%d)\n",
3105 nfit_spa->spa->range_index, rc);
3106 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3111 static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
3113 lockdep_assert_held(&acpi_desc->init_mutex);
3115 acpi_desc->scrub_busy = 1;
3116 /* note this should only be set from within the workqueue */
3118 acpi_desc->scrub_tmo = tmo;
3119 queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
3122 static void sched_ars(struct acpi_nfit_desc *acpi_desc)
3124 __sched_ars(acpi_desc, 0);
3127 static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
3129 lockdep_assert_held(&acpi_desc->init_mutex);
3131 acpi_desc->scrub_busy = 0;
3132 acpi_desc->scrub_count++;
3133 if (acpi_desc->scrub_count_state)
3134 sysfs_notify_dirent(acpi_desc->scrub_count_state);
3137 static void acpi_nfit_scrub(struct work_struct *work)
3139 struct acpi_nfit_desc *acpi_desc;
3143 acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
3144 mutex_lock(&acpi_desc->init_mutex);
3145 query_rc = acpi_nfit_query_poison(acpi_desc);
3146 tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
3148 __sched_ars(acpi_desc, tmo);
3150 notify_ars_done(acpi_desc);
3151 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3152 mutex_unlock(&acpi_desc->init_mutex);
3155 static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
3156 struct nfit_spa *nfit_spa)
3158 int type = nfit_spa_type(nfit_spa->spa);
3159 struct nd_cmd_ars_cap ars_cap;
3162 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3163 memset(&ars_cap, 0, sizeof(ars_cap));
3164 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
3167 /* check that the supported scrub types match the spa type */
3168 if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
3169 & ND_ARS_VOLATILE) == 0)
3171 if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
3172 & ND_ARS_PERSISTENT) == 0)
3175 nfit_spa->max_ars = ars_cap.max_ars_out;
3176 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
3177 acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
3178 clear_bit(ARS_FAILED, &nfit_spa->ars_state);
3181 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3183 struct nfit_spa *nfit_spa;
3186 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3187 switch (nfit_spa_type(nfit_spa->spa)) {
3188 case NFIT_SPA_VOLATILE:
3190 acpi_nfit_init_ars(acpi_desc, nfit_spa);
3195 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3196 switch (nfit_spa_type(nfit_spa->spa)) {
3197 case NFIT_SPA_VOLATILE:
3199 /* register regions and kick off initial ARS run */
3200 rc = ars_register(acpi_desc, nfit_spa);
3205 /* nothing to register */
3208 case NFIT_SPA_VDISK:
3210 case NFIT_SPA_PDISK:
3212 /* register known regions that don't support ARS */
3213 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3218 /* don't register unknown regions */
3222 sched_ars(acpi_desc);
3226 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3227 struct nfit_table_prev *prev)
3229 struct device *dev = acpi_desc->dev;
3231 if (!list_empty(&prev->spas) ||
3232 !list_empty(&prev->memdevs) ||
3233 !list_empty(&prev->dcrs) ||
3234 !list_empty(&prev->bdws) ||
3235 !list_empty(&prev->idts) ||
3236 !list_empty(&prev->flushes)) {
3237 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3243 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3245 struct device *dev = acpi_desc->dev;
3246 struct kernfs_node *nfit;
3247 struct device *bus_dev;
3249 if (!ars_supported(acpi_desc->nvdimm_bus))
3252 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3253 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3255 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3258 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3260 if (!acpi_desc->scrub_count_state) {
3261 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3268 static void acpi_nfit_unregister(void *data)
3270 struct acpi_nfit_desc *acpi_desc = data;
3272 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
3275 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
3277 struct device *dev = acpi_desc->dev;
3278 struct nfit_table_prev prev;
3282 if (!acpi_desc->nvdimm_bus) {
3283 acpi_nfit_init_dsms(acpi_desc);
3285 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3286 &acpi_desc->nd_desc);
3287 if (!acpi_desc->nvdimm_bus)
3290 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
3295 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3299 /* register this acpi_desc for mce notifications */
3300 mutex_lock(&acpi_desc_lock);
3301 list_add_tail(&acpi_desc->list, &acpi_descs);
3302 mutex_unlock(&acpi_desc_lock);
3305 mutex_lock(&acpi_desc->init_mutex);
3307 INIT_LIST_HEAD(&prev.spas);
3308 INIT_LIST_HEAD(&prev.memdevs);
3309 INIT_LIST_HEAD(&prev.dcrs);
3310 INIT_LIST_HEAD(&prev.bdws);
3311 INIT_LIST_HEAD(&prev.idts);
3312 INIT_LIST_HEAD(&prev.flushes);
3314 list_cut_position(&prev.spas, &acpi_desc->spas,
3315 acpi_desc->spas.prev);
3316 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3317 acpi_desc->memdevs.prev);
3318 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3319 acpi_desc->dcrs.prev);
3320 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3321 acpi_desc->bdws.prev);
3322 list_cut_position(&prev.idts, &acpi_desc->idts,
3323 acpi_desc->idts.prev);
3324 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3325 acpi_desc->flushes.prev);
3328 while (!IS_ERR_OR_NULL(data))
3329 data = add_table(acpi_desc, &prev, data, end);
3332 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
3337 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3341 rc = nfit_mem_init(acpi_desc);
3345 rc = acpi_nfit_register_dimms(acpi_desc);
3349 rc = acpi_nfit_register_regions(acpi_desc);
3352 mutex_unlock(&acpi_desc->init_mutex);
3355 EXPORT_SYMBOL_GPL(acpi_nfit_init);
3357 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3359 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3360 struct device *dev = acpi_desc->dev;
3362 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3366 /* Bounce the init_mutex to complete initial registration */
3367 mutex_lock(&acpi_desc->init_mutex);
3368 mutex_unlock(&acpi_desc->init_mutex);
3373 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3374 struct nvdimm *nvdimm, unsigned int cmd)
3376 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
3380 if (cmd != ND_CMD_ARS_START)
3384 * The kernel and userspace may race to initiate a scrub, but
3385 * the scrub thread is prepared to lose that initial race. It
3386 * just needs guarantees that any ARS it initiates are not
3387 * interrupted by any intervening start requests from userspace.
3389 if (work_busy(&acpi_desc->dwork.work))
3395 /* prevent security commands from being issued via ioctl */
3396 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3397 struct nvdimm *nvdimm, unsigned int cmd, void *buf)
3399 struct nd_cmd_pkg *call_pkg = buf;
3402 if (nvdimm && cmd == ND_CMD_CALL &&
3403 call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
3404 func = call_pkg->nd_command;
3405 if ((1 << func) & NVDIMM_INTEL_SECURITY_CMDMASK)
3409 return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
3412 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
3413 enum nfit_ars_state req_type)
3415 struct device *dev = acpi_desc->dev;
3416 int scheduled = 0, busy = 0;
3417 struct nfit_spa *nfit_spa;
3419 mutex_lock(&acpi_desc->init_mutex);
3420 if (acpi_desc->cancel) {
3421 mutex_unlock(&acpi_desc->init_mutex);
3425 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3426 int type = nfit_spa_type(nfit_spa->spa);
3428 if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
3430 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3433 if (test_and_set_bit(req_type, &nfit_spa->ars_state))
3439 sched_ars(acpi_desc);
3440 dev_dbg(dev, "ars_scan triggered\n");
3442 mutex_unlock(&acpi_desc->init_mutex);
3451 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3453 struct nvdimm_bus_descriptor *nd_desc;
3455 dev_set_drvdata(dev, acpi_desc);
3456 acpi_desc->dev = dev;
3457 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3458 nd_desc = &acpi_desc->nd_desc;
3459 nd_desc->provider_name = "ACPI.NFIT";
3460 nd_desc->module = THIS_MODULE;
3461 nd_desc->ndctl = acpi_nfit_ctl;
3462 nd_desc->flush_probe = acpi_nfit_flush_probe;
3463 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3464 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3466 INIT_LIST_HEAD(&acpi_desc->spas);
3467 INIT_LIST_HEAD(&acpi_desc->dcrs);
3468 INIT_LIST_HEAD(&acpi_desc->bdws);
3469 INIT_LIST_HEAD(&acpi_desc->idts);
3470 INIT_LIST_HEAD(&acpi_desc->flushes);
3471 INIT_LIST_HEAD(&acpi_desc->memdevs);
3472 INIT_LIST_HEAD(&acpi_desc->dimms);
3473 INIT_LIST_HEAD(&acpi_desc->list);
3474 mutex_init(&acpi_desc->init_mutex);
3475 acpi_desc->scrub_tmo = 1;
3476 INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
3478 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3480 static void acpi_nfit_put_table(void *table)
3482 acpi_put_table(table);
3485 void acpi_nfit_shutdown(void *data)
3487 struct acpi_nfit_desc *acpi_desc = data;
3488 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3491 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3494 mutex_lock(&acpi_desc_lock);
3495 list_del(&acpi_desc->list);
3496 mutex_unlock(&acpi_desc_lock);
3498 mutex_lock(&acpi_desc->init_mutex);
3499 acpi_desc->cancel = 1;
3500 cancel_delayed_work_sync(&acpi_desc->dwork);
3501 mutex_unlock(&acpi_desc->init_mutex);
3504 * Bounce the nvdimm bus lock to make sure any in-flight
3505 * acpi_nfit_ars_rescan() submissions have had a chance to
3506 * either submit or see ->cancel set.
3508 device_lock(bus_dev);
3509 device_unlock(bus_dev);
3511 flush_workqueue(nfit_wq);
3513 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3515 static int acpi_nfit_add(struct acpi_device *adev)
3517 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3518 struct acpi_nfit_desc *acpi_desc;
3519 struct device *dev = &adev->dev;
3520 struct acpi_table_header *tbl;
3521 acpi_status status = AE_OK;
3525 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3526 if (ACPI_FAILURE(status)) {
3527 /* The NVDIMM root device allows OS to trigger enumeration of
3528 * NVDIMMs through NFIT at boot time and re-enumeration at
3529 * root level via the _FIT method during runtime.
3530 * This is ok to return 0 here, we could have an nvdimm
3531 * hotplugged later and evaluate _FIT method which returns
3532 * data in the format of a series of NFIT Structures.
3534 dev_dbg(dev, "failed to find NFIT at startup\n");
3538 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3543 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3546 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3548 /* Save the acpi header for exporting the revision via sysfs */
3549 acpi_desc->acpi_header = *tbl;
3551 /* Evaluate _FIT and override with that if present */
3552 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3553 if (ACPI_SUCCESS(status) && buf.length > 0) {
3554 union acpi_object *obj = buf.pointer;
3556 if (obj->type == ACPI_TYPE_BUFFER)
3557 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3558 obj->buffer.length);
3560 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3564 /* skip over the lead-in header table */
3565 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3566 + sizeof(struct acpi_table_nfit),
3567 sz - sizeof(struct acpi_table_nfit));
3571 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3574 static int acpi_nfit_remove(struct acpi_device *adev)
3576 /* see acpi_nfit_unregister */
3580 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3582 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3583 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3584 union acpi_object *obj;
3589 /* dev->driver may be null if we're being removed */
3590 dev_dbg(dev, "no driver found for dev\n");
3595 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3598 acpi_nfit_desc_init(acpi_desc, dev);
3601 * Finish previous registration before considering new
3604 flush_workqueue(nfit_wq);
3608 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3609 if (ACPI_FAILURE(status)) {
3610 dev_err(dev, "failed to evaluate _FIT\n");
3615 if (obj->type == ACPI_TYPE_BUFFER) {
3616 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3617 obj->buffer.length);
3619 dev_err(dev, "failed to merge updated NFIT\n");
3621 dev_err(dev, "Invalid _FIT\n");
3625 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3627 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3629 if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
3630 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
3632 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
3635 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3637 dev_dbg(dev, "event: 0x%x\n", event);
3640 case NFIT_NOTIFY_UPDATE:
3641 return acpi_nfit_update_notify(dev, handle);
3642 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3643 return acpi_nfit_uc_error_notify(dev, handle);
3648 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3650 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3652 device_lock(&adev->dev);
3653 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3654 device_unlock(&adev->dev);
3657 static const struct acpi_device_id acpi_nfit_ids[] = {
3661 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3663 static struct acpi_driver acpi_nfit_driver = {
3664 .name = KBUILD_MODNAME,
3665 .ids = acpi_nfit_ids,
3667 .add = acpi_nfit_add,
3668 .remove = acpi_nfit_remove,
3669 .notify = acpi_nfit_notify,
3673 static __init int nfit_init(void)
3677 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3678 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3679 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3680 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3681 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3682 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3683 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3684 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
3686 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3687 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3688 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3689 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3690 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3691 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3692 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3693 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3694 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3695 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3696 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3697 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3698 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3700 nfit_wq = create_singlethread_workqueue("nfit");
3704 nfit_mce_register();
3705 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3707 nfit_mce_unregister();
3708 destroy_workqueue(nfit_wq);
3715 static __exit void nfit_exit(void)
3717 nfit_mce_unregister();
3718 acpi_bus_unregister_driver(&acpi_nfit_driver);
3719 destroy_workqueue(nfit_wq);
3720 WARN_ON(!list_empty(&acpi_descs));
3723 module_init(nfit_init);
3724 module_exit(nfit_exit);
3725 MODULE_LICENSE("GPL v2");
3726 MODULE_AUTHOR("Intel Corporation");