1 /* $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $ */
4 * Copyright (c) 2004 Olivier Houchard
5 * Copyright (c) 1994-1998 Mark Brinicombe.
6 * Copyright (c) 1994 Brini.
9 * This code is derived from software written for Brini by Mark Brinicombe
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Mark Brinicombe
22 * for the NetBSD Project.
23 * 4. The name of the company nor the name of the author may be used to
24 * endorse or promote products derived from this software without specific
25 * prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Machine dependent functions for kernel setup
42 * Updated : 18/04/01 updated for new wscons
45 #include "opt_compat.h"
47 #include "opt_kstack_pages.h"
48 #include "opt_platform.h"
49 #include "opt_sched.h"
50 #include "opt_timer.h"
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
60 #include <sys/devmap.h>
62 #include <sys/imgact.h>
64 #include <sys/kernel.h>
65 #include <sys/linker.h>
66 #include <sys/msgbuf.h>
67 #include <sys/rwlock.h>
68 #include <sys/sched.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/sysent.h>
71 #include <sys/sysproto.h>
72 #include <sys/vmmeter.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_pager.h>
78 #include <machine/debug_monitor.h>
79 #include <machine/machdep.h>
80 #include <machine/metadata.h>
81 #include <machine/pcb.h>
82 #include <machine/physmem.h>
83 #include <machine/platform.h>
84 #include <machine/sysarch.h>
85 #include <machine/undefined.h>
86 #include <machine/vfp.h>
87 #include <machine/vmparam.h>
90 #include <dev/fdt/fdt_common.h>
91 #include <machine/ofw_machdep.h>
95 #define debugf(fmt, args...) printf(fmt, ##args)
97 #define debugf(fmt, args...)
100 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
101 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) || \
102 defined(COMPAT_FREEBSD9)
103 #error FreeBSD/arm doesn't provide compatibility with releases prior to 10
106 #if __ARM_ARCH >= 6 && !defined(INTRNG)
107 #error armv6 requires INTRNG
110 struct pcpu __pcpu[MAXCPU];
111 struct pcpu *pcpup = &__pcpu[0];
113 static struct trapframe proc0_tf;
114 uint32_t cpu_reset_address = 0;
116 vm_offset_t vector_page;
118 int (*_arm_memcpy)(void *, void *, int, int) = NULL;
119 int (*_arm_bzero)(void *, int, int) = NULL;
120 int _min_memcpy_size = 0;
121 int _min_bzero_size = 0;
128 vm_offset_t systempage;
129 vm_offset_t irqstack;
130 vm_offset_t undstack;
131 vm_offset_t abtstack;
134 * This is the number of L2 page tables required for covering max
135 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
136 * stacks etc.), uprounded to be divisible by 4.
138 #define KERNEL_PT_MAX 78
139 static struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
140 struct pv_addr systempage;
141 static struct pv_addr msgbufpv;
142 struct pv_addr irqstack;
143 struct pv_addr undstack;
144 struct pv_addr abtstack;
145 static struct pv_addr kernelstack;
146 #endif /* __ARM_ARCH >= 6 */
150 static delay_func *delay_impl;
151 static void *delay_arg;
154 struct kva_md_info kmi;
159 * Initialize the vector page, and select whether or not to
160 * relocate the vectors.
162 * NOTE: We expect the vector page to be mapped at its expected
166 extern unsigned int page0[], page0_data[];
168 arm_vector_init(vm_offset_t va, int which)
170 unsigned int *vectors = (int *) va;
171 unsigned int *vectors_data = vectors + (page0_data - page0);
175 * Loop through the vectors we're taking over, and copy the
176 * vector's insn and data word.
178 for (vec = 0; vec < ARM_NVEC; vec++) {
179 if ((which & (1 << vec)) == 0) {
180 /* Don't want to take over this vector. */
183 vectors[vec] = page0[vec];
184 vectors_data[vec] = page0_data[vec];
187 /* Now sync the vectors. */
188 icache_sync(va, (ARM_NVEC * 2) * sizeof(u_int));
192 if (va == ARM_VECTORS_HIGH) {
194 * Enable high vectors in the system control reg (SCTLR).
196 * Assume the MD caller knows what it's doing here, and really
197 * does want the vector page relocated.
199 * Note: This has to be done here (and not just in
200 * cpu_setup()) because the vector page needs to be
201 * accessible *before* cpu_startup() is called.
204 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
210 cpu_startup(void *dummy)
212 struct pcb *pcb = thread0.td_pcb;
213 const unsigned int mbyte = 1024 * 1024;
214 #if __ARM_ARCH < 6 && !defined(ARM_CACHE_LOCK_ENABLE)
220 vm_ksubmap_init(&kmi);
223 * Display the RAM layout.
225 printf("real memory = %ju (%ju MB)\n",
226 (uintmax_t)arm32_ptob(realmem),
227 (uintmax_t)arm32_ptob(realmem) / mbyte);
228 printf("avail memory = %ju (%ju MB)\n",
229 (uintmax_t)arm32_ptob(vm_cnt.v_free_count),
230 (uintmax_t)arm32_ptob(vm_cnt.v_free_count) / mbyte);
232 arm_physmem_print_tables();
233 devmap_print_table();
237 vm_pager_bufferinit();
238 pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack +
239 USPACE_SVC_STACK_TOP;
240 pmap_set_pcb_pagedir(kernel_pmap, pcb);
242 vector_page_setprot(VM_PROT_READ);
244 #ifdef ARM_CACHE_LOCK_ENABLE
245 pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
246 arm_lock_cache_line(ARM_TP_ADDRESS);
248 m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
249 pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
251 *(uint32_t *)ARM_RAS_START = 0;
252 *(uint32_t *)ARM_RAS_END = 0xffffffff;
256 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
259 * Flush the D-cache for non-DMA I/O so that the I-cache can
260 * be made coherent later.
263 cpu_flush_dcache(void *ptr, size_t len)
266 dcache_wb_poc((vm_offset_t)ptr, (vm_paddr_t)vtophys(ptr), len);
269 /* Get current clock frequency for the given cpu id. */
271 cpu_est_clockrate(int cpu_id, uint64_t *rate)
281 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
283 #ifndef NO_EVENTTIMERS
287 if (!sched_runnable())
289 #ifndef NO_EVENTTIMERS
294 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
298 cpu_idle_wakeup(int cpu)
304 #ifdef NO_EVENTTIMERS
306 * Most ARM platforms don't need to do anything special to init their clocks
307 * (they get intialized during normal device attachment), and by not defining a
308 * cpu_initclocks() function they get this generic one. Any platform that needs
309 * to do something special can just provide their own implementation, which will
310 * override this one due to the weak linkage.
313 arm_generic_initclocks(void)
316 __weak_reference(arm_generic_initclocks, cpu_initclocks);
324 if (PCPU_GET(cpuid) == 0)
325 cpu_initclocks_bsp();
329 cpu_initclocks_bsp();
336 arm_set_delay(delay_func *impl, void *arg)
339 KASSERT(impl != NULL, ("No DELAY implementation"));
348 delay_impl(usec, delay_arg);
353 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
364 if (td->td_md.md_spinlock_count == 0) {
365 cspr = disable_interrupts(PSR_I | PSR_F);
366 td->td_md.md_spinlock_count = 1;
367 td->td_md.md_saved_cspr = cspr;
369 td->td_md.md_spinlock_count++;
381 cspr = td->td_md.md_saved_cspr;
382 td->td_md.md_spinlock_count--;
383 if (td->td_md.md_spinlock_count == 0)
384 restore_interrupts(cspr);
388 * Clear registers on exec
391 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
393 struct trapframe *tf = td->td_frame;
395 memset(tf, 0, sizeof(*tf));
396 tf->tf_usr_sp = stack;
397 tf->tf_usr_lr = imgp->entry_addr;
398 tf->tf_svc_lr = 0x77777777;
399 tf->tf_pc = imgp->entry_addr;
400 tf->tf_spsr = PSR_USR32_MODE;
406 * Get machine VFP context.
409 get_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
414 if (td == curthread) {
416 vfp_store(&pcb->pcb_vfpstate, false);
419 MPASS(TD_IS_SUSPENDED(td));
420 memcpy(vfp->mcv_reg, pcb->pcb_vfpstate.reg,
421 sizeof(vfp->mcv_reg));
422 vfp->mcv_fpscr = pcb->pcb_vfpstate.fpscr;
426 * Set machine VFP context.
429 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
434 if (td == curthread) {
439 MPASS(TD_IS_SUSPENDED(td));
440 memcpy(pcb->pcb_vfpstate.reg, vfp->mcv_reg,
441 sizeof(pcb->pcb_vfpstate.reg));
442 pcb->pcb_vfpstate.fpscr = vfp->mcv_fpscr;
447 arm_get_vfpstate(struct thread *td, void *args)
450 struct arm_get_vfpstate_args ua;
451 mcontext_vfp_t mcontext_vfp;
453 rv = copyin(args, &ua, sizeof(ua));
456 if (ua.mc_vfp_size != sizeof(mcontext_vfp_t))
459 get_vfpcontext(td, &mcontext_vfp);
461 bzero(&mcontext_vfp, sizeof(mcontext_vfp));
464 rv = copyout(&mcontext_vfp, ua.mc_vfp, sizeof(mcontext_vfp));
471 * Get machine context.
474 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
476 struct trapframe *tf = td->td_frame;
477 __greg_t *gr = mcp->__gregs;
479 if (clear_ret & GET_MC_CLEAR_RET) {
481 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C;
483 gr[_REG_R0] = tf->tf_r0;
484 gr[_REG_CPSR] = tf->tf_spsr;
486 gr[_REG_R1] = tf->tf_r1;
487 gr[_REG_R2] = tf->tf_r2;
488 gr[_REG_R3] = tf->tf_r3;
489 gr[_REG_R4] = tf->tf_r4;
490 gr[_REG_R5] = tf->tf_r5;
491 gr[_REG_R6] = tf->tf_r6;
492 gr[_REG_R7] = tf->tf_r7;
493 gr[_REG_R8] = tf->tf_r8;
494 gr[_REG_R9] = tf->tf_r9;
495 gr[_REG_R10] = tf->tf_r10;
496 gr[_REG_R11] = tf->tf_r11;
497 gr[_REG_R12] = tf->tf_r12;
498 gr[_REG_SP] = tf->tf_usr_sp;
499 gr[_REG_LR] = tf->tf_usr_lr;
500 gr[_REG_PC] = tf->tf_pc;
502 mcp->mc_vfp_size = 0;
503 mcp->mc_vfp_ptr = NULL;
504 memset(&mcp->mc_spare, 0, sizeof(mcp->mc_spare));
510 * Set machine context.
512 * However, we don't set any but the user modifiable flags, and we won't
513 * touch the cs selector.
516 set_mcontext(struct thread *td, mcontext_t *mcp)
518 mcontext_vfp_t mc_vfp, *vfp;
519 struct trapframe *tf = td->td_frame;
520 const __greg_t *gr = mcp->__gregs;
523 if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_size != sizeof(mc_vfp)) {
524 printf("%s: %s: Malformed mc_vfp_size: %d (0x%08X)\n",
525 td->td_proc->p_comm, __func__,
526 mcp->mc_vfp_size, mcp->mc_vfp_size);
527 } else if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_ptr == NULL) {
528 printf("%s: %s: c_vfp_size != 0 but mc_vfp_ptr == NULL\n",
529 td->td_proc->p_comm, __func__);
533 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != NULL) {
534 if (copyin(mcp->mc_vfp_ptr, &mc_vfp, sizeof(mc_vfp)) != 0)
541 tf->tf_r0 = gr[_REG_R0];
542 tf->tf_r1 = gr[_REG_R1];
543 tf->tf_r2 = gr[_REG_R2];
544 tf->tf_r3 = gr[_REG_R3];
545 tf->tf_r4 = gr[_REG_R4];
546 tf->tf_r5 = gr[_REG_R5];
547 tf->tf_r6 = gr[_REG_R6];
548 tf->tf_r7 = gr[_REG_R7];
549 tf->tf_r8 = gr[_REG_R8];
550 tf->tf_r9 = gr[_REG_R9];
551 tf->tf_r10 = gr[_REG_R10];
552 tf->tf_r11 = gr[_REG_R11];
553 tf->tf_r12 = gr[_REG_R12];
554 tf->tf_usr_sp = gr[_REG_SP];
555 tf->tf_usr_lr = gr[_REG_LR];
556 tf->tf_pc = gr[_REG_PC];
557 tf->tf_spsr = gr[_REG_CPSR];
560 set_vfpcontext(td, vfp);
566 sendsig(catcher, ksi, mask)
573 struct trapframe *tf;
574 struct sigframe *fp, frame;
576 struct sysentvec *sysent;
583 PROC_LOCK_ASSERT(p, MA_OWNED);
584 sig = ksi->ksi_signo;
585 code = ksi->ksi_code;
587 mtx_assert(&psp->ps_mtx, MA_OWNED);
589 onstack = sigonstack(tf->tf_usr_sp);
591 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
594 /* Allocate and validate space for the signal handler context. */
595 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
596 SIGISMEMBER(psp->ps_sigonstack, sig)) {
597 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
598 td->td_sigstk.ss_size);
599 #if defined(COMPAT_43)
600 td->td_sigstk.ss_flags |= SS_ONSTACK;
603 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
605 /* make room on the stack */
608 /* make the stack aligned */
609 fp = (struct sigframe *)STACKALIGN(fp);
610 /* Populate the siginfo frame. */
611 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
613 get_vfpcontext(td, &frame.sf_vfp);
614 frame.sf_uc.uc_mcontext.mc_vfp_size = sizeof(fp->sf_vfp);
615 frame.sf_uc.uc_mcontext.mc_vfp_ptr = &fp->sf_vfp;
617 frame.sf_uc.uc_mcontext.mc_vfp_size = 0;
618 frame.sf_uc.uc_mcontext.mc_vfp_ptr = NULL;
620 frame.sf_si = ksi->ksi_info;
621 frame.sf_uc.uc_sigmask = *mask;
622 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
623 ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
624 frame.sf_uc.uc_stack = td->td_sigstk;
625 mtx_unlock(&psp->ps_mtx);
626 PROC_UNLOCK(td->td_proc);
628 /* Copy the sigframe out to the user's stack. */
629 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
630 /* Process has trashed its stack. Kill it. */
631 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
637 * Build context to run handler in. We invoke the handler
638 * directly, only returning via the trampoline. Note the
639 * trampoline version numbers are coordinated with machine-
640 * dependent code in libc.
644 tf->tf_r1 = (register_t)&fp->sf_si;
645 tf->tf_r2 = (register_t)&fp->sf_uc;
647 /* the trampoline uses r5 as the uc address */
648 tf->tf_r5 = (register_t)&fp->sf_uc;
649 tf->tf_pc = (register_t)catcher;
650 tf->tf_usr_sp = (register_t)fp;
651 sysent = p->p_sysent;
652 if (sysent->sv_sigcode_base != 0)
653 tf->tf_usr_lr = (register_t)sysent->sv_sigcode_base;
655 tf->tf_usr_lr = (register_t)(sysent->sv_psstrings -
656 *(sysent->sv_szsigcode));
657 /* Set the mode to enter in the signal handler */
659 if ((register_t)catcher & 1)
660 tf->tf_spsr |= PSR_T;
662 tf->tf_spsr &= ~PSR_T;
665 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
669 mtx_lock(&psp->ps_mtx);
673 sys_sigreturn(td, uap)
675 struct sigreturn_args /* {
676 const struct __ucontext *sigcntxp;
684 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
687 * Make sure the processor mode has not been tampered with and
688 * interrupts have not been disabled.
690 spsr = uc.uc_mcontext.__gregs[_REG_CPSR];
691 if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
692 (spsr & (PSR_I | PSR_F)) != 0)
694 /* Restore register context. */
695 set_mcontext(td, &uc.uc_mcontext);
697 /* Restore signal mask. */
698 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
700 return (EJUSTRETURN);
704 * Construct a PCB from a trapframe. This is called from kdb_trap() where
705 * we want to start a backtrace from the function that caused us to enter
706 * the debugger. We have the context in the trapframe, but base the trace
707 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
708 * enough for a backtrace.
711 makectx(struct trapframe *tf, struct pcb *pcb)
713 pcb->pcb_regs.sf_r4 = tf->tf_r4;
714 pcb->pcb_regs.sf_r5 = tf->tf_r5;
715 pcb->pcb_regs.sf_r6 = tf->tf_r6;
716 pcb->pcb_regs.sf_r7 = tf->tf_r7;
717 pcb->pcb_regs.sf_r8 = tf->tf_r8;
718 pcb->pcb_regs.sf_r9 = tf->tf_r9;
719 pcb->pcb_regs.sf_r10 = tf->tf_r10;
720 pcb->pcb_regs.sf_r11 = tf->tf_r11;
721 pcb->pcb_regs.sf_r12 = tf->tf_r12;
722 pcb->pcb_regs.sf_pc = tf->tf_pc;
723 pcb->pcb_regs.sf_lr = tf->tf_usr_lr;
724 pcb->pcb_regs.sf_sp = tf->tf_usr_sp;
731 set_curthread(&thread0);
733 pcpu_init(pcpup, 0, sizeof(struct pcpu));
734 PCPU_SET(curthread, &thread0);
741 init_proc0(vm_offset_t kstack)
743 proc_linkup0(&proc0, &thread0);
744 thread0.td_kstack = kstack;
745 thread0.td_pcb = (struct pcb *)
746 (thread0.td_kstack + kstack_pages * PAGE_SIZE) - 1;
747 thread0.td_pcb->pcb_flags = 0;
748 thread0.td_pcb->pcb_vfpcpu = -1;
749 thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN;
750 thread0.td_frame = &proc0_tf;
751 pcpup->pc_curpcb = thread0.td_pcb;
756 set_stackptrs(int cpu)
759 set_stackptr(PSR_IRQ32_MODE,
760 irqstack + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
761 set_stackptr(PSR_ABT32_MODE,
762 abtstack + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
763 set_stackptr(PSR_UND32_MODE,
764 undstack + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
768 set_stackptrs(int cpu)
771 set_stackptr(PSR_IRQ32_MODE,
772 irqstack.pv_va + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
773 set_stackptr(PSR_ABT32_MODE,
774 abtstack.pv_va + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
775 set_stackptr(PSR_UND32_MODE,
776 undstack.pv_va + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
784 initarm(struct arm_boot_params *abp)
786 struct mem_region mem_regions[FDT_MEM_REGIONS];
787 struct pv_addr kernel_l1pt;
788 struct pv_addr dpcpu;
789 vm_offset_t dtbp, freemempos, l2_start, lastaddr;
795 int i, j, err_devmap, mem_regions_sz;
797 lastaddr = parse_boot_param(abp);
798 arm_physmem_kernaddr = abp->abp_physaddr;
806 * Find the dtb passed in by the boot loader.
808 kmdp = preload_search_by_type("elf kernel");
810 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
812 dtbp = (vm_offset_t)NULL;
814 #if defined(FDT_DTB_STATIC)
816 * In case the device tree blob was not retrieved (from metadata) try
817 * to use the statically embedded one.
819 if (dtbp == (vm_offset_t)NULL)
820 dtbp = (vm_offset_t)&fdt_static_dtb;
823 if (OF_install(OFW_FDT, 0) == FALSE)
824 panic("Cannot install FDT");
826 if (OF_init((void *)dtbp) != 0)
827 panic("OF_init failed with the found device tree");
829 /* Grab physical memory regions information from device tree. */
830 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, &memsize) != 0)
831 panic("Cannot get physical memory regions");
832 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
834 /* Grab reserved memory regions information from device tree. */
835 if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
836 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
837 EXFLAG_NODUMP | EXFLAG_NOALLOC);
839 /* Platform-specific initialisation */
840 platform_probe_and_attach();
844 /* Do basic tuning, hz etc */
847 /* Calculate number of L2 tables needed for mapping vm_page_array */
848 l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
849 l2size = (l2size >> L1_S_SHIFT) + 1;
852 * Add one table for end of kernel map, one for stacks, msgbuf and
853 * L1 and L2 tables map, one for vectors map and two for
854 * l2 structures from pmap_bootstrap.
858 /* Make it divisible by 4 */
859 l2size = (l2size + 3) & ~3;
861 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
863 /* Define a macro to simplify memory allocation */
864 #define valloc_pages(var, np) \
865 alloc_pages((var).pv_va, (np)); \
866 (var).pv_pa = (var).pv_va + (abp->abp_physaddr - KERNVIRTADDR);
868 #define alloc_pages(var, np) \
869 (var) = freemempos; \
870 freemempos += (np * PAGE_SIZE); \
871 memset((char *)(var), 0, ((np) * PAGE_SIZE));
873 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
874 freemempos += PAGE_SIZE;
875 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
877 for (i = 0, j = 0; i < l2size; ++i) {
878 if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
879 valloc_pages(kernel_pt_table[i],
880 L2_TABLE_SIZE / PAGE_SIZE);
883 kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
884 L2_TABLE_SIZE_REAL * (i - j);
885 kernel_pt_table[i].pv_pa =
886 kernel_pt_table[i].pv_va - KERNVIRTADDR +
892 * Allocate a page for the system page mapped to 0x00000000
893 * or 0xffff0000. This page will just contain the system vectors
894 * and can be shared by all processes.
896 valloc_pages(systempage, 1);
898 /* Allocate dynamic per-cpu area. */
899 valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
900 dpcpu_init((void *)dpcpu.pv_va, 0);
902 /* Allocate stacks for all modes */
903 valloc_pages(irqstack, IRQ_STACK_SIZE * MAXCPU);
904 valloc_pages(abtstack, ABT_STACK_SIZE * MAXCPU);
905 valloc_pages(undstack, UND_STACK_SIZE * MAXCPU);
906 valloc_pages(kernelstack, kstack_pages * MAXCPU);
907 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
910 * Now we start construction of the L1 page table
911 * We start by mapping the L2 page tables into the L1.
912 * This means that we can replace L1 mappings later on if necessary
914 l1pagetable = kernel_l1pt.pv_va;
917 * Try to map as much as possible of kernel text and data using
918 * 1MB section mapping and for the rest of initial kernel address
919 * space use L2 coarse tables.
921 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
922 * and kernel structures
924 l2_start = lastaddr & ~(L1_S_OFFSET);
925 for (i = 0 ; i < l2size - 1; i++)
926 pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
927 &kernel_pt_table[i]);
929 pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
931 /* Map kernel code and data */
932 pmap_map_chunk(l1pagetable, KERNVIRTADDR, abp->abp_physaddr,
933 (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
934 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
936 /* Map L1 directory and allocated L2 page tables */
937 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
938 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
940 pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
941 kernel_pt_table[0].pv_pa,
942 L2_TABLE_SIZE_REAL * l2size,
943 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
945 /* Map allocated DPCPU, stacks and msgbuf */
946 pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
947 freemempos - dpcpu.pv_va,
948 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
950 /* Link and map the vector page */
951 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
952 &kernel_pt_table[l2size - 1]);
953 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
954 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE, PTE_CACHE);
956 /* Establish static device mappings. */
957 err_devmap = platform_devmap_init();
958 devmap_bootstrap(l1pagetable, NULL);
959 vm_max_kernel_address = platform_lastaddr();
961 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) | DOMAIN_CLIENT);
962 pmap_pa = kernel_l1pt.pv_pa;
963 cpu_setttb(kernel_l1pt.pv_pa);
965 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
968 * Now that proper page tables are installed, call cpu_setup() to enable
969 * instruction and data caches and other chip-specific features.
974 * Only after the SOC registers block is mapped we can perform device
975 * tree fixups, as they may attempt to read parameters from hardware.
977 OF_interpret("perform-fixup", 0);
979 platform_gpio_init();
983 debugf("initarm: console initialized\n");
984 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
985 debugf(" boothowto = 0x%08x\n", boothowto);
986 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
989 env = kern_getenv("kernelname");
991 strlcpy(kernelname, env, sizeof(kernelname));
996 printf("WARNING: could not fully configure devmap, error=%d\n",
999 platform_late_init();
1002 * Pages were allocated during the secondary bootstrap for the
1003 * stacks for different CPU modes.
1004 * We must now set the r13 registers in the different CPU modes to
1005 * point to these stacks.
1006 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1007 * of the stack memory.
1009 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
1014 * We must now clean the cache again....
1015 * Cleaning may be done by reading new data to displace any
1016 * dirty data in the cache. This will have happened in cpu_setttb()
1017 * but since we are boot strapping the addresses used for the read
1018 * may have just been remapped and thus the cache could be out
1019 * of sync. A re-clean after the switch will cure this.
1020 * After booting there are no gross relocations of the kernel thus
1021 * this problem will not occur after initarm().
1023 cpu_idcache_wbinv_all();
1027 init_proc0(kernelstack.pv_va);
1029 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1030 pmap_bootstrap(freemempos, &kernel_l1pt);
1031 msgbufp = (void *)msgbufpv.pv_va;
1032 msgbufinit(msgbufp, msgbufsize);
1036 * Exclude the kernel (and all the things we allocated which immediately
1037 * follow the kernel) from the VM allocation pool but not from crash
1038 * dumps. virtual_avail is a global variable which tracks the kva we've
1039 * "allocated" while setting up pmaps.
1041 * Prepare the list of physical memory available to the vm subsystem.
1043 arm_physmem_exclude_region(abp->abp_physaddr,
1044 (virtual_avail - KERNVIRTADDR), EXFLAG_NOALLOC);
1045 arm_physmem_init_kernel_globals();
1047 init_param2(physmem);
1051 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
1052 sizeof(struct pcb)));
1054 #else /* __ARM_ARCH < 6 */
1056 initarm(struct arm_boot_params *abp)
1058 struct mem_region mem_regions[FDT_MEM_REGIONS];
1059 vm_paddr_t lastaddr;
1060 vm_offset_t dtbp, kernelstack, dpcpu;
1063 int err_devmap, mem_regions_sz;
1065 struct efi_map_header *efihdr;
1068 /* get last allocated physical address */
1069 arm_physmem_kernaddr = abp->abp_physaddr;
1070 lastaddr = parse_boot_param(abp) - KERNVIRTADDR + arm_physmem_kernaddr;
1076 * Find the dtb passed in by the boot loader.
1078 kmdp = preload_search_by_type("elf kernel");
1079 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1080 #if defined(FDT_DTB_STATIC)
1082 * In case the device tree blob was not retrieved (from metadata) try
1083 * to use the statically embedded one.
1085 if (dtbp == (vm_offset_t)NULL)
1086 dtbp = (vm_offset_t)&fdt_static_dtb;
1089 if (OF_install(OFW_FDT, 0) == FALSE)
1090 panic("Cannot install FDT");
1092 if (OF_init((void *)dtbp) != 0)
1093 panic("OF_init failed with the found device tree");
1095 #if defined(LINUX_BOOT_ABI)
1096 arm_parse_fdt_bootargs();
1100 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1101 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1102 if (efihdr != NULL) {
1103 arm_add_efi_map_entries(efihdr, mem_regions, &mem_regions_sz);
1107 /* Grab physical memory regions information from device tree. */
1108 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,NULL) != 0)
1109 panic("Cannot get physical memory regions");
1111 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1113 /* Grab reserved memory regions information from device tree. */
1114 if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
1115 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1116 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1119 * Set TEX remapping registers.
1120 * Setup kernel page tables and switch to kernel L1 page table.
1123 pmap_bootstrap_prepare(lastaddr);
1126 * Now that proper page tables are installed, call cpu_setup() to enable
1127 * instruction and data caches and other chip-specific features.
1131 /* Platform-specific initialisation */
1132 platform_probe_and_attach();
1135 /* Do basic tuning, hz etc */
1139 * Allocate a page for the system page mapped to 0xffff0000
1140 * This page will just contain the system vectors and can be
1141 * shared by all processes.
1143 systempage = pmap_preboot_get_pages(1);
1145 /* Map the vector page. */
1146 pmap_preboot_map_pages(systempage, ARM_VECTORS_HIGH, 1);
1147 if (virtual_end >= ARM_VECTORS_HIGH)
1148 virtual_end = ARM_VECTORS_HIGH - 1;
1150 /* Allocate dynamic per-cpu area. */
1151 dpcpu = pmap_preboot_get_vpages(DPCPU_SIZE / PAGE_SIZE);
1152 dpcpu_init((void *)dpcpu, 0);
1154 /* Allocate stacks for all modes */
1155 irqstack = pmap_preboot_get_vpages(IRQ_STACK_SIZE * MAXCPU);
1156 abtstack = pmap_preboot_get_vpages(ABT_STACK_SIZE * MAXCPU);
1157 undstack = pmap_preboot_get_vpages(UND_STACK_SIZE * MAXCPU );
1158 kernelstack = pmap_preboot_get_vpages(kstack_pages * MAXCPU);
1160 /* Allocate message buffer. */
1161 msgbufp = (void *)pmap_preboot_get_vpages(
1162 round_page(msgbufsize) / PAGE_SIZE);
1165 * Pages were allocated during the secondary bootstrap for the
1166 * stacks for different CPU modes.
1167 * We must now set the r13 registers in the different CPU modes to
1168 * point to these stacks.
1169 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1170 * of the stack memory.
1175 /* Establish static device mappings. */
1176 err_devmap = platform_devmap_init();
1177 devmap_bootstrap(0, NULL);
1178 vm_max_kernel_address = platform_lastaddr();
1181 * Only after the SOC registers block is mapped we can perform device
1182 * tree fixups, as they may attempt to read parameters from hardware.
1184 OF_interpret("perform-fixup", 0);
1185 platform_gpio_init();
1188 debugf("initarm: console initialized\n");
1189 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1190 debugf(" boothowto = 0x%08x\n", boothowto);
1191 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1192 debugf(" lastaddr1: 0x%08x\n", lastaddr);
1195 env = kern_getenv("kernelname");
1197 strlcpy(kernelname, env, sizeof(kernelname));
1199 if (err_devmap != 0)
1200 printf("WARNING: could not fully configure devmap, error=%d\n",
1203 platform_late_init();
1206 * We must now clean the cache again....
1207 * Cleaning may be done by reading new data to displace any
1208 * dirty data in the cache. This will have happened in cpu_setttb()
1209 * but since we are boot strapping the addresses used for the read
1210 * may have just been remapped and thus the cache could be out
1211 * of sync. A re-clean after the switch will cure this.
1212 * After booting there are no gross relocations of the kernel thus
1213 * this problem will not occur after initarm().
1215 /* Set stack for exception handlers */
1217 init_proc0(kernelstack);
1218 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1219 enable_interrupts(PSR_A);
1222 /* Exclude the kernel (and all the things we allocated which immediately
1223 * follow the kernel) from the VM allocation pool but not from crash
1224 * dumps. virtual_avail is a global variable which tracks the kva we've
1225 * "allocated" while setting up pmaps.
1227 * Prepare the list of physical memory available to the vm subsystem.
1229 arm_physmem_exclude_region(abp->abp_physaddr,
1230 pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC);
1231 arm_physmem_init_kernel_globals();
1233 init_param2(physmem);
1234 /* Init message buffer. */
1235 msgbufinit(msgbufp, msgbufsize);
1238 return ((void *)STACKALIGN(thread0.td_pcb));
1242 #endif /* __ARM_ARCH < 6 */