]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
drm/amdgpu: update RAS error handling
[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
31 #include "amdgpu.h"
32 #include "amdgpu_ras.h"
33 #include "amdgpu_atomfirmware.h"
34 #include "amdgpu_xgmi.h"
35 #include "ivsrcid/nbio/irqsrcs_nbif_7_4.h"
36
37 const char *ras_error_string[] = {
38         "none",
39         "parity",
40         "single_correctable",
41         "multi_uncorrectable",
42         "poison",
43 };
44
45 const char *ras_block_string[] = {
46         "umc",
47         "sdma",
48         "gfx",
49         "mmhub",
50         "athub",
51         "pcie_bif",
52         "hdp",
53         "xgmi_wafl",
54         "df",
55         "smn",
56         "sem",
57         "mp0",
58         "mp1",
59         "fuse",
60 };
61
62 #define ras_err_str(i) (ras_error_string[ffs(i)])
63 #define ras_block_str(i) (ras_block_string[i])
64
65 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS           1
66 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET         2
67 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
68
69 /* inject address is 52 bits */
70 #define RAS_UMC_INJECT_ADDR_LIMIT       (0x1ULL << 52)
71
72 enum amdgpu_ras_retire_page_reservation {
73         AMDGPU_RAS_RETIRE_PAGE_RESERVED,
74         AMDGPU_RAS_RETIRE_PAGE_PENDING,
75         AMDGPU_RAS_RETIRE_PAGE_FAULT,
76 };
77
78 atomic_t amdgpu_ras_in_intr = ATOMIC_INIT(0);
79
80 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
81                                 uint64_t addr);
82
83 void amdgpu_ras_set_error_query_ready(struct amdgpu_device *adev, bool ready)
84 {
85         if (adev && amdgpu_ras_get_context(adev))
86                 amdgpu_ras_get_context(adev)->error_query_ready = ready;
87 }
88
89 bool amdgpu_ras_get_error_query_ready(struct amdgpu_device *adev)
90 {
91         if (adev && amdgpu_ras_get_context(adev))
92                 return amdgpu_ras_get_context(adev)->error_query_ready;
93
94         return false;
95 }
96
97 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
98                                         size_t size, loff_t *pos)
99 {
100         struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
101         struct ras_query_if info = {
102                 .head = obj->head,
103         };
104         ssize_t s;
105         char val[128];
106
107         if (amdgpu_ras_error_query(obj->adev, &info))
108                 return -EINVAL;
109
110         s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
111                         "ue", info.ue_count,
112                         "ce", info.ce_count);
113         if (*pos >= s)
114                 return 0;
115
116         s -= *pos;
117         s = min_t(u64, s, size);
118
119
120         if (copy_to_user(buf, &val[*pos], s))
121                 return -EINVAL;
122
123         *pos += s;
124
125         return s;
126 }
127
128 static const struct file_operations amdgpu_ras_debugfs_ops = {
129         .owner = THIS_MODULE,
130         .read = amdgpu_ras_debugfs_read,
131         .write = NULL,
132         .llseek = default_llseek
133 };
134
135 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
136 {
137         int i;
138
139         for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
140                 *block_id = i;
141                 if (strcmp(name, ras_block_str(i)) == 0)
142                         return 0;
143         }
144         return -EINVAL;
145 }
146
147 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
148                 const char __user *buf, size_t size,
149                 loff_t *pos, struct ras_debug_if *data)
150 {
151         ssize_t s = min_t(u64, 64, size);
152         char str[65];
153         char block_name[33];
154         char err[9] = "ue";
155         int op = -1;
156         int block_id;
157         uint32_t sub_block;
158         u64 address, value;
159
160         if (*pos)
161                 return -EINVAL;
162         *pos = size;
163
164         memset(str, 0, sizeof(str));
165         memset(data, 0, sizeof(*data));
166
167         if (copy_from_user(str, buf, s))
168                 return -EINVAL;
169
170         if (sscanf(str, "disable %32s", block_name) == 1)
171                 op = 0;
172         else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
173                 op = 1;
174         else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
175                 op = 2;
176         else if (str[0] && str[1] && str[2] && str[3])
177                 /* ascii string, but commands are not matched. */
178                 return -EINVAL;
179
180         if (op != -1) {
181                 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
182                         return -EINVAL;
183
184                 data->head.block = block_id;
185                 /* only ue and ce errors are supported */
186                 if (!memcmp("ue", err, 2))
187                         data->head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
188                 else if (!memcmp("ce", err, 2))
189                         data->head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
190                 else
191                         return -EINVAL;
192
193                 data->op = op;
194
195                 if (op == 2) {
196                         if (sscanf(str, "%*s %*s %*s %u %llu %llu",
197                                                 &sub_block, &address, &value) != 3)
198                                 if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
199                                                         &sub_block, &address, &value) != 3)
200                                         return -EINVAL;
201                         data->head.sub_block_index = sub_block;
202                         data->inject.address = address;
203                         data->inject.value = value;
204                 }
205         } else {
206                 if (size < sizeof(*data))
207                         return -EINVAL;
208
209                 if (copy_from_user(data, buf, sizeof(*data)))
210                         return -EINVAL;
211         }
212
213         return 0;
214 }
215
216 /**
217  * DOC: AMDGPU RAS debugfs control interface
218  *
219  * It accepts struct ras_debug_if who has two members.
220  *
221  * First member: ras_debug_if::head or ras_debug_if::inject.
222  *
223  * head is used to indicate which IP block will be under control.
224  *
225  * head has four members, they are block, type, sub_block_index, name.
226  * block: which IP will be under control.
227  * type: what kind of error will be enabled/disabled/injected.
228  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
229  * name: the name of IP.
230  *
231  * inject has two more members than head, they are address, value.
232  * As their names indicate, inject operation will write the
233  * value to the address.
234  *
235  * The second member: struct ras_debug_if::op.
236  * It has three kinds of operations.
237  *
238  * - 0: disable RAS on the block. Take ::head as its data.
239  * - 1: enable RAS on the block. Take ::head as its data.
240  * - 2: inject errors on the block. Take ::inject as its data.
241  *
242  * How to use the interface?
243  *
244  * Programs
245  *
246  * Copy the struct ras_debug_if in your codes and initialize it.
247  * Write the struct to the control node.
248  *
249  * Shells
250  *
251  * .. code-block:: bash
252  *
253  *      echo op block [error [sub_block address value]] > .../ras/ras_ctrl
254  *
255  * Parameters:
256  *
257  * op: disable, enable, inject
258  *      disable: only block is needed
259  *      enable: block and error are needed
260  *      inject: error, address, value are needed
261  * block: umc, sdma, gfx, .........
262  *      see ras_block_string[] for details
263  * error: ue, ce
264  *      ue: multi_uncorrectable
265  *      ce: single_correctable
266  * sub_block:
267  *      sub block index, pass 0 if there is no sub block
268  *
269  * here are some examples for bash commands:
270  *
271  * .. code-block:: bash
272  *
273  *      echo inject umc ue 0x0 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
274  *      echo inject umc ce 0 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
275  *      echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
276  *
277  * How to check the result?
278  *
279  * For disable/enable, please check ras features at
280  * /sys/class/drm/card[0/1/2...]/device/ras/features
281  *
282  * For inject, please check corresponding err count at
283  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
284  *
285  * .. note::
286  *      Operations are only allowed on blocks which are supported.
287  *      Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
288  *      to see which blocks support RAS on a particular asic.
289  *
290  */
291 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
292                 size_t size, loff_t *pos)
293 {
294         struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
295         struct ras_debug_if data;
296         int ret = 0;
297
298         if (!amdgpu_ras_get_error_query_ready(adev)) {
299                 dev_warn(adev->dev, "RAS WARN: error injection "
300                                 "currently inaccessible\n");
301                 return size;
302         }
303
304         ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
305         if (ret)
306                 return -EINVAL;
307
308         if (!amdgpu_ras_is_supported(adev, data.head.block))
309                 return -EINVAL;
310
311         switch (data.op) {
312         case 0:
313                 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
314                 break;
315         case 1:
316                 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
317                 break;
318         case 2:
319                 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
320                     (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
321                         ret = -EINVAL;
322                         break;
323                 }
324
325                 /* umc ce/ue error injection for a bad page is not allowed */
326                 if ((data.head.block == AMDGPU_RAS_BLOCK__UMC) &&
327                     amdgpu_ras_check_bad_page(adev, data.inject.address)) {
328                         dev_warn(adev->dev, "RAS WARN: 0x%llx has been marked "
329                                         "as bad before error injection!\n",
330                                         data.inject.address);
331                         break;
332                 }
333
334                 /* data.inject.address is offset instead of absolute gpu address */
335                 ret = amdgpu_ras_error_inject(adev, &data.inject);
336                 break;
337         default:
338                 ret = -EINVAL;
339                 break;
340         }
341
342         if (ret)
343                 return -EINVAL;
344
345         return size;
346 }
347
348 /**
349  * DOC: AMDGPU RAS debugfs EEPROM table reset interface
350  *
351  * Some boards contain an EEPROM which is used to persistently store a list of
352  * bad pages which experiences ECC errors in vram.  This interface provides
353  * a way to reset the EEPROM, e.g., after testing error injection.
354  *
355  * Usage:
356  *
357  * .. code-block:: bash
358  *
359  *      echo 1 > ../ras/ras_eeprom_reset
360  *
361  * will reset EEPROM table to 0 entries.
362  *
363  */
364 static ssize_t amdgpu_ras_debugfs_eeprom_write(struct file *f, const char __user *buf,
365                 size_t size, loff_t *pos)
366 {
367         struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
368         int ret;
369
370         ret = amdgpu_ras_eeprom_reset_table(&adev->psp.ras.ras->eeprom_control);
371
372         return ret == 1 ? size : -EIO;
373 }
374
375 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
376         .owner = THIS_MODULE,
377         .read = NULL,
378         .write = amdgpu_ras_debugfs_ctrl_write,
379         .llseek = default_llseek
380 };
381
382 static const struct file_operations amdgpu_ras_debugfs_eeprom_ops = {
383         .owner = THIS_MODULE,
384         .read = NULL,
385         .write = amdgpu_ras_debugfs_eeprom_write,
386         .llseek = default_llseek
387 };
388
389 /**
390  * DOC: AMDGPU RAS sysfs Error Count Interface
391  *
392  * It allows the user to read the error count for each IP block on the gpu through
393  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
394  *
395  * It outputs the multiple lines which report the uncorrected (ue) and corrected
396  * (ce) error counts.
397  *
398  * The format of one line is below,
399  *
400  * [ce|ue]: count
401  *
402  * Example:
403  *
404  * .. code-block:: bash
405  *
406  *      ue: 0
407  *      ce: 1
408  *
409  */
410 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
411                 struct device_attribute *attr, char *buf)
412 {
413         struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
414         struct ras_query_if info = {
415                 .head = obj->head,
416         };
417
418         if (!amdgpu_ras_get_error_query_ready(obj->adev))
419                 return snprintf(buf, PAGE_SIZE,
420                                 "Query currently inaccessible\n");
421
422         if (amdgpu_ras_error_query(obj->adev, &info))
423                 return -EINVAL;
424
425         return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
426                         "ue", info.ue_count,
427                         "ce", info.ce_count);
428 }
429
430 /* obj begin */
431
432 #define get_obj(obj) do { (obj)->use++; } while (0)
433 #define alive_obj(obj) ((obj)->use)
434
435 static inline void put_obj(struct ras_manager *obj)
436 {
437         if (obj && --obj->use == 0)
438                 list_del(&obj->node);
439         if (obj && obj->use < 0) {
440                  DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
441         }
442 }
443
444 /* make one obj and return it. */
445 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
446                 struct ras_common_if *head)
447 {
448         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
449         struct ras_manager *obj;
450
451         if (!con)
452                 return NULL;
453
454         if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
455                 return NULL;
456
457         obj = &con->objs[head->block];
458         /* already exist. return obj? */
459         if (alive_obj(obj))
460                 return NULL;
461
462         obj->head = *head;
463         obj->adev = adev;
464         list_add(&obj->node, &con->head);
465         get_obj(obj);
466
467         return obj;
468 }
469
470 /* return an obj equal to head, or the first when head is NULL */
471 struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
472                 struct ras_common_if *head)
473 {
474         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
475         struct ras_manager *obj;
476         int i;
477
478         if (!con)
479                 return NULL;
480
481         if (head) {
482                 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
483                         return NULL;
484
485                 obj = &con->objs[head->block];
486
487                 if (alive_obj(obj)) {
488                         WARN_ON(head->block != obj->head.block);
489                         return obj;
490                 }
491         } else {
492                 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
493                         obj = &con->objs[i];
494                         if (alive_obj(obj)) {
495                                 WARN_ON(i != obj->head.block);
496                                 return obj;
497                         }
498                 }
499         }
500
501         return NULL;
502 }
503 /* obj end */
504
505 void amdgpu_ras_parse_status_code(struct amdgpu_device* adev,
506                                   const char*           invoke_type,
507                                   const char*           block_name,
508                                   enum ta_ras_status    ret)
509 {
510         switch (ret) {
511         case TA_RAS_STATUS__SUCCESS:
512                 return;
513         case TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE:
514                 dev_warn(adev->dev,
515                         "RAS WARN: %s %s currently unavailable\n",
516                         invoke_type,
517                         block_name);
518                 break;
519         default:
520                 dev_err(adev->dev,
521                         "RAS ERROR: %s %s error failed ret 0x%X\n",
522                         invoke_type,
523                         block_name,
524                         ret);
525         }
526 }
527
528 /* feature ctl begin */
529 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
530                 struct ras_common_if *head)
531 {
532         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
533
534         return con->hw_supported & BIT(head->block);
535 }
536
537 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
538                 struct ras_common_if *head)
539 {
540         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
541
542         return con->features & BIT(head->block);
543 }
544
545 /*
546  * if obj is not created, then create one.
547  * set feature enable flag.
548  */
549 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
550                 struct ras_common_if *head, int enable)
551 {
552         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
553         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
554
555         /* If hardware does not support ras, then do not create obj.
556          * But if hardware support ras, we can create the obj.
557          * Ras framework checks con->hw_supported to see if it need do
558          * corresponding initialization.
559          * IP checks con->support to see if it need disable ras.
560          */
561         if (!amdgpu_ras_is_feature_allowed(adev, head))
562                 return 0;
563         if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
564                 return 0;
565
566         if (enable) {
567                 if (!obj) {
568                         obj = amdgpu_ras_create_obj(adev, head);
569                         if (!obj)
570                                 return -EINVAL;
571                 } else {
572                         /* In case we create obj somewhere else */
573                         get_obj(obj);
574                 }
575                 con->features |= BIT(head->block);
576         } else {
577                 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
578                         con->features &= ~BIT(head->block);
579                         put_obj(obj);
580                 }
581         }
582
583         return 0;
584 }
585
586 /* wrapper of psp_ras_enable_features */
587 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
588                 struct ras_common_if *head, bool enable)
589 {
590         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
591         union ta_ras_cmd_input info;
592         int ret;
593
594         if (!con)
595                 return -EINVAL;
596
597         if (!enable) {
598                 info.disable_features = (struct ta_ras_disable_features_input) {
599                         .block_id =  amdgpu_ras_block_to_ta(head->block),
600                         .error_type = amdgpu_ras_error_to_ta(head->type),
601                 };
602         } else {
603                 info.enable_features = (struct ta_ras_enable_features_input) {
604                         .block_id =  amdgpu_ras_block_to_ta(head->block),
605                         .error_type = amdgpu_ras_error_to_ta(head->type),
606                 };
607         }
608
609         /* Do not enable if it is not allowed. */
610         WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
611         /* Are we alerady in that state we are going to set? */
612         if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
613                 return 0;
614
615         if (!amdgpu_ras_intr_triggered()) {
616                 ret = psp_ras_enable_features(&adev->psp, &info, enable);
617                 if (ret) {
618                         amdgpu_ras_parse_status_code(adev,
619                                                      enable ? "enable":"disable",
620                                                      ras_block_str(head->block),
621                                                     (enum ta_ras_status)ret);
622                         if (ret == TA_RAS_STATUS__RESET_NEEDED)
623                                 return -EAGAIN;
624                         return -EINVAL;
625                 }
626         }
627
628         /* setup the obj */
629         __amdgpu_ras_feature_enable(adev, head, enable);
630
631         return 0;
632 }
633
634 /* Only used in device probe stage and called only once. */
635 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
636                 struct ras_common_if *head, bool enable)
637 {
638         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
639         int ret;
640
641         if (!con)
642                 return -EINVAL;
643
644         if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
645                 if (enable) {
646                         /* There is no harm to issue a ras TA cmd regardless of
647                          * the currecnt ras state.
648                          * If current state == target state, it will do nothing
649                          * But sometimes it requests driver to reset and repost
650                          * with error code -EAGAIN.
651                          */
652                         ret = amdgpu_ras_feature_enable(adev, head, 1);
653                         /* With old ras TA, we might fail to enable ras.
654                          * Log it and just setup the object.
655                          * TODO need remove this WA in the future.
656                          */
657                         if (ret == -EINVAL) {
658                                 ret = __amdgpu_ras_feature_enable(adev, head, 1);
659                                 if (!ret)
660                                         dev_info(adev->dev,
661                                                 "RAS INFO: %s setup object\n",
662                                                 ras_block_str(head->block));
663                         }
664                 } else {
665                         /* setup the object then issue a ras TA disable cmd.*/
666                         ret = __amdgpu_ras_feature_enable(adev, head, 1);
667                         if (ret)
668                                 return ret;
669
670                         ret = amdgpu_ras_feature_enable(adev, head, 0);
671                 }
672         } else
673                 ret = amdgpu_ras_feature_enable(adev, head, enable);
674
675         return ret;
676 }
677
678 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
679                 bool bypass)
680 {
681         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
682         struct ras_manager *obj, *tmp;
683
684         list_for_each_entry_safe(obj, tmp, &con->head, node) {
685                 /* bypass psp.
686                  * aka just release the obj and corresponding flags
687                  */
688                 if (bypass) {
689                         if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
690                                 break;
691                 } else {
692                         if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
693                                 break;
694                 }
695         }
696
697         return con->features;
698 }
699
700 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
701                 bool bypass)
702 {
703         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
704         int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
705         int i;
706         const enum amdgpu_ras_error_type default_ras_type =
707                 AMDGPU_RAS_ERROR__NONE;
708
709         for (i = 0; i < ras_block_count; i++) {
710                 struct ras_common_if head = {
711                         .block = i,
712                         .type = default_ras_type,
713                         .sub_block_index = 0,
714                 };
715                 strcpy(head.name, ras_block_str(i));
716                 if (bypass) {
717                         /*
718                          * bypass psp. vbios enable ras for us.
719                          * so just create the obj
720                          */
721                         if (__amdgpu_ras_feature_enable(adev, &head, 1))
722                                 break;
723                 } else {
724                         if (amdgpu_ras_feature_enable(adev, &head, 1))
725                                 break;
726                 }
727         }
728
729         return con->features;
730 }
731 /* feature ctl end */
732
733 /* query/inject/cure begin */
734 int amdgpu_ras_error_query(struct amdgpu_device *adev,
735                 struct ras_query_if *info)
736 {
737         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
738         struct ras_err_data err_data = {0, 0, 0, NULL};
739         int i;
740
741         if (!obj)
742                 return -EINVAL;
743
744         switch (info->head.block) {
745         case AMDGPU_RAS_BLOCK__UMC:
746                 if (adev->umc.funcs->query_ras_error_count)
747                         adev->umc.funcs->query_ras_error_count(adev, &err_data);
748                 /* umc query_ras_error_address is also responsible for clearing
749                  * error status
750                  */
751                 if (adev->umc.funcs->query_ras_error_address)
752                         adev->umc.funcs->query_ras_error_address(adev, &err_data);
753                 break;
754         case AMDGPU_RAS_BLOCK__SDMA:
755                 if (adev->sdma.funcs->query_ras_error_count) {
756                         for (i = 0; i < adev->sdma.num_instances; i++)
757                                 adev->sdma.funcs->query_ras_error_count(adev, i,
758                                                                         &err_data);
759                 }
760                 break;
761         case AMDGPU_RAS_BLOCK__GFX:
762                 if (adev->gfx.funcs->query_ras_error_count)
763                         adev->gfx.funcs->query_ras_error_count(adev, &err_data);
764                 break;
765         case AMDGPU_RAS_BLOCK__MMHUB:
766                 if (adev->mmhub.funcs->query_ras_error_count)
767                         adev->mmhub.funcs->query_ras_error_count(adev, &err_data);
768                 break;
769         case AMDGPU_RAS_BLOCK__PCIE_BIF:
770                 if (adev->nbio.funcs->query_ras_error_count)
771                         adev->nbio.funcs->query_ras_error_count(adev, &err_data);
772                 break;
773         case AMDGPU_RAS_BLOCK__XGMI_WAFL:
774                 amdgpu_xgmi_query_ras_error_count(adev, &err_data);
775                 break;
776         default:
777                 break;
778         }
779
780         obj->err_data.ue_count += err_data.ue_count;
781         obj->err_data.ce_count += err_data.ce_count;
782
783         info->ue_count = obj->err_data.ue_count;
784         info->ce_count = obj->err_data.ce_count;
785
786         if (err_data.ce_count) {
787                 dev_info(adev->dev, "%ld correctable hardware errors "
788                                         "detected in %s block, no user "
789                                         "action is needed.\n",
790                                         obj->err_data.ce_count,
791                                         ras_block_str(info->head.block));
792         }
793         if (err_data.ue_count) {
794                 dev_info(adev->dev, "%ld uncorrectable hardware errors "
795                                         "detected in %s block\n",
796                                         obj->err_data.ue_count,
797                                         ras_block_str(info->head.block));
798         }
799
800         return 0;
801 }
802
803 /* wrapper of psp_ras_trigger_error */
804 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
805                 struct ras_inject_if *info)
806 {
807         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
808         struct ta_ras_trigger_error_input block_info = {
809                 .block_id =  amdgpu_ras_block_to_ta(info->head.block),
810                 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
811                 .sub_block_index = info->head.sub_block_index,
812                 .address = info->address,
813                 .value = info->value,
814         };
815         int ret = 0;
816
817         if (!obj)
818                 return -EINVAL;
819
820         /* Calculate XGMI relative offset */
821         if (adev->gmc.xgmi.num_physical_nodes > 1) {
822                 block_info.address =
823                         amdgpu_xgmi_get_relative_phy_addr(adev,
824                                                           block_info.address);
825         }
826
827         switch (info->head.block) {
828         case AMDGPU_RAS_BLOCK__GFX:
829                 if (adev->gfx.funcs->ras_error_inject)
830                         ret = adev->gfx.funcs->ras_error_inject(adev, info);
831                 else
832                         ret = -EINVAL;
833                 break;
834         case AMDGPU_RAS_BLOCK__UMC:
835         case AMDGPU_RAS_BLOCK__MMHUB:
836         case AMDGPU_RAS_BLOCK__XGMI_WAFL:
837         case AMDGPU_RAS_BLOCK__PCIE_BIF:
838                 ret = psp_ras_trigger_error(&adev->psp, &block_info);
839                 break;
840         default:
841                 dev_info(adev->dev, "%s error injection is not supported yet\n",
842                          ras_block_str(info->head.block));
843                 ret = -EINVAL;
844         }
845
846         amdgpu_ras_parse_status_code(adev,
847                                      "inject",
848                                      ras_block_str(info->head.block),
849                                      (enum ta_ras_status)ret);
850
851         return ret;
852 }
853
854 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
855                 struct ras_cure_if *info)
856 {
857         /* psp fw has no cure interface for now. */
858         return 0;
859 }
860
861 /* get the total error counts on all IPs */
862 unsigned long amdgpu_ras_query_error_count(struct amdgpu_device *adev,
863                 bool is_ce)
864 {
865         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
866         struct ras_manager *obj;
867         struct ras_err_data data = {0, 0};
868
869         if (!con)
870                 return 0;
871
872         list_for_each_entry(obj, &con->head, node) {
873                 struct ras_query_if info = {
874                         .head = obj->head,
875                 };
876
877                 if (amdgpu_ras_error_query(adev, &info))
878                         return 0;
879
880                 data.ce_count += info.ce_count;
881                 data.ue_count += info.ue_count;
882         }
883
884         return is_ce ? data.ce_count : data.ue_count;
885 }
886 /* query/inject/cure end */
887
888
889 /* sysfs begin */
890
891 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
892                 struct ras_badpage **bps, unsigned int *count);
893
894 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
895 {
896         switch (flags) {
897         case AMDGPU_RAS_RETIRE_PAGE_RESERVED:
898                 return "R";
899         case AMDGPU_RAS_RETIRE_PAGE_PENDING:
900                 return "P";
901         case AMDGPU_RAS_RETIRE_PAGE_FAULT:
902         default:
903                 return "F";
904         };
905 }
906
907 /**
908  * DOC: AMDGPU RAS sysfs gpu_vram_bad_pages Interface
909  *
910  * It allows user to read the bad pages of vram on the gpu through
911  * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
912  *
913  * It outputs multiple lines, and each line stands for one gpu page.
914  *
915  * The format of one line is below,
916  * gpu pfn : gpu page size : flags
917  *
918  * gpu pfn and gpu page size are printed in hex format.
919  * flags can be one of below character,
920  *
921  * R: reserved, this gpu page is reserved and not able to use.
922  *
923  * P: pending for reserve, this gpu page is marked as bad, will be reserved
924  * in next window of page_reserve.
925  *
926  * F: unable to reserve. this gpu page can't be reserved due to some reasons.
927  *
928  * Examples:
929  *
930  * .. code-block:: bash
931  *
932  *      0x00000001 : 0x00001000 : R
933  *      0x00000002 : 0x00001000 : P
934  *
935  */
936
937 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
938                 struct kobject *kobj, struct bin_attribute *attr,
939                 char *buf, loff_t ppos, size_t count)
940 {
941         struct amdgpu_ras *con =
942                 container_of(attr, struct amdgpu_ras, badpages_attr);
943         struct amdgpu_device *adev = con->adev;
944         const unsigned int element_size =
945                 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
946         unsigned int start = div64_ul(ppos + element_size - 1, element_size);
947         unsigned int end = div64_ul(ppos + count - 1, element_size);
948         ssize_t s = 0;
949         struct ras_badpage *bps = NULL;
950         unsigned int bps_count = 0;
951
952         memset(buf, 0, count);
953
954         if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
955                 return 0;
956
957         for (; start < end && start < bps_count; start++)
958                 s += scnprintf(&buf[s], element_size + 1,
959                                 "0x%08x : 0x%08x : %1s\n",
960                                 bps[start].bp,
961                                 bps[start].size,
962                                 amdgpu_ras_badpage_flags_str(bps[start].flags));
963
964         kfree(bps);
965
966         return s;
967 }
968
969 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
970                 struct device_attribute *attr, char *buf)
971 {
972         struct amdgpu_ras *con =
973                 container_of(attr, struct amdgpu_ras, features_attr);
974
975         return scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
976 }
977
978 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
979 {
980         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
981         struct attribute *attrs[] = {
982                 &con->features_attr.attr,
983                 NULL
984         };
985         struct bin_attribute *bin_attrs[] = {
986                 &con->badpages_attr,
987                 NULL
988         };
989         struct attribute_group group = {
990                 .name = "ras",
991                 .attrs = attrs,
992                 .bin_attrs = bin_attrs,
993         };
994
995         con->features_attr = (struct device_attribute) {
996                 .attr = {
997                         .name = "features",
998                         .mode = S_IRUGO,
999                 },
1000                         .show = amdgpu_ras_sysfs_features_read,
1001         };
1002
1003         con->badpages_attr = (struct bin_attribute) {
1004                 .attr = {
1005                         .name = "gpu_vram_bad_pages",
1006                         .mode = S_IRUGO,
1007                 },
1008                 .size = 0,
1009                 .private = NULL,
1010                 .read = amdgpu_ras_sysfs_badpages_read,
1011         };
1012
1013         sysfs_attr_init(attrs[0]);
1014         sysfs_bin_attr_init(bin_attrs[0]);
1015
1016         return sysfs_create_group(&adev->dev->kobj, &group);
1017 }
1018
1019 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
1020 {
1021         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1022         struct attribute *attrs[] = {
1023                 &con->features_attr.attr,
1024                 NULL
1025         };
1026         struct bin_attribute *bin_attrs[] = {
1027                 &con->badpages_attr,
1028                 NULL
1029         };
1030         struct attribute_group group = {
1031                 .name = "ras",
1032                 .attrs = attrs,
1033                 .bin_attrs = bin_attrs,
1034         };
1035
1036         sysfs_remove_group(&adev->dev->kobj, &group);
1037
1038         return 0;
1039 }
1040
1041 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
1042                 struct ras_fs_if *head)
1043 {
1044         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1045
1046         if (!obj || obj->attr_inuse)
1047                 return -EINVAL;
1048
1049         get_obj(obj);
1050
1051         memcpy(obj->fs_data.sysfs_name,
1052                         head->sysfs_name,
1053                         sizeof(obj->fs_data.sysfs_name));
1054
1055         obj->sysfs_attr = (struct device_attribute){
1056                 .attr = {
1057                         .name = obj->fs_data.sysfs_name,
1058                         .mode = S_IRUGO,
1059                 },
1060                         .show = amdgpu_ras_sysfs_read,
1061         };
1062         sysfs_attr_init(&obj->sysfs_attr.attr);
1063
1064         if (sysfs_add_file_to_group(&adev->dev->kobj,
1065                                 &obj->sysfs_attr.attr,
1066                                 "ras")) {
1067                 put_obj(obj);
1068                 return -EINVAL;
1069         }
1070
1071         obj->attr_inuse = 1;
1072
1073         return 0;
1074 }
1075
1076 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
1077                 struct ras_common_if *head)
1078 {
1079         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1080
1081         if (!obj || !obj->attr_inuse)
1082                 return -EINVAL;
1083
1084         sysfs_remove_file_from_group(&adev->dev->kobj,
1085                                 &obj->sysfs_attr.attr,
1086                                 "ras");
1087         obj->attr_inuse = 0;
1088         put_obj(obj);
1089
1090         return 0;
1091 }
1092
1093 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
1094 {
1095         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1096         struct ras_manager *obj, *tmp;
1097
1098         list_for_each_entry_safe(obj, tmp, &con->head, node) {
1099                 amdgpu_ras_sysfs_remove(adev, &obj->head);
1100         }
1101
1102         amdgpu_ras_sysfs_remove_feature_node(adev);
1103
1104         return 0;
1105 }
1106 /* sysfs end */
1107
1108 /**
1109  * DOC: AMDGPU RAS Reboot Behavior for Unrecoverable Errors
1110  *
1111  * Normally when there is an uncorrectable error, the driver will reset
1112  * the GPU to recover.  However, in the event of an unrecoverable error,
1113  * the driver provides an interface to reboot the system automatically
1114  * in that event.
1115  *
1116  * The following file in debugfs provides that interface:
1117  * /sys/kernel/debug/dri/[0/1/2...]/ras/auto_reboot
1118  *
1119  * Usage:
1120  *
1121  * .. code-block:: bash
1122  *
1123  *      echo true > .../ras/auto_reboot
1124  *
1125  */
1126 /* debugfs begin */
1127 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
1128 {
1129         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1130         struct drm_minor *minor = adev->ddev->primary;
1131
1132         con->dir = debugfs_create_dir("ras", minor->debugfs_root);
1133         debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
1134                                 adev, &amdgpu_ras_debugfs_ctrl_ops);
1135         debugfs_create_file("ras_eeprom_reset", S_IWUGO | S_IRUGO, con->dir,
1136                                 adev, &amdgpu_ras_debugfs_eeprom_ops);
1137
1138         /*
1139          * After one uncorrectable error happens, usually GPU recovery will
1140          * be scheduled. But due to the known problem in GPU recovery failing
1141          * to bring GPU back, below interface provides one direct way to
1142          * user to reboot system automatically in such case within
1143          * ERREVENT_ATHUB_INTERRUPT generated. Normal GPU recovery routine
1144          * will never be called.
1145          */
1146         debugfs_create_bool("auto_reboot", S_IWUGO | S_IRUGO, con->dir,
1147                                 &con->reboot);
1148 }
1149
1150 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
1151                 struct ras_fs_if *head)
1152 {
1153         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1154         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
1155
1156         if (!obj || obj->ent)
1157                 return;
1158
1159         get_obj(obj);
1160
1161         memcpy(obj->fs_data.debugfs_name,
1162                         head->debugfs_name,
1163                         sizeof(obj->fs_data.debugfs_name));
1164
1165         obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
1166                                        S_IWUGO | S_IRUGO, con->dir, obj,
1167                                        &amdgpu_ras_debugfs_ops);
1168 }
1169
1170 void amdgpu_ras_debugfs_create_all(struct amdgpu_device *adev)
1171 {
1172         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1173         struct ras_manager *obj;
1174         struct ras_fs_if fs_info;
1175
1176         /*
1177          * it won't be called in resume path, no need to check
1178          * suspend and gpu reset status
1179          */
1180         if (!con)
1181                 return;
1182
1183         amdgpu_ras_debugfs_create_ctrl_node(adev);
1184
1185         list_for_each_entry(obj, &con->head, node) {
1186                 if (amdgpu_ras_is_supported(adev, obj->head.block) &&
1187                         (obj->attr_inuse == 1)) {
1188                         sprintf(fs_info.debugfs_name, "%s_err_inject",
1189                                         ras_block_str(obj->head.block));
1190                         fs_info.head = obj->head;
1191                         amdgpu_ras_debugfs_create(adev, &fs_info);
1192                 }
1193         }
1194 }
1195
1196 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1197                 struct ras_common_if *head)
1198 {
1199         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1200
1201         if (!obj || !obj->ent)
1202                 return;
1203
1204         debugfs_remove(obj->ent);
1205         obj->ent = NULL;
1206         put_obj(obj);
1207 }
1208
1209 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1210 {
1211         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1212         struct ras_manager *obj, *tmp;
1213
1214         list_for_each_entry_safe(obj, tmp, &con->head, node) {
1215                 amdgpu_ras_debugfs_remove(adev, &obj->head);
1216         }
1217
1218         debugfs_remove_recursive(con->dir);
1219         con->dir = NULL;
1220 }
1221 /* debugfs end */
1222
1223 /* ras fs */
1224
1225 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1226 {
1227         amdgpu_ras_sysfs_create_feature_node(adev);
1228
1229         return 0;
1230 }
1231
1232 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1233 {
1234         amdgpu_ras_debugfs_remove_all(adev);
1235         amdgpu_ras_sysfs_remove_all(adev);
1236         return 0;
1237 }
1238 /* ras fs end */
1239
1240 /* ih begin */
1241 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1242 {
1243         struct ras_ih_data *data = &obj->ih_data;
1244         struct amdgpu_iv_entry entry;
1245         int ret;
1246         struct ras_err_data err_data = {0, 0, 0, NULL};
1247
1248         while (data->rptr != data->wptr) {
1249                 rmb();
1250                 memcpy(&entry, &data->ring[data->rptr],
1251                                 data->element_size);
1252
1253                 wmb();
1254                 data->rptr = (data->aligned_element_size +
1255                                 data->rptr) % data->ring_size;
1256
1257                 /* Let IP handle its data, maybe we need get the output
1258                  * from the callback to udpate the error type/count, etc
1259                  */
1260                 if (data->cb) {
1261                         ret = data->cb(obj->adev, &err_data, &entry);
1262                         /* ue will trigger an interrupt, and in that case
1263                          * we need do a reset to recovery the whole system.
1264                          * But leave IP do that recovery, here we just dispatch
1265                          * the error.
1266                          */
1267                         if (ret == AMDGPU_RAS_SUCCESS) {
1268                                 /* these counts could be left as 0 if
1269                                  * some blocks do not count error number
1270                                  */
1271                                 obj->err_data.ue_count += err_data.ue_count;
1272                                 obj->err_data.ce_count += err_data.ce_count;
1273                         }
1274                 }
1275         }
1276 }
1277
1278 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1279 {
1280         struct ras_ih_data *data =
1281                 container_of(work, struct ras_ih_data, ih_work);
1282         struct ras_manager *obj =
1283                 container_of(data, struct ras_manager, ih_data);
1284
1285         amdgpu_ras_interrupt_handler(obj);
1286 }
1287
1288 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1289                 struct ras_dispatch_if *info)
1290 {
1291         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1292         struct ras_ih_data *data = &obj->ih_data;
1293
1294         if (!obj)
1295                 return -EINVAL;
1296
1297         if (data->inuse == 0)
1298                 return 0;
1299
1300         /* Might be overflow... */
1301         memcpy(&data->ring[data->wptr], info->entry,
1302                         data->element_size);
1303
1304         wmb();
1305         data->wptr = (data->aligned_element_size +
1306                         data->wptr) % data->ring_size;
1307
1308         schedule_work(&data->ih_work);
1309
1310         return 0;
1311 }
1312
1313 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1314                 struct ras_ih_if *info)
1315 {
1316         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1317         struct ras_ih_data *data;
1318
1319         if (!obj)
1320                 return -EINVAL;
1321
1322         data = &obj->ih_data;
1323         if (data->inuse == 0)
1324                 return 0;
1325
1326         cancel_work_sync(&data->ih_work);
1327
1328         kfree(data->ring);
1329         memset(data, 0, sizeof(*data));
1330         put_obj(obj);
1331
1332         return 0;
1333 }
1334
1335 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1336                 struct ras_ih_if *info)
1337 {
1338         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1339         struct ras_ih_data *data;
1340
1341         if (!obj) {
1342                 /* in case we registe the IH before enable ras feature */
1343                 obj = amdgpu_ras_create_obj(adev, &info->head);
1344                 if (!obj)
1345                         return -EINVAL;
1346         } else
1347                 get_obj(obj);
1348
1349         data = &obj->ih_data;
1350         /* add the callback.etc */
1351         *data = (struct ras_ih_data) {
1352                 .inuse = 0,
1353                 .cb = info->cb,
1354                 .element_size = sizeof(struct amdgpu_iv_entry),
1355                 .rptr = 0,
1356                 .wptr = 0,
1357         };
1358
1359         INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1360
1361         data->aligned_element_size = ALIGN(data->element_size, 8);
1362         /* the ring can store 64 iv entries. */
1363         data->ring_size = 64 * data->aligned_element_size;
1364         data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1365         if (!data->ring) {
1366                 put_obj(obj);
1367                 return -ENOMEM;
1368         }
1369
1370         /* IH is ready */
1371         data->inuse = 1;
1372
1373         return 0;
1374 }
1375
1376 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1377 {
1378         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1379         struct ras_manager *obj, *tmp;
1380
1381         list_for_each_entry_safe(obj, tmp, &con->head, node) {
1382                 struct ras_ih_if info = {
1383                         .head = obj->head,
1384                 };
1385                 amdgpu_ras_interrupt_remove_handler(adev, &info);
1386         }
1387
1388         return 0;
1389 }
1390 /* ih end */
1391
1392 /* traversal all IPs except NBIO to query error counter */
1393 static void amdgpu_ras_log_on_err_counter(struct amdgpu_device *adev)
1394 {
1395         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1396         struct ras_manager *obj;
1397
1398         if (!con)
1399                 return;
1400
1401         list_for_each_entry(obj, &con->head, node) {
1402                 struct ras_query_if info = {
1403                         .head = obj->head,
1404                 };
1405
1406                 /*
1407                  * PCIE_BIF IP has one different isr by ras controller
1408                  * interrupt, the specific ras counter query will be
1409                  * done in that isr. So skip such block from common
1410                  * sync flood interrupt isr calling.
1411                  */
1412                 if (info.head.block == AMDGPU_RAS_BLOCK__PCIE_BIF)
1413                         continue;
1414
1415                 amdgpu_ras_error_query(adev, &info);
1416         }
1417 }
1418
1419 /* recovery begin */
1420
1421 /* return 0 on success.
1422  * caller need free bps.
1423  */
1424 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1425                 struct ras_badpage **bps, unsigned int *count)
1426 {
1427         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1428         struct ras_err_handler_data *data;
1429         int i = 0;
1430         int ret = 0;
1431
1432         if (!con || !con->eh_data || !bps || !count)
1433                 return -EINVAL;
1434
1435         mutex_lock(&con->recovery_lock);
1436         data = con->eh_data;
1437         if (!data || data->count == 0) {
1438                 *bps = NULL;
1439                 ret = -EINVAL;
1440                 goto out;
1441         }
1442
1443         *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1444         if (!*bps) {
1445                 ret = -ENOMEM;
1446                 goto out;
1447         }
1448
1449         for (; i < data->count; i++) {
1450                 (*bps)[i] = (struct ras_badpage){
1451                         .bp = data->bps[i].retired_page,
1452                         .size = AMDGPU_GPU_PAGE_SIZE,
1453                         .flags = AMDGPU_RAS_RETIRE_PAGE_RESERVED,
1454                 };
1455
1456                 if (data->last_reserved <= i)
1457                         (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_PENDING;
1458                 else if (data->bps_bo[i] == NULL)
1459                         (*bps)[i].flags = AMDGPU_RAS_RETIRE_PAGE_FAULT;
1460         }
1461
1462         *count = data->count;
1463 out:
1464         mutex_unlock(&con->recovery_lock);
1465         return ret;
1466 }
1467
1468 static void amdgpu_ras_do_recovery(struct work_struct *work)
1469 {
1470         struct amdgpu_ras *ras =
1471                 container_of(work, struct amdgpu_ras, recovery_work);
1472         struct amdgpu_device *remote_adev = NULL;
1473         struct amdgpu_device *adev = ras->adev;
1474         struct list_head device_list, *device_list_handle =  NULL;
1475         struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, false);
1476
1477         /* Build list of devices to query RAS related errors */
1478         if  (hive && adev->gmc.xgmi.num_physical_nodes > 1)
1479                 device_list_handle = &hive->device_list;
1480         else {
1481                 INIT_LIST_HEAD(&device_list);
1482                 list_add_tail(&adev->gmc.xgmi.head, &device_list);
1483                 device_list_handle = &device_list;
1484         }
1485
1486         list_for_each_entry(remote_adev, device_list_handle, gmc.xgmi.head) {
1487                 amdgpu_ras_log_on_err_counter(remote_adev);
1488         }
1489
1490         if (amdgpu_device_should_recover_gpu(ras->adev))
1491                 amdgpu_device_gpu_recover(ras->adev, 0);
1492         atomic_set(&ras->in_recovery, 0);
1493 }
1494
1495 /* alloc/realloc bps array */
1496 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1497                 struct ras_err_handler_data *data, int pages)
1498 {
1499         unsigned int old_space = data->count + data->space_left;
1500         unsigned int new_space = old_space + pages;
1501         unsigned int align_space = ALIGN(new_space, 512);
1502         void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1503         struct amdgpu_bo **bps_bo =
1504                         kmalloc(align_space * sizeof(*data->bps_bo), GFP_KERNEL);
1505
1506         if (!bps || !bps_bo) {
1507                 kfree(bps);
1508                 kfree(bps_bo);
1509                 return -ENOMEM;
1510         }
1511
1512         if (data->bps) {
1513                 memcpy(bps, data->bps,
1514                                 data->count * sizeof(*data->bps));
1515                 kfree(data->bps);
1516         }
1517         if (data->bps_bo) {
1518                 memcpy(bps_bo, data->bps_bo,
1519                                 data->count * sizeof(*data->bps_bo));
1520                 kfree(data->bps_bo);
1521         }
1522
1523         data->bps = bps;
1524         data->bps_bo = bps_bo;
1525         data->space_left += align_space - old_space;
1526         return 0;
1527 }
1528
1529 /* it deal with vram only. */
1530 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1531                 struct eeprom_table_record *bps, int pages)
1532 {
1533         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1534         struct ras_err_handler_data *data;
1535         int ret = 0;
1536
1537         if (!con || !con->eh_data || !bps || pages <= 0)
1538                 return 0;
1539
1540         mutex_lock(&con->recovery_lock);
1541         data = con->eh_data;
1542         if (!data)
1543                 goto out;
1544
1545         if (data->space_left <= pages)
1546                 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1547                         ret = -ENOMEM;
1548                         goto out;
1549                 }
1550
1551         memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
1552         data->count += pages;
1553         data->space_left -= pages;
1554
1555 out:
1556         mutex_unlock(&con->recovery_lock);
1557
1558         return ret;
1559 }
1560
1561 /*
1562  * write error record array to eeprom, the function should be
1563  * protected by recovery_lock
1564  */
1565 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1566 {
1567         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1568         struct ras_err_handler_data *data;
1569         struct amdgpu_ras_eeprom_control *control;
1570         int save_count;
1571
1572         if (!con || !con->eh_data)
1573                 return 0;
1574
1575         control = &con->eeprom_control;
1576         data = con->eh_data;
1577         save_count = data->count - control->num_recs;
1578         /* only new entries are saved */
1579         if (save_count > 0)
1580                 if (amdgpu_ras_eeprom_process_recods(control,
1581                                                         &data->bps[control->num_recs],
1582                                                         true,
1583                                                         save_count)) {
1584                         dev_err(adev->dev, "Failed to save EEPROM table data!");
1585                         return -EIO;
1586                 }
1587
1588         return 0;
1589 }
1590
1591 /*
1592  * read error record array in eeprom and reserve enough space for
1593  * storing new bad pages
1594  */
1595 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1596 {
1597         struct amdgpu_ras_eeprom_control *control =
1598                                         &adev->psp.ras.ras->eeprom_control;
1599         struct eeprom_table_record *bps = NULL;
1600         int ret = 0;
1601
1602         /* no bad page record, skip eeprom access */
1603         if (!control->num_recs)
1604                 return ret;
1605
1606         bps = kcalloc(control->num_recs, sizeof(*bps), GFP_KERNEL);
1607         if (!bps)
1608                 return -ENOMEM;
1609
1610         if (amdgpu_ras_eeprom_process_recods(control, bps, false,
1611                 control->num_recs)) {
1612                 dev_err(adev->dev, "Failed to load EEPROM table records!");
1613                 ret = -EIO;
1614                 goto out;
1615         }
1616
1617         ret = amdgpu_ras_add_bad_pages(adev, bps, control->num_recs);
1618
1619 out:
1620         kfree(bps);
1621         return ret;
1622 }
1623
1624 /*
1625  * check if an address belongs to bad page
1626  *
1627  * Note: this check is only for umc block
1628  */
1629 static bool amdgpu_ras_check_bad_page(struct amdgpu_device *adev,
1630                                 uint64_t addr)
1631 {
1632         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1633         struct ras_err_handler_data *data;
1634         int i;
1635         bool ret = false;
1636
1637         if (!con || !con->eh_data)
1638                 return ret;
1639
1640         mutex_lock(&con->recovery_lock);
1641         data = con->eh_data;
1642         if (!data)
1643                 goto out;
1644
1645         addr >>= AMDGPU_GPU_PAGE_SHIFT;
1646         for (i = 0; i < data->count; i++)
1647                 if (addr == data->bps[i].retired_page) {
1648                         ret = true;
1649                         goto out;
1650                 }
1651
1652 out:
1653         mutex_unlock(&con->recovery_lock);
1654         return ret;
1655 }
1656
1657 /* called in gpu recovery/init */
1658 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1659 {
1660         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1661         struct ras_err_handler_data *data;
1662         uint64_t bp;
1663         struct amdgpu_bo *bo = NULL;
1664         int i, ret = 0;
1665
1666         if (!con || !con->eh_data)
1667                 return 0;
1668
1669         mutex_lock(&con->recovery_lock);
1670         data = con->eh_data;
1671         if (!data)
1672                 goto out;
1673         /* reserve vram at driver post stage. */
1674         for (i = data->last_reserved; i < data->count; i++) {
1675                 bp = data->bps[i].retired_page;
1676
1677                 /* There are two cases of reserve error should be ignored:
1678                  * 1) a ras bad page has been allocated (used by someone);
1679                  * 2) a ras bad page has been reserved (duplicate error injection
1680                  *    for one page);
1681                  */
1682                 if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
1683                                                AMDGPU_GPU_PAGE_SIZE,
1684                                                AMDGPU_GEM_DOMAIN_VRAM,
1685                                                &bo, NULL))
1686                         dev_warn(adev->dev, "RAS WARN: reserve vram for "
1687                                         "retired page %llx fail\n", bp);
1688
1689                 data->bps_bo[i] = bo;
1690                 data->last_reserved = i + 1;
1691                 bo = NULL;
1692         }
1693
1694         /* continue to save bad pages to eeprom even reesrve_vram fails */
1695         ret = amdgpu_ras_save_bad_pages(adev);
1696 out:
1697         mutex_unlock(&con->recovery_lock);
1698         return ret;
1699 }
1700
1701 /* called when driver unload */
1702 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1703 {
1704         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1705         struct ras_err_handler_data *data;
1706         struct amdgpu_bo *bo;
1707         int i;
1708
1709         if (!con || !con->eh_data)
1710                 return 0;
1711
1712         mutex_lock(&con->recovery_lock);
1713         data = con->eh_data;
1714         if (!data)
1715                 goto out;
1716
1717         for (i = data->last_reserved - 1; i >= 0; i--) {
1718                 bo = data->bps_bo[i];
1719
1720                 amdgpu_bo_free_kernel(&bo, NULL, NULL);
1721
1722                 data->bps_bo[i] = bo;
1723                 data->last_reserved = i;
1724         }
1725 out:
1726         mutex_unlock(&con->recovery_lock);
1727         return 0;
1728 }
1729
1730 int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1731 {
1732         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1733         struct ras_err_handler_data **data;
1734         int ret;
1735
1736         if (con)
1737                 data = &con->eh_data;
1738         else
1739                 return 0;
1740
1741         *data = kmalloc(sizeof(**data), GFP_KERNEL | __GFP_ZERO);
1742         if (!*data) {
1743                 ret = -ENOMEM;
1744                 goto out;
1745         }
1746
1747         mutex_init(&con->recovery_lock);
1748         INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1749         atomic_set(&con->in_recovery, 0);
1750         con->adev = adev;
1751
1752         ret = amdgpu_ras_eeprom_init(&con->eeprom_control);
1753         if (ret)
1754                 goto free;
1755
1756         if (con->eeprom_control.num_recs) {
1757                 ret = amdgpu_ras_load_bad_pages(adev);
1758                 if (ret)
1759                         goto free;
1760                 ret = amdgpu_ras_reserve_bad_pages(adev);
1761                 if (ret)
1762                         goto release;
1763         }
1764
1765         return 0;
1766
1767 release:
1768         amdgpu_ras_release_bad_pages(adev);
1769 free:
1770         kfree((*data)->bps);
1771         kfree((*data)->bps_bo);
1772         kfree(*data);
1773         con->eh_data = NULL;
1774 out:
1775         dev_warn(adev->dev, "Failed to initialize ras recovery!\n");
1776
1777         return ret;
1778 }
1779
1780 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1781 {
1782         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1783         struct ras_err_handler_data *data = con->eh_data;
1784
1785         /* recovery_init failed to init it, fini is useless */
1786         if (!data)
1787                 return 0;
1788
1789         cancel_work_sync(&con->recovery_work);
1790         amdgpu_ras_release_bad_pages(adev);
1791
1792         mutex_lock(&con->recovery_lock);
1793         con->eh_data = NULL;
1794         kfree(data->bps);
1795         kfree(data->bps_bo);
1796         kfree(data);
1797         mutex_unlock(&con->recovery_lock);
1798
1799         return 0;
1800 }
1801 /* recovery end */
1802
1803 /* return 0 if ras will reset gpu and repost.*/
1804 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1805                 unsigned int block)
1806 {
1807         struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1808
1809         if (!ras)
1810                 return -EINVAL;
1811
1812         ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1813         return 0;
1814 }
1815
1816 /*
1817  * check hardware's ras ability which will be saved in hw_supported.
1818  * if hardware does not support ras, we can skip some ras initializtion and
1819  * forbid some ras operations from IP.
1820  * if software itself, say boot parameter, limit the ras ability. We still
1821  * need allow IP do some limited operations, like disable. In such case,
1822  * we have to initialize ras as normal. but need check if operation is
1823  * allowed or not in each function.
1824  */
1825 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1826                 uint32_t *hw_supported, uint32_t *supported)
1827 {
1828         *hw_supported = 0;
1829         *supported = 0;
1830
1831         if (amdgpu_sriov_vf(adev) || !adev->is_atom_fw ||
1832             (adev->asic_type != CHIP_VEGA20 &&
1833              adev->asic_type != CHIP_ARCTURUS))
1834                 return;
1835
1836         if (amdgpu_atomfirmware_mem_ecc_supported(adev)) {
1837                 dev_info(adev->dev, "HBM ECC is active.\n");
1838                 *hw_supported |= (1 << AMDGPU_RAS_BLOCK__UMC |
1839                                 1 << AMDGPU_RAS_BLOCK__DF);
1840         } else
1841                 dev_info(adev->dev, "HBM ECC is not presented.\n");
1842
1843         if (amdgpu_atomfirmware_sram_ecc_supported(adev)) {
1844                 dev_info(adev->dev, "SRAM ECC is active.\n");
1845                 *hw_supported |= ~(1 << AMDGPU_RAS_BLOCK__UMC |
1846                                 1 << AMDGPU_RAS_BLOCK__DF);
1847         } else
1848                 dev_info(adev->dev, "SRAM ECC is not presented.\n");
1849
1850         /* hw_supported needs to be aligned with RAS block mask. */
1851         *hw_supported &= AMDGPU_RAS_BLOCK_MASK;
1852
1853         *supported = amdgpu_ras_enable == 0 ?
1854                         0 : *hw_supported & amdgpu_ras_mask;
1855 }
1856
1857 int amdgpu_ras_init(struct amdgpu_device *adev)
1858 {
1859         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1860         int r;
1861
1862         if (con)
1863                 return 0;
1864
1865         con = kmalloc(sizeof(struct amdgpu_ras) +
1866                         sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1867                         GFP_KERNEL|__GFP_ZERO);
1868         if (!con)
1869                 return -ENOMEM;
1870
1871         con->objs = (struct ras_manager *)(con + 1);
1872
1873         amdgpu_ras_set_context(adev, con);
1874
1875         amdgpu_ras_check_supported(adev, &con->hw_supported,
1876                         &con->supported);
1877         if (!con->hw_supported) {
1878                 amdgpu_ras_set_context(adev, NULL);
1879                 kfree(con);
1880                 return 0;
1881         }
1882
1883         con->features = 0;
1884         INIT_LIST_HEAD(&con->head);
1885         /* Might need get this flag from vbios. */
1886         con->flags = RAS_DEFAULT_FLAGS;
1887
1888         if (adev->nbio.funcs->init_ras_controller_interrupt) {
1889                 r = adev->nbio.funcs->init_ras_controller_interrupt(adev);
1890                 if (r)
1891                         return r;
1892         }
1893
1894         if (adev->nbio.funcs->init_ras_err_event_athub_interrupt) {
1895                 r = adev->nbio.funcs->init_ras_err_event_athub_interrupt(adev);
1896                 if (r)
1897                         return r;
1898         }
1899
1900         amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1901
1902         if (amdgpu_ras_fs_init(adev))
1903                 goto fs_out;
1904
1905         dev_info(adev->dev, "RAS INFO: ras initialized successfully, "
1906                         "hardware ability[%x] ras_mask[%x]\n",
1907                         con->hw_supported, con->supported);
1908         return 0;
1909 fs_out:
1910         amdgpu_ras_set_context(adev, NULL);
1911         kfree(con);
1912
1913         return -EINVAL;
1914 }
1915
1916 /* helper function to handle common stuff in ip late init phase */
1917 int amdgpu_ras_late_init(struct amdgpu_device *adev,
1918                          struct ras_common_if *ras_block,
1919                          struct ras_fs_if *fs_info,
1920                          struct ras_ih_if *ih_info)
1921 {
1922         int r;
1923
1924         /* disable RAS feature per IP block if it is not supported */
1925         if (!amdgpu_ras_is_supported(adev, ras_block->block)) {
1926                 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
1927                 return 0;
1928         }
1929
1930         r = amdgpu_ras_feature_enable_on_boot(adev, ras_block, 1);
1931         if (r) {
1932                 if (r == -EAGAIN) {
1933                         /* request gpu reset. will run again */
1934                         amdgpu_ras_request_reset_on_boot(adev,
1935                                         ras_block->block);
1936                         return 0;
1937                 } else if (adev->in_suspend || adev->in_gpu_reset) {
1938                         /* in resume phase, if fail to enable ras,
1939                          * clean up all ras fs nodes, and disable ras */
1940                         goto cleanup;
1941                 } else
1942                         return r;
1943         }
1944
1945         /* in resume phase, no need to create ras fs node */
1946         if (adev->in_suspend || adev->in_gpu_reset)
1947                 return 0;
1948
1949         if (ih_info->cb) {
1950                 r = amdgpu_ras_interrupt_add_handler(adev, ih_info);
1951                 if (r)
1952                         goto interrupt;
1953         }
1954
1955         r = amdgpu_ras_sysfs_create(adev, fs_info);
1956         if (r)
1957                 goto sysfs;
1958
1959         return 0;
1960 cleanup:
1961         amdgpu_ras_sysfs_remove(adev, ras_block);
1962 sysfs:
1963         if (ih_info->cb)
1964                 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1965 interrupt:
1966         amdgpu_ras_feature_enable(adev, ras_block, 0);
1967         return r;
1968 }
1969
1970 /* helper function to remove ras fs node and interrupt handler */
1971 void amdgpu_ras_late_fini(struct amdgpu_device *adev,
1972                           struct ras_common_if *ras_block,
1973                           struct ras_ih_if *ih_info)
1974 {
1975         if (!ras_block || !ih_info)
1976                 return;
1977
1978         amdgpu_ras_sysfs_remove(adev, ras_block);
1979         if (ih_info->cb)
1980                 amdgpu_ras_interrupt_remove_handler(adev, ih_info);
1981         amdgpu_ras_feature_enable(adev, ras_block, 0);
1982 }
1983
1984 /* do some init work after IP late init as dependence.
1985  * and it runs in resume/gpu reset/booting up cases.
1986  */
1987 void amdgpu_ras_resume(struct amdgpu_device *adev)
1988 {
1989         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1990         struct ras_manager *obj, *tmp;
1991
1992         if (!con)
1993                 return;
1994
1995         if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1996                 /* Set up all other IPs which are not implemented. There is a
1997                  * tricky thing that IP's actual ras error type should be
1998                  * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1999                  * ERROR_NONE make sense anyway.
2000                  */
2001                 amdgpu_ras_enable_all_features(adev, 1);
2002
2003                 /* We enable ras on all hw_supported block, but as boot
2004                  * parameter might disable some of them and one or more IP has
2005                  * not implemented yet. So we disable them on behalf.
2006                  */
2007                 list_for_each_entry_safe(obj, tmp, &con->head, node) {
2008                         if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
2009                                 amdgpu_ras_feature_enable(adev, &obj->head, 0);
2010                                 /* there should be no any reference. */
2011                                 WARN_ON(alive_obj(obj));
2012                         }
2013                 }
2014         }
2015
2016         if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
2017                 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
2018                 /* setup ras obj state as disabled.
2019                  * for init_by_vbios case.
2020                  * if we want to enable ras, just enable it in a normal way.
2021                  * If we want do disable it, need setup ras obj as enabled,
2022                  * then issue another TA disable cmd.
2023                  * See feature_enable_on_boot
2024                  */
2025                 amdgpu_ras_disable_all_features(adev, 1);
2026                 amdgpu_ras_reset_gpu(adev);
2027         }
2028 }
2029
2030 void amdgpu_ras_suspend(struct amdgpu_device *adev)
2031 {
2032         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2033
2034         if (!con)
2035                 return;
2036
2037         amdgpu_ras_disable_all_features(adev, 0);
2038         /* Make sure all ras objects are disabled. */
2039         if (con->features)
2040                 amdgpu_ras_disable_all_features(adev, 1);
2041 }
2042
2043 /* do some fini work before IP fini as dependence */
2044 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
2045 {
2046         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2047
2048         if (!con)
2049                 return 0;
2050
2051         /* Need disable ras on all IPs here before ip [hw/sw]fini */
2052         amdgpu_ras_disable_all_features(adev, 0);
2053         amdgpu_ras_recovery_fini(adev);
2054         return 0;
2055 }
2056
2057 int amdgpu_ras_fini(struct amdgpu_device *adev)
2058 {
2059         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
2060
2061         if (!con)
2062                 return 0;
2063
2064         amdgpu_ras_fs_fini(adev);
2065         amdgpu_ras_interrupt_remove_all(adev);
2066
2067         WARN(con->features, "Feature mask is not cleared");
2068
2069         if (con->features)
2070                 amdgpu_ras_disable_all_features(adev, 1);
2071
2072         amdgpu_ras_set_context(adev, NULL);
2073         kfree(con);
2074
2075         return 0;
2076 }
2077
2078 void amdgpu_ras_global_ras_isr(struct amdgpu_device *adev)
2079 {
2080         uint32_t hw_supported, supported;
2081
2082         amdgpu_ras_check_supported(adev, &hw_supported, &supported);
2083         if (!hw_supported)
2084                 return;
2085
2086         if (atomic_cmpxchg(&amdgpu_ras_in_intr, 0, 1) == 0) {
2087                 dev_info(adev->dev, "uncorrectable hardware error"
2088                         "(ERREVENT_ATHUB_INTERRUPT) detected!\n");
2089
2090                 amdgpu_ras_reset_gpu(adev);
2091         }
2092 }
This page took 0.159645 seconds and 4 git commands to generate.