2 * Copyright (c) 2014 Andrew Turner
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include "opt_platform.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
42 #include <sys/devmap.h>
45 #include <sys/imgact.h>
47 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/linker.h>
51 #include <sys/msgbuf.h>
53 #include <sys/physmem.h>
55 #include <sys/ptrace.h>
56 #include <sys/reboot.h>
57 #include <sys/rwlock.h>
58 #include <sys/sched.h>
59 #include <sys/signalvar.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysent.h>
62 #include <sys/sysproto.h>
63 #include <sys/ucontext.h>
65 #include <sys/vmmeter.h>
68 #include <vm/vm_param.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_phys.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_pager.h>
77 #include <machine/armreg.h>
78 #include <machine/cpu.h>
79 #include <machine/debug_monitor.h>
80 #include <machine/kdb.h>
81 #include <machine/machdep.h>
82 #include <machine/metadata.h>
83 #include <machine/md_var.h>
84 #include <machine/pcb.h>
85 #include <machine/reg.h>
86 #include <machine/undefined.h>
87 #include <machine/vmparam.h>
90 #include <machine/vfp.h>
94 #include <contrib/dev/acpica/include/acpi.h>
95 #include <machine/acpica_machdep.h>
99 #include <dev/fdt/fdt_common.h>
100 #include <dev/ofw/openfirm.h>
103 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
106 * XXX: The .bss is assumed to be in the boot CPU NUMA domain. If not we
107 * could relocate this, but will need to keep the same virtual address as
108 * it's reverenced by the EARLY_COUNTER macro.
112 static struct trapframe proc0_tf;
118 struct kva_md_info kmi;
120 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
124 * Physical address of the EFI System Table. Stashed from the metadata hints
125 * passed into the kernel and used by the EFI code to call runtime services.
127 vm_paddr_t efi_systbl_phys;
128 static struct efi_map_header *efihdr;
130 /* pagezero_* implementations are provided in support.S */
131 void pagezero_simple(void *);
132 void pagezero_cache(void *);
134 /* pagezero_simple is default pagezero */
135 void (*pagezero)(void *p) = pagezero_simple;
137 int (*apei_nmi)(void);
142 uint64_t id_aa64mfr1;
144 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
145 if (ID_AA64MMFR1_PAN_VAL(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
154 * The LLVM integrated assembler doesn't understand the PAN
155 * PSTATE field. Because of this we need to manually create
156 * the instruction in an asm block. This is equivalent to:
159 * This sets the PAN bit, stopping the kernel from accessing
160 * memory when userspace can also access it unless the kernel
161 * uses the userspace load/store instructions.
164 WRITE_SPECIALREG(sctlr_el1,
165 READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
166 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
174 return (boot_el == 2);
178 cpu_startup(void *dummy)
183 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
184 ptoa((uintmax_t)realmem) / 1024 / 1024);
187 printf("Physical memory chunk(s):\n");
188 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
189 size = phys_avail[i + 1] - phys_avail[i];
190 printf("%#016jx - %#016jx, %ju bytes (%ju pages)\n",
191 (uintmax_t)phys_avail[i],
192 (uintmax_t)phys_avail[i + 1] - 1,
193 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
197 printf("avail memory = %ju (%ju MB)\n",
198 ptoa((uintmax_t)vm_free_count()),
199 ptoa((uintmax_t)vm_free_count()) / 1024 / 1024);
202 install_cpu_errata();
204 vm_ksubmap_init(&kmi);
206 vm_pager_bufferinit();
209 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
212 late_ifunc_resolve(void *dummy __unused)
214 link_elf_late_ireloc();
216 SYSINIT(late_ifunc_resolve, SI_SUB_CPU, SI_ORDER_ANY, late_ifunc_resolve, NULL);
219 cpu_idle_wakeup(int cpu)
232 if (!sched_runnable())
245 /* We should have shutdown by now, if not enter a low power sleep */
248 __asm __volatile("wfi");
253 * Flush the D-cache for non-DMA I/O so that the I-cache can
254 * be made coherent later.
257 cpu_flush_dcache(void *ptr, size_t len)
263 /* Get current clock frequency for the given CPU ID. */
265 cpu_est_clockrate(int cpu_id, uint64_t *rate)
269 pc = pcpu_find(cpu_id);
270 if (pc == NULL || rate == NULL)
273 if (pc->pc_clock == 0)
276 *rate = pc->pc_clock;
281 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
284 pcpu->pc_acpi_id = 0xffffffff;
285 pcpu->pc_mpidr = 0xffffffff;
295 if (td->td_md.md_spinlock_count == 0) {
296 daif = intr_disable();
297 td->td_md.md_spinlock_count = 1;
298 td->td_md.md_saved_daif = daif;
301 td->td_md.md_spinlock_count++;
311 daif = td->td_md.md_saved_daif;
312 td->td_md.md_spinlock_count--;
313 if (td->td_md.md_spinlock_count == 0) {
320 * Construct a PCB from a trapframe. This is called from kdb_trap() where
321 * we want to start a backtrace from the function that caused us to enter
322 * the debugger. We have the context in the trapframe, but base the trace
323 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
324 * enough for a backtrace.
327 makectx(struct trapframe *tf, struct pcb *pcb)
331 for (i = 0; i < nitems(pcb->pcb_x); i++)
332 pcb->pcb_x[i] = tf->tf_x[i];
334 /* NB: pcb_lr is the PC, see PC_REGS() in db_machdep.h */
335 pcb->pcb_lr = tf->tf_elr;
336 pcb->pcb_sp = tf->tf_sp;
340 init_proc0(vm_offset_t kstack)
344 pcpup = cpuid_to_pcpu[0];
345 MPASS(pcpup != NULL);
347 proc_linkup0(&proc0, &thread0);
348 thread0.td_kstack = kstack;
349 thread0.td_kstack_pages = KSTACK_PAGES;
350 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
351 thread0.td_kstack_pages * PAGE_SIZE) - 1;
352 thread0.td_pcb->pcb_fpflags = 0;
353 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
354 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
355 thread0.td_frame = &proc0_tf;
356 pcpup->pc_curpcb = thread0.td_pcb;
359 * Unmask SError exceptions. They are used to signal a RAS failure,
360 * or other hardware error.
371 } EFI_MEMORY_DESCRIPTOR;
373 typedef void (*efi_map_entry_cb)(struct efi_md *);
376 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
378 struct efi_md *map, *p;
383 * Memory map data provided by UEFI via the GetMemoryMap
386 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
387 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
389 if (efihdr->descriptor_size == 0)
391 ndesc = efihdr->memory_size / efihdr->descriptor_size;
393 for (i = 0, p = map; i < ndesc; i++,
394 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
400 exclude_efi_map_entry(struct efi_md *p)
403 switch (p->md_type) {
404 case EFI_MD_TYPE_CODE:
405 case EFI_MD_TYPE_DATA:
406 case EFI_MD_TYPE_BS_CODE:
407 case EFI_MD_TYPE_BS_DATA:
408 case EFI_MD_TYPE_FREE:
410 * We're allowed to use any entry with these types.
414 physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
420 exclude_efi_map_entries(struct efi_map_header *efihdr)
423 foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
427 add_efi_map_entry(struct efi_md *p)
430 switch (p->md_type) {
431 case EFI_MD_TYPE_RT_DATA:
433 * Runtime data will be excluded after the DMAP
434 * region is created to stop it from being added
437 case EFI_MD_TYPE_CODE:
438 case EFI_MD_TYPE_DATA:
439 case EFI_MD_TYPE_BS_CODE:
440 case EFI_MD_TYPE_BS_DATA:
441 case EFI_MD_TYPE_FREE:
443 * We're allowed to use any entry with these types.
445 physmem_hardware_region(p->md_phys,
446 p->md_pages * PAGE_SIZE);
452 add_efi_map_entries(struct efi_map_header *efihdr)
455 foreach_efi_map_entry(efihdr, add_efi_map_entry);
459 print_efi_map_entry(struct efi_md *p)
462 static const char *types[] = {
468 "RuntimeServicesCode",
469 "RuntimeServicesData",
470 "ConventionalMemory",
475 "MemoryMappedIOPortSpace",
480 if (p->md_type < nitems(types))
481 type = types[p->md_type];
484 printf("%23s %012lx %12p %08lx ", type, p->md_phys,
485 p->md_virt, p->md_pages);
486 if (p->md_attr & EFI_MD_ATTR_UC)
488 if (p->md_attr & EFI_MD_ATTR_WC)
490 if (p->md_attr & EFI_MD_ATTR_WT)
492 if (p->md_attr & EFI_MD_ATTR_WB)
494 if (p->md_attr & EFI_MD_ATTR_UCE)
496 if (p->md_attr & EFI_MD_ATTR_WP)
498 if (p->md_attr & EFI_MD_ATTR_RP)
500 if (p->md_attr & EFI_MD_ATTR_XP)
502 if (p->md_attr & EFI_MD_ATTR_NV)
504 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
505 printf("MORE_RELIABLE ");
506 if (p->md_attr & EFI_MD_ATTR_RO)
508 if (p->md_attr & EFI_MD_ATTR_RT)
514 print_efi_map_entries(struct efi_map_header *efihdr)
517 printf("%23s %12s %12s %8s %4s\n",
518 "Type", "Physical", "Virtual", "#Pages", "Attr");
519 foreach_efi_map_entry(efihdr, print_efi_map_entry);
524 try_load_dtb(caddr_t kmdp)
528 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
529 #if defined(FDT_DTB_STATIC)
531 * In case the device tree blob was not retrieved (from metadata) try
532 * to use the statically embedded one.
535 dtbp = (vm_offset_t)&fdt_static_dtb;
538 if (dtbp == (vm_offset_t)NULL) {
539 printf("ERROR loading DTB\n");
543 if (OF_install(OFW_FDT, 0) == FALSE)
544 panic("Cannot install FDT");
546 if (OF_init((void *)dtbp) != 0)
547 panic("OF_init failed with the found device tree");
549 parse_fdt_bootargs();
556 bool has_acpi, has_fdt;
559 has_acpi = has_fdt = false;
562 has_fdt = (OF_peer(0) != 0);
565 has_acpi = (AcpiOsGetRootPointer() != 0);
568 env = kern_getenv("kern.cfg.order");
571 while (order != NULL) {
573 strncmp(order, "acpi", 4) == 0 &&
574 (order[4] == ',' || order[4] == '\0')) {
575 arm64_bus_method = ARM64_BUS_ACPI;
579 strncmp(order, "fdt", 3) == 0 &&
580 (order[3] == ',' || order[3] == '\0')) {
581 arm64_bus_method = ARM64_BUS_FDT;
584 order = strchr(order, ',');
588 /* If we set the bus method it is valid */
589 if (arm64_bus_method != ARM64_BUS_NONE)
592 /* If no order or an invalid order was set use the default */
593 if (arm64_bus_method == ARM64_BUS_NONE) {
595 arm64_bus_method = ARM64_BUS_FDT;
597 arm64_bus_method = ARM64_BUS_ACPI;
601 * If no option was set the default is valid, otherwise we are
602 * setting one to get cninit() working, then calling panic to tell
603 * the user about the invalid bus setup.
605 return (env == NULL);
611 int dczva_line_shift;
614 identify_cache(READ_SPECIALREG(ctr_el0));
616 dczid_el0 = READ_SPECIALREG(dczid_el0);
618 /* Check if dc zva is not prohibited */
619 if (dczid_el0 & DCZID_DZP)
622 /* Same as with above calculations */
623 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
624 dczva_line_size = sizeof(int) << dczva_line_shift;
626 /* Change pagezero function */
627 pagezero = pagezero_cache;
632 memory_mapping_mode(vm_paddr_t pa)
634 struct efi_md *map, *p;
639 return (VM_MEMATTR_WRITE_BACK);
642 * Memory map data provided by UEFI via the GetMemoryMap
645 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
646 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
648 if (efihdr->descriptor_size == 0)
649 return (VM_MEMATTR_WRITE_BACK);
650 ndesc = efihdr->memory_size / efihdr->descriptor_size;
652 for (i = 0, p = map; i < ndesc; i++,
653 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
654 if (pa < p->md_phys ||
655 pa >= p->md_phys + p->md_pages * EFI_PAGE_SIZE)
657 if (p->md_type == EFI_MD_TYPE_IOMEM ||
658 p->md_type == EFI_MD_TYPE_IOPORT)
659 return (VM_MEMATTR_DEVICE);
660 else if ((p->md_attr & EFI_MD_ATTR_WB) != 0 ||
661 p->md_type == EFI_MD_TYPE_RECLAIM)
662 return (VM_MEMATTR_WRITE_BACK);
663 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
664 return (VM_MEMATTR_WRITE_THROUGH);
665 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
666 return (VM_MEMATTR_WRITE_COMBINING);
670 return (VM_MEMATTR_DEVICE);
674 initarm(struct arm64_bootparams *abp)
676 struct efi_fb *efifb;
680 struct mem_region mem_regions[FDT_MEM_REGIONS];
683 char dts_version[255];
685 vm_offset_t lastaddr;
689 boot_el = abp->boot_el;
691 /* Parse loader or FDT boot parametes. Determine last used address. */
692 lastaddr = parse_boot_param(abp);
694 /* Find the kernel address */
695 kmdp = preload_search_by_type("elf kernel");
697 kmdp = preload_search_by_type("elf64 kernel");
700 update_special_regs(0);
702 link_elf_ireloc(kmdp);
705 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
707 /* Load the physical memory ranges */
708 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
709 MODINFO_METADATA | MODINFOMD_EFI_MAP);
711 add_efi_map_entries(efihdr);
714 /* Grab physical memory regions information from device tree. */
715 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
717 panic("Cannot get physical memory regions");
718 physmem_hardware_regions(mem_regions, mem_regions_sz);
720 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
721 physmem_exclude_regions(mem_regions, mem_regions_sz,
722 EXFLAG_NODUMP | EXFLAG_NOALLOC);
725 /* Exclude the EFI framebuffer from our view of physical memory. */
726 efifb = (struct efi_fb *)preload_search_info(kmdp,
727 MODINFO_METADATA | MODINFOMD_EFI_FB);
729 physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
732 /* Set the pcpu data, this is needed by pmap_bootstrap */
734 pcpu_init(pcpup, 0, sizeof(struct pcpu));
737 * Set the pcpu pointer with a backup in tpidr_el1 to be
738 * loaded when entering the kernel from userland.
742 "msr tpidr_el1, %0" :: "r"(pcpup));
744 PCPU_SET(curthread, &thread0);
745 PCPU_SET(midr, get_midr());
747 /* Do basic tuning, hz etc */
753 /* Bootstrap enough of pmap to enter the kernel proper */
754 pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
755 KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
756 /* Exclude entries neexed in teh DMAP region, but not phys_avail */
758 exclude_efi_map_entries(efihdr);
759 physmem_init_kernel_globals();
761 devmap_bootstrap(0, NULL);
766 set_ttbr0(abp->kern_ttbr0);
770 panic("Invalid bus configuration: %s",
771 kern_getenv("kern.cfg.order"));
774 * Dump the boot metadata. We have to wait for cninit() since console
775 * output is required. If it's grossly incorrect the kernel will never
778 if (getenv_is_true("debug.dump_modinfo_at_boot"))
781 init_proc0(abp->kern_stack);
782 msgbufinit(msgbufp, msgbufsize);
784 init_param2(physmem);
792 env = kern_getenv("kernelname");
794 strlcpy(kernelname, env, sizeof(kernelname));
797 if (arm64_bus_method == ARM64_BUS_FDT) {
798 root = OF_finddevice("/");
799 if (OF_getprop(root, "freebsd,dts-version", dts_version, sizeof(dts_version)) > 0) {
800 if (strcmp(LINUX_DTS_VERSION, dts_version) != 0)
801 printf("WARNING: DTB version is %s while kernel expects %s, "
802 "please update the DTB in the ESP\n",
806 printf("WARNING: Cannot find freebsd,dts-version property, "
807 "cannot check DTB compliance\n");
812 if (boothowto & RB_VERBOSE) {
814 print_efi_map_entries(efihdr);
815 physmem_print_tables();
826 WRITE_SPECIALREG(oslar_el1, 0);
828 /* This permits DDB to use debug registers for watchpoints. */
831 /* TODO: Eventually will need to initialize debug registers here. */
837 DB_SHOW_COMMAND(specialregs, db_show_spregs)
839 #define PRINT_REG(reg) \
840 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
842 PRINT_REG(actlr_el1);
843 PRINT_REG(afsr0_el1);
844 PRINT_REG(afsr1_el1);
846 PRINT_REG(amair_el1);
847 PRINT_REG(ccsidr_el1);
848 PRINT_REG(clidr_el1);
849 PRINT_REG(contextidr_el1);
850 PRINT_REG(cpacr_el1);
851 PRINT_REG(csselr_el1);
853 PRINT_REG(currentel);
855 PRINT_REG(dczid_el0);
860 /* ARM64TODO: Enable VFP before reading floating-point registers */
864 PRINT_REG(id_aa64afr0_el1);
865 PRINT_REG(id_aa64afr1_el1);
866 PRINT_REG(id_aa64dfr0_el1);
867 PRINT_REG(id_aa64dfr1_el1);
868 PRINT_REG(id_aa64isar0_el1);
869 PRINT_REG(id_aa64isar1_el1);
870 PRINT_REG(id_aa64pfr0_el1);
871 PRINT_REG(id_aa64pfr1_el1);
872 PRINT_REG(id_afr0_el1);
873 PRINT_REG(id_dfr0_el1);
874 PRINT_REG(id_isar0_el1);
875 PRINT_REG(id_isar1_el1);
876 PRINT_REG(id_isar2_el1);
877 PRINT_REG(id_isar3_el1);
878 PRINT_REG(id_isar4_el1);
879 PRINT_REG(id_isar5_el1);
880 PRINT_REG(id_mmfr0_el1);
881 PRINT_REG(id_mmfr1_el1);
882 PRINT_REG(id_mmfr2_el1);
883 PRINT_REG(id_mmfr3_el1);
885 /* Missing from llvm */
886 PRINT_REG(id_mmfr4_el1);
888 PRINT_REG(id_pfr0_el1);
889 PRINT_REG(id_pfr1_el1);
893 PRINT_REG(mpidr_el1);
894 PRINT_REG(mvfr0_el1);
895 PRINT_REG(mvfr1_el1);
896 PRINT_REG(mvfr2_el1);
897 PRINT_REG(revidr_el1);
898 PRINT_REG(sctlr_el1);
903 PRINT_REG(tpidr_el0);
904 PRINT_REG(tpidr_el1);
905 PRINT_REG(tpidrro_el0);
906 PRINT_REG(ttbr0_el1);
907 PRINT_REG(ttbr1_el1);
912 DB_SHOW_COMMAND(vtop, db_show_vtop)
917 phys = arm64_address_translate_s1e1r(addr);
918 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
919 phys = arm64_address_translate_s1e1w(addr);
920 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
921 phys = arm64_address_translate_s1e0r(addr);
922 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
923 phys = arm64_address_translate_s1e0w(addr);
924 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
926 db_printf("show vtop <virt_addr>\n");