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