head.S 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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, #32] )
  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_end @ 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. * On exit,
  310. * r0, r1, r2, r3, r9, r10, r12 corrupted
  311. * This routine must preserve:
  312. * r4, r5, r6, r7, r8
  313. */
  314. .align 5
  315. cache_on: mov r3, #8 @ cache_on function
  316. b call_cache_fn
  317. /*
  318. * Initialize the highest priority protection region, PR7
  319. * to cover all 32bit address and cacheable and bufferable.
  320. */
  321. __armv4_mpu_cache_on:
  322. mov r0, #0x3f @ 4G, the whole
  323. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  324. mcr p15, 0, r0, c6, c7, 1
  325. mov r0, #0x80 @ PR7
  326. mcr p15, 0, r0, c2, c0, 0 @ D-cache on
  327. mcr p15, 0, r0, c2, c0, 1 @ I-cache on
  328. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  329. mov r0, #0xc000
  330. mcr p15, 0, r0, c5, c0, 1 @ I-access permission
  331. mcr p15, 0, r0, c5, c0, 0 @ D-access permission
  332. mov r0, #0
  333. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  334. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  335. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  336. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  337. @ ...I .... ..D. WC.M
  338. orr r0, r0, #0x002d @ .... .... ..1. 11.1
  339. orr r0, r0, #0x1000 @ ...1 .... .... ....
  340. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  341. mov r0, #0
  342. mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
  343. mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
  344. mov pc, lr
  345. __armv3_mpu_cache_on:
  346. mov r0, #0x3f @ 4G, the whole
  347. mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
  348. mov r0, #0x80 @ PR7
  349. mcr p15, 0, r0, c2, c0, 0 @ cache on
  350. mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
  351. mov r0, #0xc000
  352. mcr p15, 0, r0, c5, c0, 0 @ access permission
  353. mov r0, #0
  354. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  355. /*
  356. * ?? ARMv3 MMU does not allow reading the control register,
  357. * does this really work on ARMv3 MPU?
  358. */
  359. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  360. @ .... .... .... WC.M
  361. orr r0, r0, #0x000d @ .... .... .... 11.1
  362. /* ?? this overwrites the value constructed above? */
  363. mov r0, #0
  364. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  365. /* ?? invalidate for the second time? */
  366. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  367. mov pc, lr
  368. __setup_mmu: sub r3, r4, #16384 @ Page directory size
  369. bic r3, r3, #0xff @ Align the pointer
  370. bic r3, r3, #0x3f00
  371. /*
  372. * Initialise the page tables, turning on the cacheable and bufferable
  373. * bits for the RAM area only.
  374. */
  375. mov r0, r3
  376. mov r9, r0, lsr #18
  377. mov r9, r9, lsl #18 @ start of RAM
  378. add r10, r9, #0x10000000 @ a reasonable RAM size
  379. mov r1, #0x12
  380. orr r1, r1, #3 << 10
  381. add r2, r3, #16384
  382. 1: cmp r1, r9 @ if virt > start of RAM
  383. orrhs r1, r1, #0x0c @ set cacheable, bufferable
  384. cmp r1, r10 @ if virt > end of RAM
  385. bichs r1, r1, #0x0c @ clear cacheable, bufferable
  386. str r1, [r0], #4 @ 1:1 mapping
  387. add r1, r1, #1048576
  388. teq r0, r2
  389. bne 1b
  390. /*
  391. * If ever we are running from Flash, then we surely want the cache
  392. * to be enabled also for our execution instance... We map 2MB of it
  393. * so there is no map overlap problem for up to 1 MB compressed kernel.
  394. * If the execution is in RAM then we would only be duplicating the above.
  395. */
  396. mov r1, #0x1e
  397. orr r1, r1, #3 << 10
  398. mov r2, pc, lsr #20
  399. orr r1, r1, r2, lsl #20
  400. add r0, r3, r2, lsl #2
  401. str r1, [r0], #4
  402. add r1, r1, #1048576
  403. str r1, [r0]
  404. mov pc, lr
  405. ENDPROC(__setup_mmu)
  406. __armv4_mmu_cache_on:
  407. mov r12, lr
  408. #ifdef CONFIG_MMU
  409. bl __setup_mmu
  410. mov r0, #0
  411. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  412. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  413. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  414. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  415. orr r0, r0, #0x0030
  416. #ifdef CONFIG_CPU_ENDIAN_BE8
  417. orr r0, r0, #1 << 25 @ big-endian page tables
  418. #endif
  419. bl __common_mmu_cache_on
  420. mov r0, #0
  421. mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  422. #endif
  423. mov pc, r12
  424. __armv7_mmu_cache_on:
  425. mov r12, lr
  426. #ifdef CONFIG_MMU
  427. mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
  428. tst r11, #0xf @ VMSA
  429. blne __setup_mmu
  430. mov r0, #0
  431. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  432. tst r11, #0xf @ VMSA
  433. mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
  434. #endif
  435. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  436. orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
  437. orr r0, r0, #0x003c @ write buffer
  438. #ifdef CONFIG_MMU
  439. #ifdef CONFIG_CPU_ENDIAN_BE8
  440. orr r0, r0, #1 << 25 @ big-endian page tables
  441. #endif
  442. orrne r0, r0, #1 @ MMU enabled
  443. movne r1, #-1
  444. mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
  445. mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
  446. #endif
  447. mcr p15, 0, r0, c1, c0, 0 @ load control register
  448. mrc p15, 0, r0, c1, c0, 0 @ and read it back
  449. mov r0, #0
  450. mcr p15, 0, r0, c7, c5, 4 @ ISB
  451. mov pc, r12
  452. __fa526_cache_on:
  453. mov r12, lr
  454. bl __setup_mmu
  455. mov r0, #0
  456. mcr p15, 0, r0, c7, c7, 0 @ Invalidate whole cache
  457. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  458. mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
  459. mrc p15, 0, r0, c1, c0, 0 @ read control reg
  460. orr r0, r0, #0x1000 @ I-cache enable
  461. bl __common_mmu_cache_on
  462. mov r0, #0
  463. mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
  464. mov pc, r12
  465. __arm6_mmu_cache_on:
  466. mov r12, lr
  467. bl __setup_mmu
  468. mov r0, #0
  469. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  470. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  471. mov r0, #0x30
  472. bl __common_mmu_cache_on
  473. mov r0, #0
  474. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  475. mov pc, r12
  476. __common_mmu_cache_on:
  477. #ifndef CONFIG_THUMB2_KERNEL
  478. #ifndef DEBUG
  479. orr r0, r0, #0x000d @ Write buffer, mmu
  480. #endif
  481. mov r1, #-1
  482. mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
  483. mcr p15, 0, r1, c3, c0, 0 @ load domain access control
  484. b 1f
  485. .align 5 @ cache line aligned
  486. 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
  487. mrc p15, 0, r0, c1, c0, 0 @ and read it back to
  488. sub pc, lr, r0, lsr #32 @ properly flush pipeline
  489. #endif
  490. /*
  491. * All code following this line is relocatable. It is relocated by
  492. * the above code to the end of the decompressed kernel image and
  493. * executed there. During this time, we have no stacks.
  494. *
  495. * r0 = decompressed kernel length
  496. * r1-r3 = unused
  497. * r4 = kernel execution address
  498. * r5 = decompressed kernel start
  499. * r7 = architecture ID
  500. * r8 = atags pointer
  501. * r9-r12,r14 = corrupted
  502. */
  503. .align 5
  504. reloc_start: add r9, r5, r0
  505. sub r9, r9, #128 @ do not copy the stack
  506. debug_reloc_start
  507. mov r1, r4
  508. 1:
  509. .rept 4
  510. ldmia r5!, {r0, r2, r3, r10 - r12, r14} @ relocate kernel
  511. stmia r1!, {r0, r2, r3, r10 - r12, r14}
  512. .endr
  513. cmp r5, r9
  514. blo 1b
  515. mov sp, r1
  516. add sp, sp, #128 @ relocate the stack
  517. debug_reloc_end
  518. call_kernel: bl cache_clean_flush
  519. bl cache_off
  520. mov r0, #0 @ must be zero
  521. mov r1, r7 @ restore architecture number
  522. mov r2, r8 @ restore atags pointer
  523. mov pc, r4 @ call kernel
  524. /*
  525. * Here follow the relocatable cache support functions for the
  526. * various processors. This is a generic hook for locating an
  527. * entry and jumping to an instruction at the specified offset
  528. * from the start of the block. Please note this is all position
  529. * independent code.
  530. *
  531. * r1 = corrupted
  532. * r2 = corrupted
  533. * r3 = block offset
  534. * r9 = corrupted
  535. * r12 = corrupted
  536. */
  537. call_cache_fn: adr r12, proc_types
  538. #ifdef CONFIG_CPU_CP15
  539. mrc p15, 0, r9, c0, c0 @ get processor ID
  540. #else
  541. ldr r9, =CONFIG_PROCESSOR_ID
  542. #endif
  543. 1: ldr r1, [r12, #0] @ get value
  544. ldr r2, [r12, #4] @ get mask
  545. eor r1, r1, r9 @ (real ^ match)
  546. tst r1, r2 @ & mask
  547. ARM( addeq pc, r12, r3 ) @ call cache function
  548. THUMB( addeq r12, r3 )
  549. THUMB( moveq pc, r12 ) @ call cache function
  550. add r12, r12, #4*5
  551. b 1b
  552. /*
  553. * Table for cache operations. This is basically:
  554. * - CPU ID match
  555. * - CPU ID mask
  556. * - 'cache on' method instruction
  557. * - 'cache off' method instruction
  558. * - 'cache flush' method instruction
  559. *
  560. * We match an entry using: ((real_id ^ match) & mask) == 0
  561. *
  562. * Writethrough caches generally only need 'on' and 'off'
  563. * methods. Writeback caches _must_ have the flush method
  564. * defined.
  565. */
  566. .align 2
  567. .type proc_types,#object
  568. proc_types:
  569. .word 0x41560600 @ ARM6/610
  570. .word 0xffffffe0
  571. W(b) __arm6_mmu_cache_off @ works, but slow
  572. W(b) __arm6_mmu_cache_off
  573. mov pc, lr
  574. THUMB( nop )
  575. @ b __arm6_mmu_cache_on @ untested
  576. @ b __arm6_mmu_cache_off
  577. @ b __armv3_mmu_cache_flush
  578. .word 0x00000000 @ old ARM ID
  579. .word 0x0000f000
  580. mov pc, lr
  581. THUMB( nop )
  582. mov pc, lr
  583. THUMB( nop )
  584. mov pc, lr
  585. THUMB( nop )
  586. .word 0x41007000 @ ARM7/710
  587. .word 0xfff8fe00
  588. W(b) __arm7_mmu_cache_off
  589. W(b) __arm7_mmu_cache_off
  590. mov pc, lr
  591. THUMB( nop )
  592. .word 0x41807200 @ ARM720T (writethrough)
  593. .word 0xffffff00
  594. W(b) __armv4_mmu_cache_on
  595. W(b) __armv4_mmu_cache_off
  596. mov pc, lr
  597. THUMB( nop )
  598. .word 0x41007400 @ ARM74x
  599. .word 0xff00ff00
  600. W(b) __armv3_mpu_cache_on
  601. W(b) __armv3_mpu_cache_off
  602. W(b) __armv3_mpu_cache_flush
  603. .word 0x41009400 @ ARM94x
  604. .word 0xff00ff00
  605. W(b) __armv4_mpu_cache_on
  606. W(b) __armv4_mpu_cache_off
  607. W(b) __armv4_mpu_cache_flush
  608. .word 0x00007000 @ ARM7 IDs
  609. .word 0x0000f000
  610. mov pc, lr
  611. THUMB( nop )
  612. mov pc, lr
  613. THUMB( nop )
  614. mov pc, lr
  615. THUMB( nop )
  616. @ Everything from here on will be the new ID system.
  617. .word 0x4401a100 @ sa110 / sa1100
  618. .word 0xffffffe0
  619. W(b) __armv4_mmu_cache_on
  620. W(b) __armv4_mmu_cache_off
  621. W(b) __armv4_mmu_cache_flush
  622. .word 0x6901b110 @ sa1110
  623. .word 0xfffffff0
  624. W(b) __armv4_mmu_cache_on
  625. W(b) __armv4_mmu_cache_off
  626. W(b) __armv4_mmu_cache_flush
  627. .word 0x56056900
  628. .word 0xffffff00 @ PXA9xx
  629. W(b) __armv4_mmu_cache_on
  630. W(b) __armv4_mmu_cache_off
  631. W(b) __armv4_mmu_cache_flush
  632. .word 0x56158000 @ PXA168
  633. .word 0xfffff000
  634. W(b) __armv4_mmu_cache_on
  635. W(b) __armv4_mmu_cache_off
  636. W(b) __armv5tej_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,
  700. * r0, r1, r2, r3, r9, r12 corrupted
  701. * This routine must preserve:
  702. * r4, r6, r7
  703. */
  704. .align 5
  705. cache_off: mov r3, #12 @ cache_off function
  706. b call_cache_fn
  707. __armv4_mpu_cache_off:
  708. mrc p15, 0, r0, c1, c0
  709. bic r0, r0, #0x000d
  710. mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
  711. mov r0, #0
  712. mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
  713. mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
  714. mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
  715. mov pc, lr
  716. __armv3_mpu_cache_off:
  717. mrc p15, 0, r0, c1, c0
  718. bic r0, r0, #0x000d
  719. mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
  720. mov r0, #0
  721. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  722. mov pc, lr
  723. __armv4_mmu_cache_off:
  724. #ifdef CONFIG_MMU
  725. mrc p15, 0, r0, c1, c0
  726. bic r0, r0, #0x000d
  727. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  728. mov r0, #0
  729. mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
  730. mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
  731. #endif
  732. mov pc, lr
  733. __armv7_mmu_cache_off:
  734. mrc p15, 0, r0, c1, c0
  735. #ifdef CONFIG_MMU
  736. bic r0, r0, #0x000d
  737. #else
  738. bic r0, r0, #0x000c
  739. #endif
  740. mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
  741. mov r12, lr
  742. bl __armv7_mmu_cache_flush
  743. mov r0, #0
  744. #ifdef CONFIG_MMU
  745. mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
  746. #endif
  747. mcr p15, 0, r0, c7, c5, 6 @ invalidate BTC
  748. mcr p15, 0, r0, c7, c10, 4 @ DSB
  749. mcr p15, 0, r0, c7, c5, 4 @ ISB
  750. mov pc, r12
  751. __arm6_mmu_cache_off:
  752. mov r0, #0x00000030 @ ARM6 control reg.
  753. b __armv3_mmu_cache_off
  754. __arm7_mmu_cache_off:
  755. mov r0, #0x00000070 @ ARM7 control reg.
  756. b __armv3_mmu_cache_off
  757. __armv3_mmu_cache_off:
  758. mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
  759. mov r0, #0
  760. mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
  761. mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
  762. mov pc, lr
  763. /*
  764. * Clean and flush the cache to maintain consistency.
  765. *
  766. * On exit,
  767. * r1, r2, r3, r9, r10, r11, r12 corrupted
  768. * This routine must preserve:
  769. * r0, r4, r5, r6, r7
  770. */
  771. .align 5
  772. cache_clean_flush:
  773. mov r3, #16
  774. b call_cache_fn
  775. __armv4_mpu_cache_flush:
  776. mov r2, #1
  777. mov r3, #0
  778. mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
  779. mov r1, #7 << 5 @ 8 segments
  780. 1: orr r3, r1, #63 << 26 @ 64 entries
  781. 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
  782. subs r3, r3, #1 << 26
  783. bcs 2b @ entries 63 to 0
  784. subs r1, r1, #1 << 5
  785. bcs 1b @ segments 7 to 0
  786. teq r2, #0
  787. mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
  788. mcr p15, 0, ip, c7, c10, 4 @ drain WB
  789. mov pc, lr
  790. __fa526_cache_flush:
  791. mov r1, #0
  792. mcr p15, 0, r1, c7, c14, 0 @ clean and invalidate D cache
  793. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  794. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  795. mov pc, lr
  796. __armv6_mmu_cache_flush:
  797. mov r1, #0
  798. mcr p15, 0, r1, c7, c14, 0 @ clean+invalidate D
  799. mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
  800. mcr p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
  801. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  802. mov pc, lr
  803. __armv7_mmu_cache_flush:
  804. mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
  805. tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
  806. mov r10, #0
  807. beq hierarchical
  808. mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
  809. b iflush
  810. hierarchical:
  811. mcr p15, 0, r10, c7, c10, 5 @ DMB
  812. stmfd sp!, {r0-r7, r9-r11}
  813. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  814. ands r3, r0, #0x7000000 @ extract loc from clidr
  815. mov r3, r3, lsr #23 @ left align loc bit field
  816. beq finished @ if loc is 0, then no need to clean
  817. mov r10, #0 @ start clean at cache level 0
  818. loop1:
  819. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  820. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  821. and r1, r1, #7 @ mask of the bits for current cache only
  822. cmp r1, #2 @ see what cache we have at this level
  823. blt skip @ skip if no cache, or just i-cache
  824. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  825. mcr p15, 0, r10, c7, c5, 4 @ isb to sych the new cssr&csidr
  826. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  827. and r2, r1, #7 @ extract the length of the cache lines
  828. add r2, r2, #4 @ add 4 (line length offset)
  829. ldr r4, =0x3ff
  830. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  831. clz r5, r4 @ find bit position of way size increment
  832. ldr r7, =0x7fff
  833. ands r7, r7, r1, lsr #13 @ extract max number of the index size
  834. loop2:
  835. mov r9, r4 @ create working copy of max way size
  836. loop3:
  837. ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
  838. ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
  839. THUMB( lsl r6, r9, r5 )
  840. THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
  841. THUMB( lsl r6, r7, r2 )
  842. THUMB( orr r11, r11, r6 ) @ factor index number into r11
  843. mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
  844. subs r9, r9, #1 @ decrement the way
  845. bge loop3
  846. subs r7, r7, #1 @ decrement the index
  847. bge loop2
  848. skip:
  849. add r10, r10, #2 @ increment cache number
  850. cmp r3, r10
  851. bgt loop1
  852. finished:
  853. ldmfd sp!, {r0-r7, r9-r11}
  854. mov r10, #0 @ swith back to cache level 0
  855. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  856. iflush:
  857. mcr p15, 0, r10, c7, c10, 4 @ DSB
  858. mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
  859. mcr p15, 0, r10, c7, c10, 4 @ DSB
  860. mcr p15, 0, r10, c7, c5, 4 @ ISB
  861. mov pc, lr
  862. __armv5tej_mmu_cache_flush:
  863. 1: mrc p15, 0, r15, c7, c14, 3 @ test,clean,invalidate D cache
  864. bne 1b
  865. mcr p15, 0, r0, c7, c5, 0 @ flush I cache
  866. mcr p15, 0, r0, c7, c10, 4 @ drain WB
  867. mov pc, lr
  868. __armv4_mmu_cache_flush:
  869. mov r2, #64*1024 @ default: 32K dcache size (*2)
  870. mov r11, #32 @ default: 32 byte line size
  871. mrc p15, 0, r3, c0, c0, 1 @ read cache type
  872. teq r3, r9 @ cache ID register present?
  873. beq no_cache_id
  874. mov r1, r3, lsr #18
  875. and r1, r1, #7
  876. mov r2, #1024
  877. mov r2, r2, lsl r1 @ base dcache size *2
  878. tst r3, #1 << 14 @ test M bit
  879. addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
  880. mov r3, r3, lsr #12
  881. and r3, r3, #3
  882. mov r11, #8
  883. mov r11, r11, lsl r3 @ cache line size in bytes
  884. no_cache_id:
  885. mov r1, pc
  886. bic r1, r1, #63 @ align to longest cache line
  887. add r2, r1, r2
  888. 1:
  889. ARM( ldr r3, [r1], r11 ) @ s/w flush D cache
  890. THUMB( ldr r3, [r1] ) @ s/w flush D cache
  891. THUMB( add r1, r1, r11 )
  892. teq r1, r2
  893. bne 1b
  894. mcr p15, 0, r1, c7, c5, 0 @ flush I cache
  895. mcr p15, 0, r1, c7, c6, 0 @ flush D cache
  896. mcr p15, 0, r1, c7, c10, 4 @ drain WB
  897. mov pc, lr
  898. __armv3_mmu_cache_flush:
  899. __armv3_mpu_cache_flush:
  900. mov r1, #0
  901. mcr p15, 0, r1, c7, c0, 0 @ invalidate whole cache v3
  902. mov pc, lr
  903. /*
  904. * Various debugging routines for printing hex characters and
  905. * memory, which again must be relocatable.
  906. */
  907. #ifdef DEBUG
  908. .align 2
  909. .type phexbuf,#object
  910. phexbuf: .space 12
  911. .size phexbuf, . - phexbuf
  912. phex: adr r3, phexbuf
  913. mov r2, #0
  914. strb r2, [r3, r1]
  915. 1: subs r1, r1, #1
  916. movmi r0, r3
  917. bmi puts
  918. and r2, r0, #15
  919. mov r0, r0, lsr #4
  920. cmp r2, #10
  921. addge r2, r2, #7
  922. add r2, r2, #'0'
  923. strb r2, [r3, r1]
  924. b 1b
  925. puts: loadsp r3, r1
  926. 1: ldrb r2, [r0], #1
  927. teq r2, #0
  928. moveq pc, lr
  929. 2: writeb r2, r3
  930. mov r1, #0x00020000
  931. 3: subs r1, r1, #1
  932. bne 3b
  933. teq r2, #'\n'
  934. moveq r2, #'\r'
  935. beq 2b
  936. teq r0, #0
  937. bne 1b
  938. mov pc, lr
  939. putc:
  940. mov r2, r0
  941. mov r0, #0
  942. loadsp r3, r1
  943. b 2b
  944. memdump: mov r12, r0
  945. mov r10, lr
  946. mov r11, #0
  947. 2: mov r0, r11, lsl #2
  948. add r0, r0, r12
  949. mov r1, #8
  950. bl phex
  951. mov r0, #':'
  952. bl putc
  953. 1: mov r0, #' '
  954. bl putc
  955. ldr r0, [r12, r11, lsl #2]
  956. mov r1, #8
  957. bl phex
  958. and r0, r11, #7
  959. teq r0, #3
  960. moveq r0, #' '
  961. bleq putc
  962. and r0, r11, #7
  963. add r11, r11, #1
  964. teq r0, #7
  965. bne 1b
  966. mov r0, #'\n'
  967. bl putc
  968. cmp r11, #64
  969. blt 2b
  970. mov pc, r10
  971. #endif
  972. .ltorg
  973. reloc_end:
  974. .align
  975. .section ".stack", "w"
  976. user_stack: .space 4096
  977. user_stack_end: