head.S 35 KB

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