blob: 359e54e83bd5c5e9541651a4ae565a5af53d03cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/head.S
3 *
4 * Copyright (C) 1994-2002 Russell King
Russell Kinge65f38e2005-06-18 09:33:31 +01005 * Copyright (c) 2003 ARM Limited
6 * All Rights Reserved
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Kernel startup code for all 32-bit CPUs
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/linkage.h>
15#include <linux/init.h>
16
17#include <asm/assembler.h>
18#include <asm/domain.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/ptrace.h>
Sam Ravnborge6ae7442005-09-09 21:08:59 +020020#include <asm/asm-offsets.h>
Nicolas Pitref09b9972005-10-29 21:44:55 +010021#include <asm/memory.h>
Russell King4f7a1812005-05-05 13:11:00 +010022#include <asm/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/system.h>
24
Jeremy Kerrc2933932010-07-07 11:19:48 +080025#ifdef CONFIG_DEBUG_LL
26#include <mach/debug-macro.S>
27#endif
28
Linus Walleijd4e1c882007-01-21 20:08:33 +010029#if (PHYS_OFFSET & 0x001fffff)
30#error "PHYS_OFFSET must be at an even 2MiB boundary!"
31#endif
32
Russell Kingf06b97f2006-12-11 22:29:16 +000033#define KERNEL_RAM_VADDR (PAGE_OFFSET + TEXT_OFFSET)
34#define KERNEL_RAM_PADDR (PHYS_OFFSET + TEXT_OFFSET)
Russell King9d4f13e2006-01-03 17:28:33 +000035
Bill Gatliff9d20fdd2007-05-31 22:02:22 +010036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
Nicolas Pitre37d07b72005-10-29 21:44:56 +010038 * swapper_pg_dir is the virtual address of the initial page table.
Russell Kingf06b97f2006-12-11 22:29:16 +000039 * We place the page tables 16K below KERNEL_RAM_VADDR. Therefore, we must
40 * make sure that KERNEL_RAM_VADDR is correctly set. Currently, we expect
Nicolas Pitre37d07b72005-10-29 21:44:56 +010041 * the least significant 16 bits to be 0x8000, but we could probably
Russell Kingf06b97f2006-12-11 22:29:16 +000042 * relax this restriction to KERNEL_RAM_VADDR >= PAGE_OFFSET + 0x4000.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
Russell Kingf06b97f2006-12-11 22:29:16 +000044#if (KERNEL_RAM_VADDR & 0xffff) != 0x8000
45#error KERNEL_RAM_VADDR must start at 0xXXXX8000
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#endif
47
48 .globl swapper_pg_dir
Russell Kingf06b97f2006-12-11 22:29:16 +000049 .equ swapper_pg_dir, KERNEL_RAM_VADDR - 0x4000
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Nicolas Pitre37d07b72005-10-29 21:44:56 +010051 .macro pgtbl, rd
Russell Kingf06b97f2006-12-11 22:29:16 +000052 ldr \rd, =(KERNEL_RAM_PADDR - 0x4000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .endm
Nicolas Pitre37d07b72005-10-29 21:44:56 +010054
55#ifdef CONFIG_XIP_KERNEL
Nicolas Pitree98ff7f2007-02-22 16:18:09 +010056#define KERNEL_START XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)
57#define KERNEL_END _edata_loc
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
Nicolas Pitree98ff7f2007-02-22 16:18:09 +010059#define KERNEL_START KERNEL_RAM_VADDR
60#define KERNEL_END _end
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
63/*
64 * Kernel startup entry point.
65 * ---------------------------
66 *
67 * This is normally called from the decompressor code. The requirements
68 * are: MMU = off, D-cache = off, I-cache = dont care, r0 = 0,
Bill Gatliff9d20fdd2007-05-31 22:02:22 +010069 * r1 = machine nr, r2 = atags pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * This code is mostly position independent, so if you link the kernel at
72 * 0xc0008000, you call this at __pa(0xc0008000).
73 *
74 * See linux/arch/arm/tools/mach-types for the complete list of machine
75 * numbers for r1.
76 *
77 * We're trying to keep crap to a minimum; DO NOT add any machine specific
78 * crap here - that's what the boot loader (or in extreme, well justified
79 * circumstances, zImage) is for.
80 */
Tim Abbott2abc1c52009-10-02 16:32:46 -040081 __HEAD
Linus Torvalds1da177e2005-04-16 15:20:36 -070082ENTRY(stext)
Catalin Marinasb86040a2009-07-24 12:32:54 +010083 setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, r9 @ ensure svc mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 @ and irqs disabled
Russell King0f44ba12006-02-24 21:04:56 +000085 mrc p15, 0, r9, c0, c0 @ get processor id
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 bl __lookup_processor_type @ r5=procinfo r9=cpuid
87 movs r10, r5 @ invalid processor (r5=0)?
Russell King3c0bdac2005-11-25 15:43:22 +000088 beq __error_p @ yes, error 'p'
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 bl __lookup_machine_type @ r5=machinfo
90 movs r8, r5 @ invalid machine (r5=0)?
91 beq __error_a @ yes, error 'a'
Russell King0eb0511d2010-11-22 12:06:28 +000092
93 /*
94 * r1 = machine no, r2 = atags,
95 * r8 = machinfo, r9 = cpuid, r10 = procinfo
96 */
Bill Gatliff9d20fdd2007-05-31 22:02:22 +010097 bl __vet_atags
Russell Kingf00ec482010-09-04 10:47:48 +010098#ifdef CONFIG_SMP_ON_UP
99 bl __fixup_smp
100#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 bl __create_page_tables
102
103 /*
104 * The following calls CPU specific code in a position independent
105 * manner. See arch/arm/mm/proc-*.S for details. r10 = base of
106 * xxx_proc_info structure selected by __lookup_machine_type
107 * above. On return, the CPU will be ready for the MMU to be
108 * turned on, and r0 will hold the CPU control register value.
109 */
Russell Kinga4ae4132010-10-04 16:22:34 +0100110 ldr r13, =__mmap_switched @ address to jump to after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 @ mmu has been enabled
Russell King00945012010-10-04 17:56:13 +0100112 adr lr, BSYM(1f) @ return (PIC) address
Catalin Marinasb86040a2009-07-24 12:32:54 +0100113 ARM( add pc, r10, #PROCINFO_INITFUNC )
114 THUMB( add r12, r10, #PROCINFO_INITFUNC )
115 THUMB( mov pc, r12 )
Russell King00945012010-10-04 17:56:13 +01001161: b __enable_mmu
Catalin Marinas93ed3972008-08-28 11:22:32 +0100117ENDPROC(stext)
Russell Kinga4ae4132010-10-04 16:22:34 +0100118 .ltorg
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * Setup the initial page tables. We only setup the barest
122 * amount which are required to get the kernel running, which
123 * generally means mapping in the kernel code.
124 *
125 * r8 = machinfo
126 * r9 = cpuid
127 * r10 = procinfo
128 *
129 * Returns:
Russell King786f1b72010-10-04 17:51:54 +0100130 * r0, r3, r5-r7 corrupted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * r4 = physical page table address
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133__create_page_tables:
Nicolas Pitre37d07b72005-10-29 21:44:56 +0100134 pgtbl r4 @ page table address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /*
137 * Clear the 16K level 1 swapper page table
138 */
139 mov r0, r4
140 mov r3, #0
141 add r6, r0, #0x4000
1421: str r3, [r0], #4
143 str r3, [r0], #4
144 str r3, [r0], #4
145 str r3, [r0], #4
146 teq r0, r6
147 bne 1b
148
Russell King8799ee92006-06-29 18:24:21 +0100149 ldr r7, [r10, #PROCINFO_MM_MMUFLAGS] @ mm_mmuflags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /*
Russell King786f1b72010-10-04 17:51:54 +0100152 * Create identity mapping to cater for __enable_mmu.
153 * This identity mapping will be removed by paging_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Russell King786f1b72010-10-04 17:51:54 +0100155 adr r0, __enable_mmu_loc
156 ldmia r0, {r3, r5, r6}
157 sub r0, r0, r3 @ virt->phys offset
158 add r5, r5, r0 @ phys __enable_mmu
159 add r6, r6, r0 @ phys __enable_mmu_end
160 mov r5, r5, lsr #20
161 mov r6, r6, lsr #20
162
1631: orr r3, r7, r5, lsl #20 @ flags + kernel base
164 str r3, [r4, r5, lsl #2] @ identity mapping
165 teq r5, r6
166 addne r5, r5, #1 @ next section
167 bne 1b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /*
170 * Now setup the pagetables for our kernel direct
Lennert Buytenhek2552fc22006-09-29 21:14:05 +0100171 * mapped region.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Russell King786f1b72010-10-04 17:51:54 +0100173 mov r3, pc
174 mov r3, r3, lsr #20
175 orr r3, r7, r3, lsl #20
Nicolas Pitree98ff7f2007-02-22 16:18:09 +0100176 add r0, r4, #(KERNEL_START & 0xff000000) >> 18
177 str r3, [r0, #(KERNEL_START & 0x00f00000) >> 18]!
178 ldr r6, =(KERNEL_END - 1)
179 add r0, r0, #4
180 add r6, r4, r6, lsr #18
1811: cmp r0, r6
182 add r3, r3, #1 << 20
183 strls r3, [r0], #4
184 bls 1b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Nicolas Pitreec3622d2007-02-21 15:32:28 +0100186#ifdef CONFIG_XIP_KERNEL
187 /*
188 * Map some ram to cover our .data and .bss areas.
189 */
190 orr r3, r7, #(KERNEL_RAM_PADDR & 0xff000000)
Nicolas Pitre40435792007-02-21 15:58:13 +0100191 .if (KERNEL_RAM_PADDR & 0x00f00000)
Nicolas Pitreec3622d2007-02-21 15:32:28 +0100192 orr r3, r3, #(KERNEL_RAM_PADDR & 0x00f00000)
Nicolas Pitre40435792007-02-21 15:58:13 +0100193 .endif
Nicolas Pitreec3622d2007-02-21 15:32:28 +0100194 add r0, r4, #(KERNEL_RAM_VADDR & 0xff000000) >> 18
195 str r3, [r0, #(KERNEL_RAM_VADDR & 0x00f00000) >> 18]!
196 ldr r6, =(_end - 1)
197 add r0, r0, #4
198 add r6, r4, r6, lsr #18
1991: cmp r0, r6
200 add r3, r3, #1 << 20
201 strls r3, [r0], #4
202 bls 1b
203#endif
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /*
206 * Then map first 1MB of ram in case it contains our boot params.
207 */
Nicolas Pitref09b9972005-10-29 21:44:55 +0100208 add r0, r4, #PAGE_OFFSET >> 18
Linus Walleijd4e1c882007-01-21 20:08:33 +0100209 orr r6, r7, #(PHYS_OFFSET & 0xff000000)
Nicolas Pitre40435792007-02-21 15:58:13 +0100210 .if (PHYS_OFFSET & 0x00f00000)
211 orr r6, r6, #(PHYS_OFFSET & 0x00f00000)
212 .endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 str r6, [r0]
214
Russell Kingc77b0422005-07-01 11:56:55 +0100215#ifdef CONFIG_DEBUG_LL
Jeremy Kerrc2933932010-07-07 11:19:48 +0800216#ifndef CONFIG_DEBUG_ICEDCC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * Map in IO space for serial debugging.
219 * This allows debug messages to be output
220 * via a serial console before paging_init.
221 */
Jeremy Kerrc2933932010-07-07 11:19:48 +0800222 addruart r7, r3
223
224 mov r3, r3, lsr #20
225 mov r3, r3, lsl #2
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 add r0, r4, r3
228 rsb r3, r3, #0x4000 @ PTRS_PER_PGD*sizeof(long)
229 cmp r3, #0x0800 @ limit to 512MB
230 movhi r3, #0x0800
231 add r6, r0, r3
Jeremy Kerrc2933932010-07-07 11:19:48 +0800232 mov r3, r7, lsr #20
233 ldr r7, [r10, #PROCINFO_IO_MMUFLAGS] @ io_mmuflags
234 orr r3, r7, r3, lsl #20
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351: str r3, [r0], #4
236 add r3, r3, #1 << 20
237 teq r0, r6
238 bne 1b
Jeremy Kerrc2933932010-07-07 11:19:48 +0800239
240#else /* CONFIG_DEBUG_ICEDCC */
241 /* we don't need any serial debugging mappings for ICEDCC */
242 ldr r7, [r10, #PROCINFO_IO_MMUFLAGS] @ io_mmuflags
243#endif /* !CONFIG_DEBUG_ICEDCC */
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#if defined(CONFIG_ARCH_NETWINDER) || defined(CONFIG_ARCH_CATS)
246 /*
Russell King3c0bdac2005-11-25 15:43:22 +0000247 * If we're using the NetWinder or CATS, we also need to map
248 * in the 16550-type serial port for the debug messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Russell Kingc77b0422005-07-01 11:56:55 +0100250 add r0, r4, #0xff000000 >> 18
251 orr r3, r7, #0x7c000000
252 str r3, [r0]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#ifdef CONFIG_ARCH_RPC
255 /*
256 * Map in screen at 0x02000000 & SCREEN2_BASE
257 * Similar reasons here - for debug. This is
258 * only for Acorn RiscPC architectures.
259 */
Russell Kingc77b0422005-07-01 11:56:55 +0100260 add r0, r4, #0x02000000 >> 18
261 orr r3, r7, #0x02000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 str r3, [r0]
Russell Kingc77b0422005-07-01 11:56:55 +0100263 add r0, r4, #0xd8000000 >> 18
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 str r3, [r0]
265#endif
Russell Kingc77b0422005-07-01 11:56:55 +0100266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100268ENDPROC(__create_page_tables)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .ltorg
Russell King786f1b72010-10-04 17:51:54 +0100270__enable_mmu_loc:
271 .long .
272 .long __enable_mmu
273 .long __enable_mmu_end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Russell King00945012010-10-04 17:56:13 +0100275#if defined(CONFIG_SMP)
276 __CPUINIT
277ENTRY(secondary_startup)
278 /*
279 * Common entry point for secondary CPUs.
280 *
281 * Ensure that we're in SVC mode, and IRQs are disabled. Lookup
282 * the processor type - there is no need to check the machine type
283 * as it has already been validated by the primary processor.
284 */
285 setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, r9
286 mrc p15, 0, r9, c0, c0 @ get processor id
287 bl __lookup_processor_type
288 movs r10, r5 @ invalid processor?
289 moveq r0, #'p' @ yes, error 'p'
290 beq __error_p
291
292 /*
293 * Use the page tables supplied from __cpu_up.
294 */
295 adr r4, __secondary_data
296 ldmia r4, {r5, r7, r12} @ address to jump to after
297 sub r4, r4, r5 @ mmu has been enabled
298 ldr r4, [r7, r4] @ get secondary_data.pgdir
299 adr lr, BSYM(__enable_mmu) @ return address
300 mov r13, r12 @ __secondary_switched address
301 ARM( add pc, r10, #PROCINFO_INITFUNC ) @ initialise processor
302 @ (return control reg)
303 THUMB( add r12, r10, #PROCINFO_INITFUNC )
304 THUMB( mov pc, r12 )
305ENDPROC(secondary_startup)
306
307 /*
308 * r6 = &secondary_data
309 */
310ENTRY(__secondary_switched)
311 ldr sp, [r7, #4] @ get secondary_data.stack
312 mov fp, #0
313 b secondary_start_kernel
314ENDPROC(__secondary_switched)
315
316 .type __secondary_data, %object
317__secondary_data:
318 .long .
319 .long secondary_data
320 .long __secondary_switched
321#endif /* defined(CONFIG_SMP) */
322
323
324
325/*
326 * Setup common bits before finally enabling the MMU. Essentially
327 * this is just loading the page table pointer and domain access
328 * registers.
Russell King865a4fa2010-10-04 18:02:59 +0100329 *
330 * r0 = cp#15 control register
331 * r1 = machine ID
332 * r2 = atags pointer
333 * r4 = page table pointer
334 * r9 = processor ID
335 * r13 = *virtual* address to jump to upon completion
Russell King00945012010-10-04 17:56:13 +0100336 */
337__enable_mmu:
338#ifdef CONFIG_ALIGNMENT_TRAP
339 orr r0, r0, #CR_A
340#else
341 bic r0, r0, #CR_A
342#endif
343#ifdef CONFIG_CPU_DCACHE_DISABLE
344 bic r0, r0, #CR_C
345#endif
346#ifdef CONFIG_CPU_BPREDICT_DISABLE
347 bic r0, r0, #CR_Z
348#endif
349#ifdef CONFIG_CPU_ICACHE_DISABLE
350 bic r0, r0, #CR_I
351#endif
352 mov r5, #(domain_val(DOMAIN_USER, DOMAIN_MANAGER) | \
353 domain_val(DOMAIN_KERNEL, DOMAIN_MANAGER) | \
354 domain_val(DOMAIN_TABLE, DOMAIN_MANAGER) | \
355 domain_val(DOMAIN_IO, DOMAIN_CLIENT))
356 mcr p15, 0, r5, c3, c0, 0 @ load domain access register
357 mcr p15, 0, r4, c2, c0, 0 @ load page table pointer
358 b __turn_mmu_on
359ENDPROC(__enable_mmu)
360
361/*
362 * Enable the MMU. This completely changes the structure of the visible
363 * memory space. You will not be able to trace execution through this.
364 * If you have an enquiry about this, *please* check the linux-arm-kernel
365 * mailing list archives BEFORE sending another post to the list.
366 *
367 * r0 = cp#15 control register
Russell King865a4fa2010-10-04 18:02:59 +0100368 * r1 = machine ID
369 * r2 = atags pointer
370 * r9 = processor ID
Russell King00945012010-10-04 17:56:13 +0100371 * r13 = *virtual* address to jump to upon completion
372 *
373 * other registers depend on the function called upon completion
374 */
375 .align 5
376__turn_mmu_on:
377 mov r0, r0
378 mcr p15, 0, r0, c1, c0, 0 @ write control reg
379 mrc p15, 0, r3, c0, c0, 0 @ read id reg
380 mov r3, r3
381 mov r3, r13
382 mov pc, r3
383__enable_mmu_end:
384ENDPROC(__turn_mmu_on)
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Russell Kingf00ec482010-09-04 10:47:48 +0100387#ifdef CONFIG_SMP_ON_UP
388__fixup_smp:
Russell King0eb0511d2010-11-22 12:06:28 +0000389 mov r4, #0x00070000
390 orr r3, r4, #0xff000000 @ mask 0xff070000
391 orr r4, r4, #0x41000000 @ val 0x41070000
392 and r0, r9, r3
393 teq r0, r4 @ ARM CPU and ARMv6/v7?
Russell Kingf00ec482010-09-04 10:47:48 +0100394 bne __fixup_smp_on_up @ no, assume UP
395
Russell King0eb0511d2010-11-22 12:06:28 +0000396 orr r3, r3, #0x0000ff00
397 orr r3, r3, #0x000000f0 @ mask 0xff07fff0
398 orr r4, r4, #0x0000b000
399 orr r4, r4, #0x00000020 @ val 0x4107b020
400 and r0, r9, r3
401 teq r0, r4 @ ARM 11MPCore?
Russell Kingf00ec482010-09-04 10:47:48 +0100402 moveq pc, lr @ yes, assume SMP
403
404 mrc p15, 0, r0, c0, c0, 5 @ read MPIDR
405 tst r0, #1 << 31
406 movne pc, lr @ bit 31 => SMP
407
408__fixup_smp_on_up:
409 adr r0, 1f
Russell King0eb0511d2010-11-22 12:06:28 +0000410 ldmia r0, {r3 - r5}
Russell Kingf00ec482010-09-04 10:47:48 +0100411 sub r3, r0, r3
Russell King0eb0511d2010-11-22 12:06:28 +0000412 add r4, r4, r3
413 add r5, r5, r3
4142: cmp r4, r5
Dave Martined3768a2010-12-01 15:39:23 +0100415 movhs pc, lr
Russell King0eb0511d2010-11-22 12:06:28 +0000416 ldmia r4!, {r0, r6}
Dave Martined3768a2010-12-01 15:39:23 +0100417 ARM( str r6, [r0, r3] )
418 THUMB( add r0, r0, r3 )
419#ifdef __ARMEB__
420 THUMB( mov r6, r6, ror #16 ) @ Convert word order for big-endian.
421#endif
422 THUMB( strh r6, [r0], #2 ) @ For Thumb-2, store as two halfwords
423 THUMB( mov r6, r6, lsr #16 ) @ to be robust against misaligned r3.
424 THUMB( strh r6, [r0] )
425 b 2b
Russell Kingf00ec482010-09-04 10:47:48 +0100426ENDPROC(__fixup_smp)
427
4281: .word .
429 .word __smpalt_begin
430 .word __smpalt_end
431
432 .pushsection .data
433 .globl smp_on_up
434smp_on_up:
435 ALT_SMP(.long 1)
436 ALT_UP(.long 0)
437 .popsection
438
439#endif
440
Hyok S. Choi75d90832006-03-27 14:58:25 +0100441#include "head-common.S"