head.S 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. /*
  13. * Debugging stuff
  14. *
  15. * Note that these macros must not contain any code which is not
  16. * 100% relocatable. Any attempt to do so will result in a crash.
  17. * Please select one of the following when turning on debugging.
  18. */
  19. #ifdef DEBUG
  20. #if defined(CONFIG_DEBUG_ICEDCC)
  21. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
  22. .macro loadsp, rb, tmp
  23. .endm
  24. .macro writeb, ch, rb
  25. mcr p14, 0, \ch, c0, c5, 0
  26. .endm
  27. #elif defined(CONFIG_CPU_XSCALE)
  28. .macro loadsp, rb, tmp
  29. .endm
  30. .macro writeb, ch, rb
  31. mcr p14, 0, \ch, c8, c0, 0
  32. .endm
  33. #else
  34. .macro loadsp, rb, tmp
  35. .endm
  36. .macro writeb, ch, rb
  37. mcr p14, 0, \ch, c1, c0, 0
  38. .endm
  39. #endif
  40. #else
  41. #include <mach/debug-macro.S>
  42. .macro writeb, ch, rb
  43. senduart \ch, \rb
  44. .endm
  45. #if defined(CONFIG_ARCH_SA1100)
  46. .macro loadsp, rb, tmp
  47. mov \rb, #0x80000000 @ physical base address
  48. #ifdef CONFIG_DEBUG_LL_SER3
  49. add \rb, \rb, #0x00050000 @ Ser3
  50. #else
  51. add \rb, \rb, #0x00010000 @ Ser1
  52. #endif
  53. .endm
  54. #elif defined(CONFIG_ARCH_S3C2410)
  55. .macro loadsp, rb, tmp
  56. mov \rb, #0x50000000
  57. add \rb, \rb, #0x4000 * CONFIG_S3C_LOWLEVEL_UART_PORT
  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. .arm @ Always enter in ARM state
  109. start:
  110. .type start,#function
  111. .rept 7
  112. mov r0, r0
  113. .endr
  114. ARM( mov r0, r0 )
  115. ARM( b 1f )
  116. THUMB( adr r12, BSYM(1f) )
  117. THUMB( bx r12 )
  118. .word 0x016f2818 @ Magic numbers to help the loader
  119. .word start @ absolute load/run zImage address
  120. .word _edata @ zImage end address
  121. THUMB( .thumb )
  122. 1: mov r7, r1 @ save architecture ID
  123. mov r8, r2 @ save atags pointer
  124. #ifndef __ARM_ARCH_2__
  125. /*
  126. * Booting from Angel - need to enter SVC mode and disable
  127. * FIQs/IRQs (numeric definitions from angel arm.h source).
  128. * We only do this if we were in user mode on entry.
  129. */
  130. mrs r2, cpsr @ get current mode
  131. tst r2, #3 @ not user?
  132. bne not_angel
  133. mov r0, #0x17 @ angel_SWIreason_EnterSVC
  134. ARM( swi 0x123456 ) @ angel_SWI_ARM
  135. THUMB( svc 0xab ) @ angel_SWI_THUMB
  136. not_angel:
  137. mrs r2, cpsr @ turn off interrupts to
  138. orr r2, r2, #0xc0 @ prevent angel from running
  139. msr cpsr_c, r2
  140. #else
  141. teqp pc, #0x0c000003 @ turn off interrupts
  142. #endif
  143. /*
  144. * Note that some cache flushing and other stuff may
  145. * be needed here - is there an Angel SWI call for this?
  146. */
  147. /*
  148. * some architecture specific code can be inserted
  149. * by the linker here, but it should preserve r7, r8, and r9.
  150. */
  151. .text
  152. #ifdef CONFIG_AUTO_ZRELADDR
  153. @ determine final kernel image address
  154. mov r4, pc
  155. and r4, r4, #0xf8000000
  156. add r4, r4, #TEXT_OFFSET
  157. #else
  158. ldr r4, =zreladdr
  159. #endif
  160. bl cache_on
  161. restart: adr r0, LC0
  162. ldmia r0, {r1, r2, r3, r6, r10, r11, r12}
  163. ldr sp, [r0, #28]
  164. /*
  165. * We might be running at a different address. We need
  166. * to fix up various pointers.
  167. */
  168. sub r0, r0, r1 @ calculate the delta offset
  169. add r6, r6, r0 @ _edata
  170. add r10, r10, r0 @ inflated kernel size location
  171. /*
  172. * The kernel build system appends the size of the
  173. * decompressed kernel at the end of the compressed data
  174. * in little-endian form.
  175. */
  176. ldrb r9, [r10, #0]
  177. ldrb lr, [r10, #1]
  178. orr r9, r9, lr, lsl #8
  179. ldrb lr, [r10, #2]
  180. ldrb r10, [r10, #3]
  181. orr r9, r9, lr, lsl #16
  182. orr r9, r9, r10, lsl #24
  183. #ifndef CONFIG_ZBOOT_ROM
  184. /* malloc space is above the relocated stack (64k max) */
  185. add sp, sp, r0
  186. add r10, sp, #0x10000
  187. #else
  188. /*
  189. * With ZBOOT_ROM the bss/stack is non relocatable,
  190. * but someone could still run this code from RAM,
  191. * in which case our reference is _edata.
  192. */
  193. mov r10, r6
  194. #endif
  195. mov r5, #0 @ init dtb size to 0
  196. #ifdef CONFIG_ARM_APPENDED_DTB
  197. /*
  198. * r0 = delta
  199. * r2 = BSS start
  200. * r3 = BSS end
  201. * r4 = final kernel address
  202. * r5 = appended dtb size (still unknown)
  203. * r6 = _edata
  204. * r7 = architecture ID
  205. * r8 = atags/device tree pointer
  206. * r9 = size of decompressed image
  207. * r10 = end of this image, including bss/stack/malloc space if non XIP
  208. * r11 = GOT start
  209. * r12 = GOT end
  210. * sp = stack pointer
  211. *
  212. * if there are device trees (dtb) appended to zImage, advance r10 so that the
  213. * dtb data will get relocated along with the kernel if necessary.
  214. */
  215. ldr lr, [r6, #0]
  216. #ifndef __ARMEB__
  217. ldr r1, =0xedfe0dd0 @ sig is 0xd00dfeed big endian
  218. #else
  219. ldr r1, =0xd00dfeed
  220. #endif
  221. cmp lr, r1
  222. bne dtb_check_done @ not found
  223. mov r8, r6 @ use the appended device tree
  224. /* Get the dtb's size */
  225. ldr r5, [r6, #4]
  226. #ifndef __ARMEB__
  227. /* convert r5 (dtb size) to little endian */
  228. eor r1, r5, r5, ror #16
  229. bic r1, r1, #0x00ff0000
  230. mov r5, r5, ror #8
  231. eor r5, r5, r1, lsr #8
  232. #endif
  233. /* preserve 64-bit alignment */
  234. add r5, r5, #7
  235. bic r5, r5, #7
  236. /* relocate some pointers past the appended dtb */
  237. add r6, r6, r5
  238. add r10, r10, r5
  239. add sp, sp, r5
  240. dtb_check_done:
  241. #endif
  242. /*
  243. * Check to see if we will overwrite ourselves.
  244. * r4 = final kernel address
  245. * r9 = size of decompressed image
  246. * r10 = end of this image, including bss/stack/malloc space if non XIP
  247. * We basically want:
  248. * r4 - 16k page directory >= r10 -> OK
  249. * r4 + image length <= current position (pc) -> OK
  250. */
  251. add r10, r10, #16384
  252. cmp r4, r10
  253. bhs wont_overwrite
  254. add r10, r4, r9
  255. ARM( cmp r10, pc )
  256. THUMB( mov lr, pc )
  257. THUMB( cmp r10, lr )
  258. bls wont_overwrite
  259. /*
  260. * Relocate ourselves past the end of the decompressed kernel.
  261. * r6 = _edata
  262. * r10 = end of the decompressed kernel
  263. * Because we always copy ahead, we need to do it from the end and go
  264. * backward in case the source and destination overlap.
  265. */
  266. /*
  267. * Bump to the next 256-byte boundary with the size of
  268. * the relocation code added. This avoids overwriting
  269. * ourself when the offset is small.
  270. */
  271. add r10, r10, #((reloc_code_end - restart + 256) & ~255)
  272. bic r10, r10, #255
  273. /* Get start of code we want to copy and align it down. */
  274. adr r5, restart
  275. bic r5, r5, #31
  276. sub r9, r6, r5 @ size to copy
  277. add r9, r9, #31 @ rounded up to a multiple
  278. bic r9, r9, #31 @ ... of 32 bytes
  279. add r6, r9, r5
  280. add r9, r9, r10
  281. 1: ldmdb r6!, {r0 - r3, r10 - r12, lr}
  282. cmp r6, r5
  283. stmdb r9!, {r0 - r3, r10 - r12, lr}
  284. bhi 1b
  285. /* Preserve offset to relocated code. */
  286. sub r6, r9, r6
  287. #ifndef CONFIG_ZBOOT_ROM
  288. /* cache_clean_flush may use the stack, so relocate it */
  289. add sp, sp, r6
  290. #endif
  291. bl cache_clean_flush
  292. adr r0, BSYM(restart)
  293. add r0, r0, r6
  294. mov pc, r0
  295. wont_overwrite:
  296. /*
  297. * If delta is zero, we are running at the address we were linked at.
  298. * r0 = delta
  299. * r2 = BSS start
  300. * r3 = BSS end
  301. * r4 = kernel execution address
  302. * r5 = appended dtb size (0 if not present)
  303. * r7 = architecture ID
  304. * r8 = atags pointer
  305. * r11 = GOT start
  306. * r12 = GOT end
  307. * sp = stack pointer
  308. */
  309. orrs r1, r0, r5
  310. beq not_relocated
  311. add r11, r11, r0
  312. add r12, r12, r0
  313. #ifndef CONFIG_ZBOOT_ROM
  314. /*
  315. * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
  316. * we need to fix up pointers into the BSS region.
  317. * Note that the stack pointer has already been fixed up.
  318. */
  319. add r2, r2, r0
  320. add r3, r3, r0
  321. /*
  322. * Relocate all entries in the GOT table.
  323. * Bump bss entries to _edata + dtb size
  324. */
  325. 1: ldr r1, [r11, #0] @ relocate entries in the GOT
  326. add r1, r1, r0 @ This fixes up C references
  327. cmp r1, r2 @ if entry >= bss_start &&
  328. cmphs r3, r1 @ bss_end > entry
  329. addhi r1, r1, r5 @ entry += dtb size
  330. str r1, [r11], #4 @ next entry
  331. cmp r11, r12
  332. blo 1b
  333. /* bump our bss pointers too */
  334. add r2, r2, r5
  335. add r3, r3, r5
  336. #else
  337. /*
  338. * Relocate entries in the GOT table. We only relocate
  339. * the entries that are outside the (relocated) BSS region.
  340. */
  341. 1: ldr r1, [r11, #0] @ relocate entries in the GOT
  342. cmp r1, r2 @ entry < bss_start ||
  343. cmphs r3, r1 @ _end < entry
  344. addlo r1, r1, r0 @ table. This fixes up the
  345. str r1, [r11], #4 @ C references.
  346. cmp r11, r12
  347. blo 1b
  348. #endif
  349. not_relocated: mov r0, #0
  350. 1: str r0, [r2], #4 @ clear bss
  351. str r0, [r2], #4
  352. str r0, [r2], #4
  353. str r0, [r2], #4
  354. cmp r2, r3
  355. blo 1b
  356. /*
  357. * The C runtime environment should now be setup sufficiently.
  358. * Set up some pointers, and start decompressing.
  359. * r4 = kernel execution address
  360. * r7 = architecture ID
  361. * r8 = atags pointer
  362. */
  363. mov r0, r4
  364. mov r1, sp @ malloc space above stack
  365. add r2, sp, #0x10000 @ 64k max
  366. mov r3, r7
  367. bl decompress_kernel
  368. bl cache_clean_flush
  369. bl cache_off
  370. mov r0, #0 @ must be zero
  371. mov r1, r7 @ restore architecture number
  372. mov r2, r8 @ restore atags pointer
  373. ARM( mov pc, r4 ) @ call kernel
  374. THUMB( bx r4 ) @ entry point is always ARM
  375. .align 2
  376. .type LC0, #object
  377. LC0: .word LC0 @ r1
  378. .word __bss_start @ r2
  379. .word _end @ r3
  380. .word _edata @ r6
  381. .word input_data_end - 4 @ r10 (inflated size location)
  382. .word _got_start @ r11
  383. .word _got_end @ ip
  384. .word .L_user_stack_end @ sp
  385. .size LC0, . - LC0
  386. #ifdef CONFIG_ARCH_RPC
  387. .globl params
  388. params: ldr r0, =0x10000100 @ params_phys for RPC
  389. mov pc, lr
  390. .ltorg
  391. .align
  392. #endif
  393. /*
  394. * Turn on the cache. We need to setup some page tables so that we
  395. * can have both the I and D caches on.
  396. *
  397. * We place the page tables 16k down from the kernel execution address,
  398. * and we hope that nothing else is using it. If we're using it, we
  399. * will go pop!
  400. *
  401. * On entry,
  402. * r4 = kernel execution address
  403. * r7 = architecture number
  404. * r8 = atags pointer
  405. * On exit,
  406. * r0, r1, r2, r3, r9, r10, r12 corrupted
  407. * This routine must preserve:
  408. * r4, r7, r8
  409. */
  410. .align 5
  411. cache_on: mov r3, #8 @ cache_on function
  412. b call_cache_fn
  413. /*
  414. * Initialize the highest priority protection region, PR7
  415. * to cover all 32bit address and cacheable and bufferable.
  416. */
  417. __armv4_mpu_cache_on:
  418. mov r0, #0x3f @ 4G, the whole
  419. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  420. mcr p15, 0, r0, c6, c7, 1
  421. mov r0, #0x80 @ PR7
  422. mcr p15, 0, r0, c2, c0, 0 @ D-cache on
  423. mcr p15, 0, r0, c2, c0, 1 @ I-cache on
  424. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  425. mov r0, #0xc000
  426. mcr p15, 0, r0, c5, c0, 1 @ I-access permission
  427. mcr p15, 0, r0, c5, c0, 0 @ D-access permission
  428. mov r0, #0
  429. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  430. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  431. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  432. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  433. @ ...I .... ..D. WC.M
  434. orr r0, r0, #0x002d @ .... .... ..1. 11.1
  435. orr r0, r0, #0x1000 @ ...1 .... .... ....
  436. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  437. mov r0, #0
  438. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  439. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  440. mov pc, lr
  441. __armv3_mpu_cache_on:
  442. mov r0, #0x3f @ 4G, the whole
  443. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  444. mov r0, #0x80 @ PR7
  445. mcr p15, 0, r0, c2, c0, 0 @ cache on
  446. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  447. mov r0, #0xc000
  448. mcr p15, 0, r0, c5, c0, 0 @ access permission
  449. mov r0, #0
  450. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  451. /*
  452. * ?? ARMv3 MMU does not allow reading the control register,
  453. * does this really work on ARMv3 MPU?
  454. */
  455. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  456. @ .... .... .... WC.M
  457. orr r0, r0, #0x000d @ .... .... .... 11.1
  458. /* ?? this overwrites the value constructed above? */
  459. mov r0, #0
  460. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  461. /* ?? invalidate for the second time? */
  462. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  463. mov pc, lr
  464. __setup_mmu: sub r3, r4, #16384 @ Page directory size
  465. bic r3, r3, #0xff @ Align the pointer
  466. bic r3, r3, #0x3f00
  467. /*
  468. * Initialise the page tables, turning on the cacheable and bufferable
  469. * bits for the RAM area only.
  470. */
  471. mov r0, r3
  472. mov r9, r0, lsr #18
  473. mov r9, r9, lsl #18 @ start of RAM
  474. add r10, r9, #0x10000000 @ a reasonable RAM size
  475. mov r1, #0x12
  476. orr r1, r1, #3 << 10
  477. add r2, r3, #16384
  478. 1: cmp r1, r9 @ if virt > start of RAM
  479. #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
  480. orrhs r1, r1, #0x08 @ set cacheable
  481. #else
  482. orrhs r1, r1, #0x0c @ set cacheable, bufferable
  483. #endif
  484. cmp r1, r10 @ if virt > end of RAM
  485. bichs r1, r1, #0x0c @ clear cacheable, bufferable
  486. str r1, [r0], #4 @ 1:1 mapping
  487. add r1, r1, #1048576
  488. teq r0, r2
  489. bne 1b
  490. /*
  491. * If ever we are running from Flash, then we surely want the cache
  492. * to be enabled also for our execution instance... We map 2MB of it
  493. * so there is no map overlap problem for up to 1 MB compressed kernel.
  494. * If the execution is in RAM then we would only be duplicating the above.
  495. */
  496. mov r1, #0x1e
  497. orr r1, r1, #3 << 10
  498. mov r2, pc
  499. mov r2, r2, lsr #20
  500. orr r1, r1, r2, lsl #20
  501. add r0, r3, r2, lsl #2
  502. str r1, [r0], #4
  503. add r1, r1, #1048576
  504. str r1, [r0]
  505. mov pc, lr
  506. ENDPROC(__setup_mmu)
  507. __arm926ejs_mmu_cache_on:
  508. #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
  509. mov r0, #4 @ put dcache in WT mode
  510. mcr p15, 7, r0, c15, c0, 0
  511. #endif
  512. __armv4_mmu_cache_on:
  513. mov r12, lr
  514. #ifdef CONFIG_MMU
  515. bl __setup_mmu
  516. mov r0, #0
  517. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  518. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  519. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  520. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  521. orr r0, r0, #0x0030
  522. #ifdef CONFIG_CPU_ENDIAN_BE8
  523. orr r0, r0, #1 << 25 @ big-endian page tables
  524. #endif
  525. bl __common_mmu_cache_on
  526. mov r0, #0
  527. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  528. #endif
  529. mov pc, r12
  530. __armv7_mmu_cache_on:
  531. mov r12, lr
  532. #ifdef CONFIG_MMU
  533. mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
  534. tst r11, #0xf @ VMSA
  535. blne __setup_mmu
  536. mov r0, #0
  537. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  538. tst r11, #0xf @ VMSA
  539. mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  540. #endif
  541. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  542. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  543. orr r0, r0, #0x003c @ write buffer
  544. #ifdef CONFIG_MMU
  545. #ifdef CONFIG_CPU_ENDIAN_BE8
  546. orr r0, r0, #1 << 25 @ big-endian page tables
  547. #endif
  548. orrne r0, r0, #1 @ MMU enabled
  549. movne r1, #-1
  550. mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
  551. mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
  552. #endif
  553. mcr p15, 0, r0, c1, c0, 0 @ load control register
  554. mrc p15, 0, r0, c1, c0, 0 @ and read it back
  555. mov r0, #0
  556. mcr p15, 0, r0, c7, c5, 4 @ ISB
  557. mov pc, r12
  558. __fa526_cache_on:
  559. mov r12, lr
  560. bl __setup_mmu
  561. mov r0, #0
  562. mcr p15, 0, r0, c7, c7, 0 @ Invalidate whole cache
  563. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  564. mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
  565. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  566. orr r0, r0, #0x1000 @ I-cache enable
  567. bl __common_mmu_cache_on
  568. mov r0, #0
  569. mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
  570. mov pc, r12
  571. __arm6_mmu_cache_on:
  572. mov r12, lr
  573. bl __setup_mmu
  574. mov r0, #0
  575. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  576. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  577. mov r0, #0x30
  578. bl __common_mmu_cache_on
  579. mov r0, #0
  580. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  581. mov pc, r12
  582. __common_mmu_cache_on:
  583. #ifndef CONFIG_THUMB2_KERNEL
  584. #ifndef DEBUG
  585. orr r0, r0, #0x000d @ Write buffer, mmu
  586. #endif
  587. mov r1, #-1
  588. mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
  589. mcr p15, 0, r1, c3, c0, 0 @ load domain access control
  590. b 1f
  591. .align 5 @ cache line aligned
  592. 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
  593. mrc p15, 0, r0, c1, c0, 0 @ and read it back to
  594. sub pc, lr, r0, lsr #32 @ properly flush pipeline
  595. #endif
  596. #define PROC_ENTRY_SIZE (4*5)
  597. /*
  598. * Here follow the relocatable cache support functions for the
  599. * various processors. This is a generic hook for locating an
  600. * entry and jumping to an instruction at the specified offset
  601. * from the start of the block. Please note this is all position
  602. * independent code.
  603. *
  604. * r1 = corrupted
  605. * r2 = corrupted
  606. * r3 = block offset
  607. * r9 = corrupted
  608. * r12 = corrupted
  609. */
  610. call_cache_fn: adr r12, proc_types
  611. #ifdef CONFIG_CPU_CP15
  612. mrc p15, 0, r9, c0, c0 @ get processor ID
  613. #else
  614. ldr r9, =CONFIG_PROCESSOR_ID
  615. #endif
  616. 1: ldr r1, [r12, #0] @ get value
  617. ldr r2, [r12, #4] @ get mask
  618. eor r1, r1, r9 @ (real ^ match)
  619. tst r1, r2 @ & mask
  620. ARM( addeq pc, r12, r3 ) @ call cache function
  621. THUMB( addeq r12, r3 )
  622. THUMB( moveq pc, r12 ) @ call cache function
  623. add r12, r12, #PROC_ENTRY_SIZE
  624. b 1b
  625. /*
  626. * Table for cache operations. This is basically:
  627. * - CPU ID match
  628. * - CPU ID mask
  629. * - 'cache on' method instruction
  630. * - 'cache off' method instruction
  631. * - 'cache flush' method instruction
  632. *
  633. * We match an entry using: ((real_id ^ match) & mask) == 0
  634. *
  635. * Writethrough caches generally only need 'on' and 'off'
  636. * methods. Writeback caches _must_ have the flush method
  637. * defined.
  638. */
  639. .align 2
  640. .type proc_types,#object
  641. proc_types:
  642. .word 0x41560600 @ ARM6/610
  643. .word 0xffffffe0
  644. W(b) __arm6_mmu_cache_off @ works, but slow
  645. W(b) __arm6_mmu_cache_off
  646. mov pc, lr
  647. THUMB( nop )
  648. @ b __arm6_mmu_cache_on @ untested
  649. @ b __arm6_mmu_cache_off
  650. @ b __armv3_mmu_cache_flush
  651. .word 0x00000000 @ old ARM ID
  652. .word 0x0000f000
  653. mov pc, lr
  654. THUMB( nop )
  655. mov pc, lr
  656. THUMB( nop )
  657. mov pc, lr
  658. THUMB( nop )
  659. .word 0x41007000 @ ARM7/710
  660. .word 0xfff8fe00
  661. W(b) __arm7_mmu_cache_off
  662. W(b) __arm7_mmu_cache_off
  663. mov pc, lr
  664. THUMB( nop )
  665. .word 0x41807200 @ ARM720T (writethrough)
  666. .word 0xffffff00
  667. W(b) __armv4_mmu_cache_on
  668. W(b) __armv4_mmu_cache_off
  669. mov pc, lr
  670. THUMB( nop )
  671. .word 0x41007400 @ ARM74x
  672. .word 0xff00ff00
  673. W(b) __armv3_mpu_cache_on
  674. W(b) __armv3_mpu_cache_off
  675. W(b) __armv3_mpu_cache_flush
  676. .word 0x41009400 @ ARM94x
  677. .word 0xff00ff00
  678. W(b) __armv4_mpu_cache_on
  679. W(b) __armv4_mpu_cache_off
  680. W(b) __armv4_mpu_cache_flush
  681. .word 0x41069260 @ ARM926EJ-S (v5TEJ)
  682. .word 0xff0ffff0
  683. W(b) __arm926ejs_mmu_cache_on
  684. W(b) __armv4_mmu_cache_off
  685. W(b) __armv5tej_mmu_cache_flush
  686. .word 0x00007000 @ ARM7 IDs
  687. .word 0x0000f000
  688. mov pc, lr
  689. THUMB( nop )
  690. mov pc, lr
  691. THUMB( nop )
  692. mov pc, lr
  693. THUMB( nop )
  694. @ Everything from here on will be the new ID system.
  695. .word 0x4401a100 @ sa110 / sa1100
  696. .word 0xffffffe0
  697. W(b) __armv4_mmu_cache_on
  698. W(b) __armv4_mmu_cache_off
  699. W(b) __armv4_mmu_cache_flush
  700. .word 0x6901b110 @ sa1110
  701. .word 0xfffffff0
  702. W(b) __armv4_mmu_cache_on
  703. W(b) __armv4_mmu_cache_off
  704. W(b) __armv4_mmu_cache_flush
  705. .word 0x56056900
  706. .word 0xffffff00 @ PXA9xx
  707. W(b) __armv4_mmu_cache_on
  708. W(b) __armv4_mmu_cache_off
  709. W(b) __armv4_mmu_cache_flush
  710. .word 0x56158000 @ PXA168
  711. .word 0xfffff000
  712. W(b) __armv4_mmu_cache_on
  713. W(b) __armv4_mmu_cache_off
  714. W(b) __armv5tej_mmu_cache_flush
  715. .word 0x56050000 @ Feroceon
  716. .word 0xff0f0000
  717. W(b) __armv4_mmu_cache_on
  718. W(b) __armv4_mmu_cache_off
  719. W(b) __armv5tej_mmu_cache_flush
  720. #ifdef CONFIG_CPU_FEROCEON_OLD_ID
  721. /* this conflicts with the standard ARMv5TE entry */
  722. .long 0x41009260 @ Old Feroceon
  723. .long 0xff00fff0
  724. b __armv4_mmu_cache_on
  725. b __armv4_mmu_cache_off
  726. b __armv5tej_mmu_cache_flush
  727. #endif
  728. .word 0x66015261 @ FA526
  729. .word 0xff01fff1
  730. W(b) __fa526_cache_on
  731. W(b) __armv4_mmu_cache_off
  732. W(b) __fa526_cache_flush
  733. @ These match on the architecture ID
  734. .word 0x00020000 @ ARMv4T
  735. .word 0x000f0000
  736. W(b) __armv4_mmu_cache_on
  737. W(b) __armv4_mmu_cache_off
  738. W(b) __armv4_mmu_cache_flush
  739. .word 0x00050000 @ ARMv5TE
  740. .word 0x000f0000
  741. W(b) __armv4_mmu_cache_on
  742. W(b) __armv4_mmu_cache_off
  743. W(b) __armv4_mmu_cache_flush
  744. .word 0x00060000 @ ARMv5TEJ
  745. .word 0x000f0000
  746. W(b) __armv4_mmu_cache_on
  747. W(b) __armv4_mmu_cache_off
  748. W(b) __armv5tej_mmu_cache_flush
  749. .word 0x0007b000 @ ARMv6
  750. .word 0x000ff000
  751. W(b) __armv4_mmu_cache_on
  752. W(b) __armv4_mmu_cache_off
  753. W(b) __armv6_mmu_cache_flush
  754. .word 0x000f0000 @ new CPU Id
  755. .word 0x000f0000
  756. W(b) __armv7_mmu_cache_on
  757. W(b) __armv7_mmu_cache_off
  758. W(b) __armv7_mmu_cache_flush
  759. .word 0 @ unrecognised type
  760. .word 0
  761. mov pc, lr
  762. THUMB( nop )
  763. mov pc, lr
  764. THUMB( nop )
  765. mov pc, lr
  766. THUMB( nop )
  767. .size proc_types, . - proc_types
  768. /*
  769. * If you get a "non-constant expression in ".if" statement"
  770. * error from the assembler on this line, check that you have
  771. * not accidentally written a "b" instruction where you should
  772. * have written W(b).
  773. */
  774. .if (. - proc_types) % PROC_ENTRY_SIZE != 0
  775. .error "The size of one or more proc_types entries is wrong."
  776. .endif
  777. /*
  778. * Turn off the Cache and MMU. ARMv3 does not support
  779. * reading the control register, but ARMv4 does.
  780. *
  781. * On exit,
  782. * r0, r1, r2, r3, r9, r12 corrupted
  783. * This routine must preserve:
  784. * r4, r7, r8
  785. */
  786. .align 5
  787. cache_off: mov r3, #12 @ cache_off function
  788. b call_cache_fn
  789. __armv4_mpu_cache_off:
  790. mrc p15, 0, r0, c1, c0
  791. bic r0, r0, #0x000d
  792. mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
  793. mov r0, #0
  794. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  795. mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
  796. mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
  797. mov pc, lr
  798. __armv3_mpu_cache_off:
  799. mrc p15, 0, r0, c1, c0
  800. bic r0, r0, #0x000d
  801. mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
  802. mov r0, #0
  803. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  804. mov pc, lr
  805. __armv4_mmu_cache_off:
  806. #ifdef CONFIG_MMU
  807. mrc p15, 0, r0, c1, c0
  808. bic r0, r0, #0x000d
  809. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  810. mov r0, #0
  811. mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
  812. mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
  813. #endif
  814. mov pc, lr
  815. __armv7_mmu_cache_off:
  816. mrc p15, 0, r0, c1, c0
  817. #ifdef CONFIG_MMU
  818. bic r0, r0, #0x000d
  819. #else
  820. bic r0, r0, #0x000c
  821. #endif
  822. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  823. mov r12, lr
  824. bl __armv7_mmu_cache_flush
  825. mov r0, #0
  826. #ifdef CONFIG_MMU
  827. mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
  828. #endif
  829. mcr p15, 0, r0, c7, c5, 6 @ invalidate BTC
  830. mcr p15, 0, r0, c7, c10, 4 @ DSB
  831. mcr p15, 0, r0, c7, c5, 4 @ ISB
  832. mov pc, r12
  833. __arm6_mmu_cache_off:
  834. mov r0, #0x00000030 @ ARM6 control reg.
  835. b __armv3_mmu_cache_off
  836. __arm7_mmu_cache_off:
  837. mov r0, #0x00000070 @ ARM7 control reg.
  838. b __armv3_mmu_cache_off
  839. __armv3_mmu_cache_off:
  840. mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
  841. mov r0, #0
  842. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  843. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  844. mov pc, lr
  845. /*
  846. * Clean and flush the cache to maintain consistency.
  847. *
  848. * On exit,
  849. * r1, r2, r3, r9, r10, r11, r12 corrupted
  850. * This routine must preserve:
  851. * r4, r6, r7, r8
  852. */
  853. .align 5
  854. cache_clean_flush:
  855. mov r3, #16
  856. b call_cache_fn
  857. __armv4_mpu_cache_flush:
  858. mov r2, #1
  859. mov r3, #0
  860. mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
  861. mov r1, #7 << 5 @ 8 segments
  862. 1: orr r3, r1, #63 << 26 @ 64 entries
  863. 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
  864. subs r3, r3, #1 << 26
  865. bcs 2b @ entries 63 to 0
  866. subs r1, r1, #1 << 5
  867. bcs 1b @ segments 7 to 0
  868. teq r2, #0
  869. mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
  870. mcr p15, 0, ip, c7, c10, 4 @ drain WB
  871. mov pc, lr
  872. __fa526_cache_flush:
  873. mov r1, #0
  874. mcr p15, 0, r1, c7, c14, 0 @ clean and invalidate D cache
  875. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  876. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  877. mov pc, lr
  878. __armv6_mmu_cache_flush:
  879. mov r1, #0
  880. mcr p15, 0, r1, c7, c14, 0 @ clean+invalidate D
  881. mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
  882. mcr p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
  883. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  884. mov pc, lr
  885. __armv7_mmu_cache_flush:
  886. mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
  887. tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
  888. mov r10, #0
  889. beq hierarchical
  890. mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
  891. b iflush
  892. hierarchical:
  893. mcr p15, 0, r10, c7, c10, 5 @ DMB
  894. stmfd sp!, {r0-r7, r9-r11}
  895. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  896. ands r3, r0, #0x7000000 @ extract loc from clidr
  897. mov r3, r3, lsr #23 @ left align loc bit field
  898. beq finished @ if loc is 0, then no need to clean
  899. mov r10, #0 @ start clean at cache level 0
  900. loop1:
  901. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  902. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  903. and r1, r1, #7 @ mask of the bits for current cache only
  904. cmp r1, #2 @ see what cache we have at this level
  905. blt skip @ skip if no cache, or just i-cache
  906. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  907. mcr p15, 0, r10, c7, c5, 4 @ isb to sych the new cssr&csidr
  908. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  909. and r2, r1, #7 @ extract the length of the cache lines
  910. add r2, r2, #4 @ add 4 (line length offset)
  911. ldr r4, =0x3ff
  912. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  913. clz r5, r4 @ find bit position of way size increment
  914. ldr r7, =0x7fff
  915. ands r7, r7, r1, lsr #13 @ extract max number of the index size
  916. loop2:
  917. mov r9, r4 @ create working copy of max way size
  918. loop3:
  919. ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
  920. ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
  921. THUMB( lsl r6, r9, r5 )
  922. THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
  923. THUMB( lsl r6, r7, r2 )
  924. THUMB( orr r11, r11, r6 ) @ factor index number into r11
  925. mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
  926. subs r9, r9, #1 @ decrement the way
  927. bge loop3
  928. subs r7, r7, #1 @ decrement the index
  929. bge loop2
  930. skip:
  931. add r10, r10, #2 @ increment cache number
  932. cmp r3, r10
  933. bgt loop1
  934. finished:
  935. ldmfd sp!, {r0-r7, r9-r11}
  936. mov r10, #0 @ swith back to cache level 0
  937. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  938. iflush:
  939. mcr p15, 0, r10, c7, c10, 4 @ DSB
  940. mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
  941. mcr p15, 0, r10, c7, c10, 4 @ DSB
  942. mcr p15, 0, r10, c7, c5, 4 @ ISB
  943. mov pc, lr
  944. __armv5tej_mmu_cache_flush:
  945. 1: mrc p15, 0, r15, c7, c14, 3 @ test,clean,invalidate D cache
  946. bne 1b
  947. mcr p15, 0, r0, c7, c5, 0 @ flush I cache
  948. mcr p15, 0, r0, c7, c10, 4 @ drain WB
  949. mov pc, lr
  950. __armv4_mmu_cache_flush:
  951. mov r2, #64*1024 @ default: 32K dcache size (*2)
  952. mov r11, #32 @ default: 32 byte line size
  953. mrc p15, 0, r3, c0, c0, 1 @ read cache type
  954. teq r3, r9 @ cache ID register present?
  955. beq no_cache_id
  956. mov r1, r3, lsr #18
  957. and r1, r1, #7
  958. mov r2, #1024
  959. mov r2, r2, lsl r1 @ base dcache size *2
  960. tst r3, #1 << 14 @ test M bit
  961. addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
  962. mov r3, r3, lsr #12
  963. and r3, r3, #3
  964. mov r11, #8
  965. mov r11, r11, lsl r3 @ cache line size in bytes
  966. no_cache_id:
  967. mov r1, pc
  968. bic r1, r1, #63 @ align to longest cache line
  969. add r2, r1, r2
  970. 1:
  971. ARM( ldr r3, [r1], r11 ) @ s/w flush D cache
  972. THUMB( ldr r3, [r1] ) @ s/w flush D cache
  973. THUMB( add r1, r1, r11 )
  974. teq r1, r2
  975. bne 1b
  976. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  977. mcr p15, 0, r1, c7, c6, 0 @ flush D cache
  978. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  979. mov pc, lr
  980. __armv3_mmu_cache_flush:
  981. __armv3_mpu_cache_flush:
  982. mov r1, #0
  983. mcr p15, 0, r1, c7, c0, 0 @ invalidate whole cache v3
  984. mov pc, lr
  985. /*
  986. * Various debugging routines for printing hex characters and
  987. * memory, which again must be relocatable.
  988. */
  989. #ifdef DEBUG
  990. .align 2
  991. .type phexbuf,#object
  992. phexbuf: .space 12
  993. .size phexbuf, . - phexbuf
  994. @ phex corrupts {r0, r1, r2, r3}
  995. phex: adr r3, phexbuf
  996. mov r2, #0
  997. strb r2, [r3, r1]
  998. 1: subs r1, r1, #1
  999. movmi r0, r3
  1000. bmi puts
  1001. and r2, r0, #15
  1002. mov r0, r0, lsr #4
  1003. cmp r2, #10
  1004. addge r2, r2, #7
  1005. add r2, r2, #'0'
  1006. strb r2, [r3, r1]
  1007. b 1b
  1008. @ puts corrupts {r0, r1, r2, r3}
  1009. puts: loadsp r3, r1
  1010. 1: ldrb r2, [r0], #1
  1011. teq r2, #0
  1012. moveq pc, lr
  1013. 2: writeb r2, r3
  1014. mov r1, #0x00020000
  1015. 3: subs r1, r1, #1
  1016. bne 3b
  1017. teq r2, #'\n'
  1018. moveq r2, #'\r'
  1019. beq 2b
  1020. teq r0, #0
  1021. bne 1b
  1022. mov pc, lr
  1023. @ putc corrupts {r0, r1, r2, r3}
  1024. putc:
  1025. mov r2, r0
  1026. mov r0, #0
  1027. loadsp r3, r1
  1028. b 2b
  1029. @ memdump corrupts {r0, r1, r2, r3, r10, r11, r12, lr}
  1030. memdump: mov r12, r0
  1031. mov r10, lr
  1032. mov r11, #0
  1033. 2: mov r0, r11, lsl #2
  1034. add r0, r0, r12
  1035. mov r1, #8
  1036. bl phex
  1037. mov r0, #':'
  1038. bl putc
  1039. 1: mov r0, #' '
  1040. bl putc
  1041. ldr r0, [r12, r11, lsl #2]
  1042. mov r1, #8
  1043. bl phex
  1044. and r0, r11, #7
  1045. teq r0, #3
  1046. moveq r0, #' '
  1047. bleq putc
  1048. and r0, r11, #7
  1049. add r11, r11, #1
  1050. teq r0, #7
  1051. bne 1b
  1052. mov r0, #'\n'
  1053. bl putc
  1054. cmp r11, #64
  1055. blt 2b
  1056. mov pc, r10
  1057. #endif
  1058. .ltorg
  1059. reloc_code_end:
  1060. .align
  1061. .section ".stack", "aw", %nobits
  1062. .L_user_stack: .space 4096
  1063. .L_user_stack_end: