blob: 714cc92d03e0f6164959372016729ef3271d582a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rick Chen6eedd922017-12-26 13:55:49 +08002/*
3 * Copyright (c) 2016-17 Microsemi Corporation.
4 * Padmarao Begari, Microsemi Corporation <padmarao.begari@microsemi.com>
5 *
6 * Copyright (C) 2017 Andes Technology Corporation
7 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
Sean Andersone8b46a12019-12-25 00:27:44 -05008 *
9 * Copyright (C) 2019 Sean Anderson <seanga2@gmail.com>
Rick Chen6eedd922017-12-26 13:55:49 +080010 */
11
Kautuk Consulc86cb4a2022-12-07 17:12:35 +053012#include <linux/compat.h>
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000013#include <efi_loader.h>
Simon Glassf11478f2019-12-28 10:45:07 -070014#include <hang.h>
Heinrich Schuchardt8bf50cd2023-10-31 14:55:51 +020015#include <interrupt.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070016#include <irq_func.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Rick Chen6eedd922017-12-26 13:55:49 +080018#include <asm/ptrace.h>
19#include <asm/system.h>
20#include <asm/encoding.h>
Kautuk Consulc86cb4a2022-12-07 17:12:35 +053021#include <semihosting.h>
Rick Chen6eedd922017-12-26 13:55:49 +080022
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000023DECLARE_GLOBAL_DATA_PTR;
24
Heinrich Schuchardt8bf50cd2023-10-31 14:55:51 +020025static struct resume_data *resume;
26
27void set_resume(struct resume_data *data)
28{
29 resume = data;
30}
31
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000032static void show_efi_loaded_images(uintptr_t epc)
33{
34 efi_print_image_infos((void *)epc);
35}
36
Heinrich Schuchardt99e92102024-08-11 13:01:03 +020037static void __maybe_unused show_regs(struct pt_regs *regs)
Sean Andersone8b46a12019-12-25 00:27:44 -050038{
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +010039 printf("\nSP: " REG_FMT " GP: " REG_FMT " TP: " REG_FMT "\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000040 regs->sp, regs->gp, regs->tp);
41 printf("T0: " REG_FMT " T1: " REG_FMT " T2: " REG_FMT "\n",
42 regs->t0, regs->t1, regs->t2);
43 printf("S0: " REG_FMT " S1: " REG_FMT " A0: " REG_FMT "\n",
44 regs->s0, regs->s1, regs->a0);
45 printf("A1: " REG_FMT " A2: " REG_FMT " A3: " REG_FMT "\n",
46 regs->a1, regs->a2, regs->a3);
47 printf("A4: " REG_FMT " A5: " REG_FMT " A6: " REG_FMT "\n",
48 regs->a4, regs->a5, regs->a6);
49 printf("A7: " REG_FMT " S2: " REG_FMT " S3: " REG_FMT "\n",
50 regs->a7, regs->s2, regs->s3);
51 printf("S4: " REG_FMT " S5: " REG_FMT " S6: " REG_FMT "\n",
52 regs->s4, regs->s5, regs->s6);
53 printf("S7: " REG_FMT " S8: " REG_FMT " S9: " REG_FMT "\n",
54 regs->s7, regs->s8, regs->s9);
55 printf("S10: " REG_FMT " S11: " REG_FMT " T3: " REG_FMT "\n",
56 regs->s10, regs->s11, regs->t3);
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +010057 printf("T4: " REG_FMT " T5: " REG_FMT " T6: " REG_FMT "\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000058 regs->t4, regs->t5, regs->t6);
Sean Andersone8b46a12019-12-25 00:27:44 -050059}
60
Heinrich Schuchardtb68fc1d2024-05-14 07:51:42 +020061static void __maybe_unused show_backtrace(struct pt_regs *regs)
Ben Dooks8a813c12023-09-05 13:12:53 +010062{
63 uintptr_t *fp = (uintptr_t *)regs->s0;
64 unsigned count = 0;
65 ulong ra;
66
Heinrich Schuchardtb68fc1d2024-05-14 07:51:42 +020067 printf("\nbacktrace:\n");
Ben Dooks8a813c12023-09-05 13:12:53 +010068
69 /* there are a few entry points where the s0 register is
70 * set to gd, so to avoid changing those, just abort if
71 * the value is the same */
72 while (fp != NULL && fp != (uintptr_t *)gd) {
73 ra = fp[-1];
Heinrich Schuchardtb68fc1d2024-05-14 07:51:42 +020074 printf("%3d: FP: " REG_FMT " RA: " REG_FMT,
Ben Dooks8a813c12023-09-05 13:12:53 +010075 count, (ulong)fp, ra);
76
77 if (gd && gd->flags & GD_FLG_RELOC)
78 printf(" - RA: " REG_FMT " reloc adjusted\n",
79 ra - gd->reloc_off);
80 else
81 printf("\n");
82
83 fp = (uintptr_t *)fp[-2];
84 count++;
85 }
86}
Ben Dooks8a813c12023-09-05 13:12:53 +010087
Heinrich Schuchardt79c68552021-09-04 10:36:49 +020088/**
89 * instr_len() - get instruction length
90 *
91 * @i: low 16 bits of the instruction
92 * Return: number of u16 in instruction
93 */
94static int instr_len(u16 i)
95{
96 if ((i & 0x03) != 0x03)
97 return 1;
98 /* Instructions with more than 32 bits are not yet specified */
99 return 2;
100}
101
102/**
103 * show_code() - display code leading to exception
104 *
105 * @epc: program counter
106 */
107static void show_code(ulong epc)
108{
109 u16 *pos = (u16 *)(epc & ~1UL);
110 int i, len = instr_len(*pos);
111
112 printf("\nCode: ");
113 for (i = -8; i; ++i)
114 printf("%04x ", pos[i]);
115 printf("(");
116 for (i = 0; i < len; ++i)
117 printf("%04x%s", pos[i], i + 1 == len ? ")\n" : " ");
118}
119
Sean Andersone8b46a12019-12-25 00:27:44 -0500120static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs)
Bin Mengbcc6c742018-12-12 06:12:44 -0800121{
122 static const char * const exception_code[] = {
123 "Instruction address misaligned",
124 "Instruction access fault",
125 "Illegal instruction",
126 "Breakpoint",
127 "Load address misaligned",
128 "Load access fault",
129 "Store/AMO address misaligned",
130 "Store/AMO access fault",
131 "Environment call from U-mode",
132 "Environment call from S-mode",
133 "Reserved",
134 "Environment call from M-mode",
135 "Instruction page fault",
136 "Load page fault",
137 "Reserved",
138 "Store/AMO page fault",
139 };
140
Heinrich Schuchardt8bf50cd2023-10-31 14:55:51 +0200141 if (resume) {
142 resume->code = code;
143 longjmp(resume->jump, 1);
144 }
145
Sean Andersone8b46a12019-12-25 00:27:44 -0500146 if (code < ARRAY_SIZE(exception_code))
147 printf("Unhandled exception: %s\n", exception_code[code]);
148 else
149 printf("Unhandled exception code: %ld\n", code);
Bin Mengbcc6c742018-12-12 06:12:44 -0800150
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000151 printf("EPC: " REG_FMT " RA: " REG_FMT " TVAL: " REG_FMT "\n",
152 epc, regs->ra, tval);
Sean Anderson2c4c7d12020-09-21 07:51:40 -0400153 /* Print relocation adjustments, but only if gd is initialized */
154 if (gd && gd->flags & GD_FLG_RELOC)
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +0100155 printf("EPC: " REG_FMT " RA: " REG_FMT " reloc adjusted\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000156 epc - gd->reloc_off, regs->ra - gd->reloc_off);
157
Heinrich Schuchardt99e92102024-08-11 13:01:03 +0200158 if (CONFIG_IS_ENABLED(SHOW_REGS))
159 show_regs(regs);
Heinrich Schuchardtb68fc1d2024-05-14 07:51:42 +0200160 if (CONFIG_IS_ENABLED(FRAMEPOINTER))
161 show_backtrace(regs);
Heinrich Schuchardt79c68552021-09-04 10:36:49 +0200162 show_code(epc);
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000163 show_efi_loaded_images(epc);
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +0100164 panic("\n");
Bin Mengbcc6c742018-12-12 06:12:44 -0800165}
Rick Chen6eedd922017-12-26 13:55:49 +0800166
167int interrupt_init(void)
168{
169 return 0;
170}
171
172/*
173 * enable interrupts
174 */
175void enable_interrupts(void)
176{
177}
178
179/*
180 * disable interrupts
181 */
182int disable_interrupts(void)
183{
184 return 0;
185}
186
Sean Andersone8b46a12019-12-25 00:27:44 -0500187ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs)
Rick Chen6eedd922017-12-26 13:55:49 +0800188{
Anup Patel89b39342018-12-03 10:57:40 +0530189 ulong is_irq, irq;
190
Heinrich Schuchardtfbef54d2020-09-26 07:50:36 +0200191 /* An UEFI application may have changed gd. Restore U-Boot's gd. */
192 efi_restore_gd();
193
Kautuk Consulc86cb4a2022-12-07 17:12:35 +0530194 if (cause == CAUSE_BREAKPOINT &&
195 CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)) {
196 ulong pre_addr = epc - 4, post_addr = epc + 4;
197
198 /* Check for prior and post addresses to be in same page. */
199 if ((pre_addr & ~(PAGE_SIZE - 1)) ==
200 (post_addr & ~(PAGE_SIZE - 1))) {
201 u32 pre = *(u32 *)pre_addr;
202 u32 post = *(u32 *)post_addr;
203
204 /* Check for semihosting, i.e.:
205 * slli zero,zero,0x1f
206 * ebreak
207 * srai zero,zero,0x7
208 */
209 if (pre == 0x01f01013 && post == 0x40705013) {
210 disable_semihosting();
211 epc += 4;
212 return epc;
213 }
214 }
215 }
216
Anup Patel89b39342018-12-03 10:57:40 +0530217 is_irq = (cause & MCAUSE_INT);
218 irq = (cause & ~MCAUSE_INT);
Rick Chen6eedd922017-12-26 13:55:49 +0800219
Anup Patel89b39342018-12-03 10:57:40 +0530220 if (is_irq) {
221 switch (irq) {
222 case IRQ_M_EXT:
223 case IRQ_S_EXT:
224 external_interrupt(0); /* handle external interrupt */
225 break;
226 case IRQ_M_TIMER:
227 case IRQ_S_TIMER:
228 timer_interrupt(0); /* handle timer interrupt */
229 break;
230 default:
Sean Andersone8b46a12019-12-25 00:27:44 -0500231 _exit_trap(cause, epc, tval, regs);
Anup Patel89b39342018-12-03 10:57:40 +0530232 break;
233 };
234 } else {
Sean Andersone8b46a12019-12-25 00:27:44 -0500235 _exit_trap(cause, epc, tval, regs);
Anup Patel89b39342018-12-03 10:57:40 +0530236 }
Rick Chen6eedd922017-12-26 13:55:49 +0800237
238 return epc;
239}
240
241/*
242 *Entry Point for PLIC Interrupt Handler
243 */
244__attribute__((weak)) void external_interrupt(struct pt_regs *regs)
245{
246}
247
248__attribute__((weak)) void timer_interrupt(struct pt_regs *regs)
249{
250}