head.S 26 KB

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