2 * linux/arch/arm/boot/compressed/head.S
4 * Copyright (C) 1996-2002 Russell King
5 * Copyright (C) 2004 Hyok S. Choi (MPU support)
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.
11 #include <linux/config.h>
12 #include <linux/linkage.h>
17 * Note that these macros must not contain any code which is not
18 * 100% relocatable. Any attempt to do so will result in a crash.
19 * Please select one of the following when turning on debugging.
23 #if defined(CONFIG_DEBUG_ICEDCC)
27 mcr p14, 0, \ch, c0, c1, 0
31 #include <asm/arch/debug-macro.S>
37 #if defined(CONFIG_ARCH_SA1100)
39 mov \rb, #0x80000000 @ physical base address
40 #ifdef CONFIG_DEBUG_LL_SER3
41 add \rb, \rb, #0x00050000 @ Ser3
43 add \rb, \rb, #0x00010000 @ Ser1
46 #elif defined(CONFIG_ARCH_IOP331)
49 orr \rb, \rb, #0x00ff0000
50 orr \rb, \rb, #0x0000f700 @ location of the UART
52 #elif defined(CONFIG_ARCH_S3C2410)
55 add \rb, \rb, #0x4000 * CONFIG_S3C2410_LOWLEVEL_UART_PORT
76 .macro debug_reloc_start
79 kphex r6, 8 /* processor id */
81 kphex r7, 8 /* architecture id */
83 mrc p15, 0, r0, c1, c0
84 kphex r0, 8 /* control reg */
86 kphex r5, 8 /* decompressed kernel start */
88 kphex r9, 8 /* decompressed kernel end */
90 kphex r4, 8 /* kernel execution address */
95 .macro debug_reloc_end
97 kphex r5, 8 /* end of kernel */
100 bl memdump /* dump 256 bytes at start of kernel */
104 .section ".start", #alloc, #execinstr
106 * sort out different calling conventions
110 .type start,#function
116 .word 0x016f2818 @ Magic numbers to help the loader
117 .word start @ absolute load/run zImage address
118 .word _edata @ zImage end address
119 1: mov r7, r1 @ save architecture ID
120 mov r8, r2 @ save atags pointer
122 #ifndef __ARM_ARCH_2__
124 * Booting from Angel - need to enter SVC mode and disable
125 * FIQs/IRQs (numeric definitions from angel arm.h source).
126 * We only do this if we were in user mode on entry.
128 mrs r2, cpsr @ get current mode
129 tst r2, #3 @ not user?
131 mov r0, #0x17 @ angel_SWIreason_EnterSVC
132 swi 0x123456 @ angel_SWI_ARM
134 mrs r2, cpsr @ turn off interrupts to
135 orr r2, r2, #0xc0 @ prevent angel from running
138 teqp pc, #0x0c000003 @ turn off interrupts
142 * Note that some cache flushing and other stuff may
143 * be needed here - is there an Angel SWI call for this?
147 * some architecture specific code can be inserted
148 * by the linker here, but it should preserve r7, r8, and r9.
153 ldmia r0, {r1, r2, r3, r4, r5, r6, ip, sp}
154 subs r0, r0, r1 @ calculate the delta offset
156 @ if delta is zero, we are
157 beq not_relocated @ running at the address we
161 * We're running at a different address. We need to fix
162 * up various pointers:
163 * r5 - zImage base address
171 #ifndef CONFIG_ZBOOT_ROM
173 * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
174 * we need to fix up pointers into the BSS region.
184 * Relocate all entries in the GOT table.
186 1: ldr r1, [r6, #0] @ relocate entries in the GOT
187 add r1, r1, r0 @ table. This fixes up the
188 str r1, [r6], #4 @ C references.
194 * Relocate entries in the GOT table. We only relocate
195 * the entries that are outside the (relocated) BSS region.
197 1: ldr r1, [r6, #0] @ relocate entries in the GOT
198 cmp r1, r2 @ entry < bss_start ||
199 cmphs r3, r1 @ _end < entry
200 addlo r1, r1, r0 @ table. This fixes up the
201 str r1, [r6], #4 @ C references.
206 not_relocated: mov r0, #0
207 1: str r0, [r2], #4 @ clear bss
215 * The C runtime environment should now be setup
216 * sufficiently. Turn the cache on, set up some
217 * pointers, and start decompressing.
221 mov r1, sp @ malloc space above stack
222 add r2, sp, #0x10000 @ 64k max
225 * Check to see if we will overwrite ourselves.
226 * r4 = final kernel address
227 * r5 = start of this image
228 * r2 = end of malloc space (and therefore this image)
231 * r4 + image length <= r5 -> OK
235 add r0, r4, #4096*1024 @ 4MB largest kernel size
239 mov r5, r2 @ decompress after malloc space
245 bic r0, r0, #127 @ align the kernel length
247 * r0 = decompressed kernel length
249 * r4 = kernel execution address
250 * r5 = decompressed kernel start
252 * r7 = architecture ID
256 add r1, r5, r0 @ end of decompressed kernel
260 1: ldmia r2!, {r9 - r14} @ copy relocation code
261 stmia r1!, {r9 - r14}
262 ldmia r2!, {r9 - r14}
263 stmia r1!, {r9 - r14}
268 add pc, r5, r0 @ call relocation code
271 * We're not in danger of overwriting ourselves. Do this the simple way.
273 * r4 = kernel execution address
274 * r7 = architecture ID
276 wont_overwrite: mov r0, r4
283 .word __bss_start @ r2
287 .word _got_start @ r6
289 .word user_stack+4096 @ sp
290 LC1: .word reloc_end - reloc_start
293 #ifdef CONFIG_ARCH_RPC
295 params: ldr r0, =params_phys
302 * Turn on the cache. We need to setup some page tables so that we
303 * can have both the I and D caches on.
305 * We place the page tables 16k down from the kernel execution address,
306 * and we hope that nothing else is using it. If we're using it, we
310 * r4 = kernel execution address
312 * r7 = architecture number
314 * r9 = run-time address of "start" (???)
316 * r1, r2, r3, r9, r10, r12 corrupted
317 * This routine must preserve:
321 cache_on: mov r3, #8 @ cache_on function
325 * Initialize the highest priority protection region, PR7
326 * to cover all 32bit address and cacheable and bufferable.
328 __armv4_mpu_cache_on:
329 mov r0, #0x3f @ 4G, the whole
330 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
331 mcr p15, 0, r0, c6, c7, 1
334 mcr p15, 0, r0, c2, c0, 0 @ D-cache on
335 mcr p15, 0, r0, c2, c0, 1 @ I-cache on
336 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
339 mcr p15, 0, r0, c5, c0, 1 @ I-access permission
340 mcr p15, 0, r0, c5, c0, 0 @ D-access permission
343 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
344 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
345 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
346 mrc p15, 0, r0, c1, c0, 0 @ read control reg
347 @ ...I .... ..D. WC.M
348 orr r0, r0, #0x002d @ .... .... ..1. 11.1
349 orr r0, r0, #0x1000 @ ...1 .... .... ....
351 mcr p15, 0, r0, c1, c0, 0 @ write control reg
354 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
355 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
358 __armv3_mpu_cache_on:
359 mov r0, #0x3f @ 4G, the whole
360 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
363 mcr p15, 0, r0, c2, c0, 0 @ cache on
364 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
367 mcr p15, 0, r0, c5, c0, 0 @ access permission
370 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
371 mrc p15, 0, r0, c1, c0, 0 @ read control reg
372 @ .... .... .... WC.M
373 orr r0, r0, #0x000d @ .... .... .... 11.1
375 mcr p15, 0, r0, c1, c0, 0 @ write control reg
377 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
380 __setup_mmu: sub r3, r4, #16384 @ Page directory size
381 bic r3, r3, #0xff @ Align the pointer
384 * Initialise the page tables, turning on the cacheable and bufferable
385 * bits for the RAM area only.
389 mov r9, r9, lsl #18 @ start of RAM
390 add r10, r9, #0x10000000 @ a reasonable RAM size
394 1: cmp r1, r9 @ if virt > start of RAM
395 orrhs r1, r1, #0x0c @ set cacheable, bufferable
396 cmp r1, r10 @ if virt > end of RAM
397 bichs r1, r1, #0x0c @ clear cacheable, bufferable
398 str r1, [r0], #4 @ 1:1 mapping
403 * If ever we are running from Flash, then we surely want the cache
404 * to be enabled also for our execution instance... We map 2MB of it
405 * so there is no map overlap problem for up to 1 MB compressed kernel.
406 * If the execution is in RAM then we would only be duplicating the above.
411 orr r1, r1, r2, lsl #20
412 add r0, r3, r2, lsl #2
418 __armv4_mmu_cache_on:
422 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
423 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
424 mrc p15, 0, r0, c1, c0, 0 @ read control reg
425 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
427 bl __common_mmu_cache_on
429 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
436 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
437 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
439 bl __common_mmu_cache_on
441 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
444 __common_mmu_cache_on:
446 orr r0, r0, #0x000d @ Write buffer, mmu
449 mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
450 mcr p15, 0, r1, c3, c0, 0 @ load domain access control
451 mcr p15, 0, r0, c1, c0, 0 @ load control register
455 * All code following this line is relocatable. It is relocated by
456 * the above code to the end of the decompressed kernel image and
457 * executed there. During this time, we have no stacks.
459 * r0 = decompressed kernel length
461 * r4 = kernel execution address
462 * r5 = decompressed kernel start
464 * r7 = architecture ID
469 reloc_start: add r9, r5, r0
474 ldmia r5!, {r0, r2, r3, r10 - r14} @ relocate kernel
475 stmia r1!, {r0, r2, r3, r10 - r14}
482 call_kernel: bl cache_clean_flush
484 mov r0, #0 @ must be zero
485 mov r1, r7 @ restore architecture number
486 mov r2, r8 @ restore atags pointer
487 mov pc, r4 @ call kernel
490 * Here follow the relocatable cache support functions for the
491 * various processors. This is a generic hook for locating an
492 * entry and jumping to an instruction at the specified offset
493 * from the start of the block. Please note this is all position
503 call_cache_fn: adr r12, proc_types
504 mrc p15, 0, r6, c0, c0 @ get processor ID
505 1: ldr r1, [r12, #0] @ get value
506 ldr r2, [r12, #4] @ get mask
507 eor r1, r1, r6 @ (real ^ match)
509 addeq pc, r12, r3 @ call cache function
514 * Table for cache operations. This is basically:
517 * - 'cache on' method instruction
518 * - 'cache off' method instruction
519 * - 'cache flush' method instruction
521 * We match an entry using: ((real_id ^ match) & mask) == 0
523 * Writethrough caches generally only need 'on' and 'off'
524 * methods. Writeback caches _must_ have the flush method
527 .type proc_types,#object
529 .word 0x41560600 @ ARM6/610
531 b __arm6_mmu_cache_off @ works, but slow
532 b __arm6_mmu_cache_off
534 @ b __arm6_mmu_cache_on @ untested
535 @ b __arm6_mmu_cache_off
536 @ b __armv3_mmu_cache_flush
538 .word 0x00000000 @ old ARM ID
544 .word 0x41007000 @ ARM7/710
546 b __arm7_mmu_cache_off
547 b __arm7_mmu_cache_off
550 .word 0x41807200 @ ARM720T (writethrough)
552 b __armv4_mmu_cache_on
553 b __armv4_mmu_cache_off
556 .word 0x41007400 @ ARM74x
558 b __armv3_mpu_cache_on
559 b __armv3_mpu_cache_off
560 b __armv3_mpu_cache_flush
562 .word 0x41009400 @ ARM94x
564 b __armv4_mpu_cache_on
565 b __armv4_mpu_cache_off
566 b __armv4_mpu_cache_flush
568 .word 0x00007000 @ ARM7 IDs
574 @ Everything from here on will be the new ID system.
576 .word 0x4401a100 @ sa110 / sa1100
578 b __armv4_mmu_cache_on
579 b __armv4_mmu_cache_off
580 b __armv4_mmu_cache_flush
582 .word 0x6901b110 @ sa1110
584 b __armv4_mmu_cache_on
585 b __armv4_mmu_cache_off
586 b __armv4_mmu_cache_flush
588 @ These match on the architecture ID
590 .word 0x00020000 @ ARMv4T
592 b __armv4_mmu_cache_on
593 b __armv4_mmu_cache_off
594 b __armv4_mmu_cache_flush
596 .word 0x00050000 @ ARMv5TE
598 b __armv4_mmu_cache_on
599 b __armv4_mmu_cache_off
600 b __armv4_mmu_cache_flush
602 .word 0x00060000 @ ARMv5TEJ
604 b __armv4_mmu_cache_on
605 b __armv4_mmu_cache_off
606 b __armv4_mmu_cache_flush
608 .word 0x0007b000 @ ARMv6
610 b __armv4_mmu_cache_on
611 b __armv4_mmu_cache_off
612 b __armv6_mmu_cache_flush
614 .word 0 @ unrecognised type
620 .size proc_types, . - proc_types
623 * Turn off the Cache and MMU. ARMv3 does not support
624 * reading the control register, but ARMv4 does.
626 * On entry, r6 = processor ID
627 * On exit, r0, r1, r2, r3, r12 corrupted
628 * This routine must preserve: r4, r6, r7
631 cache_off: mov r3, #12 @ cache_off function
634 __armv4_mpu_cache_off:
635 mrc p15, 0, r0, c1, c0
637 mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
639 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
640 mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
641 mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
644 __armv3_mpu_cache_off:
645 mrc p15, 0, r0, c1, c0
647 mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
649 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
652 __armv4_mmu_cache_off:
653 mrc p15, 0, r0, c1, c0
655 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
657 mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
658 mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
661 __arm6_mmu_cache_off:
662 mov r0, #0x00000030 @ ARM6 control reg.
663 b __armv3_mmu_cache_off
665 __arm7_mmu_cache_off:
666 mov r0, #0x00000070 @ ARM7 control reg.
667 b __armv3_mmu_cache_off
669 __armv3_mmu_cache_off:
670 mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
672 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
673 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
677 * Clean and flush the cache to maintain consistency.
682 * r1, r2, r3, r11, r12 corrupted
683 * This routine must preserve:
691 __armv4_mpu_cache_flush:
694 mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
695 mov r1, #7 << 5 @ 8 segments
696 1: orr r3, r1, #63 << 26 @ 64 entries
697 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
698 subs r3, r3, #1 << 26
699 bcs 2b @ entries 63 to 0
701 bcs 1b @ segments 7 to 0
704 mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
705 mcr p15, 0, ip, c7, c10, 4 @ drain WB
709 __armv6_mmu_cache_flush:
711 mcr p15, 0, r1, c7, c14, 0 @ clean+invalidate D
712 mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
713 mcr p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
714 mcr p15, 0, r1, c7, c10, 4 @ drain WB
717 __armv4_mmu_cache_flush:
718 mov r2, #64*1024 @ default: 32K dcache size (*2)
719 mov r11, #32 @ default: 32 byte line size
720 mrc p15, 0, r3, c0, c0, 1 @ read cache type
721 teq r3, r6 @ cache ID register present?
726 mov r2, r2, lsl r1 @ base dcache size *2
727 tst r3, #1 << 14 @ test M bit
728 addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
732 mov r11, r11, lsl r3 @ cache line size in bytes
734 bic r1, pc, #63 @ align to longest cache line
736 1: ldr r3, [r1], r11 @ s/w flush D cache
740 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
741 mcr p15, 0, r1, c7, c6, 0 @ flush D cache
742 mcr p15, 0, r1, c7, c10, 4 @ drain WB
745 __armv3_mmu_cache_flush:
746 __armv3_mpu_cache_flush:
748 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
752 * Various debugging routines for printing hex characters and
753 * memory, which again must be relocatable.
756 .type phexbuf,#object
758 .size phexbuf, . - phexbuf
760 phex: adr r3, phexbuf
797 2: mov r0, r11, lsl #2
805 ldr r0, [r12, r11, lsl #2]
826 .section ".stack", "w"
827 user_stack: .space 4096