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