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