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 #include <linux/pci.h> 24 25 #include "amdgpu.h" 26 #include "amdgpu_i2c.h" 27 #include "smu_v11_0_i2c.h" 28 #include "atom.h" 29 #include "amdgpu_fru_eeprom.h" 30 #include "amdgpu_eeprom.h" 31 32 #define FRU_EEPROM_MADDR 0x60000 33 #define I2C_PRODUCT_INFO_OFFSET 0xC0 34 35 static bool is_fru_eeprom_supported(struct amdgpu_device *adev) 36 { 37 /* Only server cards have the FRU EEPROM 38 * TODO: See if we can figure this out dynamically instead of 39 * having to parse VBIOS versions. 40 */ 41 struct atom_context *atom_ctx = adev->mode_info.atom_context; 42 43 /* The i2c access is blocked on VF 44 * TODO: Need other way to get the info 45 */ 46 if (amdgpu_sriov_vf(adev)) 47 return false; 48 49 /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification, 50 * we can use just the "DXXX" portion. If there were more models, we 51 * could convert the 3 characters to a hex integer and use a switch 52 * for ease/speed/readability. For now, 2 string comparisons are 53 * reasonable and not too expensive 54 */ 55 switch (adev->asic_type) { 56 case CHIP_VEGA20: 57 /* D161 and D163 are the VG20 server SKUs */ 58 if (strnstr(atom_ctx->vbios_version, "D161", 59 sizeof(atom_ctx->vbios_version)) || 60 strnstr(atom_ctx->vbios_version, "D163", 61 sizeof(atom_ctx->vbios_version))) 62 return true; 63 else 64 return false; 65 case CHIP_ALDEBARAN: 66 /* All Aldebaran SKUs have the FRU */ 67 return true; 68 case CHIP_SIENNA_CICHLID: 69 if (strnstr(atom_ctx->vbios_version, "D603", 70 sizeof(atom_ctx->vbios_version))) 71 return true; 72 else 73 return false; 74 default: 75 return false; 76 } 77 } 78 79 static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr, 80 unsigned char *buff) 81 { 82 int ret, size; 83 84 ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr, buff, 1); 85 if (ret < 1) { 86 DRM_WARN("FRU: Failed to get size field"); 87 return ret; 88 } 89 90 /* The size returned by the i2c requires subtraction of 0xC0 since the 91 * size apparently always reports as 0xC0+actual size. 92 */ 93 size = buff[0] - I2C_PRODUCT_INFO_OFFSET; 94 95 ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr + 1, buff, size); 96 if (ret < 1) { 97 DRM_WARN("FRU: Failed to get data field"); 98 return ret; 99 } 100 101 return size; 102 } 103 104 int amdgpu_fru_get_product_info(struct amdgpu_device *adev) 105 { 106 unsigned char buff[AMDGPU_PRODUCT_NAME_LEN+2]; 107 u32 addrptr; 108 int size, len; 109 int offset = 2; 110 111 if (!is_fru_eeprom_supported(adev)) 112 return 0; 113 114 if (adev->asic_type == CHIP_ALDEBARAN) 115 offset = 0; 116 117 /* If algo exists, it means that the i2c_adapter's initialized */ 118 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) { 119 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized"); 120 return -ENODEV; 121 } 122 123 /* There's a lot of repetition here. This is due to the FRU having 124 * variable-length fields. To get the information, we have to find the 125 * size of each field, and then keep reading along and reading along 126 * until we get all of the data that we want. We use addrptr to track 127 * the address as we go 128 */ 129 130 /* The first fields are all of size 1-byte, from 0-7 are offsets that 131 * contain information that isn't useful to us. 132 * Bytes 8-a are all 1-byte and refer to the size of the entire struct, 133 * and the language field, so just start from 0xb, manufacturer size 134 */ 135 addrptr = FRU_EEPROM_MADDR + 0xb; 136 size = amdgpu_fru_read_eeprom(adev, addrptr, buff); 137 if (size < 1) { 138 DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size); 139 return -EINVAL; 140 } 141 142 /* Increment the addrptr by the size of the field, and 1 due to the 143 * size field being 1 byte. This pattern continues below. 144 */ 145 addrptr += size + 1; 146 size = amdgpu_fru_read_eeprom(adev, addrptr, buff); 147 if (size < 1) { 148 DRM_ERROR("Failed to read FRU product name, ret:%d", size); 149 return -EINVAL; 150 } 151 152 len = size; 153 if (len >= AMDGPU_PRODUCT_NAME_LEN) { 154 DRM_WARN("FRU Product Name is larger than %d characters. This is likely a mistake", 155 AMDGPU_PRODUCT_NAME_LEN); 156 len = AMDGPU_PRODUCT_NAME_LEN - 1; 157 } 158 /* Start at 2 due to buff using fields 0 and 1 for the address */ 159 memcpy(adev->product_name, &buff[offset], len); 160 adev->product_name[len] = '\0'; 161 162 addrptr += size + 1; 163 size = amdgpu_fru_read_eeprom(adev, addrptr, buff); 164 if (size < 1) { 165 DRM_ERROR("Failed to read FRU product number, ret:%d", size); 166 return -EINVAL; 167 } 168 169 len = size; 170 /* Product number should only be 16 characters. Any more, 171 * and something could be wrong. Cap it at 16 to be safe 172 */ 173 if (len >= sizeof(adev->product_number)) { 174 DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake"); 175 len = sizeof(adev->product_number) - 1; 176 } 177 memcpy(adev->product_number, &buff[offset], len); 178 adev->product_number[len] = '\0'; 179 180 addrptr += size + 1; 181 size = amdgpu_fru_read_eeprom(adev, addrptr, buff); 182 183 if (size < 1) { 184 DRM_ERROR("Failed to read FRU product version, ret:%d", size); 185 return -EINVAL; 186 } 187 188 addrptr += size + 1; 189 size = amdgpu_fru_read_eeprom(adev, addrptr, buff); 190 191 if (size < 1) { 192 DRM_ERROR("Failed to read FRU serial number, ret:%d", size); 193 return -EINVAL; 194 } 195 196 len = size; 197 /* Serial number should only be 16 characters. Any more, 198 * and something could be wrong. Cap it at 16 to be safe 199 */ 200 if (len >= sizeof(adev->serial)) { 201 DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake"); 202 len = sizeof(adev->serial) - 1; 203 } 204 memcpy(adev->serial, &buff[offset], len); 205 adev->serial[len] = '\0'; 206 207 return 0; 208 } 209