1 /* $NetBSD: efi_machdep.c,v 1.4 2023/07/10 07:00:12 rin 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> and Nick Hudson
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.4 2023/07/10 07:00:12 rin Exp $");
34 
35 #include <sys/param.h>
36 
37 #include <uvm/uvm_extern.h>
38 
39 #include <arm/efirt.h>
40 #include <arm/vfpreg.h>
41 
42 #include <arm/arm/efi_runtime.h>
43 
44 #include <arm/arm32/machdep.h>
45 
46 static struct {
47           struct faultbuf     aert_faultbuf;
48           uint32_t  aert_tpidrprw;
49           uint32_t  aert_fpexc;
50 } arm_efirt_state;
51 
52 int
arm_efirt_md_enter(void)53 arm_efirt_md_enter(void)
54 {
55           kpreempt_disable();
56 
57           struct lwp * const l = curlwp;
58 
59           arm_efirt_state.aert_tpidrprw = armreg_tpidrprw_read();
60 
61           /* Disable the VFP.  */
62           arm_efirt_state.aert_fpexc = armreg_fpexc_read();
63           armreg_fpexc_write(arm_efirt_state.aert_fpexc & ~VFP_FPEXC_EN);
64           isb();
65 
66           /*
67            * Install custom fault handler. EFI lock is held across calls so
68            * shared faultbuf is safe here.
69            */
70           int err = cpu_set_onfault(&arm_efirt_state.aert_faultbuf);
71           if (err)
72                     return err;
73 
74           if ((l->l_flag & LW_SYSTEM) == 0) {
75                     pmap_deactivate(l);
76           }
77           pmap_activate_efirt();
78 
79           return 0;
80 }
81 
82 void
arm_efirt_md_exit(void)83 arm_efirt_md_exit(void)
84 {
85           struct lwp * const l = curlwp;
86 
87           pmap_deactivate_efirt();
88           if ((l->l_flag & LW_SYSTEM) == 0) {
89                     pmap_activate(l);
90           }
91 
92           armreg_tpidrprw_write(arm_efirt_state.aert_tpidrprw);
93 
94           /* Restore FP access (if it existed) */
95           armreg_fpexc_write(arm_efirt_state.aert_fpexc);
96           isb();
97 
98           /* Remove custom fault handler */
99           cpu_unset_onfault();
100 
101           kpreempt_enable();
102 }
103 
104 
105 void
cpu_efirt_map_range(vaddr_t va,paddr_t pa,size_t sz,enum cpu_efirt_mem_type type)106 cpu_efirt_map_range(vaddr_t va, paddr_t pa, size_t sz,
107     enum cpu_efirt_mem_type type)
108 {
109           int flags = 0;
110           int prot = 0;
111 
112           switch (type) {
113           case ARM_EFIRT_MEM_CODE:
114                     /* need write permission because fw devs */
115                     prot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
116                     flags = prot;
117                     break;
118           case ARM_EFIRT_MEM_DATA:
119                     prot = VM_PROT_READ | VM_PROT_WRITE;
120                     flags = prot;
121                     break;
122           case ARM_EFIRT_MEM_MMIO:
123                     prot = VM_PROT_READ | VM_PROT_WRITE;
124                     flags = prot | PMAP_DEV;
125                     break;
126           default:
127                     panic("%s: unsupported type %d", __func__, type);
128           }
129           if (va >= VM_MAXUSER_ADDRESS || va >= VM_MAXUSER_ADDRESS - sz) {
130                     printf("Incorrect EFI mapping range %" PRIxVADDR
131                         "- %" PRIxVADDR "\n", va, va + sz);
132           }
133 
134           while (sz != 0) {
135                     pmap_enter(pmap_efirt(), va, pa, prot, flags | PMAP_WIRED);
136                     va += PAGE_SIZE;
137                     pa += PAGE_SIZE;
138                     sz -= PAGE_SIZE;
139           }
140           pmap_update(pmap_efirt());
141 }
142