1bd607166SKent Russell /*
2bd607166SKent Russell * Copyright 2019 Advanced Micro Devices, Inc.
3bd607166SKent Russell *
4bd607166SKent Russell * Permission is hereby granted, free of charge, to any person obtaining a
5bd607166SKent Russell * copy of this software and associated documentation files (the "Software"),
6bd607166SKent Russell * to deal in the Software without restriction, including without limitation
7bd607166SKent Russell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bd607166SKent Russell * and/or sell copies of the Software, and to permit persons to whom the
9bd607166SKent Russell * Software is furnished to do so, subject to the following conditions:
10bd607166SKent Russell *
11bd607166SKent Russell * The above copyright notice and this permission notice shall be included in
12bd607166SKent Russell * all copies or substantial portions of the Software.
13bd607166SKent Russell *
14bd607166SKent Russell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bd607166SKent Russell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bd607166SKent Russell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17bd607166SKent Russell * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18bd607166SKent Russell * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bd607166SKent Russell * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bd607166SKent Russell * OTHER DEALINGS IN THE SOFTWARE.
21bd607166SKent Russell *
22bd607166SKent Russell */
231ea2b260SKent Russell #include <linux/pci.h>
241ea2b260SKent Russell
25bd607166SKent Russell #include "amdgpu.h"
26bd607166SKent Russell #include "amdgpu_i2c.h"
27bd607166SKent Russell #include "smu_v11_0_i2c.h"
28bd607166SKent Russell #include "atom.h"
29d43f7ff6SAlex Deucher #include "amdgpu_fru_eeprom.h"
3025e5c09fSAlex Deucher #include "amdgpu_eeprom.h"
31bd607166SKent Russell
32afbe5d1eSLuben Tuikov #define FRU_EEPROM_MADDR_6 0x60000
33afbe5d1eSLuben Tuikov #define FRU_EEPROM_MADDR_8 0x80000
34bd607166SKent Russell
is_fru_eeprom_supported(struct amdgpu_device * adev,u32 * fru_addr)35afbe5d1eSLuben Tuikov static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr)
36fabe01d7SJohn Clements {
37f94582e4SKent Russell /* Only server cards have the FRU EEPROM
38f94582e4SKent Russell * TODO: See if we can figure this out dynamically instead of
39f94582e4SKent Russell * having to parse VBIOS versions.
401ea2b260SKent Russell */
41f94582e4SKent Russell struct atom_context *atom_ctx = adev->mode_info.atom_context;
42f94582e4SKent Russell
43901abf36Sshaoyunl /* The i2c access is blocked on VF
44901abf36Sshaoyunl * TODO: Need other way to get the info
45901abf36Sshaoyunl */
46901abf36Sshaoyunl if (amdgpu_sriov_vf(adev))
47901abf36Sshaoyunl return false;
48901abf36Sshaoyunl
49afbe5d1eSLuben Tuikov /* The default I2C EEPROM address of the FRU.
50afbe5d1eSLuben Tuikov */
51afbe5d1eSLuben Tuikov if (fru_addr)
52afbe5d1eSLuben Tuikov *fru_addr = FRU_EEPROM_MADDR_8;
53afbe5d1eSLuben Tuikov
543ed89339SLuben Tuikov /* VBIOS is of the format ###-DXXXYYYY-##. For SKU identification,
55f94582e4SKent Russell * we can use just the "DXXX" portion. If there were more models, we
56f94582e4SKent Russell * could convert the 3 characters to a hex integer and use a switch
57f94582e4SKent Russell * for ease/speed/readability. For now, 2 string comparisons are
58f94582e4SKent Russell * reasonable and not too expensive
59f94582e4SKent Russell */
60f94582e4SKent Russell switch (adev->asic_type) {
61f94582e4SKent Russell case CHIP_VEGA20:
62f94582e4SKent Russell /* D161 and D163 are the VG20 server SKUs */
63adf64e21SMario Limonciello if (strnstr(atom_ctx->vbios_pn, "D161",
64adf64e21SMario Limonciello sizeof(atom_ctx->vbios_pn)) ||
65adf64e21SMario Limonciello strnstr(atom_ctx->vbios_pn, "D163",
66adf64e21SMario Limonciello sizeof(atom_ctx->vbios_pn))) {
6728afcb0aSLuben Tuikov if (fru_addr)
68afbe5d1eSLuben Tuikov *fru_addr = FRU_EEPROM_MADDR_6;
691ea2b260SKent Russell return true;
70afbe5d1eSLuben Tuikov } else {
71fabe01d7SJohn Clements return false;
72afbe5d1eSLuben Tuikov }
7367416bf8SKent Russell case CHIP_ALDEBARAN:
74afbe5d1eSLuben Tuikov /* All Aldebaran SKUs have an FRU */
75adf64e21SMario Limonciello if (!strnstr(atom_ctx->vbios_pn, "D673",
76adf64e21SMario Limonciello sizeof(atom_ctx->vbios_pn)))
77afbe5d1eSLuben Tuikov if (fru_addr)
78afbe5d1eSLuben Tuikov *fru_addr = FRU_EEPROM_MADDR_6;
7967416bf8SKent Russell return true;
803ed89339SLuben Tuikov case CHIP_SIENNA_CICHLID:
81adf64e21SMario Limonciello if (strnstr(atom_ctx->vbios_pn, "D603",
82adf64e21SMario Limonciello sizeof(atom_ctx->vbios_pn))) {
83adf64e21SMario Limonciello if (strnstr(atom_ctx->vbios_pn, "D603GLXE",
84adf64e21SMario Limonciello sizeof(atom_ctx->vbios_pn))) {
853ed89339SLuben Tuikov return false;
86ce83aa7bSSrinivasan Shanmugam }
87ce83aa7bSSrinivasan Shanmugam
8828afcb0aSLuben Tuikov if (fru_addr)
89afbe5d1eSLuben Tuikov *fru_addr = FRU_EEPROM_MADDR_6;
90c8fea927SGuchun Chen return true;
91ce83aa7bSSrinivasan Shanmugam
92c8fea927SGuchun Chen } else {
93c8fea927SGuchun Chen return false;
94c8fea927SGuchun Chen }
95f94582e4SKent Russell default:
96f94582e4SKent Russell return false;
97f94582e4SKent Russell }
98fabe01d7SJohn Clements }
99fabe01d7SJohn Clements
amdgpu_fru_get_product_info(struct amdgpu_device * adev)100bd607166SKent Russell int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
101bd607166SKent Russell {
1020dbf2c56SLuben Tuikov unsigned char buf[8], *pia;
1030dbf2c56SLuben Tuikov u32 addr, fru_addr;
104ccdfbfecSLuben Tuikov int size, len;
1050dbf2c56SLuben Tuikov u8 csum;
106bd607166SKent Russell
107afbe5d1eSLuben Tuikov if (!is_fru_eeprom_supported(adev, &fru_addr))
108fabe01d7SJohn Clements return 0;
109fabe01d7SJohn Clements
110bd607166SKent Russell /* If algo exists, it means that the i2c_adapter's initialized */
1112f60dd50SLuben Tuikov if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
112bd607166SKent Russell DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
11302b865f8SJiansong Chen return -ENODEV;
114bd607166SKent Russell }
115bd607166SKent Russell
1160dbf2c56SLuben Tuikov /* Read the IPMI Common header */
1170dbf2c56SLuben Tuikov len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
1180dbf2c56SLuben Tuikov sizeof(buf));
1190dbf2c56SLuben Tuikov if (len != 8) {
1200dbf2c56SLuben Tuikov DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
1210dbf2c56SLuben Tuikov return len < 0 ? len : -EIO;
1220dbf2c56SLuben Tuikov }
1230dbf2c56SLuben Tuikov
1240dbf2c56SLuben Tuikov if (buf[0] != 1) {
1250dbf2c56SLuben Tuikov DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
1260dbf2c56SLuben Tuikov return -EIO;
1270dbf2c56SLuben Tuikov }
1280dbf2c56SLuben Tuikov
1290dbf2c56SLuben Tuikov for (csum = 0; len > 0; len--)
1300dbf2c56SLuben Tuikov csum += buf[len - 1];
1310dbf2c56SLuben Tuikov if (csum) {
1320dbf2c56SLuben Tuikov DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
1330dbf2c56SLuben Tuikov return -EIO;
1340dbf2c56SLuben Tuikov }
1350dbf2c56SLuben Tuikov
1360dbf2c56SLuben Tuikov /* Get the offset to the Product Info Area (PIA). */
1370dbf2c56SLuben Tuikov addr = buf[4] * 8;
1380dbf2c56SLuben Tuikov if (!addr)
1390dbf2c56SLuben Tuikov return 0;
1400dbf2c56SLuben Tuikov
1410dbf2c56SLuben Tuikov /* Get the absolute address to the PIA. */
1420dbf2c56SLuben Tuikov addr += fru_addr;
1430dbf2c56SLuben Tuikov
1440dbf2c56SLuben Tuikov /* Read the header of the PIA. */
1450dbf2c56SLuben Tuikov len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
1460dbf2c56SLuben Tuikov if (len != 3) {
1470dbf2c56SLuben Tuikov DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
1480dbf2c56SLuben Tuikov return len < 0 ? len : -EIO;
1490dbf2c56SLuben Tuikov }
1500dbf2c56SLuben Tuikov
1510dbf2c56SLuben Tuikov if (buf[0] != 1) {
1520dbf2c56SLuben Tuikov DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
1530dbf2c56SLuben Tuikov return -EIO;
1540dbf2c56SLuben Tuikov }
1550dbf2c56SLuben Tuikov
1560dbf2c56SLuben Tuikov size = buf[1] * 8;
1570dbf2c56SLuben Tuikov pia = kzalloc(size, GFP_KERNEL);
1580dbf2c56SLuben Tuikov if (!pia)
1590dbf2c56SLuben Tuikov return -ENOMEM;
1600dbf2c56SLuben Tuikov
1610dbf2c56SLuben Tuikov /* Read the whole PIA. */
1620dbf2c56SLuben Tuikov len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
1630dbf2c56SLuben Tuikov if (len != size) {
1640dbf2c56SLuben Tuikov kfree(pia);
1650dbf2c56SLuben Tuikov DRM_ERROR("Couldn't read the Product Info Area: %d", len);
1660dbf2c56SLuben Tuikov return len < 0 ? len : -EIO;
1670dbf2c56SLuben Tuikov }
1680dbf2c56SLuben Tuikov
1690dbf2c56SLuben Tuikov for (csum = 0; size > 0; size--)
1700dbf2c56SLuben Tuikov csum += pia[size - 1];
1710dbf2c56SLuben Tuikov if (csum) {
1720dbf2c56SLuben Tuikov DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
173*5d061675SLuben Tuikov kfree(pia);
1740dbf2c56SLuben Tuikov return -EIO;
1750dbf2c56SLuben Tuikov }
1760dbf2c56SLuben Tuikov
1770dbf2c56SLuben Tuikov /* Now extract useful information from the PIA.
1780dbf2c56SLuben Tuikov *
1790dbf2c56SLuben Tuikov * Skip the Manufacturer Name at [3] and go directly to
1800dbf2c56SLuben Tuikov * the Product Name field.
181bd607166SKent Russell */
1820dbf2c56SLuben Tuikov addr = 3 + 1 + (pia[3] & 0x3F);
1830dbf2c56SLuben Tuikov if (addr + 1 >= len)
1840dbf2c56SLuben Tuikov goto Out;
1850dbf2c56SLuben Tuikov memcpy(adev->product_name, pia + addr + 1,
1860dbf2c56SLuben Tuikov min_t(size_t,
1870dbf2c56SLuben Tuikov sizeof(adev->product_name),
1880dbf2c56SLuben Tuikov pia[addr] & 0x3F));
1890dbf2c56SLuben Tuikov adev->product_name[sizeof(adev->product_name) - 1] = '\0';
190bd607166SKent Russell
1910dbf2c56SLuben Tuikov /* Go to the Product Part/Model Number field. */
1920dbf2c56SLuben Tuikov addr += 1 + (pia[addr] & 0x3F);
1930dbf2c56SLuben Tuikov if (addr + 1 >= len)
1940dbf2c56SLuben Tuikov goto Out;
1950dbf2c56SLuben Tuikov memcpy(adev->product_number, pia + addr + 1,
1960dbf2c56SLuben Tuikov min_t(size_t,
1970dbf2c56SLuben Tuikov sizeof(adev->product_number),
1980dbf2c56SLuben Tuikov pia[addr] & 0x3F));
1990dbf2c56SLuben Tuikov adev->product_number[sizeof(adev->product_number) - 1] = '\0';
200bd607166SKent Russell
2010dbf2c56SLuben Tuikov /* Go to the Product Version field. */
2020dbf2c56SLuben Tuikov addr += 1 + (pia[addr] & 0x3F);
203bd607166SKent Russell
2040dbf2c56SLuben Tuikov /* Go to the Product Serial Number field. */
2050dbf2c56SLuben Tuikov addr += 1 + (pia[addr] & 0x3F);
2060dbf2c56SLuben Tuikov if (addr + 1 >= len)
2070dbf2c56SLuben Tuikov goto Out;
2080dbf2c56SLuben Tuikov memcpy(adev->serial, pia + addr + 1, min_t(size_t,
2090dbf2c56SLuben Tuikov sizeof(adev->serial),
2100dbf2c56SLuben Tuikov pia[addr] & 0x3F));
2110dbf2c56SLuben Tuikov adev->serial[sizeof(adev->serial) - 1] = '\0';
2120dbf2c56SLuben Tuikov Out:
2130dbf2c56SLuben Tuikov kfree(pia);
214bd607166SKent Russell return 0;
215bd607166SKent Russell }
2167957ec80SLijo Lazar
2177957ec80SLijo Lazar /**
2187957ec80SLijo Lazar * DOC: product_name
2197957ec80SLijo Lazar *
2207957ec80SLijo Lazar * The amdgpu driver provides a sysfs API for reporting the product name
2217957ec80SLijo Lazar * for the device
2227957ec80SLijo Lazar * The file product_name is used for this and returns the product name
2237957ec80SLijo Lazar * as returned from the FRU.
2247957ec80SLijo Lazar * NOTE: This is only available for certain server cards
2257957ec80SLijo Lazar */
2267957ec80SLijo Lazar
amdgpu_fru_product_name_show(struct device * dev,struct device_attribute * attr,char * buf)2277957ec80SLijo Lazar static ssize_t amdgpu_fru_product_name_show(struct device *dev,
2287957ec80SLijo Lazar struct device_attribute *attr,
2297957ec80SLijo Lazar char *buf)
2307957ec80SLijo Lazar {
2317957ec80SLijo Lazar struct drm_device *ddev = dev_get_drvdata(dev);
2327957ec80SLijo Lazar struct amdgpu_device *adev = drm_to_adev(ddev);
2337957ec80SLijo Lazar
2347957ec80SLijo Lazar return sysfs_emit(buf, "%s\n", adev->product_name);
2357957ec80SLijo Lazar }
2367957ec80SLijo Lazar
2377957ec80SLijo Lazar static DEVICE_ATTR(product_name, 0444, amdgpu_fru_product_name_show, NULL);
2387957ec80SLijo Lazar
2397957ec80SLijo Lazar /**
2407957ec80SLijo Lazar * DOC: product_number
2417957ec80SLijo Lazar *
2427957ec80SLijo Lazar * The amdgpu driver provides a sysfs API for reporting the part number
2437957ec80SLijo Lazar * for the device
2447957ec80SLijo Lazar * The file product_number is used for this and returns the part number
2457957ec80SLijo Lazar * as returned from the FRU.
2467957ec80SLijo Lazar * NOTE: This is only available for certain server cards
2477957ec80SLijo Lazar */
2487957ec80SLijo Lazar
amdgpu_fru_product_number_show(struct device * dev,struct device_attribute * attr,char * buf)2497957ec80SLijo Lazar static ssize_t amdgpu_fru_product_number_show(struct device *dev,
2507957ec80SLijo Lazar struct device_attribute *attr,
2517957ec80SLijo Lazar char *buf)
2527957ec80SLijo Lazar {
2537957ec80SLijo Lazar struct drm_device *ddev = dev_get_drvdata(dev);
2547957ec80SLijo Lazar struct amdgpu_device *adev = drm_to_adev(ddev);
2557957ec80SLijo Lazar
2567957ec80SLijo Lazar return sysfs_emit(buf, "%s\n", adev->product_number);
2577957ec80SLijo Lazar }
2587957ec80SLijo Lazar
2597957ec80SLijo Lazar static DEVICE_ATTR(product_number, 0444, amdgpu_fru_product_number_show, NULL);
2607957ec80SLijo Lazar
2617957ec80SLijo Lazar /**
2627957ec80SLijo Lazar * DOC: serial_number
2637957ec80SLijo Lazar *
2647957ec80SLijo Lazar * The amdgpu driver provides a sysfs API for reporting the serial number
2657957ec80SLijo Lazar * for the device
2667957ec80SLijo Lazar * The file serial_number is used for this and returns the serial number
2677957ec80SLijo Lazar * as returned from the FRU.
2687957ec80SLijo Lazar * NOTE: This is only available for certain server cards
2697957ec80SLijo Lazar */
2707957ec80SLijo Lazar
amdgpu_fru_serial_number_show(struct device * dev,struct device_attribute * attr,char * buf)2717957ec80SLijo Lazar static ssize_t amdgpu_fru_serial_number_show(struct device *dev,
2727957ec80SLijo Lazar struct device_attribute *attr,
2737957ec80SLijo Lazar char *buf)
2747957ec80SLijo Lazar {
2757957ec80SLijo Lazar struct drm_device *ddev = dev_get_drvdata(dev);
2767957ec80SLijo Lazar struct amdgpu_device *adev = drm_to_adev(ddev);
2777957ec80SLijo Lazar
2787957ec80SLijo Lazar return sysfs_emit(buf, "%s\n", adev->serial);
2797957ec80SLijo Lazar }
2807957ec80SLijo Lazar
2817957ec80SLijo Lazar static DEVICE_ATTR(serial_number, 0444, amdgpu_fru_serial_number_show, NULL);
2827957ec80SLijo Lazar
2837957ec80SLijo Lazar static const struct attribute *amdgpu_fru_attributes[] = {
2847957ec80SLijo Lazar &dev_attr_product_name.attr,
2857957ec80SLijo Lazar &dev_attr_product_number.attr,
2867957ec80SLijo Lazar &dev_attr_serial_number.attr,
2877957ec80SLijo Lazar NULL
2887957ec80SLijo Lazar };
2897957ec80SLijo Lazar
amdgpu_fru_sysfs_init(struct amdgpu_device * adev)2907957ec80SLijo Lazar int amdgpu_fru_sysfs_init(struct amdgpu_device *adev)
2917957ec80SLijo Lazar {
2927957ec80SLijo Lazar if (!is_fru_eeprom_supported(adev, NULL))
2937957ec80SLijo Lazar return 0;
2947957ec80SLijo Lazar
2957957ec80SLijo Lazar return sysfs_create_files(&adev->dev->kobj, amdgpu_fru_attributes);
2967957ec80SLijo Lazar }
2977957ec80SLijo Lazar
amdgpu_fru_sysfs_fini(struct amdgpu_device * adev)2987957ec80SLijo Lazar void amdgpu_fru_sysfs_fini(struct amdgpu_device *adev)
2997957ec80SLijo Lazar {
3007957ec80SLijo Lazar if (!is_fru_eeprom_supported(adev, NULL))
3017957ec80SLijo Lazar return;
3027957ec80SLijo Lazar
3037957ec80SLijo Lazar sysfs_remove_files(&adev->dev->kobj, amdgpu_fru_attributes);
3047957ec80SLijo Lazar }
305