Try to put the CPU into a low power state if we failed to otherwise halt
[freebsd.git] / sys / arm64 / arm64 / machdep.c
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include "opt_platform.h"
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/buf.h>
36 #include <sys/bus.h>
37 #include <sys/cons.h>
38 #include <sys/cpu.h>
39 #include <sys/efi.h>
40 #include <sys/exec.h>
41 #include <sys/imgact.h>
42 #include <sys/kdb.h> 
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/linker.h>
46 #include <sys/msgbuf.h>
47 #include <sys/pcpu.h>
48 #include <sys/proc.h>
49 #include <sys/ptrace.h>
50 #include <sys/reboot.h>
51 #include <sys/rwlock.h>
52 #include <sys/sched.h>
53 #include <sys/signalvar.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysent.h>
56 #include <sys/sysproto.h>
57 #include <sys/ucontext.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_page.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_pager.h>
66
67 #include <machine/armreg.h>
68 #include <machine/cpu.h>
69 #include <machine/debug_monitor.h>
70 #include <machine/kdb.h>
71 #include <machine/devmap.h>
72 #include <machine/machdep.h>
73 #include <machine/metadata.h>
74 #include <machine/pcb.h>
75 #include <machine/reg.h>
76 #include <machine/vmparam.h>
77
78 #ifdef VFP
79 #include <machine/vfp.h>
80 #endif
81
82 #ifdef FDT
83 #include <dev/fdt/fdt_common.h>
84 #include <dev/ofw/openfirm.h>
85 #endif
86
87 struct pcpu __pcpu[MAXCPU];
88
89 static struct trapframe proc0_tf;
90
91 vm_paddr_t phys_avail[PHYS_AVAIL_SIZE + 2];
92 vm_paddr_t dump_avail[PHYS_AVAIL_SIZE + 2];
93
94 int early_boot = 1;
95 int cold = 1;
96 long realmem = 0;
97 long Maxmem = 0;
98
99 #define PHYSMAP_SIZE    (2 * (VM_PHYSSEG_MAX - 1))
100 vm_paddr_t physmap[PHYSMAP_SIZE];
101 u_int physmap_idx;
102
103 struct kva_md_info kmi;
104
105 int64_t dcache_line_size;       /* The minimum D cache line size */
106 int64_t icache_line_size;       /* The minimum I cache line size */
107 int64_t idcache_line_size;      /* The minimum cache line size */
108
109 static void
110 cpu_startup(void *dummy)
111 {
112
113         identify_cpu();
114
115         vm_ksubmap_init(&kmi);
116         bufinit();
117         vm_pager_bufferinit();
118 }
119
120 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
121
122 int
123 cpu_idle_wakeup(int cpu)
124 {
125
126         return (0);
127 }
128
129 void
130 bzero(void *buf, size_t len)
131 {
132         uint8_t *p;
133
134         p = buf;
135         while(len-- > 0)
136                 *p++ = 0;
137 }
138
139 int
140 fill_regs(struct thread *td, struct reg *regs)
141 {
142         struct trapframe *frame;
143
144         frame = td->td_frame;
145         regs->sp = frame->tf_sp;
146         regs->lr = frame->tf_lr;
147         regs->elr = frame->tf_elr;
148         regs->spsr = frame->tf_spsr;
149
150         memcpy(regs->x, frame->tf_x, sizeof(regs->x));
151
152         return (0);
153 }
154
155 int
156 set_regs(struct thread *td, struct reg *regs)
157 {
158         struct trapframe *frame;
159
160         frame = td->td_frame;
161         frame->tf_sp = regs->sp;
162         frame->tf_lr = regs->lr;
163         frame->tf_elr = regs->elr;
164         frame->tf_spsr = regs->spsr;
165
166         memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
167
168         return (0);
169 }
170
171 int
172 fill_fpregs(struct thread *td, struct fpreg *regs)
173 {
174 #ifdef VFP
175         struct pcb *pcb;
176
177         pcb = td->td_pcb;
178         if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
179                 /*
180                  * If we have just been running VFP instructions we will
181                  * need to save the state to memcpy it below.
182                  */
183                 vfp_save_state(td);
184
185                 memcpy(regs->fp_q, pcb->pcb_vfp, sizeof(regs->fp_q));
186                 regs->fp_cr = pcb->pcb_fpcr;
187                 regs->fp_sr = pcb->pcb_fpsr;
188         } else
189 #endif
190                 memset(regs->fp_q, 0, sizeof(regs->fp_q));
191         return (0);
192 }
193
194 int
195 set_fpregs(struct thread *td, struct fpreg *regs)
196 {
197 #ifdef VFP
198         struct pcb *pcb;
199
200         pcb = td->td_pcb;
201         memcpy(pcb->pcb_vfp, regs->fp_q, sizeof(regs->fp_q));
202         pcb->pcb_fpcr = regs->fp_cr;
203         pcb->pcb_fpsr = regs->fp_sr;
204 #endif
205         return (0);
206 }
207
208 int
209 fill_dbregs(struct thread *td, struct dbreg *regs)
210 {
211
212         panic("ARM64TODO: fill_dbregs");
213 }
214
215 int
216 set_dbregs(struct thread *td, struct dbreg *regs)
217 {
218
219         panic("ARM64TODO: set_dbregs");
220 }
221
222 int
223 ptrace_set_pc(struct thread *td, u_long addr)
224 {
225
226         panic("ARM64TODO: ptrace_set_pc");
227         return (0);
228 }
229
230 int
231 ptrace_single_step(struct thread *td)
232 {
233
234         /* TODO; */
235         return (0);
236 }
237
238 int
239 ptrace_clear_single_step(struct thread *td)
240 {
241
242         /* TODO; */
243         return (0);
244 }
245
246 void
247 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
248 {
249         struct trapframe *tf = td->td_frame;
250
251         memset(tf, 0, sizeof(struct trapframe));
252
253         tf->tf_sp = stack;
254         tf->tf_lr = imgp->entry_addr;
255         tf->tf_elr = imgp->entry_addr;
256 }
257
258 /* Sanity check these are the same size, they will be memcpy'd to and fro */
259 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
260     sizeof((struct gpregs *)0)->gp_x);
261 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
262     sizeof((struct reg *)0)->x);
263
264 int
265 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
266 {
267         struct trapframe *tf = td->td_frame;
268
269         if (clear_ret & GET_MC_CLEAR_RET) {
270                 mcp->mc_gpregs.gp_x[0] = 0;
271                 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
272         } else {
273                 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
274                 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
275         }
276
277         memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
278             sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
279
280         mcp->mc_gpregs.gp_sp = tf->tf_sp;
281         mcp->mc_gpregs.gp_lr = tf->tf_lr;
282         mcp->mc_gpregs.gp_elr = tf->tf_elr;
283
284         return (0);
285 }
286
287 int
288 set_mcontext(struct thread *td, mcontext_t *mcp)
289 {
290         struct trapframe *tf = td->td_frame;
291
292         memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
293
294         tf->tf_sp = mcp->mc_gpregs.gp_sp;
295         tf->tf_lr = mcp->mc_gpregs.gp_lr;
296         tf->tf_elr = mcp->mc_gpregs.gp_elr;
297         tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
298
299         return (0);
300 }
301
302 static void
303 get_fpcontext(struct thread *td, mcontext_t *mcp)
304 {
305 #ifdef VFP
306         struct pcb *curpcb;
307
308         critical_enter();
309
310         curpcb = curthread->td_pcb;
311
312         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
313                 /*
314                  * If we have just been running VFP instructions we will
315                  * need to save the state to memcpy it below.
316                  */
317                 vfp_save_state(td);
318
319                 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_vfp,
320                     sizeof(mcp->mc_fpregs));
321                 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpcr;
322                 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpsr;
323                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
324                 mcp->mc_flags |= _MC_FP_VALID;
325         }
326
327         critical_exit();
328 #endif
329 }
330
331 static void
332 set_fpcontext(struct thread *td, mcontext_t *mcp)
333 {
334 #ifdef VFP
335         struct pcb *curpcb;
336
337         critical_enter();
338
339         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
340                 curpcb = curthread->td_pcb;
341
342                 /*
343                  * Discard any vfp state for the current thread, we
344                  * are about to override it.
345                  */
346                 vfp_discard(td);
347
348                 memcpy(curpcb->pcb_vfp, mcp->mc_fpregs.fp_q,
349                     sizeof(mcp->mc_fpregs));
350                 curpcb->pcb_fpcr = mcp->mc_fpregs.fp_cr;
351                 curpcb->pcb_fpsr = mcp->mc_fpregs.fp_sr;
352                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags;
353         }
354
355         critical_exit();
356 #endif
357 }
358
359 void
360 cpu_idle(int busy)
361 {
362
363         spinlock_enter();
364         if (!busy)
365                 cpu_idleclock();
366         if (!sched_runnable())
367                 __asm __volatile(
368                     "dsb sy \n"
369                     "wfi    \n");
370         if (!busy)
371                 cpu_activeclock();
372         spinlock_exit();
373 }
374
375 void
376 cpu_halt(void)
377 {
378
379         /* We should have shutdown by now, if not enter a low power sleep */
380         intr_disable();
381         while (1) {
382                 __asm __volatile("wfi");
383         }
384 }
385
386 /*
387  * Flush the D-cache for non-DMA I/O so that the I-cache can
388  * be made coherent later.
389  */
390 void
391 cpu_flush_dcache(void *ptr, size_t len)
392 {
393
394         /* ARM64TODO TBD */
395 }
396
397 /* Get current clock frequency for the given CPU ID. */
398 int
399 cpu_est_clockrate(int cpu_id, uint64_t *rate)
400 {
401
402         panic("ARM64TODO: cpu_est_clockrate");
403 }
404
405 void
406 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
407 {
408
409         pcpu->pc_acpi_id = 0xffffffff;
410 }
411
412 void
413 spinlock_enter(void)
414 {
415         struct thread *td;
416         register_t daif;
417
418         td = curthread;
419         if (td->td_md.md_spinlock_count == 0) {
420                 daif = intr_disable();
421                 td->td_md.md_spinlock_count = 1;
422                 td->td_md.md_saved_daif = daif;
423         } else
424                 td->td_md.md_spinlock_count++;
425         critical_enter();
426 }
427
428 void
429 spinlock_exit(void)
430 {
431         struct thread *td;
432         register_t daif;
433
434         td = curthread;
435         critical_exit();
436         daif = td->td_md.md_saved_daif;
437         td->td_md.md_spinlock_count--;
438         if (td->td_md.md_spinlock_count == 0)
439                 intr_restore(daif);
440 }
441
442 #ifndef _SYS_SYSPROTO_H_
443 struct sigreturn_args {
444         ucontext_t *ucp;
445 };
446 #endif
447
448 int
449 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
450 {
451         ucontext_t uc;
452         uint32_t spsr;
453
454         if (uap == NULL)
455                 return (EFAULT);
456         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
457                 return (EFAULT);
458
459         spsr = uc.uc_mcontext.mc_gpregs.gp_spsr;
460         if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
461             (spsr & (PSR_F | PSR_I | PSR_A | PSR_D)) != 0)
462                 return (EINVAL); 
463
464         set_mcontext(td, &uc.uc_mcontext);
465         set_fpcontext(td, &uc.uc_mcontext);
466
467         /* Restore signal mask. */
468         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
469
470         return (EJUSTRETURN);
471 }
472
473 /*
474  * Construct a PCB from a trapframe. This is called from kdb_trap() where
475  * we want to start a backtrace from the function that caused us to enter
476  * the debugger. We have the context in the trapframe, but base the trace
477  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
478  * enough for a backtrace.
479  */
480 void
481 makectx(struct trapframe *tf, struct pcb *pcb)
482 {
483         int i;
484
485         for (i = 0; i < PCB_LR; i++)
486                 pcb->pcb_x[i] = tf->tf_x[i];
487
488         pcb->pcb_x[PCB_LR] = tf->tf_lr;
489         pcb->pcb_pc = tf->tf_elr;
490         pcb->pcb_sp = tf->tf_sp;
491 }
492
493 void
494 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
495 {
496         struct thread *td;
497         struct proc *p;
498         struct trapframe *tf;
499         struct sigframe *fp, frame;
500         struct sigacts *psp;
501         int code, onstack, sig;
502
503         td = curthread;
504         p = td->td_proc;
505         PROC_LOCK_ASSERT(p, MA_OWNED);
506
507         sig = ksi->ksi_signo;
508         code = ksi->ksi_code;
509         psp = p->p_sigacts;
510         mtx_assert(&psp->ps_mtx, MA_OWNED);
511
512         tf = td->td_frame;
513         onstack = sigonstack(tf->tf_sp);
514
515         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
516             catcher, sig);
517
518         /* Allocate and validate space for the signal handler context. */
519         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
520             SIGISMEMBER(psp->ps_sigonstack, sig)) {
521                 fp = (struct sigframe *)(td->td_sigstk.ss_sp +
522                     td->td_sigstk.ss_size);
523 #if defined(COMPAT_43)
524                 td->td_sigstk.ss_flags |= SS_ONSTACK;
525 #endif
526         } else {
527                 fp = (struct sigframe *)td->td_frame->tf_sp;
528         }
529
530         /* Make room, keeping the stack aligned */
531         fp--;
532         fp = (struct sigframe *)STACKALIGN(fp);
533
534         /* Fill in the frame to copy out */
535         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
536         get_fpcontext(td, &frame.sf_uc.uc_mcontext);
537         frame.sf_si = ksi->ksi_info;
538         frame.sf_uc.uc_sigmask = *mask;
539         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
540             ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
541         frame.sf_uc.uc_stack = td->td_sigstk;
542         mtx_unlock(&psp->ps_mtx);
543         PROC_UNLOCK(td->td_proc);
544
545         /* Copy the sigframe out to the user's stack. */
546         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
547                 /* Process has trashed its stack. Kill it. */
548                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
549                 PROC_LOCK(p);
550                 sigexit(td, SIGILL);
551         }
552
553         tf->tf_x[0]= sig;
554         tf->tf_x[1] = (register_t)&fp->sf_si;
555         tf->tf_x[2] = (register_t)&fp->sf_uc;
556
557         tf->tf_elr = (register_t)catcher;
558         tf->tf_sp = (register_t)fp;
559         tf->tf_lr = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
560
561         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
562             tf->tf_sp);
563
564         PROC_LOCK(p);
565         mtx_lock(&psp->ps_mtx);
566 }
567
568 static void
569 init_proc0(vm_offset_t kstack)
570 {
571         struct pcpu *pcpup = &__pcpu[0];
572
573         proc_linkup0(&proc0, &thread0);
574         thread0.td_kstack = kstack;
575         thread0.td_pcb = (struct pcb *)(thread0.td_kstack) - 1;
576         thread0.td_pcb->pcb_fpflags = 0;
577         thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
578         thread0.td_frame = &proc0_tf;
579         pcpup->pc_curpcb = thread0.td_pcb;
580 }
581
582 typedef struct {
583         uint32_t type;
584         uint64_t phys_start;
585         uint64_t virt_start;
586         uint64_t num_pages;
587         uint64_t attr;
588 } EFI_MEMORY_DESCRIPTOR;
589
590 static int
591 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
592     u_int *physmap_idxp)
593 {
594         u_int i, insert_idx, _physmap_idx;
595
596         _physmap_idx = *physmap_idxp;
597
598         if (length == 0)
599                 return (1);
600
601         /*
602          * Find insertion point while checking for overlap.  Start off by
603          * assuming the new entry will be added to the end.
604          */
605         insert_idx = _physmap_idx;
606         for (i = 0; i <= _physmap_idx; i += 2) {
607                 if (base < physmap[i + 1]) {
608                         if (base + length <= physmap[i]) {
609                                 insert_idx = i;
610                                 break;
611                         }
612                         if (boothowto & RB_VERBOSE)
613                                 printf(
614                     "Overlapping memory regions, ignoring second region\n");
615                         return (1);
616                 }
617         }
618
619         /* See if we can prepend to the next entry. */
620         if (insert_idx <= _physmap_idx &&
621             base + length == physmap[insert_idx]) {
622                 physmap[insert_idx] = base;
623                 return (1);
624         }
625
626         /* See if we can append to the previous entry. */
627         if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
628                 physmap[insert_idx - 1] += length;
629                 return (1);
630         }
631
632         _physmap_idx += 2;
633         *physmap_idxp = _physmap_idx;
634         if (_physmap_idx == PHYSMAP_SIZE) {
635                 printf(
636                 "Too many segments in the physical address map, giving up\n");
637                 return (0);
638         }
639
640         /*
641          * Move the last 'N' entries down to make room for the new
642          * entry if needed.
643          */
644         for (i = _physmap_idx; i > insert_idx; i -= 2) {
645                 physmap[i] = physmap[i - 2];
646                 physmap[i + 1] = physmap[i - 1];
647         }
648
649         /* Insert the new entry. */
650         physmap[insert_idx] = base;
651         physmap[insert_idx + 1] = base + length;
652         return (1);
653 }
654
655 #define efi_next_descriptor(ptr, size) \
656         ((struct efi_md *)(((uint8_t *) ptr) + size))
657
658 static void
659 add_efi_map_entries(struct efi_map_header *efihdr, vm_paddr_t *physmap,
660     u_int *physmap_idxp)
661 {
662         struct efi_md *map, *p;
663         const char *type;
664         size_t efisz;
665         int ndesc, i;
666
667         static const char *types[] = {
668                 "Reserved",
669                 "LoaderCode",
670                 "LoaderData",
671                 "BootServicesCode",
672                 "BootServicesData",
673                 "RuntimeServicesCode",
674                 "RuntimeServicesData",
675                 "ConventionalMemory",
676                 "UnusableMemory",
677                 "ACPIReclaimMemory",
678                 "ACPIMemoryNVS",
679                 "MemoryMappedIO",
680                 "MemoryMappedIOPortSpace",
681                 "PalCode"
682         };
683
684         /*
685          * Memory map data provided by UEFI via the GetMemoryMap
686          * Boot Services API.
687          */
688         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
689         map = (struct efi_md *)((uint8_t *)efihdr + efisz); 
690
691         if (efihdr->descriptor_size == 0)
692                 return;
693         ndesc = efihdr->memory_size / efihdr->descriptor_size;
694
695         if (boothowto & RB_VERBOSE)
696                 printf("%23s %12s %12s %8s %4s\n",
697                     "Type", "Physical", "Virtual", "#Pages", "Attr");
698
699         for (i = 0, p = map; i < ndesc; i++,
700             p = efi_next_descriptor(p, efihdr->descriptor_size)) {
701                 if (boothowto & RB_VERBOSE) {
702                         if (p->md_type <= EFI_MD_TYPE_PALCODE)
703                                 type = types[p->md_type];
704                         else
705                                 type = "<INVALID>";
706                         printf("%23s %012lx %12p %08lx ", type, p->md_phys,
707                             p->md_virt, p->md_pages);
708                         if (p->md_attr & EFI_MD_ATTR_UC)
709                                 printf("UC ");
710                         if (p->md_attr & EFI_MD_ATTR_WC)
711                                 printf("WC ");
712                         if (p->md_attr & EFI_MD_ATTR_WT)
713                                 printf("WT ");
714                         if (p->md_attr & EFI_MD_ATTR_WB)
715                                 printf("WB ");
716                         if (p->md_attr & EFI_MD_ATTR_UCE)
717                                 printf("UCE ");
718                         if (p->md_attr & EFI_MD_ATTR_WP)
719                                 printf("WP ");
720                         if (p->md_attr & EFI_MD_ATTR_RP)
721                                 printf("RP ");
722                         if (p->md_attr & EFI_MD_ATTR_XP)
723                                 printf("XP ");
724                         if (p->md_attr & EFI_MD_ATTR_RT)
725                                 printf("RUNTIME");
726                         printf("\n");
727                 }
728
729                 switch (p->md_type) {
730                 case EFI_MD_TYPE_CODE:
731                 case EFI_MD_TYPE_DATA:
732                 case EFI_MD_TYPE_BS_CODE:
733                 case EFI_MD_TYPE_BS_DATA:
734                 case EFI_MD_TYPE_FREE:
735                         /*
736                          * We're allowed to use any entry with these types.
737                          */
738                         break;
739                 default:
740                         continue;
741                 }
742
743                 if (!add_physmap_entry(p->md_phys, (p->md_pages * PAGE_SIZE),
744                     physmap, physmap_idxp))
745                         break;
746         }
747 }
748
749 #ifdef FDT
750 static void
751 try_load_dtb(caddr_t kmdp)
752 {
753         vm_offset_t dtbp;
754
755         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
756         if (dtbp == (vm_offset_t)NULL) {
757                 printf("ERROR loading DTB\n");
758                 return;
759         }
760
761         if (OF_install(OFW_FDT, 0) == FALSE)
762                 panic("Cannot install FDT");
763
764         if (OF_init((void *)dtbp) != 0)
765                 panic("OF_init failed with the found device tree");
766 }
767 #endif
768
769 static void
770 cache_setup(void)
771 {
772         int dcache_line_shift, icache_line_shift;
773         uint32_t ctr_el0;
774
775         ctr_el0 = READ_SPECIALREG(ctr_el0);
776
777         /* Read the log2 words in each D cache line */
778         dcache_line_shift = CTR_DLINE_SIZE(ctr_el0);
779         /* Get the D cache line size */
780         dcache_line_size = sizeof(int) << dcache_line_shift;
781
782         /* And the same for the I cache */
783         icache_line_shift = CTR_ILINE_SIZE(ctr_el0);
784         icache_line_size = sizeof(int) << icache_line_shift;
785
786         idcache_line_size = MIN(dcache_line_size, icache_line_size);
787 }
788
789 void
790 initarm(struct arm64_bootparams *abp)
791 {
792         struct efi_map_header *efihdr;
793         struct pcpu *pcpup;
794         vm_offset_t lastaddr;
795         caddr_t kmdp;
796         vm_paddr_t mem_len;
797         int i;
798
799         /* Set the module data location */
800         preload_metadata = (caddr_t)(uintptr_t)(abp->modulep);
801
802         /* Find the kernel address */
803         kmdp = preload_search_by_type("elf kernel");
804         if (kmdp == NULL)
805                 kmdp = preload_search_by_type("elf64 kernel");
806
807         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
808         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
809
810 #ifdef FDT
811         try_load_dtb(kmdp);
812 #endif
813
814         /* Find the address to start allocating from */
815         lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
816
817         /* Load the physical memory ranges */
818         physmap_idx = 0;
819         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
820             MODINFO_METADATA | MODINFOMD_EFI_MAP);
821         add_efi_map_entries(efihdr, physmap, &physmap_idx);
822
823         /* Print the memory map */
824         mem_len = 0;
825         for (i = 0; i < physmap_idx; i += 2)
826                 mem_len += physmap[i + 1] - physmap[i];
827
828         /* Set the pcpu data, this is needed by pmap_bootstrap */
829         pcpup = &__pcpu[0];
830         pcpu_init(pcpup, 0, sizeof(struct pcpu));
831
832         /*
833          * Set the pcpu pointer with a backup in tpidr_el1 to be
834          * loaded when entering the kernel from userland.
835          */
836         __asm __volatile(
837             "mov x18, %0 \n"
838             "msr tpidr_el1, %0" :: "r"(pcpup));
839
840         PCPU_SET(curthread, &thread0);
841
842         /* Do basic tuning, hz etc */
843         init_param1();
844
845         cache_setup();
846
847         /* Bootstrap enough of pmap  to enter the kernel proper */
848         pmap_bootstrap(abp->kern_l1pt, KERNBASE - abp->kern_delta,
849             lastaddr - KERNBASE);
850
851         arm_devmap_bootstrap(0, NULL);
852
853         cninit();
854
855         init_proc0(abp->kern_stack);
856         msgbufinit(msgbufp, msgbufsize);
857         mutex_init();
858         init_param2(physmem);
859
860         dbg_monitor_init();
861         kdb_init();
862
863         early_boot = 0;
864 }
865