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