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