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
45 * Also, FRU not valid for APU devices.
47 if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU))
50 /* The default I2C EEPROM address of the FRU.
53 *fru_addr = FRU_EEPROM_MADDR_8;
55 /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
56 * we can use just the "DXXX" portion. If there were more models, we
57 * could convert the 3 characters to a hex integer and use a switch
58 * for ease/speed/readability. For now, 2 string comparisons are
59 * reasonable and not too expensive
61 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
62 case IP_VERSION(11, 0, 2):
63 switch (adev->asic_type) {
65 /* D161 and D163 are the VG20 server SKUs */
66 if (strnstr(atom_ctx->vbios_pn, "D161",
67 sizeof(atom_ctx->vbios_pn)) ||
68 strnstr(atom_ctx->vbios_pn, "D163",
69 sizeof(atom_ctx->vbios_pn))) {
71 *fru_addr = FRU_EEPROM_MADDR_6;
80 case IP_VERSION(11, 0, 7):
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;
95 case IP_VERSION(13, 0, 2):
96 /* All Aldebaran SKUs have an FRU */
97 if (!strnstr(atom_ctx->vbios_pn, "D673",
98 sizeof(atom_ctx->vbios_pn)))
100 *fru_addr = FRU_EEPROM_MADDR_6;
102 case IP_VERSION(13, 0, 6):
103 case IP_VERSION(13, 0, 14):
105 *fru_addr = FRU_EEPROM_MADDR_8;
112 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
114 struct amdgpu_fru_info *fru_info;
115 unsigned char buf[8], *pia;
120 if (!is_fru_eeprom_supported(adev, &fru_addr))
123 if (!adev->fru_info) {
124 adev->fru_info = kzalloc(sizeof(*adev->fru_info), GFP_KERNEL);
129 fru_info = adev->fru_info;
130 /* For Arcturus-and-later, default value of serial_number is unique_id
131 * so convert it to a 16-digit HEX string for convenience and
132 * backwards-compatibility.
134 sprintf(fru_info->serial, "%llx", adev->unique_id);
136 /* If algo exists, it means that the i2c_adapter's initialized */
137 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
138 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
142 /* Read the IPMI Common header */
143 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
146 DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
147 return len < 0 ? len : -EIO;
151 DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
155 for (csum = 0; len > 0; len--)
156 csum += buf[len - 1];
158 DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
162 /* Get the offset to the Product Info Area (PIA). */
167 /* Get the absolute address to the PIA. */
170 /* Read the header of the PIA. */
171 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
173 DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
174 return len < 0 ? len : -EIO;
178 DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
183 pia = kzalloc(size, GFP_KERNEL);
187 /* Read the whole PIA. */
188 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
191 DRM_ERROR("Couldn't read the Product Info Area: %d", len);
192 return len < 0 ? len : -EIO;
195 for (csum = 0; size > 0; size--)
196 csum += pia[size - 1];
198 DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
203 /* Now extract useful information from the PIA.
205 * Read Manufacturer Name field whose length is [3].
210 memcpy(fru_info->manufacturer_name, pia + addr + 1,
211 min_t(size_t, sizeof(fru_info->manufacturer_name),
213 fru_info->manufacturer_name[sizeof(fru_info->manufacturer_name) - 1] =
216 /* Read Product Name field. */
217 addr += 1 + (pia[addr] & 0x3F);
220 memcpy(fru_info->product_name, pia + addr + 1,
221 min_t(size_t, sizeof(fru_info->product_name), pia[addr] & 0x3F));
222 fru_info->product_name[sizeof(fru_info->product_name) - 1] = '\0';
224 /* Go to the Product Part/Model Number field. */
225 addr += 1 + (pia[addr] & 0x3F);
228 memcpy(fru_info->product_number, pia + addr + 1,
229 min_t(size_t, sizeof(fru_info->product_number),
231 fru_info->product_number[sizeof(fru_info->product_number) - 1] = '\0';
233 /* Go to the Product Version field. */
234 addr += 1 + (pia[addr] & 0x3F);
236 /* Go to the Product Serial Number field. */
237 addr += 1 + (pia[addr] & 0x3F);
240 memcpy(fru_info->serial, pia + addr + 1,
241 min_t(size_t, sizeof(fru_info->serial), pia[addr] & 0x3F));
242 fru_info->serial[sizeof(fru_info->serial) - 1] = '\0';
244 /* Asset Tag field */
245 addr += 1 + (pia[addr] & 0x3F);
247 /* FRU File Id field. This could be 'null'. */
248 addr += 1 + (pia[addr] & 0x3F);
249 if ((addr + 1 >= len) || !(pia[addr] & 0x3F))
251 memcpy(fru_info->fru_id, pia + addr + 1,
252 min_t(size_t, sizeof(fru_info->fru_id), pia[addr] & 0x3F));
253 fru_info->fru_id[sizeof(fru_info->fru_id) - 1] = '\0';
263 * The amdgpu driver provides a sysfs API for reporting the product name
265 * The file product_name is used for this and returns the product name
266 * as returned from the FRU.
267 * NOTE: This is only available for certain server cards
270 static ssize_t amdgpu_fru_product_name_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->fru_info->product_name);
280 static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL);
283 * DOC: product_number
285 * The amdgpu driver provides a sysfs API for reporting the part number
287 * The file product_number is used for this and returns the part number
288 * as returned from the FRU.
289 * NOTE: This is only available for certain server cards
292 static ssize_t amdgpu_fru_product_number_show(struct device *dev,
293 struct device_attribute *attr,
296 struct drm_device *ddev = dev_get_drvdata(dev);
297 struct amdgpu_device *adev = drm_to_adev(ddev);
299 return sysfs_emit(buf, "%s\n", adev->fru_info->product_number);
302 static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL);
307 * The amdgpu driver provides a sysfs API for reporting the serial number
309 * The file serial_number is used for this and returns the serial number
310 * as returned from the FRU.
311 * NOTE: This is only available for certain server cards
314 static ssize_t amdgpu_fru_serial_number_show(struct device *dev,
315 struct device_attribute *attr,
318 struct drm_device *ddev = dev_get_drvdata(dev);
319 struct amdgpu_device *adev = drm_to_adev(ddev);
321 return sysfs_emit(buf, "%s\n", adev->fru_info->serial);
324 static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL);
329 * The amdgpu driver provides a sysfs API for reporting FRU File Id
331 * The file fru_id is used for this and returns the File Id value
332 * as returned from the FRU.
333 * NOTE: This is only available for certain server cards
336 static ssize_t amdgpu_fru_id_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 struct drm_device *ddev = dev_get_drvdata(dev);
340 struct amdgpu_device *adev = drm_to_adev(ddev);
342 return sysfs_emit(buf, "%s\n", adev->fru_info->fru_id);
345 static DEVICE_ATTR(fru_id, 0444, amdgpu_fru_id_show, NULL);
350 * The amdgpu driver provides a sysfs API for reporting manufacturer name from
352 * The file manufacturer returns the value as returned from the FRU.
353 * NOTE: This is only available for certain server cards
356 static ssize_t amdgpu_fru_manufacturer_name_show(struct device *dev,
357 struct device_attribute *attr,
360 struct drm_device *ddev = dev_get_drvdata(dev);
361 struct amdgpu_device *adev = drm_to_adev(ddev);
363 return sysfs_emit(buf, "%s\n", adev->fru_info->manufacturer_name);
366 static DEVICE_ATTR(manufacturer, 0444, amdgpu_fru_manufacturer_name_show, NULL);
368 static const struct attribute *amdgpu_fru_attributes[] = {
369 &dev_attr_product_name.attr,
370 &dev_attr_product_number.attr,
371 &dev_attr_serial_number.attr,
372 &dev_attr_fru_id.attr,
373 &dev_attr_manufacturer.attr,
377 int amdgpu_fru_sysfs_init(struct amdgpu_device *adev)
379 if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info)
382 return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes);
385 void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev)
390 sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes);