xref: /freebsd/sys/arm64/include/pmap.h (revision 9815c092decefb1e8dfba8b6ecf932298835a4a8)
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  * $FreeBSD$
34412042e2SAndrew Turner  */
35412042e2SAndrew Turner 
36412042e2SAndrew Turner #ifndef _MACHINE_PMAP_H_
37412042e2SAndrew Turner #define	_MACHINE_PMAP_H_
38412042e2SAndrew Turner 
39412042e2SAndrew Turner #include <machine/pte.h>
40412042e2SAndrew Turner 
41412042e2SAndrew Turner #ifndef LOCORE
42412042e2SAndrew Turner 
43412042e2SAndrew Turner #include <sys/queue.h>
44412042e2SAndrew Turner #include <sys/_lock.h>
45412042e2SAndrew Turner #include <sys/_mutex.h>
46412042e2SAndrew Turner 
473969d2f9SAndrew Turner #include <vm/_vm_radix.h>
483969d2f9SAndrew Turner 
49412042e2SAndrew Turner #ifdef _KERNEL
50412042e2SAndrew Turner 
51412042e2SAndrew Turner #define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
52412042e2SAndrew Turner 
53412042e2SAndrew Turner #endif
54412042e2SAndrew Turner 
55412042e2SAndrew Turner #define	pmap_page_get_memattr(m)	((m)->md.pv_memattr)
565cff1f4dSMark Johnston #define	pmap_page_is_write_mapped(m)	(((m)->a.flags & PGA_WRITEABLE) != 0)
57412042e2SAndrew Turner void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma);
58412042e2SAndrew Turner 
59412042e2SAndrew Turner /*
60412042e2SAndrew Turner  * Pmap stuff
61412042e2SAndrew Turner  */
62412042e2SAndrew Turner 
63412042e2SAndrew Turner struct md_page {
64412042e2SAndrew Turner 	TAILQ_HEAD(,pv_entry)	pv_list;
65412042e2SAndrew Turner 	int			pv_gen;
66412042e2SAndrew Turner 	vm_memattr_t		pv_memattr;
67412042e2SAndrew Turner };
68412042e2SAndrew Turner 
69412042e2SAndrew Turner /*
70412042e2SAndrew Turner  * This structure is used to hold a virtual<->physical address
71412042e2SAndrew Turner  * association and is used mostly by bootstrap code
72412042e2SAndrew Turner  */
73412042e2SAndrew Turner struct pv_addr {
74412042e2SAndrew Turner 	SLIST_ENTRY(pv_addr) pv_list;
75412042e2SAndrew Turner 	vm_offset_t	pv_va;
76412042e2SAndrew Turner 	vm_paddr_t	pv_pa;
77412042e2SAndrew Turner };
78412042e2SAndrew Turner 
7923982ffdSAndrew Turner enum pmap_stage {
8023982ffdSAndrew Turner 	PM_INVALID,
8123982ffdSAndrew Turner 	PM_STAGE1,
8223982ffdSAndrew Turner 	PM_STAGE2,
8323982ffdSAndrew Turner };
84412042e2SAndrew Turner 
85412042e2SAndrew Turner struct pmap {
86412042e2SAndrew Turner 	struct mtx		pm_mtx;
8750e3ab6bSAlan Cox 	struct pmap_statistics	pm_stats;	/* pmap statistics */
88*9815c092SAndrew Turner 	uint64_t		pm_ttbr;
8950e3ab6bSAlan Cox 	vm_paddr_t		pm_l0_paddr;
90f2f21fafSAndrew Turner 	pd_entry_t		*pm_l0;
91412042e2SAndrew Turner 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
923969d2f9SAndrew Turner 	struct vm_radix		pm_root;	/* spare page table pages */
9350e3ab6bSAlan Cox 	long			pm_cookie;	/* encodes the pmap's ASID */
94249ecb63SAndrew Turner 	struct asid_set		*pm_asid_set;	/* The ASID/VMID set to use */
9523982ffdSAndrew Turner 	enum pmap_stage		pm_stage;
96*9815c092SAndrew Turner 	int			pm_levels;
97412042e2SAndrew Turner };
981f15b0e6SMark Johnston typedef struct pmap *pmap_t;
99412042e2SAndrew Turner 
100412042e2SAndrew Turner typedef struct pv_entry {
101412042e2SAndrew Turner 	vm_offset_t		pv_va;	/* virtual address for mapping */
102412042e2SAndrew Turner 	TAILQ_ENTRY(pv_entry)	pv_next;
103412042e2SAndrew Turner } *pv_entry_t;
104412042e2SAndrew Turner 
105412042e2SAndrew Turner /*
106412042e2SAndrew Turner  * pv_entries are allocated in chunks per-process.  This avoids the
107412042e2SAndrew Turner  * need to track per-pmap assignments.
108412042e2SAndrew Turner  */
109412042e2SAndrew Turner #define	_NPCM	3
110412042e2SAndrew Turner #define	_NPCPV	168
1111f15b0e6SMark Johnston #define	PV_CHUNK_HEADER							\
1121f15b0e6SMark Johnston 	pmap_t			pc_pmap;				\
1131f15b0e6SMark Johnston 	TAILQ_ENTRY(pv_chunk)	pc_list;				\
1141f15b0e6SMark Johnston 	uint64_t		pc_map[_NPCM];	/* bitmap; 1 = free */	\
115412042e2SAndrew Turner 	TAILQ_ENTRY(pv_chunk)	pc_lru;
1161f15b0e6SMark Johnston 
1171f15b0e6SMark Johnston struct pv_chunk_header {
1181f15b0e6SMark Johnston 	PV_CHUNK_HEADER
119412042e2SAndrew Turner };
120412042e2SAndrew Turner 
1211f15b0e6SMark Johnston struct pv_chunk {
1221f15b0e6SMark Johnston 	PV_CHUNK_HEADER
1231f15b0e6SMark Johnston 	struct pv_entry		pc_pventry[_NPCPV];
1241f15b0e6SMark Johnston };
125412042e2SAndrew Turner 
12645a91d45SAndrew Turner struct thread;
12745a91d45SAndrew Turner 
128412042e2SAndrew Turner #ifdef _KERNEL
129412042e2SAndrew Turner extern struct pmap	kernel_pmap_store;
130412042e2SAndrew Turner #define	kernel_pmap	(&kernel_pmap_store)
131412042e2SAndrew Turner #define	pmap_kernel()	kernel_pmap
132412042e2SAndrew Turner 
133412042e2SAndrew Turner #define	PMAP_ASSERT_LOCKED(pmap) \
134412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
135412042e2SAndrew Turner #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
136412042e2SAndrew Turner #define	PMAP_LOCK_ASSERT(pmap, type) \
137412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, (type))
138412042e2SAndrew Turner #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
139412042e2SAndrew Turner #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
140412042e2SAndrew Turner 				    NULL, MTX_DEF | MTX_DUPOK)
141412042e2SAndrew Turner #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
142412042e2SAndrew Turner #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
143412042e2SAndrew Turner #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
144412042e2SAndrew Turner #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
145412042e2SAndrew Turner 
14650e3ab6bSAlan Cox #define	ASID_RESERVED_FOR_PID_0	0
14750e3ab6bSAlan Cox #define	ASID_RESERVED_FOR_EFI	1
14850e3ab6bSAlan Cox #define	ASID_FIRST_AVAILABLE	(ASID_RESERVED_FOR_EFI + 1)
14950e3ab6bSAlan Cox #define	ASID_TO_OPERAND_SHIFT	48
15050e3ab6bSAlan Cox #define	ASID_TO_OPERAND(asid)	({					\
15150e3ab6bSAlan Cox 	KASSERT((asid) != -1, ("invalid ASID"));			\
15250e3ab6bSAlan Cox 	(uint64_t)(asid) << ASID_TO_OPERAND_SHIFT;			\
15350e3ab6bSAlan Cox })
15450e3ab6bSAlan Cox 
155412042e2SAndrew Turner extern vm_offset_t virtual_avail;
156412042e2SAndrew Turner extern vm_offset_t virtual_end;
157412042e2SAndrew Turner 
158412042e2SAndrew Turner /*
159412042e2SAndrew Turner  * Macros to test if a mapping is mappable with an L1 Section mapping
160412042e2SAndrew Turner  * or an L2 Large Page mapping.
161412042e2SAndrew Turner  */
162412042e2SAndrew Turner #define	L1_MAPPABLE_P(va, pa, size)					\
163412042e2SAndrew Turner 	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE)
164412042e2SAndrew Turner 
1652cb0e95fSAndrew Turner void	pmap_activate_vm(pmap_t);
166f2f21fafSAndrew Turner void	pmap_bootstrap(vm_offset_t, vm_offset_t, vm_paddr_t, vm_size_t);
1670760b4c2SEd Maste int	pmap_change_attr(vm_offset_t va, vm_size_t size, int mode);
16878442297SEmmanuel Vadot void	pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode);
169e5acd89cSAndrew Turner void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t);
170412042e2SAndrew Turner vm_paddr_t pmap_kextract(vm_offset_t va);
171412042e2SAndrew Turner void	pmap_kremove(vm_offset_t);
172e5acd89cSAndrew Turner void	pmap_kremove_device(vm_offset_t, vm_size_t);
17378442297SEmmanuel Vadot void	*pmap_mapdev_attr(vm_offset_t pa, vm_size_t size, vm_memattr_t ma);
174d4586dd3SMark Johnston bool	pmap_page_is_mapped(vm_page_t m);
175*9815c092SAndrew Turner int	pmap_pinit_stage(pmap_t, enum pmap_stage, int);
176398a929fSMark Johnston bool	pmap_ps_enabled(pmap_t pmap);
17750e3ab6bSAlan Cox uint64_t pmap_to_ttbr0(pmap_t pmap);
178412042e2SAndrew Turner 
179412042e2SAndrew Turner void	*pmap_mapdev(vm_offset_t, vm_size_t);
18085ffc586SAndrew Turner void	*pmap_mapbios(vm_paddr_t, vm_size_t);
181412042e2SAndrew Turner void	pmap_unmapdev(vm_offset_t, vm_size_t);
18285ffc586SAndrew Turner void	pmap_unmapbios(vm_offset_t, vm_size_t);
183412042e2SAndrew Turner 
184412042e2SAndrew Turner boolean_t pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
185412042e2SAndrew Turner void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
186412042e2SAndrew Turner 
1879a0de24bSAndrew Turner bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **,
188f2f21fafSAndrew Turner     pd_entry_t **, pt_entry_t **);
1899a0de24bSAndrew Turner 
190510a3f1bSAndrew Turner int	pmap_fault(pmap_t, uint64_t, uint64_t);
191510a3f1bSAndrew Turner 
192268f7e25SRuslan Bukin /* System MMU (SMMU). */
193268f7e25SRuslan Bukin int pmap_senter(pmap_t pmap, vm_offset_t va, vm_paddr_t pa, vm_prot_t prot,
194268f7e25SRuslan Bukin     u_int flags);
195268f7e25SRuslan Bukin int pmap_sremove(pmap_t pmap, vm_offset_t va);
196268f7e25SRuslan Bukin void pmap_sremove_pages(pmap_t pmap);
197268f7e25SRuslan Bukin 
19845a91d45SAndrew Turner struct pcb *pmap_switch(struct thread *, struct thread *);
19945a91d45SAndrew Turner 
2002cb0e95fSAndrew Turner extern void (*pmap_clean_stage2_tlbi)(void);
2012cb0e95fSAndrew Turner extern void (*pmap_invalidate_vpipt_icache)(void);
2022cb0e95fSAndrew Turner 
203e7a9df16SKonstantin Belousov static inline int
204e7a9df16SKonstantin Belousov pmap_vmspace_copy(pmap_t dst_pmap __unused, pmap_t src_pmap __unused)
205e7a9df16SKonstantin Belousov {
206e7a9df16SKonstantin Belousov 
207e7a9df16SKonstantin Belousov 	return (0);
208e7a9df16SKonstantin Belousov }
209e7a9df16SKonstantin Belousov 
210412042e2SAndrew Turner #endif	/* _KERNEL */
211412042e2SAndrew Turner 
212412042e2SAndrew Turner #endif	/* !LOCORE */
213412042e2SAndrew Turner 
214412042e2SAndrew Turner #endif	/* !_MACHINE_PMAP_H_ */
215