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