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