head.S 26 KB

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