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