head.S 35 KB

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