Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * David Feng <fenghua@phytium.com.cn> |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Sean Anderson | bbe310c | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 8 | #include <asm/esr.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Simon Glass | 25a5818 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 10 | #include <asm/ptrace.h> |
Simon Glass | c30b7ad | 2019-11-14 12:57:41 -0700 | [diff] [blame] | 11 | #include <irq_func.h> |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Sean Anderson | bbe310c | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 14 | #include <semihosting.h> |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 15 | |
Peng Fan | 082693f | 2017-11-28 10:08:08 +0800 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 17 | |
| 18 | int interrupt_init(void) |
| 19 | { |
Ovidiu Panait | 5cf9e3b | 2020-04-20 10:31:44 +0300 | [diff] [blame] | 20 | enable_interrupts(); |
| 21 | |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | void enable_interrupts(void) |
| 26 | { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | int disable_interrupts(void) |
| 31 | { |
| 32 | return 0; |
| 33 | } |
| 34 | |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 35 | static void show_efi_loaded_images(struct pt_regs *regs) |
| 36 | { |
| 37 | efi_print_image_infos((void *)regs->elr); |
| 38 | } |
| 39 | |
Heinrich Schuchardt | 57bbf44 | 2019-09-12 19:09:26 +0200 | [diff] [blame] | 40 | static void dump_instr(struct pt_regs *regs) |
| 41 | { |
| 42 | u32 *addr = (u32 *)(regs->elr & ~3UL); |
| 43 | int i; |
| 44 | |
| 45 | printf("Code: "); |
| 46 | for (i = -4; i < 1; i++) |
| 47 | printf(i == 0 ? "(%08x) " : "%08x ", addr[i]); |
| 48 | printf("\n"); |
| 49 | } |
| 50 | |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 51 | void show_regs(struct pt_regs *regs) |
| 52 | { |
| 53 | int i; |
| 54 | |
Karl Beldan | fa7b8ea | 2018-02-20 23:30:08 +0000 | [diff] [blame] | 55 | if (gd->flags & GD_FLG_RELOC) |
| 56 | printf("elr: %016lx lr : %016lx (reloc)\n", |
| 57 | regs->elr - gd->reloc_off, |
| 58 | regs->regs[30] - gd->reloc_off); |
| 59 | printf("elr: %016lx lr : %016lx\n", regs->elr, regs->regs[30]); |
| 60 | |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 61 | for (i = 0; i < 29; i += 2) |
| 62 | printf("x%-2d: %016lx x%-2d: %016lx\n", |
| 63 | i, regs->regs[i], i+1, regs->regs[i+1]); |
| 64 | printf("\n"); |
Heinrich Schuchardt | 57bbf44 | 2019-09-12 19:09:26 +0200 | [diff] [blame] | 65 | dump_instr(regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
Sean Anderson | bbe310c | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 69 | * Try to "emulate" a semihosting call in the event that we don't have a |
| 70 | * debugger attached. |
| 71 | */ |
| 72 | static bool smh_emulate_trap(struct pt_regs *regs) |
| 73 | { |
| 74 | int size; |
| 75 | |
| 76 | if (ESR_ELx_EC(regs->esr) != ESR_ELx_EC_UNKNOWN) |
| 77 | return false; |
| 78 | |
| 79 | if (regs->spsr & PSR_MODE32_BIT) { |
| 80 | if (regs->spsr & PSR_AA32_T_BIT) { |
| 81 | u16 *insn = (u16 *)ALIGN_DOWN(regs->elr, 2); |
| 82 | |
| 83 | if (*insn != SMH_T32_SVC && *insn != SMH_T32_HLT) |
| 84 | return false; |
| 85 | size = 2; |
| 86 | } else { |
| 87 | u32 *insn = (u32 *)ALIGN_DOWN(regs->elr, 4); |
| 88 | |
| 89 | if (*insn != SMH_A32_SVC && *insn != SMH_A32_HLT) |
| 90 | return false; |
| 91 | size = 4; |
| 92 | } |
| 93 | } else { |
| 94 | u32 *insn = (u32 *)ALIGN_DOWN(regs->elr, 4); |
| 95 | |
| 96 | if (*insn != SMH_A64_HLT) |
| 97 | return false; |
| 98 | size = 4; |
| 99 | } |
| 100 | |
| 101 | /* Avoid future semihosting calls */ |
| 102 | disable_semihosting(); |
| 103 | |
| 104 | /* Just pretend the call failed */ |
| 105 | regs->regs[0] = -1; |
| 106 | regs->elr += size; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /* |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 111 | * do_bad_sync handles the impossible case in the Synchronous Abort vector. |
| 112 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 113 | void do_bad_sync(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 114 | { |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 115 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 116 | printf("Bad mode in \"Synchronous Abort\" handler, esr 0x%08lx\n", |
| 117 | pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 118 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 119 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 120 | panic("Resetting CPU ...\n"); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * do_bad_irq handles the impossible case in the Irq vector. |
| 125 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 126 | void do_bad_irq(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 127 | { |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 128 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 129 | printf("Bad mode in \"Irq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 130 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 131 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 132 | panic("Resetting CPU ...\n"); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * do_bad_fiq handles the impossible case in the Fiq vector. |
| 137 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 138 | void do_bad_fiq(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 139 | { |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 140 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 141 | printf("Bad mode in \"Fiq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 142 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 143 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 144 | panic("Resetting CPU ...\n"); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * do_bad_error handles the impossible case in the Error vector. |
| 149 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 150 | void do_bad_error(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 151 | { |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 152 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 153 | printf("Bad mode in \"Error\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 154 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 155 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 156 | panic("Resetting CPU ...\n"); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * do_sync handles the Synchronous Abort exception. |
| 161 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 162 | void do_sync(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 163 | { |
Sean Anderson | bbe310c | 2022-03-22 16:59:31 -0400 | [diff] [blame] | 164 | if (CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK) && |
| 165 | smh_emulate_trap(pt_regs)) |
| 166 | return; |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 167 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 168 | printf("\"Synchronous Abort\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 169 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 170 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 171 | panic("Resetting CPU ...\n"); |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * do_irq handles the Irq exception. |
| 176 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 177 | void do_irq(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 178 | { |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 179 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 180 | printf("\"Irq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 181 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 182 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 183 | panic("Resetting CPU ...\n"); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * do_fiq handles the Fiq exception. |
| 188 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 189 | void do_fiq(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 190 | { |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 191 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 192 | printf("\"Fiq\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 193 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 194 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 195 | panic("Resetting CPU ...\n"); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * do_error handles the Error exception. |
| 200 | * Errors are more likely to be processor specific, |
| 201 | * it is defined with weak attribute and can be redefined |
| 202 | * in processor specific code. |
| 203 | */ |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 204 | void __weak do_error(struct pt_regs *pt_regs) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 205 | { |
Alexander Graf | 6498291 | 2016-03-04 01:10:06 +0100 | [diff] [blame] | 206 | efi_restore_gd(); |
Sean Anderson | e97ac47 | 2022-03-22 17:17:35 -0400 | [diff] [blame] | 207 | printf("\"Error\" handler, esr 0x%08lx\n", pt_regs->esr); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 208 | show_regs(pt_regs); |
Heinrich Schuchardt | bba8165 | 2019-04-04 22:06:25 +0200 | [diff] [blame] | 209 | show_efi_loaded_images(pt_regs); |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 210 | panic("Resetting CPU ...\n"); |
| 211 | } |