]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
5ece0671f108c4206061aa2b5f6ffc1ef8121e5a
[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 "amdgpu.h"
28 #include "amdgpu_ras.h"
29 #include "amdgpu_atomfirmware.h"
30
31 struct ras_ih_data {
32         /* interrupt bottom half */
33         struct work_struct ih_work;
34         int inuse;
35         /* IP callback */
36         ras_ih_cb cb;
37         /* full of entries */
38         unsigned char *ring;
39         unsigned int ring_size;
40         unsigned int element_size;
41         unsigned int aligned_element_size;
42         unsigned int rptr;
43         unsigned int wptr;
44 };
45
46 struct ras_fs_data {
47         char sysfs_name[32];
48         char debugfs_name[32];
49 };
50
51 struct ras_err_data {
52         unsigned long ue_count;
53         unsigned long ce_count;
54 };
55
56 struct ras_err_handler_data {
57         /* point to bad pages array */
58         struct {
59                 unsigned long bp;
60                 struct amdgpu_bo *bo;
61         } *bps;
62         /* the count of entries */
63         int count;
64         /* the space can place new entries */
65         int space_left;
66         /* last reserved entry's index + 1 */
67         int last_reserved;
68 };
69
70 struct ras_manager {
71         struct ras_common_if head;
72         /* reference count */
73         int use;
74         /* ras block link */
75         struct list_head node;
76         /* the device */
77         struct amdgpu_device *adev;
78         /* debugfs */
79         struct dentry *ent;
80         /* sysfs */
81         struct device_attribute sysfs_attr;
82         int attr_inuse;
83
84         /* fs node name */
85         struct ras_fs_data fs_data;
86
87         /* IH data */
88         struct ras_ih_data ih_data;
89
90         struct ras_err_data err_data;
91 };
92
93 const char *ras_error_string[] = {
94         "none",
95         "parity",
96         "single_correctable",
97         "multi_uncorrectable",
98         "poison",
99 };
100
101 const char *ras_block_string[] = {
102         "umc",
103         "sdma",
104         "gfx",
105         "mmhub",
106         "athub",
107         "pcie_bif",
108         "hdp",
109         "xgmi_wafl",
110         "df",
111         "smn",
112         "sem",
113         "mp0",
114         "mp1",
115         "fuse",
116 };
117
118 #define ras_err_str(i) (ras_error_string[ffs(i)])
119 #define ras_block_str(i) (ras_block_string[i])
120
121 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
122 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
123
124 static void amdgpu_ras_self_test(struct amdgpu_device *adev)
125 {
126         /* TODO */
127 }
128
129 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
130                                         size_t size, loff_t *pos)
131 {
132         struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
133         struct ras_query_if info = {
134                 .head = obj->head,
135         };
136         ssize_t s;
137         char val[128];
138
139         if (amdgpu_ras_error_query(obj->adev, &info))
140                 return -EINVAL;
141
142         s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
143                         "ue", info.ue_count,
144                         "ce", info.ce_count);
145         if (*pos >= s)
146                 return 0;
147
148         s -= *pos;
149         s = min_t(u64, s, size);
150
151
152         if (copy_to_user(buf, &val[*pos], s))
153                 return -EINVAL;
154
155         *pos += s;
156
157         return s;
158 }
159
160 static ssize_t amdgpu_ras_debugfs_write(struct file *f, const char __user *buf,
161                 size_t size, loff_t *pos)
162 {
163         struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
164         struct ras_inject_if info = {
165                 .head = obj->head,
166         };
167         ssize_t s = min_t(u64, 64, size);
168         char val[64];
169         char *str = val;
170         memset(val, 0, sizeof(val));
171
172         if (*pos)
173                 return -EINVAL;
174
175         if (copy_from_user(str, buf, s))
176                 return -EINVAL;
177
178         /* only care ue/ce for now. */
179         if (memcmp(str, "ue", 2) == 0) {
180                 info.head.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
181                 str += 2;
182         } else if (memcmp(str, "ce", 2) == 0) {
183                 info.head.type = AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
184                 str += 2;
185         }
186
187         if (sscanf(str, "0x%llx 0x%llx", &info.address, &info.value) != 2) {
188                 if (sscanf(str, "%llu %llu", &info.address, &info.value) != 2)
189                         return -EINVAL;
190         }
191
192         *pos = s;
193
194         if (amdgpu_ras_error_inject(obj->adev, &info))
195                 return -EINVAL;
196
197         return size;
198 }
199
200 static const struct file_operations amdgpu_ras_debugfs_ops = {
201         .owner = THIS_MODULE,
202         .read = amdgpu_ras_debugfs_read,
203         .write = amdgpu_ras_debugfs_write,
204         .llseek = default_llseek
205 };
206
207 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
208 {
209         int i;
210
211         for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
212                 *block_id = i;
213                 if (strcmp(name, ras_block_str(i)) == 0)
214                         return 0;
215         }
216         return -EINVAL;
217 }
218
219 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
220                 const char __user *buf, size_t size,
221                 loff_t *pos, struct ras_debug_if *data)
222 {
223         ssize_t s = min_t(u64, 64, size);
224         char str[65];
225         char block_name[33];
226         char err[9] = "ue";
227         int op = -1;
228         int block_id;
229         u64 address, value;
230
231         if (*pos)
232                 return -EINVAL;
233         *pos = size;
234
235         memset(str, 0, sizeof(str));
236         memset(data, 0, sizeof(*data));
237
238         if (copy_from_user(str, buf, s))
239                 return -EINVAL;
240
241         if (sscanf(str, "disable %32s", block_name) == 1)
242                 op = 0;
243         else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
244                 op = 1;
245         else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
246                 op = 2;
247         else if (str[0] && str[1] && str[2] && str[3])
248                 /* ascii string, but commands are not matched. */
249                 return -EINVAL;
250
251         if (op != -1) {
252                 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
253                         return -EINVAL;
254
255                 data->head.block = block_id;
256                 data->head.type = memcmp("ue", err, 2) == 0 ?
257                         AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
258                         AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
259                 data->op = op;
260
261                 if (op == 2) {
262                         if (sscanf(str, "%*s %*s %*s %llu %llu",
263                                                 &address, &value) != 2)
264                                 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
265                                                         &address, &value) != 2)
266                                         return -EINVAL;
267                         data->inject.address = address;
268                         data->inject.value = value;
269                 }
270         } else {
271                 if (size < sizeof(*data))
272                         return -EINVAL;
273
274                 if (copy_from_user(data, buf, sizeof(*data)))
275                         return -EINVAL;
276         }
277
278         return 0;
279 }
280 /*
281  * DOC: ras debugfs control interface
282  *
283  * It accepts struct ras_debug_if who has two members.
284  *
285  * First member: ras_debug_if::head or ras_debug_if::inject.
286  *
287  * head is used to indicate which IP block will be under control.
288  *
289  * head has four members, they are block, type, sub_block_index, name.
290  * block: which IP will be under control.
291  * type: what kind of error will be enabled/disabled/injected.
292  * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
293  * name: the name of IP.
294  *
295  * inject has two more members than head, they are address, value.
296  * As their names indicate, inject operation will write the
297  * value to the address.
298  *
299  * Second member: struct ras_debug_if::op.
300  * It has three kinds of operations.
301  *  0: disable RAS on the block. Take ::head as its data.
302  *  1: enable RAS on the block. Take ::head as its data.
303  *  2: inject errors on the block. Take ::inject as its data.
304  *
305  * How to use the interface?
306  * programs:
307  * copy the struct ras_debug_if in your codes and initialize it.
308  * write the struct to the control node.
309  *
310  * bash:
311  * echo op block [error [address value]] > .../ras/ras_ctrl
312  *      op: disable, enable, inject
313  *              disable: only block is needed
314  *              enable: block and error are needed
315  *              inject: error, address, value are needed
316  *      block: umc, smda, gfx, .........
317  *              see ras_block_string[] for details
318  *      error: ue, ce
319  *              ue: multi_uncorrectable
320  *              ce: single_correctable
321  *
322  * here are some examples for bash commands,
323  *      echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
324  *      echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
325  *      echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
326  *
327  * How to check the result?
328  *
329  * For disable/enable, please check ras features at
330  * /sys/class/drm/card[0/1/2...]/device/ras/features
331  *
332  * For inject, please check corresponding err count at
333  * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
334  *
335  * NOTE: operation is only allowed on blocks which are supported.
336  * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
337  */
338 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
339                 size_t size, loff_t *pos)
340 {
341         struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
342         struct ras_debug_if data;
343         int ret = 0;
344
345         ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
346         if (ret)
347                 return -EINVAL;
348
349         if (!amdgpu_ras_is_supported(adev, data.head.block))
350                 return -EINVAL;
351
352         switch (data.op) {
353         case 0:
354                 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
355                 break;
356         case 1:
357                 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
358                 break;
359         case 2:
360                 ret = amdgpu_ras_error_inject(adev, &data.inject);
361                 break;
362         default:
363                 ret = -EINVAL;
364                 break;
365         };
366
367         if (ret)
368                 return -EINVAL;
369
370         return size;
371 }
372
373 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
374         .owner = THIS_MODULE,
375         .read = NULL,
376         .write = amdgpu_ras_debugfs_ctrl_write,
377         .llseek = default_llseek
378 };
379
380 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
381                 struct device_attribute *attr, char *buf)
382 {
383         struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
384         struct ras_query_if info = {
385                 .head = obj->head,
386         };
387
388         if (amdgpu_ras_error_query(obj->adev, &info))
389                 return -EINVAL;
390
391         return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
392                         "ue", info.ue_count,
393                         "ce", info.ce_count);
394 }
395
396 /* obj begin */
397
398 #define get_obj(obj) do { (obj)->use++; } while (0)
399 #define alive_obj(obj) ((obj)->use)
400
401 static inline void put_obj(struct ras_manager *obj)
402 {
403         if (obj && --obj->use == 0)
404                 list_del(&obj->node);
405         if (obj && obj->use < 0) {
406                  DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
407         }
408 }
409
410 /* make one obj and return it. */
411 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
412                 struct ras_common_if *head)
413 {
414         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
415         struct ras_manager *obj;
416
417         if (!con)
418                 return NULL;
419
420         if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
421                 return NULL;
422
423         obj = &con->objs[head->block];
424         /* already exist. return obj? */
425         if (alive_obj(obj))
426                 return NULL;
427
428         obj->head = *head;
429         obj->adev = adev;
430         list_add(&obj->node, &con->head);
431         get_obj(obj);
432
433         return obj;
434 }
435
436 /* return an obj equal to head, or the first when head is NULL */
437 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
438                 struct ras_common_if *head)
439 {
440         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
441         struct ras_manager *obj;
442         int i;
443
444         if (!con)
445                 return NULL;
446
447         if (head) {
448                 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
449                         return NULL;
450
451                 obj = &con->objs[head->block];
452
453                 if (alive_obj(obj)) {
454                         WARN_ON(head->block != obj->head.block);
455                         return obj;
456                 }
457         } else {
458                 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
459                         obj = &con->objs[i];
460                         if (alive_obj(obj)) {
461                                 WARN_ON(i != obj->head.block);
462                                 return obj;
463                         }
464                 }
465         }
466
467         return NULL;
468 }
469 /* obj end */
470
471 /* feature ctl begin */
472 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
473                 struct ras_common_if *head)
474 {
475         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
476
477         return con->hw_supported & BIT(head->block);
478 }
479
480 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
481                 struct ras_common_if *head)
482 {
483         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
484
485         return con->features & BIT(head->block);
486 }
487
488 /*
489  * if obj is not created, then create one.
490  * set feature enable flag.
491  */
492 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
493                 struct ras_common_if *head, int enable)
494 {
495         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
496         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
497
498         /* If hardware does not support ras, then do not create obj.
499          * But if hardware support ras, we can create the obj.
500          * Ras framework checks con->hw_supported to see if it need do
501          * corresponding initialization.
502          * IP checks con->support to see if it need disable ras.
503          */
504         if (!amdgpu_ras_is_feature_allowed(adev, head))
505                 return 0;
506         if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
507                 return 0;
508
509         if (enable) {
510                 if (!obj) {
511                         obj = amdgpu_ras_create_obj(adev, head);
512                         if (!obj)
513                                 return -EINVAL;
514                 } else {
515                         /* In case we create obj somewhere else */
516                         get_obj(obj);
517                 }
518                 con->features |= BIT(head->block);
519         } else {
520                 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
521                         con->features &= ~BIT(head->block);
522                         put_obj(obj);
523                 }
524         }
525
526         return 0;
527 }
528
529 /* wrapper of psp_ras_enable_features */
530 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
531                 struct ras_common_if *head, bool enable)
532 {
533         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
534         union ta_ras_cmd_input info;
535         int ret;
536
537         if (!con)
538                 return -EINVAL;
539
540         if (!enable) {
541                 info.disable_features = (struct ta_ras_disable_features_input) {
542                         .block_id =  head->block,
543                         .error_type = head->type,
544                 };
545         } else {
546                 info.enable_features = (struct ta_ras_enable_features_input) {
547                         .block_id =  head->block,
548                         .error_type = head->type,
549                 };
550         }
551
552         /* Do not enable if it is not allowed. */
553         WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
554         /* Are we alerady in that state we are going to set? */
555         if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
556                 return 0;
557
558         ret = psp_ras_enable_features(&adev->psp, &info, enable);
559         if (ret) {
560                 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
561                                 enable ? "enable":"disable",
562                                 ras_block_str(head->block),
563                                 ret);
564                 return -EINVAL;
565         }
566
567         /* setup the obj */
568         __amdgpu_ras_feature_enable(adev, head, enable);
569
570         return 0;
571 }
572
573 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
574                 bool bypass)
575 {
576         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
577         struct ras_manager *obj, *tmp;
578
579         list_for_each_entry_safe(obj, tmp, &con->head, node) {
580                 /* bypass psp.
581                  * aka just release the obj and corresponding flags
582                  */
583                 if (bypass) {
584                         if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
585                                 break;
586                 } else {
587                         if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
588                                 break;
589                 }
590         }
591
592         return con->features;
593 }
594
595 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
596                 bool bypass)
597 {
598         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
599         int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
600         int i;
601
602         for (i = 0; i < ras_block_count; i++) {
603                 struct ras_common_if head = {
604                         .block = i,
605                         .type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE,
606                         .sub_block_index = 0,
607                 };
608                 strcpy(head.name, ras_block_str(i));
609                 if (bypass) {
610                         /*
611                          * bypass psp. vbios enable ras for us.
612                          * so just create the obj
613                          */
614                         if (__amdgpu_ras_feature_enable(adev, &head, 1))
615                                 break;
616                 } else {
617                         if (amdgpu_ras_feature_enable(adev, &head, 1))
618                                 break;
619                 }
620         }
621
622         return con->features;
623 }
624 /* feature ctl end */
625
626 /* query/inject/cure begin */
627 int amdgpu_ras_error_query(struct amdgpu_device *adev,
628                 struct ras_query_if *info)
629 {
630         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
631
632         if (!obj)
633                 return -EINVAL;
634         /* TODO might read the register to read the count */
635
636         info->ue_count = obj->err_data.ue_count;
637         info->ce_count = obj->err_data.ce_count;
638
639         return 0;
640 }
641
642 /* wrapper of psp_ras_trigger_error */
643 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
644                 struct ras_inject_if *info)
645 {
646         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
647         struct ta_ras_trigger_error_input block_info = {
648                 .block_id = info->head.block,
649                 .inject_error_type = info->head.type,
650                 .sub_block_index = info->head.sub_block_index,
651                 .address = info->address,
652                 .value = info->value,
653         };
654         int ret = 0;
655
656         if (!obj)
657                 return -EINVAL;
658
659         ret = psp_ras_trigger_error(&adev->psp, &block_info);
660         if (ret)
661                 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
662                                 ras_block_str(info->head.block),
663                                 ret);
664
665         return ret;
666 }
667
668 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
669                 struct ras_cure_if *info)
670 {
671         /* psp fw has no cure interface for now. */
672         return 0;
673 }
674
675 /* get the total error counts on all IPs */
676 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
677                 bool is_ce)
678 {
679         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
680         struct ras_manager *obj;
681         struct ras_err_data data = {0, 0};
682
683         if (!con)
684                 return -EINVAL;
685
686         list_for_each_entry(obj, &con->head, node) {
687                 struct ras_query_if info = {
688                         .head = obj->head,
689                 };
690
691                 if (amdgpu_ras_error_query(adev, &info))
692                         return -EINVAL;
693
694                 data.ce_count += info.ce_count;
695                 data.ue_count += info.ue_count;
696         }
697
698         return is_ce ? data.ce_count : data.ue_count;
699 }
700 /* query/inject/cure end */
701
702
703 /* sysfs begin */
704
705 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
706                 struct device_attribute *attr, char *buf)
707 {
708         struct amdgpu_ras *con =
709                 container_of(attr, struct amdgpu_ras, features_attr);
710         struct drm_device *ddev = dev_get_drvdata(dev);
711         struct amdgpu_device *adev = ddev->dev_private;
712         struct ras_common_if head;
713         int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
714         int i;
715         ssize_t s;
716         struct ras_manager *obj;
717
718         s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
719
720         for (i = 0; i < ras_block_count; i++) {
721                 head.block = i;
722
723                 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
724                         obj = amdgpu_ras_find_obj(adev, &head);
725                         s += scnprintf(&buf[s], PAGE_SIZE - s,
726                                         "%s: %s\n",
727                                         ras_block_str(i),
728                                         ras_err_str(obj->head.type));
729                 } else
730                         s += scnprintf(&buf[s], PAGE_SIZE - s,
731                                         "%s: disabled\n",
732                                         ras_block_str(i));
733         }
734
735         return s;
736 }
737
738 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
739 {
740         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
741         struct attribute *attrs[] = {
742                 &con->features_attr.attr,
743                 NULL
744         };
745         struct attribute_group group = {
746                 .name = "ras",
747                 .attrs = attrs,
748         };
749
750         con->features_attr = (struct device_attribute) {
751                 .attr = {
752                         .name = "features",
753                         .mode = S_IRUGO,
754                 },
755                         .show = amdgpu_ras_sysfs_features_read,
756         };
757         sysfs_attr_init(attrs[0]);
758
759         return sysfs_create_group(&adev->dev->kobj, &group);
760 }
761
762 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
763 {
764         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
765         struct attribute *attrs[] = {
766                 &con->features_attr.attr,
767                 NULL
768         };
769         struct attribute_group group = {
770                 .name = "ras",
771                 .attrs = attrs,
772         };
773
774         sysfs_remove_group(&adev->dev->kobj, &group);
775
776         return 0;
777 }
778
779 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
780                 struct ras_fs_if *head)
781 {
782         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
783
784         if (!obj || obj->attr_inuse)
785                 return -EINVAL;
786
787         get_obj(obj);
788
789         memcpy(obj->fs_data.sysfs_name,
790                         head->sysfs_name,
791                         sizeof(obj->fs_data.sysfs_name));
792
793         obj->sysfs_attr = (struct device_attribute){
794                 .attr = {
795                         .name = obj->fs_data.sysfs_name,
796                         .mode = S_IRUGO,
797                 },
798                         .show = amdgpu_ras_sysfs_read,
799         };
800         sysfs_attr_init(&obj->sysfs_attr.attr);
801
802         if (sysfs_add_file_to_group(&adev->dev->kobj,
803                                 &obj->sysfs_attr.attr,
804                                 "ras")) {
805                 put_obj(obj);
806                 return -EINVAL;
807         }
808
809         obj->attr_inuse = 1;
810
811         return 0;
812 }
813
814 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
815                 struct ras_common_if *head)
816 {
817         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
818
819         if (!obj || !obj->attr_inuse)
820                 return -EINVAL;
821
822         sysfs_remove_file_from_group(&adev->dev->kobj,
823                                 &obj->sysfs_attr.attr,
824                                 "ras");
825         obj->attr_inuse = 0;
826         put_obj(obj);
827
828         return 0;
829 }
830
831 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
832 {
833         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
834         struct ras_manager *obj, *tmp;
835
836         list_for_each_entry_safe(obj, tmp, &con->head, node) {
837                 amdgpu_ras_sysfs_remove(adev, &obj->head);
838         }
839
840         amdgpu_ras_sysfs_remove_feature_node(adev);
841
842         return 0;
843 }
844 /* sysfs end */
845
846 /* debugfs begin */
847 static int amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
848 {
849         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
850         struct drm_minor *minor = adev->ddev->primary;
851         struct dentry *root = minor->debugfs_root, *dir;
852         struct dentry *ent;
853
854         dir = debugfs_create_dir("ras", root);
855         if (IS_ERR(dir))
856                 return -EINVAL;
857
858         con->dir = dir;
859
860         ent = debugfs_create_file("ras_ctrl",
861                         S_IWUGO | S_IRUGO, con->dir,
862                         adev, &amdgpu_ras_debugfs_ctrl_ops);
863         if (IS_ERR(ent)) {
864                 debugfs_remove(con->dir);
865                 return -EINVAL;
866         }
867
868         con->ent = ent;
869         return 0;
870 }
871
872 int amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
873                 struct ras_fs_if *head)
874 {
875         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
876         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
877         struct dentry *ent;
878
879         if (!obj || obj->ent)
880                 return -EINVAL;
881
882         get_obj(obj);
883
884         memcpy(obj->fs_data.debugfs_name,
885                         head->debugfs_name,
886                         sizeof(obj->fs_data.debugfs_name));
887
888         ent = debugfs_create_file(obj->fs_data.debugfs_name,
889                         S_IWUGO | S_IRUGO, con->dir,
890                         obj, &amdgpu_ras_debugfs_ops);
891
892         if (IS_ERR(ent))
893                 return -EINVAL;
894
895         obj->ent = ent;
896
897         return 0;
898 }
899
900 int amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
901                 struct ras_common_if *head)
902 {
903         struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
904
905         if (!obj || !obj->ent)
906                 return 0;
907
908         debugfs_remove(obj->ent);
909         obj->ent = NULL;
910         put_obj(obj);
911
912         return 0;
913 }
914
915 static int amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
916 {
917         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
918         struct ras_manager *obj, *tmp;
919
920         list_for_each_entry_safe(obj, tmp, &con->head, node) {
921                 amdgpu_ras_debugfs_remove(adev, &obj->head);
922         }
923
924         debugfs_remove(con->ent);
925         debugfs_remove(con->dir);
926         con->dir = NULL;
927         con->ent = NULL;
928
929         return 0;
930 }
931 /* debugfs end */
932
933 /* ras fs */
934
935 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
936 {
937         amdgpu_ras_sysfs_create_feature_node(adev);
938         amdgpu_ras_debugfs_create_ctrl_node(adev);
939
940         return 0;
941 }
942
943 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
944 {
945         amdgpu_ras_debugfs_remove_all(adev);
946         amdgpu_ras_sysfs_remove_all(adev);
947         return 0;
948 }
949 /* ras fs end */
950
951 /* ih begin */
952 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
953 {
954         struct ras_ih_data *data = &obj->ih_data;
955         struct amdgpu_iv_entry entry;
956         int ret;
957
958         while (data->rptr != data->wptr) {
959                 rmb();
960                 memcpy(&entry, &data->ring[data->rptr],
961                                 data->element_size);
962
963                 wmb();
964                 data->rptr = (data->aligned_element_size +
965                                 data->rptr) % data->ring_size;
966
967                 /* Let IP handle its data, maybe we need get the output
968                  * from the callback to udpate the error type/count, etc
969                  */
970                 if (data->cb) {
971                         ret = data->cb(obj->adev, &entry);
972                         /* ue will trigger an interrupt, and in that case
973                          * we need do a reset to recovery the whole system.
974                          * But leave IP do that recovery, here we just dispatch
975                          * the error.
976                          */
977                         if (ret == AMDGPU_RAS_UE) {
978                                 obj->err_data.ue_count++;
979                         }
980                         /* Might need get ce count by register, but not all IP
981                          * saves ce count, some IP just use one bit or two bits
982                          * to indicate ce happened.
983                          */
984                 }
985         }
986 }
987
988 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
989 {
990         struct ras_ih_data *data =
991                 container_of(work, struct ras_ih_data, ih_work);
992         struct ras_manager *obj =
993                 container_of(data, struct ras_manager, ih_data);
994
995         amdgpu_ras_interrupt_handler(obj);
996 }
997
998 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
999                 struct ras_dispatch_if *info)
1000 {
1001         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1002         struct ras_ih_data *data = &obj->ih_data;
1003
1004         if (!obj)
1005                 return -EINVAL;
1006
1007         if (data->inuse == 0)
1008                 return 0;
1009
1010         /* Might be overflow... */
1011         memcpy(&data->ring[data->wptr], info->entry,
1012                         data->element_size);
1013
1014         wmb();
1015         data->wptr = (data->aligned_element_size +
1016                         data->wptr) % data->ring_size;
1017
1018         schedule_work(&data->ih_work);
1019
1020         return 0;
1021 }
1022
1023 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1024                 struct ras_ih_if *info)
1025 {
1026         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1027         struct ras_ih_data *data;
1028
1029         if (!obj)
1030                 return -EINVAL;
1031
1032         data = &obj->ih_data;
1033         if (data->inuse == 0)
1034                 return 0;
1035
1036         cancel_work_sync(&data->ih_work);
1037
1038         kfree(data->ring);
1039         memset(data, 0, sizeof(*data));
1040         put_obj(obj);
1041
1042         return 0;
1043 }
1044
1045 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1046                 struct ras_ih_if *info)
1047 {
1048         struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1049         struct ras_ih_data *data;
1050
1051         if (!obj) {
1052                 /* in case we registe the IH before enable ras feature */
1053                 obj = amdgpu_ras_create_obj(adev, &info->head);
1054                 if (!obj)
1055                         return -EINVAL;
1056         } else
1057                 get_obj(obj);
1058
1059         data = &obj->ih_data;
1060         /* add the callback.etc */
1061         *data = (struct ras_ih_data) {
1062                 .inuse = 0,
1063                 .cb = info->cb,
1064                 .element_size = sizeof(struct amdgpu_iv_entry),
1065                 .rptr = 0,
1066                 .wptr = 0,
1067         };
1068
1069         INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1070
1071         data->aligned_element_size = ALIGN(data->element_size, 8);
1072         /* the ring can store 64 iv entries. */
1073         data->ring_size = 64 * data->aligned_element_size;
1074         data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1075         if (!data->ring) {
1076                 put_obj(obj);
1077                 return -ENOMEM;
1078         }
1079
1080         /* IH is ready */
1081         data->inuse = 1;
1082
1083         return 0;
1084 }
1085
1086 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1087 {
1088         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1089         struct ras_manager *obj, *tmp;
1090
1091         list_for_each_entry_safe(obj, tmp, &con->head, node) {
1092                 struct ras_ih_if info = {
1093                         .head = obj->head,
1094                 };
1095                 amdgpu_ras_interrupt_remove_handler(adev, &info);
1096         }
1097
1098         return 0;
1099 }
1100 /* ih end */
1101
1102 /* recovery begin */
1103 static void amdgpu_ras_do_recovery(struct work_struct *work)
1104 {
1105         struct amdgpu_ras *ras =
1106                 container_of(work, struct amdgpu_ras, recovery_work);
1107
1108         amdgpu_device_gpu_recover(ras->adev, 0);
1109         atomic_set(&ras->in_recovery, 0);
1110 }
1111
1112 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1113                 struct amdgpu_bo **bo_ptr)
1114 {
1115         /* no need to free it actually. */
1116         amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1117         return 0;
1118 }
1119
1120 /* reserve vram with size@offset */
1121 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1122                 uint64_t offset, uint64_t size,
1123                 struct amdgpu_bo **bo_ptr)
1124 {
1125         struct ttm_operation_ctx ctx = { false, false };
1126         struct amdgpu_bo_param bp;
1127         int r = 0;
1128         int i;
1129         struct amdgpu_bo *bo;
1130
1131         if (bo_ptr)
1132                 *bo_ptr = NULL;
1133         memset(&bp, 0, sizeof(bp));
1134         bp.size = size;
1135         bp.byte_align = PAGE_SIZE;
1136         bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1137         bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1138                 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1139         bp.type = ttm_bo_type_kernel;
1140         bp.resv = NULL;
1141
1142         r = amdgpu_bo_create(adev, &bp, &bo);
1143         if (r)
1144                 return -EINVAL;
1145
1146         r = amdgpu_bo_reserve(bo, false);
1147         if (r)
1148                 goto error_reserve;
1149
1150         offset = ALIGN(offset, PAGE_SIZE);
1151         for (i = 0; i < bo->placement.num_placement; ++i) {
1152                 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1153                 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1154         }
1155
1156         ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1157         r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1158         if (r)
1159                 goto error_pin;
1160
1161         r = amdgpu_bo_pin_restricted(bo,
1162                         AMDGPU_GEM_DOMAIN_VRAM,
1163                         offset,
1164                         offset + size);
1165         if (r)
1166                 goto error_pin;
1167
1168         if (bo_ptr)
1169                 *bo_ptr = bo;
1170
1171         amdgpu_bo_unreserve(bo);
1172         return r;
1173
1174 error_pin:
1175         amdgpu_bo_unreserve(bo);
1176 error_reserve:
1177         amdgpu_bo_unref(&bo);
1178         return r;
1179 }
1180
1181 /* alloc/realloc bps array */
1182 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1183                 struct ras_err_handler_data *data, int pages)
1184 {
1185         unsigned int old_space = data->count + data->space_left;
1186         unsigned int new_space = old_space + pages;
1187         unsigned int align_space = ALIGN(new_space, 1024);
1188         void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1189
1190         if (!tmp)
1191                 return -ENOMEM;
1192
1193         if (data->bps) {
1194                 memcpy(tmp, data->bps,
1195                                 data->count * sizeof(*data->bps));
1196                 kfree(data->bps);
1197         }
1198
1199         data->bps = tmp;
1200         data->space_left += align_space - old_space;
1201         return 0;
1202 }
1203
1204 /* it deal with vram only. */
1205 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1206                 unsigned long *bps, int pages)
1207 {
1208         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1209         struct ras_err_handler_data *data;
1210         int i = pages;
1211         int ret = 0;
1212
1213         if (!con || !con->eh_data || !bps || pages <= 0)
1214                 return 0;
1215
1216         mutex_lock(&con->recovery_lock);
1217         data = con->eh_data;
1218         if (!data)
1219                 goto out;
1220
1221         if (data->space_left <= pages)
1222                 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1223                         ret = -ENOMEM;
1224                         goto out;
1225                 }
1226
1227         while (i--)
1228                 data->bps[data->count++].bp = bps[i];
1229
1230         data->space_left -= pages;
1231 out:
1232         mutex_unlock(&con->recovery_lock);
1233
1234         return ret;
1235 }
1236
1237 /* called in gpu recovery/init */
1238 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1239 {
1240         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1241         struct ras_err_handler_data *data;
1242         uint64_t bp;
1243         struct amdgpu_bo *bo;
1244         int i;
1245
1246         if (!con || !con->eh_data)
1247                 return 0;
1248
1249         mutex_lock(&con->recovery_lock);
1250         data = con->eh_data;
1251         if (!data)
1252                 goto out;
1253         /* reserve vram at driver post stage. */
1254         for (i = data->last_reserved; i < data->count; i++) {
1255                 bp = data->bps[i].bp;
1256
1257                 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1258                                         PAGE_SIZE, &bo))
1259                         DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1260
1261                 data->bps[i].bo = bo;
1262                 data->last_reserved = i + 1;
1263         }
1264 out:
1265         mutex_unlock(&con->recovery_lock);
1266         return 0;
1267 }
1268
1269 /* called when driver unload */
1270 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1271 {
1272         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1273         struct ras_err_handler_data *data;
1274         struct amdgpu_bo *bo;
1275         int i;
1276
1277         if (!con || !con->eh_data)
1278                 return 0;
1279
1280         mutex_lock(&con->recovery_lock);
1281         data = con->eh_data;
1282         if (!data)
1283                 goto out;
1284
1285         for (i = data->last_reserved - 1; i >= 0; i--) {
1286                 bo = data->bps[i].bo;
1287
1288                 amdgpu_ras_release_vram(adev, &bo);
1289
1290                 data->bps[i].bo = bo;
1291                 data->last_reserved = i;
1292         }
1293 out:
1294         mutex_unlock(&con->recovery_lock);
1295         return 0;
1296 }
1297
1298 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1299 {
1300         /* TODO
1301          * write the array to eeprom when SMU disabled.
1302          */
1303         return 0;
1304 }
1305
1306 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1307 {
1308         /* TODO
1309          * read the array to eeprom when SMU disabled.
1310          */
1311         return 0;
1312 }
1313
1314 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1315 {
1316         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1317         struct ras_err_handler_data **data = &con->eh_data;
1318
1319         *data = kmalloc(sizeof(**data),
1320                         GFP_KERNEL|__GFP_ZERO);
1321         if (!*data)
1322                 return -ENOMEM;
1323
1324         mutex_init(&con->recovery_lock);
1325         INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1326         atomic_set(&con->in_recovery, 0);
1327         con->adev = adev;
1328
1329         amdgpu_ras_load_bad_pages(adev);
1330         amdgpu_ras_reserve_bad_pages(adev);
1331
1332         return 0;
1333 }
1334
1335 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1336 {
1337         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1338         struct ras_err_handler_data *data = con->eh_data;
1339
1340         cancel_work_sync(&con->recovery_work);
1341         amdgpu_ras_save_bad_pages(adev);
1342         amdgpu_ras_release_bad_pages(adev);
1343
1344         mutex_lock(&con->recovery_lock);
1345         con->eh_data = NULL;
1346         kfree(data->bps);
1347         kfree(data);
1348         mutex_unlock(&con->recovery_lock);
1349
1350         return 0;
1351 }
1352 /* recovery end */
1353
1354 /*
1355  * check hardware's ras ability which will be saved in hw_supported.
1356  * if hardware does not support ras, we can skip some ras initializtion and
1357  * forbid some ras operations from IP.
1358  * if software itself, say boot parameter, limit the ras ability. We still
1359  * need allow IP do some limited operations, like disable. In such case,
1360  * we have to initialize ras as normal. but need check if operation is
1361  * allowed or not in each function.
1362  */
1363 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1364                 uint32_t *hw_supported, uint32_t *supported)
1365 {
1366         *hw_supported = 0;
1367         *supported = 0;
1368
1369         if (amdgpu_sriov_vf(adev) ||
1370                         adev->asic_type != CHIP_VEGA20)
1371                 return;
1372
1373         if (adev->is_atom_fw &&
1374                         (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1375                          amdgpu_atomfirmware_sram_ecc_supported(adev)))
1376                 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1377
1378         *supported = amdgpu_ras_enable == 0 ?
1379                                 0 : *hw_supported & amdgpu_ras_mask;
1380 }
1381
1382 int amdgpu_ras_init(struct amdgpu_device *adev)
1383 {
1384         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1385
1386         if (con)
1387                 return 0;
1388
1389         con = kmalloc(sizeof(struct amdgpu_ras) +
1390                         sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1391                         GFP_KERNEL|__GFP_ZERO);
1392         if (!con)
1393                 return -ENOMEM;
1394
1395         con->objs = (struct ras_manager *)(con + 1);
1396
1397         amdgpu_ras_set_context(adev, con);
1398
1399         amdgpu_ras_check_supported(adev, &con->hw_supported,
1400                         &con->supported);
1401         con->features = 0;
1402         INIT_LIST_HEAD(&con->head);
1403         /* Might need get this flag from vbios. */
1404         con->flags = RAS_DEFAULT_FLAGS;
1405
1406         if (amdgpu_ras_recovery_init(adev))
1407                 goto recovery_out;
1408
1409         amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1410
1411         if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
1412                 amdgpu_ras_enable_all_features(adev, 1);
1413
1414         if (amdgpu_ras_fs_init(adev))
1415                 goto fs_out;
1416
1417         amdgpu_ras_self_test(adev);
1418
1419         DRM_INFO("RAS INFO: ras initialized successfully, "
1420                         "hardware ability[%x] ras_mask[%x]\n",
1421                         con->hw_supported, con->supported);
1422         return 0;
1423 fs_out:
1424         amdgpu_ras_recovery_fini(adev);
1425 recovery_out:
1426         amdgpu_ras_set_context(adev, NULL);
1427         kfree(con);
1428
1429         return -EINVAL;
1430 }
1431
1432 /* do some init work after IP late init as dependence */
1433 void amdgpu_ras_post_init(struct amdgpu_device *adev)
1434 {
1435         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1436         struct ras_manager *obj, *tmp;
1437
1438         if (!con)
1439                 return;
1440
1441         /* We enable ras on all hw_supported block, but as boot parameter might
1442          * disable some of them and one or more IP has not implemented yet.
1443          * So we disable them on behalf.
1444          */
1445         if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1446                 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1447                         if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1448                                 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1449                                 /* there should be no any reference. */
1450                                 WARN_ON(alive_obj(obj));
1451                         }
1452                 };
1453         }
1454 }
1455
1456 /* do some fini work before IP fini as dependence */
1457 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1458 {
1459         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1460
1461         if (!con)
1462                 return 0;
1463
1464         /* Need disable ras on all IPs here before ip [hw/sw]fini */
1465         amdgpu_ras_disable_all_features(adev, 0);
1466         amdgpu_ras_recovery_fini(adev);
1467         return 0;
1468 }
1469
1470 int amdgpu_ras_fini(struct amdgpu_device *adev)
1471 {
1472         struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1473
1474         if (!con)
1475                 return 0;
1476
1477         amdgpu_ras_fs_fini(adev);
1478         amdgpu_ras_interrupt_remove_all(adev);
1479
1480         WARN(con->features, "Feature mask is not cleared");
1481
1482         if (con->features)
1483                 amdgpu_ras_disable_all_features(adev, 1);
1484
1485         amdgpu_ras_set_context(adev, NULL);
1486         kfree(con);
1487
1488         return 0;
1489 }