xref: /freebsd/sys/dev/nvdimm/nvdimm_acpi.c (revision 5c7af84925aa0916983551b0d470b461a70c0f1f)
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
find_dimm(ACPI_HANDLE handle,UINT32 nesting_level,void * context,void ** return_value)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
get_dimm_acpi_handle(ACPI_HANDLE root_handle,nfit_handle_t adr)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
nvdimm_root_create_devs(device_t dev,ACPI_TABLE_NFIT * nfitbl)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", DEVICE_UNIT_ANY);
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
nvdimm_root_create_spas(struct nvdimm_root_dev * dev,ACPI_TABLE_NFIT * nfitbl)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
nvdimm_root_probe(device_t dev)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
nvdimm_root_attach(device_t dev)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 void
nvdimm_root_child_deleted(device_t dev,device_t child)201 nvdimm_root_child_deleted(device_t dev, device_t child)
202 {
203 	free(device_get_ivars(child), M_NVDIMM_ACPI);
204 }
205 
206 static int
nvdimm_root_detach(device_t dev)207 nvdimm_root_detach(device_t dev)
208 {
209 	struct nvdimm_root_dev *root;
210 	struct SPA_mapping *spa, *next;
211 	int error;
212 
213 	root = device_get_softc(dev);
214 	SLIST_FOREACH_SAFE(spa, &root->spas, link, next) {
215 		nvdimm_destroy_namespaces(spa);
216 		nvdimm_spa_fini(spa);
217 		SLIST_REMOVE_HEAD(&root->spas, link);
218 		free(spa, M_NVDIMM_ACPI);
219 	}
220 	error = bus_generic_detach(dev);
221 	if (error != 0)
222 		return (error);
223 	error = device_delete_children(dev);
224 	return (error);
225 }
226 
227 static int
nvdimm_root_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)228 nvdimm_root_read_ivar(device_t dev, device_t child, int index,
229     uintptr_t *result)
230 {
231 
232 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
233 		return (ENOENT);
234 	*result = ((uintptr_t *)device_get_ivars(child))[index];
235 	return (0);
236 }
237 
238 static int
nvdimm_root_write_ivar(device_t dev,device_t child,int index,uintptr_t value)239 nvdimm_root_write_ivar(device_t dev, device_t child, int index,
240     uintptr_t value)
241 {
242 
243 	if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX)
244 		return (ENOENT);
245 	((uintptr_t *)device_get_ivars(child))[index] = value;
246 	return (0);
247 }
248 
249 static int
nvdimm_root_child_location(device_t dev,device_t child,struct sbuf * sb)250 nvdimm_root_child_location(device_t dev, device_t child, struct sbuf *sb)
251 {
252 	ACPI_HANDLE handle;
253 
254 	handle = nvdimm_root_get_acpi_handle(child);
255 	if (handle != NULL)
256 		sbuf_printf(sb, "handle=%s", acpi_name(handle));
257 
258 	return (0);
259 }
260 
261 static device_method_t nvdimm_acpi_methods[] = {
262 	DEVMETHOD(device_probe, nvdimm_root_probe),
263 	DEVMETHOD(device_attach, nvdimm_root_attach),
264 	DEVMETHOD(device_detach, nvdimm_root_detach),
265 	DEVMETHOD(bus_add_child, bus_generic_add_child),
266 	DEVMETHOD(bus_child_deleted, nvdimm_root_child_deleted),
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