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