Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 2 | /* |
| 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 Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2019 Sean Anderson <seanga2@gmail.com> |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
Kautuk Consul | ae3527f | 2022-12-07 17:12:35 +0530 | [diff] [blame] | 12 | #include <linux/compat.h> |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 13 | #include <efi_loader.h> |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 14 | #include <hang.h> |
Heinrich Schuchardt | 9757cae | 2023-10-31 14:55:51 +0200 | [diff] [blame] | 15 | #include <interrupt.h> |
Simon Glass | c30b7ad | 2019-11-14 12:57:41 -0700 | [diff] [blame] | 16 | #include <irq_func.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 18 | #include <asm/ptrace.h> |
| 19 | #include <asm/system.h> |
| 20 | #include <asm/encoding.h> |
Kautuk Consul | ae3527f | 2022-12-07 17:12:35 +0530 | [diff] [blame] | 21 | #include <semihosting.h> |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 22 | |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Heinrich Schuchardt | 9757cae | 2023-10-31 14:55:51 +0200 | [diff] [blame] | 25 | static struct resume_data *resume; |
| 26 | |
| 27 | void set_resume(struct resume_data *data) |
| 28 | { |
| 29 | resume = data; |
| 30 | } |
| 31 | |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 32 | static void show_efi_loaded_images(uintptr_t epc) |
| 33 | { |
| 34 | efi_print_image_infos((void *)epc); |
| 35 | } |
| 36 | |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 37 | static void show_regs(struct pt_regs *regs) |
| 38 | { |
| 39 | #ifdef CONFIG_SHOW_REGS |
Heinrich Schuchardt | c353f2b | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 40 | printf("\nSP: " REG_FMT " GP: " REG_FMT " TP: " REG_FMT "\n", |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 41 | regs->sp, regs->gp, regs->tp); |
| 42 | printf("T0: " REG_FMT " T1: " REG_FMT " T2: " REG_FMT "\n", |
| 43 | regs->t0, regs->t1, regs->t2); |
| 44 | printf("S0: " REG_FMT " S1: " REG_FMT " A0: " REG_FMT "\n", |
| 45 | regs->s0, regs->s1, regs->a0); |
| 46 | printf("A1: " REG_FMT " A2: " REG_FMT " A3: " REG_FMT "\n", |
| 47 | regs->a1, regs->a2, regs->a3); |
| 48 | printf("A4: " REG_FMT " A5: " REG_FMT " A6: " REG_FMT "\n", |
| 49 | regs->a4, regs->a5, regs->a6); |
| 50 | printf("A7: " REG_FMT " S2: " REG_FMT " S3: " REG_FMT "\n", |
| 51 | regs->a7, regs->s2, regs->s3); |
| 52 | printf("S4: " REG_FMT " S5: " REG_FMT " S6: " REG_FMT "\n", |
| 53 | regs->s4, regs->s5, regs->s6); |
| 54 | printf("S7: " REG_FMT " S8: " REG_FMT " S9: " REG_FMT "\n", |
| 55 | regs->s7, regs->s8, regs->s9); |
| 56 | printf("S10: " REG_FMT " S11: " REG_FMT " T3: " REG_FMT "\n", |
| 57 | regs->s10, regs->s11, regs->t3); |
Heinrich Schuchardt | c353f2b | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 58 | printf("T4: " REG_FMT " T5: " REG_FMT " T6: " REG_FMT "\n", |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 59 | regs->t4, regs->t5, regs->t6); |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 60 | #endif |
| 61 | } |
| 62 | |
Heinrich Schuchardt | f6431e8 | 2021-09-04 10:36:49 +0200 | [diff] [blame] | 63 | /** |
| 64 | * instr_len() - get instruction length |
| 65 | * |
| 66 | * @i: low 16 bits of the instruction |
| 67 | * Return: number of u16 in instruction |
| 68 | */ |
| 69 | static int instr_len(u16 i) |
| 70 | { |
| 71 | if ((i & 0x03) != 0x03) |
| 72 | return 1; |
| 73 | /* Instructions with more than 32 bits are not yet specified */ |
| 74 | return 2; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * show_code() - display code leading to exception |
| 79 | * |
| 80 | * @epc: program counter |
| 81 | */ |
| 82 | static void show_code(ulong epc) |
| 83 | { |
| 84 | u16 *pos = (u16 *)(epc & ~1UL); |
| 85 | int i, len = instr_len(*pos); |
| 86 | |
| 87 | printf("\nCode: "); |
| 88 | for (i = -8; i; ++i) |
| 89 | printf("%04x ", pos[i]); |
| 90 | printf("("); |
| 91 | for (i = 0; i < len; ++i) |
| 92 | printf("%04x%s", pos[i], i + 1 == len ? ")\n" : " "); |
| 93 | } |
| 94 | |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 95 | static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs) |
Bin Meng | 7f5d35a | 2018-12-12 06:12:44 -0800 | [diff] [blame] | 96 | { |
| 97 | static const char * const exception_code[] = { |
| 98 | "Instruction address misaligned", |
| 99 | "Instruction access fault", |
| 100 | "Illegal instruction", |
| 101 | "Breakpoint", |
| 102 | "Load address misaligned", |
| 103 | "Load access fault", |
| 104 | "Store/AMO address misaligned", |
| 105 | "Store/AMO access fault", |
| 106 | "Environment call from U-mode", |
| 107 | "Environment call from S-mode", |
| 108 | "Reserved", |
| 109 | "Environment call from M-mode", |
| 110 | "Instruction page fault", |
| 111 | "Load page fault", |
| 112 | "Reserved", |
| 113 | "Store/AMO page fault", |
| 114 | }; |
| 115 | |
Heinrich Schuchardt | 9757cae | 2023-10-31 14:55:51 +0200 | [diff] [blame] | 116 | if (resume) { |
| 117 | resume->code = code; |
| 118 | longjmp(resume->jump, 1); |
| 119 | } |
| 120 | |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 121 | if (code < ARRAY_SIZE(exception_code)) |
| 122 | printf("Unhandled exception: %s\n", exception_code[code]); |
| 123 | else |
| 124 | printf("Unhandled exception code: %ld\n", code); |
Bin Meng | 7f5d35a | 2018-12-12 06:12:44 -0800 | [diff] [blame] | 125 | |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 126 | printf("EPC: " REG_FMT " RA: " REG_FMT " TVAL: " REG_FMT "\n", |
| 127 | epc, regs->ra, tval); |
Sean Anderson | 8576813 | 2020-09-21 07:51:40 -0400 | [diff] [blame] | 128 | /* Print relocation adjustments, but only if gd is initialized */ |
| 129 | if (gd && gd->flags & GD_FLG_RELOC) |
Heinrich Schuchardt | c353f2b | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 130 | printf("EPC: " REG_FMT " RA: " REG_FMT " reloc adjusted\n", |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 131 | epc - gd->reloc_off, regs->ra - gd->reloc_off); |
| 132 | |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 133 | show_regs(regs); |
Heinrich Schuchardt | f6431e8 | 2021-09-04 10:36:49 +0200 | [diff] [blame] | 134 | show_code(epc); |
Heinrich Schuchardt | 7c6ca03 | 2020-08-01 15:15:39 +0000 | [diff] [blame] | 135 | show_efi_loaded_images(epc); |
Heinrich Schuchardt | c353f2b | 2020-12-02 14:36:26 +0100 | [diff] [blame] | 136 | panic("\n"); |
Bin Meng | 7f5d35a | 2018-12-12 06:12:44 -0800 | [diff] [blame] | 137 | } |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 138 | |
| 139 | int interrupt_init(void) |
| 140 | { |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * enable interrupts |
| 146 | */ |
| 147 | void enable_interrupts(void) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * disable interrupts |
| 153 | */ |
| 154 | int disable_interrupts(void) |
| 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 159 | ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs) |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 160 | { |
Anup Patel | d2db2a8 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 161 | ulong is_irq, irq; |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 162 | |
Heinrich Schuchardt | c48e9f3 | 2020-09-26 07:50:36 +0200 | [diff] [blame] | 163 | /* An UEFI application may have changed gd. Restore U-Boot's gd. */ |
| 164 | efi_restore_gd(); |
| 165 | |
Kautuk Consul | ae3527f | 2022-12-07 17:12:35 +0530 | [diff] [blame] | 166 | if (cause == CAUSE_BREAKPOINT && |
| 167 | CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)) { |
| 168 | ulong pre_addr = epc - 4, post_addr = epc + 4; |
| 169 | |
| 170 | /* Check for prior and post addresses to be in same page. */ |
| 171 | if ((pre_addr & ~(PAGE_SIZE - 1)) == |
| 172 | (post_addr & ~(PAGE_SIZE - 1))) { |
| 173 | u32 pre = *(u32 *)pre_addr; |
| 174 | u32 post = *(u32 *)post_addr; |
| 175 | |
| 176 | /* Check for semihosting, i.e.: |
| 177 | * slli zero,zero,0x1f |
| 178 | * ebreak |
| 179 | * srai zero,zero,0x7 |
| 180 | */ |
| 181 | if (pre == 0x01f01013 && post == 0x40705013) { |
| 182 | disable_semihosting(); |
| 183 | epc += 4; |
| 184 | return epc; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Anup Patel | d2db2a8 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 189 | is_irq = (cause & MCAUSE_INT); |
| 190 | irq = (cause & ~MCAUSE_INT); |
| 191 | |
| 192 | if (is_irq) { |
| 193 | switch (irq) { |
| 194 | case IRQ_M_EXT: |
| 195 | case IRQ_S_EXT: |
| 196 | external_interrupt(0); /* handle external interrupt */ |
| 197 | break; |
| 198 | case IRQ_M_TIMER: |
| 199 | case IRQ_S_TIMER: |
| 200 | timer_interrupt(0); /* handle timer interrupt */ |
| 201 | break; |
| 202 | default: |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 203 | _exit_trap(cause, epc, tval, regs); |
Anup Patel | d2db2a8 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 204 | break; |
| 205 | }; |
| 206 | } else { |
Sean Anderson | fd1f6e9 | 2019-12-25 00:27:44 -0500 | [diff] [blame] | 207 | _exit_trap(cause, epc, tval, regs); |
Anup Patel | d2db2a8 | 2018-12-03 10:57:40 +0530 | [diff] [blame] | 208 | } |
Rick Chen | 8bbb290 | 2017-12-26 13:55:49 +0800 | [diff] [blame] | 209 | |
| 210 | return epc; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | *Entry Point for PLIC Interrupt Handler |
| 215 | */ |
| 216 | __attribute__((weak)) void external_interrupt(struct pt_regs *regs) |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | __attribute__((weak)) void timer_interrupt(struct pt_regs *regs) |
| 221 | { |
| 222 | } |