head.S 29 KB

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