1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * APEI Error INJection support 4 * 5 * EINJ provides a hardware error injection mechanism, this is useful 6 * for debugging and testing of other APEI and RAS features. 7 * 8 * For more information about EINJ, please refer to ACPI Specification 9 * version 4.0, section 17.5. 10 * 11 * Copyright 2009-2010 Intel Corp. 12 * Author: Huang Ying <ying.huang@intel.com> 13 */ 14 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/init.h> 18 #include <linux/io.h> 19 #include <linux/debugfs.h> 20 #include <linux/seq_file.h> 21 #include <linux/nmi.h> 22 #include <linux/delay.h> 23 #include <linux/mm.h> 24 #include <linux/device/faux.h> 25 #include <linux/unaligned.h> 26 27 #include "apei-internal.h" 28 29 #undef pr_fmt 30 #define pr_fmt(fmt) "EINJ: " fmt 31 32 #define SLEEP_UNIT_MIN 1000 /* 1ms */ 33 #define SLEEP_UNIT_MAX 5000 /* 5ms */ 34 /* Firmware should respond within 1 seconds */ 35 #define FIRMWARE_TIMEOUT (1 * USEC_PER_SEC) 36 #define COMPONENT_LEN 16 37 #define ACPI65_EINJV2_SUPP BIT(30) 38 #define ACPI5_VENDOR_BIT BIT(31) 39 #define MEM_ERROR_MASK (ACPI_EINJ_MEMORY_CORRECTABLE | \ 40 ACPI_EINJ_MEMORY_UNCORRECTABLE | \ 41 ACPI_EINJ_MEMORY_FATAL) 42 #define CXL_ERROR_MASK (ACPI_EINJ_CXL_CACHE_CORRECTABLE | \ 43 ACPI_EINJ_CXL_CACHE_UNCORRECTABLE | \ 44 ACPI_EINJ_CXL_CACHE_FATAL | \ 45 ACPI_EINJ_CXL_MEM_CORRECTABLE | \ 46 ACPI_EINJ_CXL_MEM_UNCORRECTABLE | \ 47 ACPI_EINJ_CXL_MEM_FATAL) 48 49 /* 50 * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action. 51 */ 52 static int acpi5; 53 54 struct syndrome_array { 55 union { 56 u8 acpi_id[COMPONENT_LEN]; 57 u8 device_id[COMPONENT_LEN]; 58 u8 pcie_sbdf[COMPONENT_LEN]; 59 u8 vendor_id[COMPONENT_LEN]; 60 } comp_id; 61 union { 62 u8 proc_synd[COMPONENT_LEN]; 63 u8 mem_synd[COMPONENT_LEN]; 64 u8 pcie_synd[COMPONENT_LEN]; 65 u8 vendor_synd[COMPONENT_LEN]; 66 } comp_synd; 67 }; 68 69 struct einjv2_extension_struct { 70 u32 length; 71 u16 revision; 72 u16 component_arr_count; 73 struct syndrome_array component_arr[] __counted_by(component_arr_count); 74 }; 75 76 struct set_error_type_with_address { 77 u32 type; 78 u32 vendor_extension; 79 u32 flags; 80 u32 apicid; 81 u64 memory_address; 82 u64 memory_address_range; 83 u32 pcie_sbdf; 84 struct einjv2_extension_struct einjv2_struct; 85 }; 86 enum { 87 SETWA_FLAGS_APICID = 1, 88 SETWA_FLAGS_MEM = 2, 89 SETWA_FLAGS_PCIE_SBDF = 4, 90 SETWA_FLAGS_EINJV2 = 8, 91 }; 92 93 /* 94 * Vendor extensions for platform specific operations 95 */ 96 struct vendor_error_type_extension { 97 u32 length; 98 u32 pcie_sbdf; 99 u16 vendor_id; 100 u16 device_id; 101 u8 rev_id; 102 u8 reserved[3]; 103 }; 104 105 static u32 notrigger; 106 107 static u32 vendor_flags; 108 static struct debugfs_blob_wrapper vendor_blob; 109 static struct debugfs_blob_wrapper vendor_errors; 110 static char vendor_dev[64]; 111 112 static u32 max_nr_components; 113 static u32 available_error_type; 114 static u32 available_error_type_v2; 115 static struct syndrome_array *syndrome_data; 116 117 /* 118 * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the 119 * EINJ table through an unpublished extension. Use with caution as 120 * most will ignore the parameter and make their own choice of address 121 * for error injection. This extension is used only if 122 * param_extension module parameter is specified. 123 */ 124 struct einj_parameter { 125 u64 type; 126 u64 reserved1; 127 u64 reserved2; 128 u64 param1; 129 u64 param2; 130 }; 131 132 #define EINJ_OP_BUSY 0x1 133 #define EINJ_STATUS_SUCCESS 0x0 134 #define EINJ_STATUS_FAIL 0x1 135 #define EINJ_STATUS_INVAL 0x2 136 137 #define EINJ_TAB_ENTRY(tab) \ 138 ((struct acpi_whea_header *)((char *)(tab) + \ 139 sizeof(struct acpi_table_einj))) 140 141 static bool param_extension; 142 module_param(param_extension, bool, 0); 143 144 static struct acpi_table_einj *einj_tab; 145 146 static struct apei_resources einj_resources; 147 148 static struct apei_exec_ins_type einj_ins_type[] = { 149 [ACPI_EINJ_READ_REGISTER] = { 150 .flags = APEI_EXEC_INS_ACCESS_REGISTER, 151 .run = apei_exec_read_register, 152 }, 153 [ACPI_EINJ_READ_REGISTER_VALUE] = { 154 .flags = APEI_EXEC_INS_ACCESS_REGISTER, 155 .run = apei_exec_read_register_value, 156 }, 157 [ACPI_EINJ_WRITE_REGISTER] = { 158 .flags = APEI_EXEC_INS_ACCESS_REGISTER, 159 .run = apei_exec_write_register, 160 }, 161 [ACPI_EINJ_WRITE_REGISTER_VALUE] = { 162 .flags = APEI_EXEC_INS_ACCESS_REGISTER, 163 .run = apei_exec_write_register_value, 164 }, 165 [ACPI_EINJ_NOOP] = { 166 .flags = 0, 167 .run = apei_exec_noop, 168 }, 169 }; 170 171 /* 172 * Prevent EINJ interpreter to run simultaneously, because the 173 * corresponding firmware implementation may not work properly when 174 * invoked simultaneously. 175 */ 176 static DEFINE_MUTEX(einj_mutex); 177 178 /* 179 * Exported APIs use this flag to exit early if einj_probe() failed. 180 */ 181 bool einj_initialized __ro_after_init; 182 183 static void __iomem *einj_param; 184 static u32 v5param_size; 185 static bool is_v2; 186 187 static void einj_exec_ctx_init(struct apei_exec_context *ctx) 188 { 189 apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type), 190 EINJ_TAB_ENTRY(einj_tab), einj_tab->entries); 191 } 192 193 static int __einj_get_available_error_type(u32 *type, int einj_action) 194 { 195 struct apei_exec_context ctx; 196 int rc; 197 198 einj_exec_ctx_init(&ctx); 199 rc = apei_exec_run(&ctx, einj_action); 200 if (rc) 201 return rc; 202 *type = apei_exec_ctx_get_output(&ctx); 203 204 return 0; 205 } 206 207 /* Get error injection capabilities of the platform */ 208 int einj_get_available_error_type(u32 *type, int einj_action) 209 { 210 int rc; 211 212 mutex_lock(&einj_mutex); 213 rc = __einj_get_available_error_type(type, einj_action); 214 mutex_unlock(&einj_mutex); 215 216 return rc; 217 } 218 219 static int einj_get_available_error_types(u32 *type1, u32 *type2) 220 { 221 int rc; 222 223 rc = einj_get_available_error_type(type1, ACPI_EINJ_GET_ERROR_TYPE); 224 if (rc) 225 return rc; 226 if (*type1 & ACPI65_EINJV2_SUPP) { 227 rc = einj_get_available_error_type(type2, 228 ACPI_EINJV2_GET_ERROR_TYPE); 229 if (rc) 230 return rc; 231 } 232 233 return 0; 234 } 235 236 static int einj_timedout(u64 *t) 237 { 238 if ((s64)*t < SLEEP_UNIT_MIN) { 239 pr_warn(FW_WARN "Firmware does not respond in time\n"); 240 return 1; 241 } 242 *t -= SLEEP_UNIT_MIN; 243 usleep_range(SLEEP_UNIT_MIN, SLEEP_UNIT_MAX); 244 245 return 0; 246 } 247 248 static void get_oem_vendor_struct(u64 paddr, int offset, 249 struct vendor_error_type_extension *v) 250 { 251 unsigned long vendor_size; 252 u64 target_pa = paddr + offset + sizeof(struct vendor_error_type_extension); 253 254 vendor_size = v->length - sizeof(struct vendor_error_type_extension); 255 256 if (vendor_size) 257 vendor_errors.data = acpi_os_map_memory(target_pa, vendor_size); 258 259 if (vendor_errors.data) 260 vendor_errors.size = vendor_size; 261 } 262 263 static void check_vendor_extension(u64 paddr, 264 struct set_error_type_with_address *v5param) 265 { 266 int offset = v5param->vendor_extension; 267 struct vendor_error_type_extension v; 268 struct vendor_error_type_extension __iomem *p; 269 u32 sbdf; 270 271 if (!offset) 272 return; 273 p = acpi_os_map_iomem(paddr + offset, sizeof(*p)); 274 if (!p) 275 return; 276 memcpy_fromio(&v, p, sizeof(v)); 277 get_oem_vendor_struct(paddr, offset, &v); 278 sbdf = v.pcie_sbdf; 279 sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n", 280 sbdf >> 24, (sbdf >> 16) & 0xff, 281 (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7, 282 v.vendor_id, v.device_id, v.rev_id); 283 acpi_os_unmap_iomem(p, sizeof(v)); 284 } 285 286 static void __iomem *einj_get_parameter_address(void) 287 { 288 int i; 289 u64 pa_v4 = 0, pa_v5 = 0; 290 struct acpi_whea_header *entry; 291 292 entry = EINJ_TAB_ENTRY(einj_tab); 293 for (i = 0; i < einj_tab->entries; i++) { 294 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE && 295 entry->instruction == ACPI_EINJ_WRITE_REGISTER && 296 entry->register_region.space_id == 297 ACPI_ADR_SPACE_SYSTEM_MEMORY) 298 pa_v4 = get_unaligned(&entry->register_region.address); 299 if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS && 300 entry->instruction == ACPI_EINJ_WRITE_REGISTER && 301 entry->register_region.space_id == 302 ACPI_ADR_SPACE_SYSTEM_MEMORY) 303 pa_v5 = get_unaligned(&entry->register_region.address); 304 entry++; 305 } 306 if (pa_v5) { 307 struct set_error_type_with_address v5param; 308 struct set_error_type_with_address __iomem *p; 309 310 v5param_size = sizeof(v5param); 311 p = acpi_os_map_iomem(pa_v5, sizeof(*p)); 312 if (p) { 313 int offset, len; 314 315 memcpy_fromio(&v5param, p, v5param_size); 316 acpi5 = 1; 317 check_vendor_extension(pa_v5, &v5param); 318 if (available_error_type & ACPI65_EINJV2_SUPP) { 319 len = v5param.einjv2_struct.length; 320 offset = offsetof(struct einjv2_extension_struct, component_arr); 321 max_nr_components = (len - offset) / 322 sizeof(v5param.einjv2_struct.component_arr[0]); 323 /* 324 * The first call to acpi_os_map_iomem above does not include the 325 * component array, instead it is used to read and calculate maximum 326 * number of components supported by the system. Below, the mapping 327 * is expanded to include the component array. 328 */ 329 acpi_os_unmap_iomem(p, v5param_size); 330 offset = offsetof(struct set_error_type_with_address, einjv2_struct); 331 v5param_size = offset + struct_size(&v5param.einjv2_struct, 332 component_arr, max_nr_components); 333 p = acpi_os_map_iomem(pa_v5, v5param_size); 334 } 335 return p; 336 } 337 } 338 if (param_extension && pa_v4) { 339 struct einj_parameter v4param; 340 struct einj_parameter __iomem *p; 341 342 p = acpi_os_map_iomem(pa_v4, sizeof(*p)); 343 if (!p) 344 return NULL; 345 memcpy_fromio(&v4param, p, sizeof(v4param)); 346 if (v4param.reserved1 || v4param.reserved2) { 347 acpi_os_unmap_iomem(p, sizeof(v4param)); 348 return NULL; 349 } 350 return p; 351 } 352 353 return NULL; 354 } 355 356 /* do sanity check to trigger table */ 357 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab) 358 { 359 if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger)) 360 return -EINVAL; 361 if (trigger_tab->table_size > PAGE_SIZE || 362 trigger_tab->table_size < trigger_tab->header_size) 363 return -EINVAL; 364 if (trigger_tab->entry_count != 365 (trigger_tab->table_size - trigger_tab->header_size) / 366 sizeof(struct acpi_einj_entry)) 367 return -EINVAL; 368 369 return 0; 370 } 371 372 static struct acpi_generic_address *einj_get_trigger_parameter_region( 373 struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2) 374 { 375 int i; 376 struct acpi_whea_header *entry; 377 378 entry = (struct acpi_whea_header *) 379 ((char *)trigger_tab + sizeof(struct acpi_einj_trigger)); 380 for (i = 0; i < trigger_tab->entry_count; i++) { 381 if (entry->action == ACPI_EINJ_TRIGGER_ERROR && 382 entry->instruction <= ACPI_EINJ_WRITE_REGISTER_VALUE && 383 entry->register_region.space_id == 384 ACPI_ADR_SPACE_SYSTEM_MEMORY && 385 (entry->register_region.address & param2) == (param1 & param2)) 386 return &entry->register_region; 387 entry++; 388 } 389 390 return NULL; 391 } 392 /* Execute instructions in trigger error action table */ 393 static int __einj_error_trigger(u64 trigger_paddr, u32 type, 394 u64 param1, u64 param2) 395 { 396 struct acpi_einj_trigger trigger_tab; 397 struct acpi_einj_trigger *full_trigger_tab; 398 struct apei_exec_context trigger_ctx; 399 struct apei_resources trigger_resources; 400 struct acpi_whea_header *trigger_entry; 401 struct resource *r; 402 u32 table_size; 403 int rc = -EIO; 404 struct acpi_generic_address *trigger_param_region = NULL; 405 struct acpi_einj_trigger __iomem *p = NULL; 406 407 r = request_mem_region(trigger_paddr, sizeof(trigger_tab), 408 "APEI EINJ Trigger Table"); 409 if (!r) { 410 pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n", 411 (unsigned long long)trigger_paddr, 412 (unsigned long long)trigger_paddr + 413 sizeof(trigger_tab) - 1); 414 goto out; 415 } 416 p = ioremap_cache(trigger_paddr, sizeof(*p)); 417 if (!p) { 418 pr_err("Failed to map trigger table!\n"); 419 goto out_rel_header; 420 } 421 memcpy_fromio(&trigger_tab, p, sizeof(trigger_tab)); 422 rc = einj_check_trigger_header(&trigger_tab); 423 if (rc) { 424 pr_warn(FW_BUG "Invalid trigger error action table.\n"); 425 goto out_rel_header; 426 } 427 428 /* No action structures in the TRIGGER_ERROR table, nothing to do */ 429 if (!trigger_tab.entry_count) 430 goto out_rel_header; 431 432 rc = -EIO; 433 table_size = trigger_tab.table_size; 434 full_trigger_tab = kmalloc(table_size, GFP_KERNEL); 435 if (!full_trigger_tab) 436 goto out_rel_header; 437 r = request_mem_region(trigger_paddr + sizeof(trigger_tab), 438 table_size - sizeof(trigger_tab), 439 "APEI EINJ Trigger Table"); 440 if (!r) { 441 pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n", 442 (unsigned long long)trigger_paddr + sizeof(trigger_tab), 443 (unsigned long long)trigger_paddr + table_size - 1); 444 goto out_free_trigger_tab; 445 } 446 iounmap(p); 447 p = ioremap_cache(trigger_paddr, table_size); 448 if (!p) { 449 pr_err("Failed to map trigger table!\n"); 450 goto out_rel_entry; 451 } 452 memcpy_fromio(full_trigger_tab, p, table_size); 453 trigger_entry = (struct acpi_whea_header *) 454 ((char *)full_trigger_tab + sizeof(struct acpi_einj_trigger)); 455 apei_resources_init(&trigger_resources); 456 apei_exec_ctx_init(&trigger_ctx, einj_ins_type, 457 ARRAY_SIZE(einj_ins_type), 458 trigger_entry, trigger_tab.entry_count); 459 rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources); 460 if (rc) 461 goto out_fini; 462 rc = apei_resources_sub(&trigger_resources, &einj_resources); 463 if (rc) 464 goto out_fini; 465 /* 466 * Some firmware will access target address specified in 467 * param1 to trigger the error when injecting memory error. 468 * This will cause resource conflict with regular memory. So 469 * remove it from trigger table resources. 470 */ 471 if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) { 472 struct apei_resources addr_resources; 473 474 apei_resources_init(&addr_resources); 475 trigger_param_region = einj_get_trigger_parameter_region( 476 full_trigger_tab, param1, param2); 477 if (trigger_param_region) { 478 rc = apei_resources_add(&addr_resources, 479 trigger_param_region->address, 480 trigger_param_region->bit_width/8, true); 481 if (rc) 482 goto out_fini; 483 rc = apei_resources_sub(&trigger_resources, 484 &addr_resources); 485 } 486 apei_resources_fini(&addr_resources); 487 if (rc) 488 goto out_fini; 489 } 490 rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger"); 491 if (rc) 492 goto out_fini; 493 rc = apei_exec_pre_map_gars(&trigger_ctx); 494 if (rc) 495 goto out_release; 496 497 rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR); 498 499 apei_exec_post_unmap_gars(&trigger_ctx); 500 out_release: 501 apei_resources_release(&trigger_resources); 502 out_fini: 503 apei_resources_fini(&trigger_resources); 504 out_rel_entry: 505 release_mem_region(trigger_paddr + sizeof(trigger_tab), 506 table_size - sizeof(trigger_tab)); 507 out_free_trigger_tab: 508 kfree(full_trigger_tab); 509 out_rel_header: 510 release_mem_region(trigger_paddr, sizeof(trigger_tab)); 511 out: 512 if (p) 513 iounmap(p); 514 515 return rc; 516 } 517 518 static bool is_end_of_list(u8 *val) 519 { 520 for (int i = 0; i < COMPONENT_LEN; ++i) { 521 if (val[i] != 0xFF) 522 return false; 523 } 524 return true; 525 } 526 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2, 527 u64 param3, u64 param4) 528 { 529 struct apei_exec_context ctx; 530 u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT; 531 int i, rc; 532 533 einj_exec_ctx_init(&ctx); 534 535 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION); 536 if (rc) 537 return rc; 538 apei_exec_ctx_set_input(&ctx, type); 539 if (acpi5) { 540 struct set_error_type_with_address *v5param; 541 542 v5param = kmalloc(v5param_size, GFP_KERNEL); 543 memcpy_fromio(v5param, einj_param, v5param_size); 544 v5param->type = type; 545 if (type & ACPI5_VENDOR_BIT) { 546 switch (vendor_flags) { 547 case SETWA_FLAGS_APICID: 548 v5param->apicid = param1; 549 break; 550 case SETWA_FLAGS_MEM: 551 v5param->memory_address = param1; 552 v5param->memory_address_range = param2; 553 break; 554 case SETWA_FLAGS_PCIE_SBDF: 555 v5param->pcie_sbdf = param1; 556 break; 557 } 558 v5param->flags = vendor_flags; 559 } else if (flags) { 560 v5param->flags = flags; 561 v5param->memory_address = param1; 562 v5param->memory_address_range = param2; 563 564 if (is_v2) { 565 for (i = 0; i < max_nr_components; i++) { 566 if (is_end_of_list(syndrome_data[i].comp_id.acpi_id)) 567 break; 568 v5param->einjv2_struct.component_arr[i].comp_id = 569 syndrome_data[i].comp_id; 570 v5param->einjv2_struct.component_arr[i].comp_synd = 571 syndrome_data[i].comp_synd; 572 } 573 v5param->einjv2_struct.component_arr_count = i; 574 } else { 575 v5param->apicid = param3; 576 v5param->pcie_sbdf = param4; 577 } 578 } else { 579 switch (type) { 580 case ACPI_EINJ_PROCESSOR_CORRECTABLE: 581 case ACPI_EINJ_PROCESSOR_UNCORRECTABLE: 582 case ACPI_EINJ_PROCESSOR_FATAL: 583 v5param->apicid = param1; 584 v5param->flags = SETWA_FLAGS_APICID; 585 break; 586 case ACPI_EINJ_MEMORY_CORRECTABLE: 587 case ACPI_EINJ_MEMORY_UNCORRECTABLE: 588 case ACPI_EINJ_MEMORY_FATAL: 589 v5param->memory_address = param1; 590 v5param->memory_address_range = param2; 591 v5param->flags = SETWA_FLAGS_MEM; 592 break; 593 case ACPI_EINJ_PCIX_CORRECTABLE: 594 case ACPI_EINJ_PCIX_UNCORRECTABLE: 595 case ACPI_EINJ_PCIX_FATAL: 596 v5param->pcie_sbdf = param1; 597 v5param->flags = SETWA_FLAGS_PCIE_SBDF; 598 break; 599 } 600 } 601 memcpy_toio(einj_param, v5param, v5param_size); 602 kfree(v5param); 603 } else { 604 rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE); 605 if (rc) 606 return rc; 607 if (einj_param) { 608 struct einj_parameter v4param; 609 610 memcpy_fromio(&v4param, einj_param, sizeof(v4param)); 611 v4param.param1 = param1; 612 v4param.param2 = param2; 613 memcpy_toio(einj_param, &v4param, sizeof(v4param)); 614 } 615 } 616 rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION); 617 if (rc) 618 return rc; 619 for (;;) { 620 rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS); 621 if (rc) 622 return rc; 623 val = apei_exec_ctx_get_output(&ctx); 624 if (!(val & EINJ_OP_BUSY)) 625 break; 626 if (einj_timedout(&timeout)) 627 return -EIO; 628 } 629 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS); 630 if (rc) 631 return rc; 632 val = apei_exec_ctx_get_output(&ctx); 633 if (val == EINJ_STATUS_FAIL) 634 return -EBUSY; 635 else if (val == EINJ_STATUS_INVAL) 636 return -EINVAL; 637 638 /* 639 * The error is injected into the platform successfully, then it needs 640 * to trigger the error. 641 */ 642 rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE); 643 if (rc) 644 return rc; 645 trigger_paddr = apei_exec_ctx_get_output(&ctx); 646 if (notrigger == 0) { 647 rc = __einj_error_trigger(trigger_paddr, type, param1, param2); 648 if (rc) 649 return rc; 650 } 651 rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION); 652 653 return rc; 654 } 655 656 /* Inject the specified hardware error */ 657 int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2, u64 param3, 658 u64 param4) 659 { 660 int rc; 661 u64 base_addr, size; 662 663 /* If user manually set "flags", make sure it is legal */ 664 if (flags && (flags & ~(SETWA_FLAGS_APICID | SETWA_FLAGS_MEM | 665 SETWA_FLAGS_PCIE_SBDF | SETWA_FLAGS_EINJV2))) 666 return -EINVAL; 667 668 /* check if type is a valid EINJv2 error type */ 669 if (is_v2) { 670 if (!(type & available_error_type_v2)) 671 return -EINVAL; 672 } 673 /* 674 * We need extra sanity checks for memory errors. 675 * Other types leap directly to injection. 676 */ 677 678 /* ensure param1/param2 existed */ 679 if (!(param_extension || acpi5)) 680 goto inject; 681 682 /* ensure injection is memory related */ 683 if (type & ACPI5_VENDOR_BIT) { 684 if (vendor_flags != SETWA_FLAGS_MEM) 685 goto inject; 686 } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM)) { 687 goto inject; 688 } 689 690 /* 691 * Injections targeting a CXL 1.0/1.1 port have to be injected 692 * via the einj_cxl_rch_error_inject() path as that does the proper 693 * validation of the given RCRB base (MMIO) address. 694 */ 695 if (einj_is_cxl_error_type(type) && (flags & SETWA_FLAGS_MEM)) 696 return -EINVAL; 697 698 /* 699 * Disallow crazy address masks that give BIOS leeway to pick 700 * injection address almost anywhere. Insist on page or 701 * better granularity and that target address is normal RAM or 702 * NVDIMM. 703 */ 704 base_addr = param1 & param2; 705 size = ~param2 + 1; 706 707 if (((param2 & PAGE_MASK) != PAGE_MASK) || 708 ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE) 709 != REGION_INTERSECTS) && 710 (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY) 711 != REGION_INTERSECTS) && 712 (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_SOFT_RESERVED) 713 != REGION_INTERSECTS) && 714 !arch_is_platform_page(base_addr))) 715 return -EINVAL; 716 717 if (is_zero_pfn(base_addr >> PAGE_SHIFT)) 718 return -EADDRINUSE; 719 720 inject: 721 mutex_lock(&einj_mutex); 722 rc = __einj_error_inject(type, flags, param1, param2, param3, param4); 723 mutex_unlock(&einj_mutex); 724 725 return rc; 726 } 727 728 int einj_cxl_rch_error_inject(u32 type, u32 flags, u64 param1, u64 param2, 729 u64 param3, u64 param4) 730 { 731 int rc; 732 733 if (!(einj_is_cxl_error_type(type) && (flags & SETWA_FLAGS_MEM))) 734 return -EINVAL; 735 736 mutex_lock(&einj_mutex); 737 rc = __einj_error_inject(type, flags, param1, param2, param3, param4); 738 mutex_unlock(&einj_mutex); 739 740 return rc; 741 } 742 743 static u32 error_type; 744 static u32 error_flags; 745 static u64 error_param1; 746 static u64 error_param2; 747 static u64 error_param3; 748 static u64 error_param4; 749 static struct dentry *einj_debug_dir; 750 static char einj_buf[32]; 751 static bool einj_v2_enabled; 752 static struct { u32 mask; const char *str; } const einj_error_type_string[] = { 753 { BIT(0), "Processor Correctable" }, 754 { BIT(1), "Processor Uncorrectable non-fatal" }, 755 { BIT(2), "Processor Uncorrectable fatal" }, 756 { BIT(3), "Memory Correctable" }, 757 { BIT(4), "Memory Uncorrectable non-fatal" }, 758 { BIT(5), "Memory Uncorrectable fatal" }, 759 { BIT(6), "PCI Express Correctable" }, 760 { BIT(7), "PCI Express Uncorrectable non-fatal" }, 761 { BIT(8), "PCI Express Uncorrectable fatal" }, 762 { BIT(9), "Platform Correctable" }, 763 { BIT(10), "Platform Uncorrectable non-fatal" }, 764 { BIT(11), "Platform Uncorrectable fatal"}, 765 { BIT(31), "Vendor Defined Error Types" }, 766 }; 767 768 static struct { u32 mask; const char *str; } const einjv2_error_type_string[] = { 769 { BIT(0), "EINJV2 Processor Error" }, 770 { BIT(1), "EINJV2 Memory Error" }, 771 { BIT(2), "EINJV2 PCI Express Error" }, 772 }; 773 774 static int available_error_type_show(struct seq_file *m, void *v) 775 { 776 777 for (int pos = 0; pos < ARRAY_SIZE(einj_error_type_string); pos++) 778 if (available_error_type & einj_error_type_string[pos].mask) 779 seq_printf(m, "0x%08x\t%s\n", einj_error_type_string[pos].mask, 780 einj_error_type_string[pos].str); 781 if ((available_error_type & ACPI65_EINJV2_SUPP) && einj_v2_enabled) { 782 for (int pos = 0; pos < ARRAY_SIZE(einjv2_error_type_string); pos++) { 783 if (available_error_type_v2 & einjv2_error_type_string[pos].mask) 784 seq_printf(m, "V2_0x%08x\t%s\n", einjv2_error_type_string[pos].mask, 785 einjv2_error_type_string[pos].str); 786 } 787 } 788 return 0; 789 } 790 791 DEFINE_SHOW_ATTRIBUTE(available_error_type); 792 793 static ssize_t error_type_get(struct file *file, char __user *buf, 794 size_t count, loff_t *ppos) 795 { 796 return simple_read_from_buffer(buf, count, ppos, einj_buf, strlen(einj_buf)); 797 } 798 799 bool einj_is_cxl_error_type(u64 type) 800 { 801 return (type & CXL_ERROR_MASK) && (!(type & ACPI5_VENDOR_BIT)); 802 } 803 804 int einj_validate_error_type(u64 type) 805 { 806 u32 tval, vendor; 807 808 /* Only low 32 bits for error type are valid */ 809 if (type & GENMASK_ULL(63, 32)) 810 return -EINVAL; 811 812 /* 813 * Vendor defined types have 0x80000000 bit set, and 814 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE 815 */ 816 vendor = type & ACPI5_VENDOR_BIT; 817 tval = type & GENMASK(30, 0); 818 819 /* Only one error type can be specified */ 820 if (tval & (tval - 1)) 821 return -EINVAL; 822 if (!vendor) 823 if (!(type & (available_error_type | available_error_type_v2))) 824 return -EINVAL; 825 826 return 0; 827 } 828 829 static ssize_t error_type_set(struct file *file, const char __user *buf, 830 size_t count, loff_t *ppos) 831 { 832 int rc; 833 u64 val; 834 835 /* Leave the last character for the NUL terminator */ 836 if (count > sizeof(einj_buf) - 1) 837 return -EINVAL; 838 839 memset(einj_buf, 0, sizeof(einj_buf)); 840 if (copy_from_user(einj_buf, buf, count)) 841 return -EFAULT; 842 843 if (strncmp(einj_buf, "V2_", 3) == 0) { 844 if (!sscanf(einj_buf, "V2_%llx", &val)) 845 return -EINVAL; 846 is_v2 = true; 847 } else { 848 if (!sscanf(einj_buf, "%llx", &val)) 849 return -EINVAL; 850 is_v2 = false; 851 } 852 853 rc = einj_validate_error_type(val); 854 if (rc) 855 return rc; 856 857 error_type = val; 858 859 return count; 860 } 861 862 static const struct file_operations error_type_fops = { 863 .read = error_type_get, 864 .write = error_type_set, 865 }; 866 867 static int error_inject_set(void *data, u64 val) 868 { 869 if (!error_type) 870 return -EINVAL; 871 872 if (is_v2) 873 error_flags |= SETWA_FLAGS_EINJV2; 874 else 875 error_flags &= ~SETWA_FLAGS_EINJV2; 876 877 return einj_error_inject(error_type, error_flags, error_param1, error_param2, 878 error_param3, error_param4); 879 } 880 881 DEFINE_DEBUGFS_ATTRIBUTE(error_inject_fops, NULL, error_inject_set, "%llu\n"); 882 883 static int einj_check_table(struct acpi_table_einj *einj_tab) 884 { 885 if ((einj_tab->header_length != 886 (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header))) 887 && (einj_tab->header_length != sizeof(struct acpi_table_einj))) 888 return -EINVAL; 889 if (einj_tab->header.length < sizeof(struct acpi_table_einj)) 890 return -EINVAL; 891 if (einj_tab->entries != 892 (einj_tab->header.length - sizeof(struct acpi_table_einj)) / 893 sizeof(struct acpi_einj_entry)) 894 return -EINVAL; 895 896 return 0; 897 } 898 899 static ssize_t u128_read(struct file *f, char __user *buf, size_t count, loff_t *off) 900 { 901 char output[2 * COMPONENT_LEN + 1]; 902 u8 *data = f->f_inode->i_private; 903 int i; 904 905 if (*off >= sizeof(output)) 906 return 0; 907 908 for (i = 0; i < COMPONENT_LEN; i++) 909 sprintf(output + 2 * i, "%.02x", data[COMPONENT_LEN - i - 1]); 910 output[2 * COMPONENT_LEN] = '\n'; 911 912 return simple_read_from_buffer(buf, count, off, output, sizeof(output)); 913 } 914 915 static ssize_t u128_write(struct file *f, const char __user *buf, size_t count, loff_t *off) 916 { 917 char input[2 + 2 * COMPONENT_LEN + 2]; 918 u8 *save = f->f_inode->i_private; 919 u8 tmp[COMPONENT_LEN]; 920 char byte[3] = {}; 921 char *s, *e; 922 ssize_t c; 923 long val; 924 int i; 925 926 /* Require that user supply whole input line in one write(2) syscall */ 927 if (*off) 928 return -EINVAL; 929 930 c = simple_write_to_buffer(input, sizeof(input), off, buf, count); 931 if (c < 0) 932 return c; 933 934 if (c < 1 || input[c - 1] != '\n') 935 return -EINVAL; 936 937 /* Empty line means invalidate this entry */ 938 if (c == 1) { 939 memset(save, 0xff, COMPONENT_LEN); 940 return c; 941 } 942 943 if (input[0] == '0' && (input[1] == 'x' || input[1] == 'X')) 944 s = input + 2; 945 else 946 s = input; 947 e = input + c - 1; 948 949 for (i = 0; i < COMPONENT_LEN; i++) { 950 byte[1] = *--e; 951 byte[0] = e > s ? *--e : '0'; 952 if (kstrtol(byte, 16, &val)) 953 return -EINVAL; 954 tmp[i] = val; 955 if (e <= s) 956 break; 957 } 958 while (++i < COMPONENT_LEN) 959 tmp[i] = 0; 960 961 memcpy(save, tmp, COMPONENT_LEN); 962 963 return c; 964 } 965 966 static const struct file_operations u128_fops = { 967 .read = u128_read, 968 .write = u128_write, 969 }; 970 971 static bool setup_einjv2_component_files(void) 972 { 973 char name[32]; 974 975 syndrome_data = kcalloc(max_nr_components, sizeof(syndrome_data[0]), GFP_KERNEL); 976 if (!syndrome_data) 977 return false; 978 979 for (int i = 0; i < max_nr_components; i++) { 980 sprintf(name, "component_id%d", i); 981 debugfs_create_file(name, 0600, einj_debug_dir, 982 &syndrome_data[i].comp_id, &u128_fops); 983 sprintf(name, "component_syndrome%d", i); 984 debugfs_create_file(name, 0600, einj_debug_dir, 985 &syndrome_data[i].comp_synd, &u128_fops); 986 } 987 988 return true; 989 } 990 991 static int __init einj_probe(struct faux_device *fdev) 992 { 993 int rc; 994 acpi_status status; 995 struct apei_exec_context ctx; 996 997 status = acpi_get_table(ACPI_SIG_EINJ, 0, 998 (struct acpi_table_header **)&einj_tab); 999 if (status == AE_NOT_FOUND) { 1000 pr_debug("EINJ table not found.\n"); 1001 return -ENODEV; 1002 } else if (ACPI_FAILURE(status)) { 1003 pr_err("Failed to get EINJ table: %s\n", 1004 acpi_format_exception(status)); 1005 return -EINVAL; 1006 } 1007 1008 rc = einj_check_table(einj_tab); 1009 if (rc) { 1010 pr_warn(FW_BUG "Invalid EINJ table.\n"); 1011 goto err_put_table; 1012 } 1013 1014 rc = einj_get_available_error_types(&available_error_type, &available_error_type_v2); 1015 if (rc) 1016 goto err_put_table; 1017 1018 rc = -ENOMEM; 1019 einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir()); 1020 1021 debugfs_create_file("available_error_type", S_IRUSR, einj_debug_dir, 1022 NULL, &available_error_type_fops); 1023 debugfs_create_file_unsafe("error_type", 0600, einj_debug_dir, 1024 NULL, &error_type_fops); 1025 debugfs_create_file_unsafe("error_inject", 0200, einj_debug_dir, 1026 NULL, &error_inject_fops); 1027 1028 apei_resources_init(&einj_resources); 1029 einj_exec_ctx_init(&ctx); 1030 rc = apei_exec_collect_resources(&ctx, &einj_resources); 1031 if (rc) { 1032 pr_err("Error collecting EINJ resources.\n"); 1033 goto err_fini; 1034 } 1035 1036 rc = apei_resources_request(&einj_resources, "APEI EINJ"); 1037 if (rc) { 1038 pr_err("Error requesting memory/port resources.\n"); 1039 goto err_fini; 1040 } 1041 1042 rc = apei_exec_pre_map_gars(&ctx); 1043 if (rc) { 1044 pr_err("Error pre-mapping GARs.\n"); 1045 goto err_release; 1046 } 1047 1048 einj_param = einj_get_parameter_address(); 1049 if ((param_extension || acpi5) && einj_param) { 1050 debugfs_create_x32("flags", S_IRUSR | S_IWUSR, einj_debug_dir, 1051 &error_flags); 1052 debugfs_create_x64("param1", S_IRUSR | S_IWUSR, einj_debug_dir, 1053 &error_param1); 1054 debugfs_create_x64("param2", S_IRUSR | S_IWUSR, einj_debug_dir, 1055 &error_param2); 1056 debugfs_create_x64("param3", S_IRUSR | S_IWUSR, einj_debug_dir, 1057 &error_param3); 1058 debugfs_create_x64("param4", S_IRUSR | S_IWUSR, einj_debug_dir, 1059 &error_param4); 1060 debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR, 1061 einj_debug_dir, ¬rigger); 1062 if (available_error_type & ACPI65_EINJV2_SUPP) 1063 einj_v2_enabled = setup_einjv2_component_files(); 1064 } 1065 1066 if (vendor_dev[0]) { 1067 vendor_blob.data = vendor_dev; 1068 vendor_blob.size = strlen(vendor_dev); 1069 debugfs_create_blob("vendor", S_IRUSR, einj_debug_dir, 1070 &vendor_blob); 1071 debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR, 1072 einj_debug_dir, &vendor_flags); 1073 } 1074 1075 if (vendor_errors.size) 1076 debugfs_create_blob("oem_error", 0600, einj_debug_dir, 1077 &vendor_errors); 1078 1079 pr_info("Error INJection is initialized.\n"); 1080 1081 return 0; 1082 1083 err_release: 1084 apei_resources_release(&einj_resources); 1085 err_fini: 1086 apei_resources_fini(&einj_resources); 1087 debugfs_remove_recursive(einj_debug_dir); 1088 err_put_table: 1089 acpi_put_table((struct acpi_table_header *)einj_tab); 1090 1091 return rc; 1092 } 1093 1094 static void __exit einj_remove(struct faux_device *fdev) 1095 { 1096 struct apei_exec_context ctx; 1097 1098 if (einj_param) { 1099 acpi_size size = (acpi5) ? 1100 v5param_size : 1101 sizeof(struct einj_parameter); 1102 1103 acpi_os_unmap_iomem(einj_param, size); 1104 if (vendor_errors.size) 1105 acpi_os_unmap_memory(vendor_errors.data, vendor_errors.size); 1106 } 1107 einj_exec_ctx_init(&ctx); 1108 apei_exec_post_unmap_gars(&ctx); 1109 apei_resources_release(&einj_resources); 1110 apei_resources_fini(&einj_resources); 1111 debugfs_remove_recursive(einj_debug_dir); 1112 kfree(syndrome_data); 1113 acpi_put_table((struct acpi_table_header *)einj_tab); 1114 } 1115 1116 static struct faux_device *einj_dev; 1117 /* 1118 * einj_remove() lives in .exit.text. For drivers registered via 1119 * platform_driver_probe() this is ok because they cannot get unbound at 1120 * runtime. So mark the driver struct with __refdata to prevent modpost 1121 * triggering a section mismatch warning. 1122 */ 1123 static struct faux_device_ops einj_device_ops __refdata = { 1124 .probe = einj_probe, 1125 .remove = __exit_p(einj_remove), 1126 }; 1127 1128 static int __init einj_init(void) 1129 { 1130 if (acpi_disabled) { 1131 pr_debug("ACPI disabled.\n"); 1132 return -ENODEV; 1133 } 1134 1135 einj_dev = faux_device_create("acpi-einj", NULL, &einj_device_ops); 1136 1137 if (einj_dev) 1138 einj_initialized = true; 1139 1140 return 0; 1141 } 1142 1143 static void __exit einj_exit(void) 1144 { 1145 faux_device_destroy(einj_dev); 1146 } 1147 1148 module_init(einj_init); 1149 module_exit(einj_exit); 1150 1151 MODULE_AUTHOR("Huang Ying"); 1152 MODULE_DESCRIPTION("APEI Error INJection support"); 1153 MODULE_LICENSE("GPL"); 1154