1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright 2026 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 */ 24 25 #include "ras.h" 26 27 #if defined(CONFIG_X86_MCE_AMD) && defined(CONFIG_ACPI_APEI) 28 #define RAS_BERT_ERR_INFO_COUNT(bits) (((bits) & GENMASK_ULL(7, 2)) >> 2) 29 #define RAS_BERT_CONTEXT_COUNT(bits) (((bits) & GENMASK_ULL(13, 8)) >> 8) 30 #define CPER_MEM_VALID_NODE 0x0008 31 #define CPER_MEM_VALID_ERROR_STATUS 0x0001 32 #define CPER_MEM_VALID_PA 0x0002 33 #define CPER_MEM_VALID_PA_MASK 0x0004 34 #define CPER_MEM_VALID_REQUESTOR_ID 0x0800 35 #define CPER_MEM_VALID_RESPONDER_ID 0x1000 36 #define CPER_MEM_VALID_TARGET_ID 0x2000 37 #define CPER_MEM_VALID_ERROR_TYPE 0x4000 38 #define AMD_CPER_BODY_VALID_MASK GENMASK_ULL(15, 1) 39 #define RAS_BERT_VALID_FRU_STRING BIT(1) 40 #define RAS_BERT_PROC_CTX_TYPE_MSR 1 41 42 struct ras_bert_hest_generic_status { 43 u32 block_status; 44 u32 raw_data_offset; 45 u32 raw_data_length; 46 u32 data_length; 47 u32 error_severity; 48 }; 49 50 struct ras_bert_hest_generic_data { 51 u8 section_type[16]; 52 u32 error_severity; 53 u16 revision; 54 u8 validation_bits; 55 u8 flags; 56 u32 error_data_length; 57 u8 fru_id[16]; 58 u8 fru_text[20]; 59 }; 60 61 struct ras_bert_hest_generic_data_v300 { 62 u8 section_type[16]; 63 u32 error_severity; 64 u16 revision; 65 u8 validation_bits; 66 u8 flags; 67 u32 error_data_length; 68 u8 fru_id[16]; 69 u8 fru_text[20]; 70 u64 time_stamp; 71 }; 72 73 struct ras_bert_sec_proc_ia { 74 u64 validation_bits; 75 u64 lapic_id; 76 u8 cpuid[48]; 77 }; 78 79 struct ras_bert_ia_err_info { 80 u8 err_type[16]; 81 u64 validation_bits; 82 u64 check_info; 83 u64 target_id; 84 u64 requestor_id; 85 u64 responder_id; 86 u64 ip; 87 }; 88 89 struct ras_bert_ia_proc_ctx { 90 u16 reg_ctx_type; 91 u16 reg_arr_size; 92 u32 msr_addr; 93 u64 mm_reg_addr; 94 }; 95 96 struct ras_bert_sec_mem_err_old { 97 u64 validation_bits; 98 u64 error_status; 99 u64 physical_addr; 100 u64 physical_addr_mask; 101 u16 node; 102 u16 card; 103 u16 module; 104 u16 bank; 105 u16 device; 106 u16 row; 107 u16 column; 108 u16 bit_pos; 109 u64 requestor_id; 110 u64 responder_id; 111 u64 target_id; 112 u8 error_type; 113 } __packed; 114 115 struct ras_bert_sec_mem_err { 116 u64 validation_bits; 117 u64 error_status; 118 u64 physical_addr; 119 u64 physical_addr_mask; 120 u16 node; 121 u16 card; 122 u16 module; 123 u16 bank; 124 u16 device; 125 u16 row; 126 u16 column; 127 u16 bit_pos; 128 u64 requestor_id; 129 u64 responder_id; 130 u64 target_id; 131 u8 error_type; 132 u8 extended; 133 u16 rank; 134 u16 mem_array_handle; 135 u16 mem_dev_handle; 136 } __packed; 137 138 static inline int ras_bert_get_version(struct ras_bert_hest_generic_data *gdata) 139 { 140 return gdata->revision >> 8; 141 } 142 143 static inline void *ras_bert_get_payload(struct ras_bert_hest_generic_data *gdata) 144 { 145 if (ras_bert_get_version(gdata) >= 3) 146 return (void *)(((struct ras_bert_hest_generic_data_v300 *)(gdata)) + 1); 147 148 return gdata + 1; 149 } 150 151 static inline int ras_bert_get_size(struct ras_bert_hest_generic_data *gdata) 152 { 153 if (ras_bert_get_version(gdata) >= 3) 154 return sizeof(struct ras_bert_hest_generic_data_v300); 155 156 return sizeof(struct ras_bert_hest_generic_data); 157 } 158 159 static inline void *ras_bert_get_next(struct ras_bert_hest_generic_data *gdata) 160 { 161 return (void *)gdata + ras_bert_get_size(gdata) + gdata->error_data_length; 162 } 163 164 static int ras_bert_estatus_check(const struct ras_bert_hest_generic_status *estatus) 165 { 166 struct ras_bert_hest_generic_data *gdata; 167 unsigned int data_len, record_size; 168 169 if (estatus->data_length && 170 estatus->data_length < sizeof(struct ras_bert_hest_generic_data)) 171 return -EINVAL; 172 if (estatus->raw_data_length && 173 estatus->raw_data_offset < sizeof(*estatus) + estatus->data_length) 174 return -EINVAL; 175 176 data_len = estatus->data_length; 177 gdata = (struct ras_bert_hest_generic_data *)(estatus + 1); 178 while ((void *)gdata - (void *)(estatus + 1) < estatus->data_length) { 179 if (ras_bert_get_size(gdata) > data_len) 180 return -EINVAL; 181 182 record_size = ras_bert_get_size(gdata) + gdata->error_data_length; 183 if (record_size > data_len) 184 return -EINVAL; 185 186 data_len -= record_size; 187 gdata = ras_bert_get_next(gdata); 188 } 189 if (data_len) 190 return -EINVAL; 191 192 return 0; 193 } 194 195 typedef int (*ras_bert_section_parser)(struct ras_core_context *ras_core, 196 struct ras_bert_hest_generic_data *gdata); 197 198 struct ras_bert_section_dispatch { 199 const u8 *section_type; 200 ras_bert_section_parser parser; 201 }; 202 203 static const struct ras_cper_guid ras_bert_guid_crashdump = GPU__CRASHDUMP; 204 static const struct ras_cper_guid ras_bert_guid_proc_ia = PROC_ERR__SECTION_TYPE; 205 static const struct ras_cper_guid ras_bert_guid_platform_mem = PLATFORM_MEM__SECTION_TYPE; 206 static const struct ras_cper_guid ras_bert_guid_pcie = PCIE_ERR__SECTION_TYPE; 207 static const struct ras_cper_guid ras_bert_guid_smn = SMN_ERR__SECTION_TYPE; 208 209 static u64 ras_bert_read_le64(const u8 *data) 210 { 211 u64 value = 0; 212 size_t i; 213 214 for (i = 0; i < sizeof(value); i++) 215 value |= (u64)data[i] << (i * 8); 216 217 return value; 218 } 219 220 static void ras_bert_fill_boot_ctx(struct ras_boot_err_ctx *ctx, 221 struct ras_bert_hest_generic_data *gdata) 222 { 223 memcpy(ctx->section_type, gdata->section_type, sizeof(ctx->section_type)); 224 ctx->error_severity = gdata->error_severity; 225 } 226 227 static bool ras_bert_amd_body_valid(const u8 *body, u32 body_len) 228 { 229 u64 valid_bits; 230 u32 context_count; 231 u32 error_count; 232 u32 min_size; 233 234 if (body_len < sizeof(struct crashdump_hdr)) 235 return false; 236 237 valid_bits = ras_bert_read_le64(body); 238 if (valid_bits & ~AMD_CPER_BODY_VALID_MASK) 239 return false; 240 error_count = RAS_BERT_ERR_INFO_COUNT(valid_bits); 241 context_count = RAS_BERT_CONTEXT_COUNT(valid_bits); 242 if (!error_count && !context_count) 243 return false; 244 245 min_size = sizeof(struct crashdump_hdr) + 246 (error_count * sizeof(struct crashdump_error_info)) + 247 (context_count * sizeof(struct ras_bert_ia_proc_ctx)); 248 249 return min_size <= body_len; 250 } 251 252 static bool ras_bert_fru_matches_device(struct ras_core_context *ras_core, 253 struct ras_bert_hest_generic_data *gdata) 254 { 255 struct device_system_info dev_info = { 0 }; 256 char fru_text[sizeof(gdata->fru_text) + 1] = { 0 }; 257 unsigned int socket_id; 258 259 if (!(gdata->validation_bits & RAS_BERT_VALID_FRU_STRING)) 260 return false; 261 if (ras_core_get_device_system_info(ras_core, &dev_info)) 262 return false; 263 264 memcpy(fru_text, gdata->fru_text, sizeof(gdata->fru_text)); 265 if (sscanf(fru_text, "OAM%u", &socket_id) != 1 && 266 sscanf(fru_text, "EAM%u", &socket_id) != 1) 267 return false; 268 269 return socket_id == dev_info.socket_id; 270 } 271 272 static void ras_bert_log_processor_section(struct ras_core_context *ras_core, 273 struct ras_bert_hest_generic_data *gdata, 274 const struct ras_bert_sec_proc_ia *proc, 275 const struct ras_bert_ia_proc_ctx *ctx_info) 276 { 277 struct ras_boot_err_ctx ctx = { 0 }; 278 u16 data_size; 279 280 ras_bert_fill_boot_ctx(&ctx, gdata); 281 ctx.apic_id = proc->lapic_id; 282 if (proc->validation_bits & CPER_PROC_VALID_APIC_ID) 283 ctx.flags |= RAS_BOOT_CTX_VALID_APIC_ID; 284 ctx.reg_ctx_type = ctx_info->reg_ctx_type; 285 ctx.msr_addr = ctx_info->msr_addr; 286 ctx.mm_reg_addr = ctx_info->mm_reg_addr; 287 data_size = min_t(u16, ctx_info->reg_arr_size, sizeof(ctx.regs)); 288 ctx.reg_arr_size = data_size; 289 memcpy(ctx.regs, ctx_info + 1, data_size); 290 291 ras_log_ring_add_log_event(ras_core, RAS_LOG_EVENT_BOOT, &ctx, sizeof(ctx), NULL); 292 } 293 294 static int ras_bert_record_amd_contexts(struct ras_core_context *ras_core, 295 struct ras_bert_hest_generic_data *gdata, 296 const u8 *body) 297 { 298 const struct ras_bert_ia_proc_ctx *context; 299 u64 valid_bits = ras_bert_read_le64(body); 300 u32 error_count = RAS_BERT_ERR_INFO_COUNT(valid_bits); 301 u32 context_count = RAS_BERT_CONTEXT_COUNT(valid_bits); 302 u32 offset = sizeof(struct crashdump_hdr) + 303 (error_count * sizeof(struct crashdump_error_info)); 304 u32 i; 305 306 for (i = 0; i < context_count; i++) { 307 struct ras_boot_err_ctx ctx = { 0 }; 308 u32 context_size; 309 u16 data_size; 310 311 if (offset > gdata->error_data_length || 312 sizeof(*context) > gdata->error_data_length - offset) 313 return -EINVAL; 314 315 context = (const struct ras_bert_ia_proc_ctx *)(body + offset); 316 context_size = ALIGN(sizeof(*context) + context->reg_arr_size, 16); 317 if (context_size > gdata->error_data_length - offset) 318 return -EINVAL; 319 320 ras_bert_fill_boot_ctx(&ctx, gdata); 321 ctx.reg_ctx_type = context->reg_ctx_type; 322 ctx.msr_addr = context->msr_addr; 323 ctx.mm_reg_addr = context->mm_reg_addr; 324 data_size = min_t(u16, context->reg_arr_size, sizeof(ctx.regs)); 325 ctx.reg_arr_size = data_size; 326 memcpy(ctx.regs, context + 1, data_size); 327 ras_log_ring_add_log_event(ras_core, RAS_LOG_EVENT_BOOT, 328 &ctx, sizeof(ctx), NULL); 329 330 offset += context_size; 331 } 332 333 return 0; 334 } 335 336 static int ras_bert_record_section(struct ras_core_context *ras_core, 337 struct ras_bert_hest_generic_data *gdata) 338 { 339 struct aca_bank_ecc err = { 0 }; 340 struct aca_bank_reg bank = { 0 }; 341 struct device_system_info dev_info = { 0 }; 342 struct ras_boot_err_ctx ctx = { 0 }; 343 u64 *regs = ras_bert_get_payload(gdata); 344 u32 data_size; 345 int ret; 346 347 if (ras_bert_amd_body_valid((u8 *)regs, gdata->error_data_length)) { 348 if (!ras_bert_fru_matches_device(ras_core, gdata)) 349 return 0; 350 351 return ras_bert_record_amd_contexts(ras_core, gdata, 352 (const u8 *)regs); 353 } 354 355 if (gdata->error_data_length < sizeof(u64) * (RAS_CPER_ACA_REG_IPID + 1)) 356 return 0; 357 358 bank.bank_type = MCE_BANK_TYPE_GPU; 359 data_size = min_t(u32, gdata->error_data_length, sizeof(bank.regs)); 360 memcpy(bank.regs, regs, data_size); 361 ret = ras_aca_parse_bank(ras_core, &bank, &err); 362 if (ret || ras_core_get_device_system_info(ras_core, &dev_info) || 363 err.bank_info.socket_id != dev_info.socket_id) 364 return 0; 365 366 ras_bert_fill_boot_ctx(&ctx, gdata); 367 ctx.reg_ctx_type = CPER_CTX_TYPE__BOOT; 368 data_size = min_t(u32, data_size, sizeof(ctx.regs)); 369 ctx.reg_arr_size = data_size; 370 memcpy(ctx.regs, regs, data_size); 371 372 ras_log_ring_add_log_event(ras_core, RAS_LOG_EVENT_BOOT, &ctx, sizeof(ctx), NULL); 373 374 return 0; 375 } 376 377 static int ras_bert_record_proc_ia_section(struct ras_core_context *ras_core, 378 struct ras_bert_hest_generic_data *gdata) 379 { 380 struct ras_bert_ia_err_info *err_info; 381 struct ras_bert_ia_proc_ctx *ctx_info; 382 struct ras_bert_sec_proc_ia *proc; 383 u8 *section_end; 384 int ctx_count; 385 int err_count; 386 int i; 387 388 if (gdata->error_data_length < sizeof(*proc)) 389 return -EINVAL; 390 391 if (!ras_core->bert_platform_owner) 392 return 0; 393 394 proc = ras_bert_get_payload(gdata); 395 section_end = (u8 *)proc + gdata->error_data_length; 396 err_count = RAS_BERT_ERR_INFO_COUNT(proc->validation_bits); 397 ctx_count = RAS_BERT_CONTEXT_COUNT(proc->validation_bits); 398 err_info = (struct ras_bert_ia_err_info *)(proc + 1); 399 if ((u8 *)err_info > section_end || 400 err_count * sizeof(*err_info) > section_end - (u8 *)err_info) 401 return -EINVAL; 402 403 err_info += err_count; 404 ctx_info = (struct ras_bert_ia_proc_ctx *)err_info; 405 for (i = 0; i < ctx_count; i++) { 406 size_t remaining; 407 size_t ctx_size; 408 409 if ((u8 *)ctx_info > section_end || 410 sizeof(*ctx_info) > section_end - (u8 *)ctx_info) 411 return -EINVAL; 412 413 remaining = section_end - (u8 *)ctx_info; 414 if (ctx_info->reg_arr_size > remaining - sizeof(*ctx_info)) 415 return -EINVAL; 416 ctx_size = ALIGN(sizeof(*ctx_info) + ctx_info->reg_arr_size, 16); 417 if (ctx_size > remaining) 418 return -EINVAL; 419 if (ctx_info->reg_ctx_type != RAS_BERT_PROC_CTX_TYPE_MSR || 420 ctx_info->msr_addr < CPER_SMCA_MC0_STATUS_MSR || 421 (ctx_info->msr_addr - CPER_SMCA_MC0_STATUS_MSR) % 422 CPER_SMCA_BANK_STRIDE) 423 goto next_ctx; 424 if (ctx_info->reg_arr_size < sizeof(u64)) 425 goto next_ctx; 426 427 ras_bert_log_processor_section(ras_core, gdata, proc, ctx_info); 428 429 next_ctx: 430 ctx_info = (struct ras_bert_ia_proc_ctx *)((u8 *)ctx_info + ctx_size); 431 } 432 433 return 0; 434 } 435 436 static int ras_bert_record_platform_mem_section(struct ras_core_context *ras_core, 437 struct ras_bert_hest_generic_data *gdata) 438 { 439 struct ras_boot_err_ctx ctx = { 0 }; 440 struct ras_bert_sec_mem_err *mem; 441 u16 data_size; 442 443 if (gdata->error_data_length != sizeof(struct ras_bert_sec_mem_err_old) && 444 gdata->error_data_length != sizeof(struct ras_bert_sec_mem_err)) 445 return -EINVAL; 446 447 if (!ras_core->bert_platform_owner) 448 return 0; 449 450 mem = ras_bert_get_payload(gdata); 451 if (gdata->error_data_length > sizeof(ctx.raw_data)) 452 return -EOVERFLOW; 453 454 ras_bert_fill_boot_ctx(&ctx, gdata); 455 data_size = gdata->error_data_length; 456 ctx.raw_data_size = data_size; 457 memcpy(ctx.raw_data, mem, data_size); 458 459 ras_log_ring_add_log_event(ras_core, RAS_LOG_EVENT_BOOT, &ctx, sizeof(ctx), NULL); 460 461 return 0; 462 } 463 464 static const struct ras_bert_section_dispatch ras_bert_section_dispatch[] = { 465 { ras_bert_guid_crashdump.b, ras_bert_record_section }, 466 { ras_bert_guid_pcie.b, ras_bert_record_section }, 467 { ras_bert_guid_smn.b, ras_bert_record_section }, 468 { ras_bert_guid_proc_ia.b, ras_bert_record_proc_ia_section }, 469 { ras_bert_guid_platform_mem.b, ras_bert_record_platform_mem_section }, 470 }; 471 472 static const struct ras_bert_section_dispatch * 473 ras_bert_find_section_parser(const u8 *section_type) 474 { 475 int i; 476 477 for (i = 0; i < ARRAY_SIZE(ras_bert_section_dispatch); i++) { 478 if (!memcmp(section_type, ras_bert_section_dispatch[i].section_type, 479 CPER_UUID_MAX_SIZE)) 480 return &ras_bert_section_dispatch[i]; 481 } 482 483 return NULL; 484 } 485 486 int ras_bert_process_records(struct ras_core_context *ras_core, 487 const void *bert, u32 bert_len) 488 { 489 struct ras_bert_hest_generic_status *estatus; 490 struct ras_bert_hest_generic_data *gdata; 491 u32 estatus_len; 492 int remain; 493 int ret; 494 495 if (!ras_core || !bert || bert_len < sizeof(struct ras_bert_hest_generic_status)) 496 return -EINVAL; 497 498 estatus = (struct ras_bert_hest_generic_status *)bert; 499 remain = bert_len; 500 501 while (remain >= sizeof(struct ras_bert_hest_generic_status)) { 502 estatus_len = estatus->raw_data_length ? 503 estatus->raw_data_offset + estatus->raw_data_length : 504 sizeof(*estatus) + estatus->data_length; 505 if (remain < estatus_len) { 506 RAS_DEV_ERR(ras_core->dev, "truncated status block (length: %u).\n", 507 estatus_len); 508 return -EINVAL; 509 } 510 if (!estatus_len) 511 return -EINVAL; 512 513 /* 514 * The generic APEI BERT path may have already consumed the boot 515 * error region and cleared block_status. AMDGPU still needs to 516 * parse the cached BERT payload, so do not use block_status to 517 * decide whether this status block contains records. 518 */ 519 if (ras_bert_estatus_check(estatus)) { 520 RAS_DEV_ERR(ras_core->dev, "invalid error record.\n"); 521 return -EINVAL; 522 } 523 524 gdata = (struct ras_bert_hest_generic_data *)(estatus + 1); 525 while ((void *)gdata - (void *)(estatus + 1) < estatus->data_length) { 526 const struct ras_bert_section_dispatch *parser; 527 528 parser = ras_bert_find_section_parser(gdata->section_type); 529 if (parser) { 530 ret = parser->parser(ras_core, gdata); 531 if (ret) 532 return ret; 533 } else { 534 RAS_DEV_INFO(ras_core->dev, "skip section: %pUl\n", 535 gdata->section_type); 536 } 537 538 gdata = ras_bert_get_next(gdata); 539 } 540 estatus = (struct ras_bert_hest_generic_status *)((u8 *)estatus + estatus_len); 541 remain -= estatus_len; 542 } 543 544 return 0; 545 } 546 #endif 547