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