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 add \rb, \rb, #0x00010000 @ Ser1
73 .macro loadsp, rb, tmp1, tmp2
74 addruart \rb, \tmp1, \tmp2
92 * Debug kernel copy by printing the memory addresses involved
94 .macro dbgkc, begin, end, cbegin, cend
100 kphex \begin, 8 /* Start of compressed kernel */
104 kphex \end, 8 /* End of compressed kernel */
109 kphex \cbegin, 8 /* Start of kernel copy */
113 kphex \cend, 8 /* End of kernel copy */
119 * Debug print of the final appended DTB location
121 .macro dbgadtb, begin, size
129 kphex \begin, 8 /* Start of appended DTB */
134 kphex \size, 8 /* Size of appended DTB */
140 .macro enable_cp15_barriers, reg
141 mrc p15, 0, \reg, c1, c0, 0 @ read SCTLR
142 tst \reg, #(1 << 5) @ CP15BEN bit set?
144 orr \reg, \reg, #(1 << 5) @ CP15 barrier instructions
145 mcr p15, 0, \reg, c1, c0, 0 @ write SCTLR
146 ARM( .inst 0xf57ff06f @ v7+ isb )
152 * The kernel build system appends the size of the
153 * decompressed kernel at the end of the compressed data
154 * in little-endian form.
156 .macro get_inflated_image_size, res:req, tmp1:req, tmp2:req
157 adr \res, .Linflated_image_size_offset
159 add \tmp1, \tmp1, \res @ address of inflated image size
161 ldrb \res, [\tmp1] @ get_unaligned_le32
162 ldrb \tmp2, [\tmp1, #1]
163 orr \res, \res, \tmp2, lsl #8
164 ldrb \tmp2, [\tmp1, #2]
165 ldrb \tmp1, [\tmp1, #3]
166 orr \res, \res, \tmp2, lsl #16
167 orr \res, \res, \tmp1, lsl #24
170 .macro be32tocpu, val, tmp
172 /* convert to little endian */
177 .section ".start", "ax"
179 * sort out different calling conventions
183 * Always enter in ARM state for CPUs that support the ARM ISA.
184 * As of today (2014) that's exactly the members of the A and R
189 .type start,#function
191 * These 7 nops along with the 1 nop immediately below for
192 * !THUMB2 form 8 nops that make the compressed kernel bootable
193 * on legacy ARM systems that were assuming the kernel in a.out
194 * binary format. The boot loaders on these systems would
195 * jump 32 bytes into the image to skip the a.out header.
196 * with these 8 nops filling exactly 32 bytes, things still
197 * work as expected on these legacy systems. Thumb2 mode keeps
198 * 7 of the nops as it turns out that some boot loaders
199 * were patching the initial instructions of the kernel, i.e
200 * had started to exploit this "patch area".
206 #ifndef CONFIG_THUMB2_KERNEL
209 AR_CLASS( sub pc, pc, #3 ) @ A/R: switch to Thumb2 mode
210 M_CLASS( nop.w ) @ M: already in Thumb2 mode
215 .word _magic_sig @ Magic numbers to help the loader
216 .word _magic_start @ absolute load/run zImage address
217 .word _magic_end @ zImage end address
218 .word 0x04030201 @ endianness flag
219 .word 0x45454545 @ another magic number to indicate
220 .word _magic_table @ additional data table
224 ARM_BE8( setend be ) @ go BE8 if compiled for BE8
225 AR_CLASS( mrs r9, cpsr )
226 #ifdef CONFIG_ARM_VIRT_EXT
227 bl __hyp_stub_install @ get into SVC mode, reversibly
229 mov r7, r1 @ save architecture ID
230 mov r8, r2 @ save atags pointer
232 #ifndef CONFIG_CPU_V7M
234 * Booting from Angel - need to enter SVC mode and disable
235 * FIQs/IRQs (numeric definitions from angel arm.h source).
236 * We only do this if we were in user mode on entry.
238 mrs r2, cpsr @ get current mode
239 tst r2, #3 @ not user?
241 mov r0, #0x17 @ angel_SWIreason_EnterSVC
242 ARM( swi 0x123456 ) @ angel_SWI_ARM
243 THUMB( svc 0xab ) @ angel_SWI_THUMB
245 safe_svcmode_maskall r0
246 msr spsr_cxsf, r9 @ Save the CPU boot mode in
250 * Note that some cache flushing and other stuff may
251 * be needed here - is there an Angel SWI call for this?
255 * some architecture specific code can be inserted
256 * by the linker here, but it should preserve r7, r8, and r9.
261 #ifdef CONFIG_AUTO_ZRELADDR
263 * Find the start of physical memory. As we are executing
264 * without the MMU on, we are in the physical address space.
265 * We just need to get rid of any offset by aligning the
268 * This alignment is a balance between the requirements of
269 * different platforms - we have chosen 128MB to allow
270 * platforms which align the start of their physical memory
271 * to 128MB to use this feature, while allowing the zImage
272 * to be placed within the first 128MB of memory on other
273 * platforms. Increasing the alignment means we place
274 * stricter alignment requirements on the start of physical
275 * memory, but relaxing it means that we break people who
276 * are already placing their zImage in (eg) the top 64MB
280 and r0, r0, #0xf8000000
283 #ifdef CONFIG_ARM_APPENDED_DTB
285 * Look for an appended DTB. If found, we cannot use it to
286 * validate the calculated start of physical memory, as its
287 * memory nodes may need to be augmented by ATAGS stored at
288 * an offset from the same start of physical memory.
290 ldr r2, [r1, #4] @ get &_edata
291 add r2, r2, r1 @ relocate it
292 ldr r2, [r2] @ get DTB signature
294 cmp r2, r3 @ do we have a DTB there?
295 beq 1f @ if yes, skip validation
296 #endif /* CONFIG_ARM_APPENDED_DTB */
299 * Make sure we have some stack before calling C code.
300 * No GOT fixup has occurred yet, but none of the code we're
301 * about to call uses any global variables.
303 ldr sp, [r1] @ get stack location
304 add sp, sp, r1 @ apply relocation
306 /* Validate calculated start against passed DTB */
308 bl fdt_check_mem_start
310 #endif /* CONFIG_USE_OF */
311 /* Determine final kernel image address. */
312 add r4, r0, #TEXT_OFFSET
318 * Set up a page table only if it won't overwrite ourself.
319 * That means r4 < pc || r4 - 16k page directory > &_end.
320 * Given that r4 > &_end is most unfrequent, we add a rough
321 * additional 1MB of room for a possible appended DTB.
328 orrcc r4, r4, #1 @ remember we skipped cache_on
337 get_inflated_image_size r9, r10, lr
339 #ifndef CONFIG_ZBOOT_ROM
340 /* malloc space is above the relocated stack (64k max) */
341 add r10, sp, #MALLOC_SIZE
344 * With ZBOOT_ROM the bss/stack is non relocatable,
345 * but someone could still run this code from RAM,
346 * in which case our reference is _edata.
351 mov r5, #0 @ init dtb size to 0
352 #ifdef CONFIG_ARM_APPENDED_DTB
354 * r4 = final kernel address (possibly with LSB set)
355 * r5 = appended dtb size (still unknown)
357 * r7 = architecture ID
358 * r8 = atags/device tree pointer
359 * r9 = size of decompressed image
360 * r10 = end of this image, including bss/stack/malloc space if non XIP
363 * if there are device trees (dtb) appended to zImage, advance r10 so that the
364 * dtb data will get relocated along with the kernel if necessary.
370 bne dtb_check_done @ not found
372 #ifdef CONFIG_ARM_ATAG_DTB_COMPAT
374 * OK... Let's do some funky business here.
375 * If we do have a DTB appended to zImage, and we do have
376 * an ATAG list around, we want the later to be translated
377 * and folded into the former here. No GOT fixup has occurred
378 * yet, but none of the code we're about to call uses any
382 /* Get the initial DTB size */
386 /* 50% DTB growth should be good enough */
387 add r5, r5, r5, lsr #1
388 /* preserve 64-bit alignment */
391 /* clamp to 32KB min and 1MB max */
396 /* temporarily relocate the stack past the DTB work space */
405 * If returned value is 1, there is no ATAG at the location
406 * pointed by r8. Try the typical 0x100 offset from start
407 * of RAM and hope for the best.
410 sub r0, r4, #TEXT_OFFSET
420 mov r8, r6 @ use the appended device tree
423 * Make sure that the DTB doesn't end up in the final
424 * kernel's .bss area. To do so, we adjust the decompressed
425 * kernel size to compensate if that .bss size is larger
426 * than the relocated code.
428 ldr r5, =_kernel_bss_size
429 adr r1, wont_overwrite
434 /* Get the current DTB size */
438 /* preserve 64-bit alignment */
442 /* relocate some pointers past the appended dtb */
450 * Check to see if we will overwrite ourselves.
451 * r4 = final kernel address (possibly with LSB set)
452 * r9 = size of decompressed image
453 * r10 = end of this image, including bss/stack/malloc space if non XIP
455 * r4 - 16k page directory >= r10 -> OK
456 * r4 + image length <= address of wont_overwrite -> OK
457 * Note: the possible LSB in r4 is harmless here.
463 adr r9, wont_overwrite
468 * Relocate ourselves past the end of the decompressed kernel.
470 * r10 = end of the decompressed kernel
471 * Because we always copy ahead, we need to do it from the end and go
472 * backward in case the source and destination overlap.
475 * Bump to the next 256-byte boundary with the size of
476 * the relocation code added. This avoids overwriting
477 * ourself when the offset is small.
479 add r10, r10, #((reloc_code_end - restart + 256) & ~255)
482 /* Get start of code we want to copy and align it down. */
486 /* Relocate the hyp vector base if necessary */
487 #ifdef CONFIG_ARM_VIRT_EXT
489 and r0, r0, #MODE_MASK
494 * Compute the address of the hyp vectors after relocation.
495 * Call __hyp_set_vectors with the new address so that we
496 * can HVC again after the copy.
498 adr_l r0, __hyp_stub_vectors
505 sub r9, r6, r5 @ size to copy
506 add r9, r9, #31 @ rounded up to a multiple
507 bic r9, r9, #31 @ ... of 32 bytes
515 * We are about to copy the kernel to a new memory area.
516 * The boundaries of the new memory area can be found in
517 * r10 and r9, whilst r5 and r6 contain the boundaries
518 * of the memory we are going to copy.
519 * Calling dbgkc will help with the printing of this
522 dbgkc r5, r6, r10, r9
525 1: ldmdb r6!, {r0 - r3, r10 - r12, lr}
527 stmdb r9!, {r0 - r3, r10 - r12, lr}
530 /* Preserve offset to relocated code. */
533 mov r0, r9 @ start of relocated zImage
534 add r1, sp, r6 @ end of relocated zImage
543 ldmia r0, {r1, r2, r3, r11, r12}
544 sub r0, r0, r1 @ calculate the delta offset
547 * If delta is zero, we are running at the address we were linked at.
551 * r4 = kernel execution address (possibly with LSB set)
552 * r5 = appended dtb size (0 if not present)
553 * r7 = architecture ID
565 #ifndef CONFIG_ZBOOT_ROM
567 * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
568 * we need to fix up pointers into the BSS region.
569 * Note that the stack pointer has already been fixed up.
575 * Relocate all entries in the GOT table.
576 * Bump bss entries to _edata + dtb size
578 1: ldr r1, [r11, #0] @ relocate entries in the GOT
579 add r1, r1, r0 @ This fixes up C references
580 cmp r1, r2 @ if entry >= bss_start &&
581 cmphs r3, r1 @ bss_end > entry
582 addhi r1, r1, r5 @ entry += dtb size
583 str r1, [r11], #4 @ next entry
587 /* bump our bss pointers too */
594 * Relocate entries in the GOT table. We only relocate
595 * the entries that are outside the (relocated) BSS region.
597 1: ldr r1, [r11, #0] @ relocate entries in the GOT
598 cmp r1, r2 @ entry < bss_start ||
599 cmphs r3, r1 @ _end < entry
600 addlo r1, r1, r0 @ table. This fixes up the
601 str r1, [r11], #4 @ C references.
606 not_relocated: mov r0, #0
607 1: str r0, [r2], #4 @ clear bss
615 * Did we skip the cache setup earlier?
616 * That is indicated by the LSB in r4.
624 * The C runtime environment should now be setup sufficiently.
625 * Set up some pointers, and start decompressing.
626 * r4 = kernel execution address
627 * r7 = architecture ID
631 mov r1, sp @ malloc space above stack
632 add r2, sp, #MALLOC_SIZE @ 64k max
636 get_inflated_image_size r1, r2, r3
638 mov r0, r4 @ start of inflated image
639 add r1, r1, r0 @ end of inflated image
643 #ifdef CONFIG_ARM_VIRT_EXT
644 mrs r0, spsr @ Get saved CPU boot mode
645 and r0, r0, #MODE_MASK
646 cmp r0, #HYP_MODE @ if not booted in HYP mode...
647 bne __enter_kernel @ boot kernel directly
649 adr_l r0, __hyp_reentry_vectors
651 __HVC(0) @ otherwise bounce to hyp mode
653 b . @ should never be reached
661 .word __bss_start @ r2
663 .word _got_start @ r11
668 LC1: .word .L_user_stack_end - LC1 @ sp
669 .word _edata - LC1 @ r6
673 .word _end - restart + 16384 + 1024*1024
675 .Linflated_image_size_offset:
676 .long (input_data_end - 4) - .
678 #ifdef CONFIG_ARCH_RPC
680 params: ldr r0, =0x10000100 @ params_phys for RPC
687 * dcache_line_size - get the minimum D-cache line size from the CTR register
690 .macro dcache_line_size, reg, tmp
691 #ifdef CONFIG_CPU_V7M
692 movw \tmp, #:lower16:BASEADDR_V7M_SCB + V7M_SCB_CTR
693 movt \tmp, #:upper16:BASEADDR_V7M_SCB + V7M_SCB_CTR
696 mrc p15, 0, \tmp, c0, c0, 1 @ read ctr
699 and \tmp, \tmp, #0xf @ cache line size encoding
700 mov \reg, #4 @ bytes per word
701 mov \reg, \reg, lsl \tmp @ actual cache line size
705 * Turn on the cache. We need to setup some page tables so that we
706 * can have both the I and D caches on.
708 * We place the page tables 16k down from the kernel execution address,
709 * and we hope that nothing else is using it. If we're using it, we
713 * r4 = kernel execution address
714 * r7 = architecture number
717 * r0, r1, r2, r3, r9, r10, r12 corrupted
718 * This routine must preserve:
722 cache_on: mov r3, #8 @ cache_on function
726 * Initialize the highest priority protection region, PR7
727 * to cover all 32bit address and cacheable and bufferable.
729 __armv4_mpu_cache_on:
730 mov r0, #0x3f @ 4G, the whole
731 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
732 mcr p15, 0, r0, c6, c7, 1
735 mcr p15, 0, r0, c2, c0, 0 @ D-cache on
736 mcr p15, 0, r0, c2, c0, 1 @ I-cache on
737 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
740 mcr p15, 0, r0, c5, c0, 1 @ I-access permission
741 mcr p15, 0, r0, c5, c0, 0 @ D-access permission
744 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
745 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
746 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
747 mrc p15, 0, r0, c1, c0, 0 @ read control reg
748 @ ...I .... ..D. WC.M
749 orr r0, r0, #0x002d @ .... .... ..1. 11.1
750 orr r0, r0, #0x1000 @ ...1 .... .... ....
752 mcr p15, 0, r0, c1, c0, 0 @ write control reg
755 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
756 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
759 __armv3_mpu_cache_on:
760 mov r0, #0x3f @ 4G, the whole
761 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
764 mcr p15, 0, r0, c2, c0, 0 @ cache on
765 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
768 mcr p15, 0, r0, c5, c0, 0 @ access permission
771 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
773 * ?? ARMv3 MMU does not allow reading the control register,
774 * does this really work on ARMv3 MPU?
776 mrc p15, 0, r0, c1, c0, 0 @ read control reg
777 @ .... .... .... WC.M
778 orr r0, r0, #0x000d @ .... .... .... 11.1
779 /* ?? this overwrites the value constructed above? */
781 mcr p15, 0, r0, c1, c0, 0 @ write control reg
783 /* ?? invalidate for the second time? */
784 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
787 #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
793 __setup_mmu: sub r3, r4, #16384 @ Page directory size
794 bic r3, r3, #0xff @ Align the pointer
797 * Initialise the page tables, turning on the cacheable and bufferable
798 * bits for the RAM area only.
802 mov r9, r9, lsl #18 @ start of RAM
803 add r10, r9, #0x10000000 @ a reasonable RAM size
804 mov r1, #0x12 @ XN|U + section mapping
805 orr r1, r1, #3 << 10 @ AP=11
807 1: cmp r1, r9 @ if virt > start of RAM
808 cmphs r10, r1 @ && end of RAM > virt
809 bic r1, r1, #0x1c @ clear XN|U + C + B
810 orrlo r1, r1, #0x10 @ Set XN|U for non-RAM
811 orrhs r1, r1, r6 @ set RAM section settings
812 str r1, [r0], #4 @ 1:1 mapping
817 * If ever we are running from Flash, then we surely want the cache
818 * to be enabled also for our execution instance... We map 2MB of it
819 * so there is no map overlap problem for up to 1 MB compressed kernel.
820 * If the execution is in RAM then we would only be duplicating the above.
822 orr r1, r6, #0x04 @ ensure B is set for this
826 orr r1, r1, r2, lsl #20
827 add r0, r3, r2, lsl #2
834 @ Enable unaligned access on v6, to allow better code generation
835 @ for the decompressor C code:
836 __armv6_mmu_cache_on:
837 mrc p15, 0, r0, c1, c0, 0 @ read SCTLR
838 bic r0, r0, #2 @ A (no unaligned access fault)
839 orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
840 mcr p15, 0, r0, c1, c0, 0 @ write SCTLR
841 b __armv4_mmu_cache_on
843 __arm926ejs_mmu_cache_on:
844 #ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
845 mov r0, #4 @ put dcache in WT mode
846 mcr p15, 7, r0, c15, c0, 0
849 __armv4_mmu_cache_on:
852 mov r6, #CB_BITS | 0x12 @ U
855 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
856 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
857 mrc p15, 0, r0, c1, c0, 0 @ read control reg
858 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
860 ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
861 bl __common_mmu_cache_on
863 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
867 __armv7_mmu_cache_on:
868 enable_cp15_barriers r11
871 mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
873 movne r6, #CB_BITS | 0x02 @ !XN
876 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
878 mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
880 mrc p15, 0, r0, c1, c0, 0 @ read control reg
881 bic r0, r0, #1 << 28 @ clear SCTLR.TRE
882 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
883 orr r0, r0, #0x003c @ write buffer
884 bic r0, r0, #2 @ A (no unaligned access fault)
885 orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
886 @ (needed for ARM1176)
888 ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
889 mrcne p15, 0, r6, c2, c0, 2 @ read ttb control reg
890 orrne r0, r0, #1 @ MMU enabled
891 movne r1, #0xfffffffd @ domain 0 = client
892 bic r6, r6, #1 << 31 @ 32-bit translation system
893 bic r6, r6, #(7 << 0) | (1 << 4) @ use only ttbr0
894 mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
895 mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
896 mcrne p15, 0, r6, c2, c0, 2 @ load ttb control
898 mcr p15, 0, r0, c7, c5, 4 @ ISB
899 mcr p15, 0, r0, c1, c0, 0 @ load control register
900 mrc p15, 0, r0, c1, c0, 0 @ and read it back
902 mcr p15, 0, r0, c7, c5, 4 @ ISB
907 mov r6, #CB_BITS | 0x12 @ U
910 mcr p15, 0, r0, c7, c7, 0 @ Invalidate whole cache
911 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
912 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
913 mrc p15, 0, r0, c1, c0, 0 @ read control reg
914 orr r0, r0, #0x1000 @ I-cache enable
915 bl __common_mmu_cache_on
917 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
920 __common_mmu_cache_on:
921 #ifndef CONFIG_THUMB2_KERNEL
923 orr r0, r0, #0x000d @ Write buffer, mmu
926 mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
927 mcr p15, 0, r1, c3, c0, 0 @ load domain access control
929 .align 5 @ cache line aligned
930 1: mcr p15, 0, r0, c1, c0, 0 @ load control register
931 mrc p15, 0, r0, c1, c0, 0 @ and read it back to
932 sub pc, lr, r0, lsr #32 @ properly flush pipeline
935 #define PROC_ENTRY_SIZE (4*5)
938 * Here follow the relocatable cache support functions for the
939 * various processors. This is a generic hook for locating an
940 * entry and jumping to an instruction at the specified offset
941 * from the start of the block. Please note this is all position
951 call_cache_fn: adr r12, proc_types
952 #ifdef CONFIG_CPU_CP15
953 mrc p15, 0, r9, c0, c0 @ get processor ID
954 #elif defined(CONFIG_CPU_V7M)
956 * On v7-M the processor id is located in the V7M_SCB_CPUID
957 * register, but as cache handling is IMPLEMENTATION DEFINED on
958 * v7-M (if existant at all) we just return early here.
959 * If V7M_SCB_CPUID were used the cpu ID functions (i.e.
960 * __armv7_mmu_cache_{on,off,flush}) would be selected which
961 * use cp15 registers that are not implemented on v7-M.
965 ldr r9, =CONFIG_PROCESSOR_ID
967 1: ldr r1, [r12, #0] @ get value
968 ldr r2, [r12, #4] @ get mask
969 eor r1, r1, r9 @ (real ^ match)
971 ARM( addeq pc, r12, r3 ) @ call cache function
972 THUMB( addeq r12, r3 )
973 THUMB( moveq pc, r12 ) @ call cache function
974 add r12, r12, #PROC_ENTRY_SIZE
978 * Table for cache operations. This is basically:
981 * - 'cache on' method instruction
982 * - 'cache off' method instruction
983 * - 'cache flush' method instruction
985 * We match an entry using: ((real_id ^ match) & mask) == 0
987 * Writethrough caches generally only need 'on' and 'off'
988 * methods. Writeback caches _must_ have the flush method
992 .type proc_types,#object
994 .word 0x41000000 @ old ARM ID
1003 .word 0x41007000 @ ARM7/710
1012 .word 0x41807200 @ ARM720T (writethrough)
1014 W(b) __armv4_mmu_cache_on
1015 W(b) __armv4_mmu_cache_off
1019 .word 0x41007400 @ ARM74x
1021 W(b) __armv3_mpu_cache_on
1022 W(b) __armv3_mpu_cache_off
1023 W(b) __armv3_mpu_cache_flush
1025 .word 0x41009400 @ ARM94x
1027 W(b) __armv4_mpu_cache_on
1028 W(b) __armv4_mpu_cache_off
1029 W(b) __armv4_mpu_cache_flush
1031 .word 0x41069260 @ ARM926EJ-S (v5TEJ)
1033 W(b) __arm926ejs_mmu_cache_on
1034 W(b) __armv4_mmu_cache_off
1035 W(b) __armv5tej_mmu_cache_flush
1037 .word 0x00007000 @ ARM7 IDs
1046 @ Everything from here on will be the new ID system.
1048 .word 0x4401a100 @ sa110 / sa1100
1050 W(b) __armv4_mmu_cache_on
1051 W(b) __armv4_mmu_cache_off
1052 W(b) __armv4_mmu_cache_flush
1054 .word 0x6901b110 @ sa1110
1056 W(b) __armv4_mmu_cache_on
1057 W(b) __armv4_mmu_cache_off
1058 W(b) __armv4_mmu_cache_flush
1061 .word 0xffffff00 @ PXA9xx
1062 W(b) __armv4_mmu_cache_on
1063 W(b) __armv4_mmu_cache_off
1064 W(b) __armv4_mmu_cache_flush
1066 .word 0x56158000 @ PXA168
1068 W(b) __armv4_mmu_cache_on
1069 W(b) __armv4_mmu_cache_off
1070 W(b) __armv5tej_mmu_cache_flush
1072 .word 0x56050000 @ Feroceon
1074 W(b) __armv4_mmu_cache_on
1075 W(b) __armv4_mmu_cache_off
1076 W(b) __armv5tej_mmu_cache_flush
1078 #ifdef CONFIG_CPU_FEROCEON_OLD_ID
1079 /* this conflicts with the standard ARMv5TE entry */
1080 .long 0x41009260 @ Old Feroceon
1082 b __armv4_mmu_cache_on
1083 b __armv4_mmu_cache_off
1084 b __armv5tej_mmu_cache_flush
1087 .word 0x66015261 @ FA526
1089 W(b) __fa526_cache_on
1090 W(b) __armv4_mmu_cache_off
1091 W(b) __fa526_cache_flush
1093 @ These match on the architecture ID
1095 .word 0x00020000 @ ARMv4T
1097 W(b) __armv4_mmu_cache_on
1098 W(b) __armv4_mmu_cache_off
1099 W(b) __armv4_mmu_cache_flush
1101 .word 0x00050000 @ ARMv5TE
1103 W(b) __armv4_mmu_cache_on
1104 W(b) __armv4_mmu_cache_off
1105 W(b) __armv4_mmu_cache_flush
1107 .word 0x00060000 @ ARMv5TEJ
1109 W(b) __armv4_mmu_cache_on
1110 W(b) __armv4_mmu_cache_off
1111 W(b) __armv5tej_mmu_cache_flush
1113 .word 0x0007b000 @ ARMv6
1115 W(b) __armv6_mmu_cache_on
1116 W(b) __armv4_mmu_cache_off
1117 W(b) __armv6_mmu_cache_flush
1119 .word 0x000f0000 @ new CPU Id
1121 W(b) __armv7_mmu_cache_on
1122 W(b) __armv7_mmu_cache_off
1123 W(b) __armv7_mmu_cache_flush
1125 .word 0 @ unrecognised type
1134 .size proc_types, . - proc_types
1137 * If you get a "non-constant expression in ".if" statement"
1138 * error from the assembler on this line, check that you have
1139 * not accidentally written a "b" instruction where you should
1140 * have written W(b).
1142 .if (. - proc_types) % PROC_ENTRY_SIZE != 0
1143 .error "The size of one or more proc_types entries is wrong."
1147 * Turn off the Cache and MMU. ARMv3 does not support
1148 * reading the control register, but ARMv4 does.
1151 * r0, r1, r2, r3, r9, r12 corrupted
1152 * This routine must preserve:
1156 cache_off: mov r3, #12 @ cache_off function
1159 __armv4_mpu_cache_off:
1160 mrc p15, 0, r0, c1, c0
1162 mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
1164 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
1165 mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
1166 mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
1169 __armv3_mpu_cache_off:
1170 mrc p15, 0, r0, c1, c0
1172 mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
1174 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
1177 __armv4_mmu_cache_off:
1179 mrc p15, 0, r0, c1, c0
1181 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
1183 mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
1184 mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
1188 __armv7_mmu_cache_off:
1189 mrc p15, 0, r0, c1, c0
1195 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
1198 mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
1200 mcr p15, 0, r0, c7, c5, 6 @ invalidate BTC
1201 mcr p15, 0, r0, c7, c10, 4 @ DSB
1202 mcr p15, 0, r0, c7, c5, 4 @ ISB
1206 * Clean and flush the cache to maintain consistency.
1209 * r0 = start address
1210 * r1 = end address (exclusive)
1212 * r1, r2, r3, r9, r10, r11, r12 corrupted
1213 * This routine must preserve:
1222 __armv4_mpu_cache_flush:
1227 mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
1228 mov r1, #7 << 5 @ 8 segments
1229 1: orr r3, r1, #63 << 26 @ 64 entries
1230 2: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
1231 subs r3, r3, #1 << 26
1232 bcs 2b @ entries 63 to 0
1233 subs r1, r1, #1 << 5
1234 bcs 1b @ segments 7 to 0
1237 mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
1238 mcr p15, 0, ip, c7, c10, 4 @ drain WB
1241 __fa526_cache_flush:
1245 mcr p15, 0, r1, c7, c14, 0 @ clean and invalidate D cache
1246 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1247 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1250 __armv6_mmu_cache_flush:
1253 mcreq p15, 0, r1, c7, c14, 0 @ clean+invalidate D
1254 mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
1255 mcreq p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
1256 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1259 __armv7_mmu_cache_flush:
1260 enable_cp15_barriers r10
1263 mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
1264 tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
1267 mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
1270 dcache_line_size r1, r2 @ r1 := dcache min line size
1271 sub r2, r1, #1 @ r2 := line size mask
1272 bic r0, r0, r2 @ round down start to line size
1273 sub r11, r11, #1 @ end address is exclusive
1274 bic r11, r11, r2 @ round down end to line size
1275 0: cmp r0, r11 @ finished?
1277 mcr p15, 0, r0, c7, c14, 1 @ Dcache clean/invalidate by VA
1281 mcr p15, 0, r10, c7, c10, 4 @ DSB
1282 mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
1283 mcr p15, 0, r10, c7, c10, 4 @ DSB
1284 mcr p15, 0, r10, c7, c5, 4 @ ISB
1287 __armv5tej_mmu_cache_flush:
1290 1: mrc p15, 0, APSR_nzcv, c7, c14, 3 @ test,clean,invalidate D cache
1292 mcr p15, 0, r0, c7, c5, 0 @ flush I cache
1293 mcr p15, 0, r0, c7, c10, 4 @ drain WB
1296 __armv4_mmu_cache_flush:
1299 mov r2, #64*1024 @ default: 32K dcache size (*2)
1300 mov r11, #32 @ default: 32 byte line size
1301 mrc p15, 0, r3, c0, c0, 1 @ read cache type
1302 teq r3, r9 @ cache ID register present?
1307 mov r2, r2, lsl r1 @ base dcache size *2
1308 tst r3, #1 << 14 @ test M bit
1309 addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
1313 mov r11, r11, lsl r3 @ cache line size in bytes
1316 bic r1, r1, #63 @ align to longest cache line
1319 ARM( ldr r3, [r1], r11 ) @ s/w flush D cache
1320 THUMB( ldr r3, [r1] ) @ s/w flush D cache
1321 THUMB( add r1, r1, r11 )
1325 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1326 mcr p15, 0, r1, c7, c6, 0 @ flush D cache
1327 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1330 __armv3_mmu_cache_flush:
1331 __armv3_mpu_cache_flush:
1335 mcr p15, 0, r1, c7, c0, 0 @ invalidate whole cache v3
1339 * Various debugging routines for printing hex characters and
1340 * memory, which again must be relocatable.
1344 .type phexbuf,#object
1346 .size phexbuf, . - phexbuf
1348 @ phex corrupts {r0, r1, r2, r3}
1349 phex: adr r3, phexbuf
1363 @ puts corrupts {r0, r1, r2, r3}
1364 puts: loadsp r3, r2, r1
1365 1: ldrb r2, [r0], #1
1368 2: writeb r2, r3, r1
1378 @ putc corrupts {r0, r1, r2, r3}
1385 @ memdump corrupts {r0, r1, r2, r3, r10, r11, r12, lr}
1386 memdump: mov r12, r0
1389 2: mov r0, r11, lsl #2
1397 ldr r0, [r12, r11, lsl #2]
1417 #ifdef CONFIG_ARM_VIRT_EXT
1419 __hyp_reentry_vectors:
1422 #ifdef CONFIG_EFI_STUB
1423 W(b) __enter_kernel_from_hyp @ hvc from HYP
1429 W(b) __enter_kernel @ hyp
1432 #endif /* CONFIG_ARM_VIRT_EXT */
1435 mov r0, #0 @ must be 0
1436 mov r1, r7 @ restore architecture number
1437 mov r2, r8 @ restore atags pointer
1438 ARM( mov pc, r4 ) @ call kernel
1439 M_CLASS( add r4, r4, #1 ) @ enter in Thumb mode for M class
1440 THUMB( bx r4 ) @ entry point is always ARM for A/R classes
1444 #ifdef CONFIG_EFI_STUB
1445 __enter_kernel_from_hyp:
1446 mrc p15, 4, r0, c1, c0, 0 @ read HSCTLR
1447 bic r0, r0, #0x5 @ disable MMU and caches
1448 mcr p15, 4, r0, c1, c0, 0 @ write HSCTLR
1452 ENTRY(efi_enter_kernel)
1453 mov r4, r0 @ preserve image base
1454 mov r8, r1 @ preserve DT pointer
1456 adr_l r0, call_cache_fn
1457 adr r1, 0f @ clean the region of code we
1458 bl cache_clean_flush @ may run with the MMU off
1460 #ifdef CONFIG_ARM_VIRT_EXT
1462 @ The EFI spec does not support booting on ARM in HYP mode,
1463 @ since it mandates that the MMU and caches are on, with all
1464 @ 32-bit addressable DRAM mapped 1:1 using short descriptors.
1466 @ While the EDK2 reference implementation adheres to this,
1467 @ U-Boot might decide to enter the EFI stub in HYP mode
1468 @ anyway, with the MMU and caches either on or off.
1470 mrs r0, cpsr @ get the current mode
1471 msr spsr_cxsf, r0 @ record boot mode
1472 and r0, r0, #MODE_MASK @ are we running in HYP mode?
1476 mrc p15, 4, r1, c1, c0, 0 @ read HSCTLR
1477 tst r1, #0x1 @ MMU enabled at HYP?
1481 @ When running in HYP mode with the caches on, we're better
1482 @ off just carrying on using the cached 1:1 mapping that the
1483 @ firmware provided. Set up the HYP vectors so HVC instructions
1484 @ issued from HYP mode take us to the correct handler code. We
1485 @ will disable the MMU before jumping to the kernel proper.
1487 ARM( bic r1, r1, #(1 << 30) ) @ clear HSCTLR.TE
1488 THUMB( orr r1, r1, #(1 << 30) ) @ set HSCTLR.TE
1489 mcr p15, 4, r1, c1, c0, 0
1490 adr r0, __hyp_reentry_vectors
1491 mcr p15, 4, r0, c12, c0, 0 @ set HYP vector base (HVBAR)
1496 @ When running in HYP mode with the caches off, we need to drop
1497 @ into SVC mode now, and let the decompressor set up its cached
1498 @ 1:1 mapping as usual.
1500 1: mov r9, r4 @ preserve image base
1501 bl __hyp_stub_install @ install HYP stub vectors
1502 safe_svcmode_maskall r1 @ drop to SVC mode
1503 msr spsr_cxsf, r0 @ record boot mode
1504 orr r4, r9, #1 @ restore image base and set LSB
1508 mrc p15, 0, r0, c1, c0, 0 @ read SCTLR
1509 tst r0, #0x1 @ MMU enabled?
1510 orreq r4, r4, #1 @ set LSB if not
1513 mov r0, r8 @ DT start
1514 add r1, r8, r2 @ DT end
1515 bl cache_clean_flush
1517 adr r0, 0f @ switch to our stack
1521 mov r5, #0 @ appended DTB size
1522 mov r7, #0xFFFFFFFF @ machine ID
1524 ENDPROC(efi_enter_kernel)
1525 0: .long .L_user_stack_end - .
1529 .section ".stack", "aw", %nobits
1530 .L_user_stack: .space 4096