xref: /freebsd/sys/dev/nvdimm/nvdimm.c (revision 28f4385e45a2681c14bd04b83fe1796eaefe8265)
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_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 = mallocarray(nfitflshaddr->HintCount,
93 	    sizeof(uint64_t *), 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 int
173 nvdimm_probe(device_t dev)
174 {
175 
176 	return (BUS_PROBE_NOWILDCARD);
177 }
178 
179 static int
180 nvdimm_attach(device_t dev)
181 {
182 	struct nvdimm_dev *nv;
183 	ACPI_TABLE_NFIT *nfitbl;
184 	ACPI_HANDLE handle;
185 	ACPI_STATUS status;
186 
187 	nv = device_get_softc(dev);
188 	handle = nvdimm_root_get_acpi_handle(dev);
189 	if (handle == NULL)
190 		return (EINVAL);
191 	nv->nv_dev = dev;
192 	nv->nv_handle = nvdimm_root_get_device_handle(dev);
193 
194 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
195 	if (ACPI_FAILURE(status)) {
196 		if (bootverbose)
197 			device_printf(dev, "cannot get NFIT\n");
198 		return (ENXIO);
199 	}
200 	nvdimm_iterate_nfit(nfitbl, ACPI_NFIT_TYPE_FLUSH_ADDRESS,
201 	    nvdimm_parse_flush_addr, nv);
202 	AcpiPutTable(&nfitbl->Header);
203 	return (0);
204 }
205 
206 static int
207 nvdimm_detach(device_t dev)
208 {
209 	struct nvdimm_dev *nv;
210 
211 	nv = device_get_softc(dev);
212 	free(nv->nv_flush_addr, M_NVDIMM);
213 	return (0);
214 }
215 
216 static int
217 nvdimm_suspend(device_t dev)
218 {
219 
220 	return (0);
221 }
222 
223 static int
224 nvdimm_resume(device_t dev)
225 {
226 
227 	return (0);
228 }
229 
230 static ACPI_STATUS
231 nvdimm_root_create_dev(ACPI_HANDLE handle, UINT32 nesting_level, void *context,
232     void **return_value)
233 {
234 	ACPI_STATUS status;
235 	ACPI_DEVICE_INFO *device_info;
236 	device_t parent, child;
237 	uintptr_t *ivars;
238 
239 	parent = context;
240 	child = BUS_ADD_CHILD(parent, 100, "nvdimm", -1);
241 	if (child == NULL) {
242 		device_printf(parent, "failed to create nvdimm\n");
243 		return_ACPI_STATUS(AE_ERROR);
244 	}
245 	status = AcpiGetObjectInfo(handle, &device_info);
246 	if (ACPI_FAILURE(status)) {
247 		device_printf(parent, "failed to get nvdimm device info\n");
248 		return_ACPI_STATUS(AE_ERROR);
249 	}
250 	ivars = mallocarray(NVDIMM_ROOT_IVAR_MAX - 1, sizeof(uintptr_t),
251 	    M_NVDIMM, M_ZERO | M_WAITOK);
252 	device_set_ivars(child, ivars);
253 	nvdimm_root_set_acpi_handle(child, handle);
254 	nvdimm_root_set_device_handle(child, device_info->Address);
255 	return_ACPI_STATUS(AE_OK);
256 }
257 
258 static char *nvdimm_root_id[] = {"ACPI0012", NULL};
259 
260 static int
261 nvdimm_root_probe(device_t dev)
262 {
263 	int rv;
264 
265 	if (acpi_disabled("nvdimm"))
266 		return (ENXIO);
267 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL);
268 	if (rv <= 0)
269 		device_set_desc(dev, "ACPI NVDIMM root device");
270 
271 	return (rv);
272 }
273 
274 static int
275 nvdimm_root_attach(device_t dev)
276 {
277 	ACPI_HANDLE handle;
278 	ACPI_STATUS status;
279 	int error;
280 
281 	handle = acpi_get_handle(dev);
282 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
283 	    nvdimm_root_create_dev, NULL, dev, NULL);
284 	if (ACPI_FAILURE(status))
285 		device_printf(dev, "failed adding children\n");
286 	error = bus_generic_attach(dev);
287 	return (error);
288 }
289 
290 static int
291 nvdimm_root_detach(device_t dev)
292 {
293 	device_t *children;
294 	int i, error, num_children;
295 
296 	error = bus_generic_detach(dev);
297 	if (error != 0)
298 		return (error);
299 	error = device_get_children(dev, &children, &num_children);
300 	if (error != 0)
301 		return (error);
302 	for (i = 0; i < num_children; i++)
303 		free(device_get_ivars(children[i]), M_NVDIMM);
304 	free(children, M_TEMP);
305 	error = device_delete_children(dev);
306 	return (error);
307 }
308 
309 static int
310 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
311     uintptr_t *result)
312 {
313 
314 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
315 		return (ENOENT);
316 	*result = ((uintptr_t *)device_get_ivars(child))[index];
317 	return (0);
318 }
319 
320 static int
321 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
322     uintptr_t value)
323 {
324 
325 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
326 		return (ENOENT);
327 	((uintptr_t *)device_get_ivars(child))[index] = value;
328 	return (0);
329 }
330 
331 static device_method_t nvdimm_methods[] = {
332 	DEVMETHOD(device_probe, nvdimm_probe),
333 	DEVMETHOD(device_attach, nvdimm_attach),
334 	DEVMETHOD(device_detach, nvdimm_detach),
335 	DEVMETHOD(device_suspend, nvdimm_suspend),
336 	DEVMETHOD(device_resume, nvdimm_resume),
337 	DEVMETHOD_END
338 };
339 
340 static driver_t	nvdimm_driver = {
341 	"nvdimm",
342 	nvdimm_methods,
343 	sizeof(struct nvdimm_dev),
344 };
345 
346 static device_method_t nvdimm_root_methods[] = {
347 	DEVMETHOD(device_probe, nvdimm_root_probe),
348 	DEVMETHOD(device_attach, nvdimm_root_attach),
349 	DEVMETHOD(device_detach, nvdimm_root_detach),
350 	DEVMETHOD(bus_add_child, bus_generic_add_child),
351 	DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar),
352 	DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar),
353 	DEVMETHOD_END
354 };
355 
356 static driver_t	nvdimm_root_driver = {
357 	"nvdimm_root",
358 	nvdimm_root_methods,
359 };
360 
361 DRIVER_MODULE(nvdimm_root, acpi, nvdimm_root_driver, nvdimm_root_devclass, NULL,
362     NULL);
363 DRIVER_MODULE(nvdimm, nvdimm_root, nvdimm_driver, nvdimm_devclass, NULL, NULL);
364 MODULE_DEPEND(nvdimm, acpi, 1, 1, 1);
365