xref: /freebsd/sys/arm64/include/pmap.h (revision c7046f76c2c027b00c0e6ba57cfd28f1a78f5e23)
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * the Systems Programming Group of the University of Utah Computer
7  * Science Department and William Jolitz of UUNET Technologies Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #ifndef _MACHINE_PMAP_H_
37 #define	_MACHINE_PMAP_H_
38 
39 #include <machine/pte.h>
40 
41 #ifndef LOCORE
42 
43 #include <sys/queue.h>
44 #include <sys/_lock.h>
45 #include <sys/_mutex.h>
46 
47 #include <vm/_vm_radix.h>
48 
49 #ifdef _KERNEL
50 
51 #define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
52 
53 #endif
54 
55 #define	pmap_page_get_memattr(m)	((m)->md.pv_memattr)
56 #define	pmap_page_is_write_mapped(m)	(((m)->a.flags & PGA_WRITEABLE) != 0)
57 void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma);
58 
59 /*
60  * Pmap stuff
61  */
62 
63 struct md_page {
64 	TAILQ_HEAD(,pv_entry)	pv_list;
65 	int			pv_gen;
66 	vm_memattr_t		pv_memattr;
67 };
68 
69 /*
70  * This structure is used to hold a virtual<->physical address
71  * association and is used mostly by bootstrap code
72  */
73 struct pv_addr {
74 	SLIST_ENTRY(pv_addr) pv_list;
75 	vm_offset_t	pv_va;
76 	vm_paddr_t	pv_pa;
77 };
78 
79 enum pmap_stage {
80 	PM_INVALID,
81 	PM_STAGE1,
82 	PM_STAGE2,
83 };
84 
85 struct pmap {
86 	struct mtx		pm_mtx;
87 	struct pmap_statistics	pm_stats;	/* pmap statistics */
88 	uint64_t		pm_ttbr;
89 	vm_paddr_t		pm_l0_paddr;
90 	pd_entry_t		*pm_l0;
91 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
92 	struct vm_radix		pm_root;	/* spare page table pages */
93 	long			pm_cookie;	/* encodes the pmap's ASID */
94 	struct asid_set		*pm_asid_set;	/* The ASID/VMID set to use */
95 	enum pmap_stage		pm_stage;
96 	int			pm_levels;
97 };
98 typedef struct pmap *pmap_t;
99 
100 typedef struct pv_entry {
101 	vm_offset_t		pv_va;	/* virtual address for mapping */
102 	TAILQ_ENTRY(pv_entry)	pv_next;
103 } *pv_entry_t;
104 
105 /*
106  * pv_entries are allocated in chunks per-process.  This avoids the
107  * need to track per-pmap assignments.
108  */
109 #if PAGE_SIZE == PAGE_SIZE_4K
110 #define	_NPCPV	168
111 #define	_NPAD	0
112 #elif PAGE_SIZE == PAGE_SIZE_16K
113 #define	_NPCPV	677
114 #define	_NPAD	1
115 #else
116 #error Unsupported page size
117 #endif
118 #define	_NPCM	howmany(_NPCPV, 64)
119 
120 #define	PV_CHUNK_HEADER							\
121 	pmap_t			pc_pmap;				\
122 	TAILQ_ENTRY(pv_chunk)	pc_list;				\
123 	uint64_t		pc_map[_NPCM];	/* bitmap; 1 = free */	\
124 	TAILQ_ENTRY(pv_chunk)	pc_lru;
125 
126 struct pv_chunk_header {
127 	PV_CHUNK_HEADER
128 };
129 
130 struct pv_chunk {
131 	PV_CHUNK_HEADER
132 	struct pv_entry		pc_pventry[_NPCPV];
133 	uint64_t		pc_pad[_NPAD];
134 };
135 
136 struct thread;
137 
138 #ifdef _KERNEL
139 extern struct pmap	kernel_pmap_store;
140 #define	kernel_pmap	(&kernel_pmap_store)
141 #define	pmap_kernel()	kernel_pmap
142 
143 #define	PMAP_ASSERT_LOCKED(pmap) \
144 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
145 #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
146 #define	PMAP_LOCK_ASSERT(pmap, type) \
147 				mtx_assert(&(pmap)->pm_mtx, (type))
148 #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
149 #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
150 				    NULL, MTX_DEF | MTX_DUPOK)
151 #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
152 #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
153 #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
154 #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
155 
156 #define	ASID_RESERVED_FOR_PID_0	0
157 #define	ASID_RESERVED_FOR_EFI	1
158 #define	ASID_FIRST_AVAILABLE	(ASID_RESERVED_FOR_EFI + 1)
159 #define	ASID_TO_OPERAND(asid)	({					\
160 	KASSERT((asid) != -1, ("invalid ASID"));			\
161 	(uint64_t)(asid) << TTBR_ASID_SHIFT;			\
162 })
163 
164 extern vm_offset_t virtual_avail;
165 extern vm_offset_t virtual_end;
166 
167 /*
168  * Macros to test if a mapping is mappable with an L1 Section mapping
169  * or an L2 Large Page mapping.
170  */
171 #define	L1_MAPPABLE_P(va, pa, size)					\
172 	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE)
173 
174 #define	pmap_vm_page_alloc_check(m)
175 
176 void	pmap_activate_vm(pmap_t);
177 void	pmap_bootstrap(vm_offset_t, vm_offset_t, vm_paddr_t, vm_size_t);
178 int	pmap_change_attr(vm_offset_t va, vm_size_t size, int mode);
179 int	pmap_change_prot(vm_offset_t va, vm_size_t size, vm_prot_t prot);
180 void	pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode);
181 void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t);
182 bool	pmap_klookup(vm_offset_t va, vm_paddr_t *pa);
183 vm_paddr_t pmap_kextract(vm_offset_t va);
184 void	pmap_kremove(vm_offset_t);
185 void	pmap_kremove_device(vm_offset_t, vm_size_t);
186 void	*pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma);
187 bool	pmap_page_is_mapped(vm_page_t m);
188 int	pmap_pinit_stage(pmap_t, enum pmap_stage, int);
189 bool	pmap_ps_enabled(pmap_t pmap);
190 uint64_t pmap_to_ttbr0(pmap_t pmap);
191 void	pmap_disable_promotion(vm_offset_t sva, vm_size_t size);
192 
193 void	*pmap_mapdev(vm_paddr_t, vm_size_t);
194 void	*pmap_mapbios(vm_paddr_t, vm_size_t);
195 void	pmap_unmapdev(vm_offset_t, vm_size_t);
196 void	pmap_unmapbios(vm_offset_t, vm_size_t);
197 
198 boolean_t pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
199 void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
200 
201 bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **,
202     pd_entry_t **, pt_entry_t **);
203 
204 int	pmap_fault(pmap_t, uint64_t, uint64_t);
205 
206 struct pcb *pmap_switch(struct thread *);
207 
208 extern void (*pmap_clean_stage2_tlbi)(void);
209 extern void (*pmap_invalidate_vpipt_icache)(void);
210 
211 static inline int
212 pmap_vmspace_copy(pmap_t dst_pmap __unused, pmap_t src_pmap __unused)
213 {
214 
215 	return (0);
216 }
217 
218 #endif	/* _KERNEL */
219 
220 #endif	/* !LOCORE */
221 
222 #endif	/* !_MACHINE_PMAP_H_ */
223