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