2 * Copyright 2004-2014 Olivier Houchard <cognet@FreeBSD.org>
3 * Copyright 2012-2014 Ian Lepore <ian@FreeBSD.org>
4 * Copyright 2013-2014 Andrew Turner <andrew@FreeBSD.org>
5 * Copyright 2014 Svatopluk Kraus <onwahe@gmail.com>
6 * Copyright 2014 Michal Meloun <meloun@miracle.cz>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/syscall.h>
33 #include <machine/asm.h>
34 #include <machine/asmacros.h>
35 #include <machine/armreg.h>
36 #include <machine/sysreg.h>
37 #include <machine/cpuconf.h>
38 #include <machine/pte.h>
40 __FBSDID("$FreeBSD$");
43 #define PTE1_OFFSET L1_S_OFFSET
44 #define PTE1_SHIFT L1_S_SHIFT
45 #define PTE1_SIZE L1_S_SIZE
48 /* A small statically-allocated stack used only during initarm() and AP startup. */
49 #define INIT_ARM_STACK_SIZE 2048
55 * On entry for FreeBSD boot ABI:
56 * r0 - metadata pointer or 0 (boothowto on AT91's boot2)
57 * r1 - if (r0 == 0) then metadata pointer
58 * On entry for Linux boot ABI:
60 * r1 - machine type (passed as arg2 to initarm)
61 * r2 - Pointer to a tagged list or dtb image (phys addr) (passed as arg1 initarm)
63 * For both types of boot we gather up the args, put them in a struct arm_boot_params
64 * structure and pass that to initarm.
69 STOP_UNWINDING /* Can't unwind into the bootloader! */
71 /* Make sure interrupts are disabled. */
74 mov r8, r0 /* 0 or boot mode from boot2 */
75 mov r9, r1 /* Save Machine type */
76 mov r10, r2 /* Save meta data */
77 mov r11, r3 /* Future expansion */
80 * Check whether data cache is enabled. If it is, then we know
81 * current tags are valid (not power-on garbage values) and there
82 * might be dirty lines that need cleaning. Disable cache to prevent
83 * new lines being allocated, then call wbinv_poc_all to clean it.
86 tst r7, #CPU_CONTROL_DC_ENABLE
87 blne dcache_wbinv_poc_all
89 /* ! Do not write to memory between wbinv and disabling cache ! */
92 * Now there are no dirty lines, but there may still be lines marked
93 * valid. Disable all caches and the MMU, and invalidate everything
94 * before setting up new page tables and re-enabling the mmu.
97 bic r7, #CPU_CONTROL_DC_ENABLE
98 bic r7, #CPU_CONTROL_MMU_ENABLE
99 bic r7, #CPU_CONTROL_IC_ENABLE
100 bic r7, #CPU_CONTROL_UNAL_ENABLE
101 bic r7, #CPU_CONTROL_BPRD_ENABLE
102 bic r7, #CPU_CONTROL_SW_ENABLE
103 orr r7, #CPU_CONTROL_AFLT_ENABLE
104 orr r7, #CPU_CONTROL_VECRELOC
107 bl dcache_inv_poc_all
112 * Build page table from scratch.
115 /* Calculate the physical address of the startup pagetable. */
117 bl translate_va_to_pa
122 /* Find the start kernels load address */
124 ldr r2, =(PTE1_OFFSET)
128 /* Map 64MiB, preserved over calls to build_pagetables */
132 /* Create the kernel map to jump to */
134 ldr r2, =(KERNVIRTADDR)
137 #if defined(SOCDEV_PA) && defined(SOCDEV_VA)
138 /* Create the custom map used for early_printf(). */
145 /* Switch to virtual addresses. */
149 /* Setup stack, clear BSS */
151 ldmia r1, {r1, r2, sp} /* Set initial stack and */
152 add sp, sp, #INIT_ARM_STACK_SIZE
153 sub r2, r2, r1 /* get zero init data */
156 str r3, [r1], #0x0004 /* get zero init data */
160 mov r1, #28 /* loader info size is 28 bytes also second arg */
161 subs sp, sp, r1 /* allocate arm_boot_params struct on stack */
162 mov r0, sp /* loader info pointer is first arg */
163 bic sp, sp, #7 /* align stack to 8 bytes */
164 str r1, [r0] /* Store length of loader info */
165 str r8, [r0, #4] /* Store r0 from boot loader */
166 str r9, [r0, #8] /* Store r1 from boot loader */
167 str r10, [r0, #12] /* store r2 from boot loader */
168 str r11, [r0, #16] /* store r3 from boot loader */
169 str r5, [r0, #20] /* store the physical address */
170 adr r4, Lpagetable /* load the pagetable address */
172 str r5, [r0, #24] /* store the pagetable address */
173 mov fp, #0 /* trace back starts here */
174 bl _C_LABEL(initarm) /* Off we go */
176 /* init arm will return the new stack pointer. */
179 bl _C_LABEL(mi_startup) /* call mi_startup()! */
181 ldr r0, =.Lmainreturned
186 #define VA_TO_PA_POINTER(name, table) \
192 * Returns the physical address of a magic va to pa pointer.
193 * r0 - The pagetable data pointer. This must be built using the
194 * VA_TO_PA_POINTER macro.
196 * VA_TO_PA_POINTER(Lpagetable, pagetable)
199 * bl translate_va_to_pa
200 * r0 will now contain the physical address of pagetable
206 /* At this point: r2 = VA - PA */
209 * Find the physical address of the table. After these two
213 * r0 = va(pagetable) - (VA - PA)
214 * = va(pagetable) - VA + PA
223 * r0 - The table base address
228 /* Setup TLB and MMU registers */
229 mcr CP15_TTBR0(r0) /* Set TTB */
231 mcr CP15_CONTEXTIDR(r0) /* Set ASID to 0 */
233 /* Set the Domain Access register */
234 mov r0, #((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT)
239 * Set TEX remap registers
240 * - All is set to uncacheable memory
247 mcr CP15_TLBIALL /* Flush TLB */
253 orr r0, r0, #CPU_CONTROL_MMU_ENABLE
254 orr r0, r0, #CPU_CONTROL_V6_EXTPAGE
256 orr r0, r0, #CPU_CONTROL_TR_ENABLE
258 orr r0, r0, #CPU_CONTROL_AF_ENABLE
262 mcr CP15_TLBIALL /* Flush TLB */
263 mcr CP15_BPIALL /* Flush Branch predictor */
270 * Init SMP coherent mode, enable caching and switch to final MMU table.
271 * Called with disabled caches
272 * r0 - The table base address
273 * r1 - clear bits for aux register
274 * r2 - set bits for aux register
276 ASENTRY_NP(reinit_mmu)
282 /* !! Be very paranoid here !! */
283 /* !! We cannot write single bit here !! */
285 #if 0 /* XXX writeback shouldn't be necessary */
286 /* Write back and invalidate all integrated caches */
287 bl dcache_wbinv_poc_all
289 bl dcache_inv_pou_all
294 /* Set auxiliary register */
296 bic r8, r7, r5 /* Mask bits */
297 eor r8, r8, r6 /* Set bits */
304 orr r7, #CPU_CONTROL_DC_ENABLE
305 orr r7, #CPU_CONTROL_IC_ENABLE
306 orr r7, #CPU_CONTROL_BPRD_ENABLE
310 mcr CP15_TTBR0(r4) /* Set new TTB */
319 #if 0 /* XXX writeback shouldn't be necessary */
320 /* Write back and invalidate all integrated caches */
321 bl dcache_wbinv_poc_all
323 bl dcache_inv_pou_all
333 * Builds the page table
334 * r0 - The table base address
335 * r1 - The physical address (trashed)
336 * r2 - The virtual address (trashed)
337 * r3 - The number of 1MiB sections
340 * Addresses must be 1MiB aligned
343 /* Set the required page attributed */
344 #if defined(ARM_NEW_PMAP)
345 ldr r4, =PTE1_V|PTE1_A|PTE1_AP_KRW|TEX1_CLASS_0
347 ldr r4, =(L1_TYPE_S|L1_S_C|L1_S_AP(AP_KRW)|L1_SHARED)
349 ldr r4, =(L1_TYPE_S|L1_S_C|L1_S_AP(AP_KRW))
353 /* Move the virtual address to the correct bit location */
354 lsr r2, #(PTE1_SHIFT - 2)
360 add r1, r1, #(PTE1_SIZE)
366 VA_TO_PA_POINTER(Lpagetable, boot_pt1)
370 .word _edata /* Note that these three items are */
371 .word _ebss /* loaded with a single ldmia and */
372 .word svcstk /* must remain in order together. */
375 .asciz "main() returned"
380 .space INIT_ARM_STACK_SIZE * MAXCPU
383 * Memory for the initial pagetable. We are unable to place this in
384 * the bss as this will be cleared after the table is loaded.
386 .section ".init_pagetable"
387 .align 14 /* 16KiB aligned */
396 .word _C_LABEL(cpufuncs)
401 /* Make sure interrupts are disabled. */
404 /* Setup core, disable all caches. */
406 bic r0, #CPU_CONTROL_MMU_ENABLE
407 bic r0, #CPU_CONTROL_DC_ENABLE
408 bic r0, #CPU_CONTROL_IC_ENABLE
409 bic r0, #CPU_CONTROL_UNAL_ENABLE
410 bic r0, #CPU_CONTROL_BPRD_ENABLE
411 bic r0, #CPU_CONTROL_SW_ENABLE
412 orr r0, #CPU_CONTROL_AFLT_ENABLE
413 orr r0, #CPU_CONTROL_VECRELOC
417 /* Invalidate L1 cache I+D cache */
418 bl dcache_inv_pou_all
422 /* Find the delta between VA and PA */
424 bl translate_va_to_pa
428 adr r1, .Lstart+8 /* Get initstack pointer from */
429 ldr sp, [r1] /* startup data. */
430 mrc CP15_MPIDR(r0) /* Get processor id number. */
432 mov r1, #INIT_ARM_STACK_SIZE
433 mul r2, r1, r0 /* Point sp to initstack */
434 add sp, sp, r2 /* area for this processor. */
436 /* Switch to virtual addresses. */
439 mov fp, #0 /* trace back starts here */
440 bl _C_LABEL(init_secondary)/* Off we go, cpu id in r0. */
448 .asciz "init_secondary() returned"
454 /* XXX re-implement !!! */
456 bl dcache_wbinv_poc_all
458 ldr r4, .Lcpu_reset_address
467 * _cpu_reset_address contains the address to branch to, to complete
468 * the cpu reset after turning the MMU off
469 * This variable is provided by the hardware specific code
472 .word _C_LABEL(cpu_reset_address)
492 .global _C_LABEL(esym)
493 _C_LABEL(esym): .word _C_LABEL(end)
504 * Call the sigreturn system call.
506 * We have to load r7 manually rather than using
507 * "ldr r7, =SYS_sigreturn" to ensure the value of szsigcode is
508 * correct. Using the alternative places esigcode at the address
509 * of the data rather than the address one past the data.
512 ldr r7, [pc, #12] /* Load SYS_sigreturn */
515 /* Well if that failed we better exit quick ! */
517 ldr r7, [pc, #8] /* Load SYS_exit */
520 /* Branch back to retry SYS_sigreturn */
528 .global _C_LABEL(esigcode)
534 .long esigcode-sigcode
536 /* End of locore.S */