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