1 /* $NetBSD: efi_machdep.c,v 1.5 2019/12/16 00:03:50 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jared McNeill <jmcneill@invisible.ca>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: efi_machdep.c,v 1.5 2019/12/16 00:03:50 jmcneill Exp $"); 34 35 #include <sys/param.h> 36 #include <uvm/uvm_extern.h> 37 #include <machine/cpufunc.h> 38 39 #include <arm/arm/efi_runtime.h> 40 41 #include <aarch64/machdep.h> 42 43 static struct { 44 struct faultbuf faultbuf; 45 bool fpu_used; 46 } arm_efirt_state; 47 48 void 49 arm_efirt_md_map_range(vaddr_t va, paddr_t pa, size_t sz, enum arm_efirt_mem_type type) 50 { 51 pt_entry_t attr; 52 53 switch (type) { 54 case ARM_EFIRT_MEM_CODE: 55 attr = LX_BLKPAG_OS_READ | LX_BLKPAG_OS_WRITE | 56 LX_BLKPAG_AF | LX_BLKPAG_AP_RW | LX_BLKPAG_UXN | 57 LX_BLKPAG_ATTR_NORMAL_WB; 58 break; 59 case ARM_EFIRT_MEM_DATA: 60 attr = LX_BLKPAG_OS_READ | LX_BLKPAG_OS_WRITE | 61 LX_BLKPAG_AF | LX_BLKPAG_AP_RW | LX_BLKPAG_UXN | LX_BLKPAG_PXN | 62 LX_BLKPAG_ATTR_NORMAL_WB; 63 break; 64 case ARM_EFIRT_MEM_MMIO: 65 attr = LX_BLKPAG_OS_READ | LX_BLKPAG_OS_WRITE | 66 LX_BLKPAG_AF | LX_BLKPAG_AP_RW | LX_BLKPAG_UXN | LX_BLKPAG_PXN | 67 LX_BLKPAG_ATTR_DEVICE_MEM; 68 break; 69 default: 70 panic("arm_efirt_md_map_range: unsupported type %d", type); 71 } 72 73 pmapboot_enter(va, pa, sz, L3_SIZE, attr, 0, bootpage_alloc, NULL); 74 while (sz >= PAGE_SIZE) { 75 aarch64_tlbi_by_va(va); 76 va += PAGE_SIZE; 77 sz -= PAGE_SIZE; 78 } 79 } 80 81 int 82 arm_efirt_md_enter(void) 83 { 84 struct lwp *l = curlwp; 85 86 /* Save FPU state */ 87 arm_efirt_state.fpu_used = fpu_used_p(l) != 0; 88 if (arm_efirt_state.fpu_used) 89 fpu_save(l); 90 91 /* Enable FP access (AArch64 UEFI calling convention) */ 92 reg_cpacr_el1_write(CPACR_FPEN_ALL); 93 __asm __volatile ("isb"); 94 95 /* 96 * Install custom fault handler. EFI lock is held across calls so 97 * shared faultbuf is safe here. 98 */ 99 return cpu_set_onfault(&arm_efirt_state.faultbuf); 100 } 101 102 void 103 arm_efirt_md_exit(void) 104 { 105 struct lwp *l = curlwp; 106 107 /* Disable FP access */ 108 reg_cpacr_el1_write(CPACR_FPEN_NONE); 109 __asm __volatile ("isb"); 110 111 /* Restore FPU state */ 112 if (arm_efirt_state.fpu_used) 113 fpu_load(l); 114 115 /* Remove custom fault handler */ 116 cpu_unset_onfault(); 117 } 118