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