blob: 571d6d58491922e93f071ce31cd1b327272b231c [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 */
14#include <linux/config.h>
15#include <linux/linkage.h>
16#include <linux/init.h>
17
18#include <asm/assembler.h>
19#include <asm/domain.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/procinfo.h>
21#include <asm/ptrace.h>
Sam Ravnborge6ae7442005-09-09 21:08:59 +020022#include <asm/asm-offsets.h>
Nicolas Pitref09b9972005-10-29 21:44:55 +010023#include <asm/memory.h>
Russell King4f7a1812005-05-05 13:11:00 +010024#include <asm/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/system.h>
26
27#define PROCINFO_MMUFLAGS 8
28#define PROCINFO_INITFUNC 12
29
30#define MACHINFO_TYPE 0
31#define MACHINFO_PHYSRAM 4
32#define MACHINFO_PHYSIO 8
33#define MACHINFO_PGOFFIO 12
34#define MACHINFO_NAME 16
35
Russell King9d4f13e2006-01-03 17:28:33 +000036#define KERNEL_RAM_ADDR (PAGE_OFFSET + TEXT_OFFSET)
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
Nicolas Pitre37d07b72005-10-29 21:44:56 +010039 * swapper_pg_dir is the virtual address of the initial page table.
40 * We place the page tables 16K below KERNEL_RAM_ADDR. Therefore, we must
41 * make sure that KERNEL_RAM_ADDR is correctly set. Currently, we expect
42 * the least significant 16 bits to be 0x8000, but we could probably
43 * relax this restriction to KERNEL_RAM_ADDR >= PAGE_OFFSET + 0x4000.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
Nicolas Pitre37d07b72005-10-29 21:44:56 +010045#if (KERNEL_RAM_ADDR & 0xffff) != 0x8000
46#error KERNEL_RAM_ADDR must start at 0xXXXX8000
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
48
49 .globl swapper_pg_dir
Nicolas Pitre37d07b72005-10-29 21:44:56 +010050 .equ swapper_pg_dir, KERNEL_RAM_ADDR - 0x4000
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Nicolas Pitre37d07b72005-10-29 21:44:56 +010052 .macro pgtbl, rd
53 ldr \rd, =(__virt_to_phys(KERNEL_RAM_ADDR - 0x4000))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .endm
Nicolas Pitre37d07b72005-10-29 21:44:56 +010055
56#ifdef CONFIG_XIP_KERNEL
57#define TEXTADDR XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
Nicolas Pitre37d07b72005-10-29 21:44:56 +010059#define TEXTADDR KERNEL_RAM_ADDR
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
62/*
63 * Kernel startup entry point.
64 * ---------------------------
65 *
66 * This is normally called from the decompressor code. The requirements
67 * are: MMU = off, D-cache = off, I-cache = dont care, r0 = 0,
68 * r1 = machine nr.
69 *
70 * This code is mostly position independent, so if you link the kernel at
71 * 0xc0008000, you call this at __pa(0xc0008000).
72 *
73 * See linux/arch/arm/tools/mach-types for the complete list of machine
74 * numbers for r1.
75 *
76 * We're trying to keep crap to a minimum; DO NOT add any machine specific
77 * crap here - that's what the boot loader (or in extreme, well justified
78 * circumstances, zImage) is for.
79 */
80 __INIT
81 .type stext, %function
82ENTRY(stext)
83 msr cpsr_c, #PSR_F_BIT | PSR_I_BIT | MODE_SVC @ ensure svc mode
84 @ 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'
92 bl __create_page_tables
93
94 /*
95 * The following calls CPU specific code in a position independent
96 * manner. See arch/arm/mm/proc-*.S for details. r10 = base of
97 * xxx_proc_info structure selected by __lookup_machine_type
98 * above. On return, the CPU will be ready for the MMU to be
99 * turned on, and r0 will hold the CPU control register value.
100 */
101 ldr r13, __switch_data @ address to jump to after
102 @ mmu has been enabled
103 adr lr, __enable_mmu @ return (PIC) address
104 add pc, r10, #PROCINFO_INITFUNC
105
106 .type __switch_data, %object
107__switch_data:
108 .long __mmap_switched
109 .long __data_loc @ r4
110 .long __data_start @ r5
111 .long __bss_start @ r6
112 .long _end @ r7
113 .long processor_id @ r4
114 .long __machine_arch_type @ r5
115 .long cr_alignment @ r6
Russell King4f7a1812005-05-05 13:11:00 +0100116 .long init_thread_union + THREAD_START_SP @ sp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
119 * The following fragment of code is executed with the MMU on, and uses
120 * absolute addresses; this is not position independent.
121 *
122 * r0 = cp#15 control register
123 * r1 = machine ID
124 * r9 = processor ID
125 */
126 .type __mmap_switched, %function
127__mmap_switched:
128 adr r3, __switch_data + 4
129
130 ldmia r3!, {r4, r5, r6, r7}
131 cmp r4, r5 @ Copy data segment if needed
1321: cmpne r5, r6
133 ldrne fp, [r4], #4
134 strne fp, [r5], #4
135 bne 1b
136
137 mov fp, #0 @ Clear BSS (and zero fp)
1381: cmp r6, r7
139 strcc fp, [r6],#4
140 bcc 1b
141
142 ldmia r3, {r4, r5, r6, sp}
143 str r9, [r4] @ Save processor ID
144 str r1, [r5] @ Save machine type
145 bic r4, r0, #CR_A @ Clear 'A' bit
146 stmia r6, {r0, r4} @ Save control register values
147 b start_kernel
148
Russell Kinge65f38e2005-06-18 09:33:31 +0100149#if defined(CONFIG_SMP)
150 .type secondary_startup, #function
151ENTRY(secondary_startup)
152 /*
153 * Common entry point for secondary CPUs.
154 *
155 * Ensure that we're in SVC mode, and IRQs are disabled. Lookup
156 * the processor type - there is no need to check the machine type
157 * as it has already been validated by the primary processor.
158 */
159 msr cpsr_c, #PSR_F_BIT | PSR_I_BIT | MODE_SVC
Russell King0f44ba12006-02-24 21:04:56 +0000160 mrc p15, 0, r9, c0, c0 @ get processor id
Russell Kinge65f38e2005-06-18 09:33:31 +0100161 bl __lookup_processor_type
162 movs r10, r5 @ invalid processor?
163 moveq r0, #'p' @ yes, error 'p'
164 beq __error
165
166 /*
167 * Use the page tables supplied from __cpu_up.
168 */
169 adr r4, __secondary_data
170 ldmia r4, {r5, r6, r13} @ address to jump to after
171 sub r4, r4, r5 @ mmu has been enabled
172 ldr r4, [r6, r4] @ get secondary_data.pgdir
173 adr lr, __enable_mmu @ return address
174 add pc, r10, #12 @ initialise processor
175 @ (return control reg)
176
177 /*
178 * r6 = &secondary_data
179 */
180ENTRY(__secondary_switched)
181 ldr sp, [r6, #4] @ get secondary_data.stack
182 mov fp, #0
183 b secondary_start_kernel
184
185 .type __secondary_data, %object
186__secondary_data:
187 .long .
188 .long secondary_data
189 .long __secondary_switched
190#endif /* defined(CONFIG_SMP) */
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193
194/*
195 * Setup common bits before finally enabling the MMU. Essentially
196 * this is just loading the page table pointer and domain access
197 * registers.
198 */
199 .type __enable_mmu, %function
200__enable_mmu:
201#ifdef CONFIG_ALIGNMENT_TRAP
202 orr r0, r0, #CR_A
203#else
204 bic r0, r0, #CR_A
205#endif
206#ifdef CONFIG_CPU_DCACHE_DISABLE
207 bic r0, r0, #CR_C
208#endif
209#ifdef CONFIG_CPU_BPREDICT_DISABLE
210 bic r0, r0, #CR_Z
211#endif
212#ifdef CONFIG_CPU_ICACHE_DISABLE
213 bic r0, r0, #CR_I
214#endif
215 mov r5, #(domain_val(DOMAIN_USER, DOMAIN_MANAGER) | \
216 domain_val(DOMAIN_KERNEL, DOMAIN_MANAGER) | \
217 domain_val(DOMAIN_TABLE, DOMAIN_MANAGER) | \
218 domain_val(DOMAIN_IO, DOMAIN_CLIENT))
219 mcr p15, 0, r5, c3, c0, 0 @ load domain access register
220 mcr p15, 0, r4, c2, c0, 0 @ load page table pointer
221 b __turn_mmu_on
222
223/*
224 * Enable the MMU. This completely changes the structure of the visible
225 * memory space. You will not be able to trace execution through this.
226 * If you have an enquiry about this, *please* check the linux-arm-kernel
227 * mailing list archives BEFORE sending another post to the list.
228 *
229 * r0 = cp#15 control register
230 * r13 = *virtual* address to jump to upon completion
231 *
232 * other registers depend on the function called upon completion
233 */
234 .align 5
235 .type __turn_mmu_on, %function
236__turn_mmu_on:
237 mov r0, r0
238 mcr p15, 0, r0, c1, c0, 0 @ write control reg
239 mrc p15, 0, r3, c0, c0, 0 @ read id reg
240 mov r3, r3
241 mov r3, r3
242 mov pc, r13
243
244
245
246/*
247 * Setup the initial page tables. We only setup the barest
248 * amount which are required to get the kernel running, which
249 * generally means mapping in the kernel code.
250 *
251 * r8 = machinfo
252 * r9 = cpuid
253 * r10 = procinfo
254 *
255 * Returns:
Nicolas Pitre2df96b32006-01-13 20:51:46 +0000256 * r0, r3, r6, r7 corrupted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * r4 = physical page table address
258 */
259 .type __create_page_tables, %function
260__create_page_tables:
Nicolas Pitre37d07b72005-10-29 21:44:56 +0100261 pgtbl r4 @ page table address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /*
264 * Clear the 16K level 1 swapper page table
265 */
266 mov r0, r4
267 mov r3, #0
268 add r6, r0, #0x4000
2691: str r3, [r0], #4
270 str r3, [r0], #4
271 str r3, [r0], #4
272 str r3, [r0], #4
273 teq r0, r6
274 bne 1b
275
276 ldr r7, [r10, #PROCINFO_MMUFLAGS] @ mmuflags
277
278 /*
279 * Create identity mapping for first MB of kernel to
280 * cater for the MMU enable. This identity mapping
281 * will be removed by paging_init(). We use our current program
282 * counter to determine corresponding section base address.
283 */
284 mov r6, pc, lsr #20 @ start of kernel section
285 orr r3, r7, r6, lsl #20 @ flags + kernel base
286 str r3, [r4, r6, lsl #2] @ identity mapping
287
288 /*
289 * Now setup the pagetables for our kernel direct
290 * mapped region. We round TEXTADDR down to the
291 * nearest megabyte boundary. It is assumed that
292 * the kernel fits within 4 contigous 1MB sections.
293 */
294 add r0, r4, #(TEXTADDR & 0xff000000) >> 18 @ start of kernel
295 str r3, [r0, #(TEXTADDR & 0x00f00000) >> 18]!
296 add r3, r3, #1 << 20
297 str r3, [r0, #4]! @ KERNEL + 1MB
298 add r3, r3, #1 << 20
299 str r3, [r0, #4]! @ KERNEL + 2MB
300 add r3, r3, #1 << 20
301 str r3, [r0, #4] @ KERNEL + 3MB
302
303 /*
304 * Then map first 1MB of ram in case it contains our boot params.
305 */
Nicolas Pitref09b9972005-10-29 21:44:55 +0100306 add r0, r4, #PAGE_OFFSET >> 18
Nicolas Pitre2df96b32006-01-13 20:51:46 +0000307 orr r6, r7, #PHYS_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 str r6, [r0]
309
310#ifdef CONFIG_XIP_KERNEL
311 /*
312 * Map some ram to cover our .data and .bss areas.
313 * Mapping 3MB should be plenty.
314 */
Nicolas Pitre2df96b32006-01-13 20:51:46 +0000315 sub r3, r4, #PHYS_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 mov r3, r3, lsr #20
317 add r0, r0, r3, lsl #2
318 add r6, r6, r3, lsl #20
319 str r6, [r0], #4
320 add r6, r6, #(1 << 20)
321 str r6, [r0], #4
322 add r6, r6, #(1 << 20)
323 str r6, [r0]
324#endif
325
Russell Kingc77b0422005-07-01 11:56:55 +0100326#ifdef CONFIG_DEBUG_LL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 bic r7, r7, #0x0c @ turn off cacheable
328 @ and bufferable bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /*
330 * Map in IO space for serial debugging.
331 * This allows debug messages to be output
332 * via a serial console before paging_init.
333 */
334 ldr r3, [r8, #MACHINFO_PGOFFIO]
335 add r0, r4, r3
336 rsb r3, r3, #0x4000 @ PTRS_PER_PGD*sizeof(long)
337 cmp r3, #0x0800 @ limit to 512MB
338 movhi r3, #0x0800
339 add r6, r0, r3
340 ldr r3, [r8, #MACHINFO_PHYSIO]
341 orr r3, r3, r7
3421: str r3, [r0], #4
343 add r3, r3, #1 << 20
344 teq r0, r6
345 bne 1b
346#if defined(CONFIG_ARCH_NETWINDER) || defined(CONFIG_ARCH_CATS)
347 /*
Russell King3c0bdac2005-11-25 15:43:22 +0000348 * If we're using the NetWinder or CATS, we also need to map
349 * in the 16550-type serial port for the debug messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Russell Kingc77b0422005-07-01 11:56:55 +0100351 add r0, r4, #0xff000000 >> 18
352 orr r3, r7, #0x7c000000
353 str r3, [r0]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355#ifdef CONFIG_ARCH_RPC
356 /*
357 * Map in screen at 0x02000000 & SCREEN2_BASE
358 * Similar reasons here - for debug. This is
359 * only for Acorn RiscPC architectures.
360 */
Russell Kingc77b0422005-07-01 11:56:55 +0100361 add r0, r4, #0x02000000 >> 18
362 orr r3, r7, #0x02000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 str r3, [r0]
Russell Kingc77b0422005-07-01 11:56:55 +0100364 add r0, r4, #0xd8000000 >> 18
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 str r3, [r0]
366#endif
Russell Kingc77b0422005-07-01 11:56:55 +0100367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 mov pc, lr
369 .ltorg
370
371
372
373/*
374 * Exception handling. Something went wrong and we can't proceed. We
375 * ought to tell the user, but since we don't have any guarantee that
376 * we're even running on the right architecture, we do virtually nothing.
377 *
378 * If CONFIG_DEBUG_LL is set we try to print out something about the error
379 * and hope for the best (useful if bootloader fails to pass a proper
380 * machine ID for example).
381 */
382
383 .type __error_p, %function
384__error_p:
385#ifdef CONFIG_DEBUG_LL
386 adr r0, str_p1
387 bl printascii
388 b __error
389str_p1: .asciz "\nError: unrecognized/unsupported processor variant.\n"
390 .align
391#endif
392
393 .type __error_a, %function
394__error_a:
395#ifdef CONFIG_DEBUG_LL
396 mov r4, r1 @ preserve machine ID
397 adr r0, str_a1
398 bl printascii
399 mov r0, r4
400 bl printhex8
401 adr r0, str_a2
402 bl printascii
403 adr r3, 3f
404 ldmia r3, {r4, r5, r6} @ get machine desc list
405 sub r4, r3, r4 @ get offset between virt&phys
406 add r5, r5, r4 @ convert virt addresses to
407 add r6, r6, r4 @ physical address space
4081: ldr r0, [r5, #MACHINFO_TYPE] @ get machine type
409 bl printhex8
410 mov r0, #'\t'
411 bl printch
412 ldr r0, [r5, #MACHINFO_NAME] @ get machine name
413 add r0, r0, r4
414 bl printascii
415 mov r0, #'\n'
416 bl printch
417 add r5, r5, #SIZEOF_MACHINE_DESC @ next machine_desc
418 cmp r5, r6
419 blo 1b
420 adr r0, str_a3
421 bl printascii
422 b __error
423str_a1: .asciz "\nError: unrecognized/unsupported machine ID (r1 = 0x"
424str_a2: .asciz ").\n\nAvailable machine support:\n\nID (hex)\tNAME\n"
425str_a3: .asciz "\nPlease check your kernel config and/or bootloader.\n"
426 .align
427#endif
428
429 .type __error, %function
430__error:
431#ifdef CONFIG_ARCH_RPC
432/*
433 * Turn the screen red on a error - RiscPC only.
434 */
435 mov r0, #0x02000000
436 mov r3, #0x11
437 orr r3, r3, r3, lsl #8
438 orr r3, r3, r3, lsl #16
439 str r3, [r0], #4
440 str r3, [r0], #4
441 str r3, [r0], #4
442 str r3, [r0], #4
443#endif
4441: mov r0, r0
445 b 1b
446
447
448/*
449 * Read processor ID register (CP#15, CR0), and look up in the linker-built
450 * supported processor list. Note that we can't use the absolute addresses
451 * for the __proc_info lists since we aren't running with the MMU on
452 * (and therefore, we are not in the correct address space). We have to
453 * calculate the offset.
454 *
Russell King0f44ba12006-02-24 21:04:56 +0000455 * r9 = cpuid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Returns:
457 * r3, r4, r6 corrupted
458 * r5 = proc_info pointer in physical address space
Russell King0f44ba12006-02-24 21:04:56 +0000459 * r9 = cpuid (preserved)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
461 .type __lookup_processor_type, %function
462__lookup_processor_type:
463 adr r3, 3f
Russell King0f44ba12006-02-24 21:04:56 +0000464 ldmda r3, {r5 - r7}
465 sub r3, r3, r7 @ get offset between virt&phys
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 add r5, r5, r3 @ convert virt addresses to
467 add r6, r6, r3 @ physical address space
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681: ldmia r5, {r3, r4} @ value, mask
469 and r4, r4, r9 @ mask wanted bits
470 teq r3, r4
471 beq 2f
472 add r5, r5, #PROC_INFO_SZ @ sizeof(proc_info_list)
473 cmp r5, r6
474 blo 1b
475 mov r5, #0 @ unknown processor
4762: mov pc, lr
477
478/*
479 * This provides a C-API version of the above function.
480 */
481ENTRY(lookup_processor_type)
Russell King0f44ba12006-02-24 21:04:56 +0000482 stmfd sp!, {r4 - r7, r9, lr}
483 mov r9, r0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 bl __lookup_processor_type
485 mov r0, r5
Russell King0f44ba12006-02-24 21:04:56 +0000486 ldmfd sp!, {r4 - r7, r9, pc}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/*
489 * Look in include/asm-arm/procinfo.h and arch/arm/kernel/arch.[ch] for
490 * more information about the __proc_info and __arch_info structures.
491 */
492 .long __proc_info_begin
493 .long __proc_info_end
4943: .long .
495 .long __arch_info_begin
496 .long __arch_info_end
497
498/*
499 * Lookup machine architecture in the linker-build list of architectures.
500 * Note that we can't use the absolute addresses for the __arch_info
501 * lists since we aren't running with the MMU on (and therefore, we are
502 * not in the correct address space). We have to calculate the offset.
503 *
504 * r1 = machine architecture number
505 * Returns:
506 * r3, r4, r6 corrupted
507 * r5 = mach_info pointer in physical address space
508 */
509 .type __lookup_machine_type, %function
510__lookup_machine_type:
511 adr r3, 3b
512 ldmia r3, {r4, r5, r6}
513 sub r3, r3, r4 @ get offset between virt&phys
514 add r5, r5, r3 @ convert virt addresses to
515 add r6, r6, r3 @ physical address space
5161: ldr r3, [r5, #MACHINFO_TYPE] @ get machine type
517 teq r3, r1 @ matches loader number?
518 beq 2f @ found
519 add r5, r5, #SIZEOF_MACHINE_DESC @ next machine_desc
520 cmp r5, r6
521 blo 1b
522 mov r5, #0 @ unknown machine
5232: mov pc, lr
524
525/*
526 * This provides a C-API version of the above function.
527 */
528ENTRY(lookup_machine_type)
529 stmfd sp!, {r4 - r6, lr}
530 mov r1, r0
531 bl __lookup_machine_type
532 mov r0, r5
533 ldmfd sp!, {r4 - r6, pc}