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