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/list.h>
19 #include <linux/acpi.h>
20 #include <linux/sort.h>
21 #include <linux/pmem.h>
26 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
29 #include <asm-generic/io-64-nonatomic-hi-lo.h>
31 static bool force_enable_dimms;
32 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
33 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
35 static u8 nfit_uuid[NFIT_UUID_MAX][16];
37 const u8 *to_nfit_uuid(enum nfit_uuids id)
41 EXPORT_SYMBOL(to_nfit_uuid);
43 static struct acpi_nfit_desc *to_acpi_nfit_desc(
44 struct nvdimm_bus_descriptor *nd_desc)
46 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
49 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
51 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
54 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
57 if (!nd_desc->provider_name
58 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
61 return to_acpi_device(acpi_desc->dev);
64 static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
65 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
68 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
69 const struct nd_cmd_desc *desc = NULL;
70 union acpi_object in_obj, in_buf, *out_obj;
71 struct device *dev = acpi_desc->dev;
72 const char *cmd_name, *dimm_name;
73 unsigned long dsm_mask;
80 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
81 struct acpi_device *adev = nfit_mem->adev;
85 dimm_name = nvdimm_name(nvdimm);
86 cmd_name = nvdimm_cmd_name(cmd);
87 dsm_mask = nfit_mem->dsm_mask;
88 desc = nd_cmd_dimm_desc(cmd);
89 uuid = to_nfit_uuid(NFIT_DEV_DIMM);
90 handle = adev->handle;
92 struct acpi_device *adev = to_acpi_dev(acpi_desc);
94 cmd_name = nvdimm_bus_cmd_name(cmd);
95 dsm_mask = nd_desc->dsm_mask;
96 desc = nd_cmd_bus_desc(cmd);
97 uuid = to_nfit_uuid(NFIT_DEV_BUS);
98 handle = adev->handle;
102 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
105 if (!test_bit(cmd, &dsm_mask))
108 in_obj.type = ACPI_TYPE_PACKAGE;
109 in_obj.package.count = 1;
110 in_obj.package.elements = &in_buf;
111 in_buf.type = ACPI_TYPE_BUFFER;
112 in_buf.buffer.pointer = buf;
113 in_buf.buffer.length = 0;
115 /* libnvdimm has already validated the input envelope */
116 for (i = 0; i < desc->in_num; i++)
117 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
120 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
121 dev_dbg(dev, "%s:%s cmd: %s input length: %d\n", __func__,
122 dimm_name, cmd_name, in_buf.buffer.length);
123 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
124 4, in_buf.buffer.pointer, min_t(u32, 128,
125 in_buf.buffer.length), true);
128 out_obj = acpi_evaluate_dsm(handle, uuid, 1, cmd, &in_obj);
130 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
135 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
136 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
137 __func__, dimm_name, cmd_name, out_obj->type);
142 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
143 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
144 dimm_name, cmd_name, out_obj->buffer.length);
145 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
146 4, out_obj->buffer.pointer, min_t(u32, 128,
147 out_obj->buffer.length), true);
150 for (i = 0, offset = 0; i < desc->out_num; i++) {
151 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
152 (u32 *) out_obj->buffer.pointer);
154 if (offset + out_size > out_obj->buffer.length) {
155 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
156 __func__, dimm_name, cmd_name, i);
160 if (in_buf.buffer.length + offset + out_size > buf_len) {
161 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
162 __func__, dimm_name, cmd_name, i);
166 memcpy(buf + in_buf.buffer.length + offset,
167 out_obj->buffer.pointer + offset, out_size);
170 if (offset + in_buf.buffer.length < buf_len) {
173 * status valid, return the number of bytes left
174 * unfilled in the output buffer
176 rc = buf_len - offset - in_buf.buffer.length;
178 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
179 __func__, dimm_name, cmd_name, buf_len,
192 static const char *spa_type_name(u16 type)
194 static const char *to_name[] = {
195 [NFIT_SPA_VOLATILE] = "volatile",
196 [NFIT_SPA_PM] = "pmem",
197 [NFIT_SPA_DCR] = "dimm-control-region",
198 [NFIT_SPA_BDW] = "block-data-window",
199 [NFIT_SPA_VDISK] = "volatile-disk",
200 [NFIT_SPA_VCD] = "volatile-cd",
201 [NFIT_SPA_PDISK] = "persistent-disk",
202 [NFIT_SPA_PCD] = "persistent-cd",
206 if (type > NFIT_SPA_PCD)
209 return to_name[type];
212 static int nfit_spa_type(struct acpi_nfit_system_address *spa)
216 for (i = 0; i < NFIT_UUID_MAX; i++)
217 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
222 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
223 struct acpi_nfit_system_address *spa)
225 struct device *dev = acpi_desc->dev;
226 struct nfit_spa *nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa),
231 INIT_LIST_HEAD(&nfit_spa->list);
233 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
234 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
236 spa_type_name(nfit_spa_type(spa)));
240 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
241 struct acpi_nfit_memory_map *memdev)
243 struct device *dev = acpi_desc->dev;
244 struct nfit_memdev *nfit_memdev = devm_kzalloc(dev,
245 sizeof(*nfit_memdev), GFP_KERNEL);
249 INIT_LIST_HEAD(&nfit_memdev->list);
250 nfit_memdev->memdev = memdev;
251 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
252 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
253 __func__, memdev->device_handle, memdev->range_index,
254 memdev->region_index);
258 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
259 struct acpi_nfit_control_region *dcr)
261 struct device *dev = acpi_desc->dev;
262 struct nfit_dcr *nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr),
267 INIT_LIST_HEAD(&nfit_dcr->list);
269 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
270 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
271 dcr->region_index, dcr->windows);
275 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
276 struct acpi_nfit_data_region *bdw)
278 struct device *dev = acpi_desc->dev;
279 struct nfit_bdw *nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw),
284 INIT_LIST_HEAD(&nfit_bdw->list);
286 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
287 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
288 bdw->region_index, bdw->windows);
292 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
293 struct acpi_nfit_interleave *idt)
295 struct device *dev = acpi_desc->dev;
296 struct nfit_idt *nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt),
301 INIT_LIST_HEAD(&nfit_idt->list);
303 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
304 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
305 idt->interleave_index, idt->line_count);
309 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
310 struct acpi_nfit_flush_address *flush)
312 struct device *dev = acpi_desc->dev;
313 struct nfit_flush *nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush),
318 INIT_LIST_HEAD(&nfit_flush->list);
319 nfit_flush->flush = flush;
320 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
321 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
322 flush->device_handle, flush->hint_count);
326 static void *add_table(struct acpi_nfit_desc *acpi_desc, void *table,
329 struct device *dev = acpi_desc->dev;
330 struct acpi_nfit_header *hdr;
331 void *err = ERR_PTR(-ENOMEM);
338 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
339 if (!add_spa(acpi_desc, table))
342 case ACPI_NFIT_TYPE_MEMORY_MAP:
343 if (!add_memdev(acpi_desc, table))
346 case ACPI_NFIT_TYPE_CONTROL_REGION:
347 if (!add_dcr(acpi_desc, table))
350 case ACPI_NFIT_TYPE_DATA_REGION:
351 if (!add_bdw(acpi_desc, table))
354 case ACPI_NFIT_TYPE_INTERLEAVE:
355 if (!add_idt(acpi_desc, table))
358 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
359 if (!add_flush(acpi_desc, table))
362 case ACPI_NFIT_TYPE_SMBIOS:
363 dev_dbg(dev, "%s: smbios\n", __func__);
366 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
370 return table + hdr->length;
373 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
374 struct nfit_mem *nfit_mem)
376 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
377 u16 dcr = nfit_mem->dcr->region_index;
378 struct nfit_spa *nfit_spa;
380 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
381 u16 range_index = nfit_spa->spa->range_index;
382 int type = nfit_spa_type(nfit_spa->spa);
383 struct nfit_memdev *nfit_memdev;
385 if (type != NFIT_SPA_BDW)
388 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
389 if (nfit_memdev->memdev->range_index != range_index)
391 if (nfit_memdev->memdev->device_handle != device_handle)
393 if (nfit_memdev->memdev->region_index != dcr)
396 nfit_mem->spa_bdw = nfit_spa->spa;
401 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
402 nfit_mem->spa_dcr->range_index);
403 nfit_mem->bdw = NULL;
406 static int nfit_mem_add(struct acpi_nfit_desc *acpi_desc,
407 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
409 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
410 struct nfit_memdev *nfit_memdev;
411 struct nfit_flush *nfit_flush;
412 struct nfit_dcr *nfit_dcr;
413 struct nfit_bdw *nfit_bdw;
414 struct nfit_idt *nfit_idt;
415 u16 idt_idx, range_index;
417 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
418 if (nfit_dcr->dcr->region_index != dcr)
420 nfit_mem->dcr = nfit_dcr->dcr;
424 if (!nfit_mem->dcr) {
425 dev_dbg(acpi_desc->dev, "SPA %d missing:%s%s\n",
426 spa->range_index, __to_nfit_memdev(nfit_mem)
427 ? "" : " MEMDEV", nfit_mem->dcr ? "" : " DCR");
432 * We've found enough to create an nvdimm, optionally
433 * find an associated BDW
435 list_add(&nfit_mem->list, &acpi_desc->dimms);
437 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
438 if (nfit_bdw->bdw->region_index != dcr)
440 nfit_mem->bdw = nfit_bdw->bdw;
447 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
449 if (!nfit_mem->spa_bdw)
452 range_index = nfit_mem->spa_bdw->range_index;
453 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
454 if (nfit_memdev->memdev->range_index != range_index ||
455 nfit_memdev->memdev->region_index != dcr)
457 nfit_mem->memdev_bdw = nfit_memdev->memdev;
458 idt_idx = nfit_memdev->memdev->interleave_index;
459 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
460 if (nfit_idt->idt->interleave_index != idt_idx)
462 nfit_mem->idt_bdw = nfit_idt->idt;
466 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
467 if (nfit_flush->flush->device_handle !=
468 nfit_memdev->memdev->device_handle)
470 nfit_mem->nfit_flush = nfit_flush;
479 static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
480 struct acpi_nfit_system_address *spa)
482 struct nfit_mem *nfit_mem, *found;
483 struct nfit_memdev *nfit_memdev;
484 int type = nfit_spa_type(spa);
495 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
498 if (nfit_memdev->memdev->range_index != spa->range_index)
501 dcr = nfit_memdev->memdev->region_index;
502 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
503 if (__to_nfit_memdev(nfit_mem)->region_index == dcr) {
511 nfit_mem = devm_kzalloc(acpi_desc->dev,
512 sizeof(*nfit_mem), GFP_KERNEL);
515 INIT_LIST_HEAD(&nfit_mem->list);
518 if (type == NFIT_SPA_DCR) {
519 struct nfit_idt *nfit_idt;
522 /* multiple dimms may share a SPA when interleaved */
523 nfit_mem->spa_dcr = spa;
524 nfit_mem->memdev_dcr = nfit_memdev->memdev;
525 idt_idx = nfit_memdev->memdev->interleave_index;
526 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
527 if (nfit_idt->idt->interleave_index != idt_idx)
529 nfit_mem->idt_dcr = nfit_idt->idt;
534 * A single dimm may belong to multiple SPA-PM
535 * ranges, record at least one in addition to
538 nfit_mem->memdev_pmem = nfit_memdev->memdev;
544 rc = nfit_mem_add(acpi_desc, nfit_mem, spa);
552 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
554 struct nfit_mem *a = container_of(_a, typeof(*a), list);
555 struct nfit_mem *b = container_of(_b, typeof(*b), list);
556 u32 handleA, handleB;
558 handleA = __to_nfit_memdev(a)->device_handle;
559 handleB = __to_nfit_memdev(b)->device_handle;
560 if (handleA < handleB)
562 else if (handleA > handleB)
567 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
569 struct nfit_spa *nfit_spa;
572 * For each SPA-DCR or SPA-PMEM address range find its
573 * corresponding MEMDEV(s). From each MEMDEV find the
574 * corresponding DCR. Then, if we're operating on a SPA-DCR,
575 * try to find a SPA-BDW and a corresponding BDW that references
576 * the DCR. Throw it all into an nfit_mem object. Note, that
579 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
582 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
587 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
592 static ssize_t revision_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
596 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
597 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
599 return sprintf(buf, "%d\n", acpi_desc->nfit->header.revision);
601 static DEVICE_ATTR_RO(revision);
603 static struct attribute *acpi_nfit_attributes[] = {
604 &dev_attr_revision.attr,
608 static struct attribute_group acpi_nfit_attribute_group = {
610 .attrs = acpi_nfit_attributes,
613 const struct attribute_group *acpi_nfit_attribute_groups[] = {
614 &nvdimm_bus_attribute_group,
615 &acpi_nfit_attribute_group,
618 EXPORT_SYMBOL_GPL(acpi_nfit_attribute_groups);
620 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
622 struct nvdimm *nvdimm = to_nvdimm(dev);
623 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
625 return __to_nfit_memdev(nfit_mem);
628 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
630 struct nvdimm *nvdimm = to_nvdimm(dev);
631 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
633 return nfit_mem->dcr;
636 static ssize_t handle_show(struct device *dev,
637 struct device_attribute *attr, char *buf)
639 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
641 return sprintf(buf, "%#x\n", memdev->device_handle);
643 static DEVICE_ATTR_RO(handle);
645 static ssize_t phys_id_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
648 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
650 return sprintf(buf, "%#x\n", memdev->physical_id);
652 static DEVICE_ATTR_RO(phys_id);
654 static ssize_t vendor_show(struct device *dev,
655 struct device_attribute *attr, char *buf)
657 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
659 return sprintf(buf, "%#x\n", dcr->vendor_id);
661 static DEVICE_ATTR_RO(vendor);
663 static ssize_t rev_id_show(struct device *dev,
664 struct device_attribute *attr, char *buf)
666 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
668 return sprintf(buf, "%#x\n", dcr->revision_id);
670 static DEVICE_ATTR_RO(rev_id);
672 static ssize_t device_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
677 return sprintf(buf, "%#x\n", dcr->device_id);
679 static DEVICE_ATTR_RO(device);
681 static ssize_t format_show(struct device *dev,
682 struct device_attribute *attr, char *buf)
684 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
686 return sprintf(buf, "%#x\n", dcr->code);
688 static DEVICE_ATTR_RO(format);
690 static ssize_t serial_show(struct device *dev,
691 struct device_attribute *attr, char *buf)
693 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
695 return sprintf(buf, "%#x\n", dcr->serial_number);
697 static DEVICE_ATTR_RO(serial);
699 static ssize_t flags_show(struct device *dev,
700 struct device_attribute *attr, char *buf)
702 u16 flags = to_nfit_memdev(dev)->flags;
704 return sprintf(buf, "%s%s%s%s%s\n",
705 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save " : "",
706 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore " : "",
707 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush " : "",
708 flags & ACPI_NFIT_MEM_ARMED ? "arm " : "",
709 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart " : "");
711 static DEVICE_ATTR_RO(flags);
713 static struct attribute *acpi_nfit_dimm_attributes[] = {
714 &dev_attr_handle.attr,
715 &dev_attr_phys_id.attr,
716 &dev_attr_vendor.attr,
717 &dev_attr_device.attr,
718 &dev_attr_format.attr,
719 &dev_attr_serial.attr,
720 &dev_attr_rev_id.attr,
721 &dev_attr_flags.attr,
725 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
726 struct attribute *a, int n)
728 struct device *dev = container_of(kobj, struct device, kobj);
730 if (to_nfit_dcr(dev))
736 static struct attribute_group acpi_nfit_dimm_attribute_group = {
738 .attrs = acpi_nfit_dimm_attributes,
739 .is_visible = acpi_nfit_dimm_attr_visible,
742 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
743 &nvdimm_attribute_group,
744 &nd_device_attribute_group,
745 &acpi_nfit_dimm_attribute_group,
749 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
752 struct nfit_mem *nfit_mem;
754 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
755 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
756 return nfit_mem->nvdimm;
761 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
762 struct nfit_mem *nfit_mem, u32 device_handle)
764 struct acpi_device *adev, *adev_dimm;
765 struct device *dev = acpi_desc->dev;
766 const u8 *uuid = to_nfit_uuid(NFIT_DEV_DIMM);
767 unsigned long long sta;
771 nfit_mem->dsm_mask = acpi_desc->dimm_dsm_force_en;
772 adev = to_acpi_dev(acpi_desc);
776 adev_dimm = acpi_find_child_device(adev, device_handle, false);
777 nfit_mem->adev = adev_dimm;
779 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
781 return force_enable_dimms ? 0 : -ENODEV;
784 status = acpi_evaluate_integer(adev_dimm->handle, "_STA", NULL, &sta);
785 if (status == AE_NOT_FOUND) {
786 dev_dbg(dev, "%s missing _STA, assuming enabled...\n",
787 dev_name(&adev_dimm->dev));
789 } else if (ACPI_FAILURE(status))
790 dev_err(dev, "%s failed to retrieve_STA, disabling...\n",
791 dev_name(&adev_dimm->dev));
792 else if ((sta & ACPI_STA_DEVICE_ENABLED) == 0)
793 dev_info(dev, "%s disabled by firmware\n",
794 dev_name(&adev_dimm->dev));
798 for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++)
799 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
800 set_bit(i, &nfit_mem->dsm_mask);
802 return force_enable_dimms ? 0 : rc;
805 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
807 struct nfit_mem *nfit_mem;
810 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
811 struct nvdimm *nvdimm;
812 unsigned long flags = 0;
817 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
818 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
821 * If for some reason we find multiple DCRs the
824 dev_err(acpi_desc->dev, "duplicate DCR detected: %s\n",
825 nvdimm_name(nvdimm));
829 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
830 flags |= NDD_ALIASING;
832 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
833 if (mem_flags & ACPI_NFIT_MEM_ARMED)
834 flags |= NDD_UNARMED;
836 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
840 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
841 acpi_nfit_dimm_attribute_groups,
842 flags, &nfit_mem->dsm_mask);
846 nfit_mem->nvdimm = nvdimm;
849 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
852 dev_info(acpi_desc->dev, "%s: failed: %s%s%s%s\n",
854 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save " : "",
855 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore " : "",
856 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush " : "",
857 mem_flags & ACPI_NFIT_MEM_ARMED ? "arm " : "");
861 return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
864 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
866 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
867 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
868 struct acpi_device *adev;
871 adev = to_acpi_dev(acpi_desc);
875 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_ARS_STATUS; i++)
876 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
877 set_bit(i, &nd_desc->dsm_mask);
880 static ssize_t range_index_show(struct device *dev,
881 struct device_attribute *attr, char *buf)
883 struct nd_region *nd_region = to_nd_region(dev);
884 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
886 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
888 static DEVICE_ATTR_RO(range_index);
890 static struct attribute *acpi_nfit_region_attributes[] = {
891 &dev_attr_range_index.attr,
895 static struct attribute_group acpi_nfit_region_attribute_group = {
897 .attrs = acpi_nfit_region_attributes,
900 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
901 &nd_region_attribute_group,
902 &nd_mapping_attribute_group,
903 &nd_device_attribute_group,
904 &nd_numa_attribute_group,
905 &acpi_nfit_region_attribute_group,
909 /* enough info to uniquely specify an interleave set */
910 struct nfit_set_info {
911 struct nfit_set_info_map {
918 static size_t sizeof_nfit_set_info(int num_mappings)
920 return sizeof(struct nfit_set_info)
921 + num_mappings * sizeof(struct nfit_set_info_map);
924 static int cmp_map(const void *m0, const void *m1)
926 const struct nfit_set_info_map *map0 = m0;
927 const struct nfit_set_info_map *map1 = m1;
929 return memcmp(&map0->region_offset, &map1->region_offset,
933 /* Retrieve the nth entry referencing this spa */
934 static struct acpi_nfit_memory_map *memdev_from_spa(
935 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
937 struct nfit_memdev *nfit_memdev;
939 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
940 if (nfit_memdev->memdev->range_index == range_index)
942 return nfit_memdev->memdev;
946 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
947 struct nd_region_desc *ndr_desc,
948 struct acpi_nfit_system_address *spa)
950 int i, spa_type = nfit_spa_type(spa);
951 struct device *dev = acpi_desc->dev;
952 struct nd_interleave_set *nd_set;
953 u16 nr = ndr_desc->num_mappings;
954 struct nfit_set_info *info;
956 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
961 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
965 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
968 for (i = 0; i < nr; i++) {
969 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
970 struct nfit_set_info_map *map = &info->mapping[i];
971 struct nvdimm *nvdimm = nd_mapping->nvdimm;
972 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
973 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
974 spa->range_index, i);
976 if (!memdev || !nfit_mem->dcr) {
977 dev_err(dev, "%s: failed to find DCR\n", __func__);
981 map->region_offset = memdev->region_offset;
982 map->serial_number = nfit_mem->dcr->serial_number;
985 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
987 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
988 ndr_desc->nd_set = nd_set;
989 devm_kfree(dev, info);
994 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
996 struct acpi_nfit_interleave *idt = mmio->idt;
997 u32 sub_line_offset, line_index, line_offset;
998 u64 line_no, table_skip_count, table_offset;
1000 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1001 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1002 line_offset = idt->line_offset[line_index]
1004 table_offset = table_skip_count * mmio->table_size;
1006 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1009 static void wmb_blk(struct nfit_blk *nfit_blk)
1012 if (nfit_blk->nvdimm_flush) {
1014 * The first wmb() is needed to 'sfence' all previous writes
1015 * such that they are architecturally visible for the platform
1016 * buffer flush. Note that we've already arranged for pmem
1017 * writes to avoid the cache via arch_memcpy_to_pmem(). The
1018 * final wmb() ensures ordering for the NVDIMM flush write.
1021 writeq(1, nfit_blk->nvdimm_flush);
1027 static u64 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1029 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1030 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1032 if (mmio->num_lines)
1033 offset = to_interleave_offset(offset, mmio);
1035 return readq(mmio->base + offset);
1038 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1039 resource_size_t dpa, unsigned int len, unsigned int write)
1042 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1045 BCW_OFFSET_MASK = (1ULL << 48)-1,
1047 BCW_LEN_MASK = (1ULL << 8) - 1,
1051 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1052 len = len >> L1_CACHE_SHIFT;
1053 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1054 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1056 offset = nfit_blk->cmd_offset + mmio->size * bw;
1057 if (mmio->num_lines)
1058 offset = to_interleave_offset(offset, mmio);
1060 writeq(cmd, mmio->base + offset);
1063 if (nfit_blk->dimm_flags & ND_BLK_DCR_LATCH)
1064 readq(mmio->base + offset);
1067 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1068 resource_size_t dpa, void *iobuf, size_t len, int rw,
1071 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1072 unsigned int copied = 0;
1076 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1077 + lane * mmio->size;
1078 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1083 if (mmio->num_lines) {
1086 offset = to_interleave_offset(base_offset + copied,
1088 div_u64_rem(offset, mmio->line_size, &line_offset);
1089 c = min_t(size_t, len, mmio->line_size - line_offset);
1091 offset = base_offset + nfit_blk->bdw_offset;
1096 memcpy_to_pmem(mmio->aperture + offset,
1099 memcpy_from_pmem(iobuf + copied,
1100 mmio->aperture + offset, c);
1109 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1113 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1114 resource_size_t dpa, void *iobuf, u64 len, int rw)
1116 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1117 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1118 struct nd_region *nd_region = nfit_blk->nd_region;
1119 unsigned int lane, copied = 0;
1122 lane = nd_region_acquire_lane(nd_region);
1124 u64 c = min(len, mmio->size);
1126 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1127 iobuf + copied, c, rw, lane);
1134 nd_region_release_lane(nd_region, lane);
1139 static void nfit_spa_mapping_release(struct kref *kref)
1141 struct nfit_spa_mapping *spa_map = to_spa_map(kref);
1142 struct acpi_nfit_system_address *spa = spa_map->spa;
1143 struct acpi_nfit_desc *acpi_desc = spa_map->acpi_desc;
1145 WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1146 dev_dbg(acpi_desc->dev, "%s: SPA%d\n", __func__, spa->range_index);
1147 iounmap(spa_map->iomem);
1148 release_mem_region(spa->address, spa->length);
1149 list_del(&spa_map->list);
1153 static struct nfit_spa_mapping *find_spa_mapping(
1154 struct acpi_nfit_desc *acpi_desc,
1155 struct acpi_nfit_system_address *spa)
1157 struct nfit_spa_mapping *spa_map;
1159 WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1160 list_for_each_entry(spa_map, &acpi_desc->spa_maps, list)
1161 if (spa_map->spa == spa)
1167 static void nfit_spa_unmap(struct acpi_nfit_desc *acpi_desc,
1168 struct acpi_nfit_system_address *spa)
1170 struct nfit_spa_mapping *spa_map;
1172 mutex_lock(&acpi_desc->spa_map_mutex);
1173 spa_map = find_spa_mapping(acpi_desc, spa);
1176 kref_put(&spa_map->kref, nfit_spa_mapping_release);
1177 mutex_unlock(&acpi_desc->spa_map_mutex);
1180 static void __iomem *__nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
1181 struct acpi_nfit_system_address *spa, enum spa_map_type type)
1183 resource_size_t start = spa->address;
1184 resource_size_t n = spa->length;
1185 struct nfit_spa_mapping *spa_map;
1186 struct resource *res;
1188 WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1190 spa_map = find_spa_mapping(acpi_desc, spa);
1192 kref_get(&spa_map->kref);
1193 return spa_map->iomem;
1196 spa_map = kzalloc(sizeof(*spa_map), GFP_KERNEL);
1200 INIT_LIST_HEAD(&spa_map->list);
1202 kref_init(&spa_map->kref);
1203 spa_map->acpi_desc = acpi_desc;
1205 res = request_mem_region(start, n, dev_name(acpi_desc->dev));
1209 if (type == SPA_MAP_APERTURE) {
1211 * TODO: memremap_pmem() support, but that requires cache
1212 * flushing when the aperture is moved.
1214 spa_map->iomem = ioremap_wc(start, n);
1216 spa_map->iomem = ioremap_nocache(start, n);
1218 if (!spa_map->iomem)
1221 list_add_tail(&spa_map->list, &acpi_desc->spa_maps);
1222 return spa_map->iomem;
1225 release_mem_region(start, n);
1232 * nfit_spa_map - interleave-aware managed-mappings of acpi_nfit_system_address ranges
1233 * @nvdimm_bus: NFIT-bus that provided the spa table entry
1234 * @nfit_spa: spa table to map
1235 * @type: aperture or control region
1237 * In the case where block-data-window apertures and
1238 * dimm-control-regions are interleaved they will end up sharing a
1239 * single request_mem_region() + ioremap() for the address range. In
1240 * the style of devm nfit_spa_map() mappings are automatically dropped
1241 * when all region devices referencing the same mapping are disabled /
1244 static void __iomem *nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
1245 struct acpi_nfit_system_address *spa, enum spa_map_type type)
1247 void __iomem *iomem;
1249 mutex_lock(&acpi_desc->spa_map_mutex);
1250 iomem = __nfit_spa_map(acpi_desc, spa, type);
1251 mutex_unlock(&acpi_desc->spa_map_mutex);
1256 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1257 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1260 mmio->num_lines = idt->line_count;
1261 mmio->line_size = idt->line_size;
1262 if (interleave_ways == 0)
1264 mmio->table_size = mmio->num_lines * interleave_ways
1271 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1272 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1274 struct nd_cmd_dimm_flags flags;
1277 memset(&flags, 0, sizeof(flags));
1278 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
1281 if (rc >= 0 && flags.status == 0)
1282 nfit_blk->dimm_flags = flags.flags;
1283 else if (rc == -ENOTTY) {
1284 /* fall back to a conservative default */
1285 nfit_blk->dimm_flags = ND_BLK_DCR_LATCH;
1293 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1296 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1297 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1298 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1299 struct nfit_flush *nfit_flush;
1300 struct nfit_blk_mmio *mmio;
1301 struct nfit_blk *nfit_blk;
1302 struct nfit_mem *nfit_mem;
1303 struct nvdimm *nvdimm;
1306 nvdimm = nd_blk_region_to_dimm(ndbr);
1307 nfit_mem = nvdimm_provider_data(nvdimm);
1308 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1309 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1310 nfit_mem ? "" : " nfit_mem",
1311 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1312 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
1316 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1319 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1320 nfit_blk->nd_region = to_nd_region(dev);
1322 /* map block aperture memory */
1323 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1324 mmio = &nfit_blk->mmio[BDW];
1325 mmio->base = nfit_spa_map(acpi_desc, nfit_mem->spa_bdw,
1328 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1329 nvdimm_name(nvdimm));
1332 mmio->size = nfit_mem->bdw->size;
1333 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1334 mmio->idt = nfit_mem->idt_bdw;
1335 mmio->spa = nfit_mem->spa_bdw;
1336 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1337 nfit_mem->memdev_bdw->interleave_ways);
1339 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1340 __func__, nvdimm_name(nvdimm));
1344 /* map block control memory */
1345 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1346 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1347 mmio = &nfit_blk->mmio[DCR];
1348 mmio->base = nfit_spa_map(acpi_desc, nfit_mem->spa_dcr,
1351 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1352 nvdimm_name(nvdimm));
1355 mmio->size = nfit_mem->dcr->window_size;
1356 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1357 mmio->idt = nfit_mem->idt_dcr;
1358 mmio->spa = nfit_mem->spa_dcr;
1359 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1360 nfit_mem->memdev_dcr->interleave_ways);
1362 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1363 __func__, nvdimm_name(nvdimm));
1367 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1369 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1370 __func__, nvdimm_name(nvdimm));
1374 nfit_flush = nfit_mem->nfit_flush;
1375 if (nfit_flush && nfit_flush->flush->hint_count != 0) {
1376 nfit_blk->nvdimm_flush = devm_ioremap_nocache(dev,
1377 nfit_flush->flush->hint_address[0], 8);
1378 if (!nfit_blk->nvdimm_flush)
1382 if (!arch_has_pmem_api() && !nfit_blk->nvdimm_flush)
1383 dev_warn(dev, "unable to guarantee persistence of writes\n");
1385 if (mmio->line_size == 0)
1388 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1389 + 8 > mmio->line_size) {
1390 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1392 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1393 + 8 > mmio->line_size) {
1394 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1401 static void acpi_nfit_blk_region_disable(struct nvdimm_bus *nvdimm_bus,
1404 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1405 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1406 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1407 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1411 return; /* never enabled */
1413 /* auto-free BLK spa mappings */
1414 for (i = 0; i < 2; i++) {
1415 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[i];
1418 nfit_spa_unmap(acpi_desc, mmio->spa);
1420 nd_blk_region_set_provider_data(ndbr, NULL);
1421 /* devm will free nfit_blk */
1424 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
1425 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
1426 struct acpi_nfit_memory_map *memdev,
1427 struct acpi_nfit_system_address *spa)
1429 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
1430 memdev->device_handle);
1431 struct nd_blk_region_desc *ndbr_desc;
1432 struct nfit_mem *nfit_mem;
1436 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
1437 spa->range_index, memdev->device_handle);
1441 nd_mapping->nvdimm = nvdimm;
1442 switch (nfit_spa_type(spa)) {
1444 case NFIT_SPA_VOLATILE:
1445 nd_mapping->start = memdev->address;
1446 nd_mapping->size = memdev->region_size;
1449 nfit_mem = nvdimm_provider_data(nvdimm);
1450 if (!nfit_mem || !nfit_mem->bdw) {
1451 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
1452 spa->range_index, nvdimm_name(nvdimm));
1454 nd_mapping->size = nfit_mem->bdw->capacity;
1455 nd_mapping->start = nfit_mem->bdw->start_address;
1456 ndr_desc->num_lanes = nfit_mem->bdw->windows;
1460 ndr_desc->nd_mapping = nd_mapping;
1461 ndr_desc->num_mappings = blk_valid;
1462 ndbr_desc = to_blk_region_desc(ndr_desc);
1463 ndbr_desc->enable = acpi_nfit_blk_region_enable;
1464 ndbr_desc->disable = acpi_nfit_blk_region_disable;
1465 ndbr_desc->do_io = acpi_desc->blk_do_io;
1466 if (!nvdimm_blk_region_create(acpi_desc->nvdimm_bus, ndr_desc))
1474 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
1475 struct nfit_spa *nfit_spa)
1477 static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
1478 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1479 struct nd_blk_region_desc ndbr_desc;
1480 struct nd_region_desc *ndr_desc;
1481 struct nfit_memdev *nfit_memdev;
1482 struct nvdimm_bus *nvdimm_bus;
1483 struct resource res;
1486 if (spa->range_index == 0) {
1487 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
1492 memset(&res, 0, sizeof(res));
1493 memset(&nd_mappings, 0, sizeof(nd_mappings));
1494 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
1495 res.start = spa->address;
1496 res.end = res.start + spa->length - 1;
1497 ndr_desc = &ndbr_desc.ndr_desc;
1498 ndr_desc->res = &res;
1499 ndr_desc->provider_data = nfit_spa;
1500 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
1501 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
1502 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
1503 spa->proximity_domain);
1505 ndr_desc->numa_node = NUMA_NO_NODE;
1507 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1508 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1509 struct nd_mapping *nd_mapping;
1511 if (memdev->range_index != spa->range_index)
1513 if (count >= ND_MAX_MAPPINGS) {
1514 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
1515 spa->range_index, ND_MAX_MAPPINGS);
1518 nd_mapping = &nd_mappings[count++];
1519 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc,
1525 ndr_desc->nd_mapping = nd_mappings;
1526 ndr_desc->num_mappings = count;
1527 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
1531 nvdimm_bus = acpi_desc->nvdimm_bus;
1532 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
1533 if (!nvdimm_pmem_region_create(nvdimm_bus, ndr_desc))
1535 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
1536 if (!nvdimm_volatile_region_create(nvdimm_bus, ndr_desc))
1542 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
1544 struct nfit_spa *nfit_spa;
1546 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1547 int rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
1555 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz)
1557 struct device *dev = acpi_desc->dev;
1562 INIT_LIST_HEAD(&acpi_desc->spa_maps);
1563 INIT_LIST_HEAD(&acpi_desc->spas);
1564 INIT_LIST_HEAD(&acpi_desc->dcrs);
1565 INIT_LIST_HEAD(&acpi_desc->bdws);
1566 INIT_LIST_HEAD(&acpi_desc->idts);
1567 INIT_LIST_HEAD(&acpi_desc->flushes);
1568 INIT_LIST_HEAD(&acpi_desc->memdevs);
1569 INIT_LIST_HEAD(&acpi_desc->dimms);
1570 mutex_init(&acpi_desc->spa_map_mutex);
1572 data = (u8 *) acpi_desc->nfit;
1574 data += sizeof(struct acpi_table_nfit);
1575 while (!IS_ERR_OR_NULL(data))
1576 data = add_table(acpi_desc, data, end);
1579 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
1581 return PTR_ERR(data);
1584 if (nfit_mem_init(acpi_desc) != 0)
1587 acpi_nfit_init_dsms(acpi_desc);
1589 rc = acpi_nfit_register_dimms(acpi_desc);
1593 return acpi_nfit_register_regions(acpi_desc);
1595 EXPORT_SYMBOL_GPL(acpi_nfit_init);
1597 static int acpi_nfit_add(struct acpi_device *adev)
1599 struct nvdimm_bus_descriptor *nd_desc;
1600 struct acpi_nfit_desc *acpi_desc;
1601 struct device *dev = &adev->dev;
1602 struct acpi_table_header *tbl;
1603 acpi_status status = AE_OK;
1607 status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz);
1608 if (ACPI_FAILURE(status)) {
1609 dev_err(dev, "failed to find NFIT\n");
1613 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
1617 dev_set_drvdata(dev, acpi_desc);
1618 acpi_desc->dev = dev;
1619 acpi_desc->nfit = (struct acpi_table_nfit *) tbl;
1620 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
1621 nd_desc = &acpi_desc->nd_desc;
1622 nd_desc->provider_name = "ACPI.NFIT";
1623 nd_desc->ndctl = acpi_nfit_ctl;
1624 nd_desc->attr_groups = acpi_nfit_attribute_groups;
1626 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, nd_desc);
1627 if (!acpi_desc->nvdimm_bus)
1630 rc = acpi_nfit_init(acpi_desc, sz);
1632 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
1638 static int acpi_nfit_remove(struct acpi_device *adev)
1640 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
1642 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
1646 static const struct acpi_device_id acpi_nfit_ids[] = {
1650 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
1652 static struct acpi_driver acpi_nfit_driver = {
1653 .name = KBUILD_MODNAME,
1654 .ids = acpi_nfit_ids,
1656 .add = acpi_nfit_add,
1657 .remove = acpi_nfit_remove,
1661 static __init int nfit_init(void)
1663 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
1664 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
1665 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
1666 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
1667 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
1668 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
1669 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
1671 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
1672 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
1673 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
1674 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
1675 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
1676 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
1677 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
1678 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
1679 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
1680 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
1682 return acpi_bus_register_driver(&acpi_nfit_driver);
1685 static __exit void nfit_exit(void)
1687 acpi_bus_unregister_driver(&acpi_nfit_driver);
1690 module_init(nfit_init);
1691 module_exit(nfit_exit);
1692 MODULE_LICENSE("GPL v2");
1693 MODULE_AUTHOR("Intel Corporation");