head.S 26 KB

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