head.S 26 KB

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