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