2 * Copyright (c) 2017 The FreeBSD Foundation
4 * Copyright (c) 2018, 2019 Intel Corporation
6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/bitstring.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acuuid.h>
49 #include <dev/acpica/acpivar.h>
50 #include <dev/nvdimm/nvdimm_var.h>
52 #define _COMPONENT ACPI_OEM
53 ACPI_MODULE_NAME("NVDIMM")
55 static struct uuid intel_nvdimm_dsm_uuid =
56 {0x4309AC30,0x0D11,0x11E4,0x91,0x91,{0x08,0x00,0x20,0x0C,0x9A,0x66}};
57 #define INTEL_NVDIMM_DSM_REV 1
58 #define INTEL_NVDIMM_DSM_GET_LABEL_SIZE 4
59 #define INTEL_NVDIMM_DSM_GET_LABEL_DATA 5
61 static devclass_t nvdimm_devclass;
62 static devclass_t nvdimm_root_devclass;
63 MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory");
66 read_label_area_size(struct nvdimm_dev *nv)
68 ACPI_OBJECT *result_buffer;
75 handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
78 result.Length = ACPI_ALLOCATE_BUFFER;
79 result.Pointer = NULL;
80 status = acpi_EvaluateDSM(handle, (uint8_t *)&intel_nvdimm_dsm_uuid,
81 INTEL_NVDIMM_DSM_REV, INTEL_NVDIMM_DSM_GET_LABEL_SIZE, NULL,
84 if (ACPI_SUCCESS(status) && result.Pointer != NULL &&
85 result.Length >= sizeof(ACPI_OBJECT)) {
86 result_buffer = result.Pointer;
87 if (result_buffer->Type == ACPI_TYPE_BUFFER &&
88 result_buffer->Buffer.Length >= 12) {
89 out = (uint32_t *)result_buffer->Buffer.Pointer;
90 nv->label_area_size = out[1];
91 nv->max_label_xfer = out[2];
95 if (result.Pointer != NULL)
96 AcpiOsFree(result.Pointer);
101 read_label_area(struct nvdimm_dev *nv, uint8_t *dest, off_t offset,
106 ACPI_OBJECT params_pkg, params_buf, *result_buf;
113 handle = nvdimm_root_get_acpi_handle(nv->nv_dev);
114 if (offset < 0 || length <= 0 ||
115 offset + length > nv->label_area_size ||
118 params_pkg.Type = ACPI_TYPE_PACKAGE;
119 params_pkg.Package.Count = 1;
120 params_pkg.Package.Elements = ¶ms_buf;
121 params_buf.Type = ACPI_TYPE_BUFFER;
122 params_buf.Buffer.Length = sizeof(params);
123 params_buf.Buffer.Pointer = (UINT8 *)params;
125 to_read = MIN(length, nv->max_label_xfer);
128 result.Length = ACPI_ALLOCATE_BUFFER;
129 result.Pointer = NULL;
130 status = acpi_EvaluateDSM(handle,
131 (uint8_t *)&intel_nvdimm_dsm_uuid, INTEL_NVDIMM_DSM_REV,
132 INTEL_NVDIMM_DSM_GET_LABEL_DATA, ¶ms_pkg, &result);
133 if (ACPI_FAILURE(status) ||
134 result.Length < sizeof(ACPI_OBJECT) ||
135 result.Pointer == NULL) {
139 result_buf = (ACPI_OBJECT *)result.Pointer;
140 if (result_buf->Type != ACPI_TYPE_BUFFER ||
141 result_buf->Buffer.Pointer == NULL ||
142 result_buf->Buffer.Length != 4 + to_read ||
143 ((uint16_t *)result_buf->Buffer.Pointer)[0] != 0) {
147 bcopy(result_buf->Buffer.Pointer + 4, dest, to_read);
151 if (result.Pointer != NULL) {
152 AcpiOsFree(result.Pointer);
153 result.Pointer = NULL;
156 if (result.Pointer != NULL)
157 AcpiOsFree(result.Pointer);
162 fletcher64(const void *data, size_t length)
170 d = (const uint32_t *)data;
171 length = length / sizeof(uint32_t);
172 for (i = 0; i < length; i++) {
176 return ((uint64_t)b << 32 | a);
180 label_index_is_valid(struct nvdimm_label_index *index, uint32_t max_labels,
181 size_t size, size_t offset)
185 index = (struct nvdimm_label_index *)((uint8_t *)index + offset);
186 if (strcmp(index->signature, NVDIMM_INDEX_BLOCK_SIGNATURE) != 0)
188 checksum = index->checksum;
190 if (checksum != fletcher64(index, size) ||
191 index->this_offset != size * offset || index->this_size != size ||
192 index->other_offset != size * (offset == 0 ? 1 : 0) ||
193 index->seq == 0 || index->seq > 3 || index->slot_cnt > max_labels ||
194 index->label_size != 1)
200 read_label(struct nvdimm_dev *nv, int num)
202 struct nvdimm_label_entry *entry, *i, *next;
207 offset = nv->label_index->label_offset +
208 num * (128 << nv->label_index->label_size);
209 entry = malloc(sizeof(*entry), M_NVDIMM, M_WAITOK);
210 error = read_label_area(nv, (uint8_t *)&entry->label, offset,
211 sizeof(struct nvdimm_label));
213 free(entry, M_NVDIMM);
216 checksum = entry->label.checksum;
217 entry->label.checksum = 0;
218 if (checksum != fletcher64(&entry->label, sizeof(entry->label)) ||
219 entry->label.slot != num) {
220 free(entry, M_NVDIMM);
224 /* Insertion ordered by dimm_phys_addr */
225 if (SLIST_EMPTY(&nv->labels) ||
226 entry->label.dimm_phys_addr <=
227 SLIST_FIRST(&nv->labels)->label.dimm_phys_addr) {
228 SLIST_INSERT_HEAD(&nv->labels, entry, link);
231 SLIST_FOREACH_SAFE(i, &nv->labels, link, next) {
233 entry->label.dimm_phys_addr <= next->label.dimm_phys_addr) {
234 SLIST_INSERT_AFTER(i, entry, link);
242 read_labels(struct nvdimm_dev *nv)
244 struct nvdimm_label_index *indices;
245 size_t bitfield_size, index_size, num_labels;
247 bool index_0_valid, index_1_valid;
249 for (index_size = 256; ; index_size += 256) {
250 num_labels = 8 * (index_size -
251 sizeof(struct nvdimm_label_index));
252 if (index_size + num_labels * sizeof(struct nvdimm_label) >=
256 num_labels = (nv->label_area_size - index_size) /
257 sizeof(struct nvdimm_label);
258 bitfield_size = roundup2(num_labels, 8) / 8;
259 indices = malloc(2 * index_size, M_NVDIMM, M_WAITOK);
260 error = read_label_area(nv, (void *)indices, 0, 2 * index_size);
262 free(indices, M_NVDIMM);
265 index_0_valid = label_index_is_valid(indices, num_labels, index_size,
267 index_1_valid = label_index_is_valid(indices, num_labels, index_size,
269 if (!index_0_valid && !index_1_valid) {
270 free(indices, M_NVDIMM);
273 if (index_0_valid && index_1_valid &&
274 (indices[1].seq > indices[0].seq ||
275 (indices[1].seq == 1 && indices[0].seq == 3)))
276 index_0_valid = false;
277 nv->label_index = malloc(index_size, M_NVDIMM, M_WAITOK);
278 bcopy(indices + (index_0_valid ? 0 : 1), nv->label_index, index_size);
279 free(indices, M_NVDIMM);
280 for (bit_ffc_at((bitstr_t *)nv->label_index->free, 0, num_labels, &n);
282 bit_ffc_at((bitstr_t *)nv->label_index->free, n + 1, num_labels,
290 nvdimm_find_by_handle(nfit_handle_t nv_handle)
292 struct nvdimm_dev *res;
294 int i, error, num_dimms;
297 error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
300 for (i = 0; i < num_dimms; i++) {
301 if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) {
302 res = device_get_softc(dimms[i]);
311 nvdimm_probe(device_t dev)
314 return (BUS_PROBE_NOWILDCARD);
318 nvdimm_attach(device_t dev)
320 struct nvdimm_dev *nv;
321 ACPI_TABLE_NFIT *nfitbl;
326 nv = device_get_softc(dev);
327 handle = nvdimm_root_get_acpi_handle(dev);
331 nv->nv_handle = nvdimm_root_get_device_handle(dev);
333 status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
334 if (ACPI_FAILURE(status)) {
336 device_printf(dev, "cannot get NFIT\n");
339 acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
340 &nv->nv_flush_addr_cnt);
341 AcpiPutTable(&nfitbl->Header);
342 error = read_label_area_size(nv);
345 * Ignoring errors reading labels. Not all NVDIMMs
346 * support labels and namespaces.
354 nvdimm_detach(device_t dev)
356 struct nvdimm_dev *nv;
357 struct nvdimm_label_entry *label, *next;
359 nv = device_get_softc(dev);
360 free(nv->nv_flush_addr, M_NVDIMM);
361 free(nv->label_index, M_NVDIMM);
362 SLIST_FOREACH_SAFE(label, &nv->labels, link, next) {
363 SLIST_REMOVE_HEAD(&nv->labels, link);
364 free(label, M_NVDIMM);
370 nvdimm_suspend(device_t dev)
377 nvdimm_resume(device_t dev)
384 find_dimm(ACPI_HANDLE handle, UINT32 nesting_level, void *context,
387 ACPI_DEVICE_INFO *device_info;
390 status = AcpiGetObjectInfo(handle, &device_info);
391 if (ACPI_FAILURE(status))
392 return_ACPI_STATUS(AE_ERROR);
393 if (device_info->Address == (uintptr_t)context) {
394 *(ACPI_HANDLE *)return_value = handle;
395 return_ACPI_STATUS(AE_CTRL_TERMINATE);
397 return_ACPI_STATUS(AE_OK);
401 get_dimm_acpi_handle(ACPI_HANDLE root_handle, nfit_handle_t adr)
407 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, root_handle, 1, find_dimm,
408 NULL, (void *)(uintptr_t)adr, &res);
409 if (ACPI_FAILURE(status))
415 nvdimm_root_create_devs(device_t dev, ACPI_TABLE_NFIT *nfitbl)
417 ACPI_HANDLE root_handle, dimm_handle;
419 nfit_handle_t *dimm_ids, *dimm;
423 root_handle = acpi_get_handle(dev);
424 acpi_nfit_get_dimm_ids(nfitbl, &dimm_ids, &num_dimm_ids);
425 for (dimm = dimm_ids; dimm < dimm_ids + num_dimm_ids; dimm++) {
426 dimm_handle = get_dimm_acpi_handle(root_handle, *dimm);
427 child = BUS_ADD_CHILD(dev, 100, "nvdimm", -1);
429 device_printf(dev, "failed to create nvdimm\n");
432 ivars = mallocarray(NVDIMM_ROOT_IVAR_MAX, sizeof(uintptr_t),
433 M_NVDIMM, M_ZERO | M_WAITOK);
434 device_set_ivars(child, ivars);
435 nvdimm_root_set_acpi_handle(child, dimm_handle);
436 nvdimm_root_set_device_handle(child, *dimm);
438 free(dimm_ids, M_NVDIMM);
443 nvdimm_root_create_spas(struct nvdimm_root_dev *dev, ACPI_TABLE_NFIT *nfitbl)
445 ACPI_NFIT_SYSTEM_ADDRESS **spas, **spa;
446 struct SPA_mapping *spa_mapping;
447 enum SPA_mapping_type spa_type;
451 acpi_nfit_get_spa_ranges(nfitbl, &spas, &num_spas);
452 for (spa = spas; spa < spas + num_spas; spa++) {
453 spa_type = nvdimm_spa_type_from_uuid(
454 (struct uuid *)(*spa)->RangeGuid);
455 if (spa_type == SPA_TYPE_UNKNOWN)
457 spa_mapping = malloc(sizeof(struct SPA_mapping), M_NVDIMM,
459 error = nvdimm_spa_init(spa_mapping, *spa, spa_type);
461 nvdimm_spa_fini(spa_mapping);
465 nvdimm_create_namespaces(spa_mapping, nfitbl);
466 SLIST_INSERT_HEAD(&dev->spas, spa_mapping, link);
468 free(spas, M_NVDIMM);
472 static char *nvdimm_root_id[] = {"ACPI0012", NULL};
475 nvdimm_root_probe(device_t dev)
479 if (acpi_disabled("nvdimm"))
481 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL);
483 device_set_desc(dev, "ACPI NVDIMM root device");
489 nvdimm_root_attach(device_t dev)
491 struct nvdimm_root_dev *root;
492 ACPI_TABLE_NFIT *nfitbl;
496 status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
497 if (ACPI_FAILURE(status)) {
498 device_printf(dev, "cannot get NFIT\n");
501 error = nvdimm_root_create_devs(dev, nfitbl);
504 error = bus_generic_attach(dev);
507 root = device_get_softc(dev);
508 error = nvdimm_root_create_spas(root, nfitbl);
509 AcpiPutTable(&nfitbl->Header);
514 nvdimm_root_detach(device_t dev)
516 struct nvdimm_root_dev *root;
517 struct SPA_mapping *spa, *next;
519 int i, error, num_children;
521 root = device_get_softc(dev);
522 SLIST_FOREACH_SAFE(spa, &root->spas, link, next) {
523 nvdimm_destroy_namespaces(spa);
524 nvdimm_spa_fini(spa);
525 SLIST_REMOVE_HEAD(&root->spas, link);
528 error = bus_generic_detach(dev);
531 error = device_get_children(dev, &children, &num_children);
534 for (i = 0; i < num_children; i++)
535 free(device_get_ivars(children[i]), M_NVDIMM);
536 free(children, M_TEMP);
537 error = device_delete_children(dev);
542 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
546 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
548 *result = ((uintptr_t *)device_get_ivars(child))[index];
553 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
557 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
559 ((uintptr_t *)device_get_ivars(child))[index] = value;
564 nvdimm_root_child_location_str(device_t dev, device_t child, char *buf,
570 handle = nvdimm_root_get_acpi_handle(child);
572 res = snprintf(buf, buflen, "handle=%s", acpi_name(handle));
574 res = snprintf(buf, buflen, "");
581 static device_method_t nvdimm_methods[] = {
582 DEVMETHOD(device_probe, nvdimm_probe),
583 DEVMETHOD(device_attach, nvdimm_attach),
584 DEVMETHOD(device_detach, nvdimm_detach),
585 DEVMETHOD(device_suspend, nvdimm_suspend),
586 DEVMETHOD(device_resume, nvdimm_resume),
590 static driver_t nvdimm_driver = {
593 sizeof(struct nvdimm_dev),
596 static device_method_t nvdimm_root_methods[] = {
597 DEVMETHOD(device_probe, nvdimm_root_probe),
598 DEVMETHOD(device_attach, nvdimm_root_attach),
599 DEVMETHOD(device_detach, nvdimm_root_detach),
600 DEVMETHOD(bus_add_child, bus_generic_add_child),
601 DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar),
602 DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar),
603 DEVMETHOD(bus_child_location_str, nvdimm_root_child_location_str),
607 static driver_t nvdimm_root_driver = {
610 sizeof(struct nvdimm_root_dev),
613 DRIVER_MODULE(nvdimm_root, acpi, nvdimm_root_driver, nvdimm_root_devclass, NULL,
615 DRIVER_MODULE(nvdimm, nvdimm_root, nvdimm_driver, nvdimm_devclass, NULL, NULL);
616 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);