1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/arch/arm/boot/compressed/head.S
5 * Copyright (C) 1996-2002 Russell King
6 * Copyright (C) 2004 Hyok S. Choi (MPU support)
8 #include <linux/linkage.h>
9 #include <asm/assembler.h>
12 #include "efi-header.S"
15 #define OF_DT_MAGIC 0xd00dfeed
17 #define OF_DT_MAGIC 0xedfe0dd0
20 AR_CLASS( .arch armv7-a )
21 M_CLASS( .arch armv7-m )
26 * Note that these macros must not contain any code which is not
27 * 100% relocatable. Any attempt to do so will result in a crash.
28 * Please select one of the following when turning on debugging.
32 #if defined(CONFIG_DEBUG_ICEDCC)
34 #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
35 .macro loadsp, rb, tmp1, tmp2
37 .macro writeb, ch, rb, tmp
38 mcr p14, 0, \ch, c0, c5, 0
40 #elif defined(CONFIG_CPU_XSCALE)
41 .macro loadsp, rb, tmp1, tmp2
43 .macro writeb, ch, rb, tmp
44 mcr p14, 0, \ch, c8, c0, 0
47 .macro loadsp, rb, tmp1, tmp2
49 .macro writeb, ch, rb, tmp
50 mcr p14, 0, \ch, c1, c0, 0
56 #include CONFIG_DEBUG_LL_INCLUDE
58 .macro writeb, ch, rb, tmp
59 #ifdef CONFIG_DEBUG_UART_FLOW_CONTROL
62 waituarttxrdy \tmp, \rb
67 #if defined(CONFIG_ARCH_SA1100)
68 .macro loadsp, rb, tmp1, tmp2
69 mov \rb, #0x80000000 @ physical base address
70 #ifdef CONFIG_DEBUG_LL_SER3
71 add \rb, \rb, #0x00050000 @ Ser3
73 add \rb, \rb, #0x00010000 @ Ser1
77 .macro loadsp, rb, tmp1, tmp2
78 addruart \rb, \tmp1, \tmp2
96 * Debug kernel copy by printing the memory addresses involved
98 .macro dbgkc, begin, end, cbegin, cend
104 kphex \begin, 8 /* Start of compressed kernel */
108 kphex \end, 8 /* End of compressed kernel */
113 kphex \cbegin, 8 /* Start of kernel copy */
117 kphex \cend, 8 /* End of kernel copy */
123 * Debug print of the final appended DTB location
125 .macro dbgadtb, begin, size
133 kphex \begin, 8 /* Start of appended DTB */
138 kphex \size, 8 /* Size of appended DTB */
144 .macro enable_cp15_barriers, reg
145 mrc p15, 0, \reg, c1, c0, 0 @ read SCTLR
146 tst \reg, #(1 << 5) @ CP15BEN bit set?
148 orr \reg, \reg, #(1 << 5) @ CP15 barrier instructions
149 mcr p15, 0, \reg, c1, c0, 0 @ write SCTLR
150 ARM( .inst 0xf57ff06f @ v7+ isb )
156 * The kernel build system appends the size of the
157 * decompressed kernel at the end of the compressed data
158 * in little-endian form.
160 .macro get_inflated_image_size, res:req, tmp1:req, tmp2:req
161 adr \res, .Linflated_image_size_offset
163 add \tmp1, \tmp1, \res @ address of inflated image size
165 ldrb \res, [\tmp1] @ get_unaligned_le32
166 ldrb \tmp2, [\tmp1, #1]
167 orr \res, \res, \tmp2, lsl #8
168 ldrb \tmp2, [\tmp1, #2]
169 ldrb \tmp1, [\tmp1, #3]
170 orr \res, \res, \tmp2, lsl #16
171 orr \res, \res, \tmp1, lsl #24
174 .macro be32tocpu, val, tmp
176 /* convert to little endian */
177 eor \tmp, \val, \val, ror #16
178 bic \tmp, \tmp, #0x00ff0000
179 mov \val, \val, ror #8
180 eor \val, \val, \tmp, lsr #8
184 .section ".start", "ax"
186 * sort out different calling conventions
190 * Always enter in ARM state for CPUs that support the ARM ISA.
191 * As of today (2014) that's exactly the members of the A and R
196 .type start,#function
198 * These 7 nops along with the 1 nop immediately below for
199 * !THUMB2 form 8 nops that make the compressed kernel bootable
200 * on legacy ARM systems that were assuming the kernel in a.out
201 * binary format. The boot loaders on these systems would
202 * jump 32 bytes into the image to skip the a.out header.
203 * with these 8 nops filling exactly 32 bytes, things still
204 * work as expected on these legacy systems. Thumb2 mode keeps
205 * 7 of the nops as it turns out that some boot loaders
206 * were patching the initial instructions of the kernel, i.e
207 * had started to exploit this "patch area".
212 #ifndef CONFIG_THUMB2_KERNEL
215 AR_CLASS( sub pc, pc, #3 ) @ A/R: switch to Thumb2 mode
216 M_CLASS( nop.w ) @ M: already in Thumb2 mode
221 .word _magic_sig @ Magic numbers to help the loader
222 .word _magic_start @ absolute load/run zImage address
223 .word _magic_end @ zImage end address
224 .word 0x04030201 @ endianness flag
225 .word 0x45454545 @ another magic number to indicate
226 .word _magic_table @ additional data table
230 ARM_BE8( setend be ) @ go BE8 if compiled for BE8
231 AR_CLASS( mrs r9, cpsr )
232 #ifdef CONFIG_ARM_VIRT_EXT
233 bl __hyp_stub_install @ get into SVC mode, reversibly
235 mov r7, r1 @ save architecture ID
236 mov r8, r2 @ save atags pointer
238 #ifndef CONFIG_CPU_V7M
240 * Booting from Angel - need to enter SVC mode and disable
241 * FIQs/IRQs (numeric definitions from angel arm.h source).
242 * We only do this if we were in user mode on entry.
244 mrs r2, cpsr @ get current mode
245 tst r2, #3 @ not user?
247 mov r0, #0x17 @ angel_SWIreason_EnterSVC
248 ARM( swi 0x123456 ) @ angel_SWI_ARM
249 THUMB( svc 0xab ) @ angel_SWI_THUMB
251 safe_svcmode_maskall r0
252 msr spsr_cxsf, r9 @ Save the CPU boot mode in
256 * Note that some cache flushing and other stuff may
257 * be needed here - is there an Angel SWI call for this?
261 * some architecture specific code can be inserted
262 * by the linker here, but it should preserve r7, r8, and r9.
267 #ifdef CONFIG_AUTO_ZRELADDR
269 * Find the start of physical memory. As we are executing
270 * without the MMU on, we are in the physical address space.
271 * We just need to get rid of any offset by aligning the
274 * This alignment is a balance between the requirements of
275 * different platforms - we have chosen 128MB to allow
276 * platforms which align the start of their physical memory
277 * to 128MB to use this feature, while allowing the zImage
278 * to be placed within the first 128MB of memory on other
279 * platforms. Increasing the alignment means we place
280 * stricter alignment requirements on the start of physical
281 * memory, but relaxing it means that we break people who
282 * are already placing their zImage in (eg) the top 64MB
286 and r4, r4, #0xf8000000
287 /* Determine final kernel image address. */
288 add r4, r4, #TEXT_OFFSET
294 * Set up a page table only if it won't overwrite ourself.
295 * That means r4 < pc || r4 - 16k page directory > &_end.
296 * Given that r4 > &_end is most unfrequent, we add a rough
297 * additional 1MB of room for a possible appended DTB.
304 orrcc r4, r4, #1 @ remember we skipped cache_on
313 get_inflated_image_size r9, r10, lr
315 #ifndef CONFIG_ZBOOT_ROM
316 /* malloc space is above the relocated stack (64k max) */
317 add r10, sp, #MALLOC_SIZE
320 * With ZBOOT_ROM the bss/stack is non relocatable,
321 * but someone could still run this code from RAM,
322 * in which case our reference is _edata.
327 mov r5, #0 @ init dtb size to 0
328 #ifdef CONFIG_ARM_APPENDED_DTB
330 * r4 = final kernel address (possibly with LSB set)
331 * r5 = appended dtb size (still unknown)
333 * r7 = architecture ID
334 * r8 = atags/device tree pointer
335 * r9 = size of decompressed image
336 * r10 = end of this image, including bss/stack/malloc space if non XIP
339 * if there are device trees (dtb) appended to zImage, advance r10 so that the
340 * dtb data will get relocated along with the kernel if necessary.
346 bne dtb_check_done @ not found
348 #ifdef CONFIG_ARM_ATAG_DTB_COMPAT
350 * OK... Let's do some funky business here.
351 * If we do have a DTB appended to zImage, and we do have
352 * an ATAG list around, we want the later to be translated
353 * and folded into the former here. No GOT fixup has occurred
354 * yet, but none of the code we're about to call uses any
358 /* Get the initial DTB size */
362 /* 50% DTB growth should be good enough */
363 add r5, r5, r5, lsr #1
364 /* preserve 64-bit alignment */
367 /* clamp to 32KB min and 1MB max */
372 /* temporarily relocate the stack past the DTB work space */
381 * If returned value is 1, there is no ATAG at the location
382 * pointed by r8. Try the typical 0x100 offset from start
383 * of RAM and hope for the best.
386 sub r0, r4, #TEXT_OFFSET
396 mov r8, r6 @ use the appended device tree
399 * Make sure that the DTB doesn't end up in the final
400 * kernel's .bss area. To do so, we adjust the decompressed
401 * kernel size to compensate if that .bss size is larger
402 * than the relocated code.
404 ldr r5, =_kernel_bss_size
405 adr r1, wont_overwrite
410 /* Get the current DTB size */
414 /* preserve 64-bit alignment */
418 /* relocate some pointers past the appended dtb */
426 * Check to see if we will overwrite ourselves.
427 * r4 = final kernel address (possibly with LSB set)
428 * r9 = size of decompressed image
429 * r10 = end of this image, including bss/stack/malloc space if non XIP
431 * r4 - 16k page directory >= r10 -> OK
432 * r4 + image length <= address of wont_overwrite -> OK
433 * Note: the possible LSB in r4 is harmless here.
439 adr r9, wont_overwrite
444 * Relocate ourselves past the end of the decompressed kernel.
446 * r10 = end of the decompressed kernel
447 * Because we always copy ahead, we need to do it from the end and go
448 * backward in case the source and destination overlap.
451 * Bump to the next 256-byte boundary with the size of
452 * the relocation code added. This avoids overwriting
453 * ourself when the offset is small.
455 add r10, r10, #((reloc_code_end - restart + 256) & ~255)
458 /* Get start of code we want to copy and align it down. */
462 /* Relocate the hyp vector base if necessary */
463 #ifdef CONFIG_ARM_VIRT_EXT
465 and r0, r0, #MODE_MASK
470 * Compute the address of the hyp vectors after relocation.
471 * Call __hyp_set_vectors with the new address so that we
472 * can HVC again after the copy.
474 adr_l r0, __hyp_stub_vectors
481 sub r9, r6, r5 @ size to copy
482 add r9, r9, #31 @ rounded up to a multiple
483 bic r9, r9, #31 @ ... of 32 bytes
491 * We are about to copy the kernel to a new memory area.
492 * The boundaries of the new memory area can be found in
493 * r10 and r9, whilst r5 and r6 contain the boundaries
494 * of the memory we are going to copy.
495 * Calling dbgkc will help with the printing of this
498 dbgkc r5, r6, r10, r9
501 1: ldmdb r6!, {r0 - r3, r10 - r12, lr}
503 stmdb r9!, {r0 - r3, r10 - r12, lr}
506 /* Preserve offset to relocated code. */
509 mov r0, r9 @ start of relocated zImage
510 add r1, sp, r6 @ end of relocated zImage
519 ldmia r0, {r1, r2, r3, r11, r12}
520 sub r0, r0, r1 @ calculate the delta offset
523 * If delta is zero, we are running at the address we were linked at.
527 * r4 = kernel execution address (possibly with LSB set)
528 * r5 = appended dtb size (0 if not present)
529 * r7 = architecture ID
541 #ifndef CONFIG_ZBOOT_ROM
543 * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
544 * we need to fix up pointers into the BSS region.
545 * Note that the stack pointer has already been fixed up.
551 * Relocate all entries in the GOT table.
552 * Bump bss entries to _edata + dtb size
554 1: ldr r1, [r11, #0] @ relocate entries in the GOT
555 add r1, r1, r0 @ This fixes up C references
556 cmp r1, r2 @ if entry >= bss_start &&
557 cmphs r3, r1 @ bss_end > entry
558 addhi r1, r1, r5 @ entry += dtb size
559 str r1, [r11], #4 @ next entry
563 /* bump our bss pointers too */
570 * Relocate entries in the GOT table. We only relocate
571 * the entries that are outside the (relocated) BSS region.
573 1: ldr r1, [r11, #0] @ relocate entries in the GOT
574 cmp r1, r2 @ entry < bss_start ||
575 cmphs r3, r1 @ _end < entry
576 addlo r1, r1, r0 @ table. This fixes up the
577 str r1, [r11], #4 @ C references.
582 not_relocated: mov r0, #0
583 1: str r0, [r2], #4 @ clear bss
591 * Did we skip the cache setup earlier?
592 * That is indicated by the LSB in r4.
600 * The C runtime environment should now be setup sufficiently.
601 * Set up some pointers, and start decompressing.
602 * r4 = kernel execution address
603 * r7 = architecture ID
607 mov r1, sp @ malloc space above stack
608 add r2, sp, #MALLOC_SIZE @ 64k max
612 get_inflated_image_size r1, r2, r3
614 mov r0, r4 @ start of inflated image
615 add r1, r1, r0 @ end of inflated image
619 #ifdef CONFIG_ARM_VIRT_EXT
620 mrs r0, spsr @ Get saved CPU boot mode
621 and r0, r0, #MODE_MASK
622 cmp r0, #HYP_MODE @ if not booted in HYP mode...
623 bne __enter_kernel @ boot kernel directly
625 adr_l r0, __hyp_reentry_vectors
627 __HVC(0) @ otherwise bounce to hyp mode
629 b . @ should never be reached
637 .word __bss_start @ r2
639 .word _got_start @ r11
644 LC1: .word .L_user_stack_end - LC1 @ sp
645 .word _edata - LC1 @ r6
649 .word _end - restart + 16384 + 1024*1024
651 .Linflated_image_size_offset:
652 .long (input_data_end - 4) - .
654 #ifdef CONFIG_ARCH_RPC
656 params: ldr r0, =0x10000100 @ params_phys for RPC
663 * dcache_line_size - get the minimum D-cache line size from the CTR register
666 .macro dcache_line_size, reg, tmp
667 #ifdef CONFIG_CPU_V7M
668 movw \tmp, #:lower16:BASEADDR_V7M_SCB + V7M_SCB_CTR
669 movt \tmp, #:upper16:BASEADDR_V7M_SCB + V7M_SCB_CTR
672 mrc p15, 0, \tmp, c0, c0, 1 @ read ctr
675 and \tmp, \tmp, #0xf @ cache line size encoding
676 mov \reg, #4 @ bytes per word
677 mov \reg, \reg, lsl \tmp @ actual cache line size
681 * Turn on the cache. We need to setup some page tables so that we
682 * can have both the I and D caches on.
684 * We place the page tables 16k down from the kernel execution address,
685 * and we hope that nothing else is using it. If we're using it, we
689 * r4 = kernel execution address
690 * r7 = architecture number
693 * r0, r1, r2, r3, r9, r10, r12 corrupted
694 * This routine must preserve:
698 cache_on: mov r3, #8 @ cache_on function
702 * Initialize the highest priority protection region, PR7
703 * to cover all 32bit address and cacheable and bufferable.
705 __armv4_mpu_cache_on:
706 mov r0, #0x3f @ 4G, the whole
707 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
708 mcr p15, 0, r0, c6, c7, 1
711 mcr p15, 0, r0, c2, c0, 0 @ D-cache on
712 mcr p15, 0, r0, c2, c0, 1 @ I-cache on
713 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
716 mcr p15, 0, r0, c5, c0, 1 @ I-access permission
717 mcr p15, 0, r0, c5, c0, 0 @ D-access permission
720 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
721 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
722 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
723 mrc p15, 0, r0, c1, c0, 0 @ read control reg
724 @ ...I .... ..D. WC.M
725 orr r0, r0, #0x002d @ .... .... ..1. 11.1
726 orr r0, r0, #0x1000 @ ...1 .... .... ....
728 mcr p15, 0, r0, c1, c0, 0 @ write control reg
731 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
732 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
735 __armv3_mpu_cache_on:
736 mov r0, #0x3f @ 4G, the whole
737 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
740 mcr p15, 0, r0, c2, c0, 0 @ cache on
741 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
744 mcr p15, 0, r0, c5, c0, 0 @ access permission
747 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
749 * ?? ARMv3 MMU does not allow reading the control register,
750 * does this really work on ARMv3 MPU?
752 mrc p15, 0, r0, c1, c0, 0 @ read control reg
753 @ .... .... .... WC.M
754 orr r0, r0, #0x000d @ .... .... .... 11.1
755 /* ?? this overwrites the value constructed above? */
757 mcr p15, 0, r0, c1, c0, 0 @ write control reg
759 /* ?? invalidate for the second time? */
760 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
763 #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
769 __setup_mmu: sub r3, r4, #16384 @ Page directory size
770 bic r3, r3, #0xff @ Align the pointer
773 * Initialise the page tables, turning on the cacheable and bufferable
774 * bits for the RAM area only.
778 mov r9, r9, lsl #18 @ start of RAM
779 add r10, r9, #0x10000000 @ a reasonable RAM size
780 mov r1, #0x12 @ XN|U + section mapping
781 orr r1, r1, #3 << 10 @ AP=11
783 1: cmp r1, r9 @ if virt > start of RAM
784 cmphs r10, r1 @ && end of RAM > virt
785 bic r1, r1, #0x1c @ clear XN|U + C + B
786 orrlo r1, r1, #0x10 @ Set XN|U for non-RAM
787 orrhs r1, r1, r6 @ set RAM section settings
788 str r1, [r0], #4 @ 1:1 mapping
793 * If ever we are running from Flash, then we surely want the cache
794 * to be enabled also for our execution instance... We map 2MB of it
795 * so there is no map overlap problem for up to 1 MB compressed kernel.
796 * If the execution is in RAM then we would only be duplicating the above.
798 orr r1, r6, #0x04 @ ensure B is set for this
802 orr r1, r1, r2, lsl #20
803 add r0, r3, r2, lsl #2
810 @ Enable unaligned access on v6, to allow better code generation
811 @ for the decompressor C code:
812 __armv6_mmu_cache_on:
813 mrc p15, 0, r0, c1, c0, 0 @ read SCTLR
814 bic r0, r0, #2 @ A (no unaligned access fault)
815 orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
816 mcr p15, 0, r0, c1, c0, 0 @ write SCTLR
817 b __armv4_mmu_cache_on
819 __arm926ejs_mmu_cache_on:
820 #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
821 mov r0, #4 @ put dcache in WT mode
822 mcr p15, 7, r0, c15, c0, 0
825 __armv4_mmu_cache_on:
828 mov r6, #CB_BITS | 0x12 @ U
831 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
832 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
833 mrc p15, 0, r0, c1, c0, 0 @ read control reg
834 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
836 ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
837 bl __common_mmu_cache_on
839 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
843 __armv7_mmu_cache_on:
844 enable_cp15_barriers r11
847 mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
849 movne r6, #CB_BITS | 0x02 @ !XN
852 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
854 mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
856 mrc p15, 0, r0, c1, c0, 0 @ read control reg
857 bic r0, r0, #1 << 28 @ clear SCTLR.TRE
858 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
859 orr r0, r0, #0x003c @ write buffer
860 bic r0, r0, #2 @ A (no unaligned access fault)
861 orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
862 @ (needed for ARM1176)
864 ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
865 mrcne p15, 0, r6, c2, c0, 2 @ read ttb control reg
866 orrne r0, r0, #1 @ MMU enabled
867 movne r1, #0xfffffffd @ domain 0 = client
868 bic r6, r6, #1 << 31 @ 32-bit translation system
869 bic r6, r6, #(7 << 0) | (1 << 4) @ use only ttbr0
870 mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
871 mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
872 mcrne p15, 0, r6, c2, c0, 2 @ load ttb control
874 mcr p15, 0, r0, c7, c5, 4 @ ISB
875 mcr p15, 0, r0, c1, c0, 0 @ load control register
876 mrc p15, 0, r0, c1, c0, 0 @ and read it back
878 mcr p15, 0, r0, c7, c5, 4 @ ISB
883 mov r6, #CB_BITS | 0x12 @ U
886 mcr p15, 0, r0, c7, c7, 0 @ Invalidate whole cache
887 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
888 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
889 mrc p15, 0, r0, c1, c0, 0 @ read control reg
890 orr r0, r0, #0x1000 @ I-cache enable
891 bl __common_mmu_cache_on
893 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
896 __common_mmu_cache_on:
897 #ifndef CONFIG_THUMB2_KERNEL
899 orr r0, r0, #0x000d @ Write buffer, mmu
902 mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
903 mcr p15, 0, r1, c3, c0, 0 @ load domain access control
905 .align 5 @ cache line aligned
906 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
907 mrc p15, 0, r0, c1, c0, 0 @ and read it back to
908 sub pc, lr, r0, lsr #32 @ properly flush pipeline
911 #define PROC_ENTRY_SIZE (4*5)
914 * Here follow the relocatable cache support functions for the
915 * various processors. This is a generic hook for locating an
916 * entry and jumping to an instruction at the specified offset
917 * from the start of the block. Please note this is all position
927 call_cache_fn: adr r12, proc_types
928 #ifdef CONFIG_CPU_CP15
929 mrc p15, 0, r9, c0, c0 @ get processor ID
930 #elif defined(CONFIG_CPU_V7M)
932 * On v7-M the processor id is located in the V7M_SCB_CPUID
933 * register, but as cache handling is IMPLEMENTATION DEFINED on
934 * v7-M (if existant at all) we just return early here.
935 * If V7M_SCB_CPUID were used the cpu ID functions (i.e.
936 * __armv7_mmu_cache_{on,off,flush}) would be selected which
937 * use cp15 registers that are not implemented on v7-M.
941 ldr r9, =CONFIG_PROCESSOR_ID
943 1: ldr r1, [r12, #0] @ get value
944 ldr r2, [r12, #4] @ get mask
945 eor r1, r1, r9 @ (real ^ match)
947 ARM( addeq pc, r12, r3 ) @ call cache function
948 THUMB( addeq r12, r3 )
949 THUMB( moveq pc, r12 ) @ call cache function
950 add r12, r12, #PROC_ENTRY_SIZE
954 * Table for cache operations. This is basically:
957 * - 'cache on' method instruction
958 * - 'cache off' method instruction
959 * - 'cache flush' method instruction
961 * We match an entry using: ((real_id ^ match) & mask) == 0
963 * Writethrough caches generally only need 'on' and 'off'
964 * methods. Writeback caches _must_ have the flush method
968 .type proc_types,#object
970 .word 0x41000000 @ old ARM ID
979 .word 0x41007000 @ ARM7/710
988 .word 0x41807200 @ ARM720T (writethrough)
990 W(b) __armv4_mmu_cache_on
991 W(b) __armv4_mmu_cache_off
995 .word 0x41007400 @ ARM74x
997 W(b) __armv3_mpu_cache_on
998 W(b) __armv3_mpu_cache_off
999 W(b) __armv3_mpu_cache_flush
1001 .word 0x41009400 @ ARM94x
1003 W(b) __armv4_mpu_cache_on
1004 W(b) __armv4_mpu_cache_off
1005 W(b) __armv4_mpu_cache_flush
1007 .word 0x41069260 @ ARM926EJ-S (v5TEJ)
1009 W(b) __arm926ejs_mmu_cache_on
1010 W(b) __armv4_mmu_cache_off
1011 W(b) __armv5tej_mmu_cache_flush
1013 .word 0x00007000 @ ARM7 IDs
1022 @ Everything from here on will be the new ID system.
1024 .word 0x4401a100 @ sa110 / sa1100
1026 W(b) __armv4_mmu_cache_on
1027 W(b) __armv4_mmu_cache_off
1028 W(b) __armv4_mmu_cache_flush
1030 .word 0x6901b110 @ sa1110
1032 W(b) __armv4_mmu_cache_on
1033 W(b) __armv4_mmu_cache_off
1034 W(b) __armv4_mmu_cache_flush
1037 .word 0xffffff00 @ PXA9xx
1038 W(b) __armv4_mmu_cache_on
1039 W(b) __armv4_mmu_cache_off
1040 W(b) __armv4_mmu_cache_flush
1042 .word 0x56158000 @ PXA168
1044 W(b) __armv4_mmu_cache_on
1045 W(b) __armv4_mmu_cache_off
1046 W(b) __armv5tej_mmu_cache_flush
1048 .word 0x56050000 @ Feroceon
1050 W(b) __armv4_mmu_cache_on
1051 W(b) __armv4_mmu_cache_off
1052 W(b) __armv5tej_mmu_cache_flush
1054 #ifdef CONFIG_CPU_FEROCEON_OLD_ID
1055 /* this conflicts with the standard ARMv5TE entry */
1056 .long 0x41009260 @ Old Feroceon
1058 b __armv4_mmu_cache_on
1059 b __armv4_mmu_cache_off
1060 b __armv5tej_mmu_cache_flush
1063 .word 0x66015261 @ FA526
1065 W(b) __fa526_cache_on
1066 W(b) __armv4_mmu_cache_off
1067 W(b) __fa526_cache_flush
1069 @ These match on the architecture ID
1071 .word 0x00020000 @ ARMv4T
1073 W(b) __armv4_mmu_cache_on
1074 W(b) __armv4_mmu_cache_off
1075 W(b) __armv4_mmu_cache_flush
1077 .word 0x00050000 @ ARMv5TE
1079 W(b) __armv4_mmu_cache_on
1080 W(b) __armv4_mmu_cache_off
1081 W(b) __armv4_mmu_cache_flush
1083 .word 0x00060000 @ ARMv5TEJ
1085 W(b) __armv4_mmu_cache_on
1086 W(b) __armv4_mmu_cache_off
1087 W(b) __armv5tej_mmu_cache_flush
1089 .word 0x0007b000 @ ARMv6
1091 W(b) __armv6_mmu_cache_on
1092 W(b) __armv4_mmu_cache_off
1093 W(b) __armv6_mmu_cache_flush
1095 .word 0x000f0000 @ new CPU Id
1097 W(b) __armv7_mmu_cache_on
1098 W(b) __armv7_mmu_cache_off
1099 W(b) __armv7_mmu_cache_flush
1101 .word 0 @ unrecognised type
1110 .size proc_types, . - proc_types
1113 * If you get a "non-constant expression in ".if" statement"
1114 * error from the assembler on this line, check that you have
1115 * not accidentally written a "b" instruction where you should
1116 * have written W(b).
1118 .if (. - proc_types) % PROC_ENTRY_SIZE != 0
1119 .error "The size of one or more proc_types entries is wrong."
1123 * Turn off the Cache and MMU. ARMv3 does not support
1124 * reading the control register, but ARMv4 does.
1127 * r0, r1, r2, r3, r9, r12 corrupted
1128 * This routine must preserve:
1132 cache_off: mov r3, #12 @ cache_off function
1135 __armv4_mpu_cache_off:
1136 mrc p15, 0, r0, c1, c0
1138 mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
1140 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
1141 mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
1142 mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
1145 __armv3_mpu_cache_off:
1146 mrc p15, 0, r0, c1, c0
1148 mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
1150 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
1153 __armv4_mmu_cache_off:
1155 mrc p15, 0, r0, c1, c0
1157 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
1159 mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
1160 mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
1164 __armv7_mmu_cache_off:
1165 mrc p15, 0, r0, c1, c0
1171 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
1174 mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
1176 mcr p15, 0, r0, c7, c5, 6 @ invalidate BTC
1177 mcr p15, 0, r0, c7, c10, 4 @ DSB
1178 mcr p15, 0, r0, c7, c5, 4 @ ISB
1182 * Clean and flush the cache to maintain consistency.
1185 * r0 = start address
1186 * r1 = end address (exclusive)
1188 * r1, r2, r3, r9, r10, r11, r12 corrupted
1189 * This routine must preserve:
1198 __armv4_mpu_cache_flush:
1203 mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
1204 mov r1, #7 << 5 @ 8 segments
1205 1: orr r3, r1, #63 << 26 @ 64 entries
1206 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
1207 subs r3, r3, #1 << 26
1208 bcs 2b @ entries 63 to 0
1209 subs r1, r1, #1 << 5
1210 bcs 1b @ segments 7 to 0
1213 mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
1214 mcr p15, 0, ip, c7, c10, 4 @ drain WB
1217 __fa526_cache_flush:
1221 mcr p15, 0, r1, c7, c14, 0 @ clean and invalidate D cache
1222 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1223 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1226 __armv6_mmu_cache_flush:
1229 mcreq p15, 0, r1, c7, c14, 0 @ clean+invalidate D
1230 mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
1231 mcreq p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
1232 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1235 __armv7_mmu_cache_flush:
1236 enable_cp15_barriers r10
1239 mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
1240 tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
1243 mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
1246 dcache_line_size r1, r2 @ r1 := dcache min line size
1247 sub r2, r1, #1 @ r2 := line size mask
1248 bic r0, r0, r2 @ round down start to line size
1249 sub r11, r11, #1 @ end address is exclusive
1250 bic r11, r11, r2 @ round down end to line size
1251 0: cmp r0, r11 @ finished?
1253 mcr p15, 0, r0, c7, c14, 1 @ Dcache clean/invalidate by VA
1257 mcr p15, 0, r10, c7, c10, 4 @ DSB
1258 mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
1259 mcr p15, 0, r10, c7, c10, 4 @ DSB
1260 mcr p15, 0, r10, c7, c5, 4 @ ISB
1263 __armv5tej_mmu_cache_flush:
1266 1: mrc p15, 0, APSR_nzcv, c7, c14, 3 @ test,clean,invalidate D cache
1268 mcr p15, 0, r0, c7, c5, 0 @ flush I cache
1269 mcr p15, 0, r0, c7, c10, 4 @ drain WB
1272 __armv4_mmu_cache_flush:
1275 mov r2, #64*1024 @ default: 32K dcache size (*2)
1276 mov r11, #32 @ default: 32 byte line size
1277 mrc p15, 0, r3, c0, c0, 1 @ read cache type
1278 teq r3, r9 @ cache ID register present?
1283 mov r2, r2, lsl r1 @ base dcache size *2
1284 tst r3, #1 << 14 @ test M bit
1285 addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
1289 mov r11, r11, lsl r3 @ cache line size in bytes
1292 bic r1, r1, #63 @ align to longest cache line
1295 ARM( ldr r3, [r1], r11 ) @ s/w flush D cache
1296 THUMB( ldr r3, [r1] ) @ s/w flush D cache
1297 THUMB( add r1, r1, r11 )
1301 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1302 mcr p15, 0, r1, c7, c6, 0 @ flush D cache
1303 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1306 __armv3_mmu_cache_flush:
1307 __armv3_mpu_cache_flush:
1311 mcr p15, 0, r1, c7, c0, 0 @ invalidate whole cache v3
1315 * Various debugging routines for printing hex characters and
1316 * memory, which again must be relocatable.
1320 .type phexbuf,#object
1322 .size phexbuf, . - phexbuf
1324 @ phex corrupts {r0, r1, r2, r3}
1325 phex: adr r3, phexbuf
1339 @ puts corrupts {r0, r1, r2, r3}
1340 puts: loadsp r3, r2, r1
1341 1: ldrb r2, [r0], #1
1344 2: writeb r2, r3, r1
1354 @ putc corrupts {r0, r1, r2, r3}
1361 @ memdump corrupts {r0, r1, r2, r3, r10, r11, r12, lr}
1362 memdump: mov r12, r0
1365 2: mov r0, r11, lsl #2
1373 ldr r0, [r12, r11, lsl #2]
1393 #ifdef CONFIG_ARM_VIRT_EXT
1395 __hyp_reentry_vectors:
1398 #ifdef CONFIG_EFI_STUB
1399 W(b) __enter_kernel_from_hyp @ hvc from HYP
1405 W(b) __enter_kernel @ hyp
1408 #endif /* CONFIG_ARM_VIRT_EXT */
1411 mov r0, #0 @ must be 0
1412 mov r1, r7 @ restore architecture number
1413 mov r2, r8 @ restore atags pointer
1414 ARM( mov pc, r4 ) @ call kernel
1415 M_CLASS( add r4, r4, #1 ) @ enter in Thumb mode for M class
1416 THUMB( bx r4 ) @ entry point is always ARM for A/R classes
1420 #ifdef CONFIG_EFI_STUB
1421 __enter_kernel_from_hyp:
1422 mrc p15, 4, r0, c1, c0, 0 @ read HSCTLR
1423 bic r0, r0, #0x5 @ disable MMU and caches
1424 mcr p15, 4, r0, c1, c0, 0 @ write HSCTLR
1428 ENTRY(efi_enter_kernel)
1429 mov r4, r0 @ preserve image base
1430 mov r8, r1 @ preserve DT pointer
1432 adr_l r0, call_cache_fn
1433 adr r1, 0f @ clean the region of code we
1434 bl cache_clean_flush @ may run with the MMU off
1436 #ifdef CONFIG_ARM_VIRT_EXT
1438 @ The EFI spec does not support booting on ARM in HYP mode,
1439 @ since it mandates that the MMU and caches are on, with all
1440 @ 32-bit addressable DRAM mapped 1:1 using short descriptors.
1442 @ While the EDK2 reference implementation adheres to this,
1443 @ U-Boot might decide to enter the EFI stub in HYP mode
1444 @ anyway, with the MMU and caches either on or off.
1446 mrs r0, cpsr @ get the current mode
1447 msr spsr_cxsf, r0 @ record boot mode
1448 and r0, r0, #MODE_MASK @ are we running in HYP mode?
1452 mrc p15, 4, r1, c1, c0, 0 @ read HSCTLR
1453 tst r1, #0x1 @ MMU enabled at HYP?
1457 @ When running in HYP mode with the caches on, we're better
1458 @ off just carrying on using the cached 1:1 mapping that the
1459 @ firmware provided. Set up the HYP vectors so HVC instructions
1460 @ issued from HYP mode take us to the correct handler code. We
1461 @ will disable the MMU before jumping to the kernel proper.
1463 ARM( bic r1, r1, #(1 << 30) ) @ clear HSCTLR.TE
1464 THUMB( orr r1, r1, #(1 << 30) ) @ set HSCTLR.TE
1465 mcr p15, 4, r1, c1, c0, 0
1466 adr r0, __hyp_reentry_vectors
1467 mcr p15, 4, r0, c12, c0, 0 @ set HYP vector base (HVBAR)
1472 @ When running in HYP mode with the caches off, we need to drop
1473 @ into SVC mode now, and let the decompressor set up its cached
1474 @ 1:1 mapping as usual.
1476 1: mov r9, r4 @ preserve image base
1477 bl __hyp_stub_install @ install HYP stub vectors
1478 safe_svcmode_maskall r1 @ drop to SVC mode
1479 msr spsr_cxsf, r0 @ record boot mode
1480 orr r4, r9, #1 @ restore image base and set LSB
1484 mrc p15, 0, r0, c1, c0, 0 @ read SCTLR
1485 tst r0, #0x1 @ MMU enabled?
1486 orreq r4, r4, #1 @ set LSB if not
1489 mov r0, r8 @ DT start
1490 add r1, r8, r2 @ DT end
1491 bl cache_clean_flush
1493 adr r0, 0f @ switch to our stack
1497 mov r5, #0 @ appended DTB size
1498 mov r7, #0xFFFFFFFF @ machine ID
1500 ENDPROC(efi_enter_kernel)
1501 0: .long .L_user_stack_end - .
1505 .section ".stack", "aw", %nobits
1506 .L_user_stack: .space 4096