Merge tag 'printk-for-5.19-fixup' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_fru_eeprom.c
CommitLineData
bd607166
KR
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 */
1ea2b260
KR
23#include <linux/pci.h>
24
bd607166
KR
25#include "amdgpu.h"
26#include "amdgpu_i2c.h"
27#include "smu_v11_0_i2c.h"
28#include "atom.h"
d43f7ff6 29#include "amdgpu_fru_eeprom.h"
25e5c09f 30#include "amdgpu_eeprom.h"
bd607166 31
ccdfbfec 32#define FRU_EEPROM_MADDR 0x60000
bd607166 33
f3167919 34static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
fabe01d7 35{
f94582e4
KR
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.
1ea2b260 39 */
f94582e4
KR
40 struct atom_context *atom_ctx = adev->mode_info.atom_context;
41
901abf36 42 /* The i2c access is blocked on VF
43 * TODO: Need other way to get the info
44 */
45 if (amdgpu_sriov_vf(adev))
46 return false;
47
3ed89339 48 /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
f94582e4
KR
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
53 */
54 switch (adev->asic_type) {
55 case CHIP_VEGA20:
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)))
61 return true;
62 else
63 return false;
67416bf8
KR
64 case CHIP_ALDEBARAN:
65 /* All Aldebaran SKUs have the FRU */
66 return true;
3ed89339
LT
67 case CHIP_SIENNA_CICHLID:
68 if (strnstr(atom_ctx->vbios_version, "D603",
69 sizeof(atom_ctx->vbios_version)))
70 return true;
71 else
72 return false;
f94582e4
KR
73 default:
74 return false;
75 }
fabe01d7
JC
76}
77
f3167919 78static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
00b14ce0 79 unsigned char *buf, size_t buf_size)
bd607166 80{
00b14ce0
LT
81 int ret;
82 u8 size;
bd607166 83
3f1e2e9d 84 ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr, buf, 1);
bd607166
KR
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 */
00b14ce0
LT
93 size = buf[0] & 0x3F;
94 size = min_t(size_t, size, buf_size);
bd607166 95
00b14ce0
LT
96 ret = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addrptr + 1,
97 buf, size);
bd607166
KR
98 if (ret < 1) {
99 DRM_WARN("FRU: Failed to get data field");
100 return ret;
101 }
102
103 return size;
104}
105
106int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
107{
3f3a24a0 108 unsigned char buf[AMDGPU_PRODUCT_NAME_LEN];
ccdfbfec
LT
109 u32 addrptr;
110 int size, len;
bd607166 111
fabe01d7
JC
112 if (!is_fru_eeprom_supported(adev))
113 return 0;
114
bd607166 115 /* If algo exists, it means that the i2c_adapter's initialized */
2f60dd50 116 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
bd607166 117 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
02b865f8 118 return -ENODEV;
bd607166
KR
119 }
120
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
126 */
127
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
132 */
ccdfbfec 133 addrptr = FRU_EEPROM_MADDR + 0xb;
00b14ce0 134 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
bd607166
KR
135 if (size < 1) {
136 DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
02b865f8 137 return -EINVAL;
bd607166
KR
138 }
139
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.
142 */
143 addrptr += size + 1;
00b14ce0 144 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
bd607166
KR
145 if (size < 1) {
146 DRM_ERROR("Failed to read FRU product name, ret:%d", size);
02b865f8 147 return -EINVAL;
bd607166
KR
148 }
149
02b865f8 150 len = size;
6c92fe5f
KR
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;
714309f0 155 }
3f3a24a0 156 memcpy(adev->product_name, buf, len);
02b865f8 157 adev->product_name[len] = '\0';
bd607166
KR
158
159 addrptr += size + 1;
00b14ce0 160 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
bd607166
KR
161 if (size < 1) {
162 DRM_ERROR("Failed to read FRU product number, ret:%d", size);
02b865f8 163 return -EINVAL;
bd607166
KR
164 }
165
02b865f8 166 len = size;
714309f0
KR
167 /* Product number should only be 16 characters. Any more,
168 * and something could be wrong. Cap it at 16 to be safe
169 */
02b865f8 170 if (len >= sizeof(adev->product_number)) {
714309f0 171 DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
02b865f8 172 len = sizeof(adev->product_number) - 1;
714309f0 173 }
3f3a24a0 174 memcpy(adev->product_number, buf, len);
02b865f8 175 adev->product_number[len] = '\0';
bd607166
KR
176
177 addrptr += size + 1;
00b14ce0 178 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
bd607166
KR
179
180 if (size < 1) {
181 DRM_ERROR("Failed to read FRU product version, ret:%d", size);
02b865f8 182 return -EINVAL;
bd607166
KR
183 }
184
185 addrptr += size + 1;
00b14ce0 186 size = amdgpu_fru_read_eeprom(adev, addrptr, buf, sizeof(buf));
bd607166
KR
187
188 if (size < 1) {
189 DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
02b865f8 190 return -EINVAL;
bd607166
KR
191 }
192
02b865f8 193 len = size;
714309f0
KR
194 /* Serial number should only be 16 characters. Any more,
195 * and something could be wrong. Cap it at 16 to be safe
196 */
02b865f8 197 if (len >= sizeof(adev->serial)) {
714309f0 198 DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
02b865f8 199 len = sizeof(adev->serial) - 1;
714309f0 200 }
3f3a24a0 201 memcpy(adev->serial, buf, len);
02b865f8 202 adev->serial[len] = '\0';
bd607166
KR
203
204 return 0;
205}