1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 *  linux/arch/arm/kernel/head-common.S
4 *
5 *  Copyright (C) 1994-2002 Russell King
6 *  Copyright (c) 2003 ARM Limited
7 *  All Rights Reserved
8 */
9#include <asm/assembler.h>
10
11#define ATAG_CORE 0x54410001
12#define ATAG_CORE_SIZE ((2*4 + 3*4) >> 2)
13#define ATAG_CORE_SIZE_EMPTY ((2*4) >> 2)
14
15#ifdef CONFIG_CPU_BIG_ENDIAN
16#define OF_DT_MAGIC 0xd00dfeed
17#else
18#define OF_DT_MAGIC 0xedfe0dd0 /* 0xd00dfeed in big-endian */
19#endif
20
21/*
22 * Exception handling.  Something went wrong and we can't proceed.  We
23 * ought to tell the user, but since we don't have any guarantee that
24 * we're even running on the right architecture, we do virtually nothing.
25 *
26 * If CONFIG_DEBUG_LL is set we try to print out something about the error
27 * and hope for the best (useful if bootloader fails to pass a proper
28 * machine ID for example).
29 */
30	__HEAD
31
32/* Determine validity of the r2 atags pointer.  The heuristic requires
33 * that the pointer be aligned, in the first 16k of physical RAM and
34 * that the ATAG_CORE marker is first and present.  If CONFIG_OF_FLATTREE
35 * is selected, then it will also accept a dtb pointer.  Future revisions
36 * of this function may be more lenient with the physical address and
37 * may also be able to move the ATAGS block if necessary.
38 *
39 * Returns:
40 *  r2 either valid atags pointer, valid dtb pointer, or zero
41 *  r5, r6 corrupted
42 */
43__vet_atags:
44	tst	r2, #0x3			@ aligned?
45	bne	1f
46
47	ldr	r5, [r2, #0]
48#ifdef CONFIG_OF_FLATTREE
49	ldr	r6, =OF_DT_MAGIC		@ is it a DTB?
50	cmp	r5, r6
51	beq	2f
52#endif
53	cmp	r5, #ATAG_CORE_SIZE		@ is first tag ATAG_CORE?
54	cmpne	r5, #ATAG_CORE_SIZE_EMPTY
55	bne	1f
56	ldr	r5, [r2, #4]
57	ldr	r6, =ATAG_CORE
58	cmp	r5, r6
59	bne	1f
60
612:	ret	lr				@ atag/dtb pointer is ok
62
631:	mov	r2, #0
64	ret	lr
65ENDPROC(__vet_atags)
66
67/*
68 * The following fragment of code is executed with the MMU on in MMU mode,
69 * and uses absolute addresses; this is not position independent.
70 *
71 *  r0  = cp#15 control register (exc_ret for M-class)
72 *  r1  = machine ID
73 *  r2  = atags/dtb pointer
74 *  r9  = processor ID
75 */
76	__INIT
77__mmap_switched:
78
79	mov	r7, r1
80	mov	r8, r2
81	mov	r10, r0
82
83	adr	r4, __mmap_switched_data
84	mov	fp, #0
85
86#if defined(CONFIG_XIP_DEFLATED_DATA)
87   ARM(	ldr	sp, [r4], #4 )
88 THUMB(	ldr	sp, [r4] )
89 THUMB(	add	r4, #4 )
90	bl	__inflate_kernel_data		@ decompress .data to RAM
91	teq	r0, #0
92	bne	__error
93#elif defined(CONFIG_XIP_KERNEL)
94   ARM(	ldmia	r4!, {r0, r1, r2, sp} )
95 THUMB(	ldmia	r4!, {r0, r1, r2, r3} )
96 THUMB(	mov	sp, r3 )
97	sub	r2, r2, r1
98	bl	__memcpy			@ copy .data to RAM
99#endif
100
101   ARM(	ldmia	r4!, {r0, r1, sp} )
102 THUMB(	ldmia	r4!, {r0, r1, r3} )
103 THUMB(	mov	sp, r3 )
104	sub	r2, r1, r0
105	mov	r1, #0
106	bl	__memset			@ clear .bss
107
108#ifdef CONFIG_CURRENT_POINTER_IN_TPIDRURO
109	adr_l	r0, init_task			@ get swapper task_struct
110	set_current r0
111#endif
112
113	ldmia	r4, {r0, r1, r2, r3}
114	str	r9, [r0]			@ Save processor ID
115	str	r7, [r1]			@ Save machine type
116	str	r8, [r2]			@ Save atags pointer
117	cmp	r3, #0
118	strne	r10, [r3]			@ Save control register values
119#ifdef CONFIG_KASAN
120	bl	kasan_early_init
121#endif
122	mov	lr, #0
123	b	start_kernel
124ENDPROC(__mmap_switched)
125
126	.align	2
127	.type	__mmap_switched_data, %object
128__mmap_switched_data:
129#ifdef CONFIG_XIP_KERNEL
130#ifndef CONFIG_XIP_DEFLATED_DATA
131	.long	_sdata				@ r0
132	.long	__data_loc			@ r1
133	.long	_edata_loc			@ r2
134#endif
135	.long	__bss_stop			@ sp (temporary stack in .bss)
136#endif
137
138	.long	__bss_start			@ r0
139	.long	__bss_stop			@ r1
140	.long	init_thread_union + THREAD_START_SP @ sp
141
142	.long	processor_id			@ r0
143	.long	__machine_arch_type		@ r1
144	.long	__atags_pointer			@ r2
145#ifdef CONFIG_CPU_CP15
146	.long	cr_alignment			@ r3
147#else
148M_CLASS(.long	exc_ret)			@ r3
149AR_CLASS(.long	0)				@ r3
150#endif
151	.size	__mmap_switched_data, . - __mmap_switched_data
152
153	__FINIT
154	.text
155
156/*
157 * This provides a C-API version of __lookup_processor_type
158 */
159ENTRY(lookup_processor_type)
160	stmfd	sp!, {r4 - r6, r9, lr}
161	mov	r9, r0
162	bl	__lookup_processor_type
163	mov	r0, r5
164	ldmfd	sp!, {r4 - r6, r9, pc}
165ENDPROC(lookup_processor_type)
166
167/*
168 * Read processor ID register (CP#15, CR0), and look up in the linker-built
169 * supported processor list.  Note that we can't use the absolute addresses
170 * for the __proc_info lists since we aren't running with the MMU on
171 * (and therefore, we are not in the correct address space).  We have to
172 * calculate the offset.
173 *
174 *	r9 = cpuid
175 * Returns:
176 *	r3, r4, r6 corrupted
177 *	r5 = proc_info pointer in physical address space
178 *	r9 = cpuid (preserved)
179 */
180__lookup_processor_type:
181	/*
182	 * Look in <asm/procinfo.h> for information about the __proc_info
183	 * structure.
184	 */
185	adr_l	r5, __proc_info_begin
186	adr_l	r6, __proc_info_end
1871:	ldmia	r5, {r3, r4}			@ value, mask
188	and	r4, r4, r9			@ mask wanted bits
189	teq	r3, r4
190	beq	2f
191	add	r5, r5, #PROC_INFO_SZ		@ sizeof(proc_info_list)
192	cmp	r5, r6
193	blo	1b
194	mov	r5, #0				@ unknown processor
1952:	ret	lr
196ENDPROC(__lookup_processor_type)
197
198__error_lpae:
199#ifdef CONFIG_DEBUG_LL
200	adr	r0, str_lpae
201	bl 	printascii
202	b	__error
203str_lpae: .asciz "\nError: Kernel with LPAE support, but CPU does not support LPAE.\n"
204#else
205	b	__error
206#endif
207	.align
208ENDPROC(__error_lpae)
209
210__error_p:
211#ifdef CONFIG_DEBUG_LL
212	adr	r0, str_p1
213	bl	printascii
214	mov	r0, r9
215	bl	printhex8
216	adr	r0, str_p2
217	bl	printascii
218	b	__error
219str_p1:	.asciz	"\nError: unrecognized/unsupported processor variant (0x"
220str_p2:	.asciz	").\n"
221	.align
222#endif
223ENDPROC(__error_p)
224
225__error:
226#ifdef CONFIG_ARCH_RPC
227/*
228 * Turn the screen red on a error - RiscPC only.
229 */
230	mov	r0, #0x02000000
231	mov	r3, #0x11
232	orr	r3, r3, r3, lsl #8
233	orr	r3, r3, r3, lsl #16
234	str	r3, [r0], #4
235	str	r3, [r0], #4
236	str	r3, [r0], #4
237	str	r3, [r0], #4
238#endif
2391:	mov	r0, r0
240	b	1b
241ENDPROC(__error)
242