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/ndctl.h>
17 #include <linux/list.h>
18 #include <linux/acpi.h>
21 static bool force_enable_dimms;
22 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
23 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
25 static u8 nfit_uuid[NFIT_UUID_MAX][16];
27 static const u8 *to_nfit_uuid(enum nfit_uuids id)
32 static struct acpi_nfit_desc *to_acpi_nfit_desc(
33 struct nvdimm_bus_descriptor *nd_desc)
35 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
38 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
40 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
43 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
46 if (!nd_desc->provider_name
47 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
50 return to_acpi_device(acpi_desc->dev);
53 static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
54 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
57 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
58 const struct nd_cmd_desc *desc = NULL;
59 union acpi_object in_obj, in_buf, *out_obj;
60 struct device *dev = acpi_desc->dev;
61 const char *cmd_name, *dimm_name;
62 unsigned long dsm_mask;
69 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
70 struct acpi_device *adev = nfit_mem->adev;
74 dimm_name = dev_name(&adev->dev);
75 cmd_name = nvdimm_cmd_name(cmd);
76 dsm_mask = nfit_mem->dsm_mask;
77 desc = nd_cmd_dimm_desc(cmd);
78 uuid = to_nfit_uuid(NFIT_DEV_DIMM);
79 handle = adev->handle;
81 struct acpi_device *adev = to_acpi_dev(acpi_desc);
83 cmd_name = nvdimm_bus_cmd_name(cmd);
84 dsm_mask = nd_desc->dsm_mask;
85 desc = nd_cmd_bus_desc(cmd);
86 uuid = to_nfit_uuid(NFIT_DEV_BUS);
87 handle = adev->handle;
91 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
94 if (!test_bit(cmd, &dsm_mask))
97 in_obj.type = ACPI_TYPE_PACKAGE;
98 in_obj.package.count = 1;
99 in_obj.package.elements = &in_buf;
100 in_buf.type = ACPI_TYPE_BUFFER;
101 in_buf.buffer.pointer = buf;
102 in_buf.buffer.length = 0;
104 /* libnvdimm has already validated the input envelope */
105 for (i = 0; i < desc->in_num; i++)
106 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
109 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
110 dev_dbg(dev, "%s:%s cmd: %s input length: %d\n", __func__,
111 dimm_name, cmd_name, in_buf.buffer.length);
112 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
113 4, in_buf.buffer.pointer, min_t(u32, 128,
114 in_buf.buffer.length), true);
117 out_obj = acpi_evaluate_dsm(handle, uuid, 1, cmd, &in_obj);
119 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
124 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
125 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
126 __func__, dimm_name, cmd_name, out_obj->type);
131 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
132 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
133 dimm_name, cmd_name, out_obj->buffer.length);
134 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
135 4, out_obj->buffer.pointer, min_t(u32, 128,
136 out_obj->buffer.length), true);
139 for (i = 0, offset = 0; i < desc->out_num; i++) {
140 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
141 (u32 *) out_obj->buffer.pointer);
143 if (offset + out_size > out_obj->buffer.length) {
144 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
145 __func__, dimm_name, cmd_name, i);
149 if (in_buf.buffer.length + offset + out_size > buf_len) {
150 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
151 __func__, dimm_name, cmd_name, i);
155 memcpy(buf + in_buf.buffer.length + offset,
156 out_obj->buffer.pointer + offset, out_size);
159 if (offset + in_buf.buffer.length < buf_len) {
162 * status valid, return the number of bytes left
163 * unfilled in the output buffer
165 rc = buf_len - offset - in_buf.buffer.length;
167 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
168 __func__, dimm_name, cmd_name, buf_len,
181 static const char *spa_type_name(u16 type)
183 static const char *to_name[] = {
184 [NFIT_SPA_VOLATILE] = "volatile",
185 [NFIT_SPA_PM] = "pmem",
186 [NFIT_SPA_DCR] = "dimm-control-region",
187 [NFIT_SPA_BDW] = "block-data-window",
188 [NFIT_SPA_VDISK] = "volatile-disk",
189 [NFIT_SPA_VCD] = "volatile-cd",
190 [NFIT_SPA_PDISK] = "persistent-disk",
191 [NFIT_SPA_PCD] = "persistent-cd",
195 if (type > NFIT_SPA_PCD)
198 return to_name[type];
201 static int nfit_spa_type(struct acpi_nfit_system_address *spa)
205 for (i = 0; i < NFIT_UUID_MAX; i++)
206 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
211 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
212 struct acpi_nfit_system_address *spa)
214 struct device *dev = acpi_desc->dev;
215 struct nfit_spa *nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa),
220 INIT_LIST_HEAD(&nfit_spa->list);
222 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
223 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
225 spa_type_name(nfit_spa_type(spa)));
229 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
230 struct acpi_nfit_memory_map *memdev)
232 struct device *dev = acpi_desc->dev;
233 struct nfit_memdev *nfit_memdev = devm_kzalloc(dev,
234 sizeof(*nfit_memdev), GFP_KERNEL);
238 INIT_LIST_HEAD(&nfit_memdev->list);
239 nfit_memdev->memdev = memdev;
240 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
241 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
242 __func__, memdev->device_handle, memdev->range_index,
243 memdev->region_index);
247 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
248 struct acpi_nfit_control_region *dcr)
250 struct device *dev = acpi_desc->dev;
251 struct nfit_dcr *nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr),
256 INIT_LIST_HEAD(&nfit_dcr->list);
258 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
259 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
260 dcr->region_index, dcr->windows);
264 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
265 struct acpi_nfit_data_region *bdw)
267 struct device *dev = acpi_desc->dev;
268 struct nfit_bdw *nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw),
273 INIT_LIST_HEAD(&nfit_bdw->list);
275 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
276 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
277 bdw->region_index, bdw->windows);
281 static void *add_table(struct acpi_nfit_desc *acpi_desc, void *table,
284 struct device *dev = acpi_desc->dev;
285 struct acpi_nfit_header *hdr;
286 void *err = ERR_PTR(-ENOMEM);
293 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
294 if (!add_spa(acpi_desc, table))
297 case ACPI_NFIT_TYPE_MEMORY_MAP:
298 if (!add_memdev(acpi_desc, table))
301 case ACPI_NFIT_TYPE_CONTROL_REGION:
302 if (!add_dcr(acpi_desc, table))
305 case ACPI_NFIT_TYPE_DATA_REGION:
306 if (!add_bdw(acpi_desc, table))
310 case ACPI_NFIT_TYPE_INTERLEAVE:
311 dev_dbg(dev, "%s: idt\n", __func__);
313 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
314 dev_dbg(dev, "%s: flush\n", __func__);
316 case ACPI_NFIT_TYPE_SMBIOS:
317 dev_dbg(dev, "%s: smbios\n", __func__);
320 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
324 return table + hdr->length;
327 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
328 struct nfit_mem *nfit_mem)
330 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
331 u16 dcr = nfit_mem->dcr->region_index;
332 struct nfit_spa *nfit_spa;
334 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
335 u16 range_index = nfit_spa->spa->range_index;
336 int type = nfit_spa_type(nfit_spa->spa);
337 struct nfit_memdev *nfit_memdev;
339 if (type != NFIT_SPA_BDW)
342 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
343 if (nfit_memdev->memdev->range_index != range_index)
345 if (nfit_memdev->memdev->device_handle != device_handle)
347 if (nfit_memdev->memdev->region_index != dcr)
350 nfit_mem->spa_bdw = nfit_spa->spa;
355 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
356 nfit_mem->spa_dcr->range_index);
357 nfit_mem->bdw = NULL;
360 static int nfit_mem_add(struct acpi_nfit_desc *acpi_desc,
361 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
363 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
364 struct nfit_dcr *nfit_dcr;
365 struct nfit_bdw *nfit_bdw;
367 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
368 if (nfit_dcr->dcr->region_index != dcr)
370 nfit_mem->dcr = nfit_dcr->dcr;
374 if (!nfit_mem->dcr) {
375 dev_dbg(acpi_desc->dev, "SPA %d missing:%s%s\n",
376 spa->range_index, __to_nfit_memdev(nfit_mem)
377 ? "" : " MEMDEV", nfit_mem->dcr ? "" : " DCR");
382 * We've found enough to create an nvdimm, optionally
383 * find an associated BDW
385 list_add(&nfit_mem->list, &acpi_desc->dimms);
387 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
388 if (nfit_bdw->bdw->region_index != dcr)
390 nfit_mem->bdw = nfit_bdw->bdw;
397 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
401 static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
402 struct acpi_nfit_system_address *spa)
404 struct nfit_mem *nfit_mem, *found;
405 struct nfit_memdev *nfit_memdev;
406 int type = nfit_spa_type(spa);
417 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
420 if (nfit_memdev->memdev->range_index != spa->range_index)
423 dcr = nfit_memdev->memdev->region_index;
424 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
425 if (__to_nfit_memdev(nfit_mem)->region_index == dcr) {
433 nfit_mem = devm_kzalloc(acpi_desc->dev,
434 sizeof(*nfit_mem), GFP_KERNEL);
437 INIT_LIST_HEAD(&nfit_mem->list);
440 if (type == NFIT_SPA_DCR) {
441 /* multiple dimms may share a SPA when interleaved */
442 nfit_mem->spa_dcr = spa;
443 nfit_mem->memdev_dcr = nfit_memdev->memdev;
446 * A single dimm may belong to multiple SPA-PM
447 * ranges, record at least one in addition to
450 nfit_mem->memdev_pmem = nfit_memdev->memdev;
456 rc = nfit_mem_add(acpi_desc, nfit_mem, spa);
464 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
466 struct nfit_mem *a = container_of(_a, typeof(*a), list);
467 struct nfit_mem *b = container_of(_b, typeof(*b), list);
468 u32 handleA, handleB;
470 handleA = __to_nfit_memdev(a)->device_handle;
471 handleB = __to_nfit_memdev(b)->device_handle;
472 if (handleA < handleB)
474 else if (handleA > handleB)
479 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
481 struct nfit_spa *nfit_spa;
484 * For each SPA-DCR or SPA-PMEM address range find its
485 * corresponding MEMDEV(s). From each MEMDEV find the
486 * corresponding DCR. Then, if we're operating on a SPA-DCR,
487 * try to find a SPA-BDW and a corresponding BDW that references
488 * the DCR. Throw it all into an nfit_mem object. Note, that
491 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
494 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
499 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
504 static ssize_t revision_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
507 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
508 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
509 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
511 return sprintf(buf, "%d\n", acpi_desc->nfit->header.revision);
513 static DEVICE_ATTR_RO(revision);
515 static struct attribute *acpi_nfit_attributes[] = {
516 &dev_attr_revision.attr,
520 static struct attribute_group acpi_nfit_attribute_group = {
522 .attrs = acpi_nfit_attributes,
525 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
526 &nvdimm_bus_attribute_group,
527 &acpi_nfit_attribute_group,
531 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
533 struct nvdimm *nvdimm = to_nvdimm(dev);
534 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
536 return __to_nfit_memdev(nfit_mem);
539 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
541 struct nvdimm *nvdimm = to_nvdimm(dev);
542 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
544 return nfit_mem->dcr;
547 static ssize_t handle_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
550 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
552 return sprintf(buf, "%#x\n", memdev->device_handle);
554 static DEVICE_ATTR_RO(handle);
556 static ssize_t phys_id_show(struct device *dev,
557 struct device_attribute *attr, char *buf)
559 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
561 return sprintf(buf, "%#x\n", memdev->physical_id);
563 static DEVICE_ATTR_RO(phys_id);
565 static ssize_t vendor_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
568 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
570 return sprintf(buf, "%#x\n", dcr->vendor_id);
572 static DEVICE_ATTR_RO(vendor);
574 static ssize_t rev_id_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
577 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
579 return sprintf(buf, "%#x\n", dcr->revision_id);
581 static DEVICE_ATTR_RO(rev_id);
583 static ssize_t device_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
588 return sprintf(buf, "%#x\n", dcr->device_id);
590 static DEVICE_ATTR_RO(device);
592 static ssize_t format_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
597 return sprintf(buf, "%#x\n", dcr->code);
599 static DEVICE_ATTR_RO(format);
601 static ssize_t serial_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
606 return sprintf(buf, "%#x\n", dcr->serial_number);
608 static DEVICE_ATTR_RO(serial);
610 static struct attribute *acpi_nfit_dimm_attributes[] = {
611 &dev_attr_handle.attr,
612 &dev_attr_phys_id.attr,
613 &dev_attr_vendor.attr,
614 &dev_attr_device.attr,
615 &dev_attr_format.attr,
616 &dev_attr_serial.attr,
617 &dev_attr_rev_id.attr,
621 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
622 struct attribute *a, int n)
624 struct device *dev = container_of(kobj, struct device, kobj);
626 if (to_nfit_dcr(dev))
632 static struct attribute_group acpi_nfit_dimm_attribute_group = {
634 .attrs = acpi_nfit_dimm_attributes,
635 .is_visible = acpi_nfit_dimm_attr_visible,
638 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
639 &nvdimm_attribute_group,
640 &nd_device_attribute_group,
641 &acpi_nfit_dimm_attribute_group,
645 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
648 struct nfit_mem *nfit_mem;
650 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
651 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
652 return nfit_mem->nvdimm;
657 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
658 struct nfit_mem *nfit_mem, u32 device_handle)
660 struct acpi_device *adev, *adev_dimm;
661 struct device *dev = acpi_desc->dev;
662 const u8 *uuid = to_nfit_uuid(NFIT_DEV_DIMM);
663 unsigned long long sta;
667 nfit_mem->dsm_mask = acpi_desc->dimm_dsm_force_en;
668 adev = to_acpi_dev(acpi_desc);
672 adev_dimm = acpi_find_child_device(adev, device_handle, false);
673 nfit_mem->adev = adev_dimm;
675 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
677 return force_enable_dimms ? 0 : -ENODEV;
680 status = acpi_evaluate_integer(adev_dimm->handle, "_STA", NULL, &sta);
681 if (status == AE_NOT_FOUND) {
682 dev_dbg(dev, "%s missing _STA, assuming enabled...\n",
683 dev_name(&adev_dimm->dev));
685 } else if (ACPI_FAILURE(status))
686 dev_err(dev, "%s failed to retrieve_STA, disabling...\n",
687 dev_name(&adev_dimm->dev));
688 else if ((sta & ACPI_STA_DEVICE_ENABLED) == 0)
689 dev_info(dev, "%s disabled by firmware\n",
690 dev_name(&adev_dimm->dev));
694 for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++)
695 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
696 set_bit(i, &nfit_mem->dsm_mask);
698 return force_enable_dimms ? 0 : rc;
701 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
703 struct nfit_mem *nfit_mem;
706 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
707 struct nvdimm *nvdimm;
708 unsigned long flags = 0;
712 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
713 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
716 * If for some reason we find multiple DCRs the
719 dev_err(acpi_desc->dev, "duplicate DCR detected: %s\n",
720 nvdimm_name(nvdimm));
724 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
725 flags |= NDD_ALIASING;
727 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
731 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
732 acpi_nfit_dimm_attribute_groups,
733 flags, &nfit_mem->dsm_mask);
737 nfit_mem->nvdimm = nvdimm;
741 return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
744 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
746 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
747 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
748 struct acpi_device *adev;
751 adev = to_acpi_dev(acpi_desc);
755 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_ARS_STATUS; i++)
756 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
757 set_bit(i, &nd_desc->dsm_mask);
760 static ssize_t range_index_show(struct device *dev,
761 struct device_attribute *attr, char *buf)
763 struct nd_region *nd_region = to_nd_region(dev);
764 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
766 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
768 static DEVICE_ATTR_RO(range_index);
770 static struct attribute *acpi_nfit_region_attributes[] = {
771 &dev_attr_range_index.attr,
775 static struct attribute_group acpi_nfit_region_attribute_group = {
777 .attrs = acpi_nfit_region_attributes,
780 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
781 &nd_region_attribute_group,
782 &nd_mapping_attribute_group,
783 &nd_device_attribute_group,
784 &acpi_nfit_region_attribute_group,
788 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
789 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
790 struct acpi_nfit_memory_map *memdev,
791 struct acpi_nfit_system_address *spa)
793 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
794 memdev->device_handle);
795 struct nfit_mem *nfit_mem;
799 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
800 spa->range_index, memdev->device_handle);
804 nd_mapping->nvdimm = nvdimm;
805 switch (nfit_spa_type(spa)) {
807 case NFIT_SPA_VOLATILE:
808 nd_mapping->start = memdev->address;
809 nd_mapping->size = memdev->region_size;
812 nfit_mem = nvdimm_provider_data(nvdimm);
813 if (!nfit_mem || !nfit_mem->bdw) {
814 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
815 spa->range_index, nvdimm_name(nvdimm));
817 nd_mapping->size = nfit_mem->bdw->capacity;
818 nd_mapping->start = nfit_mem->bdw->start_address;
822 ndr_desc->nd_mapping = nd_mapping;
823 ndr_desc->num_mappings = blk_valid;
824 if (!nvdimm_blk_region_create(acpi_desc->nvdimm_bus, ndr_desc))
832 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
833 struct nfit_spa *nfit_spa)
835 static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
836 struct acpi_nfit_system_address *spa = nfit_spa->spa;
837 struct nfit_memdev *nfit_memdev;
838 struct nd_region_desc ndr_desc;
839 struct nvdimm_bus *nvdimm_bus;
843 if (spa->range_index == 0) {
844 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
849 memset(&res, 0, sizeof(res));
850 memset(&nd_mappings, 0, sizeof(nd_mappings));
851 memset(&ndr_desc, 0, sizeof(ndr_desc));
852 res.start = spa->address;
853 res.end = res.start + spa->length - 1;
855 ndr_desc.provider_data = nfit_spa;
856 ndr_desc.attr_groups = acpi_nfit_region_attribute_groups;
857 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
858 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
859 struct nd_mapping *nd_mapping;
862 if (memdev->range_index != spa->range_index)
864 if (count >= ND_MAX_MAPPINGS) {
865 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
866 spa->range_index, ND_MAX_MAPPINGS);
869 nd_mapping = &nd_mappings[count++];
870 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, &ndr_desc,
876 ndr_desc.nd_mapping = nd_mappings;
877 ndr_desc.num_mappings = count;
878 nvdimm_bus = acpi_desc->nvdimm_bus;
879 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
880 if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
882 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
883 if (!nvdimm_volatile_region_create(nvdimm_bus, &ndr_desc))
889 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
891 struct nfit_spa *nfit_spa;
893 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
894 int rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
902 static int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz)
904 struct device *dev = acpi_desc->dev;
909 INIT_LIST_HEAD(&acpi_desc->spas);
910 INIT_LIST_HEAD(&acpi_desc->dcrs);
911 INIT_LIST_HEAD(&acpi_desc->bdws);
912 INIT_LIST_HEAD(&acpi_desc->memdevs);
913 INIT_LIST_HEAD(&acpi_desc->dimms);
915 data = (u8 *) acpi_desc->nfit;
917 data += sizeof(struct acpi_table_nfit);
918 while (!IS_ERR_OR_NULL(data))
919 data = add_table(acpi_desc, data, end);
922 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
924 return PTR_ERR(data);
927 if (nfit_mem_init(acpi_desc) != 0)
930 acpi_nfit_init_dsms(acpi_desc);
932 rc = acpi_nfit_register_dimms(acpi_desc);
936 return acpi_nfit_register_regions(acpi_desc);
939 static int acpi_nfit_add(struct acpi_device *adev)
941 struct nvdimm_bus_descriptor *nd_desc;
942 struct acpi_nfit_desc *acpi_desc;
943 struct device *dev = &adev->dev;
944 struct acpi_table_header *tbl;
945 acpi_status status = AE_OK;
949 status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz);
950 if (ACPI_FAILURE(status)) {
951 dev_err(dev, "failed to find NFIT\n");
955 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
959 dev_set_drvdata(dev, acpi_desc);
960 acpi_desc->dev = dev;
961 acpi_desc->nfit = (struct acpi_table_nfit *) tbl;
962 nd_desc = &acpi_desc->nd_desc;
963 nd_desc->provider_name = "ACPI.NFIT";
964 nd_desc->ndctl = acpi_nfit_ctl;
965 nd_desc->attr_groups = acpi_nfit_attribute_groups;
967 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, nd_desc);
968 if (!acpi_desc->nvdimm_bus)
971 rc = acpi_nfit_init(acpi_desc, sz);
973 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
979 static int acpi_nfit_remove(struct acpi_device *adev)
981 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
983 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
987 static const struct acpi_device_id acpi_nfit_ids[] = {
991 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
993 static struct acpi_driver acpi_nfit_driver = {
994 .name = KBUILD_MODNAME,
995 .ids = acpi_nfit_ids,
997 .add = acpi_nfit_add,
998 .remove = acpi_nfit_remove,
1002 static __init int nfit_init(void)
1004 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
1005 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
1006 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
1007 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
1008 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
1009 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
1010 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
1012 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
1013 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
1014 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
1015 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
1016 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
1017 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
1018 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
1019 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
1020 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
1021 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
1023 return acpi_bus_register_driver(&acpi_nfit_driver);
1026 static __exit void nfit_exit(void)
1028 acpi_bus_unregister_driver(&acpi_nfit_driver);
1031 module_init(nfit_init);
1032 module_exit(nfit_exit);
1033 MODULE_LICENSE("GPL v2");
1034 MODULE_AUTHOR("Intel Corporation");