Linux Audio

Check our new training course

Loading...
v6.2
  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#include "amdgpu_fru_eeprom.h"
 30#include "amdgpu_eeprom.h"
 31
 32#define FRU_EEPROM_MADDR_6      0x60000
 33#define FRU_EEPROM_MADDR_8      0x80000
 
 34
 35static bool is_fru_eeprom_supported(struct amdgpu_device *adev, u32 *fru_addr)
 36{
 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.
 40	 */
 41	struct atom_context *atom_ctx = adev->mode_info.atom_context;
 42
 43	/* The i2c access is blocked on VF
 44	 * TODO: Need other way to get the info
 45	 */
 46	if (amdgpu_sriov_vf(adev))
 47		return false;
 48
 49	/* The default I2C EEPROM address of the FRU.
 50	 */
 51	if (fru_addr)
 52		*fru_addr = FRU_EEPROM_MADDR_8;
 53
 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
 59	 */
 60	switch (adev->asic_type) {
 61	case CHIP_VEGA20:
 62		/* D161 and D163 are the VG20 server SKUs */
 63		if (strnstr(atom_ctx->vbios_version, "D161",
 64			    sizeof(atom_ctx->vbios_version)) ||
 65		    strnstr(atom_ctx->vbios_version, "D163",
 66			    sizeof(atom_ctx->vbios_version))) {
 67			if (fru_addr)
 68				*fru_addr = FRU_EEPROM_MADDR_6;
 69			return true;
 70		} else {
 71			return false;
 72		}
 73	case CHIP_ALDEBARAN:
 74		/* All Aldebaran SKUs have an FRU */
 75		if (!strnstr(atom_ctx->vbios_version, "D673",
 76			     sizeof(atom_ctx->vbios_version)))
 77			if (fru_addr)
 78				*fru_addr = FRU_EEPROM_MADDR_6;
 79		return true;
 80	case CHIP_SIENNA_CICHLID:
 81		if (strnstr(atom_ctx->vbios_version, "D603",
 82			    sizeof(atom_ctx->vbios_version))) {
 83			if (strnstr(atom_ctx->vbios_version, "D603GLXE",
 84				    sizeof(atom_ctx->vbios_version))) {
 85				return false;
 86			} else {
 87				if (fru_addr)
 88					*fru_addr = FRU_EEPROM_MADDR_6;
 89				return true;
 90			}
 91		} else {
 92			return false;
 93		}
 94	default:
 95		return false;
 96	}
 97}
 98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 99int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
100{
101	unsigned char buf[8], *pia;
102	u32 addr, fru_addr;
103	int size, len;
104	u8 csum;
105
106	if (!is_fru_eeprom_supported(adev, &fru_addr))
107		return 0;
108
109	/* If algo exists, it means that the i2c_adapter's initialized */
110	if (!adev->pm.fru_eeprom_i2c_bus || !adev->pm.fru_eeprom_i2c_bus->algo) {
111		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
112		return -ENODEV;
113	}
114
115	/* Read the IPMI Common header */
116	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, fru_addr, buf,
117				 sizeof(buf));
118	if (len != 8) {
119		DRM_ERROR("Couldn't read the IPMI Common Header: %d", len);
120		return len < 0 ? len : -EIO;
 
 
 
 
 
 
 
 
 
 
 
121	}
122
123	if (buf[0] != 1) {
124		DRM_ERROR("Bad IPMI Common Header version: 0x%02x", buf[0]);
125		return -EIO;
 
 
 
 
 
126	}
127
128	for (csum = 0; len > 0; len--)
129		csum += buf[len - 1];
130	if (csum) {
131		DRM_ERROR("Bad IPMI Common Header checksum: 0x%02x", csum);
132		return -EIO;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133	}
 
 
134
135	/* Get the offset to the Product Info Area (PIA). */
136	addr = buf[4] * 8;
137	if (!addr)
138		return 0;
139
140	/* Get the absolute address to the PIA. */
141	addr += fru_addr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
143	/* Read the header of the PIA. */
144	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, buf, 3);
145	if (len != 3) {
146		DRM_ERROR("Couldn't read the Product Info Area header: %d", len);
147		return len < 0 ? len : -EIO;
148	}
149
150	if (buf[0] != 1) {
151		DRM_ERROR("Bad IPMI Product Info Area version: 0x%02x", buf[0]);
152		return -EIO;
153	}
154
155	size = buf[1] * 8;
156	pia = kzalloc(size, GFP_KERNEL);
157	if (!pia)
158		return -ENOMEM;
159
160	/* Read the whole PIA. */
161	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
162	if (len != size) {
163		kfree(pia);
164		DRM_ERROR("Couldn't read the Product Info Area: %d", len);
165		return len < 0 ? len : -EIO;
166	}
167
168	for (csum = 0; size > 0; size--)
169		csum += pia[size - 1];
170	if (csum) {
171		DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
172		return -EIO;
173	}
174
175	/* Now extract useful information from the PIA.
176	 *
177	 * Skip the Manufacturer Name at [3] and go directly to
178	 * the Product Name field.
179	 */
180	addr = 3 + 1 + (pia[3] & 0x3F);
181	if (addr + 1 >= len)
182		goto Out;
183	memcpy(adev->product_name, pia + addr + 1,
184	       min_t(size_t,
185		     sizeof(adev->product_name),
186		     pia[addr] & 0x3F));
187	adev->product_name[sizeof(adev->product_name) - 1] = '\0';
188
189	/* Go to the Product Part/Model Number field. */
190	addr += 1 + (pia[addr] & 0x3F);
191	if (addr + 1 >= len)
192		goto Out;
193	memcpy(adev->product_number, pia + addr + 1,
194	       min_t(size_t,
195		     sizeof(adev->product_number),
196		     pia[addr] & 0x3F));
197	adev->product_number[sizeof(adev->product_number) - 1] = '\0';
198
199	/* Go to the Product Version field. */
200	addr += 1 + (pia[addr] & 0x3F);
201
202	/* Go to the Product Serial Number field. */
203	addr += 1 + (pia[addr] & 0x3F);
204	if (addr + 1 >= len)
205		goto Out;
206	memcpy(adev->serial, pia + addr + 1, min_t(size_t,
207						   sizeof(adev->serial),
208						   pia[addr] & 0x3F));
209	adev->serial[sizeof(adev->serial) - 1] = '\0';
210Out:
211	kfree(pia);
212	return 0;
213}
v5.14.15
  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#include "amdgpu_fru_eeprom.h"
 
 30
 31#define I2C_PRODUCT_INFO_ADDR		0xAC
 32#define I2C_PRODUCT_INFO_ADDR_SIZE	0x2
 33#define I2C_PRODUCT_INFO_OFFSET		0xC0
 34
 35static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
 36{
 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.
 40	 */
 41	struct atom_context *atom_ctx = adev->mode_info.atom_context;
 42
 43	/* VBIOS is of the format ###-DXXXYY-##. For SKU identification,
 
 
 
 
 
 
 
 
 
 
 
 44	 * we can use just the "DXXX" portion. If there were more models, we
 45	 * could convert the 3 characters to a hex integer and use a switch
 46	 * for ease/speed/readability. For now, 2 string comparisons are
 47	 * reasonable and not too expensive
 48	 */
 49	switch (adev->asic_type) {
 50	case CHIP_VEGA20:
 51		/* D161 and D163 are the VG20 server SKUs */
 52		if (strnstr(atom_ctx->vbios_version, "D161",
 53			    sizeof(atom_ctx->vbios_version)) ||
 54		    strnstr(atom_ctx->vbios_version, "D163",
 55			    sizeof(atom_ctx->vbios_version)))
 
 
 56			return true;
 57		else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 58			return false;
 
 59	default:
 60		return false;
 61	}
 62}
 63
 64static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
 65			   unsigned char *buff)
 66{
 67	int ret, size;
 68	struct i2c_msg msg = {
 69			.addr   = I2C_PRODUCT_INFO_ADDR,
 70			.flags  = I2C_M_RD,
 71			.buf    = buff,
 72	};
 73	buff[0] = 0;
 74	buff[1] = addrptr;
 75	msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + 1;
 76	ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
 77
 78	if (ret < 1) {
 79		DRM_WARN("FRU: Failed to get size field");
 80		return ret;
 81	}
 82
 83	/* The size returned by the i2c requires subtraction of 0xC0 since the
 84	 * size apparently always reports as 0xC0+actual size.
 85	 */
 86	size = buff[2] - I2C_PRODUCT_INFO_OFFSET;
 87	/* Add 1 since address field was 1 byte */
 88	buff[1] = addrptr + 1;
 89
 90	msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + size;
 91	ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
 92
 93	if (ret < 1) {
 94		DRM_WARN("FRU: Failed to get data field");
 95		return ret;
 96	}
 97
 98	return size;
 99}
100
101int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
102{
103	unsigned char buff[34];
104	int addrptr, size;
105	int len;
 
106
107	if (!is_fru_eeprom_supported(adev))
108		return 0;
109
110	/* If algo exists, it means that the i2c_adapter's initialized */
111	if (!adev->pm.smu_i2c.algo) {
112		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
113		return -ENODEV;
114	}
115
116	/* There's a lot of repetition here. This is due to the FRU having
117	 * variable-length fields. To get the information, we have to find the
118	 * size of each field, and then keep reading along and reading along
119	 * until we get all of the data that we want. We use addrptr to track
120	 * the address as we go
121	 */
122
123	/* The first fields are all of size 1-byte, from 0-7 are offsets that
124	 * contain information that isn't useful to us.
125	 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
126	 * and the language field, so just start from 0xb, manufacturer size
127	 */
128	addrptr = 0xb;
129	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
130	if (size < 1) {
131		DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
132		return -EINVAL;
133	}
134
135	/* Increment the addrptr by the size of the field, and 1 due to the
136	 * size field being 1 byte. This pattern continues below.
137	 */
138	addrptr += size + 1;
139	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
140	if (size < 1) {
141		DRM_ERROR("Failed to read FRU product name, ret:%d", size);
142		return -EINVAL;
143	}
144
145	len = size;
146	/* Product name should only be 32 characters. Any more,
147	 * and something could be wrong. Cap it at 32 to be safe
148	 */
149	if (len >= sizeof(adev->product_name)) {
150		DRM_WARN("FRU Product Number is larger than 32 characters. This is likely a mistake");
151		len = sizeof(adev->product_name) - 1;
152	}
153	/* Start at 2 due to buff using fields 0 and 1 for the address */
154	memcpy(adev->product_name, &buff[2], len);
155	adev->product_name[len] = '\0';
156
157	addrptr += size + 1;
158	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
159	if (size < 1) {
160		DRM_ERROR("Failed to read FRU product number, ret:%d", size);
161		return -EINVAL;
162	}
163
164	len = size;
165	/* Product number should only be 16 characters. Any more,
166	 * and something could be wrong. Cap it at 16 to be safe
167	 */
168	if (len >= sizeof(adev->product_number)) {
169		DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
170		len = sizeof(adev->product_number) - 1;
171	}
172	memcpy(adev->product_number, &buff[2], len);
173	adev->product_number[len] = '\0';
174
175	addrptr += size + 1;
176	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
 
 
177
178	if (size < 1) {
179		DRM_ERROR("Failed to read FRU product version, ret:%d", size);
180		return -EINVAL;
181	}
182
183	addrptr += size + 1;
184	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
185
186	if (size < 1) {
187		DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
188		return -EINVAL;
189	}
190
191	len = size;
192	/* Serial number should only be 16 characters. Any more,
193	 * and something could be wrong. Cap it at 16 to be safe
194	 */
195	if (len >= sizeof(adev->serial)) {
196		DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
197		len = sizeof(adev->serial) - 1;
198	}
199	memcpy(adev->serial, &buff[2], len);
200	adev->serial[len] = '\0';
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202	return 0;
203}