start.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * (C) Copyright 2008 - 2013 Tensilica Inc.
  3. * (C) Copyright 2014 - 2016 Cadence Design Systems Inc.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <config.h>
  8. #include <asm/asmmacro.h>
  9. #include <asm/cacheasm.h>
  10. #include <asm/regs.h>
  11. #include <asm/arch/tie.h>
  12. #include <asm-offsets.h>
  13. /*
  14. * Offsets into the the pt_regs struture.
  15. * Make sure these always match with the structure defined in ptrace.h!
  16. */
  17. #define PT_PC 0
  18. #define PT_PS 4
  19. #define PT_DEPC 8
  20. #define PT_EXCCAUSE 12
  21. #define PT_EXCVADDR 16
  22. #define PT_DEBUGCAUSE 20
  23. #define PT_WMASK 24
  24. #define PT_LBEG 28
  25. #define PT_LEND 32
  26. #define PT_LCOUNT 36
  27. #define PT_SAR 40
  28. #define PT_WINDOWBASE 44
  29. #define PT_WINDOWSTART 48
  30. #define PT_SYSCALL 52
  31. #define PT_ICOUNTLEVEL 56
  32. #define PT_RESERVED 60
  33. #define PT_AREG 64
  34. #define PT_SIZE (64 + 64)
  35. /*
  36. * Cache attributes are different for full MMU and region protection.
  37. */
  38. #if XCHAL_HAVE_PTP_MMU
  39. #define CA_WRITEBACK (0x7)
  40. #else
  41. #define CA_WRITEBACK (0x4)
  42. #endif
  43. /*
  44. * Reset vector.
  45. * Only a trampoline to jump to _start
  46. * (Note that we have to mark the section writable as the section contains
  47. * a relocatable literal)
  48. */
  49. .section .ResetVector.text, "awx"
  50. .global _ResetVector
  51. _ResetVector:
  52. j 1f
  53. .align 4
  54. 2: .long _start
  55. 1: l32r a2, 2b
  56. jx a2
  57. /*
  58. * Processor initialization. We still run in rom space.
  59. *
  60. * NOTE: Running in ROM
  61. * For Xtensa, we currently don't allow to run some code from ROM but
  62. * unpack the data immediately to memory. This requires, for example,
  63. * that DDR has been set up before running U-Boot. (See also comments
  64. * inline for ways to change it)
  65. */
  66. .section .reset.text, "ax"
  67. .global _start
  68. .align 4
  69. _start:
  70. /* Keep a0 = 0 for various initializations */
  71. movi a0, 0
  72. /*
  73. * For full MMU cores, put page table at unmapped virtual address.
  74. * This ensures that accesses outside the static maps result
  75. * in miss exceptions rather than random behaviour.
  76. */
  77. #if XCHAL_HAVE_PTP_MMU
  78. wsr a0, PTEVADDR
  79. #endif
  80. /* Disable dbreak debug exceptions */
  81. #if XCHAL_HAVE_DEBUG && XCHAL_NUM_DBREAK > 0
  82. .set _index, 0
  83. .rept XCHAL_NUM_DBREAK
  84. wsr a0, DBREAKC + _index
  85. .set _index, _index + 1
  86. .endr
  87. #endif
  88. /* Reset windowbase and windowstart */
  89. #if XCHAL_HAVE_WINDOWED
  90. movi a3, 1
  91. wsr a3, windowstart
  92. wsr a0, windowbase
  93. rsync
  94. movi a0, 0 /* windowbase might have changed */
  95. #endif
  96. /*
  97. * Vecbase in bitstream may differ from header files
  98. * set or check it.
  99. */
  100. #if XCHAL_HAVE_VECBASE
  101. movi a3, XCHAL_VECBASE_RESET_VADDR /* VECBASE reset value */
  102. wsr a3, VECBASE
  103. #endif
  104. #if XCHAL_HAVE_LOOPS
  105. /* Disable loops */
  106. wsr a0, LCOUNT
  107. #endif
  108. /* Set PS.WOE = 0, PS.EXCM = 0 (for loop), PS.INTLEVEL = EXCM level */
  109. #if XCHAL_HAVE_XEA1
  110. movi a2, 1
  111. #else
  112. movi a2, XCHAL_EXCM_LEVEL
  113. #endif
  114. wsr a2, PS
  115. rsync
  116. /* Unlock and invalidate caches */
  117. ___unlock_dcache_all a2, a3
  118. ___invalidate_dcache_all a2, a3
  119. ___unlock_icache_all a2, a3
  120. ___invalidate_icache_all a2, a3
  121. isync
  122. /* Unpack data sections */
  123. movi a2, __reloc_table_start
  124. movi a3, __reloc_table_end
  125. 1: beq a2, a3, 3f # no more entries?
  126. l32i a4, a2, 0 # start destination (in RAM)
  127. l32i a5, a2, 4 # end destination (in RAM)
  128. l32i a6, a2, 8 # start source (in ROM)
  129. addi a2, a2, 12 # next entry
  130. beq a4, a5, 1b # skip, empty entry
  131. beq a4, a6, 1b # skip, source and destination are the same
  132. /* If there's memory protection option with 512MB TLB regions and
  133. * cache attributes in TLB entries and caching is not inhibited,
  134. * enable data/instruction cache for relocated image.
  135. */
  136. #if XCHAL_HAVE_SPANNING_WAY && \
  137. (!defined(CONFIG_SYS_DCACHE_OFF) || \
  138. !defined(CONFIG_SYS_ICACHE_OFF))
  139. srli a7, a4, 29
  140. slli a7, a7, 29
  141. addi a7, a7, XCHAL_SPANNING_WAY
  142. #ifndef CONFIG_SYS_DCACHE_OFF
  143. rdtlb1 a8, a7
  144. srli a8, a8, 4
  145. slli a8, a8, 4
  146. addi a8, a8, CA_WRITEBACK
  147. wdtlb a8, a7
  148. #endif
  149. #ifndef CONFIG_SYS_ICACHE_OFF
  150. ritlb1 a8, a7
  151. srli a8, a8, 4
  152. slli a8, a8, 4
  153. addi a8, a8, CA_WRITEBACK
  154. witlb a8, a7
  155. #endif
  156. isync
  157. #endif
  158. 2: l32i a7, a6, 0
  159. addi a6, a6, 4
  160. s32i a7, a4, 0
  161. addi a4, a4, 4
  162. bltu a4, a5, 2b
  163. j 1b
  164. 3: /* All code and initalized data segments have been copied */
  165. /* Setup PS, PS.WOE = 1, PS.EXCM = 0, PS.INTLEVEL = EXCM level. */
  166. #if __XTENSA_CALL0_ABI__
  167. movi a2, XCHAL_EXCM_LEVEL
  168. #else
  169. movi a2, (1<<PS_WOE_BIT) | XCHAL_EXCM_LEVEL
  170. #endif
  171. wsr a2, PS
  172. rsync
  173. /* Writeback */
  174. ___flush_dcache_all a2, a3
  175. #ifdef __XTENSA_WINDOWED_ABI__
  176. /*
  177. * In windowed ABI caller and call target need to be within the same
  178. * gigabyte. Put the rest of the code into the text segment and jump
  179. * there.
  180. */
  181. movi a4, .Lboard_init_code
  182. jx a4
  183. .text
  184. .align 4
  185. .Lboard_init_code:
  186. #endif
  187. movi a0, 0
  188. movi sp, (CONFIG_SYS_TEXT_ADDR - 16) & 0xfffffff0
  189. #ifdef CONFIG_DEBUG_UART
  190. movi a4, debug_uart_init
  191. #ifdef __XTENSA_CALL0_ABI__
  192. callx0 a4
  193. #else
  194. callx4 a4
  195. #endif
  196. #endif
  197. movi a4, board_init_f_alloc_reserve
  198. #ifdef __XTENSA_CALL0_ABI__
  199. mov a2, sp
  200. callx0 a4
  201. mov sp, a2
  202. #else
  203. mov a6, sp
  204. callx4 a4
  205. movsp sp, a6
  206. #endif
  207. movi a4, board_init_f_init_reserve
  208. #ifdef __XTENSA_CALL0_ABI__
  209. callx0 a4
  210. #else
  211. callx4 a4
  212. #endif
  213. /*
  214. * Call board initialization routine (never returns).
  215. */
  216. movi a4, board_init_f
  217. #ifdef __XTENSA_CALL0_ABI__
  218. movi a2, 0
  219. callx0 a4
  220. #else
  221. movi a6, 0
  222. callx4 a4
  223. #endif
  224. /* Never Returns */
  225. ill
  226. /*
  227. * void relocate_code (addr_sp, gd, addr_moni)
  228. *
  229. * This "function" does not return, instead it continues in RAM
  230. * after relocating the monitor code.
  231. *
  232. * a2 = addr_sp
  233. * a3 = gd
  234. * a4 = destination address
  235. */
  236. .text
  237. .globl relocate_code
  238. .align 4
  239. relocate_code:
  240. abi_entry
  241. #ifdef __XTENSA_CALL0_ABI__
  242. mov a1, a2
  243. mov a2, a3
  244. mov a3, a4
  245. movi a0, board_init_r
  246. callx0 a0
  247. #else
  248. /* We can't movsp here, because the chain of stack frames may cross
  249. * the now reserved memory. We need to toss all window frames except
  250. * the current, create new pristine stack frame and start from scratch.
  251. */
  252. rsr a0, windowbase
  253. ssl a0
  254. movi a0, 1
  255. sll a0, a0
  256. wsr a0, windowstart
  257. rsync
  258. movi a0, 0
  259. /* Reserve 16-byte save area */
  260. addi sp, a2, -16
  261. mov a6, a3
  262. mov a7, a4
  263. movi a4, board_init_r
  264. callx4 a4
  265. #endif
  266. ill
  267. #if XCHAL_HAVE_EXCEPTIONS
  268. /*
  269. * Exception vectors.
  270. *
  271. * Various notes:
  272. * - We currently don't use the user exception vector (PS.UM is always 0),
  273. * but do define such a vector, just in case. They both jump to the
  274. * same exception handler, though.
  275. * - We currently only save the bare minimum number of registers:
  276. * a0...a15, sar, loop-registers, exception register (epc1, excvaddr,
  277. * exccause, depc)
  278. * - WINDOWSTART is only saved to identify if registers have been spilled
  279. * to the wrong stack (exception stack) while executing the exception
  280. * handler.
  281. */
  282. .section .KernelExceptionVector.text, "ax"
  283. .global _KernelExceptionVector
  284. _KernelExceptionVector:
  285. wsr a2, EXCSAVE1
  286. movi a2, ExceptionHandler
  287. jx a2
  288. .section .UserExceptionVector.text, "ax"
  289. .global _UserExceptionVector
  290. _UserExceptionVector:
  291. wsr a2, EXCSAVE1
  292. movi a2, ExceptionHandler
  293. jx a2
  294. #if !XCHAL_HAVE_XEA1
  295. .section .DoubleExceptionVector.text, "ax"
  296. .global _DoubleExceptionVector
  297. _DoubleExceptionVector:
  298. #ifdef __XTENSA_CALL0_ABI__
  299. wsr a0, EXCSAVE1
  300. movi a0, hang # report and ask user to reset board
  301. callx0 a0
  302. #else
  303. wsr a4, EXCSAVE1
  304. movi a4, hang # report and ask user to reset board
  305. callx4 a4
  306. #endif
  307. #endif
  308. /* Does not return here */
  309. .text
  310. .align 4
  311. ExceptionHandler:
  312. rsr a2, EXCCAUSE # find handler
  313. #if XCHAL_HAVE_WINDOWED
  314. /* Special case for alloca handler */
  315. bnei a2, 5, 1f # jump if not alloca exception
  316. addi a1, a1, -16 - 4 # create a small stack frame
  317. s32i a3, a1, 0 # and save a3 (a2 still in excsave1)
  318. movi a2, fast_alloca_exception
  319. jx a2 # jump to fast_alloca_exception
  320. #endif
  321. /* All other exceptions go here: */
  322. /* Create ptrace stack and save a0...a3 */
  323. 1: addi a2, a1, - PT_SIZE - 16
  324. s32i a0, a2, PT_AREG + 0 * 4
  325. s32i a1, a2, PT_AREG + 1 * 4
  326. s32i a3, a2, PT_AREG + 3 * 4
  327. rsr a3, EXCSAVE1
  328. s32i a3, a2, PT_AREG + 2 * 4
  329. mov a1, a2
  330. /* Save remaining AR registers */
  331. s32i a4, a1, PT_AREG + 4 * 4
  332. s32i a5, a1, PT_AREG + 5 * 4
  333. s32i a6, a1, PT_AREG + 6 * 4
  334. s32i a7, a1, PT_AREG + 7 * 4
  335. s32i a8, a1, PT_AREG + 8 * 4
  336. s32i a9, a1, PT_AREG + 9 * 4
  337. s32i a10, a1, PT_AREG + 10 * 4
  338. s32i a11, a1, PT_AREG + 11 * 4
  339. s32i a12, a1, PT_AREG + 12 * 4
  340. s32i a13, a1, PT_AREG + 13 * 4
  341. s32i a14, a1, PT_AREG + 14 * 4
  342. s32i a15, a1, PT_AREG + 15 * 4
  343. /* Save SRs */
  344. #if XCHAL_HAVE_WINDOWED
  345. rsr a2, WINDOWSTART
  346. s32i a2, a1, PT_WINDOWSTART
  347. #endif
  348. rsr a2, SAR
  349. rsr a3, EPC1
  350. rsr a4, EXCVADDR
  351. s32i a2, a1, PT_SAR
  352. s32i a3, a1, PT_PC
  353. s32i a4, a1, PT_EXCVADDR
  354. #if XCHAL_HAVE_LOOPS
  355. movi a2, 0
  356. rsr a3, LBEG
  357. xsr a2, LCOUNT
  358. s32i a3, a1, PT_LBEG
  359. rsr a3, LEND
  360. s32i a2, a1, PT_LCOUNT
  361. s32i a3, a1, PT_LEND
  362. #endif
  363. /* Set up C environment and call registered handler */
  364. /* Setup stack, PS.WOE = 1, PS.EXCM = 0, PS.INTLEVEL = EXCM level. */
  365. rsr a2, EXCCAUSE
  366. #if XCHAL_HAVE_XEA1
  367. movi a3, (1<<PS_WOE_BIT) | 1
  368. #elif __XTENSA_CALL0_ABI__
  369. movi a3, XCHAL_EXCM_LEVEL
  370. #else
  371. movi a3, (1<<PS_WOE_BIT) | XCHAL_EXCM_LEVEL
  372. #endif
  373. xsr a3, PS
  374. rsync
  375. s32i a2, a1, PT_EXCCAUSE
  376. s32i a3, a1, PT_PS
  377. movi a0, exc_table
  378. addx4 a0, a2, a0
  379. l32i a0, a0, 0
  380. #ifdef __XTENSA_CALL0_ABI__
  381. mov a2, a1 # Provide stack frame as only argument
  382. callx0 a0
  383. l32i a3, a1, PT_PS
  384. #else
  385. mov a6, a1 # Provide stack frame as only argument
  386. callx4 a0
  387. #endif
  388. /* Restore PS and go to exception mode (PS.EXCM=1) */
  389. wsr a3, PS
  390. /* Restore SR registers */
  391. #if XCHAL_HAVE_LOOPS
  392. l32i a2, a1, PT_LBEG
  393. l32i a3, a1, PT_LEND
  394. l32i a4, a1, PT_LCOUNT
  395. wsr a2, LBEG
  396. wsr a3, LEND
  397. wsr a4, LCOUNT
  398. #endif
  399. l32i a2, a1, PT_SAR
  400. l32i a3, a1, PT_PC
  401. wsr a2, SAR
  402. wsr a3, EPC1
  403. #if XCHAL_HAVE_WINDOWED
  404. /* Do we need to simulate a MOVSP? */
  405. l32i a2, a1, PT_WINDOWSTART
  406. addi a3, a2, -1
  407. and a2, a2, a3
  408. beqz a2, 1f # Skip if regs were spilled before exc.
  409. rsr a2, WINDOWSTART
  410. addi a3, a2, -1
  411. and a2, a2, a3
  412. bnez a2, 1f # Skip if registers aren't spilled now
  413. addi a2, a1, -16
  414. l32i a4, a2, 0
  415. l32i a5, a2, 4
  416. s32i a4, a1, PT_SIZE + 0
  417. s32i a5, a1, PT_SIZE + 4
  418. l32i a4, a2, 8
  419. l32i a5, a2, 12
  420. s32i a4, a1, PT_SIZE + 8
  421. s32i a5, a1, PT_SIZE + 12
  422. #endif
  423. /* Restore address register */
  424. 1: l32i a15, a1, PT_AREG + 15 * 4
  425. l32i a14, a1, PT_AREG + 14 * 4
  426. l32i a13, a1, PT_AREG + 13 * 4
  427. l32i a12, a1, PT_AREG + 12 * 4
  428. l32i a11, a1, PT_AREG + 11 * 4
  429. l32i a10, a1, PT_AREG + 10 * 4
  430. l32i a9, a1, PT_AREG + 9 * 4
  431. l32i a8, a1, PT_AREG + 8 * 4
  432. l32i a7, a1, PT_AREG + 7 * 4
  433. l32i a6, a1, PT_AREG + 6 * 4
  434. l32i a5, a1, PT_AREG + 5 * 4
  435. l32i a4, a1, PT_AREG + 4 * 4
  436. l32i a3, a1, PT_AREG + 3 * 4
  437. l32i a2, a1, PT_AREG + 2 * 4
  438. l32i a0, a1, PT_AREG + 0 * 4
  439. l32i a1, a1, PT_AREG + 1 * 4 # Remove ptrace stack frame
  440. rfe
  441. #endif /* XCHAL_HAVE_EXCEPTIONS */
  442. #if XCHAL_HAVE_WINDOWED
  443. /*
  444. * Window overflow and underflow handlers.
  445. * The handlers must be 64 bytes apart, first starting with the underflow
  446. * handlers underflow-4 to underflow-12, then the overflow handlers
  447. * overflow-4 to overflow-12.
  448. *
  449. * Note: We rerun the underflow handlers if we hit an exception, so
  450. * we try to access any page that would cause a page fault early.
  451. */
  452. .section .WindowVectors.text, "ax"
  453. /* 4-Register Window Overflow Vector (Handler) */
  454. .align 64
  455. .global _WindowOverflow4
  456. _WindowOverflow4:
  457. s32e a0, a5, -16
  458. s32e a1, a5, -12
  459. s32e a2, a5, -8
  460. s32e a3, a5, -4
  461. rfwo
  462. /* 4-Register Window Underflow Vector (Handler) */
  463. .align 64
  464. .global _WindowUnderflow4
  465. _WindowUnderflow4:
  466. l32e a0, a5, -16
  467. l32e a1, a5, -12
  468. l32e a2, a5, -8
  469. l32e a3, a5, -4
  470. rfwu
  471. /*
  472. * a0: a0
  473. * a1: new stack pointer = a1 - 16 - 4
  474. * a2: available, saved in excsave1
  475. * a3: available, saved on stack *a1
  476. */
  477. /* 15*/ .byte 0xff
  478. fast_alloca_exception: /* must be at _WindowUnderflow4 + 16 */
  479. /* 16*/ rsr a2, PS
  480. /* 19*/ rsr a3, WINDOWBASE
  481. /* 22*/ extui a2, a2, PS_OWB_SHIFT, PS_OWB_SHIFT
  482. /* 25*/ xor a2, a2, a3
  483. /* 28*/ rsr a3, PS
  484. /* 31*/ slli a2, a2, PS_OWB_SHIFT
  485. /* 34*/ xor a2, a3, a2
  486. /* 37*/ wsr a2, PS
  487. /* 40*/ _l32i a3, a1, 0
  488. /* 43*/ addi a1, a1, 16 + 4
  489. /* 46*/ rsr a2, EXCSAVE1
  490. /* 49*/ rotw -1
  491. /* 52*/ _bbci.l a4, 31, _WindowUnderflow4 /* 0x: call4 */
  492. /* 55*/ rotw -1
  493. /* 58*/ _bbci.l a8, 30, _WindowUnderflow8 /* 10: call8 */
  494. /* 61*/ _j __WindowUnderflow12 /* 11: call12 */
  495. /* 64*/
  496. /* 8-Register Window Overflow Vector (Handler) */
  497. .align 64
  498. .global _WindowOverflow8
  499. _WindowOverflow8:
  500. s32e a0, a9, -16
  501. l32e a0, a1, -12
  502. s32e a2, a9, -8
  503. s32e a1, a9, -12
  504. s32e a3, a9, -4
  505. s32e a4, a0, -32
  506. s32e a5, a0, -28
  507. s32e a6, a0, -24
  508. s32e a7, a0, -20
  509. rfwo
  510. /* 8-Register Window Underflow Vector (Handler) */
  511. .align 64
  512. .global _WindowUnderflow8
  513. _WindowUnderflow8:
  514. l32e a1, a9, -12
  515. l32e a0, a9, -16
  516. l32e a7, a1, -12
  517. l32e a2, a9, -8
  518. l32e a4, a7, -32
  519. l32e a3, a9, -4
  520. l32e a5, a7, -28
  521. l32e a6, a7, -24
  522. l32e a7, a7, -20
  523. rfwu
  524. /* 12-Register Window Overflow Vector (Handler) */
  525. .align 64
  526. .global _WindowOverflow12
  527. _WindowOverflow12:
  528. s32e a0, a13, -16
  529. l32e a0, a1, -12
  530. s32e a1, a13, -12
  531. s32e a2, a13, -8
  532. s32e a3, a13, -4
  533. s32e a4, a0, -48
  534. s32e a5, a0, -44
  535. s32e a6, a0, -40
  536. s32e a7, a0, -36
  537. s32e a8, a0, -32
  538. s32e a9, a0, -28
  539. s32e a10, a0, -24
  540. s32e a11, a0, -20
  541. rfwo
  542. /* 12-Register Window Underflow Vector (Handler) */
  543. .org _WindowOverflow12 + 64 - 3
  544. __WindowUnderflow12:
  545. rotw -1
  546. .global _WindowUnderflow12
  547. _WindowUnderflow12:
  548. l32e a1, a13, -12
  549. l32e a0, a13, -16
  550. l32e a11, a1, -12
  551. l32e a2, a13, -8
  552. l32e a4, a11, -48
  553. l32e a8, a11, -32
  554. l32e a3, a13, -4
  555. l32e a5, a11, -44
  556. l32e a6, a11, -40
  557. l32e a7, a11, -36
  558. l32e a9, a11, -28
  559. l32e a10, a11, -24
  560. l32e a11, a11, -20
  561. rfwu
  562. #endif /* XCHAL_HAVE_WINDOWED */