ras_common.c 4.55 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved.
3
 * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
8
#include <stdbool.h>
9

10
11
12
13
14
15
16
17
#include <arch_helpers.h>
#include <bl31/ea_handle.h>
#include <bl31/ehf.h>
#include <common/debug.h>
#include <lib/extensions/ras.h>
#include <lib/extensions/ras_arch.h>
#include <plat/common/platform.h>

18
19
20
21
#ifndef PLAT_RAS_PRI
# error Platform must define RAS priority value
#endif

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Function to convert architecturally-defined primary error code SERR,
 * bits[7:0] from ERR<n>STATUS to its corresponding error string.
 */
const char *ras_serr_to_str(unsigned int serr)
{
	const char *str[ERROR_STATUS_NUM_SERR] = {
		"No error",
		"IMPLEMENTATION DEFINED error",
		"Data value from (non-associative) internal memory",
		"IMPLEMENTATION DEFINED pin",
		"Assertion failure",
		"Error detected on internal data path",
		"Data value from associative memory",
		"Address/control value from associative memory",
		"Data value from a TLB",
		"Address/control value from a TLB",
		"Data value from producer",
		"Address/control value from producer",
		"Data value from (non-associative) external memory",
		"Illegal address (software fault)",
		"Illegal access (software fault)",
		"Illegal state (software fault)",
		"Internal data register",
		"Internal control register",
		"Error response from slave",
		"External timeout",
		"Internal timeout",
		"Deferred error from slave not supported at master"
	};

	/*
	 * All other values are reserved. Reserved values might be defined
	 * in a future version of the architecture
	 */
	if (serr >= ERROR_STATUS_NUM_SERR)
		return "unknown SERR";

	return str[serr];
}

63
64
65
66
/* Handler that receives External Aborts on RAS-capable systems */
int ras_ea_handler(unsigned int ea_reason, uint64_t syndrome, void *cookie,
		void *handle, uint64_t flags)
{
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
67
68
	unsigned int i, n_handled = 0;
	int probe_data, ret;
69
70
71
72
73
	struct err_record_info *info;

	const struct err_handler_data err_data = {
		.version = ERR_HANDLER_VERSION,
		.ea_reason = ea_reason,
74
		.interrupt = 0,
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
75
		.syndrome = (uint32_t) syndrome,
76
77
78
79
80
81
82
83
84
85
		.flags = flags,
		.cookie = cookie,
		.handle = handle
	};

	for_each_err_record_info(i, info) {
		assert(info->probe != NULL);
		assert(info->handler != NULL);

		/* Continue probing until the record group signals no error */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
86
		while (true) {
87
88
89
90
91
92
93
94
95
96
97
98
			if (info->probe(info, &probe_data) == 0)
				break;

			/* Handle error */
			ret = info->handler(info, probe_data, &err_data);
			if (ret != 0)
				return ret;

			n_handled++;
		}
	}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
99
	return (n_handled != 0U) ? 1 : 0;
100
}
101
102
103
104
105

#if ENABLE_ASSERTIONS
static void assert_interrupts_sorted(void)
{
	unsigned int i, last;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
106
	struct ras_interrupt *start = ras_interrupt_mappings.intrs;
107

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
108
	if (ras_interrupt_mappings.num_intrs == 0UL)
109
110
111
		return;

	last = start[0].intr_number;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
112
	for (i = 1; i < ras_interrupt_mappings.num_intrs; i++) {
113
114
115
116
117
118
119
120
121
122
123
124
125
		assert(start[i].intr_number > last);
		last = start[i].intr_number;
	}
}
#endif

/*
 * Given an RAS interrupt number, locate the registered handler and call it. If
 * no handler was found for the interrupt number, this function panics.
 */
static int ras_interrupt_handler(uint32_t intr_raw, uint32_t flags,
		void *handle, void *cookie)
{
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
126
	struct ras_interrupt *ras_inrs = ras_interrupt_mappings.intrs;
127
	struct ras_interrupt *selected = NULL;
128
129
	int probe_data = 0;
	int start, end, mid, ret __unused;
130
131
132
133
134
135
136
137
138

	const struct err_handler_data err_data = {
		.version = ERR_HANDLER_VERSION,
		.interrupt = intr_raw,
		.flags = flags,
		.cookie = cookie,
		.handle = handle
	};

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
139
	assert(ras_interrupt_mappings.num_intrs > 0UL);
140
141

	start = 0;
142
	end = (int)ras_interrupt_mappings.num_intrs - 1;
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	while (start <= end) {
		mid = ((end + start) / 2);
		if (intr_raw == ras_inrs[mid].intr_number) {
			selected = &ras_inrs[mid];
			break;
		} else if (intr_raw < ras_inrs[mid].intr_number) {
			/* Move left */
			end = mid - 1;
		} else {
			/* Move right */
			start = mid + 1;
		}
	}

	if (selected == NULL) {
		ERROR("RAS interrupt %u has no handler!\n", intr_raw);
		panic();
	}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
162
	if (selected->err_record->probe != NULL) {
163
164
165
		ret = selected->err_record->probe(selected->err_record, &probe_data);
		assert(ret != 0);
	}
166
167
168

	/* Call error handler for the record group */
	assert(selected->err_record->handler != NULL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
169
	(void) selected->err_record->handler(selected->err_record, probe_data,
170
171
172
173
174
			&err_data);

	return 0;
}

175
void __init ras_init(void)
176
177
178
179
180
181
182
183
184
{
#if ENABLE_ASSERTIONS
	/* Check RAS interrupts are sorted */
	assert_interrupts_sorted();
#endif

	/* Register RAS priority handler */
	ehf_register_priority_handler(PLAT_RAS_PRI, ras_interrupt_handler);
}