xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c (revision 92937f170d3f49f41d7acb86243ee691a98eb2be)
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 #define EEPROM_I2C_MADDR_VEGA20         0x0
37 #define EEPROM_I2C_MADDR_ARCTURUS       0x40000
38 #define EEPROM_I2C_MADDR_ARCTURUS_D342  0x0
39 #define EEPROM_I2C_MADDR_SIENNA_CICHLID 0x0
40 #define EEPROM_I2C_MADDR_ALDEBARAN      0x0
41 
42 /*
43  * The 2 macros bellow represent the actual size in bytes that
44  * those entities occupy in the EEPROM memory.
45  * RAS_TABLE_RECORD_SIZE is different than sizeof(eeprom_table_record) which
46  * uses uint64 to store 6b fields such as retired_page.
47  */
48 #define RAS_TABLE_HEADER_SIZE   20
49 #define RAS_TABLE_RECORD_SIZE   24
50 
51 /* Table hdr is 'AMDR' */
52 #define RAS_TABLE_HDR_VAL       0x414d4452
53 #define RAS_TABLE_VER           0x00010000
54 
55 /* Bad GPU tag ‘BADG’ */
56 #define RAS_TABLE_HDR_BAD       0x42414447
57 
58 /* Assume 2-Mbit size EEPROM and take up the whole space. */
59 #define RAS_TBL_SIZE_BYTES      (256 * 1024)
60 #define RAS_TABLE_START         0
61 #define RAS_HDR_START           RAS_TABLE_START
62 #define RAS_RECORD_START        (RAS_HDR_START + RAS_TABLE_HEADER_SIZE)
63 #define RAS_MAX_RECORD_COUNT    ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE) \
64 				 / RAS_TABLE_RECORD_SIZE)
65 
66 /* Given a zero-based index of an EEPROM RAS record, yields the EEPROM
67  * offset off of RAS_TABLE_START.  That is, this is something you can
68  * add to control->i2c_address, and then tell I2C layer to read
69  * from/write to there. _N is the so called absolute index,
70  * because it starts right after the table header.
71  */
72 #define RAS_INDEX_TO_OFFSET(_C, _N) ((_C)->ras_record_offset + \
73 				     (_N) * RAS_TABLE_RECORD_SIZE)
74 
75 #define RAS_OFFSET_TO_INDEX(_C, _O) (((_O) - \
76 				      (_C)->ras_record_offset) / RAS_TABLE_RECORD_SIZE)
77 
78 /* Given a 0-based relative record index, 0, 1, 2, ..., etc., off
79  * of "fri", return the absolute record index off of the end of
80  * the table header.
81  */
82 #define RAS_RI_TO_AI(_C, _I) (((_I) + (_C)->ras_fri) % \
83 			      (_C)->ras_max_record_count)
84 
85 #define RAS_NUM_RECS(_tbl_hdr)  (((_tbl_hdr)->tbl_size - \
86 				  RAS_TABLE_HEADER_SIZE) / RAS_TABLE_RECORD_SIZE)
87 
88 #define to_amdgpu_device(x) (container_of(x, struct amdgpu_ras, eeprom_control))->adev
89 
90 static bool __is_ras_eeprom_supported(struct amdgpu_device *adev)
91 {
92 	return  adev->asic_type == CHIP_VEGA20 ||
93 		adev->asic_type == CHIP_ARCTURUS ||
94 		adev->asic_type == CHIP_SIENNA_CICHLID ||
95 		adev->asic_type == CHIP_ALDEBARAN;
96 }
97 
98 static bool __get_eeprom_i2c_addr_arct(struct amdgpu_device *adev,
99 				       struct amdgpu_ras_eeprom_control *control)
100 {
101 	struct atom_context *atom_ctx = adev->mode_info.atom_context;
102 
103 	if (!control || !atom_ctx)
104 		return false;
105 
106 	if (strnstr(atom_ctx->vbios_version,
107 	            "D342",
108 		    sizeof(atom_ctx->vbios_version)))
109 		control->i2c_address = EEPROM_I2C_MADDR_ARCTURUS_D342;
110 	else
111 		control->i2c_address = EEPROM_I2C_MADDR_ARCTURUS;
112 
113 	return true;
114 }
115 
116 static bool __get_eeprom_i2c_addr(struct amdgpu_device *adev,
117 				  struct amdgpu_ras_eeprom_control *control)
118 {
119 	u8 i2c_addr;
120 
121 	if (!control)
122 		return false;
123 
124 	if (amdgpu_atomfirmware_ras_rom_addr(adev, &i2c_addr)) {
125 		/* The address given by VBIOS is an 8-bit, wire-format
126 		 * address, i.e. the most significant byte.
127 		 *
128 		 * Normalize it to a 19-bit EEPROM address. Remove the
129 		 * device type identifier and make it a 7-bit address;
130 		 * then make it a 19-bit EEPROM address. See top of
131 		 * amdgpu_eeprom.c.
132 		 */
133 		i2c_addr = (i2c_addr & 0x0F) >> 1;
134 		control->i2c_address = ((u32) i2c_addr) << 16;
135 
136 		return true;
137 	}
138 
139 	switch (adev->asic_type) {
140 	case CHIP_VEGA20:
141 		control->i2c_address = EEPROM_I2C_MADDR_VEGA20;
142 		break;
143 
144 	case CHIP_ARCTURUS:
145 		return __get_eeprom_i2c_addr_arct(adev, control);
146 
147 	case CHIP_SIENNA_CICHLID:
148 		control->i2c_address = EEPROM_I2C_MADDR_SIENNA_CICHLID;
149 		break;
150 
151 	case CHIP_ALDEBARAN:
152 		control->i2c_address = EEPROM_I2C_MADDR_ALDEBARAN;
153 		break;
154 
155 	default:
156 		return false;
157 	}
158 
159 	return true;
160 }
161 
162 static void
163 __encode_table_header_to_buf(struct amdgpu_ras_eeprom_table_header *hdr,
164 			     unsigned char *buf)
165 {
166 	u32 *pp = (uint32_t *)buf;
167 
168 	pp[0] = cpu_to_le32(hdr->header);
169 	pp[1] = cpu_to_le32(hdr->version);
170 	pp[2] = cpu_to_le32(hdr->first_rec_offset);
171 	pp[3] = cpu_to_le32(hdr->tbl_size);
172 	pp[4] = cpu_to_le32(hdr->checksum);
173 }
174 
175 static void
176 __decode_table_header_from_buf(struct amdgpu_ras_eeprom_table_header *hdr,
177 			       unsigned char *buf)
178 {
179 	u32 *pp = (uint32_t *)buf;
180 
181 	hdr->header	      = le32_to_cpu(pp[0]);
182 	hdr->version	      = le32_to_cpu(pp[1]);
183 	hdr->first_rec_offset = le32_to_cpu(pp[2]);
184 	hdr->tbl_size	      = le32_to_cpu(pp[3]);
185 	hdr->checksum	      = le32_to_cpu(pp[4]);
186 }
187 
188 static int __write_table_header(struct amdgpu_ras_eeprom_control *control)
189 {
190 	u8 buf[RAS_TABLE_HEADER_SIZE];
191 	struct amdgpu_device *adev = to_amdgpu_device(control);
192 	int res;
193 
194 	memset(buf, 0, sizeof(buf));
195 	__encode_table_header_to_buf(&control->tbl_hdr, buf);
196 
197 	/* i2c may be unstable in gpu reset */
198 	down_read(&adev->reset_domain->sem);
199 	res = amdgpu_eeprom_write(&adev->pm.smu_i2c,
200 				  control->i2c_address +
201 				  control->ras_header_offset,
202 				  buf, RAS_TABLE_HEADER_SIZE);
203 	up_read(&adev->reset_domain->sem);
204 
205 	if (res < 0) {
206 		DRM_ERROR("Failed to write EEPROM table header:%d", res);
207 	} else if (res < RAS_TABLE_HEADER_SIZE) {
208 		DRM_ERROR("Short write:%d out of %d\n",
209 			  res, RAS_TABLE_HEADER_SIZE);
210 		res = -EIO;
211 	} else {
212 		res = 0;
213 	}
214 
215 	return res;
216 }
217 
218 static u8 __calc_hdr_byte_sum(const struct amdgpu_ras_eeprom_control *control)
219 {
220 	int ii;
221 	u8  *pp, csum;
222 	size_t sz;
223 
224 	/* Header checksum, skip checksum field in the calculation */
225 	sz = sizeof(control->tbl_hdr) - sizeof(control->tbl_hdr.checksum);
226 	pp = (u8 *) &control->tbl_hdr;
227 	csum = 0;
228 	for (ii = 0; ii < sz; ii++, pp++)
229 		csum += *pp;
230 
231 	return csum;
232 }
233 
234 static int amdgpu_ras_eeprom_correct_header_tag(
235 	struct amdgpu_ras_eeprom_control *control,
236 	uint32_t header)
237 {
238 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
239 	u8 *hh;
240 	int res;
241 	u8 csum;
242 
243 	csum = -hdr->checksum;
244 
245 	hh = (void *) &hdr->header;
246 	csum -= (hh[0] + hh[1] + hh[2] + hh[3]);
247 	hh = (void *) &header;
248 	csum += hh[0] + hh[1] + hh[2] + hh[3];
249 	csum = -csum;
250 	mutex_lock(&control->ras_tbl_mutex);
251 	hdr->header = header;
252 	hdr->checksum = csum;
253 	res = __write_table_header(control);
254 	mutex_unlock(&control->ras_tbl_mutex);
255 
256 	return res;
257 }
258 
259 /**
260  * amdgpu_ras_eeprom_reset_table -- Reset the RAS EEPROM table
261  * @control: pointer to control structure
262  *
263  * Reset the contents of the header of the RAS EEPROM table.
264  * Return 0 on success, -errno on error.
265  */
266 int amdgpu_ras_eeprom_reset_table(struct amdgpu_ras_eeprom_control *control)
267 {
268 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
269 	u8 csum;
270 	int res;
271 
272 	mutex_lock(&control->ras_tbl_mutex);
273 
274 	hdr->header = RAS_TABLE_HDR_VAL;
275 	hdr->version = RAS_TABLE_VER;
276 	hdr->first_rec_offset = RAS_RECORD_START;
277 	hdr->tbl_size = RAS_TABLE_HEADER_SIZE;
278 
279 	csum = __calc_hdr_byte_sum(control);
280 	csum = -csum;
281 	hdr->checksum = csum;
282 	res = __write_table_header(control);
283 
284 	control->ras_num_recs = 0;
285 	control->ras_fri = 0;
286 
287 	amdgpu_ras_debugfs_set_ret_size(control);
288 
289 	mutex_unlock(&control->ras_tbl_mutex);
290 
291 	return res;
292 }
293 
294 static void
295 __encode_table_record_to_buf(struct amdgpu_ras_eeprom_control *control,
296 			     struct eeprom_table_record *record,
297 			     unsigned char *buf)
298 {
299 	__le64 tmp = 0;
300 	int i = 0;
301 
302 	/* Next are all record fields according to EEPROM page spec in LE foramt */
303 	buf[i++] = record->err_type;
304 
305 	buf[i++] = record->bank;
306 
307 	tmp = cpu_to_le64(record->ts);
308 	memcpy(buf + i, &tmp, 8);
309 	i += 8;
310 
311 	tmp = cpu_to_le64((record->offset & 0xffffffffffff));
312 	memcpy(buf + i, &tmp, 6);
313 	i += 6;
314 
315 	buf[i++] = record->mem_channel;
316 	buf[i++] = record->mcumc_id;
317 
318 	tmp = cpu_to_le64((record->retired_page & 0xffffffffffff));
319 	memcpy(buf + i, &tmp, 6);
320 }
321 
322 static void
323 __decode_table_record_from_buf(struct amdgpu_ras_eeprom_control *control,
324 			       struct eeprom_table_record *record,
325 			       unsigned char *buf)
326 {
327 	__le64 tmp = 0;
328 	int i =  0;
329 
330 	/* Next are all record fields according to EEPROM page spec in LE foramt */
331 	record->err_type = buf[i++];
332 
333 	record->bank = buf[i++];
334 
335 	memcpy(&tmp, buf + i, 8);
336 	record->ts = le64_to_cpu(tmp);
337 	i += 8;
338 
339 	memcpy(&tmp, buf + i, 6);
340 	record->offset = (le64_to_cpu(tmp) & 0xffffffffffff);
341 	i += 6;
342 
343 	record->mem_channel = buf[i++];
344 	record->mcumc_id = buf[i++];
345 
346 	memcpy(&tmp, buf + i,  6);
347 	record->retired_page = (le64_to_cpu(tmp) & 0xffffffffffff);
348 }
349 
350 bool amdgpu_ras_eeprom_check_err_threshold(struct amdgpu_device *adev)
351 {
352 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
353 
354 	if (!__is_ras_eeprom_supported(adev))
355 		return false;
356 
357 	/* skip check eeprom table for VEGA20 Gaming */
358 	if (!con)
359 		return false;
360 	else
361 		if (!(con->features & BIT(AMDGPU_RAS_BLOCK__UMC)))
362 			return false;
363 
364 	if (con->eeprom_control.tbl_hdr.header == RAS_TABLE_HDR_BAD) {
365 		dev_warn(adev->dev, "This GPU is in BAD status.");
366 		dev_warn(adev->dev, "Please retire it or set a larger "
367 			 "threshold value when reloading driver.\n");
368 		return true;
369 	}
370 
371 	return false;
372 }
373 
374 /**
375  * __amdgpu_ras_eeprom_write -- write indexed from buffer to EEPROM
376  * @control: pointer to control structure
377  * @buf: pointer to buffer containing data to write
378  * @fri: start writing at this index
379  * @num: number of records to write
380  *
381  * The caller must hold the table mutex in @control.
382  * Return 0 on success, -errno otherwise.
383  */
384 static int __amdgpu_ras_eeprom_write(struct amdgpu_ras_eeprom_control *control,
385 				     u8 *buf, const u32 fri, const u32 num)
386 {
387 	struct amdgpu_device *adev = to_amdgpu_device(control);
388 	u32 buf_size;
389 	int res;
390 
391 	/* i2c may be unstable in gpu reset */
392 	down_read(&adev->reset_domain->sem);
393 	buf_size = num * RAS_TABLE_RECORD_SIZE;
394 	res = amdgpu_eeprom_write(&adev->pm.smu_i2c,
395 				  control->i2c_address +
396 				  RAS_INDEX_TO_OFFSET(control, fri),
397 				  buf, buf_size);
398 	up_read(&adev->reset_domain->sem);
399 	if (res < 0) {
400 		DRM_ERROR("Writing %d EEPROM table records error:%d",
401 			  num, res);
402 	} else if (res < buf_size) {
403 		/* Short write, return error.
404 		 */
405 		DRM_ERROR("Wrote %d records out of %d",
406 			  res / RAS_TABLE_RECORD_SIZE, num);
407 		res = -EIO;
408 	} else {
409 		res = 0;
410 	}
411 
412 	return res;
413 }
414 
415 static int
416 amdgpu_ras_eeprom_append_table(struct amdgpu_ras_eeprom_control *control,
417 			       struct eeprom_table_record *record,
418 			       const u32 num)
419 {
420 	u32 a, b, i;
421 	u8 *buf, *pp;
422 	int res;
423 
424 	buf = kcalloc(num, RAS_TABLE_RECORD_SIZE, GFP_KERNEL);
425 	if (!buf)
426 		return -ENOMEM;
427 
428 	/* Encode all of them in one go.
429 	 */
430 	pp = buf;
431 	for (i = 0; i < num; i++, pp += RAS_TABLE_RECORD_SIZE)
432 		__encode_table_record_to_buf(control, &record[i], pp);
433 
434 	/* a, first record index to write into.
435 	 * b, last record index to write into.
436 	 * a = first index to read (fri) + number of records in the table,
437 	 * b = a + @num - 1.
438 	 * Let N = control->ras_max_num_record_count, then we have,
439 	 * case 0: 0 <= a <= b < N,
440 	 *   just append @num records starting at a;
441 	 * case 1: 0 <= a < N <= b,
442 	 *   append (N - a) records starting at a, and
443 	 *   append the remainder,  b % N + 1, starting at 0.
444 	 * case 2: 0 <= fri < N <= a <= b, then modulo N we get two subcases,
445 	 * case 2a: 0 <= a <= b < N
446 	 *   append num records starting at a; and fix fri if b overwrote it,
447 	 *   and since a <= b, if b overwrote it then a must've also,
448 	 *   and if b didn't overwrite it, then a didn't also.
449 	 * case 2b: 0 <= b < a < N
450 	 *   write num records starting at a, which wraps around 0=N
451 	 *   and overwrite fri unconditionally. Now from case 2a,
452 	 *   this means that b eclipsed fri to overwrite it and wrap
453 	 *   around 0 again, i.e. b = 2N+r pre modulo N, so we unconditionally
454 	 *   set fri = b + 1 (mod N).
455 	 * Now, since fri is updated in every case, except the trivial case 0,
456 	 * the number of records present in the table after writing, is,
457 	 * num_recs - 1 = b - fri (mod N), and we take the positive value,
458 	 * by adding an arbitrary multiple of N before taking the modulo N
459 	 * as shown below.
460 	 */
461 	a = control->ras_fri + control->ras_num_recs;
462 	b = a + num  - 1;
463 	if (b < control->ras_max_record_count) {
464 		res = __amdgpu_ras_eeprom_write(control, buf, a, num);
465 	} else if (a < control->ras_max_record_count) {
466 		u32 g0, g1;
467 
468 		g0 = control->ras_max_record_count - a;
469 		g1 = b % control->ras_max_record_count + 1;
470 		res = __amdgpu_ras_eeprom_write(control, buf, a, g0);
471 		if (res)
472 			goto Out;
473 		res = __amdgpu_ras_eeprom_write(control,
474 						buf + g0 * RAS_TABLE_RECORD_SIZE,
475 						0, g1);
476 		if (res)
477 			goto Out;
478 		if (g1 > control->ras_fri)
479 			control->ras_fri = g1 % control->ras_max_record_count;
480 	} else {
481 		a %= control->ras_max_record_count;
482 		b %= control->ras_max_record_count;
483 
484 		if (a <= b) {
485 			/* Note that, b - a + 1 = num. */
486 			res = __amdgpu_ras_eeprom_write(control, buf, a, num);
487 			if (res)
488 				goto Out;
489 			if (b >= control->ras_fri)
490 				control->ras_fri = (b + 1) % control->ras_max_record_count;
491 		} else {
492 			u32 g0, g1;
493 
494 			/* b < a, which means, we write from
495 			 * a to the end of the table, and from
496 			 * the start of the table to b.
497 			 */
498 			g0 = control->ras_max_record_count - a;
499 			g1 = b + 1;
500 			res = __amdgpu_ras_eeprom_write(control, buf, a, g0);
501 			if (res)
502 				goto Out;
503 			res = __amdgpu_ras_eeprom_write(control,
504 							buf + g0 * RAS_TABLE_RECORD_SIZE,
505 							0, g1);
506 			if (res)
507 				goto Out;
508 			control->ras_fri = g1 % control->ras_max_record_count;
509 		}
510 	}
511 	control->ras_num_recs = 1 + (control->ras_max_record_count + b
512 				     - control->ras_fri)
513 		% control->ras_max_record_count;
514 Out:
515 	kfree(buf);
516 	return res;
517 }
518 
519 static int
520 amdgpu_ras_eeprom_update_header(struct amdgpu_ras_eeprom_control *control)
521 {
522 	struct amdgpu_device *adev = to_amdgpu_device(control);
523 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
524 	u8 *buf, *pp, csum;
525 	u32 buf_size;
526 	int res;
527 
528 	/* Modify the header if it exceeds.
529 	 */
530 	if (amdgpu_bad_page_threshold != 0 &&
531 	    control->ras_num_recs >= ras->bad_page_cnt_threshold) {
532 		dev_warn(adev->dev,
533 			"Saved bad pages %d reaches threshold value %d\n",
534 			control->ras_num_recs, ras->bad_page_cnt_threshold);
535 		control->tbl_hdr.header = RAS_TABLE_HDR_BAD;
536 	}
537 
538 	control->tbl_hdr.version = RAS_TABLE_VER;
539 	control->tbl_hdr.first_rec_offset = RAS_INDEX_TO_OFFSET(control, control->ras_fri);
540 	control->tbl_hdr.tbl_size = RAS_TABLE_HEADER_SIZE + control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
541 	control->tbl_hdr.checksum = 0;
542 
543 	buf_size = control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
544 	buf = kcalloc(control->ras_num_recs, RAS_TABLE_RECORD_SIZE, GFP_KERNEL);
545 	if (!buf) {
546 		DRM_ERROR("allocating memory for table of size %d bytes failed\n",
547 			  control->tbl_hdr.tbl_size);
548 		res = -ENOMEM;
549 		goto Out;
550 	}
551 
552 	down_read(&adev->reset_domain->sem);
553 	res = amdgpu_eeprom_read(&adev->pm.smu_i2c,
554 				 control->i2c_address +
555 				 control->ras_record_offset,
556 				 buf, buf_size);
557 	up_read(&adev->reset_domain->sem);
558 	if (res < 0) {
559 		DRM_ERROR("EEPROM failed reading records:%d\n",
560 			  res);
561 		goto Out;
562 	} else if (res < buf_size) {
563 		DRM_ERROR("EEPROM read %d out of %d bytes\n",
564 			  res, buf_size);
565 		res = -EIO;
566 		goto Out;
567 	}
568 
569 	/* Recalc the checksum.
570 	 */
571 	csum = 0;
572 	for (pp = buf; pp < buf + buf_size; pp++)
573 		csum += *pp;
574 
575 	csum += __calc_hdr_byte_sum(control);
576 	/* avoid sign extension when assigning to "checksum" */
577 	csum = -csum;
578 	control->tbl_hdr.checksum = csum;
579 	res = __write_table_header(control);
580 Out:
581 	kfree(buf);
582 	return res;
583 }
584 
585 /**
586  * amdgpu_ras_eeprom_append -- append records to the EEPROM RAS table
587  * @control: pointer to control structure
588  * @record: array of records to append
589  * @num: number of records in @record array
590  *
591  * Append @num records to the table, calculate the checksum and write
592  * the table back to EEPROM. The maximum number of records that
593  * can be appended is between 1 and control->ras_max_record_count,
594  * regardless of how many records are already stored in the table.
595  *
596  * Return 0 on success or if EEPROM is not supported, -errno on error.
597  */
598 int amdgpu_ras_eeprom_append(struct amdgpu_ras_eeprom_control *control,
599 			     struct eeprom_table_record *record,
600 			     const u32 num)
601 {
602 	struct amdgpu_device *adev = to_amdgpu_device(control);
603 	int res;
604 
605 	if (!__is_ras_eeprom_supported(adev))
606 		return 0;
607 
608 	if (num == 0) {
609 		DRM_ERROR("will not append 0 records\n");
610 		return -EINVAL;
611 	} else if (num > control->ras_max_record_count) {
612 		DRM_ERROR("cannot append %d records than the size of table %d\n",
613 			  num, control->ras_max_record_count);
614 		return -EINVAL;
615 	}
616 
617 	mutex_lock(&control->ras_tbl_mutex);
618 
619 	res = amdgpu_ras_eeprom_append_table(control, record, num);
620 	if (!res)
621 		res = amdgpu_ras_eeprom_update_header(control);
622 	if (!res)
623 		amdgpu_ras_debugfs_set_ret_size(control);
624 
625 	mutex_unlock(&control->ras_tbl_mutex);
626 	return res;
627 }
628 
629 /**
630  * __amdgpu_ras_eeprom_read -- read indexed from EEPROM into buffer
631  * @control: pointer to control structure
632  * @buf: pointer to buffer to read into
633  * @fri: first record index, start reading at this index, absolute index
634  * @num: number of records to read
635  *
636  * The caller must hold the table mutex in @control.
637  * Return 0 on success, -errno otherwise.
638  */
639 static int __amdgpu_ras_eeprom_read(struct amdgpu_ras_eeprom_control *control,
640 				    u8 *buf, const u32 fri, const u32 num)
641 {
642 	struct amdgpu_device *adev = to_amdgpu_device(control);
643 	u32 buf_size;
644 	int res;
645 
646 	/* i2c may be unstable in gpu reset */
647 	down_read(&adev->reset_domain->sem);
648 	buf_size = num * RAS_TABLE_RECORD_SIZE;
649 	res = amdgpu_eeprom_read(&adev->pm.smu_i2c,
650 				 control->i2c_address +
651 				 RAS_INDEX_TO_OFFSET(control, fri),
652 				 buf, buf_size);
653 	up_read(&adev->reset_domain->sem);
654 	if (res < 0) {
655 		DRM_ERROR("Reading %d EEPROM table records error:%d",
656 			  num, res);
657 	} else if (res < buf_size) {
658 		/* Short read, return error.
659 		 */
660 		DRM_ERROR("Read %d records out of %d",
661 			  res / RAS_TABLE_RECORD_SIZE, num);
662 		res = -EIO;
663 	} else {
664 		res = 0;
665 	}
666 
667 	return res;
668 }
669 
670 /**
671  * amdgpu_ras_eeprom_read -- read EEPROM
672  * @control: pointer to control structure
673  * @record: array of records to read into
674  * @num: number of records in @record
675  *
676  * Reads num records from the RAS table in EEPROM and
677  * writes the data into @record array.
678  *
679  * Returns 0 on success, -errno on error.
680  */
681 int amdgpu_ras_eeprom_read(struct amdgpu_ras_eeprom_control *control,
682 			   struct eeprom_table_record *record,
683 			   const u32 num)
684 {
685 	struct amdgpu_device *adev = to_amdgpu_device(control);
686 	int i, res;
687 	u8 *buf, *pp;
688 	u32 g0, g1;
689 
690 	if (!__is_ras_eeprom_supported(adev))
691 		return 0;
692 
693 	if (num == 0) {
694 		DRM_ERROR("will not read 0 records\n");
695 		return -EINVAL;
696 	} else if (num > control->ras_num_recs) {
697 		DRM_ERROR("too many records to read:%d available:%d\n",
698 			  num, control->ras_num_recs);
699 		return -EINVAL;
700 	}
701 
702 	buf = kcalloc(num, RAS_TABLE_RECORD_SIZE, GFP_KERNEL);
703 	if (!buf)
704 		return -ENOMEM;
705 
706 	/* Determine how many records to read, from the first record
707 	 * index, fri, to the end of the table, and from the beginning
708 	 * of the table, such that the total number of records is
709 	 * @num, and we handle wrap around when fri > 0 and
710 	 * fri + num > RAS_MAX_RECORD_COUNT.
711 	 *
712 	 * First we compute the index of the last element
713 	 * which would be fetched from each region,
714 	 * g0 is in [fri, fri + num - 1], and
715 	 * g1 is in [0, RAS_MAX_RECORD_COUNT - 1].
716 	 * Then, if g0 < RAS_MAX_RECORD_COUNT, the index of
717 	 * the last element to fetch, we set g0 to _the number_
718 	 * of elements to fetch, @num, since we know that the last
719 	 * indexed to be fetched does not exceed the table.
720 	 *
721 	 * If, however, g0 >= RAS_MAX_RECORD_COUNT, then
722 	 * we set g0 to the number of elements to read
723 	 * until the end of the table, and g1 to the number of
724 	 * elements to read from the beginning of the table.
725 	 */
726 	g0 = control->ras_fri + num - 1;
727 	g1 = g0 % control->ras_max_record_count;
728 	if (g0 < control->ras_max_record_count) {
729 		g0 = num;
730 		g1 = 0;
731 	} else {
732 		g0 = control->ras_max_record_count - control->ras_fri;
733 		g1 += 1;
734 	}
735 
736 	mutex_lock(&control->ras_tbl_mutex);
737 	res = __amdgpu_ras_eeprom_read(control, buf, control->ras_fri, g0);
738 	if (res)
739 		goto Out;
740 	if (g1) {
741 		res = __amdgpu_ras_eeprom_read(control,
742 					       buf + g0 * RAS_TABLE_RECORD_SIZE,
743 					       0, g1);
744 		if (res)
745 			goto Out;
746 	}
747 
748 	res = 0;
749 
750 	/* Read up everything? Then transform.
751 	 */
752 	pp = buf;
753 	for (i = 0; i < num; i++, pp += RAS_TABLE_RECORD_SIZE)
754 		__decode_table_record_from_buf(control, &record[i], pp);
755 Out:
756 	kfree(buf);
757 	mutex_unlock(&control->ras_tbl_mutex);
758 
759 	return res;
760 }
761 
762 uint32_t amdgpu_ras_eeprom_max_record_count(void)
763 {
764 	return RAS_MAX_RECORD_COUNT;
765 }
766 
767 static ssize_t
768 amdgpu_ras_debugfs_eeprom_size_read(struct file *f, char __user *buf,
769 				    size_t size, loff_t *pos)
770 {
771 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
772 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
773 	struct amdgpu_ras_eeprom_control *control = ras ? &ras->eeprom_control : NULL;
774 	u8 data[50];
775 	int res;
776 
777 	if (!size)
778 		return size;
779 
780 	if (!ras || !control) {
781 		res = snprintf(data, sizeof(data), "Not supported\n");
782 	} else {
783 		res = snprintf(data, sizeof(data), "%d bytes or %d records\n",
784 			       RAS_TBL_SIZE_BYTES, control->ras_max_record_count);
785 	}
786 
787 	if (*pos >= res)
788 		return 0;
789 
790 	res -= *pos;
791 	res = min_t(size_t, res, size);
792 
793 	if (copy_to_user(buf, &data[*pos], res))
794 		return -EFAULT;
795 
796 	*pos += res;
797 
798 	return res;
799 }
800 
801 const struct file_operations amdgpu_ras_debugfs_eeprom_size_ops = {
802 	.owner = THIS_MODULE,
803 	.read = amdgpu_ras_debugfs_eeprom_size_read,
804 	.write = NULL,
805 	.llseek = default_llseek,
806 };
807 
808 static const char *tbl_hdr_str = " Signature    Version  FirstOffs       Size   Checksum\n";
809 static const char *tbl_hdr_fmt = "0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n";
810 #define tbl_hdr_fmt_size (5 * (2+8) + 4 + 1)
811 static const char *rec_hdr_str = "Index  Offset ErrType Bank/CU          TimeStamp      Offs/Addr MemChl MCUMCID    RetiredPage\n";
812 static const char *rec_hdr_fmt = "%5d 0x%05X %7s    0x%02X 0x%016llX 0x%012llX   0x%02X    0x%02X 0x%012llX\n";
813 #define rec_hdr_fmt_size (5 + 1 + 7 + 1 + 7 + 1 + 7 + 1 + 18 + 1 + 14 + 1 + 6 + 1 + 7 + 1 + 14 + 1)
814 
815 static const char *record_err_type_str[AMDGPU_RAS_EEPROM_ERR_COUNT] = {
816 	"ignore",
817 	"re",
818 	"ue",
819 };
820 
821 static loff_t amdgpu_ras_debugfs_table_size(struct amdgpu_ras_eeprom_control *control)
822 {
823 	return strlen(tbl_hdr_str) + tbl_hdr_fmt_size +
824 		strlen(rec_hdr_str) + rec_hdr_fmt_size * control->ras_num_recs;
825 }
826 
827 void amdgpu_ras_debugfs_set_ret_size(struct amdgpu_ras_eeprom_control *control)
828 {
829 	struct amdgpu_ras *ras = container_of(control, struct amdgpu_ras,
830 					      eeprom_control);
831 	struct dentry *de = ras->de_ras_eeprom_table;
832 
833 	if (de)
834 		d_inode(de)->i_size = amdgpu_ras_debugfs_table_size(control);
835 }
836 
837 static ssize_t amdgpu_ras_debugfs_table_read(struct file *f, char __user *buf,
838 					     size_t size, loff_t *pos)
839 {
840 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
841 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
842 	struct amdgpu_ras_eeprom_control *control = &ras->eeprom_control;
843 	const size_t orig_size = size;
844 	int res = -EFAULT;
845 	size_t data_len;
846 
847 	mutex_lock(&control->ras_tbl_mutex);
848 
849 	/* We want *pos - data_len > 0, which means there's
850 	 * bytes to be printed from data.
851 	 */
852 	data_len = strlen(tbl_hdr_str);
853 	if (*pos < data_len) {
854 		data_len -= *pos;
855 		data_len = min_t(size_t, data_len, size);
856 		if (copy_to_user(buf, &tbl_hdr_str[*pos], data_len))
857 			goto Out;
858 		buf += data_len;
859 		size -= data_len;
860 		*pos += data_len;
861 	}
862 
863 	data_len = strlen(tbl_hdr_str) + tbl_hdr_fmt_size;
864 	if (*pos < data_len && size > 0) {
865 		u8 data[tbl_hdr_fmt_size + 1];
866 		loff_t lpos;
867 
868 		snprintf(data, sizeof(data), tbl_hdr_fmt,
869 			 control->tbl_hdr.header,
870 			 control->tbl_hdr.version,
871 			 control->tbl_hdr.first_rec_offset,
872 			 control->tbl_hdr.tbl_size,
873 			 control->tbl_hdr.checksum);
874 
875 		data_len -= *pos;
876 		data_len = min_t(size_t, data_len, size);
877 		lpos = *pos - strlen(tbl_hdr_str);
878 		if (copy_to_user(buf, &data[lpos], data_len))
879 			goto Out;
880 		buf += data_len;
881 		size -= data_len;
882 		*pos += data_len;
883 	}
884 
885 	data_len = strlen(tbl_hdr_str) + tbl_hdr_fmt_size + strlen(rec_hdr_str);
886 	if (*pos < data_len && size > 0) {
887 		loff_t lpos;
888 
889 		data_len -= *pos;
890 		data_len = min_t(size_t, data_len, size);
891 		lpos = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size;
892 		if (copy_to_user(buf, &rec_hdr_str[lpos], data_len))
893 			goto Out;
894 		buf += data_len;
895 		size -= data_len;
896 		*pos += data_len;
897 	}
898 
899 	data_len = amdgpu_ras_debugfs_table_size(control);
900 	if (*pos < data_len && size > 0) {
901 		u8 dare[RAS_TABLE_RECORD_SIZE];
902 		u8 data[rec_hdr_fmt_size + 1];
903 		struct eeprom_table_record record;
904 		int s, r;
905 
906 		/* Find the starting record index
907 		 */
908 		s = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size -
909 			strlen(rec_hdr_str);
910 		s = s / rec_hdr_fmt_size;
911 		r = *pos - strlen(tbl_hdr_str) - tbl_hdr_fmt_size -
912 			strlen(rec_hdr_str);
913 		r = r % rec_hdr_fmt_size;
914 
915 		for ( ; size > 0 && s < control->ras_num_recs; s++) {
916 			u32 ai = RAS_RI_TO_AI(control, s);
917 			/* Read a single record
918 			 */
919 			res = __amdgpu_ras_eeprom_read(control, dare, ai, 1);
920 			if (res)
921 				goto Out;
922 			__decode_table_record_from_buf(control, &record, dare);
923 			snprintf(data, sizeof(data), rec_hdr_fmt,
924 				 s,
925 				 RAS_INDEX_TO_OFFSET(control, ai),
926 				 record_err_type_str[record.err_type],
927 				 record.bank,
928 				 record.ts,
929 				 record.offset,
930 				 record.mem_channel,
931 				 record.mcumc_id,
932 				 record.retired_page);
933 
934 			data_len = min_t(size_t, rec_hdr_fmt_size - r, size);
935 			if (copy_to_user(buf, &data[r], data_len)) {
936 				res = -EFAULT;
937 				goto Out;
938 			}
939 			buf += data_len;
940 			size -= data_len;
941 			*pos += data_len;
942 			r = 0;
943 		}
944 	}
945 	res = 0;
946 Out:
947 	mutex_unlock(&control->ras_tbl_mutex);
948 	return res < 0 ? res : orig_size - size;
949 }
950 
951 static ssize_t
952 amdgpu_ras_debugfs_eeprom_table_read(struct file *f, char __user *buf,
953 				     size_t size, loff_t *pos)
954 {
955 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
956 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
957 	struct amdgpu_ras_eeprom_control *control = ras ? &ras->eeprom_control : NULL;
958 	u8 data[81];
959 	int res;
960 
961 	if (!size)
962 		return size;
963 
964 	if (!ras || !control) {
965 		res = snprintf(data, sizeof(data), "Not supported\n");
966 		if (*pos >= res)
967 			return 0;
968 
969 		res -= *pos;
970 		res = min_t(size_t, res, size);
971 
972 		if (copy_to_user(buf, &data[*pos], res))
973 			return -EFAULT;
974 
975 		*pos += res;
976 
977 		return res;
978 	} else {
979 		return amdgpu_ras_debugfs_table_read(f, buf, size, pos);
980 	}
981 }
982 
983 const struct file_operations amdgpu_ras_debugfs_eeprom_table_ops = {
984 	.owner = THIS_MODULE,
985 	.read = amdgpu_ras_debugfs_eeprom_table_read,
986 	.write = NULL,
987 	.llseek = default_llseek,
988 };
989 
990 /**
991  * __verify_ras_table_checksum -- verify the RAS EEPROM table checksum
992  * @control: pointer to control structure
993  *
994  * Check the checksum of the stored in EEPROM RAS table.
995  *
996  * Return 0 if the checksum is correct,
997  * positive if it is not correct, and
998  * -errno on I/O error.
999  */
1000 static int __verify_ras_table_checksum(struct amdgpu_ras_eeprom_control *control)
1001 {
1002 	struct amdgpu_device *adev = to_amdgpu_device(control);
1003 	int buf_size, res;
1004 	u8  csum, *buf, *pp;
1005 
1006 	buf_size = RAS_TABLE_HEADER_SIZE +
1007 		control->ras_num_recs * RAS_TABLE_RECORD_SIZE;
1008 	buf = kzalloc(buf_size, GFP_KERNEL);
1009 	if (!buf) {
1010 		DRM_ERROR("Out of memory checking RAS table checksum.\n");
1011 		return -ENOMEM;
1012 	}
1013 
1014 	res = amdgpu_eeprom_read(&adev->pm.smu_i2c,
1015 				 control->i2c_address +
1016 				 control->ras_header_offset,
1017 				 buf, buf_size);
1018 	if (res < buf_size) {
1019 		DRM_ERROR("Partial read for checksum, res:%d\n", res);
1020 		/* On partial reads, return -EIO.
1021 		 */
1022 		if (res >= 0)
1023 			res = -EIO;
1024 		goto Out;
1025 	}
1026 
1027 	csum = 0;
1028 	for (pp = buf; pp < buf + buf_size; pp++)
1029 		csum += *pp;
1030 Out:
1031 	kfree(buf);
1032 	return res < 0 ? res : csum;
1033 }
1034 
1035 int amdgpu_ras_eeprom_init(struct amdgpu_ras_eeprom_control *control,
1036 			   bool *exceed_err_limit)
1037 {
1038 	struct amdgpu_device *adev = to_amdgpu_device(control);
1039 	unsigned char buf[RAS_TABLE_HEADER_SIZE] = { 0 };
1040 	struct amdgpu_ras_eeprom_table_header *hdr = &control->tbl_hdr;
1041 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1042 	int res;
1043 
1044 	*exceed_err_limit = false;
1045 
1046 	if (!__is_ras_eeprom_supported(adev))
1047 		return 0;
1048 
1049 	/* Verify i2c adapter is initialized */
1050 	if (!adev->pm.smu_i2c.algo)
1051 		return -ENOENT;
1052 
1053 	if (!__get_eeprom_i2c_addr(adev, control))
1054 		return -EINVAL;
1055 
1056 	control->ras_header_offset = RAS_HDR_START;
1057 	control->ras_record_offset = RAS_RECORD_START;
1058 	control->ras_max_record_count  = RAS_MAX_RECORD_COUNT;
1059 	mutex_init(&control->ras_tbl_mutex);
1060 
1061 	/* Read the table header from EEPROM address */
1062 	res = amdgpu_eeprom_read(&adev->pm.smu_i2c,
1063 				 control->i2c_address + control->ras_header_offset,
1064 				 buf, RAS_TABLE_HEADER_SIZE);
1065 	if (res < RAS_TABLE_HEADER_SIZE) {
1066 		DRM_ERROR("Failed to read EEPROM table header, res:%d", res);
1067 		return res >= 0 ? -EIO : res;
1068 	}
1069 
1070 	__decode_table_header_from_buf(hdr, buf);
1071 
1072 	control->ras_num_recs = RAS_NUM_RECS(hdr);
1073 	control->ras_fri = RAS_OFFSET_TO_INDEX(control, hdr->first_rec_offset);
1074 
1075 	if (hdr->header == RAS_TABLE_HDR_VAL) {
1076 		DRM_DEBUG_DRIVER("Found existing EEPROM table with %d records",
1077 				 control->ras_num_recs);
1078 		res = __verify_ras_table_checksum(control);
1079 		if (res)
1080 			DRM_ERROR("RAS table incorrect checksum or error:%d\n",
1081 				  res);
1082 
1083 		/* Warn if we are at 90% of the threshold or above
1084 		 */
1085 		if (10 * control->ras_num_recs >= 9 * ras->bad_page_cnt_threshold)
1086 			dev_warn(adev->dev, "RAS records:%u exceeds 90%% of threshold:%d",
1087 					control->ras_num_recs,
1088 					ras->bad_page_cnt_threshold);
1089 	} else if (hdr->header == RAS_TABLE_HDR_BAD &&
1090 		   amdgpu_bad_page_threshold != 0) {
1091 		res = __verify_ras_table_checksum(control);
1092 		if (res)
1093 			DRM_ERROR("RAS Table incorrect checksum or error:%d\n",
1094 				  res);
1095 		if (ras->bad_page_cnt_threshold > control->ras_num_recs) {
1096 			/* This means that, the threshold was increased since
1097 			 * the last time the system was booted, and now,
1098 			 * ras->bad_page_cnt_threshold - control->num_recs > 0,
1099 			 * so that at least one more record can be saved,
1100 			 * before the page count threshold is reached.
1101 			 */
1102 			dev_info(adev->dev,
1103 				 "records:%d threshold:%d, resetting "
1104 				 "RAS table header signature",
1105 				 control->ras_num_recs,
1106 				 ras->bad_page_cnt_threshold);
1107 			res = amdgpu_ras_eeprom_correct_header_tag(control,
1108 								   RAS_TABLE_HDR_VAL);
1109 		} else {
1110 			dev_err(adev->dev, "RAS records:%d exceed threshold:%d",
1111 				control->ras_num_recs, ras->bad_page_cnt_threshold);
1112 			if (amdgpu_bad_page_threshold == -2) {
1113 				dev_warn(adev->dev, "GPU will be initialized due to bad_page_threshold = -2.");
1114 				res = 0;
1115 			} else {
1116 				*exceed_err_limit = true;
1117 				dev_err(adev->dev,
1118 					"RAS records:%d exceed threshold:%d, "
1119 					"GPU will not be initialized. Replace this GPU or increase the threshold",
1120 					control->ras_num_recs, ras->bad_page_cnt_threshold);
1121 			}
1122 		}
1123 	} else {
1124 		DRM_INFO("Creating a new EEPROM table");
1125 
1126 		res = amdgpu_ras_eeprom_reset_table(control);
1127 	}
1128 
1129 	return res < 0 ? res : 0;
1130 }
1131