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 "smu_v11_0_i2c.h" 29 #include "atom.h" 30 31 #define EEPROM_I2C_TARGET_ADDR_VEGA20 0xA0 32 #define EEPROM_I2C_TARGET_ADDR_ARCTURUS 0xA8 33 #define EEPROM_I2C_TARGET_ADDR_ARCTURUS_D342 0xA0 34 35 /* 36 * The 2 macros bellow represent the actual size in bytes that 37 * those entities occupy in the EEPROM memory. 38 * EEPROM_TABLE_RECORD_SIZE is different than sizeof(eeprom_table_record) which 39 * uses uint64 to store 6b fields such as retired_page. 40 */ 41 #define EEPROM_TABLE_HEADER_SIZE 20 42 #define EEPROM_TABLE_RECORD_SIZE 24 43 44 #define EEPROM_ADDRESS_SIZE 0x2 45 46 /* Table hdr is 'AMDR' */ 47 #define EEPROM_TABLE_HDR_VAL 0x414d4452 48 #define EEPROM_TABLE_VER 0x00010000 49 50 /* Assume 2 Mbit size */ 51 #define EEPROM_SIZE_BYTES 256000 52 #define EEPROM_PAGE__SIZE_BYTES 256 53 #define EEPROM_HDR_START 0 54 #define EEPROM_RECORD_START (EEPROM_HDR_START + EEPROM_TABLE_HEADER_SIZE) 55 #define EEPROM_MAX_RECORD_NUM ((EEPROM_SIZE_BYTES - EEPROM_TABLE_HEADER_SIZE) / EEPROM_TABLE_RECORD_SIZE) 56 #define EEPROM_ADDR_MSB_MASK GENMASK(17, 8) 57 58 #define to_amdgpu_device(x) (container_of(x, struct amdgpu_ras, eeprom_control))->adev 59 60 static bool __get_eeprom_i2c_addr_arct(struct amdgpu_device *adev, 61 uint16_t *i2c_addr) 62 { 63 struct atom_context *atom_ctx = adev->mode_info.atom_context; 64 65 if (!i2c_addr || !atom_ctx) 66 return false; 67 68 if (strnstr(atom_ctx->vbios_version, 69 "D342", 70 sizeof(atom_ctx->vbios_version))) 71 *i2c_addr = EEPROM_I2C_TARGET_ADDR_ARCTURUS_D342; 72 else 73 *i2c_addr = EEPROM_I2C_TARGET_ADDR_ARCTURUS; 74 75 return true; 76 } 77 78 static bool __get_eeprom_i2c_addr(struct amdgpu_device *adev, 79 uint16_t *i2c_addr) 80 { 81 if (!i2c_addr) 82 return false; 83 84 switch (adev->asic_type) { 85 case CHIP_VEGA20: 86 *i2c_addr = EEPROM_I2C_TARGET_ADDR_VEGA20; 87 break; 88 89 case CHIP_ARCTURUS: 90 return __get_eeprom_i2c_addr_arct(adev, i2c_addr); 91 92 default: 93 return false; 94 } 95 96 return true; 97 } 98 99 static void __encode_table_header_to_buff(struct amdgpu_ras_eeprom_table_header *hdr, 100 unsigned char *buff) 101 { 102 uint32_t *pp = (uint32_t *) buff; 103 104 pp[0] = cpu_to_le32(hdr->header); 105 pp[1] = cpu_to_le32(hdr->version); 106 pp[2] = cpu_to_le32(hdr->first_rec_offset); 107 pp[3] = cpu_to_le32(hdr->tbl_size); 108 pp[4] = cpu_to_le32(hdr->checksum); 109 } 110 111 static void __decode_table_header_from_buff(struct amdgpu_ras_eeprom_table_header *hdr, 112 unsigned char *buff) 113 { 114 uint32_t *pp = (uint32_t *)buff; 115 116 hdr->header = le32_to_cpu(pp[0]); 117 hdr->version = le32_to_cpu(pp[1]); 118 hdr->first_rec_offset = le32_to_cpu(pp[2]); 119 hdr->tbl_size = le32_to_cpu(pp[3]); 120 hdr->checksum = le32_to_cpu(pp[4]); 121 } 122 123 static int __update_table_header(struct amdgpu_ras_eeprom_control *control, 124 unsigned char *buff) 125 { 126 int ret = 0; 127 struct i2c_msg msg = { 128 .addr = 0, 129 .flags = 0, 130 .len = EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE, 131 .buf = buff, 132 }; 133 134 135 *(uint16_t *)buff = EEPROM_HDR_START; 136 __encode_table_header_to_buff(&control->tbl_hdr, buff + EEPROM_ADDRESS_SIZE); 137 138 msg.addr = control->i2c_address; 139 140 ret = i2c_transfer(&control->eeprom_accessor, &msg, 1); 141 if (ret < 1) 142 DRM_ERROR("Failed to write EEPROM table header, ret:%d", ret); 143 144 return ret; 145 } 146 147 static uint32_t __calc_hdr_byte_sum(struct amdgpu_ras_eeprom_control *control) 148 { 149 int i; 150 uint32_t tbl_sum = 0; 151 152 /* Header checksum, skip checksum field in the calculation */ 153 for (i = 0; i < sizeof(control->tbl_hdr) - sizeof(control->tbl_hdr.checksum); i++) 154 tbl_sum += *(((unsigned char *)&control->tbl_hdr) + i); 155 156 return tbl_sum; 157 } 158 159 static uint32_t __calc_recs_byte_sum(struct eeprom_table_record *records, 160 int num) 161 { 162 int i, j; 163 uint32_t tbl_sum = 0; 164 165 /* Records checksum */ 166 for (i = 0; i < num; i++) { 167 struct eeprom_table_record *record = &records[i]; 168 169 for (j = 0; j < sizeof(*record); j++) { 170 tbl_sum += *(((unsigned char *)record) + j); 171 } 172 } 173 174 return tbl_sum; 175 } 176 177 static inline uint32_t __calc_tbl_byte_sum(struct amdgpu_ras_eeprom_control *control, 178 struct eeprom_table_record *records, int num) 179 { 180 return __calc_hdr_byte_sum(control) + __calc_recs_byte_sum(records, num); 181 } 182 183 /* Checksum = 256 -((sum of all table entries) mod 256) */ 184 static void __update_tbl_checksum(struct amdgpu_ras_eeprom_control *control, 185 struct eeprom_table_record *records, int num, 186 uint32_t old_hdr_byte_sum) 187 { 188 /* 189 * This will update the table sum with new records. 190 * 191 * TODO: What happens when the EEPROM table is to be wrapped around 192 * and old records from start will get overridden. 193 */ 194 195 /* need to recalculate updated header byte sum */ 196 control->tbl_byte_sum -= old_hdr_byte_sum; 197 control->tbl_byte_sum += __calc_tbl_byte_sum(control, records, num); 198 199 control->tbl_hdr.checksum = 256 - (control->tbl_byte_sum % 256); 200 } 201 202 /* table sum mod 256 + checksum must equals 256 */ 203 static bool __validate_tbl_checksum(struct amdgpu_ras_eeprom_control *control, 204 struct eeprom_table_record *records, int num) 205 { 206 control->tbl_byte_sum = __calc_tbl_byte_sum(control, records, num); 207 208 if (control->tbl_hdr.checksum + (control->tbl_byte_sum % 256) != 256) { 209 DRM_WARN("Checksum mismatch, checksum: %u ", control->tbl_hdr.checksum); 210 return false; 211 } 212 213 return true; 214 } 215 216 int amdgpu_ras_eeprom_reset_table(struct amdgpu_ras_eeprom_control *control) 217 { 218 unsigned char buff[EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE] = { 0 }; 219 struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 220 int ret = 0; 221 222 mutex_lock(&control->tbl_mutex); 223 224 hdr->header = EEPROM_TABLE_HDR_VAL; 225 hdr->version = EEPROM_TABLE_VER; 226 hdr->first_rec_offset = EEPROM_RECORD_START; 227 hdr->tbl_size = EEPROM_TABLE_HEADER_SIZE; 228 229 control->tbl_byte_sum = 0; 230 __update_tbl_checksum(control, NULL, 0, 0); 231 control->next_addr = EEPROM_RECORD_START; 232 233 ret = __update_table_header(control, buff); 234 235 mutex_unlock(&control->tbl_mutex); 236 237 return ret; 238 239 } 240 241 int amdgpu_ras_eeprom_init(struct amdgpu_ras_eeprom_control *control) 242 { 243 int ret = 0; 244 struct amdgpu_device *adev = to_amdgpu_device(control); 245 unsigned char buff[EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE] = { 0 }; 246 struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 247 struct i2c_msg msg = { 248 .addr = 0, 249 .flags = I2C_M_RD, 250 .len = EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE, 251 .buf = buff, 252 }; 253 254 if (!__get_eeprom_i2c_addr(adev, &control->i2c_address)) 255 return -EINVAL; 256 257 mutex_init(&control->tbl_mutex); 258 259 switch (adev->asic_type) { 260 case CHIP_VEGA20: 261 ret = smu_v11_0_i2c_eeprom_control_init(&control->eeprom_accessor); 262 break; 263 264 case CHIP_ARCTURUS: 265 ret = smu_i2c_eeprom_init(&adev->smu, &control->eeprom_accessor); 266 break; 267 268 default: 269 return 0; 270 } 271 272 if (ret) { 273 DRM_ERROR("Failed to init I2C controller, ret:%d", ret); 274 return ret; 275 } 276 277 msg.addr = control->i2c_address; 278 279 /* Read/Create table header from EEPROM address 0 */ 280 ret = i2c_transfer(&control->eeprom_accessor, &msg, 1); 281 if (ret < 1) { 282 DRM_ERROR("Failed to read EEPROM table header, ret:%d", ret); 283 return ret; 284 } 285 286 __decode_table_header_from_buff(hdr, &buff[2]); 287 288 if (hdr->header == EEPROM_TABLE_HDR_VAL) { 289 control->num_recs = (hdr->tbl_size - EEPROM_TABLE_HEADER_SIZE) / 290 EEPROM_TABLE_RECORD_SIZE; 291 control->tbl_byte_sum = __calc_hdr_byte_sum(control); 292 control->next_addr = EEPROM_RECORD_START; 293 294 DRM_DEBUG_DRIVER("Found existing EEPROM table with %d records", 295 control->num_recs); 296 297 } else { 298 DRM_INFO("Creating new EEPROM table"); 299 300 ret = amdgpu_ras_eeprom_reset_table(control); 301 } 302 303 return ret == 1 ? 0 : -EIO; 304 } 305 306 void amdgpu_ras_eeprom_fini(struct amdgpu_ras_eeprom_control *control) 307 { 308 struct amdgpu_device *adev = to_amdgpu_device(control); 309 310 switch (adev->asic_type) { 311 case CHIP_VEGA20: 312 smu_v11_0_i2c_eeprom_control_fini(&control->eeprom_accessor); 313 break; 314 case CHIP_ARCTURUS: 315 smu_i2c_eeprom_fini(&adev->smu, &control->eeprom_accessor); 316 break; 317 318 default: 319 return; 320 } 321 } 322 323 static void __encode_table_record_to_buff(struct amdgpu_ras_eeprom_control *control, 324 struct eeprom_table_record *record, 325 unsigned char *buff) 326 { 327 __le64 tmp = 0; 328 int i = 0; 329 330 /* Next are all record fields according to EEPROM page spec in LE foramt */ 331 buff[i++] = record->err_type; 332 333 buff[i++] = record->bank; 334 335 tmp = cpu_to_le64(record->ts); 336 memcpy(buff + i, &tmp, 8); 337 i += 8; 338 339 tmp = cpu_to_le64((record->offset & 0xffffffffffff)); 340 memcpy(buff + i, &tmp, 6); 341 i += 6; 342 343 buff[i++] = record->mem_channel; 344 buff[i++] = record->mcumc_id; 345 346 tmp = cpu_to_le64((record->retired_page & 0xffffffffffff)); 347 memcpy(buff + i, &tmp, 6); 348 } 349 350 static void __decode_table_record_from_buff(struct amdgpu_ras_eeprom_control *control, 351 struct eeprom_table_record *record, 352 unsigned char *buff) 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 record->err_type = buff[i++]; 359 360 record->bank = buff[i++]; 361 362 memcpy(&tmp, buff + i, 8); 363 record->ts = le64_to_cpu(tmp); 364 i += 8; 365 366 memcpy(&tmp, buff + i, 6); 367 record->offset = (le64_to_cpu(tmp) & 0xffffffffffff); 368 i += 6; 369 370 record->mem_channel = buff[i++]; 371 record->mcumc_id = buff[i++]; 372 373 memcpy(&tmp, buff + i, 6); 374 record->retired_page = (le64_to_cpu(tmp) & 0xffffffffffff); 375 } 376 377 /* 378 * When reaching end of EEPROM memory jump back to 0 record address 379 * When next record access will go beyond EEPROM page boundary modify bits A17/A8 380 * in I2C selector to go to next page 381 */ 382 static uint32_t __correct_eeprom_dest_address(uint32_t curr_address) 383 { 384 uint32_t next_address = curr_address + EEPROM_TABLE_RECORD_SIZE; 385 386 /* When all EEPROM memory used jump back to 0 address */ 387 if (next_address > EEPROM_SIZE_BYTES) { 388 DRM_INFO("Reached end of EEPROM memory, jumping to 0 " 389 "and overriding old record"); 390 return EEPROM_RECORD_START; 391 } 392 393 /* 394 * To check if we overflow page boundary compare next address with 395 * current and see if bits 17/8 of the EEPROM address will change 396 * If they do start from the next 256b page 397 * 398 * https://www.st.com/resource/en/datasheet/m24m02-dr.pdf sec. 5.1.2 399 */ 400 if ((curr_address & EEPROM_ADDR_MSB_MASK) != (next_address & EEPROM_ADDR_MSB_MASK)) { 401 DRM_DEBUG_DRIVER("Reached end of EEPROM memory page, jumping to next: %lx", 402 (next_address & EEPROM_ADDR_MSB_MASK)); 403 404 return (next_address & EEPROM_ADDR_MSB_MASK); 405 } 406 407 return curr_address; 408 } 409 410 int amdgpu_ras_eeprom_process_recods(struct amdgpu_ras_eeprom_control *control, 411 struct eeprom_table_record *records, 412 bool write, 413 int num) 414 { 415 int i, ret = 0; 416 struct i2c_msg *msgs, *msg; 417 unsigned char *buffs, *buff; 418 struct eeprom_table_record *record; 419 struct amdgpu_device *adev = to_amdgpu_device(control); 420 421 if (adev->asic_type != CHIP_VEGA20 && adev->asic_type != CHIP_ARCTURUS) 422 return 0; 423 424 buffs = kcalloc(num, EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE, 425 GFP_KERNEL); 426 if (!buffs) 427 return -ENOMEM; 428 429 mutex_lock(&control->tbl_mutex); 430 431 msgs = kcalloc(num, sizeof(*msgs), GFP_KERNEL); 432 if (!msgs) { 433 ret = -ENOMEM; 434 goto free_buff; 435 } 436 437 /* In case of overflow just start from beginning to not lose newest records */ 438 if (write && (control->next_addr + EEPROM_TABLE_RECORD_SIZE * num > EEPROM_SIZE_BYTES)) 439 control->next_addr = EEPROM_RECORD_START; 440 441 442 /* 443 * TODO Currently makes EEPROM writes for each record, this creates 444 * internal fragmentation. Optimized the code to do full page write of 445 * 256b 446 */ 447 for (i = 0; i < num; i++) { 448 buff = &buffs[i * (EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE)]; 449 record = &records[i]; 450 msg = &msgs[i]; 451 452 control->next_addr = __correct_eeprom_dest_address(control->next_addr); 453 454 /* 455 * Update bits 16,17 of EEPROM address in I2C address by setting them 456 * to bits 1,2 of Device address byte 457 */ 458 msg->addr = control->i2c_address | 459 ((control->next_addr & EEPROM_ADDR_MSB_MASK) >> 15); 460 msg->flags = write ? 0 : I2C_M_RD; 461 msg->len = EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE; 462 msg->buf = buff; 463 464 /* Insert the EEPROM dest addess, bits 0-15 */ 465 buff[0] = ((control->next_addr >> 8) & 0xff); 466 buff[1] = (control->next_addr & 0xff); 467 468 /* EEPROM table content is stored in LE format */ 469 if (write) 470 __encode_table_record_to_buff(control, record, buff + EEPROM_ADDRESS_SIZE); 471 472 /* 473 * The destination EEPROM address might need to be corrected to account 474 * for page or entire memory wrapping 475 */ 476 control->next_addr += EEPROM_TABLE_RECORD_SIZE; 477 } 478 479 ret = i2c_transfer(&control->eeprom_accessor, msgs, num); 480 if (ret < 1) { 481 DRM_ERROR("Failed to process EEPROM table records, ret:%d", ret); 482 483 /* TODO Restore prev next EEPROM address ? */ 484 goto free_msgs; 485 } 486 487 488 if (!write) { 489 for (i = 0; i < num; i++) { 490 buff = &buffs[i*(EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE)]; 491 record = &records[i]; 492 493 __decode_table_record_from_buff(control, record, buff + EEPROM_ADDRESS_SIZE); 494 } 495 } 496 497 if (write) { 498 uint32_t old_hdr_byte_sum = __calc_hdr_byte_sum(control); 499 500 /* 501 * Update table header with size and CRC and account for table 502 * wrap around where the assumption is that we treat it as empty 503 * table 504 * 505 * TODO - Check the assumption is correct 506 */ 507 control->num_recs += num; 508 control->num_recs %= EEPROM_MAX_RECORD_NUM; 509 control->tbl_hdr.tbl_size += EEPROM_TABLE_RECORD_SIZE * num; 510 if (control->tbl_hdr.tbl_size > EEPROM_SIZE_BYTES) 511 control->tbl_hdr.tbl_size = EEPROM_TABLE_HEADER_SIZE + 512 control->num_recs * EEPROM_TABLE_RECORD_SIZE; 513 514 __update_tbl_checksum(control, records, num, old_hdr_byte_sum); 515 516 __update_table_header(control, buffs); 517 } else if (!__validate_tbl_checksum(control, records, num)) { 518 DRM_WARN("EEPROM Table checksum mismatch!"); 519 /* TODO Uncomment when EEPROM read/write is relliable */ 520 /* ret = -EIO; */ 521 } 522 523 free_msgs: 524 kfree(msgs); 525 526 free_buff: 527 kfree(buffs); 528 529 mutex_unlock(&control->tbl_mutex); 530 531 return ret == num ? 0 : -EIO; 532 } 533 534 /* Used for testing if bugs encountered */ 535 #if 0 536 void amdgpu_ras_eeprom_test(struct amdgpu_ras_eeprom_control *control) 537 { 538 int i; 539 struct eeprom_table_record *recs = kcalloc(1, sizeof(*recs), GFP_KERNEL); 540 541 if (!recs) 542 return; 543 544 for (i = 0; i < 1 ; i++) { 545 recs[i].address = 0xdeadbeef; 546 recs[i].retired_page = i; 547 } 548 549 if (!amdgpu_ras_eeprom_process_recods(control, recs, true, 1)) { 550 551 memset(recs, 0, sizeof(*recs) * 1); 552 553 control->next_addr = EEPROM_RECORD_START; 554 555 if (!amdgpu_ras_eeprom_process_recods(control, recs, false, 1)) { 556 for (i = 0; i < 1; i++) 557 DRM_INFO("rec.address :0x%llx, rec.retired_page :%llu", 558 recs[i].address, recs[i].retired_page); 559 } else 560 DRM_ERROR("Failed in reading from table"); 561 562 } else 563 DRM_ERROR("Failed in writing to table"); 564 } 565 #endif 566