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