xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c (revision 95298d63c67673c654c08952672d016212b26054)
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 
30 #define I2C_PRODUCT_INFO_ADDR		0xAC
31 #define I2C_PRODUCT_INFO_ADDR_SIZE	0x2
32 #define I2C_PRODUCT_INFO_OFFSET		0xC0
33 
34 bool is_fru_eeprom_supported(struct amdgpu_device *adev)
35 {
36 	/* TODO: Gaming SKUs don't have the FRU EEPROM.
37 	 * Use this hack to address hangs on modprobe on gaming SKUs
38 	 * until a proper solution can be implemented by only supporting
39 	 * the explicit chip IDs for VG20 Server cards
40 	 *
41 	 * TODO: Add list of supported Arcturus DIDs once confirmed
42 	 */
43 	if ((adev->asic_type == CHIP_VEGA20 && adev->pdev->device == 0x66a0) ||
44 	    (adev->asic_type == CHIP_VEGA20 && adev->pdev->device == 0x66a1) ||
45 	    (adev->asic_type == CHIP_VEGA20 && adev->pdev->device == 0x66a4))
46 		return true;
47 	return false;
48 }
49 
50 int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
51 			   unsigned char *buff)
52 {
53 	int ret, size;
54 	struct i2c_msg msg = {
55 			.addr   = I2C_PRODUCT_INFO_ADDR,
56 			.flags  = I2C_M_RD,
57 			.buf    = buff,
58 	};
59 	buff[0] = 0;
60 	buff[1] = addrptr;
61 	msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + 1;
62 	ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
63 
64 	if (ret < 1) {
65 		DRM_WARN("FRU: Failed to get size field");
66 		return ret;
67 	}
68 
69 	/* The size returned by the i2c requires subtraction of 0xC0 since the
70 	 * size apparently always reports as 0xC0+actual size.
71 	 */
72 	size = buff[2] - I2C_PRODUCT_INFO_OFFSET;
73 	/* Add 1 since address field was 1 byte */
74 	buff[1] = addrptr + 1;
75 
76 	msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + size;
77 	ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
78 
79 	if (ret < 1) {
80 		DRM_WARN("FRU: Failed to get data field");
81 		return ret;
82 	}
83 
84 	return size;
85 }
86 
87 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
88 {
89 	unsigned char buff[34];
90 	int addrptr = 0, size = 0;
91 
92 	if (!is_fru_eeprom_supported(adev))
93 		return 0;
94 
95 	/* If algo exists, it means that the i2c_adapter's initialized */
96 	if (!adev->pm.smu_i2c.algo) {
97 		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
98 		return 0;
99 	}
100 
101 	/* There's a lot of repetition here. This is due to the FRU having
102 	 * variable-length fields. To get the information, we have to find the
103 	 * size of each field, and then keep reading along and reading along
104 	 * until we get all of the data that we want. We use addrptr to track
105 	 * the address as we go
106 	 */
107 
108 	/* The first fields are all of size 1-byte, from 0-7 are offsets that
109 	 * contain information that isn't useful to us.
110 	 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
111 	 * and the language field, so just start from 0xb, manufacturer size
112 	 */
113 	addrptr = 0xb;
114 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
115 	if (size < 1) {
116 		DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
117 		return size;
118 	}
119 
120 	/* Increment the addrptr by the size of the field, and 1 due to the
121 	 * size field being 1 byte. This pattern continues below.
122 	 */
123 	addrptr += size + 1;
124 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
125 	if (size < 1) {
126 		DRM_ERROR("Failed to read FRU product name, ret:%d", size);
127 		return size;
128 	}
129 
130 	/* Product name should only be 32 characters. Any more,
131 	 * and something could be wrong. Cap it at 32 to be safe
132 	 */
133 	if (size > 32) {
134 		DRM_WARN("FRU Product Number is larger than 32 characters. This is likely a mistake");
135 		size = 32;
136 	}
137 	/* Start at 2 due to buff using fields 0 and 1 for the address */
138 	memcpy(adev->product_name, &buff[2], size);
139 	adev->product_name[size] = '\0';
140 
141 	addrptr += size + 1;
142 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
143 	if (size < 1) {
144 		DRM_ERROR("Failed to read FRU product number, ret:%d", size);
145 		return size;
146 	}
147 
148 	/* Product number should only be 16 characters. Any more,
149 	 * and something could be wrong. Cap it at 16 to be safe
150 	 */
151 	if (size > 16) {
152 		DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
153 		size = 16;
154 	}
155 	memcpy(adev->product_number, &buff[2], size);
156 	adev->product_number[size] = '\0';
157 
158 	addrptr += size + 1;
159 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
160 
161 	if (size < 1) {
162 		DRM_ERROR("Failed to read FRU product version, ret:%d", size);
163 		return size;
164 	}
165 
166 	addrptr += size + 1;
167 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
168 
169 	if (size < 1) {
170 		DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
171 		return size;
172 	}
173 
174 	/* Serial number should only be 16 characters. Any more,
175 	 * and something could be wrong. Cap it at 16 to be safe
176 	 */
177 	if (size > 16) {
178 		DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
179 		size = 16;
180 	}
181 	memcpy(adev->serial, &buff[2], size);
182 	adev->serial[size] = '\0';
183 
184 	return 0;
185 }
186