2 * Copyright (c) 2017 The FreeBSD Foundation
3 * Copyright (c) 2018, 2019 Intel Corporation
5 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/bitstring.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acuuid.h>
50 #include <dev/acpica/acpivar.h>
52 #include <dev/nvdimm/nvdimm_var.h>
54 #define _COMPONENT ACPI_OEM
55 ACPI_MODULE_NAME("NVDIMM_ACPI")
57 struct nvdimm_root_dev {
58 SLIST_HEAD(, SPA_mapping) spas;
61 static MALLOC_DEFINE(M_NVDIMM_ACPI, "nvdimm_acpi", "NVDIMM ACPI bus memory");
64 find_dimm(ACPI_HANDLE handle, UINT32 nesting_level, void *context,
67 ACPI_DEVICE_INFO *device_info;
70 status = AcpiGetObjectInfo(handle, &device_info);
71 if (ACPI_FAILURE(status))
72 return_ACPI_STATUS(AE_ERROR);
73 if (device_info->Address == (uintptr_t)context) {
74 *(ACPI_HANDLE *)return_value = handle;
75 return_ACPI_STATUS(AE_CTRL_TERMINATE);
77 return_ACPI_STATUS(AE_OK);
81 get_dimm_acpi_handle(ACPI_HANDLE root_handle, nfit_handle_t adr)
87 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, root_handle, 1, find_dimm,
88 NULL, (void *)(uintptr_t)adr, &res);
89 if (ACPI_FAILURE(status))
95 nvdimm_root_create_devs(device_t dev, ACPI_TABLE_NFIT *nfitbl)
97 ACPI_HANDLE root_handle, dimm_handle;
99 nfit_handle_t *dimm_ids, *dimm;
103 root_handle = acpi_get_handle(dev);
104 acpi_nfit_get_dimm_ids(nfitbl, &dimm_ids, &num_dimm_ids);
105 for (dimm = dimm_ids; dimm < dimm_ids + num_dimm_ids; dimm++) {
106 dimm_handle = get_dimm_acpi_handle(root_handle, *dimm);
107 if (dimm_handle == NULL)
110 child = BUS_ADD_CHILD(dev, 100, "nvdimm", -1);
112 device_printf(dev, "failed to create nvdimm\n");
115 ivars = mallocarray(NVDIMM_ROOT_IVAR_MAX, sizeof(uintptr_t),
116 M_NVDIMM_ACPI, M_ZERO | M_WAITOK);
117 device_set_ivars(child, ivars);
118 nvdimm_root_set_acpi_handle(child, dimm_handle);
119 nvdimm_root_set_device_handle(child, *dimm);
121 free(dimm_ids, M_NVDIMM_ACPI);
126 nvdimm_root_create_spas(struct nvdimm_root_dev *dev, ACPI_TABLE_NFIT *nfitbl)
128 ACPI_NFIT_SYSTEM_ADDRESS **spas, **spa;
129 struct SPA_mapping *spa_mapping;
130 enum SPA_mapping_type spa_type;
134 acpi_nfit_get_spa_ranges(nfitbl, &spas, &num_spas);
135 for (spa = spas; spa < spas + num_spas; spa++) {
136 spa_type = nvdimm_spa_type_from_uuid(
137 (struct uuid *)(*spa)->RangeGuid);
138 if (spa_type == SPA_TYPE_UNKNOWN)
140 spa_mapping = malloc(sizeof(struct SPA_mapping), M_NVDIMM_ACPI,
142 error = nvdimm_spa_init(spa_mapping, *spa, spa_type);
144 nvdimm_spa_fini(spa_mapping);
145 free(spa_mapping, M_NVDIMM_ACPI);
148 if (nvdimm_spa_type_user_accessible(spa_type) &&
149 spa_type != SPA_TYPE_CONTROL_REGION)
150 nvdimm_create_namespaces(spa_mapping, nfitbl);
151 SLIST_INSERT_HEAD(&dev->spas, spa_mapping, link);
153 free(spas, M_NVDIMM_ACPI);
157 static char *nvdimm_root_id[] = {"ACPI0012", NULL};
160 nvdimm_root_probe(device_t dev)
164 if (acpi_disabled("nvdimm"))
166 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL);
168 device_set_desc(dev, "ACPI NVDIMM root device");
174 nvdimm_root_attach(device_t dev)
176 struct nvdimm_root_dev *root;
177 ACPI_TABLE_NFIT *nfitbl;
181 status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
182 if (ACPI_FAILURE(status)) {
183 device_printf(dev, "cannot get NFIT\n");
186 error = nvdimm_root_create_devs(dev, nfitbl);
189 error = bus_generic_attach(dev);
192 root = device_get_softc(dev);
193 error = nvdimm_root_create_spas(root, nfitbl);
194 AcpiPutTable(&nfitbl->Header);
199 nvdimm_root_detach(device_t dev)
201 struct nvdimm_root_dev *root;
202 struct SPA_mapping *spa, *next;
204 int i, error, num_children;
206 root = device_get_softc(dev);
207 SLIST_FOREACH_SAFE(spa, &root->spas, link, next) {
208 nvdimm_destroy_namespaces(spa);
209 nvdimm_spa_fini(spa);
210 SLIST_REMOVE_HEAD(&root->spas, link);
211 free(spa, M_NVDIMM_ACPI);
213 error = bus_generic_detach(dev);
216 error = device_get_children(dev, &children, &num_children);
219 for (i = 0; i < num_children; i++)
220 free(device_get_ivars(children[i]), M_NVDIMM_ACPI);
221 free(children, M_TEMP);
222 error = device_delete_children(dev);
227 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
231 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
233 *result = ((uintptr_t *)device_get_ivars(child))[index];
238 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
242 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
244 ((uintptr_t *)device_get_ivars(child))[index] = value;
249 nvdimm_root_child_location(device_t dev, device_t child, struct sbuf *sb)
253 handle = nvdimm_root_get_acpi_handle(child);
255 sbuf_printf(sb, "handle=%s", acpi_name(handle));
260 static device_method_t nvdimm_acpi_methods[] = {
261 DEVMETHOD(device_probe, nvdimm_root_probe),
262 DEVMETHOD(device_attach, nvdimm_root_attach),
263 DEVMETHOD(device_detach, nvdimm_root_detach),
264 DEVMETHOD(bus_add_child, bus_generic_add_child),
265 DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar),
266 DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar),
267 DEVMETHOD(bus_child_location, nvdimm_root_child_location),
268 DEVMETHOD(bus_get_device_path, acpi_get_acpi_device_path),
272 static driver_t nvdimm_acpi_driver = {
275 sizeof(struct nvdimm_root_dev),
278 DRIVER_MODULE(nvdimm_acpi_root, acpi, nvdimm_acpi_driver, NULL, NULL);
279 MODULE_DEPEND(nvdimm_acpi_root, acpi, 1, 1, 1);