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