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