blob: 8484294bc623f03c8a427aa290402927a8d5517a [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
2 * arch/xtensa/kernel/head.S
3 *
4 * Xtensa Processor startup code.
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 *
Marc Gauthier2d1c6452013-01-05 04:57:17 +040010 * Copyright (C) 2001 - 2008 Tensilica Inc.
Chris Zankel5a0015d2005-06-23 22:01:16 -070011 *
12 * Chris Zankel <chris@zankel.net>
13 * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
14 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
15 * Kevin Chea
16 */
17
Max Filippov0b537252021-05-01 15:32:58 -070018#include <asm/asmmacro.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070019#include <asm/processor.h>
20#include <asm/page.h>
Chris Zankel173d6682006-12-10 02:18:48 -080021#include <asm/cacheasm.h>
Max Filippovc622b292012-11-19 07:00:41 +040022#include <asm/initialize_mmu.h>
Max Filippovf6151362013-10-17 02:42:26 +040023#include <asm/mxregs.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070024
Tim Abbott0ebdcb42009-04-25 22:10:57 -040025#include <linux/init.h>
Chris Zankeladba09f2007-05-31 17:48:07 -070026#include <linux/linkage.h>
27
Chris Zankel5a0015d2005-06-23 22:01:16 -070028/*
29 * This module contains the entry code for kernel images. It performs the
30 * minimal setup needed to call the generic C routines.
31 *
32 * Prerequisites:
33 *
34 * - The kernel image has been loaded to the actual address where it was
35 * compiled to.
36 * - a2 contains either 0 or a pointer to a list of boot parameters.
37 * (see setup.c for more details)
38 *
39 */
40
Chris Zankel5a0015d2005-06-23 22:01:16 -070041/*
42 * _start
43 *
44 * The bootloader passes a pointer to a list of boot parameters in a2.
45 */
46
47 /* The first bytes of the kernel image must be an instruction, so we
48 * manually allocate and define the literal constant we need for a jx
49 * instruction.
50 */
51
Tim Abbott0ebdcb42009-04-25 22:10:57 -040052 __HEAD
Max Filippove85e3352012-12-03 15:01:43 +040053 .begin no-absolute-literals
54
Chris Zankeld1538c42012-11-16 16:16:20 -080055ENTRY(_start)
56
Max Filippove85e3352012-12-03 15:01:43 +040057 /* Preserve the pointer to the boot parameter list in EXCSAVE_1 */
58 wsr a2, excsave1
Max Filippovf6151362013-10-17 02:42:26 +040059 _j _SetupOCD
Max Filippove85e3352012-12-03 15:01:43 +040060
Chris Zankel5a0015d2005-06-23 22:01:16 -070061 .align 4
Max Filippove85e3352012-12-03 15:01:43 +040062 .literal_position
Max Filippovf6151362013-10-17 02:42:26 +040063_SetupOCD:
64 /*
65 * Initialize WB, WS, and clear PS.EXCM (to allow loop instructions).
66 * Set Interrupt Level just below XCHAL_DEBUGLEVEL to allow
67 * xt-gdb to single step via DEBUG exceptions received directly
68 * by ocd.
69 */
Max Filippov09af39f62021-07-26 07:25:28 -070070#if XCHAL_HAVE_WINDOWED
Max Filippovf6151362013-10-17 02:42:26 +040071 movi a1, 1
72 movi a0, 0
73 wsr a1, windowstart
74 wsr a0, windowbase
75 rsync
Max Filippov09af39f62021-07-26 07:25:28 -070076#endif
Max Filippovf6151362013-10-17 02:42:26 +040077
78 movi a1, LOCKLEVEL
79 wsr a1, ps
80 rsync
81
Max Filippove85e3352012-12-03 15:01:43 +040082 .global _SetupMMU
83_SetupMMU:
84 Offset = _SetupMMU - _start
85
86#ifdef CONFIG_INITIALIZE_XTENSA_MMU_INSIDE_VMLINUX
87 initialize_mmu
Max Filippovc5a771d2013-06-09 04:52:11 +040088#if defined(CONFIG_MMU) && XCHAL_HAVE_PTP_MMU && XCHAL_HAVE_SPANNING_WAY
89 rsr a2, excsave1
Max Filippov40dc9482018-11-13 23:46:42 -080090 movi a3, XCHAL_KSEG_PADDR
91 bltu a2, a3, 1f
92 sub a2, a2, a3
93 movi a3, XCHAL_KSEG_SIZE
Max Filippovc5a771d2013-06-09 04:52:11 +040094 bgeu a2, a3, 1f
Max Filippov40dc9482018-11-13 23:46:42 -080095 movi a3, XCHAL_KSEG_CACHED_VADDR
Max Filippovc5a771d2013-06-09 04:52:11 +040096 add a2, a2, a3
97 wsr a2, excsave1
981:
99#endif
Max Filippove85e3352012-12-03 15:01:43 +0400100#endif
Max Filippove85e3352012-12-03 15:01:43 +0400101
Max Filippov03760272018-12-05 12:48:19 -0800102 movi a0, _startup
Chris Zankel5a0015d2005-06-23 22:01:16 -0700103 jx a0
104
Chris Zankeld1538c42012-11-16 16:16:20 -0800105ENDPROC(_start)
Max Filippov03760272018-12-05 12:48:19 -0800106 .end no-absolute-literals
Chris Zankeld1538c42012-11-16 16:16:20 -0800107
Max Filippov49b424f2013-10-17 02:42:28 +0400108 __REF
Max Filippove85e3352012-12-03 15:01:43 +0400109 .literal_position
Chris Zankeld1538c42012-11-16 16:16:20 -0800110
111ENTRY(_startup)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700112
Chris Zankel5a0015d2005-06-23 22:01:16 -0700113 /* Set a0 to 0 for the remaining initialization. */
114
115 movi a0, 0
116
Max Filippov53490122014-10-04 05:12:27 +0400117#if XCHAL_HAVE_VECBASE
Max Filippova9f2fc62016-04-13 05:20:02 +0300118 movi a2, VECBASE_VADDR
Max Filippov53490122014-10-04 05:12:27 +0400119 wsr a2, vecbase
120#endif
121
Chris Zankel5a0015d2005-06-23 22:01:16 -0700122 /* Clear debugging registers. */
123
124#if XCHAL_HAVE_DEBUG
Max Filippovd83ff0b2013-03-04 03:40:42 +0400125#if XCHAL_NUM_IBREAK > 0
Max Filippovbc5378f2012-10-15 03:55:38 +0400126 wsr a0, ibreakenable
Max Filippovd83ff0b2013-03-04 03:40:42 +0400127#endif
Max Filippovbc5378f2012-10-15 03:55:38 +0400128 wsr a0, icount
Chris Zankel5a0015d2005-06-23 22:01:16 -0700129 movi a1, 15
Max Filippovbc5378f2012-10-15 03:55:38 +0400130 wsr a0, icountlevel
Chris Zankel5a0015d2005-06-23 22:01:16 -0700131
Chris Zankel173d6682006-12-10 02:18:48 -0800132 .set _index, 0
Max Filippov7de7ac72016-03-03 18:34:29 +0300133 .rept XCHAL_NUM_DBREAK
Max Filippovbc5378f2012-10-15 03:55:38 +0400134 wsr a0, SREG_DBREAKC + _index
Chris Zankel173d6682006-12-10 02:18:48 -0800135 .set _index, _index + 1
136 .endr
Chris Zankel5a0015d2005-06-23 22:01:16 -0700137#endif
138
139 /* Clear CCOUNT (not really necessary, but nice) */
140
Max Filippovbc5378f2012-10-15 03:55:38 +0400141 wsr a0, ccount # not really necessary, but nice
Chris Zankel5a0015d2005-06-23 22:01:16 -0700142
143 /* Disable zero-loops. */
144
145#if XCHAL_HAVE_LOOPS
Max Filippovbc5378f2012-10-15 03:55:38 +0400146 wsr a0, lcount
Chris Zankel5a0015d2005-06-23 22:01:16 -0700147#endif
148
149 /* Disable all timers. */
150
Chris Zankel173d6682006-12-10 02:18:48 -0800151 .set _index, 0
Max Filippov79fcf522012-12-11 01:26:22 +0400152 .rept XCHAL_NUM_TIMERS
Max Filippovbc5378f2012-10-15 03:55:38 +0400153 wsr a0, SREG_CCOMPARE + _index
Chris Zankel173d6682006-12-10 02:18:48 -0800154 .set _index, _index + 1
155 .endr
Chris Zankel5a0015d2005-06-23 22:01:16 -0700156
157 /* Interrupt initialization. */
158
159 movi a2, XCHAL_INTTYPE_MASK_SOFTWARE | XCHAL_INTTYPE_MASK_EXTERN_EDGE
Max Filippovbc5378f2012-10-15 03:55:38 +0400160 wsr a0, intenable
161 wsr a2, intclear
Chris Zankel5a0015d2005-06-23 22:01:16 -0700162
163 /* Disable coprocessors. */
164
Max Filippoveab5e7a2012-12-11 01:26:23 +0400165#if XCHAL_HAVE_CP
Max Filippovbc5378f2012-10-15 03:55:38 +0400166 wsr a0, cpenable
Chris Zankel5a0015d2005-06-23 22:01:16 -0700167#endif
168
Chris Zankel5a0015d2005-06-23 22:01:16 -0700169 /* Initialize the caches.
Chris Zankel173d6682006-12-10 02:18:48 -0800170 * a2, a3 are just working registers (clobbered).
Chris Zankel5a0015d2005-06-23 22:01:16 -0700171 */
172
Chris Zankel173d6682006-12-10 02:18:48 -0800173#if XCHAL_DCACHE_LINE_LOCKABLE
174 ___unlock_dcache_all a2 a3
175#endif
176
177#if XCHAL_ICACHE_LINE_LOCKABLE
178 ___unlock_icache_all a2 a3
179#endif
180
181 ___invalidate_dcache_all a2 a3
182 ___invalidate_icache_all a2 a3
183
184 isync
Chris Zankel5a0015d2005-06-23 22:01:16 -0700185
Max Filippov7bb516ca2018-08-12 06:01:40 -0700186 initialize_cacheattr
187
Max Filippovf6151362013-10-17 02:42:26 +0400188#ifdef CONFIG_HAVE_SMP
189 movi a2, CCON # MX External Register to Configure Cache
190 movi a3, 1
191 wer a3, a2
192#endif
193
194 /* Setup stack and enable window exceptions (keep irqs disabled) */
195
196 movi a1, start_info
197 l32i a1, a1, 0
198
Max Filippov0b537252021-05-01 15:32:58 -0700199 /* Disable interrupts. */
200 /* Enable window exceptions if kernel is built with windowed ABI. */
201 movi a2, KERNEL_PS_WOE_MASK | LOCKLEVEL
202 wsr a2, ps
Max Filippovf6151362013-10-17 02:42:26 +0400203 rsync
204
Max Filippovf6151362013-10-17 02:42:26 +0400205#ifdef CONFIG_SMP
206 /*
207 * Notice that we assume with SMP that cores have PRID
208 * supported by the cores.
209 */
210 rsr a2, prid
211 bnez a2, .Lboot_secondary
212
213#endif /* CONFIG_SMP */
214
Chris Zankel5a0015d2005-06-23 22:01:16 -0700215 /* Unpack data sections
216 *
217 * The linker script used to build the Linux kernel image
218 * creates a table located at __boot_reloc_table_start
Bhaskar Chowdhurye1532772021-03-25 09:38:32 +0530219 * that contains the information what data needs to be unpacked.
Chris Zankel5a0015d2005-06-23 22:01:16 -0700220 *
221 * Uses a2-a7.
222 */
223
224 movi a2, __boot_reloc_table_start
225 movi a3, __boot_reloc_table_end
226
2271: beq a2, a3, 3f # no more entries?
228 l32i a4, a2, 0 # start destination (in RAM)
Bhaskar Chowdhurye1532772021-03-25 09:38:32 +0530229 l32i a5, a2, 4 # end destination (in RAM)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700230 l32i a6, a2, 8 # start source (in ROM)
231 addi a2, a2, 12 # next entry
232 beq a4, a5, 1b # skip, empty entry
233 beq a4, a6, 1b # skip, source and dest. are the same
234
2352: l32i a7, a6, 0 # load word
236 addi a6, a6, 4
237 s32i a7, a4, 0 # store word
238 addi a4, a4, 4
239 bltu a4, a5, 2b
240 j 1b
241
2423:
243 /* All code and initialized data segments have been copied.
244 * Now clear the BSS segment.
245 */
246
Chris Zankel8b307f92010-05-01 23:05:29 -0700247 movi a2, __bss_start # start of BSS
248 movi a3, __bss_stop # end of BSS
Chris Zankel5a0015d2005-06-23 22:01:16 -0700249
Chris Zankel173d6682006-12-10 02:18:48 -0800250 __loopt a2, a3, a4, 2
Chris Zankel5a0015d2005-06-23 22:01:16 -0700251 s32i a0, a2, 0
Max Filippov50296152015-09-24 23:11:53 +0300252 __endla a2, a3, 4
Chris Zankel5a0015d2005-06-23 22:01:16 -0700253
254#if XCHAL_DCACHE_IS_WRITEBACK
255
256 /* After unpacking, flush the writeback cache to memory so the
257 * instructions/data are available.
258 */
259
Chris Zankel173d6682006-12-10 02:18:48 -0800260 ___flush_dcache_all a2 a3
Chris Zankel5a0015d2005-06-23 22:01:16 -0700261#endif
Max Filippove85e3352012-12-03 15:01:43 +0400262 memw
263 isync
264 ___invalidate_icache_all a2 a3
265 isync
Chris Zankel5a0015d2005-06-23 22:01:16 -0700266
Max Filippov7af710d2017-01-03 17:57:51 -0800267#ifdef CONFIG_XIP_KERNEL
268 /* Setup bootstrap CPU stack in XIP kernel */
269
270 movi a1, start_info
271 l32i a1, a1, 0
272#endif
273
Max Filippov0b537252021-05-01 15:32:58 -0700274 movi abi_arg0, 0
275 xsr abi_arg0, excsave1
Chris Zankel5a0015d2005-06-23 22:01:16 -0700276
277 /* init_arch kick-starts the linux kernel */
278
Max Filippov0b537252021-05-01 15:32:58 -0700279 abi_call init_arch
280 abi_call start_kernel
Chris Zankel5a0015d2005-06-23 22:01:16 -0700281
282should_never_return:
283 j should_never_return
284
Max Filippovf6151362013-10-17 02:42:26 +0400285#ifdef CONFIG_SMP
286.Lboot_secondary:
287
288 movi a2, cpu_start_ccount
2891:
Max Filippov32a7726c2018-12-21 08:26:20 -0800290 memw
Max Filippovf6151362013-10-17 02:42:26 +0400291 l32i a3, a2, 0
292 beqi a3, 0, 1b
293 movi a3, 0
294 s32i a3, a2, 0
Max Filippovf6151362013-10-17 02:42:26 +04002951:
Max Filippov32a7726c2018-12-21 08:26:20 -0800296 memw
Max Filippovf6151362013-10-17 02:42:26 +0400297 l32i a3, a2, 0
298 beqi a3, 0, 1b
299 wsr a3, ccount
300 movi a3, 0
301 s32i a3, a2, 0
302 memw
303
Max Filippov0b537252021-05-01 15:32:58 -0700304 movi abi_arg0, 0
305 wsr abi_arg0, excsave1
Max Filippovf6151362013-10-17 02:42:26 +0400306
Max Filippov0b537252021-05-01 15:32:58 -0700307 abi_call secondary_start_kernel
Max Filippovf6151362013-10-17 02:42:26 +0400308 j should_never_return
309
310#endif /* CONFIG_SMP */
311
Chris Zankeld1538c42012-11-16 16:16:20 -0800312ENDPROC(_startup)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700313
Max Filippov49b424f2013-10-17 02:42:28 +0400314#ifdef CONFIG_HOTPLUG_CPU
315
316ENTRY(cpu_restart)
317
318#if XCHAL_DCACHE_IS_WRITEBACK
319 ___flush_invalidate_dcache_all a2 a3
320#else
321 ___invalidate_dcache_all a2 a3
322#endif
323 memw
324 movi a2, CCON # MX External Register to Configure Cache
325 movi a3, 0
326 wer a3, a2
327 extw
328
329 rsr a0, prid
330 neg a2, a0
331 movi a3, cpu_start_id
Max Filippov32a7726c2018-12-21 08:26:20 -0800332 memw
Max Filippov49b424f2013-10-17 02:42:28 +0400333 s32i a2, a3, 0
334#if XCHAL_DCACHE_IS_WRITEBACK
335 dhwbi a3, 0
336#endif
3371:
Max Filippov32a7726c2018-12-21 08:26:20 -0800338 memw
Max Filippov49b424f2013-10-17 02:42:28 +0400339 l32i a2, a3, 0
340 dhi a3, 0
341 bne a2, a0, 1b
342
343 /*
344 * Initialize WB, WS, and clear PS.EXCM (to allow loop instructions).
345 * Set Interrupt Level just below XCHAL_DEBUGLEVEL to allow
346 * xt-gdb to single step via DEBUG exceptions received directly
347 * by ocd.
348 */
349 movi a1, 1
350 movi a0, 0
351 wsr a1, windowstart
352 wsr a0, windowbase
353 rsync
354
355 movi a1, LOCKLEVEL
356 wsr a1, ps
357 rsync
358
359 j _startup
360
361ENDPROC(cpu_restart)
362
363#endif /* CONFIG_HOTPLUG_CPU */
364
Chris Zankeladba09f2007-05-31 17:48:07 -0700365/*
Max Filippovf6151362013-10-17 02:42:26 +0400366 * DATA section
367 */
368
Max Filippov9fab17c2019-09-27 17:21:25 -0700369 __REFDATA
370 .align 4
Max Filippovf6151362013-10-17 02:42:26 +0400371ENTRY(start_info)
Max Filippov9fab17c2019-09-27 17:21:25 -0700372 .long init_thread_union + KERNEL_STACK_SIZE
Max Filippovf6151362013-10-17 02:42:26 +0400373
374/*
Chris Zankeladba09f2007-05-31 17:48:07 -0700375 * BSS section
376 */
377
Tim Abbott02b7da32009-09-20 18:14:14 -0400378__PAGE_ALIGNED_BSS
Johannes Weinere5083a62009-03-04 16:21:31 +0100379#ifdef CONFIG_MMU
Chris Zankeladba09f2007-05-31 17:48:07 -0700380ENTRY(swapper_pg_dir)
381 .fill PAGE_SIZE, 1, 0
Chris Zankeld1538c42012-11-16 16:16:20 -0800382END(swapper_pg_dir)
Johannes Weinere5083a62009-03-04 16:21:31 +0100383#endif
Chris Zankeladba09f2007-05-31 17:48:07 -0700384ENTRY(empty_zero_page)
385 .fill PAGE_SIZE, 1, 0
Chris Zankeld1538c42012-11-16 16:16:20 -0800386END(empty_zero_page)