xref: /freebsd/sys/dev/nvdimm/nvdimm_acpi.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * Copyright (c) 2017 The FreeBSD Foundation
3  * Copyright (c) 2018, 2019 Intel Corporation
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/bitstring.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 
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>
50 
51 #include <dev/nvdimm/nvdimm_var.h>
52 
53 #define _COMPONENT	ACPI_OEM
54 ACPI_MODULE_NAME("NVDIMM_ACPI")
55 
56 struct nvdimm_root_dev {
57 	SLIST_HEAD(, SPA_mapping) spas;
58 };
59 
60 static MALLOC_DEFINE(M_NVDIMM_ACPI, "nvdimm_acpi", "NVDIMM ACPI bus memory");
61 
62 static ACPI_STATUS
63 find_dimm(ACPI_HANDLE handle, UINT32 nesting_level, void *context,
64     void **return_value)
65 {
66 	ACPI_DEVICE_INFO *device_info;
67 	ACPI_STATUS status;
68 
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);
75 	}
76 	return_ACPI_STATUS(AE_OK);
77 }
78 
79 static ACPI_HANDLE
80 get_dimm_acpi_handle(ACPI_HANDLE root_handle, nfit_handle_t adr)
81 {
82 	ACPI_HANDLE res;
83 	ACPI_STATUS status;
84 
85 	res = NULL;
86 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, root_handle, 1, find_dimm,
87 	    NULL, (void *)(uintptr_t)adr, &res);
88 	if (ACPI_FAILURE(status))
89 		res = NULL;
90 	return (res);
91 }
92 
93 static int
94 nvdimm_root_create_devs(device_t dev, ACPI_TABLE_NFIT *nfitbl)
95 {
96 	ACPI_HANDLE root_handle, dimm_handle;
97 	device_t child;
98 	nfit_handle_t *dimm_ids, *dimm;
99 	uintptr_t *ivars;
100 	int num_dimm_ids;
101 
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)
107 			continue;
108 
109 		child = BUS_ADD_CHILD(dev, 100, "nvdimm", -1);
110 		if (child == NULL) {
111 			device_printf(dev, "failed to create nvdimm\n");
112 			return (ENXIO);
113 		}
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);
119 	}
120 	free(dimm_ids, M_NVDIMM_ACPI);
121 	return (0);
122 }
123 
124 static int
125 nvdimm_root_create_spas(struct nvdimm_root_dev *dev, ACPI_TABLE_NFIT *nfitbl)
126 {
127 	ACPI_NFIT_SYSTEM_ADDRESS **spas, **spa;
128 	struct SPA_mapping *spa_mapping;
129 	enum SPA_mapping_type spa_type;
130 	int error, num_spas;
131 
132 	error = 0;
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)
138 			continue;
139 		spa_mapping = malloc(sizeof(struct SPA_mapping), M_NVDIMM_ACPI,
140 		    M_WAITOK | M_ZERO);
141 		error = nvdimm_spa_init(spa_mapping, *spa, spa_type);
142 		if (error != 0) {
143 			nvdimm_spa_fini(spa_mapping);
144 			free(spa_mapping, M_NVDIMM_ACPI);
145 			break;
146 		}
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);
151 	}
152 	free(spas, M_NVDIMM_ACPI);
153 	return (error);
154 }
155 
156 static char *nvdimm_root_id[] = {"ACPI0012", NULL};
157 
158 static int
159 nvdimm_root_probe(device_t dev)
160 {
161 	int rv;
162 
163 	if (acpi_disabled("nvdimm"))
164 		return (ENXIO);
165 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL);
166 	if (rv <= 0)
167 		device_set_desc(dev, "ACPI NVDIMM root device");
168 
169 	return (rv);
170 }
171 
172 static int
173 nvdimm_root_attach(device_t dev)
174 {
175 	struct nvdimm_root_dev *root;
176 	ACPI_TABLE_NFIT *nfitbl;
177 	ACPI_STATUS status;
178 	int error;
179 
180 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
181 	if (ACPI_FAILURE(status)) {
182 		device_printf(dev, "cannot get NFIT\n");
183 		return (ENXIO);
184 	}
185 	error = nvdimm_root_create_devs(dev, nfitbl);
186 	if (error != 0)
187 		return (error);
188 	error = bus_generic_attach(dev);
189 	if (error != 0)
190 		return (error);
191 	root = device_get_softc(dev);
192 	error = nvdimm_root_create_spas(root, nfitbl);
193 	AcpiPutTable(&nfitbl->Header);
194 	return (error);
195 }
196 
197 static int
198 nvdimm_root_detach(device_t dev)
199 {
200 	struct nvdimm_root_dev *root;
201 	struct SPA_mapping *spa, *next;
202 	device_t *children;
203 	int i, error, num_children;
204 
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);
211 	}
212 	error = bus_generic_detach(dev);
213 	if (error != 0)
214 		return (error);
215 	error = device_get_children(dev, &children, &num_children);
216 	if (error != 0)
217 		return (error);
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);
222 	return (error);
223 }
224 
225 static int
226 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
227     uintptr_t *result)
228 {
229 
230 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
231 		return (ENOENT);
232 	*result = ((uintptr_t *)device_get_ivars(child))[index];
233 	return (0);
234 }
235 
236 static int
237 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
238     uintptr_t value)
239 {
240 
241 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
242 		return (ENOENT);
243 	((uintptr_t *)device_get_ivars(child))[index] = value;
244 	return (0);
245 }
246 
247 static int
248 nvdimm_root_child_location_str(device_t dev, device_t child, char *buf,
249     size_t buflen)
250 {
251 	ACPI_HANDLE handle;
252 	int res;
253 
254 	handle = nvdimm_root_get_acpi_handle(child);
255 	if (handle != NULL)
256 		res = snprintf(buf, buflen, "handle=%s", acpi_name(handle));
257 	else
258 		res = snprintf(buf, buflen, "");
259 
260 	if (res >= buflen)
261 		return (EOVERFLOW);
262 	return (0);
263 }
264 
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),
273 	DEVMETHOD_END
274 };
275 
276 static driver_t	nvdimm_acpi_driver = {
277 	"nvdimm_acpi_root",
278 	nvdimm_acpi_methods,
279 	sizeof(struct nvdimm_root_dev),
280 };
281 
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);
286