head.S 26 KB

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