head.S 38 KB

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