head.S 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /*
  2. * linux/arch/arm/boot/compressed/head.S
  3. *
  4. * Copyright (C) 1996-2002 Russell King
  5. * Copyright (C) 2004 Hyok S. Choi (MPU support)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/linkage.h>
  12. #include <asm/assembler.h>
  13. #include <asm/v7m.h>
  14. #include "efi-header.S"
  15. AR_CLASS( .arch armv7-a )
  16. M_CLASS( .arch armv7-m )
  17. /*
  18. * Debugging stuff
  19. *
  20. * Note that these macros must not contain any code which is not
  21. * 100% relocatable. Any attempt to do so will result in a crash.
  22. * Please select one of the following when turning on debugging.
  23. */
  24. #ifdef DEBUG
  25. #if defined(CONFIG_DEBUG_ICEDCC)
  26. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
  27. .macro loadsp, rb, tmp1, tmp2
  28. .endm
  29. .macro writeb, ch, rb
  30. mcr p14, 0, \ch, c0, c5, 0
  31. .endm
  32. #elif defined(CONFIG_CPU_XSCALE)
  33. .macro loadsp, rb, tmp1, tmp2
  34. .endm
  35. .macro writeb, ch, rb
  36. mcr p14, 0, \ch, c8, c0, 0
  37. .endm
  38. #else
  39. .macro loadsp, rb, tmp1, tmp2
  40. .endm
  41. .macro writeb, ch, rb
  42. mcr p14, 0, \ch, c1, c0, 0
  43. .endm
  44. #endif
  45. #else
  46. #include CONFIG_DEBUG_LL_INCLUDE
  47. .macro writeb, ch, rb
  48. senduart \ch, \rb
  49. .endm
  50. #if defined(CONFIG_ARCH_SA1100)
  51. .macro loadsp, rb, tmp1, tmp2
  52. mov \rb, #0x80000000 @ physical base address
  53. #ifdef CONFIG_DEBUG_LL_SER3
  54. add \rb, \rb, #0x00050000 @ Ser3
  55. #else
  56. add \rb, \rb, #0x00010000 @ Ser1
  57. #endif
  58. .endm
  59. #else
  60. .macro loadsp, rb, tmp1, tmp2
  61. addruart \rb, \tmp1, \tmp2
  62. .endm
  63. #endif
  64. #endif
  65. #endif
  66. .macro kputc,val
  67. mov r0, \val
  68. bl putc
  69. .endm
  70. .macro kphex,val,len
  71. mov r0, \val
  72. mov r1, #\len
  73. bl phex
  74. .endm
  75. .macro debug_reloc_start
  76. #ifdef DEBUG
  77. kputc #'\n'
  78. kphex r6, 8 /* processor id */
  79. kputc #':'
  80. kphex r7, 8 /* architecture id */
  81. #ifdef CONFIG_CPU_CP15
  82. kputc #':'
  83. mrc p15, 0, r0, c1, c0
  84. kphex r0, 8 /* control reg */
  85. #endif
  86. kputc #'\n'
  87. kphex r5, 8 /* decompressed kernel start */
  88. kputc #'-'
  89. kphex r9, 8 /* decompressed kernel end */
  90. kputc #'>'
  91. kphex r4, 8 /* kernel execution address */
  92. kputc #'\n'
  93. #endif
  94. .endm
  95. .macro debug_reloc_end
  96. #ifdef DEBUG
  97. kphex r5, 8 /* end of kernel */
  98. kputc #'\n'
  99. mov r0, r4
  100. bl memdump /* dump 256 bytes at start of kernel */
  101. #endif
  102. .endm
  103. /*
  104. * Debug kernel copy by printing the memory addresses involved
  105. */
  106. .macro dbgkc, begin, end, cbegin, cend
  107. #ifdef DEBUG
  108. kputc #'\n'
  109. kputc #'C'
  110. kputc #':'
  111. kputc #'0'
  112. kputc #'x'
  113. kphex \begin, 8 /* Start of compressed kernel */
  114. kputc #'-'
  115. kputc #'0'
  116. kputc #'x'
  117. kphex \end, 8 /* End of compressed kernel */
  118. kputc #'-'
  119. kputc #'>'
  120. kputc #'0'
  121. kputc #'x'
  122. kphex \cbegin, 8 /* Start of kernel copy */
  123. kputc #'-'
  124. kputc #'0'
  125. kputc #'x'
  126. kphex \cend, 8 /* End of kernel copy */
  127. kputc #'\n'
  128. kputc #'\r'
  129. #endif
  130. .endm
  131. .section ".start", #alloc, #execinstr
  132. /*
  133. * sort out different calling conventions
  134. */
  135. .align
  136. /*
  137. * Always enter in ARM state for CPUs that support the ARM ISA.
  138. * As of today (2014) that's exactly the members of the A and R
  139. * classes.
  140. */
  141. AR_CLASS( .arm )
  142. start:
  143. .type start,#function
  144. .rept 7
  145. __nop
  146. .endr
  147. #ifndef CONFIG_THUMB2_KERNEL
  148. mov r0, r0
  149. #else
  150. AR_CLASS( sub pc, pc, #3 ) @ A/R: switch to Thumb2 mode
  151. M_CLASS( nop.w ) @ M: already in Thumb2 mode
  152. .thumb
  153. #endif
  154. W(b) 1f
  155. .word _magic_sig @ Magic numbers to help the loader
  156. .word _magic_start @ absolute load/run zImage address
  157. .word _magic_end @ zImage end address
  158. .word 0x04030201 @ endianness flag
  159. .word 0x45454545 @ another magic number to indicate
  160. .word _magic_table @ additional data table
  161. __EFI_HEADER
  162. 1:
  163. ARM_BE8( setend be ) @ go BE8 if compiled for BE8
  164. AR_CLASS( mrs r9, cpsr )
  165. #ifdef CONFIG_ARM_VIRT_EXT
  166. bl __hyp_stub_install @ get into SVC mode, reversibly
  167. #endif
  168. mov r7, r1 @ save architecture ID
  169. mov r8, r2 @ save atags pointer
  170. #ifndef CONFIG_CPU_V7M
  171. /*
  172. * Booting from Angel - need to enter SVC mode and disable
  173. * FIQs/IRQs (numeric definitions from angel arm.h source).
  174. * We only do this if we were in user mode on entry.
  175. */
  176. mrs r2, cpsr @ get current mode
  177. tst r2, #3 @ not user?
  178. bne not_angel
  179. mov r0, #0x17 @ angel_SWIreason_EnterSVC
  180. ARM( swi 0x123456 ) @ angel_SWI_ARM
  181. THUMB( svc 0xab ) @ angel_SWI_THUMB
  182. not_angel:
  183. safe_svcmode_maskall r0
  184. msr spsr_cxsf, r9 @ Save the CPU boot mode in
  185. @ SPSR
  186. #endif
  187. /*
  188. * Note that some cache flushing and other stuff may
  189. * be needed here - is there an Angel SWI call for this?
  190. */
  191. /*
  192. * some architecture specific code can be inserted
  193. * by the linker here, but it should preserve r7, r8, and r9.
  194. */
  195. .text
  196. #ifdef CONFIG_AUTO_ZRELADDR
  197. /*
  198. * Find the start of physical memory. As we are executing
  199. * without the MMU on, we are in the physical address space.
  200. * We just need to get rid of any offset by aligning the
  201. * address.
  202. *
  203. * This alignment is a balance between the requirements of
  204. * different platforms - we have chosen 128MB to allow
  205. * platforms which align the start of their physical memory
  206. * to 128MB to use this feature, while allowing the zImage
  207. * to be placed within the first 128MB of memory on other
  208. * platforms. Increasing the alignment means we place
  209. * stricter alignment requirements on the start of physical
  210. * memory, but relaxing it means that we break people who
  211. * are already placing their zImage in (eg) the top 64MB
  212. * of this range.
  213. */
  214. mov r4, pc
  215. and r4, r4, #0xf8000000
  216. /* Determine final kernel image address. */
  217. add r4, r4, #TEXT_OFFSET
  218. #else
  219. ldr r4, =zreladdr
  220. #endif
  221. /*
  222. * Set up a page table only if it won't overwrite ourself.
  223. * That means r4 < pc || r4 - 16k page directory > &_end.
  224. * Given that r4 > &_end is most unfrequent, we add a rough
  225. * additional 1MB of room for a possible appended DTB.
  226. */
  227. mov r0, pc
  228. cmp r0, r4
  229. ldrcc r0, LC0+32
  230. addcc r0, r0, pc
  231. cmpcc r4, r0
  232. orrcc r4, r4, #1 @ remember we skipped cache_on
  233. blcs cache_on
  234. restart: adr r0, LC0
  235. ldmia r0, {r1, r2, r3, r6, r10, r11, r12}
  236. ldr sp, [r0, #28]
  237. /*
  238. * We might be running at a different address. We need
  239. * to fix up various pointers.
  240. */
  241. sub r0, r0, r1 @ calculate the delta offset
  242. add r6, r6, r0 @ _edata
  243. add r10, r10, r0 @ inflated kernel size location
  244. /*
  245. * The kernel build system appends the size of the
  246. * decompressed kernel at the end of the compressed data
  247. * in little-endian form.
  248. */
  249. ldrb r9, [r10, #0]
  250. ldrb lr, [r10, #1]
  251. orr r9, r9, lr, lsl #8
  252. ldrb lr, [r10, #2]
  253. ldrb r10, [r10, #3]
  254. orr r9, r9, lr, lsl #16
  255. orr r9, r9, r10, lsl #24
  256. #ifndef CONFIG_ZBOOT_ROM
  257. /* malloc space is above the relocated stack (64k max) */
  258. add sp, sp, r0
  259. add r10, sp, #0x10000
  260. #else
  261. /*
  262. * With ZBOOT_ROM the bss/stack is non relocatable,
  263. * but someone could still run this code from RAM,
  264. * in which case our reference is _edata.
  265. */
  266. mov r10, r6
  267. #endif
  268. mov r5, #0 @ init dtb size to 0
  269. #ifdef CONFIG_ARM_APPENDED_DTB
  270. /*
  271. * r0 = delta
  272. * r2 = BSS start
  273. * r3 = BSS end
  274. * r4 = final kernel address (possibly with LSB set)
  275. * r5 = appended dtb size (still unknown)
  276. * r6 = _edata
  277. * r7 = architecture ID
  278. * r8 = atags/device tree pointer
  279. * r9 = size of decompressed image
  280. * r10 = end of this image, including bss/stack/malloc space if non XIP
  281. * r11 = GOT start
  282. * r12 = GOT end
  283. * sp = stack pointer
  284. *
  285. * if there are device trees (dtb) appended to zImage, advance r10 so that the
  286. * dtb data will get relocated along with the kernel if necessary.
  287. */
  288. ldr lr, [r6, #0]
  289. #ifndef __ARMEB__
  290. ldr r1, =0xedfe0dd0 @ sig is 0xd00dfeed big endian
  291. #else
  292. ldr r1, =0xd00dfeed
  293. #endif
  294. cmp lr, r1
  295. bne dtb_check_done @ not found
  296. #ifdef CONFIG_ARM_ATAG_DTB_COMPAT
  297. /*
  298. * OK... Let's do some funky business here.
  299. * If we do have a DTB appended to zImage, and we do have
  300. * an ATAG list around, we want the later to be translated
  301. * and folded into the former here. No GOT fixup has occurred
  302. * yet, but none of the code we're about to call uses any
  303. * global variable.
  304. */
  305. /* Get the initial DTB size */
  306. ldr r5, [r6, #4]
  307. #ifndef __ARMEB__
  308. /* convert to little endian */
  309. eor r1, r5, r5, ror #16
  310. bic r1, r1, #0x00ff0000
  311. mov r5, r5, ror #8
  312. eor r5, r5, r1, lsr #8
  313. #endif
  314. /* 50% DTB growth should be good enough */
  315. add r5, r5, r5, lsr #1
  316. /* preserve 64-bit alignment */
  317. add r5, r5, #7
  318. bic r5, r5, #7
  319. /* clamp to 32KB min and 1MB max */
  320. cmp r5, #(1 << 15)
  321. movlo r5, #(1 << 15)
  322. cmp r5, #(1 << 20)
  323. movhi r5, #(1 << 20)
  324. /* temporarily relocate the stack past the DTB work space */
  325. add sp, sp, r5
  326. stmfd sp!, {r0-r3, ip, lr}
  327. mov r0, r8
  328. mov r1, r6
  329. mov r2, r5
  330. bl atags_to_fdt
  331. /*
  332. * If returned value is 1, there is no ATAG at the location
  333. * pointed by r8. Try the typical 0x100 offset from start
  334. * of RAM and hope for the best.
  335. */
  336. cmp r0, #1
  337. sub r0, r4, #TEXT_OFFSET
  338. bic r0, r0, #1
  339. add r0, r0, #0x100
  340. mov r1, r6
  341. mov r2, r5
  342. bleq atags_to_fdt
  343. ldmfd sp!, {r0-r3, ip, lr}
  344. sub sp, sp, r5
  345. #endif
  346. mov r8, r6 @ use the appended device tree
  347. /*
  348. * Make sure that the DTB doesn't end up in the final
  349. * kernel's .bss area. To do so, we adjust the decompressed
  350. * kernel size to compensate if that .bss size is larger
  351. * than the relocated code.
  352. */
  353. ldr r5, =_kernel_bss_size
  354. adr r1, wont_overwrite
  355. sub r1, r6, r1
  356. subs r1, r5, r1
  357. addhi r9, r9, r1
  358. /* Get the current DTB size */
  359. ldr r5, [r6, #4]
  360. #ifndef __ARMEB__
  361. /* convert r5 (dtb size) to little endian */
  362. eor r1, r5, r5, ror #16
  363. bic r1, r1, #0x00ff0000
  364. mov r5, r5, ror #8
  365. eor r5, r5, r1, lsr #8
  366. #endif
  367. /* preserve 64-bit alignment */
  368. add r5, r5, #7
  369. bic r5, r5, #7
  370. /* relocate some pointers past the appended dtb */
  371. add r6, r6, r5
  372. add r10, r10, r5
  373. add sp, sp, r5
  374. dtb_check_done:
  375. #endif
  376. /*
  377. * Check to see if we will overwrite ourselves.
  378. * r4 = final kernel address (possibly with LSB set)
  379. * r9 = size of decompressed image
  380. * r10 = end of this image, including bss/stack/malloc space if non XIP
  381. * We basically want:
  382. * r4 - 16k page directory >= r10 -> OK
  383. * r4 + image length <= address of wont_overwrite -> OK
  384. * Note: the possible LSB in r4 is harmless here.
  385. */
  386. add r10, r10, #16384
  387. cmp r4, r10
  388. bhs wont_overwrite
  389. add r10, r4, r9
  390. adr r9, wont_overwrite
  391. cmp r10, r9
  392. bls wont_overwrite
  393. /*
  394. * Relocate ourselves past the end of the decompressed kernel.
  395. * r6 = _edata
  396. * r10 = end of the decompressed kernel
  397. * Because we always copy ahead, we need to do it from the end and go
  398. * backward in case the source and destination overlap.
  399. */
  400. /*
  401. * Bump to the next 256-byte boundary with the size of
  402. * the relocation code added. This avoids overwriting
  403. * ourself when the offset is small.
  404. */
  405. add r10, r10, #((reloc_code_end - restart + 256) & ~255)
  406. bic r10, r10, #255
  407. /* Get start of code we want to copy and align it down. */
  408. adr r5, restart
  409. bic r5, r5, #31
  410. /* Relocate the hyp vector base if necessary */
  411. #ifdef CONFIG_ARM_VIRT_EXT
  412. mrs r0, spsr
  413. and r0, r0, #MODE_MASK
  414. cmp r0, #HYP_MODE
  415. bne 1f
  416. /*
  417. * Compute the address of the hyp vectors after relocation.
  418. * This requires some arithmetic since we cannot directly
  419. * reference __hyp_stub_vectors in a PC-relative way.
  420. * Call __hyp_set_vectors with the new address so that we
  421. * can HVC again after the copy.
  422. */
  423. 0: adr r0, 0b
  424. movw r1, #:lower16:__hyp_stub_vectors - 0b
  425. movt r1, #:upper16:__hyp_stub_vectors - 0b
  426. add r0, r0, r1
  427. sub r0, r0, r5
  428. add r0, r0, r10
  429. bl __hyp_set_vectors
  430. 1:
  431. #endif
  432. sub r9, r6, r5 @ size to copy
  433. add r9, r9, #31 @ rounded up to a multiple
  434. bic r9, r9, #31 @ ... of 32 bytes
  435. add r6, r9, r5
  436. add r9, r9, r10
  437. #ifdef DEBUG
  438. sub r10, r6, r5
  439. sub r10, r9, r10
  440. /*
  441. * We are about to copy the kernel to a new memory area.
  442. * The boundaries of the new memory area can be found in
  443. * r10 and r9, whilst r5 and r6 contain the boundaries
  444. * of the memory we are going to copy.
  445. * Calling dbgkc will help with the printing of this
  446. * information.
  447. */
  448. dbgkc r5, r6, r10, r9
  449. #endif
  450. 1: ldmdb r6!, {r0 - r3, r10 - r12, lr}
  451. cmp r6, r5
  452. stmdb r9!, {r0 - r3, r10 - r12, lr}
  453. bhi 1b
  454. /* Preserve offset to relocated code. */
  455. sub r6, r9, r6
  456. #ifndef CONFIG_ZBOOT_ROM
  457. /* cache_clean_flush may use the stack, so relocate it */
  458. add sp, sp, r6
  459. #endif
  460. bl cache_clean_flush
  461. badr r0, restart
  462. add r0, r0, r6
  463. mov pc, r0
  464. wont_overwrite:
  465. /*
  466. * If delta is zero, we are running at the address we were linked at.
  467. * r0 = delta
  468. * r2 = BSS start
  469. * r3 = BSS end
  470. * r4 = kernel execution address (possibly with LSB set)
  471. * r5 = appended dtb size (0 if not present)
  472. * r7 = architecture ID
  473. * r8 = atags pointer
  474. * r11 = GOT start
  475. * r12 = GOT end
  476. * sp = stack pointer
  477. */
  478. orrs r1, r0, r5
  479. beq not_relocated
  480. add r11, r11, r0
  481. add r12, r12, r0
  482. #ifndef CONFIG_ZBOOT_ROM
  483. /*
  484. * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
  485. * we need to fix up pointers into the BSS region.
  486. * Note that the stack pointer has already been fixed up.
  487. */
  488. add r2, r2, r0
  489. add r3, r3, r0
  490. /*
  491. * Relocate all entries in the GOT table.
  492. * Bump bss entries to _edata + dtb size
  493. */
  494. 1: ldr r1, [r11, #0] @ relocate entries in the GOT
  495. add r1, r1, r0 @ This fixes up C references
  496. cmp r1, r2 @ if entry >= bss_start &&
  497. cmphs r3, r1 @ bss_end > entry
  498. addhi r1, r1, r5 @ entry += dtb size
  499. str r1, [r11], #4 @ next entry
  500. cmp r11, r12
  501. blo 1b
  502. /* bump our bss pointers too */
  503. add r2, r2, r5
  504. add r3, r3, r5
  505. #else
  506. /*
  507. * Relocate entries in the GOT table. We only relocate
  508. * the entries that are outside the (relocated) BSS region.
  509. */
  510. 1: ldr r1, [r11, #0] @ relocate entries in the GOT
  511. cmp r1, r2 @ entry < bss_start ||
  512. cmphs r3, r1 @ _end < entry
  513. addlo r1, r1, r0 @ table. This fixes up the
  514. str r1, [r11], #4 @ C references.
  515. cmp r11, r12
  516. blo 1b
  517. #endif
  518. not_relocated: mov r0, #0
  519. 1: str r0, [r2], #4 @ clear bss
  520. str r0, [r2], #4
  521. str r0, [r2], #4
  522. str r0, [r2], #4
  523. cmp r2, r3
  524. blo 1b
  525. /*
  526. * Did we skip the cache setup earlier?
  527. * That is indicated by the LSB in r4.
  528. * Do it now if so.
  529. */
  530. tst r4, #1
  531. bic r4, r4, #1
  532. blne cache_on
  533. /*
  534. * The C runtime environment should now be setup sufficiently.
  535. * Set up some pointers, and start decompressing.
  536. * r4 = kernel execution address
  537. * r7 = architecture ID
  538. * r8 = atags pointer
  539. */
  540. mov r0, r4
  541. mov r1, sp @ malloc space above stack
  542. add r2, sp, #0x10000 @ 64k max
  543. mov r3, r7
  544. bl decompress_kernel
  545. bl cache_clean_flush
  546. bl cache_off
  547. #ifdef CONFIG_ARM_VIRT_EXT
  548. mrs r0, spsr @ Get saved CPU boot mode
  549. and r0, r0, #MODE_MASK
  550. cmp r0, #HYP_MODE @ if not booted in HYP mode...
  551. bne __enter_kernel @ boot kernel directly
  552. adr r12, .L__hyp_reentry_vectors_offset
  553. ldr r0, [r12]
  554. add r0, r0, r12
  555. bl __hyp_set_vectors
  556. __HVC(0) @ otherwise bounce to hyp mode
  557. b . @ should never be reached
  558. .align 2
  559. .L__hyp_reentry_vectors_offset: .long __hyp_reentry_vectors - .
  560. #else
  561. b __enter_kernel
  562. #endif
  563. .align 2
  564. .type LC0, #object
  565. LC0: .word LC0 @ r1
  566. .word __bss_start @ r2
  567. .word _end @ r3
  568. .word _edata @ r6
  569. .word input_data_end - 4 @ r10 (inflated size location)
  570. .word _got_start @ r11
  571. .word _got_end @ ip
  572. .word .L_user_stack_end @ sp
  573. .word _end - restart + 16384 + 1024*1024
  574. .size LC0, . - LC0
  575. #ifdef CONFIG_ARCH_RPC
  576. .globl params
  577. params: ldr r0, =0x10000100 @ params_phys for RPC
  578. mov pc, lr
  579. .ltorg
  580. .align
  581. #endif
  582. /*
  583. * Turn on the cache. We need to setup some page tables so that we
  584. * can have both the I and D caches on.
  585. *
  586. * We place the page tables 16k down from the kernel execution address,
  587. * and we hope that nothing else is using it. If we're using it, we
  588. * will go pop!
  589. *
  590. * On entry,
  591. * r4 = kernel execution address
  592. * r7 = architecture number
  593. * r8 = atags pointer
  594. * On exit,
  595. * r0, r1, r2, r3, r9, r10, r12 corrupted
  596. * This routine must preserve:
  597. * r4, r7, r8
  598. */
  599. .align 5
  600. cache_on: mov r3, #8 @ cache_on function
  601. b call_cache_fn
  602. /*
  603. * Initialize the highest priority protection region, PR7
  604. * to cover all 32bit address and cacheable and bufferable.
  605. */
  606. __armv4_mpu_cache_on:
  607. mov r0, #0x3f @ 4G, the whole
  608. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  609. mcr p15, 0, r0, c6, c7, 1
  610. mov r0, #0x80 @ PR7
  611. mcr p15, 0, r0, c2, c0, 0 @ D-cache on
  612. mcr p15, 0, r0, c2, c0, 1 @ I-cache on
  613. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  614. mov r0, #0xc000
  615. mcr p15, 0, r0, c5, c0, 1 @ I-access permission
  616. mcr p15, 0, r0, c5, c0, 0 @ D-access permission
  617. mov r0, #0
  618. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  619. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  620. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  621. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  622. @ ...I .... ..D. WC.M
  623. orr r0, r0, #0x002d @ .... .... ..1. 11.1
  624. orr r0, r0, #0x1000 @ ...1 .... .... ....
  625. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  626. mov r0, #0
  627. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  628. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  629. mov pc, lr
  630. __armv3_mpu_cache_on:
  631. mov r0, #0x3f @ 4G, the whole
  632. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  633. mov r0, #0x80 @ PR7
  634. mcr p15, 0, r0, c2, c0, 0 @ cache on
  635. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  636. mov r0, #0xc000
  637. mcr p15, 0, r0, c5, c0, 0 @ access permission
  638. mov r0, #0
  639. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  640. /*
  641. * ?? ARMv3 MMU does not allow reading the control register,
  642. * does this really work on ARMv3 MPU?
  643. */
  644. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  645. @ .... .... .... WC.M
  646. orr r0, r0, #0x000d @ .... .... .... 11.1
  647. /* ?? this overwrites the value constructed above? */
  648. mov r0, #0
  649. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  650. /* ?? invalidate for the second time? */
  651. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  652. mov pc, lr
  653. #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
  654. #define CB_BITS 0x08
  655. #else
  656. #define CB_BITS 0x0c
  657. #endif
  658. __setup_mmu: sub r3, r4, #16384 @ Page directory size
  659. bic r3, r3, #0xff @ Align the pointer
  660. bic r3, r3, #0x3f00
  661. /*
  662. * Initialise the page tables, turning on the cacheable and bufferable
  663. * bits for the RAM area only.
  664. */
  665. mov r0, r3
  666. mov r9, r0, lsr #18
  667. mov r9, r9, lsl #18 @ start of RAM
  668. add r10, r9, #0x10000000 @ a reasonable RAM size
  669. mov r1, #0x12 @ XN|U + section mapping
  670. orr r1, r1, #3 << 10 @ AP=11
  671. add r2, r3, #16384
  672. 1: cmp r1, r9 @ if virt > start of RAM
  673. cmphs r10, r1 @ && end of RAM > virt
  674. bic r1, r1, #0x1c @ clear XN|U + C + B
  675. orrlo r1, r1, #0x10 @ Set XN|U for non-RAM
  676. orrhs r1, r1, r6 @ set RAM section settings
  677. str r1, [r0], #4 @ 1:1 mapping
  678. add r1, r1, #1048576
  679. teq r0, r2
  680. bne 1b
  681. /*
  682. * If ever we are running from Flash, then we surely want the cache
  683. * to be enabled also for our execution instance... We map 2MB of it
  684. * so there is no map overlap problem for up to 1 MB compressed kernel.
  685. * If the execution is in RAM then we would only be duplicating the above.
  686. */
  687. orr r1, r6, #0x04 @ ensure B is set for this
  688. orr r1, r1, #3 << 10
  689. mov r2, pc
  690. mov r2, r2, lsr #20
  691. orr r1, r1, r2, lsl #20
  692. add r0, r3, r2, lsl #2
  693. str r1, [r0], #4
  694. add r1, r1, #1048576
  695. str r1, [r0]
  696. mov pc, lr
  697. ENDPROC(__setup_mmu)
  698. @ Enable unaligned access on v6, to allow better code generation
  699. @ for the decompressor C code:
  700. __armv6_mmu_cache_on:
  701. mrc p15, 0, r0, c1, c0, 0 @ read SCTLR
  702. bic r0, r0, #2 @ A (no unaligned access fault)
  703. orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
  704. mcr p15, 0, r0, c1, c0, 0 @ write SCTLR
  705. b __armv4_mmu_cache_on
  706. __arm926ejs_mmu_cache_on:
  707. #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
  708. mov r0, #4 @ put dcache in WT mode
  709. mcr p15, 7, r0, c15, c0, 0
  710. #endif
  711. __armv4_mmu_cache_on:
  712. mov r12, lr
  713. #ifdef CONFIG_MMU
  714. mov r6, #CB_BITS | 0x12 @ U
  715. bl __setup_mmu
  716. mov r0, #0
  717. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  718. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  719. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  720. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  721. orr r0, r0, #0x0030
  722. ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
  723. bl __common_mmu_cache_on
  724. mov r0, #0
  725. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  726. #endif
  727. mov pc, r12
  728. __armv7_mmu_cache_on:
  729. mov r12, lr
  730. #ifdef CONFIG_MMU
  731. mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
  732. tst r11, #0xf @ VMSA
  733. movne r6, #CB_BITS | 0x02 @ !XN
  734. blne __setup_mmu
  735. mov r0, #0
  736. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  737. tst r11, #0xf @ VMSA
  738. mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  739. #endif
  740. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  741. bic r0, r0, #1 << 28 @ clear SCTLR.TRE
  742. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  743. orr r0, r0, #0x003c @ write buffer
  744. bic r0, r0, #2 @ A (no unaligned access fault)
  745. orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
  746. @ (needed for ARM1176)
  747. #ifdef CONFIG_MMU
  748. ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
  749. mrcne p15, 0, r6, c2, c0, 2 @ read ttb control reg
  750. orrne r0, r0, #1 @ MMU enabled
  751. movne r1, #0xfffffffd @ domain 0 = client
  752. bic r6, r6, #1 << 31 @ 32-bit translation system
  753. bic r6, r6, #(7 << 0) | (1 << 4) @ use only ttbr0
  754. mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
  755. mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
  756. mcrne p15, 0, r6, c2, c0, 2 @ load ttb control
  757. #endif
  758. mcr p15, 0, r0, c7, c5, 4 @ ISB
  759. mcr p15, 0, r0, c1, c0, 0 @ load control register
  760. mrc p15, 0, r0, c1, c0, 0 @ and read it back
  761. mov r0, #0
  762. mcr p15, 0, r0, c7, c5, 4 @ ISB
  763. mov pc, r12
  764. __fa526_cache_on:
  765. mov r12, lr
  766. mov r6, #CB_BITS | 0x12 @ U
  767. bl __setup_mmu
  768. mov r0, #0
  769. mcr p15, 0, r0, c7, c7, 0 @ Invalidate whole cache
  770. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  771. mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
  772. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  773. orr r0, r0, #0x1000 @ I-cache enable
  774. bl __common_mmu_cache_on
  775. mov r0, #0
  776. mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
  777. mov pc, r12
  778. __common_mmu_cache_on:
  779. #ifndef CONFIG_THUMB2_KERNEL
  780. #ifndef DEBUG
  781. orr r0, r0, #0x000d @ Write buffer, mmu
  782. #endif
  783. mov r1, #-1
  784. mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
  785. mcr p15, 0, r1, c3, c0, 0 @ load domain access control
  786. b 1f
  787. .align 5 @ cache line aligned
  788. 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
  789. mrc p15, 0, r0, c1, c0, 0 @ and read it back to
  790. sub pc, lr, r0, lsr #32 @ properly flush pipeline
  791. #endif
  792. #define PROC_ENTRY_SIZE (4*5)
  793. /*
  794. * Here follow the relocatable cache support functions for the
  795. * various processors. This is a generic hook for locating an
  796. * entry and jumping to an instruction at the specified offset
  797. * from the start of the block. Please note this is all position
  798. * independent code.
  799. *
  800. * r1 = corrupted
  801. * r2 = corrupted
  802. * r3 = block offset
  803. * r9 = corrupted
  804. * r12 = corrupted
  805. */
  806. call_cache_fn: adr r12, proc_types
  807. #ifdef CONFIG_CPU_CP15
  808. mrc p15, 0, r9, c0, c0 @ get processor ID
  809. #elif defined(CONFIG_CPU_V7M)
  810. /*
  811. * On v7-M the processor id is located in the V7M_SCB_CPUID
  812. * register, but as cache handling is IMPLEMENTATION DEFINED on
  813. * v7-M (if existant at all) we just return early here.
  814. * If V7M_SCB_CPUID were used the cpu ID functions (i.e.
  815. * __armv7_mmu_cache_{on,off,flush}) would be selected which
  816. * use cp15 registers that are not implemented on v7-M.
  817. */
  818. bx lr
  819. #else
  820. ldr r9, =CONFIG_PROCESSOR_ID
  821. #endif
  822. 1: ldr r1, [r12, #0] @ get value
  823. ldr r2, [r12, #4] @ get mask
  824. eor r1, r1, r9 @ (real ^ match)
  825. tst r1, r2 @ & mask
  826. ARM( addeq pc, r12, r3 ) @ call cache function
  827. THUMB( addeq r12, r3 )
  828. THUMB( moveq pc, r12 ) @ call cache function
  829. add r12, r12, #PROC_ENTRY_SIZE
  830. b 1b
  831. /*
  832. * Table for cache operations. This is basically:
  833. * - CPU ID match
  834. * - CPU ID mask
  835. * - 'cache on' method instruction
  836. * - 'cache off' method instruction
  837. * - 'cache flush' method instruction
  838. *
  839. * We match an entry using: ((real_id ^ match) & mask) == 0
  840. *
  841. * Writethrough caches generally only need 'on' and 'off'
  842. * methods. Writeback caches _must_ have the flush method
  843. * defined.
  844. */
  845. .align 2
  846. .type proc_types,#object
  847. proc_types:
  848. .word 0x41000000 @ old ARM ID
  849. .word 0xff00f000
  850. mov pc, lr
  851. THUMB( nop )
  852. mov pc, lr
  853. THUMB( nop )
  854. mov pc, lr
  855. THUMB( nop )
  856. .word 0x41007000 @ ARM7/710
  857. .word 0xfff8fe00
  858. mov pc, lr
  859. THUMB( nop )
  860. mov pc, lr
  861. THUMB( nop )
  862. mov pc, lr
  863. THUMB( nop )
  864. .word 0x41807200 @ ARM720T (writethrough)
  865. .word 0xffffff00
  866. W(b) __armv4_mmu_cache_on
  867. W(b) __armv4_mmu_cache_off
  868. mov pc, lr
  869. THUMB( nop )
  870. .word 0x41007400 @ ARM74x
  871. .word 0xff00ff00
  872. W(b) __armv3_mpu_cache_on
  873. W(b) __armv3_mpu_cache_off
  874. W(b) __armv3_mpu_cache_flush
  875. .word 0x41009400 @ ARM94x
  876. .word 0xff00ff00
  877. W(b) __armv4_mpu_cache_on
  878. W(b) __armv4_mpu_cache_off
  879. W(b) __armv4_mpu_cache_flush
  880. .word 0x41069260 @ ARM926EJ-S (v5TEJ)
  881. .word 0xff0ffff0
  882. W(b) __arm926ejs_mmu_cache_on
  883. W(b) __armv4_mmu_cache_off
  884. W(b) __armv5tej_mmu_cache_flush
  885. .word 0x00007000 @ ARM7 IDs
  886. .word 0x0000f000
  887. mov pc, lr
  888. THUMB( nop )
  889. mov pc, lr
  890. THUMB( nop )
  891. mov pc, lr
  892. THUMB( nop )
  893. @ Everything from here on will be the new ID system.
  894. .word 0x4401a100 @ sa110 / sa1100
  895. .word 0xffffffe0
  896. W(b) __armv4_mmu_cache_on
  897. W(b) __armv4_mmu_cache_off
  898. W(b) __armv4_mmu_cache_flush
  899. .word 0x6901b110 @ sa1110
  900. .word 0xfffffff0
  901. W(b) __armv4_mmu_cache_on
  902. W(b) __armv4_mmu_cache_off
  903. W(b) __armv4_mmu_cache_flush
  904. .word 0x56056900
  905. .word 0xffffff00 @ PXA9xx
  906. W(b) __armv4_mmu_cache_on
  907. W(b) __armv4_mmu_cache_off
  908. W(b) __armv4_mmu_cache_flush
  909. .word 0x56158000 @ PXA168
  910. .word 0xfffff000
  911. W(b) __armv4_mmu_cache_on
  912. W(b) __armv4_mmu_cache_off
  913. W(b) __armv5tej_mmu_cache_flush
  914. .word 0x56050000 @ Feroceon
  915. .word 0xff0f0000
  916. W(b) __armv4_mmu_cache_on
  917. W(b) __armv4_mmu_cache_off
  918. W(b) __armv5tej_mmu_cache_flush
  919. #ifdef CONFIG_CPU_FEROCEON_OLD_ID
  920. /* this conflicts with the standard ARMv5TE entry */
  921. .long 0x41009260 @ Old Feroceon
  922. .long 0xff00fff0
  923. b __armv4_mmu_cache_on
  924. b __armv4_mmu_cache_off
  925. b __armv5tej_mmu_cache_flush
  926. #endif
  927. .word 0x66015261 @ FA526
  928. .word 0xff01fff1
  929. W(b) __fa526_cache_on
  930. W(b) __armv4_mmu_cache_off
  931. W(b) __fa526_cache_flush
  932. @ These match on the architecture ID
  933. .word 0x00020000 @ ARMv4T
  934. .word 0x000f0000
  935. W(b) __armv4_mmu_cache_on
  936. W(b) __armv4_mmu_cache_off
  937. W(b) __armv4_mmu_cache_flush
  938. .word 0x00050000 @ ARMv5TE
  939. .word 0x000f0000
  940. W(b) __armv4_mmu_cache_on
  941. W(b) __armv4_mmu_cache_off
  942. W(b) __armv4_mmu_cache_flush
  943. .word 0x00060000 @ ARMv5TEJ
  944. .word 0x000f0000
  945. W(b) __armv4_mmu_cache_on
  946. W(b) __armv4_mmu_cache_off
  947. W(b) __armv5tej_mmu_cache_flush
  948. .word 0x0007b000 @ ARMv6
  949. .word 0x000ff000
  950. W(b) __armv6_mmu_cache_on
  951. W(b) __armv4_mmu_cache_off
  952. W(b) __armv6_mmu_cache_flush
  953. .word 0x000f0000 @ new CPU Id
  954. .word 0x000f0000
  955. W(b) __armv7_mmu_cache_on
  956. W(b) __armv7_mmu_cache_off
  957. W(b) __armv7_mmu_cache_flush
  958. .word 0 @ unrecognised type
  959. .word 0
  960. mov pc, lr
  961. THUMB( nop )
  962. mov pc, lr
  963. THUMB( nop )
  964. mov pc, lr
  965. THUMB( nop )
  966. .size proc_types, . - proc_types
  967. /*
  968. * If you get a "non-constant expression in ".if" statement"
  969. * error from the assembler on this line, check that you have
  970. * not accidentally written a "b" instruction where you should
  971. * have written W(b).
  972. */
  973. .if (. - proc_types) % PROC_ENTRY_SIZE != 0
  974. .error "The size of one or more proc_types entries is wrong."
  975. .endif
  976. /*
  977. * Turn off the Cache and MMU. ARMv3 does not support
  978. * reading the control register, but ARMv4 does.
  979. *
  980. * On exit,
  981. * r0, r1, r2, r3, r9, r12 corrupted
  982. * This routine must preserve:
  983. * r4, r7, r8
  984. */
  985. .align 5
  986. cache_off: mov r3, #12 @ cache_off function
  987. b call_cache_fn
  988. __armv4_mpu_cache_off:
  989. mrc p15, 0, r0, c1, c0
  990. bic r0, r0, #0x000d
  991. mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
  992. mov r0, #0
  993. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  994. mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
  995. mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
  996. mov pc, lr
  997. __armv3_mpu_cache_off:
  998. mrc p15, 0, r0, c1, c0
  999. bic r0, r0, #0x000d
  1000. mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
  1001. mov r0, #0
  1002. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  1003. mov pc, lr
  1004. __armv4_mmu_cache_off:
  1005. #ifdef CONFIG_MMU
  1006. mrc p15, 0, r0, c1, c0
  1007. bic r0, r0, #0x000d
  1008. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  1009. mov r0, #0
  1010. mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
  1011. mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
  1012. #endif
  1013. mov pc, lr
  1014. __armv7_mmu_cache_off:
  1015. mrc p15, 0, r0, c1, c0
  1016. #ifdef CONFIG_MMU
  1017. bic r0, r0, #0x000d
  1018. #else
  1019. bic r0, r0, #0x000c
  1020. #endif
  1021. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  1022. mov r12, lr
  1023. bl __armv7_mmu_cache_flush
  1024. mov r0, #0
  1025. #ifdef CONFIG_MMU
  1026. mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
  1027. #endif
  1028. mcr p15, 0, r0, c7, c5, 6 @ invalidate BTC
  1029. mcr p15, 0, r0, c7, c10, 4 @ DSB
  1030. mcr p15, 0, r0, c7, c5, 4 @ ISB
  1031. mov pc, r12
  1032. /*
  1033. * Clean and flush the cache to maintain consistency.
  1034. *
  1035. * On exit,
  1036. * r1, r2, r3, r9, r10, r11, r12 corrupted
  1037. * This routine must preserve:
  1038. * r4, r6, r7, r8
  1039. */
  1040. .align 5
  1041. cache_clean_flush:
  1042. mov r3, #16
  1043. b call_cache_fn
  1044. __armv4_mpu_cache_flush:
  1045. tst r4, #1
  1046. movne pc, lr
  1047. mov r2, #1
  1048. mov r3, #0
  1049. mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
  1050. mov r1, #7 << 5 @ 8 segments
  1051. 1: orr r3, r1, #63 << 26 @ 64 entries
  1052. 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
  1053. subs r3, r3, #1 << 26
  1054. bcs 2b @ entries 63 to 0
  1055. subs r1, r1, #1 << 5
  1056. bcs 1b @ segments 7 to 0
  1057. teq r2, #0
  1058. mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
  1059. mcr p15, 0, ip, c7, c10, 4 @ drain WB
  1060. mov pc, lr
  1061. __fa526_cache_flush:
  1062. tst r4, #1
  1063. movne pc, lr
  1064. mov r1, #0
  1065. mcr p15, 0, r1, c7, c14, 0 @ clean and invalidate D cache
  1066. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  1067. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  1068. mov pc, lr
  1069. __armv6_mmu_cache_flush:
  1070. mov r1, #0
  1071. tst r4, #1
  1072. mcreq p15, 0, r1, c7, c14, 0 @ clean+invalidate D
  1073. mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
  1074. mcreq p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
  1075. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  1076. mov pc, lr
  1077. __armv7_mmu_cache_flush:
  1078. tst r4, #1
  1079. bne iflush
  1080. mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
  1081. tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
  1082. mov r10, #0
  1083. beq hierarchical
  1084. mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
  1085. b iflush
  1086. hierarchical:
  1087. mcr p15, 0, r10, c7, c10, 5 @ DMB
  1088. stmfd sp!, {r0-r7, r9-r11}
  1089. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  1090. ands r3, r0, #0x7000000 @ extract loc from clidr
  1091. mov r3, r3, lsr #23 @ left align loc bit field
  1092. beq finished @ if loc is 0, then no need to clean
  1093. mov r10, #0 @ start clean at cache level 0
  1094. loop1:
  1095. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  1096. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  1097. and r1, r1, #7 @ mask of the bits for current cache only
  1098. cmp r1, #2 @ see what cache we have at this level
  1099. blt skip @ skip if no cache, or just i-cache
  1100. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  1101. mcr p15, 0, r10, c7, c5, 4 @ isb to sych the new cssr&csidr
  1102. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  1103. and r2, r1, #7 @ extract the length of the cache lines
  1104. add r2, r2, #4 @ add 4 (line length offset)
  1105. ldr r4, =0x3ff
  1106. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  1107. clz r5, r4 @ find bit position of way size increment
  1108. ldr r7, =0x7fff
  1109. ands r7, r7, r1, lsr #13 @ extract max number of the index size
  1110. loop2:
  1111. mov r9, r4 @ create working copy of max way size
  1112. loop3:
  1113. ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
  1114. ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
  1115. THUMB( lsl r6, r9, r5 )
  1116. THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
  1117. THUMB( lsl r6, r7, r2 )
  1118. THUMB( orr r11, r11, r6 ) @ factor index number into r11
  1119. mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
  1120. subs r9, r9, #1 @ decrement the way
  1121. bge loop3
  1122. subs r7, r7, #1 @ decrement the index
  1123. bge loop2
  1124. skip:
  1125. add r10, r10, #2 @ increment cache number
  1126. cmp r3, r10
  1127. bgt loop1
  1128. finished:
  1129. ldmfd sp!, {r0-r7, r9-r11}
  1130. mov r10, #0 @ switch back to cache level 0
  1131. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  1132. iflush:
  1133. mcr p15, 0, r10, c7, c10, 4 @ DSB
  1134. mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
  1135. mcr p15, 0, r10, c7, c10, 4 @ DSB
  1136. mcr p15, 0, r10, c7, c5, 4 @ ISB
  1137. mov pc, lr
  1138. __armv5tej_mmu_cache_flush:
  1139. tst r4, #1
  1140. movne pc, lr
  1141. 1: mrc p15, 0, r15, c7, c14, 3 @ test,clean,invalidate D cache
  1142. bne 1b
  1143. mcr p15, 0, r0, c7, c5, 0 @ flush I cache
  1144. mcr p15, 0, r0, c7, c10, 4 @ drain WB
  1145. mov pc, lr
  1146. __armv4_mmu_cache_flush:
  1147. tst r4, #1
  1148. movne pc, lr
  1149. mov r2, #64*1024 @ default: 32K dcache size (*2)
  1150. mov r11, #32 @ default: 32 byte line size
  1151. mrc p15, 0, r3, c0, c0, 1 @ read cache type
  1152. teq r3, r9 @ cache ID register present?
  1153. beq no_cache_id
  1154. mov r1, r3, lsr #18
  1155. and r1, r1, #7
  1156. mov r2, #1024
  1157. mov r2, r2, lsl r1 @ base dcache size *2
  1158. tst r3, #1 << 14 @ test M bit
  1159. addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
  1160. mov r3, r3, lsr #12
  1161. and r3, r3, #3
  1162. mov r11, #8
  1163. mov r11, r11, lsl r3 @ cache line size in bytes
  1164. no_cache_id:
  1165. mov r1, pc
  1166. bic r1, r1, #63 @ align to longest cache line
  1167. add r2, r1, r2
  1168. 1:
  1169. ARM( ldr r3, [r1], r11 ) @ s/w flush D cache
  1170. THUMB( ldr r3, [r1] ) @ s/w flush D cache
  1171. THUMB( add r1, r1, r11 )
  1172. teq r1, r2
  1173. bne 1b
  1174. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  1175. mcr p15, 0, r1, c7, c6, 0 @ flush D cache
  1176. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  1177. mov pc, lr
  1178. __armv3_mmu_cache_flush:
  1179. __armv3_mpu_cache_flush:
  1180. tst r4, #1
  1181. movne pc, lr
  1182. mov r1, #0
  1183. mcr p15, 0, r1, c7, c0, 0 @ invalidate whole cache v3
  1184. mov pc, lr
  1185. /*
  1186. * Various debugging routines for printing hex characters and
  1187. * memory, which again must be relocatable.
  1188. */
  1189. #ifdef DEBUG
  1190. .align 2
  1191. .type phexbuf,#object
  1192. phexbuf: .space 12
  1193. .size phexbuf, . - phexbuf
  1194. @ phex corrupts {r0, r1, r2, r3}
  1195. phex: adr r3, phexbuf
  1196. mov r2, #0
  1197. strb r2, [r3, r1]
  1198. 1: subs r1, r1, #1
  1199. movmi r0, r3
  1200. bmi puts
  1201. and r2, r0, #15
  1202. mov r0, r0, lsr #4
  1203. cmp r2, #10
  1204. addge r2, r2, #7
  1205. add r2, r2, #'0'
  1206. strb r2, [r3, r1]
  1207. b 1b
  1208. @ puts corrupts {r0, r1, r2, r3}
  1209. puts: loadsp r3, r2, r1
  1210. 1: ldrb r2, [r0], #1
  1211. teq r2, #0
  1212. moveq pc, lr
  1213. 2: writeb r2, r3
  1214. mov r1, #0x00020000
  1215. 3: subs r1, r1, #1
  1216. bne 3b
  1217. teq r2, #'\n'
  1218. moveq r2, #'\r'
  1219. beq 2b
  1220. teq r0, #0
  1221. bne 1b
  1222. mov pc, lr
  1223. @ putc corrupts {r0, r1, r2, r3}
  1224. putc:
  1225. mov r2, r0
  1226. loadsp r3, r1, r0
  1227. mov r0, #0
  1228. b 2b
  1229. @ memdump corrupts {r0, r1, r2, r3, r10, r11, r12, lr}
  1230. memdump: mov r12, r0
  1231. mov r10, lr
  1232. mov r11, #0
  1233. 2: mov r0, r11, lsl #2
  1234. add r0, r0, r12
  1235. mov r1, #8
  1236. bl phex
  1237. mov r0, #':'
  1238. bl putc
  1239. 1: mov r0, #' '
  1240. bl putc
  1241. ldr r0, [r12, r11, lsl #2]
  1242. mov r1, #8
  1243. bl phex
  1244. and r0, r11, #7
  1245. teq r0, #3
  1246. moveq r0, #' '
  1247. bleq putc
  1248. and r0, r11, #7
  1249. add r11, r11, #1
  1250. teq r0, #7
  1251. bne 1b
  1252. mov r0, #'\n'
  1253. bl putc
  1254. cmp r11, #64
  1255. blt 2b
  1256. mov pc, r10
  1257. #endif
  1258. .ltorg
  1259. #ifdef CONFIG_ARM_VIRT_EXT
  1260. .align 5
  1261. __hyp_reentry_vectors:
  1262. W(b) . @ reset
  1263. W(b) . @ undef
  1264. W(b) . @ svc
  1265. W(b) . @ pabort
  1266. W(b) . @ dabort
  1267. W(b) __enter_kernel @ hyp
  1268. W(b) . @ irq
  1269. W(b) . @ fiq
  1270. #endif /* CONFIG_ARM_VIRT_EXT */
  1271. __enter_kernel:
  1272. mov r0, #0 @ must be 0
  1273. mov r1, r7 @ restore architecture number
  1274. mov r2, r8 @ restore atags pointer
  1275. ARM( mov pc, r4 ) @ call kernel
  1276. M_CLASS( add r4, r4, #1 ) @ enter in Thumb mode for M class
  1277. THUMB( bx r4 ) @ entry point is always ARM for A/R classes
  1278. reloc_code_end:
  1279. #ifdef CONFIG_EFI_STUB
  1280. .align 2
  1281. _start: .long start - .
  1282. ENTRY(efi_stub_entry)
  1283. @ allocate space on stack for passing current zImage address
  1284. @ and for the EFI stub to return of new entry point of
  1285. @ zImage, as EFI stub may copy the kernel. Pointer address
  1286. @ is passed in r2. r0 and r1 are passed through from the
  1287. @ EFI firmware to efi_entry
  1288. adr ip, _start
  1289. ldr r3, [ip]
  1290. add r3, r3, ip
  1291. stmfd sp!, {r3, lr}
  1292. mov r2, sp @ pass zImage address in r2
  1293. bl efi_entry
  1294. @ Check for error return from EFI stub. r0 has FDT address
  1295. @ or error code.
  1296. cmn r0, #1
  1297. beq efi_load_fail
  1298. @ Preserve return value of efi_entry() in r4
  1299. mov r4, r0
  1300. bl cache_clean_flush
  1301. bl cache_off
  1302. @ Set parameters for booting zImage according to boot protocol
  1303. @ put FDT address in r2, it was returned by efi_entry()
  1304. @ r1 is the machine type, and r0 needs to be 0
  1305. mov r0, #0
  1306. mov r1, #0xFFFFFFFF
  1307. mov r2, r4
  1308. @ Branch to (possibly) relocated zImage that is in [sp]
  1309. ldr lr, [sp]
  1310. ldr ip, =start_offset
  1311. add lr, lr, ip
  1312. mov pc, lr @ no mode switch
  1313. efi_load_fail:
  1314. @ Return EFI_LOAD_ERROR to EFI firmware on error.
  1315. ldr r0, =0x80000001
  1316. ldmfd sp!, {ip, pc}
  1317. ENDPROC(efi_stub_entry)
  1318. #endif
  1319. .align
  1320. .section ".stack", "aw", %nobits
  1321. .L_user_stack: .space 4096
  1322. .L_user_stack_end: