xref: /freebsd/sys/arm64/include/pmap.h (revision ba31362694fe465b88a025ac7c35ad7f378efc86)
1412042e2SAndrew Turner /*-
2412042e2SAndrew Turner  * Copyright (c) 1991 Regents of the University of California.
3412042e2SAndrew Turner  * All rights reserved.
4412042e2SAndrew Turner  *
5412042e2SAndrew Turner  * This code is derived from software contributed to Berkeley by
6412042e2SAndrew Turner  * the Systems Programming Group of the University of Utah Computer
7412042e2SAndrew Turner  * Science Department and William Jolitz of UUNET Technologies Inc.
8412042e2SAndrew Turner  *
9412042e2SAndrew Turner  * Redistribution and use in source and binary forms, with or without
10412042e2SAndrew Turner  * modification, are permitted provided that the following conditions
11412042e2SAndrew Turner  * are met:
12412042e2SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
13412042e2SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
14412042e2SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
15412042e2SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
16412042e2SAndrew Turner  *    documentation and/or other materials provided with the distribution.
17f72c920cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
18412042e2SAndrew Turner  *    may be used to endorse or promote products derived from this software
19412042e2SAndrew Turner  *    without specific prior written permission.
20412042e2SAndrew Turner  *
21412042e2SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22412042e2SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23412042e2SAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24412042e2SAndrew Turner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25412042e2SAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26412042e2SAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27412042e2SAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28412042e2SAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29412042e2SAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30412042e2SAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31412042e2SAndrew Turner  * SUCH DAMAGE.
32412042e2SAndrew Turner  */
33412042e2SAndrew Turner 
34d5d97bedSMike Karels #ifdef __arm__
35d5d97bedSMike Karels #include <arm/pmap.h>
36d5d97bedSMike Karels #else /* !__arm__ */
37d5d97bedSMike Karels 
38412042e2SAndrew Turner #ifndef _MACHINE_PMAP_H_
39412042e2SAndrew Turner #define	_MACHINE_PMAP_H_
40412042e2SAndrew Turner 
41412042e2SAndrew Turner #include <machine/pte.h>
42412042e2SAndrew Turner 
43412042e2SAndrew Turner #ifndef LOCORE
44412042e2SAndrew Turner 
45412042e2SAndrew Turner #include <sys/queue.h>
46412042e2SAndrew Turner #include <sys/_lock.h>
47412042e2SAndrew Turner #include <sys/_mutex.h>
484d90a5afSJohn Baldwin #include <sys/_pv_entry.h>
49412042e2SAndrew Turner 
503969d2f9SAndrew Turner #include <vm/_vm_radix.h>
513969d2f9SAndrew Turner 
52412042e2SAndrew Turner #ifdef _KERNEL
53412042e2SAndrew Turner 
54412042e2SAndrew Turner #define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
55412042e2SAndrew Turner 
56412042e2SAndrew Turner #endif
57412042e2SAndrew Turner 
58412042e2SAndrew Turner #define	pmap_page_get_memattr(m)	((m)->md.pv_memattr)
595cff1f4dSMark Johnston #define	pmap_page_is_write_mapped(m)	(((m)->a.flags & PGA_WRITEABLE) != 0)
60412042e2SAndrew Turner void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma);
61412042e2SAndrew Turner 
62412042e2SAndrew Turner /*
63412042e2SAndrew Turner  * Pmap stuff
64412042e2SAndrew Turner  */
65412042e2SAndrew Turner 
66412042e2SAndrew Turner struct md_page {
67412042e2SAndrew Turner 	TAILQ_HEAD(,pv_entry)	pv_list;
68412042e2SAndrew Turner 	int			pv_gen;
69412042e2SAndrew Turner 	vm_memattr_t		pv_memattr;
70412042e2SAndrew Turner };
71412042e2SAndrew Turner 
72412042e2SAndrew Turner /*
73412042e2SAndrew Turner  * This structure is used to hold a virtual<->physical address
74412042e2SAndrew Turner  * association and is used mostly by bootstrap code
75412042e2SAndrew Turner  */
76412042e2SAndrew Turner struct pv_addr {
77412042e2SAndrew Turner 	SLIST_ENTRY(pv_addr) pv_list;
78412042e2SAndrew Turner 	vm_offset_t	pv_va;
79412042e2SAndrew Turner 	vm_paddr_t	pv_pa;
80412042e2SAndrew Turner };
81412042e2SAndrew Turner 
8223982ffdSAndrew Turner enum pmap_stage {
8323982ffdSAndrew Turner 	PM_INVALID,
8423982ffdSAndrew Turner 	PM_STAGE1,
8523982ffdSAndrew Turner 	PM_STAGE2,
8623982ffdSAndrew Turner };
87412042e2SAndrew Turner 
88412042e2SAndrew Turner struct pmap {
89412042e2SAndrew Turner 	struct mtx		pm_mtx;
9050e3ab6bSAlan Cox 	struct pmap_statistics	pm_stats;	/* pmap statistics */
919815c092SAndrew Turner 	uint64_t		pm_ttbr;
9250e3ab6bSAlan Cox 	vm_paddr_t		pm_l0_paddr;
93f2f21fafSAndrew Turner 	pd_entry_t		*pm_l0;
94412042e2SAndrew Turner 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
953969d2f9SAndrew Turner 	struct vm_radix		pm_root;	/* spare page table pages */
9650e3ab6bSAlan Cox 	long			pm_cookie;	/* encodes the pmap's ASID */
97249ecb63SAndrew Turner 	struct asid_set		*pm_asid_set;	/* The ASID/VMID set to use */
9823982ffdSAndrew Turner 	enum pmap_stage		pm_stage;
999815c092SAndrew Turner 	int			pm_levels;
1006a9c2e63SAndrew Turner 	uint64_t		pm_reserved[4];
101412042e2SAndrew Turner };
1021f15b0e6SMark Johnston typedef struct pmap *pmap_t;
103412042e2SAndrew Turner 
10445a91d45SAndrew Turner struct thread;
10545a91d45SAndrew Turner 
106412042e2SAndrew Turner #ifdef _KERNEL
107412042e2SAndrew Turner extern struct pmap	kernel_pmap_store;
108412042e2SAndrew Turner #define	kernel_pmap	(&kernel_pmap_store)
109412042e2SAndrew Turner #define	pmap_kernel()	kernel_pmap
110412042e2SAndrew Turner 
111412042e2SAndrew Turner #define	PMAP_ASSERT_LOCKED(pmap) \
112412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
113412042e2SAndrew Turner #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
114412042e2SAndrew Turner #define	PMAP_LOCK_ASSERT(pmap, type) \
115412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, (type))
116412042e2SAndrew Turner #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
117412042e2SAndrew Turner #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
118412042e2SAndrew Turner 				    NULL, MTX_DEF | MTX_DUPOK)
119412042e2SAndrew Turner #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
120412042e2SAndrew Turner #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
121412042e2SAndrew Turner #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
122412042e2SAndrew Turner #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
123412042e2SAndrew Turner 
12450e3ab6bSAlan Cox #define	ASID_RESERVED_FOR_PID_0	0
12550e3ab6bSAlan Cox #define	ASID_RESERVED_FOR_EFI	1
12650e3ab6bSAlan Cox #define	ASID_FIRST_AVAILABLE	(ASID_RESERVED_FOR_EFI + 1)
12750e3ab6bSAlan Cox #define	ASID_TO_OPERAND(asid)	({					\
12850e3ab6bSAlan Cox 	KASSERT((asid) != -1, ("invalid ASID"));			\
129459e0b92SAndrew Turner 	(uint64_t)(asid) << TTBR_ASID_SHIFT;			\
13050e3ab6bSAlan Cox })
13150e3ab6bSAlan Cox 
1328882b785SKonstantin Belousov #define	PMAP_WANT_ACTIVE_CPUS_NAIVE
1338882b785SKonstantin Belousov 
134412042e2SAndrew Turner extern vm_offset_t virtual_avail;
135412042e2SAndrew Turner extern vm_offset_t virtual_end;
136412042e2SAndrew Turner 
137412042e2SAndrew Turner /*
138412042e2SAndrew Turner  * Macros to test if a mapping is mappable with an L1 Section mapping
139412042e2SAndrew Turner  * or an L2 Large Page mapping.
140412042e2SAndrew Turner  */
141412042e2SAndrew Turner #define	L1_MAPPABLE_P(va, pa, size)					\
142412042e2SAndrew Turner 	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE)
143412042e2SAndrew Turner 
144041b7317SKonstantin Belousov #define	pmap_vm_page_alloc_check(m)
145041b7317SKonstantin Belousov 
1462cb0e95fSAndrew Turner void	pmap_activate_vm(pmap_t);
147*ba313626SAndrew Turner void	pmap_bootstrap(vm_size_t);
1480760b4c2SEd Maste int	pmap_change_attr(vm_offset_t va, vm_size_t size, int mode);
149a85ce4adSAndrew Turner int	pmap_change_prot(vm_offset_t va, vm_size_t size, vm_prot_t prot);
15078442297SEmmanuel Vadot void	pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode);
151e5acd89cSAndrew Turner void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t);
152f64329bcSAndrew Turner bool	pmap_klookup(vm_offset_t va, vm_paddr_t *pa);
153412042e2SAndrew Turner vm_paddr_t pmap_kextract(vm_offset_t va);
154412042e2SAndrew Turner void	pmap_kremove(vm_offset_t);
155e5acd89cSAndrew Turner void	pmap_kremove_device(vm_offset_t, vm_size_t);
156ea8f128cSJohn Baldwin void	*pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma);
157d4586dd3SMark Johnston bool	pmap_page_is_mapped(vm_page_t m);
1589815c092SAndrew Turner int	pmap_pinit_stage(pmap_t, enum pmap_stage, int);
159398a929fSMark Johnston bool	pmap_ps_enabled(pmap_t pmap);
16050e3ab6bSAlan Cox uint64_t pmap_to_ttbr0(pmap_t pmap);
161e3917bb2SAndrew Turner void	pmap_disable_promotion(vm_offset_t sva, vm_size_t size);
162020edaeaSAndrew Turner void	pmap_map_delete(pmap_t, vm_offset_t, vm_offset_t);
163412042e2SAndrew Turner 
164ea8f128cSJohn Baldwin void	*pmap_mapdev(vm_paddr_t, vm_size_t);
16585ffc586SAndrew Turner void	*pmap_mapbios(vm_paddr_t, vm_size_t);
1667ae99f80SJohn Baldwin void	pmap_unmapdev(void *, vm_size_t);
1677ae99f80SJohn Baldwin void	pmap_unmapbios(void *, vm_size_t);
168412042e2SAndrew Turner 
1694961faaaSJohn Baldwin bool	pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, bool);
1704961faaaSJohn Baldwin void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, bool);
171412042e2SAndrew Turner 
1729a0de24bSAndrew Turner bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **,
173f2f21fafSAndrew Turner     pd_entry_t **, pt_entry_t **);
1749a0de24bSAndrew Turner 
175510a3f1bSAndrew Turner int	pmap_fault(pmap_t, uint64_t, uint64_t);
176510a3f1bSAndrew Turner 
17785b7c566SAndrew Turner struct pcb *pmap_switch(struct thread *);
17845a91d45SAndrew Turner 
1792cb0e95fSAndrew Turner extern void (*pmap_clean_stage2_tlbi)(void);
1802cb0e95fSAndrew Turner extern void (*pmap_invalidate_vpipt_icache)(void);
1816419b48fSAndrew Turner extern void (*pmap_stage2_invalidate_range)(uint64_t, vm_offset_t, vm_offset_t,
1826419b48fSAndrew Turner     bool);
1836419b48fSAndrew Turner extern void (*pmap_stage2_invalidate_all)(uint64_t);
1842cb0e95fSAndrew Turner 
185e7a9df16SKonstantin Belousov static inline int
186e7a9df16SKonstantin Belousov pmap_vmspace_copy(pmap_t dst_pmap __unused, pmap_t src_pmap __unused)
187e7a9df16SKonstantin Belousov {
188e7a9df16SKonstantin Belousov 
189e7a9df16SKonstantin Belousov 	return (0);
190e7a9df16SKonstantin Belousov }
191e7a9df16SKonstantin Belousov 
19289c52f9dSKyle Evans #if defined(KASAN) || defined(KMSAN)
19389c52f9dSKyle Evans struct arm64_bootparams;
19489c52f9dSKyle Evans 
1955fae5358SAndrew Turner void	pmap_bootstrap_san(void);
19689c52f9dSKyle Evans void	pmap_san_enter(vm_offset_t);
19789c52f9dSKyle Evans void	pmap_san_bootstrap(struct arm64_bootparams *);
19889c52f9dSKyle Evans #endif
19989c52f9dSKyle Evans 
200412042e2SAndrew Turner #endif	/* _KERNEL */
201412042e2SAndrew Turner 
202412042e2SAndrew Turner #endif	/* !LOCORE */
203412042e2SAndrew Turner 
204412042e2SAndrew Turner #endif	/* !_MACHINE_PMAP_H_ */
205d5d97bedSMike Karels 
206d5d97bedSMike Karels #endif /* !__arm__ */
207