xref: /freebsd/sys/dev/nvdimm/nvdimm.c (revision 7815283df299be63807225a9fe9b6e54406eae28)
1 /*-
2  * Copyright (c) 2017 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6  * under sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_acpi.h"
34 #include "opt_ddb.h"
35 
36 #include <sys/param.h>
37 #include <sys/bio.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/uuid.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acuuid.h>
47 #include <dev/acpica/acpivar.h>
48 #include <dev/nvdimm/nvdimm_var.h>
49 
50 #define _COMPONENT	ACPI_OEM
51 ACPI_MODULE_NAME("NVDIMM")
52 
53 static devclass_t nvdimm_devclass;
54 static device_t *nvdimm_devs;
55 static int nvdimm_devcnt;
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 	device_t dev;
62 	struct nvdimm_dev *res, *nv;
63 	int i;
64 
65 	res = NULL;
66 	for (i = 0; i < nvdimm_devcnt; i++) {
67 		dev = nvdimm_devs[i];
68 		if (dev == NULL)
69 			continue;
70 		nv = device_get_softc(dev);
71 		if (nv->nv_handle == nv_handle) {
72 			res = nv;
73 			break;
74 		}
75 	}
76 	return (res);
77 }
78 
79 static int
80 nvdimm_parse_flush_addr(void *nfitsubtbl, void *arg)
81 {
82 	ACPI_NFIT_FLUSH_ADDRESS *nfitflshaddr;
83 	struct nvdimm_dev *nv;
84 	int i;
85 
86 	nfitflshaddr = nfitsubtbl;
87 	nv = arg;
88 	if (nfitflshaddr->DeviceHandle != nv->nv_handle)
89 		return (0);
90 
91 	MPASS(nv->nv_flush_addr == NULL && nv->nv_flush_addr_cnt == 0);
92 	nv->nv_flush_addr = malloc(nfitflshaddr->HintCount * sizeof(uint64_t *),
93 	    M_NVDIMM, M_WAITOK);
94 	for (i = 0; i < nfitflshaddr->HintCount; i++)
95 		nv->nv_flush_addr[i] = (uint64_t *)nfitflshaddr->HintAddress[i];
96 	nv->nv_flush_addr_cnt = nfitflshaddr->HintCount;
97 	return (0);
98 }
99 
100 int
101 nvdimm_iterate_nfit(ACPI_TABLE_NFIT *nfitbl, enum AcpiNfitType type,
102     int (*cb)(void *, void *), void *arg)
103 {
104 	ACPI_NFIT_HEADER *nfithdr;
105 	ACPI_NFIT_SYSTEM_ADDRESS *nfitaddr;
106 	ACPI_NFIT_MEMORY_MAP *nfitmap;
107 	ACPI_NFIT_INTERLEAVE *nfitintrl;
108 	ACPI_NFIT_SMBIOS *nfitsmbios;
109 	ACPI_NFIT_CONTROL_REGION *nfitctlreg;
110 	ACPI_NFIT_DATA_REGION *nfitdtreg;
111 	ACPI_NFIT_FLUSH_ADDRESS *nfitflshaddr;
112 	char *ptr;
113 	int error;
114 
115 	error = 0;
116 	for (ptr = (char *)(nfitbl + 1);
117 	    ptr < (char *)nfitbl + nfitbl->Header.Length;
118 	    ptr += nfithdr->Length) {
119 		nfithdr = (ACPI_NFIT_HEADER *)ptr;
120 		if (nfithdr->Type != type)
121 			continue;
122 		switch (nfithdr->Type) {
123 		case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
124 			nfitaddr = __containerof(nfithdr,
125 			    ACPI_NFIT_SYSTEM_ADDRESS, Header);
126 			error = cb(nfitaddr, arg);
127 			break;
128 		case ACPI_NFIT_TYPE_MEMORY_MAP:
129 			nfitmap = __containerof(nfithdr,
130 			    ACPI_NFIT_MEMORY_MAP, Header);
131 			error = cb(nfitmap, arg);
132 			break;
133 		case ACPI_NFIT_TYPE_INTERLEAVE:
134 			nfitintrl = __containerof(nfithdr,
135 			    ACPI_NFIT_INTERLEAVE, Header);
136 			error = cb(nfitintrl, arg);
137 			break;
138 		case ACPI_NFIT_TYPE_SMBIOS:
139 			nfitsmbios = __containerof(nfithdr,
140 			    ACPI_NFIT_SMBIOS, Header);
141 			error = cb(nfitsmbios, arg);
142 			break;
143 		case ACPI_NFIT_TYPE_CONTROL_REGION:
144 			nfitctlreg = __containerof(nfithdr,
145 			    ACPI_NFIT_CONTROL_REGION, Header);
146 			error = cb(nfitctlreg, arg);
147 			break;
148 		case ACPI_NFIT_TYPE_DATA_REGION:
149 			nfitdtreg = __containerof(nfithdr,
150 			    ACPI_NFIT_DATA_REGION, Header);
151 			error = cb(nfitdtreg, arg);
152 			break;
153 		case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
154 			nfitflshaddr = __containerof(nfithdr,
155 			    ACPI_NFIT_FLUSH_ADDRESS, Header);
156 			error = cb(nfitflshaddr, arg);
157 			break;
158 		case ACPI_NFIT_TYPE_RESERVED:
159 		default:
160 			if (bootverbose)
161 				printf("NFIT subtype %d unknown\n",
162 				    nfithdr->Type);
163 			error = 0;
164 			break;
165 		}
166 		if (error != 0)
167 			break;
168 	}
169 	return (error);
170 }
171 
172 static ACPI_STATUS
173 nvdimm_walk_dev(ACPI_HANDLE handle, UINT32 level, void *ctx, void **st)
174 {
175 	ACPI_STATUS status;
176 	struct nvdimm_ns_walk_ctx *wctx;
177 
178 	wctx = ctx;
179 	status = wctx->func(handle, wctx->arg);
180 	return_ACPI_STATUS(status);
181 }
182 
183 static ACPI_STATUS
184 nvdimm_walk_root(ACPI_HANDLE handle, UINT32 level, void *ctx, void **st)
185 {
186 	ACPI_STATUS status;
187 
188 	if (!acpi_MatchHid(handle, "ACPI0012"))
189 		return_ACPI_STATUS(AE_OK);
190 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 100,
191 	    nvdimm_walk_dev, NULL, ctx, NULL);
192 	if (ACPI_FAILURE(status))
193 		return_ACPI_STATUS(status);
194 	return_ACPI_STATUS(AE_CTRL_TERMINATE);
195 }
196 
197 static ACPI_STATUS
198 nvdimm_foreach_acpi(ACPI_STATUS (*func)(ACPI_HANDLE, void *), void *arg)
199 {
200 	struct nvdimm_ns_walk_ctx wctx;
201 	ACPI_STATUS status;
202 
203 	wctx.func = func;
204 	wctx.arg = arg;
205 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 100,
206 	    nvdimm_walk_root, NULL, &wctx, NULL);
207 	return_ACPI_STATUS(status);
208 }
209 
210 static ACPI_STATUS
211 nvdimm_count_devs(ACPI_HANDLE handle __unused, void *arg)
212 {
213 	int *cnt;
214 
215 	cnt = arg;
216 	(*cnt)++;
217 
218 	ACPI_BUFFER name;
219 	ACPI_STATUS status;
220 	if (bootverbose) {
221 		name.Length = ACPI_ALLOCATE_BUFFER;
222 		status = AcpiGetName(handle, ACPI_FULL_PATHNAME, &name);
223 		if (ACPI_FAILURE(status))
224 			return_ACPI_STATUS(status);
225 		printf("nvdimm: enumerated %s\n", (char *)name.Pointer);
226 		AcpiOsFree(name.Pointer);
227 	}
228 
229 	return_ACPI_STATUS(AE_OK);
230 }
231 
232 struct nvdimm_create_dev_arg {
233 	device_t acpi0;
234 	int *cnt;
235 };
236 
237 static ACPI_STATUS
238 nvdimm_create_dev(ACPI_HANDLE handle, void *arg)
239 {
240 	struct nvdimm_create_dev_arg *narg;
241 	device_t child;
242 	int idx;
243 
244 	narg = arg;
245 	idx = *(narg->cnt);
246 	child = device_find_child(narg->acpi0, "nvdimm", idx);
247 	if (child == NULL)
248 		child = BUS_ADD_CHILD(narg->acpi0, 1, "nvdimm", idx);
249 	if (child == NULL) {
250 		if (bootverbose)
251 			device_printf(narg->acpi0,
252 			    "failed to create nvdimm%d\n", idx);
253 		return_ACPI_STATUS(AE_ERROR);
254 	}
255 	acpi_set_handle(child, handle);
256 	KASSERT(nvdimm_devs[idx] == NULL, ("nvdimm_devs[%d] not NULL", idx));
257 	nvdimm_devs[idx] = child;
258 
259 	(*(narg->cnt))++;
260 	return_ACPI_STATUS(AE_OK);
261 }
262 
263 static bool
264 nvdimm_init(void)
265 {
266 	ACPI_STATUS status;
267 
268 	if (nvdimm_devcnt != 0)
269 		return (true);
270 	if (acpi_disabled("nvdimm"))
271 		return (false);
272 	status = nvdimm_foreach_acpi(nvdimm_count_devs, &nvdimm_devcnt);
273 	if (ACPI_FAILURE(status)) {
274 		if (bootverbose)
275 			printf("nvdimm_init: count failed\n");
276 		return (false);
277 	}
278 	nvdimm_devs = malloc(nvdimm_devcnt * sizeof(device_t), M_NVDIMM,
279 	    M_WAITOK | M_ZERO);
280 	return (true);
281 }
282 
283 static void
284 nvdimm_identify(driver_t *driver, device_t parent)
285 {
286 	struct nvdimm_create_dev_arg narg;
287 	ACPI_STATUS status;
288 	int i;
289 
290 	if (!nvdimm_init())
291 		return;
292 	narg.acpi0 = parent;
293 	narg.cnt = &i;
294 	i = 0;
295 	status = nvdimm_foreach_acpi(nvdimm_create_dev, &narg);
296 	if (ACPI_FAILURE(status) && bootverbose)
297 		printf("nvdimm_identify: create failed\n");
298 }
299 
300 static int
301 nvdimm_probe(device_t dev)
302 {
303 
304 	return (BUS_PROBE_NOWILDCARD);
305 }
306 
307 static int
308 nvdimm_attach(device_t dev)
309 {
310 	struct nvdimm_dev *nv;
311 	ACPI_TABLE_NFIT *nfitbl;
312 	ACPI_HANDLE handle;
313 	ACPI_STATUS status;
314 	int i;
315 
316 	nv = device_get_softc(dev);
317 	handle = acpi_get_handle(dev);
318 	if (handle == NULL)
319 		return (EINVAL);
320 	nv->nv_dev = dev;
321 	for (i = 0; i < nvdimm_devcnt; i++) {
322 		if (nvdimm_devs[i] == dev) {
323 			nv->nv_devs_idx = i;
324 			break;
325 		}
326 	}
327 	MPASS(i < nvdimm_devcnt);
328 	if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &nv->nv_handle))) {
329 		device_printf(dev, "cannot get handle\n");
330 		return (ENXIO);
331 	}
332 
333 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
334 	if (ACPI_FAILURE(status)) {
335 		if (bootverbose)
336 			device_printf(dev, "cannot get NFIT\n");
337 		return (ENXIO);
338 	}
339 	nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_FLUSH_ADDRESS,
340 	    nvdimm_parse_flush_addr, nv);
341 	AcpiPutTable(&nfitbl->Header);
342 	return (0);
343 }
344 
345 static int
346 nvdimm_detach(device_t dev)
347 {
348 	struct nvdimm_dev *nv;
349 
350 	nv = device_get_softc(dev);
351 	nvdimm_devs[nv->nv_devs_idx] = NULL;
352 	free(nv->nv_flush_addr, M_NVDIMM);
353 	return (0);
354 }
355 
356 static int
357 nvdimm_suspend(device_t dev)
358 {
359 
360 	return (0);
361 }
362 
363 static int
364 nvdimm_resume(device_t dev)
365 {
366 
367 	return (0);
368 }
369 
370 static device_method_t nvdimm_methods[] = {
371 	DEVMETHOD(device_identify, nvdimm_identify),
372 	DEVMETHOD(device_probe, nvdimm_probe),
373 	DEVMETHOD(device_attach, nvdimm_attach),
374 	DEVMETHOD(device_detach, nvdimm_detach),
375 	DEVMETHOD(device_suspend, nvdimm_suspend),
376 	DEVMETHOD(device_resume, nvdimm_resume),
377 	DEVMETHOD_END
378 };
379 
380 static driver_t	nvdimm_driver = {
381 	"nvdimm",
382 	nvdimm_methods,
383 	sizeof(struct nvdimm_dev),
384 };
385 
386 static void
387 nvdimm_fini(void)
388 {
389 
390 	free(nvdimm_devs, M_NVDIMM);
391 	nvdimm_devs = NULL;
392 	nvdimm_devcnt = 0;
393 }
394 
395 static int
396 nvdimm_modev(struct module *mod, int what, void *arg)
397 {
398 	int error;
399 
400 	switch (what) {
401 	case MOD_LOAD:
402 		error = 0;
403 		break;
404 
405 	case MOD_UNLOAD:
406 		nvdimm_fini();
407 		error = 0;
408 		break;
409 
410 	case MOD_QUIESCE:
411 		error = 0;
412 		break;
413 
414 	default:
415 		error = EOPNOTSUPP;
416 		break;
417 	}
418 
419 	return (error);
420 }
421 
422 DRIVER_MODULE(nvdimm, acpi, nvdimm_driver, nvdimm_devclass, nvdimm_modev, NULL);
423 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);
424