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