head.S 26 KB

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