1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2001 Mitsuru IWASAKI 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/sysctl.h> 37 38 #include <vm/vm.h> 39 #include <vm/pmap.h> 40 41 #include <contrib/dev/acpica/include/acpi.h> 42 #include <contrib/dev/acpica/include/accommon.h> 43 #include <contrib/dev/acpica/include/actables.h> 44 45 #include <dev/acpica/acpivar.h> 46 47 #include <machine/nexusvar.h> 48 49 int acpi_resume_beep; 50 SYSCTL_INT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RWTUN, 51 &acpi_resume_beep, 0, "Beep the PC speaker when resuming"); 52 53 int acpi_reset_video; 54 TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video); 55 56 static int intr_model = ACPI_INTR_PIC; 57 58 int 59 acpi_machdep_init(device_t dev) 60 { 61 struct acpi_softc *sc; 62 63 sc = device_get_softc(dev); 64 65 acpi_apm_init(sc); 66 67 if (intr_model != ACPI_INTR_PIC) 68 acpi_SetIntrModel(intr_model); 69 70 SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, 71 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, 72 "reset_video", CTLFLAG_RW, &acpi_reset_video, 0, 73 "Call the VESA reset BIOS vector on the resume path"); 74 75 return (0); 76 } 77 78 void 79 acpi_SetDefaultIntrModel(int model) 80 { 81 82 intr_model = model; 83 } 84 85 int 86 acpi_machdep_quirks(int *quirks) 87 { 88 89 return (0); 90 } 91 92 /* 93 * Support for mapping ACPI tables during early boot. Currently this 94 * uses the crashdump map to map each table. However, the crashdump 95 * map is created in pmap_bootstrap() right after the direct map, so 96 * we should be able to just use pmap_mapbios() here instead. 97 * 98 * This makes the following assumptions about how we use this KVA: 99 * pages 0 and 1 are used to map in the header of each table found via 100 * the RSDT or XSDT and pages 2 to n are used to map in the RSDT or 101 * XSDT. This has to use 2 pages for the table headers in case a 102 * header spans a page boundary. 103 * 104 * XXX: We don't ensure the table fits in the available address space 105 * in the crashdump map. 106 */ 107 108 /* 109 * Map some memory using the crashdump map. 'offset' is an offset in 110 * pages into the crashdump map to use for the start of the mapping. 111 */ 112 static void * 113 table_map(vm_paddr_t pa, int offset, vm_offset_t length) 114 { 115 vm_offset_t va, off; 116 void *data; 117 118 off = pa & PAGE_MASK; 119 length = round_page(length + off); 120 pa = pa & PG_FRAME; 121 va = (vm_offset_t)pmap_kenter_temporary(pa, offset) + 122 (offset * PAGE_SIZE); 123 data = (void *)(va + off); 124 length -= PAGE_SIZE; 125 while (length > 0) { 126 va += PAGE_SIZE; 127 pa += PAGE_SIZE; 128 length -= PAGE_SIZE; 129 pmap_kenter(va, pa); 130 invlpg(va); 131 } 132 return (data); 133 } 134 135 /* Unmap memory previously mapped with table_map(). */ 136 static void 137 table_unmap(void *data, vm_offset_t length) 138 { 139 vm_offset_t va, off; 140 141 va = (vm_offset_t)data; 142 off = va & PAGE_MASK; 143 length = round_page(length + off); 144 va &= ~PAGE_MASK; 145 while (length > 0) { 146 pmap_kremove(va); 147 invlpg(va); 148 va += PAGE_SIZE; 149 length -= PAGE_SIZE; 150 } 151 } 152 153 /* 154 * Map a table at a given offset into the crashdump map. It first 155 * maps the header to determine the table length and then maps the 156 * entire table. 157 */ 158 static void * 159 map_table(vm_paddr_t pa, int offset, const char *sig) 160 { 161 ACPI_TABLE_HEADER *header; 162 vm_offset_t length; 163 void *table; 164 165 header = table_map(pa, offset, sizeof(ACPI_TABLE_HEADER)); 166 if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) { 167 table_unmap(header, sizeof(ACPI_TABLE_HEADER)); 168 return (NULL); 169 } 170 length = header->Length; 171 table_unmap(header, sizeof(ACPI_TABLE_HEADER)); 172 table = table_map(pa, offset, length); 173 if (ACPI_FAILURE(AcpiTbChecksum(table, length))) { 174 if (bootverbose) 175 printf("ACPI: Failed checksum for table %s\n", sig); 176 #if (ACPI_CHECKSUM_ABORT) 177 table_unmap(table, length); 178 return (NULL); 179 #endif 180 } 181 return (table); 182 } 183 184 /* 185 * See if a given ACPI table is the requested table. Returns the 186 * length of the able if it matches or zero on failure. 187 */ 188 static int 189 probe_table(vm_paddr_t address, const char *sig) 190 { 191 ACPI_TABLE_HEADER *table; 192 193 table = table_map(address, 0, sizeof(ACPI_TABLE_HEADER)); 194 if (table == NULL) { 195 if (bootverbose) 196 printf("ACPI: Failed to map table at 0x%jx\n", 197 (uintmax_t)address); 198 return (0); 199 } 200 if (bootverbose) 201 printf("Table '%.4s' at 0x%jx\n", table->Signature, 202 (uintmax_t)address); 203 204 if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) { 205 table_unmap(table, sizeof(ACPI_TABLE_HEADER)); 206 return (0); 207 } 208 table_unmap(table, sizeof(ACPI_TABLE_HEADER)); 209 return (1); 210 } 211 212 /* 213 * Try to map a table at a given physical address previously returned 214 * by acpi_find_table(). 215 */ 216 void * 217 acpi_map_table(vm_paddr_t pa, const char *sig) 218 { 219 220 return (map_table(pa, 0, sig)); 221 } 222 223 /* Unmap a table previously mapped via acpi_map_table(). */ 224 void 225 acpi_unmap_table(void *table) 226 { 227 ACPI_TABLE_HEADER *header; 228 229 header = (ACPI_TABLE_HEADER *)table; 230 table_unmap(table, header->Length); 231 } 232 233 /* 234 * Return the physical address of the requested table or zero if one 235 * is not found. 236 */ 237 vm_paddr_t 238 acpi_find_table(const char *sig) 239 { 240 ACPI_PHYSICAL_ADDRESS rsdp_ptr; 241 ACPI_TABLE_RSDP *rsdp; 242 ACPI_TABLE_RSDT *rsdt; 243 ACPI_TABLE_XSDT *xsdt; 244 ACPI_TABLE_HEADER *table; 245 vm_paddr_t addr; 246 int i, count; 247 248 if (resource_disabled("acpi", 0)) 249 return (0); 250 251 /* 252 * Map in the RSDP. Since ACPI uses AcpiOsMapMemory() which in turn 253 * calls pmap_mapbios() to find the RSDP, we assume that we can use 254 * pmap_mapbios() to map the RSDP. 255 */ 256 if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0) 257 return (0); 258 rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP)); 259 if (rsdp == NULL) { 260 if (bootverbose) 261 printf("ACPI: Failed to map RSDP\n"); 262 return (0); 263 } 264 265 /* 266 * For ACPI >= 2.0, use the XSDT if it is available. 267 * Otherwise, use the RSDT. We map the XSDT or RSDT at page 2 268 * in the crashdump area. Pages 0 and 1 are used to map in the 269 * headers of candidate ACPI tables. 270 */ 271 addr = 0; 272 if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) { 273 /* 274 * AcpiOsGetRootPointer only verifies the checksum for 275 * the version 1.0 portion of the RSDP. Version 2.0 has 276 * an additional checksum that we verify first. 277 */ 278 if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) { 279 if (bootverbose) 280 printf("ACPI: RSDP failed extended checksum\n"); 281 return (0); 282 } 283 xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT); 284 if (xsdt == NULL) { 285 if (bootverbose) 286 printf("ACPI: Failed to map XSDT\n"); 287 return (0); 288 } 289 count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) / 290 sizeof(UINT64); 291 for (i = 0; i < count; i++) 292 if (probe_table(xsdt->TableOffsetEntry[i], sig)) { 293 addr = xsdt->TableOffsetEntry[i]; 294 break; 295 } 296 acpi_unmap_table(xsdt); 297 } else { 298 rsdt = map_table(rsdp->RsdtPhysicalAddress, 2, ACPI_SIG_RSDT); 299 if (rsdt == NULL) { 300 if (bootverbose) 301 printf("ACPI: Failed to map RSDT\n"); 302 return (0); 303 } 304 count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) / 305 sizeof(UINT32); 306 for (i = 0; i < count; i++) 307 if (probe_table(rsdt->TableOffsetEntry[i], sig)) { 308 addr = rsdt->TableOffsetEntry[i]; 309 break; 310 } 311 acpi_unmap_table(rsdt); 312 } 313 pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP)); 314 if (addr == 0) { 315 if (bootverbose) 316 printf("ACPI: No %s table found\n", sig); 317 return (0); 318 } 319 if (bootverbose) 320 printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr); 321 322 /* 323 * Verify that we can map the full table and that its checksum is 324 * correct, etc. 325 */ 326 table = map_table(addr, 0, sig); 327 if (table == NULL) 328 return (0); 329 acpi_unmap_table(table); 330 331 return (addr); 332 } 333 334 /* 335 * ACPI nexus(4) driver. 336 */ 337 static int 338 nexus_acpi_probe(device_t dev) 339 { 340 int error; 341 342 error = acpi_identify(); 343 if (error) 344 return (error); 345 346 return (BUS_PROBE_DEFAULT); 347 } 348 349 static int 350 nexus_acpi_attach(device_t dev) 351 { 352 device_t acpi_dev; 353 int error; 354 355 nexus_init_resources(); 356 bus_generic_probe(dev); 357 acpi_dev = BUS_ADD_CHILD(dev, 10, "acpi", 0); 358 if (acpi_dev == NULL) 359 panic("failed to add acpi0 device"); 360 361 error = bus_generic_attach(dev); 362 if (error == 0) 363 acpi_install_wakeup_handler(device_get_softc(acpi_dev)); 364 365 return (error); 366 } 367 368 static device_method_t nexus_acpi_methods[] = { 369 /* Device interface */ 370 DEVMETHOD(device_probe, nexus_acpi_probe), 371 DEVMETHOD(device_attach, nexus_acpi_attach), 372 373 { 0, 0 } 374 }; 375 376 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver); 377 static devclass_t nexus_devclass; 378 379 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0); 380