head.S 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. * linux/arch/arm/boot/compressed/head.S
  3. *
  4. * Copyright (C) 1996-2002 Russell King
  5. * Copyright (C) 2004 Hyok S. Choi (MPU support)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/linkage.h>
  12. #include <asm/assembler.h>
  13. #include <asm/v7m.h>
  14. #include "efi-header.S"
  15. AR_CLASS( .arch armv7-a )
  16. M_CLASS( .arch armv7-m )
  17. /*
  18. * Debugging stuff
  19. *
  20. * Note that these macros must not contain any code which is not
  21. * 100% relocatable. Any attempt to do so will result in a crash.
  22. * Please select one of the following when turning on debugging.
  23. */
  24. #ifdef DEBUG
  25. #if defined(CONFIG_DEBUG_ICEDCC)
  26. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
  27. .macro loadsp, rb, tmp1, tmp2
  28. .endm
  29. .macro writeb, ch, rb
  30. mcr p14, 0, \ch, c0, c5, 0
  31. .endm
  32. #elif defined(CONFIG_CPU_XSCALE)
  33. .macro loadsp, rb, tmp1, tmp2
  34. .endm
  35. .macro writeb, ch, rb
  36. mcr p14, 0, \ch, c8, c0, 0
  37. .endm
  38. #else
  39. .macro loadsp, rb, tmp1, tmp2
  40. .endm
  41. .macro writeb, ch, rb
  42. mcr p14, 0, \ch, c1, c0, 0
  43. .endm
  44. #endif
  45. #else
  46. #include CONFIG_DEBUG_LL_INCLUDE
  47. .macro writeb, ch, rb
  48. senduart \ch, \rb
  49. .endm
  50. #if defined(CONFIG_ARCH_SA1100)
  51. .macro loadsp, rb, tmp1, tmp2
  52. mov \rb, #0x80000000 @ physical base address
  53. #ifdef CONFIG_DEBUG_LL_SER3
  54. add \rb, \rb, #0x00050000 @ Ser3
  55. #else
  56. add \rb, \rb, #0x00010000 @ Ser1
  57. #endif
  58. .endm
  59. #else
  60. .macro loadsp, rb, tmp1, tmp2
  61. addruart \rb, \tmp1, \tmp2
  62. .endm
  63. #endif
  64. #endif
  65. #endif
  66. .macro kputc,val
  67. mov r0, \val
  68. bl putc
  69. .endm
  70. .macro kphex,val,len
  71. mov r0, \val
  72. mov r1, #\len
  73. bl phex
  74. .endm
  75. .macro debug_reloc_start
  76. #ifdef DEBUG
  77. kputc #'\n'
  78. kphex r6, 8 /* processor id */
  79. kputc #':'
  80. kphex r7, 8 /* architecture id */
  81. #ifdef CONFIG_CPU_CP15
  82. kputc #':'
  83. mrc p15, 0, r0, c1, c0
  84. kphex r0, 8 /* control reg */
  85. #endif
  86. kputc #'\n'
  87. kphex r5, 8 /* decompressed kernel start */
  88. kputc #'-'
  89. kphex r9, 8 /* decompressed kernel end */
  90. kputc #'>'
  91. kphex r4, 8 /* kernel execution address */
  92. kputc #'\n'
  93. #endif
  94. .endm
  95. .macro debug_reloc_end
  96. #ifdef DEBUG
  97. kphex r5, 8 /* end of kernel */
  98. kputc #'\n'
  99. mov r0, r4
  100. bl memdump /* dump 256 bytes at start of kernel */
  101. #endif
  102. .endm
  103. .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. #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, #0x0005
  977. #else
  978. bic r0, r0, #0x0004
  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, r2, 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. loadsp r3, r1, r0
  1186. mov r0, #0
  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. mov r1, r7 @ restore architecture number
  1233. mov r2, r8 @ restore atags pointer
  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. @ our cache maintenance code relies on CP15 barrier instructions
  1260. @ but since we arrived here with the MMU and caches configured
  1261. @ by UEFI, we must check that the CP15BEN bit is set in SCTLR.
  1262. @ Note that this bit is RAO/WI on v6 and earlier, so the ISB in
  1263. @ the enable path will be executed on v7+ only.
  1264. mrc p15, 0, r1, c1, c0, 0 @ read SCTLR
  1265. tst r1, #(1 << 5) @ CP15BEN bit set?
  1266. bne 0f
  1267. orr r1, r1, #(1 << 5) @ CP15 barrier instructions
  1268. mcr p15, 0, r1, c1, c0, 0 @ write SCTLR
  1269. ARM( .inst 0xf57ff06f @ v7+ isb )
  1270. THUMB( isb )
  1271. 0: bl cache_clean_flush
  1272. bl cache_off
  1273. @ Set parameters for booting zImage according to boot protocol
  1274. @ put FDT address in r2, it was returned by efi_entry()
  1275. @ r1 is the machine type, and r0 needs to be 0
  1276. mov r0, #0
  1277. mov r1, #0xFFFFFFFF
  1278. mov r2, r4
  1279. @ Branch to (possibly) relocated zImage that is in [sp]
  1280. ldr lr, [sp]
  1281. ldr ip, =start_offset
  1282. add lr, lr, ip
  1283. mov pc, lr @ no mode switch
  1284. efi_load_fail:
  1285. @ Return EFI_LOAD_ERROR to EFI firmware on error.
  1286. ldr r0, =0x80000001
  1287. ldmfd sp!, {ip, pc}
  1288. ENDPROC(efi_stub_entry)
  1289. #endif
  1290. .align
  1291. .section ".stack", "aw", %nobits
  1292. .L_user_stack: .space 4096
  1293. .L_user_stack_end: