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):
104 *fru_addr = FRU_EEPROM_MADDR_8;
111 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
113 struct amdgpu_fru_info *fru_info;
114 unsigned char buf[8], *pia;
119 if (!is_fru_eeprom_supported(adev, &fru_addr))
122 if (!adev->fru_info) {
123 adev->fru_info = kzalloc(sizeof(*adev->fru_info), GFP_KERNEL);
128 fru_info = adev->fru_info;
129 /* For Arcturus-and-later, default value of serial_number is unique_id
130 * so convert it to a 16-digit HEX string for convenience and
131 * backwards-compatibility.
133 sprintf(fru_info->serial, "%llx", adev->unique_id);
135 /* If algo exists, it means that the i2c_adapter's initialized */
136 if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
137 DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
141 /* Read the IPMI Common header */
142 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
145 DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
146 return len < 0 ? len : -EIO;
150 DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
154 for (csum = 0; len > 0; len--)
155 csum += buf[len - 1];
157 DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
161 /* Get the offset to the Product Info Area (PIA). */
166 /* Get the absolute address to the PIA. */
169 /* Read the header of the PIA. */
170 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
172 DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
173 return len < 0 ? len : -EIO;
177 DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
182 pia = kzalloc(size, GFP_KERNEL);
186 /* Read the whole PIA. */
187 len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
190 DRM_ERROR("Couldn't read the Product Info Area: %d", len);
191 return len < 0 ? len : -EIO;
194 for (csum = 0; size > 0; size--)
195 csum += pia[size - 1];
197 DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
202 /* Now extract useful information from the PIA.
204 * Read Manufacturer Name field whose length is [3].
209 memcpy(fru_info->manufacturer_name, pia + addr + 1,
210 min_t(size_t, sizeof(fru_info->manufacturer_name),
212 fru_info->manufacturer_name[sizeof(fru_info->manufacturer_name) - 1] =
215 /* Read Product Name field. */
216 addr += 1 + (pia[addr] & 0x3F);
219 memcpy(fru_info->product_name, pia + addr + 1,
220 min_t(size_t, sizeof(fru_info->product_name), pia[addr] & 0x3F));
221 fru_info->product_name[sizeof(fru_info->product_name) - 1] = '\0';
223 /* Go to the Product Part/Model Number field. */
224 addr += 1 + (pia[addr] & 0x3F);
227 memcpy(fru_info->product_number, pia + addr + 1,
228 min_t(size_t, sizeof(fru_info->product_number),
230 fru_info->product_number[sizeof(fru_info->product_number) - 1] = '\0';
232 /* Go to the Product Version field. */
233 addr += 1 + (pia[addr] & 0x3F);
235 /* Go to the Product Serial Number field. */
236 addr += 1 + (pia[addr] & 0x3F);
239 memcpy(fru_info->serial, pia + addr + 1,
240 min_t(size_t, sizeof(fru_info->serial), pia[addr] & 0x3F));
241 fru_info->serial[sizeof(fru_info->serial) - 1] = '\0';
243 /* Asset Tag field */
244 addr += 1 + (pia[addr] & 0x3F);
246 /* FRU File Id field. This could be 'null'. */
247 addr += 1 + (pia[addr] & 0x3F);
248 if ((addr + 1 >= len) || !(pia[addr] & 0x3F))
250 memcpy(fru_info->fru_id, pia + addr + 1,
251 min_t(size_t, sizeof(fru_info->fru_id), pia[addr] & 0x3F));
252 fru_info->fru_id[sizeof(fru_info->fru_id) - 1] = '\0';
262 * The amdgpu driver provides a sysfs API for reporting the product name
264 * The file product_name is used for this and returns the product name
265 * as returned from the FRU.
266 * NOTE: This is only available for certain server cards
269 static ssize_t amdgpu_fru_product_name_show(struct device *dev,
270 struct device_attribute *attr,
273 struct drm_device *ddev = dev_get_drvdata(dev);
274 struct amdgpu_device *adev = drm_to_adev(ddev);
276 return sysfs_emit(buf, "%s\n", adev->fru_info->product_name);
279 static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL);
282 * DOC: product_number
284 * The amdgpu driver provides a sysfs API for reporting the part number
286 * The file product_number is used for this and returns the part number
287 * as returned from the FRU.
288 * NOTE: This is only available for certain server cards
291 static ssize_t amdgpu_fru_product_number_show(struct device *dev,
292 struct device_attribute *attr,
295 struct drm_device *ddev = dev_get_drvdata(dev);
296 struct amdgpu_device *adev = drm_to_adev(ddev);
298 return sysfs_emit(buf, "%s\n", adev->fru_info->product_number);
301 static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL);
306 * The amdgpu driver provides a sysfs API for reporting the serial number
308 * The file serial_number is used for this and returns the serial number
309 * as returned from the FRU.
310 * NOTE: This is only available for certain server cards
313 static ssize_t amdgpu_fru_serial_number_show(struct device *dev,
314 struct device_attribute *attr,
317 struct drm_device *ddev = dev_get_drvdata(dev);
318 struct amdgpu_device *adev = drm_to_adev(ddev);
320 return sysfs_emit(buf, "%s\n", adev->fru_info->serial);
323 static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL);
328 * The amdgpu driver provides a sysfs API for reporting FRU File Id
330 * The file fru_id is used for this and returns the File Id value
331 * as returned from the FRU.
332 * NOTE: This is only available for certain server cards
335 static ssize_t amdgpu_fru_id_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
338 struct drm_device *ddev = dev_get_drvdata(dev);
339 struct amdgpu_device *adev = drm_to_adev(ddev);
341 return sysfs_emit(buf, "%s\n", adev->fru_info->fru_id);
344 static DEVICE_ATTR(fru_id, 0444, amdgpu_fru_id_show, NULL);
349 * The amdgpu driver provides a sysfs API for reporting manufacturer name from
351 * The file manufacturer returns the value as returned from the FRU.
352 * NOTE: This is only available for certain server cards
355 static ssize_t amdgpu_fru_manufacturer_name_show(struct device *dev,
356 struct device_attribute *attr,
359 struct drm_device *ddev = dev_get_drvdata(dev);
360 struct amdgpu_device *adev = drm_to_adev(ddev);
362 return sysfs_emit(buf, "%s\n", adev->fru_info->manufacturer_name);
365 static DEVICE_ATTR(manufacturer, 0444, amdgpu_fru_manufacturer_name_show, NULL);
367 static const struct attribute *amdgpu_fru_attributes[] = {
368 &dev_attr_product_name.attr,
369 &dev_attr_product_number.attr,
370 &dev_attr_serial_number.attr,
371 &dev_attr_fru_id.attr,
372 &dev_attr_manufacturer.attr,
376 int amdgpu_fru_sysfs_init(struct amdgpu_device *adev)
378 if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info)
381 return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes);
384 void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev)
386 if (!is_fru_eeprom_supported(adev, NULL) || !adev->fru_info)
389 sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes);