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"
34 /* interrupt bottom half */
35 struct work_struct ih_work;
41 unsigned int ring_size;
42 unsigned int element_size;
43 unsigned int aligned_element_size;
50 char debugfs_name[32];
54 unsigned long ue_count;
55 unsigned long ce_count;
58 struct ras_err_handler_data {
59 /* point to bad pages array */
64 /* the count of entries */
66 /* the space can place new entries */
68 /* last reserved entry's index + 1 */
73 struct ras_common_if head;
77 struct list_head node;
79 struct amdgpu_device *adev;
83 struct device_attribute sysfs_attr;
87 struct ras_fs_data fs_data;
90 struct ras_ih_data ih_data;
92 struct ras_err_data err_data;
101 const char *ras_error_string[] = {
104 "single_correctable",
105 "multi_uncorrectable",
109 const char *ras_block_string[] = {
126 #define ras_err_str(i) (ras_error_string[ffs(i)])
127 #define ras_block_str(i) (ras_block_string[i])
129 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
130 #define AMDGPU_RAS_FLAG_INIT_NEED_RESET 2
131 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
133 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
134 uint64_t offset, uint64_t size,
135 struct amdgpu_bo **bo_ptr);
136 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
137 struct amdgpu_bo **bo_ptr);
139 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
140 size_t size, loff_t *pos)
142 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
143 struct ras_query_if info = {
149 if (amdgpu_ras_error_query(obj->adev, &info))
152 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
154 "ce", info.ce_count);
159 s = min_t(u64, s, size);
162 if (copy_to_user(buf, &val[*pos], s))
170 static const struct file_operations amdgpu_ras_debugfs_ops = {
171 .owner = THIS_MODULE,
172 .read = amdgpu_ras_debugfs_read,
174 .llseek = default_llseek
177 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
181 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
183 if (strcmp(name, ras_block_str(i)) == 0)
189 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
190 const char __user *buf, size_t size,
191 loff_t *pos, struct ras_debug_if *data)
193 ssize_t s = min_t(u64, 64, size);
205 memset(str, 0, sizeof(str));
206 memset(data, 0, sizeof(*data));
208 if (copy_from_user(str, buf, s))
211 if (sscanf(str, "disable %32s", block_name) == 1)
213 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
215 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
217 else if (str[0] && str[1] && str[2] && str[3])
218 /* ascii string, but commands are not matched. */
222 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
225 data->head.block = block_id;
226 data->head.type = memcmp("ue", err, 2) == 0 ?
227 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
228 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
232 if (sscanf(str, "%*s %*s %*s %llu %llu",
233 &address, &value) != 2)
234 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
235 &address, &value) != 2)
237 data->inject.address = address;
238 data->inject.value = value;
241 if (size < sizeof(*data))
244 if (copy_from_user(data, buf, sizeof(*data)))
251 * DOC: AMDGPU RAS debugfs control interface
253 * It accepts struct ras_debug_if who has two members.
255 * First member: ras_debug_if::head or ras_debug_if::inject.
257 * head is used to indicate which IP block will be under control.
259 * head has four members, they are block, type, sub_block_index, name.
260 * block: which IP will be under control.
261 * type: what kind of error will be enabled/disabled/injected.
262 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
263 * name: the name of IP.
265 * inject has two more members than head, they are address, value.
266 * As their names indicate, inject operation will write the
267 * value to the address.
269 * Second member: struct ras_debug_if::op.
270 * It has three kinds of operations.
271 * 0: disable RAS on the block. Take ::head as its data.
272 * 1: enable RAS on the block. Take ::head as its data.
273 * 2: inject errors on the block. Take ::inject as its data.
275 * How to use the interface?
277 * copy the struct ras_debug_if in your codes and initialize it.
278 * write the struct to the control node.
281 * echo op block [error [address value]] > .../ras/ras_ctrl
282 * op: disable, enable, inject
283 * disable: only block is needed
284 * enable: block and error are needed
285 * inject: error, address, value are needed
286 * block: umc, smda, gfx, .........
287 * see ras_block_string[] for details
289 * ue: multi_uncorrectable
290 * ce: single_correctable
292 * here are some examples for bash commands,
293 * echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
294 * echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
295 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
297 * How to check the result?
299 * For disable/enable, please check ras features at
300 * /sys/class/drm/card[0/1/2...]/device/ras/features
302 * For inject, please check corresponding err count at
303 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
305 * NOTE: operation is only allowed on blocks which are supported.
306 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
308 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
309 size_t size, loff_t *pos)
311 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
312 struct ras_debug_if data;
313 struct amdgpu_bo *bo;
316 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
320 if (!amdgpu_ras_is_supported(adev, data.head.block))
325 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
328 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
331 ret = amdgpu_ras_reserve_vram(adev,
332 data.inject.address, PAGE_SIZE, &bo);
334 /* address was offset, now it is absolute.*/
335 data.inject.address += adev->gmc.vram_start;
336 if (data.inject.address > adev->gmc.vram_end)
339 data.inject.address = amdgpu_bo_gpu_offset(bo);
340 ret = amdgpu_ras_error_inject(adev, &data.inject);
341 amdgpu_ras_release_vram(adev, &bo);
354 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
355 .owner = THIS_MODULE,
357 .write = amdgpu_ras_debugfs_ctrl_write,
358 .llseek = default_llseek
361 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
362 struct device_attribute *attr, char *buf)
364 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
365 struct ras_query_if info = {
369 if (amdgpu_ras_error_query(obj->adev, &info))
372 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
374 "ce", info.ce_count);
379 #define get_obj(obj) do { (obj)->use++; } while (0)
380 #define alive_obj(obj) ((obj)->use)
382 static inline void put_obj(struct ras_manager *obj)
384 if (obj && --obj->use == 0)
385 list_del(&obj->node);
386 if (obj && obj->use < 0) {
387 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
391 /* make one obj and return it. */
392 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
393 struct ras_common_if *head)
395 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
396 struct ras_manager *obj;
401 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
404 obj = &con->objs[head->block];
405 /* already exist. return obj? */
411 list_add(&obj->node, &con->head);
417 /* return an obj equal to head, or the first when head is NULL */
418 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
419 struct ras_common_if *head)
421 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
422 struct ras_manager *obj;
429 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
432 obj = &con->objs[head->block];
434 if (alive_obj(obj)) {
435 WARN_ON(head->block != obj->head.block);
439 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
441 if (alive_obj(obj)) {
442 WARN_ON(i != obj->head.block);
452 /* feature ctl begin */
453 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
454 struct ras_common_if *head)
456 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
458 return con->hw_supported & BIT(head->block);
461 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
462 struct ras_common_if *head)
464 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
466 return con->features & BIT(head->block);
470 * if obj is not created, then create one.
471 * set feature enable flag.
473 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
474 struct ras_common_if *head, int enable)
476 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
477 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
479 /* If hardware does not support ras, then do not create obj.
480 * But if hardware support ras, we can create the obj.
481 * Ras framework checks con->hw_supported to see if it need do
482 * corresponding initialization.
483 * IP checks con->support to see if it need disable ras.
485 if (!amdgpu_ras_is_feature_allowed(adev, head))
487 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
492 obj = amdgpu_ras_create_obj(adev, head);
496 /* In case we create obj somewhere else */
499 con->features |= BIT(head->block);
501 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
502 con->features &= ~BIT(head->block);
510 /* wrapper of psp_ras_enable_features */
511 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
512 struct ras_common_if *head, bool enable)
514 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
515 union ta_ras_cmd_input info;
522 info.disable_features = (struct ta_ras_disable_features_input) {
523 .block_id = amdgpu_ras_block_to_ta(head->block),
524 .error_type = amdgpu_ras_error_to_ta(head->type),
527 info.enable_features = (struct ta_ras_enable_features_input) {
528 .block_id = amdgpu_ras_block_to_ta(head->block),
529 .error_type = amdgpu_ras_error_to_ta(head->type),
533 /* Do not enable if it is not allowed. */
534 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
535 /* Are we alerady in that state we are going to set? */
536 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
539 ret = psp_ras_enable_features(&adev->psp, &info, enable);
541 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
542 enable ? "enable":"disable",
543 ras_block_str(head->block),
545 if (ret == TA_RAS_STATUS__RESET_NEEDED)
551 __amdgpu_ras_feature_enable(adev, head, enable);
556 /* Only used in device probe stage and called only once. */
557 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
558 struct ras_common_if *head, bool enable)
560 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
566 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
568 /* There is no harm to issue a ras TA cmd regardless of
569 * the currecnt ras state.
570 * If current state == target state, it will do nothing
571 * But sometimes it requests driver to reset and repost
572 * with error code -EAGAIN.
574 ret = amdgpu_ras_feature_enable(adev, head, 1);
575 /* With old ras TA, we might fail to enable ras.
576 * Log it and just setup the object.
577 * TODO need remove this WA in the future.
579 if (ret == -EINVAL) {
580 ret = __amdgpu_ras_feature_enable(adev, head, 1);
582 DRM_INFO("RAS INFO: %s setup object\n",
583 ras_block_str(head->block));
586 /* setup the object then issue a ras TA disable cmd.*/
587 ret = __amdgpu_ras_feature_enable(adev, head, 1);
591 ret = amdgpu_ras_feature_enable(adev, head, 0);
594 ret = amdgpu_ras_feature_enable(adev, head, enable);
599 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
602 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
603 struct ras_manager *obj, *tmp;
605 list_for_each_entry_safe(obj, tmp, &con->head, node) {
607 * aka just release the obj and corresponding flags
610 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
613 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
618 return con->features;
621 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
624 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
625 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
627 const enum amdgpu_ras_error_type default_ras_type =
628 AMDGPU_RAS_ERROR__NONE;
630 for (i = 0; i < ras_block_count; i++) {
631 struct ras_common_if head = {
633 .type = default_ras_type,
634 .sub_block_index = 0,
636 strcpy(head.name, ras_block_str(i));
639 * bypass psp. vbios enable ras for us.
640 * so just create the obj
642 if (__amdgpu_ras_feature_enable(adev, &head, 1))
645 if (amdgpu_ras_feature_enable(adev, &head, 1))
650 return con->features;
652 /* feature ctl end */
654 /* query/inject/cure begin */
655 int amdgpu_ras_error_query(struct amdgpu_device *adev,
656 struct ras_query_if *info)
658 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
662 /* TODO might read the register to read the count */
664 info->ue_count = obj->err_data.ue_count;
665 info->ce_count = obj->err_data.ce_count;
670 /* wrapper of psp_ras_trigger_error */
671 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
672 struct ras_inject_if *info)
674 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
675 struct ta_ras_trigger_error_input block_info = {
676 .block_id = amdgpu_ras_block_to_ta(info->head.block),
677 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
678 .sub_block_index = info->head.sub_block_index,
679 .address = info->address,
680 .value = info->value,
687 if (block_info.block_id != TA_RAS_BLOCK__UMC) {
688 DRM_INFO("%s error injection is not supported yet\n",
689 ras_block_str(info->head.block));
693 ret = psp_ras_trigger_error(&adev->psp, &block_info);
695 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
696 ras_block_str(info->head.block),
702 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
703 struct ras_cure_if *info)
705 /* psp fw has no cure interface for now. */
709 /* get the total error counts on all IPs */
710 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
713 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
714 struct ras_manager *obj;
715 struct ras_err_data data = {0, 0};
720 list_for_each_entry(obj, &con->head, node) {
721 struct ras_query_if info = {
725 if (amdgpu_ras_error_query(adev, &info))
728 data.ce_count += info.ce_count;
729 data.ue_count += info.ue_count;
732 return is_ce ? data.ce_count : data.ue_count;
734 /* query/inject/cure end */
739 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
740 struct ras_badpage **bps, unsigned int *count);
742 static char *amdgpu_ras_badpage_flags_str(unsigned int flags)
756 * DOC: ras sysfs gpu_vram_bad_pages interface
758 * It allows user to read the bad pages of vram on the gpu through
759 * /sys/class/drm/card[0/1/2...]/device/ras/gpu_vram_bad_pages
761 * It outputs multiple lines, and each line stands for one gpu page.
763 * The format of one line is below,
764 * gpu pfn : gpu page size : flags
766 * gpu pfn and gpu page size are printed in hex format.
767 * flags can be one of below character,
768 * R: reserved, this gpu page is reserved and not able to use.
769 * P: pending for reserve, this gpu page is marked as bad, will be reserved
770 * in next window of page_reserve.
771 * F: unable to reserve. this gpu page can't be reserved due to some reasons.
774 * 0x00000001 : 0x00001000 : R
775 * 0x00000002 : 0x00001000 : P
778 static ssize_t amdgpu_ras_sysfs_badpages_read(struct file *f,
779 struct kobject *kobj, struct bin_attribute *attr,
780 char *buf, loff_t ppos, size_t count)
782 struct amdgpu_ras *con =
783 container_of(attr, struct amdgpu_ras, badpages_attr);
784 struct amdgpu_device *adev = con->adev;
785 const unsigned int element_size =
786 sizeof("0xabcdabcd : 0x12345678 : R\n") - 1;
787 unsigned int start = div64_ul(ppos + element_size - 1, element_size);
788 unsigned int end = div64_ul(ppos + count - 1, element_size);
790 struct ras_badpage *bps = NULL;
791 unsigned int bps_count = 0;
793 memset(buf, 0, count);
795 if (amdgpu_ras_badpages_read(adev, &bps, &bps_count))
798 for (; start < end && start < bps_count; start++)
799 s += scnprintf(&buf[s], element_size + 1,
800 "0x%08x : 0x%08x : %1s\n",
803 amdgpu_ras_badpage_flags_str(bps[start].flags));
810 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
811 struct device_attribute *attr, char *buf)
813 struct amdgpu_ras *con =
814 container_of(attr, struct amdgpu_ras, features_attr);
815 struct drm_device *ddev = dev_get_drvdata(dev);
816 struct amdgpu_device *adev = ddev->dev_private;
817 struct ras_common_if head;
818 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
821 struct ras_manager *obj;
823 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
825 for (i = 0; i < ras_block_count; i++) {
828 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
829 obj = amdgpu_ras_find_obj(adev, &head);
830 s += scnprintf(&buf[s], PAGE_SIZE - s,
833 ras_err_str(obj->head.type));
835 s += scnprintf(&buf[s], PAGE_SIZE - s,
843 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
845 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
846 struct attribute *attrs[] = {
847 &con->features_attr.attr,
850 struct bin_attribute *bin_attrs[] = {
854 struct attribute_group group = {
857 .bin_attrs = bin_attrs,
860 con->features_attr = (struct device_attribute) {
865 .show = amdgpu_ras_sysfs_features_read,
868 con->badpages_attr = (struct bin_attribute) {
870 .name = "gpu_vram_bad_pages",
875 .read = amdgpu_ras_sysfs_badpages_read,
878 sysfs_attr_init(attrs[0]);
879 sysfs_bin_attr_init(bin_attrs[0]);
881 return sysfs_create_group(&adev->dev->kobj, &group);
884 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
886 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
887 struct attribute *attrs[] = {
888 &con->features_attr.attr,
891 struct bin_attribute *bin_attrs[] = {
895 struct attribute_group group = {
898 .bin_attrs = bin_attrs,
901 sysfs_remove_group(&adev->dev->kobj, &group);
906 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
907 struct ras_fs_if *head)
909 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
911 if (!obj || obj->attr_inuse)
916 memcpy(obj->fs_data.sysfs_name,
918 sizeof(obj->fs_data.sysfs_name));
920 obj->sysfs_attr = (struct device_attribute){
922 .name = obj->fs_data.sysfs_name,
925 .show = amdgpu_ras_sysfs_read,
927 sysfs_attr_init(&obj->sysfs_attr.attr);
929 if (sysfs_add_file_to_group(&adev->dev->kobj,
930 &obj->sysfs_attr.attr,
941 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
942 struct ras_common_if *head)
944 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
946 if (!obj || !obj->attr_inuse)
949 sysfs_remove_file_from_group(&adev->dev->kobj,
950 &obj->sysfs_attr.attr,
958 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
960 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
961 struct ras_manager *obj, *tmp;
963 list_for_each_entry_safe(obj, tmp, &con->head, node) {
964 amdgpu_ras_sysfs_remove(adev, &obj->head);
967 amdgpu_ras_sysfs_remove_feature_node(adev);
974 static void amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
976 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
977 struct drm_minor *minor = adev->ddev->primary;
979 con->dir = debugfs_create_dir("ras", minor->debugfs_root);
980 con->ent = debugfs_create_file("ras_ctrl", S_IWUGO | S_IRUGO, con->dir,
981 adev, &amdgpu_ras_debugfs_ctrl_ops);
984 void amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
985 struct ras_fs_if *head)
987 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
988 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
990 if (!obj || obj->ent)
995 memcpy(obj->fs_data.debugfs_name,
997 sizeof(obj->fs_data.debugfs_name));
999 obj->ent = debugfs_create_file(obj->fs_data.debugfs_name,
1000 S_IWUGO | S_IRUGO, con->dir, obj,
1001 &amdgpu_ras_debugfs_ops);
1004 void amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
1005 struct ras_common_if *head)
1007 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
1009 if (!obj || !obj->ent)
1012 debugfs_remove(obj->ent);
1017 static void amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
1019 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1020 struct ras_manager *obj, *tmp;
1022 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1023 amdgpu_ras_debugfs_remove(adev, &obj->head);
1026 debugfs_remove(con->ent);
1027 debugfs_remove(con->dir);
1035 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
1037 amdgpu_ras_sysfs_create_feature_node(adev);
1038 amdgpu_ras_debugfs_create_ctrl_node(adev);
1043 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
1045 amdgpu_ras_debugfs_remove_all(adev);
1046 amdgpu_ras_sysfs_remove_all(adev);
1052 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
1054 struct ras_ih_data *data = &obj->ih_data;
1055 struct amdgpu_iv_entry entry;
1058 while (data->rptr != data->wptr) {
1060 memcpy(&entry, &data->ring[data->rptr],
1061 data->element_size);
1064 data->rptr = (data->aligned_element_size +
1065 data->rptr) % data->ring_size;
1067 /* Let IP handle its data, maybe we need get the output
1068 * from the callback to udpate the error type/count, etc
1071 ret = data->cb(obj->adev, &entry);
1072 /* ue will trigger an interrupt, and in that case
1073 * we need do a reset to recovery the whole system.
1074 * But leave IP do that recovery, here we just dispatch
1077 if (ret == AMDGPU_RAS_UE) {
1078 obj->err_data.ue_count++;
1080 /* Might need get ce count by register, but not all IP
1081 * saves ce count, some IP just use one bit or two bits
1082 * to indicate ce happened.
1088 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
1090 struct ras_ih_data *data =
1091 container_of(work, struct ras_ih_data, ih_work);
1092 struct ras_manager *obj =
1093 container_of(data, struct ras_manager, ih_data);
1095 amdgpu_ras_interrupt_handler(obj);
1098 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
1099 struct ras_dispatch_if *info)
1101 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1102 struct ras_ih_data *data = &obj->ih_data;
1107 if (data->inuse == 0)
1110 /* Might be overflow... */
1111 memcpy(&data->ring[data->wptr], info->entry,
1112 data->element_size);
1115 data->wptr = (data->aligned_element_size +
1116 data->wptr) % data->ring_size;
1118 schedule_work(&data->ih_work);
1123 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1124 struct ras_ih_if *info)
1126 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1127 struct ras_ih_data *data;
1132 data = &obj->ih_data;
1133 if (data->inuse == 0)
1136 cancel_work_sync(&data->ih_work);
1139 memset(data, 0, sizeof(*data));
1145 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1146 struct ras_ih_if *info)
1148 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1149 struct ras_ih_data *data;
1152 /* in case we registe the IH before enable ras feature */
1153 obj = amdgpu_ras_create_obj(adev, &info->head);
1159 data = &obj->ih_data;
1160 /* add the callback.etc */
1161 *data = (struct ras_ih_data) {
1164 .element_size = sizeof(struct amdgpu_iv_entry),
1169 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1171 data->aligned_element_size = ALIGN(data->element_size, 8);
1172 /* the ring can store 64 iv entries. */
1173 data->ring_size = 64 * data->aligned_element_size;
1174 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1186 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1188 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1189 struct ras_manager *obj, *tmp;
1191 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1192 struct ras_ih_if info = {
1195 amdgpu_ras_interrupt_remove_handler(adev, &info);
1202 /* recovery begin */
1204 /* return 0 on success.
1205 * caller need free bps.
1207 static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
1208 struct ras_badpage **bps, unsigned int *count)
1210 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1211 struct ras_err_handler_data *data;
1215 if (!con || !con->eh_data || !bps || !count)
1218 mutex_lock(&con->recovery_lock);
1219 data = con->eh_data;
1220 if (!data || data->count == 0) {
1225 *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
1231 for (; i < data->count; i++) {
1232 (*bps)[i] = (struct ras_badpage){
1233 .bp = data->bps[i].bp,
1234 .size = AMDGPU_GPU_PAGE_SIZE,
1238 if (data->last_reserved <= i)
1239 (*bps)[i].flags = 1;
1240 else if (data->bps[i].bo == NULL)
1241 (*bps)[i].flags = 2;
1244 *count = data->count;
1246 mutex_unlock(&con->recovery_lock);
1250 static void amdgpu_ras_do_recovery(struct work_struct *work)
1252 struct amdgpu_ras *ras =
1253 container_of(work, struct amdgpu_ras, recovery_work);
1255 amdgpu_device_gpu_recover(ras->adev, 0);
1256 atomic_set(&ras->in_recovery, 0);
1259 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1260 struct amdgpu_bo **bo_ptr)
1262 /* no need to free it actually. */
1263 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1267 /* reserve vram with size@offset */
1268 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1269 uint64_t offset, uint64_t size,
1270 struct amdgpu_bo **bo_ptr)
1272 struct ttm_operation_ctx ctx = { false, false };
1273 struct amdgpu_bo_param bp;
1276 struct amdgpu_bo *bo;
1280 memset(&bp, 0, sizeof(bp));
1282 bp.byte_align = PAGE_SIZE;
1283 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1284 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1285 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1286 bp.type = ttm_bo_type_kernel;
1289 r = amdgpu_bo_create(adev, &bp, &bo);
1293 r = amdgpu_bo_reserve(bo, false);
1297 offset = ALIGN(offset, PAGE_SIZE);
1298 for (i = 0; i < bo->placement.num_placement; ++i) {
1299 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1300 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1303 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1304 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1308 r = amdgpu_bo_pin_restricted(bo,
1309 AMDGPU_GEM_DOMAIN_VRAM,
1318 amdgpu_bo_unreserve(bo);
1322 amdgpu_bo_unreserve(bo);
1324 amdgpu_bo_unref(&bo);
1328 /* alloc/realloc bps array */
1329 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1330 struct ras_err_handler_data *data, int pages)
1332 unsigned int old_space = data->count + data->space_left;
1333 unsigned int new_space = old_space + pages;
1334 unsigned int align_space = ALIGN(new_space, 1024);
1335 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1341 memcpy(tmp, data->bps,
1342 data->count * sizeof(*data->bps));
1347 data->space_left += align_space - old_space;
1351 /* it deal with vram only. */
1352 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1353 unsigned long *bps, int pages)
1355 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1356 struct ras_err_handler_data *data;
1360 if (!con || !con->eh_data || !bps || pages <= 0)
1363 mutex_lock(&con->recovery_lock);
1364 data = con->eh_data;
1368 if (data->space_left <= pages)
1369 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1375 data->bps[data->count++].bp = bps[i];
1377 data->space_left -= pages;
1379 mutex_unlock(&con->recovery_lock);
1384 /* called in gpu recovery/init */
1385 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1387 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1388 struct ras_err_handler_data *data;
1390 struct amdgpu_bo *bo;
1393 if (!con || !con->eh_data)
1396 mutex_lock(&con->recovery_lock);
1397 data = con->eh_data;
1400 /* reserve vram at driver post stage. */
1401 for (i = data->last_reserved; i < data->count; i++) {
1402 bp = data->bps[i].bp;
1404 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1406 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1408 data->bps[i].bo = bo;
1409 data->last_reserved = i + 1;
1412 mutex_unlock(&con->recovery_lock);
1416 /* called when driver unload */
1417 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1419 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1420 struct ras_err_handler_data *data;
1421 struct amdgpu_bo *bo;
1424 if (!con || !con->eh_data)
1427 mutex_lock(&con->recovery_lock);
1428 data = con->eh_data;
1432 for (i = data->last_reserved - 1; i >= 0; i--) {
1433 bo = data->bps[i].bo;
1435 amdgpu_ras_release_vram(adev, &bo);
1437 data->bps[i].bo = bo;
1438 data->last_reserved = i;
1441 mutex_unlock(&con->recovery_lock);
1445 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1448 * write the array to eeprom when SMU disabled.
1453 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1456 * read the array to eeprom when SMU disabled.
1461 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1463 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1464 struct ras_err_handler_data **data = &con->eh_data;
1466 *data = kmalloc(sizeof(**data),
1467 GFP_KERNEL|__GFP_ZERO);
1471 mutex_init(&con->recovery_lock);
1472 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1473 atomic_set(&con->in_recovery, 0);
1476 amdgpu_ras_load_bad_pages(adev);
1477 amdgpu_ras_reserve_bad_pages(adev);
1482 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1484 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1485 struct ras_err_handler_data *data = con->eh_data;
1487 cancel_work_sync(&con->recovery_work);
1488 amdgpu_ras_save_bad_pages(adev);
1489 amdgpu_ras_release_bad_pages(adev);
1491 mutex_lock(&con->recovery_lock);
1492 con->eh_data = NULL;
1495 mutex_unlock(&con->recovery_lock);
1501 /* return 0 if ras will reset gpu and repost.*/
1502 int amdgpu_ras_request_reset_on_boot(struct amdgpu_device *adev,
1505 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1510 ras->flags |= AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1515 * check hardware's ras ability which will be saved in hw_supported.
1516 * if hardware does not support ras, we can skip some ras initializtion and
1517 * forbid some ras operations from IP.
1518 * if software itself, say boot parameter, limit the ras ability. We still
1519 * need allow IP do some limited operations, like disable. In such case,
1520 * we have to initialize ras as normal. but need check if operation is
1521 * allowed or not in each function.
1523 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1524 uint32_t *hw_supported, uint32_t *supported)
1529 if (amdgpu_sriov_vf(adev) ||
1530 adev->asic_type != CHIP_VEGA20)
1533 if (adev->is_atom_fw &&
1534 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1535 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1536 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1538 *supported = amdgpu_ras_enable == 0 ?
1539 0 : *hw_supported & amdgpu_ras_mask;
1542 int amdgpu_ras_init(struct amdgpu_device *adev)
1544 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1549 con = kmalloc(sizeof(struct amdgpu_ras) +
1550 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1551 GFP_KERNEL|__GFP_ZERO);
1555 con->objs = (struct ras_manager *)(con + 1);
1557 amdgpu_ras_set_context(adev, con);
1559 amdgpu_ras_check_supported(adev, &con->hw_supported,
1561 if (!con->hw_supported) {
1562 amdgpu_ras_set_context(adev, NULL);
1568 INIT_LIST_HEAD(&con->head);
1569 /* Might need get this flag from vbios. */
1570 con->flags = RAS_DEFAULT_FLAGS;
1572 if (amdgpu_ras_recovery_init(adev))
1575 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1577 if (amdgpu_ras_fs_init(adev))
1580 DRM_INFO("RAS INFO: ras initialized successfully, "
1581 "hardware ability[%x] ras_mask[%x]\n",
1582 con->hw_supported, con->supported);
1585 amdgpu_ras_recovery_fini(adev);
1587 amdgpu_ras_set_context(adev, NULL);
1593 /* do some init work after IP late init as dependence.
1594 * and it runs in resume/gpu reset/booting up cases.
1596 void amdgpu_ras_resume(struct amdgpu_device *adev)
1598 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1599 struct ras_manager *obj, *tmp;
1604 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1605 /* Set up all other IPs which are not implemented. There is a
1606 * tricky thing that IP's actual ras error type should be
1607 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1608 * ERROR_NONE make sense anyway.
1610 amdgpu_ras_enable_all_features(adev, 1);
1612 /* We enable ras on all hw_supported block, but as boot
1613 * parameter might disable some of them and one or more IP has
1614 * not implemented yet. So we disable them on behalf.
1616 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1617 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1618 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1619 /* there should be no any reference. */
1620 WARN_ON(alive_obj(obj));
1625 if (con->flags & AMDGPU_RAS_FLAG_INIT_NEED_RESET) {
1626 con->flags &= ~AMDGPU_RAS_FLAG_INIT_NEED_RESET;
1627 /* setup ras obj state as disabled.
1628 * for init_by_vbios case.
1629 * if we want to enable ras, just enable it in a normal way.
1630 * If we want do disable it, need setup ras obj as enabled,
1631 * then issue another TA disable cmd.
1632 * See feature_enable_on_boot
1634 amdgpu_ras_disable_all_features(adev, 1);
1635 amdgpu_ras_reset_gpu(adev, 0);
1639 void amdgpu_ras_suspend(struct amdgpu_device *adev)
1641 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1646 amdgpu_ras_disable_all_features(adev, 0);
1647 /* Make sure all ras objects are disabled. */
1649 amdgpu_ras_disable_all_features(adev, 1);
1652 /* do some fini work before IP fini as dependence */
1653 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1655 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1660 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1661 amdgpu_ras_disable_all_features(adev, 0);
1662 amdgpu_ras_recovery_fini(adev);
1666 int amdgpu_ras_fini(struct amdgpu_device *adev)
1668 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1673 amdgpu_ras_fs_fini(adev);
1674 amdgpu_ras_interrupt_remove_all(adev);
1676 WARN(con->features, "Feature mask is not cleared");
1679 amdgpu_ras_disable_all_features(adev, 1);
1681 amdgpu_ras_set_context(adev, NULL);