Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2019 Advanced Micro Devices, Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  */
0023 #include <linux/pci.h>
0024 
0025 #include "amdgpu.h"
0026 #include "amdgpu_i2c.h"
0027 #include "smu_v11_0_i2c.h"
0028 #include "atom.h"
0029 #include "amdgpu_fru_eeprom.h"
0030 #include "amdgpu_eeprom.h"
0031 
0032 #define FRU_EEPROM_MADDR        0x60000
0033 
0034 static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
0035 {
0036     /* Only server cards have the FRU EEPROM
0037      * TODO: See if we can figure this out dynamically instead of
0038      * having to parse VBIOS versions.
0039      */
0040     struct atom_context *atom_ctx = adev->mode_info.atom_context;
0041 
0042     /* The i2c access is blocked on VF
0043      * TODO: Need other way to get the info
0044      */
0045     if (amdgpu_sriov_vf(adev))
0046         return false;
0047 
0048     /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
0049      * we can use just the "DXXX" portion. If there were more models, we
0050      * could convert the 3 characters to a hex integer and use a switch
0051      * for ease/speed/readability. For now, 2 string comparisons are
0052      * reasonable and not too expensive
0053      */
0054     switch (adev->asic_type) {
0055     case CHIP_VEGA20:
0056         /* D161 and D163 are the VG20 server SKUs */
0057         if (strnstr(atom_ctx->vbios_version, "D161",
0058                 sizeof(atom_ctx->vbios_version)) ||
0059             strnstr(atom_ctx->vbios_version, "D163",
0060                 sizeof(atom_ctx->vbios_version)))
0061             return true;
0062         else
0063             return false;
0064     case CHIP_ALDEBARAN:
0065         /* All Aldebaran SKUs have the FRU */
0066         return true;
0067     case CHIP_SIENNA_CICHLID:
0068         if (strnstr(atom_ctx->vbios_version, "D603",
0069             sizeof(atom_ctx->vbios_version))) {
0070             if (strnstr(atom_ctx->vbios_version, "D603GLXE",
0071                 sizeof(atom_ctx->vbios_version)))
0072                 return false;
0073             else
0074                 return true;
0075         } else {
0076             return false;
0077         }
0078     default:
0079         return false;
0080     }
0081 }
0082 
0083 static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
0084                   unsigned char *buf, size_t buf_size)
0085 {
0086     int ret;
0087     u8 size;
0088 
0089     ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr, buf, 1);
0090     if (ret < 1) {
0091         DRM_WARN("FRU: Failed to get size field");
0092         return ret;
0093     }
0094 
0095     /* The size returned by the i2c requires subtraction of 0xC0 since the
0096      * size apparently always reports as 0xC0+actual size.
0097      */
0098     size = buf[0] & 0x3F;
0099     size = min_t(size_t, size, buf_size);
0100 
0101     ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr + 1,
0102                  buf, size);
0103     if (ret < 1) {
0104         DRM_WARN("FRU: Failed to get data field");
0105         return ret;
0106     }
0107 
0108     return size;
0109 }
0110 
0111 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
0112 {
0113     unsigned char buf[AMDGPU_PRODUCT_NAME_LEN];
0114     u32 addrptr;
0115     int size, len;
0116 
0117     if (!is_fru_eeprom_supported(adev))
0118         return 0;
0119 
0120     /* If algo exists, it means that the i2c_adapter's initialized */
0121     if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
0122         DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
0123         return -ENODEV;
0124     }
0125 
0126     /* There's a lot of repetition here. This is due to the FRU having
0127      * variable-length fields. To get the information, we have to find the
0128      * size of each field, and then keep reading along and reading along
0129      * until we get all of the data that we want. We use addrptr to track
0130      * the address as we go
0131      */
0132 
0133     /* The first fields are all of size 1-byte, from 0-7 are offsets that
0134      * contain information that isn't useful to us.
0135      * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
0136      * and the language field, so just start from 0xb, manufacturer size
0137      */
0138     addrptr = FRU_EEPROM_MADDR + 0xb;
0139     size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
0140     if (size < 1) {
0141         DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
0142         return -EINVAL;
0143     }
0144 
0145     /* Increment the addrptr by the size of the field, and 1 due to the
0146      * size field being 1 byte. This pattern continues below.
0147      */
0148     addrptr += size + 1;
0149     size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
0150     if (size < 1) {
0151         DRM_ERROR("Failed to read FRU product name, ret:%d", size);
0152         return -EINVAL;
0153     }
0154 
0155     len = size;
0156     if (len >= AMDGPU_PRODUCT_NAME_LEN) {
0157         DRM_WARN("FRU Product Name is larger than %d characters. This is likely a mistake",
0158                 AMDGPU_PRODUCT_NAME_LEN);
0159         len = AMDGPU_PRODUCT_NAME_LEN - 1;
0160     }
0161     memcpy(adev->product_name, buf, len);
0162     adev->product_name[len] = '\0';
0163 
0164     addrptr += size + 1;
0165     size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
0166     if (size < 1) {
0167         DRM_ERROR("Failed to read FRU product number, ret:%d", size);
0168         return -EINVAL;
0169     }
0170 
0171     len = size;
0172     /* Product number should only be 16 characters. Any more,
0173      * and something could be wrong. Cap it at 16 to be safe
0174      */
0175     if (len >= sizeof(adev->product_number)) {
0176         DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
0177         len = sizeof(adev->product_number) - 1;
0178     }
0179     memcpy(adev->product_number, buf, len);
0180     adev->product_number[len] = '\0';
0181 
0182     addrptr += size + 1;
0183     size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
0184 
0185     if (size < 1) {
0186         DRM_ERROR("Failed to read FRU product version, ret:%d", size);
0187         return -EINVAL;
0188     }
0189 
0190     addrptr += size + 1;
0191     size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
0192 
0193     if (size < 1) {
0194         DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
0195         return -EINVAL;
0196     }
0197 
0198     len = size;
0199     /* Serial number should only be 16 characters. Any more,
0200      * and something could be wrong. Cap it at 16 to be safe
0201      */
0202     if (len >= sizeof(adev->serial)) {
0203         DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
0204         len = sizeof(adev->serial) - 1;
0205     }
0206     memcpy(adev->serial, buf, len);
0207     adev->serial[len] = '\0';
0208 
0209     return 0;
0210 }