1 /* 2 * Copyright 2019 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 */ 23 24 #include "amdgpu_ras_eeprom.h" 25 #include "amdgpu.h" 26 #include "amdgpu_ras.h" 27 #include <linux/bits.h> 28 #include "atom.h" 29 #include "amdgpu_eeprom.h" 30 #include "amdgpu_atomfirmware.h" 31 #include <linux/debugfs.h> 32 #include <linux/uaccess.h> 33 34 #define EEPROM_I2C_MADDR_VEGA20 0x0 35 #define EEPROM_I2C_MADDR_ARCTURUS 0x40000 36 #define EEPROM_I2C_MADDR_ARCTURUS_D342 0x0 37 #define EEPROM_I2C_MADDR_SIENNA_CICHLID 0x0 38 #define EEPROM_I2C_MADDR_ALDEBARAN 0x0 39 40 /* 41 * The 2 macros bellow represent the actual size in bytes that 42 * those entities occupy in the EEPROM memory. 43 * RAS_TABLE_RECORD_SIZE is different than sizeof(eeprom_table_record) which 44 * uses uint64 to store 6b fields such as retired_page. 45 */ 46 #define RAS_TABLE_HEADER_SIZE 20 47 #define RAS_TABLE_RECORD_SIZE 24 48 49 /* Table hdr is 'AMDR' */ 50 #define RAS_TABLE_HDR_VAL 0x414d4452 51 #define RAS_TABLE_VER 0x00010000 52 53 /* Bad GPU tag ‘BADG’ */ 54 #define RAS_TABLE_HDR_BAD 0x42414447 55 56 /* Assume 2-Mbit size EEPROM and take up the whole space. */ 57 #define RAS_TBL_SIZE_BYTES (256 * 1024) 58 #define RAS_TABLE_START 0 59 #define RAS_HDR_START RAS_TABLE_START 60 #define RAS_RECORD_START (RAS_HDR_START + RAS_TABLE_HEADER_SIZE) 61 #define RAS_MAX_RECORD_COUNT ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE) \ 62 / RAS_TABLE_RECORD_SIZE) 63 64 /* Given a zero-based index of an EEPROM RAS record, yields the EEPROM 65 * offset off of RAS_TABLE_START. That is, this is something you can 66 * add to control->i2c_address, and then tell I2C layer to read 67 * from/write to there. _N is the so called absolute index, 68 * because it starts right after the table header. 69 */ 70 #define RAS_INDEX_TO_OFFSET(_C, _N) ((_C)->ras_record_offset + \ 71 (_N) * RAS_TABLE_RECORD_SIZE) 72 73 #define RAS_OFFSET_TO_INDEX(_C, _O) (((_O) - \ 74 (_C)->ras_record_offset) / RAS_TABLE_RECORD_SIZE) 75 76 /* Given a 0-based relative record index, 0, 1, 2, ..., etc., off 77 * of "fri", return the absolute record index off of the end of 78 * the table header. 79 */ 80 #define RAS_RI_TO_AI(_C, _I) (((_I) + (_C)->ras_fri) % \ 81 (_C)->ras_max_record_count) 82 83 #define RAS_NUM_RECS(_tbl_hdr) (((_tbl_hdr)->tbl_size - \ 84 RAS_TABLE_HEADER_SIZE) / RAS_TABLE_RECORD_SIZE) 85 86 #define to_amdgpu_device(x) (container_of(x, struct amdgpu_ras, eeprom_control))->adev 87 88 static bool __is_ras_eeprom_supported(struct amdgpu_device *adev) 89 { 90 return adev->asic_type == CHIP_VEGA20 || 91 adev->asic_type == CHIP_ARCTURUS || 92 adev->asic_type == CHIP_SIENNA_CICHLID || 93 adev->asic_type == CHIP_ALDEBARAN; 94 } 95 96 static bool __get_eeprom_i2c_addr_arct(struct amdgpu_device *adev, 97 struct amdgpu_ras_eeprom_control *control) 98 { 99 STUB(); 100 return false; 101 #ifdef notyet 102 struct atom_context *atom_ctx = adev->mode_info.atom_context; 103 104 if (!control || !atom_ctx) 105 return false; 106 107 if (strnstr(atom_ctx->vbios_version, 108 "D342", 109 sizeof(atom_ctx->vbios_version))) 110 control->i2c_address = EEPROM_I2C_MADDR_ARCTURUS_D342; 111 else 112 control->i2c_address = EEPROM_I2C_MADDR_ARCTURUS; 113 114 return true; 115 #endif 116 } 117 118 static bool __get_eeprom_i2c_addr(struct amdgpu_device *adev, 119 struct amdgpu_ras_eeprom_control *control) 120 { 121 u8 i2c_addr; 122 123 if (!control) 124 return false; 125 126 if (amdgpu_atomfirmware_ras_rom_addr(adev, &i2c_addr)) { 127 /* The address given by VBIOS is an 8-bit, wire-format 128 * address, i.e. the most significant byte. 129 * 130 * Normalize it to a 19-bit EEPROM address. Remove the 131 * device type identifier and make it a 7-bit address; 132 * then make it a 19-bit EEPROM address. See top of 133 * amdgpu_eeprom.c. 134 */ 135 i2c_addr = (i2c_addr & 0x0F) >> 1; 136 control->i2c_address = ((u32) i2c_addr) << 16; 137 138 return true; 139 } 140 141 switch (adev->asic_type) { 142 case CHIP_VEGA20: 143 control->i2c_address = EEPROM_I2C_MADDR_VEGA20; 144 break; 145 146 case CHIP_ARCTURUS: 147 return __get_eeprom_i2c_addr_arct(adev, control); 148 149 case CHIP_SIENNA_CICHLID: 150 control->i2c_address = EEPROM_I2C_MADDR_SIENNA_CICHLID; 151 break; 152 153 case CHIP_ALDEBARAN: 154 control->i2c_address = EEPROM_I2C_MADDR_ALDEBARAN; 155 break; 156 157 default: 158 return false; 159 } 160 161 return true; 162 } 163 164 static void 165 __encode_table_header_to_buf(struct amdgpu_ras_eeprom_table_header *hdr, 166 unsigned char *buf) 167 { 168 u32 *pp = (uint32_t *)buf; 169 170 pp[0] = cpu_to_le32(hdr->header); 171 pp[1] = cpu_to_le32(hdr->version); 172 pp[2] = cpu_to_le32(hdr->first_rec_offset); 173 pp[3] = cpu_to_le32(hdr->tbl_size); 174 pp[4] = cpu_to_le32(hdr->checksum); 175 } 176 177 static void 178 __decode_table_header_from_buf(struct amdgpu_ras_eeprom_table_header *hdr, 179 unsigned char *buf) 180 { 181 u32 *pp = (uint32_t *)buf; 182 183 hdr->header = le32_to_cpu(pp[0]); 184 hdr->version = le32_to_cpu(pp[1]); 185 hdr->first_rec_offset = le32_to_cpu(pp[2]); 186 hdr->tbl_size = le32_to_cpu(pp[3]); 187 hdr->checksum = le32_to_cpu(pp[4]); 188 } 189 190 static int __write_table_header(struct amdgpu_ras_eeprom_control *control) 191 { 192 u8 buf[RAS_TABLE_HEADER_SIZE]; 193 struct amdgpu_device *adev = to_amdgpu_device(control); 194 int res; 195 196 memset(buf, 0, sizeof(buf)); 197 __encode_table_header_to_buf(&control->tbl_hdr, buf); 198 199 /* i2c may be unstable in gpu reset */ 200 down_read(&adev->reset_sem); 201 res = amdgpu_eeprom_write(&adev->pm.smu_i2c, 202 control->i2c_address + 203 control->ras_header_offset, 204 buf, RAS_TABLE_HEADER_SIZE); 205 up_read(&adev->reset_sem); 206 207 if (res < 0) { 208 DRM_ERROR("Failed to write EEPROM table header:%d", res); 209 } else if (res < RAS_TABLE_HEADER_SIZE) { 210 DRM_ERROR("Short write:%d out of %d\n", 211 res, RAS_TABLE_HEADER_SIZE); 212 res = -EIO; 213 } else { 214 res = 0; 215 } 216 217 return res; 218 } 219 220 static u8 __calc_hdr_byte_sum(const struct amdgpu_ras_eeprom_control *control) 221 { 222 int ii; 223 u8 *pp, csum; 224 size_t sz; 225 226 /* Header checksum, skip checksum field in the calculation */ 227 sz = sizeof(control->tbl_hdr) - sizeof(control->tbl_hdr.checksum); 228 pp = (u8 *) &control->tbl_hdr; 229 csum = 0; 230 for (ii = 0; ii < sz; ii++, pp++) 231 csum += *pp; 232 233 return csum; 234 } 235 236 static int amdgpu_ras_eeprom_correct_header_tag( 237 struct amdgpu_ras_eeprom_control *control, 238 uint32_t header) 239 { 240 struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 241 u8 *hh; 242 int res; 243 u8 csum; 244 245 csum = -hdr->checksum; 246 247 hh = (void *) &hdr->header; 248 csum -= (hh[0] + hh[1] + hh[2] + hh[3]); 249 hh = (void *) &header; 250 csum += hh[0] + hh[1] + hh[2] + hh[3]; 251 csum = -csum; 252 mutex_lock(&control->ras_tbl_mutex); 253 hdr->header = header; 254 hdr->checksum = csum; 255 res = __write_table_header(control); 256 mutex_unlock(&control->ras_tbl_mutex); 257 258 return res; 259 } 260 261 /** 262 * amdgpu_ras_eeprom_reset_table -- Reset the RAS EEPROM table 263 * @control: pointer to control structure 264 * 265 * Reset the contents of the header of the RAS EEPROM table. 266 * Return 0 on success, -errno on error. 267 */ 268 int amdgpu_ras_eeprom_reset_table(struct amdgpu_ras_eeprom_control *control) 269 { 270 struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 271 u8 csum; 272 int res; 273 274 mutex_lock(&control->ras_tbl_mutex); 275 276 hdr->header = RAS_TABLE_HDR_VAL; 277 hdr->version = RAS_TABLE_VER; 278 hdr->first_rec_offset = RAS_RECORD_START; 279 hdr->tbl_size = RAS_TABLE_HEADER_SIZE; 280 281 csum = __calc_hdr_byte_sum(control); 282 csum = -csum; 283 hdr->checksum = csum; 284 res = __write_table_header(control); 285 286 control->ras_num_recs = 0; 287 control->ras_fri = 0; 288 289 amdgpu_ras_debugfs_set_ret_size(control); 290 291 mutex_unlock(&control->ras_tbl_mutex); 292 293 return res; 294 } 295 296 static void 297 __encode_table_record_to_buf(struct amdgpu_ras_eeprom_control *control, 298 struct eeprom_table_record *record, 299 unsigned char *buf) 300 { 301 __le64 tmp = 0; 302 int i = 0; 303 304 /* Next are all record fields according to EEPROM page spec in LE foramt */ 305 buf[i++] = record->err_type; 306 307 buf[i++] = record->bank; 308 309 tmp = cpu_to_le64(record->ts); 310 memcpy(buf + i, &tmp, 8); 311 i += 8; 312 313 tmp = cpu_to_le64((record->offset & 0xffffffffffff)); 314 memcpy(buf + i, &tmp, 6); 315 i += 6; 316 317 buf[i++] = record->mem_channel; 318 buf[i++] = record->mcumc_id; 319 320 tmp = cpu_to_le64((record->retired_page & 0xffffffffffff)); 321 memcpy(buf + i, &tmp, 6); 322 } 323 324 static void 325 __decode_table_record_from_buf(struct amdgpu_ras_eeprom_control *control, 326 struct eeprom_table_record *record, 327 unsigned char *buf) 328 { 329 __le64 tmp = 0; 330 int i = 0; 331 332 /* Next are all record fields according to EEPROM page spec in LE foramt */ 333 record->err_type = buf[i++]; 334 335 record->bank = buf[i++]; 336 337 memcpy(&tmp, buf + i, 8); 338 record->ts = le64_to_cpu(tmp); 339 i += 8; 340 341 memcpy(&tmp, buf + i, 6); 342 record->offset = (le64_to_cpu(tmp) & 0xffffffffffff); 343 i += 6; 344 345 record->mem_channel = buf[i++]; 346 record->mcumc_id = buf[i++]; 347 348 memcpy(&tmp, buf + i, 6); 349 record->retired_page = (le64_to_cpu(tmp) & 0xffffffffffff); 350 } 351 352 bool amdgpu_ras_eeprom_check_err_threshold(struct amdgpu_device *adev) 353 { 354 struct amdgpu_ras *con = amdgpu_ras_get_context(adev); 355 356 if (!__is_ras_eeprom_supported(adev)) 357 return false; 358 359 /* skip check eeprom table for VEGA20 Gaming */ 360 if (!con) 361 return false; 362 else 363 if (!(con->features & BIT(AMDGPU_RAS_BLOCK__UMC))) 364 return false; 365 366 if (con->eeprom_control.tbl_hdr.header == RAS_TABLE_HDR_BAD) { 367 dev_warn(adev->dev, "This GPU is in BAD status."); 368 dev_warn(adev->dev, "Please retire it or set a larger " 369 "threshold value when reloading driver.\n"); 370 return true; 371 } 372 373 return false; 374 } 375 376 /** 377 * __amdgpu_ras_eeprom_write -- write indexed from buffer to EEPROM 378 * @control: pointer to control structure 379 * @buf: pointer to buffer containing data to write 380 * @fri: start writing at this index 381 * @num: number of records to write 382 * 383 * The caller must hold the table mutex in @control. 384 * Return 0 on success, -errno otherwise. 385 */ 386 static int __amdgpu_ras_eeprom_write(struct amdgpu_ras_eeprom_control *control, 387 u8 *buf, const u32 fri, const u32 num) 388 { 389 struct amdgpu_device *adev = to_amdgpu_device(control); 390 u32 buf_size; 391 int res; 392 393 /* i2c may be unstable in gpu reset */ 394 down_read(&adev->reset_sem); 395 buf_size = num * RAS_TABLE_RECORD_SIZE; 396 res = amdgpu_eeprom_write(&adev->pm.smu_i2c, 397 control->i2c_address + 398 RAS_INDEX_TO_OFFSET(control, fri), 399 buf, buf_size); 400 up_read(&adev->reset_sem); 401 if (res < 0) { 402 DRM_ERROR("Writing %d EEPROM table records error:%d", 403 num, res); 404 } else if (res < buf_size) { 405 /* Short write, return error. 406 */ 407 DRM_ERROR("Wrote %d records out of %d", 408 res / RAS_TABLE_RECORD_SIZE, num); 409 res = -EIO; 410 } else { 411 res = 0; 412 } 413 414 return res; 415 } 416 417 static int 418 amdgpu_ras_eeprom_append_table(struct amdgpu_ras_eeprom_control *control, 419 struct eeprom_table_record *record, 420 const u32 num) 421 { 422 u32 a, b, i; 423 u8 *buf, *pp; 424 int res; 425 426 buf = kcalloc(num, RAS_TABLE_RECORD_SIZE, GFP_KERNEL); 427 if (!buf) 428 return -ENOMEM; 429 430 /* Encode all of them in one go. 431 */ 432 pp = buf; 433 for (i = 0; i < num; i++, pp += RAS_TABLE_RECORD_SIZE) 434 __encode_table_record_to_buf(control, &record[i], pp); 435 436 /* a, first record index to write into. 437 * b, last record index to write into. 438 * a = first index to read (fri) + number of records in the table, 439 * b = a + @num - 1. 440 * Let N = control->ras_max_num_record_count, then we have, 441 * case 0: 0 <= a <= b < N, 442 * just append @num records starting at a; 443 * case 1: 0 <= a < N <= b, 444 * append (N - a) records starting at a, and 445 * append the remainder, b % N + 1, starting at 0. 446 * case 2: 0 <= fri < N <= a <= b, then modulo N we get two subcases, 447 * case 2a: 0 <= a <= b < N 448 * append num records starting at a; and fix fri if b overwrote it, 449 * and since a <= b, if b overwrote it then a must've also, 450 * and if b didn't overwrite it, then a didn't also. 451 * case 2b: 0 <= b < a < N 452 * write num records starting at a, which wraps around 0=N 453 * and overwrite fri unconditionally. Now from case 2a, 454 * this means that b eclipsed fri to overwrite it and wrap 455 * around 0 again, i.e. b = 2N+r pre modulo N, so we unconditionally 456 * set fri = b + 1 (mod N). 457 * Now, since fri is updated in every case, except the trivial case 0, 458 * the number of records present in the table after writing, is, 459 * num_recs - 1 = b - fri (mod N), and we take the positive value, 460 * by adding an arbitrary multiple of N before taking the modulo N 461 * as shown below. 462 */ 463 a = control->ras_fri + control->ras_num_recs; 464 b = a + num - 1; 465 if (b < control->ras_max_record_count) { 466 res = __amdgpu_ras_eeprom_write(control, buf, a, num); 467 } else if (a < control->ras_max_record_count) { 468 u32 g0, g1; 469 470 g0 = control->ras_max_record_count - a; 471 g1 = b % control->ras_max_record_count + 1; 472 res = __amdgpu_ras_eeprom_write(control, buf, a, g0); 473 if (res) 474 goto Out; 475 res = __amdgpu_ras_eeprom_write(control, 476 buf + g0 * RAS_TABLE_RECORD_SIZE, 477 0, g1); 478 if (res) 479 goto Out; 480 if (g1 > control->ras_fri) 481 control->ras_fri = g1 % control->ras_max_record_count; 482 } else { 483 a %= control->ras_max_record_count; 484 b %= control->ras_max_record_count; 485 486 if (a <= b) { 487 /* Note that, b - a + 1 = num. */ 488 res = __amdgpu_ras_eeprom_write(control, buf, a, num); 489 if (res) 490 goto Out; 491 if (b >= control->ras_fri) 492 control->ras_fri = (b + 1) % control->ras_max_record_count; 493 } else { 494 u32 g0, g1; 495 496 /* b < a, which means, we write from 497 * a to the end of the table, and from 498 * the start of the table to b. 499 */ 500 g0 = control->ras_max_record_count - a; 501 g1 = b + 1; 502 res = __amdgpu_ras_eeprom_write(control, buf, a, g0); 503 if (res) 504 goto Out; 505 res = __amdgpu_ras_eeprom_write(control, 506 buf + g0 * RAS_TABLE_RECORD_SIZE, 507 0, g1); 508 if (res) 509 goto Out; 510 control->ras_fri = g1 % control->ras_max_record_count; 511 } 512 } 513 control->ras_num_recs = 1 + (control->ras_max_record_count + b 514 - control->ras_fri) 515 % control->ras_max_record_count; 516 Out: 517 kfree(buf); 518 return res; 519 } 520 521 static int 522 amdgpu_ras_eeprom_update_header(struct amdgpu_ras_eeprom_control *control) 523 { 524 struct amdgpu_device *adev = to_amdgpu_device(control); 525 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 526 u8 *buf, *pp, csum; 527 u32 buf_size; 528 int res; 529 530 /* Modify the header if it exceeds. 531 */ 532 if (amdgpu_bad_page_threshold != 0 && 533 control->ras_num_recs >= ras->bad_page_cnt_threshold) { 534 dev_warn(adev->dev, 535 "Saved bad pages %d reaches threshold value %d\n", 536 control->ras_num_recs, ras->bad_page_cnt_threshold); 537 control->tbl_hdr.header = RAS_TABLE_HDR_BAD; 538 } 539 540 control->tbl_hdr.version = RAS_TABLE_VER; 541 control->tbl_hdr.first_rec_offset = RAS_INDEX_TO_OFFSET(control, control->ras_fri); 542 control->tbl_hdr.tbl_size = RAS_TABLE_HEADER_SIZE + control->ras_num_recs * RAS_TABLE_RECORD_SIZE; 543 control->tbl_hdr.checksum = 0; 544 545 buf_size = control->ras_num_recs * RAS_TABLE_RECORD_SIZE; 546 buf = kcalloc(control->ras_num_recs, RAS_TABLE_RECORD_SIZE, GFP_KERNEL); 547 if (!buf) { 548 DRM_ERROR("allocating memory for table of size %d bytes failed\n", 549 control->tbl_hdr.tbl_size); 550 res = -ENOMEM; 551 goto Out; 552 } 553 554 down_read(&adev->reset_sem); 555 res = amdgpu_eeprom_read(&adev->pm.smu_i2c, 556 control->i2c_address + 557 control->ras_record_offset, 558 buf, buf_size); 559 up_read(&adev->reset_sem); 560 if (res < 0) { 561 DRM_ERROR("EEPROM failed reading records:%d\n", 562 res); 563 goto Out; 564 } else if (res < buf_size) { 565 DRM_ERROR("EEPROM read %d out of %d bytes\n", 566 res, buf_size); 567 res = -EIO; 568 goto Out; 569 } 570 571 /* Recalc the checksum. 572 */ 573 csum = 0; 574 for (pp = buf; pp < buf + buf_size; pp++) 575 csum += *pp; 576 577 csum += __calc_hdr_byte_sum(control); 578 /* avoid sign extension when assigning to "checksum" */ 579 csum = -csum; 580 control->tbl_hdr.checksum = csum; 581 res = __write_table_header(control); 582 Out: 583 kfree(buf); 584 return res; 585 } 586 587 /** 588 * amdgpu_ras_eeprom_append -- append records to the EEPROM RAS table 589 * @control: pointer to control structure 590 * @record: array of records to append 591 * @num: number of records in @record array 592 * 593 * Append @num records to the table, calculate the checksum and write 594 * the table back to EEPROM. The maximum number of records that 595 * can be appended is between 1 and control->ras_max_record_count, 596 * regardless of how many records are already stored in the table. 597 * 598 * Return 0 on success or if EEPROM is not supported, -errno on error. 599 */ 600 int amdgpu_ras_eeprom_append(struct amdgpu_ras_eeprom_control *control, 601 struct eeprom_table_record *record, 602 const u32 num) 603 { 604 struct amdgpu_device *adev = to_amdgpu_device(control); 605 int res; 606 607 if (!__is_ras_eeprom_supported(adev)) 608 return 0; 609 610 if (num == 0) { 611 DRM_ERROR("will not append 0 records\n"); 612 return -EINVAL; 613 } else if (num > control->ras_max_record_count) { 614 DRM_ERROR("cannot append %d records than the size of table %d\n", 615 num, control->ras_max_record_count); 616 return -EINVAL; 617 } 618 619 mutex_lock(&control->ras_tbl_mutex); 620 621 res = amdgpu_ras_eeprom_append_table(control, record, num); 622 if (!res) 623 res = amdgpu_ras_eeprom_update_header(control); 624 if (!res) 625 amdgpu_ras_debugfs_set_ret_size(control); 626 627 mutex_unlock(&control->ras_tbl_mutex); 628 return res; 629 } 630 631 /** 632 * __amdgpu_ras_eeprom_read -- read indexed from EEPROM into buffer 633 * @control: pointer to control structure 634 * @buf: pointer to buffer to read into 635 * @fri: first record index, start reading at this index, absolute index 636 * @num: number of records to read 637 * 638 * The caller must hold the table mutex in @control. 639 * Return 0 on success, -errno otherwise. 640 */ 641 static int __amdgpu_ras_eeprom_read(struct amdgpu_ras_eeprom_control *control, 642 u8 *buf, const u32 fri, const u32 num) 643 { 644 struct amdgpu_device *adev = to_amdgpu_device(control); 645 u32 buf_size; 646 int res; 647 648 /* i2c may be unstable in gpu reset */ 649 down_read(&adev->reset_sem); 650 buf_size = num * RAS_TABLE_RECORD_SIZE; 651 res = amdgpu_eeprom_read(&adev->pm.smu_i2c, 652 control->i2c_address + 653 RAS_INDEX_TO_OFFSET(control, fri), 654 buf, buf_size); 655 up_read(&adev->reset_sem); 656 if (res < 0) { 657 DRM_ERROR("Reading %d EEPROM table records error:%d", 658 num, res); 659 } else if (res < buf_size) { 660 /* Short read, return error. 661 */ 662 DRM_ERROR("Read %d records out of %d", 663 res / RAS_TABLE_RECORD_SIZE, num); 664 res = -EIO; 665 } else { 666 res = 0; 667 } 668 669 return res; 670 } 671 672 /** 673 * amdgpu_ras_eeprom_read -- read EEPROM 674 * @control: pointer to control structure 675 * @record: array of records to read into 676 * @num: number of records in @record 677 * 678 * Reads num records from the RAS table in EEPROM and 679 * writes the data into @record array. 680 * 681 * Returns 0 on success, -errno on error. 682 */ 683 int amdgpu_ras_eeprom_read(struct amdgpu_ras_eeprom_control *control, 684 struct eeprom_table_record *record, 685 const u32 num) 686 { 687 struct amdgpu_device *adev = to_amdgpu_device(control); 688 int i, res; 689 u8 *buf, *pp; 690 u32 g0, g1; 691 692 if (!__is_ras_eeprom_supported(adev)) 693 return 0; 694 695 if (num == 0) { 696 DRM_ERROR("will not read 0 records\n"); 697 return -EINVAL; 698 } else if (num > control->ras_num_recs) { 699 DRM_ERROR("too many records to read:%d available:%d\n", 700 num, control->ras_num_recs); 701 return -EINVAL; 702 } 703 704 buf = kcalloc(num, RAS_TABLE_RECORD_SIZE, GFP_KERNEL); 705 if (!buf) 706 return -ENOMEM; 707 708 /* Determine how many records to read, from the first record 709 * index, fri, to the end of the table, and from the beginning 710 * of the table, such that the total number of records is 711 * @num, and we handle wrap around when fri > 0 and 712 * fri + num > RAS_MAX_RECORD_COUNT. 713 * 714 * First we compute the index of the last element 715 * which would be fetched from each region, 716 * g0 is in [fri, fri + num - 1], and 717 * g1 is in [0, RAS_MAX_RECORD_COUNT - 1]. 718 * Then, if g0 < RAS_MAX_RECORD_COUNT, the index of 719 * the last element to fetch, we set g0 to _the number_ 720 * of elements to fetch, @num, since we know that the last 721 * indexed to be fetched does not exceed the table. 722 * 723 * If, however, g0 >= RAS_MAX_RECORD_COUNT, then 724 * we set g0 to the number of elements to read 725 * until the end of the table, and g1 to the number of 726 * elements to read from the beginning of the table. 727 */ 728 g0 = control->ras_fri + num - 1; 729 g1 = g0 % control->ras_max_record_count; 730 if (g0 < control->ras_max_record_count) { 731 g0 = num; 732 g1 = 0; 733 } else { 734 g0 = control->ras_max_record_count - control->ras_fri; 735 g1 += 1; 736 } 737 738 mutex_lock(&control->ras_tbl_mutex); 739 res = __amdgpu_ras_eeprom_read(control, buf, control->ras_fri, g0); 740 if (res) 741 goto Out; 742 if (g1) { 743 res = __amdgpu_ras_eeprom_read(control, 744 buf + g0 * RAS_TABLE_RECORD_SIZE, 745 0, g1); 746 if (res) 747 goto Out; 748 } 749 750 res = 0; 751 752 /* Read up everything? Then transform. 753 */ 754 pp = buf; 755 for (i = 0; i < num; i++, pp += RAS_TABLE_RECORD_SIZE) 756 __decode_table_record_from_buf(control, &record[i], pp); 757 Out: 758 kfree(buf); 759 mutex_unlock(&control->ras_tbl_mutex); 760 761 return res; 762 } 763 764 uint32_t amdgpu_ras_eeprom_max_record_count(void) 765 { 766 return RAS_MAX_RECORD_COUNT; 767 } 768 769 #ifdef __linux__ 770 static ssize_t 771 amdgpu_ras_debugfs_eeprom_size_read(struct file *f, char __user *buf, 772 size_t size, loff_t *pos) 773 { 774 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; 775 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 776 struct amdgpu_ras_eeprom_control *control = ras ? &ras->eeprom_control : NULL; 777 u8 data[50]; 778 int res; 779 780 if (!size) 781 return size; 782 783 if (!ras || !control) { 784 res = snprintf(data, sizeof(data), "Not supported\n"); 785 } else { 786 res = snprintf(data, sizeof(data), "%d bytes or %d records\n", 787 RAS_TBL_SIZE_BYTES, control->ras_max_record_count); 788 } 789 790 if (*pos >= res) 791 return 0; 792 793 res -= *pos; 794 res = min_t(size_t, res, size); 795 796 if (copy_to_user(buf, &data[*pos], res)) 797 return -EFAULT; 798 799 *pos += res; 800 801 return res; 802 } 803 804 const struct file_operations amdgpu_ras_debugfs_eeprom_size_ops = { 805 .owner = THIS_MODULE, 806 .read = amdgpu_ras_debugfs_eeprom_size_read, 807 .write = NULL, 808 .llseek = default_llseek, 809 }; 810 811 static const char *tbl_hdr_str = " Signature Version FirstOffs Size Checksum\n"; 812 static const char *tbl_hdr_fmt = "0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n"; 813 #define tbl_hdr_fmt_size (5 * (2+8) + 4 + 1) 814 static const char *rec_hdr_str = "Index Offset ErrType Bank/CU TimeStamp Offs/Addr MemChl MCUMCID RetiredPage\n"; 815 static const char *rec_hdr_fmt = "%5d 0x%05X %7s 0x%02X 0x%016llX 0x%012llX 0x%02X 0x%02X 0x%012llX\n"; 816 #define rec_hdr_fmt_size (5 + 1 + 7 + 1 + 7 + 1 + 7 + 1 + 18 + 1 + 14 + 1 + 6 + 1 + 7 + 1 + 14 + 1) 817 818 static const char *record_err_type_str[AMDGPU_RAS_EEPROM_ERR_COUNT] = { 819 "ignore", 820 "re", 821 "ue", 822 }; 823 824 static loff_t amdgpu_ras_debugfs_table_size(struct amdgpu_ras_eeprom_control *control) 825 { 826 return strlen(tbl_hdr_str) + tbl_hdr_fmt_size + 827 strlen(rec_hdr_str) + rec_hdr_fmt_size * control->ras_num_recs; 828 } 829 830 void amdgpu_ras_debugfs_set_ret_size(struct amdgpu_ras_eeprom_control *control) 831 { 832 struct amdgpu_ras *ras = container_of(control, struct amdgpu_ras, 833 eeprom_control); 834 struct dentry *de = ras->de_ras_eeprom_table; 835 836 if (de) 837 d_inode(de)->i_size = amdgpu_ras_debugfs_table_size(control); 838 } 839 840 static ssize_t amdgpu_ras_debugfs_table_read(struct file *f, char __user *buf, 841 size_t size, loff_t *pos) 842 { 843 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; 844 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 845 struct amdgpu_ras_eeprom_control *control = &ras->eeprom_control; 846 const size_t orig_size = size; 847 int res = -EFAULT; 848 size_t data_len; 849 850 mutex_lock(&control->ras_tbl_mutex); 851 852 /* We want *pos - data_len > 0, which means there's 853 * bytes to be printed from data. 854 */ 855 data_len = strlen(tbl_hdr_str); 856 if (*pos < data_len) { 857 data_len -= *pos; 858 data_len = min_t(size_t, data_len, size); 859 if (copy_to_user(buf, &tbl_hdr_str[*pos], data_len)) 860 goto Out; 861 buf += data_len; 862 size -= data_len; 863 *pos += data_len; 864 } 865 866 data_len = strlen(tbl_hdr_str) + tbl_hdr_fmt_size; 867 if (*pos < data_len && size > 0) { 868 u8 data[tbl_hdr_fmt_size + 1]; 869 loff_t lpos; 870 871 snprintf(data, sizeof(data), tbl_hdr_fmt, 872 control->tbl_hdr.header, 873 control->tbl_hdr.version, 874 control->tbl_hdr.first_rec_offset, 875 control->tbl_hdr.tbl_size, 876 control->tbl_hdr.checksum); 877 878 data_len -= *pos; 879 data_len = min_t(size_t, data_len, size); 880 lpos = *pos - strlen(tbl_hdr_str); 881 if (copy_to_user(buf, &data[lpos], data_len)) 882 goto Out; 883 buf += data_len; 884 size -= data_len; 885 *pos += data_len; 886 } 887 888 data_len = strlen(tbl_hdr_str) + tbl_hdr_fmt_size + strlen(rec_hdr_str); 889 if (*pos < data_len && size > 0) { 890 loff_t lpos; 891 892 data_len -= *pos; 893 data_len = min_t(size_t, data_len, size); 894 lpos = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size; 895 if (copy_to_user(buf, &rec_hdr_str[lpos], data_len)) 896 goto Out; 897 buf += data_len; 898 size -= data_len; 899 *pos += data_len; 900 } 901 902 data_len = amdgpu_ras_debugfs_table_size(control); 903 if (*pos < data_len && size > 0) { 904 u8 dare[RAS_TABLE_RECORD_SIZE]; 905 u8 data[rec_hdr_fmt_size + 1]; 906 struct eeprom_table_record record; 907 int s, r; 908 909 /* Find the starting record index 910 */ 911 s = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size - 912 strlen(rec_hdr_str); 913 s = s / rec_hdr_fmt_size; 914 r = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size - 915 strlen(rec_hdr_str); 916 r = r % rec_hdr_fmt_size; 917 918 for ( ; size > 0 && s < control->ras_num_recs; s++) { 919 u32 ai = RAS_RI_TO_AI(control, s); 920 /* Read a single record 921 */ 922 res = __amdgpu_ras_eeprom_read(control, dare, ai, 1); 923 if (res) 924 goto Out; 925 __decode_table_record_from_buf(control, &record, dare); 926 snprintf(data, sizeof(data), rec_hdr_fmt, 927 s, 928 RAS_INDEX_TO_OFFSET(control, ai), 929 record_err_type_str[record.err_type], 930 record.bank, 931 record.ts, 932 record.offset, 933 record.mem_channel, 934 record.mcumc_id, 935 record.retired_page); 936 937 data_len = min_t(size_t, rec_hdr_fmt_size - r, size); 938 if (copy_to_user(buf, &data[r], data_len)) { 939 res = -EFAULT; 940 goto Out; 941 } 942 buf += data_len; 943 size -= data_len; 944 *pos += data_len; 945 r = 0; 946 } 947 } 948 res = 0; 949 Out: 950 mutex_unlock(&control->ras_tbl_mutex); 951 return res < 0 ? res : orig_size - size; 952 } 953 954 static ssize_t 955 amdgpu_ras_debugfs_eeprom_table_read(struct file *f, char __user *buf, 956 size_t size, loff_t *pos) 957 { 958 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; 959 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 960 struct amdgpu_ras_eeprom_control *control = ras ? &ras->eeprom_control : NULL; 961 u8 data[81]; 962 int res; 963 964 if (!size) 965 return size; 966 967 if (!ras || !control) { 968 res = snprintf(data, sizeof(data), "Not supported\n"); 969 if (*pos >= res) 970 return 0; 971 972 res -= *pos; 973 res = min_t(size_t, res, size); 974 975 if (copy_to_user(buf, &data[*pos], res)) 976 return -EFAULT; 977 978 *pos += res; 979 980 return res; 981 } else { 982 return amdgpu_ras_debugfs_table_read(f, buf, size, pos); 983 } 984 } 985 986 const struct file_operations amdgpu_ras_debugfs_eeprom_table_ops = { 987 .owner = THIS_MODULE, 988 .read = amdgpu_ras_debugfs_eeprom_table_read, 989 .write = NULL, 990 .llseek = default_llseek, 991 }; 992 #else /* !__linux__ */ 993 void amdgpu_ras_debugfs_set_ret_size(struct amdgpu_ras_eeprom_control *control) 994 { 995 } 996 #endif 997 998 /** 999 * __verify_ras_table_checksum -- verify the RAS EEPROM table checksum 1000 * @control: pointer to control structure 1001 * 1002 * Check the checksum of the stored in EEPROM RAS table. 1003 * 1004 * Return 0 if the checksum is correct, 1005 * positive if it is not correct, and 1006 * -errno on I/O error. 1007 */ 1008 static int __verify_ras_table_checksum(struct amdgpu_ras_eeprom_control *control) 1009 { 1010 struct amdgpu_device *adev = to_amdgpu_device(control); 1011 int buf_size, res; 1012 u8 csum, *buf, *pp; 1013 1014 buf_size = RAS_TABLE_HEADER_SIZE + 1015 control->ras_num_recs * RAS_TABLE_RECORD_SIZE; 1016 buf = kzalloc(buf_size, GFP_KERNEL); 1017 if (!buf) { 1018 DRM_ERROR("Out of memory checking RAS table checksum.\n"); 1019 return -ENOMEM; 1020 } 1021 1022 res = amdgpu_eeprom_read(&adev->pm.smu_i2c, 1023 control->i2c_address + 1024 control->ras_header_offset, 1025 buf, buf_size); 1026 if (res < buf_size) { 1027 DRM_ERROR("Partial read for checksum, res:%d\n", res); 1028 /* On partial reads, return -EIO. 1029 */ 1030 if (res >= 0) 1031 res = -EIO; 1032 goto Out; 1033 } 1034 1035 csum = 0; 1036 for (pp = buf; pp < buf + buf_size; pp++) 1037 csum += *pp; 1038 Out: 1039 kfree(buf); 1040 return res < 0 ? res : csum; 1041 } 1042 1043 int amdgpu_ras_eeprom_init(struct amdgpu_ras_eeprom_control *control, 1044 bool *exceed_err_limit) 1045 { 1046 struct amdgpu_device *adev = to_amdgpu_device(control); 1047 unsigned char buf[RAS_TABLE_HEADER_SIZE] = { 0 }; 1048 struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 1049 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 1050 int res; 1051 1052 *exceed_err_limit = false; 1053 1054 if (!__is_ras_eeprom_supported(adev)) 1055 return 0; 1056 1057 /* Verify i2c adapter is initialized */ 1058 if (!adev->pm.smu_i2c.algo) 1059 return -ENOENT; 1060 1061 if (!__get_eeprom_i2c_addr(adev, control)) 1062 return -EINVAL; 1063 1064 control->ras_header_offset = RAS_HDR_START; 1065 control->ras_record_offset = RAS_RECORD_START; 1066 control->ras_max_record_count = RAS_MAX_RECORD_COUNT; 1067 rw_init(&control->ras_tbl_mutex, "rastbl"); 1068 1069 /* Read the table header from EEPROM address */ 1070 res = amdgpu_eeprom_read(&adev->pm.smu_i2c, 1071 control->i2c_address + control->ras_header_offset, 1072 buf, RAS_TABLE_HEADER_SIZE); 1073 if (res < RAS_TABLE_HEADER_SIZE) { 1074 DRM_ERROR("Failed to read EEPROM table header, res:%d", res); 1075 return res >= 0 ? -EIO : res; 1076 } 1077 1078 __decode_table_header_from_buf(hdr, buf); 1079 1080 control->ras_num_recs = RAS_NUM_RECS(hdr); 1081 control->ras_fri = RAS_OFFSET_TO_INDEX(control, hdr->first_rec_offset); 1082 1083 if (hdr->header == RAS_TABLE_HDR_VAL) { 1084 DRM_DEBUG_DRIVER("Found existing EEPROM table with %d records", 1085 control->ras_num_recs); 1086 res = __verify_ras_table_checksum(control); 1087 if (res) 1088 DRM_ERROR("RAS table incorrect checksum or error:%d\n", 1089 res); 1090 } else if (hdr->header == RAS_TABLE_HDR_BAD && 1091 amdgpu_bad_page_threshold != 0) { 1092 res = __verify_ras_table_checksum(control); 1093 if (res) 1094 DRM_ERROR("RAS Table incorrect checksum or error:%d\n", 1095 res); 1096 if (ras->bad_page_cnt_threshold > control->ras_num_recs) { 1097 /* This means that, the threshold was increased since 1098 * the last time the system was booted, and now, 1099 * ras->bad_page_cnt_threshold - control->num_recs > 0, 1100 * so that at least one more record can be saved, 1101 * before the page count threshold is reached. 1102 */ 1103 dev_info(adev->dev, 1104 "records:%d threshold:%d, resetting " 1105 "RAS table header signature", 1106 control->ras_num_recs, 1107 ras->bad_page_cnt_threshold); 1108 res = amdgpu_ras_eeprom_correct_header_tag(control, 1109 RAS_TABLE_HDR_VAL); 1110 } else { 1111 *exceed_err_limit = true; 1112 dev_err(adev->dev, 1113 "RAS records:%d exceed threshold:%d, " 1114 "maybe retire this GPU?", 1115 control->ras_num_recs, ras->bad_page_cnt_threshold); 1116 } 1117 } else { 1118 DRM_INFO("Creating a new EEPROM table"); 1119 1120 res = amdgpu_ras_eeprom_reset_table(control); 1121 } 1122 1123 return res < 0 ? res : 0; 1124 } 1125