head.S 26 KB

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