blob: 100be2e9662e480c11e5c68c8d21851feb251edb [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
12#include <common.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>
20
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000021DECLARE_GLOBAL_DATA_PTR;
22
23static void show_efi_loaded_images(uintptr_t epc)
24{
25 efi_print_image_infos((void *)epc);
26}
27
Sean Andersone8b46a12019-12-25 00:27:44 -050028static void show_regs(struct pt_regs *regs)
29{
30#ifdef CONFIG_SHOW_REGS
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +010031 printf("\nSP: " REG_FMT " GP: " REG_FMT " TP: " REG_FMT "\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000032 regs->sp, regs->gp, regs->tp);
33 printf("T0: " REG_FMT " T1: " REG_FMT " T2: " REG_FMT "\n",
34 regs->t0, regs->t1, regs->t2);
35 printf("S0: " REG_FMT " S1: " REG_FMT " A0: " REG_FMT "\n",
36 regs->s0, regs->s1, regs->a0);
37 printf("A1: " REG_FMT " A2: " REG_FMT " A3: " REG_FMT "\n",
38 regs->a1, regs->a2, regs->a3);
39 printf("A4: " REG_FMT " A5: " REG_FMT " A6: " REG_FMT "\n",
40 regs->a4, regs->a5, regs->a6);
41 printf("A7: " REG_FMT " S2: " REG_FMT " S3: " REG_FMT "\n",
42 regs->a7, regs->s2, regs->s3);
43 printf("S4: " REG_FMT " S5: " REG_FMT " S6: " REG_FMT "\n",
44 regs->s4, regs->s5, regs->s6);
45 printf("S7: " REG_FMT " S8: " REG_FMT " S9: " REG_FMT "\n",
46 regs->s7, regs->s8, regs->s9);
47 printf("S10: " REG_FMT " S11: " REG_FMT " T3: " REG_FMT "\n",
48 regs->s10, regs->s11, regs->t3);
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +010049 printf("T4: " REG_FMT " T5: " REG_FMT " T6: " REG_FMT "\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +000050 regs->t4, regs->t5, regs->t6);
Sean Andersone8b46a12019-12-25 00:27:44 -050051#endif
52}
53
Heinrich Schuchardt79c68552021-09-04 10:36:49 +020054/**
55 * instr_len() - get instruction length
56 *
57 * @i: low 16 bits of the instruction
58 * Return: number of u16 in instruction
59 */
60static int instr_len(u16 i)
61{
62 if ((i & 0x03) != 0x03)
63 return 1;
64 /* Instructions with more than 32 bits are not yet specified */
65 return 2;
66}
67
68/**
69 * show_code() - display code leading to exception
70 *
71 * @epc: program counter
72 */
73static void show_code(ulong epc)
74{
75 u16 *pos = (u16 *)(epc & ~1UL);
76 int i, len = instr_len(*pos);
77
78 printf("\nCode: ");
79 for (i = -8; i; ++i)
80 printf("%04x ", pos[i]);
81 printf("(");
82 for (i = 0; i < len; ++i)
83 printf("%04x%s", pos[i], i + 1 == len ? ")\n" : " ");
84}
85
Sean Andersone8b46a12019-12-25 00:27:44 -050086static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs)
Bin Mengbcc6c742018-12-12 06:12:44 -080087{
88 static const char * const exception_code[] = {
89 "Instruction address misaligned",
90 "Instruction access fault",
91 "Illegal instruction",
92 "Breakpoint",
93 "Load address misaligned",
94 "Load access fault",
95 "Store/AMO address misaligned",
96 "Store/AMO access fault",
97 "Environment call from U-mode",
98 "Environment call from S-mode",
99 "Reserved",
100 "Environment call from M-mode",
101 "Instruction page fault",
102 "Load page fault",
103 "Reserved",
104 "Store/AMO page fault",
105 };
106
Sean Andersone8b46a12019-12-25 00:27:44 -0500107 if (code < ARRAY_SIZE(exception_code))
108 printf("Unhandled exception: %s\n", exception_code[code]);
109 else
110 printf("Unhandled exception code: %ld\n", code);
Bin Mengbcc6c742018-12-12 06:12:44 -0800111
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000112 printf("EPC: " REG_FMT " RA: " REG_FMT " TVAL: " REG_FMT "\n",
113 epc, regs->ra, tval);
Sean Anderson2c4c7d12020-09-21 07:51:40 -0400114 /* Print relocation adjustments, but only if gd is initialized */
115 if (gd && gd->flags & GD_FLG_RELOC)
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +0100116 printf("EPC: " REG_FMT " RA: " REG_FMT " reloc adjusted\n",
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000117 epc - gd->reloc_off, regs->ra - gd->reloc_off);
118
Sean Andersone8b46a12019-12-25 00:27:44 -0500119 show_regs(regs);
Heinrich Schuchardt79c68552021-09-04 10:36:49 +0200120 show_code(epc);
Heinrich Schuchardt77efe242020-08-01 15:15:39 +0000121 show_efi_loaded_images(epc);
Heinrich Schuchardtb881ba82020-12-02 14:36:26 +0100122 panic("\n");
Bin Mengbcc6c742018-12-12 06:12:44 -0800123}
Rick Chen6eedd922017-12-26 13:55:49 +0800124
125int interrupt_init(void)
126{
127 return 0;
128}
129
130/*
131 * enable interrupts
132 */
133void enable_interrupts(void)
134{
135}
136
137/*
138 * disable interrupts
139 */
140int disable_interrupts(void)
141{
142 return 0;
143}
144
Sean Andersone8b46a12019-12-25 00:27:44 -0500145ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs)
Rick Chen6eedd922017-12-26 13:55:49 +0800146{
Anup Patel89b39342018-12-03 10:57:40 +0530147 ulong is_irq, irq;
148
Heinrich Schuchardtfbef54d2020-09-26 07:50:36 +0200149 /* An UEFI application may have changed gd. Restore U-Boot's gd. */
150 efi_restore_gd();
151
Anup Patel89b39342018-12-03 10:57:40 +0530152 is_irq = (cause & MCAUSE_INT);
153 irq = (cause & ~MCAUSE_INT);
Rick Chen6eedd922017-12-26 13:55:49 +0800154
Anup Patel89b39342018-12-03 10:57:40 +0530155 if (is_irq) {
156 switch (irq) {
157 case IRQ_M_EXT:
158 case IRQ_S_EXT:
159 external_interrupt(0); /* handle external interrupt */
160 break;
161 case IRQ_M_TIMER:
162 case IRQ_S_TIMER:
163 timer_interrupt(0); /* handle timer interrupt */
164 break;
165 default:
Sean Andersone8b46a12019-12-25 00:27:44 -0500166 _exit_trap(cause, epc, tval, regs);
Anup Patel89b39342018-12-03 10:57:40 +0530167 break;
168 };
169 } else {
Sean Andersone8b46a12019-12-25 00:27:44 -0500170 _exit_trap(cause, epc, tval, regs);
Anup Patel89b39342018-12-03 10:57:40 +0530171 }
Rick Chen6eedd922017-12-26 13:55:49 +0800172
173 return epc;
174}
175
176/*
177 *Entry Point for PLIC Interrupt Handler
178 */
179__attribute__((weak)) void external_interrupt(struct pt_regs *regs)
180{
181}
182
183__attribute__((weak)) void timer_interrupt(struct pt_regs *regs)
184{
185}