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