blob: ef1056eeb6f0215469000545769d13fa509d3db0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rick Chen8bbb2902017-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 Andersonfd1f6e92019-12-25 00:27:44 -05008 *
9 * Copyright (C) 2019 Sean Anderson <seanga2@gmail.com>
Rick Chen8bbb2902017-12-26 13:55:49 +080010 */
11
Kautuk Consulae3527f2022-12-07 17:12:35 +053012#include <linux/compat.h>
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +000013#include <efi_loader.h>
Simon Glassdb41d652019-12-28 10:45:07 -070014#include <hang.h>
Heinrich Schuchardt9757cae2023-10-31 14:55:51 +020015#include <interrupt.h>
Simon Glassc30b7ad2019-11-14 12:57:41 -070016#include <irq_func.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080018#include <asm/ptrace.h>
19#include <asm/system.h>
20#include <asm/encoding.h>
Kautuk Consulae3527f2022-12-07 17:12:35 +053021#include <semihosting.h>
Rick Chen8bbb2902017-12-26 13:55:49 +080022
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +000023DECLARE_GLOBAL_DATA_PTR;
24
Heinrich Schuchardt9757cae2023-10-31 14:55:51 +020025void set_resume(struct resume_data *data)
26{
Anton Blanchard8e1acda2024-08-08 02:14:17 +000027 gd->arch.resume = data;
Heinrich Schuchardt9757cae2023-10-31 14:55:51 +020028}
29
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +000030static void show_efi_loaded_images(uintptr_t epc)
31{
32 efi_print_image_infos((void *)epc);
33}
34
Heinrich Schuchardt73e73f02024-08-11 13:01:03 +020035static void __maybe_unused show_regs(struct pt_regs *regs)
Sean Andersonfd1f6e92019-12-25 00:27:44 -050036{
Heinrich Schuchardtc353f2b2020-12-02 14:36:26 +010037 printf("\nSP: " REG_FMT " GP: " REG_FMT " TP: " REG_FMT "\n",
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +000038 regs->sp, regs->gp, regs->tp);
39 printf("T0: " REG_FMT " T1: " REG_FMT " T2: " REG_FMT "\n",
40 regs->t0, regs->t1, regs->t2);
41 printf("S0: " REG_FMT " S1: " REG_FMT " A0: " REG_FMT "\n",
42 regs->s0, regs->s1, regs->a0);
43 printf("A1: " REG_FMT " A2: " REG_FMT " A3: " REG_FMT "\n",
44 regs->a1, regs->a2, regs->a3);
45 printf("A4: " REG_FMT " A5: " REG_FMT " A6: " REG_FMT "\n",
46 regs->a4, regs->a5, regs->a6);
47 printf("A7: " REG_FMT " S2: " REG_FMT " S3: " REG_FMT "\n",
48 regs->a7, regs->s2, regs->s3);
49 printf("S4: " REG_FMT " S5: " REG_FMT " S6: " REG_FMT "\n",
50 regs->s4, regs->s5, regs->s6);
51 printf("S7: " REG_FMT " S8: " REG_FMT " S9: " REG_FMT "\n",
52 regs->s7, regs->s8, regs->s9);
53 printf("S10: " REG_FMT " S11: " REG_FMT " T3: " REG_FMT "\n",
54 regs->s10, regs->s11, regs->t3);
Heinrich Schuchardtc353f2b2020-12-02 14:36:26 +010055 printf("T4: " REG_FMT " T5: " REG_FMT " T6: " REG_FMT "\n",
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +000056 regs->t4, regs->t5, regs->t6);
Sean Andersonfd1f6e92019-12-25 00:27:44 -050057}
58
Heinrich Schuchardt409259e2024-05-14 07:51:42 +020059static void __maybe_unused show_backtrace(struct pt_regs *regs)
Ben Dookse4f69492023-09-05 13:12:53 +010060{
61 uintptr_t *fp = (uintptr_t *)regs->s0;
62 unsigned count = 0;
63 ulong ra;
64
Heinrich Schuchardt409259e2024-05-14 07:51:42 +020065 printf("\nbacktrace:\n");
Ben Dookse4f69492023-09-05 13:12:53 +010066
67 /* there are a few entry points where the s0 register is
68 * set to gd, so to avoid changing those, just abort if
69 * the value is the same */
70 while (fp != NULL && fp != (uintptr_t *)gd) {
71 ra = fp[-1];
Heinrich Schuchardt409259e2024-05-14 07:51:42 +020072 printf("%3d: FP: " REG_FMT " RA: " REG_FMT,
Ben Dookse4f69492023-09-05 13:12:53 +010073 count, (ulong)fp, ra);
74
75 if (gd && gd->flags & GD_FLG_RELOC)
76 printf(" - RA: " REG_FMT " reloc adjusted\n",
77 ra - gd->reloc_off);
78 else
79 printf("\n");
80
81 fp = (uintptr_t *)fp[-2];
82 count++;
83 }
84}
Ben Dookse4f69492023-09-05 13:12:53 +010085
Heinrich Schuchardtf6431e82021-09-04 10:36:49 +020086/**
87 * instr_len() - get instruction length
88 *
89 * @i: low 16 bits of the instruction
90 * Return: number of u16 in instruction
91 */
92static int instr_len(u16 i)
93{
94 if ((i & 0x03) != 0x03)
95 return 1;
96 /* Instructions with more than 32 bits are not yet specified */
97 return 2;
98}
99
100/**
101 * show_code() - display code leading to exception
102 *
103 * @epc: program counter
104 */
105static void show_code(ulong epc)
106{
107 u16 *pos = (u16 *)(epc & ~1UL);
108 int i, len = instr_len(*pos);
109
110 printf("\nCode: ");
111 for (i = -8; i; ++i)
112 printf("%04x ", pos[i]);
113 printf("(");
114 for (i = 0; i < len; ++i)
115 printf("%04x%s", pos[i], i + 1 == len ? ")\n" : " ");
116}
117
Sean Andersonfd1f6e92019-12-25 00:27:44 -0500118static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs)
Bin Meng7f5d35a2018-12-12 06:12:44 -0800119{
120 static const char * const exception_code[] = {
121 "Instruction address misaligned",
122 "Instruction access fault",
123 "Illegal instruction",
124 "Breakpoint",
125 "Load address misaligned",
126 "Load access fault",
127 "Store/AMO address misaligned",
128 "Store/AMO access fault",
129 "Environment call from U-mode",
130 "Environment call from S-mode",
131 "Reserved",
132 "Environment call from M-mode",
133 "Instruction page fault",
134 "Load page fault",
135 "Reserved",
136 "Store/AMO page fault",
137 };
138
Anton Blanchard8e1acda2024-08-08 02:14:17 +0000139 if (gd->arch.resume) {
140 gd->arch.resume->code = code;
141 longjmp(gd->arch.resume->jump, 1);
Heinrich Schuchardt9757cae2023-10-31 14:55:51 +0200142 }
143
Sean Andersonfd1f6e92019-12-25 00:27:44 -0500144 if (code < ARRAY_SIZE(exception_code))
145 printf("Unhandled exception: %s\n", exception_code[code]);
146 else
147 printf("Unhandled exception code: %ld\n", code);
Bin Meng7f5d35a2018-12-12 06:12:44 -0800148
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +0000149 printf("EPC: " REG_FMT " RA: " REG_FMT " TVAL: " REG_FMT "\n",
150 epc, regs->ra, tval);
Sean Anderson85768132020-09-21 07:51:40 -0400151 /* Print relocation adjustments, but only if gd is initialized */
152 if (gd && gd->flags & GD_FLG_RELOC)
Heinrich Schuchardtc353f2b2020-12-02 14:36:26 +0100153 printf("EPC: " REG_FMT " RA: " REG_FMT " reloc adjusted\n",
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +0000154 epc - gd->reloc_off, regs->ra - gd->reloc_off);
155
Heinrich Schuchardt73e73f02024-08-11 13:01:03 +0200156 if (CONFIG_IS_ENABLED(SHOW_REGS))
157 show_regs(regs);
Heinrich Schuchardt409259e2024-05-14 07:51:42 +0200158 if (CONFIG_IS_ENABLED(FRAMEPOINTER))
159 show_backtrace(regs);
Heinrich Schuchardtf6431e82021-09-04 10:36:49 +0200160 show_code(epc);
Heinrich Schuchardt7c6ca032020-08-01 15:15:39 +0000161 show_efi_loaded_images(epc);
Heinrich Schuchardtc353f2b2020-12-02 14:36:26 +0100162 panic("\n");
Bin Meng7f5d35a2018-12-12 06:12:44 -0800163}
Rick Chen8bbb2902017-12-26 13:55:49 +0800164
165int interrupt_init(void)
166{
167 return 0;
168}
169
170/*
171 * enable interrupts
172 */
173void enable_interrupts(void)
174{
175}
176
177/*
178 * disable interrupts
179 */
180int disable_interrupts(void)
181{
182 return 0;
183}
184
Sean Andersonfd1f6e92019-12-25 00:27:44 -0500185ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs)
Rick Chen8bbb2902017-12-26 13:55:49 +0800186{
Anup Pateld2db2a82018-12-03 10:57:40 +0530187 ulong is_irq, irq;
Rick Chen8bbb2902017-12-26 13:55:49 +0800188
Heinrich Schuchardtc48e9f32020-09-26 07:50:36 +0200189 /* An UEFI application may have changed gd. Restore U-Boot's gd. */
190 efi_restore_gd();
191
Kautuk Consulae3527f2022-12-07 17:12:35 +0530192 if (cause == CAUSE_BREAKPOINT &&
193 CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)) {
194 ulong pre_addr = epc - 4, post_addr = epc + 4;
195
196 /* Check for prior and post addresses to be in same page. */
197 if ((pre_addr & ~(PAGE_SIZE - 1)) ==
198 (post_addr & ~(PAGE_SIZE - 1))) {
199 u32 pre = *(u32 *)pre_addr;
200 u32 post = *(u32 *)post_addr;
201
202 /* Check for semihosting, i.e.:
203 * slli zero,zero,0x1f
204 * ebreak
205 * srai zero,zero,0x7
206 */
207 if (pre == 0x01f01013 && post == 0x40705013) {
208 disable_semihosting();
209 epc += 4;
210 return epc;
211 }
212 }
213 }
214
Anup Pateld2db2a82018-12-03 10:57:40 +0530215 is_irq = (cause & MCAUSE_INT);
216 irq = (cause & ~MCAUSE_INT);
217
218 if (is_irq) {
219 switch (irq) {
220 case IRQ_M_EXT:
221 case IRQ_S_EXT:
222 external_interrupt(0); /* handle external interrupt */
223 break;
224 case IRQ_M_TIMER:
225 case IRQ_S_TIMER:
226 timer_interrupt(0); /* handle timer interrupt */
227 break;
228 default:
Sean Andersonfd1f6e92019-12-25 00:27:44 -0500229 _exit_trap(cause, epc, tval, regs);
Anup Pateld2db2a82018-12-03 10:57:40 +0530230 break;
231 };
232 } else {
Sean Andersonfd1f6e92019-12-25 00:27:44 -0500233 _exit_trap(cause, epc, tval, regs);
Anup Pateld2db2a82018-12-03 10:57:40 +0530234 }
Rick Chen8bbb2902017-12-26 13:55:49 +0800235
236 return epc;
237}
238
239/*
240 *Entry Point for PLIC Interrupt Handler
241 */
242__attribute__((weak)) void external_interrupt(struct pt_regs *regs)
243{
244}
245
246__attribute__((weak)) void timer_interrupt(struct pt_regs *regs)
247{
248}