head.S 35 KB

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