1 /* $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2004 Olivier Houchard
7 * Copyright (c) 1994-1998 Mark Brinicombe.
8 * Copyright (c) 1994 Brini.
11 * This code is derived from software written for Brini by Mark Brinicombe
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Mark Brinicombe
24 * for the NetBSD Project.
25 * 4. The name of the company nor the name of the author may be used to
26 * endorse or promote products derived from this software without specific
27 * prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Machine dependent functions for kernel setup
44 * Updated : 18/04/01 updated for new wscons
47 #include "opt_compat.h"
49 #include "opt_kstack_pages.h"
50 #include "opt_platform.h"
51 #include "opt_sched.h"
52 #include "opt_timer.h"
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
62 #include <sys/devmap.h>
64 #include <sys/imgact.h>
66 #include <sys/kernel.h>
67 #include <sys/linker.h>
68 #include <sys/msgbuf.h>
69 #include <sys/rwlock.h>
70 #include <sys/sched.h>
71 #include <sys/syscallsubr.h>
72 #include <sys/sysent.h>
73 #include <sys/sysproto.h>
74 #include <sys/vmmeter.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_pager.h>
80 #include <machine/debug_monitor.h>
81 #include <machine/machdep.h>
82 #include <machine/metadata.h>
83 #include <machine/pcb.h>
84 #include <machine/physmem.h>
85 #include <machine/platform.h>
86 #include <machine/sysarch.h>
87 #include <machine/undefined.h>
88 #include <machine/vfp.h>
89 #include <machine/vmparam.h>
92 #include <dev/fdt/fdt_common.h>
93 #include <machine/ofw_machdep.h>
97 #define debugf(fmt, args...) printf(fmt, ##args)
99 #define debugf(fmt, args...)
102 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
103 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) || \
104 defined(COMPAT_FREEBSD9)
105 #error FreeBSD/arm doesn't provide compatibility with releases prior to 10
108 #if __ARM_ARCH >= 6 && !defined(INTRNG)
109 #error armv6 requires INTRNG
112 struct pcpu __pcpu[MAXCPU];
113 struct pcpu *pcpup = &__pcpu[0];
115 static struct trapframe proc0_tf;
116 uint32_t cpu_reset_address = 0;
118 vm_offset_t vector_page;
120 int (*_arm_memcpy)(void *, void *, int, int) = NULL;
121 int (*_arm_bzero)(void *, int, int) = NULL;
122 int _min_memcpy_size = 0;
123 int _min_bzero_size = 0;
130 vm_offset_t systempage;
131 vm_offset_t irqstack;
132 vm_offset_t undstack;
133 vm_offset_t abtstack;
136 * This is the number of L2 page tables required for covering max
137 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
138 * stacks etc.), uprounded to be divisible by 4.
140 #define KERNEL_PT_MAX 78
141 static struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
142 struct pv_addr systempage;
143 static struct pv_addr msgbufpv;
144 struct pv_addr irqstack;
145 struct pv_addr undstack;
146 struct pv_addr abtstack;
147 static struct pv_addr kernelstack;
148 #endif /* __ARM_ARCH >= 6 */
152 static delay_func *delay_impl;
153 static void *delay_arg;
156 struct kva_md_info kmi;
161 * Initialize the vector page, and select whether or not to
162 * relocate the vectors.
164 * NOTE: We expect the vector page to be mapped at its expected
168 extern unsigned int page0[], page0_data[];
170 arm_vector_init(vm_offset_t va, int which)
172 unsigned int *vectors = (int *) va;
173 unsigned int *vectors_data = vectors + (page0_data - page0);
177 * Loop through the vectors we're taking over, and copy the
178 * vector's insn and data word.
180 for (vec = 0; vec < ARM_NVEC; vec++) {
181 if ((which & (1 << vec)) == 0) {
182 /* Don't want to take over this vector. */
185 vectors[vec] = page0[vec];
186 vectors_data[vec] = page0_data[vec];
189 /* Now sync the vectors. */
190 icache_sync(va, (ARM_NVEC * 2) * sizeof(u_int));
194 if (va == ARM_VECTORS_HIGH) {
196 * Enable high vectors in the system control reg (SCTLR).
198 * Assume the MD caller knows what it's doing here, and really
199 * does want the vector page relocated.
201 * Note: This has to be done here (and not just in
202 * cpu_setup()) because the vector page needs to be
203 * accessible *before* cpu_startup() is called.
206 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
212 cpu_startup(void *dummy)
214 struct pcb *pcb = thread0.td_pcb;
215 const unsigned int mbyte = 1024 * 1024;
216 #if __ARM_ARCH < 6 && !defined(ARM_CACHE_LOCK_ENABLE)
222 vm_ksubmap_init(&kmi);
225 * Display the RAM layout.
227 printf("real memory = %ju (%ju MB)\n",
228 (uintmax_t)arm32_ptob(realmem),
229 (uintmax_t)arm32_ptob(realmem) / mbyte);
230 printf("avail memory = %ju (%ju MB)\n",
231 (uintmax_t)arm32_ptob(vm_free_count()),
232 (uintmax_t)arm32_ptob(vm_free_count()) / mbyte);
234 arm_physmem_print_tables();
235 devmap_print_table();
239 vm_pager_bufferinit();
240 pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack +
241 USPACE_SVC_STACK_TOP;
242 pmap_set_pcb_pagedir(kernel_pmap, pcb);
244 vector_page_setprot(VM_PROT_READ);
246 #ifdef ARM_CACHE_LOCK_ENABLE
247 pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
248 arm_lock_cache_line(ARM_TP_ADDRESS);
250 m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
251 pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
253 *(uint32_t *)ARM_RAS_START = 0;
254 *(uint32_t *)ARM_RAS_END = 0xffffffff;
258 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
261 * Flush the D-cache for non-DMA I/O so that the I-cache can
262 * be made coherent later.
265 cpu_flush_dcache(void *ptr, size_t len)
268 dcache_wb_poc((vm_offset_t)ptr, (vm_paddr_t)vtophys(ptr), len);
271 /* Get current clock frequency for the given cpu id. */
273 cpu_est_clockrate(int cpu_id, uint64_t *rate)
283 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
285 #ifndef NO_EVENTTIMERS
289 if (!sched_runnable())
291 #ifndef NO_EVENTTIMERS
296 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
300 cpu_idle_wakeup(int cpu)
306 #ifdef NO_EVENTTIMERS
308 * Most ARM platforms don't need to do anything special to init their clocks
309 * (they get intialized during normal device attachment), and by not defining a
310 * cpu_initclocks() function they get this generic one. Any platform that needs
311 * to do something special can just provide their own implementation, which will
312 * override this one due to the weak linkage.
315 arm_generic_initclocks(void)
318 __weak_reference(arm_generic_initclocks, cpu_initclocks);
326 if (PCPU_GET(cpuid) == 0)
327 cpu_initclocks_bsp();
331 cpu_initclocks_bsp();
338 arm_set_delay(delay_func *impl, void *arg)
341 KASSERT(impl != NULL, ("No DELAY implementation"));
351 delay_impl(usec, delay_arg);
357 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
368 if (td->td_md.md_spinlock_count == 0) {
369 cspr = disable_interrupts(PSR_I | PSR_F);
370 td->td_md.md_spinlock_count = 1;
371 td->td_md.md_saved_cspr = cspr;
373 td->td_md.md_spinlock_count++;
385 cspr = td->td_md.md_saved_cspr;
386 td->td_md.md_spinlock_count--;
387 if (td->td_md.md_spinlock_count == 0)
388 restore_interrupts(cspr);
392 * Clear registers on exec
395 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
397 struct trapframe *tf = td->td_frame;
399 memset(tf, 0, sizeof(*tf));
400 tf->tf_usr_sp = stack;
401 tf->tf_usr_lr = imgp->entry_addr;
402 tf->tf_svc_lr = 0x77777777;
403 tf->tf_pc = imgp->entry_addr;
404 tf->tf_spsr = PSR_USR32_MODE;
410 * Get machine VFP context.
413 get_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
418 if (td == curthread) {
420 vfp_store(&pcb->pcb_vfpstate, false);
423 MPASS(TD_IS_SUSPENDED(td));
424 memcpy(vfp->mcv_reg, pcb->pcb_vfpstate.reg,
425 sizeof(vfp->mcv_reg));
426 vfp->mcv_fpscr = pcb->pcb_vfpstate.fpscr;
430 * Set machine VFP context.
433 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
438 if (td == curthread) {
443 MPASS(TD_IS_SUSPENDED(td));
444 memcpy(pcb->pcb_vfpstate.reg, vfp->mcv_reg,
445 sizeof(pcb->pcb_vfpstate.reg));
446 pcb->pcb_vfpstate.fpscr = vfp->mcv_fpscr;
451 arm_get_vfpstate(struct thread *td, void *args)
454 struct arm_get_vfpstate_args ua;
455 mcontext_vfp_t mcontext_vfp;
457 rv = copyin(args, &ua, sizeof(ua));
460 if (ua.mc_vfp_size != sizeof(mcontext_vfp_t))
463 get_vfpcontext(td, &mcontext_vfp);
465 bzero(&mcontext_vfp, sizeof(mcontext_vfp));
468 rv = copyout(&mcontext_vfp, ua.mc_vfp, sizeof(mcontext_vfp));
475 * Get machine context.
478 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
480 struct trapframe *tf = td->td_frame;
481 __greg_t *gr = mcp->__gregs;
483 if (clear_ret & GET_MC_CLEAR_RET) {
485 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C;
487 gr[_REG_R0] = tf->tf_r0;
488 gr[_REG_CPSR] = tf->tf_spsr;
490 gr[_REG_R1] = tf->tf_r1;
491 gr[_REG_R2] = tf->tf_r2;
492 gr[_REG_R3] = tf->tf_r3;
493 gr[_REG_R4] = tf->tf_r4;
494 gr[_REG_R5] = tf->tf_r5;
495 gr[_REG_R6] = tf->tf_r6;
496 gr[_REG_R7] = tf->tf_r7;
497 gr[_REG_R8] = tf->tf_r8;
498 gr[_REG_R9] = tf->tf_r9;
499 gr[_REG_R10] = tf->tf_r10;
500 gr[_REG_R11] = tf->tf_r11;
501 gr[_REG_R12] = tf->tf_r12;
502 gr[_REG_SP] = tf->tf_usr_sp;
503 gr[_REG_LR] = tf->tf_usr_lr;
504 gr[_REG_PC] = tf->tf_pc;
506 mcp->mc_vfp_size = 0;
507 mcp->mc_vfp_ptr = NULL;
508 memset(&mcp->mc_spare, 0, sizeof(mcp->mc_spare));
514 * Set machine context.
516 * However, we don't set any but the user modifiable flags, and we won't
517 * touch the cs selector.
520 set_mcontext(struct thread *td, mcontext_t *mcp)
522 mcontext_vfp_t mc_vfp, *vfp;
523 struct trapframe *tf = td->td_frame;
524 const __greg_t *gr = mcp->__gregs;
528 * Make sure the processor mode has not been tampered with and
529 * interrupts have not been disabled.
531 spsr = gr[_REG_CPSR];
532 if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
533 (spsr & (PSR_I | PSR_F)) != 0)
537 if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_size != sizeof(mc_vfp)) {
538 printf("%s: %s: Malformed mc_vfp_size: %d (0x%08X)\n",
539 td->td_proc->p_comm, __func__,
540 mcp->mc_vfp_size, mcp->mc_vfp_size);
541 } else if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_ptr == NULL) {
542 printf("%s: %s: c_vfp_size != 0 but mc_vfp_ptr == NULL\n",
543 td->td_proc->p_comm, __func__);
547 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != NULL) {
548 if (copyin(mcp->mc_vfp_ptr, &mc_vfp, sizeof(mc_vfp)) != 0)
555 tf->tf_r0 = gr[_REG_R0];
556 tf->tf_r1 = gr[_REG_R1];
557 tf->tf_r2 = gr[_REG_R2];
558 tf->tf_r3 = gr[_REG_R3];
559 tf->tf_r4 = gr[_REG_R4];
560 tf->tf_r5 = gr[_REG_R5];
561 tf->tf_r6 = gr[_REG_R6];
562 tf->tf_r7 = gr[_REG_R7];
563 tf->tf_r8 = gr[_REG_R8];
564 tf->tf_r9 = gr[_REG_R9];
565 tf->tf_r10 = gr[_REG_R10];
566 tf->tf_r11 = gr[_REG_R11];
567 tf->tf_r12 = gr[_REG_R12];
568 tf->tf_usr_sp = gr[_REG_SP];
569 tf->tf_usr_lr = gr[_REG_LR];
570 tf->tf_pc = gr[_REG_PC];
571 tf->tf_spsr = gr[_REG_CPSR];
574 set_vfpcontext(td, vfp);
580 sendsig(catcher, ksi, mask)
587 struct trapframe *tf;
588 struct sigframe *fp, frame;
590 struct sysentvec *sysent;
597 PROC_LOCK_ASSERT(p, MA_OWNED);
598 sig = ksi->ksi_signo;
599 code = ksi->ksi_code;
601 mtx_assert(&psp->ps_mtx, MA_OWNED);
603 onstack = sigonstack(tf->tf_usr_sp);
605 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
608 /* Allocate and validate space for the signal handler context. */
609 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
610 SIGISMEMBER(psp->ps_sigonstack, sig)) {
611 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
612 td->td_sigstk.ss_size);
613 #if defined(COMPAT_43)
614 td->td_sigstk.ss_flags |= SS_ONSTACK;
617 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
619 /* make room on the stack */
622 /* make the stack aligned */
623 fp = (struct sigframe *)STACKALIGN(fp);
624 /* Populate the siginfo frame. */
625 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
627 get_vfpcontext(td, &frame.sf_vfp);
628 frame.sf_uc.uc_mcontext.mc_vfp_size = sizeof(fp->sf_vfp);
629 frame.sf_uc.uc_mcontext.mc_vfp_ptr = &fp->sf_vfp;
631 frame.sf_uc.uc_mcontext.mc_vfp_size = 0;
632 frame.sf_uc.uc_mcontext.mc_vfp_ptr = NULL;
634 frame.sf_si = ksi->ksi_info;
635 frame.sf_uc.uc_sigmask = *mask;
636 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
637 ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
638 frame.sf_uc.uc_stack = td->td_sigstk;
639 mtx_unlock(&psp->ps_mtx);
640 PROC_UNLOCK(td->td_proc);
642 /* Copy the sigframe out to the user's stack. */
643 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
644 /* Process has trashed its stack. Kill it. */
645 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
651 * Build context to run handler in. We invoke the handler
652 * directly, only returning via the trampoline. Note the
653 * trampoline version numbers are coordinated with machine-
654 * dependent code in libc.
658 tf->tf_r1 = (register_t)&fp->sf_si;
659 tf->tf_r2 = (register_t)&fp->sf_uc;
661 /* the trampoline uses r5 as the uc address */
662 tf->tf_r5 = (register_t)&fp->sf_uc;
663 tf->tf_pc = (register_t)catcher;
664 tf->tf_usr_sp = (register_t)fp;
665 sysent = p->p_sysent;
666 if (sysent->sv_sigcode_base != 0)
667 tf->tf_usr_lr = (register_t)sysent->sv_sigcode_base;
669 tf->tf_usr_lr = (register_t)(sysent->sv_psstrings -
670 *(sysent->sv_szsigcode));
671 /* Set the mode to enter in the signal handler */
673 if ((register_t)catcher & 1)
674 tf->tf_spsr |= PSR_T;
676 tf->tf_spsr &= ~PSR_T;
679 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
683 mtx_lock(&psp->ps_mtx);
687 sys_sigreturn(td, uap)
689 struct sigreturn_args /* {
690 const struct __ucontext *sigcntxp;
698 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
700 /* Restore register context. */
701 error = set_mcontext(td, &uc.uc_mcontext);
705 /* Restore signal mask. */
706 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
708 return (EJUSTRETURN);
712 * Construct a PCB from a trapframe. This is called from kdb_trap() where
713 * we want to start a backtrace from the function that caused us to enter
714 * the debugger. We have the context in the trapframe, but base the trace
715 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
716 * enough for a backtrace.
719 makectx(struct trapframe *tf, struct pcb *pcb)
721 pcb->pcb_regs.sf_r4 = tf->tf_r4;
722 pcb->pcb_regs.sf_r5 = tf->tf_r5;
723 pcb->pcb_regs.sf_r6 = tf->tf_r6;
724 pcb->pcb_regs.sf_r7 = tf->tf_r7;
725 pcb->pcb_regs.sf_r8 = tf->tf_r8;
726 pcb->pcb_regs.sf_r9 = tf->tf_r9;
727 pcb->pcb_regs.sf_r10 = tf->tf_r10;
728 pcb->pcb_regs.sf_r11 = tf->tf_r11;
729 pcb->pcb_regs.sf_r12 = tf->tf_r12;
730 pcb->pcb_regs.sf_pc = tf->tf_pc;
731 pcb->pcb_regs.sf_lr = tf->tf_usr_lr;
732 pcb->pcb_regs.sf_sp = tf->tf_usr_sp;
739 set_curthread(&thread0);
741 pcpu_init(pcpup, 0, sizeof(struct pcpu));
742 PCPU_SET(curthread, &thread0);
749 init_proc0(vm_offset_t kstack)
751 proc_linkup0(&proc0, &thread0);
752 thread0.td_kstack = kstack;
753 thread0.td_pcb = (struct pcb *)
754 (thread0.td_kstack + kstack_pages * PAGE_SIZE) - 1;
755 thread0.td_pcb->pcb_flags = 0;
756 thread0.td_pcb->pcb_vfpcpu = -1;
757 thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN;
758 thread0.td_frame = &proc0_tf;
759 pcpup->pc_curpcb = thread0.td_pcb;
764 set_stackptrs(int cpu)
767 set_stackptr(PSR_IRQ32_MODE,
768 irqstack + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
769 set_stackptr(PSR_ABT32_MODE,
770 abtstack + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
771 set_stackptr(PSR_UND32_MODE,
772 undstack + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
776 set_stackptrs(int cpu)
779 set_stackptr(PSR_IRQ32_MODE,
780 irqstack.pv_va + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
781 set_stackptr(PSR_ABT32_MODE,
782 abtstack.pv_va + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
783 set_stackptr(PSR_UND32_MODE,
784 undstack.pv_va + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
792 initarm(struct arm_boot_params *abp)
794 struct mem_region mem_regions[FDT_MEM_REGIONS];
795 struct pv_addr kernel_l1pt;
796 struct pv_addr dpcpu;
797 vm_offset_t dtbp, freemempos, l2_start, lastaddr;
803 int i, j, err_devmap, mem_regions_sz;
805 lastaddr = parse_boot_param(abp);
806 arm_physmem_kernaddr = abp->abp_physaddr;
814 * Find the dtb passed in by the boot loader.
816 kmdp = preload_search_by_type("elf kernel");
818 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
820 dtbp = (vm_offset_t)NULL;
822 #if defined(FDT_DTB_STATIC)
824 * In case the device tree blob was not retrieved (from metadata) try
825 * to use the statically embedded one.
827 if (dtbp == (vm_offset_t)NULL)
828 dtbp = (vm_offset_t)&fdt_static_dtb;
831 if (OF_install(OFW_FDT, 0) == FALSE)
832 panic("Cannot install FDT");
834 if (OF_init((void *)dtbp) != 0)
835 panic("OF_init failed with the found device tree");
837 /* Grab physical memory regions information from device tree. */
838 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, &memsize) != 0)
839 panic("Cannot get physical memory regions");
840 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
842 /* Grab reserved memory regions information from device tree. */
843 if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
844 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
845 EXFLAG_NODUMP | EXFLAG_NOALLOC);
847 /* Platform-specific initialisation */
848 platform_probe_and_attach();
852 /* Do basic tuning, hz etc */
855 /* Calculate number of L2 tables needed for mapping vm_page_array */
856 l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
857 l2size = (l2size >> L1_S_SHIFT) + 1;
860 * Add one table for end of kernel map, one for stacks, msgbuf and
861 * L1 and L2 tables map, one for vectors map and two for
862 * l2 structures from pmap_bootstrap.
866 /* Make it divisible by 4 */
867 l2size = (l2size + 3) & ~3;
869 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
871 /* Define a macro to simplify memory allocation */
872 #define valloc_pages(var, np) \
873 alloc_pages((var).pv_va, (np)); \
874 (var).pv_pa = (var).pv_va + (abp->abp_physaddr - KERNVIRTADDR);
876 #define alloc_pages(var, np) \
877 (var) = freemempos; \
878 freemempos += (np * PAGE_SIZE); \
879 memset((char *)(var), 0, ((np) * PAGE_SIZE));
881 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
882 freemempos += PAGE_SIZE;
883 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
885 for (i = 0, j = 0; i < l2size; ++i) {
886 if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
887 valloc_pages(kernel_pt_table[i],
888 L2_TABLE_SIZE / PAGE_SIZE);
891 kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
892 L2_TABLE_SIZE_REAL * (i - j);
893 kernel_pt_table[i].pv_pa =
894 kernel_pt_table[i].pv_va - KERNVIRTADDR +
900 * Allocate a page for the system page mapped to 0x00000000
901 * or 0xffff0000. This page will just contain the system vectors
902 * and can be shared by all processes.
904 valloc_pages(systempage, 1);
906 /* Allocate dynamic per-cpu area. */
907 valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
908 dpcpu_init((void *)dpcpu.pv_va, 0);
910 /* Allocate stacks for all modes */
911 valloc_pages(irqstack, IRQ_STACK_SIZE * MAXCPU);
912 valloc_pages(abtstack, ABT_STACK_SIZE * MAXCPU);
913 valloc_pages(undstack, UND_STACK_SIZE * MAXCPU);
914 valloc_pages(kernelstack, kstack_pages * MAXCPU);
915 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
918 * Now we start construction of the L1 page table
919 * We start by mapping the L2 page tables into the L1.
920 * This means that we can replace L1 mappings later on if necessary
922 l1pagetable = kernel_l1pt.pv_va;
925 * Try to map as much as possible of kernel text and data using
926 * 1MB section mapping and for the rest of initial kernel address
927 * space use L2 coarse tables.
929 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
930 * and kernel structures
932 l2_start = lastaddr & ~(L1_S_OFFSET);
933 for (i = 0 ; i < l2size - 1; i++)
934 pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
935 &kernel_pt_table[i]);
937 pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
939 /* Map kernel code and data */
940 pmap_map_chunk(l1pagetable, KERNVIRTADDR, abp->abp_physaddr,
941 (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
942 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
944 /* Map L1 directory and allocated L2 page tables */
945 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
946 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
948 pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
949 kernel_pt_table[0].pv_pa,
950 L2_TABLE_SIZE_REAL * l2size,
951 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
953 /* Map allocated DPCPU, stacks and msgbuf */
954 pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
955 freemempos - dpcpu.pv_va,
956 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
958 /* Link and map the vector page */
959 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
960 &kernel_pt_table[l2size - 1]);
961 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
962 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE, PTE_CACHE);
964 /* Establish static device mappings. */
965 err_devmap = platform_devmap_init();
966 devmap_bootstrap(l1pagetable, NULL);
967 vm_max_kernel_address = platform_lastaddr();
969 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) | DOMAIN_CLIENT);
970 pmap_pa = kernel_l1pt.pv_pa;
971 cpu_setttb(kernel_l1pt.pv_pa);
973 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
976 * Now that proper page tables are installed, call cpu_setup() to enable
977 * instruction and data caches and other chip-specific features.
982 * Only after the SOC registers block is mapped we can perform device
983 * tree fixups, as they may attempt to read parameters from hardware.
985 OF_interpret("perform-fixup", 0);
987 platform_gpio_init();
991 debugf("initarm: console initialized\n");
992 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
993 debugf(" boothowto = 0x%08x\n", boothowto);
994 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
997 env = kern_getenv("kernelname");
999 strlcpy(kernelname, env, sizeof(kernelname));
1003 if (err_devmap != 0)
1004 printf("WARNING: could not fully configure devmap, error=%d\n",
1007 platform_late_init();
1010 * Pages were allocated during the secondary bootstrap for the
1011 * stacks for different CPU modes.
1012 * We must now set the r13 registers in the different CPU modes to
1013 * point to these stacks.
1014 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1015 * of the stack memory.
1017 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
1022 * We must now clean the cache again....
1023 * Cleaning may be done by reading new data to displace any
1024 * dirty data in the cache. This will have happened in cpu_setttb()
1025 * but since we are boot strapping the addresses used for the read
1026 * may have just been remapped and thus the cache could be out
1027 * of sync. A re-clean after the switch will cure this.
1028 * After booting there are no gross relocations of the kernel thus
1029 * this problem will not occur after initarm().
1031 cpu_idcache_wbinv_all();
1035 init_proc0(kernelstack.pv_va);
1037 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1038 pmap_bootstrap(freemempos, &kernel_l1pt);
1039 msgbufp = (void *)msgbufpv.pv_va;
1040 msgbufinit(msgbufp, msgbufsize);
1044 * Exclude the kernel (and all the things we allocated which immediately
1045 * follow the kernel) from the VM allocation pool but not from crash
1046 * dumps. virtual_avail is a global variable which tracks the kva we've
1047 * "allocated" while setting up pmaps.
1049 * Prepare the list of physical memory available to the vm subsystem.
1051 arm_physmem_exclude_region(abp->abp_physaddr,
1052 (virtual_avail - KERNVIRTADDR), EXFLAG_NOALLOC);
1053 arm_physmem_init_kernel_globals();
1055 init_param2(physmem);
1059 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
1060 sizeof(struct pcb)));
1062 #else /* __ARM_ARCH < 6 */
1064 initarm(struct arm_boot_params *abp)
1066 struct mem_region mem_regions[FDT_MEM_REGIONS];
1067 vm_paddr_t lastaddr;
1068 vm_offset_t dtbp, kernelstack, dpcpu;
1071 int err_devmap, mem_regions_sz;
1073 struct efi_map_header *efihdr;
1076 /* get last allocated physical address */
1077 arm_physmem_kernaddr = abp->abp_physaddr;
1078 lastaddr = parse_boot_param(abp) - KERNVIRTADDR + arm_physmem_kernaddr;
1084 * Find the dtb passed in by the boot loader.
1086 kmdp = preload_search_by_type("elf kernel");
1087 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1088 #if defined(FDT_DTB_STATIC)
1090 * In case the device tree blob was not retrieved (from metadata) try
1091 * to use the statically embedded one.
1093 if (dtbp == (vm_offset_t)NULL)
1094 dtbp = (vm_offset_t)&fdt_static_dtb;
1097 if (OF_install(OFW_FDT, 0) == FALSE)
1098 panic("Cannot install FDT");
1100 if (OF_init((void *)dtbp) != 0)
1101 panic("OF_init failed with the found device tree");
1103 #if defined(LINUX_BOOT_ABI)
1104 arm_parse_fdt_bootargs();
1108 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1109 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1110 if (efihdr != NULL) {
1111 arm_add_efi_map_entries(efihdr, mem_regions, &mem_regions_sz);
1115 /* Grab physical memory regions information from device tree. */
1116 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,NULL) != 0)
1117 panic("Cannot get physical memory regions");
1119 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1121 /* Grab reserved memory regions information from device tree. */
1122 if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
1123 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1124 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1127 * Set TEX remapping registers.
1128 * Setup kernel page tables and switch to kernel L1 page table.
1131 pmap_bootstrap_prepare(lastaddr);
1134 * If EARLY_PRINTF support is enabled, we need to re-establish the
1135 * mapping after pmap_bootstrap_prepare() switches to new page tables.
1136 * Note that we can only do the remapping if the VA is outside the
1137 * kernel, now that we have real virtual (not VA=PA) mappings in effect.
1138 * Early printf does not work between the time pmap_set_tex() does
1139 * cp15_prrr_set() and this code remaps the VA.
1141 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE
1142 pmap_preboot_map_attr(SOCDEV_PA, SOCDEV_VA, 1024 * 1024,
1143 VM_PROT_READ | VM_PROT_WRITE, VM_MEMATTR_DEVICE);
1147 * Now that proper page tables are installed, call cpu_setup() to enable
1148 * instruction and data caches and other chip-specific features.
1152 /* Platform-specific initialisation */
1153 platform_probe_and_attach();
1156 /* Do basic tuning, hz etc */
1160 * Allocate a page for the system page mapped to 0xffff0000
1161 * This page will just contain the system vectors and can be
1162 * shared by all processes.
1164 systempage = pmap_preboot_get_pages(1);
1166 /* Map the vector page. */
1167 pmap_preboot_map_pages(systempage, ARM_VECTORS_HIGH, 1);
1168 if (virtual_end >= ARM_VECTORS_HIGH)
1169 virtual_end = ARM_VECTORS_HIGH - 1;
1171 /* Allocate dynamic per-cpu area. */
1172 dpcpu = pmap_preboot_get_vpages(DPCPU_SIZE / PAGE_SIZE);
1173 dpcpu_init((void *)dpcpu, 0);
1175 /* Allocate stacks for all modes */
1176 irqstack = pmap_preboot_get_vpages(IRQ_STACK_SIZE * MAXCPU);
1177 abtstack = pmap_preboot_get_vpages(ABT_STACK_SIZE * MAXCPU);
1178 undstack = pmap_preboot_get_vpages(UND_STACK_SIZE * MAXCPU );
1179 kernelstack = pmap_preboot_get_vpages(kstack_pages * MAXCPU);
1181 /* Allocate message buffer. */
1182 msgbufp = (void *)pmap_preboot_get_vpages(
1183 round_page(msgbufsize) / PAGE_SIZE);
1186 * Pages were allocated during the secondary bootstrap for the
1187 * stacks for different CPU modes.
1188 * We must now set the r13 registers in the different CPU modes to
1189 * point to these stacks.
1190 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1191 * of the stack memory.
1196 /* Establish static device mappings. */
1197 err_devmap = platform_devmap_init();
1198 devmap_bootstrap(0, NULL);
1199 vm_max_kernel_address = platform_lastaddr();
1202 * Only after the SOC registers block is mapped we can perform device
1203 * tree fixups, as they may attempt to read parameters from hardware.
1205 OF_interpret("perform-fixup", 0);
1206 platform_gpio_init();
1210 * If we made a mapping for EARLY_PRINTF after pmap_bootstrap_prepare(),
1211 * undo it now that the normal console printf works.
1213 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE
1214 pmap_kremove(SOCDEV_VA);
1217 debugf("initarm: console initialized\n");
1218 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1219 debugf(" boothowto = 0x%08x\n", boothowto);
1220 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1221 debugf(" lastaddr1: 0x%08x\n", lastaddr);
1224 env = kern_getenv("kernelname");
1226 strlcpy(kernelname, env, sizeof(kernelname));
1228 if (err_devmap != 0)
1229 printf("WARNING: could not fully configure devmap, error=%d\n",
1232 platform_late_init();
1235 * We must now clean the cache again....
1236 * Cleaning may be done by reading new data to displace any
1237 * dirty data in the cache. This will have happened in cpu_setttb()
1238 * but since we are boot strapping the addresses used for the read
1239 * may have just been remapped and thus the cache could be out
1240 * of sync. A re-clean after the switch will cure this.
1241 * After booting there are no gross relocations of the kernel thus
1242 * this problem will not occur after initarm().
1244 /* Set stack for exception handlers */
1246 init_proc0(kernelstack);
1247 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1248 enable_interrupts(PSR_A);
1251 /* Exclude the kernel (and all the things we allocated which immediately
1252 * follow the kernel) from the VM allocation pool but not from crash
1253 * dumps. virtual_avail is a global variable which tracks the kva we've
1254 * "allocated" while setting up pmaps.
1256 * Prepare the list of physical memory available to the vm subsystem.
1258 arm_physmem_exclude_region(abp->abp_physaddr,
1259 pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC);
1260 arm_physmem_init_kernel_globals();
1262 init_param2(physmem);
1263 /* Init message buffer. */
1264 msgbufinit(msgbufp, msgbufsize);
1267 /* Apply possible BP hardening. */
1268 cpuinfo_init_bp_hardening();
1269 return ((void *)STACKALIGN(thread0.td_pcb));
1273 #endif /* __ARM_ARCH < 6 */