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_6 0x60000
33 #define FRU_EEPROM_MADDR_8 0x80000
35 static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr)
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 /* The i2c access is blocked on VF
44 * TODO: Need other way to get the info
46 if (amdgpu_sriov_vf(adev))
49 /* The default I2C EEPROM address of the FRU.
52 *fru_addr = FRU_EEPROM_MADDR_8;
54 /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
55 * we can use just the "DXXX" portion. If there were more models, we
56 * could convert the 3 characters to a hex integer and use a switch
57 * for ease/speed/readability. For now, 2 string comparisons are
58 * reasonable and not too expensive
60 switch (adev->asic_type) {
62 /* D161 and D163 are the VG20 server SKUs */
63 if (strnstr(atom_ctx->vbios_pn, "D161",
64 sizeof(atom_ctx->vbios_pn)) ||
65 strnstr(atom_ctx->vbios_pn, "D163",
66 sizeof(atom_ctx->vbios_pn))) {
68 *fru_addr = FRU_EEPROM_MADDR_6;
74 /* All Aldebaran SKUs have an FRU */
75 if (!strnstr(atom_ctx->vbios_pn, "D673",
76 sizeof(atom_ctx->vbios_pn)))
78 *fru_addr = FRU_EEPROM_MADDR_6;
80 case CHIP_SIENNA_CICHLID:
81 if (strnstr(atom_ctx->vbios_pn, "D603",
82 sizeof(atom_ctx->vbios_pn))) {
83 if (strnstr(atom_ctx->vbios_pn, "D603GLXE",
84 sizeof(atom_ctx->vbios_pn))) {
89 *fru_addr = FRU_EEPROM_MADDR_6;
100 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
102 unsigned char buf[8], *pia;
107 if (!is_fru_eeprom_supported(adev, &fru_addr))
110 /* If algo exists, it means that the i2c_adapter's initialized */
111 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
112 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
116 /* Read the IPMI Common header */
117 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
120 DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
121 return len < 0 ? len : -EIO;
125 DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
129 for (csum = 0; len > 0; len--)
130 csum += buf[len - 1];
132 DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
136 /* Get the offset to the Product Info Area (PIA). */
141 /* Get the absolute address to the PIA. */
144 /* Read the header of the PIA. */
145 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
147 DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
148 return len < 0 ? len : -EIO;
152 DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
157 pia = kzalloc(size, GFP_KERNEL);
161 /* Read the whole PIA. */
162 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
165 DRM_ERROR("Couldn't read the Product Info Area: %d", len);
166 return len < 0 ? len : -EIO;
169 for (csum = 0; size > 0; size--)
170 csum += pia[size - 1];
172 DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
176 /* Now extract useful information from the PIA.
178 * Skip the Manufacturer Name at [3] and go directly to
179 * the Product Name field.
181 addr = 3 + 1 + (pia[3] & 0x3F);
184 memcpy(adev->product_name, pia + addr + 1,
186 sizeof(adev->product_name),
188 adev->product_name[sizeof(adev->product_name) - 1] = '\0';
190 /* Go to the Product Part/Model Number field. */
191 addr += 1 + (pia[addr] & 0x3F);
194 memcpy(adev->product_number, pia + addr + 1,
196 sizeof(adev->product_number),
198 adev->product_number[sizeof(adev->product_number) - 1] = '\0';
200 /* Go to the Product Version field. */
201 addr += 1 + (pia[addr] & 0x3F);
203 /* Go to the Product Serial Number field. */
204 addr += 1 + (pia[addr] & 0x3F);
207 memcpy(adev->serial, pia + addr + 1, min_t(size_t,
208 sizeof(adev->serial),
210 adev->serial[sizeof(adev->serial) - 1] = '\0';
219 * The amdgpu driver provides a sysfs API for reporting the product name
221 * The file product_name is used for this and returns the product name
222 * as returned from the FRU.
223 * NOTE: This is only available for certain server cards
226 static ssize_t amdgpu_fru_product_name_show(struct device *dev,
227 struct device_attribute *attr,
230 struct drm_device *ddev = dev_get_drvdata(dev);
231 struct amdgpu_device *adev = drm_to_adev(ddev);
233 return sysfs_emit(buf, "%s\n", adev->product_name);
236 static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL);
239 * DOC: product_number
241 * The amdgpu driver provides a sysfs API for reporting the part number
243 * The file product_number is used for this and returns the part number
244 * as returned from the FRU.
245 * NOTE: This is only available for certain server cards
248 static ssize_t amdgpu_fru_product_number_show(struct device *dev,
249 struct device_attribute *attr,
252 struct drm_device *ddev = dev_get_drvdata(dev);
253 struct amdgpu_device *adev = drm_to_adev(ddev);
255 return sysfs_emit(buf, "%s\n", adev->product_number);
258 static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL);
263 * The amdgpu driver provides a sysfs API for reporting the serial number
265 * The file serial_number is used for this and returns the serial number
266 * as returned from the FRU.
267 * NOTE: This is only available for certain server cards
270 static ssize_t amdgpu_fru_serial_number_show(struct device *dev,
271 struct device_attribute *attr,
274 struct drm_device *ddev = dev_get_drvdata(dev);
275 struct amdgpu_device *adev = drm_to_adev(ddev);
277 return sysfs_emit(buf, "%s\n", adev->serial);
280 static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL);
282 static const struct attribute *amdgpu_fru_attributes[] = {
283 &dev_attr_product_name.attr,
284 &dev_attr_product_number.attr,
285 &dev_attr_serial_number.attr,
289 int amdgpu_fru_sysfs_init(struct amdgpu_device *adev)
291 if (!is_fru_eeprom_supported(adev, NULL))
294 return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes);
297 void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev)
299 if (!is_fru_eeprom_supported(adev, NULL))
302 sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes);