2 * Copyright 2019 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include <linux/pci.h>
26 #include "amdgpu_i2c.h"
27 #include "smu_v11_0_i2c.h"
29 #include "amdgpu_fru_eeprom.h"
30 #include "amdgpu_eeprom.h"
32 #define FRU_EEPROM_MADDR 0x60000
34 static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
36 /* Only server cards have the FRU EEPROM
37 * TODO: See if we can figure this out dynamically instead of
38 * having to parse VBIOS versions.
40 struct atom_context *atom_ctx = adev->mode_info.atom_context;
42 /* The i2c access is blocked on VF
43 * TODO: Need other way to get the info
45 if (amdgpu_sriov_vf(adev))
48 /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
49 * we can use just the "DXXX" portion. If there were more models, we
50 * could convert the 3 characters to a hex integer and use a switch
51 * for ease/speed/readability. For now, 2 string comparisons are
52 * reasonable and not too expensive
54 switch (adev->asic_type) {
56 /* D161 and D163 are the VG20 server SKUs */
57 if (strnstr(atom_ctx->vbios_version, "D161",
58 sizeof(atom_ctx->vbios_version)) ||
59 strnstr(atom_ctx->vbios_version, "D163",
60 sizeof(atom_ctx->vbios_version)))
65 /* All Aldebaran SKUs have the FRU */
67 case CHIP_SIENNA_CICHLID:
68 if (strnstr(atom_ctx->vbios_version, "D603",
69 sizeof(atom_ctx->vbios_version)))
78 static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
79 unsigned char *buf, size_t buf_size)
84 ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr, buf, 1);
86 DRM_WARN("FRU: Failed to get size field");
90 /* The size returned by the i2c requires subtraction of 0xC0 since the
91 * size apparently always reports as 0xC0+actual size.
94 size = min_t(size_t, size, buf_size);
96 ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr + 1,
99 DRM_WARN("FRU: Failed to get data field");
106 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
108 unsigned char buf[AMDGPU_PRODUCT_NAME_LEN];
112 if (!is_fru_eeprom_supported(adev))
115 /* If algo exists, it means that the i2c_adapter's initialized */
116 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
117 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
121 /* There's a lot of repetition here. This is due to the FRU having
122 * variable-length fields. To get the information, we have to find the
123 * size of each field, and then keep reading along and reading along
124 * until we get all of the data that we want. We use addrptr to track
125 * the address as we go
128 /* The first fields are all of size 1-byte, from 0-7 are offsets that
129 * contain information that isn't useful to us.
130 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
131 * and the language field, so just start from 0xb, manufacturer size
133 addrptr = FRU_EEPROM_MADDR + 0xb;
134 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
136 DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
140 /* Increment the addrptr by the size of the field, and 1 due to the
141 * size field being 1 byte. This pattern continues below.
144 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
146 DRM_ERROR("Failed to read FRU product name, ret:%d", size);
151 if (len >= AMDGPU_PRODUCT_NAME_LEN) {
152 DRM_WARN("FRU Product Name is larger than %d characters. This is likely a mistake",
153 AMDGPU_PRODUCT_NAME_LEN);
154 len = AMDGPU_PRODUCT_NAME_LEN - 1;
156 memcpy(adev->product_name, buf, len);
157 adev->product_name[len] = '\0';
160 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
162 DRM_ERROR("Failed to read FRU product number, ret:%d", size);
167 /* Product number should only be 16 characters. Any more,
168 * and something could be wrong. Cap it at 16 to be safe
170 if (len >= sizeof(adev->product_number)) {
171 DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
172 len = sizeof(adev->product_number) - 1;
174 memcpy(adev->product_number, buf, len);
175 adev->product_number[len] = '\0';
178 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
181 DRM_ERROR("Failed to read FRU product version, ret:%d", size);
186 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
189 DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
194 /* Serial number should only be 16 characters. Any more,
195 * and something could be wrong. Cap it at 16 to be safe
197 if (len >= sizeof(adev->serial)) {
198 DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
199 len = sizeof(adev->serial) - 1;
201 memcpy(adev->serial, buf, len);
202 adev->serial[len] = '\0';