head.S 27 KB

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