xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_mca.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
13907c492SJohn Clements /*
23907c492SJohn Clements  * Copyright 2021 Advanced Micro Devices, Inc.
33907c492SJohn Clements  *
43907c492SJohn Clements  * Permission is hereby granted, free of charge, to any person obtaining a
53907c492SJohn Clements  * copy of this software and associated documentation files (the "Software"),
63907c492SJohn Clements  * to deal in the Software without restriction, including without limitation
73907c492SJohn Clements  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83907c492SJohn Clements  * and/or sell copies of the Software, and to permit persons to whom the
93907c492SJohn Clements  * Software is furnished to do so, subject to the following conditions:
103907c492SJohn Clements  *
113907c492SJohn Clements  * The above copyright notice and this permission notice shall be included in
123907c492SJohn Clements  * all copies or substantial portions of the Software.
133907c492SJohn Clements  *
143907c492SJohn Clements  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
153907c492SJohn Clements  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
163907c492SJohn Clements  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
173907c492SJohn Clements  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
183907c492SJohn Clements  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
193907c492SJohn Clements  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
203907c492SJohn Clements  * OTHER DEALINGS IN THE SOFTWARE.
213907c492SJohn Clements  *
223907c492SJohn Clements  */
233907c492SJohn Clements #include "amdgpu_ras.h"
243907c492SJohn Clements #include "amdgpu.h"
253907c492SJohn Clements #include "amdgpu_mca.h"
263907c492SJohn Clements 
273907c492SJohn Clements #include "umc/umc_6_7_0_offset.h"
283907c492SJohn Clements #include "umc/umc_6_7_0_sh_mask.h"
293907c492SJohn Clements 
amdgpu_mca_is_deferred_error(struct amdgpu_device * adev,uint64_t mc_status)30afb617f3SYiPeng Chai static bool amdgpu_mca_is_deferred_error(struct amdgpu_device *adev,
31afb617f3SYiPeng Chai 					uint64_t mc_status)
32afb617f3SYiPeng Chai {
33afb617f3SYiPeng Chai 	if (adev->umc.ras->check_ecc_err_status)
34afb617f3SYiPeng Chai 		return adev->umc.ras->check_ecc_err_status(adev,
35afb617f3SYiPeng Chai 				AMDGPU_MCA_ERROR_TYPE_DE, &mc_status);
36afb617f3SYiPeng Chai 
37afb617f3SYiPeng Chai 	return false;
38afb617f3SYiPeng Chai }
39afb617f3SYiPeng Chai 
amdgpu_mca_query_correctable_error_count(struct amdgpu_device * adev,uint64_t mc_status_addr,unsigned long * error_count)403907c492SJohn Clements void amdgpu_mca_query_correctable_error_count(struct amdgpu_device *adev,
413907c492SJohn Clements 					      uint64_t mc_status_addr,
423907c492SJohn Clements 					      unsigned long *error_count)
433907c492SJohn Clements {
44640ae42eSJohn Clements 	uint64_t mc_status = RREG64_PCIE(mc_status_addr);
453907c492SJohn Clements 
463907c492SJohn Clements 	if (REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, Val) == 1 &&
473907c492SJohn Clements 	    REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, CECC) == 1)
483907c492SJohn Clements 		*error_count += 1;
493907c492SJohn Clements }
503907c492SJohn Clements 
amdgpu_mca_query_uncorrectable_error_count(struct amdgpu_device * adev,uint64_t mc_status_addr,unsigned long * error_count)513907c492SJohn Clements void amdgpu_mca_query_uncorrectable_error_count(struct amdgpu_device *adev,
523907c492SJohn Clements 						uint64_t mc_status_addr,
533907c492SJohn Clements 						unsigned long *error_count)
543907c492SJohn Clements {
55640ae42eSJohn Clements 	uint64_t mc_status = RREG64_PCIE(mc_status_addr);
563907c492SJohn Clements 
573907c492SJohn Clements 	if ((REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, Val) == 1) &&
583907c492SJohn Clements 	    (REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, Deferred) == 1 ||
593907c492SJohn Clements 	    REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, UECC) == 1 ||
603907c492SJohn Clements 	    REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, PCC) == 1 ||
613907c492SJohn Clements 	    REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, UC) == 1 ||
623907c492SJohn Clements 	    REG_GET_FIELD(mc_status, MCA_UMC_UMC0_MCUMC_STATUST0, TCC) == 1))
633907c492SJohn Clements 		*error_count += 1;
643907c492SJohn Clements }
653907c492SJohn Clements 
amdgpu_mca_reset_error_count(struct amdgpu_device * adev,uint64_t mc_status_addr)663907c492SJohn Clements void amdgpu_mca_reset_error_count(struct amdgpu_device *adev,
673907c492SJohn Clements 				  uint64_t mc_status_addr)
683907c492SJohn Clements {
69640ae42eSJohn Clements 	WREG64_PCIE(mc_status_addr, 0x0ULL);
703907c492SJohn Clements }
713907c492SJohn Clements 
amdgpu_mca_query_ras_error_count(struct amdgpu_device * adev,uint64_t mc_status_addr,void * ras_error_status)723907c492SJohn Clements void amdgpu_mca_query_ras_error_count(struct amdgpu_device *adev,
733907c492SJohn Clements 				      uint64_t mc_status_addr,
743907c492SJohn Clements 				      void *ras_error_status)
753907c492SJohn Clements {
763907c492SJohn Clements 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
773907c492SJohn Clements 
783907c492SJohn Clements 	amdgpu_mca_query_correctable_error_count(adev, mc_status_addr, &(err_data->ce_count));
793907c492SJohn Clements 	amdgpu_mca_query_uncorrectable_error_count(adev, mc_status_addr, &(err_data->ue_count));
803907c492SJohn Clements 
813907c492SJohn Clements 	amdgpu_mca_reset_error_count(adev, mc_status_addr);
823907c492SJohn Clements }
837f544c54SHawking Zhang 
amdgpu_mca_mp0_ras_sw_init(struct amdgpu_device * adev)847f544c54SHawking Zhang int amdgpu_mca_mp0_ras_sw_init(struct amdgpu_device *adev)
857f544c54SHawking Zhang {
867f544c54SHawking Zhang 	int err;
877f544c54SHawking Zhang 	struct amdgpu_mca_ras_block *ras;
887f544c54SHawking Zhang 
897f544c54SHawking Zhang 	if (!adev->mca.mp0.ras)
907f544c54SHawking Zhang 		return 0;
917f544c54SHawking Zhang 
927f544c54SHawking Zhang 	ras = adev->mca.mp0.ras;
937f544c54SHawking Zhang 
947f544c54SHawking Zhang 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
957f544c54SHawking Zhang 	if (err) {
967f544c54SHawking Zhang 		dev_err(adev->dev, "Failed to register mca.mp0 ras block!\n");
977f544c54SHawking Zhang 		return err;
987f544c54SHawking Zhang 	}
997f544c54SHawking Zhang 
1007f544c54SHawking Zhang 	strcpy(ras->ras_block.ras_comm.name, "mca.mp0");
1017f544c54SHawking Zhang 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__MCA;
1027f544c54SHawking Zhang 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
1037f544c54SHawking Zhang 	adev->mca.mp0.ras_if = &ras->ras_block.ras_comm;
1047f544c54SHawking Zhang 
1057f544c54SHawking Zhang 	return 0;
1067f544c54SHawking Zhang }
1077f544c54SHawking Zhang 
amdgpu_mca_mp1_ras_sw_init(struct amdgpu_device * adev)1087f544c54SHawking Zhang int amdgpu_mca_mp1_ras_sw_init(struct amdgpu_device *adev)
1097f544c54SHawking Zhang {
1107f544c54SHawking Zhang 	int err;
1117f544c54SHawking Zhang 	struct amdgpu_mca_ras_block *ras;
1127f544c54SHawking Zhang 
1137f544c54SHawking Zhang 	if (!adev->mca.mp1.ras)
1147f544c54SHawking Zhang 		return 0;
1157f544c54SHawking Zhang 
1167f544c54SHawking Zhang 	ras = adev->mca.mp1.ras;
1177f544c54SHawking Zhang 
1187f544c54SHawking Zhang 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
1197f544c54SHawking Zhang 	if (err) {
1207f544c54SHawking Zhang 		dev_err(adev->dev, "Failed to register mca.mp1 ras block!\n");
1217f544c54SHawking Zhang 		return err;
1227f544c54SHawking Zhang 	}
1237f544c54SHawking Zhang 
1247f544c54SHawking Zhang 	strcpy(ras->ras_block.ras_comm.name, "mca.mp1");
1257f544c54SHawking Zhang 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__MCA;
1267f544c54SHawking Zhang 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
1277f544c54SHawking Zhang 	adev->mca.mp1.ras_if = &ras->ras_block.ras_comm;
1287f544c54SHawking Zhang 
1297f544c54SHawking Zhang 	return 0;
1307f544c54SHawking Zhang }
1317f544c54SHawking Zhang 
amdgpu_mca_mpio_ras_sw_init(struct amdgpu_device * adev)1327f544c54SHawking Zhang int amdgpu_mca_mpio_ras_sw_init(struct amdgpu_device *adev)
1337f544c54SHawking Zhang {
1347f544c54SHawking Zhang 	int err;
1357f544c54SHawking Zhang 	struct amdgpu_mca_ras_block *ras;
1367f544c54SHawking Zhang 
1377f544c54SHawking Zhang 	if (!adev->mca.mpio.ras)
1387f544c54SHawking Zhang 		return 0;
1397f544c54SHawking Zhang 
1407f544c54SHawking Zhang 	ras = adev->mca.mpio.ras;
1417f544c54SHawking Zhang 
1427f544c54SHawking Zhang 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
1437f544c54SHawking Zhang 	if (err) {
1447f544c54SHawking Zhang 		dev_err(adev->dev, "Failed to register mca.mpio ras block!\n");
1457f544c54SHawking Zhang 		return err;
1467f544c54SHawking Zhang 	}
1477f544c54SHawking Zhang 
1487f544c54SHawking Zhang 	strcpy(ras->ras_block.ras_comm.name, "mca.mpio");
1497f544c54SHawking Zhang 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__MCA;
1507f544c54SHawking Zhang 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
1517f544c54SHawking Zhang 	adev->mca.mpio.ras_if = &ras->ras_block.ras_comm;
1527f544c54SHawking Zhang 
1537f544c54SHawking Zhang 	return 0;
1547f544c54SHawking Zhang }
1557ff607e2SYang Wang 
amdgpu_mca_bank_set_init(struct mca_bank_set * mca_set)1567e0357beSYang Wang static void amdgpu_mca_bank_set_init(struct mca_bank_set *mca_set)
15707c1db70SYang Wang {
15807c1db70SYang Wang 	if (!mca_set)
15907c1db70SYang Wang 		return;
16007c1db70SYang Wang 
16107c1db70SYang Wang 	memset(mca_set, 0, sizeof(*mca_set));
16207c1db70SYang Wang 	INIT_LIST_HEAD(&mca_set->list);
16307c1db70SYang Wang }
16407c1db70SYang Wang 
amdgpu_mca_bank_set_add_entry(struct mca_bank_set * mca_set,struct mca_bank_entry * entry)1657e0357beSYang Wang static int amdgpu_mca_bank_set_add_entry(struct mca_bank_set *mca_set, struct mca_bank_entry *entry)
16607c1db70SYang Wang {
16707c1db70SYang Wang 	struct mca_bank_node *node;
16807c1db70SYang Wang 
16907c1db70SYang Wang 	if (!entry)
17007c1db70SYang Wang 		return -EINVAL;
17107c1db70SYang Wang 
17207c1db70SYang Wang 	node = kvzalloc(sizeof(*node), GFP_KERNEL);
17307c1db70SYang Wang 	if (!node)
17407c1db70SYang Wang 		return -ENOMEM;
17507c1db70SYang Wang 
17607c1db70SYang Wang 	memcpy(&node->entry, entry, sizeof(*entry));
17707c1db70SYang Wang 
17807c1db70SYang Wang 	INIT_LIST_HEAD(&node->node);
17907c1db70SYang Wang 	list_add_tail(&node->node, &mca_set->list);
18007c1db70SYang Wang 
18107c1db70SYang Wang 	mca_set->nr_entries++;
18207c1db70SYang Wang 
18307c1db70SYang Wang 	return 0;
18407c1db70SYang Wang }
18507c1db70SYang Wang 
amdgpu_mca_bank_set_merge(struct mca_bank_set * mca_set,struct mca_bank_set * new)18676ad30f5SYang Wang static int amdgpu_mca_bank_set_merge(struct mca_bank_set *mca_set, struct mca_bank_set *new)
18776ad30f5SYang Wang {
18876ad30f5SYang Wang 	struct mca_bank_node *node;
18976ad30f5SYang Wang 
19076ad30f5SYang Wang 	list_for_each_entry(node, &new->list, node)
19176ad30f5SYang Wang 		amdgpu_mca_bank_set_add_entry(mca_set, &node->entry);
19276ad30f5SYang Wang 
19376ad30f5SYang Wang 	return 0;
19476ad30f5SYang Wang }
19576ad30f5SYang Wang 
amdgpu_mca_bank_set_remove_node(struct mca_bank_set * mca_set,struct mca_bank_node * node)1969817f061SYang Wang static void amdgpu_mca_bank_set_remove_node(struct mca_bank_set *mca_set, struct mca_bank_node *node)
19776ad30f5SYang Wang {
19876ad30f5SYang Wang 	if (!node)
1999817f061SYang Wang 		return;
20076ad30f5SYang Wang 
20176ad30f5SYang Wang 	list_del(&node->node);
20276ad30f5SYang Wang 	kvfree(node);
20376ad30f5SYang Wang 
20476ad30f5SYang Wang 	mca_set->nr_entries--;
20576ad30f5SYang Wang }
20676ad30f5SYang Wang 
amdgpu_mca_bank_set_release(struct mca_bank_set * mca_set)2077e0357beSYang Wang static void amdgpu_mca_bank_set_release(struct mca_bank_set *mca_set)
20807c1db70SYang Wang {
20907c1db70SYang Wang 	struct mca_bank_node *node, *tmp;
21007c1db70SYang Wang 
2119817f061SYang Wang 	if (list_empty(&mca_set->list))
2129817f061SYang Wang 		return;
2139817f061SYang Wang 
2149817f061SYang Wang 	list_for_each_entry_safe(node, tmp, &mca_set->list, node)
2159817f061SYang Wang 		amdgpu_mca_bank_set_remove_node(mca_set, node);
21607c1db70SYang Wang }
21707c1db70SYang Wang 
amdgpu_mca_smu_init_funcs(struct amdgpu_device * adev,const struct amdgpu_mca_smu_funcs * mca_funcs)2187ff607e2SYang Wang void amdgpu_mca_smu_init_funcs(struct amdgpu_device *adev, const struct amdgpu_mca_smu_funcs *mca_funcs)
2197ff607e2SYang Wang {
2207ff607e2SYang Wang 	struct amdgpu_mca *mca = &adev->mca;
2217ff607e2SYang Wang 
2227ff607e2SYang Wang 	mca->mca_funcs = mca_funcs;
2237ff607e2SYang Wang }
2247ff607e2SYang Wang 
amdgpu_mca_init(struct amdgpu_device * adev)22576ad30f5SYang Wang int amdgpu_mca_init(struct amdgpu_device *adev)
22676ad30f5SYang Wang {
22776ad30f5SYang Wang 	struct amdgpu_mca *mca = &adev->mca;
22876ad30f5SYang Wang 	struct mca_bank_cache *mca_cache;
22976ad30f5SYang Wang 	int i;
23076ad30f5SYang Wang 
2315eccab32SYang Wang 	atomic_set(&mca->ue_update_flag, 0);
2325eccab32SYang Wang 
23376ad30f5SYang Wang 	for (i = 0; i < ARRAY_SIZE(mca->mca_caches); i++) {
23476ad30f5SYang Wang 		mca_cache = &mca->mca_caches[i];
2358c9ee180SYang Wang 		mutex_init(&mca_cache->lock);
23676ad30f5SYang Wang 		amdgpu_mca_bank_set_init(&mca_cache->mca_set);
23776ad30f5SYang Wang 	}
23876ad30f5SYang Wang 
23976ad30f5SYang Wang 	return 0;
24076ad30f5SYang Wang }
24176ad30f5SYang Wang 
amdgpu_mca_fini(struct amdgpu_device * adev)24276ad30f5SYang Wang void amdgpu_mca_fini(struct amdgpu_device *adev)
24376ad30f5SYang Wang {
24476ad30f5SYang Wang 	struct amdgpu_mca *mca = &adev->mca;
24576ad30f5SYang Wang 	struct mca_bank_cache *mca_cache;
24676ad30f5SYang Wang 	int i;
24776ad30f5SYang Wang 
2485eccab32SYang Wang 	atomic_set(&mca->ue_update_flag, 0);
2495eccab32SYang Wang 
25076ad30f5SYang Wang 	for (i = 0; i < ARRAY_SIZE(mca->mca_caches); i++) {
25176ad30f5SYang Wang 		mca_cache = &mca->mca_caches[i];
25276ad30f5SYang Wang 		amdgpu_mca_bank_set_release(&mca_cache->mca_set);
2538c9ee180SYang Wang 		mutex_destroy(&mca_cache->lock);
25476ad30f5SYang Wang 	}
25576ad30f5SYang Wang }
25676ad30f5SYang Wang 
amdgpu_mca_reset(struct amdgpu_device * adev)25776ad30f5SYang Wang int amdgpu_mca_reset(struct amdgpu_device *adev)
25876ad30f5SYang Wang {
25976ad30f5SYang Wang 	amdgpu_mca_fini(adev);
26076ad30f5SYang Wang 
26176ad30f5SYang Wang 	return amdgpu_mca_init(adev);
26276ad30f5SYang Wang }
26376ad30f5SYang Wang 
amdgpu_mca_smu_set_debug_mode(struct amdgpu_device * adev,bool enable)2647ff607e2SYang Wang int amdgpu_mca_smu_set_debug_mode(struct amdgpu_device *adev, bool enable)
2657ff607e2SYang Wang {
2667ff607e2SYang Wang 	const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs;
2677ff607e2SYang Wang 
2687ff607e2SYang Wang 	if (mca_funcs && mca_funcs->mca_set_debug_mode)
2697ff607e2SYang Wang 		return mca_funcs->mca_set_debug_mode(adev, enable);
2707ff607e2SYang Wang 
2717ff607e2SYang Wang 	return -EOPNOTSUPP;
2727ff607e2SYang Wang }
2737ff607e2SYang Wang 
amdgpu_mca_smu_mca_bank_dump(struct amdgpu_device * adev,int idx,struct mca_bank_entry * entry,struct ras_query_context * qctx)2749dc57c2aSYang Wang static void amdgpu_mca_smu_mca_bank_dump(struct amdgpu_device *adev, int idx, struct mca_bank_entry *entry,
2759dc57c2aSYang Wang 					 struct ras_query_context *qctx)
27607c1db70SYang Wang {
27775ac6a25SYang Wang 	u64 event_id = qctx ? qctx->evid.event_id : RAS_EVENT_INVALID_ID;
2789dc57c2aSYang Wang 
2799dc57c2aSYang Wang 	RAS_EVENT_LOG(adev, event_id, HW_ERR "Accelerator Check Architecture events logged\n");
2809dc57c2aSYang Wang 	RAS_EVENT_LOG(adev, event_id, HW_ERR "aca entry[%02d].STATUS=0x%016llx\n",
281d406aec8SHawking Zhang 		      idx, entry->regs[MCA_REG_IDX_STATUS]);
2829dc57c2aSYang Wang 	RAS_EVENT_LOG(adev, event_id, HW_ERR "aca entry[%02d].ADDR=0x%016llx\n",
283d406aec8SHawking Zhang 		      idx, entry->regs[MCA_REG_IDX_ADDR]);
2849dc57c2aSYang Wang 	RAS_EVENT_LOG(adev, event_id, HW_ERR "aca entry[%02d].MISC0=0x%016llx\n",
285d406aec8SHawking Zhang 		      idx, entry->regs[MCA_REG_IDX_MISC0]);
2869dc57c2aSYang Wang 	RAS_EVENT_LOG(adev, event_id, HW_ERR "aca entry[%02d].IPID=0x%016llx\n",
287d406aec8SHawking Zhang 		      idx, entry->regs[MCA_REG_IDX_IPID]);
2889dc57c2aSYang Wang 	RAS_EVENT_LOG(adev, event_id, HW_ERR "aca entry[%02d].SYND=0x%016llx\n",
289d406aec8SHawking Zhang 		      idx, entry->regs[MCA_REG_IDX_SYND]);
29007c1db70SYang Wang }
29107c1db70SYang Wang 
amdgpu_mca_smu_get_valid_mca_count(struct amdgpu_device * adev,enum amdgpu_mca_error_type type,uint32_t * count)2927e0357beSYang Wang static int amdgpu_mca_smu_get_valid_mca_count(struct amdgpu_device *adev, enum amdgpu_mca_error_type type, uint32_t *count)
2937e0357beSYang Wang {
2947e0357beSYang Wang 	const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs;
2957e0357beSYang Wang 
2967e0357beSYang Wang 	if (!count)
2977e0357beSYang Wang 		return -EINVAL;
2987e0357beSYang Wang 
2997e0357beSYang Wang 	if (mca_funcs && mca_funcs->mca_get_valid_mca_count)
3007e0357beSYang Wang 		return mca_funcs->mca_get_valid_mca_count(adev, type, count);
3017e0357beSYang Wang 
3027e0357beSYang Wang 	return -EOPNOTSUPP;
3037e0357beSYang Wang }
3047e0357beSYang Wang 
amdgpu_mca_smu_get_mca_entry(struct amdgpu_device * adev,enum amdgpu_mca_error_type type,int idx,struct mca_bank_entry * entry)3057e0357beSYang Wang static int amdgpu_mca_smu_get_mca_entry(struct amdgpu_device *adev, enum amdgpu_mca_error_type type,
3067e0357beSYang Wang 					int idx, struct mca_bank_entry *entry)
3077e0357beSYang Wang {
3087e0357beSYang Wang 	const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs;
3097e0357beSYang Wang 	int count;
3107e0357beSYang Wang 
3117e0357beSYang Wang 	if (!mca_funcs || !mca_funcs->mca_get_mca_entry)
3127e0357beSYang Wang 		return -EOPNOTSUPP;
3137e0357beSYang Wang 
3147e0357beSYang Wang 	switch (type) {
3157e0357beSYang Wang 	case AMDGPU_MCA_ERROR_TYPE_UE:
3167e0357beSYang Wang 		count = mca_funcs->max_ue_count;
3177e0357beSYang Wang 		break;
3187e0357beSYang Wang 	case AMDGPU_MCA_ERROR_TYPE_CE:
3197e0357beSYang Wang 		count = mca_funcs->max_ce_count;
3207e0357beSYang Wang 		break;
3217e0357beSYang Wang 	default:
3227e0357beSYang Wang 		return -EINVAL;
3237e0357beSYang Wang 	}
3247e0357beSYang Wang 
3257e0357beSYang Wang 	if (idx >= count)
3267e0357beSYang Wang 		return -EINVAL;
3277e0357beSYang Wang 
3287e0357beSYang Wang 	return mca_funcs->mca_get_mca_entry(adev, type, idx, entry);
3297e0357beSYang Wang }
3307e0357beSYang Wang 
amdgpu_mca_bank_should_update(struct amdgpu_device * adev,enum amdgpu_mca_error_type type)3315eccab32SYang Wang static bool amdgpu_mca_bank_should_update(struct amdgpu_device *adev, enum amdgpu_mca_error_type type)
3325eccab32SYang Wang {
3335eccab32SYang Wang 	struct amdgpu_mca *mca = &adev->mca;
3345eccab32SYang Wang 	bool ret = true;
3355eccab32SYang Wang 
3365eccab32SYang Wang 	/*
3375eccab32SYang Wang 	 * Because the UE Valid MCA count will only be cleared after reset,
3385eccab32SYang Wang 	 * in order to avoid repeated counting of the error count,
3395eccab32SYang Wang 	 * the aca bank is only updated once during the gpu recovery stage.
3405eccab32SYang Wang 	 */
3415eccab32SYang Wang 	if (type == AMDGPU_MCA_ERROR_TYPE_UE) {
3425eccab32SYang Wang 		if (amdgpu_ras_intr_triggered())
3435eccab32SYang Wang 			ret = atomic_cmpxchg(&mca->ue_update_flag, 0, 1) == 0;
3445eccab32SYang Wang 		else
3455eccab32SYang Wang 			atomic_set(&mca->ue_update_flag, 0);
3465eccab32SYang Wang 	}
3475eccab32SYang Wang 
3485eccab32SYang Wang 	return ret;
3495eccab32SYang Wang }
3505eccab32SYang Wang 
amdgpu_mca_smu_get_mca_set(struct amdgpu_device * adev,enum amdgpu_mca_error_type type,struct mca_bank_set * mca_set,struct ras_query_context * qctx)3518fb20d95SYang Wang static int amdgpu_mca_smu_get_mca_set(struct amdgpu_device *adev, enum amdgpu_mca_error_type type, struct mca_bank_set *mca_set,
3528fb20d95SYang Wang 				      struct ras_query_context *qctx)
3537e0357beSYang Wang {
3547e0357beSYang Wang 	struct mca_bank_entry entry;
3557e0357beSYang Wang 	uint32_t count = 0, i;
3567e0357beSYang Wang 	int ret;
3577e0357beSYang Wang 
3587e0357beSYang Wang 	if (!mca_set)
3597e0357beSYang Wang 		return -EINVAL;
3607e0357beSYang Wang 
3615eccab32SYang Wang 	if (!amdgpu_mca_bank_should_update(adev, type))
3625eccab32SYang Wang 		return 0;
3635eccab32SYang Wang 
3647e0357beSYang Wang 	ret = amdgpu_mca_smu_get_valid_mca_count(adev, type, &count);
3657e0357beSYang Wang 	if (ret)
3667e0357beSYang Wang 		return ret;
3677e0357beSYang Wang 
3687e0357beSYang Wang 	for (i = 0; i < count; i++) {
3697e0357beSYang Wang 		memset(&entry, 0, sizeof(entry));
3707e0357beSYang Wang 		ret = amdgpu_mca_smu_get_mca_entry(adev, type, i, &entry);
3717e0357beSYang Wang 		if (ret)
3727e0357beSYang Wang 			return ret;
3737e0357beSYang Wang 
3747e0357beSYang Wang 		amdgpu_mca_bank_set_add_entry(mca_set, &entry);
3758fb20d95SYang Wang 
3768fb20d95SYang Wang 		amdgpu_mca_smu_mca_bank_dump(adev, i, &entry, qctx);
3777e0357beSYang Wang 	}
3787e0357beSYang Wang 
3797e0357beSYang Wang 	return 0;
3807e0357beSYang Wang }
3817e0357beSYang Wang 
amdgpu_mca_smu_parse_mca_error_count(struct amdgpu_device * adev,enum amdgpu_ras_block blk,enum amdgpu_mca_error_type type,struct mca_bank_entry * entry,uint32_t * count)3827e0357beSYang Wang static int amdgpu_mca_smu_parse_mca_error_count(struct amdgpu_device *adev, enum amdgpu_ras_block blk,
3837e0357beSYang Wang 						enum amdgpu_mca_error_type type, struct mca_bank_entry *entry, uint32_t *count)
3847e0357beSYang Wang {
3857e0357beSYang Wang 	const struct amdgpu_mca_smu_funcs *mca_funcs = adev->mca.mca_funcs;
3867e0357beSYang Wang 
3877e0357beSYang Wang 	if (!count || !entry)
3887e0357beSYang Wang 		return -EINVAL;
3897e0357beSYang Wang 
3907e0357beSYang Wang 	if (!mca_funcs || !mca_funcs->mca_parse_mca_error_count)
3917e0357beSYang Wang 		return -EOPNOTSUPP;
3927e0357beSYang Wang 
3937e0357beSYang Wang 	return mca_funcs->mca_parse_mca_error_count(adev, blk, type, entry, count);
3947e0357beSYang Wang }
3957e0357beSYang Wang 
amdgpu_mca_dispatch_mca_set(struct amdgpu_device * adev,enum amdgpu_ras_block blk,enum amdgpu_mca_error_type type,struct mca_bank_set * mca_set,struct ras_err_data * err_data)3968fb20d95SYang Wang static int amdgpu_mca_dispatch_mca_set(struct amdgpu_device *adev, enum amdgpu_ras_block blk, enum amdgpu_mca_error_type type,
3978fb20d95SYang Wang 				       struct mca_bank_set *mca_set, struct ras_err_data *err_data)
39807c1db70SYang Wang {
39907c1db70SYang Wang 	struct amdgpu_smuio_mcm_config_info mcm_info;
40076ad30f5SYang Wang 	struct mca_bank_node *node, *tmp;
40107c1db70SYang Wang 	struct mca_bank_entry *entry;
40207c1db70SYang Wang 	uint32_t count;
4038fb20d95SYang Wang 	int ret;
40407c1db70SYang Wang 
4058fb20d95SYang Wang 	if (!mca_set)
4068fb20d95SYang Wang 		return -EINVAL;
40707c1db70SYang Wang 
4088fb20d95SYang Wang 	if (!mca_set->nr_entries)
4098fb20d95SYang Wang 		return 0;
41007c1db70SYang Wang 
41176ad30f5SYang Wang 	list_for_each_entry_safe(node, tmp, &mca_set->list, node) {
41207c1db70SYang Wang 		entry = &node->entry;
41307c1db70SYang Wang 
41407c1db70SYang Wang 		count = 0;
41507c1db70SYang Wang 		ret = amdgpu_mca_smu_parse_mca_error_count(adev, blk, type, entry, &count);
41685a24a3eSYang Wang 		if (ret && ret != -EOPNOTSUPP)
4178fb20d95SYang Wang 			return ret;
41807c1db70SYang Wang 
41907c1db70SYang Wang 		if (!count)
42007c1db70SYang Wang 			continue;
42107c1db70SYang Wang 
4228fb20d95SYang Wang 		memset(&mcm_info, 0, sizeof(mcm_info));
4238fb20d95SYang Wang 
42407c1db70SYang Wang 		mcm_info.socket_id = entry->info.socket_id;
42507c1db70SYang Wang 		mcm_info.die_id = entry->info.aid;
42607c1db70SYang Wang 
4278fb20d95SYang Wang 		if (type == AMDGPU_MCA_ERROR_TYPE_UE) {
4289f91e983SYiPeng Chai 			amdgpu_ras_error_statistic_ue_count(err_data,
429*671af066SYang Wang 							    &mcm_info, (uint64_t)count);
4308fb20d95SYang Wang 		} else {
431afb617f3SYiPeng Chai 			if (amdgpu_mca_is_deferred_error(adev, entry->regs[MCA_REG_IDX_STATUS]))
43246e2231cSCandice Li 				amdgpu_ras_error_statistic_de_count(err_data,
433*671af066SYang Wang 								    &mcm_info, (uint64_t)count);
43407c1db70SYang Wang 			else
4359f91e983SYiPeng Chai 				amdgpu_ras_error_statistic_ce_count(err_data,
436*671af066SYang Wang 								    &mcm_info, (uint64_t)count);
43707c1db70SYang Wang 		}
43876ad30f5SYang Wang 
43976ad30f5SYang Wang 		amdgpu_mca_bank_set_remove_node(mca_set, node);
44046e2231cSCandice Li 	}
44107c1db70SYang Wang 
4428fb20d95SYang Wang 	return 0;
4438fb20d95SYang Wang }
4448fb20d95SYang Wang 
amdgpu_mca_add_mca_set_to_cache(struct amdgpu_device * adev,enum amdgpu_mca_error_type type,struct mca_bank_set * new)44576ad30f5SYang Wang static int amdgpu_mca_add_mca_set_to_cache(struct amdgpu_device *adev, enum amdgpu_mca_error_type type, struct mca_bank_set *new)
44676ad30f5SYang Wang {
44776ad30f5SYang Wang 	struct mca_bank_cache *mca_cache = &adev->mca.mca_caches[type];
44876ad30f5SYang Wang 	int ret;
44976ad30f5SYang Wang 
4508c9ee180SYang Wang 	mutex_lock(&mca_cache->lock);
45176ad30f5SYang Wang 	ret = amdgpu_mca_bank_set_merge(&mca_cache->mca_set, new);
4528c9ee180SYang Wang 	mutex_unlock(&mca_cache->lock);
45376ad30f5SYang Wang 
45476ad30f5SYang Wang 	return ret;
45576ad30f5SYang Wang }
45676ad30f5SYang Wang 
amdgpu_mca_smu_log_ras_error(struct amdgpu_device * adev,enum amdgpu_ras_block blk,enum amdgpu_mca_error_type type,struct ras_err_data * err_data,struct ras_query_context * qctx)4578fb20d95SYang Wang int amdgpu_mca_smu_log_ras_error(struct amdgpu_device *adev, enum amdgpu_ras_block blk, enum amdgpu_mca_error_type type,
4588fb20d95SYang Wang 				 struct ras_err_data *err_data, struct ras_query_context *qctx)
4598fb20d95SYang Wang {
4608fb20d95SYang Wang 	struct mca_bank_set mca_set;
46176ad30f5SYang Wang 	struct mca_bank_cache *mca_cache = &adev->mca.mca_caches[type];
4628fb20d95SYang Wang 	int ret;
4638fb20d95SYang Wang 
4648fb20d95SYang Wang 	amdgpu_mca_bank_set_init(&mca_set);
4658fb20d95SYang Wang 
4668fb20d95SYang Wang 	ret = amdgpu_mca_smu_get_mca_set(adev, type, &mca_set, qctx);
4678fb20d95SYang Wang 	if (ret)
4688fb20d95SYang Wang 		goto out_mca_release;
4698fb20d95SYang Wang 
4708fb20d95SYang Wang 	ret = amdgpu_mca_dispatch_mca_set(adev, blk, type, &mca_set, err_data);
47176ad30f5SYang Wang 	if (ret)
47276ad30f5SYang Wang 		goto out_mca_release;
47376ad30f5SYang Wang 
47476ad30f5SYang Wang 	/* add remain mca bank to mca cache */
47576ad30f5SYang Wang 	if (mca_set.nr_entries) {
47676ad30f5SYang Wang 		ret = amdgpu_mca_add_mca_set_to_cache(adev, type, &mca_set);
47776ad30f5SYang Wang 		if (ret)
47876ad30f5SYang Wang 			goto out_mca_release;
47976ad30f5SYang Wang 	}
48076ad30f5SYang Wang 
48176ad30f5SYang Wang 	/* dispatch mca set again if mca cache has valid data */
4828c9ee180SYang Wang 	mutex_lock(&mca_cache->lock);
48376ad30f5SYang Wang 	if (mca_cache->mca_set.nr_entries)
48476ad30f5SYang Wang 		ret = amdgpu_mca_dispatch_mca_set(adev, blk, type, &mca_cache->mca_set, err_data);
4858c9ee180SYang Wang 	mutex_unlock(&mca_cache->lock);
4868fb20d95SYang Wang 
48707c1db70SYang Wang out_mca_release:
48807c1db70SYang Wang 	amdgpu_mca_bank_set_release(&mca_set);
48907c1db70SYang Wang 
49007c1db70SYang Wang 	return ret;
49107c1db70SYang Wang }
49207c1db70SYang Wang 
4934051844cSYang Wang #if defined(CONFIG_DEBUG_FS)
amdgpu_mca_smu_debug_mode_set(void * data,u64 val)4944051844cSYang Wang static int amdgpu_mca_smu_debug_mode_set(void *data, u64 val)
4954051844cSYang Wang {
4964051844cSYang Wang 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
4974051844cSYang Wang 	int ret;
4984051844cSYang Wang 
499201761b5SLijo Lazar 	ret = amdgpu_ras_set_mca_debug_mode(adev, val ? true : false);
5004051844cSYang Wang 	if (ret)
5014051844cSYang Wang 		return ret;
5024051844cSYang Wang 
5034051844cSYang Wang 	dev_info(adev->dev, "amdgpu set smu mca debug mode %s success\n", val ? "on" : "off");
5044051844cSYang Wang 
5054051844cSYang Wang 	return 0;
5064051844cSYang Wang }
5074051844cSYang Wang 
mca_dump_entry(struct seq_file * m,struct mca_bank_entry * entry)5084051844cSYang Wang static void mca_dump_entry(struct seq_file *m, struct mca_bank_entry *entry)
5094051844cSYang Wang {
5104051844cSYang Wang 	int i, idx = entry->idx;
5118140b07bSYang Wang 	int reg_idx_array[] = {
5128140b07bSYang Wang 		MCA_REG_IDX_STATUS,
5138140b07bSYang Wang 		MCA_REG_IDX_ADDR,
5148140b07bSYang Wang 		MCA_REG_IDX_MISC0,
5158140b07bSYang Wang 		MCA_REG_IDX_IPID,
5168140b07bSYang Wang 		MCA_REG_IDX_SYND,
5178140b07bSYang Wang 	};
5184051844cSYang Wang 
5194051844cSYang Wang 	seq_printf(m, "mca entry[%d].type: %s\n", idx, entry->type == AMDGPU_MCA_ERROR_TYPE_UE ? "UE" : "CE");
5204051844cSYang Wang 	seq_printf(m, "mca entry[%d].ip: %d\n", idx, entry->ip);
5214051844cSYang Wang 	seq_printf(m, "mca entry[%d].info: socketid:%d aid:%d hwid:0x%03x mcatype:0x%04x\n",
5224051844cSYang Wang 		   idx, entry->info.socket_id, entry->info.aid, entry->info.hwid, entry->info.mcatype);
5234051844cSYang Wang 
5248140b07bSYang Wang 	for (i = 0; i < ARRAY_SIZE(reg_idx_array); i++)
5258140b07bSYang Wang 		seq_printf(m, "mca entry[%d].regs[%d]: 0x%016llx\n", idx, reg_idx_array[i], entry->regs[reg_idx_array[i]]);
5264051844cSYang Wang }
5274051844cSYang Wang 
mca_dump_show(struct seq_file * m,enum amdgpu_mca_error_type type)5284051844cSYang Wang static int mca_dump_show(struct seq_file *m, enum amdgpu_mca_error_type type)
5294051844cSYang Wang {
5304051844cSYang Wang 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
5318fb20d95SYang Wang 	struct mca_bank_node *node;
5328fb20d95SYang Wang 	struct mca_bank_set mca_set;
5338fb20d95SYang Wang 	struct ras_query_context qctx;
5348fb20d95SYang Wang 	int ret;
5354051844cSYang Wang 
5368fb20d95SYang Wang 	amdgpu_mca_bank_set_init(&mca_set);
5378fb20d95SYang Wang 
53875ac6a25SYang Wang 	qctx.evid.event_id = RAS_EVENT_INVALID_ID;
5398fb20d95SYang Wang 	ret = amdgpu_mca_smu_get_mca_set(adev, type, &mca_set, &qctx);
5404051844cSYang Wang 	if (ret)
5418fb20d95SYang Wang 		goto err_free_mca_set;
5424051844cSYang Wang 
5434051844cSYang Wang 	seq_printf(m, "amdgpu smu %s valid mca count: %d\n",
5448fb20d95SYang Wang 		   type == AMDGPU_MCA_ERROR_TYPE_UE ? "UE" : "CE", mca_set.nr_entries);
5454051844cSYang Wang 
5468fb20d95SYang Wang 	if (!mca_set.nr_entries)
5478fb20d95SYang Wang 		goto err_free_mca_set;
5484051844cSYang Wang 
5498fb20d95SYang Wang 	list_for_each_entry(node, &mca_set.list, node)
5508fb20d95SYang Wang 		mca_dump_entry(m, &node->entry);
5514051844cSYang Wang 
55276ad30f5SYang Wang 	/* add mca bank to mca bank cache */
55376ad30f5SYang Wang 	ret = amdgpu_mca_add_mca_set_to_cache(adev, type, &mca_set);
55476ad30f5SYang Wang 
5558fb20d95SYang Wang err_free_mca_set:
5568fb20d95SYang Wang 	amdgpu_mca_bank_set_release(&mca_set);
5574051844cSYang Wang 
5584051844cSYang Wang 	return ret;
5594051844cSYang Wang }
5604051844cSYang Wang 
mca_dump_ce_show(struct seq_file * m,void * unused)5614051844cSYang Wang static int mca_dump_ce_show(struct seq_file *m, void *unused)
5624051844cSYang Wang {
5634051844cSYang Wang 	return mca_dump_show(m, AMDGPU_MCA_ERROR_TYPE_CE);
5644051844cSYang Wang }
5654051844cSYang Wang 
mca_dump_ce_open(struct inode * inode,struct file * file)5664051844cSYang Wang static int mca_dump_ce_open(struct inode *inode, struct file *file)
5674051844cSYang Wang {
5684051844cSYang Wang 	return single_open(file, mca_dump_ce_show, inode->i_private);
5694051844cSYang Wang }
5704051844cSYang Wang 
5714051844cSYang Wang static const struct file_operations mca_ce_dump_debug_fops = {
5724051844cSYang Wang 	.owner = THIS_MODULE,
5734051844cSYang Wang 	.open = mca_dump_ce_open,
5744051844cSYang Wang 	.read = seq_read,
5754051844cSYang Wang 	.llseek = seq_lseek,
5764051844cSYang Wang 	.release = single_release,
5774051844cSYang Wang };
5784051844cSYang Wang 
mca_dump_ue_show(struct seq_file * m,void * unused)5794051844cSYang Wang static int mca_dump_ue_show(struct seq_file *m, void *unused)
5804051844cSYang Wang {
5814051844cSYang Wang 	return mca_dump_show(m, AMDGPU_MCA_ERROR_TYPE_UE);
5824051844cSYang Wang }
5834051844cSYang Wang 
mca_dump_ue_open(struct inode * inode,struct file * file)5844051844cSYang Wang static int mca_dump_ue_open(struct inode *inode, struct file *file)
5854051844cSYang Wang {
5864051844cSYang Wang 	return single_open(file, mca_dump_ue_show, inode->i_private);
5874051844cSYang Wang }
5884051844cSYang Wang 
5894051844cSYang Wang static const struct file_operations mca_ue_dump_debug_fops = {
5904051844cSYang Wang 	.owner = THIS_MODULE,
5914051844cSYang Wang 	.open = mca_dump_ue_open,
5924051844cSYang Wang 	.read = seq_read,
5934051844cSYang Wang 	.llseek = seq_lseek,
5944051844cSYang Wang 	.release = single_release,
5954051844cSYang Wang };
5964051844cSYang Wang 
5974051844cSYang Wang DEFINE_DEBUGFS_ATTRIBUTE(mca_debug_mode_fops, NULL, amdgpu_mca_smu_debug_mode_set, "%llu\n");
5984051844cSYang Wang #endif
5994051844cSYang Wang 
amdgpu_mca_smu_debugfs_init(struct amdgpu_device * adev,struct dentry * root)6004051844cSYang Wang void amdgpu_mca_smu_debugfs_init(struct amdgpu_device *adev, struct dentry *root)
6014051844cSYang Wang {
6024051844cSYang Wang #if defined(CONFIG_DEBUG_FS)
6039817f061SYang Wang 	if (!root)
6044051844cSYang Wang 		return;
6054051844cSYang Wang 
6064051844cSYang Wang 	debugfs_create_file("mca_debug_mode", 0200, root, adev, &mca_debug_mode_fops);
6074051844cSYang Wang 	debugfs_create_file("mca_ue_dump", 0400, root, adev, &mca_ue_dump_debug_fops);
6084051844cSYang Wang 	debugfs_create_file("mca_ce_dump", 0400, root, adev, &mca_ce_dump_debug_fops);
6094051844cSYang Wang #endif
6104051844cSYang Wang }
6114051844cSYang Wang 
612