xref: /freebsd/sys/arm64/include/pmap.h (revision d4586dd328e2fd0df86626e65fcfa2414967b357)
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)
56412042e2SAndrew Turner #define	pmap_page_is_write_mapped(m)	(((m)->aflags & 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 
79412042e2SAndrew Turner 
80412042e2SAndrew Turner struct pmap {
81412042e2SAndrew Turner 	struct mtx		pm_mtx;
82412042e2SAndrew Turner 	struct pmap_statistics	pm_stats;	/* pmap statictics */
83f2f21fafSAndrew Turner 	pd_entry_t		*pm_l0;
84412042e2SAndrew Turner 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
853969d2f9SAndrew Turner 	struct vm_radix		pm_root;	/* spare page table pages */
86412042e2SAndrew Turner };
871f15b0e6SMark Johnston typedef struct pmap *pmap_t;
88412042e2SAndrew Turner 
89412042e2SAndrew Turner typedef struct pv_entry {
90412042e2SAndrew Turner 	vm_offset_t		pv_va;	/* virtual address for mapping */
91412042e2SAndrew Turner 	TAILQ_ENTRY(pv_entry)	pv_next;
92412042e2SAndrew Turner } *pv_entry_t;
93412042e2SAndrew Turner 
94412042e2SAndrew Turner /*
95412042e2SAndrew Turner  * pv_entries are allocated in chunks per-process.  This avoids the
96412042e2SAndrew Turner  * need to track per-pmap assignments.
97412042e2SAndrew Turner  */
98412042e2SAndrew Turner #define	_NPCM	3
99412042e2SAndrew Turner #define	_NPCPV	168
1001f15b0e6SMark Johnston #define	PV_CHUNK_HEADER							\
1011f15b0e6SMark Johnston 	pmap_t			pc_pmap;				\
1021f15b0e6SMark Johnston 	TAILQ_ENTRY(pv_chunk)	pc_list;				\
1031f15b0e6SMark Johnston 	uint64_t		pc_map[_NPCM];	/* bitmap; 1 = free */	\
104412042e2SAndrew Turner 	TAILQ_ENTRY(pv_chunk)	pc_lru;
1051f15b0e6SMark Johnston 
1061f15b0e6SMark Johnston struct pv_chunk_header {
1071f15b0e6SMark Johnston 	PV_CHUNK_HEADER
108412042e2SAndrew Turner };
109412042e2SAndrew Turner 
1101f15b0e6SMark Johnston struct pv_chunk {
1111f15b0e6SMark Johnston 	PV_CHUNK_HEADER
1121f15b0e6SMark Johnston 	struct pv_entry		pc_pventry[_NPCPV];
1131f15b0e6SMark Johnston };
114412042e2SAndrew Turner 
11545a91d45SAndrew Turner struct thread;
11645a91d45SAndrew Turner 
117412042e2SAndrew Turner #ifdef _KERNEL
118412042e2SAndrew Turner extern struct pmap	kernel_pmap_store;
119412042e2SAndrew Turner #define	kernel_pmap	(&kernel_pmap_store)
120412042e2SAndrew Turner #define	pmap_kernel()	kernel_pmap
121412042e2SAndrew Turner 
122412042e2SAndrew Turner #define	PMAP_ASSERT_LOCKED(pmap) \
123412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
124412042e2SAndrew Turner #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
125412042e2SAndrew Turner #define	PMAP_LOCK_ASSERT(pmap, type) \
126412042e2SAndrew Turner 				mtx_assert(&(pmap)->pm_mtx, (type))
127412042e2SAndrew Turner #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
128412042e2SAndrew Turner #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
129412042e2SAndrew Turner 				    NULL, MTX_DEF | MTX_DUPOK)
130412042e2SAndrew Turner #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
131412042e2SAndrew Turner #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
132412042e2SAndrew Turner #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
133412042e2SAndrew Turner #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
134412042e2SAndrew Turner 
135412042e2SAndrew Turner extern vm_offset_t virtual_avail;
136412042e2SAndrew Turner extern vm_offset_t virtual_end;
137412042e2SAndrew Turner 
138412042e2SAndrew Turner /*
139412042e2SAndrew Turner  * Macros to test if a mapping is mappable with an L1 Section mapping
140412042e2SAndrew Turner  * or an L2 Large Page mapping.
141412042e2SAndrew Turner  */
142412042e2SAndrew Turner #define	L1_MAPPABLE_P(va, pa, size)					\
143412042e2SAndrew Turner 	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE)
144412042e2SAndrew Turner 
145f2f21fafSAndrew Turner void	pmap_bootstrap(vm_offset_t, vm_offset_t, vm_paddr_t, vm_size_t);
1460760b4c2SEd Maste int	pmap_change_attr(vm_offset_t va, vm_size_t size, int mode);
14778442297SEmmanuel Vadot void	pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode);
148e5acd89cSAndrew Turner void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t);
149412042e2SAndrew Turner vm_paddr_t pmap_kextract(vm_offset_t va);
150412042e2SAndrew Turner void	pmap_kremove(vm_offset_t);
151e5acd89cSAndrew Turner void	pmap_kremove_device(vm_offset_t, vm_size_t);
15278442297SEmmanuel Vadot void	*pmap_mapdev_attr(vm_offset_t pa, vm_size_t size, vm_memattr_t ma);
153*d4586dd3SMark Johnston bool	pmap_page_is_mapped(vm_page_t m);
154398a929fSMark Johnston bool	pmap_ps_enabled(pmap_t pmap);
155412042e2SAndrew Turner 
156412042e2SAndrew Turner void	*pmap_mapdev(vm_offset_t, vm_size_t);
15785ffc586SAndrew Turner void	*pmap_mapbios(vm_paddr_t, vm_size_t);
158412042e2SAndrew Turner void	pmap_unmapdev(vm_offset_t, vm_size_t);
15985ffc586SAndrew Turner void	pmap_unmapbios(vm_offset_t, vm_size_t);
160412042e2SAndrew Turner 
161412042e2SAndrew Turner boolean_t pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
162412042e2SAndrew Turner void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
163412042e2SAndrew Turner 
1649a0de24bSAndrew Turner bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **,
165f2f21fafSAndrew Turner     pd_entry_t **, pt_entry_t **);
1669a0de24bSAndrew Turner 
167510a3f1bSAndrew Turner int	pmap_fault(pmap_t, uint64_t, uint64_t);
168510a3f1bSAndrew Turner 
16945a91d45SAndrew Turner struct pcb *pmap_switch(struct thread *, struct thread *);
17045a91d45SAndrew Turner 
171e7a9df16SKonstantin Belousov static inline int
172e7a9df16SKonstantin Belousov pmap_vmspace_copy(pmap_t dst_pmap __unused, pmap_t src_pmap __unused)
173e7a9df16SKonstantin Belousov {
174e7a9df16SKonstantin Belousov 
175e7a9df16SKonstantin Belousov 	return (0);
176e7a9df16SKonstantin Belousov }
177e7a9df16SKonstantin Belousov 
178412042e2SAndrew Turner #endif	/* _KERNEL */
179412042e2SAndrew Turner 
180412042e2SAndrew Turner #endif	/* !LOCORE */
181412042e2SAndrew Turner 
182412042e2SAndrew Turner #endif	/* !_MACHINE_PMAP_H_ */
183