head.S 26 KB

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