head.S 27 KB

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