xref: /freebsd/sys/dev/nvdimm/nvdimm.c (revision f0483545503a78e16e256d46d458a2faae2f07ea)
1 /*-
2  * Copyright (c) 2017 The FreeBSD Foundation
3  * All rights reserved.
4  * Copyright (c) 2018, 2019 Intel Corporation
5  *
6  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_acpi.h"
35 #include "opt_ddb.h"
36 
37 #include <sys/param.h>
38 #include <sys/bio.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/uuid.h>
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/acuuid.h>
48 #include <dev/acpica/acpivar.h>
49 #include <dev/nvdimm/nvdimm_var.h>
50 
51 #define _COMPONENT	ACPI_OEM
52 ACPI_MODULE_NAME("NVDIMM")
53 
54 static devclass_t nvdimm_devclass;
55 static devclass_t nvdimm_root_devclass;
56 MALLOC_DEFINE(M_NVDIMM, "nvdimm", "NVDIMM driver memory");
57 
58 struct nvdimm_dev *
59 nvdimm_find_by_handle(nfit_handle_t nv_handle)
60 {
61 	struct nvdimm_dev *res;
62 	device_t *dimms;
63 	int i, error, num_dimms;
64 
65 	res = NULL;
66 	error = devclass_get_devices(nvdimm_devclass, &dimms, &num_dimms);
67 	if (error != 0)
68 		return (NULL);
69 	for (i = 0; i < num_dimms; i++) {
70 		if (nvdimm_root_get_device_handle(dimms[i]) == nv_handle) {
71 			res = device_get_softc(dimms[i]);
72 			break;
73 		}
74 	}
75 	free(dimms, M_TEMP);
76 	return (res);
77 }
78 
79 static int
80 nvdimm_probe(device_t dev)
81 {
82 
83 	return (BUS_PROBE_NOWILDCARD);
84 }
85 
86 static int
87 nvdimm_attach(device_t dev)
88 {
89 	struct nvdimm_dev *nv;
90 	ACPI_TABLE_NFIT *nfitbl;
91 	ACPI_HANDLE handle;
92 	ACPI_STATUS status;
93 
94 	nv = device_get_softc(dev);
95 	handle = nvdimm_root_get_acpi_handle(dev);
96 	if (handle == NULL)
97 		return (EINVAL);
98 	nv->nv_dev = dev;
99 	nv->nv_handle = nvdimm_root_get_device_handle(dev);
100 
101 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
102 	if (ACPI_FAILURE(status)) {
103 		if (bootverbose)
104 			device_printf(dev, "cannot get NFIT\n");
105 		return (ENXIO);
106 	}
107 	acpi_nfit_get_flush_addrs(nfitbl, nv->nv_handle, &nv->nv_flush_addr,
108 	    &nv->nv_flush_addr_cnt);
109 	AcpiPutTable(&nfitbl->Header);
110 	return (0);
111 }
112 
113 static int
114 nvdimm_detach(device_t dev)
115 {
116 	struct nvdimm_dev *nv;
117 
118 	nv = device_get_softc(dev);
119 	free(nv->nv_flush_addr, M_NVDIMM);
120 	return (0);
121 }
122 
123 static int
124 nvdimm_suspend(device_t dev)
125 {
126 
127 	return (0);
128 }
129 
130 static int
131 nvdimm_resume(device_t dev)
132 {
133 
134 	return (0);
135 }
136 
137 static ACPI_STATUS
138 find_dimm(ACPI_HANDLE handle, UINT32 nesting_level, void *context,
139     void **return_value)
140 {
141 	ACPI_DEVICE_INFO *device_info;
142 	ACPI_STATUS status;
143 
144 	status = AcpiGetObjectInfo(handle, &device_info);
145 	if (ACPI_FAILURE(status))
146 		return_ACPI_STATUS(AE_ERROR);
147 	if (device_info->Address == (uintptr_t)context) {
148 		*(ACPI_HANDLE *)return_value = handle;
149 		return_ACPI_STATUS(AE_CTRL_TERMINATE);
150 	}
151 	return_ACPI_STATUS(AE_OK);
152 }
153 
154 static ACPI_HANDLE
155 get_dimm_acpi_handle(ACPI_HANDLE root_handle, nfit_handle_t adr)
156 {
157 	ACPI_HANDLE res;
158 	ACPI_STATUS status;
159 
160 	res = NULL;
161 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, root_handle, 1, find_dimm,
162 	    NULL, (void *)(uintptr_t)adr, &res);
163 	if (ACPI_FAILURE(status))
164 		res = NULL;
165 	return (res);
166 }
167 
168 static int
169 nvdimm_root_create_devs(device_t dev, ACPI_TABLE_NFIT *nfitbl)
170 {
171 	ACPI_HANDLE root_handle, dimm_handle;
172 	device_t child;
173 	nfit_handle_t *dimm_ids, *dimm;
174 	uintptr_t *ivars;
175 	int num_dimm_ids;
176 
177 	root_handle = acpi_get_handle(dev);
178 	acpi_nfit_get_dimm_ids(nfitbl, &dimm_ids, &num_dimm_ids);
179 	for (dimm = dimm_ids; dimm < dimm_ids + num_dimm_ids; dimm++) {
180 		dimm_handle = get_dimm_acpi_handle(root_handle, *dimm);
181 		child = BUS_ADD_CHILD(dev, 100, "nvdimm", -1);
182 		if (child == NULL) {
183 			device_printf(dev, "failed to create nvdimm\n");
184 			return (ENXIO);
185 		}
186 		ivars = mallocarray(NVDIMM_ROOT_IVAR_MAX, sizeof(uintptr_t),
187 		    M_NVDIMM, M_ZERO | M_WAITOK);
188 		device_set_ivars(child, ivars);
189 		nvdimm_root_set_acpi_handle(child, dimm_handle);
190 		nvdimm_root_set_device_handle(child, *dimm);
191 	}
192 	free(dimm_ids, M_NVDIMM);
193 	return (0);
194 }
195 
196 static int
197 nvdimm_root_create_spas(struct nvdimm_root_dev *dev, ACPI_TABLE_NFIT *nfitbl)
198 {
199 	ACPI_NFIT_SYSTEM_ADDRESS **spas, **spa;
200 	struct SPA_mapping *spa_mapping;
201 	enum SPA_mapping_type spa_type;
202 	int error, num_spas;
203 
204 	error = 0;
205 	acpi_nfit_get_spa_ranges(nfitbl, &spas, &num_spas);
206 	for (spa = spas; spa < spas + num_spas; spa++) {
207 		spa_type = nvdimm_spa_type_from_uuid(
208 			(struct uuid *)(*spa)->RangeGuid);
209 		if (spa_type == SPA_TYPE_UNKNOWN)
210 			continue;
211 		spa_mapping = malloc(sizeof(struct SPA_mapping), M_NVDIMM,
212 		    M_WAITOK | M_ZERO);
213 		error = nvdimm_spa_init(spa_mapping, *spa, spa_type);
214 		if (error != 0) {
215 			nvdimm_spa_fini(spa_mapping);
216 			free(spa, M_NVDIMM);
217 			break;
218 		}
219 		SLIST_INSERT_HEAD(&dev->spas, spa_mapping, link);
220 	}
221 	free(spas, M_NVDIMM);
222 	return (error);
223 }
224 
225 static char *nvdimm_root_id[] = {"ACPI0012", NULL};
226 
227 static int
228 nvdimm_root_probe(device_t dev)
229 {
230 	int rv;
231 
232 	if (acpi_disabled("nvdimm"))
233 		return (ENXIO);
234 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL);
235 	if (rv <= 0)
236 		device_set_desc(dev, "ACPI NVDIMM root device");
237 
238 	return (rv);
239 }
240 
241 static int
242 nvdimm_root_attach(device_t dev)
243 {
244 	struct nvdimm_root_dev *root;
245 	ACPI_TABLE_NFIT *nfitbl;
246 	ACPI_STATUS status;
247 	int error;
248 
249 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
250 	if (ACPI_FAILURE(status)) {
251 		device_printf(dev, "cannot get NFIT\n");
252 		return (ENXIO);
253 	}
254 	error = nvdimm_root_create_devs(dev, nfitbl);
255 	if (error != 0)
256 		return (error);
257 	error = bus_generic_attach(dev);
258 	if (error != 0)
259 		return (error);
260 	root = device_get_softc(dev);
261 	error = nvdimm_root_create_spas(root, nfitbl);
262 	AcpiPutTable(&nfitbl->Header);
263 	return (error);
264 }
265 
266 static int
267 nvdimm_root_detach(device_t dev)
268 {
269 	struct nvdimm_root_dev *root;
270 	struct SPA_mapping *spa, *next;
271 	device_t *children;
272 	int i, error, num_children;
273 
274 	root = device_get_softc(dev);
275 	SLIST_FOREACH_SAFE(spa, &root->spas, link, next) {
276 		nvdimm_spa_fini(spa);
277 		SLIST_REMOVE_HEAD(&root->spas, link);
278 		free(spa, M_NVDIMM);
279 	}
280 	error = bus_generic_detach(dev);
281 	if (error != 0)
282 		return (error);
283 	error = device_get_children(dev, &children, &num_children);
284 	if (error != 0)
285 		return (error);
286 	for (i = 0; i < num_children; i++)
287 		free(device_get_ivars(children[i]), M_NVDIMM);
288 	free(children, M_TEMP);
289 	error = device_delete_children(dev);
290 	return (error);
291 }
292 
293 static int
294 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
295     uintptr_t *result)
296 {
297 
298 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
299 		return (ENOENT);
300 	*result = ((uintptr_t *)device_get_ivars(child))[index];
301 	return (0);
302 }
303 
304 static int
305 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
306     uintptr_t value)
307 {
308 
309 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
310 		return (ENOENT);
311 	((uintptr_t *)device_get_ivars(child))[index] = value;
312 	return (0);
313 }
314 
315 static device_method_t nvdimm_methods[] = {
316 	DEVMETHOD(device_probe, nvdimm_probe),
317 	DEVMETHOD(device_attach, nvdimm_attach),
318 	DEVMETHOD(device_detach, nvdimm_detach),
319 	DEVMETHOD(device_suspend, nvdimm_suspend),
320 	DEVMETHOD(device_resume, nvdimm_resume),
321 	DEVMETHOD_END
322 };
323 
324 static driver_t	nvdimm_driver = {
325 	"nvdimm",
326 	nvdimm_methods,
327 	sizeof(struct nvdimm_dev),
328 };
329 
330 static device_method_t nvdimm_root_methods[] = {
331 	DEVMETHOD(device_probe, nvdimm_root_probe),
332 	DEVMETHOD(device_attach, nvdimm_root_attach),
333 	DEVMETHOD(device_detach, nvdimm_root_detach),
334 	DEVMETHOD(bus_add_child, bus_generic_add_child),
335 	DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar),
336 	DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar),
337 	DEVMETHOD_END
338 };
339 
340 static driver_t	nvdimm_root_driver = {
341 	"nvdimm_root",
342 	nvdimm_root_methods,
343 	sizeof(struct nvdimm_root_dev),
344 };
345 
346 DRIVER_MODULE(nvdimm_root, acpi, nvdimm_root_driver, nvdimm_root_devclass, NULL,
347     NULL);
348 DRIVER_MODULE(nvdimm, nvdimm_root, nvdimm_driver, nvdimm_devclass, NULL, NULL);
349 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);
350