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