164f55e62SAndrey Grodzovsky /* 264f55e62SAndrey Grodzovsky * Copyright 2019 Advanced Micro Devices, Inc. 364f55e62SAndrey Grodzovsky * 464f55e62SAndrey Grodzovsky * Permission is hereby granted, free of charge, to any person obtaining a 564f55e62SAndrey Grodzovsky * copy of this software and associated documentation files (the "Software"), 664f55e62SAndrey Grodzovsky * to deal in the Software without restriction, including without limitation 764f55e62SAndrey Grodzovsky * the rights to use, copy, modify, merge, publish, distribute, sublicense, 864f55e62SAndrey Grodzovsky * and/or sell copies of the Software, and to permit persons to whom the 964f55e62SAndrey Grodzovsky * Software is furnished to do so, subject to the following conditions: 1064f55e62SAndrey Grodzovsky * 1164f55e62SAndrey Grodzovsky * The above copyright notice and this permission notice shall be included in 1264f55e62SAndrey Grodzovsky * all copies or substantial portions of the Software. 1364f55e62SAndrey Grodzovsky * 1464f55e62SAndrey Grodzovsky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1564f55e62SAndrey Grodzovsky * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1664f55e62SAndrey Grodzovsky * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1764f55e62SAndrey Grodzovsky * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 1864f55e62SAndrey Grodzovsky * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 1964f55e62SAndrey Grodzovsky * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 2064f55e62SAndrey Grodzovsky * OTHER DEALINGS IN THE SOFTWARE. 2164f55e62SAndrey Grodzovsky * 2264f55e62SAndrey Grodzovsky */ 2364f55e62SAndrey Grodzovsky 2464f55e62SAndrey Grodzovsky #include "amdgpu_ras_eeprom.h" 2564f55e62SAndrey Grodzovsky #include "amdgpu.h" 2664f55e62SAndrey Grodzovsky #include "amdgpu_ras.h" 2764f55e62SAndrey Grodzovsky #include <linux/bits.h> 28ef1caf48SJohn Clements #include "atom.h" 2964f55e62SAndrey Grodzovsky 305985ebbeSJohn Clements #define EEPROM_I2C_TARGET_ADDR_VEGA20 0xA0 31ef1caf48SJohn Clements #define EEPROM_I2C_TARGET_ADDR_ARCTURUS 0xA8 32ef1caf48SJohn Clements #define EEPROM_I2C_TARGET_ADDR_ARCTURUS_D342 0xA0 3364f55e62SAndrey Grodzovsky 3464f55e62SAndrey Grodzovsky /* 3564f55e62SAndrey Grodzovsky * The 2 macros bellow represent the actual size in bytes that 3664f55e62SAndrey Grodzovsky * those entities occupy in the EEPROM memory. 3764f55e62SAndrey Grodzovsky * EEPROM_TABLE_RECORD_SIZE is different than sizeof(eeprom_table_record) which 3864f55e62SAndrey Grodzovsky * uses uint64 to store 6b fields such as retired_page. 3964f55e62SAndrey Grodzovsky */ 4064f55e62SAndrey Grodzovsky #define EEPROM_TABLE_HEADER_SIZE 20 4164f55e62SAndrey Grodzovsky #define EEPROM_TABLE_RECORD_SIZE 24 4264f55e62SAndrey Grodzovsky 4364f55e62SAndrey Grodzovsky #define EEPROM_ADDRESS_SIZE 0x2 4464f55e62SAndrey Grodzovsky 4564f55e62SAndrey Grodzovsky /* Table hdr is 'AMDR' */ 4664f55e62SAndrey Grodzovsky #define EEPROM_TABLE_HDR_VAL 0x414d4452 4764f55e62SAndrey Grodzovsky #define EEPROM_TABLE_VER 0x00010000 4864f55e62SAndrey Grodzovsky 491d6a9d12SGuchun Chen /* Bad GPU tag ‘BADG’ */ 501d6a9d12SGuchun Chen #define EEPROM_TABLE_HDR_BAD 0x42414447 511d6a9d12SGuchun Chen 5264f55e62SAndrey Grodzovsky /* Assume 2 Mbit size */ 5364f55e62SAndrey Grodzovsky #define EEPROM_SIZE_BYTES 256000 5464f55e62SAndrey Grodzovsky #define EEPROM_PAGE__SIZE_BYTES 256 5564f55e62SAndrey Grodzovsky #define EEPROM_HDR_START 0 5664f55e62SAndrey Grodzovsky #define EEPROM_RECORD_START (EEPROM_HDR_START + EEPROM_TABLE_HEADER_SIZE) 5764f55e62SAndrey Grodzovsky #define EEPROM_MAX_RECORD_NUM ((EEPROM_SIZE_BYTES - EEPROM_TABLE_HEADER_SIZE) / EEPROM_TABLE_RECORD_SIZE) 5864f55e62SAndrey Grodzovsky #define EEPROM_ADDR_MSB_MASK GENMASK(17, 8) 5964f55e62SAndrey Grodzovsky 6064f55e62SAndrey Grodzovsky #define to_amdgpu_device(x) (container_of(x, struct amdgpu_ras, eeprom_control))->adev 6164f55e62SAndrey Grodzovsky 62ef1caf48SJohn Clements static bool __get_eeprom_i2c_addr_arct(struct amdgpu_device *adev, 63ef1caf48SJohn Clements uint16_t *i2c_addr) 64ef1caf48SJohn Clements { 65ef1caf48SJohn Clements struct atom_context *atom_ctx = adev->mode_info.atom_context; 66ef1caf48SJohn Clements 67ef1caf48SJohn Clements if (!i2c_addr || !atom_ctx) 68ef1caf48SJohn Clements return false; 69ef1caf48SJohn Clements 70ef1caf48SJohn Clements if (strnstr(atom_ctx->vbios_version, 71ef1caf48SJohn Clements "D342", 72ef1caf48SJohn Clements sizeof(atom_ctx->vbios_version))) 73ef1caf48SJohn Clements *i2c_addr = EEPROM_I2C_TARGET_ADDR_ARCTURUS_D342; 74ef1caf48SJohn Clements else 75ef1caf48SJohn Clements *i2c_addr = EEPROM_I2C_TARGET_ADDR_ARCTURUS; 76ef1caf48SJohn Clements 77ef1caf48SJohn Clements return true; 78ef1caf48SJohn Clements } 79ef1caf48SJohn Clements 80ef1caf48SJohn Clements static bool __get_eeprom_i2c_addr(struct amdgpu_device *adev, 81ef1caf48SJohn Clements uint16_t *i2c_addr) 82ef1caf48SJohn Clements { 83ef1caf48SJohn Clements if (!i2c_addr) 84ef1caf48SJohn Clements return false; 85ef1caf48SJohn Clements 86ef1caf48SJohn Clements switch (adev->asic_type) { 87ef1caf48SJohn Clements case CHIP_VEGA20: 88ef1caf48SJohn Clements *i2c_addr = EEPROM_I2C_TARGET_ADDR_VEGA20; 89ef1caf48SJohn Clements break; 90ef1caf48SJohn Clements 91ef1caf48SJohn Clements case CHIP_ARCTURUS: 92ef1caf48SJohn Clements return __get_eeprom_i2c_addr_arct(adev, i2c_addr); 93ef1caf48SJohn Clements 94ef1caf48SJohn Clements default: 95ef1caf48SJohn Clements return false; 96ef1caf48SJohn Clements } 97ef1caf48SJohn Clements 98ef1caf48SJohn Clements return true; 99ef1caf48SJohn Clements } 100ef1caf48SJohn Clements 10164f55e62SAndrey Grodzovsky static void __encode_table_header_to_buff(struct amdgpu_ras_eeprom_table_header *hdr, 10264f55e62SAndrey Grodzovsky unsigned char *buff) 10364f55e62SAndrey Grodzovsky { 10464f55e62SAndrey Grodzovsky uint32_t *pp = (uint32_t *) buff; 10564f55e62SAndrey Grodzovsky 10664f55e62SAndrey Grodzovsky pp[0] = cpu_to_le32(hdr->header); 10764f55e62SAndrey Grodzovsky pp[1] = cpu_to_le32(hdr->version); 10864f55e62SAndrey Grodzovsky pp[2] = cpu_to_le32(hdr->first_rec_offset); 10964f55e62SAndrey Grodzovsky pp[3] = cpu_to_le32(hdr->tbl_size); 11064f55e62SAndrey Grodzovsky pp[4] = cpu_to_le32(hdr->checksum); 11164f55e62SAndrey Grodzovsky } 11264f55e62SAndrey Grodzovsky 11364f55e62SAndrey Grodzovsky static void __decode_table_header_from_buff(struct amdgpu_ras_eeprom_table_header *hdr, 11464f55e62SAndrey Grodzovsky unsigned char *buff) 11564f55e62SAndrey Grodzovsky { 11664f55e62SAndrey Grodzovsky uint32_t *pp = (uint32_t *)buff; 11764f55e62SAndrey Grodzovsky 11864f55e62SAndrey Grodzovsky hdr->header = le32_to_cpu(pp[0]); 11964f55e62SAndrey Grodzovsky hdr->version = le32_to_cpu(pp[1]); 12064f55e62SAndrey Grodzovsky hdr->first_rec_offset = le32_to_cpu(pp[2]); 12164f55e62SAndrey Grodzovsky hdr->tbl_size = le32_to_cpu(pp[3]); 12264f55e62SAndrey Grodzovsky hdr->checksum = le32_to_cpu(pp[4]); 12364f55e62SAndrey Grodzovsky } 12464f55e62SAndrey Grodzovsky 12564f55e62SAndrey Grodzovsky static int __update_table_header(struct amdgpu_ras_eeprom_control *control, 12664f55e62SAndrey Grodzovsky unsigned char *buff) 12764f55e62SAndrey Grodzovsky { 12864f55e62SAndrey Grodzovsky int ret = 0; 1299015d60cSAndrey Grodzovsky struct amdgpu_device *adev = to_amdgpu_device(control); 13064f55e62SAndrey Grodzovsky struct i2c_msg msg = { 1315985ebbeSJohn Clements .addr = 0, 13264f55e62SAndrey Grodzovsky .flags = 0, 13364f55e62SAndrey Grodzovsky .len = EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE, 13464f55e62SAndrey Grodzovsky .buf = buff, 13564f55e62SAndrey Grodzovsky }; 13664f55e62SAndrey Grodzovsky 13764f55e62SAndrey Grodzovsky 13864f55e62SAndrey Grodzovsky *(uint16_t *)buff = EEPROM_HDR_START; 13964f55e62SAndrey Grodzovsky __encode_table_header_to_buff(&control->tbl_hdr, buff + EEPROM_ADDRESS_SIZE); 14064f55e62SAndrey Grodzovsky 1415985ebbeSJohn Clements msg.addr = control->i2c_address; 1425985ebbeSJohn Clements 1439015d60cSAndrey Grodzovsky ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1); 14464f55e62SAndrey Grodzovsky if (ret < 1) 14564f55e62SAndrey Grodzovsky DRM_ERROR("Failed to write EEPROM table header, ret:%d", ret); 14664f55e62SAndrey Grodzovsky 14764f55e62SAndrey Grodzovsky return ret; 14864f55e62SAndrey Grodzovsky } 14964f55e62SAndrey Grodzovsky 150db338e16SAndrey Grodzovsky static uint32_t __calc_hdr_byte_sum(struct amdgpu_ras_eeprom_control *control) 151db338e16SAndrey Grodzovsky { 152db338e16SAndrey Grodzovsky int i; 153db338e16SAndrey Grodzovsky uint32_t tbl_sum = 0; 154db338e16SAndrey Grodzovsky 155db338e16SAndrey Grodzovsky /* Header checksum, skip checksum field in the calculation */ 156db338e16SAndrey Grodzovsky for (i = 0; i < sizeof(control->tbl_hdr) - sizeof(control->tbl_hdr.checksum); i++) 157db338e16SAndrey Grodzovsky tbl_sum += *(((unsigned char *)&control->tbl_hdr) + i); 158db338e16SAndrey Grodzovsky 159db338e16SAndrey Grodzovsky return tbl_sum; 160db338e16SAndrey Grodzovsky } 161db338e16SAndrey Grodzovsky 162db338e16SAndrey Grodzovsky static uint32_t __calc_recs_byte_sum(struct eeprom_table_record *records, 163db338e16SAndrey Grodzovsky int num) 164db338e16SAndrey Grodzovsky { 165db338e16SAndrey Grodzovsky int i, j; 166db338e16SAndrey Grodzovsky uint32_t tbl_sum = 0; 167db338e16SAndrey Grodzovsky 168db338e16SAndrey Grodzovsky /* Records checksum */ 169db338e16SAndrey Grodzovsky for (i = 0; i < num; i++) { 170db338e16SAndrey Grodzovsky struct eeprom_table_record *record = &records[i]; 171db338e16SAndrey Grodzovsky 172db338e16SAndrey Grodzovsky for (j = 0; j < sizeof(*record); j++) { 173db338e16SAndrey Grodzovsky tbl_sum += *(((unsigned char *)record) + j); 174db338e16SAndrey Grodzovsky } 175db338e16SAndrey Grodzovsky } 176db338e16SAndrey Grodzovsky 177db338e16SAndrey Grodzovsky return tbl_sum; 178db338e16SAndrey Grodzovsky } 179db338e16SAndrey Grodzovsky 180db338e16SAndrey Grodzovsky static inline uint32_t __calc_tbl_byte_sum(struct amdgpu_ras_eeprom_control *control, 181db338e16SAndrey Grodzovsky struct eeprom_table_record *records, int num) 182db338e16SAndrey Grodzovsky { 183db338e16SAndrey Grodzovsky return __calc_hdr_byte_sum(control) + __calc_recs_byte_sum(records, num); 184db338e16SAndrey Grodzovsky } 185db338e16SAndrey Grodzovsky 186db338e16SAndrey Grodzovsky /* Checksum = 256 -((sum of all table entries) mod 256) */ 187db338e16SAndrey Grodzovsky static void __update_tbl_checksum(struct amdgpu_ras_eeprom_control *control, 188db338e16SAndrey Grodzovsky struct eeprom_table_record *records, int num, 189db338e16SAndrey Grodzovsky uint32_t old_hdr_byte_sum) 190db338e16SAndrey Grodzovsky { 191db338e16SAndrey Grodzovsky /* 192db338e16SAndrey Grodzovsky * This will update the table sum with new records. 193db338e16SAndrey Grodzovsky * 194db338e16SAndrey Grodzovsky * TODO: What happens when the EEPROM table is to be wrapped around 195db338e16SAndrey Grodzovsky * and old records from start will get overridden. 196db338e16SAndrey Grodzovsky */ 197db338e16SAndrey Grodzovsky 198db338e16SAndrey Grodzovsky /* need to recalculate updated header byte sum */ 199db338e16SAndrey Grodzovsky control->tbl_byte_sum -= old_hdr_byte_sum; 200db338e16SAndrey Grodzovsky control->tbl_byte_sum += __calc_tbl_byte_sum(control, records, num); 201db338e16SAndrey Grodzovsky 202db338e16SAndrey Grodzovsky control->tbl_hdr.checksum = 256 - (control->tbl_byte_sum % 256); 203db338e16SAndrey Grodzovsky } 204db338e16SAndrey Grodzovsky 205db338e16SAndrey Grodzovsky /* table sum mod 256 + checksum must equals 256 */ 206db338e16SAndrey Grodzovsky static bool __validate_tbl_checksum(struct amdgpu_ras_eeprom_control *control, 207db338e16SAndrey Grodzovsky struct eeprom_table_record *records, int num) 208db338e16SAndrey Grodzovsky { 209db338e16SAndrey Grodzovsky control->tbl_byte_sum = __calc_tbl_byte_sum(control, records, num); 210db338e16SAndrey Grodzovsky 211db338e16SAndrey Grodzovsky if (control->tbl_hdr.checksum + (control->tbl_byte_sum % 256) != 256) { 212db338e16SAndrey Grodzovsky DRM_WARN("Checksum mismatch, checksum: %u ", control->tbl_hdr.checksum); 213db338e16SAndrey Grodzovsky return false; 214db338e16SAndrey Grodzovsky } 215db338e16SAndrey Grodzovsky 216db338e16SAndrey Grodzovsky return true; 217db338e16SAndrey Grodzovsky } 21864f55e62SAndrey Grodzovsky 219d01b400bSAndrey Grodzovsky int amdgpu_ras_eeprom_reset_table(struct amdgpu_ras_eeprom_control *control) 220d01b400bSAndrey Grodzovsky { 221d01b400bSAndrey Grodzovsky unsigned char buff[EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE] = { 0 }; 222d01b400bSAndrey Grodzovsky struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 223db338e16SAndrey Grodzovsky int ret = 0; 224db338e16SAndrey Grodzovsky 225db338e16SAndrey Grodzovsky mutex_lock(&control->tbl_mutex); 226d01b400bSAndrey Grodzovsky 227d01b400bSAndrey Grodzovsky hdr->header = EEPROM_TABLE_HDR_VAL; 228d01b400bSAndrey Grodzovsky hdr->version = EEPROM_TABLE_VER; 229d01b400bSAndrey Grodzovsky hdr->first_rec_offset = EEPROM_RECORD_START; 230d01b400bSAndrey Grodzovsky hdr->tbl_size = EEPROM_TABLE_HEADER_SIZE; 231d01b400bSAndrey Grodzovsky 232db338e16SAndrey Grodzovsky control->tbl_byte_sum = 0; 233db338e16SAndrey Grodzovsky __update_tbl_checksum(control, NULL, 0, 0); 234db338e16SAndrey Grodzovsky control->next_addr = EEPROM_RECORD_START; 235db338e16SAndrey Grodzovsky 236db338e16SAndrey Grodzovsky ret = __update_table_header(control, buff); 237db338e16SAndrey Grodzovsky 238db338e16SAndrey Grodzovsky mutex_unlock(&control->tbl_mutex); 239db338e16SAndrey Grodzovsky 240db338e16SAndrey Grodzovsky return ret; 241db338e16SAndrey Grodzovsky 242d01b400bSAndrey Grodzovsky } 243d01b400bSAndrey Grodzovsky 244b82e65a9SGuchun Chen int amdgpu_ras_eeprom_init(struct amdgpu_ras_eeprom_control *control, 245b82e65a9SGuchun Chen bool *exceed_err_limit) 24664f55e62SAndrey Grodzovsky { 24764f55e62SAndrey Grodzovsky int ret = 0; 24864f55e62SAndrey Grodzovsky struct amdgpu_device *adev = to_amdgpu_device(control); 24964f55e62SAndrey Grodzovsky unsigned char buff[EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE] = { 0 }; 25064f55e62SAndrey Grodzovsky struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 25164f55e62SAndrey Grodzovsky struct i2c_msg msg = { 2525985ebbeSJohn Clements .addr = 0, 25364f55e62SAndrey Grodzovsky .flags = I2C_M_RD, 25464f55e62SAndrey Grodzovsky .len = EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE, 25564f55e62SAndrey Grodzovsky .buf = buff, 25664f55e62SAndrey Grodzovsky }; 25764f55e62SAndrey Grodzovsky 258b82e65a9SGuchun Chen *exceed_err_limit = false; 259b82e65a9SGuchun Chen 2609015d60cSAndrey Grodzovsky /* Verify i2c adapter is initialized */ 2619015d60cSAndrey Grodzovsky if (!adev->pm.smu_i2c.algo) 2629015d60cSAndrey Grodzovsky return -ENOENT; 2639015d60cSAndrey Grodzovsky 264ef1caf48SJohn Clements if (!__get_eeprom_i2c_addr(adev, &control->i2c_address)) 265ef1caf48SJohn Clements return -EINVAL; 266ef1caf48SJohn Clements 26764f55e62SAndrey Grodzovsky mutex_init(&control->tbl_mutex); 26864f55e62SAndrey Grodzovsky 2695985ebbeSJohn Clements msg.addr = control->i2c_address; 27064f55e62SAndrey Grodzovsky /* Read/Create table header from EEPROM address 0 */ 2719015d60cSAndrey Grodzovsky ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1); 27264f55e62SAndrey Grodzovsky if (ret < 1) { 27364f55e62SAndrey Grodzovsky DRM_ERROR("Failed to read EEPROM table header, ret:%d", ret); 27464f55e62SAndrey Grodzovsky return ret; 27564f55e62SAndrey Grodzovsky } 27664f55e62SAndrey Grodzovsky 27764f55e62SAndrey Grodzovsky __decode_table_header_from_buff(hdr, &buff[2]); 27864f55e62SAndrey Grodzovsky 27964f55e62SAndrey Grodzovsky if (hdr->header == EEPROM_TABLE_HDR_VAL) { 28064f55e62SAndrey Grodzovsky control->num_recs = (hdr->tbl_size - EEPROM_TABLE_HEADER_SIZE) / 28164f55e62SAndrey Grodzovsky EEPROM_TABLE_RECORD_SIZE; 282db338e16SAndrey Grodzovsky control->tbl_byte_sum = __calc_hdr_byte_sum(control); 283db338e16SAndrey Grodzovsky control->next_addr = EEPROM_RECORD_START; 284db338e16SAndrey Grodzovsky 28564f55e62SAndrey Grodzovsky DRM_DEBUG_DRIVER("Found existing EEPROM table with %d records", 28664f55e62SAndrey Grodzovsky control->num_recs); 28764f55e62SAndrey Grodzovsky 288b82e65a9SGuchun Chen } else if ((hdr->header == EEPROM_TABLE_HDR_BAD) && 289b82e65a9SGuchun Chen (amdgpu_bad_page_threshold != 0)) { 290b82e65a9SGuchun Chen *exceed_err_limit = true; 291b82e65a9SGuchun Chen dev_err(adev->dev, "Exceeding the bad_page_threshold parameter, " 292b82e65a9SGuchun Chen "disabling the GPU.\n"); 29364f55e62SAndrey Grodzovsky } else { 29464f55e62SAndrey Grodzovsky DRM_INFO("Creating new EEPROM table"); 29564f55e62SAndrey Grodzovsky 296d01b400bSAndrey Grodzovsky ret = amdgpu_ras_eeprom_reset_table(control); 29764f55e62SAndrey Grodzovsky } 29864f55e62SAndrey Grodzovsky 29964f55e62SAndrey Grodzovsky return ret == 1 ? 0 : -EIO; 30064f55e62SAndrey Grodzovsky } 30164f55e62SAndrey Grodzovsky 30264f55e62SAndrey Grodzovsky static void __encode_table_record_to_buff(struct amdgpu_ras_eeprom_control *control, 30364f55e62SAndrey Grodzovsky struct eeprom_table_record *record, 30464f55e62SAndrey Grodzovsky unsigned char *buff) 30564f55e62SAndrey Grodzovsky { 30664f55e62SAndrey Grodzovsky __le64 tmp = 0; 30764f55e62SAndrey Grodzovsky int i = 0; 30864f55e62SAndrey Grodzovsky 30964f55e62SAndrey Grodzovsky /* Next are all record fields according to EEPROM page spec in LE foramt */ 31064f55e62SAndrey Grodzovsky buff[i++] = record->err_type; 31164f55e62SAndrey Grodzovsky 31264f55e62SAndrey Grodzovsky buff[i++] = record->bank; 31364f55e62SAndrey Grodzovsky 31464f55e62SAndrey Grodzovsky tmp = cpu_to_le64(record->ts); 31564f55e62SAndrey Grodzovsky memcpy(buff + i, &tmp, 8); 31664f55e62SAndrey Grodzovsky i += 8; 31764f55e62SAndrey Grodzovsky 31864f55e62SAndrey Grodzovsky tmp = cpu_to_le64((record->offset & 0xffffffffffff)); 31964f55e62SAndrey Grodzovsky memcpy(buff + i, &tmp, 6); 32064f55e62SAndrey Grodzovsky i += 6; 32164f55e62SAndrey Grodzovsky 32264f55e62SAndrey Grodzovsky buff[i++] = record->mem_channel; 32364f55e62SAndrey Grodzovsky buff[i++] = record->mcumc_id; 32464f55e62SAndrey Grodzovsky 32564f55e62SAndrey Grodzovsky tmp = cpu_to_le64((record->retired_page & 0xffffffffffff)); 32664f55e62SAndrey Grodzovsky memcpy(buff + i, &tmp, 6); 32764f55e62SAndrey Grodzovsky } 32864f55e62SAndrey Grodzovsky 32964f55e62SAndrey Grodzovsky static void __decode_table_record_from_buff(struct amdgpu_ras_eeprom_control *control, 33064f55e62SAndrey Grodzovsky struct eeprom_table_record *record, 33164f55e62SAndrey Grodzovsky unsigned char *buff) 33264f55e62SAndrey Grodzovsky { 33364f55e62SAndrey Grodzovsky __le64 tmp = 0; 33464f55e62SAndrey Grodzovsky int i = 0; 33564f55e62SAndrey Grodzovsky 33664f55e62SAndrey Grodzovsky /* Next are all record fields according to EEPROM page spec in LE foramt */ 33764f55e62SAndrey Grodzovsky record->err_type = buff[i++]; 33864f55e62SAndrey Grodzovsky 33964f55e62SAndrey Grodzovsky record->bank = buff[i++]; 34064f55e62SAndrey Grodzovsky 34164f55e62SAndrey Grodzovsky memcpy(&tmp, buff + i, 8); 34264f55e62SAndrey Grodzovsky record->ts = le64_to_cpu(tmp); 34364f55e62SAndrey Grodzovsky i += 8; 34464f55e62SAndrey Grodzovsky 34564f55e62SAndrey Grodzovsky memcpy(&tmp, buff + i, 6); 34664f55e62SAndrey Grodzovsky record->offset = (le64_to_cpu(tmp) & 0xffffffffffff); 34764f55e62SAndrey Grodzovsky i += 6; 34864f55e62SAndrey Grodzovsky 3494bc22340SAndrey Grodzovsky record->mem_channel = buff[i++]; 3504bc22340SAndrey Grodzovsky record->mcumc_id = buff[i++]; 35164f55e62SAndrey Grodzovsky 35264f55e62SAndrey Grodzovsky memcpy(&tmp, buff + i, 6); 35364f55e62SAndrey Grodzovsky record->retired_page = (le64_to_cpu(tmp) & 0xffffffffffff); 35464f55e62SAndrey Grodzovsky } 35564f55e62SAndrey Grodzovsky 35664f55e62SAndrey Grodzovsky /* 35764f55e62SAndrey Grodzovsky * When reaching end of EEPROM memory jump back to 0 record address 35864f55e62SAndrey Grodzovsky * When next record access will go beyond EEPROM page boundary modify bits A17/A8 35964f55e62SAndrey Grodzovsky * in I2C selector to go to next page 36064f55e62SAndrey Grodzovsky */ 36164f55e62SAndrey Grodzovsky static uint32_t __correct_eeprom_dest_address(uint32_t curr_address) 36264f55e62SAndrey Grodzovsky { 36364f55e62SAndrey Grodzovsky uint32_t next_address = curr_address + EEPROM_TABLE_RECORD_SIZE; 36464f55e62SAndrey Grodzovsky 36564f55e62SAndrey Grodzovsky /* When all EEPROM memory used jump back to 0 address */ 36664f55e62SAndrey Grodzovsky if (next_address > EEPROM_SIZE_BYTES) { 36764f55e62SAndrey Grodzovsky DRM_INFO("Reached end of EEPROM memory, jumping to 0 " 36864f55e62SAndrey Grodzovsky "and overriding old record"); 36964f55e62SAndrey Grodzovsky return EEPROM_RECORD_START; 37064f55e62SAndrey Grodzovsky } 37164f55e62SAndrey Grodzovsky 37264f55e62SAndrey Grodzovsky /* 37364f55e62SAndrey Grodzovsky * To check if we overflow page boundary compare next address with 37464f55e62SAndrey Grodzovsky * current and see if bits 17/8 of the EEPROM address will change 37564f55e62SAndrey Grodzovsky * If they do start from the next 256b page 37664f55e62SAndrey Grodzovsky * 37764f55e62SAndrey Grodzovsky * https://www.st.com/resource/en/datasheet/m24m02-dr.pdf sec. 5.1.2 37864f55e62SAndrey Grodzovsky */ 37964f55e62SAndrey Grodzovsky if ((curr_address & EEPROM_ADDR_MSB_MASK) != (next_address & EEPROM_ADDR_MSB_MASK)) { 38092ead9faSColin Ian King DRM_DEBUG_DRIVER("Reached end of EEPROM memory page, jumping to next: %lx", 38164f55e62SAndrey Grodzovsky (next_address & EEPROM_ADDR_MSB_MASK)); 38264f55e62SAndrey Grodzovsky 38364f55e62SAndrey Grodzovsky return (next_address & EEPROM_ADDR_MSB_MASK); 38464f55e62SAndrey Grodzovsky } 38564f55e62SAndrey Grodzovsky 38664f55e62SAndrey Grodzovsky return curr_address; 38764f55e62SAndrey Grodzovsky } 38864f55e62SAndrey Grodzovsky 389*e8fbaf03SGuchun Chen int amdgpu_ras_eeprom_check_err_threshold( 390*e8fbaf03SGuchun Chen struct amdgpu_ras_eeprom_control *control, 391*e8fbaf03SGuchun Chen bool *exceed_err_limit) 392*e8fbaf03SGuchun Chen { 393*e8fbaf03SGuchun Chen struct amdgpu_device *adev = to_amdgpu_device(control); 394*e8fbaf03SGuchun Chen unsigned char buff[EEPROM_ADDRESS_SIZE + 395*e8fbaf03SGuchun Chen EEPROM_TABLE_HEADER_SIZE] = { 0 }; 396*e8fbaf03SGuchun Chen struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr; 397*e8fbaf03SGuchun Chen struct i2c_msg msg = { 398*e8fbaf03SGuchun Chen .addr = control->i2c_address, 399*e8fbaf03SGuchun Chen .flags = I2C_M_RD, 400*e8fbaf03SGuchun Chen .len = EEPROM_ADDRESS_SIZE + EEPROM_TABLE_HEADER_SIZE, 401*e8fbaf03SGuchun Chen .buf = buff, 402*e8fbaf03SGuchun Chen }; 403*e8fbaf03SGuchun Chen int ret; 404*e8fbaf03SGuchun Chen 405*e8fbaf03SGuchun Chen *exceed_err_limit = false; 406*e8fbaf03SGuchun Chen 407*e8fbaf03SGuchun Chen /* read EEPROM table header */ 408*e8fbaf03SGuchun Chen mutex_lock(&control->tbl_mutex); 409*e8fbaf03SGuchun Chen ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1); 410*e8fbaf03SGuchun Chen if (ret < 1) { 411*e8fbaf03SGuchun Chen dev_err(adev->dev, "Failed to read EEPROM table header.\n"); 412*e8fbaf03SGuchun Chen goto err; 413*e8fbaf03SGuchun Chen } 414*e8fbaf03SGuchun Chen 415*e8fbaf03SGuchun Chen __decode_table_header_from_buff(hdr, &buff[2]); 416*e8fbaf03SGuchun Chen 417*e8fbaf03SGuchun Chen if (hdr->header == EEPROM_TABLE_HDR_BAD) { 418*e8fbaf03SGuchun Chen dev_warn(adev->dev, "This GPU is in BAD status."); 419*e8fbaf03SGuchun Chen dev_warn(adev->dev, "Please retire it or setting one bigger " 420*e8fbaf03SGuchun Chen "threshold value when reloading driver.\n"); 421*e8fbaf03SGuchun Chen *exceed_err_limit = true; 422*e8fbaf03SGuchun Chen } 423*e8fbaf03SGuchun Chen 424*e8fbaf03SGuchun Chen err: 425*e8fbaf03SGuchun Chen mutex_unlock(&control->tbl_mutex); 426*e8fbaf03SGuchun Chen return 0; 427*e8fbaf03SGuchun Chen } 428*e8fbaf03SGuchun Chen 42964f55e62SAndrey Grodzovsky int amdgpu_ras_eeprom_process_recods(struct amdgpu_ras_eeprom_control *control, 43064f55e62SAndrey Grodzovsky struct eeprom_table_record *records, 43164f55e62SAndrey Grodzovsky bool write, 43264f55e62SAndrey Grodzovsky int num) 43364f55e62SAndrey Grodzovsky { 43464f55e62SAndrey Grodzovsky int i, ret = 0; 4355222d261SGuchun Chen struct i2c_msg *msgs, *msg; 4365222d261SGuchun Chen unsigned char *buffs, *buff; 4379c06f91fSGuchun Chen bool sched_ras_recovery = false; 4385222d261SGuchun Chen struct eeprom_table_record *record; 43964f55e62SAndrey Grodzovsky struct amdgpu_device *adev = to_amdgpu_device(control); 4409c06f91fSGuchun Chen struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 44164f55e62SAndrey Grodzovsky 442cf52ecc8SAndrey Grodzovsky if (adev->asic_type != CHIP_VEGA20 && adev->asic_type != CHIP_ARCTURUS) 44364f55e62SAndrey Grodzovsky return 0; 44464f55e62SAndrey Grodzovsky 44564f55e62SAndrey Grodzovsky buffs = kcalloc(num, EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE, 44664f55e62SAndrey Grodzovsky GFP_KERNEL); 44764f55e62SAndrey Grodzovsky if (!buffs) 44864f55e62SAndrey Grodzovsky return -ENOMEM; 44964f55e62SAndrey Grodzovsky 45064f55e62SAndrey Grodzovsky mutex_lock(&control->tbl_mutex); 45164f55e62SAndrey Grodzovsky 45264f55e62SAndrey Grodzovsky msgs = kcalloc(num, sizeof(*msgs), GFP_KERNEL); 45364f55e62SAndrey Grodzovsky if (!msgs) { 45464f55e62SAndrey Grodzovsky ret = -ENOMEM; 45564f55e62SAndrey Grodzovsky goto free_buff; 45664f55e62SAndrey Grodzovsky } 45764f55e62SAndrey Grodzovsky 4589c06f91fSGuchun Chen /* 4599c06f91fSGuchun Chen * If saved bad pages number exceeds the bad page threshold for 4609c06f91fSGuchun Chen * the whole VRAM, update table header to mark the BAD GPU tag 4619c06f91fSGuchun Chen * and schedule one ras recovery after eeprom write is done, 4629c06f91fSGuchun Chen * this can avoid the missing for latest records. 4639c06f91fSGuchun Chen * 4649c06f91fSGuchun Chen * This new header will be picked up and checked in the bootup 4659c06f91fSGuchun Chen * by ras recovery, which may break bootup process to notify 4669c06f91fSGuchun Chen * user this GPU is in bad state and to retire such GPU for 4679c06f91fSGuchun Chen * further check. 4689c06f91fSGuchun Chen */ 4699c06f91fSGuchun Chen if (write && (amdgpu_bad_page_threshold != 0) && 4709c06f91fSGuchun Chen ((control->num_recs + num) >= ras->bad_page_cnt_threshold)) { 4719c06f91fSGuchun Chen dev_warn(adev->dev, 4729c06f91fSGuchun Chen "Saved bad pages(%d) reaches threshold value(%d).\n", 4739c06f91fSGuchun Chen control->num_recs + num, ras->bad_page_cnt_threshold); 4749c06f91fSGuchun Chen control->tbl_hdr.header = EEPROM_TABLE_HDR_BAD; 4759c06f91fSGuchun Chen sched_ras_recovery = true; 4769c06f91fSGuchun Chen } 4779c06f91fSGuchun Chen 47864f55e62SAndrey Grodzovsky /* In case of overflow just start from beginning to not lose newest records */ 47964f55e62SAndrey Grodzovsky if (write && (control->next_addr + EEPROM_TABLE_RECORD_SIZE * num > EEPROM_SIZE_BYTES)) 48064f55e62SAndrey Grodzovsky control->next_addr = EEPROM_RECORD_START; 48164f55e62SAndrey Grodzovsky 48264f55e62SAndrey Grodzovsky /* 48364f55e62SAndrey Grodzovsky * TODO Currently makes EEPROM writes for each record, this creates 48464f55e62SAndrey Grodzovsky * internal fragmentation. Optimized the code to do full page write of 48564f55e62SAndrey Grodzovsky * 256b 48664f55e62SAndrey Grodzovsky */ 48764f55e62SAndrey Grodzovsky for (i = 0; i < num; i++) { 4885222d261SGuchun Chen buff = &buffs[i * (EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE)]; 4895222d261SGuchun Chen record = &records[i]; 4905222d261SGuchun Chen msg = &msgs[i]; 49164f55e62SAndrey Grodzovsky 49264f55e62SAndrey Grodzovsky control->next_addr = __correct_eeprom_dest_address(control->next_addr); 49364f55e62SAndrey Grodzovsky 49464f55e62SAndrey Grodzovsky /* 49564f55e62SAndrey Grodzovsky * Update bits 16,17 of EEPROM address in I2C address by setting them 49664f55e62SAndrey Grodzovsky * to bits 1,2 of Device address byte 49764f55e62SAndrey Grodzovsky */ 4985985ebbeSJohn Clements msg->addr = control->i2c_address | 49964f55e62SAndrey Grodzovsky ((control->next_addr & EEPROM_ADDR_MSB_MASK) >> 15); 50064f55e62SAndrey Grodzovsky msg->flags = write ? 0 : I2C_M_RD; 50164f55e62SAndrey Grodzovsky msg->len = EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE; 50264f55e62SAndrey Grodzovsky msg->buf = buff; 50364f55e62SAndrey Grodzovsky 50464f55e62SAndrey Grodzovsky /* Insert the EEPROM dest addess, bits 0-15 */ 50564f55e62SAndrey Grodzovsky buff[0] = ((control->next_addr >> 8) & 0xff); 50664f55e62SAndrey Grodzovsky buff[1] = (control->next_addr & 0xff); 50764f55e62SAndrey Grodzovsky 50864f55e62SAndrey Grodzovsky /* EEPROM table content is stored in LE format */ 50964f55e62SAndrey Grodzovsky if (write) 51064f55e62SAndrey Grodzovsky __encode_table_record_to_buff(control, record, buff + EEPROM_ADDRESS_SIZE); 51164f55e62SAndrey Grodzovsky 51264f55e62SAndrey Grodzovsky /* 51364f55e62SAndrey Grodzovsky * The destination EEPROM address might need to be corrected to account 51464f55e62SAndrey Grodzovsky * for page or entire memory wrapping 51564f55e62SAndrey Grodzovsky */ 51664f55e62SAndrey Grodzovsky control->next_addr += EEPROM_TABLE_RECORD_SIZE; 51764f55e62SAndrey Grodzovsky } 51864f55e62SAndrey Grodzovsky 5199015d60cSAndrey Grodzovsky ret = i2c_transfer(&adev->pm.smu_i2c, msgs, num); 52064f55e62SAndrey Grodzovsky if (ret < 1) { 52164f55e62SAndrey Grodzovsky DRM_ERROR("Failed to process EEPROM table records, ret:%d", ret); 52264f55e62SAndrey Grodzovsky 52364f55e62SAndrey Grodzovsky /* TODO Restore prev next EEPROM address ? */ 52464f55e62SAndrey Grodzovsky goto free_msgs; 52564f55e62SAndrey Grodzovsky } 52664f55e62SAndrey Grodzovsky 52764f55e62SAndrey Grodzovsky 52864f55e62SAndrey Grodzovsky if (!write) { 52964f55e62SAndrey Grodzovsky for (i = 0; i < num; i++) { 5305222d261SGuchun Chen buff = &buffs[i*(EEPROM_ADDRESS_SIZE + EEPROM_TABLE_RECORD_SIZE)]; 5315222d261SGuchun Chen record = &records[i]; 53264f55e62SAndrey Grodzovsky 53364f55e62SAndrey Grodzovsky __decode_table_record_from_buff(control, record, buff + EEPROM_ADDRESS_SIZE); 53464f55e62SAndrey Grodzovsky } 53564f55e62SAndrey Grodzovsky } 53664f55e62SAndrey Grodzovsky 53764f55e62SAndrey Grodzovsky if (write) { 53864f55e62SAndrey Grodzovsky uint32_t old_hdr_byte_sum = __calc_hdr_byte_sum(control); 53964f55e62SAndrey Grodzovsky 54064f55e62SAndrey Grodzovsky /* 54164f55e62SAndrey Grodzovsky * Update table header with size and CRC and account for table 54264f55e62SAndrey Grodzovsky * wrap around where the assumption is that we treat it as empty 54364f55e62SAndrey Grodzovsky * table 54464f55e62SAndrey Grodzovsky * 54564f55e62SAndrey Grodzovsky * TODO - Check the assumption is correct 54664f55e62SAndrey Grodzovsky */ 54764f55e62SAndrey Grodzovsky control->num_recs += num; 54864f55e62SAndrey Grodzovsky control->num_recs %= EEPROM_MAX_RECORD_NUM; 54964f55e62SAndrey Grodzovsky control->tbl_hdr.tbl_size += EEPROM_TABLE_RECORD_SIZE * num; 55064f55e62SAndrey Grodzovsky if (control->tbl_hdr.tbl_size > EEPROM_SIZE_BYTES) 55164f55e62SAndrey Grodzovsky control->tbl_hdr.tbl_size = EEPROM_TABLE_HEADER_SIZE + 55264f55e62SAndrey Grodzovsky control->num_recs * EEPROM_TABLE_RECORD_SIZE; 55364f55e62SAndrey Grodzovsky 55464f55e62SAndrey Grodzovsky __update_tbl_checksum(control, records, num, old_hdr_byte_sum); 55564f55e62SAndrey Grodzovsky 55664f55e62SAndrey Grodzovsky __update_table_header(control, buffs); 5579c06f91fSGuchun Chen 5589c06f91fSGuchun Chen if (sched_ras_recovery) { 5599c06f91fSGuchun Chen /* 5609c06f91fSGuchun Chen * Before scheduling ras recovery, assert the related 5619c06f91fSGuchun Chen * flag first, which shall bypass common bad page 5629c06f91fSGuchun Chen * reservation execution in amdgpu_ras_reset_gpu. 5639c06f91fSGuchun Chen */ 5649c06f91fSGuchun Chen amdgpu_ras_get_context(adev)->flags |= 5659c06f91fSGuchun Chen AMDGPU_RAS_FLAG_SKIP_BAD_PAGE_RESV; 5669c06f91fSGuchun Chen 5679c06f91fSGuchun Chen dev_warn(adev->dev, "Conduct ras recovery due to bad " 5689c06f91fSGuchun Chen "page threshold reached.\n"); 5699c06f91fSGuchun Chen amdgpu_ras_reset_gpu(adev); 5709c06f91fSGuchun Chen } 57164f55e62SAndrey Grodzovsky } else if (!__validate_tbl_checksum(control, records, num)) { 57264f55e62SAndrey Grodzovsky DRM_WARN("EEPROM Table checksum mismatch!"); 57364f55e62SAndrey Grodzovsky /* TODO Uncomment when EEPROM read/write is relliable */ 57464f55e62SAndrey Grodzovsky /* ret = -EIO; */ 57564f55e62SAndrey Grodzovsky } 57664f55e62SAndrey Grodzovsky 57764f55e62SAndrey Grodzovsky free_msgs: 57864f55e62SAndrey Grodzovsky kfree(msgs); 57964f55e62SAndrey Grodzovsky 58064f55e62SAndrey Grodzovsky free_buff: 58164f55e62SAndrey Grodzovsky kfree(buffs); 58264f55e62SAndrey Grodzovsky 58364f55e62SAndrey Grodzovsky mutex_unlock(&control->tbl_mutex); 58464f55e62SAndrey Grodzovsky 58564f55e62SAndrey Grodzovsky return ret == num ? 0 : -EIO; 58664f55e62SAndrey Grodzovsky } 58764f55e62SAndrey Grodzovsky 588c84d4670SGuchun Chen inline uint32_t amdgpu_ras_eeprom_get_record_max_length(void) 589c84d4670SGuchun Chen { 590c84d4670SGuchun Chen return EEPROM_MAX_RECORD_NUM; 591c84d4670SGuchun Chen } 592c84d4670SGuchun Chen 59364f55e62SAndrey Grodzovsky /* Used for testing if bugs encountered */ 59464f55e62SAndrey Grodzovsky #if 0 59564f55e62SAndrey Grodzovsky void amdgpu_ras_eeprom_test(struct amdgpu_ras_eeprom_control *control) 59664f55e62SAndrey Grodzovsky { 59764f55e62SAndrey Grodzovsky int i; 59864f55e62SAndrey Grodzovsky struct eeprom_table_record *recs = kcalloc(1, sizeof(*recs), GFP_KERNEL); 59964f55e62SAndrey Grodzovsky 60064f55e62SAndrey Grodzovsky if (!recs) 60164f55e62SAndrey Grodzovsky return; 60264f55e62SAndrey Grodzovsky 60364f55e62SAndrey Grodzovsky for (i = 0; i < 1 ; i++) { 60464f55e62SAndrey Grodzovsky recs[i].address = 0xdeadbeef; 60564f55e62SAndrey Grodzovsky recs[i].retired_page = i; 60664f55e62SAndrey Grodzovsky } 60764f55e62SAndrey Grodzovsky 60864f55e62SAndrey Grodzovsky if (!amdgpu_ras_eeprom_process_recods(control, recs, true, 1)) { 60964f55e62SAndrey Grodzovsky 61064f55e62SAndrey Grodzovsky memset(recs, 0, sizeof(*recs) * 1); 61164f55e62SAndrey Grodzovsky 61264f55e62SAndrey Grodzovsky control->next_addr = EEPROM_RECORD_START; 61364f55e62SAndrey Grodzovsky 61464f55e62SAndrey Grodzovsky if (!amdgpu_ras_eeprom_process_recods(control, recs, false, 1)) { 61564f55e62SAndrey Grodzovsky for (i = 0; i < 1; i++) 61664f55e62SAndrey Grodzovsky DRM_INFO("rec.address :0x%llx, rec.retired_page :%llu", 61764f55e62SAndrey Grodzovsky recs[i].address, recs[i].retired_page); 61864f55e62SAndrey Grodzovsky } else 61964f55e62SAndrey Grodzovsky DRM_ERROR("Failed in reading from table"); 62064f55e62SAndrey Grodzovsky 62164f55e62SAndrey Grodzovsky } else 62264f55e62SAndrey Grodzovsky DRM_ERROR("Failed in writing to table"); 62364f55e62SAndrey Grodzovsky } 62464f55e62SAndrey Grodzovsky #endif 625