xref: /freebsd/sys/dev/nvdimm/nvdimm_acpi.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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/sbuf.h>
45 #include <sys/uuid.h>
46 
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acuuid.h>
50 #include <dev/acpica/acpivar.h>
51 
52 #include <dev/nvdimm/nvdimm_var.h>
53 
54 #define _COMPONENT	ACPI_OEM
55 ACPI_MODULE_NAME("NVDIMM_ACPI")
56 
57 struct nvdimm_root_dev {
58 	SLIST_HEAD(, SPA_mapping) spas;
59 };
60 
61 static MALLOC_DEFINE(M_NVDIMM_ACPI, "nvdimm_acpi", "NVDIMM ACPI bus memory");
62 
63 static ACPI_STATUS
64 find_dimm(ACPI_HANDLE handle, UINT32 nesting_level, void *context,
65     void **return_value)
66 {
67 	ACPI_DEVICE_INFO *device_info;
68 	ACPI_STATUS status;
69 
70 	device_info = NULL;
71 	status = AcpiGetObjectInfo(handle, &device_info);
72 	if (ACPI_FAILURE(status))
73 		return_ACPI_STATUS(AE_ERROR);
74 	if (device_info->Address == (uintptr_t)context) {
75 		*(ACPI_HANDLE *)return_value = handle;
76 		status = AE_CTRL_TERMINATE;
77 	} else
78 		status = AE_OK;
79 
80 	AcpiOsFree(device_info);
81 	return_ACPI_STATUS(status);
82 }
83 
84 static ACPI_HANDLE
85 get_dimm_acpi_handle(ACPI_HANDLE root_handle, nfit_handle_t adr)
86 {
87 	ACPI_HANDLE res;
88 	ACPI_STATUS status;
89 
90 	res = NULL;
91 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, root_handle, 1, find_dimm,
92 	    NULL, (void *)(uintptr_t)adr, &res);
93 	if (ACPI_FAILURE(status))
94 		res = NULL;
95 	return (res);
96 }
97 
98 static int
99 nvdimm_root_create_devs(device_t dev, ACPI_TABLE_NFIT *nfitbl)
100 {
101 	ACPI_HANDLE root_handle, dimm_handle;
102 	device_t child;
103 	nfit_handle_t *dimm_ids, *dimm;
104 	uintptr_t *ivars;
105 	int num_dimm_ids;
106 
107 	root_handle = acpi_get_handle(dev);
108 	acpi_nfit_get_dimm_ids(nfitbl, &dimm_ids, &num_dimm_ids);
109 	for (dimm = dimm_ids; dimm < dimm_ids + num_dimm_ids; dimm++) {
110 		dimm_handle = get_dimm_acpi_handle(root_handle, *dimm);
111 		if (dimm_handle == NULL)
112 			continue;
113 
114 		child = BUS_ADD_CHILD(dev, 100, "nvdimm", -1);
115 		if (child == NULL) {
116 			device_printf(dev, "failed to create nvdimm\n");
117 			return (ENXIO);
118 		}
119 		ivars = mallocarray(NVDIMM_ROOT_IVAR_MAX, sizeof(uintptr_t),
120 		    M_NVDIMM_ACPI, M_ZERO | M_WAITOK);
121 		device_set_ivars(child, ivars);
122 		nvdimm_root_set_acpi_handle(child, dimm_handle);
123 		nvdimm_root_set_device_handle(child, *dimm);
124 	}
125 	free(dimm_ids, M_NVDIMM_ACPI);
126 	return (0);
127 }
128 
129 static int
130 nvdimm_root_create_spas(struct nvdimm_root_dev *dev, ACPI_TABLE_NFIT *nfitbl)
131 {
132 	ACPI_NFIT_SYSTEM_ADDRESS **spas, **spa;
133 	struct SPA_mapping *spa_mapping;
134 	enum SPA_mapping_type spa_type;
135 	int error, num_spas;
136 
137 	error = 0;
138 	acpi_nfit_get_spa_ranges(nfitbl, &spas, &num_spas);
139 	for (spa = spas; spa < spas + num_spas; spa++) {
140 		spa_type = nvdimm_spa_type_from_uuid(
141 			(struct uuid *)(*spa)->RangeGuid);
142 		if (spa_type == SPA_TYPE_UNKNOWN)
143 			continue;
144 		spa_mapping = malloc(sizeof(struct SPA_mapping), M_NVDIMM_ACPI,
145 		    M_WAITOK | M_ZERO);
146 		error = nvdimm_spa_init(spa_mapping, *spa, spa_type);
147 		if (error != 0) {
148 			nvdimm_spa_fini(spa_mapping);
149 			free(spa_mapping, M_NVDIMM_ACPI);
150 			break;
151 		}
152 		if (nvdimm_spa_type_user_accessible(spa_type) &&
153 		    spa_type != SPA_TYPE_CONTROL_REGION)
154 			nvdimm_create_namespaces(spa_mapping, nfitbl);
155 		SLIST_INSERT_HEAD(&dev->spas, spa_mapping, link);
156 	}
157 	free(spas, M_NVDIMM_ACPI);
158 	return (error);
159 }
160 
161 static char *nvdimm_root_id[] = {"ACPI0012", NULL};
162 
163 static int
164 nvdimm_root_probe(device_t dev)
165 {
166 	int rv;
167 
168 	if (acpi_disabled("nvdimm"))
169 		return (ENXIO);
170 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL);
171 	if (rv <= 0)
172 		device_set_desc(dev, "ACPI NVDIMM root device");
173 
174 	return (rv);
175 }
176 
177 static int
178 nvdimm_root_attach(device_t dev)
179 {
180 	struct nvdimm_root_dev *root;
181 	ACPI_TABLE_NFIT *nfitbl;
182 	ACPI_STATUS status;
183 	int error;
184 
185 	status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl);
186 	if (ACPI_FAILURE(status)) {
187 		device_printf(dev, "cannot get NFIT\n");
188 		return (ENXIO);
189 	}
190 	error = nvdimm_root_create_devs(dev, nfitbl);
191 	if (error != 0)
192 		return (error);
193 	error = bus_generic_attach(dev);
194 	if (error != 0)
195 		return (error);
196 	root = device_get_softc(dev);
197 	error = nvdimm_root_create_spas(root, nfitbl);
198 	AcpiPutTable(&nfitbl->Header);
199 	return (error);
200 }
201 
202 static int
203 nvdimm_root_detach(device_t dev)
204 {
205 	struct nvdimm_root_dev *root;
206 	struct SPA_mapping *spa, *next;
207 	device_t *children;
208 	int i, error, num_children;
209 
210 	root = device_get_softc(dev);
211 	SLIST_FOREACH_SAFE(spa, &root->spas, link, next) {
212 		nvdimm_destroy_namespaces(spa);
213 		nvdimm_spa_fini(spa);
214 		SLIST_REMOVE_HEAD(&root->spas, link);
215 		free(spa, M_NVDIMM_ACPI);
216 	}
217 	error = bus_generic_detach(dev);
218 	if (error != 0)
219 		return (error);
220 	error = device_get_children(dev, &children, &num_children);
221 	if (error != 0)
222 		return (error);
223 	for (i = 0; i < num_children; i++)
224 		free(device_get_ivars(children[i]), M_NVDIMM_ACPI);
225 	free(children, M_TEMP);
226 	error = device_delete_children(dev);
227 	return (error);
228 }
229 
230 static int
231 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
232     uintptr_t *result)
233 {
234 
235 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
236 		return (ENOENT);
237 	*result = ((uintptr_t *)device_get_ivars(child))[index];
238 	return (0);
239 }
240 
241 static int
242 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
243     uintptr_t value)
244 {
245 
246 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
247 		return (ENOENT);
248 	((uintptr_t *)device_get_ivars(child))[index] = value;
249 	return (0);
250 }
251 
252 static int
253 nvdimm_root_child_location(device_t dev, device_t child, struct sbuf *sb)
254 {
255 	ACPI_HANDLE handle;
256 
257 	handle = nvdimm_root_get_acpi_handle(child);
258 	if (handle != NULL)
259 		sbuf_printf(sb, "handle=%s", acpi_name(handle));
260 
261 	return (0);
262 }
263 
264 static device_method_t nvdimm_acpi_methods[] = {
265 	DEVMETHOD(device_probe, nvdimm_root_probe),
266 	DEVMETHOD(device_attach, nvdimm_root_attach),
267 	DEVMETHOD(device_detach, nvdimm_root_detach),
268 	DEVMETHOD(bus_add_child, bus_generic_add_child),
269 	DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar),
270 	DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar),
271 	DEVMETHOD(bus_child_location, nvdimm_root_child_location),
272 	DEVMETHOD(bus_get_device_path, acpi_get_acpi_device_path),
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 DRIVER_MODULE(nvdimm_acpi_root, acpi, nvdimm_acpi_driver, NULL, NULL);
283 MODULE_DEPEND(nvdimm_acpi_root, acpi, 1, 1, 1);
284