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>
23 #include <linux/pmem.h>
26 #include <asm/cacheflush.h>
30 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
33 #include <linux/io-64-nonatomic-hi-lo.h>
35 static bool force_enable_dimms;
36 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
37 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
39 static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
40 module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
41 MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
43 /* after three payloads of overflow, it's dead jim */
44 static unsigned int scrub_overflow_abort = 3;
45 module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
46 MODULE_PARM_DESC(scrub_overflow_abort,
47 "Number of times we overflow ARS results before abort");
49 static bool disable_vendor_specific;
50 module_param(disable_vendor_specific, bool, S_IRUGO);
51 MODULE_PARM_DESC(disable_vendor_specific,
52 "Limit commands to the publicly specified set\n");
54 LIST_HEAD(acpi_descs);
55 DEFINE_MUTEX(acpi_desc_lock);
57 static struct workqueue_struct *nfit_wq;
59 struct nfit_table_prev {
60 struct list_head spas;
61 struct list_head memdevs;
62 struct list_head dcrs;
63 struct list_head bdws;
64 struct list_head idts;
65 struct list_head flushes;
68 static u8 nfit_uuid[NFIT_UUID_MAX][16];
70 const u8 *to_nfit_uuid(enum nfit_uuids id)
74 EXPORT_SYMBOL(to_nfit_uuid);
76 static struct acpi_nfit_desc *to_acpi_nfit_desc(
77 struct nvdimm_bus_descriptor *nd_desc)
79 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
82 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
84 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
87 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
90 if (!nd_desc->provider_name
91 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
94 return to_acpi_device(acpi_desc->dev);
97 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
99 struct nd_cmd_clear_error *clear_err;
100 struct nd_cmd_ars_status *ars_status;
105 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
112 /* No supported scan types for this range */
113 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
114 if ((status >> 16 & flags) == 0)
117 case ND_CMD_ARS_START:
118 /* ARS is in progress */
119 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
126 case ND_CMD_ARS_STATUS:
131 /* Check extended status (Upper two bytes) */
132 if (status == NFIT_ARS_STATUS_DONE)
135 /* ARS is in progress */
136 if (status == NFIT_ARS_STATUS_BUSY)
139 /* No ARS performed for the current boot */
140 if (status == NFIT_ARS_STATUS_NONE)
144 * ARS interrupted, either we overflowed or some other
145 * agent wants the scan to stop. If we didn't overflow
146 * then just continue with the returned results.
148 if (status == NFIT_ARS_STATUS_INTR) {
149 if (ars_status->out_length >= 40 && (ars_status->flags
150 & NFIT_ARS_F_OVERFLOW))
159 case ND_CMD_CLEAR_ERROR:
163 if (!clear_err->cleared)
165 if (clear_err->length > clear_err->cleared)
166 return clear_err->cleared;
172 /* all other non-zero status results in an error */
178 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
182 return xlat_bus_status(buf, cmd, status);
188 static int cmd_to_func(struct nfit_mem *nfit_mem, unsigned int cmd,
189 struct nd_cmd_pkg *call_pkg)
194 if (nfit_mem->family != call_pkg->nd_family)
197 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
198 if (call_pkg->nd_reserved2[i])
200 return call_pkg->nd_command;
203 /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
204 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
208 * Force function number validation to fail since 0 is never
209 * published as a valid function in dsm_mask.
214 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
215 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
217 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
218 union acpi_object in_obj, in_buf, *out_obj;
219 const struct nd_cmd_desc *desc = NULL;
220 struct device *dev = acpi_desc->dev;
221 struct nd_cmd_pkg *call_pkg = NULL;
222 const char *cmd_name, *dimm_name;
223 unsigned long cmd_mask, dsm_mask;
224 u32 offset, fw_status = 0;
233 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
234 struct acpi_device *adev = nfit_mem->adev;
239 if (cmd == ND_CMD_CALL)
241 func = cmd_to_func(nfit_mem, cmd, call_pkg);
244 dimm_name = nvdimm_name(nvdimm);
245 cmd_name = nvdimm_cmd_name(cmd);
246 cmd_mask = nvdimm_cmd_mask(nvdimm);
247 dsm_mask = nfit_mem->dsm_mask;
248 desc = nd_cmd_dimm_desc(cmd);
249 uuid = to_nfit_uuid(nfit_mem->family);
250 handle = adev->handle;
252 struct acpi_device *adev = to_acpi_dev(acpi_desc);
255 cmd_name = nvdimm_bus_cmd_name(cmd);
256 cmd_mask = nd_desc->cmd_mask;
258 desc = nd_cmd_bus_desc(cmd);
259 uuid = to_nfit_uuid(NFIT_DEV_BUS);
260 handle = adev->handle;
264 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
268 * Check for a valid command. For ND_CMD_CALL, we also have to
269 * make sure that the DSM function is supported.
271 if (cmd == ND_CMD_CALL && !test_bit(func, &dsm_mask))
273 else if (!test_bit(cmd, &cmd_mask))
276 in_obj.type = ACPI_TYPE_PACKAGE;
277 in_obj.package.count = 1;
278 in_obj.package.elements = &in_buf;
279 in_buf.type = ACPI_TYPE_BUFFER;
280 in_buf.buffer.pointer = buf;
281 in_buf.buffer.length = 0;
283 /* libnvdimm has already validated the input envelope */
284 for (i = 0; i < desc->in_num; i++)
285 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
289 /* skip over package wrapper */
290 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
291 in_buf.buffer.length = call_pkg->nd_size_in;
294 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
295 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
296 __func__, dimm_name, cmd, func,
297 in_buf.buffer.length);
298 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
299 in_buf.buffer.pointer,
300 min_t(u32, 256, in_buf.buffer.length), true);
303 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
305 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
310 if (out_obj->type != ACPI_TYPE_BUFFER) {
311 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
312 dimm_name, cmd_name, out_obj->type);
318 call_pkg->nd_fw_size = out_obj->buffer.length;
319 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
320 out_obj->buffer.pointer,
321 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
325 * Need to support FW function w/o known size in advance.
326 * Caller can determine required size based upon nd_fw_size.
327 * If we return an error (like elsewhere) then caller wouldn't
328 * be able to rely upon data returned to make calculation.
335 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
336 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
337 dimm_name, cmd_name, out_obj->buffer.length);
338 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
339 4, out_obj->buffer.pointer, min_t(u32, 128,
340 out_obj->buffer.length), true);
343 for (i = 0, offset = 0; i < desc->out_num; i++) {
344 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
345 (u32 *) out_obj->buffer.pointer,
346 out_obj->buffer.length - offset);
348 if (offset + out_size > out_obj->buffer.length) {
349 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
350 __func__, dimm_name, cmd_name, i);
354 if (in_buf.buffer.length + offset + out_size > buf_len) {
355 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
356 __func__, dimm_name, cmd_name, i);
360 memcpy(buf + in_buf.buffer.length + offset,
361 out_obj->buffer.pointer + offset, out_size);
366 * Set fw_status for all the commands with a known format to be
367 * later interpreted by xlat_status().
369 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
370 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
371 fw_status = *(u32 *) out_obj->buffer.pointer;
373 if (offset + in_buf.buffer.length < buf_len) {
376 * status valid, return the number of bytes left
377 * unfilled in the output buffer
379 rc = buf_len - offset - in_buf.buffer.length;
381 *cmd_rc = xlat_status(nvdimm, buf, cmd,
384 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
385 __func__, dimm_name, cmd_name, buf_len,
392 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
400 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
402 static const char *spa_type_name(u16 type)
404 static const char *to_name[] = {
405 [NFIT_SPA_VOLATILE] = "volatile",
406 [NFIT_SPA_PM] = "pmem",
407 [NFIT_SPA_DCR] = "dimm-control-region",
408 [NFIT_SPA_BDW] = "block-data-window",
409 [NFIT_SPA_VDISK] = "volatile-disk",
410 [NFIT_SPA_VCD] = "volatile-cd",
411 [NFIT_SPA_PDISK] = "persistent-disk",
412 [NFIT_SPA_PCD] = "persistent-cd",
416 if (type > NFIT_SPA_PCD)
419 return to_name[type];
422 int nfit_spa_type(struct acpi_nfit_system_address *spa)
426 for (i = 0; i < NFIT_UUID_MAX; i++)
427 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
432 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
433 struct nfit_table_prev *prev,
434 struct acpi_nfit_system_address *spa)
436 struct device *dev = acpi_desc->dev;
437 struct nfit_spa *nfit_spa;
439 if (spa->header.length != sizeof(*spa))
442 list_for_each_entry(nfit_spa, &prev->spas, list) {
443 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
444 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
449 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
453 INIT_LIST_HEAD(&nfit_spa->list);
454 memcpy(nfit_spa->spa, spa, sizeof(*spa));
455 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
456 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
458 spa_type_name(nfit_spa_type(spa)));
462 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
463 struct nfit_table_prev *prev,
464 struct acpi_nfit_memory_map *memdev)
466 struct device *dev = acpi_desc->dev;
467 struct nfit_memdev *nfit_memdev;
469 if (memdev->header.length != sizeof(*memdev))
472 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
473 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
474 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
478 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
482 INIT_LIST_HEAD(&nfit_memdev->list);
483 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
484 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
485 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
486 __func__, memdev->device_handle, memdev->range_index,
487 memdev->region_index);
492 * An implementation may provide a truncated control region if no block windows
495 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
497 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
502 return offsetof(struct acpi_nfit_control_region, window_size);
505 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
506 struct nfit_table_prev *prev,
507 struct acpi_nfit_control_region *dcr)
509 struct device *dev = acpi_desc->dev;
510 struct nfit_dcr *nfit_dcr;
512 if (!sizeof_dcr(dcr))
515 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
516 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
517 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
521 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
525 INIT_LIST_HEAD(&nfit_dcr->list);
526 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
527 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
528 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
529 dcr->region_index, dcr->windows);
533 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
534 struct nfit_table_prev *prev,
535 struct acpi_nfit_data_region *bdw)
537 struct device *dev = acpi_desc->dev;
538 struct nfit_bdw *nfit_bdw;
540 if (bdw->header.length != sizeof(*bdw))
542 list_for_each_entry(nfit_bdw, &prev->bdws, list)
543 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
544 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
548 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
552 INIT_LIST_HEAD(&nfit_bdw->list);
553 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
554 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
555 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
556 bdw->region_index, bdw->windows);
560 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
562 if (idt->header.length < sizeof(*idt))
564 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
567 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
568 struct nfit_table_prev *prev,
569 struct acpi_nfit_interleave *idt)
571 struct device *dev = acpi_desc->dev;
572 struct nfit_idt *nfit_idt;
574 if (!sizeof_idt(idt))
577 list_for_each_entry(nfit_idt, &prev->idts, list) {
578 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
581 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
582 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
587 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
591 INIT_LIST_HEAD(&nfit_idt->list);
592 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
593 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
594 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
595 idt->interleave_index, idt->line_count);
599 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
601 if (flush->header.length < sizeof(*flush))
603 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
606 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
607 struct nfit_table_prev *prev,
608 struct acpi_nfit_flush_address *flush)
610 struct device *dev = acpi_desc->dev;
611 struct nfit_flush *nfit_flush;
613 if (!sizeof_flush(flush))
616 list_for_each_entry(nfit_flush, &prev->flushes, list) {
617 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
620 if (memcmp(nfit_flush->flush, flush,
621 sizeof_flush(flush)) == 0) {
622 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
627 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
628 + sizeof_flush(flush), GFP_KERNEL);
631 INIT_LIST_HEAD(&nfit_flush->list);
632 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
633 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
634 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
635 flush->device_handle, flush->hint_count);
639 static void *add_table(struct acpi_nfit_desc *acpi_desc,
640 struct nfit_table_prev *prev, void *table, const void *end)
642 struct device *dev = acpi_desc->dev;
643 struct acpi_nfit_header *hdr;
644 void *err = ERR_PTR(-ENOMEM);
651 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
657 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
658 if (!add_spa(acpi_desc, prev, table))
661 case ACPI_NFIT_TYPE_MEMORY_MAP:
662 if (!add_memdev(acpi_desc, prev, table))
665 case ACPI_NFIT_TYPE_CONTROL_REGION:
666 if (!add_dcr(acpi_desc, prev, table))
669 case ACPI_NFIT_TYPE_DATA_REGION:
670 if (!add_bdw(acpi_desc, prev, table))
673 case ACPI_NFIT_TYPE_INTERLEAVE:
674 if (!add_idt(acpi_desc, prev, table))
677 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
678 if (!add_flush(acpi_desc, prev, table))
681 case ACPI_NFIT_TYPE_SMBIOS:
682 dev_dbg(dev, "%s: smbios\n", __func__);
685 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
689 return table + hdr->length;
692 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
693 struct nfit_mem *nfit_mem)
695 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
696 u16 dcr = nfit_mem->dcr->region_index;
697 struct nfit_spa *nfit_spa;
699 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
700 u16 range_index = nfit_spa->spa->range_index;
701 int type = nfit_spa_type(nfit_spa->spa);
702 struct nfit_memdev *nfit_memdev;
704 if (type != NFIT_SPA_BDW)
707 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
708 if (nfit_memdev->memdev->range_index != range_index)
710 if (nfit_memdev->memdev->device_handle != device_handle)
712 if (nfit_memdev->memdev->region_index != dcr)
715 nfit_mem->spa_bdw = nfit_spa->spa;
720 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
721 nfit_mem->spa_dcr->range_index);
722 nfit_mem->bdw = NULL;
725 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
726 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
728 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
729 struct nfit_memdev *nfit_memdev;
730 struct nfit_bdw *nfit_bdw;
731 struct nfit_idt *nfit_idt;
732 u16 idt_idx, range_index;
734 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
735 if (nfit_bdw->bdw->region_index != dcr)
737 nfit_mem->bdw = nfit_bdw->bdw;
744 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
746 if (!nfit_mem->spa_bdw)
749 range_index = nfit_mem->spa_bdw->range_index;
750 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
751 if (nfit_memdev->memdev->range_index != range_index ||
752 nfit_memdev->memdev->region_index != dcr)
754 nfit_mem->memdev_bdw = nfit_memdev->memdev;
755 idt_idx = nfit_memdev->memdev->interleave_index;
756 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
757 if (nfit_idt->idt->interleave_index != idt_idx)
759 nfit_mem->idt_bdw = nfit_idt->idt;
766 static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
767 struct acpi_nfit_system_address *spa)
769 struct nfit_mem *nfit_mem, *found;
770 struct nfit_memdev *nfit_memdev;
771 int type = nfit_spa_type(spa);
781 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
782 struct nfit_flush *nfit_flush;
783 struct nfit_dcr *nfit_dcr;
787 if (nfit_memdev->memdev->range_index != spa->range_index)
790 dcr = nfit_memdev->memdev->region_index;
791 device_handle = nfit_memdev->memdev->device_handle;
792 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
793 if (__to_nfit_memdev(nfit_mem)->device_handle
802 nfit_mem = devm_kzalloc(acpi_desc->dev,
803 sizeof(*nfit_mem), GFP_KERNEL);
806 INIT_LIST_HEAD(&nfit_mem->list);
807 nfit_mem->acpi_desc = acpi_desc;
808 list_add(&nfit_mem->list, &acpi_desc->dimms);
811 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
812 if (nfit_dcr->dcr->region_index != dcr)
815 * Record the control region for the dimm. For
816 * the ACPI 6.1 case, where there are separate
817 * control regions for the pmem vs blk
818 * interfaces, be sure to record the extended
822 nfit_mem->dcr = nfit_dcr->dcr;
823 else if (nfit_mem->dcr->windows == 0
824 && nfit_dcr->dcr->windows)
825 nfit_mem->dcr = nfit_dcr->dcr;
829 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
830 struct acpi_nfit_flush_address *flush;
833 if (nfit_flush->flush->device_handle != device_handle)
835 nfit_mem->nfit_flush = nfit_flush;
836 flush = nfit_flush->flush;
837 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
839 * sizeof(struct resource), GFP_KERNEL);
840 if (!nfit_mem->flush_wpq)
842 for (i = 0; i < flush->hint_count; i++) {
843 struct resource *res = &nfit_mem->flush_wpq[i];
845 res->start = flush->hint_address[i];
846 res->end = res->start + 8 - 1;
851 if (dcr && !nfit_mem->dcr) {
852 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
853 spa->range_index, dcr);
857 if (type == NFIT_SPA_DCR) {
858 struct nfit_idt *nfit_idt;
861 /* multiple dimms may share a SPA when interleaved */
862 nfit_mem->spa_dcr = spa;
863 nfit_mem->memdev_dcr = nfit_memdev->memdev;
864 idt_idx = nfit_memdev->memdev->interleave_index;
865 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
866 if (nfit_idt->idt->interleave_index != idt_idx)
868 nfit_mem->idt_dcr = nfit_idt->idt;
871 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
874 * A single dimm may belong to multiple SPA-PM
875 * ranges, record at least one in addition to
878 nfit_mem->memdev_pmem = nfit_memdev->memdev;
885 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
887 struct nfit_mem *a = container_of(_a, typeof(*a), list);
888 struct nfit_mem *b = container_of(_b, typeof(*b), list);
889 u32 handleA, handleB;
891 handleA = __to_nfit_memdev(a)->device_handle;
892 handleB = __to_nfit_memdev(b)->device_handle;
893 if (handleA < handleB)
895 else if (handleA > handleB)
900 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
902 struct nfit_spa *nfit_spa;
905 * For each SPA-DCR or SPA-PMEM address range find its
906 * corresponding MEMDEV(s). From each MEMDEV find the
907 * corresponding DCR. Then, if we're operating on a SPA-DCR,
908 * try to find a SPA-BDW and a corresponding BDW that references
909 * the DCR. Throw it all into an nfit_mem object. Note, that
912 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
915 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
920 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
925 static ssize_t revision_show(struct device *dev,
926 struct device_attribute *attr, char *buf)
928 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
929 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
930 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
932 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
934 static DEVICE_ATTR_RO(revision);
936 static ssize_t hw_error_scrub_show(struct device *dev,
937 struct device_attribute *attr, char *buf)
939 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
940 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
941 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
943 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
947 * The 'hw_error_scrub' attribute can have the following values written to it:
948 * '0': Switch to the default mode where an exception will only insert
949 * the address of the memory error into the poison and badblocks lists.
950 * '1': Enable a full scrub to happen if an exception for a memory error is
953 static ssize_t hw_error_scrub_store(struct device *dev,
954 struct device_attribute *attr, const char *buf, size_t size)
956 struct nvdimm_bus_descriptor *nd_desc;
960 rc = kstrtol(buf, 0, &val);
965 nd_desc = dev_get_drvdata(dev);
967 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
970 case HW_ERROR_SCRUB_ON:
971 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
973 case HW_ERROR_SCRUB_OFF:
974 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
986 static DEVICE_ATTR_RW(hw_error_scrub);
989 * This shows the number of full Address Range Scrubs that have been
990 * completed since driver load time. Userspace can wait on this using
991 * select/poll etc. A '+' at the end indicates an ARS is in progress
993 static ssize_t scrub_show(struct device *dev,
994 struct device_attribute *attr, char *buf)
996 struct nvdimm_bus_descriptor *nd_desc;
1000 nd_desc = dev_get_drvdata(dev);
1002 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1004 mutex_lock(&acpi_desc->init_mutex);
1005 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1006 work_busy(&acpi_desc->work)
1007 && !acpi_desc->cancel ? "+\n" : "\n");
1008 mutex_unlock(&acpi_desc->init_mutex);
1014 static ssize_t scrub_store(struct device *dev,
1015 struct device_attribute *attr, const char *buf, size_t size)
1017 struct nvdimm_bus_descriptor *nd_desc;
1021 rc = kstrtol(buf, 0, &val);
1028 nd_desc = dev_get_drvdata(dev);
1030 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1032 rc = acpi_nfit_ars_rescan(acpi_desc);
1039 static DEVICE_ATTR_RW(scrub);
1041 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1043 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1044 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1045 | 1 << ND_CMD_ARS_STATUS;
1047 return (nd_desc->cmd_mask & mask) == mask;
1050 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1052 struct device *dev = container_of(kobj, struct device, kobj);
1053 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1055 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1060 static struct attribute *acpi_nfit_attributes[] = {
1061 &dev_attr_revision.attr,
1062 &dev_attr_scrub.attr,
1063 &dev_attr_hw_error_scrub.attr,
1067 static struct attribute_group acpi_nfit_attribute_group = {
1069 .attrs = acpi_nfit_attributes,
1070 .is_visible = nfit_visible,
1073 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1074 &nvdimm_bus_attribute_group,
1075 &acpi_nfit_attribute_group,
1079 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1081 struct nvdimm *nvdimm = to_nvdimm(dev);
1082 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1084 return __to_nfit_memdev(nfit_mem);
1087 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1089 struct nvdimm *nvdimm = to_nvdimm(dev);
1090 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1092 return nfit_mem->dcr;
1095 static ssize_t handle_show(struct device *dev,
1096 struct device_attribute *attr, char *buf)
1098 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1100 return sprintf(buf, "%#x\n", memdev->device_handle);
1102 static DEVICE_ATTR_RO(handle);
1104 static ssize_t phys_id_show(struct device *dev,
1105 struct device_attribute *attr, char *buf)
1107 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1109 return sprintf(buf, "%#x\n", memdev->physical_id);
1111 static DEVICE_ATTR_RO(phys_id);
1113 static ssize_t vendor_show(struct device *dev,
1114 struct device_attribute *attr, char *buf)
1116 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1118 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1120 static DEVICE_ATTR_RO(vendor);
1122 static ssize_t rev_id_show(struct device *dev,
1123 struct device_attribute *attr, char *buf)
1125 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1127 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1129 static DEVICE_ATTR_RO(rev_id);
1131 static ssize_t device_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1134 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1136 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1138 static DEVICE_ATTR_RO(device);
1140 static ssize_t subsystem_vendor_show(struct device *dev,
1141 struct device_attribute *attr, char *buf)
1143 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1145 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1147 static DEVICE_ATTR_RO(subsystem_vendor);
1149 static ssize_t subsystem_rev_id_show(struct device *dev,
1150 struct device_attribute *attr, char *buf)
1152 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1154 return sprintf(buf, "0x%04x\n",
1155 be16_to_cpu(dcr->subsystem_revision_id));
1157 static DEVICE_ATTR_RO(subsystem_rev_id);
1159 static ssize_t subsystem_device_show(struct device *dev,
1160 struct device_attribute *attr, char *buf)
1162 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1164 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1166 static DEVICE_ATTR_RO(subsystem_device);
1168 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1170 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1173 if (nfit_mem->memdev_pmem)
1175 if (nfit_mem->memdev_bdw)
1180 static ssize_t format_show(struct device *dev,
1181 struct device_attribute *attr, char *buf)
1183 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1185 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1187 static DEVICE_ATTR_RO(format);
1189 static ssize_t format1_show(struct device *dev,
1190 struct device_attribute *attr, char *buf)
1193 ssize_t rc = -ENXIO;
1194 struct nfit_mem *nfit_mem;
1195 struct nfit_memdev *nfit_memdev;
1196 struct acpi_nfit_desc *acpi_desc;
1197 struct nvdimm *nvdimm = to_nvdimm(dev);
1198 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1200 nfit_mem = nvdimm_provider_data(nvdimm);
1201 acpi_desc = nfit_mem->acpi_desc;
1202 handle = to_nfit_memdev(dev)->device_handle;
1204 /* assumes DIMMs have at most 2 published interface codes */
1205 mutex_lock(&acpi_desc->init_mutex);
1206 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1207 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1208 struct nfit_dcr *nfit_dcr;
1210 if (memdev->device_handle != handle)
1213 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1214 if (nfit_dcr->dcr->region_index != memdev->region_index)
1216 if (nfit_dcr->dcr->code == dcr->code)
1218 rc = sprintf(buf, "0x%04x\n",
1219 le16_to_cpu(nfit_dcr->dcr->code));
1225 mutex_unlock(&acpi_desc->init_mutex);
1228 static DEVICE_ATTR_RO(format1);
1230 static ssize_t formats_show(struct device *dev,
1231 struct device_attribute *attr, char *buf)
1233 struct nvdimm *nvdimm = to_nvdimm(dev);
1235 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1237 static DEVICE_ATTR_RO(formats);
1239 static ssize_t serial_show(struct device *dev,
1240 struct device_attribute *attr, char *buf)
1242 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1244 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1246 static DEVICE_ATTR_RO(serial);
1248 static ssize_t family_show(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1251 struct nvdimm *nvdimm = to_nvdimm(dev);
1252 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1254 if (nfit_mem->family < 0)
1256 return sprintf(buf, "%d\n", nfit_mem->family);
1258 static DEVICE_ATTR_RO(family);
1260 static ssize_t dsm_mask_show(struct device *dev,
1261 struct device_attribute *attr, char *buf)
1263 struct nvdimm *nvdimm = to_nvdimm(dev);
1264 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1266 if (nfit_mem->family < 0)
1268 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1270 static DEVICE_ATTR_RO(dsm_mask);
1272 static ssize_t flags_show(struct device *dev,
1273 struct device_attribute *attr, char *buf)
1275 u16 flags = to_nfit_memdev(dev)->flags;
1277 return sprintf(buf, "%s%s%s%s%s\n",
1278 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1279 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1280 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1281 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1282 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
1284 static DEVICE_ATTR_RO(flags);
1286 static ssize_t id_show(struct device *dev,
1287 struct device_attribute *attr, char *buf)
1289 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1291 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1292 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1293 be16_to_cpu(dcr->vendor_id),
1294 dcr->manufacturing_location,
1295 be16_to_cpu(dcr->manufacturing_date),
1296 be32_to_cpu(dcr->serial_number));
1298 return sprintf(buf, "%04x-%08x\n",
1299 be16_to_cpu(dcr->vendor_id),
1300 be32_to_cpu(dcr->serial_number));
1302 static DEVICE_ATTR_RO(id);
1304 static struct attribute *acpi_nfit_dimm_attributes[] = {
1305 &dev_attr_handle.attr,
1306 &dev_attr_phys_id.attr,
1307 &dev_attr_vendor.attr,
1308 &dev_attr_device.attr,
1309 &dev_attr_rev_id.attr,
1310 &dev_attr_subsystem_vendor.attr,
1311 &dev_attr_subsystem_device.attr,
1312 &dev_attr_subsystem_rev_id.attr,
1313 &dev_attr_format.attr,
1314 &dev_attr_formats.attr,
1315 &dev_attr_format1.attr,
1316 &dev_attr_serial.attr,
1317 &dev_attr_flags.attr,
1319 &dev_attr_family.attr,
1320 &dev_attr_dsm_mask.attr,
1324 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1325 struct attribute *a, int n)
1327 struct device *dev = container_of(kobj, struct device, kobj);
1328 struct nvdimm *nvdimm = to_nvdimm(dev);
1330 if (!to_nfit_dcr(dev))
1332 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1337 static struct attribute_group acpi_nfit_dimm_attribute_group = {
1339 .attrs = acpi_nfit_dimm_attributes,
1340 .is_visible = acpi_nfit_dimm_attr_visible,
1343 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1344 &nvdimm_attribute_group,
1345 &nd_device_attribute_group,
1346 &acpi_nfit_dimm_attribute_group,
1350 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1353 struct nfit_mem *nfit_mem;
1355 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1356 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1357 return nfit_mem->nvdimm;
1362 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1364 struct nfit_mem *nfit_mem;
1365 struct acpi_nfit_desc *acpi_desc;
1367 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1370 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1371 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1376 acpi_desc = dev_get_drvdata(dev->parent);
1381 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1384 nfit_mem = dev_get_drvdata(dev);
1385 if (nfit_mem && nfit_mem->flags_attr)
1386 sysfs_notify_dirent(nfit_mem->flags_attr);
1388 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1390 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1392 struct acpi_device *adev = data;
1393 struct device *dev = &adev->dev;
1395 device_lock(dev->parent);
1396 __acpi_nvdimm_notify(dev, event);
1397 device_unlock(dev->parent);
1400 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1401 struct nfit_mem *nfit_mem, u32 device_handle)
1403 struct acpi_device *adev, *adev_dimm;
1404 struct device *dev = acpi_desc->dev;
1405 unsigned long dsm_mask;
1409 /* nfit test assumes 1:1 relationship between commands and dsms */
1410 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1411 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1412 adev = to_acpi_dev(acpi_desc);
1416 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1417 nfit_mem->adev = adev_dimm;
1419 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1421 return force_enable_dimms ? 0 : -ENODEV;
1424 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1425 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1426 dev_err(dev, "%s: notification registration failed\n",
1427 dev_name(&adev_dimm->dev));
1431 * Record nfit_mem for the notification path to track back to
1432 * the nfit sysfs attributes for this dimm device object.
1434 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1437 * Until standardization materializes we need to consider 4
1438 * different command sets. Note, that checking for function0 (bit0)
1439 * tells us if any commands are reachable through this uuid.
1441 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
1442 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1445 /* limit the supported commands to those that are publicly documented */
1446 nfit_mem->family = i;
1447 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1449 if (disable_vendor_specific)
1450 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1451 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1452 dsm_mask = 0x1c3c76;
1453 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1455 if (disable_vendor_specific)
1456 dsm_mask &= ~(1 << 8);
1457 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1458 dsm_mask = 0xffffffff;
1460 dev_dbg(dev, "unknown dimm command family\n");
1461 nfit_mem->family = -1;
1462 /* DSMs are optional, continue loading the driver... */
1467 * Function 0 is the command interrogation function, don't
1468 * export it to potential userspace use, and enable it to be
1469 * used as an error value in acpi_nfit_ctl().
1473 uuid = to_nfit_uuid(nfit_mem->family);
1474 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1475 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1476 set_bit(i, &nfit_mem->dsm_mask);
1481 static void shutdown_dimm_notify(void *data)
1483 struct acpi_nfit_desc *acpi_desc = data;
1484 struct nfit_mem *nfit_mem;
1486 mutex_lock(&acpi_desc->init_mutex);
1488 * Clear out the nfit_mem->flags_attr and shut down dimm event
1491 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1492 struct acpi_device *adev_dimm = nfit_mem->adev;
1494 if (nfit_mem->flags_attr) {
1495 sysfs_put(nfit_mem->flags_attr);
1496 nfit_mem->flags_attr = NULL;
1499 acpi_remove_notify_handler(adev_dimm->handle,
1500 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1501 dev_set_drvdata(&adev_dimm->dev, NULL);
1504 mutex_unlock(&acpi_desc->init_mutex);
1507 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1509 struct nfit_mem *nfit_mem;
1510 int dimm_count = 0, rc;
1511 struct nvdimm *nvdimm;
1513 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1514 struct acpi_nfit_flush_address *flush;
1515 unsigned long flags = 0, cmd_mask;
1519 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1520 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1526 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1527 flags |= NDD_ALIASING;
1529 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
1530 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
1531 flags |= NDD_UNARMED;
1533 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1538 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1539 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1540 * userspace interface.
1542 cmd_mask = 1UL << ND_CMD_CALL;
1543 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1544 cmd_mask |= nfit_mem->dsm_mask;
1546 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1548 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
1549 acpi_nfit_dimm_attribute_groups,
1550 flags, cmd_mask, flush ? flush->hint_count : 0,
1551 nfit_mem->flush_wpq);
1555 nfit_mem->nvdimm = nvdimm;
1558 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1561 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
1562 nvdimm_name(nvdimm),
1563 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1564 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1565 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
1566 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
1570 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1575 * Now that dimms are successfully registered, and async registration
1576 * is flushed, attempt to enable event notification.
1578 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1579 struct kernfs_node *nfit_kernfs;
1581 nvdimm = nfit_mem->nvdimm;
1585 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1587 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1589 sysfs_put(nfit_kernfs);
1590 if (!nfit_mem->flags_attr)
1591 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1592 nvdimm_name(nvdimm));
1595 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1599 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1601 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1602 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1603 struct acpi_device *adev;
1606 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
1607 adev = to_acpi_dev(acpi_desc);
1611 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
1612 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
1613 set_bit(i, &nd_desc->cmd_mask);
1616 static ssize_t range_index_show(struct device *dev,
1617 struct device_attribute *attr, char *buf)
1619 struct nd_region *nd_region = to_nd_region(dev);
1620 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1622 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1624 static DEVICE_ATTR_RO(range_index);
1626 static struct attribute *acpi_nfit_region_attributes[] = {
1627 &dev_attr_range_index.attr,
1631 static struct attribute_group acpi_nfit_region_attribute_group = {
1633 .attrs = acpi_nfit_region_attributes,
1636 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1637 &nd_region_attribute_group,
1638 &nd_mapping_attribute_group,
1639 &nd_device_attribute_group,
1640 &nd_numa_attribute_group,
1641 &acpi_nfit_region_attribute_group,
1645 /* enough info to uniquely specify an interleave set */
1646 struct nfit_set_info {
1647 struct nfit_set_info_map {
1654 static size_t sizeof_nfit_set_info(int num_mappings)
1656 return sizeof(struct nfit_set_info)
1657 + num_mappings * sizeof(struct nfit_set_info_map);
1660 static int cmp_map_compat(const void *m0, const void *m1)
1662 const struct nfit_set_info_map *map0 = m0;
1663 const struct nfit_set_info_map *map1 = m1;
1665 return memcmp(&map0->region_offset, &map1->region_offset,
1669 static int cmp_map(const void *m0, const void *m1)
1671 const struct nfit_set_info_map *map0 = m0;
1672 const struct nfit_set_info_map *map1 = m1;
1674 if (map0->region_offset < map1->region_offset)
1676 else if (map0->region_offset > map1->region_offset)
1681 /* Retrieve the nth entry referencing this spa */
1682 static struct acpi_nfit_memory_map *memdev_from_spa(
1683 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1685 struct nfit_memdev *nfit_memdev;
1687 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1688 if (nfit_memdev->memdev->range_index == range_index)
1690 return nfit_memdev->memdev;
1694 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1695 struct nd_region_desc *ndr_desc,
1696 struct acpi_nfit_system_address *spa)
1698 int i, spa_type = nfit_spa_type(spa);
1699 struct device *dev = acpi_desc->dev;
1700 struct nd_interleave_set *nd_set;
1701 u16 nr = ndr_desc->num_mappings;
1702 struct nfit_set_info *info;
1704 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1709 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1713 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1716 for (i = 0; i < nr; i++) {
1717 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
1718 struct nfit_set_info_map *map = &info->mapping[i];
1719 struct nvdimm *nvdimm = mapping->nvdimm;
1720 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1721 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1722 spa->range_index, i);
1724 if (!memdev || !nfit_mem->dcr) {
1725 dev_err(dev, "%s: failed to find DCR\n", __func__);
1729 map->region_offset = memdev->region_offset;
1730 map->serial_number = nfit_mem->dcr->serial_number;
1733 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1735 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1737 /* support namespaces created with the wrong sort order */
1738 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1739 cmp_map_compat, NULL);
1740 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1742 ndr_desc->nd_set = nd_set;
1743 devm_kfree(dev, info);
1748 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1750 struct acpi_nfit_interleave *idt = mmio->idt;
1751 u32 sub_line_offset, line_index, line_offset;
1752 u64 line_no, table_skip_count, table_offset;
1754 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1755 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1756 line_offset = idt->line_offset[line_index]
1758 table_offset = table_skip_count * mmio->table_size;
1760 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1763 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1765 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1766 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1767 const u32 STATUS_MASK = 0x80000037;
1769 if (mmio->num_lines)
1770 offset = to_interleave_offset(offset, mmio);
1772 return readl(mmio->addr.base + offset) & STATUS_MASK;
1775 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1776 resource_size_t dpa, unsigned int len, unsigned int write)
1779 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1782 BCW_OFFSET_MASK = (1ULL << 48)-1,
1784 BCW_LEN_MASK = (1ULL << 8) - 1,
1788 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1789 len = len >> L1_CACHE_SHIFT;
1790 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1791 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1793 offset = nfit_blk->cmd_offset + mmio->size * bw;
1794 if (mmio->num_lines)
1795 offset = to_interleave_offset(offset, mmio);
1797 writeq(cmd, mmio->addr.base + offset);
1798 nvdimm_flush(nfit_blk->nd_region);
1800 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
1801 readq(mmio->addr.base + offset);
1804 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1805 resource_size_t dpa, void *iobuf, size_t len, int rw,
1808 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1809 unsigned int copied = 0;
1813 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1814 + lane * mmio->size;
1815 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1820 if (mmio->num_lines) {
1823 offset = to_interleave_offset(base_offset + copied,
1825 div_u64_rem(offset, mmio->line_size, &line_offset);
1826 c = min_t(size_t, len, mmio->line_size - line_offset);
1828 offset = base_offset + nfit_blk->bdw_offset;
1833 memcpy_to_pmem(mmio->addr.aperture + offset,
1836 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
1837 mmio_flush_range((void __force *)
1838 mmio->addr.aperture + offset, c);
1840 memcpy_from_pmem(iobuf + copied,
1841 mmio->addr.aperture + offset, c);
1849 nvdimm_flush(nfit_blk->nd_region);
1851 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1855 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1856 resource_size_t dpa, void *iobuf, u64 len, int rw)
1858 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1859 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1860 struct nd_region *nd_region = nfit_blk->nd_region;
1861 unsigned int lane, copied = 0;
1864 lane = nd_region_acquire_lane(nd_region);
1866 u64 c = min(len, mmio->size);
1868 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1869 iobuf + copied, c, rw, lane);
1876 nd_region_release_lane(nd_region, lane);
1881 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1882 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1885 mmio->num_lines = idt->line_count;
1886 mmio->line_size = idt->line_size;
1887 if (interleave_ways == 0)
1889 mmio->table_size = mmio->num_lines * interleave_ways
1896 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1897 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1899 struct nd_cmd_dimm_flags flags;
1902 memset(&flags, 0, sizeof(flags));
1903 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
1904 sizeof(flags), NULL);
1906 if (rc >= 0 && flags.status == 0)
1907 nfit_blk->dimm_flags = flags.flags;
1908 else if (rc == -ENOTTY) {
1909 /* fall back to a conservative default */
1910 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
1918 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1921 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1922 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1923 struct nfit_blk_mmio *mmio;
1924 struct nfit_blk *nfit_blk;
1925 struct nfit_mem *nfit_mem;
1926 struct nvdimm *nvdimm;
1929 nvdimm = nd_blk_region_to_dimm(ndbr);
1930 nfit_mem = nvdimm_provider_data(nvdimm);
1931 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1932 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1933 nfit_mem ? "" : " nfit_mem",
1934 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1935 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
1939 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1942 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1943 nfit_blk->nd_region = to_nd_region(dev);
1945 /* map block aperture memory */
1946 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1947 mmio = &nfit_blk->mmio[BDW];
1948 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1949 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
1950 if (!mmio->addr.base) {
1951 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1952 nvdimm_name(nvdimm));
1955 mmio->size = nfit_mem->bdw->size;
1956 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1957 mmio->idt = nfit_mem->idt_bdw;
1958 mmio->spa = nfit_mem->spa_bdw;
1959 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1960 nfit_mem->memdev_bdw->interleave_ways);
1962 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1963 __func__, nvdimm_name(nvdimm));
1967 /* map block control memory */
1968 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1969 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1970 mmio = &nfit_blk->mmio[DCR];
1971 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1972 nfit_mem->spa_dcr->length);
1973 if (!mmio->addr.base) {
1974 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1975 nvdimm_name(nvdimm));
1978 mmio->size = nfit_mem->dcr->window_size;
1979 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1980 mmio->idt = nfit_mem->idt_dcr;
1981 mmio->spa = nfit_mem->spa_dcr;
1982 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1983 nfit_mem->memdev_dcr->interleave_ways);
1985 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1986 __func__, nvdimm_name(nvdimm));
1990 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1992 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1993 __func__, nvdimm_name(nvdimm));
1997 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
1998 dev_warn(dev, "unable to guarantee persistence of writes\n");
2000 if (mmio->line_size == 0)
2003 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2004 + 8 > mmio->line_size) {
2005 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2007 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2008 + 8 > mmio->line_size) {
2009 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2016 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2017 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2019 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2020 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2023 cmd->address = spa->address;
2024 cmd->length = spa->length;
2025 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2026 sizeof(*cmd), &cmd_rc);
2032 static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
2036 struct nd_cmd_ars_start ars_start;
2037 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2038 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2040 memset(&ars_start, 0, sizeof(ars_start));
2041 ars_start.address = spa->address;
2042 ars_start.length = spa->length;
2043 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2044 ars_start.type = ND_ARS_PERSISTENT;
2045 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2046 ars_start.type = ND_ARS_VOLATILE;
2050 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2051 sizeof(ars_start), &cmd_rc);
2058 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2061 struct nd_cmd_ars_start ars_start;
2062 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2063 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2065 memset(&ars_start, 0, sizeof(ars_start));
2066 ars_start.address = ars_status->restart_address;
2067 ars_start.length = ars_status->restart_length;
2068 ars_start.type = ars_status->type;
2069 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2070 sizeof(ars_start), &cmd_rc);
2076 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2078 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2079 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2082 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2083 acpi_desc->ars_status_size, &cmd_rc);
2089 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
2090 struct nd_cmd_ars_status *ars_status)
2092 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2097 * First record starts at 44 byte offset from the start of the
2100 if (ars_status->out_length < 44)
2102 for (i = 0; i < ars_status->num_records; i++) {
2103 /* only process full records */
2104 if (ars_status->out_length
2105 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2107 rc = nvdimm_bus_add_poison(nvdimm_bus,
2108 ars_status->records[i].err_address,
2109 ars_status->records[i].length);
2113 if (i < ars_status->num_records)
2114 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2119 static void acpi_nfit_remove_resource(void *data)
2121 struct resource *res = data;
2123 remove_resource(res);
2126 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2127 struct nd_region_desc *ndr_desc)
2129 struct resource *res, *nd_res = ndr_desc->res;
2132 /* No operation if the region is already registered as PMEM */
2133 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2134 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2135 if (is_pmem == REGION_INTERSECTS)
2138 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2142 res->name = "Persistent Memory";
2143 res->start = nd_res->start;
2144 res->end = nd_res->end;
2145 res->flags = IORESOURCE_MEM;
2146 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2148 ret = insert_resource(&iomem_resource, res);
2152 ret = devm_add_action_or_reset(acpi_desc->dev,
2153 acpi_nfit_remove_resource,
2161 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2162 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2163 struct acpi_nfit_memory_map *memdev,
2164 struct nfit_spa *nfit_spa)
2166 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2167 memdev->device_handle);
2168 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2169 struct nd_blk_region_desc *ndbr_desc;
2170 struct nfit_mem *nfit_mem;
2174 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2175 spa->range_index, memdev->device_handle);
2179 mapping->nvdimm = nvdimm;
2180 switch (nfit_spa_type(spa)) {
2182 case NFIT_SPA_VOLATILE:
2183 mapping->start = memdev->address;
2184 mapping->size = memdev->region_size;
2187 nfit_mem = nvdimm_provider_data(nvdimm);
2188 if (!nfit_mem || !nfit_mem->bdw) {
2189 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2190 spa->range_index, nvdimm_name(nvdimm));
2192 mapping->size = nfit_mem->bdw->capacity;
2193 mapping->start = nfit_mem->bdw->start_address;
2194 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2198 ndr_desc->mapping = mapping;
2199 ndr_desc->num_mappings = blk_valid;
2200 ndbr_desc = to_blk_region_desc(ndr_desc);
2201 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2202 ndbr_desc->do_io = acpi_desc->blk_do_io;
2203 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2205 if (!nfit_spa->nd_region)
2213 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2215 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2216 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2217 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2218 nfit_spa_type(spa) == NFIT_SPA_PCD);
2221 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2222 struct nfit_spa *nfit_spa)
2224 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2225 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2226 struct nd_blk_region_desc ndbr_desc;
2227 struct nd_region_desc *ndr_desc;
2228 struct nfit_memdev *nfit_memdev;
2229 struct nvdimm_bus *nvdimm_bus;
2230 struct resource res;
2233 if (nfit_spa->nd_region)
2236 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2237 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2242 memset(&res, 0, sizeof(res));
2243 memset(&mappings, 0, sizeof(mappings));
2244 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2245 res.start = spa->address;
2246 res.end = res.start + spa->length - 1;
2247 ndr_desc = &ndbr_desc.ndr_desc;
2248 ndr_desc->res = &res;
2249 ndr_desc->provider_data = nfit_spa;
2250 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2251 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2252 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2253 spa->proximity_domain);
2255 ndr_desc->numa_node = NUMA_NO_NODE;
2257 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2258 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2259 struct nd_mapping_desc *mapping;
2261 /* range index 0 == unmapped in SPA or invalid-SPA */
2262 if (memdev->range_index == 0 || spa->range_index == 0)
2264 if (memdev->range_index != spa->range_index)
2266 if (count >= ND_MAX_MAPPINGS) {
2267 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2268 spa->range_index, ND_MAX_MAPPINGS);
2271 mapping = &mappings[count++];
2272 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2278 ndr_desc->mapping = mappings;
2279 ndr_desc->num_mappings = count;
2280 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2284 nvdimm_bus = acpi_desc->nvdimm_bus;
2285 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2286 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2288 dev_warn(acpi_desc->dev,
2289 "failed to insert pmem resource to iomem: %d\n",
2294 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2296 if (!nfit_spa->nd_region)
2298 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
2299 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2301 if (!nfit_spa->nd_region)
2303 } else if (nfit_spa_is_virtual(spa)) {
2304 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2306 if (!nfit_spa->nd_region)
2312 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2313 nfit_spa->spa->range_index);
2317 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2320 struct device *dev = acpi_desc->dev;
2321 struct nd_cmd_ars_status *ars_status;
2323 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2324 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2328 if (acpi_desc->ars_status)
2329 devm_kfree(dev, acpi_desc->ars_status);
2330 acpi_desc->ars_status = NULL;
2331 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2334 acpi_desc->ars_status = ars_status;
2335 acpi_desc->ars_status_size = max_ars;
2339 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2340 struct nfit_spa *nfit_spa)
2342 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2345 if (!nfit_spa->max_ars) {
2346 struct nd_cmd_ars_cap ars_cap;
2348 memset(&ars_cap, 0, sizeof(ars_cap));
2349 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2352 nfit_spa->max_ars = ars_cap.max_ars_out;
2353 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2354 /* check that the supported scrub types match the spa type */
2355 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2356 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2358 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2359 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2363 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2366 rc = ars_get_status(acpi_desc);
2367 if (rc < 0 && rc != -ENOSPC)
2370 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
2376 static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2377 struct nfit_spa *nfit_spa)
2379 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2380 unsigned int overflow_retry = scrub_overflow_abort;
2381 u64 init_ars_start = 0, init_ars_len = 0;
2382 struct device *dev = acpi_desc->dev;
2383 unsigned int tmo = scrub_timeout;
2386 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
2389 rc = ars_start(acpi_desc, nfit_spa);
2391 * If we timed out the initial scan we'll still be busy here,
2392 * and will wait another timeout before giving up permanently.
2394 if (rc < 0 && rc != -EBUSY)
2398 u64 ars_start, ars_len;
2400 if (acpi_desc->cancel)
2402 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2405 if (rc == -EBUSY && !tmo) {
2406 dev_warn(dev, "range %d ars timeout, aborting\n",
2413 * Note, entries may be appended to the list
2414 * while the lock is dropped, but the workqueue
2415 * being active prevents entries being deleted /
2418 mutex_unlock(&acpi_desc->init_mutex);
2421 mutex_lock(&acpi_desc->init_mutex);
2425 /* we got some results, but there are more pending... */
2426 if (rc == -ENOSPC && overflow_retry--) {
2427 if (!init_ars_len) {
2428 init_ars_len = acpi_desc->ars_status->length;
2429 init_ars_start = acpi_desc->ars_status->address;
2431 rc = ars_continue(acpi_desc);
2435 dev_warn(dev, "range %d ars continuation failed\n",
2441 ars_start = init_ars_start;
2442 ars_len = init_ars_len;
2444 ars_start = acpi_desc->ars_status->address;
2445 ars_len = acpi_desc->ars_status->length;
2447 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2448 spa->range_index, ars_start, ars_len);
2449 /* notify the region about new poison entries */
2450 nvdimm_region_notify(nfit_spa->nd_region,
2451 NVDIMM_REVALIDATE_POISON);
2456 static void acpi_nfit_scrub(struct work_struct *work)
2459 u64 init_scrub_length = 0;
2460 struct nfit_spa *nfit_spa;
2461 u64 init_scrub_address = 0;
2462 bool init_ars_done = false;
2463 struct acpi_nfit_desc *acpi_desc;
2464 unsigned int tmo = scrub_timeout;
2465 unsigned int overflow_retry = scrub_overflow_abort;
2467 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2468 dev = acpi_desc->dev;
2471 * We scrub in 2 phases. The first phase waits for any platform
2472 * firmware initiated scrubs to complete and then we go search for the
2473 * affected spa regions to mark them scanned. In the second phase we
2474 * initiate a directed scrub for every range that was not scrubbed in
2475 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2476 * the first phase, but really only care about running phase 2, where
2477 * regions can be notified of new poison.
2480 /* process platform firmware initiated scrubs */
2482 mutex_lock(&acpi_desc->init_mutex);
2483 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2484 struct nd_cmd_ars_status *ars_status;
2485 struct acpi_nfit_system_address *spa;
2486 u64 ars_start, ars_len;
2489 if (acpi_desc->cancel)
2492 if (nfit_spa->nd_region)
2495 if (init_ars_done) {
2497 * No need to re-query, we're now just
2498 * reconciling all the ranges covered by the
2503 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2505 if (rc == -ENOTTY) {
2506 /* no ars capability, just register spa and move on */
2507 acpi_nfit_register_region(acpi_desc, nfit_spa);
2511 if (rc == -EBUSY && !tmo) {
2512 /* fallthrough to directed scrub in phase 2 */
2513 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2515 } else if (rc == -EBUSY) {
2516 mutex_unlock(&acpi_desc->init_mutex);
2522 /* we got some results, but there are more pending... */
2523 if (rc == -ENOSPC && overflow_retry--) {
2524 ars_status = acpi_desc->ars_status;
2526 * Record the original scrub range, so that we
2527 * can recall all the ranges impacted by the
2530 if (!init_scrub_length) {
2531 init_scrub_length = ars_status->length;
2532 init_scrub_address = ars_status->address;
2534 rc = ars_continue(acpi_desc);
2536 mutex_unlock(&acpi_desc->init_mutex);
2543 * Initial scrub failed, we'll give it one more
2549 /* We got some final results, record completed ranges */
2550 ars_status = acpi_desc->ars_status;
2551 if (init_scrub_length) {
2552 ars_start = init_scrub_address;
2553 ars_len = ars_start + init_scrub_length;
2555 ars_start = ars_status->address;
2556 ars_len = ars_status->length;
2558 spa = nfit_spa->spa;
2560 if (!init_ars_done) {
2561 init_ars_done = true;
2562 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2563 ars_start, ars_len);
2565 if (ars_start <= spa->address && ars_start + ars_len
2566 >= spa->address + spa->length)
2567 acpi_nfit_register_region(acpi_desc, nfit_spa);
2571 * For all the ranges not covered by an initial scrub we still
2572 * want to see if there are errors, but it's ok to discover them
2575 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2577 * Flag all the ranges that still need scrubbing, but
2578 * register them now to make data available.
2580 if (!nfit_spa->nd_region) {
2581 nfit_spa->ars_required = 1;
2582 acpi_nfit_register_region(acpi_desc, nfit_spa);
2586 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2587 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
2588 acpi_desc->scrub_count++;
2589 if (acpi_desc->scrub_count_state)
2590 sysfs_notify_dirent(acpi_desc->scrub_count_state);
2591 mutex_unlock(&acpi_desc->init_mutex);
2594 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2596 struct nfit_spa *nfit_spa;
2598 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2599 int rc, type = nfit_spa_type(nfit_spa->spa);
2601 /* PMEM and VMEM will be registered by the ARS workqueue */
2602 if (type == NFIT_SPA_PM || type == NFIT_SPA_VOLATILE)
2604 /* BLK apertures belong to BLK region registration below */
2605 if (type == NFIT_SPA_BDW)
2607 /* BLK regions don't need to wait for ARS results */
2608 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2613 queue_work(nfit_wq, &acpi_desc->work);
2617 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2618 struct nfit_table_prev *prev)
2620 struct device *dev = acpi_desc->dev;
2622 if (!list_empty(&prev->spas) ||
2623 !list_empty(&prev->memdevs) ||
2624 !list_empty(&prev->dcrs) ||
2625 !list_empty(&prev->bdws) ||
2626 !list_empty(&prev->idts) ||
2627 !list_empty(&prev->flushes)) {
2628 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2634 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2636 struct device *dev = acpi_desc->dev;
2637 struct kernfs_node *nfit;
2638 struct device *bus_dev;
2640 if (!ars_supported(acpi_desc->nvdimm_bus))
2643 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2644 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2646 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2649 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2651 if (!acpi_desc->scrub_count_state) {
2652 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2659 static void acpi_nfit_destruct(void *data)
2661 struct acpi_nfit_desc *acpi_desc = data;
2662 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2665 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2668 mutex_lock(&acpi_desc_lock);
2669 acpi_desc->cancel = 1;
2671 * Bounce the nvdimm bus lock to make sure any in-flight
2672 * acpi_nfit_ars_rescan() submissions have had a chance to
2673 * either submit or see ->cancel set.
2675 device_lock(bus_dev);
2676 device_unlock(bus_dev);
2678 flush_workqueue(nfit_wq);
2679 if (acpi_desc->scrub_count_state)
2680 sysfs_put(acpi_desc->scrub_count_state);
2681 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2682 acpi_desc->nvdimm_bus = NULL;
2683 list_del(&acpi_desc->list);
2684 mutex_unlock(&acpi_desc_lock);
2687 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
2689 struct device *dev = acpi_desc->dev;
2690 struct nfit_table_prev prev;
2694 if (!acpi_desc->nvdimm_bus) {
2695 acpi_nfit_init_dsms(acpi_desc);
2697 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2698 &acpi_desc->nd_desc);
2699 if (!acpi_desc->nvdimm_bus)
2702 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2707 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2711 /* register this acpi_desc for mce notifications */
2712 mutex_lock(&acpi_desc_lock);
2713 list_add_tail(&acpi_desc->list, &acpi_descs);
2714 mutex_unlock(&acpi_desc_lock);
2717 mutex_lock(&acpi_desc->init_mutex);
2719 INIT_LIST_HEAD(&prev.spas);
2720 INIT_LIST_HEAD(&prev.memdevs);
2721 INIT_LIST_HEAD(&prev.dcrs);
2722 INIT_LIST_HEAD(&prev.bdws);
2723 INIT_LIST_HEAD(&prev.idts);
2724 INIT_LIST_HEAD(&prev.flushes);
2726 list_cut_position(&prev.spas, &acpi_desc->spas,
2727 acpi_desc->spas.prev);
2728 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2729 acpi_desc->memdevs.prev);
2730 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2731 acpi_desc->dcrs.prev);
2732 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2733 acpi_desc->bdws.prev);
2734 list_cut_position(&prev.idts, &acpi_desc->idts,
2735 acpi_desc->idts.prev);
2736 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2737 acpi_desc->flushes.prev);
2740 while (!IS_ERR_OR_NULL(data))
2741 data = add_table(acpi_desc, &prev, data, end);
2744 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2750 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2754 rc = nfit_mem_init(acpi_desc);
2758 rc = acpi_nfit_register_dimms(acpi_desc);
2762 rc = acpi_nfit_register_regions(acpi_desc);
2765 mutex_unlock(&acpi_desc->init_mutex);
2768 EXPORT_SYMBOL_GPL(acpi_nfit_init);
2770 struct acpi_nfit_flush_work {
2771 struct work_struct work;
2772 struct completion cmp;
2775 static void flush_probe(struct work_struct *work)
2777 struct acpi_nfit_flush_work *flush;
2779 flush = container_of(work, typeof(*flush), work);
2780 complete(&flush->cmp);
2783 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2785 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2786 struct device *dev = acpi_desc->dev;
2787 struct acpi_nfit_flush_work flush;
2790 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2795 * Scrub work could take 10s of seconds, userspace may give up so we
2796 * need to be interruptible while waiting.
2798 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2799 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2800 queue_work(nfit_wq, &flush.work);
2802 rc = wait_for_completion_interruptible(&flush.cmp);
2803 cancel_work_sync(&flush.work);
2807 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2808 struct nvdimm *nvdimm, unsigned int cmd)
2810 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2814 if (cmd != ND_CMD_ARS_START)
2818 * The kernel and userspace may race to initiate a scrub, but
2819 * the scrub thread is prepared to lose that initial race. It
2820 * just needs guarantees that any ars it initiates are not
2821 * interrupted by any intervening start reqeusts from userspace.
2823 if (work_busy(&acpi_desc->work))
2829 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
2831 struct device *dev = acpi_desc->dev;
2832 struct nfit_spa *nfit_spa;
2834 if (work_busy(&acpi_desc->work))
2837 if (acpi_desc->cancel)
2840 mutex_lock(&acpi_desc->init_mutex);
2841 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2842 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2844 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2847 nfit_spa->ars_required = 1;
2849 queue_work(nfit_wq, &acpi_desc->work);
2850 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2851 mutex_unlock(&acpi_desc->init_mutex);
2856 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
2858 struct nvdimm_bus_descriptor *nd_desc;
2860 dev_set_drvdata(dev, acpi_desc);
2861 acpi_desc->dev = dev;
2862 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2863 nd_desc = &acpi_desc->nd_desc;
2864 nd_desc->provider_name = "ACPI.NFIT";
2865 nd_desc->module = THIS_MODULE;
2866 nd_desc->ndctl = acpi_nfit_ctl;
2867 nd_desc->flush_probe = acpi_nfit_flush_probe;
2868 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
2869 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2871 INIT_LIST_HEAD(&acpi_desc->spas);
2872 INIT_LIST_HEAD(&acpi_desc->dcrs);
2873 INIT_LIST_HEAD(&acpi_desc->bdws);
2874 INIT_LIST_HEAD(&acpi_desc->idts);
2875 INIT_LIST_HEAD(&acpi_desc->flushes);
2876 INIT_LIST_HEAD(&acpi_desc->memdevs);
2877 INIT_LIST_HEAD(&acpi_desc->dimms);
2878 INIT_LIST_HEAD(&acpi_desc->list);
2879 mutex_init(&acpi_desc->init_mutex);
2880 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
2882 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
2884 static int acpi_nfit_add(struct acpi_device *adev)
2886 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
2887 struct acpi_nfit_desc *acpi_desc;
2888 struct device *dev = &adev->dev;
2889 struct acpi_table_header *tbl;
2890 acpi_status status = AE_OK;
2894 status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz);
2895 if (ACPI_FAILURE(status)) {
2896 /* This is ok, we could have an nvdimm hotplugged later */
2897 dev_dbg(dev, "failed to find NFIT at startup\n");
2901 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2904 acpi_nfit_desc_init(acpi_desc, &adev->dev);
2906 /* Save the acpi header for exporting the revision via sysfs */
2907 acpi_desc->acpi_header = *tbl;
2909 /* Evaluate _FIT and override with that if present */
2910 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2911 if (ACPI_SUCCESS(status) && buf.length > 0) {
2912 union acpi_object *obj = buf.pointer;
2914 if (obj->type == ACPI_TYPE_BUFFER)
2915 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2916 obj->buffer.length);
2918 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2919 __func__, (int) obj->type);
2922 /* skip over the lead-in header table */
2923 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2924 + sizeof(struct acpi_table_nfit),
2925 sz - sizeof(struct acpi_table_nfit));
2929 static int acpi_nfit_remove(struct acpi_device *adev)
2931 /* see acpi_nfit_destruct */
2935 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
2937 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
2938 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
2939 union acpi_object *obj;
2943 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2945 if (event != NFIT_NOTIFY_UPDATE)
2949 /* dev->driver may be null if we're being removed */
2950 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
2955 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2958 acpi_nfit_desc_init(acpi_desc, dev);
2961 * Finish previous registration before considering new
2964 flush_workqueue(nfit_wq);
2968 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
2969 if (ACPI_FAILURE(status)) {
2970 dev_err(dev, "failed to evaluate _FIT\n");
2975 if (obj->type == ACPI_TYPE_BUFFER) {
2976 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2977 obj->buffer.length);
2979 dev_err(dev, "failed to merge updated NFIT\n");
2981 dev_err(dev, "Invalid _FIT\n");
2984 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
2986 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2988 device_lock(&adev->dev);
2989 __acpi_nfit_notify(&adev->dev, adev->handle, event);
2990 device_unlock(&adev->dev);
2993 static const struct acpi_device_id acpi_nfit_ids[] = {
2997 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2999 static struct acpi_driver acpi_nfit_driver = {
3000 .name = KBUILD_MODNAME,
3001 .ids = acpi_nfit_ids,
3003 .add = acpi_nfit_add,
3004 .remove = acpi_nfit_remove,
3005 .notify = acpi_nfit_notify,
3009 static __init int nfit_init(void)
3013 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3014 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3015 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3016 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3017 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3018 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3019 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3021 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
3022 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
3023 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
3024 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
3025 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
3026 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
3027 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
3028 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
3029 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
3030 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
3031 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3032 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3033 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3035 nfit_wq = create_singlethread_workqueue("nfit");
3039 nfit_mce_register();
3040 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3042 nfit_mce_unregister();
3043 destroy_workqueue(nfit_wq);
3050 static __exit void nfit_exit(void)
3052 nfit_mce_unregister();
3053 acpi_bus_unregister_driver(&acpi_nfit_driver);
3054 destroy_workqueue(nfit_wq);
3055 WARN_ON(!list_empty(&acpi_descs));
3058 module_init(nfit_init);
3059 module_exit(nfit_exit);
3060 MODULE_LICENSE("GPL v2");
3061 MODULE_AUTHOR("Intel Corporation");