1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Alexander Motin <mav@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_acpi.h" 32 #include "opt_pci.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/callout.h> 38 #include <sys/interrupt.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/queue.h> 43 #include <sys/rman.h> 44 #include <vm/vm.h> 45 #include <vm/pmap.h> 46 47 #include <contrib/dev/acpica/include/acpi.h> 48 #include <contrib/dev/acpica/include/accommon.h> 49 #include <contrib/dev/acpica/include/aclocal.h> 50 #include <contrib/dev/acpica/include/actables.h> 51 52 #include <dev/acpica/acpivar.h> 53 #include <dev/pci/pcireg.h> 54 #include <dev/pci/pcivar.h> 55 56 struct apei_ge { 57 union { 58 ACPI_HEST_GENERIC v1; 59 ACPI_HEST_GENERIC_V2 v2; 60 }; 61 int res_type; 62 int res_rid; 63 struct resource *res; 64 int res2_type; 65 int res2_rid; 66 struct resource *res2; 67 uint8_t *buf, *copybuf; 68 TAILQ_ENTRY(apei_ge) link; 69 TAILQ_ENTRY(apei_ge) nlink; 70 }; 71 72 /* NMI */ 73 struct apei_nges { 74 void *swi_ih; 75 TAILQ_HEAD(, apei_ge) ges; 76 } *apei_nmi_nges; 77 78 /* Interrupt */ 79 struct apei_iges { 80 TAILQ_HEAD(, apei_ge) ges; 81 }; 82 83 /* Polling */ 84 struct apei_pges { 85 sbintime_t interval; 86 struct callout poll; 87 TAILQ_HEAD(, apei_ge) ges; 88 }; 89 90 struct apei_softc { 91 ACPI_TABLE_HEST *hest; 92 TAILQ_HEAD(, apei_ge) ges; 93 struct apei_nges nges; 94 struct apei_iges iges; 95 struct apei_pges pges[32]; 96 }; 97 98 struct apei_mem_error { 99 uint64_t ValidationBits; 100 uint64_t ErrorStatus; 101 uint64_t PhysicalAddress; 102 uint64_t PhysicalAddressMask; 103 uint16_t Node; 104 uint16_t Card; 105 uint16_t Module; 106 uint16_t Bank; 107 uint16_t Device; 108 uint16_t Row; 109 uint16_t Column; 110 uint16_t BitPosition; 111 uint64_t RequesterID; 112 uint64_t ResponderID; 113 uint64_t TargetID; 114 uint8_t MemoryErrorType; 115 uint8_t Extended; 116 uint16_t RankNumber; 117 uint16_t CardHandle; 118 uint16_t ModuleHandle; 119 }; 120 121 struct apei_pcie_error { 122 uint64_t ValidationBits; 123 uint32_t PortType; 124 uint32_t Version; 125 uint32_t CommandStatus; 126 uint32_t Reserved; 127 uint8_t DeviceID[16]; 128 uint8_t DeviceSerialNumber[8]; 129 uint8_t BridgeControlStatus[4]; 130 uint8_t CapabilityStructure[60]; 131 uint8_t AERInfo[96]; 132 }; 133 134 #ifdef __i386__ 135 static __inline uint64_t 136 apei_bus_read_8(struct resource *res, bus_size_t offset) 137 { 138 return (bus_read_4(res, offset) | 139 ((uint64_t)bus_read_4(res, offset + 4)) << 32); 140 } 141 static __inline void 142 apei_bus_write_8(struct resource *res, bus_size_t offset, uint64_t val) 143 { 144 bus_write_4(res, offset, val); 145 bus_write_4(res, offset + 4, val >> 32); 146 } 147 #define READ8(r, o) apei_bus_read_8((r), (o)) 148 #define WRITE8(r, o, v) apei_bus_write_8((r), (o), (v)) 149 #else 150 #define READ8(r, o) bus_read_8((r), (o)) 151 #define WRITE8(r, o, v) bus_write_8((r), (o), (v)) 152 #endif 153 154 #define GED_SIZE(ged) ((ged)->Revision >= 0x300 ? \ 155 sizeof(ACPI_HEST_GENERIC_DATA_V300) : sizeof(ACPI_HEST_GENERIC_DATA)) 156 #define GED_DATA(ged) ((uint8_t *)(ged) + GED_SIZE(ged)) 157 158 #define PGE_ID(ge) (fls(MAX(1, (ge)->v1.Notify.PollInterval)) - 1) 159 160 int apei_nmi_handler(void); 161 162 static const char * 163 apei_severity(uint32_t s) 164 { 165 switch (s) { 166 case ACPI_HEST_GEN_ERROR_RECOVERABLE: 167 return ("Recoverable"); 168 case ACPI_HEST_GEN_ERROR_FATAL: 169 return ("Fatal"); 170 case ACPI_HEST_GEN_ERROR_CORRECTED: 171 return ("Corrected"); 172 case ACPI_HEST_GEN_ERROR_NONE: 173 return ("Informational"); 174 } 175 return ("???"); 176 } 177 178 static int 179 apei_mem_handler(ACPI_HEST_GENERIC_DATA *ged) 180 { 181 struct apei_mem_error *p = (struct apei_mem_error *)GED_DATA(ged); 182 183 printf("APEI %s Memory Error:\n", apei_severity(ged->ErrorSeverity)); 184 if (p->ValidationBits & 0x01) 185 printf(" Error Status: 0x%jx\n", p->ErrorStatus); 186 if (p->ValidationBits & 0x02) 187 printf(" Physical Address: 0x%jx\n", p->PhysicalAddress); 188 if (p->ValidationBits & 0x04) 189 printf(" Physical Address Mask: 0x%jx\n", p->PhysicalAddressMask); 190 if (p->ValidationBits & 0x08) 191 printf(" Node: %u\n", p->Node); 192 if (p->ValidationBits & 0x10) 193 printf(" Card: %u\n", p->Card); 194 if (p->ValidationBits & 0x20) 195 printf(" Module: %u\n", p->Module); 196 if (p->ValidationBits & 0x40) 197 printf(" Bank: %u\n", p->Bank); 198 if (p->ValidationBits & 0x80) 199 printf(" Device: %u\n", p->Device); 200 if (p->ValidationBits & 0x100) 201 printf(" Row: %u\n", p->Row); 202 if (p->ValidationBits & 0x200) 203 printf(" Column: %u\n", p->Column); 204 if (p->ValidationBits & 0x400) 205 printf(" Bit Position: %u\n", p->BitPosition); 206 if (p->ValidationBits & 0x800) 207 printf(" Requester ID: 0x%jx\n", p->RequesterID); 208 if (p->ValidationBits & 0x1000) 209 printf(" Responder ID: 0x%jx\n", p->ResponderID); 210 if (p->ValidationBits & 0x2000) 211 printf(" Target ID: 0x%jx\n", p->TargetID); 212 if (p->ValidationBits & 0x4000) 213 printf(" Memory Error Type: %u\n", p->MemoryErrorType); 214 if (p->ValidationBits & 0x8000) 215 printf(" Rank Number: %u\n", p->RankNumber); 216 if (p->ValidationBits & 0x10000) 217 printf(" Card Handle: 0x%x\n", p->CardHandle); 218 if (p->ValidationBits & 0x20000) 219 printf(" Module Handle: 0x%x\n", p->ModuleHandle); 220 if (p->ValidationBits & 0x40000) 221 printf(" Extended Row: %u\n", 222 (uint32_t)(p->Extended & 0x3) << 16 | p->Row); 223 if (p->ValidationBits & 0x80000) 224 printf(" Bank Group: %u\n", p->Bank >> 8); 225 if (p->ValidationBits & 0x100000) 226 printf(" Bank Address: %u\n", p->Bank & 0xff); 227 if (p->ValidationBits & 0x200000) 228 printf(" Chip Identification: %u\n", (p->Extended >> 5) & 0x7); 229 230 return (0); 231 } 232 233 static int 234 apei_pcie_handler(ACPI_HEST_GENERIC_DATA *ged) 235 { 236 struct apei_pcie_error *p = (struct apei_pcie_error *)GED_DATA(ged); 237 int h = 0, off; 238 #ifdef DEV_PCI 239 device_t dev; 240 int sev; 241 242 if ((p->ValidationBits & 0x8) == 0x8) { 243 mtx_lock(&Giant); 244 dev = pci_find_dbsf((uint32_t)p->DeviceID[10] << 8 | 245 p->DeviceID[9], p->DeviceID[11], p->DeviceID[8], 246 p->DeviceID[7]); 247 if (dev != NULL) { 248 switch (ged->ErrorSeverity) { 249 case ACPI_HEST_GEN_ERROR_FATAL: 250 sev = PCIEM_STA_FATAL_ERROR; 251 break; 252 case ACPI_HEST_GEN_ERROR_RECOVERABLE: 253 sev = PCIEM_STA_NON_FATAL_ERROR; 254 break; 255 default: 256 sev = PCIEM_STA_CORRECTABLE_ERROR; 257 break; 258 } 259 pcie_apei_error(dev, sev, 260 (p->ValidationBits & 0x80) ? p->AERInfo : NULL); 261 h = 1; 262 } 263 mtx_unlock(&Giant); 264 } 265 if (h) 266 return (h); 267 #endif 268 269 printf("APEI %s PCIe Error:\n", apei_severity(ged->ErrorSeverity)); 270 if (p->ValidationBits & 0x01) 271 printf(" Port Type: %u\n", p->PortType); 272 if (p->ValidationBits & 0x02) 273 printf(" Version: %x\n", p->Version); 274 if (p->ValidationBits & 0x04) 275 printf(" Command Status: 0x%08x\n", p->CommandStatus); 276 if (p->ValidationBits & 0x08) { 277 printf(" DeviceID:"); 278 for (off = 0; off < sizeof(p->DeviceID); off++) 279 printf(" %02x", p->DeviceID[off]); 280 printf("\n"); 281 } 282 if (p->ValidationBits & 0x10) { 283 printf(" Device Serial Number:"); 284 for (off = 0; off < sizeof(p->DeviceSerialNumber); off++) 285 printf(" %02x", p->DeviceSerialNumber[off]); 286 printf("\n"); 287 } 288 if (p->ValidationBits & 0x20) { 289 printf(" Bridge Control Status:"); 290 for (off = 0; off < sizeof(p->BridgeControlStatus); off++) 291 printf(" %02x", p->BridgeControlStatus[off]); 292 printf("\n"); 293 } 294 if (p->ValidationBits & 0x40) { 295 printf(" Capability Structure:\n"); 296 for (off = 0; off < sizeof(p->CapabilityStructure); off++) { 297 printf(" %02x", p->CapabilityStructure[off]); 298 if ((off % 16) == 15 || 299 off + 1 == sizeof(p->CapabilityStructure)) 300 printf("\n"); 301 } 302 } 303 if (p->ValidationBits & 0x80) { 304 printf(" AER Info:\n"); 305 for (off = 0; off < sizeof(p->AERInfo); off++) { 306 printf(" %02x", p->AERInfo[off]); 307 if ((off % 16) == 15 || off + 1 == sizeof(p->AERInfo)) 308 printf("\n"); 309 } 310 } 311 return (h); 312 } 313 314 static void 315 apei_ged_handler(ACPI_HEST_GENERIC_DATA *ged) 316 { 317 ACPI_HEST_GENERIC_DATA_V300 *ged3 = (ACPI_HEST_GENERIC_DATA_V300 *)ged; 318 /* A5BC1114-6F64-4EDE-B863-3E83ED7C83B1 */ 319 static uint8_t mem_uuid[ACPI_UUID_LENGTH] = { 320 0x14, 0x11, 0xBC, 0xA5, 0x64, 0x6F, 0xDE, 0x4E, 321 0xB8, 0x63, 0x3E, 0x83, 0xED, 0x7C, 0x83, 0xB1 322 }; 323 /* D995E954-BBC1-430F-AD91-B44DCB3C6F35 */ 324 static uint8_t pcie_uuid[ACPI_UUID_LENGTH] = { 325 0x54, 0xE9, 0x95, 0xD9, 0xC1, 0xBB, 0x0F, 0x43, 326 0xAD, 0x91, 0xB4, 0x4D, 0xCB, 0x3C, 0x6F, 0x35 327 }; 328 uint8_t *t; 329 int h = 0, off; 330 331 if (memcmp(mem_uuid, ged->SectionType, ACPI_UUID_LENGTH) == 0) { 332 h = apei_mem_handler(ged); 333 } else if (memcmp(pcie_uuid, ged->SectionType, ACPI_UUID_LENGTH) == 0) { 334 h = apei_pcie_handler(ged); 335 } else { 336 t = ged->SectionType; 337 printf("APEI %s Error %02x%02x%02x%02x-%02x%02x-" 338 "%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x:\n", 339 apei_severity(ged->ErrorSeverity), 340 t[3], t[2], t[1], t[0], t[5], t[4], t[7], t[6], 341 t[8], t[9], t[10], t[11], t[12], t[13], t[14], t[15]); 342 printf(" Error Data:\n"); 343 t = (uint8_t *)GED_DATA(ged); 344 for (off = 0; off < ged->ErrorDataLength; off++) { 345 printf(" %02x", t[off]); 346 if ((off % 16) == 15 || off + 1 == ged->ErrorDataLength) 347 printf("\n"); 348 } 349 } 350 if (h) 351 return; 352 353 printf(" Flags: 0x%x\n", ged->Flags); 354 if (ged->ValidationBits & ACPI_HEST_GEN_VALID_FRU_ID) { 355 t = ged->FruId; 356 printf(" FRU Id: %02x%02x%02x%02x-%02x%02x-%02x%02x-" 357 "%02x%02x-%02x%02x%02x%02x%02x%02x\n", 358 t[3], t[2], t[1], t[0], t[5], t[4], t[7], t[6], 359 t[8], t[9], t[10], t[11], t[12], t[13], t[14], t[15]); 360 } 361 if (ged->ValidationBits & ACPI_HEST_GEN_VALID_FRU_STRING) 362 printf(" FRU Text: %.20s\n", ged->FruText); 363 if (ged->Revision >= 0x300 && 364 ged->ValidationBits & ACPI_HEST_GEN_VALID_TIMESTAMP) 365 printf(" Timestamp: %016jx\n", ged3->TimeStamp); 366 } 367 368 static int 369 apei_ge_handler(struct apei_ge *ge, bool copy) 370 { 371 uint8_t *buf = copy ? ge->copybuf : ge->buf; 372 ACPI_HEST_GENERIC_STATUS *ges = (ACPI_HEST_GENERIC_STATUS *)buf; 373 ACPI_HEST_GENERIC_DATA *ged; 374 size_t off, len; 375 uint32_t sev; 376 int i, c; 377 378 if (ges == NULL || ges->BlockStatus == 0) 379 return (0); 380 381 c = (ges->BlockStatus >> 4) & 0x3ff; 382 sev = ges->ErrorSeverity; 383 384 /* Process error entries. */ 385 len = MIN(ge->v1.ErrorBlockLength - sizeof(*ges), ges->DataLength); 386 for (off = i = 0; i < c && off + sizeof(*ged) <= len; i++) { 387 ged = (ACPI_HEST_GENERIC_DATA *)&buf[sizeof(*ges) + off]; 388 if ((uint64_t)GED_SIZE(ged) + ged->ErrorDataLength > len - off) 389 break; 390 apei_ged_handler(ged); 391 off += GED_SIZE(ged) + ged->ErrorDataLength; 392 } 393 394 /* Acknowledge the error has been processed. */ 395 ges->BlockStatus = 0; 396 if (!copy && ge->v1.Header.Type == ACPI_HEST_TYPE_GENERIC_ERROR_V2 && 397 ge->res2) { 398 uint64_t val = READ8(ge->res2, 0); 399 val &= ge->v2.ReadAckPreserve; 400 val |= ge->v2.ReadAckWrite; 401 WRITE8(ge->res2, 0, val); 402 } 403 404 /* If ACPI told the error is fatal -- make it so. */ 405 if (sev == ACPI_HEST_GEN_ERROR_FATAL) 406 panic("APEI Fatal Hardware Error!"); 407 408 return (1); 409 } 410 411 static void 412 apei_nmi_swi(void *arg) 413 { 414 struct apei_nges *nges = arg; 415 struct apei_ge *ge; 416 417 TAILQ_FOREACH(ge, &nges->ges, nlink) 418 apei_ge_handler(ge, true); 419 } 420 421 int 422 apei_nmi_handler(void) 423 { 424 struct apei_nges *nges = apei_nmi_nges; 425 struct apei_ge *ge; 426 ACPI_HEST_GENERIC_STATUS *ges, *gesc; 427 int handled = 0; 428 429 if (nges == NULL) 430 return (0); 431 432 TAILQ_FOREACH(ge, &nges->ges, nlink) { 433 ges = (ACPI_HEST_GENERIC_STATUS *)ge->buf; 434 if (ges == NULL || ges->BlockStatus == 0) 435 continue; 436 437 /* If ACPI told the error is fatal -- make it so. */ 438 if (ges->ErrorSeverity == ACPI_HEST_GEN_ERROR_FATAL) 439 panic("APEI Fatal Hardware Error!"); 440 441 /* Copy the buffer for later processing. */ 442 gesc = (ACPI_HEST_GENERIC_STATUS *)ge->copybuf; 443 if (gesc->BlockStatus == 0) 444 memcpy(ge->copybuf, ge->buf, ge->v1.ErrorBlockLength); 445 446 /* Acknowledge the error has been processed. */ 447 ges->BlockStatus = 0; 448 if (ge->v1.Header.Type == ACPI_HEST_TYPE_GENERIC_ERROR_V2 && 449 ge->res2) { 450 uint64_t val = READ8(ge->res2, 0); 451 val &= ge->v2.ReadAckPreserve; 452 val |= ge->v2.ReadAckWrite; 453 WRITE8(ge->res2, 0, val); 454 } 455 handled = 1; 456 } 457 458 /* Schedule SWI for real handling. */ 459 if (handled) 460 swi_sched(nges->swi_ih, SWI_FROMNMI); 461 462 return (handled); 463 } 464 465 static void 466 apei_callout_handler(void *context) 467 { 468 struct apei_pges *pges = context; 469 struct apei_ge *ge; 470 471 TAILQ_FOREACH(ge, &pges->ges, nlink) 472 apei_ge_handler(ge, false); 473 callout_schedule_sbt(&pges->poll, pges->interval, pges->interval, 0); 474 } 475 476 static void 477 apei_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 478 { 479 device_t dev = context; 480 struct apei_softc *sc = device_get_softc(dev); 481 struct apei_ge *ge; 482 483 TAILQ_FOREACH(ge, &sc->iges.ges, nlink) 484 apei_ge_handler(ge, false); 485 } 486 487 static int 488 hest_parse_structure(struct apei_softc *sc, void *addr, int remaining) 489 { 490 ACPI_HEST_HEADER *hdr = addr; 491 struct apei_ge *ge; 492 493 if (remaining < (int)sizeof(ACPI_HEST_HEADER)) 494 return (-1); 495 496 switch (hdr->Type) { 497 case ACPI_HEST_TYPE_IA32_CHECK: { 498 ACPI_HEST_IA_MACHINE_CHECK *s = addr; 499 return (sizeof(*s) + s->NumHardwareBanks * 500 sizeof(ACPI_HEST_IA_ERROR_BANK)); 501 } 502 case ACPI_HEST_TYPE_IA32_CORRECTED_CHECK: { 503 ACPI_HEST_IA_CORRECTED *s = addr; 504 return (sizeof(*s) + s->NumHardwareBanks * 505 sizeof(ACPI_HEST_IA_ERROR_BANK)); 506 } 507 case ACPI_HEST_TYPE_IA32_NMI: { 508 ACPI_HEST_IA_NMI *s = addr; 509 return (sizeof(*s)); 510 } 511 case ACPI_HEST_TYPE_AER_ROOT_PORT: { 512 ACPI_HEST_AER_ROOT *s = addr; 513 return (sizeof(*s)); 514 } 515 case ACPI_HEST_TYPE_AER_ENDPOINT: { 516 ACPI_HEST_AER *s = addr; 517 return (sizeof(*s)); 518 } 519 case ACPI_HEST_TYPE_AER_BRIDGE: { 520 ACPI_HEST_AER_BRIDGE *s = addr; 521 return (sizeof(*s)); 522 } 523 case ACPI_HEST_TYPE_GENERIC_ERROR: { 524 ACPI_HEST_GENERIC *s = addr; 525 ge = malloc(sizeof(*ge), M_DEVBUF, M_WAITOK | M_ZERO); 526 ge->v1 = *s; 527 TAILQ_INSERT_TAIL(&sc->ges, ge, link); 528 return (sizeof(*s)); 529 } 530 case ACPI_HEST_TYPE_GENERIC_ERROR_V2: { 531 ACPI_HEST_GENERIC_V2 *s = addr; 532 ge = malloc(sizeof(*ge), M_DEVBUF, M_WAITOK | M_ZERO); 533 ge->v2 = *s; 534 TAILQ_INSERT_TAIL(&sc->ges, ge, link); 535 return (sizeof(*s)); 536 } 537 case ACPI_HEST_TYPE_IA32_DEFERRED_CHECK: { 538 ACPI_HEST_IA_DEFERRED_CHECK *s = addr; 539 return (sizeof(*s) + s->NumHardwareBanks * 540 sizeof(ACPI_HEST_IA_ERROR_BANK)); 541 } 542 default: 543 return (-1); 544 } 545 } 546 547 static void 548 hest_parse_table(struct apei_softc *sc) 549 { 550 ACPI_TABLE_HEST *hest = sc->hest; 551 char *cp; 552 int remaining, consumed; 553 554 remaining = hest->Header.Length - sizeof(ACPI_TABLE_HEST); 555 while (remaining > 0) { 556 cp = (char *)hest + hest->Header.Length - remaining; 557 consumed = hest_parse_structure(sc, cp, remaining); 558 if (consumed <= 0) 559 break; 560 else 561 remaining -= consumed; 562 } 563 } 564 565 static char *apei_ids[] = { "PNP0C33", NULL }; 566 567 static ACPI_STATUS 568 apei_find(ACPI_HANDLE handle, UINT32 level, void *context, 569 void **status) 570 { 571 int *found = (int *)status; 572 char **ids; 573 574 for (ids = apei_ids; *ids != NULL; ids++) { 575 if (acpi_MatchHid(handle, *ids)) { 576 *found = 1; 577 break; 578 } 579 } 580 return (AE_OK); 581 } 582 583 static void 584 apei_identify(driver_t *driver, device_t parent) 585 { 586 device_t child; 587 int found; 588 ACPI_TABLE_HEADER *hest; 589 ACPI_STATUS status; 590 591 if (acpi_disabled("apei")) 592 return; 593 594 /* Without HEST table we have nothing to do. */ 595 status = AcpiGetTable(ACPI_SIG_HEST, 0, &hest); 596 if (ACPI_FAILURE(status)) 597 return; 598 AcpiPutTable(hest); 599 600 /* Only one APEI device can exist. */ 601 if (devclass_get_device(devclass_find("apei"), 0)) 602 return; 603 604 /* Search for ACPI error device to be used. */ 605 found = 0; 606 AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 607 100, apei_find, NULL, NULL, (void *)&found); 608 if (found) 609 return; 610 611 /* If not found - create a fake one. */ 612 child = BUS_ADD_CHILD(parent, 2, "apei", 0); 613 if (child == NULL) 614 printf("%s: can't add child\n", __func__); 615 } 616 617 static int 618 apei_probe(device_t dev) 619 { 620 ACPI_TABLE_HEADER *hest; 621 ACPI_STATUS status; 622 int rv; 623 624 if (acpi_disabled("apei")) 625 return (ENXIO); 626 627 if (acpi_get_handle(dev) != NULL) { 628 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, apei_ids, NULL); 629 if (rv > 0) 630 return (rv); 631 } else 632 rv = 0; 633 634 /* Without HEST table we have nothing to do. */ 635 status = AcpiGetTable(ACPI_SIG_HEST, 0, &hest); 636 if (ACPI_FAILURE(status)) 637 return (ENXIO); 638 AcpiPutTable(hest); 639 640 device_set_desc(dev, "ACPI Platform Error Interface"); 641 return (rv); 642 } 643 644 static int 645 apei_attach(device_t dev) 646 { 647 struct apei_softc *sc = device_get_softc(dev); 648 struct apei_pges *pges; 649 struct apei_ge *ge; 650 ACPI_STATUS status; 651 int rid; 652 653 TAILQ_INIT(&sc->ges); 654 TAILQ_INIT(&sc->nges.ges); 655 TAILQ_INIT(&sc->iges.ges); 656 for (int i = 0; i < nitems(sc->pges); i++) { 657 pges = &sc->pges[i]; 658 pges->interval = SBT_1MS << i; 659 callout_init(&pges->poll, 1); 660 TAILQ_INIT(&pges->ges); 661 } 662 663 /* Search and parse HEST table. */ 664 status = AcpiGetTable(ACPI_SIG_HEST, 0, (ACPI_TABLE_HEADER **)&sc->hest); 665 if (ACPI_FAILURE(status)) 666 return (ENXIO); 667 hest_parse_table(sc); 668 AcpiPutTable((ACPI_TABLE_HEADER *)sc->hest); 669 670 rid = 0; 671 TAILQ_FOREACH(ge, &sc->ges, link) { 672 ge->res_rid = rid++; 673 acpi_bus_alloc_gas(dev, &ge->res_type, &ge->res_rid, 674 &ge->v1.ErrorStatusAddress, &ge->res, 0); 675 if (ge->res) { 676 ge->buf = pmap_mapdev_attr(READ8(ge->res, 0), 677 ge->v1.ErrorBlockLength, VM_MEMATTR_WRITE_COMBINING); 678 } else { 679 device_printf(dev, "Can't allocate status resource.\n"); 680 } 681 if (ge->v1.Header.Type == ACPI_HEST_TYPE_GENERIC_ERROR_V2) { 682 ge->res2_rid = rid++; 683 acpi_bus_alloc_gas(dev, &ge->res2_type, &ge->res2_rid, 684 &ge->v2.ReadAckRegister, &ge->res2, 0); 685 if (ge->res2 == NULL) 686 device_printf(dev, "Can't allocate ack resource.\n"); 687 } 688 if (ge->v1.Notify.Type == ACPI_HEST_NOTIFY_POLLED) { 689 pges = &sc->pges[PGE_ID(ge)]; 690 TAILQ_INSERT_TAIL(&sc->pges[PGE_ID(ge)].ges, ge, nlink); 691 callout_reset_sbt(&pges->poll, pges->interval, pges->interval, 692 apei_callout_handler, pges, 0); 693 } else if (ge->v1.Notify.Type == ACPI_HEST_NOTIFY_SCI || 694 ge->v1.Notify.Type == ACPI_HEST_NOTIFY_GPIO || 695 ge->v1.Notify.Type == ACPI_HEST_NOTIFY_GSIV) { 696 TAILQ_INSERT_TAIL(&sc->iges.ges, ge, nlink); 697 } else if (ge->v1.Notify.Type == ACPI_HEST_NOTIFY_NMI) { 698 ge->copybuf = malloc(ge->v1.ErrorBlockLength, 699 M_DEVBUF, M_WAITOK | M_ZERO); 700 TAILQ_INSERT_TAIL(&sc->nges.ges, ge, nlink); 701 if (sc->nges.swi_ih == NULL) { 702 swi_add(&clk_intr_event, "apei", apei_nmi_swi, 703 &sc->nges, SWI_CLOCK, INTR_MPSAFE, 704 &sc->nges.swi_ih); 705 apei_nmi_nges = &sc->nges; 706 apei_nmi = apei_nmi_handler; 707 } 708 } 709 } 710 711 if (acpi_get_handle(dev) != NULL) { 712 AcpiInstallNotifyHandler(acpi_get_handle(dev), 713 ACPI_DEVICE_NOTIFY, apei_notify_handler, dev); 714 } 715 return (0); 716 } 717 718 static int 719 apei_detach(device_t dev) 720 { 721 struct apei_softc *sc = device_get_softc(dev); 722 struct apei_ge *ge; 723 724 apei_nmi = NULL; 725 apei_nmi_nges = NULL; 726 if (sc->nges.swi_ih != NULL) { 727 swi_remove(&sc->nges.swi_ih); 728 sc->nges.swi_ih = NULL; 729 } 730 if (acpi_get_handle(dev) != NULL) { 731 AcpiRemoveNotifyHandler(acpi_get_handle(dev), 732 ACPI_DEVICE_NOTIFY, apei_notify_handler); 733 } 734 for (int i = 0; i < nitems(sc->pges); i++) 735 callout_drain(&sc->pges[i].poll); 736 737 while ((ge = TAILQ_FIRST(&sc->ges)) != NULL) { 738 TAILQ_REMOVE(&sc->ges, ge, link); 739 if (ge->res) { 740 bus_release_resource(dev, ge->res_type, 741 ge->res_rid, ge->res); 742 } 743 if (ge->res2) { 744 bus_release_resource(dev, ge->res2_type, 745 ge->res2_rid, ge->res2); 746 } 747 if (ge->v1.Notify.Type == ACPI_HEST_NOTIFY_POLLED) { 748 TAILQ_REMOVE(&sc->pges[PGE_ID(ge)].ges, ge, nlink); 749 } else if (ge->v1.Notify.Type == ACPI_HEST_NOTIFY_SCI || 750 ge->v1.Notify.Type == ACPI_HEST_NOTIFY_GPIO || 751 ge->v1.Notify.Type == ACPI_HEST_NOTIFY_GSIV) { 752 TAILQ_REMOVE(&sc->iges.ges, ge, nlink); 753 } else if (ge->v1.Notify.Type == ACPI_HEST_NOTIFY_NMI) { 754 TAILQ_REMOVE(&sc->nges.ges, ge, nlink); 755 free(ge->copybuf, M_DEVBUF); 756 } 757 if (ge->buf) { 758 pmap_unmapdev((vm_offset_t)ge->buf, 759 ge->v1.ErrorBlockLength); 760 } 761 free(ge, M_DEVBUF); 762 } 763 return (0); 764 } 765 766 static device_method_t apei_methods[] = { 767 /* Device interface */ 768 DEVMETHOD(device_identify, apei_identify), 769 DEVMETHOD(device_probe, apei_probe), 770 DEVMETHOD(device_attach, apei_attach), 771 DEVMETHOD(device_detach, apei_detach), 772 DEVMETHOD_END 773 }; 774 775 static driver_t apei_driver = { 776 "apei", 777 apei_methods, 778 sizeof(struct apei_softc), 779 }; 780 781 DRIVER_MODULE(apei, acpi, apei_driver, 0, 0); 782 MODULE_DEPEND(apei, acpi, 1, 1, 1); 783