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