traps.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * U-boot - traps.c Routines related to interrupts and exceptions
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  5. *
  6. * This file is based on
  7. * No original Copyright holder listed,
  8. * Probabily original (C) Roman Zippel (assigned DJD, 1999)
  9. *
  10. * Copyright 2003 Metrowerks - for Blackfin
  11. * Copyright 2000-2001 Lineo, Inc. D. Jeff Dionne <jeff@lineo.ca>
  12. * Copyright 1999-2000 D. Jeff Dionne, <jeff@uclinux.org>
  13. *
  14. * (C) Copyright 2000-2004
  15. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  16. *
  17. * Licensed under the GPL-2 or later.
  18. */
  19. #include <common.h>
  20. #include <linux/types.h>
  21. #include <asm/traps.h>
  22. #include <asm/cplb.h>
  23. #include <asm/io.h>
  24. #include <asm/mach-common/bits/core.h>
  25. #include <asm/mach-common/bits/mpu.h>
  26. #include <asm/mach-common/bits/trace.h>
  27. #include "cpu.h"
  28. #define trace_buffer_save(x) \
  29. do { \
  30. (x) = bfin_read_TBUFCTL(); \
  31. bfin_write_TBUFCTL((x) & ~TBUFEN); \
  32. } while (0)
  33. #define trace_buffer_restore(x) \
  34. bfin_write_TBUFCTL((x))
  35. /* The purpose of this map is to provide a mapping of address<->cplb settings
  36. * rather than an exact map of what is actually addressable on the part. This
  37. * map covers all current Blackfin parts. If you try to access an address that
  38. * is in this map but not actually on the part, you won't get an exception and
  39. * reboot, you'll get an external hardware addressing error and reboot. Since
  40. * only the ends matter (you did something wrong and the board reset), the means
  41. * are largely irrelevant.
  42. */
  43. struct memory_map {
  44. uint32_t start, end;
  45. uint32_t data_flags, inst_flags;
  46. };
  47. const struct memory_map const bfin_memory_map[] = {
  48. { /* external memory */
  49. .start = 0x00000000,
  50. .end = 0x20000000,
  51. .data_flags = SDRAM_DGENERIC,
  52. .inst_flags = SDRAM_IGENERIC,
  53. },
  54. { /* async banks */
  55. .start = 0x20000000,
  56. .end = 0x30000000,
  57. .data_flags = SDRAM_EBIU,
  58. .inst_flags = SDRAM_INON_CHBL,
  59. },
  60. { /* everything on chip */
  61. .start = 0xE0000000,
  62. .end = 0xFFFFFFFF,
  63. .data_flags = L1_DMEMORY,
  64. .inst_flags = L1_IMEMORY,
  65. }
  66. };
  67. void trap_c(struct pt_regs *regs)
  68. {
  69. uint32_t trapnr = (regs->seqstat & EXCAUSE);
  70. bool data = false;
  71. switch (trapnr) {
  72. /* 0x26 - Data CPLB Miss */
  73. case VEC_CPLB_M:
  74. if (ANOMALY_05000261) {
  75. static uint32_t last_cplb_fault_retx;
  76. /*
  77. * Work around an anomaly: if we see a new DCPLB fault,
  78. * return without doing anything. Then,
  79. * if we get the same fault again, handle it.
  80. */
  81. if (last_cplb_fault_retx != regs->retx) {
  82. last_cplb_fault_retx = regs->retx;
  83. return;
  84. }
  85. }
  86. data = true;
  87. /* fall through */
  88. /* 0x27 - Instruction CPLB Miss */
  89. case VEC_CPLB_I_M: {
  90. volatile uint32_t *CPLB_ADDR_BASE, *CPLB_DATA_BASE, *CPLB_ADDR, *CPLB_DATA;
  91. uint32_t new_cplb_addr = 0, new_cplb_data = 0;
  92. static size_t last_evicted;
  93. size_t i;
  94. new_cplb_addr = (data ? bfin_read_DCPLB_FAULT_ADDR() : bfin_read_ICPLB_FAULT_ADDR()) & ~(4 * 1024 * 1024 - 1);
  95. for (i = 0; i < ARRAY_SIZE(bfin_memory_map); ++i) {
  96. /* if the exception is inside this range, lets use it */
  97. if (new_cplb_addr >= bfin_memory_map[i].start &&
  98. new_cplb_addr < bfin_memory_map[i].end)
  99. break;
  100. }
  101. if (i == ARRAY_SIZE(bfin_memory_map)) {
  102. printf("%cCPLB exception outside of memory map at 0x%p\n",
  103. (data ? 'D' : 'I'), new_cplb_addr);
  104. bfin_panic(regs);
  105. } else
  106. debug("CPLB addr %p matches map 0x%p - 0x%p\n", new_cplb_addr, bfin_memory_map[i].start, bfin_memory_map[i].end);
  107. new_cplb_data = (data ? bfin_memory_map[i].data_flags : bfin_memory_map[i].inst_flags);
  108. /* Turn the cache off */
  109. SSYNC();
  110. if (data) {
  111. asm(" .align 8; ");
  112. *pDMEM_CONTROL &= ~ENDCPLB;
  113. } else {
  114. asm(" .align 8; ");
  115. *pIMEM_CONTROL &= ~ENICPLB;
  116. }
  117. SSYNC();
  118. if (data) {
  119. CPLB_ADDR_BASE = (uint32_t *)DCPLB_ADDR0;
  120. CPLB_DATA_BASE = (uint32_t *)DCPLB_DATA0;
  121. } else {
  122. CPLB_ADDR_BASE = (uint32_t *)ICPLB_ADDR0;
  123. CPLB_DATA_BASE = (uint32_t *)ICPLB_DATA0;
  124. }
  125. /* find the next unlocked entry and evict it */
  126. i = last_evicted & 0xF;
  127. debug("last evicted = %i\n", i);
  128. CPLB_DATA = CPLB_DATA_BASE + i;
  129. while (*CPLB_DATA & CPLB_LOCK) {
  130. debug("skipping %i %p - %08X\n", i, CPLB_DATA, *CPLB_DATA);
  131. i = (i + 1) & 0xF; /* wrap around */
  132. CPLB_DATA = CPLB_DATA_BASE + i;
  133. }
  134. CPLB_ADDR = CPLB_ADDR_BASE + i;
  135. debug("evicting entry %i: 0x%p 0x%08X\n", i, *CPLB_ADDR, *CPLB_DATA);
  136. last_evicted = i + 1;
  137. *CPLB_ADDR = new_cplb_addr;
  138. *CPLB_DATA = new_cplb_data;
  139. /* dump current table for debugging purposes */
  140. CPLB_ADDR = CPLB_ADDR_BASE;
  141. CPLB_DATA = CPLB_DATA_BASE;
  142. for (i = 0; i < 16; ++i)
  143. debug("%2i 0x%p 0x%08X\n", i, *CPLB_ADDR++, *CPLB_DATA++);
  144. /* Turn the cache back on */
  145. SSYNC();
  146. if (data) {
  147. asm(" .align 8; ");
  148. *pDMEM_CONTROL |= ENDCPLB;
  149. } else {
  150. asm(" .align 8; ");
  151. *pIMEM_CONTROL |= ENICPLB;
  152. }
  153. SSYNC();
  154. break;
  155. }
  156. default:
  157. /* All traps come here */
  158. bfin_panic(regs);
  159. }
  160. }
  161. #ifdef CONFIG_DEBUG_DUMP
  162. # define ENABLE_DUMP 1
  163. #else
  164. # define ENABLE_DUMP 0
  165. #endif
  166. #ifdef CONFIG_DEBUG_DUMP_SYMS
  167. # define ENABLE_DUMP_SYMS 1
  168. #else
  169. # define ENABLE_DUMP_SYMS 0
  170. #endif
  171. static const char *symbol_lookup(unsigned long addr, unsigned long *caddr)
  172. {
  173. if (!ENABLE_DUMP_SYMS)
  174. return NULL;
  175. extern const char system_map[] __attribute__((__weak__));
  176. const char *sym, *csym;
  177. char *esym;
  178. unsigned long sym_addr;
  179. sym = system_map;
  180. csym = NULL;
  181. *caddr = 0;
  182. while (*sym) {
  183. sym_addr = simple_strtoul(sym, &esym, 16);
  184. sym = esym + 1;
  185. if (sym_addr > addr)
  186. break;
  187. *caddr = sym_addr;
  188. csym = sym;
  189. sym += strlen(sym) + 1;
  190. }
  191. return csym;
  192. }
  193. static void decode_address(char *buf, unsigned long address)
  194. {
  195. unsigned long sym_addr;
  196. const char *sym = symbol_lookup(address, &sym_addr);
  197. if (sym) {
  198. sprintf(buf, "<0x%p> { %s + 0x%x }", address, sym, address - sym_addr);
  199. return;
  200. }
  201. if (!address)
  202. sprintf(buf, "<0x%p> /* Maybe null pointer? */", address);
  203. else if (address >= CONFIG_SYS_MONITOR_BASE &&
  204. address < CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
  205. sprintf(buf, "<0x%p> /* somewhere in u-boot */", address);
  206. else
  207. sprintf(buf, "<0x%p> /* unknown address */", address);
  208. }
  209. static char *strhwerrcause(uint16_t hwerrcause)
  210. {
  211. switch (hwerrcause) {
  212. case 0x02: return "system mmr error";
  213. case 0x03: return "external memory addressing error";
  214. case 0x12: return "performance monitor overflow";
  215. case 0x18: return "raise 5 instruction";
  216. default: return "undef";
  217. }
  218. }
  219. static char *strexcause(uint16_t excause)
  220. {
  221. switch (excause) {
  222. case 0x00 ... 0xf: return "custom exception";
  223. case 0x10: return "single step";
  224. case 0x11: return "trace buffer full";
  225. case 0x21: return "undef inst";
  226. case 0x22: return "illegal inst";
  227. case 0x23: return "dcplb prot violation";
  228. case 0x24: return "misaligned data";
  229. case 0x25: return "unrecoverable event";
  230. case 0x26: return "dcplb miss";
  231. case 0x27: return "multiple dcplb hit";
  232. case 0x28: return "emulation watchpoint";
  233. case 0x2a: return "misaligned inst";
  234. case 0x2b: return "icplb prot violation";
  235. case 0x2c: return "icplb miss";
  236. case 0x2d: return "multiple icplb hit";
  237. case 0x2e: return "illegal use of supervisor resource";
  238. default: return "undef";
  239. }
  240. }
  241. void dump(struct pt_regs *fp)
  242. {
  243. char buf[150];
  244. size_t i;
  245. uint16_t hwerrcause, excause;
  246. if (!ENABLE_DUMP)
  247. return;
  248. /* fp->ipend is garbage, so load it ourself */
  249. fp->ipend = bfin_read_IPEND();
  250. hwerrcause = (fp->seqstat & HWERRCAUSE) >> HWERRCAUSE_P;
  251. excause = (fp->seqstat & EXCAUSE) >> EXCAUSE_P;
  252. printf("SEQUENCER STATUS:\n");
  253. printf(" SEQSTAT: %08lx IPEND: %04lx SYSCFG: %04lx\n",
  254. fp->seqstat, fp->ipend, fp->syscfg);
  255. printf(" HWERRCAUSE: 0x%lx: %s\n", hwerrcause, strhwerrcause(hwerrcause));
  256. printf(" EXCAUSE : 0x%lx: %s\n", excause, strexcause(excause));
  257. for (i = 6; i <= 15; ++i) {
  258. if (fp->ipend & (1 << i)) {
  259. decode_address(buf, bfin_read32(EVT0 + 4*i));
  260. printf(" physical IVG%i asserted : %s\n", i, buf);
  261. }
  262. }
  263. decode_address(buf, fp->rete);
  264. printf(" RETE: %s\n", buf);
  265. decode_address(buf, fp->retn);
  266. printf(" RETN: %s\n", buf);
  267. decode_address(buf, fp->retx);
  268. printf(" RETX: %s\n", buf);
  269. decode_address(buf, fp->rets);
  270. printf(" RETS: %s\n", buf);
  271. /* we lie and store RETI in "pc" */
  272. decode_address(buf, fp->pc);
  273. printf(" RETI: %s\n", buf);
  274. if (fp->seqstat & EXCAUSE) {
  275. decode_address(buf, bfin_read_DCPLB_FAULT_ADDR());
  276. printf("DCPLB_FAULT_ADDR: %s\n", buf);
  277. decode_address(buf, bfin_read_ICPLB_FAULT_ADDR());
  278. printf("ICPLB_FAULT_ADDR: %s\n", buf);
  279. }
  280. printf("\nPROCESSOR STATE:\n");
  281. printf(" R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
  282. fp->r0, fp->r1, fp->r2, fp->r3);
  283. printf(" R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
  284. fp->r4, fp->r5, fp->r6, fp->r7);
  285. printf(" P0 : %08lx P1 : %08lx P2 : %08lx P3 : %08lx\n",
  286. fp->p0, fp->p1, fp->p2, fp->p3);
  287. printf(" P4 : %08lx P5 : %08lx FP : %08lx SP : %08lx\n",
  288. fp->p4, fp->p5, fp->fp, fp);
  289. printf(" LB0: %08lx LT0: %08lx LC0: %08lx\n",
  290. fp->lb0, fp->lt0, fp->lc0);
  291. printf(" LB1: %08lx LT1: %08lx LC1: %08lx\n",
  292. fp->lb1, fp->lt1, fp->lc1);
  293. printf(" B0 : %08lx L0 : %08lx M0 : %08lx I0 : %08lx\n",
  294. fp->b0, fp->l0, fp->m0, fp->i0);
  295. printf(" B1 : %08lx L1 : %08lx M1 : %08lx I1 : %08lx\n",
  296. fp->b1, fp->l1, fp->m1, fp->i1);
  297. printf(" B2 : %08lx L2 : %08lx M2 : %08lx I2 : %08lx\n",
  298. fp->b2, fp->l2, fp->m2, fp->i2);
  299. printf(" B3 : %08lx L3 : %08lx M3 : %08lx I3 : %08lx\n",
  300. fp->b3, fp->l3, fp->m3, fp->i3);
  301. printf("A0.w: %08lx A0.x: %08lx A1.w: %08lx A1.x: %08lx\n",
  302. fp->a0w, fp->a0x, fp->a1w, fp->a1x);
  303. printf("USP : %08lx ASTAT: %08lx\n",
  304. fp->usp, fp->astat);
  305. printf("\n");
  306. }
  307. void dump_bfin_trace_buffer(void)
  308. {
  309. char buf[150];
  310. unsigned long tflags;
  311. size_t i = 0;
  312. if (!ENABLE_DUMP)
  313. return;
  314. trace_buffer_save(tflags);
  315. printf("Hardware Trace:\n");
  316. if (bfin_read_TBUFSTAT() & TBUFCNT) {
  317. for (; bfin_read_TBUFSTAT() & TBUFCNT; i++) {
  318. decode_address(buf, bfin_read_TBUF());
  319. printf("%4i Target : %s\n", i, buf);
  320. decode_address(buf, bfin_read_TBUF());
  321. printf(" Source : %s\n", buf);
  322. }
  323. }
  324. trace_buffer_restore(tflags);
  325. }
  326. void bfin_panic(struct pt_regs *regs)
  327. {
  328. if (ENABLE_DUMP) {
  329. unsigned long tflags;
  330. trace_buffer_save(tflags);
  331. }
  332. puts(
  333. "\n"
  334. "\n"
  335. "\n"
  336. "Ack! Something bad happened to the Blackfin!\n"
  337. "\n"
  338. );
  339. dump(regs);
  340. dump_bfin_trace_buffer();
  341. puts("\n");
  342. bfin_reset_or_hang();
  343. }