xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c (revision 16280ded45fba1216d1d4c6acfc20c2d5b45ef50)
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 
24 #include "amdgpu_ras_eeprom.h"
25 #include "amdgpu.h"
26 #include "amdgpu_ras.h"
27 #include <linux/bits.h>
28 #include "atom.h"
29 #include "amdgpu_eeprom.h"
30 #include "amdgpu_atomfirmware.h"
31 #include <linux/debugfs.h>
32 #include <linux/uaccess.h>
33 
34 #include "amdgpu_reset.h"
35 
36 /* These are memory addresses as would be seen by one or more EEPROM
37  * chips strung on the I2C bus, usually by manipulating pins 1-3 of a
38  * set of EEPROM devices. They form a continuous memory space.
39  *
40  * The I2C device address includes the device type identifier, 1010b,
41  * which is a reserved value and indicates that this is an I2C EEPROM
42  * device. It also includes the top 3 bits of the 19 bit EEPROM memory
43  * address, namely bits 18, 17, and 16. This makes up the 7 bit
44  * address sent on the I2C bus with bit 0 being the direction bit,
45  * which is not represented here, and sent by the hardware directly.
46  *
47  * For instance,
48  *   50h = 1010000b => device type identifier 1010b, bits 18:16 = 000b, address 0.
49  *   54h = 1010100b => --"--, bits 18:16 = 100b, address 40000h.
50  *   56h = 1010110b => --"--, bits 18:16 = 110b, address 60000h.
51  * Depending on the size of the I2C EEPROM device(s), bits 18:16 may
52  * address memory in a device or a device on the I2C bus, depending on
53  * the status of pins 1-3. See top of amdgpu_eeprom.c.
54  *
55  * The RAS table lives either at address 0 or address 40000h of EEPROM.
56  */
57 #define EEPROM_I2C_MADDR_0      0x0
58 #define EEPROM_I2C_MADDR_4      0x40000
59 
60 /*
61  * The 2 macros below represent the actual size in bytes that
62  * those entities occupy in the EEPROM memory.
63  * RAS_TABLE_RECORD_SIZE is different than sizeof(eeprom_table_record) which
64  * uses uint64 to store 6b fields such as retired_page.
65  */
66 #define RAS_TABLE_HEADER_SIZE   20
67 #define RAS_TABLE_RECORD_SIZE   24
68 
69 /* Table hdr is 'AMDR' */
70 #define RAS_TABLE_HDR_VAL       0x414d4452
71 
72 /* Bad GPU tag ‘BADG’ */
73 #define RAS_TABLE_HDR_BAD       0x42414447
74 
75 /*
76  * EEPROM Table structure v1
77  * ---------------------------------
78  * |                               |
79  * |     EEPROM TABLE HEADER       |
80  * |      ( size 20 Bytes )        |
81  * |                               |
82  * ---------------------------------
83  * |                               |
84  * |    BAD PAGE RECORD AREA       |
85  * |                               |
86  * ---------------------------------
87  */
88 
89 /* Assume 2-Mbit size EEPROM and take up the whole space. */
90 #define RAS_TBL_SIZE_BYTES      (256 * 1024)
91 #define RAS_TABLE_START         0
92 #define RAS_HDR_START           RAS_TABLE_START
93 #define RAS_RECORD_START        (RAS_HDR_START + RAS_TABLE_HEADER_SIZE)
94 #define RAS_MAX_RECORD_COUNT    ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE) \
95 				 / RAS_TABLE_RECORD_SIZE)
96 
97 /*
98  * EEPROM Table structrue v2.1
99  * ---------------------------------
100  * |                               |
101  * |     EEPROM TABLE HEADER       |
102  * |      ( size 20 Bytes )        |
103  * |                               |
104  * ---------------------------------
105  * |                               |
106  * |     EEPROM TABLE RAS INFO     |
107  * | (available info size 4 Bytes) |
108  * |  ( reserved size 252 Bytes )  |
109  * |                               |
110  * ---------------------------------
111  * |                               |
112  * |     BAD PAGE RECORD AREA      |
113  * |                               |
114  * ---------------------------------
115  */
116 
117 /* EEPROM Table V2_1 */
118 #define RAS_TABLE_V2_1_INFO_SIZE       256
119 #define RAS_TABLE_V2_1_INFO_START      RAS_TABLE_HEADER_SIZE
120 #define RAS_RECORD_START_V2_1          (RAS_HDR_START + RAS_TABLE_HEADER_SIZE + \
121 					RAS_TABLE_V2_1_INFO_SIZE)
122 #define RAS_MAX_RECORD_COUNT_V2_1      ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE - \
123 					RAS_TABLE_V2_1_INFO_SIZE) \
124 					/ RAS_TABLE_RECORD_SIZE)
125 
126 /* Given a zero-based index of an EEPROM RAS record, yields the EEPROM
127  * offset off of RAS_TABLE_START.  That is, this is something you can
128  * add to control->i2c_address, and then tell I2C layer to read
129  * from/write to there. _N is the so called absolute index,
130  * because it starts right after the table header.
131  */
132 #define RAS_INDEX_TO_OFFSET(_C, _N) ((_C)->ras_record_offset + \
133 				     (_N) * RAS_TABLE_RECORD_SIZE)
134 
135 #define RAS_OFFSET_TO_INDEX(_C, _O) (((_O) - \
136 				      (_C)->ras_record_offset) / RAS_TABLE_RECORD_SIZE)
137 
138 /* Given a 0-based relative record index, 0, 1, 2, ..., etc., off
139  * of "fri", return the absolute record index off of the end of
140  * the table header.
141  */
142 #define RAS_RI_TO_AI(_C, _I) (((_I) + (_C)->ras_fri) % \
143 			      (_C)->ras_max_record_count)
144 
145 #define RAS_NUM_RECS(_tbl_hdr)  (((_tbl_hdr)->tbl_size - \
146 				  RAS_TABLE_HEADER_SIZE) / RAS_TABLE_RECORD_SIZE)
147 
148 #define RAS_NUM_RECS_V2_1(_tbl_hdr)  (((_tbl_hdr)->tbl_size - \
149 				       RAS_TABLE_HEADER_SIZE - \
150 				       RAS_TABLE_V2_1_INFO_SIZE) / RAS_TABLE_RECORD_SIZE)
151 
152 #define to_amdgpu_device(x) ((container_of(x, struct amdgpu_ras, eeprom_control))->adev)
153 
154 static bool __is_ras_eeprom_supported(struct amdgpu_device *adev)
155 {
156 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
157 	case IP_VERSION(11, 0, 2): /* VEGA20 and ARCTURUS */
158 	case IP_VERSION(11, 0, 7): /* Sienna cichlid */
159 	case IP_VERSION(13, 0, 0):
160 	case IP_VERSION(13, 0, 2): /* Aldebaran */
161 	case IP_VERSION(13, 0, 10):
162 		return true;
163 	case IP_VERSION(13, 0, 6):
164 	case IP_VERSION(13, 0, 14):
165 		return (adev->gmc.is_app_apu) ? false : true;
166 	default:
167 		return false;
168 	}
169 }
170 
171 static bool __get_eeprom_i2c_addr(struct amdgpu_device *adev,
172 				  struct amdgpu_ras_eeprom_control *control)
173 {
174 	struct atom_context *atom_ctx = adev->mode_info.atom_context;
175 	u8 i2c_addr;
176 
177 	if (!control)
178 		return false;
179 
180 	if (adev->bios && amdgpu_atomfirmware_ras_rom_addr(adev, &i2c_addr)) {
181 		/* The address given by VBIOS is an 8-bit, wire-format
182 		 * address, i.e. the most significant byte.
183 		 *
184 		 * Normalize it to a 19-bit EEPROM address. Remove the
185 		 * device type identifier and make it a 7-bit address;
186 		 * then make it a 19-bit EEPROM address. See top of
187 		 * amdgpu_eeprom.c.
188 		 */
189 		i2c_addr = (i2c_addr & 0x0F) >> 1;
190 		control->i2c_address = ((u32) i2c_addr) << 16;
191 
192 		return true;
193 	}
194 
195 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
196 	case IP_VERSION(11, 0, 2):
197 		/* VEGA20 and ARCTURUS */
198 		if (adev->asic_type == CHIP_VEGA20)
199 			control->i2c_address = EEPROM_I2C_MADDR_0;
200 		else if (strnstr(atom_ctx->vbios_pn,
201 				 "D342",
202 				 sizeof(atom_ctx->vbios_pn)))
203 			control->i2c_address = EEPROM_I2C_MADDR_0;
204 		else
205 			control->i2c_address = EEPROM_I2C_MADDR_4;
206 		return true;
207 	case IP_VERSION(11, 0, 7):
208 		control->i2c_address = EEPROM_I2C_MADDR_0;
209 		return true;
210 	case IP_VERSION(13, 0, 2):
211 		if (strnstr(atom_ctx->vbios_pn, "D673",
212 			    sizeof(atom_ctx->vbios_pn)))
213 			control->i2c_address = EEPROM_I2C_MADDR_4;
214 		else
215 			control->i2c_address = EEPROM_I2C_MADDR_0;
216 		return true;
217 	case IP_VERSION(13, 0, 0):
218 		if (strnstr(atom_ctx->vbios_pn, "D707",
219 			    sizeof(atom_ctx->vbios_pn)))
220 			control->i2c_address = EEPROM_I2C_MADDR_0;
221 		else
222 			control->i2c_address = EEPROM_I2C_MADDR_4;
223 		return true;
224 	case IP_VERSION(13, 0, 6):
225 	case IP_VERSION(13, 0, 10):
226 	case IP_VERSION(13, 0, 14):
227 		control->i2c_address = EEPROM_I2C_MADDR_4;
228 		return true;
229 	default:
230 		return false;
231 	}
232 }
233 
234 static void
235 __encode_table_header_to_buf(struct amdgpu_ras_eeprom_table_header *hdr,
236 			     unsigned char *buf)
237 {
238 	u32 *pp = (uint32_t *)buf;
239 
240 	pp[0] = cpu_to_le32(hdr->header);
241 	pp[1] = cpu_to_le32(hdr->version);
242 	pp[2] = cpu_to_le32(hdr->first_rec_offset);
243 	pp[3] = cpu_to_le32(hdr->tbl_size);
244 	pp[4] = cpu_to_le32(hdr->checksum);
245 }
246 
247 static void
248 __decode_table_header_from_buf(struct amdgpu_ras_eeprom_table_header *hdr,
249 			       unsigned char *buf)
250 {
251 	u32 *pp = (uint32_t *)buf;
252 
253 	hdr->header	      = le32_to_cpu(pp[0]);
254 	hdr->version	      = le32_to_cpu(pp[1]);
255 	hdr->first_rec_offset = le32_to_cpu(pp[2]);
256 	hdr->tbl_size	      = le32_to_cpu(pp[3]);
257 	hdr->checksum	      = le32_to_cpu(pp[4]);
258 }
259 
260 static int __write_table_header(struct amdgpu_ras_eeprom_control *control)
261 {
262 	u8 buf[RAS_TABLE_HEADER_SIZE];
263 	struct amdgpu_device *adev = to_amdgpu_device(control);
264 	int res;
265 
266 	memset(buf, 0, sizeof(buf));
267 	__encode_table_header_to_buf(&control->tbl_hdr, buf);
268 
269 	/* i2c may be unstable in gpu reset */
270 	down_read(&adev->reset_domain->sem);
271 	res = amdgpu_eeprom_write(adev->pm.ras_eeprom_i2c_bus,
272 				  control->i2c_address +
273 				  control->ras_header_offset,
274 				  buf, RAS_TABLE_HEADER_SIZE);
275 	up_read(&adev->reset_domain->sem);
276 
277 	if (res < 0) {
278 		DRM_ERROR("Failed to write EEPROM table header:%d", res);
279 	} else if (res < RAS_TABLE_HEADER_SIZE) {
280 		DRM_ERROR("Short write:%d out of %d\n",
281 			  res, RAS_TABLE_HEADER_SIZE);
282 		res = -EIO;
283 	} else {
284 		res = 0;
285 	}
286 
287 	return res;
288 }
289 
290 static void
291 __encode_table_ras_info_to_buf(struct amdgpu_ras_eeprom_table_ras_info *rai,
292 			       unsigned char *buf)
293 {
294 	u32 *pp = (uint32_t *)buf;
295 	u32 tmp;
296 
297 	tmp = ((uint32_t)(rai->rma_status) & 0xFF) |
298 	      (((uint32_t)(rai->health_percent) << 8) & 0xFF00) |
299 	      (((uint32_t)(rai->ecc_page_threshold) << 16) & 0xFFFF0000);
300 	pp[0] = cpu_to_le32(tmp);
301 }
302 
303 static void
304 __decode_table_ras_info_from_buf(struct amdgpu_ras_eeprom_table_ras_info *rai,
305 				 unsigned char *buf)
306 {
307 	u32 *pp = (uint32_t *)buf;
308 	u32 tmp;
309 
310 	tmp = le32_to_cpu(pp[0]);
311 	rai->rma_status = tmp & 0xFF;
312 	rai->health_percent = (tmp >> 8) & 0xFF;
313 	rai->ecc_page_threshold = (tmp >> 16) & 0xFFFF;
314 }
315 
316 static int __write_table_ras_info(struct amdgpu_ras_eeprom_control *control)
317 {
318 	struct amdgpu_device *adev = to_amdgpu_device(control);
319 	u8 *buf;
320 	int res;
321 
322 	buf = kzalloc(RAS_TABLE_V2_1_INFO_SIZE, GFP_KERNEL);
323 	if (!buf) {
324 		DRM_ERROR("Failed to alloc buf to write table ras info\n");
325 		return -ENOMEM;
326 	}
327 
328 	__encode_table_ras_info_to_buf(&control->tbl_rai, buf);
329 
330 	/* i2c may be unstable in gpu reset */
331 	down_read(&adev->reset_domain->sem);
332 	res = amdgpu_eeprom_write(adev->pm.ras_eeprom_i2c_bus,
333 				  control->i2c_address +
334 				  control->ras_info_offset,
335 				  buf, RAS_TABLE_V2_1_INFO_SIZE);
336 	up_read(&adev->reset_domain->sem);
337 
338 	if (res < 0) {
339 		DRM_ERROR("Failed to write EEPROM table ras info:%d", res);
340 	} else if (res < RAS_TABLE_V2_1_INFO_SIZE) {
341 		DRM_ERROR("Short write:%d out of %d\n",
342 			  res, RAS_TABLE_V2_1_INFO_SIZE);
343 		res = -EIO;
344 	} else {
345 		res = 0;
346 	}
347 
348 	kfree(buf);
349 
350 	return res;
351 }
352 
353 static u8 __calc_hdr_byte_sum(const struct amdgpu_ras_eeprom_control *control)
354 {
355 	int ii;
356 	u8  *pp, csum;
357 	size_t sz;
358 
359 	/* Header checksum, skip checksum field in the calculation */
360 	sz = sizeof(control->tbl_hdr) - sizeof(control->tbl_hdr.checksum);
361 	pp = (u8 *) &control->tbl_hdr;
362 	csum = 0;
363 	for (ii = 0; ii < sz; ii++, pp++)
364 		csum += *pp;
365 
366 	return csum;
367 }
368 
369 static u8 __calc_ras_info_byte_sum(const struct amdgpu_ras_eeprom_control *control)
370 {
371 	int ii;
372 	u8  *pp, csum;
373 	size_t sz;
374 
375 	sz = sizeof(control->tbl_rai);
376 	pp = (u8 *) &control->tbl_rai;
377 	csum = 0;
378 	for (ii = 0; ii < sz; ii++, pp++)
379 		csum += *pp;
380 
381 	return csum;
382 }
383 
384 static int amdgpu_ras_eeprom_correct_header_tag(
385 	struct amdgpu_ras_eeprom_control *control,
386 	uint32_t header)
387 {
388 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
389 	u8 *hh;
390 	int res;
391 	u8 csum;
392 
393 	csum = -hdr->checksum;
394 
395 	hh = (void *) &hdr->header;
396 	csum -= (hh[0] + hh[1] + hh[2] + hh[3]);
397 	hh = (void *) &header;
398 	csum += hh[0] + hh[1] + hh[2] + hh[3];
399 	csum = -csum;
400 	mutex_lock(&control->ras_tbl_mutex);
401 	hdr->header = header;
402 	hdr->checksum = csum;
403 	res = __write_table_header(control);
404 	mutex_unlock(&control->ras_tbl_mutex);
405 
406 	return res;
407 }
408 
409 static void amdgpu_ras_set_eeprom_table_version(struct amdgpu_ras_eeprom_control *control)
410 {
411 	struct amdgpu_device *adev = to_amdgpu_device(control);
412 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
413 
414 	switch (amdgpu_ip_version(adev, UMC_HWIP, 0)) {
415 	case IP_VERSION(8, 10, 0):
416 	case IP_VERSION(12, 0, 0):
417 		hdr->version = RAS_TABLE_VER_V2_1;
418 		return;
419 	default:
420 		hdr->version = RAS_TABLE_VER_V1;
421 		return;
422 	}
423 }
424 
425 /**
426  * amdgpu_ras_eeprom_reset_table -- Reset the RAS EEPROM table
427  * @control: pointer to control structure
428  *
429  * Reset the contents of the header of the RAS EEPROM table.
430  * Return 0 on success, -errno on error.
431  */
432 int amdgpu_ras_eeprom_reset_table(struct amdgpu_ras_eeprom_control *control)
433 {
434 	struct amdgpu_device *adev = to_amdgpu_device(control);
435 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
436 	struct amdgpu_ras_eeprom_table_ras_info *rai = &control->tbl_rai;
437 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
438 	u8 csum;
439 	int res;
440 
441 	mutex_lock(&control->ras_tbl_mutex);
442 
443 	hdr->header = RAS_TABLE_HDR_VAL;
444 	amdgpu_ras_set_eeprom_table_version(control);
445 
446 	if (hdr->version == RAS_TABLE_VER_V2_1) {
447 		hdr->first_rec_offset = RAS_RECORD_START_V2_1;
448 		hdr->tbl_size = RAS_TABLE_HEADER_SIZE +
449 				RAS_TABLE_V2_1_INFO_SIZE;
450 		rai->rma_status = GPU_HEALTH_USABLE;
451 		/**
452 		 * GPU health represented as a percentage.
453 		 * 0 means worst health, 100 means fully health.
454 		 */
455 		rai->health_percent = 100;
456 		/* ecc_page_threshold = 0 means disable bad page retirement */
457 		rai->ecc_page_threshold = con->bad_page_cnt_threshold;
458 	} else {
459 		hdr->first_rec_offset = RAS_RECORD_START;
460 		hdr->tbl_size = RAS_TABLE_HEADER_SIZE;
461 	}
462 
463 	csum = __calc_hdr_byte_sum(control);
464 	if (hdr->version == RAS_TABLE_VER_V2_1)
465 		csum += __calc_ras_info_byte_sum(control);
466 	csum = -csum;
467 	hdr->checksum = csum;
468 	res = __write_table_header(control);
469 	if (!res && hdr->version > RAS_TABLE_VER_V1)
470 		res = __write_table_ras_info(control);
471 
472 	control->ras_num_recs = 0;
473 	control->ras_num_bad_pages = 0;
474 	control->ras_fri = 0;
475 
476 	amdgpu_dpm_send_hbm_bad_pages_num(adev, control->ras_num_bad_pages);
477 
478 	control->bad_channel_bitmap = 0;
479 	amdgpu_dpm_send_hbm_bad_channel_flag(adev, control->bad_channel_bitmap);
480 	con->update_channel_flag = false;
481 
482 	amdgpu_ras_debugfs_set_ret_size(control);
483 
484 	mutex_unlock(&control->ras_tbl_mutex);
485 
486 	return res;
487 }
488 
489 static void
490 __encode_table_record_to_buf(struct amdgpu_ras_eeprom_control *control,
491 			     struct eeprom_table_record *record,
492 			     unsigned char *buf)
493 {
494 	__le64 tmp = 0;
495 	int i = 0;
496 
497 	/* Next are all record fields according to EEPROM page spec in LE foramt */
498 	buf[i++] = record->err_type;
499 
500 	buf[i++] = record->bank;
501 
502 	tmp = cpu_to_le64(record->ts);
503 	memcpy(buf + i, &tmp, 8);
504 	i += 8;
505 
506 	tmp = cpu_to_le64((record->offset & 0xffffffffffff));
507 	memcpy(buf + i, &tmp, 6);
508 	i += 6;
509 
510 	buf[i++] = record->mem_channel;
511 	buf[i++] = record->mcumc_id;
512 
513 	tmp = cpu_to_le64((record->retired_page & 0xffffffffffff));
514 	memcpy(buf + i, &tmp, 6);
515 }
516 
517 static void
518 __decode_table_record_from_buf(struct amdgpu_ras_eeprom_control *control,
519 			       struct eeprom_table_record *record,
520 			       unsigned char *buf)
521 {
522 	__le64 tmp = 0;
523 	int i =  0;
524 
525 	/* Next are all record fields according to EEPROM page spec in LE foramt */
526 	record->err_type = buf[i++];
527 
528 	record->bank = buf[i++];
529 
530 	memcpy(&tmp, buf + i, 8);
531 	record->ts = le64_to_cpu(tmp);
532 	i += 8;
533 
534 	memcpy(&tmp, buf + i, 6);
535 	record->offset = (le64_to_cpu(tmp) & 0xffffffffffff);
536 	i += 6;
537 
538 	record->mem_channel = buf[i++];
539 	record->mcumc_id = buf[i++];
540 
541 	memcpy(&tmp, buf + i,  6);
542 	record->retired_page = (le64_to_cpu(tmp) & 0xffffffffffff);
543 }
544 
545 bool amdgpu_ras_eeprom_check_err_threshold(struct amdgpu_device *adev)
546 {
547 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
548 
549 	if (!__is_ras_eeprom_supported(adev) ||
550 	    !amdgpu_bad_page_threshold)
551 		return false;
552 
553 	/* skip check eeprom table for VEGA20 Gaming */
554 	if (!con)
555 		return false;
556 	else
557 		if (!(con->features & BIT(AMDGPU_RAS_BLOCK__UMC)))
558 			return false;
559 
560 	if (con->eeprom_control.tbl_hdr.header == RAS_TABLE_HDR_BAD) {
561 		if (con->eeprom_control.ras_num_bad_pages > con->bad_page_cnt_threshold)
562 			dev_warn(adev->dev, "RAS records:%d exceed threshold:%d",
563 				 con->eeprom_control.ras_num_bad_pages, con->bad_page_cnt_threshold);
564 		if ((amdgpu_bad_page_threshold == -1) ||
565 		    (amdgpu_bad_page_threshold == -2)) {
566 			dev_warn(adev->dev,
567 				 "Please consult AMD Service Action Guide (SAG) for appropriate service procedures.\n");
568 			return false;
569 		} else {
570 			dev_warn(adev->dev,
571 				 "Please consider adjusting the customized threshold.\n");
572 			return true;
573 		}
574 	}
575 
576 	return false;
577 }
578 
579 /**
580  * __amdgpu_ras_eeprom_write -- write indexed from buffer to EEPROM
581  * @control: pointer to control structure
582  * @buf: pointer to buffer containing data to write
583  * @fri: start writing at this index
584  * @num: number of records to write
585  *
586  * The caller must hold the table mutex in @control.
587  * Return 0 on success, -errno otherwise.
588  */
589 static int __amdgpu_ras_eeprom_write(struct amdgpu_ras_eeprom_control *control,
590 				     u8 *buf, const u32 fri, const u32 num)
591 {
592 	struct amdgpu_device *adev = to_amdgpu_device(control);
593 	u32 buf_size;
594 	int res;
595 
596 	/* i2c may be unstable in gpu reset */
597 	down_read(&adev->reset_domain->sem);
598 	buf_size = num * RAS_TABLE_RECORD_SIZE;
599 	res = amdgpu_eeprom_write(adev->pm.ras_eeprom_i2c_bus,
600 				  control->i2c_address +
601 				  RAS_INDEX_TO_OFFSET(control, fri),
602 				  buf, buf_size);
603 	up_read(&adev->reset_domain->sem);
604 	if (res < 0) {
605 		DRM_ERROR("Writing %d EEPROM table records error:%d",
606 			  num, res);
607 	} else if (res < buf_size) {
608 		/* Short write, return error.
609 		 */
610 		DRM_ERROR("Wrote %d records out of %d",
611 			  res / RAS_TABLE_RECORD_SIZE, num);
612 		res = -EIO;
613 	} else {
614 		res = 0;
615 	}
616 
617 	return res;
618 }
619 
620 static int
621 amdgpu_ras_eeprom_append_table(struct amdgpu_ras_eeprom_control *control,
622 			       struct eeprom_table_record *record,
623 			       const u32 num)
624 {
625 	struct amdgpu_ras *con = amdgpu_ras_get_context(to_amdgpu_device(control));
626 	struct amdgpu_device *adev = to_amdgpu_device(control);
627 	u32 a, b, i;
628 	u8 *buf, *pp;
629 	int res;
630 
631 	buf = kcalloc(num, RAS_TABLE_RECORD_SIZE, GFP_KERNEL);
632 	if (!buf)
633 		return -ENOMEM;
634 
635 	/* Encode all of them in one go.
636 	 */
637 	pp = buf;
638 	for (i = 0; i < num; i++, pp += RAS_TABLE_RECORD_SIZE) {
639 		__encode_table_record_to_buf(control, &record[i], pp);
640 
641 		/* update bad channel bitmap */
642 		if ((record[i].mem_channel < BITS_PER_TYPE(control->bad_channel_bitmap)) &&
643 		    !(control->bad_channel_bitmap & (1 << record[i].mem_channel))) {
644 			control->bad_channel_bitmap |= 1 << record[i].mem_channel;
645 			con->update_channel_flag = true;
646 		}
647 	}
648 
649 	/* a, first record index to write into.
650 	 * b, last record index to write into.
651 	 * a = first index to read (fri) + number of records in the table,
652 	 * b = a + @num - 1.
653 	 * Let N = control->ras_max_num_record_count, then we have,
654 	 * case 0: 0 <= a <= b < N,
655 	 *   just append @num records starting at a;
656 	 * case 1: 0 <= a < N <= b,
657 	 *   append (N - a) records starting at a, and
658 	 *   append the remainder,  b % N + 1, starting at 0.
659 	 * case 2: 0 <= fri < N <= a <= b, then modulo N we get two subcases,
660 	 * case 2a: 0 <= a <= b < N
661 	 *   append num records starting at a; and fix fri if b overwrote it,
662 	 *   and since a <= b, if b overwrote it then a must've also,
663 	 *   and if b didn't overwrite it, then a didn't also.
664 	 * case 2b: 0 <= b < a < N
665 	 *   write num records starting at a, which wraps around 0=N
666 	 *   and overwrite fri unconditionally. Now from case 2a,
667 	 *   this means that b eclipsed fri to overwrite it and wrap
668 	 *   around 0 again, i.e. b = 2N+r pre modulo N, so we unconditionally
669 	 *   set fri = b + 1 (mod N).
670 	 * Now, since fri is updated in every case, except the trivial case 0,
671 	 * the number of records present in the table after writing, is,
672 	 * num_recs - 1 = b - fri (mod N), and we take the positive value,
673 	 * by adding an arbitrary multiple of N before taking the modulo N
674 	 * as shown below.
675 	 */
676 	a = control->ras_fri + control->ras_num_recs;
677 	b = a + num  - 1;
678 	if (b < control->ras_max_record_count) {
679 		res = __amdgpu_ras_eeprom_write(control, buf, a, num);
680 	} else if (a < control->ras_max_record_count) {
681 		u32 g0, g1;
682 
683 		g0 = control->ras_max_record_count - a;
684 		g1 = b % control->ras_max_record_count + 1;
685 		res = __amdgpu_ras_eeprom_write(control, buf, a, g0);
686 		if (res)
687 			goto Out;
688 		res = __amdgpu_ras_eeprom_write(control,
689 						buf + g0 * RAS_TABLE_RECORD_SIZE,
690 						0, g1);
691 		if (res)
692 			goto Out;
693 		if (g1 > control->ras_fri)
694 			control->ras_fri = g1 % control->ras_max_record_count;
695 	} else {
696 		a %= control->ras_max_record_count;
697 		b %= control->ras_max_record_count;
698 
699 		if (a <= b) {
700 			/* Note that, b - a + 1 = num. */
701 			res = __amdgpu_ras_eeprom_write(control, buf, a, num);
702 			if (res)
703 				goto Out;
704 			if (b >= control->ras_fri)
705 				control->ras_fri = (b + 1) % control->ras_max_record_count;
706 		} else {
707 			u32 g0, g1;
708 
709 			/* b < a, which means, we write from
710 			 * a to the end of the table, and from
711 			 * the start of the table to b.
712 			 */
713 			g0 = control->ras_max_record_count - a;
714 			g1 = b + 1;
715 			res = __amdgpu_ras_eeprom_write(control, buf, a, g0);
716 			if (res)
717 				goto Out;
718 			res = __amdgpu_ras_eeprom_write(control,
719 							buf + g0 * RAS_TABLE_RECORD_SIZE,
720 							0, g1);
721 			if (res)
722 				goto Out;
723 			control->ras_fri = g1 % control->ras_max_record_count;
724 		}
725 	}
726 	control->ras_num_recs = 1 + (control->ras_max_record_count + b
727 				     - control->ras_fri)
728 		% control->ras_max_record_count;
729 
730 	control->ras_num_mca_recs += num;
731 	control->ras_num_bad_pages += num * adev->umc.retire_unit;
732 
733 Out:
734 	kfree(buf);
735 	return res;
736 }
737 
738 static int
739 amdgpu_ras_eeprom_update_header(struct amdgpu_ras_eeprom_control *control)
740 {
741 	struct amdgpu_device *adev = to_amdgpu_device(control);
742 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
743 	u8 *buf, *pp, csum;
744 	u32 buf_size;
745 	int res;
746 
747 	/* Modify the header if it exceeds.
748 	 */
749 	if (amdgpu_bad_page_threshold != 0 &&
750 	    control->ras_num_bad_pages > ras->bad_page_cnt_threshold) {
751 		dev_warn(adev->dev,
752 			"Saved bad pages %d reaches threshold value %d\n",
753 			control->ras_num_bad_pages, ras->bad_page_cnt_threshold);
754 		control->tbl_hdr.header = RAS_TABLE_HDR_BAD;
755 		if (control->tbl_hdr.version == RAS_TABLE_VER_V2_1) {
756 			control->tbl_rai.rma_status = GPU_RETIRED__ECC_REACH_THRESHOLD;
757 			control->tbl_rai.health_percent = 0;
758 		}
759 
760 		if ((amdgpu_bad_page_threshold != -1) &&
761 		    (amdgpu_bad_page_threshold != -2))
762 			ras->is_rma = true;
763 
764 		/* ignore the -ENOTSUPP return value */
765 		amdgpu_dpm_send_rma_reason(adev);
766 	}
767 
768 	if (control->tbl_hdr.version == RAS_TABLE_VER_V2_1)
769 		control->tbl_hdr.tbl_size = RAS_TABLE_HEADER_SIZE +
770 					    RAS_TABLE_V2_1_INFO_SIZE +
771 					    control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
772 	else
773 		control->tbl_hdr.tbl_size = RAS_TABLE_HEADER_SIZE +
774 					    control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
775 	control->tbl_hdr.checksum = 0;
776 
777 	buf_size = control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
778 	buf = kcalloc(control->ras_num_recs, RAS_TABLE_RECORD_SIZE, GFP_KERNEL);
779 	if (!buf) {
780 		DRM_ERROR("allocating memory for table of size %d bytes failed\n",
781 			  control->tbl_hdr.tbl_size);
782 		res = -ENOMEM;
783 		goto Out;
784 	}
785 
786 	down_read(&adev->reset_domain->sem);
787 	res = amdgpu_eeprom_read(adev->pm.ras_eeprom_i2c_bus,
788 				 control->i2c_address +
789 				 control->ras_record_offset,
790 				 buf, buf_size);
791 	up_read(&adev->reset_domain->sem);
792 	if (res < 0) {
793 		DRM_ERROR("EEPROM failed reading records:%d\n",
794 			  res);
795 		goto Out;
796 	} else if (res < buf_size) {
797 		DRM_ERROR("EEPROM read %d out of %d bytes\n",
798 			  res, buf_size);
799 		res = -EIO;
800 		goto Out;
801 	}
802 
803 	/**
804 	 * bad page records have been stored in eeprom,
805 	 * now calculate gpu health percent
806 	 */
807 	if (amdgpu_bad_page_threshold != 0 &&
808 	    control->tbl_hdr.version == RAS_TABLE_VER_V2_1 &&
809 	    control->ras_num_bad_pages <= ras->bad_page_cnt_threshold)
810 		control->tbl_rai.health_percent = ((ras->bad_page_cnt_threshold -
811 						   control->ras_num_bad_pages) * 100) /
812 						   ras->bad_page_cnt_threshold;
813 
814 	/* Recalc the checksum.
815 	 */
816 	csum = 0;
817 	for (pp = buf; pp < buf + buf_size; pp++)
818 		csum += *pp;
819 
820 	csum += __calc_hdr_byte_sum(control);
821 	if (control->tbl_hdr.version == RAS_TABLE_VER_V2_1)
822 		csum += __calc_ras_info_byte_sum(control);
823 	/* avoid sign extension when assigning to "checksum" */
824 	csum = -csum;
825 	control->tbl_hdr.checksum = csum;
826 	res = __write_table_header(control);
827 	if (!res && control->tbl_hdr.version > RAS_TABLE_VER_V1)
828 		res = __write_table_ras_info(control);
829 Out:
830 	kfree(buf);
831 	return res;
832 }
833 
834 /**
835  * amdgpu_ras_eeprom_append -- append records to the EEPROM RAS table
836  * @control: pointer to control structure
837  * @record: array of records to append
838  * @num: number of records in @record array
839  *
840  * Append @num records to the table, calculate the checksum and write
841  * the table back to EEPROM. The maximum number of records that
842  * can be appended is between 1 and control->ras_max_record_count,
843  * regardless of how many records are already stored in the table.
844  *
845  * Return 0 on success or if EEPROM is not supported, -errno on error.
846  */
847 int amdgpu_ras_eeprom_append(struct amdgpu_ras_eeprom_control *control,
848 			     struct eeprom_table_record *record,
849 			     const u32 num)
850 {
851 	struct amdgpu_device *adev = to_amdgpu_device(control);
852 	int res, i;
853 	uint64_t nps = AMDGPU_NPS1_PARTITION_MODE;
854 
855 	if (!__is_ras_eeprom_supported(adev))
856 		return 0;
857 
858 	if (num == 0) {
859 		DRM_ERROR("will not append 0 records\n");
860 		return -EINVAL;
861 	} else if (num > control->ras_max_record_count) {
862 		DRM_ERROR("cannot append %d records than the size of table %d\n",
863 			  num, control->ras_max_record_count);
864 		return -EINVAL;
865 	}
866 
867 	if (adev->gmc.gmc_funcs->query_mem_partition_mode)
868 		nps = adev->gmc.gmc_funcs->query_mem_partition_mode(adev);
869 
870 	/* set the new channel index flag */
871 	for (i = 0; i < num; i++)
872 		record[i].retired_page |= (nps << UMC_NPS_SHIFT);
873 
874 	mutex_lock(&control->ras_tbl_mutex);
875 
876 	res = amdgpu_ras_eeprom_append_table(control, record, num);
877 	if (!res)
878 		res = amdgpu_ras_eeprom_update_header(control);
879 	if (!res)
880 		amdgpu_ras_debugfs_set_ret_size(control);
881 
882 	mutex_unlock(&control->ras_tbl_mutex);
883 
884 	/* clear channel index flag, the flag is only saved on eeprom */
885 	for (i = 0; i < num; i++)
886 		record[i].retired_page &= ~(nps << UMC_NPS_SHIFT);
887 
888 	return res;
889 }
890 
891 /**
892  * __amdgpu_ras_eeprom_read -- read indexed from EEPROM into buffer
893  * @control: pointer to control structure
894  * @buf: pointer to buffer to read into
895  * @fri: first record index, start reading at this index, absolute index
896  * @num: number of records to read
897  *
898  * The caller must hold the table mutex in @control.
899  * Return 0 on success, -errno otherwise.
900  */
901 static int __amdgpu_ras_eeprom_read(struct amdgpu_ras_eeprom_control *control,
902 				    u8 *buf, const u32 fri, const u32 num)
903 {
904 	struct amdgpu_device *adev = to_amdgpu_device(control);
905 	u32 buf_size;
906 	int res;
907 
908 	/* i2c may be unstable in gpu reset */
909 	down_read(&adev->reset_domain->sem);
910 	buf_size = num * RAS_TABLE_RECORD_SIZE;
911 	res = amdgpu_eeprom_read(adev->pm.ras_eeprom_i2c_bus,
912 				 control->i2c_address +
913 				 RAS_INDEX_TO_OFFSET(control, fri),
914 				 buf, buf_size);
915 	up_read(&adev->reset_domain->sem);
916 	if (res < 0) {
917 		DRM_ERROR("Reading %d EEPROM table records error:%d",
918 			  num, res);
919 	} else if (res < buf_size) {
920 		/* Short read, return error.
921 		 */
922 		DRM_ERROR("Read %d records out of %d",
923 			  res / RAS_TABLE_RECORD_SIZE, num);
924 		res = -EIO;
925 	} else {
926 		res = 0;
927 	}
928 
929 	return res;
930 }
931 
932 /**
933  * amdgpu_ras_eeprom_read -- read EEPROM
934  * @control: pointer to control structure
935  * @record: array of records to read into
936  * @num: number of records in @record
937  *
938  * Reads num records from the RAS table in EEPROM and
939  * writes the data into @record array.
940  *
941  * Returns 0 on success, -errno on error.
942  */
943 int amdgpu_ras_eeprom_read(struct amdgpu_ras_eeprom_control *control,
944 			   struct eeprom_table_record *record,
945 			   const u32 num)
946 {
947 	struct amdgpu_device *adev = to_amdgpu_device(control);
948 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
949 	int i, res;
950 	u8 *buf, *pp;
951 	u32 g0, g1;
952 
953 	if (!__is_ras_eeprom_supported(adev))
954 		return 0;
955 
956 	if (num == 0) {
957 		DRM_ERROR("will not read 0 records\n");
958 		return -EINVAL;
959 	} else if (num > control->ras_num_recs) {
960 		DRM_ERROR("too many records to read:%d available:%d\n",
961 			  num, control->ras_num_recs);
962 		return -EINVAL;
963 	}
964 
965 	buf = kcalloc(num, RAS_TABLE_RECORD_SIZE, GFP_KERNEL);
966 	if (!buf)
967 		return -ENOMEM;
968 
969 	/* Determine how many records to read, from the first record
970 	 * index, fri, to the end of the table, and from the beginning
971 	 * of the table, such that the total number of records is
972 	 * @num, and we handle wrap around when fri > 0 and
973 	 * fri + num > RAS_MAX_RECORD_COUNT.
974 	 *
975 	 * First we compute the index of the last element
976 	 * which would be fetched from each region,
977 	 * g0 is in [fri, fri + num - 1], and
978 	 * g1 is in [0, RAS_MAX_RECORD_COUNT - 1].
979 	 * Then, if g0 < RAS_MAX_RECORD_COUNT, the index of
980 	 * the last element to fetch, we set g0 to _the number_
981 	 * of elements to fetch, @num, since we know that the last
982 	 * indexed to be fetched does not exceed the table.
983 	 *
984 	 * If, however, g0 >= RAS_MAX_RECORD_COUNT, then
985 	 * we set g0 to the number of elements to read
986 	 * until the end of the table, and g1 to the number of
987 	 * elements to read from the beginning of the table.
988 	 */
989 	g0 = control->ras_fri + num - 1;
990 	g1 = g0 % control->ras_max_record_count;
991 	if (g0 < control->ras_max_record_count) {
992 		g0 = num;
993 		g1 = 0;
994 	} else {
995 		g0 = control->ras_max_record_count - control->ras_fri;
996 		g1 += 1;
997 	}
998 
999 	mutex_lock(&control->ras_tbl_mutex);
1000 	res = __amdgpu_ras_eeprom_read(control, buf, control->ras_fri, g0);
1001 	if (res)
1002 		goto Out;
1003 	if (g1) {
1004 		res = __amdgpu_ras_eeprom_read(control,
1005 					       buf + g0 * RAS_TABLE_RECORD_SIZE,
1006 					       0, g1);
1007 		if (res)
1008 			goto Out;
1009 	}
1010 
1011 	res = 0;
1012 
1013 	/* Read up everything? Then transform.
1014 	 */
1015 	pp = buf;
1016 	for (i = 0; i < num; i++, pp += RAS_TABLE_RECORD_SIZE) {
1017 		__decode_table_record_from_buf(control, &record[i], pp);
1018 
1019 		/* update bad channel bitmap */
1020 		if ((record[i].mem_channel < BITS_PER_TYPE(control->bad_channel_bitmap)) &&
1021 		    !(control->bad_channel_bitmap & (1 << record[i].mem_channel))) {
1022 			control->bad_channel_bitmap |= 1 << record[i].mem_channel;
1023 			con->update_channel_flag = true;
1024 		}
1025 	}
1026 Out:
1027 	kfree(buf);
1028 	mutex_unlock(&control->ras_tbl_mutex);
1029 
1030 	return res;
1031 }
1032 
1033 uint32_t amdgpu_ras_eeprom_max_record_count(struct amdgpu_ras_eeprom_control *control)
1034 {
1035 	/* get available eeprom table version first before eeprom table init */
1036 	amdgpu_ras_set_eeprom_table_version(control);
1037 
1038 	if (control->tbl_hdr.version == RAS_TABLE_VER_V2_1)
1039 		return RAS_MAX_RECORD_COUNT_V2_1;
1040 	else
1041 		return RAS_MAX_RECORD_COUNT;
1042 }
1043 
1044 static ssize_t
1045 amdgpu_ras_debugfs_eeprom_size_read(struct file *f, char __user *buf,
1046 				    size_t size, loff_t *pos)
1047 {
1048 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
1049 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1050 	struct amdgpu_ras_eeprom_control *control = ras ? &ras->eeprom_control : NULL;
1051 	u8 data[50];
1052 	int res;
1053 
1054 	if (!size)
1055 		return size;
1056 
1057 	if (!ras || !control) {
1058 		res = snprintf(data, sizeof(data), "Not supported\n");
1059 	} else {
1060 		res = snprintf(data, sizeof(data), "%d bytes or %d records\n",
1061 			       RAS_TBL_SIZE_BYTES, control->ras_max_record_count);
1062 	}
1063 
1064 	if (*pos >= res)
1065 		return 0;
1066 
1067 	res -= *pos;
1068 	res = min_t(size_t, res, size);
1069 
1070 	if (copy_to_user(buf, &data[*pos], res))
1071 		return -EFAULT;
1072 
1073 	*pos += res;
1074 
1075 	return res;
1076 }
1077 
1078 const struct file_operations amdgpu_ras_debugfs_eeprom_size_ops = {
1079 	.owner = THIS_MODULE,
1080 	.read = amdgpu_ras_debugfs_eeprom_size_read,
1081 	.write = NULL,
1082 	.llseek = default_llseek,
1083 };
1084 
1085 static const char *tbl_hdr_str = " Signature    Version  FirstOffs       Size   Checksum\n";
1086 static const char *tbl_hdr_fmt = "0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n";
1087 #define tbl_hdr_fmt_size (5 * (2+8) + 4 + 1)
1088 static const char *rec_hdr_str = "Index  Offset ErrType Bank/CU          TimeStamp      Offs/Addr MemChl MCUMCID    RetiredPage\n";
1089 static const char *rec_hdr_fmt = "%5d 0x%05X %7s    0x%02X 0x%016llX 0x%012llX   0x%02X    0x%02X 0x%012llX\n";
1090 #define rec_hdr_fmt_size (5 + 1 + 7 + 1 + 7 + 1 + 7 + 1 + 18 + 1 + 14 + 1 + 6 + 1 + 7 + 1 + 14 + 1)
1091 
1092 static const char *record_err_type_str[AMDGPU_RAS_EEPROM_ERR_COUNT] = {
1093 	"ignore",
1094 	"re",
1095 	"ue",
1096 };
1097 
1098 static loff_t amdgpu_ras_debugfs_table_size(struct amdgpu_ras_eeprom_control *control)
1099 {
1100 	return strlen(tbl_hdr_str) + tbl_hdr_fmt_size +
1101 		strlen(rec_hdr_str) + rec_hdr_fmt_size * control->ras_num_recs;
1102 }
1103 
1104 void amdgpu_ras_debugfs_set_ret_size(struct amdgpu_ras_eeprom_control *control)
1105 {
1106 	struct amdgpu_ras *ras = container_of(control, struct amdgpu_ras,
1107 					      eeprom_control);
1108 	struct dentry *de = ras->de_ras_eeprom_table;
1109 
1110 	if (de)
1111 		d_inode(de)->i_size = amdgpu_ras_debugfs_table_size(control);
1112 }
1113 
1114 static ssize_t amdgpu_ras_debugfs_table_read(struct file *f, char __user *buf,
1115 					     size_t size, loff_t *pos)
1116 {
1117 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
1118 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1119 	struct amdgpu_ras_eeprom_control *control = &ras->eeprom_control;
1120 	const size_t orig_size = size;
1121 	int res = -EFAULT;
1122 	size_t data_len;
1123 
1124 	mutex_lock(&control->ras_tbl_mutex);
1125 
1126 	/* We want *pos - data_len > 0, which means there's
1127 	 * bytes to be printed from data.
1128 	 */
1129 	data_len = strlen(tbl_hdr_str);
1130 	if (*pos < data_len) {
1131 		data_len -= *pos;
1132 		data_len = min_t(size_t, data_len, size);
1133 		if (copy_to_user(buf, &tbl_hdr_str[*pos], data_len))
1134 			goto Out;
1135 		buf += data_len;
1136 		size -= data_len;
1137 		*pos += data_len;
1138 	}
1139 
1140 	data_len = strlen(tbl_hdr_str) + tbl_hdr_fmt_size;
1141 	if (*pos < data_len && size > 0) {
1142 		u8 data[tbl_hdr_fmt_size + 1];
1143 		loff_t lpos;
1144 
1145 		snprintf(data, sizeof(data), tbl_hdr_fmt,
1146 			 control->tbl_hdr.header,
1147 			 control->tbl_hdr.version,
1148 			 control->tbl_hdr.first_rec_offset,
1149 			 control->tbl_hdr.tbl_size,
1150 			 control->tbl_hdr.checksum);
1151 
1152 		data_len -= *pos;
1153 		data_len = min_t(size_t, data_len, size);
1154 		lpos = *pos - strlen(tbl_hdr_str);
1155 		if (copy_to_user(buf, &data[lpos], data_len))
1156 			goto Out;
1157 		buf += data_len;
1158 		size -= data_len;
1159 		*pos += data_len;
1160 	}
1161 
1162 	data_len = strlen(tbl_hdr_str) + tbl_hdr_fmt_size + strlen(rec_hdr_str);
1163 	if (*pos < data_len && size > 0) {
1164 		loff_t lpos;
1165 
1166 		data_len -= *pos;
1167 		data_len = min_t(size_t, data_len, size);
1168 		lpos = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size;
1169 		if (copy_to_user(buf, &rec_hdr_str[lpos], data_len))
1170 			goto Out;
1171 		buf += data_len;
1172 		size -= data_len;
1173 		*pos += data_len;
1174 	}
1175 
1176 	data_len = amdgpu_ras_debugfs_table_size(control);
1177 	if (*pos < data_len && size > 0) {
1178 		u8 dare[RAS_TABLE_RECORD_SIZE];
1179 		u8 data[rec_hdr_fmt_size + 1];
1180 		struct eeprom_table_record record;
1181 		int s, r;
1182 
1183 		/* Find the starting record index
1184 		 */
1185 		s = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size -
1186 			strlen(rec_hdr_str);
1187 		s = s / rec_hdr_fmt_size;
1188 		r = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size -
1189 			strlen(rec_hdr_str);
1190 		r = r % rec_hdr_fmt_size;
1191 
1192 		for ( ; size > 0 && s < control->ras_num_recs; s++) {
1193 			u32 ai = RAS_RI_TO_AI(control, s);
1194 			/* Read a single record
1195 			 */
1196 			res = __amdgpu_ras_eeprom_read(control, dare, ai, 1);
1197 			if (res)
1198 				goto Out;
1199 			__decode_table_record_from_buf(control, &record, dare);
1200 			snprintf(data, sizeof(data), rec_hdr_fmt,
1201 				 s,
1202 				 RAS_INDEX_TO_OFFSET(control, ai),
1203 				 record_err_type_str[record.err_type],
1204 				 record.bank,
1205 				 record.ts,
1206 				 record.offset,
1207 				 record.mem_channel,
1208 				 record.mcumc_id,
1209 				 record.retired_page);
1210 
1211 			data_len = min_t(size_t, rec_hdr_fmt_size - r, size);
1212 			if (copy_to_user(buf, &data[r], data_len)) {
1213 				res = -EFAULT;
1214 				goto Out;
1215 			}
1216 			buf += data_len;
1217 			size -= data_len;
1218 			*pos += data_len;
1219 			r = 0;
1220 		}
1221 	}
1222 	res = 0;
1223 Out:
1224 	mutex_unlock(&control->ras_tbl_mutex);
1225 	return res < 0 ? res : orig_size - size;
1226 }
1227 
1228 static ssize_t
1229 amdgpu_ras_debugfs_eeprom_table_read(struct file *f, char __user *buf,
1230 				     size_t size, loff_t *pos)
1231 {
1232 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
1233 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1234 	struct amdgpu_ras_eeprom_control *control = ras ? &ras->eeprom_control : NULL;
1235 	u8 data[81];
1236 	int res;
1237 
1238 	if (!size)
1239 		return size;
1240 
1241 	if (!ras || !control) {
1242 		res = snprintf(data, sizeof(data), "Not supported\n");
1243 		if (*pos >= res)
1244 			return 0;
1245 
1246 		res -= *pos;
1247 		res = min_t(size_t, res, size);
1248 
1249 		if (copy_to_user(buf, &data[*pos], res))
1250 			return -EFAULT;
1251 
1252 		*pos += res;
1253 
1254 		return res;
1255 	} else {
1256 		return amdgpu_ras_debugfs_table_read(f, buf, size, pos);
1257 	}
1258 }
1259 
1260 const struct file_operations amdgpu_ras_debugfs_eeprom_table_ops = {
1261 	.owner = THIS_MODULE,
1262 	.read = amdgpu_ras_debugfs_eeprom_table_read,
1263 	.write = NULL,
1264 	.llseek = default_llseek,
1265 };
1266 
1267 /**
1268  * __verify_ras_table_checksum -- verify the RAS EEPROM table checksum
1269  * @control: pointer to control structure
1270  *
1271  * Check the checksum of the stored in EEPROM RAS table.
1272  *
1273  * Return 0 if the checksum is correct,
1274  * positive if it is not correct, and
1275  * -errno on I/O error.
1276  */
1277 static int __verify_ras_table_checksum(struct amdgpu_ras_eeprom_control *control)
1278 {
1279 	struct amdgpu_device *adev = to_amdgpu_device(control);
1280 	int buf_size, res;
1281 	u8  csum, *buf, *pp;
1282 
1283 	if (control->tbl_hdr.version == RAS_TABLE_VER_V2_1)
1284 		buf_size = RAS_TABLE_HEADER_SIZE +
1285 			   RAS_TABLE_V2_1_INFO_SIZE +
1286 			   control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
1287 	else
1288 		buf_size = RAS_TABLE_HEADER_SIZE +
1289 			   control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
1290 
1291 	buf = kzalloc(buf_size, GFP_KERNEL);
1292 	if (!buf) {
1293 		DRM_ERROR("Out of memory checking RAS table checksum.\n");
1294 		return -ENOMEM;
1295 	}
1296 
1297 	res = amdgpu_eeprom_read(adev->pm.ras_eeprom_i2c_bus,
1298 				 control->i2c_address +
1299 				 control->ras_header_offset,
1300 				 buf, buf_size);
1301 	if (res < buf_size) {
1302 		DRM_ERROR("Partial read for checksum, res:%d\n", res);
1303 		/* On partial reads, return -EIO.
1304 		 */
1305 		if (res >= 0)
1306 			res = -EIO;
1307 		goto Out;
1308 	}
1309 
1310 	csum = 0;
1311 	for (pp = buf; pp < buf + buf_size; pp++)
1312 		csum += *pp;
1313 Out:
1314 	kfree(buf);
1315 	return res < 0 ? res : csum;
1316 }
1317 
1318 static int __read_table_ras_info(struct amdgpu_ras_eeprom_control *control)
1319 {
1320 	struct amdgpu_ras_eeprom_table_ras_info *rai = &control->tbl_rai;
1321 	struct amdgpu_device *adev = to_amdgpu_device(control);
1322 	unsigned char *buf;
1323 	int res;
1324 
1325 	buf = kzalloc(RAS_TABLE_V2_1_INFO_SIZE, GFP_KERNEL);
1326 	if (!buf) {
1327 		DRM_ERROR("Failed to alloc buf to read EEPROM table ras info\n");
1328 		return -ENOMEM;
1329 	}
1330 
1331 	/**
1332 	 * EEPROM table V2_1 supports ras info,
1333 	 * read EEPROM table ras info
1334 	 */
1335 	res = amdgpu_eeprom_read(adev->pm.ras_eeprom_i2c_bus,
1336 				 control->i2c_address + control->ras_info_offset,
1337 				 buf, RAS_TABLE_V2_1_INFO_SIZE);
1338 	if (res < RAS_TABLE_V2_1_INFO_SIZE) {
1339 		DRM_ERROR("Failed to read EEPROM table ras info, res:%d", res);
1340 		res = res >= 0 ? -EIO : res;
1341 		goto Out;
1342 	}
1343 
1344 	__decode_table_ras_info_from_buf(rai, buf);
1345 
1346 Out:
1347 	kfree(buf);
1348 	return res == RAS_TABLE_V2_1_INFO_SIZE ? 0 : res;
1349 }
1350 
1351 int amdgpu_ras_eeprom_init(struct amdgpu_ras_eeprom_control *control)
1352 {
1353 	struct amdgpu_device *adev = to_amdgpu_device(control);
1354 	unsigned char buf[RAS_TABLE_HEADER_SIZE] = { 0 };
1355 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
1356 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1357 	int res;
1358 
1359 	ras->is_rma = false;
1360 
1361 	if (!__is_ras_eeprom_supported(adev))
1362 		return 0;
1363 
1364 	/* Verify i2c adapter is initialized */
1365 	if (!adev->pm.ras_eeprom_i2c_bus || !adev->pm.ras_eeprom_i2c_bus->algo)
1366 		return -ENOENT;
1367 
1368 	if (!__get_eeprom_i2c_addr(adev, control))
1369 		return -EINVAL;
1370 
1371 	control->ras_header_offset = RAS_HDR_START;
1372 	control->ras_info_offset = RAS_TABLE_V2_1_INFO_START;
1373 	mutex_init(&control->ras_tbl_mutex);
1374 
1375 	/* Read the table header from EEPROM address */
1376 	res = amdgpu_eeprom_read(adev->pm.ras_eeprom_i2c_bus,
1377 				 control->i2c_address + control->ras_header_offset,
1378 				 buf, RAS_TABLE_HEADER_SIZE);
1379 	if (res < RAS_TABLE_HEADER_SIZE) {
1380 		DRM_ERROR("Failed to read EEPROM table header, res:%d", res);
1381 		return res >= 0 ? -EIO : res;
1382 	}
1383 
1384 	__decode_table_header_from_buf(hdr, buf);
1385 
1386 	if (hdr->version == RAS_TABLE_VER_V2_1) {
1387 		control->ras_num_recs = RAS_NUM_RECS_V2_1(hdr);
1388 		control->ras_record_offset = RAS_RECORD_START_V2_1;
1389 		control->ras_max_record_count = RAS_MAX_RECORD_COUNT_V2_1;
1390 	} else {
1391 		control->ras_num_recs = RAS_NUM_RECS(hdr);
1392 		control->ras_record_offset = RAS_RECORD_START;
1393 		control->ras_max_record_count = RAS_MAX_RECORD_COUNT;
1394 	}
1395 	control->ras_fri = RAS_OFFSET_TO_INDEX(control, hdr->first_rec_offset);
1396 
1397 	control->ras_num_mca_recs = 0;
1398 	control->ras_num_pa_recs = 0;
1399 	return 0;
1400 }
1401 
1402 int amdgpu_ras_eeprom_check(struct amdgpu_ras_eeprom_control *control)
1403 {
1404 	struct amdgpu_device *adev = to_amdgpu_device(control);
1405 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
1406 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1407 	int res;
1408 
1409 	if (!__is_ras_eeprom_supported(adev))
1410 		return 0;
1411 
1412 	/* Verify i2c adapter is initialized */
1413 	if (!adev->pm.ras_eeprom_i2c_bus || !adev->pm.ras_eeprom_i2c_bus->algo)
1414 		return -ENOENT;
1415 
1416 	if (!__get_eeprom_i2c_addr(adev, control))
1417 		return -EINVAL;
1418 
1419 	control->ras_num_bad_pages = control->ras_num_pa_recs +
1420 			control->ras_num_mca_recs * adev->umc.retire_unit;
1421 
1422 	if (hdr->header == RAS_TABLE_HDR_VAL) {
1423 		DRM_DEBUG_DRIVER("Found existing EEPROM table with %d records",
1424 				 control->ras_num_bad_pages);
1425 
1426 		if (hdr->version == RAS_TABLE_VER_V2_1) {
1427 			res = __read_table_ras_info(control);
1428 			if (res)
1429 				return res;
1430 		}
1431 
1432 		res = __verify_ras_table_checksum(control);
1433 		if (res)
1434 			dev_err(adev->dev,
1435 				"RAS table incorrect checksum or error:%d\n",
1436 				res);
1437 
1438 		/* Warn if we are at 90% of the threshold or above
1439 		 */
1440 		if (10 * control->ras_num_bad_pages >= 9 * ras->bad_page_cnt_threshold)
1441 			dev_warn(adev->dev, "RAS records:%u exceeds 90%% of threshold:%d",
1442 					control->ras_num_bad_pages,
1443 					ras->bad_page_cnt_threshold);
1444 	} else if (hdr->header == RAS_TABLE_HDR_BAD &&
1445 		   amdgpu_bad_page_threshold != 0) {
1446 		if (hdr->version == RAS_TABLE_VER_V2_1) {
1447 			res = __read_table_ras_info(control);
1448 			if (res)
1449 				return res;
1450 		}
1451 
1452 		res = __verify_ras_table_checksum(control);
1453 		if (res) {
1454 			dev_err(adev->dev,
1455 				"RAS Table incorrect checksum or error:%d\n",
1456 				res);
1457 			return -EINVAL;
1458 		}
1459 		if (ras->bad_page_cnt_threshold >= control->ras_num_bad_pages) {
1460 			/* This means that, the threshold was increased since
1461 			 * the last time the system was booted, and now,
1462 			 * ras->bad_page_cnt_threshold - control->num_recs > 0,
1463 			 * so that at least one more record can be saved,
1464 			 * before the page count threshold is reached.
1465 			 */
1466 			dev_info(adev->dev,
1467 				 "records:%d threshold:%d, resetting "
1468 				 "RAS table header signature",
1469 				 control->ras_num_bad_pages,
1470 				 ras->bad_page_cnt_threshold);
1471 			res = amdgpu_ras_eeprom_correct_header_tag(control,
1472 								   RAS_TABLE_HDR_VAL);
1473 		} else {
1474 			dev_warn(adev->dev,
1475 				"RAS records:%d exceed threshold:%d\n",
1476 				control->ras_num_bad_pages, ras->bad_page_cnt_threshold);
1477 			if ((amdgpu_bad_page_threshold == -1) ||
1478 			    (amdgpu_bad_page_threshold == -2)) {
1479 				res = 0;
1480 				dev_warn(adev->dev,
1481 					 "Please consult AMD Service Action Guide (SAG) for appropriate service procedures\n");
1482 			} else {
1483 				ras->is_rma = true;
1484 				dev_warn(adev->dev,
1485 					 "User defined threshold is set, runtime service will be halt when threshold is reached\n");
1486 			}
1487 		}
1488 	} else {
1489 		DRM_INFO("Creating a new EEPROM table");
1490 
1491 		res = amdgpu_ras_eeprom_reset_table(control);
1492 	}
1493 
1494 	return res < 0 ? res : 0;
1495 }
1496