blob: 7a38c63b62bf005e782951037868e8415494fc83 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * linux/arch/arm/boot/compressed/head.S
4 *
5 * Copyright (C) 1996-2002 Russell King
6 * Copyright (C) 2004 Hyok S. Choi (MPU support)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8#include <linux/linkage.h>
9#include <asm/assembler.h>
10#include <asm/v7m.h>
11
12#include "efi-header.S"
13
14 AR_CLASS( .arch armv7-a )
15 M_CLASS( .arch armv7-m )
16
17/*
18 * Debugging stuff
19 *
20 * Note that these macros must not contain any code which is not
21 * 100% relocatable. Any attempt to do so will result in a crash.
22 * Please select one of the following when turning on debugging.
23 */
24#ifdef DEBUG
25
26#if defined(CONFIG_DEBUG_ICEDCC)
27
28#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
29 .macro loadsp, rb, tmp1, tmp2
30 .endm
Olivier Deprez157378f2022-04-04 15:47:50 +020031 .macro writeb, ch, rb, tmp
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032 mcr p14, 0, \ch, c0, c5, 0
33 .endm
34#elif defined(CONFIG_CPU_XSCALE)
35 .macro loadsp, rb, tmp1, tmp2
36 .endm
Olivier Deprez157378f2022-04-04 15:47:50 +020037 .macro writeb, ch, rb, tmp
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 mcr p14, 0, \ch, c8, c0, 0
39 .endm
40#else
41 .macro loadsp, rb, tmp1, tmp2
42 .endm
Olivier Deprez157378f2022-04-04 15:47:50 +020043 .macro writeb, ch, rb, tmp
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 mcr p14, 0, \ch, c1, c0, 0
45 .endm
46#endif
47
48#else
49
50#include CONFIG_DEBUG_LL_INCLUDE
51
Olivier Deprez157378f2022-04-04 15:47:50 +020052 .macro writeb, ch, rb, tmp
53#ifdef CONFIG_DEBUG_UART_FLOW_CONTROL
54 waituartcts \tmp, \rb
55#endif
56 waituarttxrdy \tmp, \rb
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057 senduart \ch, \rb
Olivier Deprez157378f2022-04-04 15:47:50 +020058 busyuart \tmp, \rb
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059 .endm
60
61#if defined(CONFIG_ARCH_SA1100)
62 .macro loadsp, rb, tmp1, tmp2
63 mov \rb, #0x80000000 @ physical base address
64#ifdef CONFIG_DEBUG_LL_SER3
65 add \rb, \rb, #0x00050000 @ Ser3
66#else
67 add \rb, \rb, #0x00010000 @ Ser1
68#endif
69 .endm
70#else
71 .macro loadsp, rb, tmp1, tmp2
72 addruart \rb, \tmp1, \tmp2
73 .endm
74#endif
75#endif
76#endif
77
78 .macro kputc,val
79 mov r0, \val
80 bl putc
81 .endm
82
83 .macro kphex,val,len
84 mov r0, \val
85 mov r1, #\len
86 bl phex
87 .endm
88
David Brazdil0f672f62019-12-10 10:32:29 +000089 /*
90 * Debug kernel copy by printing the memory addresses involved
91 */
92 .macro dbgkc, begin, end, cbegin, cend
93#ifdef DEBUG
David Brazdil0f672f62019-12-10 10:32:29 +000094 kputc #'C'
95 kputc #':'
96 kputc #'0'
97 kputc #'x'
98 kphex \begin, 8 /* Start of compressed kernel */
99 kputc #'-'
100 kputc #'0'
101 kputc #'x'
102 kphex \end, 8 /* End of compressed kernel */
103 kputc #'-'
104 kputc #'>'
105 kputc #'0'
106 kputc #'x'
107 kphex \cbegin, 8 /* Start of kernel copy */
108 kputc #'-'
109 kputc #'0'
110 kputc #'x'
111 kphex \cend, 8 /* End of kernel copy */
112 kputc #'\n'
David Brazdil0f672f62019-12-10 10:32:29 +0000113#endif
114 .endm
115
Olivier Deprez157378f2022-04-04 15:47:50 +0200116 /*
117 * Debug print of the final appended DTB location
118 */
119 .macro dbgadtb, begin, size
120#ifdef DEBUG
121 kputc #'D'
122 kputc #'T'
123 kputc #'B'
124 kputc #':'
125 kputc #'0'
126 kputc #'x'
127 kphex \begin, 8 /* Start of appended DTB */
128 kputc #' '
129 kputc #'('
130 kputc #'0'
131 kputc #'x'
132 kphex \size, 8 /* Size of appended DTB */
133 kputc #')'
134 kputc #'\n'
135#endif
136 .endm
137
138 .macro enable_cp15_barriers, reg
139 mrc p15, 0, \reg, c1, c0, 0 @ read SCTLR
140 tst \reg, #(1 << 5) @ CP15BEN bit set?
141 bne .L_\@
142 orr \reg, \reg, #(1 << 5) @ CP15 barrier instructions
143 mcr p15, 0, \reg, c1, c0, 0 @ write SCTLR
144 ARM( .inst 0xf57ff06f @ v7+ isb )
145 THUMB( isb )
146.L_\@:
147 .endm
148
149 /*
150 * The kernel build system appends the size of the
151 * decompressed kernel at the end of the compressed data
152 * in little-endian form.
153 */
154 .macro get_inflated_image_size, res:req, tmp1:req, tmp2:req
155 adr \res, .Linflated_image_size_offset
156 ldr \tmp1, [\res]
157 add \tmp1, \tmp1, \res @ address of inflated image size
158
159 ldrb \res, [\tmp1] @ get_unaligned_le32
160 ldrb \tmp2, [\tmp1, #1]
161 orr \res, \res, \tmp2, lsl #8
162 ldrb \tmp2, [\tmp1, #2]
163 ldrb \tmp1, [\tmp1, #3]
164 orr \res, \res, \tmp2, lsl #16
165 orr \res, \res, \tmp1, lsl #24
166 .endm
167
168 .section ".start", "ax"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169/*
170 * sort out different calling conventions
171 */
172 .align
173 /*
174 * Always enter in ARM state for CPUs that support the ARM ISA.
175 * As of today (2014) that's exactly the members of the A and R
176 * classes.
177 */
178 AR_CLASS( .arm )
179start:
180 .type start,#function
David Brazdil0f672f62019-12-10 10:32:29 +0000181 /*
182 * These 7 nops along with the 1 nop immediately below for
183 * !THUMB2 form 8 nops that make the compressed kernel bootable
184 * on legacy ARM systems that were assuming the kernel in a.out
185 * binary format. The boot loaders on these systems would
186 * jump 32 bytes into the image to skip the a.out header.
187 * with these 8 nops filling exactly 32 bytes, things still
188 * work as expected on these legacy systems. Thumb2 mode keeps
189 * 7 of the nops as it turns out that some boot loaders
190 * were patching the initial instructions of the kernel, i.e
191 * had started to exploit this "patch area".
192 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200193 __initial_nops
194 .rept 5
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195 __nop
196 .endr
197#ifndef CONFIG_THUMB2_KERNEL
David Brazdil0f672f62019-12-10 10:32:29 +0000198 __nop
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199#else
200 AR_CLASS( sub pc, pc, #3 ) @ A/R: switch to Thumb2 mode
201 M_CLASS( nop.w ) @ M: already in Thumb2 mode
202 .thumb
203#endif
204 W(b) 1f
205
206 .word _magic_sig @ Magic numbers to help the loader
207 .word _magic_start @ absolute load/run zImage address
208 .word _magic_end @ zImage end address
209 .word 0x04030201 @ endianness flag
210 .word 0x45454545 @ another magic number to indicate
211 .word _magic_table @ additional data table
212
213 __EFI_HEADER
2141:
215 ARM_BE8( setend be ) @ go BE8 if compiled for BE8
216 AR_CLASS( mrs r9, cpsr )
217#ifdef CONFIG_ARM_VIRT_EXT
218 bl __hyp_stub_install @ get into SVC mode, reversibly
219#endif
220 mov r7, r1 @ save architecture ID
221 mov r8, r2 @ save atags pointer
222
223#ifndef CONFIG_CPU_V7M
224 /*
225 * Booting from Angel - need to enter SVC mode and disable
226 * FIQs/IRQs (numeric definitions from angel arm.h source).
227 * We only do this if we were in user mode on entry.
228 */
229 mrs r2, cpsr @ get current mode
230 tst r2, #3 @ not user?
231 bne not_angel
232 mov r0, #0x17 @ angel_SWIreason_EnterSVC
233 ARM( swi 0x123456 ) @ angel_SWI_ARM
234 THUMB( svc 0xab ) @ angel_SWI_THUMB
235not_angel:
236 safe_svcmode_maskall r0
237 msr spsr_cxsf, r9 @ Save the CPU boot mode in
238 @ SPSR
239#endif
240 /*
241 * Note that some cache flushing and other stuff may
242 * be needed here - is there an Angel SWI call for this?
243 */
244
245 /*
246 * some architecture specific code can be inserted
247 * by the linker here, but it should preserve r7, r8, and r9.
248 */
249
250 .text
251
252#ifdef CONFIG_AUTO_ZRELADDR
253 /*
254 * Find the start of physical memory. As we are executing
255 * without the MMU on, we are in the physical address space.
256 * We just need to get rid of any offset by aligning the
257 * address.
258 *
259 * This alignment is a balance between the requirements of
260 * different platforms - we have chosen 128MB to allow
261 * platforms which align the start of their physical memory
262 * to 128MB to use this feature, while allowing the zImage
263 * to be placed within the first 128MB of memory on other
264 * platforms. Increasing the alignment means we place
265 * stricter alignment requirements on the start of physical
266 * memory, but relaxing it means that we break people who
267 * are already placing their zImage in (eg) the top 64MB
268 * of this range.
269 */
270 mov r4, pc
271 and r4, r4, #0xf8000000
272 /* Determine final kernel image address. */
273 add r4, r4, #TEXT_OFFSET
274#else
275 ldr r4, =zreladdr
276#endif
277
278 /*
279 * Set up a page table only if it won't overwrite ourself.
280 * That means r4 < pc || r4 - 16k page directory > &_end.
281 * Given that r4 > &_end is most unfrequent, we add a rough
282 * additional 1MB of room for a possible appended DTB.
283 */
284 mov r0, pc
285 cmp r0, r4
Olivier Deprez157378f2022-04-04 15:47:50 +0200286 ldrcc r0, .Lheadroom
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000287 addcc r0, r0, pc
288 cmpcc r4, r0
289 orrcc r4, r4, #1 @ remember we skipped cache_on
290 blcs cache_on
291
Olivier Deprez157378f2022-04-04 15:47:50 +0200292restart: adr r0, LC1
293 ldr sp, [r0]
294 ldr r6, [r0, #4]
295 add sp, sp, r0
296 add r6, r6, r0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297
Olivier Deprez157378f2022-04-04 15:47:50 +0200298 get_inflated_image_size r9, r10, lr
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299
300#ifndef CONFIG_ZBOOT_ROM
301 /* malloc space is above the relocated stack (64k max) */
Olivier Deprez157378f2022-04-04 15:47:50 +0200302 add r10, sp, #MALLOC_SIZE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303#else
304 /*
305 * With ZBOOT_ROM the bss/stack is non relocatable,
306 * but someone could still run this code from RAM,
307 * in which case our reference is _edata.
308 */
309 mov r10, r6
310#endif
311
312 mov r5, #0 @ init dtb size to 0
313#ifdef CONFIG_ARM_APPENDED_DTB
314/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 * r4 = final kernel address (possibly with LSB set)
316 * r5 = appended dtb size (still unknown)
317 * r6 = _edata
318 * r7 = architecture ID
319 * r8 = atags/device tree pointer
320 * r9 = size of decompressed image
321 * r10 = end of this image, including bss/stack/malloc space if non XIP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 * sp = stack pointer
323 *
324 * if there are device trees (dtb) appended to zImage, advance r10 so that the
325 * dtb data will get relocated along with the kernel if necessary.
326 */
327
328 ldr lr, [r6, #0]
329#ifndef __ARMEB__
330 ldr r1, =0xedfe0dd0 @ sig is 0xd00dfeed big endian
331#else
332 ldr r1, =0xd00dfeed
333#endif
334 cmp lr, r1
335 bne dtb_check_done @ not found
336
337#ifdef CONFIG_ARM_ATAG_DTB_COMPAT
338 /*
339 * OK... Let's do some funky business here.
340 * If we do have a DTB appended to zImage, and we do have
341 * an ATAG list around, we want the later to be translated
342 * and folded into the former here. No GOT fixup has occurred
343 * yet, but none of the code we're about to call uses any
344 * global variable.
345 */
346
347 /* Get the initial DTB size */
348 ldr r5, [r6, #4]
349#ifndef __ARMEB__
350 /* convert to little endian */
351 eor r1, r5, r5, ror #16
352 bic r1, r1, #0x00ff0000
353 mov r5, r5, ror #8
354 eor r5, r5, r1, lsr #8
355#endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200356 dbgadtb r6, r5
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000357 /* 50% DTB growth should be good enough */
358 add r5, r5, r5, lsr #1
359 /* preserve 64-bit alignment */
360 add r5, r5, #7
361 bic r5, r5, #7
362 /* clamp to 32KB min and 1MB max */
363 cmp r5, #(1 << 15)
364 movlo r5, #(1 << 15)
365 cmp r5, #(1 << 20)
366 movhi r5, #(1 << 20)
367 /* temporarily relocate the stack past the DTB work space */
368 add sp, sp, r5
369
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370 mov r0, r8
371 mov r1, r6
372 mov r2, r5
373 bl atags_to_fdt
374
375 /*
376 * If returned value is 1, there is no ATAG at the location
377 * pointed by r8. Try the typical 0x100 offset from start
378 * of RAM and hope for the best.
379 */
380 cmp r0, #1
381 sub r0, r4, #TEXT_OFFSET
382 bic r0, r0, #1
383 add r0, r0, #0x100
384 mov r1, r6
385 mov r2, r5
386 bleq atags_to_fdt
387
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388 sub sp, sp, r5
389#endif
390
391 mov r8, r6 @ use the appended device tree
392
393 /*
394 * Make sure that the DTB doesn't end up in the final
395 * kernel's .bss area. To do so, we adjust the decompressed
396 * kernel size to compensate if that .bss size is larger
397 * than the relocated code.
398 */
399 ldr r5, =_kernel_bss_size
400 adr r1, wont_overwrite
401 sub r1, r6, r1
402 subs r1, r5, r1
403 addhi r9, r9, r1
404
405 /* Get the current DTB size */
406 ldr r5, [r6, #4]
407#ifndef __ARMEB__
408 /* convert r5 (dtb size) to little endian */
409 eor r1, r5, r5, ror #16
410 bic r1, r1, #0x00ff0000
411 mov r5, r5, ror #8
412 eor r5, r5, r1, lsr #8
413#endif
414
415 /* preserve 64-bit alignment */
416 add r5, r5, #7
417 bic r5, r5, #7
418
419 /* relocate some pointers past the appended dtb */
420 add r6, r6, r5
421 add r10, r10, r5
422 add sp, sp, r5
423dtb_check_done:
424#endif
425
426/*
427 * Check to see if we will overwrite ourselves.
428 * r4 = final kernel address (possibly with LSB set)
429 * r9 = size of decompressed image
430 * r10 = end of this image, including bss/stack/malloc space if non XIP
431 * We basically want:
432 * r4 - 16k page directory >= r10 -> OK
433 * r4 + image length <= address of wont_overwrite -> OK
434 * Note: the possible LSB in r4 is harmless here.
435 */
436 add r10, r10, #16384
437 cmp r4, r10
438 bhs wont_overwrite
439 add r10, r4, r9
440 adr r9, wont_overwrite
441 cmp r10, r9
442 bls wont_overwrite
443
444/*
445 * Relocate ourselves past the end of the decompressed kernel.
446 * r6 = _edata
447 * r10 = end of the decompressed kernel
448 * Because we always copy ahead, we need to do it from the end and go
449 * backward in case the source and destination overlap.
450 */
451 /*
452 * Bump to the next 256-byte boundary with the size of
453 * the relocation code added. This avoids overwriting
454 * ourself when the offset is small.
455 */
456 add r10, r10, #((reloc_code_end - restart + 256) & ~255)
457 bic r10, r10, #255
458
459 /* Get start of code we want to copy and align it down. */
460 adr r5, restart
461 bic r5, r5, #31
462
463/* Relocate the hyp vector base if necessary */
464#ifdef CONFIG_ARM_VIRT_EXT
465 mrs r0, spsr
466 and r0, r0, #MODE_MASK
467 cmp r0, #HYP_MODE
468 bne 1f
469
470 /*
471 * Compute the address of the hyp vectors after relocation.
472 * This requires some arithmetic since we cannot directly
473 * reference __hyp_stub_vectors in a PC-relative way.
474 * Call __hyp_set_vectors with the new address so that we
475 * can HVC again after the copy.
476 */
4770: adr r0, 0b
478 movw r1, #:lower16:__hyp_stub_vectors - 0b
479 movt r1, #:upper16:__hyp_stub_vectors - 0b
480 add r0, r0, r1
481 sub r0, r0, r5
482 add r0, r0, r10
483 bl __hyp_set_vectors
4841:
485#endif
486
487 sub r9, r6, r5 @ size to copy
488 add r9, r9, #31 @ rounded up to a multiple
489 bic r9, r9, #31 @ ... of 32 bytes
490 add r6, r9, r5
491 add r9, r9, r10
492
David Brazdil0f672f62019-12-10 10:32:29 +0000493#ifdef DEBUG
494 sub r10, r6, r5
495 sub r10, r9, r10
496 /*
497 * We are about to copy the kernel to a new memory area.
498 * The boundaries of the new memory area can be found in
499 * r10 and r9, whilst r5 and r6 contain the boundaries
500 * of the memory we are going to copy.
501 * Calling dbgkc will help with the printing of this
502 * information.
503 */
504 dbgkc r5, r6, r10, r9
505#endif
506
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005071: ldmdb r6!, {r0 - r3, r10 - r12, lr}
508 cmp r6, r5
509 stmdb r9!, {r0 - r3, r10 - r12, lr}
510 bhi 1b
511
512 /* Preserve offset to relocated code. */
513 sub r6, r9, r6
514
Olivier Deprez157378f2022-04-04 15:47:50 +0200515 mov r0, r9 @ start of relocated zImage
516 add r1, sp, r6 @ end of relocated zImage
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517 bl cache_clean_flush
518
519 badr r0, restart
520 add r0, r0, r6
521 mov pc, r0
522
523wont_overwrite:
Olivier Deprez157378f2022-04-04 15:47:50 +0200524 adr r0, LC0
525 ldmia r0, {r1, r2, r3, r11, r12}
526 sub r0, r0, r1 @ calculate the delta offset
527
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528/*
529 * If delta is zero, we are running at the address we were linked at.
530 * r0 = delta
531 * r2 = BSS start
532 * r3 = BSS end
533 * r4 = kernel execution address (possibly with LSB set)
534 * r5 = appended dtb size (0 if not present)
535 * r7 = architecture ID
536 * r8 = atags pointer
537 * r11 = GOT start
538 * r12 = GOT end
539 * sp = stack pointer
540 */
541 orrs r1, r0, r5
542 beq not_relocated
543
544 add r11, r11, r0
545 add r12, r12, r0
546
547#ifndef CONFIG_ZBOOT_ROM
548 /*
549 * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
550 * we need to fix up pointers into the BSS region.
551 * Note that the stack pointer has already been fixed up.
552 */
553 add r2, r2, r0
554 add r3, r3, r0
555
556 /*
557 * Relocate all entries in the GOT table.
558 * Bump bss entries to _edata + dtb size
559 */
5601: ldr r1, [r11, #0] @ relocate entries in the GOT
561 add r1, r1, r0 @ This fixes up C references
562 cmp r1, r2 @ if entry >= bss_start &&
563 cmphs r3, r1 @ bss_end > entry
564 addhi r1, r1, r5 @ entry += dtb size
565 str r1, [r11], #4 @ next entry
566 cmp r11, r12
567 blo 1b
568
569 /* bump our bss pointers too */
570 add r2, r2, r5
571 add r3, r3, r5
572
573#else
574
575 /*
576 * Relocate entries in the GOT table. We only relocate
577 * the entries that are outside the (relocated) BSS region.
578 */
5791: ldr r1, [r11, #0] @ relocate entries in the GOT
580 cmp r1, r2 @ entry < bss_start ||
581 cmphs r3, r1 @ _end < entry
582 addlo r1, r1, r0 @ table. This fixes up the
583 str r1, [r11], #4 @ C references.
584 cmp r11, r12
585 blo 1b
586#endif
587
588not_relocated: mov r0, #0
5891: str r0, [r2], #4 @ clear bss
590 str r0, [r2], #4
591 str r0, [r2], #4
592 str r0, [r2], #4
593 cmp r2, r3
594 blo 1b
595
596 /*
597 * Did we skip the cache setup earlier?
598 * That is indicated by the LSB in r4.
599 * Do it now if so.
600 */
601 tst r4, #1
602 bic r4, r4, #1
603 blne cache_on
604
605/*
606 * The C runtime environment should now be setup sufficiently.
607 * Set up some pointers, and start decompressing.
608 * r4 = kernel execution address
609 * r7 = architecture ID
610 * r8 = atags pointer
611 */
612 mov r0, r4
613 mov r1, sp @ malloc space above stack
Olivier Deprez157378f2022-04-04 15:47:50 +0200614 add r2, sp, #MALLOC_SIZE @ 64k max
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000615 mov r3, r7
616 bl decompress_kernel
Olivier Deprez157378f2022-04-04 15:47:50 +0200617
618 get_inflated_image_size r1, r2, r3
619
620 mov r0, r4 @ start of inflated image
621 add r1, r1, r0 @ end of inflated image
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622 bl cache_clean_flush
623 bl cache_off
624
625#ifdef CONFIG_ARM_VIRT_EXT
626 mrs r0, spsr @ Get saved CPU boot mode
627 and r0, r0, #MODE_MASK
628 cmp r0, #HYP_MODE @ if not booted in HYP mode...
629 bne __enter_kernel @ boot kernel directly
630
631 adr r12, .L__hyp_reentry_vectors_offset
632 ldr r0, [r12]
633 add r0, r0, r12
634
635 bl __hyp_set_vectors
636 __HVC(0) @ otherwise bounce to hyp mode
637
638 b . @ should never be reached
639
640 .align 2
641.L__hyp_reentry_vectors_offset: .long __hyp_reentry_vectors - .
642#else
643 b __enter_kernel
644#endif
645
646 .align 2
647 .type LC0, #object
648LC0: .word LC0 @ r1
649 .word __bss_start @ r2
650 .word _end @ r3
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000651 .word _got_start @ r11
652 .word _got_end @ ip
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000653 .size LC0, . - LC0
654
Olivier Deprez157378f2022-04-04 15:47:50 +0200655 .type LC1, #object
656LC1: .word .L_user_stack_end - LC1 @ sp
657 .word _edata - LC1 @ r6
658 .size LC1, . - LC1
659
660.Lheadroom:
661 .word _end - restart + 16384 + 1024*1024
662
663.Linflated_image_size_offset:
664 .long (input_data_end - 4) - .
665
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000666#ifdef CONFIG_ARCH_RPC
667 .globl params
668params: ldr r0, =0x10000100 @ params_phys for RPC
669 mov pc, lr
670 .ltorg
671 .align
672#endif
673
674/*
Olivier Deprez157378f2022-04-04 15:47:50 +0200675 * dcache_line_size - get the minimum D-cache line size from the CTR register
676 * on ARMv7.
677 */
678 .macro dcache_line_size, reg, tmp
679#ifdef CONFIG_CPU_V7M
680 movw \tmp, #:lower16:BASEADDR_V7M_SCB + V7M_SCB_CTR
681 movt \tmp, #:upper16:BASEADDR_V7M_SCB + V7M_SCB_CTR
682 ldr \tmp, [\tmp]
683#else
684 mrc p15, 0, \tmp, c0, c0, 1 @ read ctr
685#endif
686 lsr \tmp, \tmp, #16
687 and \tmp, \tmp, #0xf @ cache line size encoding
688 mov \reg, #4 @ bytes per word
689 mov \reg, \reg, lsl \tmp @ actual cache line size
690 .endm
691
692/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693 * Turn on the cache. We need to setup some page tables so that we
694 * can have both the I and D caches on.
695 *
696 * We place the page tables 16k down from the kernel execution address,
697 * and we hope that nothing else is using it. If we're using it, we
698 * will go pop!
699 *
700 * On entry,
701 * r4 = kernel execution address
702 * r7 = architecture number
703 * r8 = atags pointer
704 * On exit,
705 * r0, r1, r2, r3, r9, r10, r12 corrupted
706 * This routine must preserve:
707 * r4, r7, r8
708 */
709 .align 5
710cache_on: mov r3, #8 @ cache_on function
711 b call_cache_fn
712
713/*
714 * Initialize the highest priority protection region, PR7
715 * to cover all 32bit address and cacheable and bufferable.
716 */
717__armv4_mpu_cache_on:
718 mov r0, #0x3f @ 4G, the whole
719 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
720 mcr p15, 0, r0, c6, c7, 1
721
722 mov r0, #0x80 @ PR7
723 mcr p15, 0, r0, c2, c0, 0 @ D-cache on
724 mcr p15, 0, r0, c2, c0, 1 @ I-cache on
725 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
726
727 mov r0, #0xc000
728 mcr p15, 0, r0, c5, c0, 1 @ I-access permission
729 mcr p15, 0, r0, c5, c0, 0 @ D-access permission
730
731 mov r0, #0
732 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
733 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
734 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
735 mrc p15, 0, r0, c1, c0, 0 @ read control reg
736 @ ...I .... ..D. WC.M
737 orr r0, r0, #0x002d @ .... .... ..1. 11.1
738 orr r0, r0, #0x1000 @ ...1 .... .... ....
739
740 mcr p15, 0, r0, c1, c0, 0 @ write control reg
741
742 mov r0, #0
743 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
744 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
745 mov pc, lr
746
747__armv3_mpu_cache_on:
748 mov r0, #0x3f @ 4G, the whole
749 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
750
751 mov r0, #0x80 @ PR7
752 mcr p15, 0, r0, c2, c0, 0 @ cache on
753 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
754
755 mov r0, #0xc000
756 mcr p15, 0, r0, c5, c0, 0 @ access permission
757
758 mov r0, #0
759 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
760 /*
761 * ?? ARMv3 MMU does not allow reading the control register,
762 * does this really work on ARMv3 MPU?
763 */
764 mrc p15, 0, r0, c1, c0, 0 @ read control reg
765 @ .... .... .... WC.M
766 orr r0, r0, #0x000d @ .... .... .... 11.1
767 /* ?? this overwrites the value constructed above? */
768 mov r0, #0
769 mcr p15, 0, r0, c1, c0, 0 @ write control reg
770
771 /* ?? invalidate for the second time? */
772 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
773 mov pc, lr
774
775#ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
776#define CB_BITS 0x08
777#else
778#define CB_BITS 0x0c
779#endif
780
781__setup_mmu: sub r3, r4, #16384 @ Page directory size
782 bic r3, r3, #0xff @ Align the pointer
783 bic r3, r3, #0x3f00
784/*
785 * Initialise the page tables, turning on the cacheable and bufferable
786 * bits for the RAM area only.
787 */
788 mov r0, r3
789 mov r9, r0, lsr #18
790 mov r9, r9, lsl #18 @ start of RAM
791 add r10, r9, #0x10000000 @ a reasonable RAM size
792 mov r1, #0x12 @ XN|U + section mapping
793 orr r1, r1, #3 << 10 @ AP=11
794 add r2, r3, #16384
7951: cmp r1, r9 @ if virt > start of RAM
796 cmphs r10, r1 @ && end of RAM > virt
797 bic r1, r1, #0x1c @ clear XN|U + C + B
798 orrlo r1, r1, #0x10 @ Set XN|U for non-RAM
799 orrhs r1, r1, r6 @ set RAM section settings
800 str r1, [r0], #4 @ 1:1 mapping
801 add r1, r1, #1048576
802 teq r0, r2
803 bne 1b
804/*
805 * If ever we are running from Flash, then we surely want the cache
806 * to be enabled also for our execution instance... We map 2MB of it
807 * so there is no map overlap problem for up to 1 MB compressed kernel.
808 * If the execution is in RAM then we would only be duplicating the above.
809 */
810 orr r1, r6, #0x04 @ ensure B is set for this
811 orr r1, r1, #3 << 10
812 mov r2, pc
813 mov r2, r2, lsr #20
814 orr r1, r1, r2, lsl #20
815 add r0, r3, r2, lsl #2
816 str r1, [r0], #4
817 add r1, r1, #1048576
818 str r1, [r0]
819 mov pc, lr
820ENDPROC(__setup_mmu)
821
822@ Enable unaligned access on v6, to allow better code generation
823@ for the decompressor C code:
824__armv6_mmu_cache_on:
825 mrc p15, 0, r0, c1, c0, 0 @ read SCTLR
826 bic r0, r0, #2 @ A (no unaligned access fault)
827 orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
828 mcr p15, 0, r0, c1, c0, 0 @ write SCTLR
829 b __armv4_mmu_cache_on
830
831__arm926ejs_mmu_cache_on:
832#ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
833 mov r0, #4 @ put dcache in WT mode
834 mcr p15, 7, r0, c15, c0, 0
835#endif
836
837__armv4_mmu_cache_on:
838 mov r12, lr
839#ifdef CONFIG_MMU
840 mov r6, #CB_BITS | 0x12 @ U
841 bl __setup_mmu
842 mov r0, #0
843 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
844 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
845 mrc p15, 0, r0, c1, c0, 0 @ read control reg
846 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
847 orr r0, r0, #0x0030
848 ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
849 bl __common_mmu_cache_on
850 mov r0, #0
851 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
852#endif
853 mov pc, r12
854
855__armv7_mmu_cache_on:
Olivier Deprez157378f2022-04-04 15:47:50 +0200856 enable_cp15_barriers r11
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000857 mov r12, lr
858#ifdef CONFIG_MMU
859 mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
860 tst r11, #0xf @ VMSA
861 movne r6, #CB_BITS | 0x02 @ !XN
862 blne __setup_mmu
863 mov r0, #0
864 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
865 tst r11, #0xf @ VMSA
866 mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
867#endif
868 mrc p15, 0, r0, c1, c0, 0 @ read control reg
869 bic r0, r0, #1 << 28 @ clear SCTLR.TRE
870 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
871 orr r0, r0, #0x003c @ write buffer
872 bic r0, r0, #2 @ A (no unaligned access fault)
873 orr r0, r0, #1 << 22 @ U (v6 unaligned access model)
874 @ (needed for ARM1176)
875#ifdef CONFIG_MMU
876 ARM_BE8( orr r0, r0, #1 << 25 ) @ big-endian page tables
877 mrcne p15, 0, r6, c2, c0, 2 @ read ttb control reg
878 orrne r0, r0, #1 @ MMU enabled
879 movne r1, #0xfffffffd @ domain 0 = client
880 bic r6, r6, #1 << 31 @ 32-bit translation system
881 bic r6, r6, #(7 << 0) | (1 << 4) @ use only ttbr0
882 mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
883 mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
884 mcrne p15, 0, r6, c2, c0, 2 @ load ttb control
885#endif
886 mcr p15, 0, r0, c7, c5, 4 @ ISB
887 mcr p15, 0, r0, c1, c0, 0 @ load control register
888 mrc p15, 0, r0, c1, c0, 0 @ and read it back
889 mov r0, #0
890 mcr p15, 0, r0, c7, c5, 4 @ ISB
891 mov pc, r12
892
893__fa526_cache_on:
894 mov r12, lr
895 mov r6, #CB_BITS | 0x12 @ U
896 bl __setup_mmu
897 mov r0, #0
898 mcr p15, 0, r0, c7, c7, 0 @ Invalidate whole cache
899 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
900 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
901 mrc p15, 0, r0, c1, c0, 0 @ read control reg
902 orr r0, r0, #0x1000 @ I-cache enable
903 bl __common_mmu_cache_on
904 mov r0, #0
905 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
906 mov pc, r12
907
908__common_mmu_cache_on:
909#ifndef CONFIG_THUMB2_KERNEL
910#ifndef DEBUG
911 orr r0, r0, #0x000d @ Write buffer, mmu
912#endif
913 mov r1, #-1
914 mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
915 mcr p15, 0, r1, c3, c0, 0 @ load domain access control
916 b 1f
917 .align 5 @ cache line aligned
9181: mcr p15, 0, r0, c1, c0, 0 @ load control register
919 mrc p15, 0, r0, c1, c0, 0 @ and read it back to
920 sub pc, lr, r0, lsr #32 @ properly flush pipeline
921#endif
922
923#define PROC_ENTRY_SIZE (4*5)
924
925/*
926 * Here follow the relocatable cache support functions for the
927 * various processors. This is a generic hook for locating an
928 * entry and jumping to an instruction at the specified offset
929 * from the start of the block. Please note this is all position
930 * independent code.
931 *
932 * r1 = corrupted
933 * r2 = corrupted
934 * r3 = block offset
935 * r9 = corrupted
936 * r12 = corrupted
937 */
938
939call_cache_fn: adr r12, proc_types
940#ifdef CONFIG_CPU_CP15
941 mrc p15, 0, r9, c0, c0 @ get processor ID
942#elif defined(CONFIG_CPU_V7M)
943 /*
944 * On v7-M the processor id is located in the V7M_SCB_CPUID
945 * register, but as cache handling is IMPLEMENTATION DEFINED on
946 * v7-M (if existant at all) we just return early here.
947 * If V7M_SCB_CPUID were used the cpu ID functions (i.e.
948 * __armv7_mmu_cache_{on,off,flush}) would be selected which
949 * use cp15 registers that are not implemented on v7-M.
950 */
951 bx lr
952#else
953 ldr r9, =CONFIG_PROCESSOR_ID
954#endif
9551: ldr r1, [r12, #0] @ get value
956 ldr r2, [r12, #4] @ get mask
957 eor r1, r1, r9 @ (real ^ match)
958 tst r1, r2 @ & mask
959 ARM( addeq pc, r12, r3 ) @ call cache function
960 THUMB( addeq r12, r3 )
961 THUMB( moveq pc, r12 ) @ call cache function
962 add r12, r12, #PROC_ENTRY_SIZE
963 b 1b
964
965/*
966 * Table for cache operations. This is basically:
967 * - CPU ID match
968 * - CPU ID mask
969 * - 'cache on' method instruction
970 * - 'cache off' method instruction
971 * - 'cache flush' method instruction
972 *
973 * We match an entry using: ((real_id ^ match) & mask) == 0
974 *
975 * Writethrough caches generally only need 'on' and 'off'
976 * methods. Writeback caches _must_ have the flush method
977 * defined.
978 */
979 .align 2
980 .type proc_types,#object
981proc_types:
982 .word 0x41000000 @ old ARM ID
983 .word 0xff00f000
984 mov pc, lr
985 THUMB( nop )
986 mov pc, lr
987 THUMB( nop )
988 mov pc, lr
989 THUMB( nop )
990
991 .word 0x41007000 @ ARM7/710
992 .word 0xfff8fe00
993 mov pc, lr
994 THUMB( nop )
995 mov pc, lr
996 THUMB( nop )
997 mov pc, lr
998 THUMB( nop )
999
1000 .word 0x41807200 @ ARM720T (writethrough)
1001 .word 0xffffff00
1002 W(b) __armv4_mmu_cache_on
1003 W(b) __armv4_mmu_cache_off
1004 mov pc, lr
1005 THUMB( nop )
1006
1007 .word 0x41007400 @ ARM74x
1008 .word 0xff00ff00
1009 W(b) __armv3_mpu_cache_on
1010 W(b) __armv3_mpu_cache_off
1011 W(b) __armv3_mpu_cache_flush
1012
1013 .word 0x41009400 @ ARM94x
1014 .word 0xff00ff00
1015 W(b) __armv4_mpu_cache_on
1016 W(b) __armv4_mpu_cache_off
1017 W(b) __armv4_mpu_cache_flush
1018
1019 .word 0x41069260 @ ARM926EJ-S (v5TEJ)
1020 .word 0xff0ffff0
1021 W(b) __arm926ejs_mmu_cache_on
1022 W(b) __armv4_mmu_cache_off
1023 W(b) __armv5tej_mmu_cache_flush
1024
1025 .word 0x00007000 @ ARM7 IDs
1026 .word 0x0000f000
1027 mov pc, lr
1028 THUMB( nop )
1029 mov pc, lr
1030 THUMB( nop )
1031 mov pc, lr
1032 THUMB( nop )
1033
1034 @ Everything from here on will be the new ID system.
1035
1036 .word 0x4401a100 @ sa110 / sa1100
1037 .word 0xffffffe0
1038 W(b) __armv4_mmu_cache_on
1039 W(b) __armv4_mmu_cache_off
1040 W(b) __armv4_mmu_cache_flush
1041
1042 .word 0x6901b110 @ sa1110
1043 .word 0xfffffff0
1044 W(b) __armv4_mmu_cache_on
1045 W(b) __armv4_mmu_cache_off
1046 W(b) __armv4_mmu_cache_flush
1047
1048 .word 0x56056900
1049 .word 0xffffff00 @ PXA9xx
1050 W(b) __armv4_mmu_cache_on
1051 W(b) __armv4_mmu_cache_off
1052 W(b) __armv4_mmu_cache_flush
1053
1054 .word 0x56158000 @ PXA168
1055 .word 0xfffff000
1056 W(b) __armv4_mmu_cache_on
1057 W(b) __armv4_mmu_cache_off
1058 W(b) __armv5tej_mmu_cache_flush
1059
1060 .word 0x56050000 @ Feroceon
1061 .word 0xff0f0000
1062 W(b) __armv4_mmu_cache_on
1063 W(b) __armv4_mmu_cache_off
1064 W(b) __armv5tej_mmu_cache_flush
1065
1066#ifdef CONFIG_CPU_FEROCEON_OLD_ID
1067 /* this conflicts with the standard ARMv5TE entry */
1068 .long 0x41009260 @ Old Feroceon
1069 .long 0xff00fff0
1070 b __armv4_mmu_cache_on
1071 b __armv4_mmu_cache_off
1072 b __armv5tej_mmu_cache_flush
1073#endif
1074
1075 .word 0x66015261 @ FA526
1076 .word 0xff01fff1
1077 W(b) __fa526_cache_on
1078 W(b) __armv4_mmu_cache_off
1079 W(b) __fa526_cache_flush
1080
1081 @ These match on the architecture ID
1082
1083 .word 0x00020000 @ ARMv4T
1084 .word 0x000f0000
1085 W(b) __armv4_mmu_cache_on
1086 W(b) __armv4_mmu_cache_off
1087 W(b) __armv4_mmu_cache_flush
1088
1089 .word 0x00050000 @ ARMv5TE
1090 .word 0x000f0000
1091 W(b) __armv4_mmu_cache_on
1092 W(b) __armv4_mmu_cache_off
1093 W(b) __armv4_mmu_cache_flush
1094
1095 .word 0x00060000 @ ARMv5TEJ
1096 .word 0x000f0000
1097 W(b) __armv4_mmu_cache_on
1098 W(b) __armv4_mmu_cache_off
1099 W(b) __armv5tej_mmu_cache_flush
1100
1101 .word 0x0007b000 @ ARMv6
1102 .word 0x000ff000
1103 W(b) __armv6_mmu_cache_on
1104 W(b) __armv4_mmu_cache_off
1105 W(b) __armv6_mmu_cache_flush
1106
1107 .word 0x000f0000 @ new CPU Id
1108 .word 0x000f0000
1109 W(b) __armv7_mmu_cache_on
1110 W(b) __armv7_mmu_cache_off
1111 W(b) __armv7_mmu_cache_flush
1112
1113 .word 0 @ unrecognised type
1114 .word 0
1115 mov pc, lr
1116 THUMB( nop )
1117 mov pc, lr
1118 THUMB( nop )
1119 mov pc, lr
1120 THUMB( nop )
1121
1122 .size proc_types, . - proc_types
1123
1124 /*
1125 * If you get a "non-constant expression in ".if" statement"
1126 * error from the assembler on this line, check that you have
1127 * not accidentally written a "b" instruction where you should
1128 * have written W(b).
1129 */
1130 .if (. - proc_types) % PROC_ENTRY_SIZE != 0
1131 .error "The size of one or more proc_types entries is wrong."
1132 .endif
1133
1134/*
1135 * Turn off the Cache and MMU. ARMv3 does not support
1136 * reading the control register, but ARMv4 does.
1137 *
1138 * On exit,
1139 * r0, r1, r2, r3, r9, r12 corrupted
1140 * This routine must preserve:
1141 * r4, r7, r8
1142 */
1143 .align 5
1144cache_off: mov r3, #12 @ cache_off function
1145 b call_cache_fn
1146
1147__armv4_mpu_cache_off:
1148 mrc p15, 0, r0, c1, c0
1149 bic r0, r0, #0x000d
1150 mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
1151 mov r0, #0
1152 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
1153 mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
1154 mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
1155 mov pc, lr
1156
1157__armv3_mpu_cache_off:
1158 mrc p15, 0, r0, c1, c0
1159 bic r0, r0, #0x000d
1160 mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
1161 mov r0, #0
1162 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
1163 mov pc, lr
1164
1165__armv4_mmu_cache_off:
1166#ifdef CONFIG_MMU
1167 mrc p15, 0, r0, c1, c0
1168 bic r0, r0, #0x000d
1169 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
1170 mov r0, #0
1171 mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
1172 mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
1173#endif
1174 mov pc, lr
1175
1176__armv7_mmu_cache_off:
1177 mrc p15, 0, r0, c1, c0
1178#ifdef CONFIG_MMU
Olivier Deprez0e641232021-09-23 10:07:05 +02001179 bic r0, r0, #0x0005
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001180#else
Olivier Deprez0e641232021-09-23 10:07:05 +02001181 bic r0, r0, #0x0004
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001182#endif
1183 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001184 mov r0, #0
1185#ifdef CONFIG_MMU
1186 mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
1187#endif
1188 mcr p15, 0, r0, c7, c5, 6 @ invalidate BTC
1189 mcr p15, 0, r0, c7, c10, 4 @ DSB
1190 mcr p15, 0, r0, c7, c5, 4 @ ISB
Olivier Deprez157378f2022-04-04 15:47:50 +02001191 mov pc, lr
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001192
1193/*
1194 * Clean and flush the cache to maintain consistency.
1195 *
Olivier Deprez157378f2022-04-04 15:47:50 +02001196 * On entry,
1197 * r0 = start address
1198 * r1 = end address (exclusive)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001199 * On exit,
1200 * r1, r2, r3, r9, r10, r11, r12 corrupted
1201 * This routine must preserve:
1202 * r4, r6, r7, r8
1203 */
1204 .align 5
1205cache_clean_flush:
1206 mov r3, #16
Olivier Deprez157378f2022-04-04 15:47:50 +02001207 mov r11, r1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001208 b call_cache_fn
1209
1210__armv4_mpu_cache_flush:
1211 tst r4, #1
1212 movne pc, lr
1213 mov r2, #1
1214 mov r3, #0
1215 mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
1216 mov r1, #7 << 5 @ 8 segments
12171: orr r3, r1, #63 << 26 @ 64 entries
12182: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
1219 subs r3, r3, #1 << 26
1220 bcs 2b @ entries 63 to 0
1221 subs r1, r1, #1 << 5
1222 bcs 1b @ segments 7 to 0
1223
1224 teq r2, #0
1225 mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
1226 mcr p15, 0, ip, c7, c10, 4 @ drain WB
1227 mov pc, lr
1228
1229__fa526_cache_flush:
1230 tst r4, #1
1231 movne pc, lr
1232 mov r1, #0
1233 mcr p15, 0, r1, c7, c14, 0 @ clean and invalidate D cache
1234 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1235 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1236 mov pc, lr
1237
1238__armv6_mmu_cache_flush:
1239 mov r1, #0
1240 tst r4, #1
1241 mcreq p15, 0, r1, c7, c14, 0 @ clean+invalidate D
1242 mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
1243 mcreq p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
1244 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1245 mov pc, lr
1246
1247__armv7_mmu_cache_flush:
Olivier Deprez157378f2022-04-04 15:47:50 +02001248 enable_cp15_barriers r10
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001249 tst r4, #1
1250 bne iflush
1251 mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
1252 tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
1253 mov r10, #0
1254 beq hierarchical
1255 mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
1256 b iflush
1257hierarchical:
Olivier Deprez157378f2022-04-04 15:47:50 +02001258 dcache_line_size r1, r2 @ r1 := dcache min line size
1259 sub r2, r1, #1 @ r2 := line size mask
1260 bic r0, r0, r2 @ round down start to line size
1261 sub r11, r11, #1 @ end address is exclusive
1262 bic r11, r11, r2 @ round down end to line size
12630: cmp r0, r11 @ finished?
1264 bgt iflush
1265 mcr p15, 0, r0, c7, c14, 1 @ Dcache clean/invalidate by VA
1266 add r0, r0, r1
1267 b 0b
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001268iflush:
1269 mcr p15, 0, r10, c7, c10, 4 @ DSB
1270 mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
1271 mcr p15, 0, r10, c7, c10, 4 @ DSB
1272 mcr p15, 0, r10, c7, c5, 4 @ ISB
1273 mov pc, lr
1274
1275__armv5tej_mmu_cache_flush:
1276 tst r4, #1
1277 movne pc, lr
Olivier Deprez157378f2022-04-04 15:47:50 +020012781: mrc p15, 0, APSR_nzcv, c7, c14, 3 @ test,clean,invalidate D cache
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001279 bne 1b
1280 mcr p15, 0, r0, c7, c5, 0 @ flush I cache
1281 mcr p15, 0, r0, c7, c10, 4 @ drain WB
1282 mov pc, lr
1283
1284__armv4_mmu_cache_flush:
1285 tst r4, #1
1286 movne pc, lr
1287 mov r2, #64*1024 @ default: 32K dcache size (*2)
1288 mov r11, #32 @ default: 32 byte line size
1289 mrc p15, 0, r3, c0, c0, 1 @ read cache type
1290 teq r3, r9 @ cache ID register present?
1291 beq no_cache_id
1292 mov r1, r3, lsr #18
1293 and r1, r1, #7
1294 mov r2, #1024
1295 mov r2, r2, lsl r1 @ base dcache size *2
1296 tst r3, #1 << 14 @ test M bit
1297 addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
1298 mov r3, r3, lsr #12
1299 and r3, r3, #3
1300 mov r11, #8
1301 mov r11, r11, lsl r3 @ cache line size in bytes
1302no_cache_id:
1303 mov r1, pc
1304 bic r1, r1, #63 @ align to longest cache line
1305 add r2, r1, r2
13061:
1307 ARM( ldr r3, [r1], r11 ) @ s/w flush D cache
1308 THUMB( ldr r3, [r1] ) @ s/w flush D cache
1309 THUMB( add r1, r1, r11 )
1310 teq r1, r2
1311 bne 1b
1312
1313 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1314 mcr p15, 0, r1, c7, c6, 0 @ flush D cache
1315 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1316 mov pc, lr
1317
1318__armv3_mmu_cache_flush:
1319__armv3_mpu_cache_flush:
1320 tst r4, #1
1321 movne pc, lr
1322 mov r1, #0
1323 mcr p15, 0, r1, c7, c0, 0 @ invalidate whole cache v3
1324 mov pc, lr
1325
1326/*
1327 * Various debugging routines for printing hex characters and
1328 * memory, which again must be relocatable.
1329 */
1330#ifdef DEBUG
1331 .align 2
1332 .type phexbuf,#object
1333phexbuf: .space 12
1334 .size phexbuf, . - phexbuf
1335
1336@ phex corrupts {r0, r1, r2, r3}
1337phex: adr r3, phexbuf
1338 mov r2, #0
1339 strb r2, [r3, r1]
13401: subs r1, r1, #1
1341 movmi r0, r3
1342 bmi puts
1343 and r2, r0, #15
1344 mov r0, r0, lsr #4
1345 cmp r2, #10
1346 addge r2, r2, #7
1347 add r2, r2, #'0'
1348 strb r2, [r3, r1]
1349 b 1b
1350
1351@ puts corrupts {r0, r1, r2, r3}
1352puts: loadsp r3, r2, r1
13531: ldrb r2, [r0], #1
1354 teq r2, #0
1355 moveq pc, lr
Olivier Deprez157378f2022-04-04 15:47:50 +020013562: writeb r2, r3, r1
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001357 mov r1, #0x00020000
13583: subs r1, r1, #1
1359 bne 3b
1360 teq r2, #'\n'
1361 moveq r2, #'\r'
1362 beq 2b
1363 teq r0, #0
1364 bne 1b
1365 mov pc, lr
1366@ putc corrupts {r0, r1, r2, r3}
1367putc:
1368 mov r2, r0
1369 loadsp r3, r1, r0
1370 mov r0, #0
1371 b 2b
1372
1373@ memdump corrupts {r0, r1, r2, r3, r10, r11, r12, lr}
1374memdump: mov r12, r0
1375 mov r10, lr
1376 mov r11, #0
13772: mov r0, r11, lsl #2
1378 add r0, r0, r12
1379 mov r1, #8
1380 bl phex
1381 mov r0, #':'
1382 bl putc
13831: mov r0, #' '
1384 bl putc
1385 ldr r0, [r12, r11, lsl #2]
1386 mov r1, #8
1387 bl phex
1388 and r0, r11, #7
1389 teq r0, #3
1390 moveq r0, #' '
1391 bleq putc
1392 and r0, r11, #7
1393 add r11, r11, #1
1394 teq r0, #7
1395 bne 1b
1396 mov r0, #'\n'
1397 bl putc
1398 cmp r11, #64
1399 blt 2b
1400 mov pc, r10
1401#endif
1402
1403 .ltorg
1404
1405#ifdef CONFIG_ARM_VIRT_EXT
1406.align 5
1407__hyp_reentry_vectors:
1408 W(b) . @ reset
1409 W(b) . @ undef
Olivier Deprez157378f2022-04-04 15:47:50 +02001410#ifdef CONFIG_EFI_STUB
1411 W(b) __enter_kernel_from_hyp @ hvc from HYP
1412#else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001413 W(b) . @ svc
Olivier Deprez157378f2022-04-04 15:47:50 +02001414#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001415 W(b) . @ pabort
1416 W(b) . @ dabort
1417 W(b) __enter_kernel @ hyp
1418 W(b) . @ irq
1419 W(b) . @ fiq
1420#endif /* CONFIG_ARM_VIRT_EXT */
1421
1422__enter_kernel:
1423 mov r0, #0 @ must be 0
1424 mov r1, r7 @ restore architecture number
1425 mov r2, r8 @ restore atags pointer
1426 ARM( mov pc, r4 ) @ call kernel
1427 M_CLASS( add r4, r4, #1 ) @ enter in Thumb mode for M class
1428 THUMB( bx r4 ) @ entry point is always ARM for A/R classes
1429
1430reloc_code_end:
1431
1432#ifdef CONFIG_EFI_STUB
Olivier Deprez157378f2022-04-04 15:47:50 +02001433__enter_kernel_from_hyp:
1434 mrc p15, 4, r0, c1, c0, 0 @ read HSCTLR
1435 bic r0, r0, #0x5 @ disable MMU and caches
1436 mcr p15, 4, r0, c1, c0, 0 @ write HSCTLR
1437 isb
1438 b __enter_kernel
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001439
Olivier Deprez157378f2022-04-04 15:47:50 +02001440ENTRY(efi_enter_kernel)
1441 mov r4, r0 @ preserve image base
1442 mov r8, r1 @ preserve DT pointer
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001443
Olivier Deprez157378f2022-04-04 15:47:50 +02001444 adr_l r0, call_cache_fn
1445 adr r1, 0f @ clean the region of code we
1446 bl cache_clean_flush @ may run with the MMU off
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001447
Olivier Deprez157378f2022-04-04 15:47:50 +02001448#ifdef CONFIG_ARM_VIRT_EXT
1449 @
1450 @ The EFI spec does not support booting on ARM in HYP mode,
1451 @ since it mandates that the MMU and caches are on, with all
1452 @ 32-bit addressable DRAM mapped 1:1 using short descriptors.
1453 @
1454 @ While the EDK2 reference implementation adheres to this,
1455 @ U-Boot might decide to enter the EFI stub in HYP mode
1456 @ anyway, with the MMU and caches either on or off.
1457 @
1458 mrs r0, cpsr @ get the current mode
1459 msr spsr_cxsf, r0 @ record boot mode
1460 and r0, r0, #MODE_MASK @ are we running in HYP mode?
1461 cmp r0, #HYP_MODE
1462 bne .Lefi_svc
David Brazdil0f672f62019-12-10 10:32:29 +00001463
Olivier Deprez157378f2022-04-04 15:47:50 +02001464 mrc p15, 4, r1, c1, c0, 0 @ read HSCTLR
1465 tst r1, #0x1 @ MMU enabled at HYP?
1466 beq 1f
David Brazdil0f672f62019-12-10 10:32:29 +00001467
Olivier Deprez157378f2022-04-04 15:47:50 +02001468 @
1469 @ When running in HYP mode with the caches on, we're better
1470 @ off just carrying on using the cached 1:1 mapping that the
1471 @ firmware provided. Set up the HYP vectors so HVC instructions
1472 @ issued from HYP mode take us to the correct handler code. We
1473 @ will disable the MMU before jumping to the kernel proper.
1474 @
1475 ARM( bic r1, r1, #(1 << 30) ) @ clear HSCTLR.TE
1476 THUMB( orr r1, r1, #(1 << 30) ) @ set HSCTLR.TE
1477 mcr p15, 4, r1, c1, c0, 0
1478 adr r0, __hyp_reentry_vectors
1479 mcr p15, 4, r0, c12, c0, 0 @ set HYP vector base (HVBAR)
1480 isb
1481 b .Lefi_hyp
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001482
Olivier Deprez157378f2022-04-04 15:47:50 +02001483 @
1484 @ When running in HYP mode with the caches off, we need to drop
1485 @ into SVC mode now, and let the decompressor set up its cached
1486 @ 1:1 mapping as usual.
1487 @
14881: mov r9, r4 @ preserve image base
1489 bl __hyp_stub_install @ install HYP stub vectors
1490 safe_svcmode_maskall r1 @ drop to SVC mode
1491 msr spsr_cxsf, r0 @ record boot mode
1492 orr r4, r9, #1 @ restore image base and set LSB
1493 b .Lefi_hyp
1494.Lefi_svc:
1495#endif
1496 mrc p15, 0, r0, c1, c0, 0 @ read SCTLR
1497 tst r0, #0x1 @ MMU enabled?
1498 orreq r4, r4, #1 @ set LSB if not
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001499
Olivier Deprez157378f2022-04-04 15:47:50 +02001500.Lefi_hyp:
1501 mov r0, r8 @ DT start
1502 add r1, r8, r2 @ DT end
1503 bl cache_clean_flush
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001504
Olivier Deprez157378f2022-04-04 15:47:50 +02001505 adr r0, 0f @ switch to our stack
1506 ldr sp, [r0]
1507 add sp, sp, r0
1508
1509 mov r5, #0 @ appended DTB size
1510 mov r7, #0xFFFFFFFF @ machine ID
1511 b wont_overwrite
1512ENDPROC(efi_enter_kernel)
15130: .long .L_user_stack_end - .
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001514#endif
1515
1516 .align
1517 .section ".stack", "aw", %nobits
1518.L_user_stack: .space 4096
1519.L_user_stack_end: