2 * Copyright 2018 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/debugfs.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
30 #include "amdgpu_ras.h"
31 #include "amdgpu_atomfirmware.h"
33 const char *ras_error_string[] = {
37 "multi_uncorrectable",
41 const char *ras_block_string[] = {
58 #define ras_err_str(i) (ras_error_string[ffs(i)])
59 #define ras_block_str(i) (ras_block_string[i])
61 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
62 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2
63 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
65 /* inject address is 52 bits */
66 #define RAS_UMC_INJECT_ADDR_LIMIT (0x1ULL << 52)
68 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
69 uint64_t offset, uint64_t size,
70 struct amdgpu_bo **bo_ptr);
71 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
72 struct amdgpu_bo **bo_ptr);
74 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
75 size_t size, loff_t *pos)
77 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
78 struct ras_query_if info = {
84 if (amdgpu_ras_error_query(obj->adev, &info))
87 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
94 s = min_t(u64, s, size);
97 if (copy_to_user(buf, &val[*pos], s))
105 static const struct file_operations amdgpu_ras_debugfs_ops = {
106 .owner = THIS_MODULE,
107 .read = amdgpu_ras_debugfs_read,
109 .llseek = default_llseek
112 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
116 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
118 if (strcmp(name, ras_block_str(i)) == 0)
124 static 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)
128 ssize_t s = min_t(u64, 64, size);
140 memset(str, 0, sizeof(str));
141 memset(data, 0, sizeof(*data));
143 if (copy_from_user(str, buf, s))
146 if (sscanf(str, "disable %32s", block_name) == 1)
148 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
150 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
152 else if (str[0] && str[1] && str[2] && str[3])
153 /* ascii string, but commands are not matched. */
157 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
160 data->head.block = block_id;
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;
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)
177 data->inject.address = address;
178 data->inject.value = value;
181 if (size < sizeof(*data))
184 if (copy_from_user(data, buf, sizeof(*data)))
191 * DOC: AMDGPU RAS debugfs control interface
193 * It accepts struct ras_debug_if who has two members.
195 * First member: ras_debug_if::head or ras_debug_if::inject.
197 * head is used to indicate which IP block will be under control.
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.
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.
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.
215 * How to use the interface?
217 * copy the struct ras_debug_if in your codes and initialize it.
218 * write the struct to the control node.
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
229 * ue: multi_uncorrectable
230 * ce: single_correctable
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
237 * How to check the result?
239 * For disable/enable, please check ras features at
240 * /sys/class/drm/card[0/1/2...]/device/ras/features
242 * For inject, please check corresponding err count at
243 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
245 * NOTE: operation is only allowed on blocks which are supported.
246 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
248 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
249 size_t size, loff_t *pos)
251 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
252 struct ras_debug_if data;
255 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
259 if (!amdgpu_ras_is_supported(adev, data.head.block))
264 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
267 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
270 if ((data.inject.address >= adev->gmc.mc_vram_size) ||
271 (data.inject.address >= RAS_UMC_INJECT_ADDR_LIMIT)) {
276 /* data.inject.address is offset instead of absolute gpu address */
277 ret = amdgpu_ras_error_inject(adev, &data.inject);
290 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
291 .owner = THIS_MODULE,
293 .write = amdgpu_ras_debugfs_ctrl_write,
294 .llseek = default_llseek
297 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
298 struct device_attribute *attr, char *buf)
300 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
301 struct ras_query_if info = {
305 if (amdgpu_ras_error_query(obj->adev, &info))
308 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
310 "ce", info.ce_count);
315 #define get_obj(obj) do { (obj)->use++; } while (0)
316 #define alive_obj(obj) ((obj)->use)
318 static inline void put_obj(struct ras_manager *obj)
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);
327 /* make one obj and return it. */
328 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
329 struct ras_common_if *head)
331 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
332 struct ras_manager *obj;
337 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
340 obj = &con->objs[head->block];
341 /* already exist. return obj? */
347 list_add(&obj->node, &con->head);
353 /* return an obj equal to head, or the first when head is NULL */
354 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
355 struct ras_common_if *head)
357 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
358 struct ras_manager *obj;
365 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
368 obj = &con->objs[head->block];
370 if (alive_obj(obj)) {
371 WARN_ON(head->block != obj->head.block);
375 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
377 if (alive_obj(obj)) {
378 WARN_ON(i != obj->head.block);
388 /* feature ctl begin */
389 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
390 struct ras_common_if *head)
392 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
394 return con->hw_supported & BIT(head->block);
397 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
398 struct ras_common_if *head)
400 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
402 return con->features & BIT(head->block);
406 * if obj is not created, then create one.
407 * set feature enable flag.
409 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
410 struct ras_common_if *head, int enable)
412 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
413 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
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.
421 if (!amdgpu_ras_is_feature_allowed(adev, head))
423 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
428 obj = amdgpu_ras_create_obj(adev, head);
432 /* In case we create obj somewhere else */
435 con->features |= BIT(head->block);
437 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
438 con->features &= ~BIT(head->block);
446 /* wrapper of psp_ras_enable_features */
447 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
448 struct ras_common_if *head, bool enable)
450 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
451 union ta_ras_cmd_input info;
458 info.disable_features = (struct ta_ras_disable_features_input) {
459 .block_id = amdgpu_ras_block_to_ta(head->block),
460 .error_type = amdgpu_ras_error_to_ta(head->type),
463 info.enable_features = (struct ta_ras_enable_features_input) {
464 .block_id = amdgpu_ras_block_to_ta(head->block),
465 .error_type = amdgpu_ras_error_to_ta(head->type),
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)))
475 ret = psp_ras_enable_features(&adev->psp, &info, enable);
477 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
478 enable ? "enable":"disable",
479 ras_block_str(head->block),
481 if (ret == TA_RAS_STATUS__RESET_NEEDED)
487 __amdgpu_ras_feature_enable(adev, head, enable);
492 /* Only used in device probe stage and called only once. */
493 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
494 struct ras_common_if *head, bool enable)
496 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
502 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
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.
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.
515 if (ret == -EINVAL) {
516 ret = __amdgpu_ras_feature_enable(adev, head, 1);
518 DRM_INFO("RAS INFO: %s setup object\n",
519 ras_block_str(head->block));
522 /* setup the object then issue a ras TA disable cmd.*/
523 ret = __amdgpu_ras_feature_enable(adev, head, 1);
527 ret = amdgpu_ras_feature_enable(adev, head, 0);
530 ret = amdgpu_ras_feature_enable(adev, head, enable);
535 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
538 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
539 struct ras_manager *obj, *tmp;
541 list_for_each_entry_safe(obj, tmp, &con->head, node) {
543 * aka just release the obj and corresponding flags
546 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
549 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
554 return con->features;
557 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
560 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
561 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
563 const enum amdgpu_ras_error_type default_ras_type =
564 AMDGPU_RAS_ERROR__NONE;
566 for (i = 0; i < ras_block_count; i++) {
567 struct ras_common_if head = {
569 .type = default_ras_type,
570 .sub_block_index = 0,
572 strcpy(head.name, ras_block_str(i));
575 * bypass psp. vbios enable ras for us.
576 * so just create the obj
578 if (__amdgpu_ras_feature_enable(adev, &head, 1))
581 if (amdgpu_ras_feature_enable(adev, &head, 1))
586 return con->features;
588 /* feature ctl end */
590 /* query/inject/cure begin */
591 int amdgpu_ras_error_query(struct amdgpu_device *adev,
592 struct ras_query_if *info)
594 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
595 struct ras_err_data err_data = {0, 0, 0, NULL};
600 switch (info->head.block) {
601 case AMDGPU_RAS_BLOCK__UMC:
602 if (adev->umc.funcs->query_ras_error_count)
603 adev->umc.funcs->query_ras_error_count(adev, &err_data);
609 obj->err_data.ue_count += err_data.ue_count;
610 obj->err_data.ce_count += err_data.ce_count;
612 info->ue_count = obj->err_data.ue_count;
613 info->ce_count = obj->err_data.ce_count;
615 if (err_data.ce_count)
616 dev_info(adev->dev, "%ld correctable errors detected in %s block\n",
617 obj->err_data.ce_count, ras_block_str(info->head.block));
618 if (err_data.ue_count)
619 dev_info(adev->dev, "%ld uncorrectable errors detected in %s block\n",
620 obj->err_data.ue_count, ras_block_str(info->head.block));
625 /* wrapper of psp_ras_trigger_error */
626 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
627 struct ras_inject_if *info)
629 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
630 struct ta_ras_trigger_error_input block_info = {
631 .block_id = amdgpu_ras_block_to_ta(info->head.block),
632 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
633 .sub_block_index = info->head.sub_block_index,
634 .address = info->address,
635 .value = info->value,
642 if (block_info.block_id != TA_RAS_BLOCK__UMC) {
643 DRM_INFO("%s error injection is not supported yet\n",
644 ras_block_str(info->head.block));
648 ret = psp_ras_trigger_error(&adev->psp, &block_info);
650 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
651 ras_block_str(info->head.block),
657 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
658 struct ras_cure_if *info)
660 /* psp fw has no cure interface for now. */
664 /* get the total error counts on all IPs */
665 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
668 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
669 struct ras_manager *obj;
670 struct ras_err_data data = {0, 0};
675 list_for_each_entry(obj, &con->head, node) {
676 struct ras_query_if info = {
680 if (amdgpu_ras_error_query(adev, &info))
683 data.ce_count += info.ce_count;
684 data.ue_count += info.ue_count;
687 return is_ce ? data.ce_count : data.ue_count;
689 /* query/inject/cure end */
694 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
695 struct ras_badpage **bps, unsigned int *count);
697 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
711 * DOC: ras sysfs gpu_vram_bad_pages interface
713 * It allows user to read the bad pages of vram on the gpu through
714 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
716 * It outputs multiple lines, and each line stands for one gpu page.
718 * The format of one line is below,
719 * gpu pfn : gpu page size : flags
721 * gpu pfn and gpu page size are printed in hex format.
722 * flags can be one of below character,
723 * R: reserved, this gpu page is reserved and not able to use.
724 * P: pending for reserve, this gpu page is marked as bad, will be reserved
725 * in next window of page_reserve.
726 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
729 * 0x00000001 : 0x00001000 : R
730 * 0x00000002 : 0x00001000 : P
733 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
734 struct kobject *kobj, struct bin_attribute *attr,
735 char *buf, loff_t ppos, size_t count)
737 struct amdgpu_ras *con =
738 container_of(attr, struct amdgpu_ras, badpages_attr);
739 struct amdgpu_device *adev = con->adev;
740 const unsigned int element_size =
741 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
742 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
743 unsigned int end = div64_ul(ppos + count - 1, element_size);
745 struct ras_badpage *bps = NULL;
746 unsigned int bps_count = 0;
748 memset(buf, 0, count);
750 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
753 for (; start < end && start < bps_count; start++)
754 s += scnprintf(&buf[s], element_size + 1,
755 "0x%08x : 0x%08x : %1s\n",
758 amdgpu_ras_badpage_flags_str(bps[start].flags));
765 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
766 struct device_attribute *attr, char *buf)
768 struct amdgpu_ras *con =
769 container_of(attr, struct amdgpu_ras, features_attr);
770 struct drm_device *ddev = dev_get_drvdata(dev);
771 struct amdgpu_device *adev = ddev->dev_private;
772 struct ras_common_if head;
773 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
776 struct ras_manager *obj;
778 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
780 for (i = 0; i < ras_block_count; i++) {
783 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
784 obj = amdgpu_ras_find_obj(adev, &head);
785 s += scnprintf(&buf[s], PAGE_SIZE - s,
788 ras_err_str(obj->head.type));
790 s += scnprintf(&buf[s], PAGE_SIZE - s,
798 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
800 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
801 struct attribute *attrs[] = {
802 &con->features_attr.attr,
805 struct bin_attribute *bin_attrs[] = {
809 struct attribute_group group = {
812 .bin_attrs = bin_attrs,
815 con->features_attr = (struct device_attribute) {
820 .show = amdgpu_ras_sysfs_features_read,
823 con->badpages_attr = (struct bin_attribute) {
825 .name = "gpu_vram_bad_pages",
830 .read = amdgpu_ras_sysfs_badpages_read,
833 sysfs_attr_init(attrs[0]);
834 sysfs_bin_attr_init(bin_attrs[0]);
836 return sysfs_create_group(&adev->dev->kobj, &group);
839 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
841 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
842 struct attribute *attrs[] = {
843 &con->features_attr.attr,
846 struct bin_attribute *bin_attrs[] = {
850 struct attribute_group group = {
853 .bin_attrs = bin_attrs,
856 sysfs_remove_group(&adev->dev->kobj, &group);
861 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
862 struct ras_fs_if *head)
864 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
866 if (!obj || obj->attr_inuse)
871 memcpy(obj->fs_data.sysfs_name,
873 sizeof(obj->fs_data.sysfs_name));
875 obj->sysfs_attr = (struct device_attribute){
877 .name = obj->fs_data.sysfs_name,
880 .show = amdgpu_ras_sysfs_read,
882 sysfs_attr_init(&obj->sysfs_attr.attr);
884 if (sysfs_add_file_to_group(&adev->dev->kobj,
885 &obj->sysfs_attr.attr,
896 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
897 struct ras_common_if *head)
899 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
901 if (!obj || !obj->attr_inuse)
904 sysfs_remove_file_from_group(&adev->dev->kobj,
905 &obj->sysfs_attr.attr,
913 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
915 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
916 struct ras_manager *obj, *tmp;
918 list_for_each_entry_safe(obj, tmp, &con->head, node) {
919 amdgpu_ras_sysfs_remove(adev, &obj->head);
922 amdgpu_ras_sysfs_remove_feature_node(adev);
929 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
931 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
932 struct drm_minor *minor = adev->ddev->primary;
934 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
935 con->ent = debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
936 adev, &amdgpu_ras_debugfs_ctrl_ops);
939 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
940 struct ras_fs_if *head)
942 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
943 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
945 if (!obj || obj->ent)
950 memcpy(obj->fs_data.debugfs_name,
952 sizeof(obj->fs_data.debugfs_name));
954 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
955 S_IWUGO | S_IRUGO, con->dir, obj,
956 &amdgpu_ras_debugfs_ops);
959 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
960 struct ras_common_if *head)
962 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
964 if (!obj || !obj->ent)
967 debugfs_remove(obj->ent);
972 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
974 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
975 struct ras_manager *obj, *tmp;
977 list_for_each_entry_safe(obj, tmp, &con->head, node) {
978 amdgpu_ras_debugfs_remove(adev, &obj->head);
981 debugfs_remove(con->ent);
982 debugfs_remove(con->dir);
990 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
992 amdgpu_ras_sysfs_create_feature_node(adev);
993 amdgpu_ras_debugfs_create_ctrl_node(adev);
998 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1000 amdgpu_ras_debugfs_remove_all(adev);
1001 amdgpu_ras_sysfs_remove_all(adev);
1007 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1009 struct ras_ih_data *data = &obj->ih_data;
1010 struct amdgpu_iv_entry entry;
1012 struct ras_err_data err_data = {0, 0, 0, NULL};
1014 while (data->rptr != data->wptr) {
1016 memcpy(&entry, &data->ring[data->rptr],
1017 data->element_size);
1020 data->rptr = (data->aligned_element_size +
1021 data->rptr) % data->ring_size;
1023 /* Let IP handle its data, maybe we need get the output
1024 * from the callback to udpate the error type/count, etc
1027 ret = data->cb(obj->adev, &err_data, &entry);
1028 /* ue will trigger an interrupt, and in that case
1029 * we need do a reset to recovery the whole system.
1030 * But leave IP do that recovery, here we just dispatch
1033 if (ret == AMDGPU_RAS_UE) {
1034 obj->err_data.ue_count += err_data.ue_count;
1036 /* Might need get ce count by register, but not all IP
1037 * saves ce count, some IP just use one bit or two bits
1038 * to indicate ce happened.
1044 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1046 struct ras_ih_data *data =
1047 container_of(work, struct ras_ih_data, ih_work);
1048 struct ras_manager *obj =
1049 container_of(data, struct ras_manager, ih_data);
1051 amdgpu_ras_interrupt_handler(obj);
1054 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1055 struct ras_dispatch_if *info)
1057 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1058 struct ras_ih_data *data = &obj->ih_data;
1063 if (data->inuse == 0)
1066 /* Might be overflow... */
1067 memcpy(&data->ring[data->wptr], info->entry,
1068 data->element_size);
1071 data->wptr = (data->aligned_element_size +
1072 data->wptr) % data->ring_size;
1074 schedule_work(&data->ih_work);
1079 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1080 struct ras_ih_if *info)
1082 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1083 struct ras_ih_data *data;
1088 data = &obj->ih_data;
1089 if (data->inuse == 0)
1092 cancel_work_sync(&data->ih_work);
1095 memset(data, 0, sizeof(*data));
1101 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1102 struct ras_ih_if *info)
1104 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1105 struct ras_ih_data *data;
1108 /* in case we registe the IH before enable ras feature */
1109 obj = amdgpu_ras_create_obj(adev, &info->head);
1115 data = &obj->ih_data;
1116 /* add the callback.etc */
1117 *data = (struct ras_ih_data) {
1120 .element_size = sizeof(struct amdgpu_iv_entry),
1125 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1127 data->aligned_element_size = ALIGN(data->element_size, 8);
1128 /* the ring can store 64 iv entries. */
1129 data->ring_size = 64 * data->aligned_element_size;
1130 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1142 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1144 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1145 struct ras_manager *obj, *tmp;
1147 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1148 struct ras_ih_if info = {
1151 amdgpu_ras_interrupt_remove_handler(adev, &info);
1158 /* recovery begin */
1160 /* return 0 on success.
1161 * caller need free bps.
1163 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1164 struct ras_badpage **bps, unsigned int *count)
1166 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1167 struct ras_err_handler_data *data;
1171 if (!con || !con->eh_data || !bps || !count)
1174 mutex_lock(&con->recovery_lock);
1175 data = con->eh_data;
1176 if (!data || data->count == 0) {
1181 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1187 for (; i < data->count; i++) {
1188 (*bps)[i] = (struct ras_badpage){
1189 .bp = data->bps[i].bp,
1190 .size = AMDGPU_GPU_PAGE_SIZE,
1194 if (data->last_reserved <= i)
1195 (*bps)[i].flags = 1;
1196 else if (data->bps[i].bo == NULL)
1197 (*bps)[i].flags = 2;
1200 *count = data->count;
1202 mutex_unlock(&con->recovery_lock);
1206 static void amdgpu_ras_do_recovery(struct work_struct *work)
1208 struct amdgpu_ras *ras =
1209 container_of(work, struct amdgpu_ras, recovery_work);
1211 amdgpu_device_gpu_recover(ras->adev, 0);
1212 atomic_set(&ras->in_recovery, 0);
1215 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1216 struct amdgpu_bo **bo_ptr)
1218 /* no need to free it actually. */
1219 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1223 /* reserve vram with size@offset */
1224 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1225 uint64_t offset, uint64_t size,
1226 struct amdgpu_bo **bo_ptr)
1228 struct ttm_operation_ctx ctx = { false, false };
1229 struct amdgpu_bo_param bp;
1232 struct amdgpu_bo *bo;
1236 memset(&bp, 0, sizeof(bp));
1238 bp.byte_align = PAGE_SIZE;
1239 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1240 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1241 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1242 bp.type = ttm_bo_type_kernel;
1245 r = amdgpu_bo_create(adev, &bp, &bo);
1249 r = amdgpu_bo_reserve(bo, false);
1253 offset = ALIGN(offset, PAGE_SIZE);
1254 for (i = 0; i < bo->placement.num_placement; ++i) {
1255 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1256 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1259 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1260 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1264 r = amdgpu_bo_pin_restricted(bo,
1265 AMDGPU_GEM_DOMAIN_VRAM,
1274 amdgpu_bo_unreserve(bo);
1278 amdgpu_bo_unreserve(bo);
1280 amdgpu_bo_unref(&bo);
1284 /* alloc/realloc bps array */
1285 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1286 struct ras_err_handler_data *data, int pages)
1288 unsigned int old_space = data->count + data->space_left;
1289 unsigned int new_space = old_space + pages;
1290 unsigned int align_space = ALIGN(new_space, 1024);
1291 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1297 memcpy(tmp, data->bps,
1298 data->count * sizeof(*data->bps));
1303 data->space_left += align_space - old_space;
1307 /* it deal with vram only. */
1308 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1309 unsigned long *bps, int pages)
1311 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1312 struct ras_err_handler_data *data;
1316 if (!con || !con->eh_data || !bps || pages <= 0)
1319 mutex_lock(&con->recovery_lock);
1320 data = con->eh_data;
1324 if (data->space_left <= pages)
1325 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1331 data->bps[data->count++].bp = bps[i];
1333 data->space_left -= pages;
1335 mutex_unlock(&con->recovery_lock);
1340 /* called in gpu recovery/init */
1341 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1343 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1344 struct ras_err_handler_data *data;
1346 struct amdgpu_bo *bo;
1349 if (!con || !con->eh_data)
1352 mutex_lock(&con->recovery_lock);
1353 data = con->eh_data;
1356 /* reserve vram at driver post stage. */
1357 for (i = data->last_reserved; i < data->count; i++) {
1358 bp = data->bps[i].bp;
1360 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1362 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1364 data->bps[i].bo = bo;
1365 data->last_reserved = i + 1;
1368 mutex_unlock(&con->recovery_lock);
1372 /* called when driver unload */
1373 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1375 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1376 struct ras_err_handler_data *data;
1377 struct amdgpu_bo *bo;
1380 if (!con || !con->eh_data)
1383 mutex_lock(&con->recovery_lock);
1384 data = con->eh_data;
1388 for (i = data->last_reserved - 1; i >= 0; i--) {
1389 bo = data->bps[i].bo;
1391 amdgpu_ras_release_vram(adev, &bo);
1393 data->bps[i].bo = bo;
1394 data->last_reserved = i;
1397 mutex_unlock(&con->recovery_lock);
1401 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1404 * write the array to eeprom when SMU disabled.
1409 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1412 * read the array to eeprom when SMU disabled.
1417 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1419 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1420 struct ras_err_handler_data **data = &con->eh_data;
1422 *data = kmalloc(sizeof(**data),
1423 GFP_KERNEL|__GFP_ZERO);
1427 mutex_init(&con->recovery_lock);
1428 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1429 atomic_set(&con->in_recovery, 0);
1432 amdgpu_ras_load_bad_pages(adev);
1433 amdgpu_ras_reserve_bad_pages(adev);
1438 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1440 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1441 struct ras_err_handler_data *data = con->eh_data;
1443 cancel_work_sync(&con->recovery_work);
1444 amdgpu_ras_save_bad_pages(adev);
1445 amdgpu_ras_release_bad_pages(adev);
1447 mutex_lock(&con->recovery_lock);
1448 con->eh_data = NULL;
1451 mutex_unlock(&con->recovery_lock);
1457 /* return 0 if ras will reset gpu and repost.*/
1458 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1461 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1466 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1471 * check hardware's ras ability which will be saved in hw_supported.
1472 * if hardware does not support ras, we can skip some ras initializtion and
1473 * forbid some ras operations from IP.
1474 * if software itself, say boot parameter, limit the ras ability. We still
1475 * need allow IP do some limited operations, like disable. In such case,
1476 * we have to initialize ras as normal. but need check if operation is
1477 * allowed or not in each function.
1479 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1480 uint32_t *hw_supported, uint32_t *supported)
1485 if (amdgpu_sriov_vf(adev) ||
1486 adev->asic_type != CHIP_VEGA20)
1489 if (adev->is_atom_fw &&
1490 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1491 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1492 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1494 *supported = amdgpu_ras_enable == 0 ?
1495 0 : *hw_supported & amdgpu_ras_mask;
1498 int amdgpu_ras_init(struct amdgpu_device *adev)
1500 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1505 con = kmalloc(sizeof(struct amdgpu_ras) +
1506 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1507 GFP_KERNEL|__GFP_ZERO);
1511 con->objs = (struct ras_manager *)(con + 1);
1513 amdgpu_ras_set_context(adev, con);
1515 amdgpu_ras_check_supported(adev, &con->hw_supported,
1517 if (!con->hw_supported) {
1518 amdgpu_ras_set_context(adev, NULL);
1524 INIT_LIST_HEAD(&con->head);
1525 /* Might need get this flag from vbios. */
1526 con->flags = RAS_DEFAULT_FLAGS;
1528 if (amdgpu_ras_recovery_init(adev))
1531 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1533 if (amdgpu_ras_fs_init(adev))
1536 DRM_INFO("RAS INFO: ras initialized successfully, "
1537 "hardware ability[%x] ras_mask[%x]\n",
1538 con->hw_supported, con->supported);
1541 amdgpu_ras_recovery_fini(adev);
1543 amdgpu_ras_set_context(adev, NULL);
1549 /* do some init work after IP late init as dependence.
1550 * and it runs in resume/gpu reset/booting up cases.
1552 void amdgpu_ras_resume(struct amdgpu_device *adev)
1554 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1555 struct ras_manager *obj, *tmp;
1560 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1561 /* Set up all other IPs which are not implemented. There is a
1562 * tricky thing that IP's actual ras error type should be
1563 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1564 * ERROR_NONE make sense anyway.
1566 amdgpu_ras_enable_all_features(adev, 1);
1568 /* We enable ras on all hw_supported block, but as boot
1569 * parameter might disable some of them and one or more IP has
1570 * not implemented yet. So we disable them on behalf.
1572 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1573 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1574 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1575 /* there should be no any reference. */
1576 WARN_ON(alive_obj(obj));
1581 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1582 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1583 /* setup ras obj state as disabled.
1584 * for init_by_vbios case.
1585 * if we want to enable ras, just enable it in a normal way.
1586 * If we want do disable it, need setup ras obj as enabled,
1587 * then issue another TA disable cmd.
1588 * See feature_enable_on_boot
1590 amdgpu_ras_disable_all_features(adev, 1);
1591 amdgpu_ras_reset_gpu(adev, 0);
1595 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1597 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1602 amdgpu_ras_disable_all_features(adev, 0);
1603 /* Make sure all ras objects are disabled. */
1605 amdgpu_ras_disable_all_features(adev, 1);
1608 /* do some fini work before IP fini as dependence */
1609 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1611 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1616 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1617 amdgpu_ras_disable_all_features(adev, 0);
1618 amdgpu_ras_recovery_fini(adev);
1622 int amdgpu_ras_fini(struct amdgpu_device *adev)
1624 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1629 amdgpu_ras_fs_fini(adev);
1630 amdgpu_ras_interrupt_remove_all(adev);
1632 WARN(con->features, "Feature mask is not cleared");
1635 amdgpu_ras_disable_all_features(adev, 1);
1637 amdgpu_ras_set_context(adev, NULL);