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;
95 const char *ras_error_string[] = {
99 "multi_uncorrectable",
103 const char *ras_block_string[] = {
120 #define ras_err_str(i) (ras_error_string[ffs(i)])
121 #define ras_block_str(i) (ras_block_string[i])
123 #define AMDGPU_RAS_FLAG_INIT_BY_VBIOS 1
124 #define RAS_DEFAULT_FLAGS (AMDGPU_RAS_FLAG_INIT_BY_VBIOS)
126 static void amdgpu_ras_self_test(struct amdgpu_device *adev)
131 static ssize_t amdgpu_ras_debugfs_read(struct file *f, char __user *buf,
132 size_t size, loff_t *pos)
134 struct ras_manager *obj = (struct ras_manager *)file_inode(f)->i_private;
135 struct ras_query_if info = {
141 if (amdgpu_ras_error_query(obj->adev, &info))
144 s = snprintf(val, sizeof(val), "%s: %lu\n%s: %lu\n",
146 "ce", info.ce_count);
151 s = min_t(u64, s, size);
154 if (copy_to_user(buf, &val[*pos], s))
162 static const struct file_operations amdgpu_ras_debugfs_ops = {
163 .owner = THIS_MODULE,
164 .read = amdgpu_ras_debugfs_read,
166 .llseek = default_llseek
169 static int amdgpu_ras_find_block_id_by_name(const char *name, int *block_id)
173 for (i = 0; i < ARRAY_SIZE(ras_block_string); i++) {
175 if (strcmp(name, ras_block_str(i)) == 0)
181 static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
182 const char __user *buf, size_t size,
183 loff_t *pos, struct ras_debug_if *data)
185 ssize_t s = min_t(u64, 64, size);
197 memset(str, 0, sizeof(str));
198 memset(data, 0, sizeof(*data));
200 if (copy_from_user(str, buf, s))
203 if (sscanf(str, "disable %32s", block_name) == 1)
205 else if (sscanf(str, "enable %32s %8s", block_name, err) == 2)
207 else if (sscanf(str, "inject %32s %8s", block_name, err) == 2)
209 else if (str[0] && str[1] && str[2] && str[3])
210 /* ascii string, but commands are not matched. */
214 if (amdgpu_ras_find_block_id_by_name(block_name, &block_id))
217 data->head.block = block_id;
218 data->head.type = memcmp("ue", err, 2) == 0 ?
219 AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE :
220 AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE;
224 if (sscanf(str, "%*s %*s %*s %llu %llu",
225 &address, &value) != 2)
226 if (sscanf(str, "%*s %*s %*s 0x%llx 0x%llx",
227 &address, &value) != 2)
229 data->inject.address = address;
230 data->inject.value = value;
233 if (size < sizeof(*data))
236 if (copy_from_user(data, buf, sizeof(*data)))
243 * DOC: ras debugfs control interface
245 * It accepts struct ras_debug_if who has two members.
247 * First member: ras_debug_if::head or ras_debug_if::inject.
249 * head is used to indicate which IP block will be under control.
251 * head has four members, they are block, type, sub_block_index, name.
252 * block: which IP will be under control.
253 * type: what kind of error will be enabled/disabled/injected.
254 * sub_block_index: some IPs have subcomponets. say, GFX, sDMA.
255 * name: the name of IP.
257 * inject has two more members than head, they are address, value.
258 * As their names indicate, inject operation will write the
259 * value to the address.
261 * Second member: struct ras_debug_if::op.
262 * It has three kinds of operations.
263 * 0: disable RAS on the block. Take ::head as its data.
264 * 1: enable RAS on the block. Take ::head as its data.
265 * 2: inject errors on the block. Take ::inject as its data.
267 * How to use the interface?
269 * copy the struct ras_debug_if in your codes and initialize it.
270 * write the struct to the control node.
273 * echo op block [error [address value]] > .../ras/ras_ctrl
274 * op: disable, enable, inject
275 * disable: only block is needed
276 * enable: block and error are needed
277 * inject: error, address, value are needed
278 * block: umc, smda, gfx, .........
279 * see ras_block_string[] for details
281 * ue: multi_uncorrectable
282 * ce: single_correctable
284 * here are some examples for bash commands,
285 * echo inject umc ue 0x0 0x0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
286 * echo inject umc ce 0 0 > /sys/kernel/debug/dri/0/ras/ras_ctrl
287 * echo disable umc > /sys/kernel/debug/dri/0/ras/ras_ctrl
289 * How to check the result?
291 * For disable/enable, please check ras features at
292 * /sys/class/drm/card[0/1/2...]/device/ras/features
294 * For inject, please check corresponding err count at
295 * /sys/class/drm/card[0/1/2...]/device/ras/[gfx/sdma/...]_err_count
297 * NOTE: operation is only allowed on blocks which are supported.
298 * Please check ras mask at /sys/module/amdgpu/parameters/ras_mask
300 static ssize_t amdgpu_ras_debugfs_ctrl_write(struct file *f, const char __user *buf,
301 size_t size, loff_t *pos)
303 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
304 struct ras_debug_if data;
307 ret = amdgpu_ras_debugfs_ctrl_parse_data(f, buf, size, pos, &data);
311 if (!amdgpu_ras_is_supported(adev, data.head.block))
316 ret = amdgpu_ras_feature_enable(adev, &data.head, 0);
319 ret = amdgpu_ras_feature_enable(adev, &data.head, 1);
322 ret = amdgpu_ras_error_inject(adev, &data.inject);
335 static const struct file_operations amdgpu_ras_debugfs_ctrl_ops = {
336 .owner = THIS_MODULE,
338 .write = amdgpu_ras_debugfs_ctrl_write,
339 .llseek = default_llseek
342 static ssize_t amdgpu_ras_sysfs_read(struct device *dev,
343 struct device_attribute *attr, char *buf)
345 struct ras_manager *obj = container_of(attr, struct ras_manager, sysfs_attr);
346 struct ras_query_if info = {
350 if (amdgpu_ras_error_query(obj->adev, &info))
353 return snprintf(buf, PAGE_SIZE, "%s: %lu\n%s: %lu\n",
355 "ce", info.ce_count);
360 #define get_obj(obj) do { (obj)->use++; } while (0)
361 #define alive_obj(obj) ((obj)->use)
363 static inline void put_obj(struct ras_manager *obj)
365 if (obj && --obj->use == 0)
366 list_del(&obj->node);
367 if (obj && obj->use < 0) {
368 DRM_ERROR("RAS ERROR: Unbalance obj(%s) use\n", obj->head.name);
372 /* make one obj and return it. */
373 static struct ras_manager *amdgpu_ras_create_obj(struct amdgpu_device *adev,
374 struct ras_common_if *head)
376 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
377 struct ras_manager *obj;
382 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
385 obj = &con->objs[head->block];
386 /* already exist. return obj? */
392 list_add(&obj->node, &con->head);
398 /* return an obj equal to head, or the first when head is NULL */
399 static struct ras_manager *amdgpu_ras_find_obj(struct amdgpu_device *adev,
400 struct ras_common_if *head)
402 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
403 struct ras_manager *obj;
410 if (head->block >= AMDGPU_RAS_BLOCK_COUNT)
413 obj = &con->objs[head->block];
415 if (alive_obj(obj)) {
416 WARN_ON(head->block != obj->head.block);
420 for (i = 0; i < AMDGPU_RAS_BLOCK_COUNT; i++) {
422 if (alive_obj(obj)) {
423 WARN_ON(i != obj->head.block);
433 /* feature ctl begin */
434 static int amdgpu_ras_is_feature_allowed(struct amdgpu_device *adev,
435 struct ras_common_if *head)
437 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
439 return con->hw_supported & BIT(head->block);
442 static int amdgpu_ras_is_feature_enabled(struct amdgpu_device *adev,
443 struct ras_common_if *head)
445 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
447 return con->features & BIT(head->block);
451 * if obj is not created, then create one.
452 * set feature enable flag.
454 static int __amdgpu_ras_feature_enable(struct amdgpu_device *adev,
455 struct ras_common_if *head, int enable)
457 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
458 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
460 /* If hardware does not support ras, then do not create obj.
461 * But if hardware support ras, we can create the obj.
462 * Ras framework checks con->hw_supported to see if it need do
463 * corresponding initialization.
464 * IP checks con->support to see if it need disable ras.
466 if (!amdgpu_ras_is_feature_allowed(adev, head))
468 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
473 obj = amdgpu_ras_create_obj(adev, head);
477 /* In case we create obj somewhere else */
480 con->features |= BIT(head->block);
482 if (obj && amdgpu_ras_is_feature_enabled(adev, head)) {
483 con->features &= ~BIT(head->block);
491 /* wrapper of psp_ras_enable_features */
492 int amdgpu_ras_feature_enable(struct amdgpu_device *adev,
493 struct ras_common_if *head, bool enable)
495 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
496 union ta_ras_cmd_input info;
503 info.disable_features = (struct ta_ras_disable_features_input) {
504 .block_id = amdgpu_ras_block_to_ta(head->block),
505 .error_type = amdgpu_ras_error_to_ta(head->type),
508 info.enable_features = (struct ta_ras_enable_features_input) {
509 .block_id = amdgpu_ras_block_to_ta(head->block),
510 .error_type = amdgpu_ras_error_to_ta(head->type),
514 /* Do not enable if it is not allowed. */
515 WARN_ON(enable && !amdgpu_ras_is_feature_allowed(adev, head));
516 /* Are we alerady in that state we are going to set? */
517 if (!(!!enable ^ !!amdgpu_ras_is_feature_enabled(adev, head)))
520 ret = psp_ras_enable_features(&adev->psp, &info, enable);
522 DRM_ERROR("RAS ERROR: %s %s feature failed ret %d\n",
523 enable ? "enable":"disable",
524 ras_block_str(head->block),
530 __amdgpu_ras_feature_enable(adev, head, enable);
535 /* Only used in device probe stage and called only once. */
536 int amdgpu_ras_feature_enable_on_boot(struct amdgpu_device *adev,
537 struct ras_common_if *head, bool enable)
539 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
545 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
546 /* If ras is enabled by vbios, we set up ras object first in
547 * both case. For enable, that is all what we need do. For
548 * disable, we need perform a ras TA disable cmd after that.
550 ret = __amdgpu_ras_feature_enable(adev, head, 1);
555 ret = amdgpu_ras_feature_enable(adev, head, 0);
557 ret = amdgpu_ras_feature_enable(adev, head, enable);
562 static int amdgpu_ras_disable_all_features(struct amdgpu_device *adev,
565 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
566 struct ras_manager *obj, *tmp;
568 list_for_each_entry_safe(obj, tmp, &con->head, node) {
570 * aka just release the obj and corresponding flags
573 if (__amdgpu_ras_feature_enable(adev, &obj->head, 0))
576 if (amdgpu_ras_feature_enable(adev, &obj->head, 0))
581 return con->features;
584 static int amdgpu_ras_enable_all_features(struct amdgpu_device *adev,
587 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
588 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
590 const enum amdgpu_ras_error_type default_ras_type =
591 AMDGPU_RAS_ERROR__NONE;
593 for (i = 0; i < ras_block_count; i++) {
594 struct ras_common_if head = {
596 .type = default_ras_type,
597 .sub_block_index = 0,
599 strcpy(head.name, ras_block_str(i));
602 * bypass psp. vbios enable ras for us.
603 * so just create the obj
605 if (__amdgpu_ras_feature_enable(adev, &head, 1))
608 if (amdgpu_ras_feature_enable(adev, &head, 1))
613 return con->features;
615 /* feature ctl end */
617 /* query/inject/cure begin */
618 int amdgpu_ras_error_query(struct amdgpu_device *adev,
619 struct ras_query_if *info)
621 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
625 /* TODO might read the register to read the count */
627 info->ue_count = obj->err_data.ue_count;
628 info->ce_count = obj->err_data.ce_count;
633 /* wrapper of psp_ras_trigger_error */
634 int amdgpu_ras_error_inject(struct amdgpu_device *adev,
635 struct ras_inject_if *info)
637 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
638 struct ta_ras_trigger_error_input block_info = {
639 .block_id = amdgpu_ras_block_to_ta(info->head.block),
640 .inject_error_type = amdgpu_ras_error_to_ta(info->head.type),
641 .sub_block_index = info->head.sub_block_index,
642 .address = info->address,
643 .value = info->value,
650 ret = psp_ras_trigger_error(&adev->psp, &block_info);
652 DRM_ERROR("RAS ERROR: inject %s error failed ret %d\n",
653 ras_block_str(info->head.block),
659 int amdgpu_ras_error_cure(struct amdgpu_device *adev,
660 struct ras_cure_if *info)
662 /* psp fw has no cure interface for now. */
666 /* get the total error counts on all IPs */
667 int amdgpu_ras_query_error_count(struct amdgpu_device *adev,
670 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
671 struct ras_manager *obj;
672 struct ras_err_data data = {0, 0};
677 list_for_each_entry(obj, &con->head, node) {
678 struct ras_query_if info = {
682 if (amdgpu_ras_error_query(adev, &info))
685 data.ce_count += info.ce_count;
686 data.ue_count += info.ue_count;
689 return is_ce ? data.ce_count : data.ue_count;
691 /* query/inject/cure end */
696 static ssize_t amdgpu_ras_sysfs_features_read(struct device *dev,
697 struct device_attribute *attr, char *buf)
699 struct amdgpu_ras *con =
700 container_of(attr, struct amdgpu_ras, features_attr);
701 struct drm_device *ddev = dev_get_drvdata(dev);
702 struct amdgpu_device *adev = ddev->dev_private;
703 struct ras_common_if head;
704 int ras_block_count = AMDGPU_RAS_BLOCK_COUNT;
707 struct ras_manager *obj;
709 s = scnprintf(buf, PAGE_SIZE, "feature mask: 0x%x\n", con->features);
711 for (i = 0; i < ras_block_count; i++) {
714 if (amdgpu_ras_is_feature_enabled(adev, &head)) {
715 obj = amdgpu_ras_find_obj(adev, &head);
716 s += scnprintf(&buf[s], PAGE_SIZE - s,
719 ras_err_str(obj->head.type));
721 s += scnprintf(&buf[s], PAGE_SIZE - s,
729 static int amdgpu_ras_sysfs_create_feature_node(struct amdgpu_device *adev)
731 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
732 struct attribute *attrs[] = {
733 &con->features_attr.attr,
736 struct attribute_group group = {
741 con->features_attr = (struct device_attribute) {
746 .show = amdgpu_ras_sysfs_features_read,
748 sysfs_attr_init(attrs[0]);
750 return sysfs_create_group(&adev->dev->kobj, &group);
753 static int amdgpu_ras_sysfs_remove_feature_node(struct amdgpu_device *adev)
755 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
756 struct attribute *attrs[] = {
757 &con->features_attr.attr,
760 struct attribute_group group = {
765 sysfs_remove_group(&adev->dev->kobj, &group);
770 int amdgpu_ras_sysfs_create(struct amdgpu_device *adev,
771 struct ras_fs_if *head)
773 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
775 if (!obj || obj->attr_inuse)
780 memcpy(obj->fs_data.sysfs_name,
782 sizeof(obj->fs_data.sysfs_name));
784 obj->sysfs_attr = (struct device_attribute){
786 .name = obj->fs_data.sysfs_name,
789 .show = amdgpu_ras_sysfs_read,
791 sysfs_attr_init(&obj->sysfs_attr.attr);
793 if (sysfs_add_file_to_group(&adev->dev->kobj,
794 &obj->sysfs_attr.attr,
805 int amdgpu_ras_sysfs_remove(struct amdgpu_device *adev,
806 struct ras_common_if *head)
808 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
810 if (!obj || !obj->attr_inuse)
813 sysfs_remove_file_from_group(&adev->dev->kobj,
814 &obj->sysfs_attr.attr,
822 static int amdgpu_ras_sysfs_remove_all(struct amdgpu_device *adev)
824 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
825 struct ras_manager *obj, *tmp;
827 list_for_each_entry_safe(obj, tmp, &con->head, node) {
828 amdgpu_ras_sysfs_remove(adev, &obj->head);
831 amdgpu_ras_sysfs_remove_feature_node(adev);
838 static int amdgpu_ras_debugfs_create_ctrl_node(struct amdgpu_device *adev)
840 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
841 struct drm_minor *minor = adev->ddev->primary;
842 struct dentry *root = minor->debugfs_root, *dir;
845 dir = debugfs_create_dir("ras", root);
851 ent = debugfs_create_file("ras_ctrl",
852 S_IWUGO | S_IRUGO, con->dir,
853 adev, &amdgpu_ras_debugfs_ctrl_ops);
855 debugfs_remove(con->dir);
863 int amdgpu_ras_debugfs_create(struct amdgpu_device *adev,
864 struct ras_fs_if *head)
866 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
867 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head->head);
870 if (!obj || obj->ent)
875 memcpy(obj->fs_data.debugfs_name,
877 sizeof(obj->fs_data.debugfs_name));
879 ent = debugfs_create_file(obj->fs_data.debugfs_name,
880 S_IWUGO | S_IRUGO, con->dir,
881 obj, &amdgpu_ras_debugfs_ops);
891 int amdgpu_ras_debugfs_remove(struct amdgpu_device *adev,
892 struct ras_common_if *head)
894 struct ras_manager *obj = amdgpu_ras_find_obj(adev, head);
896 if (!obj || !obj->ent)
899 debugfs_remove(obj->ent);
906 static int amdgpu_ras_debugfs_remove_all(struct amdgpu_device *adev)
908 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
909 struct ras_manager *obj, *tmp;
911 list_for_each_entry_safe(obj, tmp, &con->head, node) {
912 amdgpu_ras_debugfs_remove(adev, &obj->head);
915 debugfs_remove(con->ent);
916 debugfs_remove(con->dir);
926 static int amdgpu_ras_fs_init(struct amdgpu_device *adev)
928 amdgpu_ras_sysfs_create_feature_node(adev);
929 amdgpu_ras_debugfs_create_ctrl_node(adev);
934 static int amdgpu_ras_fs_fini(struct amdgpu_device *adev)
936 amdgpu_ras_debugfs_remove_all(adev);
937 amdgpu_ras_sysfs_remove_all(adev);
943 static void amdgpu_ras_interrupt_handler(struct ras_manager *obj)
945 struct ras_ih_data *data = &obj->ih_data;
946 struct amdgpu_iv_entry entry;
949 while (data->rptr != data->wptr) {
951 memcpy(&entry, &data->ring[data->rptr],
955 data->rptr = (data->aligned_element_size +
956 data->rptr) % data->ring_size;
958 /* Let IP handle its data, maybe we need get the output
959 * from the callback to udpate the error type/count, etc
962 ret = data->cb(obj->adev, &entry);
963 /* ue will trigger an interrupt, and in that case
964 * we need do a reset to recovery the whole system.
965 * But leave IP do that recovery, here we just dispatch
968 if (ret == AMDGPU_RAS_UE) {
969 obj->err_data.ue_count++;
971 /* Might need get ce count by register, but not all IP
972 * saves ce count, some IP just use one bit or two bits
973 * to indicate ce happened.
979 static void amdgpu_ras_interrupt_process_handler(struct work_struct *work)
981 struct ras_ih_data *data =
982 container_of(work, struct ras_ih_data, ih_work);
983 struct ras_manager *obj =
984 container_of(data, struct ras_manager, ih_data);
986 amdgpu_ras_interrupt_handler(obj);
989 int amdgpu_ras_interrupt_dispatch(struct amdgpu_device *adev,
990 struct ras_dispatch_if *info)
992 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
993 struct ras_ih_data *data = &obj->ih_data;
998 if (data->inuse == 0)
1001 /* Might be overflow... */
1002 memcpy(&data->ring[data->wptr], info->entry,
1003 data->element_size);
1006 data->wptr = (data->aligned_element_size +
1007 data->wptr) % data->ring_size;
1009 schedule_work(&data->ih_work);
1014 int amdgpu_ras_interrupt_remove_handler(struct amdgpu_device *adev,
1015 struct ras_ih_if *info)
1017 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1018 struct ras_ih_data *data;
1023 data = &obj->ih_data;
1024 if (data->inuse == 0)
1027 cancel_work_sync(&data->ih_work);
1030 memset(data, 0, sizeof(*data));
1036 int amdgpu_ras_interrupt_add_handler(struct amdgpu_device *adev,
1037 struct ras_ih_if *info)
1039 struct ras_manager *obj = amdgpu_ras_find_obj(adev, &info->head);
1040 struct ras_ih_data *data;
1043 /* in case we registe the IH before enable ras feature */
1044 obj = amdgpu_ras_create_obj(adev, &info->head);
1050 data = &obj->ih_data;
1051 /* add the callback.etc */
1052 *data = (struct ras_ih_data) {
1055 .element_size = sizeof(struct amdgpu_iv_entry),
1060 INIT_WORK(&data->ih_work, amdgpu_ras_interrupt_process_handler);
1062 data->aligned_element_size = ALIGN(data->element_size, 8);
1063 /* the ring can store 64 iv entries. */
1064 data->ring_size = 64 * data->aligned_element_size;
1065 data->ring = kmalloc(data->ring_size, GFP_KERNEL);
1077 static int amdgpu_ras_interrupt_remove_all(struct amdgpu_device *adev)
1079 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1080 struct ras_manager *obj, *tmp;
1082 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1083 struct ras_ih_if info = {
1086 amdgpu_ras_interrupt_remove_handler(adev, &info);
1093 /* recovery begin */
1094 static void amdgpu_ras_do_recovery(struct work_struct *work)
1096 struct amdgpu_ras *ras =
1097 container_of(work, struct amdgpu_ras, recovery_work);
1099 amdgpu_device_gpu_recover(ras->adev, 0);
1100 atomic_set(&ras->in_recovery, 0);
1103 static int amdgpu_ras_release_vram(struct amdgpu_device *adev,
1104 struct amdgpu_bo **bo_ptr)
1106 /* no need to free it actually. */
1107 amdgpu_bo_free_kernel(bo_ptr, NULL, NULL);
1111 /* reserve vram with size@offset */
1112 static int amdgpu_ras_reserve_vram(struct amdgpu_device *adev,
1113 uint64_t offset, uint64_t size,
1114 struct amdgpu_bo **bo_ptr)
1116 struct ttm_operation_ctx ctx = { false, false };
1117 struct amdgpu_bo_param bp;
1120 struct amdgpu_bo *bo;
1124 memset(&bp, 0, sizeof(bp));
1126 bp.byte_align = PAGE_SIZE;
1127 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1128 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
1129 AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
1130 bp.type = ttm_bo_type_kernel;
1133 r = amdgpu_bo_create(adev, &bp, &bo);
1137 r = amdgpu_bo_reserve(bo, false);
1141 offset = ALIGN(offset, PAGE_SIZE);
1142 for (i = 0; i < bo->placement.num_placement; ++i) {
1143 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1144 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1147 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1148 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem, &ctx);
1152 r = amdgpu_bo_pin_restricted(bo,
1153 AMDGPU_GEM_DOMAIN_VRAM,
1162 amdgpu_bo_unreserve(bo);
1166 amdgpu_bo_unreserve(bo);
1168 amdgpu_bo_unref(&bo);
1172 /* alloc/realloc bps array */
1173 static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
1174 struct ras_err_handler_data *data, int pages)
1176 unsigned int old_space = data->count + data->space_left;
1177 unsigned int new_space = old_space + pages;
1178 unsigned int align_space = ALIGN(new_space, 1024);
1179 void *tmp = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
1185 memcpy(tmp, data->bps,
1186 data->count * sizeof(*data->bps));
1191 data->space_left += align_space - old_space;
1195 /* it deal with vram only. */
1196 int amdgpu_ras_add_bad_pages(struct amdgpu_device *adev,
1197 unsigned long *bps, int pages)
1199 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1200 struct ras_err_handler_data *data;
1204 if (!con || !con->eh_data || !bps || pages <= 0)
1207 mutex_lock(&con->recovery_lock);
1208 data = con->eh_data;
1212 if (data->space_left <= pages)
1213 if (amdgpu_ras_realloc_eh_data_space(adev, data, pages)) {
1219 data->bps[data->count++].bp = bps[i];
1221 data->space_left -= pages;
1223 mutex_unlock(&con->recovery_lock);
1228 /* called in gpu recovery/init */
1229 int amdgpu_ras_reserve_bad_pages(struct amdgpu_device *adev)
1231 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1232 struct ras_err_handler_data *data;
1234 struct amdgpu_bo *bo;
1237 if (!con || !con->eh_data)
1240 mutex_lock(&con->recovery_lock);
1241 data = con->eh_data;
1244 /* reserve vram at driver post stage. */
1245 for (i = data->last_reserved; i < data->count; i++) {
1246 bp = data->bps[i].bp;
1248 if (amdgpu_ras_reserve_vram(adev, bp << PAGE_SHIFT,
1250 DRM_ERROR("RAS ERROR: reserve vram %llx fail\n", bp);
1252 data->bps[i].bo = bo;
1253 data->last_reserved = i + 1;
1256 mutex_unlock(&con->recovery_lock);
1260 /* called when driver unload */
1261 static int amdgpu_ras_release_bad_pages(struct amdgpu_device *adev)
1263 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1264 struct ras_err_handler_data *data;
1265 struct amdgpu_bo *bo;
1268 if (!con || !con->eh_data)
1271 mutex_lock(&con->recovery_lock);
1272 data = con->eh_data;
1276 for (i = data->last_reserved - 1; i >= 0; i--) {
1277 bo = data->bps[i].bo;
1279 amdgpu_ras_release_vram(adev, &bo);
1281 data->bps[i].bo = bo;
1282 data->last_reserved = i;
1285 mutex_unlock(&con->recovery_lock);
1289 static int amdgpu_ras_save_bad_pages(struct amdgpu_device *adev)
1292 * write the array to eeprom when SMU disabled.
1297 static int amdgpu_ras_load_bad_pages(struct amdgpu_device *adev)
1300 * read the array to eeprom when SMU disabled.
1305 static int amdgpu_ras_recovery_init(struct amdgpu_device *adev)
1307 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1308 struct ras_err_handler_data **data = &con->eh_data;
1310 *data = kmalloc(sizeof(**data),
1311 GFP_KERNEL|__GFP_ZERO);
1315 mutex_init(&con->recovery_lock);
1316 INIT_WORK(&con->recovery_work, amdgpu_ras_do_recovery);
1317 atomic_set(&con->in_recovery, 0);
1320 amdgpu_ras_load_bad_pages(adev);
1321 amdgpu_ras_reserve_bad_pages(adev);
1326 static int amdgpu_ras_recovery_fini(struct amdgpu_device *adev)
1328 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1329 struct ras_err_handler_data *data = con->eh_data;
1331 cancel_work_sync(&con->recovery_work);
1332 amdgpu_ras_save_bad_pages(adev);
1333 amdgpu_ras_release_bad_pages(adev);
1335 mutex_lock(&con->recovery_lock);
1336 con->eh_data = NULL;
1339 mutex_unlock(&con->recovery_lock);
1346 * check hardware's ras ability which will be saved in hw_supported.
1347 * if hardware does not support ras, we can skip some ras initializtion and
1348 * forbid some ras operations from IP.
1349 * if software itself, say boot parameter, limit the ras ability. We still
1350 * need allow IP do some limited operations, like disable. In such case,
1351 * we have to initialize ras as normal. but need check if operation is
1352 * allowed or not in each function.
1354 static void amdgpu_ras_check_supported(struct amdgpu_device *adev,
1355 uint32_t *hw_supported, uint32_t *supported)
1360 if (amdgpu_sriov_vf(adev) ||
1361 adev->asic_type != CHIP_VEGA20)
1364 if (adev->is_atom_fw &&
1365 (amdgpu_atomfirmware_mem_ecc_supported(adev) ||
1366 amdgpu_atomfirmware_sram_ecc_supported(adev)))
1367 *hw_supported = AMDGPU_RAS_BLOCK_MASK;
1369 *supported = amdgpu_ras_enable == 0 ?
1370 0 : *hw_supported & amdgpu_ras_mask;
1373 int amdgpu_ras_init(struct amdgpu_device *adev)
1375 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1380 con = kmalloc(sizeof(struct amdgpu_ras) +
1381 sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
1382 GFP_KERNEL|__GFP_ZERO);
1386 con->objs = (struct ras_manager *)(con + 1);
1388 amdgpu_ras_set_context(adev, con);
1390 amdgpu_ras_check_supported(adev, &con->hw_supported,
1393 INIT_LIST_HEAD(&con->head);
1394 /* Might need get this flag from vbios. */
1395 con->flags = RAS_DEFAULT_FLAGS;
1397 if (amdgpu_ras_recovery_init(adev))
1400 amdgpu_ras_mask &= AMDGPU_RAS_BLOCK_MASK;
1402 if (amdgpu_ras_fs_init(adev))
1405 amdgpu_ras_self_test(adev);
1407 DRM_INFO("RAS INFO: ras initialized successfully, "
1408 "hardware ability[%x] ras_mask[%x]\n",
1409 con->hw_supported, con->supported);
1412 amdgpu_ras_recovery_fini(adev);
1414 amdgpu_ras_set_context(adev, NULL);
1420 /* do some init work after IP late init as dependence */
1421 void amdgpu_ras_post_init(struct amdgpu_device *adev)
1423 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1424 struct ras_manager *obj, *tmp;
1429 if (con->flags & AMDGPU_RAS_FLAG_INIT_BY_VBIOS) {
1430 /* Set up all other IPs which are not implemented. There is a
1431 * tricky thing that IP's actual ras error type should be
1432 * MULTI_UNCORRECTABLE, but as driver does not handle it, so
1433 * ERROR_NONE make sense anyway.
1435 amdgpu_ras_enable_all_features(adev, 1);
1437 /* We enable ras on all hw_supported block, but as boot
1438 * parameter might disable some of them and one or more IP has
1439 * not implemented yet. So we disable them on behalf.
1441 list_for_each_entry_safe(obj, tmp, &con->head, node) {
1442 if (!amdgpu_ras_is_supported(adev, obj->head.block)) {
1443 amdgpu_ras_feature_enable(adev, &obj->head, 0);
1444 /* there should be no any reference. */
1445 WARN_ON(alive_obj(obj));
1451 /* do some fini work before IP fini as dependence */
1452 int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
1454 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1459 /* Need disable ras on all IPs here before ip [hw/sw]fini */
1460 amdgpu_ras_disable_all_features(adev, 0);
1461 amdgpu_ras_recovery_fini(adev);
1465 int amdgpu_ras_fini(struct amdgpu_device *adev)
1467 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
1472 amdgpu_ras_fs_fini(adev);
1473 amdgpu_ras_interrupt_remove_all(adev);
1475 WARN(con->features, "Feature mask is not cleared");
1478 amdgpu_ras_disable_all_features(adev, 1);
1480 amdgpu_ras_set_context(adev, NULL);