blob: 02dbcfd4238022b80686bba133184ff17925b356 [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>
Simon Glass9b61c7c2019-11-14 12:57:41 -070015#include <irq_func.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Rick Chen6eedd922017-12-26 13:55:49 +080017#include <asm/ptrace.h>
18#include <asm/system.h>
19#include <asm/encoding.h>
Kautuk Consulc86cb4a2022-12-07 17:12:35 +053020#include <semihosting.h>
Rick Chen6eedd922017-12-26 13:55:49 +080021
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000022DECLARE_GLOBAL_DATA_PTR;
23
24static void show_efi_loaded_images(uintptr_t epc)
25{
26 efi_print_image_infos((void *)epc);
27}
28
Sean Andersone8b46a12019-12-25 00:27:44 -050029static void show_regs(struct pt_regs *regs)
30{
31#ifdef CONFIG_SHOW_REGS
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +010032 printf("\nSP: " REG_FMT " GP: " REG_FMT " TP: " REG_FMT "\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000033 regs->sp, regs->gp, regs->tp);
34 printf("T0: " REG_FMT " T1: " REG_FMT " T2: " REG_FMT "\n",
35 regs->t0, regs->t1, regs->t2);
36 printf("S0: " REG_FMT " S1: " REG_FMT " A0: " REG_FMT "\n",
37 regs->s0, regs->s1, regs->a0);
38 printf("A1: " REG_FMT " A2: " REG_FMT " A3: " REG_FMT "\n",
39 regs->a1, regs->a2, regs->a3);
40 printf("A4: " REG_FMT " A5: " REG_FMT " A6: " REG_FMT "\n",
41 regs->a4, regs->a5, regs->a6);
42 printf("A7: " REG_FMT " S2: " REG_FMT " S3: " REG_FMT "\n",
43 regs->a7, regs->s2, regs->s3);
44 printf("S4: " REG_FMT " S5: " REG_FMT " S6: " REG_FMT "\n",
45 regs->s4, regs->s5, regs->s6);
46 printf("S7: " REG_FMT " S8: " REG_FMT " S9: " REG_FMT "\n",
47 regs->s7, regs->s8, regs->s9);
48 printf("S10: " REG_FMT " S11: " REG_FMT " T3: " REG_FMT "\n",
49 regs->s10, regs->s11, regs->t3);
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +010050 printf("T4: " REG_FMT " T5: " REG_FMT " T6: " REG_FMT "\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000051 regs->t4, regs->t5, regs->t6);
Sean Andersone8b46a12019-12-25 00:27:44 -050052#endif
53}
54
Heinrich Schuchardt79c68552021-09-04 10:36:49 +020055/**
56 * instr_len() - get instruction length
57 *
58 * @i: low 16 bits of the instruction
59 * Return: number of u16 in instruction
60 */
61static int instr_len(u16 i)
62{
63 if ((i & 0x03) != 0x03)
64 return 1;
65 /* Instructions with more than 32 bits are not yet specified */
66 return 2;
67}
68
69/**
70 * show_code() - display code leading to exception
71 *
72 * @epc: program counter
73 */
74static void show_code(ulong epc)
75{
76 u16 *pos = (u16 *)(epc & ~1UL);
77 int i, len = instr_len(*pos);
78
79 printf("\nCode: ");
80 for (i = -8; i; ++i)
81 printf("%04x ", pos[i]);
82 printf("(");
83 for (i = 0; i < len; ++i)
84 printf("%04x%s", pos[i], i + 1 == len ? ")\n" : " ");
85}
86
Sean Andersone8b46a12019-12-25 00:27:44 -050087static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs)
Bin Mengbcc6c742018-12-12 06:12:44 -080088{
89 static const char * const exception_code[] = {
90 "Instruction address misaligned",
91 "Instruction access fault",
92 "Illegal instruction",
93 "Breakpoint",
94 "Load address misaligned",
95 "Load access fault",
96 "Store/AMO address misaligned",
97 "Store/AMO access fault",
98 "Environment call from U-mode",
99 "Environment call from S-mode",
100 "Reserved",
101 "Environment call from M-mode",
102 "Instruction page fault",
103 "Load page fault",
104 "Reserved",
105 "Store/AMO page fault",
106 };
107
Sean Andersone8b46a12019-12-25 00:27:44 -0500108 if (code < ARRAY_SIZE(exception_code))
109 printf("Unhandled exception: %s\n", exception_code[code]);
110 else
111 printf("Unhandled exception code: %ld\n", code);
Bin Mengbcc6c742018-12-12 06:12:44 -0800112
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000113 printf("EPC: " REG_FMT " RA: " REG_FMT " TVAL: " REG_FMT "\n",
114 epc, regs->ra, tval);
Sean Anderson2c4c7d12020-09-21 07:51:40 -0400115 /* Print relocation adjustments, but only if gd is initialized */
116 if (gd && gd->flags & GD_FLG_RELOC)
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +0100117 printf("EPC: " REG_FMT " RA: " REG_FMT " reloc adjusted\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000118 epc - gd->reloc_off, regs->ra - gd->reloc_off);
119
Sean Andersone8b46a12019-12-25 00:27:44 -0500120 show_regs(regs);
Heinrich Schuchardt79c68552021-09-04 10:36:49 +0200121 show_code(epc);
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000122 show_efi_loaded_images(epc);
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +0100123 panic("\n");
Bin Mengbcc6c742018-12-12 06:12:44 -0800124}
Rick Chen6eedd922017-12-26 13:55:49 +0800125
126int interrupt_init(void)
127{
128 return 0;
129}
130
131/*
132 * enable interrupts
133 */
134void enable_interrupts(void)
135{
136}
137
138/*
139 * disable interrupts
140 */
141int disable_interrupts(void)
142{
143 return 0;
144}
145
Sean Andersone8b46a12019-12-25 00:27:44 -0500146ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs)
Rick Chen6eedd922017-12-26 13:55:49 +0800147{
Anup Patel89b39342018-12-03 10:57:40 +0530148 ulong is_irq, irq;
149
Heinrich Schuchardtfbef54d2020-09-26 07:50:36 +0200150 /* An UEFI application may have changed gd. Restore U-Boot's gd. */
151 efi_restore_gd();
152
Kautuk Consulc86cb4a2022-12-07 17:12:35 +0530153 if (cause == CAUSE_BREAKPOINT &&
154 CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)) {
155 ulong pre_addr = epc - 4, post_addr = epc + 4;
156
157 /* Check for prior and post addresses to be in same page. */
158 if ((pre_addr & ~(PAGE_SIZE - 1)) ==
159 (post_addr & ~(PAGE_SIZE - 1))) {
160 u32 pre = *(u32 *)pre_addr;
161 u32 post = *(u32 *)post_addr;
162
163 /* Check for semihosting, i.e.:
164 * slli zero,zero,0x1f
165 * ebreak
166 * srai zero,zero,0x7
167 */
168 if (pre == 0x01f01013 && post == 0x40705013) {
169 disable_semihosting();
170 epc += 4;
171 return epc;
172 }
173 }
174 }
175
Anup Patel89b39342018-12-03 10:57:40 +0530176 is_irq = (cause & MCAUSE_INT);
177 irq = (cause & ~MCAUSE_INT);
Rick Chen6eedd922017-12-26 13:55:49 +0800178
Anup Patel89b39342018-12-03 10:57:40 +0530179 if (is_irq) {
180 switch (irq) {
181 case IRQ_M_EXT:
182 case IRQ_S_EXT:
183 external_interrupt(0); /* handle external interrupt */
184 break;
185 case IRQ_M_TIMER:
186 case IRQ_S_TIMER:
187 timer_interrupt(0); /* handle timer interrupt */
188 break;
189 default:
Sean Andersone8b46a12019-12-25 00:27:44 -0500190 _exit_trap(cause, epc, tval, regs);
Anup Patel89b39342018-12-03 10:57:40 +0530191 break;
192 };
193 } else {
Sean Andersone8b46a12019-12-25 00:27:44 -0500194 _exit_trap(cause, epc, tval, regs);
Anup Patel89b39342018-12-03 10:57:40 +0530195 }
Rick Chen6eedd922017-12-26 13:55:49 +0800196
197 return epc;
198}
199
200/*
201 *Entry Point for PLIC Interrupt Handler
202 */
203__attribute__((weak)) void external_interrupt(struct pt_regs *regs)
204{
205}
206
207__attribute__((weak)) void timer_interrupt(struct pt_regs *regs)
208{
209}