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