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"
31 #define I2C_PRODUCT_INFO_ADDR 0xAC
32 #define I2C_PRODUCT_INFO_ADDR_SIZE 0x2
33 #define I2C_PRODUCT_INFO_OFFSET 0xC0
35 static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
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.
41 struct atom_context *atom_ctx = adev->mode_info.atom_context;
43 /* VBIOS is of the format ###-DXXXYY-##. For SKU identification,
44 * we can use just the "DXXX" portion. If there were more models, we
45 * could convert the 3 characters to a hex integer and use a switch
46 * for ease/speed/readability. For now, 2 string comparisons are
47 * reasonable and not too expensive
49 switch (adev->asic_type) {
51 /* D161 and D163 are the VG20 server SKUs */
52 if (strnstr(atom_ctx->vbios_version, "D161",
53 sizeof(atom_ctx->vbios_version)) ||
54 strnstr(atom_ctx->vbios_version, "D163",
55 sizeof(atom_ctx->vbios_version)))
64 static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
68 struct i2c_msg msg = {
69 .addr = I2C_PRODUCT_INFO_ADDR,
75 msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + 1;
76 ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
79 DRM_WARN("FRU: Failed to get size field");
83 /* The size returned by the i2c requires subtraction of 0xC0 since the
84 * size apparently always reports as 0xC0+actual size.
86 size = buff[2] - I2C_PRODUCT_INFO_OFFSET;
87 /* Add 1 since address field was 1 byte */
88 buff[1] = addrptr + 1;
90 msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + size;
91 ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
94 DRM_WARN("FRU: Failed to get data field");
101 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
103 unsigned char buff[34];
104 int addrptr = 0, size = 0;
106 if (!is_fru_eeprom_supported(adev))
109 /* If algo exists, it means that the i2c_adapter's initialized */
110 if (!adev->pm.smu_i2c.algo) {
111 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
115 /* There's a lot of repetition here. This is due to the FRU having
116 * variable-length fields. To get the information, we have to find the
117 * size of each field, and then keep reading along and reading along
118 * until we get all of the data that we want. We use addrptr to track
119 * the address as we go
122 /* The first fields are all of size 1-byte, from 0-7 are offsets that
123 * contain information that isn't useful to us.
124 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
125 * and the language field, so just start from 0xb, manufacturer size
128 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
130 DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
134 /* Increment the addrptr by the size of the field, and 1 due to the
135 * size field being 1 byte. This pattern continues below.
138 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
140 DRM_ERROR("Failed to read FRU product name, ret:%d", size);
144 /* Product name should only be 32 characters. Any more,
145 * and something could be wrong. Cap it at 32 to be safe
148 DRM_WARN("FRU Product Number is larger than 32 characters. This is likely a mistake");
151 /* Start at 2 due to buff using fields 0 and 1 for the address */
152 memcpy(adev->product_name, &buff[2], size);
153 adev->product_name[size] = '\0';
156 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
158 DRM_ERROR("Failed to read FRU product number, ret:%d", size);
162 /* Product number should only be 16 characters. Any more,
163 * and something could be wrong. Cap it at 16 to be safe
166 DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
169 memcpy(adev->product_number, &buff[2], size);
170 adev->product_number[size] = '\0';
173 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
176 DRM_ERROR("Failed to read FRU product version, ret:%d", size);
181 size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
184 DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
188 /* Serial number should only be 16 characters. Any more,
189 * and something could be wrong. Cap it at 16 to be safe
192 DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
195 memcpy(adev->serial, &buff[2], size);
196 adev->serial[size] = '\0';