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 nvdimm_create_namespaces(spa_mapping, nfitbl); 148 SLIST_INSERT_HEAD(&dev->spas, spa_mapping, link); 149 } 150 free(spas, M_NVDIMM_ACPI); 151 return (error); 152 } 153 154 static char *nvdimm_root_id[] = {"ACPI0012", NULL}; 155 156 static int 157 nvdimm_root_probe(device_t dev) 158 { 159 int rv; 160 161 if (acpi_disabled("nvdimm")) 162 return (ENXIO); 163 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, nvdimm_root_id, NULL); 164 if (rv <= 0) 165 device_set_desc(dev, "ACPI NVDIMM root device"); 166 167 return (rv); 168 } 169 170 static int 171 nvdimm_root_attach(device_t dev) 172 { 173 struct nvdimm_root_dev *root; 174 ACPI_TABLE_NFIT *nfitbl; 175 ACPI_STATUS status; 176 int error; 177 178 status = AcpiGetTable(ACPI_SIG_NFIT, 1, (ACPI_TABLE_HEADER **)&nfitbl); 179 if (ACPI_FAILURE(status)) { 180 device_printf(dev, "cannot get NFIT\n"); 181 return (ENXIO); 182 } 183 error = nvdimm_root_create_devs(dev, nfitbl); 184 if (error != 0) 185 return (error); 186 error = bus_generic_attach(dev); 187 if (error != 0) 188 return (error); 189 root = device_get_softc(dev); 190 error = nvdimm_root_create_spas(root, nfitbl); 191 AcpiPutTable(&nfitbl->Header); 192 return (error); 193 } 194 195 static int 196 nvdimm_root_detach(device_t dev) 197 { 198 struct nvdimm_root_dev *root; 199 struct SPA_mapping *spa, *next; 200 device_t *children; 201 int i, error, num_children; 202 203 root = device_get_softc(dev); 204 SLIST_FOREACH_SAFE(spa, &root->spas, link, next) { 205 nvdimm_destroy_namespaces(spa); 206 nvdimm_spa_fini(spa); 207 SLIST_REMOVE_HEAD(&root->spas, link); 208 free(spa, M_NVDIMM_ACPI); 209 } 210 error = bus_generic_detach(dev); 211 if (error != 0) 212 return (error); 213 error = device_get_children(dev, &children, &num_children); 214 if (error != 0) 215 return (error); 216 for (i = 0; i < num_children; i++) 217 free(device_get_ivars(children[i]), M_NVDIMM_ACPI); 218 free(children, M_TEMP); 219 error = device_delete_children(dev); 220 return (error); 221 } 222 223 static int 224 nvdimm_root_read_ivar(device_t dev, device_t child, int index, 225 uintptr_t *result) 226 { 227 228 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX) 229 return (ENOENT); 230 *result = ((uintptr_t *)device_get_ivars(child))[index]; 231 return (0); 232 } 233 234 static int 235 nvdimm_root_write_ivar(device_t dev, device_t child, int index, 236 uintptr_t value) 237 { 238 239 if (index < 0 || index >= NVDIMM_ROOT_IVAR_MAX) 240 return (ENOENT); 241 ((uintptr_t *)device_get_ivars(child))[index] = value; 242 return (0); 243 } 244 245 static int 246 nvdimm_root_child_location_str(device_t dev, device_t child, char *buf, 247 size_t buflen) 248 { 249 ACPI_HANDLE handle; 250 int res; 251 252 handle = nvdimm_root_get_acpi_handle(child); 253 if (handle != NULL) 254 res = snprintf(buf, buflen, "handle=%s", acpi_name(handle)); 255 else 256 res = snprintf(buf, buflen, ""); 257 258 if (res >= buflen) 259 return (EOVERFLOW); 260 return (0); 261 } 262 263 static device_method_t nvdimm_acpi_methods[] = { 264 DEVMETHOD(device_probe, nvdimm_root_probe), 265 DEVMETHOD(device_attach, nvdimm_root_attach), 266 DEVMETHOD(device_detach, nvdimm_root_detach), 267 DEVMETHOD(bus_add_child, bus_generic_add_child), 268 DEVMETHOD(bus_read_ivar, nvdimm_root_read_ivar), 269 DEVMETHOD(bus_write_ivar, nvdimm_root_write_ivar), 270 DEVMETHOD(bus_child_location_str, nvdimm_root_child_location_str), 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 static devclass_t nvdimm_acpi_root_devclass; 281 DRIVER_MODULE(nvdimm_acpi_root, acpi, nvdimm_acpi_driver, 282 nvdimm_acpi_root_devclass, NULL, NULL); 283 MODULE_DEPEND(nvdimm_acpi_root, acpi, 1, 1, 1); 284