xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_aca.c (revision 16280ded45fba1216d1d4c6acfc20c2d5b45ef50)
1 /*
2  * Copyright 2023 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/list.h>
25 #include "amdgpu.h"
26 #include "amdgpu_aca.h"
27 #include "amdgpu_ras.h"
28 
29 #define ACA_BANK_HWID(type, hwid, mcatype) [ACA_HWIP_TYPE_##type] = {hwid, mcatype}
30 
31 typedef int bank_handler_t(struct aca_handle *handle, struct aca_bank *bank, enum aca_smu_type type, void *data);
32 
33 static struct aca_hwip aca_hwid_mcatypes[ACA_HWIP_TYPE_COUNT] = {
34 	ACA_BANK_HWID(SMU,	0x01,	0x01),
35 	ACA_BANK_HWID(PCS_XGMI, 0x50,	0x00),
36 	ACA_BANK_HWID(UMC,	0x96,	0x00),
37 };
38 
39 static void aca_banks_init(struct aca_banks *banks)
40 {
41 	if (!banks)
42 		return;
43 
44 	memset(banks, 0, sizeof(*banks));
45 	INIT_LIST_HEAD(&banks->list);
46 }
47 
48 static int aca_banks_add_bank(struct aca_banks *banks, struct aca_bank *bank)
49 {
50 	struct aca_bank_node *node;
51 
52 	if (!bank)
53 		return -EINVAL;
54 
55 	node = kvzalloc(sizeof(*node), GFP_KERNEL);
56 	if (!node)
57 		return -ENOMEM;
58 
59 	memcpy(&node->bank, bank, sizeof(*bank));
60 
61 	INIT_LIST_HEAD(&node->node);
62 	list_add_tail(&node->node, &banks->list);
63 
64 	banks->nr_banks++;
65 
66 	return 0;
67 }
68 
69 static void aca_banks_release(struct aca_banks *banks)
70 {
71 	struct aca_bank_node *node, *tmp;
72 
73 	if (list_empty(&banks->list))
74 		return;
75 
76 	list_for_each_entry_safe(node, tmp, &banks->list, node) {
77 		list_del(&node->node);
78 		kvfree(node);
79 	}
80 }
81 
82 static int aca_smu_get_valid_aca_count(struct amdgpu_device *adev, enum aca_smu_type type, u32 *count)
83 {
84 	struct amdgpu_aca *aca = &adev->aca;
85 	const struct aca_smu_funcs *smu_funcs = aca->smu_funcs;
86 
87 	if (!count)
88 		return -EINVAL;
89 
90 	if (!smu_funcs || !smu_funcs->get_valid_aca_count)
91 		return -EOPNOTSUPP;
92 
93 	return smu_funcs->get_valid_aca_count(adev, type, count);
94 }
95 
96 static struct aca_regs_dump {
97 	const char *name;
98 	int reg_idx;
99 } aca_regs[] = {
100 	{"CONTROL",		ACA_REG_IDX_CTL},
101 	{"STATUS",		ACA_REG_IDX_STATUS},
102 	{"ADDR",		ACA_REG_IDX_ADDR},
103 	{"MISC",		ACA_REG_IDX_MISC0},
104 	{"CONFIG",		ACA_REG_IDX_CONFIG},
105 	{"IPID",		ACA_REG_IDX_IPID},
106 	{"SYND",		ACA_REG_IDX_SYND},
107 	{"DESTAT",		ACA_REG_IDX_DESTAT},
108 	{"DEADDR",		ACA_REG_IDX_DEADDR},
109 	{"CONTROL_MASK",	ACA_REG_IDX_CTL_MASK},
110 };
111 
112 static void aca_smu_bank_dump(struct amdgpu_device *adev, int idx, int total, struct aca_bank *bank,
113 			      struct ras_query_context *qctx)
114 {
115 	u64 event_id = qctx ? qctx->evid.event_id : RAS_EVENT_INVALID_ID;
116 	int i;
117 
118 	RAS_EVENT_LOG(adev, event_id, HW_ERR "Accelerator Check Architecture events logged\n");
119 	/* plus 1 for output format, e.g: ACA[08/08]: xxxx */
120 	for (i = 0; i < ARRAY_SIZE(aca_regs); i++)
121 		RAS_EVENT_LOG(adev, event_id, HW_ERR "ACA[%02d/%02d].%s=0x%016llx\n",
122 			      idx + 1, total, aca_regs[i].name, bank->regs[aca_regs[i].reg_idx]);
123 }
124 
125 static int aca_smu_get_valid_aca_banks(struct amdgpu_device *adev, enum aca_smu_type type,
126 				       int start, int count,
127 				       struct aca_banks *banks, struct ras_query_context *qctx)
128 {
129 	struct amdgpu_aca *aca = &adev->aca;
130 	const struct aca_smu_funcs *smu_funcs = aca->smu_funcs;
131 	struct aca_bank bank;
132 	int i, max_count, ret;
133 
134 	if (!count)
135 		return 0;
136 
137 	if (!smu_funcs || !smu_funcs->get_valid_aca_bank)
138 		return -EOPNOTSUPP;
139 
140 	switch (type) {
141 	case ACA_SMU_TYPE_UE:
142 		max_count = smu_funcs->max_ue_bank_count;
143 		break;
144 	case ACA_SMU_TYPE_CE:
145 		max_count = smu_funcs->max_ce_bank_count;
146 		break;
147 	default:
148 		return -EINVAL;
149 	}
150 
151 	if (start + count > max_count)
152 		return -EINVAL;
153 
154 	count = min_t(int, count, max_count);
155 	for (i = 0; i < count; i++) {
156 		memset(&bank, 0, sizeof(bank));
157 		ret = smu_funcs->get_valid_aca_bank(adev, type, start + i, &bank);
158 		if (ret)
159 			return ret;
160 
161 		bank.smu_err_type = type;
162 
163 		aca_smu_bank_dump(adev, i, count, &bank, qctx);
164 
165 		ret = aca_banks_add_bank(banks, &bank);
166 		if (ret)
167 			return ret;
168 	}
169 
170 	return 0;
171 }
172 
173 static bool aca_bank_hwip_is_matched(struct aca_bank *bank, enum aca_hwip_type type)
174 {
175 
176 	struct aca_hwip *hwip;
177 	int hwid, mcatype;
178 	u64 ipid;
179 
180 	if (!bank || type == ACA_HWIP_TYPE_UNKNOW)
181 		return false;
182 
183 	hwip = &aca_hwid_mcatypes[type];
184 	if (!hwip->hwid)
185 		return false;
186 
187 	ipid = bank->regs[ACA_REG_IDX_IPID];
188 	hwid = ACA_REG__IPID__HARDWAREID(ipid);
189 	mcatype = ACA_REG__IPID__MCATYPE(ipid);
190 
191 	return hwip->hwid == hwid && hwip->mcatype == mcatype;
192 }
193 
194 static bool aca_bank_is_valid(struct aca_handle *handle, struct aca_bank *bank, enum aca_smu_type type)
195 {
196 	const struct aca_bank_ops *bank_ops = handle->bank_ops;
197 
198 	if (!aca_bank_hwip_is_matched(bank, handle->hwip))
199 		return false;
200 
201 	if (!bank_ops->aca_bank_is_valid)
202 		return true;
203 
204 	return bank_ops->aca_bank_is_valid(handle, bank, type, handle->data);
205 }
206 
207 static struct aca_bank_error *new_bank_error(struct aca_error *aerr, struct aca_bank_info *info)
208 {
209 	struct aca_bank_error *bank_error;
210 
211 	bank_error = kvzalloc(sizeof(*bank_error), GFP_KERNEL);
212 	if (!bank_error)
213 		return NULL;
214 
215 	INIT_LIST_HEAD(&bank_error->node);
216 	memcpy(&bank_error->info, info, sizeof(*info));
217 
218 	mutex_lock(&aerr->lock);
219 	list_add_tail(&bank_error->node, &aerr->list);
220 	mutex_unlock(&aerr->lock);
221 
222 	return bank_error;
223 }
224 
225 static struct aca_bank_error *find_bank_error(struct aca_error *aerr, struct aca_bank_info *info)
226 {
227 	struct aca_bank_error *bank_error = NULL;
228 	struct aca_bank_info *tmp_info;
229 	bool found = false;
230 
231 	mutex_lock(&aerr->lock);
232 	list_for_each_entry(bank_error, &aerr->list, node) {
233 		tmp_info = &bank_error->info;
234 		if (tmp_info->socket_id == info->socket_id &&
235 		    tmp_info->die_id == info->die_id) {
236 			found = true;
237 			goto out_unlock;
238 		}
239 	}
240 
241 out_unlock:
242 	mutex_unlock(&aerr->lock);
243 
244 	return found ? bank_error : NULL;
245 }
246 
247 static void aca_bank_error_remove(struct aca_error *aerr, struct aca_bank_error *bank_error)
248 {
249 	if (!aerr || !bank_error)
250 		return;
251 
252 	list_del(&bank_error->node);
253 	aerr->nr_errors--;
254 
255 	kvfree(bank_error);
256 }
257 
258 static struct aca_bank_error *get_bank_error(struct aca_error *aerr, struct aca_bank_info *info)
259 {
260 	struct aca_bank_error *bank_error;
261 
262 	if (!aerr || !info)
263 		return NULL;
264 
265 	bank_error = find_bank_error(aerr, info);
266 	if (bank_error)
267 		return bank_error;
268 
269 	return new_bank_error(aerr, info);
270 }
271 
272 int aca_error_cache_log_bank_error(struct aca_handle *handle, struct aca_bank_info *info,
273 				   enum aca_error_type type, u64 count)
274 {
275 	struct aca_error_cache *error_cache = &handle->error_cache;
276 	struct aca_bank_error *bank_error;
277 	struct aca_error *aerr;
278 
279 	if (!handle || !info || type >= ACA_ERROR_TYPE_COUNT)
280 		return -EINVAL;
281 
282 	if (!count)
283 		return 0;
284 
285 	aerr = &error_cache->errors[type];
286 	bank_error = get_bank_error(aerr, info);
287 	if (!bank_error)
288 		return -ENOMEM;
289 
290 	bank_error->count += count;
291 
292 	return 0;
293 }
294 
295 static int aca_bank_parser(struct aca_handle *handle, struct aca_bank *bank, enum aca_smu_type type)
296 {
297 	const struct aca_bank_ops *bank_ops = handle->bank_ops;
298 
299 	if (!bank)
300 		return -EINVAL;
301 
302 	if (!bank_ops->aca_bank_parser)
303 		return -EOPNOTSUPP;
304 
305 	return bank_ops->aca_bank_parser(handle, bank, type,
306 					 handle->data);
307 }
308 
309 static int handler_aca_log_bank_error(struct aca_handle *handle, struct aca_bank *bank,
310 				      enum aca_smu_type type, void *data)
311 {
312 	int ret;
313 
314 	ret = aca_bank_parser(handle, bank, type);
315 	if (ret)
316 		return ret;
317 
318 	return 0;
319 }
320 
321 static int aca_dispatch_bank(struct aca_handle_manager *mgr, struct aca_bank *bank,
322 			     enum aca_smu_type type, bank_handler_t handler, void *data)
323 {
324 	struct aca_handle *handle;
325 	int ret;
326 
327 	if (list_empty(&mgr->list))
328 		return 0;
329 
330 	list_for_each_entry(handle, &mgr->list, node) {
331 		if (!aca_bank_is_valid(handle, bank, type))
332 			continue;
333 
334 		ret = handler(handle, bank, type, data);
335 		if (ret)
336 			return ret;
337 	}
338 
339 	return 0;
340 }
341 
342 static int aca_dispatch_banks(struct aca_handle_manager *mgr, struct aca_banks *banks,
343 			      enum aca_smu_type type, bank_handler_t handler, void *data)
344 {
345 	struct aca_bank_node *node;
346 	struct aca_bank *bank;
347 	int ret;
348 
349 	if (!mgr || !banks)
350 		return -EINVAL;
351 
352 	/* pre check to avoid unnecessary operations */
353 	if (list_empty(&mgr->list) || list_empty(&banks->list))
354 		return 0;
355 
356 	list_for_each_entry(node, &banks->list, node) {
357 		bank = &node->bank;
358 
359 		ret = aca_dispatch_bank(mgr, bank, type, handler, data);
360 		if (ret)
361 			return ret;
362 	}
363 
364 	return 0;
365 }
366 
367 static bool aca_bank_should_update(struct amdgpu_device *adev, enum aca_smu_type type)
368 {
369 	struct amdgpu_aca *aca = &adev->aca;
370 	bool ret = true;
371 
372 	/*
373 	 * Because the UE Valid MCA count will only be cleared after reset,
374 	 * in order to avoid repeated counting of the error count,
375 	 * the aca bank is only updated once during the gpu recovery stage.
376 	 */
377 	if (type == ACA_SMU_TYPE_UE) {
378 		if (amdgpu_ras_intr_triggered())
379 			ret = atomic_cmpxchg(&aca->ue_update_flag, 0, 1) == 0;
380 		else
381 			atomic_set(&aca->ue_update_flag, 0);
382 	}
383 
384 	return ret;
385 }
386 
387 static void aca_banks_generate_cper(struct amdgpu_device *adev,
388 				    enum aca_smu_type type,
389 				    struct aca_banks *banks,
390 				    int count)
391 {
392 	struct aca_bank_node *node;
393 	struct aca_bank *bank;
394 
395 	if (!adev->cper.enabled)
396 		return;
397 
398 	if (!banks || !count) {
399 		dev_warn(adev->dev, "fail to generate cper records\n");
400 		return;
401 	}
402 
403 	/* UEs must be encoded into separate CPER entries */
404 	if (type == ACA_SMU_TYPE_UE) {
405 		list_for_each_entry(node, &banks->list, node) {
406 			bank = &node->bank;
407 			if (amdgpu_cper_generate_ue_record(adev, bank))
408 				dev_warn(adev->dev, "fail to generate ue cper records\n");
409 		}
410 	} else {
411 		/*
412 		 * SMU_TYPE_CE banks are combined into 1 CPER entries,
413 		 * they could be CEs or DEs or both
414 		 */
415 		if (amdgpu_cper_generate_ce_records(adev, banks, count))
416 			dev_warn(adev->dev, "fail to generate ce cper records\n");
417 	}
418 }
419 
420 static int aca_banks_update(struct amdgpu_device *adev, enum aca_smu_type type,
421 			    bank_handler_t handler, struct ras_query_context *qctx, void *data)
422 {
423 	struct amdgpu_aca *aca = &adev->aca;
424 	struct aca_banks banks;
425 	u32 count = 0;
426 	int ret;
427 
428 	if (list_empty(&aca->mgr.list))
429 		return 0;
430 
431 	if (!aca_bank_should_update(adev, type))
432 		return 0;
433 
434 	ret = aca_smu_get_valid_aca_count(adev, type, &count);
435 	if (ret)
436 		return ret;
437 
438 	if (!count)
439 		return 0;
440 
441 	aca_banks_init(&banks);
442 
443 	ret = aca_smu_get_valid_aca_banks(adev, type, 0, count, &banks, qctx);
444 	if (ret)
445 		goto err_release_banks;
446 
447 	if (list_empty(&banks.list)) {
448 		ret = 0;
449 		goto err_release_banks;
450 	}
451 
452 	ret = aca_dispatch_banks(&aca->mgr, &banks, type,
453 				 handler, data);
454 	if (ret)
455 		goto err_release_banks;
456 
457 	aca_banks_generate_cper(adev, type, &banks, count);
458 
459 err_release_banks:
460 	aca_banks_release(&banks);
461 
462 	return ret;
463 }
464 
465 static int aca_log_aca_error_data(struct aca_bank_error *bank_error, enum aca_error_type type, struct ras_err_data *err_data)
466 {
467 	struct aca_bank_info *info;
468 	struct amdgpu_smuio_mcm_config_info mcm_info;
469 	u64 count;
470 
471 	if (type >= ACA_ERROR_TYPE_COUNT)
472 		return -EINVAL;
473 
474 	count = bank_error->count;
475 	if (!count)
476 		return 0;
477 
478 	info = &bank_error->info;
479 	mcm_info.die_id = info->die_id;
480 	mcm_info.socket_id = info->socket_id;
481 
482 	switch (type) {
483 	case ACA_ERROR_TYPE_UE:
484 		amdgpu_ras_error_statistic_ue_count(err_data, &mcm_info, count);
485 		break;
486 	case ACA_ERROR_TYPE_CE:
487 		amdgpu_ras_error_statistic_ce_count(err_data, &mcm_info, count);
488 		break;
489 	case ACA_ERROR_TYPE_DEFERRED:
490 		amdgpu_ras_error_statistic_de_count(err_data, &mcm_info, count);
491 		break;
492 	default:
493 		break;
494 	}
495 
496 	return 0;
497 }
498 
499 static int aca_log_aca_error(struct aca_handle *handle, enum aca_error_type type, struct ras_err_data *err_data)
500 {
501 	struct aca_error_cache *error_cache = &handle->error_cache;
502 	struct aca_error *aerr = &error_cache->errors[type];
503 	struct aca_bank_error *bank_error, *tmp;
504 
505 	mutex_lock(&aerr->lock);
506 
507 	if (list_empty(&aerr->list))
508 		goto out_unlock;
509 
510 	list_for_each_entry_safe(bank_error, tmp, &aerr->list, node) {
511 		aca_log_aca_error_data(bank_error, type, err_data);
512 		aca_bank_error_remove(aerr, bank_error);
513 	}
514 
515 out_unlock:
516 	mutex_unlock(&aerr->lock);
517 
518 	return 0;
519 }
520 
521 static int __aca_get_error_data(struct amdgpu_device *adev, struct aca_handle *handle, enum aca_error_type type,
522 				struct ras_err_data *err_data, struct ras_query_context *qctx)
523 {
524 	enum aca_smu_type smu_type;
525 	int ret;
526 
527 	switch (type) {
528 	case ACA_ERROR_TYPE_UE:
529 		smu_type = ACA_SMU_TYPE_UE;
530 		break;
531 	case ACA_ERROR_TYPE_CE:
532 	case ACA_ERROR_TYPE_DEFERRED:
533 		smu_type = ACA_SMU_TYPE_CE;
534 		break;
535 	default:
536 		return -EINVAL;
537 	}
538 
539 	/* update aca bank to aca source error_cache first */
540 	ret = aca_banks_update(adev, smu_type, handler_aca_log_bank_error, qctx, NULL);
541 	if (ret)
542 		return ret;
543 
544 	return aca_log_aca_error(handle, type, err_data);
545 }
546 
547 static bool aca_handle_is_valid(struct aca_handle *handle)
548 {
549 	if (!handle->mask || !list_empty(&handle->node))
550 		return false;
551 
552 	return true;
553 }
554 
555 int amdgpu_aca_get_error_data(struct amdgpu_device *adev, struct aca_handle *handle,
556 			      enum aca_error_type type, struct ras_err_data *err_data,
557 			      struct ras_query_context *qctx)
558 {
559 	if (!handle || !err_data)
560 		return -EINVAL;
561 
562 	if (aca_handle_is_valid(handle))
563 		return -EOPNOTSUPP;
564 
565 	if ((type < 0) || (!(BIT(type) & handle->mask)))
566 		return  0;
567 
568 	return __aca_get_error_data(adev, handle, type, err_data, qctx);
569 }
570 
571 static void aca_error_init(struct aca_error *aerr, enum aca_error_type type)
572 {
573 	mutex_init(&aerr->lock);
574 	INIT_LIST_HEAD(&aerr->list);
575 	aerr->type = type;
576 	aerr->nr_errors = 0;
577 }
578 
579 static void aca_init_error_cache(struct aca_handle *handle)
580 {
581 	struct aca_error_cache *error_cache = &handle->error_cache;
582 	int type;
583 
584 	for (type = ACA_ERROR_TYPE_UE; type < ACA_ERROR_TYPE_COUNT; type++)
585 		aca_error_init(&error_cache->errors[type], type);
586 }
587 
588 static void aca_error_fini(struct aca_error *aerr)
589 {
590 	struct aca_bank_error *bank_error, *tmp;
591 
592 	mutex_lock(&aerr->lock);
593 	if (list_empty(&aerr->list))
594 		goto out_unlock;
595 
596 	list_for_each_entry_safe(bank_error, tmp, &aerr->list, node)
597 		aca_bank_error_remove(aerr, bank_error);
598 
599 out_unlock:
600 	mutex_destroy(&aerr->lock);
601 }
602 
603 static void aca_fini_error_cache(struct aca_handle *handle)
604 {
605 	struct aca_error_cache *error_cache = &handle->error_cache;
606 	int type;
607 
608 	for (type = ACA_ERROR_TYPE_UE; type < ACA_ERROR_TYPE_COUNT; type++)
609 		aca_error_fini(&error_cache->errors[type]);
610 }
611 
612 static int add_aca_handle(struct amdgpu_device *adev, struct aca_handle_manager *mgr, struct aca_handle *handle,
613 			  const char *name, const struct aca_info *ras_info, void *data)
614 {
615 	memset(handle, 0, sizeof(*handle));
616 
617 	handle->adev = adev;
618 	handle->mgr = mgr;
619 	handle->name = name;
620 	handle->hwip = ras_info->hwip;
621 	handle->mask = ras_info->mask;
622 	handle->bank_ops = ras_info->bank_ops;
623 	handle->data = data;
624 	aca_init_error_cache(handle);
625 
626 	INIT_LIST_HEAD(&handle->node);
627 	list_add_tail(&handle->node, &mgr->list);
628 	mgr->nr_handles++;
629 
630 	return 0;
631 }
632 
633 static ssize_t aca_sysfs_read(struct device *dev,
634 			      struct device_attribute *attr, char *buf)
635 {
636 	struct aca_handle *handle = container_of(attr, struct aca_handle, aca_attr);
637 
638 	/* NOTE: the aca cache will be auto cleared once read,
639 	 * So the driver should unify the query entry point, forward request to ras query interface directly */
640 	return amdgpu_ras_aca_sysfs_read(dev, attr, handle, buf, handle->data);
641 }
642 
643 static int add_aca_sysfs(struct amdgpu_device *adev, struct aca_handle *handle)
644 {
645 	struct device_attribute *aca_attr = &handle->aca_attr;
646 
647 	snprintf(handle->attr_name, sizeof(handle->attr_name) - 1, "aca_%s", handle->name);
648 	aca_attr->show = aca_sysfs_read;
649 	aca_attr->attr.name = handle->attr_name;
650 	aca_attr->attr.mode = S_IRUGO;
651 	sysfs_attr_init(&aca_attr->attr);
652 
653 	return sysfs_add_file_to_group(&adev->dev->kobj,
654 				       &aca_attr->attr,
655 				       "ras");
656 }
657 
658 int amdgpu_aca_add_handle(struct amdgpu_device *adev, struct aca_handle *handle,
659 			  const char *name, const struct aca_info *ras_info, void *data)
660 {
661 	struct amdgpu_aca *aca = &adev->aca;
662 	int ret;
663 
664 	if (!amdgpu_aca_is_enabled(adev))
665 		return 0;
666 
667 	ret = add_aca_handle(adev, &aca->mgr, handle, name, ras_info, data);
668 	if (ret)
669 		return ret;
670 
671 	return add_aca_sysfs(adev, handle);
672 }
673 
674 static void remove_aca_handle(struct aca_handle *handle)
675 {
676 	struct aca_handle_manager *mgr = handle->mgr;
677 
678 	aca_fini_error_cache(handle);
679 	list_del(&handle->node);
680 	mgr->nr_handles--;
681 }
682 
683 static void remove_aca_sysfs(struct aca_handle *handle)
684 {
685 	struct amdgpu_device *adev = handle->adev;
686 	struct device_attribute *aca_attr = &handle->aca_attr;
687 
688 	if (adev->dev->kobj.sd)
689 		sysfs_remove_file_from_group(&adev->dev->kobj,
690 					     &aca_attr->attr,
691 					     "ras");
692 }
693 
694 void amdgpu_aca_remove_handle(struct aca_handle *handle)
695 {
696 	if (!handle || list_empty(&handle->node))
697 		return;
698 
699 	remove_aca_sysfs(handle);
700 	remove_aca_handle(handle);
701 }
702 
703 static int aca_manager_init(struct aca_handle_manager *mgr)
704 {
705 	INIT_LIST_HEAD(&mgr->list);
706 	mgr->nr_handles = 0;
707 
708 	return 0;
709 }
710 
711 static void aca_manager_fini(struct aca_handle_manager *mgr)
712 {
713 	struct aca_handle *handle, *tmp;
714 
715 	if (list_empty(&mgr->list))
716 		return;
717 
718 	list_for_each_entry_safe(handle, tmp, &mgr->list, node)
719 		amdgpu_aca_remove_handle(handle);
720 }
721 
722 bool amdgpu_aca_is_enabled(struct amdgpu_device *adev)
723 {
724 	return (adev->aca.is_enabled ||
725 		adev->debug_enable_ras_aca);
726 }
727 
728 int amdgpu_aca_init(struct amdgpu_device *adev)
729 {
730 	struct amdgpu_aca *aca = &adev->aca;
731 	int ret;
732 
733 	atomic_set(&aca->ue_update_flag, 0);
734 
735 	ret = aca_manager_init(&aca->mgr);
736 	if (ret)
737 		return ret;
738 
739 	return 0;
740 }
741 
742 void amdgpu_aca_fini(struct amdgpu_device *adev)
743 {
744 	struct amdgpu_aca *aca = &adev->aca;
745 
746 	aca_manager_fini(&aca->mgr);
747 
748 	atomic_set(&aca->ue_update_flag, 0);
749 }
750 
751 int amdgpu_aca_reset(struct amdgpu_device *adev)
752 {
753 	struct amdgpu_aca *aca = &adev->aca;
754 
755 	atomic_set(&aca->ue_update_flag, 0);
756 
757 	return 0;
758 }
759 
760 void amdgpu_aca_set_smu_funcs(struct amdgpu_device *adev, const struct aca_smu_funcs *smu_funcs)
761 {
762 	struct amdgpu_aca *aca = &adev->aca;
763 
764 	WARN_ON(aca->smu_funcs);
765 	aca->smu_funcs = smu_funcs;
766 }
767 
768 int aca_bank_info_decode(struct aca_bank *bank, struct aca_bank_info *info)
769 {
770 	u64 ipid;
771 	u32 instidhi, instidlo;
772 
773 	if (!bank || !info)
774 		return -EINVAL;
775 
776 	ipid = bank->regs[ACA_REG_IDX_IPID];
777 	info->hwid = ACA_REG__IPID__HARDWAREID(ipid);
778 	info->mcatype = ACA_REG__IPID__MCATYPE(ipid);
779 	/*
780 	 * Unfied DieID Format: SAASS. A:AID, S:Socket.
781 	 * Unfied DieID[4:4] = InstanceId[0:0]
782 	 * Unfied DieID[0:3] = InstanceIdHi[0:3]
783 	 */
784 	instidhi = ACA_REG__IPID__INSTANCEIDHI(ipid);
785 	instidlo = ACA_REG__IPID__INSTANCEIDLO(ipid);
786 	info->die_id = ((instidhi >> 2) & 0x03);
787 	info->socket_id = ((instidlo & 0x1) << 2) | (instidhi & 0x03);
788 
789 	return 0;
790 }
791 
792 static int aca_bank_get_error_code(struct amdgpu_device *adev, struct aca_bank *bank)
793 {
794 	struct amdgpu_aca *aca = &adev->aca;
795 	const struct aca_smu_funcs *smu_funcs = aca->smu_funcs;
796 
797 	if (!smu_funcs || !smu_funcs->parse_error_code)
798 		return -EOPNOTSUPP;
799 
800 	return smu_funcs->parse_error_code(adev, bank);
801 }
802 
803 int aca_bank_check_error_codes(struct amdgpu_device *adev, struct aca_bank *bank, int *err_codes, int size)
804 {
805 	int i, error_code;
806 
807 	if (!bank || !err_codes)
808 		return -EINVAL;
809 
810 	error_code = aca_bank_get_error_code(adev, bank);
811 	if (error_code < 0)
812 		return error_code;
813 
814 	for (i = 0; i < size; i++) {
815 		if (err_codes[i] == error_code)
816 			return 0;
817 	}
818 
819 	return -EINVAL;
820 }
821 
822 int amdgpu_aca_smu_set_debug_mode(struct amdgpu_device *adev, bool en)
823 {
824 	struct amdgpu_aca *aca = &adev->aca;
825 	const struct aca_smu_funcs *smu_funcs = aca->smu_funcs;
826 
827 	if (!smu_funcs || !smu_funcs->set_debug_mode)
828 		return -EOPNOTSUPP;
829 
830 	return smu_funcs->set_debug_mode(adev, en);
831 }
832 
833 #if defined(CONFIG_DEBUG_FS)
834 static int amdgpu_aca_smu_debug_mode_set(void *data, u64 val)
835 {
836 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
837 	int ret;
838 
839 	ret = amdgpu_ras_set_aca_debug_mode(adev, val ? true : false);
840 	if (ret)
841 		return ret;
842 
843 	dev_info(adev->dev, "amdgpu set smu aca debug mode %s success\n", val ? "on" : "off");
844 
845 	return 0;
846 }
847 
848 static void aca_dump_entry(struct seq_file *m, struct aca_bank *bank, enum aca_smu_type type, int idx)
849 {
850 	struct aca_bank_info info;
851 	int i, ret;
852 
853 	ret = aca_bank_info_decode(bank, &info);
854 	if (ret)
855 		return;
856 
857 	seq_printf(m, "aca entry[%d].type: %s\n", idx, type ==  ACA_SMU_TYPE_UE ? "UE" : "CE");
858 	seq_printf(m, "aca entry[%d].info: socketid:%d aid:%d hwid:0x%03x mcatype:0x%04x\n",
859 		   idx, info.socket_id, info.die_id, info.hwid, info.mcatype);
860 
861 	for (i = 0; i < ARRAY_SIZE(aca_regs); i++)
862 		seq_printf(m, "aca entry[%d].regs[%d]: 0x%016llx\n", idx, aca_regs[i].reg_idx, bank->regs[aca_regs[i].reg_idx]);
863 }
864 
865 struct aca_dump_context {
866 	struct seq_file *m;
867 	int idx;
868 };
869 
870 static int handler_aca_bank_dump(struct aca_handle *handle, struct aca_bank *bank,
871 				 enum aca_smu_type type, void *data)
872 {
873 	struct aca_dump_context *ctx = (struct aca_dump_context *)data;
874 
875 	aca_dump_entry(ctx->m, bank, type, ctx->idx++);
876 
877 	return handler_aca_log_bank_error(handle, bank, type, NULL);
878 }
879 
880 static int aca_dump_show(struct seq_file *m, enum aca_smu_type type)
881 {
882 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
883 	struct aca_dump_context context = {
884 		.m = m,
885 		.idx = 0,
886 	};
887 
888 	return aca_banks_update(adev, type, handler_aca_bank_dump, NULL, (void *)&context);
889 }
890 
891 static int aca_dump_ce_show(struct seq_file *m, void *unused)
892 {
893 	return aca_dump_show(m, ACA_SMU_TYPE_CE);
894 }
895 
896 static int aca_dump_ce_open(struct inode *inode, struct file *file)
897 {
898 	return single_open(file, aca_dump_ce_show, inode->i_private);
899 }
900 
901 static const struct file_operations aca_ce_dump_debug_fops = {
902 	.owner = THIS_MODULE,
903 	.open = aca_dump_ce_open,
904 	.read = seq_read,
905 	.llseek = seq_lseek,
906 	.release = single_release,
907 };
908 
909 static int aca_dump_ue_show(struct seq_file *m, void *unused)
910 {
911 	return aca_dump_show(m, ACA_SMU_TYPE_UE);
912 }
913 
914 static int aca_dump_ue_open(struct inode *inode, struct file *file)
915 {
916 	return single_open(file, aca_dump_ue_show, inode->i_private);
917 }
918 
919 static const struct file_operations aca_ue_dump_debug_fops = {
920 	.owner = THIS_MODULE,
921 	.open = aca_dump_ue_open,
922 	.read = seq_read,
923 	.llseek = seq_lseek,
924 	.release = single_release,
925 };
926 
927 DEFINE_DEBUGFS_ATTRIBUTE(aca_debug_mode_fops, NULL, amdgpu_aca_smu_debug_mode_set, "%llu\n");
928 #endif
929 
930 void amdgpu_aca_smu_debugfs_init(struct amdgpu_device *adev, struct dentry *root)
931 {
932 #if defined(CONFIG_DEBUG_FS)
933 	if (!root)
934 		return;
935 
936 	debugfs_create_file("aca_debug_mode", 0200, root, adev, &aca_debug_mode_fops);
937 	debugfs_create_file("aca_ue_dump", 0400, root, adev, &aca_ue_dump_debug_fops);
938 	debugfs_create_file("aca_ce_dump", 0400, root, adev, &aca_ce_dump_debug_fops);
939 #endif
940 }
941