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>
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>
51 #include <dev/nvdimm/nvdimm_var.h>
53 #define _COMPONENT ACPI_OEM
54 ACPI_MODULE_NAME("NVDIMM_ACPI")
56 struct nvdimm_root_dev {
57 SLIST_HEAD(, SPA_mapping) spas;
60 static MALLOC_DEFINE(M_NVDIMM_ACPI, "nvdimm_acpi", "NVDIMM ACPI bus memory");
63 find_dimm(ACPI_HANDLE handle, UINT32 nesting_level, void *context,
66 ACPI_DEVICE_INFO *device_info;
69 status = AcpiGetObjectInfo(handle, &device_info);
70 if (ACPI_FAILURE(status))
71 return_ACPI_STATUS(AE_ERROR);
72 if (device_info->Address == (uintptr_t)context) {
73 *(ACPI_HANDLE *)return_value = handle;
74 return_ACPI_STATUS(AE_CTRL_TERMINATE);
76 return_ACPI_STATUS(AE_OK);
80 get_dimm_acpi_handle(ACPI_HANDLE root_handle, nfit_handle_t adr)
86 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, root_handle, 1, find_dimm,
87 NULL, (void *)(uintptr_t)adr, &res);
88 if (ACPI_FAILURE(status))
94 nvdimm_root_create_devs(device_t dev, ACPI_TABLE_NFIT *nfitbl)
96 ACPI_HANDLE root_handle, dimm_handle;
98 nfit_handle_t *dimm_ids, *dimm;
102 root_handle = acpi_get_handle(dev);
103 acpi_nfit_get_dimm_ids(nfitbl, &dimm_ids, &num_dimm_ids);
104 for (dimm = dimm_ids; dimm < dimm_ids + num_dimm_ids; dimm++) {
105 dimm_handle = get_dimm_acpi_handle(root_handle, *dimm);
106 if (dimm_handle == NULL)
109 child = BUS_ADD_CHILD(dev, 100, "nvdimm", -1);
111 device_printf(dev, "failed to create nvdimm\n");
114 ivars = mallocarray(NVDIMM_ROOT_IVAR_MAX, sizeof(uintptr_t),
115 M_NVDIMM_ACPI, M_ZERO | M_WAITOK);
116 device_set_ivars(child, ivars);
117 nvdimm_root_set_acpi_handle(child, dimm_handle);
118 nvdimm_root_set_device_handle(child, *dimm);
120 free(dimm_ids, M_NVDIMM_ACPI);
125 nvdimm_root_create_spas(struct nvdimm_root_dev *dev, ACPI_TABLE_NFIT *nfitbl)
127 ACPI_NFIT_SYSTEM_ADDRESS **spas, **spa;
128 struct SPA_mapping *spa_mapping;
129 enum SPA_mapping_type spa_type;
133 acpi_nfit_get_spa_ranges(nfitbl, &spas, &num_spas);
134 for (spa = spas; spa < spas + num_spas; spa++) {
135 spa_type = nvdimm_spa_type_from_uuid(
136 (struct uuid *)(*spa)->RangeGuid);
137 if (spa_type == SPA_TYPE_UNKNOWN)
139 spa_mapping = malloc(sizeof(struct SPA_mapping), M_NVDIMM_ACPI,
141 error = nvdimm_spa_init(spa_mapping, *spa, spa_type);
143 nvdimm_spa_fini(spa_mapping);
144 free(spa_mapping, M_NVDIMM_ACPI);
147 if (nvdimm_spa_type_user_accessible(spa_type) &&
148 spa_type != SPA_TYPE_CONTROL_REGION)
149 nvdimm_create_namespaces(spa_mapping, nfitbl);
150 SLIST_INSERT_HEAD(&dev->spas, spa_mapping, link);
152 free(spas, M_NVDIMM_ACPI);
156 static char *nvdimm_root_id[] = {"ACPI0012", NULL};
159 nvdimm_root_probe(device_t dev)
163 if (acpi_disabled("nvdimm"))
165 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL);
167 device_set_desc(dev, "ACPI NVDIMM root device");
173 nvdimm_root_attach(device_t dev)
175 struct nvdimm_root_dev *root;
176 ACPI_TABLE_NFIT *nfitbl;
180 status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
181 if (ACPI_FAILURE(status)) {
182 device_printf(dev, "cannot get NFIT\n");
185 error = nvdimm_root_create_devs(dev, nfitbl);
188 error = bus_generic_attach(dev);
191 root = device_get_softc(dev);
192 error = nvdimm_root_create_spas(root, nfitbl);
193 AcpiPutTable(&nfitbl->Header);
198 nvdimm_root_detach(device_t dev)
200 struct nvdimm_root_dev *root;
201 struct SPA_mapping *spa, *next;
203 int i, error, num_children;
205 root = device_get_softc(dev);
206 SLIST_FOREACH_SAFE(spa, &root->spas, link, next) {
207 nvdimm_destroy_namespaces(spa);
208 nvdimm_spa_fini(spa);
209 SLIST_REMOVE_HEAD(&root->spas, link);
210 free(spa, M_NVDIMM_ACPI);
212 error = bus_generic_detach(dev);
215 error = device_get_children(dev, &children, &num_children);
218 for (i = 0; i < num_children; i++)
219 free(device_get_ivars(children[i]), M_NVDIMM_ACPI);
220 free(children, M_TEMP);
221 error = device_delete_children(dev);
226 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
230 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
232 *result = ((uintptr_t *)device_get_ivars(child))[index];
237 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
241 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
243 ((uintptr_t *)device_get_ivars(child))[index] = value;
248 nvdimm_root_child_location_str(device_t dev, device_t child, char *buf,
254 handle = nvdimm_root_get_acpi_handle(child);
256 res = snprintf(buf, buflen, "handle=%s", acpi_name(handle));
258 res = snprintf(buf, buflen, "");
265 static device_method_t nvdimm_acpi_methods[] = {
266 DEVMETHOD(device_probe, nvdimm_root_probe),
267 DEVMETHOD(device_attach, nvdimm_root_attach),
268 DEVMETHOD(device_detach, nvdimm_root_detach),
269 DEVMETHOD(bus_add_child, bus_generic_add_child),
270 DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar),
271 DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar),
272 DEVMETHOD(bus_child_location_str, nvdimm_root_child_location_str),
276 static driver_t nvdimm_acpi_driver = {
279 sizeof(struct nvdimm_root_dev),
282 static devclass_t nvdimm_acpi_root_devclass;
283 DRIVER_MODULE(nvdimm_acpi_root, acpi, nvdimm_acpi_driver,
284 nvdimm_acpi_root_devclass, NULL, NULL);
285 MODULE_DEPEND(nvdimm_acpi_root, acpi, 1, 1, 1);