]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
Merge drm/drm-next into drm-misc-next-fixes
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_ras.c
1 /*
2  * Copyright 2018 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 <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
28 #include <linux/reboot.h>
29 #include <linux/syscalls.h>
30 #include <linux/pm_runtime.h>
31
32 #include "amdgpu.h"
33 #include "amdgpu_ras.h"
34 #include "amdgpu_atomfirmware.h"
35 #include "amdgpu_xgmi.h"
36 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
37 #include "atom.h"
38 #include "amdgpu_reset.h"
39
40 #ifdef CONFIG_X86_MCE_AMD
41 #include <asm/mce.h>
42
43 static bool notifier_registered;
44 #endif
45 static const char *RAS_FS_NAME = "ras";
46
47 const char *ras_error_string[] = {
48         "none",
49         "parity",
50         "single_correctable",
51         "multi_uncorrectable",
52         "poison",
53 };
54
55 const char *ras_block_string[] = {
56         "umc",
57         "sdma",
58         "gfx",
59         "mmhub",
60         "athub",
61         "pcie_bif",
62         "hdp",
63         "xgmi_wafl",
64         "df",
65         "smn",
66         "sem",
67         "mp0",
68         "mp1",
69         "fuse",
70         "mca",
71         "vcn",
72         "jpeg",
73 };
74
75 const char *ras_mca_block_string[] = {
76         "mca_mp0",
77         "mca_mp1",
78         "mca_mpio",
79         "mca_iohc",
80 };
81
82 struct amdgpu_ras_block_list {
83         /* ras block link */
84         struct list_head node;
85
86         struct amdgpu_ras_block_object *ras_obj;
87 };
88
89 const char *get_ras_block_str(struct ras_common_if *ras_block)
90 {
91         if (!ras_block)
92                 return "NULL";
93
94         if (ras_block->block >= AMDGPU_RAS_BLOCK_COUNT)
95                 return "OUT OF RANGE";
96
97         if (ras_block->block == AMDGPU_RAS_BLOCK__MCA)
98                 return ras_mca_block_string[ras_block->sub_block_index];
99
100         return ras_block_string[ras_block->block];
101 }
102
103 #define ras_block_str(_BLOCK_) \
104         (((_BLOCK_) < ARRAY_SIZE(ras_block_string)) ? ras_block_string[_BLOCK_] : "Out Of Range")
105
106 #define ras_err_str(i) (ras_error_string[ffs(i)])
107
108 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
109
110 /* inject address is 52 bits */
111 #define RAS_UMC_INJECT_ADDR_LIMIT       (0x1ULL << 52)
112
113 /* typical ECC bad page rate is 1 bad page per 100MB VRAM */
114 #define RAS_BAD_PAGE_COVER              (100 * 1024 * 1024ULL)
115
116 enum amdgpu_ras_retire_page_reservation {
117         AMDGPU_RAS_RETIRE_PAGE_RESERVED,
118         AMDGPU_RAS_RETIRE_PAGE_PENDING,
119         AMDGPU_RAS_RETIRE_PAGE_FAULT,
120 };
121
122 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
123
124 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
125                                 uint64_t addr);
126 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
127                                 uint64_t addr);
128 #ifdef CONFIG_X86_MCE_AMD
129 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev);
130 struct mce_notifier_adev_list {
131         struct amdgpu_device *devs[MAX_GPU_INSTANCE];
132         int num_gpu;
133 };
134 static struct mce_notifier_adev_list mce_adev_list;
135 #endif
136
137 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
138 {
139         if (adev && amdgpu_ras_get_context(adev))
140                 amdgpu_ras_get_context(adev)->error_query_ready = ready;
141 }
142
143 static bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
144 {
145         if (adev && amdgpu_ras_get_context(adev))
146                 return amdgpu_ras_get_context(adev)->error_query_ready;
147
148         return false;
149 }
150
151 static int amdgpu_reserve_page_direct(struct amdgpu_device *adev, uint64_t address)
152 {
153         struct ras_err_data err_data = {0, 0, 0, NULL};
154         struct eeprom_table_record err_rec;
155
156         if ((address >= adev->gmc.mc_vram_size) ||
157             (address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
158                 dev_warn(adev->dev,
159                          "RAS WARN: input address 0x%llx is invalid.\n",
160                          address);
161                 return -EINVAL;
162         }
163
164         if (amdgpu_ras_check_bad_page(adev, address)) {
165                 dev_warn(adev->dev,
166                          "RAS WARN: 0x%llx has already been marked as bad page!\n",
167                          address);
168                 return 0;
169         }
170
171         memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
172         err_data.err_addr = &err_rec;
173         amdgpu_umc_fill_error_record(&err_data, address,
174                         (address >> AMDGPU_GPU_PAGE_SHIFT), 0, 0);
175
176         if (amdgpu_bad_page_threshold != 0) {
177                 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
178                                          err_data.err_addr_cnt);
179                 amdgpu_ras_save_bad_pages(adev);
180         }
181
182         dev_warn(adev->dev, "WARNING: THIS IS ONLY FOR TEST PURPOSES AND WILL CORRUPT RAS EEPROM\n");
183         dev_warn(adev->dev, "Clear EEPROM:\n");
184         dev_warn(adev->dev, "    echo 1 > /sys/kernel/debug/dri/0/ras/ras_eeprom_reset\n");
185
186         return 0;
187 }
188
189 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
190                                         size_t size, loff_t *pos)
191 {
192         struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
193         struct ras_query_if info = {
194                 .head = obj->head,
195         };
196         ssize_t s;
197         char val[128];
198
199         if (amdgpu_ras_query_error_status(obj->adev, &info))
200                 return -EINVAL;
201
202         /* Hardware counter will be reset automatically after the query on Vega20 and Arcturus */
203         if (obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
204             obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4)) {
205                 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
206                         dev_warn(obj->adev->dev, "Failed to reset error counter and error status");
207         }
208
209         s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
210                         "ue", info.ue_count,
211                         "ce", info.ce_count);
212         if (*pos >= s)
213                 return 0;
214
215         s -= *pos;
216         s = min_t(u64, s, size);
217
218
219         if (copy_to_user(buf, &val[*pos], s))
220                 return -EINVAL;
221
222         *pos += s;
223
224         return s;
225 }
226
227 static const struct file_operations amdgpu_ras_debugfs_ops = {
228         .owner = THIS_MODULE,
229         .read = amdgpu_ras_debugfs_read,
230         .write = NULL,
231         .llseek = default_llseek
232 };
233
234 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
235 {
236         int i;
237
238         for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
239                 *block_id = i;
240                 if (strcmp(name, ras_block_string[i]) == 0)
241                         return 0;
242         }
243         return -EINVAL;
244 }
245
246 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
247                 const char __user *buf, size_t size,
248                 loff_t *pos, struct ras_debug_if *data)
249 {
250         ssize_t s = min_t(u64, 64, size);
251         char str[65];
252         char block_name[33];
253         char err[9] = "ue";
254         int op = -1;
255         int block_id;
256         uint32_t sub_block;
257         u64 address, value;
258
259         if (*pos)
260                 return -EINVAL;
261         *pos = size;
262
263         memset(str, 0, sizeof(str));
264         memset(data, 0, sizeof(*data));
265
266         if (copy_from_user(str, buf, s))
267                 return -EINVAL;
268
269         if (sscanf(str, "disable %32s", block_name) == 1)
270                 op = 0;
271         else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
272                 op = 1;
273         else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
274                 op = 2;
275         else if (strstr(str, "retire_page") != NULL)
276                 op = 3;
277         else if (str[0] && str[1] && str[2] && str[3])
278                 /* ascii string, but commands are not matched. */
279                 return -EINVAL;
280
281         if (op != -1) {
282                 if (op == 3) {
283                         if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
284                             sscanf(str, "%*s %llu", &address) != 1)
285                                 return -EINVAL;
286
287                         data->op = op;
288                         data->inject.address = address;
289
290                         return 0;
291                 }
292
293                 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
294                         return -EINVAL;
295
296                 data->head.block = block_id;
297                 /* only ue and ce errors are supported */
298                 if (!memcmp("ue", err, 2))
299                         data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
300                 else if (!memcmp("ce", err, 2))
301                         data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
302                 else
303                         return -EINVAL;
304
305                 data->op = op;
306
307                 if (op == 2) {
308                         if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
309                                    &sub_block, &address, &value) != 3 &&
310                             sscanf(str, "%*s %*s %*s %u %llu %llu",
311                                    &sub_block, &address, &value) != 3)
312                                 return -EINVAL;
313                         data->head.sub_block_index = sub_block;
314                         data->inject.address = address;
315                         data->inject.value = value;
316                 }
317         } else {
318                 if (size < sizeof(*data))
319                         return -EINVAL;
320
321                 if (copy_from_user(data, buf, sizeof(*data)))
322                         return -EINVAL;
323         }
324
325         return 0;
326 }
327
328 /**
329  * DOC: AMDGPU RAS debugfs control interface
330  *
331  * The control interface accepts struct ras_debug_if which has two members.
332  *
333  * First member: ras_debug_if::head or ras_debug_if::inject.
334  *
335  * head is used to indicate which IP block will be under control.
336  *
337  * head has four members, they are block, type, sub_block_index, name.
338  * block: which IP will be under control.
339  * type: what kind of error will be enabled/disabled/injected.
340  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
341  * name: the name of IP.
342  *
343  * inject has two more members than head, they are address, value.
344  * As their names indicate, inject operation will write the
345  * value to the address.
346  *
347  * The second member: struct ras_debug_if::op.
348  * It has three kinds of operations.
349  *
350  * - 0: disable RAS on the block. Take ::head as its data.
351  * - 1: enable RAS on the block. Take ::head as its data.
352  * - 2: inject errors on the block. Take ::inject as its data.
353  *
354  * How to use the interface?
355  *
356  * In a program
357  *
358  * Copy the struct ras_debug_if in your code and initialize it.
359  * Write the struct to the control interface.
360  *
361  * From shell
362  *
363  * .. code-block:: bash
364  *
365  *      echo "disable <block>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
366  *      echo "enable  <block> <error>" > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
367  *      echo "inject  <block> <error> <sub-block> <address> <value> > /sys/kernel/debug/dri/<N>/ras/ras_ctrl
368  *
369  * Where N, is the card which you want to affect.
370  *
371  * "disable" requires only the block.
372  * "enable" requires the block and error type.
373  * "inject" requires the block, error type, address, and value.
374  *
375  * The block is one of: umc, sdma, gfx, etc.
376  *      see ras_block_string[] for details
377  *
378  * The error type is one of: ue, ce, where,
379  *      ue is multi-uncorrectable
380  *      ce is single-correctable
381  *
382  * The sub-block is a the sub-block index, pass 0 if there is no sub-block.
383  * The address and value are hexadecimal numbers, leading 0x is optional.
384  *
385  * For instance,
386  *
387  * .. code-block:: bash
388  *
389  *      echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
390  *      echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
391  *      echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
392  *
393  * How to check the result of the operation?
394  *
395  * To check disable/enable, see "ras" features at,
396  * /sys/class/drm/card[0/1/2...]/device/ras/features
397  *
398  * To check inject, see the corresponding error count at,
399  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx|sdma|umc|...]_err_count
400  *
401  * .. note::
402  *      Operations are only allowed on blocks which are supported.
403  *      Check the "ras" mask at /sys/module/amdgpu/parameters/ras_mask
404  *      to see which blocks support RAS on a particular asic.
405  *
406  */
407 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f,
408                                              const char __user *buf,
409                                              size_t size, loff_t *pos)
410 {
411         struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
412         struct ras_debug_if data;
413         int ret = 0;
414
415         if (!amdgpu_ras_get_error_query_ready(adev)) {
416                 dev_warn(adev->dev, "RAS WARN: error injection "
417                                 "currently inaccessible\n");
418                 return size;
419         }
420
421         ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
422         if (ret)
423                 return ret;
424
425         if (data.op == 3) {
426                 ret = amdgpu_reserve_page_direct(adev, data.inject.address);
427                 if (!ret)
428                         return size;
429                 else
430                         return ret;
431         }
432
433         if (!amdgpu_ras_is_supported(adev, data.head.block))
434                 return -EINVAL;
435
436         switch (data.op) {
437         case 0:
438                 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
439                 break;
440         case 1:
441                 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
442                 break;
443         case 2:
444                 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
445                     (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
446                         dev_warn(adev->dev, "RAS WARN: input address "
447                                         "0x%llx is invalid.",
448                                         data.inject.address);
449                         ret = -EINVAL;
450                         break;
451                 }
452
453                 /* umc ce/ue error injection for a bad page is not allowed */
454                 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
455                     amdgpu_ras_check_bad_page(adev, data.inject.address)) {
456                         dev_warn(adev->dev, "RAS WARN: inject: 0x%llx has "
457                                  "already been marked as bad!\n",
458                                  data.inject.address);
459                         break;
460                 }
461
462                 /* data.inject.address is offset instead of absolute gpu address */
463                 ret = amdgpu_ras_error_inject(adev, &data.inject);
464                 break;
465         default:
466                 ret = -EINVAL;
467                 break;
468         }
469
470         if (ret)
471                 return ret;
472
473         return size;
474 }
475
476 /**
477  * DOC: AMDGPU RAS debugfs EEPROM table reset interface
478  *
479  * Some boards contain an EEPROM which is used to persistently store a list of
480  * bad pages which experiences ECC errors in vram.  This interface provides
481  * a way to reset the EEPROM, e.g., after testing error injection.
482  *
483  * Usage:
484  *
485  * .. code-block:: bash
486  *
487  *      echo 1 > ../ras/ras_eeprom_reset
488  *
489  * will reset EEPROM table to 0 entries.
490  *
491  */
492 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f,
493                                                const char __user *buf,
494                                                size_t size, loff_t *pos)
495 {
496         struct amdgpu_device *adev =
497                 (struct amdgpu_device *)file_inode(f)->i_private;
498         int ret;
499
500         ret = amdgpu_ras_eeprom_reset_table(
501                 &(amdgpu_ras_get_context(adev)->eeprom_control));
502
503         if (!ret) {
504                 /* Something was written to EEPROM.
505                  */
506                 amdgpu_ras_get_context(adev)->flags = RAS_DEFAULT_FLAGS;
507                 return size;
508         } else {
509                 return ret;
510         }
511 }
512
513 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
514         .owner = THIS_MODULE,
515         .read = NULL,
516         .write = amdgpu_ras_debugfs_ctrl_write,
517         .llseek = default_llseek
518 };
519
520 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
521         .owner = THIS_MODULE,
522         .read = NULL,
523         .write = amdgpu_ras_debugfs_eeprom_write,
524         .llseek = default_llseek
525 };
526
527 /**
528  * DOC: AMDGPU RAS sysfs Error Count Interface
529  *
530  * It allows the user to read the error count for each IP block on the gpu through
531  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
532  *
533  * It outputs the multiple lines which report the uncorrected (ue) and corrected
534  * (ce) error counts.
535  *
536  * The format of one line is below,
537  *
538  * [ce|ue]: count
539  *
540  * Example:
541  *
542  * .. code-block:: bash
543  *
544  *      ue: 0
545  *      ce: 1
546  *
547  */
548 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
549                 struct device_attribute *attr, char *buf)
550 {
551         struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
552         struct ras_query_if info = {
553                 .head = obj->head,
554         };
555
556         if (!amdgpu_ras_get_error_query_ready(obj->adev))
557                 return sysfs_emit(buf, "Query currently inaccessible\n");
558
559         if (amdgpu_ras_query_error_status(obj->adev, &info))
560                 return -EINVAL;
561
562         if (obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
563             obj->adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4)) {
564                 if (amdgpu_ras_reset_error_status(obj->adev, info.head.block))
565                         dev_warn(obj->adev->dev, "Failed to reset error counter and error status");
566         }
567
568         return sysfs_emit(buf, "%s: %lu\n%s: %lu\n", "ue", info.ue_count,
569                           "ce", info.ce_count);
570 }
571
572 /* obj begin */
573
574 #define get_obj(obj) do { (obj)->use++; } while (0)
575 #define alive_obj(obj) ((obj)->use)
576
577 static inline void put_obj(struct ras_manager *obj)
578 {
579         if (obj && (--obj->use == 0))
580                 list_del(&obj->node);
581         if (obj && (obj->use < 0))
582                 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", get_ras_block_str(&obj->head));
583 }
584
585 /* make one obj and return it. */
586 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
587                 struct ras_common_if *head)
588 {
589         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
590         struct ras_manager *obj;
591
592         if (!adev->ras_enabled || !con)
593                 return NULL;
594
595         if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
596                 return NULL;
597
598         if (head->block == AMDGPU_RAS_BLOCK__MCA) {
599                 if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
600                         return NULL;
601
602                 obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
603         } else
604                 obj = &con->objs[head->block];
605
606         /* already exist. return obj? */
607         if (alive_obj(obj))
608                 return NULL;
609
610         obj->head = *head;
611         obj->adev = adev;
612         list_add(&obj->node, &con->head);
613         get_obj(obj);
614
615         return obj;
616 }
617
618 /* return an obj equal to head, or the first when head is NULL */
619 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
620                 struct ras_common_if *head)
621 {
622         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
623         struct ras_manager *obj;
624         int i;
625
626         if (!adev->ras_enabled || !con)
627                 return NULL;
628
629         if (head) {
630                 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
631                         return NULL;
632
633                 if (head->block == AMDGPU_RAS_BLOCK__MCA) {
634                         if (head->sub_block_index >= AMDGPU_RAS_MCA_BLOCK__LAST)
635                                 return NULL;
636
637                         obj = &con->objs[AMDGPU_RAS_BLOCK__LAST + head->sub_block_index];
638                 } else
639                         obj = &con->objs[head->block];
640
641                 if (alive_obj(obj))
642                         return obj;
643         } else {
644                 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT + AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
645                         obj = &con->objs[i];
646                         if (alive_obj(obj))
647                                 return obj;
648                 }
649         }
650
651         return NULL;
652 }
653 /* obj end */
654
655 /* feature ctl begin */
656 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
657                                          struct ras_common_if *head)
658 {
659         return adev->ras_hw_enabled & BIT(head->block);
660 }
661
662 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
663                 struct ras_common_if *head)
664 {
665         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
666
667         return con->features & BIT(head->block);
668 }
669
670 /*
671  * if obj is not created, then create one.
672  * set feature enable flag.
673  */
674 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
675                 struct ras_common_if *head, int enable)
676 {
677         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
678         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
679
680         /* If hardware does not support ras, then do not create obj.
681          * But if hardware support ras, we can create the obj.
682          * Ras framework checks con->hw_supported to see if it need do
683          * corresponding initialization.
684          * IP checks con->support to see if it need disable ras.
685          */
686         if (!amdgpu_ras_is_feature_allowed(adev, head))
687                 return 0;
688
689         if (enable) {
690                 if (!obj) {
691                         obj = amdgpu_ras_create_obj(adev, head);
692                         if (!obj)
693                                 return -EINVAL;
694                 } else {
695                         /* In case we create obj somewhere else */
696                         get_obj(obj);
697                 }
698                 con->features |= BIT(head->block);
699         } else {
700                 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
701                         con->features &= ~BIT(head->block);
702                         put_obj(obj);
703                 }
704         }
705
706         return 0;
707 }
708
709 /* wrapper of psp_ras_enable_features */
710 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
711                 struct ras_common_if *head, bool enable)
712 {
713         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
714         union ta_ras_cmd_input *info;
715         int ret;
716
717         if (!con)
718                 return -EINVAL;
719
720         info = kzalloc(sizeof(union ta_ras_cmd_input), GFP_KERNEL);
721         if (!info)
722                 return -ENOMEM;
723
724         if (!enable) {
725                 info->disable_features = (struct ta_ras_disable_features_input) {
726                         .block_id =  amdgpu_ras_block_to_ta(head->block),
727                         .error_type = amdgpu_ras_error_to_ta(head->type),
728                 };
729         } else {
730                 info->enable_features = (struct ta_ras_enable_features_input) {
731                         .block_id =  amdgpu_ras_block_to_ta(head->block),
732                         .error_type = amdgpu_ras_error_to_ta(head->type),
733                 };
734         }
735
736         /* Do not enable if it is not allowed. */
737         WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
738
739         /* Only enable ras feature operation handle on host side */
740         if (!amdgpu_sriov_vf(adev) &&
741                 !amdgpu_ras_intr_triggered()) {
742                 ret = psp_ras_enable_features(&adev->psp, info, enable);
743                 if (ret) {
744                         dev_err(adev->dev, "ras %s %s failed poison:%d ret:%d\n",
745                                 enable ? "enable":"disable",
746                                 get_ras_block_str(head),
747                                 amdgpu_ras_is_poison_mode_supported(adev), ret);
748                         goto out;
749                 }
750         }
751
752         /* setup the obj */
753         __amdgpu_ras_feature_enable(adev, head, enable);
754         ret = 0;
755 out:
756         kfree(info);
757         return ret;
758 }
759
760 /* Only used in device probe stage and called only once. */
761 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
762                 struct ras_common_if *head, bool enable)
763 {
764         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
765         int ret;
766
767         if (!con)
768                 return -EINVAL;
769
770         if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
771                 if (enable) {
772                         /* There is no harm to issue a ras TA cmd regardless of
773                          * the currecnt ras state.
774                          * If current state == target state, it will do nothing
775                          * But sometimes it requests driver to reset and repost
776                          * with error code -EAGAIN.
777                          */
778                         ret = amdgpu_ras_feature_enable(adev, head, 1);
779                         /* With old ras TA, we might fail to enable ras.
780                          * Log it and just setup the object.
781                          * TODO need remove this WA in the future.
782                          */
783                         if (ret == -EINVAL) {
784                                 ret = __amdgpu_ras_feature_enable(adev, head, 1);
785                                 if (!ret)
786                                         dev_info(adev->dev,
787                                                 "RAS INFO: %s setup object\n",
788                                                 get_ras_block_str(head));
789                         }
790                 } else {
791                         /* setup the object then issue a ras TA disable cmd.*/
792                         ret = __amdgpu_ras_feature_enable(adev, head, 1);
793                         if (ret)
794                                 return ret;
795
796                         /* gfx block ras dsiable cmd must send to ras-ta */
797                         if (head->block == AMDGPU_RAS_BLOCK__GFX)
798                                 con->features |= BIT(head->block);
799
800                         ret = amdgpu_ras_feature_enable(adev, head, 0);
801
802                         /* clean gfx block ras features flag */
803                         if (adev->ras_enabled && head->block == AMDGPU_RAS_BLOCK__GFX)
804                                 con->features &= ~BIT(head->block);
805                 }
806         } else
807                 ret = amdgpu_ras_feature_enable(adev, head, enable);
808
809         return ret;
810 }
811
812 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
813                 bool bypass)
814 {
815         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
816         struct ras_manager *obj, *tmp;
817
818         list_for_each_entry_safe(obj, tmp, &con->head, node) {
819                 /* bypass psp.
820                  * aka just release the obj and corresponding flags
821                  */
822                 if (bypass) {
823                         if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
824                                 break;
825                 } else {
826                         if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
827                                 break;
828                 }
829         }
830
831         return con->features;
832 }
833
834 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
835                 bool bypass)
836 {
837         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
838         int i;
839         const enum amdgpu_ras_error_type default_ras_type = AMDGPU_RAS_ERROR__NONE;
840
841         for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
842                 struct ras_common_if head = {
843                         .block = i,
844                         .type = default_ras_type,
845                         .sub_block_index = 0,
846                 };
847
848                 if (i == AMDGPU_RAS_BLOCK__MCA)
849                         continue;
850
851                 if (bypass) {
852                         /*
853                          * bypass psp. vbios enable ras for us.
854                          * so just create the obj
855                          */
856                         if (__amdgpu_ras_feature_enable(adev, &head, 1))
857                                 break;
858                 } else {
859                         if (amdgpu_ras_feature_enable(adev, &head, 1))
860                                 break;
861                 }
862         }
863
864         for (i = 0; i < AMDGPU_RAS_MCA_BLOCK_COUNT; i++) {
865                 struct ras_common_if head = {
866                         .block = AMDGPU_RAS_BLOCK__MCA,
867                         .type = default_ras_type,
868                         .sub_block_index = i,
869                 };
870
871                 if (bypass) {
872                         /*
873                          * bypass psp. vbios enable ras for us.
874                          * so just create the obj
875                          */
876                         if (__amdgpu_ras_feature_enable(adev, &head, 1))
877                                 break;
878                 } else {
879                         if (amdgpu_ras_feature_enable(adev, &head, 1))
880                                 break;
881                 }
882         }
883
884         return con->features;
885 }
886 /* feature ctl end */
887
888 static int amdgpu_ras_block_match_default(struct amdgpu_ras_block_object *block_obj,
889                 enum amdgpu_ras_block block)
890 {
891         if (!block_obj)
892                 return -EINVAL;
893
894         if (block_obj->ras_comm.block == block)
895                 return 0;
896
897         return -EINVAL;
898 }
899
900 static struct amdgpu_ras_block_object *amdgpu_ras_get_ras_block(struct amdgpu_device *adev,
901                                         enum amdgpu_ras_block block, uint32_t sub_block_index)
902 {
903         struct amdgpu_ras_block_list *node, *tmp;
904         struct amdgpu_ras_block_object *obj;
905
906         if (block >= AMDGPU_RAS_BLOCK__LAST)
907                 return NULL;
908
909         if (!amdgpu_ras_is_supported(adev, block))
910                 return NULL;
911
912         list_for_each_entry_safe(node, tmp, &adev->ras_list, node) {
913                 if (!node->ras_obj) {
914                         dev_warn(adev->dev, "Warning: abnormal ras list node.\n");
915                         continue;
916                 }
917
918                 obj = node->ras_obj;
919                 if (obj->ras_block_match) {
920                         if (obj->ras_block_match(obj, block, sub_block_index) == 0)
921                                 return obj;
922                 } else {
923                         if (amdgpu_ras_block_match_default(obj, block) == 0)
924                                 return obj;
925                 }
926         }
927
928         return NULL;
929 }
930
931 static void amdgpu_ras_get_ecc_info(struct amdgpu_device *adev, struct ras_err_data *err_data)
932 {
933         struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
934         int ret = 0;
935
936         /*
937          * choosing right query method according to
938          * whether smu support query error information
939          */
940         ret = amdgpu_dpm_get_ecc_info(adev, (void *)&(ras->umc_ecc));
941         if (ret == -EOPNOTSUPP) {
942                 if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
943                         adev->umc.ras->ras_block.hw_ops->query_ras_error_count)
944                         adev->umc.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data);
945
946                 /* umc query_ras_error_address is also responsible for clearing
947                  * error status
948                  */
949                 if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
950                     adev->umc.ras->ras_block.hw_ops->query_ras_error_address)
951                         adev->umc.ras->ras_block.hw_ops->query_ras_error_address(adev, err_data);
952         } else if (!ret) {
953                 if (adev->umc.ras &&
954                         adev->umc.ras->ecc_info_query_ras_error_count)
955                         adev->umc.ras->ecc_info_query_ras_error_count(adev, err_data);
956
957                 if (adev->umc.ras &&
958                         adev->umc.ras->ecc_info_query_ras_error_address)
959                         adev->umc.ras->ecc_info_query_ras_error_address(adev, err_data);
960         }
961 }
962
963 /* query/inject/cure begin */
964 int amdgpu_ras_query_error_status(struct amdgpu_device *adev,
965                                   struct ras_query_if *info)
966 {
967         struct amdgpu_ras_block_object *block_obj = NULL;
968         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
969         struct ras_err_data err_data = {0, 0, 0, NULL};
970
971         if (!obj)
972                 return -EINVAL;
973
974         if (info->head.block == AMDGPU_RAS_BLOCK__UMC) {
975                 amdgpu_ras_get_ecc_info(adev, &err_data);
976         } else {
977                 block_obj = amdgpu_ras_get_ras_block(adev, info->head.block, 0);
978                 if (!block_obj || !block_obj->hw_ops)   {
979                         dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
980                                      get_ras_block_str(&info->head));
981                         return -EINVAL;
982                 }
983
984                 if (block_obj->hw_ops->query_ras_error_count)
985                         block_obj->hw_ops->query_ras_error_count(adev, &err_data);
986
987                 if ((info->head.block == AMDGPU_RAS_BLOCK__SDMA) ||
988                     (info->head.block == AMDGPU_RAS_BLOCK__GFX) ||
989                     (info->head.block == AMDGPU_RAS_BLOCK__MMHUB)) {
990                                 if (block_obj->hw_ops->query_ras_error_status)
991                                         block_obj->hw_ops->query_ras_error_status(adev);
992                         }
993         }
994
995         obj->err_data.ue_count += err_data.ue_count;
996         obj->err_data.ce_count += err_data.ce_count;
997
998         info->ue_count = obj->err_data.ue_count;
999         info->ce_count = obj->err_data.ce_count;
1000
1001         if (err_data.ce_count) {
1002                 if (adev->smuio.funcs &&
1003                     adev->smuio.funcs->get_socket_id &&
1004                     adev->smuio.funcs->get_die_id) {
1005                         dev_info(adev->dev, "socket: %d, die: %d "
1006                                         "%ld correctable hardware errors "
1007                                         "detected in %s block, no user "
1008                                         "action is needed.\n",
1009                                         adev->smuio.funcs->get_socket_id(adev),
1010                                         adev->smuio.funcs->get_die_id(adev),
1011                                         obj->err_data.ce_count,
1012                                         get_ras_block_str(&info->head));
1013                 } else {
1014                         dev_info(adev->dev, "%ld correctable hardware errors "
1015                                         "detected in %s block, no user "
1016                                         "action is needed.\n",
1017                                         obj->err_data.ce_count,
1018                                         get_ras_block_str(&info->head));
1019                 }
1020         }
1021         if (err_data.ue_count) {
1022                 if (adev->smuio.funcs &&
1023                     adev->smuio.funcs->get_socket_id &&
1024                     adev->smuio.funcs->get_die_id) {
1025                         dev_info(adev->dev, "socket: %d, die: %d "
1026                                         "%ld uncorrectable hardware errors "
1027                                         "detected in %s block\n",
1028                                         adev->smuio.funcs->get_socket_id(adev),
1029                                         adev->smuio.funcs->get_die_id(adev),
1030                                         obj->err_data.ue_count,
1031                                         get_ras_block_str(&info->head));
1032                 } else {
1033                         dev_info(adev->dev, "%ld uncorrectable hardware errors "
1034                                         "detected in %s block\n",
1035                                         obj->err_data.ue_count,
1036                                         get_ras_block_str(&info->head));
1037                 }
1038         }
1039
1040         return 0;
1041 }
1042
1043 int amdgpu_ras_reset_error_status(struct amdgpu_device *adev,
1044                 enum amdgpu_ras_block block)
1045 {
1046         struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev, block, 0);
1047
1048         if (!amdgpu_ras_is_supported(adev, block))
1049                 return -EINVAL;
1050
1051         if (!block_obj || !block_obj->hw_ops)   {
1052                 dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1053                              ras_block_str(block));
1054                 return -EINVAL;
1055         }
1056
1057         if (block_obj->hw_ops->reset_ras_error_count)
1058                 block_obj->hw_ops->reset_ras_error_count(adev);
1059
1060         if ((block == AMDGPU_RAS_BLOCK__GFX) ||
1061             (block == AMDGPU_RAS_BLOCK__MMHUB)) {
1062                 if (block_obj->hw_ops->reset_ras_error_status)
1063                         block_obj->hw_ops->reset_ras_error_status(adev);
1064         }
1065
1066         return 0;
1067 }
1068
1069 /* wrapper of psp_ras_trigger_error */
1070 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
1071                 struct ras_inject_if *info)
1072 {
1073         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1074         struct ta_ras_trigger_error_input block_info = {
1075                 .block_id =  amdgpu_ras_block_to_ta(info->head.block),
1076                 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
1077                 .sub_block_index = info->head.sub_block_index,
1078                 .address = info->address,
1079                 .value = info->value,
1080         };
1081         int ret = -EINVAL;
1082         struct amdgpu_ras_block_object *block_obj = amdgpu_ras_get_ras_block(adev,
1083                                                         info->head.block,
1084                                                         info->head.sub_block_index);
1085
1086         if (!obj)
1087                 return -EINVAL;
1088
1089         if (!block_obj || !block_obj->hw_ops)   {
1090                 dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1091                              get_ras_block_str(&info->head));
1092                 return -EINVAL;
1093         }
1094
1095         /* Calculate XGMI relative offset */
1096         if (adev->gmc.xgmi.num_physical_nodes > 1) {
1097                 block_info.address =
1098                         amdgpu_xgmi_get_relative_phy_addr(adev,
1099                                                           block_info.address);
1100         }
1101
1102         if (info->head.block == AMDGPU_RAS_BLOCK__GFX) {
1103                 if (block_obj->hw_ops->ras_error_inject)
1104                         ret = block_obj->hw_ops->ras_error_inject(adev, info);
1105         } else {
1106                 /* If defined special ras_error_inject(e.g: xgmi), implement special ras_error_inject */
1107                 if (block_obj->hw_ops->ras_error_inject)
1108                         ret = block_obj->hw_ops->ras_error_inject(adev, &block_info);
1109                 else  /*If not defined .ras_error_inject, use default ras_error_inject*/
1110                         ret = psp_ras_trigger_error(&adev->psp, &block_info);
1111         }
1112
1113         if (ret)
1114                 dev_err(adev->dev, "ras inject %s failed %d\n",
1115                         get_ras_block_str(&info->head), ret);
1116
1117         return ret;
1118 }
1119
1120 /**
1121  * amdgpu_ras_query_error_count -- Get error counts of all IPs
1122  * @adev: pointer to AMD GPU device
1123  * @ce_count: pointer to an integer to be set to the count of correctible errors.
1124  * @ue_count: pointer to an integer to be set to the count of uncorrectible
1125  * errors.
1126  *
1127  * If set, @ce_count or @ue_count, count and return the corresponding
1128  * error counts in those integer pointers. Return 0 if the device
1129  * supports RAS. Return -EOPNOTSUPP if the device doesn't support RAS.
1130  */
1131 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
1132                                  unsigned long *ce_count,
1133                                  unsigned long *ue_count)
1134 {
1135         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1136         struct ras_manager *obj;
1137         unsigned long ce, ue;
1138
1139         if (!adev->ras_enabled || !con)
1140                 return -EOPNOTSUPP;
1141
1142         /* Don't count since no reporting.
1143          */
1144         if (!ce_count && !ue_count)
1145                 return 0;
1146
1147         ce = 0;
1148         ue = 0;
1149         list_for_each_entry(obj, &con->head, node) {
1150                 struct ras_query_if info = {
1151                         .head = obj->head,
1152                 };
1153                 int res;
1154
1155                 res = amdgpu_ras_query_error_status(adev, &info);
1156                 if (res)
1157                         return res;
1158
1159                 if (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
1160                     adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4)) {
1161                         if (amdgpu_ras_reset_error_status(adev, info.head.block))
1162                                 dev_warn(adev->dev, "Failed to reset error counter and error status");
1163                 }
1164
1165                 ce += info.ce_count;
1166                 ue += info.ue_count;
1167         }
1168
1169         if (ce_count)
1170                 *ce_count = ce;
1171
1172         if (ue_count)
1173                 *ue_count = ue;
1174
1175         return 0;
1176 }
1177 /* query/inject/cure end */
1178
1179
1180 /* sysfs begin */
1181
1182 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1183                 struct ras_badpage **bps, unsigned int *count);
1184
1185 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
1186 {
1187         switch (flags) {
1188         case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
1189                 return "R";
1190         case AMDGPU_RAS_RETIRE_PAGE_PENDING:
1191                 return "P";
1192         case AMDGPU_RAS_RETIRE_PAGE_FAULT:
1193         default:
1194                 return "F";
1195         }
1196 }
1197
1198 /**
1199  * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
1200  *
1201  * It allows user to read the bad pages of vram on the gpu through
1202  * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
1203  *
1204  * It outputs multiple lines, and each line stands for one gpu page.
1205  *
1206  * The format of one line is below,
1207  * gpu pfn : gpu page size : flags
1208  *
1209  * gpu pfn and gpu page size are printed in hex format.
1210  * flags can be one of below character,
1211  *
1212  * R: reserved, this gpu page is reserved and not able to use.
1213  *
1214  * P: pending for reserve, this gpu page is marked as bad, will be reserved
1215  * in next window of page_reserve.
1216  *
1217  * F: unable to reserve. this gpu page can't be reserved due to some reasons.
1218  *
1219  * Examples:
1220  *
1221  * .. code-block:: bash
1222  *
1223  *      0x00000001 : 0x00001000 : R
1224  *      0x00000002 : 0x00001000 : P
1225  *
1226  */
1227
1228 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
1229                 struct kobject *kobj, struct bin_attribute *attr,
1230                 char *buf, loff_t ppos, size_t count)
1231 {
1232         struct amdgpu_ras *con =
1233                 container_of(attr, struct amdgpu_ras, badpages_attr);
1234         struct amdgpu_device *adev = con->adev;
1235         const unsigned int element_size =
1236                 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
1237         unsigned int start = div64_ul(ppos + element_size - 1, element_size);
1238         unsigned int end = div64_ul(ppos + count - 1, element_size);
1239         ssize_t s = 0;
1240         struct ras_badpage *bps = NULL;
1241         unsigned int bps_count = 0;
1242
1243         memset(buf, 0, count);
1244
1245         if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
1246                 return 0;
1247
1248         for (; start < end && start < bps_count; start++)
1249                 s += scnprintf(&buf[s], element_size + 1,
1250                                 "0x%08x : 0x%08x : %1s\n",
1251                                 bps[start].bp,
1252                                 bps[start].size,
1253                                 amdgpu_ras_badpage_flags_str(bps[start].flags));
1254
1255         kfree(bps);
1256
1257         return s;
1258 }
1259
1260 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
1261                 struct device_attribute *attr, char *buf)
1262 {
1263         struct amdgpu_ras *con =
1264                 container_of(attr, struct amdgpu_ras, features_attr);
1265
1266         return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
1267 }
1268
1269 static void amdgpu_ras_sysfs_remove_bad_page_node(struct amdgpu_device *adev)
1270 {
1271         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1272
1273         sysfs_remove_file_from_group(&adev->dev->kobj,
1274                                 &con->badpages_attr.attr,
1275                                 RAS_FS_NAME);
1276 }
1277
1278 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1279 {
1280         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1281         struct attribute *attrs[] = {
1282                 &con->features_attr.attr,
1283                 NULL
1284         };
1285         struct attribute_group group = {
1286                 .name = RAS_FS_NAME,
1287                 .attrs = attrs,
1288         };
1289
1290         sysfs_remove_group(&adev->dev->kobj, &group);
1291
1292         return 0;
1293 }
1294
1295 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1296                 struct ras_common_if *head)
1297 {
1298         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1299
1300         if (!obj || obj->attr_inuse)
1301                 return -EINVAL;
1302
1303         get_obj(obj);
1304
1305         snprintf(obj->fs_data.sysfs_name, sizeof(obj->fs_data.sysfs_name),
1306                 "%s_err_count", head->name);
1307
1308         obj->sysfs_attr = (struct device_attribute){
1309                 .attr = {
1310                         .name = obj->fs_data.sysfs_name,
1311                         .mode = S_IRUGO,
1312                 },
1313                         .show = amdgpu_ras_sysfs_read,
1314         };
1315         sysfs_attr_init(&obj->sysfs_attr.attr);
1316
1317         if (sysfs_add_file_to_group(&adev->dev->kobj,
1318                                 &obj->sysfs_attr.attr,
1319                                 RAS_FS_NAME)) {
1320                 put_obj(obj);
1321                 return -EINVAL;
1322         }
1323
1324         obj->attr_inuse = 1;
1325
1326         return 0;
1327 }
1328
1329 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1330                 struct ras_common_if *head)
1331 {
1332         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1333
1334         if (!obj || !obj->attr_inuse)
1335                 return -EINVAL;
1336
1337         sysfs_remove_file_from_group(&adev->dev->kobj,
1338                                 &obj->sysfs_attr.attr,
1339                                 RAS_FS_NAME);
1340         obj->attr_inuse = 0;
1341         put_obj(obj);
1342
1343         return 0;
1344 }
1345
1346 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1347 {
1348         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1349         struct ras_manager *obj, *tmp;
1350
1351         list_for_each_entry_safe(obj, tmp, &con->head, node) {
1352                 amdgpu_ras_sysfs_remove(adev, &obj->head);
1353         }
1354
1355         if (amdgpu_bad_page_threshold != 0)
1356                 amdgpu_ras_sysfs_remove_bad_page_node(adev);
1357
1358         amdgpu_ras_sysfs_remove_feature_node(adev);
1359
1360         return 0;
1361 }
1362 /* sysfs end */
1363
1364 /**
1365  * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1366  *
1367  * Normally when there is an uncorrectable error, the driver will reset
1368  * the GPU to recover.  However, in the event of an unrecoverable error,
1369  * the driver provides an interface to reboot the system automatically
1370  * in that event.
1371  *
1372  * The following file in debugfs provides that interface:
1373  * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1374  *
1375  * Usage:
1376  *
1377  * .. code-block:: bash
1378  *
1379  *      echo true > .../ras/auto_reboot
1380  *
1381  */
1382 /* debugfs begin */
1383 static struct dentry *amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1384 {
1385         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1386         struct drm_minor  *minor = adev_to_drm(adev)->primary;
1387         struct dentry     *dir;
1388
1389         dir = debugfs_create_dir(RAS_FS_NAME, minor->debugfs_root);
1390         debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, dir, adev,
1391                             &amdgpu_ras_debugfs_ctrl_ops);
1392         debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, dir, adev,
1393                             &amdgpu_ras_debugfs_eeprom_ops);
1394         debugfs_create_u32("bad_page_cnt_threshold", 0444, dir,
1395                            &con->bad_page_cnt_threshold);
1396         debugfs_create_x32("ras_hw_enabled", 0444, dir, &adev->ras_hw_enabled);
1397         debugfs_create_x32("ras_enabled", 0444, dir, &adev->ras_enabled);
1398         debugfs_create_file("ras_eeprom_size", S_IRUGO, dir, adev,
1399                             &amdgpu_ras_debugfs_eeprom_size_ops);
1400         con->de_ras_eeprom_table = debugfs_create_file("ras_eeprom_table",
1401                                                        S_IRUGO, dir, adev,
1402                                                        &amdgpu_ras_debugfs_eeprom_table_ops);
1403         amdgpu_ras_debugfs_set_ret_size(&con->eeprom_control);
1404
1405         /*
1406          * After one uncorrectable error happens, usually GPU recovery will
1407          * be scheduled. But due to the known problem in GPU recovery failing
1408          * to bring GPU back, below interface provides one direct way to
1409          * user to reboot system automatically in such case within
1410          * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1411          * will never be called.
1412          */
1413         debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, dir, &con->reboot);
1414
1415         /*
1416          * User could set this not to clean up hardware's error count register
1417          * of RAS IPs during ras recovery.
1418          */
1419         debugfs_create_bool("disable_ras_err_cnt_harvest", 0644, dir,
1420                             &con->disable_ras_err_cnt_harvest);
1421         return dir;
1422 }
1423
1424 static void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1425                                       struct ras_fs_if *head,
1426                                       struct dentry *dir)
1427 {
1428         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1429
1430         if (!obj || !dir)
1431                 return;
1432
1433         get_obj(obj);
1434
1435         memcpy(obj->fs_data.debugfs_name,
1436                         head->debugfs_name,
1437                         sizeof(obj->fs_data.debugfs_name));
1438
1439         debugfs_create_file(obj->fs_data.debugfs_name, S_IWUGO | S_IRUGO, dir,
1440                             obj, &amdgpu_ras_debugfs_ops);
1441 }
1442
1443 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1444 {
1445         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1446         struct dentry *dir;
1447         struct ras_manager *obj;
1448         struct ras_fs_if fs_info;
1449
1450         /*
1451          * it won't be called in resume path, no need to check
1452          * suspend and gpu reset status
1453          */
1454         if (!IS_ENABLED(CONFIG_DEBUG_FS) || !con)
1455                 return;
1456
1457         dir = amdgpu_ras_debugfs_create_ctrl_node(adev);
1458
1459         list_for_each_entry(obj, &con->head, node) {
1460                 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1461                         (obj->attr_inuse == 1)) {
1462                         sprintf(fs_info.debugfs_name, "%s_err_inject",
1463                                         get_ras_block_str(&obj->head));
1464                         fs_info.head = obj->head;
1465                         amdgpu_ras_debugfs_create(adev, &fs_info, dir);
1466                 }
1467         }
1468 }
1469
1470 /* debugfs end */
1471
1472 /* ras fs */
1473 static BIN_ATTR(gpu_vram_bad_pages, S_IRUGO,
1474                 amdgpu_ras_sysfs_badpages_read, NULL, 0);
1475 static DEVICE_ATTR(features, S_IRUGO,
1476                 amdgpu_ras_sysfs_features_read, NULL);
1477 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1478 {
1479         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1480         struct attribute_group group = {
1481                 .name = RAS_FS_NAME,
1482         };
1483         struct attribute *attrs[] = {
1484                 &con->features_attr.attr,
1485                 NULL
1486         };
1487         struct bin_attribute *bin_attrs[] = {
1488                 NULL,
1489                 NULL,
1490         };
1491         int r;
1492
1493         /* add features entry */
1494         con->features_attr = dev_attr_features;
1495         group.attrs = attrs;
1496         sysfs_attr_init(attrs[0]);
1497
1498         if (amdgpu_bad_page_threshold != 0) {
1499                 /* add bad_page_features entry */
1500                 bin_attr_gpu_vram_bad_pages.private = NULL;
1501                 con->badpages_attr = bin_attr_gpu_vram_bad_pages;
1502                 bin_attrs[0] = &con->badpages_attr;
1503                 group.bin_attrs = bin_attrs;
1504                 sysfs_bin_attr_init(bin_attrs[0]);
1505         }
1506
1507         r = sysfs_create_group(&adev->dev->kobj, &group);
1508         if (r)
1509                 dev_err(adev->dev, "Failed to create RAS sysfs group!");
1510
1511         return 0;
1512 }
1513
1514 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1515 {
1516         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1517         struct ras_manager *con_obj, *ip_obj, *tmp;
1518
1519         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1520                 list_for_each_entry_safe(con_obj, tmp, &con->head, node) {
1521                         ip_obj = amdgpu_ras_find_obj(adev, &con_obj->head);
1522                         if (ip_obj)
1523                                 put_obj(ip_obj);
1524                 }
1525         }
1526
1527         amdgpu_ras_sysfs_remove_all(adev);
1528         return 0;
1529 }
1530 /* ras fs end */
1531
1532 /* ih begin */
1533
1534 /* For the hardware that cannot enable bif ring for both ras_controller_irq
1535  * and ras_err_evnet_athub_irq ih cookies, the driver has to poll status
1536  * register to check whether the interrupt is triggered or not, and properly
1537  * ack the interrupt if it is there
1538  */
1539 void amdgpu_ras_interrupt_fatal_error_handler(struct amdgpu_device *adev)
1540 {
1541         /* Fatal error events are handled on host side */
1542         if (amdgpu_sriov_vf(adev) ||
1543                 !amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__PCIE_BIF))
1544                 return;
1545
1546         if (adev->nbio.ras &&
1547             adev->nbio.ras->handle_ras_controller_intr_no_bifring)
1548                 adev->nbio.ras->handle_ras_controller_intr_no_bifring(adev);
1549
1550         if (adev->nbio.ras &&
1551             adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring)
1552                 adev->nbio.ras->handle_ras_err_event_athub_intr_no_bifring(adev);
1553 }
1554
1555 static void amdgpu_ras_interrupt_poison_consumption_handler(struct ras_manager *obj,
1556                                 struct amdgpu_iv_entry *entry)
1557 {
1558         bool poison_stat = false;
1559         struct amdgpu_device *adev = obj->adev;
1560         struct ras_err_data err_data = {0, 0, 0, NULL};
1561         struct amdgpu_ras_block_object *block_obj =
1562                 amdgpu_ras_get_ras_block(adev, obj->head.block, 0);
1563
1564         if (!block_obj || !block_obj->hw_ops)
1565                 return;
1566
1567         /* both query_poison_status and handle_poison_consumption are optional,
1568          * but at least one of them should be implemented if we need poison
1569          * consumption handler
1570          */
1571         if (block_obj->hw_ops->query_poison_status) {
1572                 poison_stat = block_obj->hw_ops->query_poison_status(adev);
1573                 if (!poison_stat) {
1574                         /* Not poison consumption interrupt, no need to handle it */
1575                         dev_info(adev->dev, "No RAS poison status in %s poison IH.\n",
1576                                         block_obj->ras_comm.name);
1577
1578                         return;
1579                 }
1580         }
1581
1582         if (!adev->gmc.xgmi.connected_to_cpu)
1583                 amdgpu_umc_poison_handler(adev, &err_data, false);
1584
1585         if (block_obj->hw_ops->handle_poison_consumption)
1586                 poison_stat = block_obj->hw_ops->handle_poison_consumption(adev);
1587
1588         /* gpu reset is fallback for failed and default cases */
1589         if (poison_stat) {
1590                 dev_info(adev->dev, "GPU reset for %s RAS poison consumption is issued!\n",
1591                                 block_obj->ras_comm.name);
1592                 amdgpu_ras_reset_gpu(adev);
1593         }
1594 }
1595
1596 static void amdgpu_ras_interrupt_poison_creation_handler(struct ras_manager *obj,
1597                                 struct amdgpu_iv_entry *entry)
1598 {
1599         dev_info(obj->adev->dev,
1600                 "Poison is created, no user action is needed.\n");
1601 }
1602
1603 static void amdgpu_ras_interrupt_umc_handler(struct ras_manager *obj,
1604                                 struct amdgpu_iv_entry *entry)
1605 {
1606         struct ras_ih_data *data = &obj->ih_data;
1607         struct ras_err_data err_data = {0, 0, 0, NULL};
1608         int ret;
1609
1610         if (!data->cb)
1611                 return;
1612
1613         /* Let IP handle its data, maybe we need get the output
1614          * from the callback to update the error type/count, etc
1615          */
1616         ret = data->cb(obj->adev, &err_data, entry);
1617         /* ue will trigger an interrupt, and in that case
1618          * we need do a reset to recovery the whole system.
1619          * But leave IP do that recovery, here we just dispatch
1620          * the error.
1621          */
1622         if (ret == AMDGPU_RAS_SUCCESS) {
1623                 /* these counts could be left as 0 if
1624                  * some blocks do not count error number
1625                  */
1626                 obj->err_data.ue_count += err_data.ue_count;
1627                 obj->err_data.ce_count += err_data.ce_count;
1628         }
1629 }
1630
1631 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1632 {
1633         struct ras_ih_data *data = &obj->ih_data;
1634         struct amdgpu_iv_entry entry;
1635
1636         while (data->rptr != data->wptr) {
1637                 rmb();
1638                 memcpy(&entry, &data->ring[data->rptr],
1639                                 data->element_size);
1640
1641                 wmb();
1642                 data->rptr = (data->aligned_element_size +
1643                                 data->rptr) % data->ring_size;
1644
1645                 if (amdgpu_ras_is_poison_mode_supported(obj->adev)) {
1646                         if (obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1647                                 amdgpu_ras_interrupt_poison_creation_handler(obj, &entry);
1648                         else
1649                                 amdgpu_ras_interrupt_poison_consumption_handler(obj, &entry);
1650                 } else {
1651                         if (obj->head.block == AMDGPU_RAS_BLOCK__UMC)
1652                                 amdgpu_ras_interrupt_umc_handler(obj, &entry);
1653                         else
1654                                 dev_warn(obj->adev->dev,
1655                                         "No RAS interrupt handler for non-UMC block with poison disabled.\n");
1656                 }
1657         }
1658 }
1659
1660 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1661 {
1662         struct ras_ih_data *data =
1663                 container_of(work, struct ras_ih_data, ih_work);
1664         struct ras_manager *obj =
1665                 container_of(data, struct ras_manager, ih_data);
1666
1667         amdgpu_ras_interrupt_handler(obj);
1668 }
1669
1670 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1671                 struct ras_dispatch_if *info)
1672 {
1673         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1674         struct ras_ih_data *data = &obj->ih_data;
1675
1676         if (!obj)
1677                 return -EINVAL;
1678
1679         if (data->inuse == 0)
1680                 return 0;
1681
1682         /* Might be overflow... */
1683         memcpy(&data->ring[data->wptr], info->entry,
1684                         data->element_size);
1685
1686         wmb();
1687         data->wptr = (data->aligned_element_size +
1688                         data->wptr) % data->ring_size;
1689
1690         schedule_work(&data->ih_work);
1691
1692         return 0;
1693 }
1694
1695 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1696                 struct ras_common_if *head)
1697 {
1698         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1699         struct ras_ih_data *data;
1700
1701         if (!obj)
1702                 return -EINVAL;
1703
1704         data = &obj->ih_data;
1705         if (data->inuse == 0)
1706                 return 0;
1707
1708         cancel_work_sync(&data->ih_work);
1709
1710         kfree(data->ring);
1711         memset(data, 0, sizeof(*data));
1712         put_obj(obj);
1713
1714         return 0;
1715 }
1716
1717 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1718                 struct ras_common_if *head)
1719 {
1720         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1721         struct ras_ih_data *data;
1722         struct amdgpu_ras_block_object *ras_obj;
1723
1724         if (!obj) {
1725                 /* in case we registe the IH before enable ras feature */
1726                 obj = amdgpu_ras_create_obj(adev, head);
1727                 if (!obj)
1728                         return -EINVAL;
1729         } else
1730                 get_obj(obj);
1731
1732         ras_obj = container_of(head, struct amdgpu_ras_block_object, ras_comm);
1733
1734         data = &obj->ih_data;
1735         /* add the callback.etc */
1736         *data = (struct ras_ih_data) {
1737                 .inuse = 0,
1738                 .cb = ras_obj->ras_cb,
1739                 .element_size = sizeof(struct amdgpu_iv_entry),
1740                 .rptr = 0,
1741                 .wptr = 0,
1742         };
1743
1744         INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1745
1746         data->aligned_element_size = ALIGN(data->element_size, 8);
1747         /* the ring can store 64 iv entries. */
1748         data->ring_size = 64 * data->aligned_element_size;
1749         data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1750         if (!data->ring) {
1751                 put_obj(obj);
1752                 return -ENOMEM;
1753         }
1754
1755         /* IH is ready */
1756         data->inuse = 1;
1757
1758         return 0;
1759 }
1760
1761 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1762 {
1763         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1764         struct ras_manager *obj, *tmp;
1765
1766         list_for_each_entry_safe(obj, tmp, &con->head, node) {
1767                 amdgpu_ras_interrupt_remove_handler(adev, &obj->head);
1768         }
1769
1770         return 0;
1771 }
1772 /* ih end */
1773
1774 /* traversal all IPs except NBIO to query error counter */
1775 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1776 {
1777         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1778         struct ras_manager *obj;
1779
1780         if (!adev->ras_enabled || !con)
1781                 return;
1782
1783         list_for_each_entry(obj, &con->head, node) {
1784                 struct ras_query_if info = {
1785                         .head = obj->head,
1786                 };
1787
1788                 /*
1789                  * PCIE_BIF IP has one different isr by ras controller
1790                  * interrupt, the specific ras counter query will be
1791                  * done in that isr. So skip such block from common
1792                  * sync flood interrupt isr calling.
1793                  */
1794                 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1795                         continue;
1796
1797                 /*
1798                  * this is a workaround for aldebaran, skip send msg to
1799                  * smu to get ecc_info table due to smu handle get ecc
1800                  * info table failed temporarily.
1801                  * should be removed until smu fix handle ecc_info table.
1802                  */
1803                 if ((info.head.block == AMDGPU_RAS_BLOCK__UMC) &&
1804                         (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(13, 0, 2)))
1805                         continue;
1806
1807                 amdgpu_ras_query_error_status(adev, &info);
1808
1809                 if (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 2) &&
1810                     adev->ip_versions[MP0_HWIP][0] != IP_VERSION(11, 0, 4)) {
1811                         if (amdgpu_ras_reset_error_status(adev, info.head.block))
1812                                 dev_warn(adev->dev, "Failed to reset error counter and error status");
1813                 }
1814         }
1815 }
1816
1817 /* Parse RdRspStatus and WrRspStatus */
1818 static void amdgpu_ras_error_status_query(struct amdgpu_device *adev,
1819                                           struct ras_query_if *info)
1820 {
1821         struct amdgpu_ras_block_object *block_obj;
1822         /*
1823          * Only two block need to query read/write
1824          * RspStatus at current state
1825          */
1826         if ((info->head.block != AMDGPU_RAS_BLOCK__GFX) &&
1827                 (info->head.block != AMDGPU_RAS_BLOCK__MMHUB))
1828                 return;
1829
1830         block_obj = amdgpu_ras_get_ras_block(adev,
1831                                         info->head.block,
1832                                         info->head.sub_block_index);
1833
1834         if (!block_obj || !block_obj->hw_ops) {
1835                 dev_dbg_once(adev->dev, "%s doesn't config RAS function\n",
1836                              get_ras_block_str(&info->head));
1837                 return;
1838         }
1839
1840         if (block_obj->hw_ops->query_ras_error_status)
1841                 block_obj->hw_ops->query_ras_error_status(adev);
1842
1843 }
1844
1845 static void amdgpu_ras_query_err_status(struct amdgpu_device *adev)
1846 {
1847         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1848         struct ras_manager *obj;
1849
1850         if (!adev->ras_enabled || !con)
1851                 return;
1852
1853         list_for_each_entry(obj, &con->head, node) {
1854                 struct ras_query_if info = {
1855                         .head = obj->head,
1856                 };
1857
1858                 amdgpu_ras_error_status_query(adev, &info);
1859         }
1860 }
1861
1862 /* recovery begin */
1863
1864 /* return 0 on success.
1865  * caller need free bps.
1866  */
1867 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1868                 struct ras_badpage **bps, unsigned int *count)
1869 {
1870         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1871         struct ras_err_handler_data *data;
1872         int i = 0;
1873         int ret = 0, status;
1874
1875         if (!con || !con->eh_data || !bps || !count)
1876                 return -EINVAL;
1877
1878         mutex_lock(&con->recovery_lock);
1879         data = con->eh_data;
1880         if (!data || data->count == 0) {
1881                 *bps = NULL;
1882                 ret = -EINVAL;
1883                 goto out;
1884         }
1885
1886         *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1887         if (!*bps) {
1888                 ret = -ENOMEM;
1889                 goto out;
1890         }
1891
1892         for (; i < data->count; i++) {
1893                 (*bps)[i] = (struct ras_badpage){
1894                         .bp = data->bps[i].retired_page,
1895                         .size = AMDGPU_GPU_PAGE_SIZE,
1896                         .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1897                 };
1898                 status = amdgpu_vram_mgr_query_page_status(&adev->mman.vram_mgr,
1899                                 data->bps[i].retired_page);
1900                 if (status == -EBUSY)
1901                         (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1902                 else if (status == -ENOENT)
1903                         (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1904         }
1905
1906         *count = data->count;
1907 out:
1908         mutex_unlock(&con->recovery_lock);
1909         return ret;
1910 }
1911
1912 static void amdgpu_ras_do_recovery(struct work_struct *work)
1913 {
1914         struct amdgpu_ras *ras =
1915                 container_of(work, struct amdgpu_ras, recovery_work);
1916         struct amdgpu_device *remote_adev = NULL;
1917         struct amdgpu_device *adev = ras->adev;
1918         struct list_head device_list, *device_list_handle =  NULL;
1919
1920         if (!ras->disable_ras_err_cnt_harvest) {
1921                 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
1922
1923                 /* Build list of devices to query RAS related errors */
1924                 if  (hive && adev->gmc.xgmi.num_physical_nodes > 1) {
1925                         device_list_handle = &hive->device_list;
1926                 } else {
1927                         INIT_LIST_HEAD(&device_list);
1928                         list_add_tail(&adev->gmc.xgmi.head, &device_list);
1929                         device_list_handle = &device_list;
1930                 }
1931
1932                 list_for_each_entry(remote_adev,
1933                                 device_list_handle, gmc.xgmi.head) {
1934                         amdgpu_ras_query_err_status(remote_adev);
1935                         amdgpu_ras_log_on_err_counter(remote_adev);
1936                 }
1937
1938                 amdgpu_put_xgmi_hive(hive);
1939         }
1940
1941         if (amdgpu_device_should_recover_gpu(ras->adev))
1942                 amdgpu_device_gpu_recover(ras->adev, NULL);
1943         atomic_set(&ras->in_recovery, 0);
1944 }
1945
1946 /* alloc/realloc bps array */
1947 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1948                 struct ras_err_handler_data *data, int pages)
1949 {
1950         unsigned int old_space = data->count + data->space_left;
1951         unsigned int new_space = old_space + pages;
1952         unsigned int align_space = ALIGN(new_space, 512);
1953         void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1954
1955         if (!bps) {
1956                 return -ENOMEM;
1957         }
1958
1959         if (data->bps) {
1960                 memcpy(bps, data->bps,
1961                                 data->count * sizeof(*data->bps));
1962                 kfree(data->bps);
1963         }
1964
1965         data->bps = bps;
1966         data->space_left += align_space - old_space;
1967         return 0;
1968 }
1969
1970 /* it deal with vram only. */
1971 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1972                 struct eeprom_table_record *bps, int pages)
1973 {
1974         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1975         struct ras_err_handler_data *data;
1976         int ret = 0;
1977         uint32_t i;
1978
1979         if (!con || !con->eh_data || !bps || pages <= 0)
1980                 return 0;
1981
1982         mutex_lock(&con->recovery_lock);
1983         data = con->eh_data;
1984         if (!data)
1985                 goto out;
1986
1987         for (i = 0; i < pages; i++) {
1988                 if (amdgpu_ras_check_bad_page_unlock(con,
1989                         bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT))
1990                         continue;
1991
1992                 if (!data->space_left &&
1993                         amdgpu_ras_realloc_eh_data_space(adev, data, 256)) {
1994                         ret = -ENOMEM;
1995                         goto out;
1996                 }
1997
1998                 amdgpu_vram_mgr_reserve_range(&adev->mman.vram_mgr,
1999                         bps[i].retired_page << AMDGPU_GPU_PAGE_SHIFT,
2000                         AMDGPU_GPU_PAGE_SIZE);
2001
2002                 memcpy(&data->bps[data->count], &bps[i], sizeof(*data->bps));
2003                 data->count++;
2004                 data->space_left--;
2005         }
2006 out:
2007         mutex_unlock(&con->recovery_lock);
2008
2009         return ret;
2010 }
2011
2012 /*
2013  * write error record array to eeprom, the function should be
2014  * protected by recovery_lock
2015  */
2016 int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
2017 {
2018         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2019         struct ras_err_handler_data *data;
2020         struct amdgpu_ras_eeprom_control *control;
2021         int save_count;
2022
2023         if (!con || !con->eh_data)
2024                 return 0;
2025
2026         mutex_lock(&con->recovery_lock);
2027         control = &con->eeprom_control;
2028         data = con->eh_data;
2029         save_count = data->count - control->ras_num_recs;
2030         mutex_unlock(&con->recovery_lock);
2031         /* only new entries are saved */
2032         if (save_count > 0) {
2033                 if (amdgpu_ras_eeprom_append(control,
2034                                              &data->bps[control->ras_num_recs],
2035                                              save_count)) {
2036                         dev_err(adev->dev, "Failed to save EEPROM table data!");
2037                         return -EIO;
2038                 }
2039
2040                 dev_info(adev->dev, "Saved %d pages to EEPROM table.\n", save_count);
2041         }
2042
2043         return 0;
2044 }
2045
2046 /*
2047  * read error record array in eeprom and reserve enough space for
2048  * storing new bad pages
2049  */
2050 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
2051 {
2052         struct amdgpu_ras_eeprom_control *control =
2053                 &adev->psp.ras_context.ras->eeprom_control;
2054         struct eeprom_table_record *bps;
2055         int ret;
2056
2057         /* no bad page record, skip eeprom access */
2058         if (control->ras_num_recs == 0 || amdgpu_bad_page_threshold == 0)
2059                 return 0;
2060
2061         bps = kcalloc(control->ras_num_recs, sizeof(*bps), GFP_KERNEL);
2062         if (!bps)
2063                 return -ENOMEM;
2064
2065         ret = amdgpu_ras_eeprom_read(control, bps, control->ras_num_recs);
2066         if (ret)
2067                 dev_err(adev->dev, "Failed to load EEPROM table records!");
2068         else
2069                 ret = amdgpu_ras_add_bad_pages(adev, bps, control->ras_num_recs);
2070
2071         kfree(bps);
2072         return ret;
2073 }
2074
2075 static bool amdgpu_ras_check_bad_page_unlock(struct amdgpu_ras *con,
2076                                 uint64_t addr)
2077 {
2078         struct ras_err_handler_data *data = con->eh_data;
2079         int i;
2080
2081         addr >>= AMDGPU_GPU_PAGE_SHIFT;
2082         for (i = 0; i < data->count; i++)
2083                 if (addr == data->bps[i].retired_page)
2084                         return true;
2085
2086         return false;
2087 }
2088
2089 /*
2090  * check if an address belongs to bad page
2091  *
2092  * Note: this check is only for umc block
2093  */
2094 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
2095                                 uint64_t addr)
2096 {
2097         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2098         bool ret = false;
2099
2100         if (!con || !con->eh_data)
2101                 return ret;
2102
2103         mutex_lock(&con->recovery_lock);
2104         ret = amdgpu_ras_check_bad_page_unlock(con, addr);
2105         mutex_unlock(&con->recovery_lock);
2106         return ret;
2107 }
2108
2109 static void amdgpu_ras_validate_threshold(struct amdgpu_device *adev,
2110                                           uint32_t max_count)
2111 {
2112         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2113
2114         /*
2115          * Justification of value bad_page_cnt_threshold in ras structure
2116          *
2117          * Generally, -1 <= amdgpu_bad_page_threshold <= max record length
2118          * in eeprom, and introduce two scenarios accordingly.
2119          *
2120          * Bad page retirement enablement:
2121          *    - If amdgpu_bad_page_threshold = -1,
2122          *      bad_page_cnt_threshold = typical value by formula.
2123          *
2124          *    - When the value from user is 0 < amdgpu_bad_page_threshold <
2125          *      max record length in eeprom, use it directly.
2126          *
2127          * Bad page retirement disablement:
2128          *    - If amdgpu_bad_page_threshold = 0, bad page retirement
2129          *      functionality is disabled, and bad_page_cnt_threshold will
2130          *      take no effect.
2131          */
2132
2133         if (amdgpu_bad_page_threshold < 0) {
2134                 u64 val = adev->gmc.mc_vram_size;
2135
2136                 do_div(val, RAS_BAD_PAGE_COVER);
2137                 con->bad_page_cnt_threshold = min(lower_32_bits(val),
2138                                                   max_count);
2139         } else {
2140                 con->bad_page_cnt_threshold = min_t(int, max_count,
2141                                                     amdgpu_bad_page_threshold);
2142         }
2143 }
2144
2145 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
2146 {
2147         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2148         struct ras_err_handler_data **data;
2149         u32  max_eeprom_records_count = 0;
2150         bool exc_err_limit = false;
2151         int ret;
2152
2153         if (!con)
2154                 return 0;
2155
2156         /* Allow access to RAS EEPROM via debugfs, when the ASIC
2157          * supports RAS and debugfs is enabled, but when
2158          * adev->ras_enabled is unset, i.e. when "ras_enable"
2159          * module parameter is set to 0.
2160          */
2161         con->adev = adev;
2162
2163         if (!adev->ras_enabled)
2164                 return 0;
2165
2166         data = &con->eh_data;
2167         *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
2168         if (!*data) {
2169                 ret = -ENOMEM;
2170                 goto out;
2171         }
2172
2173         mutex_init(&con->recovery_lock);
2174         INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
2175         atomic_set(&con->in_recovery, 0);
2176         con->eeprom_control.bad_channel_bitmap = 0;
2177
2178         max_eeprom_records_count = amdgpu_ras_eeprom_max_record_count();
2179         amdgpu_ras_validate_threshold(adev, max_eeprom_records_count);
2180
2181         /* Todo: During test the SMU might fail to read the eeprom through I2C
2182          * when the GPU is pending on XGMI reset during probe time
2183          * (Mostly after second bus reset), skip it now
2184          */
2185         if (adev->gmc.xgmi.pending_reset)
2186                 return 0;
2187         ret = amdgpu_ras_eeprom_init(&con->eeprom_control, &exc_err_limit);
2188         /*
2189          * This calling fails when exc_err_limit is true or
2190          * ret != 0.
2191          */
2192         if (exc_err_limit || ret)
2193                 goto free;
2194
2195         if (con->eeprom_control.ras_num_recs) {
2196                 ret = amdgpu_ras_load_bad_pages(adev);
2197                 if (ret)
2198                         goto free;
2199
2200                 amdgpu_dpm_send_hbm_bad_pages_num(adev, con->eeprom_control.ras_num_recs);
2201
2202                 if (con->update_channel_flag == true) {
2203                         amdgpu_dpm_send_hbm_bad_channel_flag(adev, con->eeprom_control.bad_channel_bitmap);
2204                         con->update_channel_flag = false;
2205                 }
2206         }
2207
2208 #ifdef CONFIG_X86_MCE_AMD
2209         if ((adev->asic_type == CHIP_ALDEBARAN) &&
2210             (adev->gmc.xgmi.connected_to_cpu))
2211                 amdgpu_register_bad_pages_mca_notifier(adev);
2212 #endif
2213         return 0;
2214
2215 free:
2216         kfree((*data)->bps);
2217         kfree(*data);
2218         con->eh_data = NULL;
2219 out:
2220         dev_warn(adev->dev, "Failed to initialize ras recovery! (%d)\n", ret);
2221
2222         /*
2223          * Except error threshold exceeding case, other failure cases in this
2224          * function would not fail amdgpu driver init.
2225          */
2226         if (!exc_err_limit)
2227                 ret = 0;
2228         else
2229                 ret = -EINVAL;
2230
2231         return ret;
2232 }
2233
2234 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
2235 {
2236         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2237         struct ras_err_handler_data *data = con->eh_data;
2238
2239         /* recovery_init failed to init it, fini is useless */
2240         if (!data)
2241                 return 0;
2242
2243         cancel_work_sync(&con->recovery_work);
2244
2245         mutex_lock(&con->recovery_lock);
2246         con->eh_data = NULL;
2247         kfree(data->bps);
2248         kfree(data);
2249         mutex_unlock(&con->recovery_lock);
2250
2251         return 0;
2252 }
2253 /* recovery end */
2254
2255 static bool amdgpu_ras_asic_supported(struct amdgpu_device *adev)
2256 {
2257         return adev->asic_type == CHIP_VEGA10 ||
2258                 adev->asic_type == CHIP_VEGA20 ||
2259                 adev->asic_type == CHIP_ARCTURUS ||
2260                 adev->asic_type == CHIP_ALDEBARAN ||
2261                 adev->asic_type == CHIP_SIENNA_CICHLID;
2262 }
2263
2264 /*
2265  * this is workaround for vega20 workstation sku,
2266  * force enable gfx ras, ignore vbios gfx ras flag
2267  * due to GC EDC can not write
2268  */
2269 static void amdgpu_ras_get_quirks(struct amdgpu_device *adev)
2270 {
2271         struct atom_context *ctx = adev->mode_info.atom_context;
2272
2273         if (!ctx)
2274                 return;
2275
2276         if (strnstr(ctx->vbios_version, "D16406",
2277                     sizeof(ctx->vbios_version)) ||
2278                 strnstr(ctx->vbios_version, "D36002",
2279                         sizeof(ctx->vbios_version)))
2280                 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX);
2281 }
2282
2283 /*
2284  * check hardware's ras ability which will be saved in hw_supported.
2285  * if hardware does not support ras, we can skip some ras initializtion and
2286  * forbid some ras operations from IP.
2287  * if software itself, say boot parameter, limit the ras ability. We still
2288  * need allow IP do some limited operations, like disable. In such case,
2289  * we have to initialize ras as normal. but need check if operation is
2290  * allowed or not in each function.
2291  */
2292 static void amdgpu_ras_check_supported(struct amdgpu_device *adev)
2293 {
2294         adev->ras_hw_enabled = adev->ras_enabled = 0;
2295
2296         if (!adev->is_atom_fw ||
2297             !amdgpu_ras_asic_supported(adev))
2298                 return;
2299
2300         /* If driver run on sriov guest side, only enable ras for aldebaran */
2301         if (amdgpu_sriov_vf(adev) &&
2302                 adev->ip_versions[MP1_HWIP][0] != IP_VERSION(13, 0, 2))
2303                 return;
2304
2305         if (!adev->gmc.xgmi.connected_to_cpu) {
2306                 if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
2307                         dev_info(adev->dev, "MEM ECC is active.\n");
2308                         adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__UMC |
2309                                                    1 << AMDGPU_RAS_BLOCK__DF);
2310                 } else {
2311                         dev_info(adev->dev, "MEM ECC is not presented.\n");
2312                 }
2313
2314                 if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
2315                         dev_info(adev->dev, "SRAM ECC is active.\n");
2316                         if (!amdgpu_sriov_vf(adev)) {
2317                                 adev->ras_hw_enabled |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
2318                                                             1 << AMDGPU_RAS_BLOCK__DF);
2319
2320                                 if (adev->ip_versions[VCN_HWIP][0] == IP_VERSION(2, 6, 0))
2321                                         adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__VCN |
2322                                                         1 << AMDGPU_RAS_BLOCK__JPEG);
2323                                 else
2324                                         adev->ras_hw_enabled &= ~(1 << AMDGPU_RAS_BLOCK__VCN |
2325                                                         1 << AMDGPU_RAS_BLOCK__JPEG);
2326                         } else {
2327                                 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__PCIE_BIF |
2328                                                                 1 << AMDGPU_RAS_BLOCK__SDMA |
2329                                                                 1 << AMDGPU_RAS_BLOCK__GFX);
2330                         }
2331                 } else {
2332                         dev_info(adev->dev, "SRAM ECC is not presented.\n");
2333                 }
2334         } else {
2335                 /* driver only manages a few IP blocks RAS feature
2336                  * when GPU is connected cpu through XGMI */
2337                 adev->ras_hw_enabled |= (1 << AMDGPU_RAS_BLOCK__GFX |
2338                                            1 << AMDGPU_RAS_BLOCK__SDMA |
2339                                            1 << AMDGPU_RAS_BLOCK__MMHUB);
2340         }
2341
2342         amdgpu_ras_get_quirks(adev);
2343
2344         /* hw_supported needs to be aligned with RAS block mask. */
2345         adev->ras_hw_enabled &= AMDGPU_RAS_BLOCK_MASK;
2346
2347         adev->ras_enabled = amdgpu_ras_enable == 0 ? 0 :
2348                 adev->ras_hw_enabled & amdgpu_ras_mask;
2349 }
2350
2351 static void amdgpu_ras_counte_dw(struct work_struct *work)
2352 {
2353         struct amdgpu_ras *con = container_of(work, struct amdgpu_ras,
2354                                               ras_counte_delay_work.work);
2355         struct amdgpu_device *adev = con->adev;
2356         struct drm_device *dev = adev_to_drm(adev);
2357         unsigned long ce_count, ue_count;
2358         int res;
2359
2360         res = pm_runtime_get_sync(dev->dev);
2361         if (res < 0)
2362                 goto Out;
2363
2364         /* Cache new values.
2365          */
2366         if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2367                 atomic_set(&con->ras_ce_count, ce_count);
2368                 atomic_set(&con->ras_ue_count, ue_count);
2369         }
2370
2371         pm_runtime_mark_last_busy(dev->dev);
2372 Out:
2373         pm_runtime_put_autosuspend(dev->dev);
2374 }
2375
2376 int amdgpu_ras_init(struct amdgpu_device *adev)
2377 {
2378         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2379         int r;
2380         bool df_poison, umc_poison;
2381
2382         if (con)
2383                 return 0;
2384
2385         con = kmalloc(sizeof(struct amdgpu_ras) +
2386                         sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT +
2387                         sizeof(struct ras_manager) * AMDGPU_RAS_MCA_BLOCK_COUNT,
2388                         GFP_KERNEL|__GFP_ZERO);
2389         if (!con)
2390                 return -ENOMEM;
2391
2392         con->adev = adev;
2393         INIT_DELAYED_WORK(&con->ras_counte_delay_work, amdgpu_ras_counte_dw);
2394         atomic_set(&con->ras_ce_count, 0);
2395         atomic_set(&con->ras_ue_count, 0);
2396
2397         con->objs = (struct ras_manager *)(con + 1);
2398
2399         amdgpu_ras_set_context(adev, con);
2400
2401         amdgpu_ras_check_supported(adev);
2402
2403         if (!adev->ras_enabled || adev->asic_type == CHIP_VEGA10) {
2404                 /* set gfx block ras context feature for VEGA20 Gaming
2405                  * send ras disable cmd to ras ta during ras late init.
2406                  */
2407                 if (!adev->ras_enabled && adev->asic_type == CHIP_VEGA20) {
2408                         con->features |= BIT(AMDGPU_RAS_BLOCK__GFX);
2409
2410                         return 0;
2411                 }
2412
2413                 r = 0;
2414                 goto release_con;
2415         }
2416
2417         con->update_channel_flag = false;
2418         con->features = 0;
2419         INIT_LIST_HEAD(&con->head);
2420         /* Might need get this flag from vbios. */
2421         con->flags = RAS_DEFAULT_FLAGS;
2422
2423         /* initialize nbio ras function ahead of any other
2424          * ras functions so hardware fatal error interrupt
2425          * can be enabled as early as possible */
2426         switch (adev->asic_type) {
2427         case CHIP_VEGA20:
2428         case CHIP_ARCTURUS:
2429         case CHIP_ALDEBARAN:
2430                 if (!adev->gmc.xgmi.connected_to_cpu) {
2431                         adev->nbio.ras = &nbio_v7_4_ras;
2432                         amdgpu_ras_register_ras_block(adev, &adev->nbio.ras->ras_block);
2433                         adev->nbio.ras_if = &adev->nbio.ras->ras_block.ras_comm;
2434                 }
2435                 break;
2436         default:
2437                 /* nbio ras is not available */
2438                 break;
2439         }
2440
2441         if (adev->nbio.ras &&
2442             adev->nbio.ras->init_ras_controller_interrupt) {
2443                 r = adev->nbio.ras->init_ras_controller_interrupt(adev);
2444                 if (r)
2445                         goto release_con;
2446         }
2447
2448         if (adev->nbio.ras &&
2449             adev->nbio.ras->init_ras_err_event_athub_interrupt) {
2450                 r = adev->nbio.ras->init_ras_err_event_athub_interrupt(adev);
2451                 if (r)
2452                         goto release_con;
2453         }
2454
2455         /* Init poison supported flag, the default value is false */
2456         if (adev->gmc.xgmi.connected_to_cpu) {
2457                 /* enabled by default when GPU is connected to CPU */
2458                 con->poison_supported = true;
2459         }
2460         else if (adev->df.funcs &&
2461             adev->df.funcs->query_ras_poison_mode &&
2462             adev->umc.ras &&
2463             adev->umc.ras->query_ras_poison_mode) {
2464                 df_poison =
2465                         adev->df.funcs->query_ras_poison_mode(adev);
2466                 umc_poison =
2467                         adev->umc.ras->query_ras_poison_mode(adev);
2468                 /* Only poison is set in both DF and UMC, we can support it */
2469                 if (df_poison && umc_poison)
2470                         con->poison_supported = true;
2471                 else if (df_poison != umc_poison)
2472                         dev_warn(adev->dev, "Poison setting is inconsistent in DF/UMC(%d:%d)!\n",
2473                                         df_poison, umc_poison);
2474         }
2475
2476         if (amdgpu_ras_fs_init(adev)) {
2477                 r = -EINVAL;
2478                 goto release_con;
2479         }
2480
2481         dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
2482                  "hardware ability[%x] ras_mask[%x]\n",
2483                  adev->ras_hw_enabled, adev->ras_enabled);
2484
2485         return 0;
2486 release_con:
2487         amdgpu_ras_set_context(adev, NULL);
2488         kfree(con);
2489
2490         return r;
2491 }
2492
2493 int amdgpu_persistent_edc_harvesting_supported(struct amdgpu_device *adev)
2494 {
2495         if (adev->gmc.xgmi.connected_to_cpu)
2496                 return 1;
2497         return 0;
2498 }
2499
2500 static int amdgpu_persistent_edc_harvesting(struct amdgpu_device *adev,
2501                                         struct ras_common_if *ras_block)
2502 {
2503         struct ras_query_if info = {
2504                 .head = *ras_block,
2505         };
2506
2507         if (!amdgpu_persistent_edc_harvesting_supported(adev))
2508                 return 0;
2509
2510         if (amdgpu_ras_query_error_status(adev, &info) != 0)
2511                 DRM_WARN("RAS init harvest failure");
2512
2513         if (amdgpu_ras_reset_error_status(adev, ras_block->block) != 0)
2514                 DRM_WARN("RAS init harvest reset failure");
2515
2516         return 0;
2517 }
2518
2519 bool amdgpu_ras_is_poison_mode_supported(struct amdgpu_device *adev)
2520 {
2521        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2522
2523        if (!con)
2524                return false;
2525
2526        return con->poison_supported;
2527 }
2528
2529 /* helper function to handle common stuff in ip late init phase */
2530 int amdgpu_ras_block_late_init(struct amdgpu_device *adev,
2531                          struct ras_common_if *ras_block)
2532 {
2533         struct amdgpu_ras_block_object *ras_obj = NULL;
2534         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2535         unsigned long ue_count, ce_count;
2536         int r;
2537
2538         /* disable RAS feature per IP block if it is not supported */
2539         if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
2540                 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
2541                 return 0;
2542         }
2543
2544         r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
2545         if (r) {
2546                 if (adev->in_suspend || amdgpu_in_reset(adev)) {
2547                         /* in resume phase, if fail to enable ras,
2548                          * clean up all ras fs nodes, and disable ras */
2549                         goto cleanup;
2550                 } else
2551                         return r;
2552         }
2553
2554         /* check for errors on warm reset edc persisant supported ASIC */
2555         amdgpu_persistent_edc_harvesting(adev, ras_block);
2556
2557         /* in resume phase, no need to create ras fs node */
2558         if (adev->in_suspend || amdgpu_in_reset(adev))
2559                 return 0;
2560
2561         ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm);
2562         if (ras_obj->ras_cb || (ras_obj->hw_ops &&
2563             (ras_obj->hw_ops->query_poison_status ||
2564             ras_obj->hw_ops->handle_poison_consumption))) {
2565                 r = amdgpu_ras_interrupt_add_handler(adev, ras_block);
2566                 if (r)
2567                         goto cleanup;
2568         }
2569
2570         r = amdgpu_ras_sysfs_create(adev, ras_block);
2571         if (r)
2572                 goto interrupt;
2573
2574         /* Those are the cached values at init.
2575          */
2576         if (amdgpu_ras_query_error_count(adev, &ce_count, &ue_count) == 0) {
2577                 atomic_set(&con->ras_ce_count, ce_count);
2578                 atomic_set(&con->ras_ue_count, ue_count);
2579         }
2580
2581         return 0;
2582
2583 interrupt:
2584         if (ras_obj->ras_cb)
2585                 amdgpu_ras_interrupt_remove_handler(adev, ras_block);
2586 cleanup:
2587         amdgpu_ras_feature_enable(adev, ras_block, 0);
2588         return r;
2589 }
2590
2591 static int amdgpu_ras_block_late_init_default(struct amdgpu_device *adev,
2592                          struct ras_common_if *ras_block)
2593 {
2594         return amdgpu_ras_block_late_init(adev, ras_block);
2595 }
2596
2597 /* helper function to remove ras fs node and interrupt handler */
2598 void amdgpu_ras_block_late_fini(struct amdgpu_device *adev,
2599                           struct ras_common_if *ras_block)
2600 {
2601         struct amdgpu_ras_block_object *ras_obj;
2602         if (!ras_block)
2603                 return;
2604
2605         amdgpu_ras_sysfs_remove(adev, ras_block);
2606
2607         ras_obj = container_of(ras_block, struct amdgpu_ras_block_object, ras_comm);
2608         if (ras_obj->ras_cb)
2609                 amdgpu_ras_interrupt_remove_handler(adev, ras_block);
2610 }
2611
2612 static void amdgpu_ras_block_late_fini_default(struct amdgpu_device *adev,
2613                           struct ras_common_if *ras_block)
2614 {
2615         return amdgpu_ras_block_late_fini(adev, ras_block);
2616 }
2617
2618 /* do some init work after IP late init as dependence.
2619  * and it runs in resume/gpu reset/booting up cases.
2620  */
2621 void amdgpu_ras_resume(struct amdgpu_device *adev)
2622 {
2623         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2624         struct ras_manager *obj, *tmp;
2625
2626         if (!adev->ras_enabled || !con) {
2627                 /* clean ras context for VEGA20 Gaming after send ras disable cmd */
2628                 amdgpu_release_ras_context(adev);
2629
2630                 return;
2631         }
2632
2633         if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
2634                 /* Set up all other IPs which are not implemented. There is a
2635                  * tricky thing that IP's actual ras error type should be
2636                  * MULTI_UNCORRECTABLE, but as driver does not handle it, so
2637                  * ERROR_NONE make sense anyway.
2638                  */
2639                 amdgpu_ras_enable_all_features(adev, 1);
2640
2641                 /* We enable ras on all hw_supported block, but as boot
2642                  * parameter might disable some of them and one or more IP has
2643                  * not implemented yet. So we disable them on behalf.
2644                  */
2645                 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2646                         if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2647                                 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2648                                 /* there should be no any reference. */
2649                                 WARN_ON(alive_obj(obj));
2650                         }
2651                 }
2652         }
2653 }
2654
2655 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2656 {
2657         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2658
2659         if (!adev->ras_enabled || !con)
2660                 return;
2661
2662         amdgpu_ras_disable_all_features(adev, 0);
2663         /* Make sure all ras objects are disabled. */
2664         if (con->features)
2665                 amdgpu_ras_disable_all_features(adev, 1);
2666 }
2667
2668 int amdgpu_ras_late_init(struct amdgpu_device *adev)
2669 {
2670         struct amdgpu_ras_block_list *node, *tmp;
2671         struct amdgpu_ras_block_object *obj;
2672         int r;
2673
2674         /* Guest side doesn't need init ras feature */
2675         if (amdgpu_sriov_vf(adev))
2676                 return 0;
2677
2678         list_for_each_entry_safe(node, tmp, &adev->ras_list, node) {
2679                 if (!node->ras_obj) {
2680                         dev_warn(adev->dev, "Warning: abnormal ras list node.\n");
2681                         continue;
2682                 }
2683
2684                 obj = node->ras_obj;
2685                 if (obj->ras_late_init) {
2686                         r = obj->ras_late_init(adev, &obj->ras_comm);
2687                         if (r) {
2688                                 dev_err(adev->dev, "%s failed to execute ras_late_init! ret:%d\n",
2689                                         obj->ras_comm.name, r);
2690                                 return r;
2691                         }
2692                 } else
2693                         amdgpu_ras_block_late_init_default(adev, &obj->ras_comm);
2694         }
2695
2696         return 0;
2697 }
2698
2699 /* do some fini work before IP fini as dependence */
2700 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2701 {
2702         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2703
2704         if (!adev->ras_enabled || !con)
2705                 return 0;
2706
2707
2708         /* Need disable ras on all IPs here before ip [hw/sw]fini */
2709         amdgpu_ras_disable_all_features(adev, 0);
2710         amdgpu_ras_recovery_fini(adev);
2711         return 0;
2712 }
2713
2714 int amdgpu_ras_fini(struct amdgpu_device *adev)
2715 {
2716         struct amdgpu_ras_block_list *ras_node, *tmp;
2717         struct amdgpu_ras_block_object *obj = NULL;
2718         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2719
2720         if (!adev->ras_enabled || !con)
2721                 return 0;
2722
2723         list_for_each_entry_safe(ras_node, tmp, &adev->ras_list, node) {
2724                 if (ras_node->ras_obj) {
2725                         obj = ras_node->ras_obj;
2726                         if (amdgpu_ras_is_supported(adev, obj->ras_comm.block) &&
2727                             obj->ras_fini)
2728                                 obj->ras_fini(adev, &obj->ras_comm);
2729                         else
2730                                 amdgpu_ras_block_late_fini_default(adev, &obj->ras_comm);
2731                 }
2732
2733                 /* Clear ras blocks from ras_list and free ras block list node */
2734                 list_del(&ras_node->node);
2735                 kfree(ras_node);
2736         }
2737
2738         amdgpu_ras_fs_fini(adev);
2739         amdgpu_ras_interrupt_remove_all(adev);
2740
2741         WARN(con->features, "Feature mask is not cleared");
2742
2743         if (con->features)
2744                 amdgpu_ras_disable_all_features(adev, 1);
2745
2746         cancel_delayed_work_sync(&con->ras_counte_delay_work);
2747
2748         amdgpu_ras_set_context(adev, NULL);
2749         kfree(con);
2750
2751         return 0;
2752 }
2753
2754 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2755 {
2756         amdgpu_ras_check_supported(adev);
2757         if (!adev->ras_hw_enabled)
2758                 return;
2759
2760         if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2761                 dev_info(adev->dev, "uncorrectable hardware error"
2762                         "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2763
2764                 amdgpu_ras_reset_gpu(adev);
2765         }
2766 }
2767
2768 bool amdgpu_ras_need_emergency_restart(struct amdgpu_device *adev)
2769 {
2770         if (adev->asic_type == CHIP_VEGA20 &&
2771             adev->pm.fw_version <= 0x283400) {
2772                 return !(amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) &&
2773                                 amdgpu_ras_intr_triggered();
2774         }
2775
2776         return false;
2777 }
2778
2779 void amdgpu_release_ras_context(struct amdgpu_device *adev)
2780 {
2781         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2782
2783         if (!con)
2784                 return;
2785
2786         if (!adev->ras_enabled && con->features & BIT(AMDGPU_RAS_BLOCK__GFX)) {
2787                 con->features &= ~BIT(AMDGPU_RAS_BLOCK__GFX);
2788                 amdgpu_ras_set_context(adev, NULL);
2789                 kfree(con);
2790         }
2791 }
2792
2793 #ifdef CONFIG_X86_MCE_AMD
2794 static struct amdgpu_device *find_adev(uint32_t node_id)
2795 {
2796         int i;
2797         struct amdgpu_device *adev = NULL;
2798
2799         for (i = 0; i < mce_adev_list.num_gpu; i++) {
2800                 adev = mce_adev_list.devs[i];
2801
2802                 if (adev && adev->gmc.xgmi.connected_to_cpu &&
2803                     adev->gmc.xgmi.physical_node_id == node_id)
2804                         break;
2805                 adev = NULL;
2806         }
2807
2808         return adev;
2809 }
2810
2811 #define GET_MCA_IPID_GPUID(m)   (((m) >> 44) & 0xF)
2812 #define GET_UMC_INST(m)         (((m) >> 21) & 0x7)
2813 #define GET_CHAN_INDEX(m)       ((((m) >> 12) & 0x3) | (((m) >> 18) & 0x4))
2814 #define GPU_ID_OFFSET           8
2815
2816 static int amdgpu_bad_page_notifier(struct notifier_block *nb,
2817                                     unsigned long val, void *data)
2818 {
2819         struct mce *m = (struct mce *)data;
2820         struct amdgpu_device *adev = NULL;
2821         uint32_t gpu_id = 0;
2822         uint32_t umc_inst = 0;
2823         uint32_t ch_inst, channel_index = 0;
2824         struct ras_err_data err_data = {0, 0, 0, NULL};
2825         struct eeprom_table_record err_rec;
2826         uint64_t retired_page;
2827
2828         /*
2829          * If the error was generated in UMC_V2, which belongs to GPU UMCs,
2830          * and error occurred in DramECC (Extended error code = 0) then only
2831          * process the error, else bail out.
2832          */
2833         if (!m || !((smca_get_bank_type(m->extcpu, m->bank) == SMCA_UMC_V2) &&
2834                     (XEC(m->status, 0x3f) == 0x0)))
2835                 return NOTIFY_DONE;
2836
2837         /*
2838          * If it is correctable error, return.
2839          */
2840         if (mce_is_correctable(m))
2841                 return NOTIFY_OK;
2842
2843         /*
2844          * GPU Id is offset by GPU_ID_OFFSET in MCA_IPID_UMC register.
2845          */
2846         gpu_id = GET_MCA_IPID_GPUID(m->ipid) - GPU_ID_OFFSET;
2847
2848         adev = find_adev(gpu_id);
2849         if (!adev) {
2850                 DRM_WARN("%s: Unable to find adev for gpu_id: %d\n", __func__,
2851                                                                 gpu_id);
2852                 return NOTIFY_DONE;
2853         }
2854
2855         /*
2856          * If it is uncorrectable error, then find out UMC instance and
2857          * channel index.
2858          */
2859         umc_inst = GET_UMC_INST(m->ipid);
2860         ch_inst = GET_CHAN_INDEX(m->ipid);
2861
2862         dev_info(adev->dev, "Uncorrectable error detected in UMC inst: %d, chan_idx: %d",
2863                              umc_inst, ch_inst);
2864
2865         /*
2866          * Translate UMC channel address to Physical address
2867          */
2868         channel_index =
2869                 adev->umc.channel_idx_tbl[umc_inst * adev->umc.channel_inst_num
2870                                           + ch_inst];
2871
2872         retired_page = ADDR_OF_8KB_BLOCK(m->addr) |
2873                         ADDR_OF_256B_BLOCK(channel_index) |
2874                         OFFSET_IN_256B_BLOCK(m->addr);
2875
2876         memset(&err_rec, 0x0, sizeof(struct eeprom_table_record));
2877         err_data.err_addr = &err_rec;
2878         amdgpu_umc_fill_error_record(&err_data, m->addr,
2879                         retired_page, channel_index, umc_inst);
2880
2881         if (amdgpu_bad_page_threshold != 0) {
2882                 amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
2883                                                 err_data.err_addr_cnt);
2884                 amdgpu_ras_save_bad_pages(adev);
2885         }
2886
2887         return NOTIFY_OK;
2888 }
2889
2890 static struct notifier_block amdgpu_bad_page_nb = {
2891         .notifier_call  = amdgpu_bad_page_notifier,
2892         .priority       = MCE_PRIO_UC,
2893 };
2894
2895 static void amdgpu_register_bad_pages_mca_notifier(struct amdgpu_device *adev)
2896 {
2897         /*
2898          * Add the adev to the mce_adev_list.
2899          * During mode2 reset, amdgpu device is temporarily
2900          * removed from the mgpu_info list which can cause
2901          * page retirement to fail.
2902          * Use this list instead of mgpu_info to find the amdgpu
2903          * device on which the UMC error was reported.
2904          */
2905         mce_adev_list.devs[mce_adev_list.num_gpu++] = adev;
2906
2907         /*
2908          * Register the x86 notifier only once
2909          * with MCE subsystem.
2910          */
2911         if (notifier_registered == false) {
2912                 mce_register_decode_chain(&amdgpu_bad_page_nb);
2913                 notifier_registered = true;
2914         }
2915 }
2916 #endif
2917
2918 struct amdgpu_ras *amdgpu_ras_get_context(struct amdgpu_device *adev)
2919 {
2920         if (!adev)
2921                 return NULL;
2922
2923         return adev->psp.ras_context.ras;
2924 }
2925
2926 int amdgpu_ras_set_context(struct amdgpu_device *adev, struct amdgpu_ras *ras_con)
2927 {
2928         if (!adev)
2929                 return -EINVAL;
2930
2931         adev->psp.ras_context.ras = ras_con;
2932         return 0;
2933 }
2934
2935 /* check if ras is supported on block, say, sdma, gfx */
2936 int amdgpu_ras_is_supported(struct amdgpu_device *adev,
2937                 unsigned int block)
2938 {
2939         struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
2940
2941         if (block >= AMDGPU_RAS_BLOCK_COUNT)
2942                 return 0;
2943         return ras && (adev->ras_enabled & (1 << block));
2944 }
2945
2946 int amdgpu_ras_reset_gpu(struct amdgpu_device *adev)
2947 {
2948         struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
2949
2950         if (atomic_cmpxchg(&ras->in_recovery, 0, 1) == 0)
2951                 amdgpu_reset_domain_schedule(ras->adev->reset_domain, &ras->recovery_work);
2952         return 0;
2953 }
2954
2955
2956 /* Register each ip ras block into amdgpu ras */
2957 int amdgpu_ras_register_ras_block(struct amdgpu_device *adev,
2958                 struct amdgpu_ras_block_object *ras_block_obj)
2959 {
2960         struct amdgpu_ras_block_list *ras_node;
2961         if (!adev || !ras_block_obj)
2962                 return -EINVAL;
2963
2964         if (!amdgpu_ras_asic_supported(adev))
2965                 return 0;
2966
2967         ras_node = kzalloc(sizeof(*ras_node), GFP_KERNEL);
2968         if (!ras_node)
2969                 return -ENOMEM;
2970
2971         INIT_LIST_HEAD(&ras_node->node);
2972         ras_node->ras_obj = ras_block_obj;
2973         list_add_tail(&ras_node->node, &adev->ras_list);
2974
2975         return 0;
2976 }
This page took 0.210411 seconds and 4 git commands to generate.