head.S 23 KB

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