xref: /rk3399_ARM-atf/plat/common/aarch64/plat_common.c (revision 0607fb7f6bd9480eea8e989700b0fd5bc7f79148)
1 /*
2  * Copyright (c) 2014-2026, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <stdint.h>
10 
11 #include <arch_features.h>
12 #include <arch_helpers.h>
13 #include <common/debug.h>
14 #include <drivers/console.h>
15 #include <lib/extensions/ras.h>
16 #include <lib/xlat_tables/xlat_mmu_helpers.h>
17 #include <plat/common/platform.h>
18 
19 /* Pointer and function to register platform function to load alernate images */
20 const struct plat_try_images_ops *plat_try_img_ops;
21 
22 void plat_setup_try_img_ops(const struct plat_try_images_ops *plat_try_ops)
23 {
24 	plat_try_img_ops = plat_try_ops;
25 }
26 
27 /*
28  * The following platform setup functions are weakly defined. They
29  * provide typical implementations that may be re-used by multiple
30  * platforms but may also be overridden by a platform if required.
31  */
32 #pragma weak bl31_plat_runtime_setup
33 
34 #if SDEI_SUPPORT
35 #pragma weak plat_sdei_handle_masked_trigger
36 #pragma weak plat_sdei_validate_entry_point
37 #endif
38 
39 #if FFH_SUPPORT
40 #pragma weak plat_ea_handler = plat_default_ea_handler
41 #endif
42 
43 void bl31_plat_runtime_setup(void)
44 {
45 }
46 
47 #if SDEI_SUPPORT
48 /*
49  * Function that handles spurious SDEI interrupts while events are masked.
50  */
51 void plat_sdei_handle_masked_trigger(uint64_t mpidr, unsigned int intr)
52 {
53 	WARN("Spurious SDEI interrupt %u on masked PE %" PRIx64 "\n", intr, mpidr);
54 }
55 
56 /*
57  * Default Function to validate SDEI entry point, which returns success.
58  * Platforms may override this with their own validation mechanism.
59  */
60 int plat_sdei_validate_entry_point(uintptr_t ep, unsigned int client_mode)
61 {
62 	return 0;
63 }
64 #endif
65 
66 /* AArch64 Exception level (as found in SPSR_EL3.M[3:0]) */
67 const char *get_el_str(unsigned int el)
68 {
69 	const char *mode = NULL;
70 
71 	switch (el) {
72 	case MODE_EL3:
73 		mode = "EL3";
74 		break;
75 	case MODE_EL2:
76 		mode = "EL2";
77 		break;
78 	case MODE_EL1:
79 		mode = "EL1";
80 		break;
81 	case MODE_EL0:
82 		mode = "EL0";
83 		break;
84 	default:
85 		assert(false);
86 		break;
87 	}
88 
89 	return mode;
90 }
91 
92 /* AArc32 Mode (as found in SPSR_EL3.M[3:0]) */
93 const char *get_mode_str(unsigned int spsr_mode)
94 {
95 	const char *mode = NULL;
96 
97 	switch (spsr_mode) {
98 	case MODE32_usr:
99 		mode = "User";
100 		break;
101 	case MODE32_fiq:
102 		mode = "FIQ";
103 		break;
104 	case MODE32_irq:
105 		mode = "IRQ";
106 		break;
107 	case MODE32_svc:
108 		mode = "Supervisor";
109 		break;
110 	case MODE32_mon:
111 		mode = "Monitor";
112 		break;
113 	case MODE32_abt:
114 		mode = "Abort";
115 		break;
116 	case MODE32_hyp:
117 		mode = "Hyp";
118 		break;
119 	case MODE32_und:
120 		mode = "Undefined";
121 		break;
122 	case MODE32_sys:
123 		mode = "System";
124 		break;
125 	default:
126 		assert(false);
127 		break;
128 	}
129 	return mode;
130 }
131 
132 #if FFH_SUPPORT
133 /* Handler for External Aborts from lower EL including RAS errors */
134 void plat_default_ea_handler(unsigned int ea_reason, uint64_t syndrome, void *cookie,
135 		void *handle, uint64_t flags)
136 {
137 	if (is_feat_ras_supported()) {
138 		/* Call RAS EA handler */
139 		int handled = ras_ea_handler(ea_reason, syndrome, cookie, handle, flags);
140 		if (handled != 0)
141 			return;
142 	}
143 	unsigned int level = (unsigned int)GET_EL(read_spsr_el3());
144 
145 	ERROR_NL();
146 	ERROR("Unhandled External Abort received on 0x%lx from %s\n",
147 		read_mpidr_el1(), get_el_str(level));
148 	ERROR("exception reason=%u syndrome=0x%" PRIx64 "\n", ea_reason, syndrome);
149 
150 	panic();
151 }
152 #endif
153